Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TIMER/DMA framework #3833

Merged
merged 14 commits into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ $(error Unknown target MCU specified.)
endif

GROUP_1_TARGETS := ALIENFLIGHTF3 ALIENFLIGHTF4 AIRHEROF3 AIRHEROF3_QUAD COLIBRI_RACE LUX_RACE SPARKY REVO SPARKY2 COLIBRI KISSFC FALCORE FF_F35_LIGHTNING FF_FORTINIF4 FF_PIKOF4 FF_PIKOF4OSD
GROUP_2_TARGETS := SPRACINGF3 SPRACINGF3EVO SPRACINGF3EVO_1SS SPRACINGF3MINI SPRACINGF3NEO SPRACINGF4EVO CLRACINGF4AIR CLRACINGF4AIRV2 BEEROTORF4 BETAFLIGHTF3 BETAFLIGHTF4 PIKOBLX
GROUP_2_TARGETS := SPRACINGF3 SPRACINGF3EVO SPRACINGF3EVO_1SS SPRACINGF3MINI SPRACINGF4EVO CLRACINGF4AIR CLRACINGF4AIRV2 BEEROTORF4 BETAFLIGHTF3 BETAFLIGHTF4 PIKOBLX
# SPRACINGF3NEO
GROUP_3_TARGETS := OMNIBUS AIRBOTF4 BLUEJAYF4 OMNIBUSF4 OMNIBUSF4PRO OMNIBUSF4V3 FIREWORKSV2 SPARKY2 MATEKF405 OMNIBUSF7 DYSF4PRO OMNIBUSF4PRO_LEDSTRIPM5 OMNIBUSF7NXT OMNIBUSF7V2 ASGARD32F4
GROUP_4_TARGETS := ANYFC ANYFCF7 ANYFCF7_EXTERNAL_BARO ANYFCM7 ALIENFLIGHTNGF7 PIXRACER YUPIF4 YUPIF4MINI YUPIF4R2 YUPIF7 MATEKF405SE MATEKF411 MATEKF722 MATEKF405OSD MATEKF405_SERVOS6
GROUP_OTHER_TARGETS := $(filter-out $(GROUP_1_TARGETS) $(GROUP_2_TARGETS) $(GROUP_3_TARGETS) $(GROUP_4_TARGETS), $(VALID_TARGETS))
Expand Down
2 changes: 1 addition & 1 deletion make/mcu/STM32F3.mk
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ MCU_COMMON_SRC = \
drivers/accgyro/accgyro.c \
drivers/adc_stm32f30x.c \
drivers/bus_i2c_stm32f30x.c \
drivers/dma.c \
drivers/dma_stm32f3xx.c \
drivers/serial_uart_stm32f30x.c \
drivers/system_stm32f30x.c \
drivers/timer_impl_stdperiph.c \
Expand Down
2 changes: 1 addition & 1 deletion src/main/common/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "fc/runtime_config.h"

#if !defined(DYNAMIC_HEAP_SIZE)
#define DYNAMIC_HEAP_SIZE (1024)
#define DYNAMIC_HEAP_SIZE (2048)
#endif

static uint32_t dynHeap[DYNAMIC_HEAP_SIZE / sizeof(uint32_t)];
Expand Down
9 changes: 9 additions & 0 deletions src/main/common/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,21 @@
#define CONCAT_HELPER(x,y) x ## y
#define CONCAT(x,y) CONCAT_HELPER(x, y)

#define CONCAT4_HELPER(x, y, z, w) x ## y ## z ## w
#define CONCAT4(x, y, z, w) CONCAT4_HELPER(x, y, z, w)

#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)

#define EXPAND_I(x) x
#define EXPAND(x) EXPAND_I(x)

// Expand all argumens and call macro with them. When expansion of some argument contains ',', it will be passed as multiple arguments
// #define TAKE3(_1,_2,_3) CONCAT3(_1,_2,_3)
// #define MULTI2 A,B
// PP_CALL(TAKE3, MULTI2, C) expands to ABC
#define PP_CALL(macro, ...) macro(__VA_ARGS__)

#if !defined(UNUSED)
#define UNUSED(x) (void)(x)
#endif
Expand Down
3 changes: 2 additions & 1 deletion src/main/drivers/bus_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "drivers/io_types.h"
#include "drivers/rcc_types.h"
#include "drivers/dma.h"

#if defined(STM32F4) || defined(STM32F3)
#define SPI_IO_AF_CFG IO_CONFIG(GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL)
Expand Down Expand Up @@ -74,7 +75,7 @@ typedef struct SPIDevice_s {
#if defined(STM32F7)
SPI_HandleTypeDef hspi;
DMA_HandleTypeDef hdma;
uint8_t dmaIrqHandler;
dmaTag_t dmaTag;
#endif
const uint16_t * divisorMap;
} spiDevice_t;
Expand Down
10 changes: 5 additions & 5 deletions src/main/drivers/bus_spi_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ static const uint16_t spiDivisorMapSlow[] = {
};

