SD for DISCO_F746NG basic demo

Dependencies:   BSP_DISCO_F746NG

Committer:
adustm
Date:
Fri Jun 09 08:35:40 2017 +0000
Revision:
3:33086de19b14
Parent:
2:522456118ea2
Child:
4:85319532f755
Align the main.cpp with BSP_DISCO_F746NG and SD_DISCO_F746NG library updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adustm 0:99e26e18b424 1 #include "mbed.h"
adustm 0:99e26e18b424 2 #include "SD_DISCO_F746NG.h"
adustm 0:99e26e18b424 3
adustm 0:99e26e18b424 4 SD_DISCO_F746NG sd;
adustm 0:99e26e18b424 5
adustm 0:99e26e18b424 6 DigitalOut led_green(LED1);
adustm 0:99e26e18b424 7 DigitalOut led_red(LED2);
adustm 0:99e26e18b424 8
adustm 0:99e26e18b424 9 Serial pc(USBTX, USBRX);
adustm 0:99e26e18b424 10
adustm 0:99e26e18b424 11 #define BLOCK_START_ADDR 0 /* Block start address */
adustm 0:99e26e18b424 12 #define NUM_OF_BLOCKS 5 /* Total number of blocks */
adustm 0:99e26e18b424 13 #define BUFFER_WORDS_SIZE ((BLOCKSIZE * NUM_OF_BLOCKS) >> 2) /* Total data size in bytes */
adustm 0:99e26e18b424 14
adustm 0:99e26e18b424 15 uint32_t aTxBuffer[BUFFER_WORDS_SIZE];
adustm 0:99e26e18b424 16 uint32_t aRxBuffer[BUFFER_WORDS_SIZE];
adustm 0:99e26e18b424 17 /* Private function prototypes -----------------------------------------------*/
adustm 0:99e26e18b424 18 void SD_main_test(void);
adustm 0:99e26e18b424 19 void SD_Detection(void);
adustm 0:99e26e18b424 20
adustm 0:99e26e18b424 21 static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLenght, uint32_t uwOffset);
adustm 0:99e26e18b424 22 static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength);
adustm 0:99e26e18b424 23
adustm 0:99e26e18b424 24 int main()
adustm 0:99e26e18b424 25 {
adustm 3:33086de19b14 26 uint8_t SD_state = MSD_OK;
adustm 0:99e26e18b424 27 pc.printf("\n\nuSD example start:\n");
adustm 0:99e26e18b424 28 led_red = 0;
adustm 0:99e26e18b424 29
adustm 0:99e26e18b424 30 SD_state = sd.Init();
adustm 3:33086de19b14 31 if(SD_state != MSD_OK){
adustm 0:99e26e18b424 32 if(SD_state == MSD_ERROR_SD_NOT_PRESENT){
adustm 0:99e26e18b424 33 pc.printf("SD shall be inserted before running test\n");
adustm 0:99e26e18b424 34 } else {
adustm 0:99e26e18b424 35 pc.printf("SD Initialization : FAIL.\n");
adustm 0:99e26e18b424 36 }
adustm 0:99e26e18b424 37 pc.printf("SD Test Aborted.\n");
adustm 0:99e26e18b424 38 } else {
adustm 0:99e26e18b424 39 pc.printf("SD Initialization : OK.\n");
adustm 0:99e26e18b424 40
adustm 3:33086de19b14 41 SD_state = sd.Erase(BLOCK_START_ADDR, (BLOCK_START_ADDR + NUM_OF_BLOCKS - 1));
adustm 3:33086de19b14 42
adustm 3:33086de19b14 43 /* Wait until SD card is ready to use for new operation */
adustm 3:33086de19b14 44 while(sd.GetCardState() != SD_TRANSFER_OK){
adustm 3:33086de19b14 45 }
adustm 3:33086de19b14 46 if (SD_state != MSD_OK){
adustm 0:99e26e18b424 47 pc.printf("SD ERASE : FAILED.\n");
adustm 0:99e26e18b424 48 pc.printf("SD Test Aborted.\n");
adustm 0:99e26e18b424 49 } else {
adustm 0:99e26e18b424 50 pc.printf("SD ERASE : OK.\n");
adustm 0:99e26e18b424 51
adustm 0:99e26e18b424 52 /* Fill the buffer to write */
adustm 3:33086de19b14 53 Fill_Buffer(aTxBuffer, BUFFER_WORDS_SIZE, 0x2300);
adustm 0:99e26e18b424 54
adustm 3:33086de19b14 55 SD_state = sd.WriteBlocks(aTxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, 10000);
adustm 3:33086de19b14 56 /* Wait until SD card is ready to use for new operation */
adustm 3:33086de19b14 57 while(sd.GetCardState() != SD_TRANSFER_OK){
adustm 3:33086de19b14 58 }
adustm 3:33086de19b14 59
adustm 3:33086de19b14 60 if (SD_state != MSD_OK){
adustm 0:99e26e18b424 61 pc.printf("SD WRITE : FAILED.\n");
adustm 0:99e26e18b424 62 pc.printf("SD Test Aborted.\n");
adustm 0:99e26e18b424 63 } else {
adustm 0:99e26e18b424 64 pc.printf("SD WRITE : OK.\n");
adustm 0:99e26e18b424 65
adustm 3:33086de19b14 66 SD_state = sd.ReadBlocks(aRxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, 10000);
adustm 3:33086de19b14 67 /* Wait until SD card is ready to use for new operation */
adustm 3:33086de19b14 68 while(sd.GetCardState() != SD_TRANSFER_OK){
adustm 3:33086de19b14 69 }
adustm 3:33086de19b14 70
adustm 3:33086de19b14 71 if (SD_state != MSD_OK){
adustm 0:99e26e18b424 72 pc.printf("SD READ : FAILED.\n");
adustm 0:99e26e18b424 73 pc.printf("SD Test Aborted.\n");
adustm 0:99e26e18b424 74 } else {
adustm 0:99e26e18b424 75 pc.printf("SD READ : OK.\n");
adustm 0:99e26e18b424 76 if(Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0){
adustm 0:99e26e18b424 77 pc.printf("SD COMPARE : FAILED.\n");
adustm 0:99e26e18b424 78 pc.printf("SD Test Aborted.\n");
adustm 0:99e26e18b424 79 } else {
adustm 0:99e26e18b424 80 pc.printf("SD Test : OK.\n");
adustm 0:99e26e18b424 81 pc.printf("SD can be removed.\n");
adustm 0:99e26e18b424 82 }
adustm 0:99e26e18b424 83 }
adustm 0:99e26e18b424 84 }
adustm 0:99e26e18b424 85 }
adustm 0:99e26e18b424 86 }
adustm 0:99e26e18b424 87
adustm 0:99e26e18b424 88 while (1) {
adustm 0:99e26e18b424 89 }
adustm 0:99e26e18b424 90 }
adustm 0:99e26e18b424 91
adustm 0:99e26e18b424 92 /**
adustm 0:99e26e18b424 93 * @brief Fills buffer with user predefined data.
adustm 0:99e26e18b424 94 * @param pBuffer: pointer on the buffer to fill
adustm 0:99e26e18b424 95 * @param uwBufferLenght: size of the buffer to fill
adustm 0:99e26e18b424 96 * @param uwOffset: first value to fill on the buffer
adustm 0:99e26e18b424 97 * @retval None
adustm 0:99e26e18b424 98 */
adustm 0:99e26e18b424 99 static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset)
adustm 0:99e26e18b424 100 {
adustm 0:99e26e18b424 101 uint32_t tmpIndex = 0;
adustm 0:99e26e18b424 102
adustm 0:99e26e18b424 103 /* Put in global buffer different values */
adustm 0:99e26e18b424 104 for (tmpIndex = 0; tmpIndex < uwBufferLength; tmpIndex++ )
adustm 0:99e26e18b424 105 {
adustm 0:99e26e18b424 106 pBuffer[tmpIndex] = tmpIndex + uwOffset;
adustm 0:99e26e18b424 107 }
adustm 0:99e26e18b424 108 }
adustm 0:99e26e18b424 109
adustm 0:99e26e18b424 110 /**
adustm 0:99e26e18b424 111 * @brief Compares two buffers.
adustm 0:99e26e18b424 112 * @param pBuffer1, pBuffer2: buffers to be compared.
adustm 0:99e26e18b424 113 * @param BufferLength: buffer's length
adustm 0:99e26e18b424 114 * @retval 1: pBuffer identical to pBuffer1
adustm 0:99e26e18b424 115 * 0: pBuffer differs from pBuffer1
adustm 0:99e26e18b424 116 */
adustm 0:99e26e18b424 117 static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength)
adustm 0:99e26e18b424 118 {
adustm 0:99e26e18b424 119 while (BufferLength--)
adustm 0:99e26e18b424 120 {
adustm 0:99e26e18b424 121 if (*pBuffer1 != *pBuffer2)
adustm 0:99e26e18b424 122 {
adustm 0:99e26e18b424 123 return 1;
adustm 0:99e26e18b424 124 }
adustm 0:99e26e18b424 125
adustm 0:99e26e18b424 126 pBuffer1++;
adustm 0:99e26e18b424 127 pBuffer2++;
adustm 0:99e26e18b424 128 }
adustm 0:99e26e18b424 129
adustm 0:99e26e18b424 130 return 0;
adustm 0:99e26e18b424 131 }
adustm 0:99e26e18b424 132