1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
30
31
37
40
48
53
54
55
60
75
76
81
97
98
103
104
105
106
107
113
118
119
120
121
136
137
138
143
144
145
146
147
148
157
163
164
165
170
171
172
173
174
175
176
177
189
190
191
192
193
194
195
196
199
200
205
206
207
208
209
210
211
212
213
214
217
218
223
224
225
226
227
228
229
232
233
238
252
253
254
255
256
257
262
263
264
265
266
267
268
269
281
282
283
284
285
286
287
288
291
292
297
298
299
300
301
302
303
304
305
306
309
310
311
312
315
316
321
322
323
324
325
326
327
330
331
332
333
336
337
342
356
357
358
359
360
361
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
422
432
433
440
455
456
461
465
466
471
475
476
479
480
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
#define BLOCK_START_ADDR 0
#define NUM_OF_BLOCKS 5
#define BUFFER_WORDS_SIZE ((BLOCKSIZE * NUM_OF_BLOCKS) >> 2)
#define HEADBAND_HEIGHT 64
Private define
uint32_t aTxBuffer[BUFFER_WORDS_SIZE];
uint32_t aRxBuffer[BUFFER_WORDS_SIZE];Private variables
void SD_main_test(void);
void SD_DMA_test(void);
void SD_Detection(void);
static void SD_SetHint(void);
static void SD_DMA_SetHint(void);
static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLenght, uint32_t uwOffset);
static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength);Private function prototypes
extern uint8_t SDDetectIT;
__IO uint32_t SDWriteStatus = 0, SDReadStatus = 0;
Global extern variables
/* ... */
void SD_demo (void)
{
SD_main_test();
while (1)
{
SD_Detection();
if(CheckForUserInput() > 0)
{
BSP_SD_DeInit();
return;
}if (CheckForUserInput() > 0) { ... }
}while (1) { ... }
}{ ... }
/* ... */
void SD_DMA_demo (void)
{
SD_DMA_test();
while (1)
{
SD_Detection();
if(CheckForUserInput() > 0)
{
BSP_SD_DeInit();
SdmmcTest = 0;
return;
}if (CheckForUserInput() > 0) { ... }
}while (1) { ... }
}{ ... }
/* ... */
void SD_exti_demo (void)
{
SD_main_test();
if(BSP_SD_IsDetected() != SD_PRESENT)
{
BSP_SD_Init();
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAt(5, BSP_LCD_GetYSize()-15, (uint8_t *)"SD Not Connected", LEFT_MODE);
}if (BSP_SD_IsDetected() != SD_PRESENT) { ... }
else
{
BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
BSP_LCD_DisplayStringAt(5, BSP_LCD_GetYSize()-15, (uint8_t *)"SD Connected ", LEFT_MODE);
}else { ... }
BSP_SD_ITConfig();
while (1)
{
if (SDDetectIT != 0)
{
SD_Detection();
SDDetectIT = 0;
}if (SDDetectIT != 0) { ... }
if(CheckForUserInput() > 0)
{
BSP_SD_DeInit();
return;
}if (CheckForUserInput() > 0) { ... }
}while (1) { ... }
}{ ... }
/* ... */
void SD_Detection(void)
{
static uint8_t prev_status = 2;
if(BSP_SD_IsDetected() != SD_PRESENT)
{
if(prev_status != SD_NOT_PRESENT)
{
prev_status = SD_NOT_PRESENT;
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAt(5, BSP_LCD_GetYSize()-15, (uint8_t *)"SD Not Connected", LEFT_MODE);
}if (prev_status != SD_NOT_PRESENT) { ... }
}if (BSP_SD_IsDetected() != SD_PRESENT) { ... }
else if (prev_status != SD_PRESENT)
{
prev_status = SD_PRESENT;
BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
BSP_LCD_DisplayStringAt(5, BSP_LCD_GetYSize()-15, (uint8_t *)"SD Connected ", LEFT_MODE);
}else if (prev_status != SD_PRESENT) { ... }
}{ ... }
/* ... */
void SD_main_test (void)
{
uint8_t SD_state = MSD_OK;
SD_SetHint();
SD_state = BSP_SD_Init();
if(SD_state != MSD_OK)
{
if(SD_state == MSD_ERROR_SD_NOT_PRESENT)
{
BSP_LCD_DisplayStringAt(5, 70, (uint8_t *)"SD shall be inserted", LEFT_MODE);
}if (SD_state == MSD_ERROR_SD_NOT_PRESENT) { ... }
else
{
BSP_LCD_DisplayStringAt(5, 70, (uint8_t *)"SD Initialization : FAIL.", LEFT_MODE);
}else { ... }
BSP_LCD_DisplayStringAt(5, 85, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (SD_state != MSD_OK) { ... }
else
{
BSP_LCD_DisplayStringAt(5, 70, (uint8_t *)"SD Initialization : OK.", LEFT_MODE);
SD_state = BSP_SD_Erase(BLOCK_START_ADDR, NUM_OF_BLOCKS);
while((BSP_SD_GetCardState() != SD_TRANSFER_OK))
{
}while ((BSP_SD_GetCardState() != SD_TRANSFER_OK)) { ... }
if(SD_state != MSD_OK)
{
BSP_LCD_DisplayStringAt(5, 85, (uint8_t *)"SD ERASE : FAILED.", LEFT_MODE);
BSP_LCD_DisplayStringAt(5, 100, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (SD_state != MSD_OK) { ... }
else
{
BSP_LCD_DisplayStringAt(5, 85, (uint8_t *)"SD ERASE : OK.", LEFT_MODE);
Fill_Buffer(aTxBuffer, BUFFER_WORDS_SIZE, 0x22FF);
SD_state = BSP_SD_WriteBlocks(aTxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, 1000);
while((BSP_SD_GetCardState() != SD_TRANSFER_OK))
{
}while ((BSP_SD_GetCardState() != SD_TRANSFER_OK)) { ... }
if(SD_state != MSD_OK)
{
BSP_LCD_DisplayStringAt(5, 100, (uint8_t *)"SD WRITE : FAILED.", LEFT_MODE);
BSP_LCD_DisplayStringAt(5, 115, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (SD_state != MSD_OK) { ... }
else
{
BSP_LCD_DisplayStringAt(5, 100, (uint8_t *)"SD WRITE : OK.", LEFT_MODE);
SD_state = BSP_SD_ReadBlocks(aRxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, 1000);
while((BSP_SD_GetCardState() != SD_TRANSFER_OK))
{
}while ((BSP_SD_GetCardState() != SD_TRANSFER_OK)) { ... }
if(SD_state != MSD_OK)
{
BSP_LCD_DisplayStringAt(5, 115, (uint8_t *)"SD READ : FAILED.", LEFT_MODE);
BSP_LCD_DisplayStringAt(5, 130, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (SD_state != MSD_OK) { ... }
else
{
BSP_LCD_DisplayStringAt(5, 115, (uint8_t *)"SD READ : OK.", LEFT_MODE);
if(Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0)
{
BSP_LCD_DisplayStringAt(5, 130, (uint8_t *)"SD COMPARE : FAILED.", LEFT_MODE);
BSP_LCD_DisplayStringAt(5, 145, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0) { ... }
else
{
BSP_LCD_DisplayStringAt(5, 130, (uint8_t *)"SD Test : OK.", LEFT_MODE);
BSP_LCD_DisplayStringAt(5, 145, (uint8_t *)"SD can be removed.", LEFT_MODE);
}else { ... }
}else { ... }
}else { ... }
}else { ... }
}else { ... }
}{ ... }
/* ... */
void SD_DMA_test (void)
{
uint8_t SD_state = MSD_OK;
SdmmcTest = 1;
SD_DMA_SetHint();
SD_state = BSP_SD_Init();
if(SD_state != MSD_OK)
{
if(SD_state == MSD_ERROR_SD_NOT_PRESENT)
{
BSP_LCD_DisplayStringAt(5, 70, (uint8_t *)"SD shall be inserted", LEFT_MODE);
}if (SD_state == MSD_ERROR_SD_NOT_PRESENT) { ... }
else
{
BSP_LCD_DisplayStringAt(5, 70, (uint8_t *)"SD Initialization : FAIL.", LEFT_MODE);
}else { ... }
BSP_LCD_DisplayStringAt(5, 85, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (SD_state != MSD_OK) { ... }
else
{
BSP_LCD_DisplayStringAt(5, 70, (uint8_t *)"SD Initialization : OK.", LEFT_MODE);
SD_state = BSP_SD_Erase(BLOCK_START_ADDR, NUM_OF_BLOCKS);
while((BSP_SD_GetCardState() != SD_TRANSFER_OK))
{
}while ((BSP_SD_GetCardState() != SD_TRANSFER_OK)) { ... }
if(SD_state != MSD_OK)
{
BSP_LCD_DisplayStringAt(5, 85, (uint8_t *)"SD ERASE : FAILED.", LEFT_MODE);
BSP_LCD_DisplayStringAt(5, 100, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (SD_state != MSD_OK) { ... }
else
{
BSP_LCD_DisplayStringAt(5, 85, (uint8_t *)"SD ERASE : OK.", LEFT_MODE);
Fill_Buffer(aTxBuffer, BUFFER_WORDS_SIZE, 0x22FF);
SD_state = BSP_SD_WriteBlocks_DMA(aTxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS);
while((SDWriteStatus == 0))
{
}while ((SDWriteStatus == 0)) { ... }
SDWriteStatus = 0;
while((BSP_SD_GetCardState() != SD_TRANSFER_OK))
{
}while ((BSP_SD_GetCardState() != SD_TRANSFER_OK)) { ... }
if(SD_state != MSD_OK)
{
BSP_LCD_DisplayStringAt(5, 100, (uint8_t *)"SD WRITE : FAILED.", LEFT_MODE);
BSP_LCD_DisplayStringAt(5, 115, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (SD_state != MSD_OK) { ... }
else
{
BSP_LCD_DisplayStringAt(5, 100, (uint8_t *)"SD WRITE : OK.", LEFT_MODE);
SD_state = BSP_SD_ReadBlocks_DMA(aRxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS);
while(SDReadStatus == 0)
{
}while (SDReadStatus == 0) { ... }
SDReadStatus = 0;
while(BSP_SD_GetCardState() != SD_TRANSFER_OK)
{
}while (BSP_SD_GetCardState() != SD_TRANSFER_OK) { ... }
if(SD_state != MSD_OK)
{
BSP_LCD_DisplayStringAt(5, 115, (uint8_t *)"SD READ : FAILED.", LEFT_MODE);
BSP_LCD_DisplayStringAt(5, 130, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (SD_state != MSD_OK) { ... }
else
{
BSP_LCD_DisplayStringAt(5, 115, (uint8_t *)"SD READ : OK.", LEFT_MODE);
if(Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0)
{
BSP_LCD_DisplayStringAt(5, 130, (uint8_t *)"SD COMPARE : FAILED.", LEFT_MODE);
BSP_LCD_DisplayStringAt(5, 145, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0) { ... }
else
{
BSP_LCD_DisplayStringAt(5, 130, (uint8_t *)"SD Test : OK.", LEFT_MODE);
BSP_LCD_DisplayStringAt(5, 145, (uint8_t *)"SD can be removed.", LEFT_MODE);
}else { ... }
}else { ... }
}else { ... }
}else { ... }
}else { ... }
}{ ... }
/* ... */
static void SD_SetHint(void)
{
BSP_LCD_Clear(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), HEADBAND_HEIGHT);
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
BSP_LCD_SetFont(&Font16);
BSP_LCD_DisplayStringAt(0, 1, (uint8_t *)"SD", CENTER_MODE);
BSP_LCD_SetFont(&Font12);
BSP_LCD_DisplayStringAt(0, 20, (uint8_t *)"This example shows how to", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, 35, (uint8_t *)"write and read on microSD", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, 50, (uint8_t *)"Detect the card presence", CENTER_MODE);
BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
}{ ... }
/* ... */
static void SD_DMA_SetHint(void)
{
BSP_LCD_Clear(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), HEADBAND_HEIGHT);
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
BSP_LCD_SetFont(&Font16);
BSP_LCD_DisplayStringAt(0, 1, (uint8_t *)"SD DMA Mode", CENTER_MODE);
BSP_LCD_SetFont(&Font12);
BSP_LCD_DisplayStringAt(0, 20, (uint8_t *)"This example shows how to", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, 35, (uint8_t *)"write and read on microSD", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, 50, (uint8_t *)"Detect the card presence", CENTER_MODE);
BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
}{ ... }
/* ... */
static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLenght, uint32_t uwOffset)
{
uint32_t tmpIndex = 0;
for (tmpIndex = 0; tmpIndex < uwBufferLenght; tmpIndex++ )
{
pBuffer[tmpIndex] = tmpIndex + uwOffset;
}for (tmpIndex = 0; tmpIndex < uwBufferLenght; tmpIndex++) { ... }
}{ ... }
/* ... */
static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength)
{
while (BufferLength--)
{
if (*pBuffer1 != *pBuffer2)
{
return 1;
}if (*pBuffer1 != *pBuffer2) { ... }
pBuffer1++;
pBuffer2++;
}while (BufferLength--) { ... }
return 0;
}{ ... }
/* ... */
void BSP_SD_WriteCpltCallback()
{
SDWriteStatus = 1;
}{ ... }
/* ... */
void BSP_SD_ReadCpltCallback()
{
SDReadStatus = 1;
}{ ... }
/* ... */
/* ... */