<?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>timer expire</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/111703/timer-expire</link><description>Hi, 
 I have a strange problem. I use a serial port timer to retrieve serial port data. When I compile with debug, the program runs normally, but when I compile with release, the program cannot run properly. Finally, I find out that the reason is that</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 05 Jun 2024 11:50:55 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/111703/timer-expire" /><item><title>RE: timer expire</title><link>https://devzone.nordicsemi.com/thread/487498?ContentTypeID=1</link><pubDate>Wed, 05 Jun 2024 11:50:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0e83e915-2d45-470f-8b1b-364d40ba3da0</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;It looks like the app_timer_start is called with some condition inside uart0 event handler. It is hard for me to say when app_timer_start is called.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Is it possible that it is called multiple times before the previous one shot timer expires?&lt;/li&gt;
&lt;li&gt;What is the baudrate used? If you run at a lower baudrate, does the debug and release version behave similar?
&lt;ol&gt;
&lt;li&gt;If yes, then it could be an issue in the way you are starting the timers.&lt;/li&gt;
&lt;li&gt;if no, then there could be a harder logic optimization in your code, that is not very obvious from the code review here. Is it possible to attach a similistic project that i can use to reproduce ono the nRf52840 DK?&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: timer expire</title><link>https://devzone.nordicsemi.com/thread/487195?ContentTypeID=1</link><pubDate>Tue, 04 Jun 2024 04:00:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2742f8b8-e975-468c-b80d-fc6a5e7d3112</guid><dc:creator>xiang san </dc:creator><description>&lt;p&gt;Hi，&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;quot;double_uart.h&amp;quot;

#include &amp;quot;nrf_uart.h&amp;quot;

#include &amp;quot;app_uart.h&amp;quot;

#include &amp;quot;app_timer.h&amp;quot;

#include &amp;quot;pca10056.h&amp;quot;

#include &amp;quot;nrf_delay.h&amp;quot;

#include &amp;quot;string.h&amp;quot;

#include &amp;quot;wifi_esp8266.h&amp;quot;

#include &amp;quot;pwm_control.h&amp;quot;

uint8_t data_rec_array[UART_RX_BUF_SIZE], data_array[UART_RX_BUF_SIZE];
uint8_t data1_rec_array[UART_RX_BUF_SIZE], data1_array[UART_RX_BUF_SIZE];
bool motor_start_flag = false, motor_end_flag = false, target_pre_send_flag = false, start_wifi_flag = false;
uint16_t target_wind_pressure = 0, inlet_wind_range = 50, outlet_wind_rang = 50;
uint16_t operate_cnt = 0;
bool white_glove = true, operate_order = true, operate_num = true, uart_rec_flag = false;
uint16_t workpiece_ok_num = 0, workpiece_err_num = 0;

static void uart0_handleEvent(app_uart_evt_t * pEvent);
static void uart1_handleEvent(app_uart_evt_t * pEvent);

APP_UART_DEF(uart0, 0, UART_RX_BUF_SIZE, uart0_handleEvent);
APP_UART_DEF(uart1, 1, UART_RX_BUF_SIZE, uart1_handleEvent);

APP_TIMER_DEF(m_rec_timer);
APP_TIMER_DEF(m_rec1_timer);

/*****************压力表量程设定******************/
static void pre_rang_set() {
  uint8_t prs_set[8];
  uint16_t prs_crc;

  if (outlet_wind_rang != 0) {
    prs_set[0] = FAN_ADD;
    prs_set[1] = WRITE_CODE;
    prs_set[2] = 0xfb;
    prs_set[3] = 0x00;
    prs_set[4] = outlet_wind_rang &amp;gt;&amp;gt; 8;
    prs_set[5] = outlet_wind_rang &amp;amp; 0xff;
    prs_crc = crc16(prs_set, 6);
    prs_set[6] = prs_crc &amp;amp; 0xff;
    prs_set[7] = prs_crc &amp;gt;&amp;gt; 8;

    for (uint8_t i = 0; i &amp;lt; 8; i++) {
      while (app_uart0_put(prs_set[i]) != NRF_SUCCESS);
    }
  }
}