static spiDevice_t spiHardwareMap[] = {
{ .dev = SPI1, .nss = IO_TAG(SPI1_NSS_PIN), .sck = IO_TAG(SPI1_SCK_PIN), .miso = IO_TAG(SPI1_MISO_PIN), .mosi = IO_TAG(SPI1_MOSI_PIN), .rcc = RCC_APB2(SPI1), .af = GPIO_AF5_SPI1, .leadingEdge = false, .dmaIrqHandler = DMA2_ST3_HANDLER, .divisorMap = spiDivisorMapFast },
{ .dev = SPI2, .nss = IO_TAG(SPI2_NSS_PIN), .sck = IO_TAG(SPI2_SCK_PIN), .miso = IO_TAG(SPI2_MISO_PIN), .mosi = IO_TAG(SPI2_MOSI_PIN), .rcc = RCC_APB1(SPI2), .af = GPIO_AF5_SPI2, .leadingEdge = false, .dmaIrqHandler = DMA1_ST4_HANDLER, .divisorMap = spiDivisorMapSlow },
{ .dev = SPI3, .nss = IO_TAG(SPI3_NSS_PIN), .sck = IO_TAG(SPI3_SCK_PIN), .miso = IO_TAG(SPI3_MISO_PIN), .mosi = IO_TAG(SPI3_MOSI_PIN), .rcc = RCC_APB1(SPI3), .af = GPIO_AF6_SPI3, .leadingEdge = false, .dmaIrqHandler = DMA1_ST7_HANDLER, .divisorMap = spiDivisorMapSlow },
{ .dev = SPI4, .nss = IO_TAG(SPI4_NSS_PIN), .sck = IO_TAG(SPI4_SCK_PIN), .miso = IO_TAG(SPI4_MISO_PIN), .mosi = IO_TAG(SPI4_MOSI_PIN), .rcc = RCC_APB2(SPI4), .af = GPIO_AF5_SPI4, .leadingEdge = false, .dmaIrqHandler = DMA2_ST1_HANDLER, .divisorMap = spiDivisorMapSlow }
{ .dev = SPI1, .nss = IO_TAG(SPI1_NSS_PIN), .sck = IO_TAG(SPI1_SCK_PIN), .miso = IO_TAG(SPI1_MISO_PIN), .mosi = IO_TAG(SPI1_MOSI_PIN), .rcc = RCC_APB2(SPI1), .af = GPIO_AF5_SPI1, .leadingEdge = false, .dmaTag = DMA_TAG(2,3,0), .divisorMap = spiDivisorMapFast },
{ .dev = SPI2, .nss = IO_TAG(SPI2_NSS_PIN), .sck = IO_TAG(SPI2_SCK_PIN), .miso = IO_TAG(SPI2_MISO_PIN), .mosi = IO_TAG(SPI2_MOSI_PIN), .rcc = RCC_APB1(SPI2), .af = GPIO_AF5_SPI2, .leadingEdge = false, .dmaTag = DMA_TAG(1,4,0), .divisorMap = spiDivisorMapSlow },
{ .dev = SPI3, .nss = IO_TAG(SPI3_NSS_PIN), .sck = IO_TAG(SPI3_SCK_PIN), .miso = IO_TAG(SPI3_MISO_PIN), .mosi = IO_TAG(SPI3_MOSI_PIN), .rcc = RCC_APB1(SPI3), .af = GPIO_AF6_SPI3, .leadingEdge = false, .dmaTag = DMA_TAG(1,7,0), .divisorMap = spiDivisorMapSlow },
{ .dev = SPI4, .nss = IO_TAG(SPI4_NSS_PIN), .sck = IO_TAG(SPI4_SCK_PIN), .miso = IO_TAG(SPI4_MISO_PIN), .mosi = IO_TAG(SPI4_MOSI_PIN), .rcc = RCC_APB2(SPI4), .af = GPIO_AF5_SPI4, .leadingEdge = false, .dmaTag = DMA_TAG(2,1,0), .divisorMap = spiDivisorMapSlow }
};

