问题描述:
1、使用MCU:STM32f103RE
2、开发环境:CubeIDE
3、固件版本:STM32Cube FW_F1 V1.8.04、硬件连接:4位共阴极数码管,GPIOA0~7通过100Ω电阻连接数码管7断码和DP位,GPIOC0~3连接NPN三极管基极控制数码管阴极接地实现位选;SPI2(GPIOB端口)连接W25Q64 Flash芯片。
5、问题概述:定时器TIM2更新事件和定时器TIM2通道1比较输出事件,分别触发DMA1_Ch2向GPIOC,DMA1_Ch5向GPIOA,传输数据,驱动数码管动态扫描显示,并且正常工作。但,当运行MX_FATFS_Init()始化FatFs文件后,数码管显示异常,跟踪定位到函数FATFS_LinkDriverEx()中的【disk.drv[disk.nbr] = drv;】,运行该句后数码管显示异常。
希望知道原因的大神不吝赐教,在此万分感谢!!!
主要实现代码如下://DMA1通道5传输数据volatile uint32_t LDS_Buffer[8]={0x00ff00f0, 0x00ff0000, 0x00ff000f, 0x00ff0000, 0x00ff00f0, 0x00ff0000, 0x00ff000f, 0x00ff0000};
//DMA1通道2传输数据
volatile uint32_t SEG_Buffer[8]={0x000f0008, 0x000f0000, 0x000f0004, 0x000f0000, 0x000f0002, 0x000f0000, 0x000f0001, 0x000f0000}; int main(void) { HAL_Init(); SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); W25QXX_Init(); MX_TIM2_Init(); HAL_DMA_Start(&hdma_tim2_ch1, (uint32_t)LDS_Buffer, (uint32_t)(&(GPIOA->BSRR)), 32); HAL_DMA_Start(&hdma_tim2_up, (uint32_t)SEG_Buffer, (uint32_t)(&(GPIOC->BSRR)), 32); __HAL_TIM_ENABLE_DMA(&htim2, TIM_DMA_CC1|TIM_DMA_UPDATE); HAL_TIM_Base_Start(&htim2);
运行完以上代码,数码管正常显示 MX_FATFS_Init(); //运行完该函数,数码管显示异常 while (1) { } }
代码跟踪:
- void MX_FATFS_Init(void)
- {
- /*## FatFS: Link the Flash driver ###########################*/
- retFlash = FATFS_LinkDriver(&Flash_Driver, FlashPath);
- }
- uint8_t FATFS_LinkDriver(Diskio_drvTypeDef *drv, char *path)
- {
- return FATFS_LinkDriverEx(drv, path, 0);
- }
- uint8_t FATFS_LinkDriverEx(Diskio_drvTypeDef *drv, char *path, uint8_t lun)
- {
- uint8_t ret = 1;
- uint8_t DiskNum = 0;
- if(disk.nbr <= _VOLUMES)
- {
- disk.is_initialized[disk.nbr] = 0;
- <b style="background-color: red;">disk.drv[disk.nbr] = drv; //跟踪至该句,运行该句后数码管显示异常</b>
- disk.lun[disk.nbr] = lun;
- DiskNum = disk.nbr++;
- path[0] = DiskNum + '0';
- path[1] = ':';
- path[2] = '/';
- path[3] = 0;
- ret = 0;
- }
- return ret;
- }
复制代码
Flash驱动移植:
- Diskio_drvTypeDef Flash_Driver =
- {
- Flash_initialize,
- Flash_status,
- Flash_read,
- #if _USE_WRITE == 1
- Flash_write,
- #endif /* _USE_WRITE == 1 */
- #if _USE_IOCTL == 1
- Flash_ioctl,
- #endif /* _USE_IOCTL == 1 */
- };
- DSTATUS Flash_initialize(BYTE pdrv) /* Physical drive nmuber to identify the drive */
- {
- W25QXX_Init();
- return RES_OK;
- }
- DSTATUS Flash_status(BYTE pdrv) /* Physical drive nmuber to identify the drive */
- {
- /* USER CODE BEGIN STATUS */
- Stat = STA_NOINIT;
- Stat = RES_OK;
- return Stat;
- /* USER CODE END STATUS */
- }
- DRESULT Flash_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
- {
- if (!count)
- return RES_PARERR;
- for(;count>0;count--)
- {
- W25QXX_Read(buff,sector*FLASH_SECTOR_SIZE, FLASH_SECTOR_SIZE);
- sector++;
- buff+=FLASH_SECTOR_SIZE;
- }
- return RES_OK;
- }
- #if _USE_WRITE == 1
- DRESULT Flash_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
- {
- if (!count)
- {
- return RES_PARERR;
- }
- for(;count>0;count--)
- {
- W25QXX_Erase_Sector(sector);
- W25QXX_Write_NoCheck((uint8_t*)buff,sector*FLASH_SECTOR_SIZE,FLASH_SECTOR_SIZE);
- sector++;
- buff+=FLASH_SECTOR_SIZE;
- }
- return RES_OK;
- }
- #endif /* _USE_WRITE == 1 */
- #if _USE_IOCTL == 1
- DRESULT Flash_ioctl (BYTE pdrv, BYTE cmd, void *buff)
- {
- DRESULT res;
- switch(cmd)
- {
- case CTRL_SYNC:
- res = RES_OK;
- break;
- case GET_SECTOR_SIZE:
- *(WORD*)buff = FLASH_SECTOR_SIZE;
- res = RES_OK;
- break;
- case GET_BLOCK_SIZE:
- *(WORD*)buff = FLASH_BLOCK_SIZE;
- res = RES_OK;
- break;
- case GET_SECTOR_COUNT:
- *(DWORD*)buff = FLASH_SECTOR_COUNT;
- res = RES_OK;
- break;
- default:
- res = RES_PARERR;
- break;
- }
- return res;
- }
复制代码
外设初始化:
- void MX_TIM2_Init(void)
- {
- TIM_ClockConfigTypeDef sClockSourceConfig = {0};
- TIM_MasterConfigTypeDef sMasterConfig = {0};
- TIM_OC_InitTypeDef sConfigOC = {0};
- htim2.Instance = TIM2;
- htim2.Init.Prescaler = 7199;
- htim2.Init.CounterMode = TIM_COUNTERMODE_UP; //向上计数
- htim2.Init.Period = 5;
- htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; //自动重载
- if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
- {
- Error_Handler();
- }
- sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
- {
- Error_Handler();
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
- {
- Error_Handler();
- }
- sConfigOC.OCMode = TIM_OCMODE_TIMING;
- sConfigOC.Pulse = 5;
- sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
- sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
- if (HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
- {
- Error_Handler();
- }
- }
- void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
- {
- if(tim_baseHandle->Instance==TIM2)
- {
- /* TIM2 clock enable */
- __HAL_RCC_TIM2_CLK_ENABLE();
- /* TIM2 DMA Init */
- /* TIM2_UP Init */
- hdma_tim2_up.Instance = DMA1_Channel2;
- hdma_tim2_up.Init.Direction = DMA_MEMORY_TO_PERIPH;
- hdma_tim2_up.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_tim2_up.Init.MemInc = DMA_MINC_ENABLE;
- hdma_tim2_up.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; //WORD
- hdma_tim2_up.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
- hdma_tim2_up.Init.Mode = DMA_CIRCULAR;
- hdma_tim2_up.Init.Priority = DMA_PRIORITY_LOW;
- if (HAL_DMA_Init(&hdma_tim2_up) != HAL_OK)
- {
- Error_Handler();
- }
- __HAL_LINKDMA(tim_baseHandle,hdma[TIM_DMA_ID_UPDATE],hdma_tim2_up);
- /* TIM2_CH1 Init */
- hdma_tim2_ch1.Instance = DMA1_Channel5;
- hdma_tim2_ch1.Init.Direction = DMA_MEMORY_TO_PERIPH;
- hdma_tim2_ch1.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_tim2_ch1.Init.MemInc = DMA_MINC_ENABLE;
- hdma_tim2_ch1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
- hdma_tim2_ch1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
- hdma_tim2_ch1.Init.Mode = DMA_CIRCULAR;
- hdma_tim2_ch1.Init.Priority = DMA_PRIORITY_LOW;
- if (HAL_DMA_Init(&hdma_tim2_ch1) != HAL_OK)
- {
- Error_Handler();
- }
- __HAL_LINKDMA(tim_baseHandle,hdma[TIM_DMA_ID_CC1],hdma_tim2_ch1);
- }
- }
- void MX_DMA_Init(void)
- {
- /* DMA controller clock enable */
- __HAL_RCC_DMA1_CLK_ENABLE();
- }
- void W25QXX_Init(void)
- {
- // uint8_t temp = 0;
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /**SPI2 GPIO Configuration
- PB12 ------> SPI2_CS
- PB13 ------> SPI2_SCK
- PB14 ------> SPI2_MISO
- PB15 ------> SPI2_MOSI
- */
- //PB12
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
- GPIO_InitStruct.Pin=GPIO_PIN_12; //PB12
- GPIO_InitStruct.Mode=GPIO_MODE_OUTPUT_PP; //推挽输出
- GPIO_InitStruct.Pull=GPIO_PULLUP; //上拉
- GPIO_InitStruct.Speed=GPIO_SPEED_FREQ_HIGH;//高速
- HAL_GPIO_Init(GPIOB,&GPIO_InitStruct); //初始化
- hspi2.Instance = SPI2;
- hspi2.Init.Mode = SPI_MODE_MASTER; //设置SPI工作模式:设置为主SPI
- hspi2.Init.Direction = SPI_DIRECTION_2LINES;
- hspi2.Init.DataSize = SPI_DATASIZE_8BIT; //设置SPI的数据大小:SPI发送接收8位帧结构
- hspi2.Init.CLKPolarity = SPI_POLARITY_HIGH; //选择了串行时钟的稳态:时钟悬空高
- hspi2.Init.CLKPhase = SPI_PHASE_2EDGE; //数据捕获(采样)于第二个时钟沿
- hspi2.Init.NSS = SPI_NSS_SOFT;
- hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
- hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
- hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
- hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
- hspi2.Init.CRCPolynomial = 10; //CRC值计算的多项式
- if (HAL_SPI_Init(&hspi2) != HAL_OK)
- {
- Error_Handler();
- }
- __HAL_SPI_ENABLE(&hspi2); //使能SPI2
- SPI2_ReadWriteByte(0Xff); //启动传输
- }
复制代码
|
评分
查看全部评分
程序正常运行,没有进入异常中断,加过串口,通信也都是正常,所谓显示异常是指不按设定的值显示,我设定显示1234,在运行MX_FATFS_Init之前都是正常显示1234,运行MX_FATFS_Init之后就变成了1288
程序没有进入HardFault,应该不是溢出,我堆和栈都设的挺大的0x2000;Flash这块能够正常模拟u盘的并且读写也都每天,Flash_Driver定义的内容应该不会有问题吧
百度跳到这里了,跟楼主遇到同样的问题,多次插拔SD卡,当执行disk.drv[disk.nbr] = drv语句之后disk.nbr变大了,导致挂载SD卡失败,正在排查为啥disk.nbr会变大,不知道哪里的内存溢出,如果楼主还经常逛这里的话可以回答一下吗:)
[md]问题已找到,是偶尔在特殊情况下没有调用FATFS_UnLinkDriverEx函数,导致disk.nbr一直等于1的情况下,再次调用FATFS_LinkDriverEx函数,从而导致disk.drv[disk.nbr] = drv时,相当于disk.drv[1]= drv,内存溢出;这是因为_VOLUMES定义为1,disk.drv[1]是不存在的数组变量