SDCard (with SDMMC interface) demo using ST BSP driver.
main.cpp@1:2b4442b30a9e, 2018-04-26 (annotated)
- Committer:
- bcostm
- Date:
- Thu Apr 26 10:57:47 2018 +0200
- Revision:
- 1:2b4442b30a9e
- Parent:
- 0:c1aa0132b74f
add mbed and BSP libraries
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 0:c1aa0132b74f | 1 | #include "mbed.h" |
bcostm | 0:c1aa0132b74f | 2 | #include "stm32l496g_discovery.h" |
bcostm | 0:c1aa0132b74f | 3 | #include "stm32l496g_discovery_lcd.h" |
bcostm | 0:c1aa0132b74f | 4 | #include "stm32l496g_discovery_sd.h" |
bcostm | 0:c1aa0132b74f | 5 | |
bcostm | 0:c1aa0132b74f | 6 | #define NUM_OF_BLOCKS 5 |
bcostm | 0:c1aa0132b74f | 7 | #define BLOCK_START_ADDR 0 |
bcostm | 0:c1aa0132b74f | 8 | #define BLOCK_SIZE 512 |
bcostm | 0:c1aa0132b74f | 9 | #define BLOCK_END_ADDR (BLOCK_SIZE * NUM_OF_BLOCKS) |
bcostm | 0:c1aa0132b74f | 10 | #define BUFFER_WORDS_SIZE ((BLOCK_SIZE * NUM_OF_BLOCKS) >> 2) // Total data size in bytes |
bcostm | 0:c1aa0132b74f | 11 | |
bcostm | 0:c1aa0132b74f | 12 | uint32_t aTxBuffer[BUFFER_WORDS_SIZE]; |
bcostm | 0:c1aa0132b74f | 13 | uint32_t aRxBuffer[BUFFER_WORDS_SIZE]; |
bcostm | 0:c1aa0132b74f | 14 | |
bcostm | 0:c1aa0132b74f | 15 | static void print_demo_title(void); |
bcostm | 0:c1aa0132b74f | 16 | static void print_PASS(void); |
bcostm | 0:c1aa0132b74f | 17 | static void print_FAIL(void); |
bcostm | 0:c1aa0132b74f | 18 | static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLenght, uint32_t uwOffset); |
bcostm | 0:c1aa0132b74f | 19 | static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength); |
bcostm | 0:c1aa0132b74f | 20 | |
bcostm | 0:c1aa0132b74f | 21 | int main() |
bcostm | 0:c1aa0132b74f | 22 | { |
bcostm | 0:c1aa0132b74f | 23 | uint8_t status; |
bcostm | 0:c1aa0132b74f | 24 | print_demo_title(); |
bcostm | 0:c1aa0132b74f | 25 | wait(0.2); |
bcostm | 0:c1aa0132b74f | 26 | |
bcostm | 0:c1aa0132b74f | 27 | // Initialization |
bcostm | 0:c1aa0132b74f | 28 | status = BSP_SD_Init(); |
bcostm | 0:c1aa0132b74f | 29 | if (status == MSD_OK) { |
bcostm | 0:c1aa0132b74f | 30 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"SD INIT OK", LEFT_MODE); |
bcostm | 0:c1aa0132b74f | 31 | } |
bcostm | 0:c1aa0132b74f | 32 | else if (status == MSD_ERROR_SD_NOT_PRESENT) { |
bcostm | 0:c1aa0132b74f | 33 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"SD CARD NOT FOUND", LEFT_MODE); |
bcostm | 0:c1aa0132b74f | 34 | print_FAIL(); |
bcostm | 0:c1aa0132b74f | 35 | } |
bcostm | 0:c1aa0132b74f | 36 | else { |
bcostm | 0:c1aa0132b74f | 37 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"SD INIT FAIL", LEFT_MODE); |
bcostm | 0:c1aa0132b74f | 38 | print_FAIL(); |
bcostm | 0:c1aa0132b74f | 39 | } |
bcostm | 0:c1aa0132b74f | 40 | |
bcostm | 0:c1aa0132b74f | 41 | // Erase |
bcostm | 0:c1aa0132b74f | 42 | status = BSP_SD_Erase(BLOCK_START_ADDR, BLOCK_END_ADDR); |
bcostm | 0:c1aa0132b74f | 43 | if (status == MSD_OK) { |
bcostm | 0:c1aa0132b74f | 44 | BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"SD ERASE OK", LEFT_MODE); |
bcostm | 0:c1aa0132b74f | 45 | } |
bcostm | 0:c1aa0132b74f | 46 | else { |
bcostm | 0:c1aa0132b74f | 47 | BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"SD ERASE FAIL", LEFT_MODE); |
bcostm | 0:c1aa0132b74f | 48 | print_FAIL(); |
bcostm | 0:c1aa0132b74f | 49 | } |
bcostm | 0:c1aa0132b74f | 50 | |
bcostm | 0:c1aa0132b74f | 51 | // Prepare the buffer to write |
bcostm | 0:c1aa0132b74f | 52 | Fill_Buffer(aTxBuffer, BUFFER_WORDS_SIZE, 0x22FF); |
bcostm | 0:c1aa0132b74f | 53 | |
bcostm | 0:c1aa0132b74f | 54 | // Write |
bcostm | 0:c1aa0132b74f | 55 | status = BSP_SD_WriteBlocks(aTxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, SD_DATATIMEOUT); |
bcostm | 0:c1aa0132b74f | 56 | if (status == MSD_OK) { |
bcostm | 0:c1aa0132b74f | 57 | BSP_LCD_DisplayStringAt(0, LINE(7), (uint8_t *)"SD WRITE OK", LEFT_MODE); |
bcostm | 0:c1aa0132b74f | 58 | } |
bcostm | 0:c1aa0132b74f | 59 | else { |
bcostm | 0:c1aa0132b74f | 60 | BSP_LCD_DisplayStringAt(0, LINE(7), (uint8_t *)"SD WRITE FAIL", LEFT_MODE); |
bcostm | 0:c1aa0132b74f | 61 | print_FAIL(); |
bcostm | 0:c1aa0132b74f | 62 | } |
bcostm | 0:c1aa0132b74f | 63 | |
bcostm | 0:c1aa0132b74f | 64 | // Read |
bcostm | 0:c1aa0132b74f | 65 | status = BSP_SD_ReadBlocks(aRxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, SD_DATATIMEOUT); |
bcostm | 0:c1aa0132b74f | 66 | if (status == MSD_OK) { |
bcostm | 0:c1aa0132b74f | 67 | BSP_LCD_DisplayStringAt(0, LINE(8), (uint8_t *)"SD READ OK", LEFT_MODE); |
bcostm | 0:c1aa0132b74f | 68 | } |
bcostm | 0:c1aa0132b74f | 69 | else { |
bcostm | 0:c1aa0132b74f | 70 | BSP_LCD_DisplayStringAt(0, LINE(8), (uint8_t *)"SD READ FAIL", LEFT_MODE); |
bcostm | 0:c1aa0132b74f | 71 | print_FAIL(); |
bcostm | 0:c1aa0132b74f | 72 | } |
bcostm | 0:c1aa0132b74f | 73 | |
bcostm | 0:c1aa0132b74f | 74 | // Compare data |
bcostm | 0:c1aa0132b74f | 75 | if (Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) == 0) { |
bcostm | 0:c1aa0132b74f | 76 | BSP_LCD_DisplayStringAt(0, LINE(9), (uint8_t *)"SD DATA OK", LEFT_MODE); |
bcostm | 0:c1aa0132b74f | 77 | } |
bcostm | 0:c1aa0132b74f | 78 | else { |
bcostm | 0:c1aa0132b74f | 79 | BSP_LCD_DisplayStringAt(0, LINE(9), (uint8_t *)"SD DATA FAIL", LEFT_MODE); |
bcostm | 0:c1aa0132b74f | 80 | print_FAIL(); |
bcostm | 0:c1aa0132b74f | 81 | } |
bcostm | 0:c1aa0132b74f | 82 | |
bcostm | 0:c1aa0132b74f | 83 | print_PASS(); |
bcostm | 0:c1aa0132b74f | 84 | } |
bcostm | 0:c1aa0132b74f | 85 | |
bcostm | 0:c1aa0132b74f | 86 | static void print_demo_title(void) |
bcostm | 0:c1aa0132b74f | 87 | { |
bcostm | 0:c1aa0132b74f | 88 | BSP_LCD_Init(); |
bcostm | 0:c1aa0132b74f | 89 | BSP_LCD_Clear(LCD_COLOR_WHITE); |
bcostm | 0:c1aa0132b74f | 90 | BSP_LCD_SetTextColor(LCD_COLOR_BLUE); |
bcostm | 0:c1aa0132b74f | 91 | BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), 80); |
bcostm | 0:c1aa0132b74f | 92 | BSP_LCD_SetTextColor(LCD_COLOR_WHITE); |
bcostm | 0:c1aa0132b74f | 93 | BSP_LCD_SetBackColor(LCD_COLOR_BLUE); |
bcostm | 0:c1aa0132b74f | 94 | BSP_LCD_SetFont(&Font24); |
bcostm | 0:c1aa0132b74f | 95 | BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"SDCARD", CENTER_MODE); |
bcostm | 0:c1aa0132b74f | 96 | BSP_LCD_SetFont(&Font12); |
bcostm | 0:c1aa0132b74f | 97 | BSP_LCD_DisplayStringAt(0, 30, (uint8_t *)"This example shows how to write", CENTER_MODE); |
bcostm | 0:c1aa0132b74f | 98 | BSP_LCD_DisplayStringAt(0, 45, (uint8_t *)"and read data on the SDCard", CENTER_MODE); |
bcostm | 0:c1aa0132b74f | 99 | BSP_LCD_SetFont(&Font20); |
bcostm | 0:c1aa0132b74f | 100 | } |
bcostm | 0:c1aa0132b74f | 101 | |
bcostm | 0:c1aa0132b74f | 102 | static void print_PASS(void) |
bcostm | 0:c1aa0132b74f | 103 | { |
bcostm | 0:c1aa0132b74f | 104 | BSP_LCD_SetTextColor(LCD_COLOR_BLACK); |
bcostm | 0:c1aa0132b74f | 105 | BSP_LCD_SetBackColor(LCD_COLOR_GREEN); |
bcostm | 0:c1aa0132b74f | 106 | BSP_LCD_DisplayStringAt(0, LINE(11), (uint8_t *)"Demo OK", CENTER_MODE); |
bcostm | 0:c1aa0132b74f | 107 | while(1); |
bcostm | 0:c1aa0132b74f | 108 | } |
bcostm | 0:c1aa0132b74f | 109 | |
bcostm | 0:c1aa0132b74f | 110 | static void print_FAIL(void) |
bcostm | 0:c1aa0132b74f | 111 | { |
bcostm | 0:c1aa0132b74f | 112 | BSP_LCD_SetBackColor(LCD_COLOR_RED); |
bcostm | 0:c1aa0132b74f | 113 | BSP_LCD_DisplayStringAt(0, LINE(11), (uint8_t *)"Demo FAILED", CENTER_MODE); |
bcostm | 0:c1aa0132b74f | 114 | while(1); |
bcostm | 0:c1aa0132b74f | 115 | } |
bcostm | 0:c1aa0132b74f | 116 | |
bcostm | 0:c1aa0132b74f | 117 | /** |
bcostm | 0:c1aa0132b74f | 118 | * @brief Fills buffer with user predefined data. |
bcostm | 0:c1aa0132b74f | 119 | * @param pBuffer: pointer on the buffer to fill |
bcostm | 0:c1aa0132b74f | 120 | * @param uwBufferLenght: size of the buffer to fill |
bcostm | 0:c1aa0132b74f | 121 | * @param uwOffset: first value to fill on the buffer |
bcostm | 0:c1aa0132b74f | 122 | * @retval None |
bcostm | 0:c1aa0132b74f | 123 | */ |
bcostm | 0:c1aa0132b74f | 124 | static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset) |
bcostm | 0:c1aa0132b74f | 125 | { |
bcostm | 0:c1aa0132b74f | 126 | uint32_t tmpIndex = 0; |
bcostm | 0:c1aa0132b74f | 127 | /* Put in global buffer different values */ |
bcostm | 0:c1aa0132b74f | 128 | for (tmpIndex = 0; tmpIndex < uwBufferLength; tmpIndex++ ) { |
bcostm | 0:c1aa0132b74f | 129 | pBuffer[tmpIndex] = tmpIndex + uwOffset; |
bcostm | 0:c1aa0132b74f | 130 | } |
bcostm | 0:c1aa0132b74f | 131 | } |
bcostm | 0:c1aa0132b74f | 132 | |
bcostm | 0:c1aa0132b74f | 133 | /** |
bcostm | 0:c1aa0132b74f | 134 | * @brief Compares two buffers. |
bcostm | 0:c1aa0132b74f | 135 | * @param pBuffer1, pBuffer2: buffers to be compared. |
bcostm | 0:c1aa0132b74f | 136 | * @param BufferLength: buffer's length |
bcostm | 0:c1aa0132b74f | 137 | * @retval 0: pBuffer2 identical to pBuffer1 |
bcostm | 0:c1aa0132b74f | 138 | * 1: pBuffer2 differs from pBuffer1 |
bcostm | 0:c1aa0132b74f | 139 | */ |
bcostm | 0:c1aa0132b74f | 140 | static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength) |
bcostm | 0:c1aa0132b74f | 141 | { |
bcostm | 0:c1aa0132b74f | 142 | while (BufferLength--) { |
bcostm | 0:c1aa0132b74f | 143 | if (*pBuffer1 != *pBuffer2) { |
bcostm | 0:c1aa0132b74f | 144 | return 1; |
bcostm | 0:c1aa0132b74f | 145 | } |
bcostm | 0:c1aa0132b74f | 146 | pBuffer1++; |
bcostm | 0:c1aa0132b74f | 147 | pBuffer2++; |
bcostm | 0:c1aa0132b74f | 148 | } |
bcostm | 0:c1aa0132b74f | 149 | return 0; |
bcostm | 0:c1aa0132b74f | 150 | } |