<?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>[SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/114453/spim-nus-conflict-between-the-spim-and-ble-thread</link><description>I am programming with the nRF52840DKs. [Toolchain Manager: v1.3.0, IDE: Visual Studio Code (VSCode), SDK: ncs v2.6.0, window11 pro] 
 I am currently working on integrating &amp;#39;NUS_peripheral&amp;#39; and &amp;#39;SPIM&amp;#39;. 
 My goal: communicate with the ADC via SPIM and transmit</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 10 Sep 2024 13:21:22 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/114453/spim-nus-conflict-between-the-spim-and-ble-thread" /><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501953?ContentTypeID=1</link><pubDate>Tue, 10 Sep 2024 13:21:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d24ac47a-2b28-4923-b7c0-8c08d9d0f3d6</guid><dc:creator>seongmincho</dc:creator><description>&lt;p&gt;I just realized that some data might have been lost during the NUS or UART process... I&amp;#39;m really sorry. I&amp;#39;ll review both NUS and UART.&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;/strong&gt;+&amp;nbsp;I have discovered an issue where the initial packets are missing on the central side. &lt;br /&gt;Thank you so much for your help so far. &lt;br /&gt;I will address the data loss issue with NUS and UART in a separate ticket. Your advice has been helpful to me&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501873?ContentTypeID=1</link><pubDate>Tue, 10 Sep 2024 09:45:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dba07792-77a1-4a88-a6c8-70dc7ec7316a</guid><dc:creator>seongmincho</dc:creator><description>[quote userid="2121" url="~/f/nordic-q-a/114453/spim-nus-conflict-between-the-spim-and-ble-thread/501736"]Please show your test result with some dummy data so I can get to understand what your problem is. Please provide the log.[/quote]
&lt;p&gt;&lt;span&gt;okay.&lt;/span&gt;&lt;span&gt;I&amp;#39;ll get back to you as soon as possible.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501856?ContentTypeID=1</link><pubDate>Tue, 10 Sep 2024 08:18:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:89766e43-7533-465b-af91-b2dcf0bc0865</guid><dc:creator>seongmincho</dc:creator><description>&lt;p&gt;&lt;span&gt;First of all, thank you so much for your response.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&amp;lt; Test Results for SPIM and NUS &amp;gt;&lt;br /&gt;1.&amp;nbsp;SPIM (Executed Individually)&lt;br /&gt;&amp;nbsp; &amp;nbsp;-&amp;nbsp;SPIM communication occurs consistently according to the &amp;#39;TIME_TO_WAIT_US&amp;#39; variable.&lt;br /&gt;&amp;nbsp; &amp;nbsp;-&amp;nbsp;It was confirmed that SPIM works correctly when TIME_TO_WAIT_US is above a certain threshold.&lt;br /&gt;&amp;nbsp; &amp;nbsp;-&amp;nbsp;&amp;#39;result for spim_tx_buf_repeat&amp;#39; was printed successfully. &amp;#39;result for spim_tx_buf_initial&amp;#39; was printed successfully.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;2.&amp;nbsp;NUS (Executed Individually)&lt;br /&gt;&amp;nbsp; -&amp;nbsp;NUS functions well when executed individually.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;3.&amp;nbsp;SPIM + NUS Simultaneous Execution &lt;br /&gt;&amp;nbsp; &amp;nbsp;- When SPIM and NUS are executed simultaneously, the issue previously mentioned occurs again.&lt;/p&gt;
[quote userid="132399" url="~/f/nordic-q-a/114453/spim-nus-conflict-between-the-spim-and-ble-thread/501625"]&lt;strong&gt;There are a few issues. The SPIM result for the spim_tx_buf_initial command is missing, and only the result for spim_tx_buf_repeat is printed to NUS.&amp;nbsp;&lt;/strong&gt;&lt;span style="font-size:inherit;"&gt;To address this, I added a few variables for debugging as shown below.(The commented-out section is the part that has been modified.)&lt;/span&gt;[/quote]&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501736?ContentTypeID=1</link><pubDate>Mon, 09 Sep 2024 12:35:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9cb605f4-958f-4c12-a6e8-cac535b4f9d2</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Seongmin,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It&amp;#39;s getting quite hard to follow here.&amp;nbsp;&lt;br /&gt;My suggestion is to try to isolate the problem and simplify the application so you can spot where the issue is.&amp;nbsp;&lt;br /&gt;As I mentioned, please try to simply printout on the log the fifo buffer instead of sending it over BLE. This way we can take BLE out of the picture and focus the debugging on SPI and the FIFO buffer.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Please show your test result with some dummy data so I can get to understand what your problem is. Please provide the log.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501625?ContentTypeID=1</link><pubDate>Mon, 09 Sep 2024 01:10:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:006584c9-920c-43e4-8106-e222fd46762e</guid><dc:creator>seongmincho</dc:creator><description>&lt;p&gt;Hello. I followed your advice and coded accordingly. Below are the modified sections, while the rest remains the same as in the previous code&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static struct fifo_data_t {
   void *fifo_reserved; // This must be the first element for FIFO
   uint8_t data[122][2]; // Buffer to hold the data
   uint16_t len; // Length of the data 
};

