<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Enable USB mass storage (SD card) only when USB connected</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/114838/enable-usb-mass-storage-sd-card-only-when-usb-connected</link><description>Hi, 
 I am working on a battery powered device that collects data, save it to files and go to sleep, when it is not connected to a laptop via USB. When connected, the device just become a &amp;quot;usb sd card reader&amp;quot; that shows the files, the sensors are disabled</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 20 Sep 2024 10:43:42 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/114838/enable-usb-mass-storage-sd-card-only-when-usb-connected" /><item><title>RE: Enable USB mass storage (SD card) only when USB connected</title><link>https://devzone.nordicsemi.com/thread/503241?ContentTypeID=1</link><pubDate>Fri, 20 Sep 2024 10:43:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ab5d98e1-30df-42be-a111-b4fb8050a81c</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Thanks for providing the solution. I would assume it&amp;#39;s not related to the USB communication but more on how the SPI is handled before SystemOFF.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Enable USB mass storage (SD card) only when USB connected</title><link>https://devzone.nordicsemi.com/thread/503170?ContentTypeID=1</link><pubDate>Thu, 19 Sep 2024 18:38:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8d43943d-baea-43b2-adf4-66f7e3914f2f</guid><dc:creator>ax_shepherd</dc:creator><description>&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/104080/mpu-fault-stacking-error-data-access-violation-when-a-sensor-device-is-added?ReplyFilter=Answers&amp;amp;ReplySortBy=Answers&amp;amp;ReplySortOrder=Descending"&gt;This &lt;/a&gt;post lead me to the solution.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;pm_device_action_run(dev_spi.bus, PM_DEVICE_ACTION_SUSPEND);&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Enable USB mass storage (SD card) only when USB connected</title><link>https://devzone.nordicsemi.com/thread/503140?ContentTypeID=1</link><pubDate>Thu, 19 Sep 2024 13:49:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fa213f3a-fe60-4300-a990-8a65893a3317</guid><dc:creator>ax_shepherd</dc:creator><description>&lt;p&gt;The &amp;quot;Division by zero&amp;quot; error was due to something else that I missed, I now fixed that issue. I now have a &amp;quot;Card error on CMD0&amp;quot; error on the first wake up after initial boot where the data saving on SD works.&lt;/p&gt;
