comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/SPI/SPI_FLASH/main.c @ 0:c59513fd84fb

Initial commit of STM32 test code.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 03 Oct 2011 21:19:15 +1030
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c59513fd84fb
1 /**
2 ******************************************************************************
3 * @file SPI/SPI_FLASH/main.c
4 * @author MCD Application Team
5 * @version V3.5.0
6 * @date 08-April-2011
7 * @brief Main program body
8 ******************************************************************************
9 * @attention
10 *
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17 *
18 * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
19 ******************************************************************************
20 */
21
22 /* Includes ------------------------------------------------------------------*/
23 #include "stm32_eval.h"
24 #include "stm32_eval_spi_flash.h"
25
26 /** @addtogroup STM32F10x_StdPeriph_Examples
27 * @{
28 */
29
30 /** @addtogroup SPI_FLASH
31 * @{
32 */
33
34 /* Private typedef -----------------------------------------------------------*/
35 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
36
37 /* Private define ------------------------------------------------------------*/
38 #define FLASH_WriteAddress 0x700000
39 #define FLASH_ReadAddress FLASH_WriteAddress
40 #define FLASH_SectorToErase FLASH_WriteAddress
41
42 #if defined(USE_STM32100B_EVAL) || defined(USE_STM32100E_EVAL)
43 #define sFLASH_ID sFLASH_M25P128_ID
44 #else
45 #define sFLASH_ID sFLASH_M25P64_ID
46 #endif
47
48 #define BufferSize (countof(Tx_Buffer)-1)
49
50 /* Private macro -------------------------------------------------------------*/
51 #define countof(a) (sizeof(a) / sizeof(*(a)))
52
53 /* Private variables ---------------------------------------------------------*/
54 uint8_t Tx_Buffer[] = "STM32F10x SPI Firmware Library Example: communication with an M25P SPI FLASH";
55 uint8_t Rx_Buffer[BufferSize];
56 __IO uint8_t Index = 0x0;
57 volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = PASSED;
58 __IO uint32_t FlashID = 0;
59
60 /* Private functions ---------------------------------------------------------*/
61 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
62
63 /**
64 * @brief Main program
65 * @param None
66 * @retval None
67 */
68 int main(void)
69 {
70 /*!< At this stage the microcontroller clock setting is already configured,
71 this is done through SystemInit() function which is called from startup
72 file (startup_stm32f10x_xx.s) before to branch to application main.
73 To reconfigure the default setting of SystemInit() function, refer to
74 system_stm32f10x.c file
75 */
76
77 /* Initialize Leds mounted on STM3210X-EVAL board */
78 STM_EVAL_LEDInit(LED1);
79 STM_EVAL_LEDInit(LED2);
80
81 /* Initialize the SPI FLASH driver */
82 sFLASH_Init();
83
84 /* Get SPI Flash ID */
85 FlashID = sFLASH_ReadID();
86
87 /* Check the SPI Flash ID */
88 if (FlashID == sFLASH_ID)
89 {
90 /* OK: Turn on LD1 */
91 STM_EVAL_LEDOn(LED1);
92
93 /* Perform a write in the Flash followed by a read of the written data */
94 /* Erase SPI FLASH Sector to write on */
95 sFLASH_EraseSector(FLASH_SectorToErase);
96
97 /* Write Tx_Buffer data to SPI FLASH memory */
98 sFLASH_WriteBuffer(Tx_Buffer, FLASH_WriteAddress, BufferSize);
99
100 /* Read data from SPI FLASH memory */
101 sFLASH_ReadBuffer(Rx_Buffer, FLASH_ReadAddress, BufferSize);
102
103 /* Check the correctness of written dada */
104 TransferStatus1 = Buffercmp(Tx_Buffer, Rx_Buffer, BufferSize);
105 /* TransferStatus1 = PASSED, if the transmitted and received data by SPI1
106 are the same */
107 /* TransferStatus1 = FAILED, if the transmitted and received data by SPI1
108 are different */
109
110 /* Perform an erase in the Flash followed by a read of the written data */
111 /* Erase SPI FLASH Sector to write on */
112 sFLASH_EraseSector(FLASH_SectorToErase);
113
114 /* Read data from SPI FLASH memory */
115 sFLASH_ReadBuffer(Rx_Buffer, FLASH_ReadAddress, BufferSize);
116
117 /* Check the correctness of erasing operation dada */
118 for (Index = 0; Index < BufferSize; Index++)
119 {
120 if (Rx_Buffer[Index] != 0xFF)
121 {
122 TransferStatus2 = FAILED;
123 }
124 }
125 /* TransferStatus2 = PASSED, if the specified sector part is erased */
126 /* TransferStatus2 = FAILED, if the specified sector part is not well erased */
127 }
128 else
129 {
130 /* Error: Turn on LD2 */
131 STM_EVAL_LEDOn(LED2);
132 }
133
134 while (1)
135 {}
136 }
137
138 /**
139 * @brief Compares two buffers.
140 * @param pBuffer1, pBuffer2: buffers to be compared.
141 * @param BufferLength: buffer's length
142 * @retval PASSED: pBuffer1 identical to pBuffer2
143 * FAILED: pBuffer1 differs from pBuffer2
144 */
145 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
146 {
147 while (BufferLength--)
148 {
149 if (*pBuffer1 != *pBuffer2)
150 {
151 return FAILED;
152 }
153
154 pBuffer1++;
155 pBuffer2++;
156 }
157
158 return PASSED;
159 }
160
161 #ifdef USE_FULL_ASSERT
162
163 /**
164 * @brief Reports the name of the source file and the source line number
165 * where the assert_param error has occurred.
166 * @param file: pointer to the source file name
167 * @param line: assert_param error line source number
168 * @retval None
169 */
170 void assert_failed(uint8_t* file, uint32_t line)
171 {
172 /* User can add his own implementation to report the file name and line number,
173 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
174
175 /* Infinite loop */
176 while (1)
177 {}
178 }
179
180 #endif
181 /**
182 * @}
183 */
184
185 /**
186 * @}
187 */
188
189 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/