Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
ptu_debug.c File Reference

Source file for PTU UART test interface. More...

#include <string.h>
#include <stdio.h>
#include "app_print.h"
#include "simple_uart.h"
#include "ptu_test_mux.h"
#include "ptu_registry.h"
#include "ptu_sensors.h"
#include "bsp.h"

Go to the source code of this file.

Macros

#define DEBUG_LINE_LENGTH   80
 
#define EVT_STRING_LENGTH_MAX   20
 
#define VTX_INCREMENT   1000
 
#define ITX_INCREMENT   100
 
#define TEMPERATURE_INCREMENT   10
 
#define EXTENDED_BEACON_THRESHOLD_MS   500
 
#define MAX_RTC_COUNTER_VAL   0x00FFFFFF
 

Functions

API implementation
bool ptu_process_command_board (uint8_t cmd)
 Function for processing board related test commands. More...
 
void sm_uart_rx_poll (void)
 Get and process uart commands.
 
void line_print (void)
 Print a DEBUG_LINE_LENGTH character long "horizontal line" to UART.
 
static bool debug_output_changed (void)
 Update m_debug_output variable. More...
 
static void debug_output_print (void)
 Print contents of m_debug_output to UART. More...
 
void debug (void)
 Process debug commands.
 
void debug_add_string (const char *s)
 Add string to debug output. More...
 

Static functions and variables

static debug_output_t m_debug_output
 
static char m_evt_evt_string [EVT_STRING_LENGTH_MAX] = ""
 
static ptu_tmux_override_t override_values
 
static bool m_uart_initialized = false
 
static bool m_simplified_beacon_print = false
 
static bool force_debug_print = false
 
static void m_set_load_variation_detected (void)
 Will syntesize a load representing a load variation to the sensors module. This will be set to true for 1.5 seconds.
 
static char * sm_state_string_get (ptu_sm_state_t state)
 Convert state to string. More...
 
static bool m_uart_chars_available (void)
 Check if there are any characters in the UART receive buffer. More...
 
static bool m_ptu_process_command_common (uint8_t cmd)
 Function for processing common test commands. More...
 
static uint32_t ms_since (uint32_t tick)
 Function for getting the time, in ms, that have passed since the argument system tick. More...
 

Detailed Description

Source file for PTU UART test interface.

Definition in file ptu_debug.c.

Macro Definition Documentation

#define DEBUG_LINE_LENGTH   80

Width of debug output table

Definition at line 29 of file ptu_debug.c.

#define EVT_STRING_LENGTH_MAX   20

Max length of event message string

Definition at line 30 of file ptu_debug.c.

#define VTX_INCREMENT   1000

Value by which the Vtx is incremented/decremented for each call to the simulated measurement function.

Definition at line 31 of file ptu_debug.c.

#define ITX_INCREMENT   100

Value by which the Itx is incremented/decremented for each call to the simulated measurement function.

Definition at line 32 of file ptu_debug.c.

#define TEMPERATURE_INCREMENT   10

Value by which the Temperature is incremented/decremented for each call to the simulated measurement function.

Definition at line 33 of file ptu_debug.c.

#define EXTENDED_BEACON_THRESHOLD_MS   500

The amount of time a beacon lasts before the debug functionality determines it is an extended beacon.

Definition at line 34 of file ptu_debug.c.

#define MAX_RTC_COUNTER_VAL   0x00FFFFFF

Maximum value of the RTC counter.

Definition at line 35 of file ptu_debug.c.

Function Documentation

static char* sm_state_string_get ( ptu_sm_state_t  state)
static

Convert state to string.

Parameters
stateThe state to translate
Returns
String representation of state.

Definition at line 97 of file ptu_debug.c.

98 {
99  switch(state)
100  {
101  case PTU_SM_STATE_CONFIGURATION:
102  return ("Conf");
103  case PTU_SM_STATE_POWER_SAVE:
104  return ("Psave");
105  case PTU_SM_STATE_LOW_POWER:
106  return ("LowP");
107  case PTU_SM_STATE_POWER_TRANSFER:
108  return ("Ptrans");
109  case PTU_SM_STATE_LATCH_FAULT:
110  return ("LatchF");
111  case PTU_SM_STATE_LOCAL_FAULT:
112  return ("LocF");
113  default:
114  return "Invalid state";
115  }
116 }
static bool m_uart_chars_available ( void  )
static

Check if there are any characters in the UART receive buffer.

This function returns the true if there is a character available for reading in the UART receive buffer.

Returns
Number of characters available

Definition at line 125 of file ptu_debug.c.

