1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
30
31
39
43
48
49
50
55
56
57
58
59
60
61
62
68
69
70
71
72
73
74
79
80
81
82
83
84
85
86
87
92
116
117
118
119
127
128
129
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
162
172
173
180
195
198
199
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
#define BLOCK_START_ADDR 0
#define BLOCKSIZE 512
#define NUM_OF_BLOCKS 2
#define BUFFER_WORDS_SIZE ((BLOCKSIZE * NUM_OF_BLOCKS) >> 2)
#define SD_OK 0
5 definesPrivate define
uint32_t aTxBuffer[BUFFER_WORDS_SIZE];
uint32_t aRxBuffer[BUFFER_WORDS_SIZE];
Private variables
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)
{
uint8_t SD_state = SD_OK;
SD_SetHint();
SD_state = BSP_SD_Init();
if(SD_state != SD_OK)
{
BSP_LCD_DisplayStringAt(5, 90, (uint8_t *)"SD Initialization", LEFT_MODE);
BSP_LCD_DisplayStringAt(5, 105, (uint8_t *)" FAILED", LEFT_MODE);
BSP_LCD_DisplayStringAt(5, 120, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (SD_state != SD_OK) { ... }
else
{
BSP_LCD_DisplayStringAt(5, 75, (uint8_t *)"SD Init : OK.", LEFT_MODE);
SD_state = BSP_SD_Erase(BLOCK_START_ADDR, NUM_OF_BLOCKS);
if(SD_state != SD_OK)
{
BSP_LCD_DisplayStringAt(5, 90, (uint8_t *)"SD ERASE : FAILED.", LEFT_MODE);
BSP_LCD_DisplayStringAt(5, 105, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (SD_state != SD_OK) { ... }
else
{
BSP_LCD_DisplayStringAt(5, 90, (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, 1);
if(SD_state != SD_OK)
{
BSP_LCD_DisplayStringAt(5, 105, (uint8_t *)"SD WRITE : FAILED.", LEFT_MODE);
BSP_LCD_DisplayStringAt(5, 120, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (SD_state != SD_OK) { ... }
else
{
BSP_LCD_DisplayStringAt(5, 105, (uint8_t *)"SD WRITE : OK.", LEFT_MODE);
SD_state = BSP_SD_ReadBlocks(aRxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, 1);
if(SD_state != SD_OK)
{
BSP_LCD_DisplayStringAt(5, 120, (uint8_t *)"SD READ : FAILED.", LEFT_MODE);
BSP_LCD_DisplayStringAt(5, 135, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (SD_state != SD_OK) { ... }
else
{
BSP_LCD_DisplayStringAt(5, 120, (uint8_t *)"SD READ : OK.", LEFT_MODE);
if(Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0)
{
BSP_LCD_DisplayStringAt(5, 135, (uint8_t *)"SD COMPARE : FAILED.", LEFT_MODE);
BSP_LCD_DisplayStringAt(5, 150, (uint8_t *)"SD Test Aborted.", LEFT_MODE);
}if (Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0) { ... }
else
{
BSP_LCD_DisplayStringAt(5, 135, (uint8_t *)"SD Test : OK.", LEFT_MODE);
BSP_LCD_DisplayStringAt(5, 150, (uint8_t *)"SD can be removed.", LEFT_MODE);
}else { ... }
}else { ... }
}else { ... }
}else { ... }
}else { ... }
while (1)
{
if(CheckForUserInput() > 0)
{
return;
}if (CheckForUserInput() > 0) { ... }
}while (1) { ... }
}{ ... }
/* ... */
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(), 55);
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
BSP_LCD_SetFont(&Font24);
BSP_LCD_SetFont(&Font12);
BSP_LCD_DisplayStringAt(0, 10, (uint8_t *)"SD Demo", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, 25, (uint8_t *)"Shows how to write", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, 40, (uint8_t *)"& read data on SD", CENTER_MODE);
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
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;
}{ ... }
/* ... */
/* ... */