Skip to content

Commit 9936aa3

Browse files
committed
stmhal: Save RAM and ROM by making SD DMA init structure const for tx/rx.
1 parent 522d454 commit 9936aa3

1 file changed

Lines changed: 26 additions & 33 deletions

File tree

stmhal/sdcard.c

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,33 @@
6464

6565
#endif
6666

67+
// TODO: Since SDIO is fundamentally half-duplex, we really only need to
68+
// tie up one DMA channel. However, the HAL DMA API doesn't
69+
// seem to provide a convenient way to change the direction. I believe that
70+
// its as simple as changing the CR register and the Init.Direction field
71+
// and make DMA_SetConfig public.
72+
6773
// TODO: I think that as an optimization, we can allocate these dynamically
6874
// if an sd card is detected. This will save approx 260 bytes of RAM
6975
// when no sdcard was being used.
7076
static SD_HandleTypeDef sd_handle;
7177
static DMA_HandleTypeDef sd_rx_dma, sd_tx_dma;
72-
static DMA_InitTypeDef sd_rx_dma_init, sd_tx_dma_init;
78+
79+
// Parameters to dma_init() for SDIO tx and rx.
80+
static const DMA_InitTypeDef dma_init_struct_sdio = {
81+
.Channel = 0,
82+
.Direction = 0,
83+
.PeriphInc = DMA_PINC_DISABLE,
84+
.MemInc = DMA_MINC_ENABLE,
85+
.PeriphDataAlignment = DMA_PDATAALIGN_WORD,
86+
.MemDataAlignment = DMA_MDATAALIGN_WORD,
87+
.Mode = DMA_PFCTRL,
88+
.Priority = DMA_PRIORITY_VERY_HIGH,
89+
.FIFOMode = DMA_FIFOMODE_ENABLE,
90+
.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL,
91+
.MemBurst = DMA_MBURST_INC4,
92+
.PeriphBurst = DMA_PBURST_INC4,
93+
};
7394

7495
void sdcard_init(void) {
7596
GPIO_InitTypeDef GPIO_Init_Structure;
@@ -106,36 +127,6 @@ void HAL_SD_MspInit(SD_HandleTypeDef *hsd) {
106127
HAL_NVIC_SetPriority(SDIO_IRQn, IRQ_PRI_SDIO, IRQ_SUBPRI_SDIO);
107128
HAL_NVIC_EnableIRQ(SDIO_IRQn);
108129

109-
// TODO: Since SDIO is fundamentally half-duplex, we really only need to
110-
// tie up one DMA channel. However, the HAL DMA API doesn't
111-
// seem to provide a convenient way to change the direction. I believe that
112-
// its as simple as changing the CR register and the Init.Direction field
113-
// and make DMA_SetConfig public.
114-
115-
// Configure DMA Rx parameters
116-
sd_rx_dma_init.PeriphInc = DMA_PINC_DISABLE;
117-
sd_rx_dma_init.MemInc = DMA_MINC_ENABLE;
118-
sd_rx_dma_init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
119-
sd_rx_dma_init.MemDataAlignment = DMA_MDATAALIGN_WORD;
120-
sd_rx_dma_init.Mode = DMA_PFCTRL;
121-
sd_rx_dma_init.Priority = DMA_PRIORITY_VERY_HIGH;
122-
sd_rx_dma_init.FIFOMode = DMA_FIFOMODE_ENABLE;
123-
sd_rx_dma_init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
124-
sd_rx_dma_init.MemBurst = DMA_MBURST_INC4;
125-
sd_rx_dma_init.PeriphBurst = DMA_PBURST_INC4;
126-
127-
// Configure DMA Tx parameters
128-
sd_tx_dma_init.PeriphInc = DMA_PINC_DISABLE;
129-
sd_tx_dma_init.MemInc = DMA_MINC_ENABLE;
130-
sd_tx_dma_init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
131-
sd_tx_dma_init.MemDataAlignment = DMA_MDATAALIGN_WORD;
132-
sd_tx_dma_init.Mode = DMA_PFCTRL;
133-
sd_tx_dma_init.Priority = DMA_PRIORITY_VERY_HIGH;
134-
sd_tx_dma_init.FIFOMode = DMA_FIFOMODE_ENABLE;
135-
sd_tx_dma_init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
136-
sd_tx_dma_init.MemBurst = DMA_MBURST_INC4;
137-
sd_tx_dma_init.PeriphBurst = DMA_PBURST_INC4;
138-
139130
// GPIO have already been initialised by sdcard_init
140131
}
141132

@@ -222,7 +213,8 @@ mp_uint_t sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blo
222213
HAL_SD_ErrorTypedef err = SD_OK;
223214

224215
if (query_irq() == IRQ_STATE_ENABLED) {
225-
dma_init(&sd_rx_dma, DMA_STREAM_SDIO_RX, &sd_rx_dma_init, DMA_CHANNEL_SDIO_RX, DMA_PERIPH_TO_MEMORY, &sd_handle);
216+
dma_init(&sd_rx_dma, DMA_STREAM_SDIO_RX, &dma_init_struct_sdio,
217+
DMA_CHANNEL_SDIO_RX, DMA_PERIPH_TO_MEMORY, &sd_handle);
226218
sd_handle.hdmarx = &sd_rx_dma;
227219

228220
err = HAL_SD_ReadBlocks_BlockNumber_DMA(&sd_handle, (uint32_t*)dest, block_num, SDCARD_BLOCK_SIZE, num_blocks);
@@ -254,7 +246,8 @@ mp_uint_t sdcard_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t n
254246
HAL_SD_ErrorTypedef err = SD_OK;
255247

256248
if (query_irq() == IRQ_STATE_ENABLED) {
257-
dma_init(&sd_tx_dma, DMA_STREAM_SDIO_TX, &sd_tx_dma_init, DMA_CHANNEL_SDIO_TX, DMA_MEMORY_TO_PERIPH, &sd_handle);
249+
dma_init(&sd_tx_dma, DMA_STREAM_SDIO_TX, &dma_init_struct_sdio,
250+
DMA_CHANNEL_SDIO_TX, DMA_MEMORY_TO_PERIPH, &sd_handle);
258251
sd_handle.hdmatx = &sd_tx_dma;
259252

260253
err = HAL_SD_WriteBlocks_BlockNumber_DMA(&sd_handle, (uint32_t*)src, block_num, SDCARD_BLOCK_SIZE, num_blocks);

0 commit comments

Comments
 (0)