126 {
127  return (NRF_UART0->EVENTS_RXDRDY == 1);
128 }
static bool m_ptu_process_command_common ( uint8_t  cmd)
static

Function for processing common test commands.

Parameters
[in]cmdCommand code to process.
Return values
trueCommand code was processed by this function.
falseCommand code was not processed by this function.

Definition at line 138 of file ptu_debug.c.

139 {
140  bool cmd_processed = true;
141  uint32_t err_code;
142 
144 
145  switch (cmd)
146  {
147  case 'z':
148  // Reset
149  print_string("\n\rCommand: Reset\r\n");
150  NVIC_SystemReset();
151  break;
152 
153  case 'b':
154  // Beacon debug print
156  print_string("\n\rCommand: Beacon debug print disabled\r\n");
157  else
158  print_string("\n\rCommand: Beacon debug print enabled\r\n");
160  break;
161 
162  case 'y':
163  print_string("\n\rCommand: Inc VTX -> ");
166  print_string("\n\r");
167  break;
168 
169  case 'h':
170  print_string("\n\rCommand: Dec VTX -> ");
173  print_string("\n\r");
174  break;
175 
176  case 'u':
177  print_string("\n\rCommand: Inc ITX -> ");
180  print_string("\n\r");
181  break;
182 
183  case 'j':
184  print_string("\n\rCommand: Dec ITX -> ");
187  print_string("\n\r");
188  break;
189 
190  case 'i':
191  print_string("\n\rCommand: Inc temperature -> ");
194  print_string("\n\r");
195  break;
196 
197  case 'k':
198  print_string("\n\rCommand: Dec temperature -> ");
201  print_string("\n\r");
202  break;
203 
204  case 'o':
205  print_string("\n\rCommand: Test-mode enabled\n\r");
206  tmux_set_select(TMUX_SELECT_OVERRIDE);
207  tmux_get_select(TMUX_SELECT_OVERRIDE);
208  force_debug_print=true;
209  break;
210 
211  case 'p':
212  print_string("SDK Version ");
214  print_string("\n\r");
215  break;
216 
217  case 'l':
218  print_string("\n\rCommand: Test-mode disabled\n\r");
219  tmux_set_select(TMUX_SELECT_NO_OVERRIDE);
220  tmux_get_select(TMUX_SELECT_NO_OVERRIDE);
221  force_debug_print=true;
222  break;
223 
224  case 'n':
225  print_string("\n\rCommand: TX power set to -40dBm\n\r");
226  err_code = sd_ble_gap_tx_power_set(-40);
227  APP_ERROR_CHECK(err_code);
228  break;
229 
230  case 'm':
231  print_string("\n\rCommand: TX power set to PTU_OUTPUT_TX_POWER\n\r");
232  err_code = sd_ble_gap_tx_power_set(PTU_OUTPUT_TX_POWER);
233  APP_ERROR_CHECK(err_code);
234  break;
235 
236  case 'v':
237  print_string("\n\rCommand: Set Load Variation TRUE\n\r");
239  break;
240 
241 
242  case '2':
243  print_string("\n\rCommand: Resonator ON\n\r");
245  break;
246 
247  case '3':
248  print_string("\n\rCommand: Resonator OFF\n\r");
250  break;
251 
252  case '4':
253  print_string("\n\rCommand: Inc poweramp level -> ");
256  print_string("\n\r");
257  break;
258 
259  case '5':
260  print_string("\n\rCommand: Dec poweramp level -> ");
263  print_string("\n\r");
264  break;
265 
266  case '6':
267  print_string("\n\rptu\n\r");
268  break;
269 
270  case '7':
271  print_string("ptu_reg_n_entries_get=");
273  print_string("\n\r");
274  break;
275 
276  case '8':
277  #ifdef DFU_SUPPORT
278  print_string("Entering DFU mode ...\n\r");
279  dfu_start();
280  break;
281  #endif //DFU_SUPPORT
282 
283  default:
284  cmd_processed = false;
285  break;
286  }
287 
289 
290  return cmd_processed;
291 }
void ptu_tmux_override_values_set(ptu_tmux_override_t *values)
Set override values.
Definition: ptu_test_mux.c:138
static bool m_simplified_beacon_print
Definition: ptu_debug.c:59
uint8_t ptu_reg_n_entries_get(void)
Get the number of devices currently in registry. This will include all connected devices, as well as all devices which is currently being registered.
Definition: ptu_registry.c:268
static ptu_tmux_override_t override_values
Definition: ptu_debug.c:57
void ptu_tmux_override_values_get(ptu_tmux_override_t *values)
Get ovveride values.
Definition: ptu_test_mux.c:144
#define PTU_OUTPUT_TX_POWER
Definition: ptu_config.h:31
void print_hex(uint32_t number, uint8_t byte_size)
Converts hex number into char and send it through UART TX.
Definition: app_print.c:42
static bool force_debug_print
Definition: ptu_debug.c:60
uint16_t power_amp_level
Definition: ptu_test_mux.h:38
#define ITX_INCREMENT
Definition: ptu_debug.c:32
#define POWERAMP_TEST_VAR_STEP_SIZE
Definition: ptu_hw_config.h:57
#define VTX_INCREMENT
Definition: ptu_debug.c:31
void print_string(char *char_array)
Decomposes char array and send chars through UART TX.
Definition: app_print.c:25
void tmux_get_select(tmux_select_t select)
Select source for all **tmux**get() functions.
Definition: pru_test_mux.c:135
static void m_set_load_variation_detected(void)
Will syntesize a load representing a load variation to the sensors module. This will be set to true f...
Definition: ptu_debug.c:65
void tmux_set_select(tmux_select_t select)
Select whether override values shall be used when writing to **hal**set() functions.
Definition: pru_test_mux.c:140
void dfu_start(void)
Starts DFU mode. The function writes a special value to retention register to tell bootloader to star...
Definition: common.c:93
#define TEMPERATURE_INCREMENT
Definition: ptu_debug.c:33
#define SDK_VERSION
Definition: wpt.h:30
static uint32_t ms_since ( uint32_t  tick)
static

