读取Flash w25x64未响应 导致卡死的问题

程序有一部分是读取Flash中存储数据,向串口发送该读取数据,若串口很长时间没有串口,能判断未连接。(出现概率极低 20小时左右出现一次)

SPI_FLASH_Init();
SPI_FLASH_BufferRead((uint8_t *)posbufs,4096 + (mPOSCOUNT.posoldCount%60000) * 32,32);
/*******************************************************************************
* Function Name  : SPI_FLASH_Init
* Description    : Initializes the peripherals used by the SPI FLASH driver.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SPI_FLASH_Init(void)
{    
    CMU_ClockEnable(cmuClock_GPIO, true);
    // 初始化模拟SPI的IO口
    // SPI_FLASH_SCK_S  PA5
    GPIO_DriveModeSet(SPI_FLASH_SCK_PORT_S, gpioDriveModeStandard);                 /* ÉèÖÃÇý¶¯µçÁ÷ΪĬÈÏ´óС       */
    GPIO_PinModeSet(SPI_FLASH_SCK_PORT_S, SPI_FLASH_SCK_PIN_S, gpioModePushPullDrive, 1); 
     
    // SPI_FLASH_MOSI_S PA7
    GPIO_DriveModeSet(SPI_FLASH_MOSI_PORY_S, gpioDriveModeStandard);                 /* ÉèÖÃÇý¶¯µçÁ÷ΪĬÈÏ´óС       */
    GPIO_PinModeSet(SPI_FLASH_MOSI_PORY_S, SPI_FLASH_MOSI_PIN_S, gpioModePushPullDrive, 1); 
    // SPI_FLASH_NSS_S  PA4
    GPIO_DriveModeSet(SPI_FLASH_NSS_PORT_S, gpioDriveModeStandard);                 /* ÉèÖÃÇý¶¯µçÁ÷ΪĬÈÏ´óС       */
     GPIO_PinModeSet(SPI_FLASH_NSS_PORT_S, SPI_FLASH_NSS_PIN_S, gpioModePushPullDrive, 1); 

    // SPI_FLASH_MISO_S PA6
    GPIO_PinModeSet(SPI_FLASH_MISO_PORY_S, SPI_FLASH_MISO_PIN_S, gpioModeInput, 1);
    SPI_FLASH_NSS_S_1;    
    SPI_FLASH_SCK_S_0;
    SPI_FLASH_CS_HIGH();
//    SPI_FLASH_ReadByte();
}
/*******************************************************************************
* Function Name  : SPI_FLASH_BufferRead
* Description    : Reads a block of data from the FLASH.
* Input          : - pBuffer : pointer to the buffer that receives the data read
*                    from the FLASH.
*                  - ReadAddr : FLASH's internal address to read from.
*                  - NumByteToRead : number of bytes to read from the FLASH.
* Output         : None
* Return         : None
*******************************************************************************/
void SPI_FLASH_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)
{
    /* Select the FLASH: Chip Select low */
    SPI_FLASH_CS_LOW();

    /* Send "Read from Memory " instruction */
    SPI_FLASH_SendByte(W25X_ReadData);

    /* Send ReadAddr high nibble address byte to read from */
    SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
    /* Send ReadAddr medium nibble address byte to read from */
    SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
    /* Send ReadAddr low nibble address byte to read from */
    SPI_FLASH_SendByte(ReadAddr & 0xFF);

    while (NumByteToRead--) /* while there is data to be read */
    { 
        //SPI_FLASH_SendByte(Dummy_Byte);
        *pBuffer = SPI_FLASH_ReadByte();//模拟SPI读取

        /* Point to the next location where the byte read will be saved */
        pBuffer++;
    }

    /* Deselect the FLASH: Chip Select high */
    SPI_FLASH_CS_HIGH();
}
/******************************************************************
 - ¹¦ÄÜÃèÊö£ºIOÄ£ÄâSPI£¬¶ÁÈ¡Ò»¸ö×Ö½Ú
 - Á¥ÊôÄ£¿é£º
 - º¯ÊýÊôÐÔ£ºÄÚ²¿
 - ²ÎÊý˵Ã÷£ºÎÞ
 - ·µ»Ø˵Ã÷£º·µ»Ø¶Áµ½µÄ×Ö½Ú
 ******************************************************************/

u8 SPI_FLASH_ReadByte(void) //SPI¶ÁÒ»¸ö×Ö½Ú
{ 
    u8 i;
    u8 byte;
    __NOP();__NOP();__NOP();__NOP();
    byte=0;
    for (i=0; i<8; i++)
    {
        byte <<= 1;
        SPI_FLASH_SCK_S_1;
        __NOP();__NOP();__NOP();__NOP();
        if(SPI_FLASH_MISO_S_G != 0)
        {
                byte |= 1;    //Wait SDO to go Hi
        }
        SPI_FLASH_SCK_S_0;
        __NOP();__NOP();__NOP();__NOP();
    }

    return byte;

}

后发现有可能是在SPI读取过程中,被中断打断导致异常,所以在读取前后添加关闭中断,打开中断函数。

OS_ENTER_CRITICAL(); /* Tell uC/OS-II that we are starting an ISR*/
    DebugValue = 1;
    SPI_FLASH_Init();
    SPI_FLASH_BufferRead((uint8_t *)posbufs,4096 + (mPOSCOUNT.posoldCount%60000) * 32,32);
    OS_EXIT_CRITICAL();
原文地址:https://www.cnblogs.com/-Donny/p/9443447.html