1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
30
31
36
39
46
47
48
53
68
69
70
75
76
77
78
79
80
86
91
92
93
94
108
109
110
115
116
117
118
119
120
130
136
137
138
143
144
145
146
147
148
149
150
162
163
164
165
166
167
168
169
170
175
176
177
178
179
180
181
182
183
184
185
186
191
215
216
217
218
219
220
221
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
259
269
270
277
292
295
296
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
#define BLOCK_START_ADDR 0
#define NUM_OF_BLOCKS 5
#define BUFFER_WORDS_SIZE ((BLOCKSIZE * NUM_OF_BLOCKS) >> 2)
Private define
uint32_t aTxBuffer[BUFFER_WORDS_SIZE];
uint32_t aRxBuffer[BUFFER_WORDS_SIZE];Private variables
void SD_main_test(void);
void SD_Detection(void);
static void SD_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
/* ... */
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_exti_demo (void)
{
SD_main_test();
if(BSP_SD_IsDetected() != SD_PRESENT)
{
BSP_SD_Init();
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAt(20, BSP_LCD_GetYSize()-30, (uint8_t *)"SD Not Connected", LEFT_MODE);
}if (BSP_SD_IsDetected() != SD_PRESENT) { ... }
else
{
BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
BSP_LCD_DisplayStringAt(20, BSP_LCD_GetYSize()-30, (uint8_t *)"SD Connected ", LEFT_MODE);
}else { ... }
BSP_SD_ITConfig();
while (1)
{
if (exti2_received == 1)
{
exti2_received = 0;
SD_Detection();
}if (exti2_received == 1) { ... }
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(20, BSP_LCD_GetYSize()-30, (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(20, BSP_LCD_GetYSize()-30, (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(20, 100, (uint8_t *)"SD shall be inserted before running test", LEFT_MODE);
}if (SD_state == MSD_ERROR_SD_NOT_PRESENT) { ... }
else
{
BSP_LCD_DisplayStringAt(20, 100, (uint8_t *)"SD Initialization : FAIL.", LEFT_MODE);
}else { ... }
BSP_LCD_DisplayStringAt(20, 115, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (SD_state !=MSD_OK) { ... }
else
{
BSP_LCD_DisplayStringAt(20, 100, (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)){}
if(SD_state !=MSD_OK)
{
BSP_LCD_DisplayStringAt(20, 115, (uint8_t *)"SD ERASE : FAILED.", LEFT_MODE);
BSP_LCD_DisplayStringAt(20, 130, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (SD_state !=MSD_OK) { ... }
else
{
BSP_LCD_DisplayStringAt(20, 115, (uint8_t *)"SD ERASE : OK.", LEFT_MODE);
BSP_LCD_DisplayStringAt(20, 175, (uint8_t *)"SD can be removed.", LEFT_MODE);
Fill_Buffer(aTxBuffer, BUFFER_WORDS_SIZE, 0x22FF);
SD_state = BSP_SD_WriteBlocks(aTxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, SD_DATATIMEOUT);
while((BSP_SD_GetCardState() != SD_TRANSFER_OK)){}
if(SD_state !=MSD_OK)
{
BSP_LCD_DisplayStringAt(20, 130, (uint8_t *)"SD WRITE : FAILED.", LEFT_MODE);
BSP_LCD_DisplayStringAt(20, 145, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (SD_state !=MSD_OK) { ... }
else
{
BSP_LCD_DisplayStringAt(20, 130, (uint8_t *)"SD WRITE : OK.", LEFT_MODE);
SD_state = BSP_SD_ReadBlocks(aRxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, SD_DATATIMEOUT);
if(SD_state !=MSD_OK)
{
BSP_LCD_DisplayStringAt(20, 145, (uint8_t *)"SD READ : FAILED.", LEFT_MODE);
BSP_LCD_DisplayStringAt(20, 160, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (SD_state !=MSD_OK) { ... }
else
{
BSP_LCD_DisplayStringAt(20, 145, (uint8_t *)"SD READ : OK.", LEFT_MODE);
if(Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0)
{
BSP_LCD_DisplayStringAt(20, 160, (uint8_t *)"SD COMPARE : FAILED.", LEFT_MODE);
BSP_LCD_DisplayStringAt(20, 175, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0) { ... }
else
{
BSP_LCD_DisplayStringAt(20, 160, (uint8_t *)"SD Test : OK.", LEFT_MODE);
BSP_LCD_DisplayStringAt(20, 175, (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(), 80);
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
BSP_LCD_SetFont(&Font24);
BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"SD", CENTER_MODE);
BSP_LCD_SetFont(&Font12);
BSP_LCD_DisplayStringAt(0, 30, (uint8_t *)"This example shows how to write", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, 45, (uint8_t *)"and read data on the microSD and also", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, 60, (uint8_t *)"how to detect the presence of the card", CENTER_MODE);
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_DrawRect(10, 90, BSP_LCD_GetXSize() - 20, BSP_LCD_GetYSize()- 100);
BSP_LCD_DrawRect(11, 91, BSP_LCD_GetXSize() - 22, BSP_LCD_GetYSize()- 102);
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;
}{ ... }
/* ... */
/* ... */