Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit 45ca928

Browse files
drivers: add dma based bitbanging driver
1 parent 673d74d commit 45ca928

File tree

5 files changed

+297
-1
lines changed

5 files changed

+297
-1
lines changed

src/drivers/boards/px4fmu-v2/board_config.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ __BEGIN_DECLS
236236
#define PWMIN_TIMER_CHANNEL 2
237237
#define GPIO_PWM_IN GPIO_TIM4_CH2IN_2
238238

239+
/* Extra Purpose DMA */
240+
//Note: Only DMA2 has access to AHB bus meaning GPIO registers are only modifiable by DMA2,
241+
//refer DMA section of stm32F4 series User manual for further details.
242+
#define DMAMAP_BITBANG STM32_DMA_MAP(DMA2,DMA_STREAM1,DMA_CHAN6)
243+
#define TIMER_BITBANG 1
244+
// Refer manual for DMA Request map to select proper timer channel
245+
#define TIMER_BITBANG_CH 1
246+
#define BITBANG_GPIO_REG_BASE STM32_GPIOE_BASE
239247
/****************************************************************************************************
240248
* Public Types
241249
****************************************************************************************************/

src/drivers/drv_dma_bitbang.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/****************************************************************************
2+
*
3+
* Copyright (C) 2012 PX4 Development Team. All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in
13+
* the documentation and/or other materials provided with the
14+
* distribution.
15+
* 3. Neither the name PX4 nor the names of its contributors may be
16+
* used to endorse or promote products derived from this software
17+
* without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
* POSSIBILITY OF SUCH DAMAGE.
31+
*
32+
****************************************************************************/
33+
34+
/**
35+
* @file px4fmu_dma_bb.c
36+
*
37+
* DMA based Bit Bang Support
38+
* Author: Siddharth Bharat Purohit
39+
*
40+
*/
41+
42+
#pragma once
43+
#include <px4_config.h>
44+
#include <nuttx/arch.h>
45+
#include <nuttx/irq.h>
46+
47+
#include <sys/types.h>
48+
#include <stdbool.h>
49+
#include <inttypes.h>
50+
51+
#include <px4_time.h>
52+
#include <queue.h>
53+
54+
__BEGIN_DECLS
55+
56+
/* Initialise DMA based Bitbanging scheme*/
57+
__EXPORT extern void dma_bb_init(uint32_t mult, uint32_t offset);
58+
59+
/* Send buffer containing pin states to be sent immediately */
60+
__EXPORT extern void dma_bb_send_buff(uint32_t* dat, uint32_t len, dma_callback_t callback, void* arg);
61+
62+
__END_DECLS

