STM32H7的DMA双缓冲控制IO输出脉冲
- keil的sct文件配置
- MPU配置
- 初始化DMA双缓冲
- 初始化TIM12用处触发DMAMUX的请求发生器
keil的sct文件配置
; *************************************************************
; *** Scatter-Loading Description File generated by uVision ***
; *************************************************************LR_IROM1 0x08000000 0x00200000 { ; load region size_regionER_IROM1 0x08000000 0x00200000 { ; load address = execution address*.o (RESET, +First)*(InRoot$$Sections).ANY (+RO)}RW_IRAM1 0x20000000 0x00020000 { ; RW data - 128KB DTCM.ANY (+RW +ZI)}RW_IRAM2 0x24000000 0x00080000 { ; RW data - 512KB AXI SRAM*(.RAM_D1) }RW_IRAM3 0x30000000 0x00048000 { ; RW data - 128KB SRAM1(0x30000000) + 128KB SRAM2(0x3002 0000) + 32KB SRAM3(0x30040000)*(.RAM_D2)}RW_IRAM4 0x38000000 0x00010000 { ; RW data - 64KB SRAM4(0x38000000)*(.RAM_D3)}
}
MPU配置
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
MPU_InitStruct.BaseAddress = 0x38000000;
MPU_InitStruct.Size = ARM_MPU_REGION_SIZE_64KB;
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
MPU_InitStruct.IsCacheable = MPU_ACCESS_CACHEABLE;
MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER2;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
MPU_InitStruct.SubRegionDisable = 0x00;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
初始化DMA双缓冲
#elif defined ( __CC_ARM )
ALIGN_32BYTES(__attribute__((section (".RAM_D3"))) uint32_t IO_Toggle[8]) ={ 0x00000002U, 0x00020000U, 0x00000002U, 0x00020000U, 0x00000002U, 0x00020000U, 0x00000002U, 0x00020000U, };
ALIGN_32BYTES(__attribute__((section (".RAM_D3"))) uint32_t IO_Toggle1[8]) ={ 0x00000002U, 0x00020000U, 0x00000002U, 0x00020000U, 0x00000002U, 0x00020000U, 0x00000002U, 0x00020000U, };
#endif
void bsp_InitTimDMA(void)
{GPIO_InitTypeDef GPIO_InitStruct;DMA_HandleTypeDef DMA_Handle = {0};HAL_DMA_MuxRequestGeneratorConfigTypeDef dmamux_ReqGenParams = {0};__HAL_RCC_GPIOB_CLK_ENABLE();GPIO_InitStruct.Pin = GPIO_PIN_1;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);__HAL_RCC_DMA1_CLK_ENABLE();DMA_Handle.Instance = DMA1_Stream1; DMA_Handle.Init.Request = DMA_REQUEST_GENERATOR0; DMA_Handle.Init.Direction = DMA_MEMORY_TO_PERIPH; DMA_Handle.Init.PeriphInc = DMA_PINC_DISABLE; DMA_Handle.Init.MemInc = DMA_MINC_ENABLE; DMA_Handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; DMA_Handle.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; DMA_Handle.Init.Mode = DMA_CIRCULAR; DMA_Handle.Init.Priority = DMA_PRIORITY_LOW; DMA_Handle.Init.FIFOMode = DMA_FIFOMODE_DISABLE; DMA_Handle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; DMA_Handle.Init.MemBurst = DMA_MBURST_SINGLE; DMA_Handle.Init.PeriphBurst = DMA_PBURST_SINGLE; if(HAL_DMA_Init(&DMA_Handle) != HAL_OK){Error_Handler(__FILE__, __LINE__); }HAL_NVIC_SetPriority(DMA1_Stream1_IRQn, 2, 0);HAL_NVIC_EnableIRQ(DMA1_Stream1_IRQn); dmamux_ReqGenParams.SignalID = HAL_DMAMUX1_REQ_GEN_TIM12_TRGO; dmamux_ReqGenParams.Polarity = HAL_DMAMUX_REQ_GEN_RISING; dmamux_ReqGenParams.RequestNumber = 1; HAL_DMAEx_ConfigMuxRequestGenerator(&DMA_Handle, &dmamux_ReqGenParams); HAL_DMAEx_EnableMuxRequestGenerator (&DMA_Handle); HAL_DMAEx_MultiBufferStart_IT(&DMA_Handle, (uint32_t)IO_Toggle, (uint32_t)&GPIOB->BSRRL,(uint32_t)IO_Toggle1, 8);TIM12_Config(0);
}
初始化TIM12用处触发DMAMUX的请求发生器
void TIM12_Config(uint8_t _Mode)
{TIM_HandleTypeDef htim ={0};TIM_MasterConfigTypeDef sMasterConfig = {0};TIM_OC_InitTypeDef sConfig = {0};uint32_t Period[2] = {1999, 19999};uint32_t Pulse[2] = {999, 9999}; __HAL_RCC_TIM12_CLK_ENABLE(); HAL_TIM_Base_DeInit(&htim);htim.Instance = TIM12;htim.Init.Period = Period[_Mode];htim.Init.Prescaler = 0;htim.Init.ClockDivision = 0;htim.Init.CounterMode = TIM_COUNTERMODE_UP;htim.Init.RepetitionCounter = 0;HAL_TIM_Base_Init(&htim);sConfig.OCMode = TIM_OCMODE_PWM1;sConfig.OCPolarity = TIM_OCPOLARITY_LOW;sConfig.Pulse = Pulse[_Mode]; if(HAL_TIM_OC_ConfigChannel(&htim, &sConfig, TIM_CHANNEL_1) != HAL_OK){Error_Handler(__FILE__, __LINE__);}if(HAL_TIM_OC_Start(&htim, TIM_CHANNEL_1) != HAL_OK){Error_Handler(__FILE__, __LINE__);}sMasterConfig.MasterOutputTrigger = TIM_TRGO_OC1REF;sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;HAL_TIMEx_MasterConfigSynchronization(&htim, &sMasterConfig);
}