ST / Mbed 2 deprecated DISCO-F469NI_SD_demo

Dependencies:   BSP_DISCO_F469NI SD_DISCO_F469NI mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SD_DISCO_F469NI.h"
00003  
00004 SD_DISCO_F469NI sd;
00005  
00006 DigitalOut led_green(LED1);
00007 DigitalOut led_red(LED2);
00008 
00009 Serial pc(USBTX, USBRX);
00010  
00011 #define BLOCK_START_ADDR         0     /* Block start address      */
00012 #define NUM_OF_BLOCKS            5     /* Total number of blocks   */
00013 #define BUFFER_WORDS_SIZE        ((512 * NUM_OF_BLOCKS) >> 2) /* Total data size in bytes */
00014  
00015 uint32_t aTxBuffer[BUFFER_WORDS_SIZE];
00016 uint32_t aRxBuffer[BUFFER_WORDS_SIZE];
00017 /* Private function prototypes -----------------------------------------------*/
00018 void SD_main_test(void);
00019 void SD_Detection(void);
00020 
00021 static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLenght, uint32_t uwOffset);
00022 static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength);
00023  
00024 int main()
00025 {
00026     uint8_t SD_state = MSD_OK;
00027     pc.printf("SD\n");
00028     pc.printf("This example shows how to write\n");
00029     pc.printf("and read data on the microSD and also\n");
00030     pc.printf("how to detect the presence of the card\n");
00031     led_red = 0;
00032   
00033     SD_state = sd.Init();
00034     if(SD_state != MSD_OK){
00035         if(SD_state == MSD_ERROR_SD_NOT_PRESENT){
00036             pc.printf("SD shall be inserted before running test\n");
00037         } else {
00038             pc.printf("SD Initialization : FAIL.\n");
00039         }
00040         pc.printf("SD Test Aborted.\n");
00041     } else {
00042         pc.printf("SD Initialization : OK.\n");
00043 
00044         if(sd.Erase(BLOCK_START_ADDR, (512 * NUM_OF_BLOCKS)) != MSD_OK){
00045             pc.printf("SD ERASE : FAILED.\n");
00046             pc.printf("SD Test Aborted.\n");
00047         } else {
00048             pc.printf("SD ERASE : OK.\n");
00049           
00050             /* Fill the buffer to write */
00051             Fill_Buffer(aTxBuffer, BUFFER_WORDS_SIZE, 0x22FF);
00052           
00053             if(sd.WriteBlocks(aTxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, SD_DATATIMEOUT) != MSD_OK){
00054                 pc.printf("SD WRITE : FAILED.\n");
00055                 pc.printf("SD Test Aborted.\n");
00056             } else {
00057                 pc.printf("SD WRITE : OK.\n");
00058             
00059                 if(sd.ReadBlocks(aRxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, SD_DATATIMEOUT)!= MSD_OK){
00060                     pc.printf("SD READ : FAILED.\n");
00061                     pc.printf("SD Test Aborted.\n");
00062                 } else {
00063                     pc.printf("SD READ : OK.\n");
00064                     if(Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0){
00065                         pc.printf("SD COMPARE : FAILED.\n");
00066                         pc.printf("SD Test Aborted.\n");
00067                     } else {
00068                         pc.printf("SD Test : OK.\n");
00069                         pc.printf("SD can be removed.\n");
00070                     }
00071                 }
00072             }
00073         }
00074     }
00075   
00076     while (1) {
00077     }
00078 } 
00079 
00080 /**
00081   * @brief  Fills buffer with user predefined data.
00082   * @param  pBuffer: pointer on the buffer to fill
00083   * @param  uwBufferLenght: size of the buffer to fill
00084   * @param  uwOffset: first value to fill on the buffer
00085   * @retval None
00086   */
00087 static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset)
00088 {
00089   uint32_t tmpIndex = 0;
00090 
00091   /* Put in global buffer different values */
00092   for (tmpIndex = 0; tmpIndex < uwBufferLength; tmpIndex++ )
00093   {
00094     pBuffer[tmpIndex] = tmpIndex + uwOffset;
00095   }
00096 }
00097  
00098 /**
00099   * @brief  Compares two buffers.
00100   * @param  pBuffer1, pBuffer2: buffers to be compared.
00101   * @param  BufferLength: buffer's length
00102   * @retval 1: pBuffer identical to pBuffer1
00103   *         0: pBuffer differs from pBuffer1
00104   */
00105 static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength)
00106 {
00107   while (BufferLength--)
00108   {
00109     if (*pBuffer1 != *pBuffer2)
00110     {
00111       return 1;
00112     }
00113 
00114     pBuffer1++;
00115     pBuffer2++;
00116   }
00117 
00118   return 0;
00119 }