|
64 | 64 |
|
65 | 65 | #endif |
66 | 66 |
|
| 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 | + |
67 | 73 | // TODO: I think that as an optimization, we can allocate these dynamically |
68 | 74 | // if an sd card is detected. This will save approx 260 bytes of RAM |
69 | 75 | // when no sdcard was being used. |
70 | 76 | static SD_HandleTypeDef sd_handle; |
71 | 77 | 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 | +}; |
73 | 94 |
|
74 | 95 | void sdcard_init(void) { |
75 | 96 | GPIO_InitTypeDef GPIO_Init_Structure; |
@@ -106,36 +127,6 @@ void HAL_SD_MspInit(SD_HandleTypeDef *hsd) { |
106 | 127 | HAL_NVIC_SetPriority(SDIO_IRQn, IRQ_PRI_SDIO, IRQ_SUBPRI_SDIO); |
107 | 128 | HAL_NVIC_EnableIRQ(SDIO_IRQn); |
108 | 129 |
|
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 | | - |
139 | 130 | // GPIO have already been initialised by sdcard_init |
140 | 131 | } |
141 | 132 |
|
@@ -222,7 +213,8 @@ mp_uint_t sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blo |
222 | 213 | HAL_SD_ErrorTypedef err = SD_OK; |
223 | 214 |
|
224 | 215 | 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); |
226 | 218 | sd_handle.hdmarx = &sd_rx_dma; |
227 | 219 |
|
228 | 220 | 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 |
254 | 246 | HAL_SD_ErrorTypedef err = SD_OK; |
255 | 247 |
|
256 | 248 | 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); |
258 | 251 | sd_handle.hdmatx = &sd_tx_dma; |
259 | 252 |
|
260 | 253 | err = HAL_SD_WriteBlocks_BlockNumber_DMA(&sd_handle, (uint32_t*)src, block_num, SDCARD_BLOCK_SIZE, num_blocks); |
|
0 commit comments