本帖最后由 nicholasldf 于 2018-12-19 20:47 编辑
STM32L431单片机,如果一开机就跑80MHZ,SpiFlash读写正常。
如果一开机就跑20MHZ,SpiFlash读写也正常。
但是一旦启用智能低功耗模式,从80M切换到20MHZ,,,按照上电的统一代码流程,复位了SPI接口,重新初始化SPI,但是SpiFlash读写偶尔出现异常。
SpiFlash芯片初始化函数如下:
HAL_StatusTypeDef bsp_w25qxx_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/*## Enable peripherals and GPIO Clocks #################################*/
//Enable GPIO clock
__HAL_RCC_GPIOB_CLK_ENABLE();
//Enable SPI2 clock
__HAL_RCC_SPI2_CLK_ENABLE();
/*## Configure peripheral GPIO ##########################################*/
//SPI CS GPIO pin configuration
//PB2-power, PB12-CS,
GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
//PB2-power-on
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(W25QXX_HWCS_GPIO_PORT, W25QXX_HWCS_PIN, GPIO_PIN_SET);
//SPI SCK MOSI MISO GPIO pin configuration
//PB13-SCK, PB14-MISO, PB15-MOSI
GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*## Configure the SPI peripheral #######################################*/
//Set the SPI parameters
SpiDataFlashHandle.Instance = SPI2;
HAL_SPI_DeInit(&SpiDataFlashHandle);//SPI-DeInit
__HAL_RCC_SPI2_FORCE_RESET();
__HAL_RCC_SPI2_RELEASE_RESET();
if(UserUsageCfg.LowPowerRunFlag){
SpiDataFlashHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;//20M/2=10M, STM32L4 SPI2 MaxCLK <= 40M
}else{
SpiDataFlashHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;//80M/4=20M, STM32L4 SPI2 MaxCLK <= 40M
}
SpiDataFlashHandle.Init.Direction = SPI_DIRECTION_2LINES;
SpiDataFlashHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
SpiDataFlashHandle.Init.CLKPolarity = SPI_POLARITY_LOW;
SpiDataFlashHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
SpiDataFlashHandle.Init.CRCPolynomial = 7;
SpiDataFlashHandle.Init.DataSize = SPI_DATASIZE_8BIT;
SpiDataFlashHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
SpiDataFlashHandle.Init.NSS = SPI_NSS_SOFT;
SpiDataFlashHandle.Init.TIMode = SPI_TIMODE_DISABLE;
SpiDataFlashHandle.Init.Mode = SPI_MODE_MASTER;
if(HAL_SPI_Init(&SpiDataFlashHandle) != HAL_OK) {
return HAL_ERROR;//Initialization Error
}else{
return HAL_OK;
}
}
|