5 years, 12 months ago.

Error run SD demo

I use STM32f469-Discovery, use to run SD demo on Mbed OS V2 without an error now I try to run on the newest Mbed OS, the program compile just find but can't write to SD card as I show on picture./media/uploads/thach_s/20180425_150550.jpg

#include "mbed.h"
#include "SD_DISCO_F469NI.h"
#include "LCD_DISCO_F469NI.h"
 
SD_DISCO_F469NI sd;
LCD_DISCO_F469NI lcd;
 
DigitalOut led_green(LED1);
DigitalOut led_red(LED2);

Serial pc(USBTX, USBRX);
 
#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];

/* Private function prototypes -----------------------------------------------*/
void SD_main_test(void);
void SD_Detection(void);
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);
 
int main()
{
    uint8_t SD_state = MSD_OK;
    led_red = 0;
    lcd.SetFont(&Font20);        
    lcd.DisplayStringAt(0, LINE(1), (uint8_t *)"SD", CENTER_MODE);
    lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"This example shows how to write", CENTER_MODE);
    lcd.DisplayStringAt(0, LINE(3), (uint8_t *)"and read data on the microSD and also", CENTER_MODE);
    lcd.DisplayStringAt(0, LINE(4), (uint8_t *)"how to detect the presence of the card", CENTER_MODE);
    SD_state = sd.Init();
    if(SD_state != MSD_OK){
        if(SD_state == MSD_ERROR_SD_NOT_PRESENT){
            lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"SD shall be inserted before running test", CENTER_MODE);
        } else {
            lcd.DisplayStringAt(0, LINE(6), (uint8_t *)"SD Initialization : FAIL.", CENTER_MODE);
        }

        lcd.DisplayStringAt(0, LINE(7), (uint8_t *)"SD Test Aborted.", CENTER_MODE);
    } else {
        lcd.DisplayStringAt(0, LINE(8), (uint8_t *)"SD Initialization : OK.", CENTER_MODE);
        if(sd.Erase(BLOCK_START_ADDR, (512 * NUM_OF_BLOCKS)) != MSD_OK){
            lcd.DisplayStringAt(0, LINE(9), (uint8_t *)"SD ERASE : FAILED.", CENTER_MODE);
            lcd.DisplayStringAt(0, LINE(10), (uint8_t *)"SD Test Aborted.", CENTER_MODE);
        } else {
            lcd.DisplayStringAt(0, LINE(11), (uint8_t *)"SD ERASE : OK.", CENTER_MODE);

            /* Fill the buffer to write */
            Fill_Buffer(aTxBuffer, BUFFER_WORDS_SIZE, 0x22FF);
            if(sd.WriteBlocks(aTxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, SD_DATATIMEOUT) != MSD_OK){
                lcd.DisplayStringAt(0, LINE(12), (uint8_t *)"SD WRITE : FAILED.", CENTER_MODE);
                lcd.DisplayStringAt(0, LINE(13), (uint8_t *)"SD Test Aborted.", CENTER_MODE);
            } else {
                lcd.DisplayStringAt(0, LINE(14), (uint8_t *)"SD WRITE : OK.", CENTER_MODE);
          
                if(sd.ReadBlocks(aRxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, SD_DATATIMEOUT)!= MSD_OK){
                    lcd.DisplayStringAt(0, LINE(15), (uint8_t *)"SD READ : FAILED.", CENTER_MODE);
                    lcd.DisplayStringAt(0, LINE(16), (uint8_t *)"SD Test Aborted.", CENTER_MODE);
                } else {
                    lcd.DisplayStringAt(0, LINE(17), (uint8_t *)"SD READ : OK.", CENTER_MODE);
                    if(Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0){
                        lcd.DisplayStringAt(0, LINE(18), (uint8_t *)"SD COMPARE : FAILED.", CENTER_MODE);
                        lcd.DisplayStringAt(0, LINE(19), (uint8_t *)"SD Test Aborted.", CENTER_MODE);
                    } else {
                        lcd.DisplayStringAt(0, LINE(20), (uint8_t *)"SD COMPARE Test : OK.", CENTER_MODE);
                        lcd.DisplayStringAt(0, LINE(21), (uint8_t *)"SD can be removed.", 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;
}

Please use {{{

your code

when posting code so that the formatting is maintained.

posted by Andy A 08 May 2018

Sorry, that should be

<<code>>
your code
<</code>>

It won't let me edit the original comment for some reason.

posted by Andy A 09 May 2018

Thank you, Andy A

posted by Thach Singkarat 17 May 2018
Be the first to answer this question.