AnsweredAssumed Answered

STM32F103C6 SPI to dataflash(M25p80) sending & receiving problem

Question asked by rap_s.vinay on Feb 11, 2015
Latest reply on Feb 12, 2015 by gonzalez.laurent
Hi,
     I am trying to send data from SPI to external data flash and reading from that flash..
Here SPI_DR (data register) not taking any data ..so RXNE bit not setting while checking status .
Please help me to find problem.

INT8U u8_gWriteBuffer[100] = "VINAY";
/*Read data Buffer from data flash*/
     INT8U u8_gReadBuffer[100];



void DATAFLASHAPP_Init(void)
{
     /*Data Flash enum Variable IntialiSation*/
     EN_DATAFLASH_RESULT_t en_lDATAFLASHResult = DATAFLASH_SUCCESS;


     INT8U u8_Temp = 0x00;


     /*Data flash IntialiSation */
     en_lDATAFLASHResult = DATAFLASH_Init();


     /* Selecting buffer ,byte location , data number of bytes*/
     DATAFLASH_WriteData(300,u8_gWriteBuffer , 5);


     //Read From Data Flash
     DATAFLASH_ReadData(300, u8_gReadBuffer, 5);




}

ST_SPI_DATA_t st_gSPIIntiConfig = {


          .en_mDevice                      = SPI1,
          .u8_mCommunicationType       = SPI_FULL_DUP,
          .u32_mMode                          = SPI_MODE0,
          .u32_mDataSize                      = SPI_8BIT_DATA,
          .u8_mSlaveSlectManagement    = SPI_SW_MANAGMENT,
          .u32_mBaudratePrescalar       = SPI_PRESCALE_256,
          .u32_mFirstBit                      = SPI_MSB_FIRST,
          .u8_mSelection                     = SPI_MASTER_SEL,
          .u32_mCRCPolynomial            = SPI_CRCPR_RESET,
          .u8_mSPIMode                     = SPI_ENABLE,
          .u8_mDMA_MODE                     = DMA_DISABLE,
          .u8_mDMA_ReqType                = SPI_CR2_DMA_DISABLE_MASK
};


/* Initialize the PORTA PIN4 Data Structure (NSS)*/
GPIO_PIN_DATA_t st_gPORTA_PIN4_Data = {


     .en_mPort  = GPIO_PORTA,
     .u8_mPin   = PORT_PIN4,
     .u8_mMode  = GPIO_MODE_OP_GENERAL_PP,
     .u8_mSpeed = GPIO_OP_SPEED_50MHZ,


};


/* Initialize the PORTA PIN5 Data Structure  (SCK)*/
GPIO_PIN_DATA_t st_gPORTA_PIN5_Data = {


     .en_mPort  = GPIO_PORTA,
     .u8_mPin   = PORT_PIN5,
     .u8_mMode  = GPIO_MODE_OP_ALTER_PP,
     .u8_mSpeed = GPIO_OP_SPEED_50MHZ,


};


/* Initialize the PORTA PIN6 Data Structure (MISO)*/
GPIO_PIN_DATA_t st_gPORTA_PIN6_Data = {


     .en_mPort  = GPIO_PORTA,
     .u8_mPin   = PORT_PIN6,
     .u8_mMode  = GPIO_MODE_OP_ALTER_PP,
     .u8_mSpeed = GPIO_OP_SPEED_50MHZ,


};


/* Initialize the PORTA PIN7 Data Structure (MOSI)*/
GPIO_PIN_DATA_t st_gPORTA_PIN7_Data = {


     .en_mPort  = GPIO_PORTA,
     .u8_mPin   = PORT_PIN7,
     .u8_mMode  = GPIO_MODE_OP_ALTER_PP,
     .u8_mSpeed = GPIO_OP_SPEED_50MHZ,


};