/*****************风机风压485给定控制方式******************/
void fan_485_conrol() {
  uint8_t twp_cmd[8];
  uint16_t twp_crc;

  //if(target_wind_pressure !=0)
  {
    if (target_wind_pressure &amp;gt; outlet_wind_rang) {
      target_wind_pressure = outlet_wind_rang;
    }
    twp_cmd[0] = FAN_ADD;
    twp_cmd[1] = WRITE_CODE;
    twp_cmd[2] = 0xfb;
    twp_cmd[3] = 0x01;
    twp_cmd[4] = target_wind_pressure &amp;gt;&amp;gt; 8;
    twp_cmd[5] = target_wind_pressure &amp;amp; 0xff;
    twp_crc = crc16(twp_cmd, 6);
    twp_cmd[6] = twp_crc &amp;amp; 0xff;
    twp_cmd[7] = twp_crc &amp;gt;&amp;gt; 8;

    for (uint8_t i = 0; i &amp;lt; 8; i++) {
      while (app_uart0_put(twp_cmd[i]) != NRF_SUCCESS);
    }
  }
}

static void recive_timeout_handler(void * p_context) {
  uart_rec_flag = true;
  USART_RX_STA |= 1 &amp;lt;&amp;lt; 15;
  operate_cnt = data_array[2];

  if (data_array[3] == 0) white_glove = true;
  else white_glove = false;
  if (data_array[4] == 0) operate_order = true;
  else operate_order = false;
  if (data_array[5] == 0) operate_num = true;
  else operate_num = false;
  workpiece_ok_num = (data_array[6] &amp;gt;&amp;gt; 8) | data_array[7];
  workpiece_err_num = (data_array[8] &amp;gt;&amp;gt; 8) | data_array[9];
  memset(data_array, 0, 15);
  memset(data_rec_array, 0, 15);
  USART_RX_STA = 0;

}
static void clean_uart1_rec_buffer() {
  USART1_RX_STA = 0;
  memset(data1_array, 0, 15);
  memset(data1_rec_array, 0, 15);
}
static void recive1_timeout_handler(void * p_context) {
  USART1_RX_STA |= 1 &amp;lt;&amp;lt; 15;
  if (data1_array[1] == 0xA5) {
    if (data1_array[13] == 0x04 &amp;amp;&amp;amp; data1_array[14] == 0xd2) //身份验证默认为输入1234,然后取消报警
    {
      nrf_gpio_pin_set(LED_G); //正常亮绿灯
      nrf_gpio_pin_clear(LED_R); //故障亮红灯，正常熄灭
      nrf_gpio_pin_clear(BUZZER); //故障响蜂鸣器，正常熄灭
    }
  }
  clean_uart1_rec_buffer();
}
/**@brief Function for initializing the timer. */
void timer_init(void) {
  ret_code_t err_code = app_timer_init();
  APP_ERROR_CHECK(err_code);
  APP_ERROR_CHECK(app_timer_create( &amp;amp; m_rec_timer, APP_TIMER_MODE_SINGLE_SHOT, recive_timeout_handler));
  APP_ERROR_CHECK(app_timer_create( &amp;amp; m_rec1_timer, APP_TIMER_MODE_SINGLE_SHOT, recive1_timeout_handler));
}