SPIDevice spiDeviceByInstance(SPI_TypeDef *instance)
Expand Down Expand Up @@ -354,7 +354,7 @@ DMA_HandleTypeDef* spiSetDMATransmit(DMA_Stream_TypeDef *Stream, uint32_t Channe
__HAL_LINKDMA(&spiHardwareMap[device].hspi, hdmatx, spiHardwareMap[device].hdma);

// DMA TX Interrupt
dmaSetHandler(spiHardwareMap[device].dmaIrqHandler, dmaSPIIRQHandler, NVIC_BUILD_PRIORITY(3, 0), (uint32_t)device);
dmaSetHandler(dmaGetByTag(spiHardwareMap[device].dmaTag), dmaSPIIRQHandler, NVIC_BUILD_PRIORITY(3, 0), (uint32_t)device);

//HAL_CLEANCACHE(pData,Size);
// And Transmit
Expand Down
108 changes: 0 additions & 108 deletions src/main/drivers/dma.c

This file was deleted.

138 changes: 85 additions & 53 deletions src/main/drivers/dma.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,61 @@
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "resource.h"

struct dmaChannelDescriptor_s;
typedef void (*dmaCallbackHandlerFuncPtr)(struct dmaChannelDescriptor_s *channelDescriptor);

typedef uint16_t dmaTag_t; // Packed DMA adapter/channel/stream
typedef struct dmaChannelDescriptor_s * DMA_t;

#if defined(UNIT_TEST)
typedef uint32_t DMA_TypeDef;
#endif

#define DMA_TAG(dma, stream, channel) ( (((dma) & 0x03) << 7) | (((stream) & 0x07) << 4) | (((channel) & 0x0F) << 0) )
#define DMA_NONE (0)

#define DMATAG_GET_DMA(x) ( ((x) >> 7) & 0x03 )
#define DMATAG_GET_STREAM(x) ( ((x) >> 4) & 0x07 )
#define DMATAG_GET_CHANNEL(x) ( ((x) >> 0) & 0x0F )

typedef void (*dmaCallbackHandlerFuncPtr)(DMA_t channelDescriptor);

typedef struct dmaChannelDescriptor_s {
dmaTag_t tag;
DMA_TypeDef* dma;
#if defined(STM32F4) || defined(STM32F7)
#if defined(STM32F4)
DMA_Stream_TypeDef* ref;
#elif defined(STM32F7)
DMA_Stream_TypeDef* ref;
DMA_HandleTypeDef hdma;
#else
DMA_Channel_TypeDef* ref;
#endif
dmaCallbackHandlerFuncPtr irqHandlerCallback;
uint8_t flagsShift;
IRQn_Type irqN;
uint32_t flagsShift;
IRQn_Type irqNumber;
uint32_t rcc;
uint32_t userParam;
resourceOwner_e owner;
uint8_t resourceIndex;
} dmaChannelDescriptor_t;

#if defined(STM32F4) || defined(STM32F7)
#if defined(STM32F4)

#define DEFINE_DMA_CHANNEL(d, s, f) { \
.tag = DMA_TAG(d, s, 0), \
.dma = DMA##d, \
.ref = DMA##d##_Stream##s, \
.irqHandlerCallback = NULL, \
.flagsShift = f, \
.irqNumber = DMA##d##_Stream##s##_IRQn, \
.rcc = RCC_AHB1Periph_DMA##d, \
.userParam = 0 \
}

typedef enum {
DMA1_ST0_HANDLER = 0,
DMA1_ST1_HANDLER,
DMA1_ST2_HANDLER,
DMA1_ST3_HANDLER,
DMA1_ST4_HANDLER,
DMA1_ST5_HANDLER,
DMA1_ST6_HANDLER,
DMA1_ST7_HANDLER,
DMA2_ST0_HANDLER,
DMA2_ST1_HANDLER,
DMA2_ST2_HANDLER,
DMA2_ST3_HANDLER,
DMA2_ST4_HANDLER,
DMA2_ST5_HANDLER,
DMA2_ST6_HANDLER,
DMA2_ST7_HANDLER,
} dmaHandlerIdentifier_e;

#define DEFINE_DMA_CHANNEL(d, s, f, i, r) {.dma = d, .ref = s, .irqHandlerCallback = NULL, .flagsShift = f, .irqN = i, .rcc = r, .userParam = 0}
#define DEFINE_DMA_IRQ_HANDLER(d, s, i) void DMA ## d ## _Stream ## s ## _IRQHandler(void) {\
if (dmaDescriptors[i].irqHandlerCallback)\
dmaDescriptors[i].irqHandlerCallback(&dmaDescriptors[i]);\
Expand All @@ -73,24 +85,47 @@ typedef enum {
#define DMA_IT_DMEIF ((uint32_t)0x00000004)
#define DMA_IT_FEIF ((uint32_t)0x00000001)

#else
#elif defined(STM32F7)

#define DEFINE_DMA_CHANNEL(d, s, f) { \
.tag = DMA_TAG(d, s, 0), \
.dma = DMA##d, \
.ref = DMA##d##_Stream##s, \
.irqHandlerCallback = NULL, \
.flagsShift = f, \
.irqNumber = DMA##d##_Stream##s##_IRQn, \
.rcc = RCC_AHB1ENR_DMA##d##EN, \
.userParam = 0 \
}