Function for getting the time, in ms, that have passed since the argument system tick.

Parameters
[in]tickSystem tick we are measureing time from.
Return values
Time,inms, that have passes since the argument system tick.

Definition at line 300 of file ptu_debug.c.

300  {
301  uint32_t current_tick;
302  uint32_t delta_ticks;
303 
304  (void) app_timer_cnt_get(&current_tick);
305  if(tick>current_tick) //Overflow occured
306  {
307  delta_ticks = (((MAX_RTC_COUNTER_VAL-tick)+current_tick) & MAX_RTC_COUNTER_VAL);
308  }
309  else
310  {
311  delta_ticks = ((current_tick-tick) & MAX_RTC_COUNTER_VAL);
312  }
313  return (1000*delta_ticks)/APP_TIMER_CLOCK_FREQ;
314 }
#define MAX_RTC_COUNTER_VAL
Definition: ptu_debug.c:35
bool ptu_process_command_board ( uint8_t  cmd)

Function for processing board related test commands.

Parameters
[in]cmdCommand code to process.
Return values
trueCommand code was processed by this function.
falseCommand code was not processed by this function.
static bool debug_output_changed ( void  )
static

Update m_debug_output variable.

Returns
true if debug output has changed.

Definition at line 377 of file ptu_debug.c.