K_FIFO_DEFINE(nus_fifo);

struct fifo_data_t *spim_buf = NULL; 
static volatile uint16_t buffer_index = 0;

//////////////////////////////////////////////////

void spim1_handler(nrfx_spim_evt_t const * p_event, void * p_context) { 

   if (spim_buf == NULL) {
      spim_buf = k_malloc(sizeof(struct fifo_data_t));
      if (spim_buf == NULL) {
         LOG_ERR(&amp;quot;Failed to allocate memory for FIFO buffer:%d&amp;quot;, buffer_index);
         return;
      }
      spim_buf-&amp;gt;len = 0;  
   }

   memcpy(spim_buf-&amp;gt;data[buffer_index], spim_rx_buf_A[buffer_index], 2);   
   buffer_index++;
   spim_buf-&amp;gt;len++;

   if (!initialization_stop) {
      initialization_counter++;

      if (initialization_counter == 30) {
         initialization_stop = true;
         spim1_inst.p_reg-&amp;gt;TXD.PTR = (uint32_t)spim_tx_buf_repeat[0];
      }

      return;
   }

   if (buffer_index &amp;gt;= 120){    //if (buffer_index &amp;gt;= 100 &amp;amp;&amp;amp; repeat_counter % 18 == 0){
      k_fifo_put(&amp;amp;nus_fifo, spim_buf); 
      
      spim1_inst.p_reg-&amp;gt;RXD.PTR = (uint32_t)spim_rx_buf_A[0];
      buffer_index = 0;

      spim_buf = NULL;
   }

   repeat_counter++;
   if (repeat_counter % 18 == 0) {
      spim1_inst.p_reg-&amp;gt;TXD.PTR = (uint32_t)spim_tx_buf_repeat[0];
      repeat_counter = 0;
   }
}

void ble_write_thread(void){
   int ret;
   struct fifo_data_t *nus_buf;

   k_sem_take(&amp;amp;nus_start, K_FOREVER);
   LOG_WRN(&amp;quot;Starting BLE Write Thread&amp;quot;);
   while (1) {
      nus_buf = k_fifo_get(&amp;amp;nus_fifo, K_FOREVER);
      ret = bt_nus_send(current_conn, (uint8_t *)nus_buf-&amp;gt;data, 2*(nus_buf-&amp;gt;len));

      k_free(nus_buf);

      if (k_sem_take(&amp;amp;disconnect_sem, K_NO_WAIT) == 0){
         LOG_WRN(&amp;quot;Disconnected, exiting BLE Write Thread&amp;quot;);
         return;
      }
   }
}

K_THREAD_DEFINE(ble_write_thread_id, 8192, ble_write_thread, NULL, NULL, NULL, PRIORITY, 0, 0);
&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;There are a few issues. The SPIM result for the spim_tx_buf_initial command is missing, and only the result for spim_tx_buf_repeat is printed to NUS.&amp;nbsp;&lt;/strong&gt;&lt;span style="font-size:inherit;"&gt;To address this, I added a few variables for debugging as shown below.(The commented-out section is the part that has been modified.)&lt;/span&gt; &lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;// #define MAX_SPI_COUNT 400
// static K_SEM_DEFINE(debugging, 0, 1);
// uint32_t recording[MAX_SPI_COUNT];
// int spim_count = 0;
///////////////////////////////////////////////////

