2020#include " camera.h"
2121#include " Wire.h"
2222#include " stm32h7xx_hal_dcmi.h"
23+ #include " stm_dma_utils.h"
2324
2425// Workaround for the broken UNUSED macro.
2526#undef UNUSED
@@ -73,10 +74,11 @@ arduino::MbedI2C CameraWire(I2C_SDA1, I2C_SCL1);
7374
7475#define DCMI_IRQ_PRI NVIC_EncodePriority (NVIC_PRIORITYGROUP_4, 2 , 0 )
7576
76- #define DCMI_DMA_CLK_ENABLE () __HAL_RCC_DMA2_CLK_ENABLE()
77- #define DCMI_DMA_STREAM DMA2_Stream3
78- #define DCMI_DMA_IRQ DMA2_Stream3_IRQn
79- #define DCMI_DMA_IRQ_PRI NVIC_EncodePriority (NVIC_PRIORITYGROUP_4, 3 , 0 )
77+ const static DMALinkInfo dcmiDMALink{
78+ .dmaIdx = 2 ,
79+ .channelIdx = 3 ,
80+ .sourceNumber = DMA_REQUEST_DCMI
81+ };
8082
8183// DCMI GPIO pins struct
8284static const struct { GPIO_TypeDef *port; uint16_t pin; } dcmi_pins[] = {
@@ -121,7 +123,6 @@ static const struct { GPIO_TypeDef *port; uint16_t pin; } dcmi_pins[] = {
121123#define NUM_DCMI_PINS (sizeof (dcmi_pins)/sizeof (dcmi_pins[0 ]))
122124
123125static TIM_HandleTypeDef htim = {0 };
124- static DMA_HandleTypeDef hdma = {0 };
125126static DCMI_HandleTypeDef hdcmi = {0 };
126127
127128// / Table to store the amount of bytes per pixel for each pixel format
@@ -207,8 +208,7 @@ void HAL_DCMI_MspDeInit(DCMI_HandleTypeDef* hdcmi)
207208 // Disable DCMI IRQs.
208209 HAL_NVIC_DisableIRQ (DCMI_IRQn);
209210
210- // Disable DMA IRQs.
211- HAL_NVIC_DisableIRQ (DCMI_DMA_IRQ);
211+ stm_free_dma_link (&dcmiDMALink);
212212
213213 // Deinit the DMA stream.
214214 if (hdcmi->DMA_Handle != NULL ) {
@@ -270,29 +270,7 @@ __weak int camera_extclk_config(int frequency)
270270uint8_t camera_dcmi_config (bool bsm_skip)
271271{
272272 // DMA Stream configuration
273- hdma.Instance = DCMI_DMA_STREAM;
274- hdma.Init .Request = DMA_REQUEST_DCMI;
275- hdma.Init .Direction = DMA_PERIPH_TO_MEMORY;
276- hdma.Init .MemInc = DMA_MINC_ENABLE;
277- hdma.Init .PeriphInc = DMA_PINC_DISABLE;
278- hdma.Init .PeriphDataAlignment = DMA_PDATAALIGN_WORD;
279- hdma.Init .MemDataAlignment = DMA_MDATAALIGN_WORD;
280- hdma.Init .Mode = DMA_NORMAL;
281- hdma.Init .Priority = DMA_PRIORITY_HIGH;
282- hdma.Init .FIFOMode = DMA_FIFOMODE_DISABLE;
283- hdma.Init .FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
284- hdma.Init .MemBurst = DMA_MBURST_INC4;
285- hdma.Init .PeriphBurst = DMA_PBURST_SINGLE;
286-
287- // Enable DMA clock
288- DCMI_DMA_CLK_ENABLE ();
289-
290- // Initialize the DMA stream
291- HAL_DMA_Init (&hdma);
292-
293- // Configure and enable DMA IRQ Channel
294- NVIC_SetPriority (DCMI_DMA_IRQ, DCMI_DMA_IRQ_PRI);
295- HAL_NVIC_EnableIRQ (DCMI_DMA_IRQ);
273+ DMA_HandleTypeDef * handle = stm_init_dma_link (&dcmiDMALink, DMA_PERIPH_TO_MEMORY, false , true , 4 , 4 , DMA_NORMAL);
296274
297275 // Configure the DCMI interface.
298276 hdcmi.Instance = DCMI;
@@ -309,7 +287,7 @@ uint8_t camera_dcmi_config(bool bsm_skip)
309287 hdcmi.Init .LineSelectStart = DCMI_OELS_ODD; // Ignored, unless LSM != ALL
310288
311289 // Link the DMA handle to the DCMI handle.
312- __HAL_LINKDMA (&hdcmi, DMA_Handle, hdma );
290+ __HAL_LINKDMA (&hdcmi, DMA_Handle, *handle );
313291
314292 // Initialize the DCMI
315293 HAL_DCMI_Init (&hdcmi);
@@ -325,12 +303,6 @@ void DCMI_IRQHandler(void)
325303{
326304 HAL_DCMI_IRQHandler (&hdcmi);
327305}
328-
329- void DMA2_Stream3_IRQHandler (void )
330- {
331- HAL_DMA_IRQHandler (&hdma);
332- }
333-
334306} // extern "C"
335307
336308FrameBuffer::FrameBuffer (int32_t x, int32_t y, int32_t bpp) :
0 commit comments