src/drivers/stm32/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ px4_add_module(
3939
drv_io_timer.c
4040
drv_pwm_servo.c
4141
drv_input_capture.c
42+
drv_dma_bitbang.c
4243
DEPENDS
4344
platforms__common
4445
)
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
/****************************************************************************
2+
*
3+
* Copyright (C) 2012 PX4 Development Team. All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in
13+
* the documentation and/or other materials provided with the
14+
* distribution.
15+
* 3. Neither the name PX4 nor the names of its contributors may be
16+
* used to endorse or promote products derived from this software
17+
* without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
* POSSIBILITY OF SUCH DAMAGE.
31+
*
32+
****************************************************************************/
33+
34+
/**
35+
* @file px4fmu_dma_bb.c
36+
*
37+
* DMA based Bit Bang driver
38+
* Author: Siddharth Bharat Purohit
39+
*
40+
*/
41+
42+
/************************************************************************************
43+
* Included Files
44+
************************************************************************************/
45+
46+
#include <stdbool.h>
47+
#include <stdio.h>
48+
#include <assert.h>
49+
#include <debug.h>
50+
#include <time.h>
51+
#include <queue.h>
52+
#include <errno.h>
53+
#include <string.h>
54+
#include <math.h>
55+
56+
#include <drivers/drv_dma_bitbang.h>
57+
58+
#include "chip.h"
59+
#include "up_internal.h"
60+
#include "up_arch.h"
61+
62+
#include "stm32.h"
63+
#include "stm32_gpio.h"
64+
#include "stm32_tim.h"
65+
66+
#ifdef DMAMAP_BITBANG
67+
68+
#if TIMER_BITBANG == 1
69+
# define DMA_TIMER_BASE STM32_TIM1_BASE
70+
# define DMA_TIMER_POWER_REG STM32_RCC_APB1ENR
71+
# define DMA_TIMER_POWER_BIT RCC_APB2ENR_TIM1EN
72+
# define DMA_TIMER_VECTOR STM32_IRQ_TIM1CC
73+
# define DMA_TIMER_CLOCK STM32_APB2_TIM1_CLKIN
74+
#elif TIMER_BITBANG == 8
75+
# define DMA_TIMER_BASE STM32_TIM8_BASE
76+
# define DMA_TIMER_POWER_REG STM32_RCC_APB2ENR
77+
# define DMA_TIMER_POWER_BIT RCC_APB2ENR_TIM8EN
78+
# define DMA_TIMER_VECTOR STM32_IRQ_TIM8CC
79+
# define DMA_TIMER_CLOCK STM32_APB2_TIM8_CLKIN
80+
#else
81+
# error must select Timer 1 or Timer 8 DMA2 doesnot support any other
82+
#endif
83+
84+
/*
85+
* Timer register accessors
86+
*/
87+
#define REG(_reg) (*(volatile uint32_t *)(DMA_TIMER_BASE + _reg))
88+
89+
#define rCR1 REG(STM32_GTIM_CR1_OFFSET)
90+
#define rCR2 REG(STM32_GTIM_CR2_OFFSET)
91+
#define rSMCR REG(STM32_GTIM_SMCR_OFFSET)
92+
#define rDIER REG(STM32_GTIM_DIER_OFFSET)
93+
#define rSR REG(STM32_GTIM_SR_OFFSET)
94+
#define rEGR REG(STM32_GTIM_EGR_OFFSET)
95+
#define rCCMR1 REG(STM32_GTIM_CCMR1_OFFSET)
96+
#define rCCMR2 REG(STM32_GTIM_CCMR2_OFFSET)
97+
#define rCCER REG(STM32_GTIM_CCER_OFFSET)
98+
#define rCNT REG(STM32_GTIM_CNT_OFFSET)
99+
#define rPSC REG(STM32_GTIM_PSC_OFFSET)
100+
#define rARR REG(STM32_GTIM_ARR_OFFSET)
101+
#define rCCR1 REG(STM32_GTIM_CCR1_OFFSET)
102+
#define rCCR2 REG(STM32_GTIM_CCR2_OFFSET)
103+
#define rCCR3 REG(STM32_GTIM_CCR3_OFFSET)
104+
#define rCCR4 REG(STM32_GTIM_CCR4_OFFSET)
105+
#define rDCR REG(STM32_GTIM_DCR_OFFSET)
106+
#define rDMAR REG(STM32_GTIM_DMAR_OFFSET)
107+
108+
109+
#if TIMER_BITBANG_CH == 1
110+
# define rCCR_DMA rCCR1 /* compare register */
111+
# define DIER_DMA_INT GTIM_DIER_CC1IE /* interrupt enable */
112+
# define DIER_DMA_REQ GTIM_DIER_CC1DE /* DMA request enable */
113+
# define SR_INT_DMA GTIM_SR_CC1IF /* interrupt status */
114+
#elif TIMER_BITBANG_CH == 2
115+
# define rCCR_DMA rCCR2 /* compare register */
116+
# define DIER_DMA_INT GTIM_DIER_CC2IE /* interrupt enable */
117+
# define DIER_DMA_REQ GTIM_DIER_CC2DE /* DMA request enable */
118+
# define SR_INT_DMA GTIM_SR_CC2IF /* interrupt status */
119+
#elif TIMER_BITBANG_CH == 3
120+
# define rCCR_DMA rCCR3 /* compare register */
121+
# define DIER_DMA_INT GTIM_DIER_CC3IE /* interrupt enable */
122+
# define DIER_DMA_REQ GTIM_DIER_CC3DE /* DMA request enable */
123+
# define SR_INT_DMA GTIM_SR_CC3IF /* interrupt status */
124+
#elif TIMER_BITBANG_CH == 4
125+
# define rCCR_DMA rCCR4 /* compare register */
126+
# define DIER_DMA_INT GTIM_DIER_CC4IE /* interrupt enable */
127+
# define DIER_DMA_REQ GTIM_DIER_CC4DE /* DMA request enable */
128+
# define SR_INT_DMA GTIM_SR_CC4IF /* interrupt status */
129+
#else
130+
# error TIMER_BITBANG_CH must be a value between 1 and 4
131+
#endif
132+
133+
static DMA_HANDLE tx_dma;
134+
135+
static void dma_reset(void);
136+
static bool init = false;
137+
138+
/* GPIO register accessors */
139+
#define GPIO_REG(_x) (*(volatile uint32_t *)(BITBANG_GPIO_REG_BASE + _x))
140+
#define rBSSR GPIO_REG(STM32_GPIO_BSRR_OFFSET)
141+
142+
// The rate of timer will be set per following equation
143+
// Timer_Rate = {(16.8*mult + offset)/168} Mhz
144+
// The fastest clock which can be generated would be:
145+
// Clk_fast = Timer_Rate/4 Hz
146+
void dma_bb_init(uint32_t mult, uint32_t offset)
147+
{
148+
/* clock/power on our timer */
149+
modifyreg32(DMA_TIMER_POWER_REG, 0, DMA_TIMER_POWER_BIT);
150+
151+
tx_dma = stm32_dmachannel(DMAMAP_BITBANG);
152+
153+
/* configure the timer according to the requested factors*/
154+
rPSC = roundf(16.8f * mult + offset) - 1;
155+
156+
/* run to only one increment*/
157+
rARR = 1;
158+
159+
rCCR_DMA = 1;
160+
161+
rCR1 = 0;
162+
rCR2 = 0;
163+
164+
/* generate an update event; reloads the counter, all registers */
165+
rEGR = GTIM_EGR_UG;
166+
167+
/* enable the timer */
168+
rCR1 = GTIM_CR1_CEN;
169+
170+
init = true;
171+
printf("[init] Starting DMA bitbang driver \n");
172+
}
173+
174+
/*
175+
Sends Set or Reset data to GPIO Register popping data from top at every timer compare
176+
177+
-- "callback" is called after completion the set of requested pulse is transmitted through
178+
to GPIO bus,
179+
-- with "arg" being the argument to the callback
180+
-- "dat" contains the list of pin states over time, data is sent to GPIOx_BSSR register which
181+
has following behaviour:
182+
183+
-- Bits 31:16 BRx
184+
0: No action
185+
1: Reset the corresponding GPIOx O/P bit
186+
187+
-- Bits 15:0 BSx
188+
0: No action
189+
1: Set the corresponding GPIOx O/P bit
190+
191+
Note: If both BSx and BRx are set, BSx has priority.
192+
*/
193+
void dma_bb_send_buff(uint32_t* dat, uint32_t len, dma_callback_t callback, void* arg)
194+
{
195+
if(!init) {
196+
return;
197+
}
198+
/* unconfigure the timer request */
199+
rDIER &= ~DIER_DMA_REQ;
200+
dma_reset();
201+
202+
stm32_dmasetup(
203+
tx_dma,
204+
(uint32_t)&rBSSR,
205+
(uint32_t)dat,
206+
len,
207+
DMA_SCR_DIR_M2P |
208+
DMA_SCR_MINC |
209+
DMA_SCR_PSIZE_32BITS |
210+
DMA_SCR_MSIZE_32BITS);
211+
stm32_dmastart(tx_dma, callback, arg, false);
212+
213+
/* Enable Timer request */
214+
rDIER |= DIER_DMA_REQ;
215+
}
216+
217+
static void
218+
dma_reset(void)
219+
{
220+
/* kill any pending DMA */
221+
stm32_dmastop(tx_dma);
222+
}
223+
224+
#endif //#ifdef DMAMAP_BITBANG

src/drivers/stm32/module.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
SRCS = drv_hrt.c \
4141
drv_pwm_servo.c \
4242
drv_io_timer.c \
43-
drv_input_capture.c
43+
drv_input_capture.c \
44+
drv_dma_bitbang.c
4445

4546

4647
INCLUDE_DIRS += $(NUTTX_SRC)/arch/arm/src/stm32 $(NUTTX_SRC)/arch/arm/src/common

0 commit comments

Comments
 (0)