// void spi_disable(NRF_SPIM_Type *p_spim_instance){
//     nrfy_spim_disable(p_spim_instance);
//     nrf_spim_int_disable(p_spim_instance, 0xFFFFFFFF);
// }

// void timer_disable(nrfx_timer_t *p_timer_instance){
//     nrfx_timer_disable(p_timer_instance);
//     nrfx_timer_uninit(p_timer_instance);
// }  

void spim1_handler(nrfx_spim_evt_t const * p_event, void * p_context) { 
   //LOG_INF(&amp;quot;spim1_handler- buffer_index: %d, spim_buf-&amp;gt;len: %d &amp;quot;,buffer_index, spim_buf-&amp;gt;len);
   //LOG_INF(&amp;quot;spim1_handler- initialization_counter: %d&amp;quot;,initialization_counter);
   //LOG_INF(&amp;quot;spim1_handler- 0x%02x%02x&amp;quot;,spim_rx_buf_A[buffer_index][0], spim_rx_buf_A[buffer_index][1]);
   // LOG_INF(&amp;quot;spim1_handler- %d &amp;gt;&amp;gt; RXD.PTR: 0x%08x&amp;quot;, buffer_index, (uint32_t)spim1_inst.p_reg-&amp;gt;RXD.PTR);
   // LOG_INF(&amp;quot;spim1_handler- %d &amp;gt;&amp;gt; TXD.PTR: 0x%08x&amp;quot;, buffer_index, (uint32_t)spim1_inst.p_reg-&amp;gt;TXD.PTR);
   // if(spim_count &amp;gt;= MAX_SPI_COUNT){                
   //    timer_disable(&amp;amp;timer1_inst);
   //    spi_disable(spim1_inst.p_reg);
   //    k_sem_give(&amp;amp;debugging);
   //    LOG_INF(&amp;quot;&amp;lt;&amp;lt;&amp;lt;&amp;lt; SPIM+TIMER END  &amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;quot;);
   //    return;
   // } 
   //recording[spim_count] = (uint32_t)spim1_inst.p_reg-&amp;gt;RXD.PTR;
   //recording[spim_count] = (uint32_t)initialization_counter;
   //recording[spim_count] = (uint32_t)initialization_stop;
   //recording[spim_count] = (uint32_t)repeat_counter;
   // spim_count++;

   if (spim_buf == NULL) {
      spim_buf = k_malloc(sizeof(struct fifo_data_t));
      if (spim_buf == NULL) {
         LOG_ERR(&amp;quot;Failed to allocate memory for FIFO buffer:%d&amp;quot;, buffer_index);
         return;
      }
      spim_buf-&amp;gt;len = 0;  // Initialize the length of the data
   }

   memcpy(spim_buf-&amp;gt;data[buffer_index], spim_rx_buf_A[buffer_index], 2);    //void *memcpy(void *dest, const void *src, size_t n); 
   //recording[spim_count] = (uint32_t)buffer_index;

   buffer_index++;
   spim_buf-&amp;gt;len++;

   if (!initialization_stop) {
      initialization_counter++;

      if (initialization_counter == 30) {
         initialization_stop = true;
         spim1_inst.p_reg-&amp;gt;TXD.PTR = (uint32_t)spim_tx_buf_repeat[0];
      }

      return;
   }

   if (buffer_index &amp;gt;= 120){    //if (buffer_index &amp;gt;= 100 &amp;amp;&amp;amp; repeat_counter % 18 == 0){

      k_fifo_put(&amp;amp;nus_fifo, spim_buf); 
      
      spim1_inst.p_reg-&amp;gt;RXD.PTR = (uint32_t)spim_rx_buf_A[0];
      buffer_index = 0;

      spim_buf = NULL;
   }

   repeat_counter++;
   if (repeat_counter % 18 == 0) {
      spim1_inst.p_reg-&amp;gt;TXD.PTR = (uint32_t)spim_tx_buf_repeat[0];
      repeat_counter = 0;
   }
}

