0
\$\begingroup\$

I need to interface SD Card using SPI communication in stm32f103c8. First i to store the temprature value in every one seconds in sd card. It work well after that i try to store temperature value in that i need to erase the previous data and store the new temperature data. I have done the code but i only erase the data and it did not store the last updated temperature value please let me know where it went wrong.

my code.

#include "main.h"
#include "fatfs.h"
#include "math.h"
#include "fatfs_sd.h"
#include "string.h"
#include "stm32f1xx_hal.h"
#include "stdio.h"
#include "ssd1306.h"
#include "fonts.h"

#define CMD32 32
#define CMD33 33

FATFS fs;
FATFS *pfs;
FIL fil;
FRESULT fresult;
FILINFO fno;
uint8_t erase_cmd[6]={0x20, 0x00, 0x00, 0x00, 0x00, 0x95};
uint8_t status;
uint8_t SD_tx_buf[6];
uint8_t SD_rx_buf[6];

uint16_t adc_buf[100];
char buffer[100];
UINT br,bw;


FRESULT open_append(FIL* fp, const char* path);   // object file create, file name to open
FRESULT open_append(FIL* fp, const char* path)
{   
    fresult = f_open(fp, path, FA_WRITE | FA_READ | FA_OPEN_ALWAYS);  // new file create
    
       if(fresult==0)
         {
             fresult= f_lseek(fp, f_size(fp));  // seek to end of the file to append data
                 if(fresult !=0)
                         f_close(fp);
         }
         return fresult;
}

int main(void)
{

  HAL_Init();


  SystemClock_Config();


  MX_GPIO_Init();
  MX_ADC1_Init();
  MX_I2C1_Init();
  MX_RTC_Init();
  MX_SPI1_Init();
  MX_TIM1_Init();
  MX_USART1_UART_Init();
  MX_FATFS_Init();

     set_time();      // time set function


           fresult= disk_initialize(0);       //initialize sd card      **** SD Card Create File ****
             fresult= f_mount(&fs, (TCHAR const*)arr,0); 
           if(fresult != FR_OK)
             {
                return fresult;
             }
        
        fresult= f_open(&fil, "Temp_Read1.txt", FA_OPEN_ALWAYS);  //create text file
          fresult= f_mount(&fs, (TCHAR const*)arr, 0);
          fresult= open_append(&fil, "Temp_Read1.txt");
            if(fresult == FR_OK)
                {
          f_puts("time\t  Temp\n", &fil);
              f_close(&fil);
                }  
                
/**************************************************************************************************************************************************************/        
    
//                      NVIC_SystemReset();        // INTERNALLY_RESET
                                for(i=0; i<100; i++)
                        {
                                 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);  /********Erase_cmd********    ****Working****/
                     
                                 HAL_SPI_Transmit(&hspi1, erase_cmd, sizeof(erase_cmd), HAL_MAX_DELAY);

                                 HAL_SPI_Receive(&hspi1, &status, 1, HAL_MAX_DELAY);
                                 
                                 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);  HAL_Delay(10);
                        }


  while (1)
  {
    HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
      HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);              // RTC_TIME
        
        hours= sTime.Hours;
        mins=  sTime.Minutes;
        sec =   sTime.Seconds;
                

        ADC_sense();
        
        sprintf(buff, "%d:%d:%d\t  T1=%d,  T1=%d,  T1=%d,  T1=%d,  T1=%d\n", hours,mins,sec, temp1,temp2,temp3,temp4,temp5);        // values stored in SD card
        
        f_mount(&fs, (TCHAR const*)arr, 0);    //mount sd drive      **** SD Card Read Condition ****
                                              
           fresult= open_append(&fil, "Temp_Read1.txt");
//      if(sec==59)                                  // one min once temp data store
//          {    
    //  count_p++; i++;
            if(fresult == FR_OK)
                        {
                   f_puts(buff, &fil);                    // values stored at SD card

                   f_close(&fil);
                            

                        while(1){   };
                        }   
}}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ That's now a third question about the same subject? Or what is the difference? \$\endgroup\$
    – Justme
    Commented Mar 16, 2023 at 14:26
  • 1
    \$\begingroup\$ Your erase command doesn't work like that, because you have no idea how SD cards over SPI work. Why don't you use fatfs, see here: electronics.stackexchange.com/questions/365757/… \$\endgroup\$
    – nanash1
    Commented Mar 16, 2023 at 14:26
  • \$\begingroup\$ You need to decide whether you're using the SD card as a filesystem, or as "raw" memory. If you're using a filesystem, then your f_open and similar calls are appropriate and if you want to erase stuff then use f_unlink to delete the file. You only use something like CMD32 if you're using the SD card as "raw" memory because doing that will destroy the filesystem (and you'll need to reformat it to be able to write files again). \$\endgroup\$
    – brhans
    Commented Mar 16, 2023 at 15:28

0

Browse other questions tagged or ask your own question.