EN_DATAFLASH_RESULT_t DATAFLASH_Init(void)
{
     EN_DATAFLASH_RESULT_t en_lResult = DATAFLASH_SUCCESS;






     /* GPIO configuration for SPI1 */
     GPIO_ConfigurePin(&st_gPORTA_PIN4_Data);
     GPIO_ConfigurePin(&st_gPORTA_PIN5_Data);
     GPIO_ConfigurePin(&st_gPORTA_PIN6_Data);
     GPIO_ConfigurePin(&st_gPORTA_PIN7_Data);


     /* InitailiSe SPI2 */
     if(SPI_SUCCESS != SPI_Init(&st_gSPIIntiConfig))
     {
          en_lResult = DATAFLASH_ERROR;
     }
     else
     {
          /* NO Action */
     }


     /* Pull to Slave Select Pin High*/
     GPIO_SetPin(GPIO_PORTA, PORT_PIN4);


     return en_lResult;
}

void DATAFLASH_WriteData(INT32U u32_fAddress,INT8U *p_fWriteBuffer, INT32U u32_fLength)
{


     /*InitiliSing Device id*/
     INT32U u32_lPageAddress = 0x0000;
     INT32U u32_lPageByteAddress = 0x0000;
     INT32U u32_lTemp = 0 , u32_lFlashStatus = 0;


     /* Check whether given address more than total size or NOT */
     if((u32_fAddress) > ( PAGE_SIZE * TOTAL_PAGES))
     {
          return;
     }


     /* For Getting Page Address */
     u32_lPageAddress = (u32_fAddress)/PAGE_SIZE;


     /* For Getting Byte Address */
     u32_lPageByteAddress = u32_fAddress % PAGE_SIZE;




     /* Pull to Slave Select Pin Low*/
     GPIO_ResetPin(GPIO_PORTA, PORT_PIN4);


     /* Checking the write enable latch bit to write data*/
     DATAFLASH_SendByte(FLASH_WRITE_ENABLE_LATCH);


     /*Page  Write address*/
     DATAFLASH_SendByte(FLASH_WRITE_PAGE);


     /* Send Sector address byte to write  */
     DATAFLASH_SendByte((u32_fAddress & 0xFF0000) >> 16);


     /* Send Page address byte to write  */
     DATAFLASH_SendByte((u32_fAddress & 0xFF00) >> 8);


     /* Send Byte address byte to write  */
     DATAFLASH_SendByte(u32_fAddress & 0xFF);




     /* write data  on the FLASH */
     while(u32_lTemp <= u32_fLength)
     {
          DATAFLASH_SendByte(p_fWriteBuffer[u32_lTemp++]);
     }


     /* Deselect the FLASH: Chip Select high */
     GPIO_SetPin(GPIO_PORTA, PORT_PIN4);


     /* Wait the end of Flash writing */


     /* Pull to Slave Select Pin Low*/
     GPIO_ResetPin(GPIO_PORTA, PORT_PIN4);


     /* Checking the status of the flash */
     DATAFLASH_SendByte(FLASH_READ_STATUS_REG);


     /* Loop as long as the memory is busy with a write cycle */
     do
     {
            /* Send a dummy byte to generate the clock needed by the FLASH
            and put the value of the status register in u32_lFlashStatus variable */
           u32_lFlashStatus = DATAFLASH_SendByte(0x00);


    }
     while ((u32_lFlashStatus & FLASH_WRITE_IN_PROGRESS) == SET); /* Write in progress */


     /* Pull to Slave Select Pin High*/
     GPIO_SetPin(GPIO_PORTA, PORT_PIN4);




}