int main(void){
   nrfx_err_t status;
   (void)status;
   int err;

   LOG_INF(&amp;quot; -----------      revision      ----------- &amp;quot;);

   /************ BLE ***********/ 
   bt_conn_cb_register(&amp;amp;conn_callbacks); 
   err = bt_conn_auth_info_cb_register(&amp;amp;conn_auth_info_callbacks); 
   //LOG_INF(&amp;quot;main - bt_conn_auth_info_cb_register: %d&amp;quot;,err);
   err = bt_enable(NULL);
   //LOG_INF(&amp;quot;main - bt_enable: %d&amp;quot;,err);
   if (IS_ENABLED(CONFIG_SETTINGS)) {
      settings_load();
   }
   err = bt_nus_init(&amp;amp;nus_cb);
   //LOG_INF(&amp;quot;main - bt_nus_init(&amp;amp;nus_cb): %d&amp;quot;,err);
   err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
   //LOG_INF(&amp;quot;main - bt_le_adv_start: %d&amp;quot;,err);   
   k_sem_take(&amp;amp;ble_setup_sem1, K_FOREVER);
   k_sem_take(&amp;amp;ble_setup_sem2, K_FOREVER);
   k_sem_take(&amp;amp;ble_setup_sem3, K_FOREVER);

   /************ SPIM ************/ 
   initialization_command();
   #if defined(__ZEPHYR__)
      IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_SPIM_INST_GET(SPIM_INST_IDX)), IRQ_PRIO_LOWEST,     
                              NRFX_SPIM_INST_HANDLER_GET(SPIM_INST_IDX), 0, 0);
      IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_TIMER_INST_GET(TIMER_INST_IDX)), IRQ_PRIO_LOWEST, 
                              NRFX_TIMER_INST_HANDLER_GET(TIMER_INST_IDX), 0, 0);
   #endif
   LOG_INF(&amp;quot;main - TIME_TO_WAIT_US: %d [us],  total_sampling_rate: %ld [Hz],  per_ch_SAMPLE_RATE: %ld [Hz]&amp;quot;, TIME_TO_WAIT_US, total_sampling_rate, per_ch_SAMPLE_RATE);
   status = nrfx_gpiote_init(&amp;amp;gpiote_inst, NRFX_GPIOTE_DEFAULT_CONFIG_IRQ_PRIORITY);
   //LOG_INF(&amp;quot;main - nrfx_gpiote_init: %d&amp;quot;, status);
   status = nrfx_gpiote_channel_alloc(&amp;amp;gpiote_inst, &amp;amp;out_channel);
   //LOG_INF(&amp;quot;main - nrfx_gpiote_channel_alloc: %d&amp;quot;, status);
   LOG_INF(&amp;quot;---------------------------------------------------------------------&amp;quot;);
   LOG_INF(&amp;quot;main - spim_tx_buf_initial address[0]: %p&amp;quot;, (void*)&amp;amp;spim_tx_buf_initial[0][0]);
   LOG_INF(&amp;quot;main - spim_tx_buf_initial address[29]: %p&amp;quot;, (void*)&amp;amp;spim_tx_buf_initial[29][0]);
   LOG_INF(&amp;quot;main - spim_tx_buf_repeat address[0]: %p&amp;quot;, (void*)&amp;amp;spim_tx_buf_repeat[0][0]);
   LOG_INF(&amp;quot;main - spim_tx_buf_repeat address[17]: %p&amp;quot;, (void*)&amp;amp;spim_tx_buf_repeat[17][0]);
   LOG_INF(&amp;quot;main - spim_rx_buf_A address[0]: %p&amp;quot;, (void*)&amp;amp;spim_rx_buf_A[0][0]);
   LOG_INF(&amp;quot;main - spim_rx_buf_A address[243]: %p&amp;quot;, (void*)&amp;amp;spim_rx_buf_A[243][0]);

   LOG_INF(&amp;quot;---------------------------------------------------------------------&amp;quot;);