void uart_init(void) {
  ret_code_t err_code;

  app_uart_comm_params_t
  const comm_params = {
    .rx_pin_no = RX_PIN_NUMBER,
    .tx_pin_no = TX_PIN_NUMBER,
    .rts_pin_no = RTS_PIN_NUMBER,
    .cts_pin_no = CTS_PIN_NUMBER,
    .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
    .use_parity = false,
    .baud_rate = UART_BAUDRATE_BAUDRATE_Baud9600
  };

  uart0.comm_params = &amp;amp; comm_params;
  err_code = app_uart_init( &amp;amp; uart0, &amp;amp; uart0_buffers, APP_IRQ_PRIORITY_LOWEST);
  APP_ERROR_CHECK(err_code);

  app_uart_comm_params_t
  const uart1_comm_params = {
    .rx_pin_no = UART1_RX_PIN,
    .tx_pin_no = UART1_TX_PIN,
    .rts_pin_no = UART_PIN_DISCONNECTED,
    .cts_pin_no = UART_PIN_DISCONNECTED,
    .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
    .use_parity = false,
    .baud_rate = UART_BAUDRATE_BAUDRATE_Baud115200
  };

  uart1.comm_params = &amp;amp; uart1_comm_params;
  err_code = app_uart_init( &amp;amp; uart1, &amp;amp; uart1_buffers, APP_IRQ_PRIORITY_LOWEST);
  APP_ERROR_CHECK(err_code);
}

void uart0_handleEvent(app_uart_evt_t * p_event) {
  //uint8_t data;
  switch (p_event -&amp;gt; evt_type) {
    //@snippet [Handling data from UART]
  case APP_UART_DATA_READY:
    if (((USART_RX_STA &amp;amp; (1 &amp;lt;&amp;lt; 15)) == 0) &amp;amp;&amp;amp; (!rec_data_flag)) {
      if (USART_RX_STA &amp;lt; UART_RX_BUF_SIZE) {
        UNUSED_VARIABLE(app_uart_get( &amp;amp; uart0, &amp;amp; data_rec_array[USART_RX_STA]));
        if ((USART_RX_STA == 0) &amp;amp;&amp;amp; (data_rec_array[0] == 0x5A)) {
          app_timer_start(m_rec_timer, APP_TIMER_TICKS(20), NULL); //此时间值设定关乎到目标风压设定值的稳定性，越小越稳定，原因分析：每一秒往屏写运行参数时，屏会回数据，在这个时间段设置风压值，回的数据和设定数据就有可能连起来导致无法识别
        }
        if (data_rec_array[0] == 0x5A) {
          data_array[USART_RX_STA] = data_rec_array[USART_RX_STA];
          USART_RX_STA++;
        }
      } else {
        USART_RX_STA |= 1 &amp;lt;&amp;lt; 15;
        memset(data_rec_array, 0, 256);
      }
    }
    break;
  case APP_UART_DATA:
    //UNUSED_VARIABLE(app_uart_get(&amp;amp;data_array[index]));
    //index++;
    //UNUSED_VARIABLE(app_uart_get(&amp;amp;c));

    break;

    //@snippet [Handling data from UART]
  case APP_UART_COMMUNICATION_ERROR:
    // NRF_LOG_ERROR(&amp;quot;Communication error occurred while handling UART.&amp;quot;);
    //APP_ERROR_HANDLER(p_event-&amp;gt;data.error_communication);
    break;

  case APP_UART_FIFO_ERROR:
    // NRF_LOG_ERROR(&amp;quot;Error occurred in FIFO module used by UART.&amp;quot;);
    APP_ERROR_HANDLER(p_event -&amp;gt; data.error_code);
    break;

  default:
    break;
  }
}