void DATAFLASH_ReadData(INT32U u32_fAddress,INT8U *p_fReadBuffer, INT32U u32_fLength)
{


     /*InitiliSing Device id*/
     INT32U u32_lPageAddress = 0x0000;
     INT32U u32_lPageByteAddress = 0x0000;
     INT32U u32_lIndex = 0x0000;
     INT32U u32_lDataBytes = 0x0000;


     /* Check whether given address more than total size or NOT */
     if((u32_fAddress) > ( PAGE_SIZE * TOTAL_PAGES))
     {
          return;
     }


     u32_lDataBytes = u32_fLength;


     /* For Getting Page Address */
     u32_lPageAddress = (u32_fAddress)/PAGE_SIZE;


     /* For Getting Byte Address */
     u32_lPageByteAddress = u32_fAddress % PAGE_SIZE;


     /* Pull to Slave Select Pin Low*/
     GPIO_ResetPin(GPIO_PORTA, PORT_PIN4);


     /* Sending Commands For Page Read*/
     DATAFLASH_SendByte(FLASH_READ_DATA_BYTES);


     /* Set the Page address to read data */
     DATAFLASH_SendByte(u32_lPageAddress);
     DATAFLASH_SendByte(u32_lPageByteAddress & BYTE_HEX);


     /* Send dummy value to wait for Read bits */
     DATAFLASH_SendByte(0x00);


     /* Reading Data From Data flash*/
     while(u32_lIndex < u32_lDataBytes)
     {
          *(p_fReadBuffer + u32_lIndex++ ) = DATAFLASH_SendByte(0x00);
     }


     /* Pull to Slave Select Pin High*/
     GPIO_SetPin(GPIO_PORTA, PORT_PIN4);


}


/*************************************************************************************************/
/* Function Name : INT8U DATAFLASH_SendByte(INT8U u8_lByte)                                                   */
/*                                                                                                            */
/* Description      : Read data from Data flash                                                                */
/*                                                                                                                     */
/* IN Parameter      : u32_fAddress : Address                                                                       */
/*                       p_fBuffer : Data Buffer                                                                      */
/*                       u32_fLength : Data length                                                                  */
/* OUT Parameter : void                                                                                            */
/*                                                                                                            */
/* Return Value      : void                                                                                              */
/*                                                                                                     */
/* Remarks          : None                                                                                */
/*************************************************************************************************/


INT8U DATAFLASH_SendByte(INT8U u8_lByte)
{
     INT8U u8_lData = 0x00;
  /* Loop while DR register in not emplty */
  while (SPI_GetFlagBitStatus(SPI1, SPI_TXE_INTERRUPT) == SPI_FLAGBIT_RESET);


  /* Send byte through the SPI1 peripheral */
  SPI_SendData(SPI1, u8_lByte);


  /* Wait to receive a byte */
  while (SPI_GetFlagBitStatus(SPI1, SPI_RXEN_INTERRUPT) == SPI_FLAGBIT_RESET);


  /* Data byte read from the SPI bus */
  u8_lData = SPI_ReceiveData(SPI1);


  return u8_lData;
}



void SPI_SendData(EN_SPI_DEVICE_SELECT_t en_lDev, INT8U Data)
{


     /* wait for Transmit buffer empty flag */
     while(!(p_stfSPIReg_all[en_lDev]->SPI_SR & SPI_SR_TXE_MASK));


     /* Write in the DR register the data to be sent */
     p_stfSPIReg_all[en_lDev]->SPI_DR = Data;


}

INT8U SPI_ReceiveData(EN_SPI_DEVICE_SELECT_t en_lDev)
{


     INT8U u8_lData;


     // wait until transmit complete
     while(!(p_stfSPIReg_all[en_lDev]->SPI_SR & SPI_SR_RXNE_MASK ));


     u8_lData = p_stfSPIReg_all[en_lDev]->SPI_DR;


     return u8_lData;
}

EN_SPI_FLAGBIT_STATUS SPI_GetFlagBitStatus(EN_SPI_DEVICE_SELECT_t en_lDev, INT16U u16_lFlagBit)
{
     EN_SPI_FLAGBIT_STATUS u32_lStatus = SPI_FLAGBIT_RESET;


     if (((p_stfSPIReg_all[en_lDev]->SPI_SR) & u16_lFlagBit ) != (INT16U)SPI_FLAGBIT_RESET)
       {
         /* SPI FLAG is set */
          u32_lStatus = SPI_FLAGBIT_SET;
       }
       else
       {
         /* SPI  FLAG is reset */
            u32_lStatus = SPI_FLAGBIT_RESET;
       }
       /* Return the SPI_I2S_FLAG status */
     return u32_lStatus;
}


Outcomes