SD demo

Dependencies:   BSP_DISCO_F413ZH mbed

Files at this revision

API Documentation at this revision

Comitter:
bcostm
Date:
Wed Sep 27 06:55:18 2017 +0000
Parent:
2:db8dcfc87a4a
Commit message:
Add mbed library v151 + improve demo

Changed in this revision

BSP_DISCO_F413ZH.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r db8dcfc87a4a -r 3ef082d30ee5 BSP_DISCO_F413ZH.lib
--- a/BSP_DISCO_F413ZH.lib	Mon May 22 10:25:48 2017 +0200
+++ b/BSP_DISCO_F413ZH.lib	Wed Sep 27 06:55:18 2017 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/teams/ST/code/BSP_DISCO_F413ZH/#a3f6b8b5a6bf
\ No newline at end of file
+https://developer.mbed.org/teams/ST/code/BSP_DISCO_F413ZH/#0f07a9ac06f7
diff -r db8dcfc87a4a -r 3ef082d30ee5 main.cpp
--- a/main.cpp	Mon May 22 10:25:48 2017 +0200
+++ b/main.cpp	Wed Sep 27 06:55:18 2017 +0000
@@ -2,39 +2,131 @@
 #include "stm32f413h_discovery.h"
 #include "stm32f413h_discovery_lcd.h"
 #include "stm32f413h_discovery_sd.h"
- 
+
+#define BLOCK_START_ADDR         0     /* Block start address      */
+#define NUM_OF_BLOCKS            5     /* Total number of blocks   */
+#define BUFFER_WORDS_SIZE        ((512 * NUM_OF_BLOCKS) >> 2) /* Total data size in bytes */
+
+uint32_t aTxBuffer[BUFFER_WORDS_SIZE];
+uint32_t aRxBuffer[BUFFER_WORDS_SIZE];
+
+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);
+static void print_error(uint8_t *msg);
+
 int main()
 {
-    uint8_t SD_state = MSD_OK;
-    static uint8_t prev_status = 2;
- 
     /* Init LCD and display example information */
     BSP_LCD_Init();
     BSP_LCD_Clear(LCD_COLOR_WHITE);
-    BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
-    BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), 40);
-    BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
-    BSP_LCD_SetBackColor(LCD_COLOR_GREEN);
     BSP_LCD_SetFont(&Font16);
- 
+
     /* SD init */
-    SD_state = BSP_SD_Init();
- 
-    BSP_LCD_DisplayStringAt(0, 15, (uint8_t *)"Insert or Removed SD", CENTER_MODE);
+    if (BSP_SD_Init() == MSD_OK) {
+        BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
+        BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), 40);
+        BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
+        BSP_LCD_SetBackColor(LCD_COLOR_GREEN);
+        BSP_LCD_DisplayStringAt(0, 15, (uint8_t *)"SD detected", CENTER_MODE);
+        BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
+    } else {
+        BSP_LCD_SetTextColor(LCD_COLOR_RED);
+        BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), 40);
+        BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
+        BSP_LCD_SetBackColor(LCD_COLOR_RED);
+        BSP_LCD_DisplayStringAt(0, 15, (uint8_t *)"SD not detected", CENTER_MODE);
+        print_error((uint8_t *)"Demo aborted");
+    }
+
+    wait(1);
+    // Check if SD card is detected
+    if (BSP_SD_IsDetected() == SD_PRESENT) {
+        BSP_LCD_DisplayStringAt(0, 50, (uint8_t *)"SD detected OK", CENTER_MODE);
+    } else {
+        print_error((uint8_t *)"SD not detected");
+    }
+
+    wait(1);
+    // Erase SD card
+    if(BSP_SD_Erase(BLOCK_START_ADDR, (512 * NUM_OF_BLOCKS)) == MSD_OK) {
+        BSP_LCD_DisplayStringAt(0, 70, (uint8_t *)"SD erased OK", CENTER_MODE);
+    } else {
+        print_error((uint8_t *)"Erased failed");
+    }
 
-    while (1) {
-        if (BSP_SD_IsDetected() != SD_PRESENT) {
-            if(prev_status != SD_NOT_PRESENT) {
-                BSP_SD_Init();
-                prev_status = SD_NOT_PRESENT;
-                BSP_LCD_SetTextColor(LCD_COLOR_RED);
-                BSP_LCD_DisplayStringAt(20, 130, (uint8_t *)"SD Not Connected", LEFT_MODE);
-            }
-        } else if (prev_status != SD_PRESENT) {
-            BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
-            BSP_LCD_DisplayStringAt(20, 130,   (uint8_t *)"SD Connected    ", LEFT_MODE);
-            prev_status = SD_PRESENT;
+    wait(1);
+    // Write data in SD card
+    Fill_Buffer(aTxBuffer, BUFFER_WORDS_SIZE, 0x22FF);
+    if (BSP_SD_WriteBlocks(aTxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, SD_DATATIMEOUT) == MSD_OK) {
+        BSP_LCD_DisplayStringAt(0, 90, (uint8_t *)"SD write OK", CENTER_MODE);
+    } else {
+        print_error((uint8_t *)"Write failed");
+    }
+
+    wait(1);
+    // Read data from SD card and verify them
+    if (BSP_SD_ReadBlocks(aRxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, SD_DATATIMEOUT) == MSD_OK) {
+        BSP_LCD_DisplayStringAt(0, 110, (uint8_t *)"SD read OK", CENTER_MODE);
+        if (Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) == 0) {
+            BSP_LCD_DisplayStringAt(0, 130, (uint8_t *)"Data OK", CENTER_MODE);
+        } else {
+            print_error((uint8_t *)"Data wrong");
         }
- 
+    } else {
+        print_error((uint8_t *)"Read failed");
+    }
+
+    wait(1);
+    BSP_LCD_DisplayStringAt(0, 200, (uint8_t *)"Demo finished OK", CENTER_MODE);
+    while(1);
+}
+
+/**
+  * @brief  Fills buffer with user predefined data.
+  * @param  pBuffer: pointer on the buffer to fill
+  * @param  uwBufferLenght: size of the buffer to fill
+  * @param  uwOffset: first value to fill on the buffer
+  * @retval None
+  */
+static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset)
+{
+    uint32_t tmpIndex = 0;
+
+    /* Put in global buffer different values */
+    for (tmpIndex = 0; tmpIndex < uwBufferLength; tmpIndex++ ) {
+        pBuffer[tmpIndex] = tmpIndex + uwOffset;
     }
 }
+
+/**
+  * @brief  Compares two buffers.
+  * @param  pBuffer1, pBuffer2: buffers to be compared.
+  * @param  BufferLength: buffer's length
+  * @retval 1: pBuffer identical to pBuffer1
+  *         0: pBuffer differs from pBuffer1
+  */
+static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength)
+{
+    while (BufferLength--) {
+        if (*pBuffer1 != *pBuffer2) {
+            return 1;
+        }
+
+        pBuffer1++;
+        pBuffer2++;
+    }
+
+    return 0;
+}
+
+/**
+  * @brief  Print an error message on LCD and stop
+  * @param  Message string
+  */
+static void print_error(uint8_t *msg)
+{
+    BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
+    BSP_LCD_SetTextColor(LCD_COLOR_RED);
+    BSP_LCD_DisplayStringAt(0, 200, msg, CENTER_MODE);
+    while(1);
+}
diff -r db8dcfc87a4a -r 3ef082d30ee5 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Sep 27 06:55:18 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/675da3299148
\ No newline at end of file