SD card use for DISCO_F469NI basic example

Dependencies:   BSP_DISCO_F469NI SD_DISCO_F469NI mbed

Committer:
adustm
Date:
Fri Mar 18 15:20:09 2016 +0000
Revision:
0:354f8b7c3755
Child:
2:fdce3ea5ef35
SD card use for DISCO_F469NI basic example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adustm 0:354f8b7c3755 1 #include "mbed.h"
adustm 0:354f8b7c3755 2 #include "SD_DISCO_F469NI.h"
adustm 0:354f8b7c3755 3
adustm 0:354f8b7c3755 4 SD_DISCO_F469NI sd;
adustm 0:354f8b7c3755 5
adustm 0:354f8b7c3755 6 DigitalOut led_green(LED1);
adustm 0:354f8b7c3755 7 DigitalOut led_red(LED2);
adustm 0:354f8b7c3755 8
adustm 0:354f8b7c3755 9 Serial pc(USBTX, USBRX);
adustm 0:354f8b7c3755 10
adustm 0:354f8b7c3755 11 #define BLOCK_START_ADDR 0 /* Block start address */
adustm 0:354f8b7c3755 12 #define BLOCKSIZE 512 /* Block Size in Bytes */
adustm 0:354f8b7c3755 13 #define NUM_OF_BLOCKS 5 /* Total number of blocks */
adustm 0:354f8b7c3755 14 #define BUFFER_WORDS_SIZE ((BLOCKSIZE * NUM_OF_BLOCKS) >> 2) /* Total data size in bytes */
adustm 0:354f8b7c3755 15
adustm 0:354f8b7c3755 16 uint32_t aTxBuffer[BUFFER_WORDS_SIZE];
adustm 0:354f8b7c3755 17 uint32_t aRxBuffer[BUFFER_WORDS_SIZE];
adustm 0:354f8b7c3755 18 /* Private function prototypes -----------------------------------------------*/
adustm 0:354f8b7c3755 19 void SD_main_test(void);
adustm 0:354f8b7c3755 20 void SD_Detection(void);
adustm 0:354f8b7c3755 21 void HAL_Delay(__IO uint32_t Delay);
adustm 0:354f8b7c3755 22
adustm 0:354f8b7c3755 23 static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLenght, uint32_t uwOffset);
adustm 0:354f8b7c3755 24 static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength);
adustm 0:354f8b7c3755 25
adustm 0:354f8b7c3755 26 int main()
adustm 0:354f8b7c3755 27 {
adustm 0:354f8b7c3755 28 uint8_t SD_state = SD_OK;
adustm 0:354f8b7c3755 29 pc.printf("SD\n");
adustm 0:354f8b7c3755 30 pc.printf("This example shows how to write\n");
adustm 0:354f8b7c3755 31 pc.printf("and read data on the microSD and also\n");
adustm 0:354f8b7c3755 32 pc.printf("how to detect the presence of the card\n");
adustm 0:354f8b7c3755 33 led_red = 0;
adustm 0:354f8b7c3755 34
adustm 0:354f8b7c3755 35 SD_state = sd.Init();
adustm 0:354f8b7c3755 36 if(SD_state != SD_OK){
adustm 0:354f8b7c3755 37 if(SD_state == MSD_ERROR_SD_NOT_PRESENT){
adustm 0:354f8b7c3755 38 pc.printf("SD shall be inserted before running test\n");
adustm 0:354f8b7c3755 39 } else {
adustm 0:354f8b7c3755 40 pc.printf("SD Initialization : FAIL.\n");
adustm 0:354f8b7c3755 41 }
adustm 0:354f8b7c3755 42 pc.printf("SD Test Aborted.\n");
adustm 0:354f8b7c3755 43 } else {
adustm 0:354f8b7c3755 44 pc.printf("SD Initialization : OK.\n");
adustm 0:354f8b7c3755 45
adustm 0:354f8b7c3755 46 if(sd.Erase(BLOCK_START_ADDR, (BLOCKSIZE * NUM_OF_BLOCKS)) != SD_OK){
adustm 0:354f8b7c3755 47 pc.printf("SD ERASE : FAILED.\n");
adustm 0:354f8b7c3755 48 pc.printf("SD Test Aborted.\n");
adustm 0:354f8b7c3755 49 } else {
adustm 0:354f8b7c3755 50 pc.printf("SD ERASE : OK.\n");
adustm 0:354f8b7c3755 51
adustm 0:354f8b7c3755 52 /* Fill the buffer to write */
adustm 0:354f8b7c3755 53 Fill_Buffer(aTxBuffer, BUFFER_WORDS_SIZE, 0x22FF);
adustm 0:354f8b7c3755 54
adustm 0:354f8b7c3755 55 if(sd.WriteBlocks(aTxBuffer, BLOCK_START_ADDR, BLOCKSIZE, NUM_OF_BLOCKS) != SD_OK){
adustm 0:354f8b7c3755 56 pc.printf("SD WRITE : FAILED.\n");
adustm 0:354f8b7c3755 57 pc.printf("SD Test Aborted.\n");
adustm 0:354f8b7c3755 58 } else {
adustm 0:354f8b7c3755 59 pc.printf("SD WRITE : OK.\n");
adustm 0:354f8b7c3755 60
adustm 0:354f8b7c3755 61 if(sd.ReadBlocks(aRxBuffer, BLOCK_START_ADDR, BLOCKSIZE, NUM_OF_BLOCKS)!= SD_OK){
adustm 0:354f8b7c3755 62 pc.printf("SD READ : FAILED.\n");
adustm 0:354f8b7c3755 63 pc.printf("SD Test Aborted.\n");
adustm 0:354f8b7c3755 64 } else {
adustm 0:354f8b7c3755 65 pc.printf("SD READ : OK.\n");
adustm 0:354f8b7c3755 66 if(Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0){
adustm 0:354f8b7c3755 67 pc.printf("SD COMPARE : FAILED.\n");
adustm 0:354f8b7c3755 68 pc.printf("SD Test Aborted.\n");
adustm 0:354f8b7c3755 69 } else {
adustm 0:354f8b7c3755 70 pc.printf("SD Test : OK.\n");
adustm 0:354f8b7c3755 71 pc.printf("SD can be removed.\n");
adustm 0:354f8b7c3755 72 }
adustm 0:354f8b7c3755 73 }
adustm 0:354f8b7c3755 74 }
adustm 0:354f8b7c3755 75 }
adustm 0:354f8b7c3755 76 }
adustm 0:354f8b7c3755 77
adustm 0:354f8b7c3755 78 while (1) {
adustm 0:354f8b7c3755 79 }
adustm 0:354f8b7c3755 80 }
adustm 0:354f8b7c3755 81
adustm 0:354f8b7c3755 82 /**
adustm 0:354f8b7c3755 83 * @brief Fills buffer with user predefined data.
adustm 0:354f8b7c3755 84 * @param pBuffer: pointer on the buffer to fill
adustm 0:354f8b7c3755 85 * @param uwBufferLenght: size of the buffer to fill
adustm 0:354f8b7c3755 86 * @param uwOffset: first value to fill on the buffer
adustm 0:354f8b7c3755 87 * @retval None
adustm 0:354f8b7c3755 88 */
adustm 0:354f8b7c3755 89 static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset)
adustm 0:354f8b7c3755 90 {
adustm 0:354f8b7c3755 91 uint32_t tmpIndex = 0;
adustm 0:354f8b7c3755 92
adustm 0:354f8b7c3755 93 /* Put in global buffer different values */
adustm 0:354f8b7c3755 94 for (tmpIndex = 0; tmpIndex < uwBufferLength; tmpIndex++ )
adustm 0:354f8b7c3755 95 {
adustm 0:354f8b7c3755 96 pBuffer[tmpIndex] = tmpIndex + uwOffset;
adustm 0:354f8b7c3755 97 }
adustm 0:354f8b7c3755 98 }
adustm 0:354f8b7c3755 99
adustm 0:354f8b7c3755 100 /**
adustm 0:354f8b7c3755 101 * @brief Compares two buffers.
adustm 0:354f8b7c3755 102 * @param pBuffer1, pBuffer2: buffers to be compared.
adustm 0:354f8b7c3755 103 * @param BufferLength: buffer's length
adustm 0:354f8b7c3755 104 * @retval 1: pBuffer identical to pBuffer1
adustm 0:354f8b7c3755 105 * 0: pBuffer differs from pBuffer1
adustm 0:354f8b7c3755 106 */
adustm 0:354f8b7c3755 107 static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength)
adustm 0:354f8b7c3755 108 {
adustm 0:354f8b7c3755 109 while (BufferLength--)
adustm 0:354f8b7c3755 110 {
adustm 0:354f8b7c3755 111 if (*pBuffer1 != *pBuffer2)
adustm 0:354f8b7c3755 112 {
adustm 0:354f8b7c3755 113 return 1;
adustm 0:354f8b7c3755 114 }
adustm 0:354f8b7c3755 115
adustm 0:354f8b7c3755 116 pBuffer1++;
adustm 0:354f8b7c3755 117 pBuffer2++;
adustm 0:354f8b7c3755 118 }
adustm 0:354f8b7c3755 119
adustm 0:354f8b7c3755 120 return 0;
adustm 0:354f8b7c3755 121 }
adustm 0:354f8b7c3755 122
adustm 0:354f8b7c3755 123
adustm 0:354f8b7c3755 124 /**
adustm 0:354f8b7c3755 125 * @brief This function provides accurate delay (in milliseconds) based
adustm 0:354f8b7c3755 126 * on variable incremented.
adustm 0:354f8b7c3755 127 * @note In the default implementation , SysTick timer is the source of time base.
adustm 0:354f8b7c3755 128 * It is used to generate interrupts at regular time intervals where uwTick
adustm 0:354f8b7c3755 129 * is incremented.
adustm 0:354f8b7c3755 130 * @note This function is the modified version of the __weak version contained in
adustm 0:354f8b7c3755 131 * stm32f4xx_hal.c
adustm 0:354f8b7c3755 132 * @param Delay: specifies the delay time length, in milliseconds.
adustm 0:354f8b7c3755 133 * @retval None
adustm 0:354f8b7c3755 134 */
adustm 0:354f8b7c3755 135 void HAL_Delay(__IO uint32_t Delay)
adustm 0:354f8b7c3755 136 {
adustm 0:354f8b7c3755 137 wait_ms((int)Delay);
adustm 0:354f8b7c3755 138 }
adustm 0:354f8b7c3755 139