<?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>nrf52 DK Bluetooth Connection Lost Due to Low Memory?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/113591/nrf52-dk-bluetooth-connection-lost-due-to-low-memory</link><description>Hi, 
 we are using nrf52 DK and our custom mobile application. We are trying to collect 15000 sensor samples and finally we are trying to send array to mobile app via BLE. We store those samples into array called data_to_send. Array is defined as: uint8_t</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 01 Aug 2024 11:37:58 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/113591/nrf52-dk-bluetooth-connection-lost-due-to-low-memory" /><item><title>RE: nrf52 DK Bluetooth Connection Lost Due to Low Memory?</title><link>https://devzone.nordicsemi.com/thread/496601?ContentTypeID=1</link><pubDate>Thu, 01 Aug 2024 11:37:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dc0837e4-8fed-49f9-821c-28f0e2ae716d</guid><dc:creator>Nguyen Hoan Hoang</dc:creator><description>&lt;p&gt;First, your data is 15K on stack but your stack size is only 8K. &amp;nbsp;Secondly, BLE max data package is 251 bytes. So why don&amp;#39;t you collect 250 max at a time instead of 15000 (15K) ? Thirdly, nRF52832 has a limit of 64K RAM. I assume that is what you are using. &amp;nbsp;So be careful with&amp;nbsp;RAM usage. &amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52 DK Bluetooth Connection Lost Due to Low Memory?</title><link>https://devzone.nordicsemi.com/thread/496599?ContentTypeID=1</link><pubDate>Thu, 01 Aug 2024 11:14:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c42ec9ff-4151-4f25-8317-1959225a5dc3</guid><dc:creator>SamiL</dc:creator><description>&lt;p&gt;I would try to avoid dynamic memory allocation, if at all possible. I believe it is not recommended by Nordics either.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52 DK Bluetooth Connection Lost Due to Low Memory?</title><link>https://devzone.nordicsemi.com/thread/496564?ContentTypeID=1</link><pubDate>Thu, 01 Aug 2024 08:34:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:21e32096-6f94-448f-846b-55cbb662568b</guid><dc:creator>Turbo J</dc:creator><description>&lt;p&gt;Just don&amp;#39;t put data_to_send on the stack. Refer to a C language tutorial if you needed to know &lt;em&gt;how.&lt;/em&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52 DK Bluetooth Connection Lost Due to Low Memory?</title><link>https://devzone.nordicsemi.com/thread/496563?ContentTypeID=1</link><pubDate>Thu, 01 Aug 2024 08:29:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d9508788-33c9-4e0b-92cc-15c0f2413493</guid><dc:creator>SamiL</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void execute_cooling_period(int MTU_SIZE) {
  LOG_INF(&amp;quot;execute_cooling_period&amp;quot;);

  uint8_t data_to_send[NUMBER_OF_SAMPLES_TO_COLLECT];
  uint16_t data_index = 0;
  float pwm_to_write = 0.0;
  int new_pulse = 0;

  long long int TIME_TO_RUN_PID_IN_NS = 5000000000;
  long long int  COOLING_TARGET_TIME_IN_NS = 40000000000;
  long long int  DELTA_TIME = COOLING_TARGET_TIME_IN_NS - TIME_TO_RUN_PID_IN_NS;

  float PWM_start_value = 80;
  float PWM_end_value = 0;
  float delta_PWM = (PWM_end_value - PWM_start_value) / (DELTA_TIME);
  bool write_marker = true;
  int MARKER = 1500;

  int32_t force_signal_in_mv = calculate_millivolt_from_adc();
  float delta_y = -110.0f;      // Test with fixed force release
  float delta_x = COOLING_TARGET_TIME_IN_MS;
  float k = delta_y / delta_x;
  float b = force_signal_in_mv;

  // Define time parameters.
  timing_t start_time_cooling, current_time_cooling;
  uint64_t total_cycles_cooling;
  uint64_t total_ns_cooling;

  timing_t start_time_pid, current_time_pid;
  uint64_t total_cycles_pid;
  uint64_t total_ns_pid;

  // Initialize and start the timer. Get the starting time and set that value to start_time.
  timing_init();
  timing_start();
  start_time_cooling = timing_counter_get();

  bool islinear = false;

  while (!is_cooling_done) {

    // Update time
    current_time_cooling = timing_counter_get();
    total_cycles_cooling = timing_cycles_get(&amp;amp;start_time_cooling, &amp;amp;current_time_cooling);
    total_ns_cooling = timing_cycles_to_ns(total_cycles_cooling); 

    // Calculate moving average value
    int32_t millivolts = calculate_millivolt_from_adc();
    int32_t average = moving_average(millivolts);

    long long int delta_time = total_ns_cooling - start_time_cooling;


    
    if (total_ns_cooling &amp;gt;= PID_RUN_TIME) {
      islinear = true;
      
      // Do linear decrease of the pwm.
      long long int time = delta_time - TIME_TO_RUN_PID_IN_NS;
      pwm_to_write = delta_PWM * time + PWM_start_value;
      pwm_to_write = (int)scale_PWM_output_to_nanoseconds(pwm_to_write);

      LOG_INF(&amp;quot;Total ns %llu. delta_time %llu. Time %llu. PWM to write %f&amp;quot;, total_ns_cooling, delta_time, time, pwm_to_write);

      if (write_marker) {
        data_to_send[data_index++] = highByte((int) MARKER);
        data_to_send[data_index++] = lowByte((int) MARKER);

        data_to_send[data_index++] = highByte((int) MARKER);
        data_to_send[data_index++] = lowByte((int) MARKER);

        data_to_send[data_index++] = highByte((int) MARKER);
        data_to_send[data_index++] = lowByte((int) MARKER);

        write_marker = false;
      }
    } else {

      float target_setpoint = k * delta_time + b;
      pid.mySetpoint = target_setpoint;

      // Run PID. 
      current_time_pid = timing_counter_get();
      total_cycles_pid = timing_cycles_get(&amp;amp;start_time_cooling, &amp;amp;current_time_pid);
      total_ns_pid = timing_cycles_to_ns(total_cycles_pid);

      if (total_ns_pid &amp;gt;= SAMPLING_RATE) {
        current_time_pid = 0;
        total_cycles_pid = 0;
        total_ns_pid = 0;

        PID_Compute(&amp;amp;pid);
        new_pulse = scale_PID_output_to_nanoseconds();
        pwm_to_write = new_pulse;

        LOG_INF(&amp;quot;PWM to write %f, target set point %f&amp;quot;, pwm_to_write, target_setpoint);
      }
    }

    // Set new PWM value based on PID calculations.
    pwm_set(pwm_dev, DEFAULT_PWM_PORT, DEFAULT_PERIOD, pwm_to_write, 0);
    
    // Store moving average value into array. 
    data_to_send[data_index++] = highByte((int) average);
    data_to_send[data_index++] = lowByte((int) average);

    if (data_index &amp;gt;= NUMBER_OF_SAMPLES_TO_COLLECT) {
      LOG_INF(&amp;quot;Data index is out of bounds. Exit loop&amp;quot;);
      is_cooling_done = true;
      //data_index = 0;
    }

    //LOG_INF(&amp;quot;Total ns %llu. Average value %u. Index %u&amp;quot;, total_ns_cooling, average, data_index);
    //thread_analyzer_run();
    //thread_analyzer_print();

    k_sleep(K_MSEC(10));
  }

  // Stop timer.
  timing_stop();

  LOG_INF(&amp;quot;Cooling is finished. Send data.&amp;quot;);

  size_t array_size = sizeof(data_to_send) / sizeof(data_to_send[0]);
  LOG_INF(&amp;quot;Size %zu&amp;quot;, array_size);

  //send_data(*data_to_send, MTU_SIZE);


  for (int i = 0; i &amp;lt;= NUMBER_OF_SAMPLES_TO_COLLECT; i++) {
    data_to_send[i] = 0;
  }

  LOG_INF(&amp;quot;Array cleaned&amp;quot;);

  // After the cooling period is finished set PWM to 0 and give data transfer notify.
  pwm_set(pwm_dev, DEFAULT_PWM_PORT, DEFAULT_PERIOD, 0, 0);
  my_lbs_send_embedded_state_notify(IDLE, 1);
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52 DK Bluetooth Connection Lost Due to Low Memory?</title><link>https://devzone.nordicsemi.com/thread/496555?ContentTypeID=1</link><pubDate>Thu, 01 Aug 2024 08:09:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:983529ea-66d0-45ab-9893-808859001813</guid><dc:creator>Turbo J</dc:creator><description>&lt;p&gt;We need the &lt;em&gt;actual &lt;/em&gt;source code here. Remember how the C compiler places variables strongly depends where you defined them...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>