This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Read 4 Digital Input and Store it in SD Card , Read Stored data from SD card and pass it to the phone

#include "nrf.h"
#include "bsp.h"
#include "ff.h"
#include "diskio_blkdev.h"
#include "nrf_block_dev_sdc.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf_gpiote.h"
#include "nrf_gpio.h"
#include "nrf_drv_gpiote.h"
#include "nrf_delay.h"

#define FILE_NAME   "Clubfoot.CSV"
//#define TEST_STRING "01"

#define  LED 17
#define  BTN 31
int c;
int MAXSTRLENGTH[100];

#define SDC_SCK_PIN     ARDUINO_13_PIN  ///< SDC serial clock (SCK) pin.
#define SDC_MOSI_PIN    ARDUINO_11_PIN  ///< SDC serial data in (DI) pin.
#define SDC_MISO_PIN    ARDUINO_12_PIN  ///< SDC serial data out (DO) pin.
#define SDC_CS_PIN      ARDUINO_10_PIN  ///< SDC chip select (CS) pin.

NRF_BLOCK_DEV_SDC_DEFINE(
        m_block_dev_sdc,
        NRF_BLOCK_DEV_SDC_CONFIG(
                SDC_SECTOR_SIZE,
                APP_SDCARD_CONFIG(SDC_MOSI_PIN, SDC_MISO_PIN, SDC_SCK_PIN, SDC_CS_PIN)
         ),
         NFR_BLOCK_DEV_INFO_CONFIG("Nordic", "SDC", "1.00")
);

/**
 * @brief Function for demonstrating FAFTS usage.
 */
 void fatfs_example(int *p)
{
int* s;
s = (int*)malloc(100);	
for (int i=0; i<100; i++) {
       s[i] = *p;
        p++;
       nrf_delay_ms(10);

}
    static FATFS fs;
    static DIR dir;
    static FILINFO fno;
    static FIL file;

    uint32_t bytes_written;
    FRESULT ff_result;
    DSTATUS disk_state = STA_NOINIT;

    // Initialize FATFS disk I/O interface by providing the block device.
    static diskio_blkdev_t drives[] =
    {
            DISKIO_BLOCKDEV_CONFIG(NRF_BLOCKDEV_BASE_ADDR(m_block_dev_sdc, block_dev), NULL)
    };

    diskio_blockdev_register(drives, ARRAY_SIZE(drives));

    for (uint32_t retries = 3; retries && disk_state; --retries)
    {
        disk_state = disk_initialize(0);
    }
    if (disk_state)
    {
        NRF_LOG_INFO("Disk initialization failed.");
        return;
    }

  
    ff_result = f_mount(&fs, "", 1);
    if (ff_result)
    {
        NRF_LOG_INFO("Mount failed.");
        return;
    }

    ff_result = f_opendir(&dir, "/");
    if (ff_result)
    {
        NRF_LOG_INFO("Directory listing failed!");
        return;
    }

   
        ff_result = f_readdir(&dir, &fno);
        if (ff_result != FR_OK)
        {
            NRF_LOG_INFO("Directory read failed.");
            return;
        }

    NRF_LOG_RAW_INFO("");

    NRF_LOG_INFO("Writing to file " FILE_NAME "...");

    ff_result = f_open(&file, FILE_NAME, FA_READ | FA_WRITE | FA_OPEN_APPEND);

    if (ff_result != FR_OK)
    {
        NRF_LOG_INFO("Unable to open or create file: " FILE_NAME ".");
        return;
    }

    ff_result = f_write(&file, s, sizeof(s), (UINT *) &bytes_written);
   
    if (ff_result != FR_OK)
    {
        NRF_LOG_INFO("Write failed\r\n.");
    }
    else
    {
        NRF_LOG_INFO("%d bytes written.", bytes_written);
    }

    (void) f_close(&file);
    return;

}

char *string_function(int *astring){

/*
char* s;
s = (char*)malloc(MAXSTRLENGTH);
s[0]=0;
strcat(s,astring);
strcat(s,"\n");
return  s;

*/
}

int main(void)
{
  
   nrf_gpio_cfg_input(BTN,NRF_GPIO_PIN_PULLUP);
   nrf_gpio_cfg_output(LED);

   bsp_board_init(BSP_INIT_LEDS);

   APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
   NRF_LOG_DEFAULT_BACKENDS_INIT();

   NRF_LOG_INFO("FATFS example started.");

   

for (int i = 0; i < 100; i++) {
        c = nrf_gpio_pin_read(BTN); 

		MAXSTRLENGTH[i] = c;
                  nrf_delay_ms(100);
}

fatfs_example(MAXSTRLENGTH);
    
    
    while (true)
    {
       
       
     }
}

I am new in nordic and just getting started with the nRF52832-DK. . Can someone please help me ? How to read 4 Digital Input and store it into SD card.and then pass that stored data into the phone.I am able to read single Digital Input.But couldn't store the Data onto the SD card properly ,when I write it only 4bytes get written which is garbage values.Can someone please help me what steps I need to follow. I tried modifying the examples code(fatfs) but I couldn't make it work.

Thanks

Parents Reply Children
Related