&lt;p&gt;As a bad workaround I added&amp;nbsp;&lt;span&gt;sys_reboot&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;SYS_REBOOT_WARM&lt;/span&gt;&lt;span&gt;); when the SD initialisation fails and this somewhat gives me the behaviour I expect but not really fixing the bug. It seems that just umounting the SD is not enough&amp;nbsp;it also needs to me&amp;nbsp;uninitialized somehow when USB MSC is enabled. With &amp;quot;&lt;/span&gt;&lt;span&gt;CONFIG_USB_MASS_STORAGE&lt;/span&gt;&lt;span&gt;=n&amp;quot;&amp;nbsp;&lt;/span&gt;&lt;span&gt;I can write to the SD after waking up from&amp;nbsp;&lt;/span&gt;sys_poweroff without problem.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void sd_card_write_work_handler(struct k_work *work) {
    LOG_INF(&amp;quot;sd_card_write_work_handler called&amp;quot;);
    // Lock the mutex to protect buffer access during SD card writing
    if (usb_connected == 0){
        k_mutex_lock(&amp;amp;buffer_mutex, K_FOREVER);
        if (sd_card_initialized == 0) {
            LOG_INF(&amp;quot;Unmount disk...&amp;quot;);
            turn_off_sd(0);
            int ret = sd_card_init();
            if (ret != 0) {
                LOG_ERR(&amp;quot;Failed to initialize SD card&amp;quot;);
                turn_off_sd(0);
                k_mutex_unlock(&amp;amp;buffer_mutex); // Unlock mutex before returning
                sys_reboot(SYS_REBOOT_WARM);
                //sys_poweroff();
                //return;
            }
            save_sd_init_config(1);
        }else{
            LOG_INF(&amp;quot;SD card already initialised.&amp;quot;);
        }

        struct tm datetime = get_datetime();
        char hwid[HW_ID_LEN] = &amp;quot;unknown&amp;quot;;
        get_hwid(hwid);
        const char *ext = &amp;quot;.csv&amp;quot;;
        char filename[32];
        snprintf(filename, sizeof(filename), &amp;quot;%s_%04d_%02d_%02d%s&amp;quot;,
                hwid,
                datetime.tm_year + 1900,
                (datetime.tm_mon + 1),
                datetime.tm_mday,
                ext);
        LOG_INF(&amp;quot;filename:%s&amp;quot;, filename);

        for (int i = 0; i &amp;lt; record_count; i++) {
            size_t size = strlen(records[i]);
            //turn_buzzer_off();
            int ret = sd_card_open_write_close(filename, records[i], &amp;amp;size);
            if (ret) {
                LOG_ERR(&amp;quot;Failed to write to file. Ret: %d&amp;quot;, ret);
            } 
        }
        if (!motion_detected) {
            LOG_INF(&amp;quot;Unmount disk...&amp;quot;);
            unmount_sd_card();
            LOG_INF(&amp;quot;Unmount done.&amp;quot;);
            turn_off_sd(0);
            
            record_count = 0;
            if(!ble_initialized){
                // if (!usb_connected){
                LOG_INF(&amp;quot;CONFIG acc_sensitivity:%f acc_sampling:%d usb_connected:%d usb_call_back_set:%d sd_card_initialized:%d&amp;quot;, 
	            acc_sensitivity, acc_sampling, usb_connected, usb_call_back_set, sd_card_initialized);
                LOG_INF(&amp;quot;Going to sleep...&amp;quot;);
                save_usb_callback_config(0);
                k_msleep(100);
                sys_poweroff();
                // }else{
                //     LOG_INF(&amp;quot;USB connected stay awake...&amp;quot;);
                //     sd_card_init();
                //     sd_card_initialized = true;
                // }
            }else{
                LOG_INF(&amp;quot;BLE enabled...&amp;quot;);
            }
        } else {
            LOG_INF(&amp;quot;Motion detected, staying awake&amp;quot;);
        }
        // Unlock the mutex after finishing buffer access
        k_mutex_unlock(&amp;amp;buffer_mutex);
    }else{
        LOG_INF(&amp;quot;CONFIG acc_sensitivity:%f acc_sampling:%d usb_connected:%d usb_call_back_set:%d sd_card_initialized:%d&amp;quot;, 
	    acc_sensitivity, acc_sampling, usb_connected, usb_call_back_set, sd_card_initialized);
        LOG_INF(&amp;quot;Writing new file disabled when usb connected.&amp;quot;);
    }

}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;SEGGER J-Link V7.94e - Real time terminal output
SEGGER J-Link (unknown) V1.0, SN=801046272
Process: JLink.exe
[00:00:00.003,906] &amp;lt;inf&amp;gt; lis2dh: LIS2DH: int2 on gpio@842500.28
[00:00:00.006,134] &amp;lt;inf&amp;gt; lis2dh: fs=2, odr=0x3 lp_en=0x8 scale=9576
[00:00:00.007,598] &amp;lt;inf&amp;gt; pcf85063a: pcf85063a@51 is initialized!
*** Booting nRF Connect SDK v3.5.99-ncs1-1 ***
[00:00:00.160,797] &amp;lt;inf&amp;gt; main: Hi! DFU Starting nrf5340dk_nrf5340_cpuapp
[00:00:11.176,910] &amp;lt;inf&amp;gt; nvs_storage: set SD_INIT config 0 at id 7
[00:00:11.177,764] &amp;lt;inf&amp;gt; nvs_storage: set acc sensitivity config 1.200 at id 4
[00:00:11.178,527] &amp;lt;inf&amp;gt; nvs_storage: config: 4, acc_sensitivity: 1.200
[00:00:11.186,676] &amp;lt;inf&amp;gt; nvs_storage: set acc sampling config 1 at id 3
[00:00:11.187,377] &amp;lt;inf&amp;gt; nvs_storage: config: 3, acc_sampling: 1
[00:00:11.187,835] &amp;lt;inf&amp;gt; nvs_storage: The integer value is: 1
[00:00:11.188,354] &amp;lt;inf&amp;gt; nvs_storage: config: 7, SD_INIT: 0
[00:00:11.188,781] &amp;lt;inf&amp;gt; nvs_storage: The integer value is: 0
[00:00:12.189,270] &amp;lt;inf&amp;gt; main: RTC init...
[00:00:13.190,277] &amp;lt;inf&amp;gt; main: Enable USB...
[00:00:13.198,211] &amp;lt;inf&amp;gt; nvs_storage: set USB_CALLBACK config 1 at id 6
[00:00:14.198,791] &amp;lt;inf&amp;gt; main: CONFIG acc_sensitivity:1.200000 acc_sampling:1 usb_connected:0 usb_call_back_set:1 sd_card_initialized:0
[00:00:14.199,951] &amp;lt;inf&amp;gt; accelerometer: Setting triggers for motion detection...
[00:00:14.203,979] &amp;lt;inf&amp;gt; lis2dh: int2_ths=0x4c range_g=2 ums2=11767979
[00:00:14.206,329] &amp;lt;inf&amp;gt; lis2dh: int2_dur=0x0
[00:00:15.207,733] &amp;lt;dbg&amp;gt; main: main: Done.
[00:00:31.012,451] &amp;lt;inf&amp;gt; accelerometer: Motion detected, starting data collection
[00:00:31.012,969] &amp;lt;inf&amp;gt; accelerometer: Motion detected, starting data collection, usb_connected:0 sd_card_initialized:0
[00:00:36.014,221] &amp;lt;inf&amp;gt; accelerometer: Motion timer expired, stopping data collection
[00:00:36.014,739] &amp;lt;inf&amp;gt; accelerometer: Submit to queue...
[00:00:36.015,167] &amp;lt;inf&amp;gt; accelerometer: sd_card_write_work_handler called
[00:00:36.015,441] &amp;lt;inf&amp;gt; accelerometer: Unmount disk...
[00:00:36.204,864] &amp;lt;inf&amp;gt; nvs_storage: set SD_INIT config 1 at id 7
[00:00:36.206,634] &amp;lt;inf&amp;gt; accelerometer: filename:2BFE6027164802EE_2020_04_12.csv
[00:00:36.221,466] &amp;lt;inf&amp;gt; accelerometer: Unmount disk...
[00:00:36.221,710] &amp;lt;inf&amp;gt; accelerometer: Disk unmounted.
[00:00:36.227,752] &amp;lt;inf&amp;gt; nvs_storage: set SD_INIT config 0 at id 7
[00:00:36.228,210] &amp;lt;inf&amp;gt; accelerometer: Unmount done.
[00:00:36.228,576] &amp;lt;inf&amp;gt; accelerometer: CONFIG acc_sensitivity:1.200000 acc_sampling:1 usb_connected:0 usb_call_back_set:1 sd_card_initialized:0
[00:00:36.229,156] &amp;lt;inf&amp;gt; accelerometer: Going to sleep...
[00:00:36.235,229] &amp;lt;inf&amp;gt; nvs_storage: set USB_CALLBACK config 0 at id 6
[00:00:00.003,875] &amp;lt;inf&amp;gt; lis2dh: LIS2DH: int2 on gpio@842500.28
[00:00:00.006,042] &amp;lt;inf&amp;gt; lis2dh: fs=2, odr=0x3 lp_en=0x8 scale=9576
[00:00:00.007,476] &amp;lt;inf&amp;gt; pcf85063a: pcf85063a@51 is initialized!
*** Booting nRF Connect SDK v3.5.99-ncs1-1 ***
[00:00:00.014,892] &amp;lt;err&amp;gt; usb_msc: Storage init ERROR !!!! - Aborting USB init
[00:00:00.015,747] &amp;lt;inf&amp;gt; main: Hi! DFU Starting nrf5340dk_nrf5340_cpuapp
[00:00:11.031,616] &amp;lt;inf&amp;gt; nvs_storage: set SD_INIT config 0 at id 7
[00:00:11.032,165] &amp;lt;inf&amp;gt; nvs_storage: config: 4, acc_sensitivity: 1.200
[00:00:11.032,714] &amp;lt;inf&amp;gt; nvs_storage: config: 3, acc_sampling: 0
[00:00:11.033,172] &amp;lt;inf&amp;gt; nvs_storage: The integer value is: 0
[00:00:11.033,721] &amp;lt;inf&amp;gt; nvs_storage: config: 7, SD_INIT: 0
[00:00:11.034,149] &amp;lt;inf&amp;gt; nvs_storage: The integer value is: 0
[00:00:12.034,606] &amp;lt;inf&amp;gt; main: RTC init...
[00:00:13.035,614] &amp;lt;inf&amp;gt; main: Enable USB...
[00:00:13.043,518] &amp;lt;inf&amp;gt; nvs_storage: set USB_CALLBACK config 1 at id 6
[00:00:14.044,250] &amp;lt;inf&amp;gt; main: CONFIG acc_sensitivity:1.200000 acc_sampling:0 usb_connected:0 usb_call_back_set:1 sd_card_initialized:0
[00:00:14.045,410] &amp;lt;inf&amp;gt; accelerometer: Setting triggers for motion detection...
[00:00:14.049,438] &amp;lt;inf&amp;gt; lis2dh: int2_ths=0x4c range_g=2 ums2=11767979
[00:00:14.051,788] &amp;lt;inf&amp;gt; lis2dh: int2_dur=0x0
[00:00:14.053,710] &amp;lt;inf&amp;gt; accelerometer: Motion detected, starting data collection
[00:00:14.054,229] &amp;lt;inf&amp;gt; accelerometer: Motion detected, starting data collection, usb_connected:0 sd_card_initialized:0
[00:00:15.053,283] &amp;lt;dbg&amp;gt; main: main: Done.
[00:00:19.055,480] &amp;lt;inf&amp;gt; accelerometer: Motion timer expired, stopping data collection
[00:00:19.055,969] &amp;lt;inf&amp;gt; accelerometer: Submit to queue...
[00:00:19.056,396] &amp;lt;inf&amp;gt; accelerometer: sd_card_write_work_handler called
[00:00:19.056,701] &amp;lt;inf&amp;gt; accelerometer: Unmount disk...
[00:00:19.091,125] &amp;lt;err&amp;gt; sd: Card error on CMD0
[00:00:19.091,369] &amp;lt;err&amp;gt; sd_card: SD card init failed, please check if SD card inserted
[00:00:19.091,705] &amp;lt;err&amp;gt; accelerometer: Failed to initialize SD card
[00:00:00.003,875] &amp;lt;inf&amp;gt; lis2dh: LIS2DH: int2 on gpio@842500.28
[00:00:00.006,042] &amp;lt;inf&amp;gt; lis2dh: fs=2, odr=0x3 lp_en=0x8 scale=9576
[00:00:00.007,476] &amp;lt;inf&amp;gt; pcf85063a: pcf85063a@51 is initialized!&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Enable USB mass storage (SD card) only when USB connected</title><link>https://devzone.nordicsemi.com/thread/503123?ContentTypeID=1</link><pubDate>Thu, 19 Sep 2024 12:20:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:df582571-9d8e-44b6-a607-eb6be937b8c1</guid><dc:creator>ax_shepherd</dc:creator><description>&lt;p&gt;Hi Hung,&lt;br /&gt;Thank you for the quick reply. Unfortunately, I do not have a DK. For sleep mode, I am using System OFF with sys_poweroff();, and I wake up from a GPIO interrupt triggered by the accelerometer.&lt;br /&gt;I will check for the &amp;quot;Division by zero&amp;quot; error and update you as soon as I find the cause. However, the &amp;quot;usb_msc: Storage init ERROR&amp;quot; still occurs on wake-up.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Enable USB mass storage (SD card) only when USB connected</title><link>https://devzone.nordicsemi.com/thread/503105?ContentTypeID=1</link><pubDate>Thu, 19 Sep 2024 11:31:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8ab7a60b-ab5d-4429-808b-3f5ff53a7ed3</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Axel,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Can you reproduce the issue with a DK (use ramdisk)?&amp;nbsp;&lt;br /&gt;&lt;br /&gt;When you say going to sleep, is it System OFF sleep or System On Idle ?&amp;nbsp;&lt;br /&gt;&lt;br /&gt;The error is &amp;quot;&amp;nbsp;Division by zero&amp;quot;, have you checked if it&amp;#39;s related to USB or something else ?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>