/*    for(int y=0 ; y &amp;lt; 30; y++){
      LOG_INF(&amp;quot;main - spim_tx_buf_initial address: %p&amp;quot;, (void*)&amp;amp;spim_tx_buf_initial[y][0]);
   }
      LOG_INF(&amp;quot;---------------------------------------------------------------------&amp;quot;);
   for(int y=0 ; y &amp;lt; 18; y++){
      LOG_INF(&amp;quot;main - spim_tx_buf_repeat address: %p&amp;quot;, (void*)&amp;amp;spim_tx_buf_repeat[y][0]);
   }
   LOG_INF(&amp;quot;---------------------------------------------------------------------&amp;quot;); */
   peripheral_setup();
   nrfx_gpiote_out_task_enable(&amp;amp;gpiote_inst, SS_PIN_MASTER);
   nrfx_gppi_channels_enable(BIT(ppi_channel_spi_start));
   nrfx_gppi_channels_enable(BIT(ppi_channel_spi_end));
   nrfx_timer_enable(&amp;amp;timer1_inst);
   k_sem_give(&amp;amp;nus_start);
   //k_sem_take(&amp;amp;debugging, K_FOREVER);
   //for(int i=0; i&amp;lt;400; i++){
      //LOG_INF(&amp;quot;spim1_handler- %d &amp;gt;&amp;gt; RXD.PTR: 0x%08x&amp;quot;, i, recording[i]);
      //LOG_INF(&amp;quot;spim1_handler- %d &amp;gt;&amp;gt; buffer_index: %d&amp;quot;, i, recording[i]);
      //LOG_INF(&amp;quot;spim1_handler- %d &amp;gt;&amp;gt; initialization_counter: %d&amp;quot;, i, recording[i]);
      //LOG_INF(&amp;quot;spim1_handler- %d &amp;gt;&amp;gt; repeat_counter: %d&amp;quot;, i, recording[i]);
   //}

   return 0;
}
&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;However, after adding these variables, the SPIM result for the spim_tx_buf_initial command is no longer missing. What could be the issue?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501501?ContentTypeID=1</link><pubDate>Fri, 06 Sep 2024 09:59:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f9f4bb16-a311-43b7-af39-ece0693f33ef</guid><dc:creator>seongmincho</dc:creator><description>&lt;p&gt;Okay, I&amp;#39;ll try that. I&amp;#39;ll get back to you as soon as possible.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501488?ContentTypeID=1</link><pubDate>Fri, 06 Sep 2024 08:12:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:002bc066-556e-4f51-b7bf-d5587e3361c8</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Seongmin,&amp;nbsp;&lt;br /&gt;Yes correct. The main point is that the BLE stack doesn&amp;#39;t buffer the data until it&amp;#39;s sent. So you need to make sure the data buffer is not modified until the BLE function is returned.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501480?ContentTypeID=1</link><pubDate>Fri, 06 Sep 2024 07:44:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:71c02ad4-6033-48db-9ad5-3c4b6b88fb18</guid><dc:creator>seongmincho</dc:creator><description>&lt;p&gt;Is the key point of your advice using &amp;#39;FIFO&amp;#39; and creating a &amp;#39;temporary buffer to store data&amp;#39;?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501381?ContentTypeID=1</link><pubDate>Thu, 05 Sep 2024 13:13:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b40b9bd3-4ace-4ecd-85ec-425ebcc31c4a</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Seongmin,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I think you may want to look at how we handle UART buffer in NUS sample of using k_fifo. Notice how the buffer is initialized at each &lt;span&gt;UART_RX_BUF_REQUEST event. And then free after it&amp;#39;s sent by&amp;nbsp;&lt;/span&gt;bt_nus_send()&lt;/p&gt;
&lt;p&gt;As I mentioned if you simply share the same buffer between BLE and SPI you will run into the issue that&amp;nbsp;SPI may change the data before it&amp;#39;s sent via BLE.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501352?ContentTypeID=1</link><pubDate>Thu, 05 Sep 2024 11:50:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c90bb789-d2ab-438a-94ae-3d5fd8464f18</guid><dc:creator>seongmincho</dc:creator><description>&lt;p&gt;First of all, thank you so much for your response.&lt;br /&gt;&lt;br /&gt;When using SPIM alone, there were no issues, and all debugging has been completed. Additionally, even when NUS and SPIM used separate buffers, the tests were conducted and everything worked fine in that case as well. (Additionally, I measured SS, SCK, MOSI, and MISO with an oscilloscope to confirm this.)&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The problem arises when the buffer is shared between the SPIM handler and the NUS thread. SPIM itself operates correctly, as confirmed by the oscilloscope, but sharing the buffer became unavoidable when I tried to send the data received by SPIM to NUS.&lt;/p&gt;
&lt;p&gt;To address this, I implemented double buffering. However, I&amp;#39;m facing difficulties in writing the code due to the limitation that I cannot wait for or block the handler(in zephyr OS).&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote userid="2121" url="~/f/nordic-q-a/114453/spim-nus-conflict-between-the-spim-and-ble-thread/501306"]Please try to do a memcpy to copy the SPI buffer to another buffer before sending as the BLE stack will not buffer it until it&amp;#39;s sending. So if the content of the buffer is changed/updated before the BLE packet is sent you will send the wrong data.[/quote]
&lt;p&gt;As you mentioned, are you referring to storing &lt;code&gt;NUS_send_counter&lt;/code&gt; in &lt;code&gt;temp&lt;/code&gt; like in my code(nus_thread)? I think it&amp;#39;s a good idea. However, I&amp;#39;m concerned that &lt;code&gt;memcpy&lt;/code&gt; might result in high CPU load.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501306?ContentTypeID=1</link><pubDate>Thu, 05 Sep 2024 08:59:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f5d1ee78-2ee5-4ca1-857c-5a7eabaedabe</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Seongmin,&amp;nbsp;&lt;br /&gt;We currently don&amp;#39;t have NUS and SPIM sample.&lt;/p&gt;
&lt;p&gt;My suggestion is to try instead of sending over BLE, you can print out on the log the data should be sent by BLE. This way you can monitor if there is any problem with the SPI buffer.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Please correct me if I&amp;#39;m wrong, you are trying to test using a loopback ? The MOSI is connected to MISO, correct?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Please try to do a memcpy to copy the SPI buffer to another buffer before sending as the BLE stack will not buffer it until it&amp;#39;s sending. So if the content of the buffer is changed/updated before the BLE packet is sent you will send the wrong data.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501262?ContentTypeID=1</link><pubDate>Thu, 05 Sep 2024 04:27:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6c50142e-3fad-4ed5-bfbe-52851ed90891</guid><dc:creator>seongmincho</dc:creator><description>&lt;p&gt;MOSI: command, MISO: result&lt;/p&gt;
&lt;p&gt;First of all, the data received from the sensor is the result (MISO) of the SPIM.&lt;/p&gt;
&lt;p&gt;While debugging the data received via BLE, there is an issue where the pattern of data received from the sensor is not being output as expected.&lt;/p&gt;
&lt;p&gt;After the result of the initial transmission (&lt;code&gt;spim_tx_buf_initial&lt;/code&gt;) is successfully sent via BLE, only the result of the repeated transmission (&lt;code&gt;spim_tx_buf_repeat&lt;/code&gt;) should be transmitted. However, even after the initial transmission is complete, some of the &amp;#39;result of&amp;nbsp;&lt;span&gt;spim_tx_buf_initial&lt;/span&gt;&amp;#39; continues to be output. As a result, only the &amp;#39;result of&amp;nbsp;&lt;code&gt;spim_tx_buf_repeat&lt;/code&gt;&amp;#39; should be repeated, but this is not happening as expected.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501201?ContentTypeID=1</link><pubDate>Wed, 04 Sep 2024 14:42:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:01ce145b-3634-4fb6-b546-4da3849ef382</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Please show the full code. I don&amp;#39;t see how spim_buffer assigned.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;I don&amp;#39;t see where you assign the SPI buffer&amp;nbsp;spim_rx_buf_A to.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501199?ContentTypeID=1</link><pubDate>Wed, 04 Sep 2024 14:38:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b5fd9a59-95a3-47aa-829c-24fe55901447</guid><dc:creator>seongmincho</dc:creator><description>&lt;div class="flex-shrink-0 flex flex-col relative items-end"&gt;
&lt;div&gt;
&lt;div class="pt-0"&gt;
&lt;div class="gizmo-bot-avatar flex h-8 w-8 items-center justify-center overflow-hidden rounded-full"&gt;
&lt;div class="relative p-1 rounded-sm flex items-center justify-center bg-token-main-surface-primary text-token-text-primary h-8 w-8"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="group/conversation-turn relative flex w-full min-w-0 flex-col agent-turn"&gt;
&lt;div class="flex-col gap-1 md:gap-3"&gt;
&lt;div class="flex max-w-full flex-col flex-grow"&gt;
&lt;div dir="auto" data-message-author-role="assistant" data-message-id="bc8f5b1a-fcfa-4901-9bd6-a1ddd7077694"&gt;
&lt;div&gt;
&lt;div class="markdown prose w-full break-words dark:prose-invert dark"&gt;
&lt;p&gt;Aside from your response, I have an additional request. Could I see an example code that integrates NUS and SPIM, similar to mine?&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501198?ContentTypeID=1</link><pubDate>Wed, 04 Sep 2024 14:35:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9aa04219-afe5-404f-b31a-5cf4a07030f8</guid><dc:creator>seongmincho</dc:creator><description>&lt;p&gt;First of all, thank you so much for your response.&lt;/p&gt;
&lt;div class="group/conversation-turn relative flex w-full min-w-0 flex-col agent-turn"&gt;
&lt;div class="flex-col gap-1 md:gap-3"&gt;
&lt;div class="flex max-w-full flex-col flex-grow"&gt;
&lt;div dir="auto" data-message-author-role="assistant" data-message-id="8cddc723-c98c-4c8d-b520-f46e06724c4c"&gt;
&lt;div&gt;
&lt;div class="result-streaming markdown prose w-full break-words dark:prose-invert dark"&gt;
&lt;p&gt;I&amp;rsquo;ll organize the debugging of my code and get back to you!&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501197?ContentTypeID=1</link><pubDate>Wed, 04 Sep 2024 14:32:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9e28a50f-6270-486b-9608-15b9c45b299a</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Please try to give as much information as possible.&amp;nbsp;&lt;br /&gt;What exactly happened when&amp;nbsp;&lt;/p&gt;
[quote user="seongmincho"] However, it seems that the tasks are not running as expected. [/quote]&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501190?ContentTypeID=1</link><pubDate>Wed, 04 Sep 2024 14:01:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d8fbfc88-ec7f-420a-bf3e-f199f0d18bee</guid><dc:creator>seongmincho</dc:creator><description>&lt;p&gt;&lt;span&gt;Assumption:&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span&gt;Task1: SPIM Communication&lt;br /&gt;Task2: SPIM Handler&lt;br /&gt;Task3: NUS_send (Executed in &amp;lsquo;ble_write_thread&amp;rsquo;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I would like the tasks to proceed as shown in the image below.&lt;br /&gt;&lt;img style="max-height:139px;max-width:408px;" height="139" src="https://devzone.nordicsemi.com/resized-image/__size/816x278/__key/communityserver-discussions-components-files/4/6082._F8ADBCB9_1.png" width="408" alt=" " /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The number of Task1 and Task2 executions before Task3 runs doesn&amp;#39;t matter. However, it seems that the tasks are not running as expected. In particular, I suspect that the transition between buffers in the double-buffering process is not working smoothly. I have tried using semaphores, mutexes, and locks, but only encountered errors, which leads me to believe that I haven&amp;#39;t used them correctly.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Here is the code I&amp;rsquo;m using:&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;      temp_buffer = spim_buffer;  
      spim_buffer = nus_buffer;
      nus_buffer = temp_buffer;

      spim1_inst.p_reg-&amp;gt;RXD.PTR = (uint32_t)spim_buffer;&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [SPIM + NUS]  conflict between the SPIM and  BLE thread.</title><link>https://devzone.nordicsemi.com/thread/501139?ContentTypeID=1</link><pubDate>Wed, 04 Sep 2024 11:36:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c6f5f88c-47a4-46b4-9158-8c93fc4a4aa9</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Seongmin,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Could you elaborate more on the issue you are facing ? Please describe the issue in more detail.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>