void uart1_handleEvent(app_uart_evt_t * p_event) {
  //uint8_t data;
  switch (p_event -&amp;gt; evt_type) {
    //@snippet [Handling data from UART]
  case APP_UART_DATA_READY:
    if ((USART1_RX_STA &amp;amp; (1 &amp;lt;&amp;lt; 15)) == 0) {
      if (USART1_RX_STA &amp;lt; UART_RX_BUF_SIZE) {
        UNUSED_VARIABLE(app_uart_get( &amp;amp; uart1, &amp;amp; data1_rec_array[USART1_RX_STA]));
        if ((USART1_RX_STA == 0) &amp;amp;&amp;amp; (data1_rec_array[0] == 0x5A)) {
          app_timer_start(m_rec1_timer, APP_TIMER_TICKS(20), NULL); //此时间值设定关乎到目标风压设定值的稳定性，越小越稳定，原因分析：每一秒往屏写运行参数时，屏会回数据，在这个时间段设置风压值，回的数据和设定数据就有可能连起来导致无法识别
        }
        //UNUSED_VARIABLE(app_uart_get(&amp;amp;uart1,&amp;amp;data1_rec_array[USART1_RX_STA]));
        //UNUSED_VARIABLE(app_uart_get(&amp;amp;data));
        if (data1_rec_array[0] == 0x5A)
        //if(data == 0x01 || data == 0x02)
        {
          data1_array[USART1_RX_STA] = data1_rec_array[USART1_RX_STA];
          USART1_RX_STA++;
        }
      } else {
        USART1_RX_STA |= 1 &amp;lt;&amp;lt; 15;
        memset(data_rec_array, 0, 256);
      }
    }
    break;
  case APP_UART_DATA:
    //UNUSED_VARIABLE(app_uart_get(&amp;amp;data_array[index]));
    //index++;
    //UNUSED_VARIABLE(app_uart_get(&amp;amp;c));

    break;

    //@snippet [Handling data from UART]
  case APP_UART_COMMUNICATION_ERROR:
    //NRF_LOG_ERROR(&amp;quot;Communication error occurred while handling UART.&amp;quot;);
    //APP_ERROR_HANDLER(p_event-&amp;gt;data.error_communication);
    break;

  case APP_UART_FIFO_ERROR:
    // NRF_LOG_ERROR(&amp;quot;Error occurred in FIFO module used by UART.&amp;quot;);
    APP_ERROR_HANDLER(p_event -&amp;gt; data.error_code);
    break;

  default:
    break;
  }
}

/************UART0 接收发送函数重构************/
uint32_t app_uart0_get(uint8_t * p_byte) {
  return app_uart_get( &amp;amp; uart0, p_byte);
}
uint32_t app_uart0_put(uint8_t byte) {
  return app_uart_put( &amp;amp; uart0, byte);
}
uint32_t app_uart0_flush() {
  return app_uart_flush( &amp;amp; uart0);
}

/************UART1 接收发送函数重构************/
uint32_t app_uart1_get(uint8_t * p_byte) {
  return app_uart_get( &amp;amp; uart1, p_byte);
}
uint32_t app_uart1_put(uint8_t byte) {
  return app_uart_put( &amp;amp; uart1, byte);
}
uint32_t app_uart1_flush() {
  return app_uart_flush( &amp;amp; uart1);
}

/******************modbus CRC16 校验******************/
uint16_t crc16(uint8_t * data, uint8_t length) {
  int i, crc_result = 0xffff;
  while (length--) {
    crc_result ^= * data++;
    for (i = 0; i &amp;lt; 8; i++) {
      if (crc_result &amp;amp; 0x01) crc_result = (crc_result &amp;gt;&amp;gt; 1) ^ 0xa001;
      else crc_result = crc_result &amp;gt;&amp;gt; 1;
    }
  }
  //return (crc_result= ((crc_result&amp;amp;0xff)&amp;lt;&amp;lt;8)|(crc_result&amp;gt;&amp;gt;8));
  return (crc_result);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;this is my&amp;nbsp; main code.Please see if there are any issues&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: timer expire</title><link>https://devzone.nordicsemi.com/thread/487027?ContentTypeID=1</link><pubDate>Mon, 03 Jun 2024 07:05:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:651cd935-4984-4a43-bdbd-f54919ae1b17</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Can you show some code snippets for us to review or use to try at our end? Hard to say what is wrong.&lt;/p&gt;
&lt;p&gt;If there is a difference in release and debug version of the code, then it is most likely some issue with the code where some unwanted things being optimized away.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>