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

I want to control the timing of writing to the SD card by BLE communication

Hello

I currently use SDK 15.0.0 and write a program in SES. and use nrf52832(BLENano2).

I'm trying to merge BLE and fatfs examples.

After writing to the SD card in the main function, if you start advertising BLE, it works without any problems.

However, what I want to do is to start writing to the SD card when receiving 0x01 from the Android terminal by BLE communication, and to stop writing to the SD card when receiving 0x00.

In order to realize this, fatfs is added to "ble_app_blinky" of the SDK.
there
static void led_write_handler (uint16_t conn_handle, ble_lbs_t * p_lbs, uint8_t led_state)
When you receive 0x01

static void led_write_handler(uint16_t conn_handle, ble_lbs_t * p_lbs, uint8_t led_state)
{
	
    if (led_state)//receive 0x01
    {
    

		
    // 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);
    }
		       nrf_gpio_cfg_output(5);
    nrf_gpio_pin_set(5);

    
		
    uint32_t blocks_per_mb = (1024uL * 1024uL) / m_block_dev_sdc.block_dev.p_ops->geometry(&m_block_dev_sdc.block_dev)->blk_size;
    uint32_t capacity = m_block_dev_sdc.block_dev.p_ops->geometry(&m_block_dev_sdc.block_dev)->blk_count / blocks_per_mb;

		
    ff_result = f_mount(&fs, "", 1);
    ff_result = f_opendir(&dir, "/");
		
		
    do
    {
        ff_result = f_readdir(&dir, &fno);

        if (fno.fname[0])
        {
            if (fno.fattrib & AM_DIR)
            {
                NRF_LOG_RAW_INFO("   <DIR>   %s",(uint32_t)fno.fname);
            }
            else
            {
                  NRF_LOG_RAW_INFO("%9lu  %s", fno.fsize, (uint32_t)fno.fname);
            }
        }
    }
    while (fno.fname[0]);

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

   
    
	       uint32_t bytes_written;
			
					
			time += t;
		
			*buf[0] = time;
            *buf[1] = m_sample[0];//x
            *buf[2] = m_sample[1];
            *buf[3] = m_sample[2];//y
            *buf[4] = m_sample[3];
            *buf[5] = m_sample[4];//z
            *buf[6] = m_sample[5];
					
				
			f_write(&file, *buf, 56 , (UINT *) &bytes_written);//time
            f_write(&file, "\r\n", 2 ,&bytes_written);

    }
    else//receive 0x00
    {
    
    f_close(&file);

    }
}

I want to do something like the code above,

for (uint32_t retries = 3; retries && disk_state; --retries)
     {
         disk_state = disk_initialize (0);
     }


Will stop the program.

Can I do "disk_initialize" except for the main function?

Also, is it possible to set up to f_open such as "disk_initialize" in the main function and execute f_write only when 0x01 is written?
I would like to know if there are other ways.

Thank you.

Related