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

Sending custom BLE characteristic to SD card

I have developed my own custom BLE service. I need to send BLE characteristic to an SD card. I have implemented the FATFS example and it works.

Now I'm trying to merge my custom BLE application with FATFS. However, I've not been able to link a BLE characteristic WRITE event to write to writing to FATFS. 

Here is the code snippet. Here I'm just trying to link a BLE WRITE event to a FATFS WRITE function to write a generic string to the file (not sending characteristic data).

However, on debugging, the program execution never seems to enter the fatfs_write() function. 

What could be happening? Is there something wrong in the program flow logic? Please help. 

Thanks

static void on_cus_evt(ble_cus_t     * p_cus_service,
                       ble_cus_evt_t * p_evt,
                       ble_evt_t const * p_ble_evt_main)
{
    ret_code_t err_code;
    
    switch(p_evt->evt_type)
    {
        
        case BLE_CUS_EVT_WRITE:

            nrf_gpio_pin_toggle(LED_3);
            printf("%s"," write to sd card ");
            flag =1;
            break;
        case 2: ....
        case 3: ....
}

void fatfs_write()
{
      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 ".");
                    printf("%s"," unable to open ");
                    return;
                }

                ff_result = f_write(&file, TEST_STRING, sizeof(TEST_STRING) - 1, (UINT *) &bytes_written);
                if (ff_result != FR_OK)
                {
                    NRF_LOG_INFO("Write failed\r\n.");
                    printf("%s"," write failed ");

                }
                else
                {
                    NRF_LOG_INFO("%d bytes written.", bytes_written);
                    printf("%s"," byted written ");
                }

                (void) f_close(&file);
                flag =0;
                return;
}

void main()
{
//initialize BLE, FATFS
for (;;)
    {
        if(flag)
        {
           fatfs_write();
        }
}
}

  • I have uploaded the project folder. I have been working on SES, with a nRF52832 Dev kit. I apologise if the code is not in a very neat format. Thanks for having a look.

    ble_custom_service.zip

  • Your example seems to work fine. This is the output from RTT terminal:

     sd card (uint32_t)fno.fname Hello World over RTT!
    hi<info> app: Initializing disk 0 (SDC)...
    <info> app: Capacity: 922 MB
    <info> app: Mounting volume...
    <info> app: 
     Listing directory: /
       <DIR>          89  NORDIC.TXT
         2000  BLE_2000.TXT
    <info> app: Template example started.
    <info> app: Fast advertising.
    <info> app: Connected to a previously bonded device.
    <info> app: Connected.
    <info> app: Connection secured: role: 1, conn_handle: 0x0, procedure: 0.
     here also 1  byted written <info> app: Writing to file NORDIC.TXT...
    <info> app: 25 bytes written.
    

  • Oh, that looks good. Thanks for checking it.

    But when I connect the SD card to my PC and open the TXT file, I don't see the new data in it.

    Also, for some reason, even after enabling RTT logging option in the SDK_CONFIG, I couldn't see the log data in the RTT viewer. That's why I had included printf statements after each log in fatfs_write(). But I don't see any of the printf statements from fatfs_write() in the output.

    Is there a something silly in what I'm doing?

  • Sounds like you have not connected the SD card correctly, or there is some issue with the initialization. I checked the content of the card, and do see the string " SD card example from BLE" in file NORDIC.TXT.

    RTT is working in your example, but you might not be able to use RTT Viewer when debugging. You should be able to see the RTT output in console window of SES when debugging.

    I can't see any UART initialization in your application, neither have you included/enabled the module retarget.c, which retargets the STDIO functions (i.e., forwards printf data to UART). You will not be able to use printf without this.

  • So when I run the FATFS example project, it writes the SD card, with the same hardware connections. I can see "SD card example" in NORDIC.TXT. But when I switch to the custom BLE service project, I don't see the new string written to the file.

    Sorry for being naive,  did you actually wire up hardware to an SD card reader and open the NORDIC.TXT file in the SD card?

    Also, without the UART initialization and retarget module, I see the other printf lines from main and the event handler. I just dont see printf lines from fatfs_write. 

Related