comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/SDIO/uSDCard/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 SDIO/uSDCard/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 "stm32f10x.h"
24 #include "stm32_eval_sdio_sd.h"
25
26 /** @addtogroup STM32F10x_StdPeriph_Examples
27 * @{
28 */
29
30 /** @addtogroup SDIO_uSDCard
31 * @{
32 */
33
34 /* Private typedef -----------------------------------------------------------*/
35 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
36
37 /* Private define ------------------------------------------------------------*/
38 #define BLOCK_SIZE 512 /* Block Size in Bytes */
39
40 #define NUMBER_OF_BLOCKS 32 /* For Multi Blocks operation (Read/Write) */
41 #define MULTI_BUFFER_SIZE (BLOCK_SIZE * NUMBER_OF_BLOCKS)
42
43 #define SD_OPERATION_ERASE 0
44 #define SD_OPERATION_BLOCK 1
45 #define SD_OPERATION_MULTI_BLOCK 2
46 #define SD_OPERATION_END 3
47
48 /* Private macro -------------------------------------------------------------*/
49 /* Private variables ---------------------------------------------------------*/
50 uint8_t Buffer_Block_Tx[BLOCK_SIZE], Buffer_Block_Rx[BLOCK_SIZE];
51 uint8_t Buffer_MultiBlock_Tx[MULTI_BUFFER_SIZE], Buffer_MultiBlock_Rx[MULTI_BUFFER_SIZE];
52 volatile TestStatus EraseStatus = FAILED, TransferStatus1 = FAILED, TransferStatus2 = FAILED;
53 SD_Error Status = SD_OK;
54 __IO uint32_t SDCardOperation = SD_OPERATION_ERASE;
55
56 /* Private function prototypes -----------------------------------------------*/
57 void NVIC_Configuration(void);
58 void SD_EraseTest(void);
59 void SD_SingleBlockTest(void);
60 void SD_MultiBlockTest(void);
61 void Fill_Buffer(uint8_t *pBuffer, uint32_t BufferLength, uint32_t Offset);
62 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint32_t BufferLength);
63 TestStatus eBuffercmp(uint8_t* pBuffer, uint32_t BufferLength);
64
65 /* Private functions ---------------------------------------------------------*/
66
67 /**
68 * @brief Main program.
69 * @param None
70 * @retval None
71 */
72 int main(void)
73 {
74 /*!< At this stage the microcontroller clock setting is already configured,
75 this is done through SystemInit() function which is called from startup
76 file (startup_stm32f10x_xx.s) before to branch to application main.
77 To reconfigure the default setting of SystemInit() function, refer to
78 system_stm32f10x.c file
79 */
80
81 /* Initialize LEDs available on STM3210X-EVAL board *************************/
82 STM_EVAL_LEDInit(LED1);
83 STM_EVAL_LEDInit(LED2);
84 STM_EVAL_LEDInit(LED3);
85 STM_EVAL_LEDInit(LED4);
86
87 /* Interrupt Config */
88 NVIC_Configuration();
89
90 /*------------------------------ SD Init ---------------------------------- */
91 if((Status = SD_Init()) != SD_OK)
92 {
93 STM_EVAL_LEDOn(LED4);
94 }
95
96 while((Status == SD_OK) && (SDCardOperation != SD_OPERATION_END) && (SD_Detect()== SD_PRESENT))
97 {
98 switch(SDCardOperation)
99 {
100 /*-------------------------- SD Erase Test ---------------------------- */
101 case (SD_OPERATION_ERASE):
102 {
103 SD_EraseTest();
104 SDCardOperation = SD_OPERATION_BLOCK;
105 break;
106 }
107 /*-------------------------- SD Single Block Test --------------------- */
108 case (SD_OPERATION_BLOCK):
109 {
110 SD_SingleBlockTest();
111 SDCardOperation = SD_OPERATION_MULTI_BLOCK;
112 break;
113 }
114 /*-------------------------- SD Multi Blocks Test --------------------- */
115 case (SD_OPERATION_MULTI_BLOCK):
116 {
117 SD_MultiBlockTest();
118 SDCardOperation = SD_OPERATION_END;
119 break;
120 }
121 }
122 }
123
124 /* Infinite loop */
125 while (1)
126 {}
127 }
128
129 /**
130 * @brief Configures SDIO IRQ channel.
131 * @param None
132 * @retval None
133 */
134 void NVIC_Configuration(void)
135 {
136 NVIC_InitTypeDef NVIC_InitStructure;
137
138 /* Configure the NVIC Preemption Priority Bits */
139 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
140
141 NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
142 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
143 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
144 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
145 NVIC_Init(&NVIC_InitStructure);
146 }
147
148 /**
149 * @brief Tests the SD card erase operation.
150 * @param None
151 * @retval None
152 */
153 void SD_EraseTest(void)
154 {
155 /*------------------- Block Erase ------------------------------------------*/
156 if (Status == SD_OK)
157 {
158 /* Erase NumberOfBlocks Blocks of WRITE_BL_LEN(512 Bytes) */
159 Status = SD_Erase(0x00, (BLOCK_SIZE * NUMBER_OF_BLOCKS));
160 }
161
162 if (Status == SD_OK)
163 {
164 Status = SD_ReadMultiBlocks(Buffer_MultiBlock_Rx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS);
165
166 /* Check if the Transfer is finished */
167 Status = SD_WaitReadOperation();
168
169 /* Wait until end of DMA transfer */
170 while(SD_GetStatus() != SD_TRANSFER_OK);
171 }
172
173 /* Check the correctness of erased blocks */
174 if (Status == SD_OK)
175 {
176 EraseStatus = eBuffercmp(Buffer_MultiBlock_Rx, MULTI_BUFFER_SIZE);
177 }
178
179 if(EraseStatus == PASSED)
180 {
181 STM_EVAL_LEDOn(LED1);
182 }
183 else
184 {
185 STM_EVAL_LEDOff(LED1);
186 STM_EVAL_LEDOn(LED4);
187 }
188 }
189
190 /**
191 * @brief Tests the SD card Single Blocks operations.
192 * @param None
193 * @retval None
194 */
195 void SD_SingleBlockTest(void)
196 {
197 /*------------------- Block Read/Write --------------------------*/
198 /* Fill the buffer to send */
199 Fill_Buffer(Buffer_Block_Tx, BLOCK_SIZE, 0x320F);
200
201 if (Status == SD_OK)
202 {
203 /* Write block of 512 bytes on address 0 */
204 Status = SD_WriteBlock(Buffer_Block_Tx, 0x00, BLOCK_SIZE);
205 /* Check if the Transfer is finished */
206 Status = SD_WaitWriteOperation();
207 while(SD_GetStatus() != SD_TRANSFER_OK);
208 }
209
210 if (Status == SD_OK)
211 {
212 /* Read block of 512 bytes from address 0 */
213 Status = SD_ReadBlock(Buffer_Block_Rx, 0x00, BLOCK_SIZE);
214 /* Check if the Transfer is finished */
215 Status = SD_WaitReadOperation();
216 while(SD_GetStatus() != SD_TRANSFER_OK);
217 }
218
219 /* Check the correctness of written data */
220 if (Status == SD_OK)
221 {
222 TransferStatus1 = Buffercmp(Buffer_Block_Tx, Buffer_Block_Rx, BLOCK_SIZE);
223 }
224
225 if(TransferStatus1 == PASSED)
226 {
227 STM_EVAL_LEDOn(LED2);
228 }
229 else
230 {
231 STM_EVAL_LEDOff(LED2);
232 STM_EVAL_LEDOn(LED4);
233 }
234 }
235
236 /**
237 * @brief Tests the SD card Multiple Blocks operations.
238 * @param None
239 * @retval None
240 */
241 void SD_MultiBlockTest(void)
242 {
243 /*--------------- Multiple Block Read/Write ---------------------*/
244 /* Fill the buffer to send */
245 Fill_Buffer(Buffer_MultiBlock_Tx, MULTI_BUFFER_SIZE, 0x0);
246
247 if (Status == SD_OK)
248 {
249 /* Write multiple block of many bytes on address 0 */
250 Status = SD_WriteMultiBlocks(Buffer_MultiBlock_Tx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS);
251 /* Check if the Transfer is finished */
252 Status = SD_WaitWriteOperation();
253 while(SD_GetStatus() != SD_TRANSFER_OK);
254 }
255
256 if (Status == SD_OK)
257 {
258 /* Read block of many bytes from address 0 */
259 Status = SD_ReadMultiBlocks(Buffer_MultiBlock_Rx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS);
260 /* Check if the Transfer is finished */
261 Status = SD_WaitReadOperation();
262 while(SD_GetStatus() != SD_TRANSFER_OK);
263 }
264
265 /* Check the correctness of written data */
266 if (Status == SD_OK)
267 {
268 TransferStatus2 = Buffercmp(Buffer_MultiBlock_Tx, Buffer_MultiBlock_Rx, MULTI_BUFFER_SIZE);
269 }
270
271 if(TransferStatus2 == PASSED)
272 {
273 STM_EVAL_LEDOn(LED3);
274 }
275 else
276 {
277 STM_EVAL_LEDOff(LED3);
278 STM_EVAL_LEDOn(LED4);
279 }
280 }
281
282 /**
283 * @brief Compares two buffers.
284 * @param pBuffer1, pBuffer2: buffers to be compared.
285 * @param BufferLength: buffer's length
286 * @retval PASSED: pBuffer1 identical to pBuffer2
287 * FAILED: pBuffer1 differs from pBuffer2
288 */
289 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint32_t BufferLength)
290 {
291 while (BufferLength--)
292 {
293 if (*pBuffer1 != *pBuffer2)
294 {
295 return FAILED;
296 }
297
298 pBuffer1++;
299 pBuffer2++;
300 }
301
302 return PASSED;
303 }
304
305 /**
306 * @brief Fills buffer with user predefined data.
307 * @param pBuffer: pointer on the Buffer to fill
308 * @param BufferLength: size of the buffer to fill
309 * @param Offset: first value to fill on the Buffer
310 * @retval None
311 */
312 void Fill_Buffer(uint8_t *pBuffer, uint32_t BufferLength, uint32_t Offset)
313 {
314 uint16_t index = 0;
315
316 /* Put in global buffer same values */
317 for (index = 0; index < BufferLength; index++)
318 {
319 pBuffer[index] = index + Offset;
320 }
321 }
322
323 /**
324 * @brief Checks if a buffer has all its values are equal to zero.
325 * @param pBuffer: buffer to be compared.
326 * @param BufferLength: buffer's length
327 * @retval PASSED: pBuffer values are zero
328 * FAILED: At least one value from pBuffer buffer is different from zero.
329 */
330 TestStatus eBuffercmp(uint8_t* pBuffer, uint32_t BufferLength)
331 {
332 while (BufferLength--)
333 {
334 /* In some SD Cards the erased state is 0xFF, in others it's 0x00 */
335 if ((*pBuffer != 0xFF) && (*pBuffer != 0x00))
336 {
337 return FAILED;
338 }
339
340 pBuffer++;
341 }
342
343 return PASSED;
344 }
345
346 #ifdef USE_FULL_ASSERT
347 /**
348 * @brief Reports the name of the source file and the source line number
349 * where the assert_param error has occurred.
350 * @param file: pointer to the source file name
351 * @param line: assert_param error line source number
352 * @retval None
353 */
354 void assert_failed(uint8_t* file, uint32_t line)
355 {
356 /* User can add his own implementation to report the file name and line number,
357 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
358
359 /* Infinite loop */
360 while (1)
361 {}
362 }
363 #endif
364
365 /**
366 * @}
367 */
368
369 /**
370 * @}
371 */
372
373 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/