378 {
379  int i;
380  bool changed = false;
381  uint32_t err_code;
382  const ptu_sensor_data_t* sensor_data;
383  uint16_t current_powerlevel;
384  bool current_poweramp_status;
385  static bool prev_poweramp_status;
386  static uint32_t prev_powerlevel;
387  static uint32_t ticks_at_last_beacon_changed;
388  static uint8_t currently_in_long_beacon = false;
389  ptu_reg_item_t * p_reg_item;
390 
391  err_code = ptu_sensors_data_get(&sensor_data);
392  APP_ERROR_CHECK(err_code);
393 
394 
395  (void)ptu_tmux_poweramp_enable_get(&current_poweramp_status);
396  (void)ptu_tmux_poweramp_level_get(&current_powerlevel);
397 
398  uint32_t delta=ms_since(ticks_at_last_beacon_changed);
399 
400  // Beacon status has changed
401  if(prev_poweramp_status != current_poweramp_status ||
402  prev_powerlevel != current_powerlevel)
403  {
404  err_code = app_timer_cnt_get(&ticks_at_last_beacon_changed);
405  APP_ERROR_CHECK(err_code);
406 
407  if(current_poweramp_status && current_powerlevel == PTU_POWERAMP_INPUT_BEACON_SHORT){
408  currently_in_long_beacon = false;
409  m_debug_output.extended_beacon_currently_running=false;
411  print_string("s");
412  }
413  else if(current_poweramp_status && current_powerlevel == PTU_POWERAMP_INPUT_BEACON_LONG)
414  {
415  currently_in_long_beacon = true;
416  m_debug_output.extended_beacon_currently_running=false;
418  print_string("e\n\r");
419  print_string("l");
420  }
421  }
422 
423  if(!current_poweramp_status){
424  currently_in_long_beacon = false;
425  m_debug_output.extended_beacon_currently_running=false;
427  print_string("e\n\r");
428  }
429  }
430  // Extended beacon
431  else if(currently_in_long_beacon && delta > EXTENDED_BEACON_THRESHOLD_MS)
432  {
433  currently_in_long_beacon = false;
434  m_debug_output.extended_beacon_currently_running=true;
436  print_string("e\n\r");
437  print_string("x");
438  }
439  }
440  prev_poweramp_status = current_poweramp_status;
441  prev_powerlevel = current_powerlevel;
442 
444  return false;
445 
446 
447 
448  if(m_debug_output.current_state != ptu_sm_execute(PTU_SM_SIGNAL_NULL, NULL))
449  {
450  changed = true;
451  m_debug_output.current_state = ptu_sm_execute(PTU_SM_SIGNAL_NULL, NULL);
452  }
453 
454  if(memcmp(&m_debug_output.sensor_data, sensor_data, sizeof(ptu_sensor_data_t)) != 0)
455  {
456  changed = true;
457  memcpy(&m_debug_output.sensor_data, sensor_data, sizeof(ptu_sensor_data_t));
458  }
459 
460  for(i = 0; i < PTU_MAX_CONNECTIONS; i++)
461  {
462  (void)ptu_reg_item_get_from_index(i, &p_reg_item);
463 
464  if(m_debug_output.reg_item_state[i] != p_reg_item->state)
465  {
466  changed = true;
467  m_debug_output.reg_item_state[i] = p_reg_item->state;
468  }
469 
470  if(m_debug_output.link_encrypted[i] != p_reg_item->link_encrypted)
471  {
472  changed = true;
473  m_debug_output.link_encrypted[i] = p_reg_item->link_encrypted;
474  }
475 
476 
477  if(memcmp(&m_debug_output.dyn_params[i], &p_reg_item->prev_pru_dynamic, sizeof(pru_dynamic_t)) != 0)
478  {
479  changed = true;
480  memcpy(&m_debug_output.dyn_params[i], &p_reg_item->prev_pru_dynamic, sizeof(pru_dynamic_t));
481  }
482  }
483 
484  if(strlen(m_evt_evt_string) > 0)
485  {
486  changed = true;
487  }
488 
489  return changed;
490 }
Registry item.
Definition: ptu_registry.h:47
uint8_t link_encrypted
Definition: ptu_registry.h:68
#define PTU_POWERAMP_INPUT_BEACON_LONG
Definition: ptu_hw_config.h:60
static bool m_simplified_beacon_print
Definition: ptu_debug.c:59
uint32_t ptu_reg_item_get_from_index(uint8_t index, ptu_reg_item_t **item_p)
Get registry item from index in database. Index must be < PTU_MAX_CONNECTIONS.
Definition: ptu_registry.c:126
#define PTU_POWERAMP_INPUT_BEACON_SHORT
Definition: ptu_hw_config.h:59
static char m_evt_evt_string[EVT_STRING_LENGTH_MAX]
Definition: ptu_debug.c:56
static uint32_t ms_since(uint32_t tick)
Function for getting the time, in ms, that have passed since the argument system tick.
Definition: ptu_debug.c:300
#define EXTENDED_BEACON_THRESHOLD_MS
Definition: ptu_debug.c:34
void print_string(char *char_array)
Decomposes char array and send chars through UART TX.
Definition: app_print.c:25
uint32_t ptu_tmux_poweramp_enable_get(bool *enable)
Test wrapper for ptu_hal_poweramp_enable_get();.
Definition: ptu_test_mux.c:79
uint32_t ptu_sensors_data_get(const ptu_sensor_data_t **sensors_data)
Get the latest data read from the PTU sensors.
Definition: ptu_sensors.c:404
pru_dynamic_t prev_pru_dynamic
Definition: ptu_registry.h:54
uint32_t ptu_tmux_poweramp_level_get(uint16_t *p_level)
Test wrapper for ptu_hal_poweramp_level_get().
Definition: ptu_test_mux.c:86
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
ptu_reg_item_state_t state
Definition: ptu_registry.h:49
static debug_output_t m_debug_output
Definition: ptu_debug.c:55
PRU Dynamic Parameter structure.
Definition: wpt.h:164
Definition of PTU sensor data.
Definition: ptu_hw_config.h:99
ptu_sm_state_t ptu_sm_execute(ptu_sm_signal_type_t signal, ptu_sm_state_vars_t *p_state)
State machine input signal handler.
Definition: ptu_sm.c:94
static void debug_output_print ( void  )
static

Print contents of m_debug_output to UART.

< Debug output collection

Definition at line 494 of file ptu_debug.c.

