STM32F10x_StdPeriph_Examples/SPI/SPI_FLASH/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    SPI/SPI_FLASH/main.c 
00004   * @author  MCD Application Team
00005   * @version V3.4.0
00006   * @date    10/15/2010
00007   * @brief   Main program body
00008   ******************************************************************************
00009   * @copy
00010   *
00011   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00012   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00013   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00014   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00015   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00016   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00017   *
00018   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00019   */ 
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32_eval.h"
00023 #include "stm32_eval_spi_flash.h"
00024 
00025 /** @addtogroup STM32F10x_StdPeriph_Examples
00026   * @{
00027   */
00028 
00029 /** @addtogroup SPI_FLASH
00030   * @{
00031   */ 
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00035 
00036 /* Private define ------------------------------------------------------------*/
00037 #define  FLASH_WriteAddress     0x700000
00038 #define  FLASH_ReadAddress      FLASH_WriteAddress
00039 #define  FLASH_SectorToErase    FLASH_WriteAddress
00040 
00041 #if defined(USE_STM32100B_EVAL) || defined(USE_STM32100E_EVAL)
00042   #define  sFLASH_ID       sFLASH_M25P128_ID
00043 #else
00044   #define  sFLASH_ID       sFLASH_M25P64_ID
00045 #endif
00046 
00047 #define  BufferSize (countof(Tx_Buffer)-1)
00048 
00049 /* Private macro -------------------------------------------------------------*/
00050 #define countof(a) (sizeof(a) / sizeof(*(a)))
00051 
00052 /* Private variables ---------------------------------------------------------*/
00053 uint8_t Tx_Buffer[] = "STM32F10x SPI Firmware Library Example: communication with an M25P SPI FLASH";
00054 uint8_t  Rx_Buffer[BufferSize];
00055 __IO uint8_t Index = 0x0;
00056 volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = PASSED;
00057 __IO uint32_t FlashID = 0;
00058 
00059 /* Private functions ---------------------------------------------------------*/
00060 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
00061 
00062 /**
00063   * @brief  Main program
00064   * @param  None
00065   * @retval None
00066   */
00067 int main(void)
00068 {
00069   /*!< At this stage the microcontroller clock setting is already configured, 
00070        this is done through SystemInit() function which is called from startup
00071        file (startup_stm32f10x_xx.s) before to branch to application main.
00072        To reconfigure the default setting of SystemInit() function, refer to
00073        system_stm32f10x.c file
00074      */     
00075        
00076   /* Initialize Leds mounted on STM3210X-EVAL board */
00077   STM_EVAL_LEDInit(LED1);
00078   STM_EVAL_LEDInit(LED2);
00079 
00080   /* Initialize the SPI FLASH driver */
00081   sFLASH_Init();
00082 
00083   /* Get SPI Flash ID */
00084   FlashID = sFLASH_ReadID();
00085   
00086   /* Check the SPI Flash ID */
00087   if (FlashID == sFLASH_ID)
00088   {
00089     /* OK: Turn on LD1 */
00090     STM_EVAL_LEDOn(LED1);
00091 
00092     /* Perform a write in the Flash followed by a read of the written data */
00093     /* Erase SPI FLASH Sector to write on */
00094     sFLASH_EraseSector(FLASH_SectorToErase);
00095 
00096     /* Write Tx_Buffer data to SPI FLASH memory */
00097     sFLASH_WriteBuffer(Tx_Buffer, FLASH_WriteAddress, BufferSize);
00098 
00099     /* Read data from SPI FLASH memory */
00100     sFLASH_ReadBuffer(Rx_Buffer, FLASH_ReadAddress, BufferSize);
00101 
00102     /* Check the corectness of written dada */
00103     TransferStatus1 = Buffercmp(Tx_Buffer, Rx_Buffer, BufferSize);
00104     /* TransferStatus1 = PASSED, if the transmitted and received data by SPI1
00105        are the same */
00106     /* TransferStatus1 = FAILED, if the transmitted and received data by SPI1
00107        are different */
00108 
00109     /* Perform an erase in the Flash followed by a read of the written data */
00110     /* Erase SPI FLASH Sector to write on */
00111     sFLASH_EraseSector(FLASH_SectorToErase);
00112 
00113     /* Read data from SPI FLASH memory */
00114     sFLASH_ReadBuffer(Rx_Buffer, FLASH_ReadAddress, BufferSize);
00115 
00116     /* Check the corectness of erasing operation dada */
00117     for (Index = 0; Index < BufferSize; Index++)
00118     {
00119       if (Rx_Buffer[Index] != 0xFF)
00120       {
00121         TransferStatus2 = FAILED;
00122       }
00123     }
00124     /* TransferStatus2 = PASSED, if the specified sector part is erased */
00125     /* TransferStatus2 = FAILED, if the specified sector part is not well erased */
00126   }
00127   else
00128   {
00129     /* Error: Turn on LD2 */
00130     STM_EVAL_LEDOn(LED2);
00131   }
00132   
00133   while (1)
00134   {}
00135 }
00136 
00137 /**
00138   * @brief  Compares two buffers.
00139   * @param  pBuffer1, pBuffer2: buffers to be compared.
00140   * @param  BufferLength: buffer's length
00141   * @retval PASSED: pBuffer1 identical to pBuffer2
00142   *         FAILED: pBuffer1 differs from pBuffer2
00143   */
00144 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
00145 {
00146   while (BufferLength--)
00147   {
00148     if (*pBuffer1 != *pBuffer2)
00149     {
00150       return FAILED;
00151     }
00152 
00153     pBuffer1++;
00154     pBuffer2++;
00155   }
00156 
00157   return PASSED;
00158 }
00159 
00160 #ifdef  USE_FULL_ASSERT
00161 
00162 /**
00163   * @brief  Reports the name of the source file and the source line number
00164   *         where the assert_param error has occurred.
00165   * @param  file: pointer to the source file name
00166   * @param  line: assert_param error line source number
00167   * @retval None
00168   */
00169 void assert_failed(uint8_t* file, uint32_t line)
00170 {
00171   /* User can add his own implementation to report the file name and line number,
00172      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00173 
00174   /* Infinite loop */
00175   while (1)
00176   {}
00177 }
00178 #endif
00179 /**
00180   * @}
00181   */ 
00182 
00183 /**
00184   * @}
00185   */ 
00186 
00187 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32