#define DEFINE_DMA_IRQ_HANDLER(d, s, i) void DMA ## d ## _Stream ## s ## _IRQHandler(void) {\
if (dmaDescriptors[i].irqHandlerCallback)\
dmaDescriptors[i].irqHandlerCallback(&dmaDescriptors[i]);\
}

#define DMA_CLEAR_FLAG(d, flag) if (d->flagsShift > 31) d->dma->HIFCR = (flag << (d->flagsShift - 32)); else d->dma->LIFCR = (flag << d->flagsShift)
#define DMA_GET_FLAG_STATUS(d, flag) (d->flagsShift > 31 ? d->dma->HISR & (flag << (d->flagsShift - 32)): d->dma->LISR & (flag << d->flagsShift))


#define DMA_IT_TCIF ((uint32_t)0x00000020)
#define DMA_IT_HTIF ((uint32_t)0x00000010)
#define DMA_IT_TEIF ((uint32_t)0x00000008)
#define DMA_IT_DMEIF ((uint32_t)0x00000004)
#define DMA_IT_FEIF ((uint32_t)0x00000001)

#else // STM32F3

#define DEFINE_DMA_CHANNEL(d, c, f) { \
.tag = DMA_TAG(d, 0, c), \
.dma = DMA##d, \
.ref = DMA##d##_Channel##c, \
.irqHandlerCallback = NULL, \
.flagsShift = f, \
.irqNumber = DMA##d##_Channel##c##_IRQn, \
.rcc = RCC_AHBPeriph_DMA##d, \
.userParam = 0 \
}

typedef enum {
DMA1_CH1_HANDLER = 0,
DMA1_CH2_HANDLER,
DMA1_CH3_HANDLER,
DMA1_CH4_HANDLER,
DMA1_CH5_HANDLER,
DMA1_CH6_HANDLER,
DMA1_CH7_HANDLER,
DMA2_CH1_HANDLER,
DMA2_CH2_HANDLER,
DMA2_CH3_HANDLER,
DMA2_CH4_HANDLER,
DMA2_CH5_HANDLER,
} dmaHandlerIdentifier_e;

#define DEFINE_DMA_CHANNEL(d, c, f, i, r) {.dma = d, .ref = c, .irqHandlerCallback = NULL, .flagsShift = f, .irqN = i, .rcc = r, .userParam = 0}
#define DEFINE_DMA_IRQ_HANDLER(d, c, i) void DMA ## d ## _Channel ## c ## _IRQHandler(void) {\
if (dmaDescriptors[i].irqHandlerCallback)\
dmaDescriptors[i].irqHandlerCallback(&dmaDescriptors[i]);\
Expand All @@ -105,18 +140,15 @@ typedef enum {

#endif

#if defined(STM32F7)
//#define HAL_CLEANINVALIDATECACHE(addr, size) (SCB_CleanInvalidateDCache_by_Addr((uint32_t*)((uint32_t)addr & ~0x1f), ((uint32_t)(addr + size + 0x1f) & ~0x1f) - ((uint32_t)addr & ~0x1f)))
//#define HAL_CLEANCACHE(addr, size) (SCB_CleanDCache_by_Addr((uint32_t*)((uint32_t)addr & ~0x1f), ((uint32_t)(addr + size + 0x1f) & ~0x1f) - ((uint32_t)addr & ~0x1f)))
#endif

#if defined(STM32F4) || defined(STM32F7)
dmaHandlerIdentifier_e dmaFindHandlerIdentifier(DMA_Stream_TypeDef* stream);
DMA_t dmaFindHandlerIdentifier(DMA_Stream_TypeDef* stream);
#else
dmaHandlerIdentifier_e dmaFindHandlerIdentifier(DMA_Channel_TypeDef* channel);
DMA_t dmaFindHandlerIdentifier(DMA_Channel_TypeDef* channel);
#endif

void dmaInit(dmaHandlerIdentifier_e identifier, resourceOwner_e owner, uint8_t resourceIndex);
void dmaEnableClock(dmaHandlerIdentifier_e identifier);
void dmaSetHandler(dmaHandlerIdentifier_e identifier, dmaCallbackHandlerFuncPtr callback, uint32_t priority, uint32_t userParam);

DMA_t dmaGetByTag(dmaTag_t tag);
uint32_t dmaGetChannelByTag(dmaTag_t tag);
void dmaInit(DMA_t dma, resourceOwner_e owner, uint8_t resourceIndex);
void dmaEnableClock(DMA_t dma);
void dmaSetHandler(DMA_t dma, dmaCallbackHandlerFuncPtr callback, uint32_t priority, uint32_t userParam);
void dmaCleanInterrupts(DMA_t dma);
Loading