495 {
496  int i;
498  uint32_t curr_ticks;
499 
500  (void) app_timer_cnt_get(&curr_ticks);
501 
502  line_print();
503 
504  // PTU main heading
505  print_string("StatusStart ");
506  print_hex((curr_ticks/32), 4); // Output current ticks value in ms.
507  print_string("\r\n");
508 
509  line_print();
510 
511  // Table heading 1
512  print_string("State\tSubst\tLddet\tLoadvar\tRobj\tResimp\tVina\tIina\tTemp\tX Beacon\r\n");
513 
514  // Table contents 1
515  sprintf(temp_string, "%s\t", sm_state_string_get(m_debug_output.current_state));
516  print_string(temp_string);
517 
518  print_string(temp_string);
519 
520  char * x_beacon_status[]={"Off", "On"};
521  sprintf(temp_string, "%u\t%u\t%u\t%u\t%u\t%u\t%d\t%s\r\n",
522  m_debug_output.sensor_data.load_detected,
523  m_debug_output.sensor_data.load_var_detected,
524  m_debug_output.sensor_data.rogue_obj_detected,
525  m_debug_output.sensor_data.res_impedance,
526  m_debug_output.sensor_data.v_ina,
527  m_debug_output.sensor_data.i_ina,
528  m_debug_output.sensor_data.temperature,
529  x_beacon_status[m_debug_output.extended_beacon_currently_running]);
530  print_string(temp_string);
531  line_print();
532 
533  // Table heading 2
534  print_string("Registered PRUs\r\n");
535  line_print();
536 
537  // Table contents 2
538  print_string("#\tVrect\tIrect\tVout\tIout\tPrect\tPutil\tTemp\tCharged\tEncrypt\r\n");
539  for(i = 0; i < PTU_MAX_CONNECTIONS; i++)
540  {
541  if(m_debug_output.reg_item_state[i] == REG_ITEM_STATE_REGISTERED)
542  {
543  sprintf(temp_string, "%u\t%u\t%u\t%u\t%u\t%u\t%u\t%i\t%u\t%u\t",
544  i,
545  m_debug_output.dyn_params[i].vrect,
546  m_debug_output.dyn_params[i].irect,
547  m_debug_output.dyn_params[i].vout,
548  m_debug_output.dyn_params[i].iout,
549  m_debug_output.dyn_params[i].prect,
550  m_debug_output.dyn_params[i].putil,
551  m_debug_output.dyn_params[i].temperature,
552  m_debug_output.dyn_params[i].alerts.charge_complete,
553  m_debug_output.link_encrypted[i]);
554  }
555  else
556  {
557  sprintf(temp_string, "%u\t-\t-\t-\t-\t-\t-\t-\t-\t-", i);
558  }
559  print_string(temp_string);
560  print_string("\r\n");
561  }
562 
563  // Print event message
564  if(strlen(m_evt_evt_string) > 0)
565  {
566  print_string("\r\n");
568  sprintf(m_evt_evt_string, "");
569  }
570 
571  print_string("StatusEnd\r\n");
572 }
static char m_evt_evt_string[EVT_STRING_LENGTH_MAX]
Definition: ptu_debug.c:56
static char * sm_state_string_get(ptu_sm_state_t state)
Convert state to string.
Definition: ptu_debug.c:97
void print_hex(uint32_t number, uint8_t byte_size)
Converts hex number into char and send it through UART TX.
Definition: app_print.c:42
void print_string(char *char_array)
Decomposes char array and send chars through UART TX.
Definition: app_print.c:25
#define DEBUG_LINE_LENGTH
Definition: ptu_debug.c:29
static char temp_string[DEBUG_LINE_LENGTH]
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
static debug_output_t m_debug_output
Definition: ptu_debug.c:55
void line_print(void)
Print a DEBUG_LINE_LENGTH character long "horizontal line" to UART.
Definition: ptu_debug.c:363

Variable Documentation

debug_output_t m_debug_output
static

Debug output collection

Definition at line 55 of file ptu_debug.c.

char m_evt_evt_string[EVT_STRING_LENGTH_MAX] = ""
static

Event message string

Definition at line 56 of file ptu_debug.c.

ptu_tmux_override_t override_values
static

Local copy of override values

Definition at line 57 of file ptu_debug.c.

bool m_uart_initialized = false
static

UART initalized flag. Used to initialize UART on first scharacter sent.

Definition at line 58 of file ptu_debug.c.

bool m_simplified_beacon_print = false
static

Simplify print flag. Used to decide if beacon debug data should be short.

Definition at line 59 of file ptu_debug.c.

bool force_debug_print = false
static

Flag to force debug print even though debug information has not changed.

Definition at line 60 of file ptu_debug.c.