Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
pru_debug.c File Reference
#include <string.h>
#include <stdio.h>
#include "simple_uart.h"
#include "app_print.h"
#include "wpt.h"
#include "pru_sm.h"
#include "pru_hw_config.h"
#include "pru_test_mux.h"
#include "debug.h"
#include "ble_wpts.h"
#include "pru.h"
#include "ble.h"
#include "nrf_soc.h"
#include "softdevice_handler.h"
#include "nrf_sdm.h"
#include "pru_sensors.h"
#include "bsp.h"

Go to the source code of this file.

Macros

#define EVT_STRING_LENGTH_MAX   20
 
#define DEBUG_LINE_LENGTH   80
 
#define VRECT_INCREMENT   500
 
#define IRECT_INCREMENT   100
 
#define VOUT_INCREMENT   500
 
#define IOUT_INCREMENT   100
 
#define TEMPERATURE_INCREMENT   5
 
#define DEBUG_CONN_HANDLE   0
 
#define DEBUG_ALERT_VALUE_HANDLE   0x0f
 

Functions

API implementation
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 pru_tmux_override_t override_values
 
static pru_control_t m_latest_control_data_received
 
static bool m_uart_initialized = false
 
static bool m_transmission_stopped = false
 
static const ble_gap_addr_t MODE_TRANS_ADDRESS
 
void wpts_test_evt_handle_cb (ble_wpts_t *p_wpts, ble_wpts_evt_t *p_evt)
 Override weak wpts_test_evt_handle_cb() More...
 
bool pru_process_command_board (uint8_t cmd)
 Function for processing board specific test commands. More...
 
static bool m_uart_chars_available (void)
 Check if there are any characters in the UART receive buffer. More...
 
static void m_send_mode_transition_alert (uint8_t mode_trans_val, bool include_address)
 Send Mode Transition alert to PTU. More...
 
static bool m_pru_process_command_common (uint8_t cmd)
 Function for processing common test commands. More...
 
static void sm_uart_rx_poll (void)
 Get and process uart commands.
 
static 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 char * m_state_string_get (pru_sm_state_t state)
 Convert state enum to string. More...
 
static void debug_output_print (void)
 Print contents of m_debug_output to UART. More...
 

Macro Definition Documentation

#define EVT_STRING_LENGTH_MAX   20

Max length of event message string

Definition at line 34 of file pru_debug.c.

#define DEBUG_LINE_LENGTH   80

Width of debug output table

Definition at line 35 of file pru_debug.c.

#define VRECT_INCREMENT   500

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

Definition at line 36 of file pru_debug.c.

#define IRECT_INCREMENT   100

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

Definition at line 37 of file pru_debug.c.

#define VOUT_INCREMENT   500

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

Definition at line 38 of file pru_debug.c.

#define IOUT_INCREMENT   100

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

Definition at line 39 of file pru_debug.c.

#define TEMPERATURE_INCREMENT   5

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

Definition at line 40 of file pru_debug.c.

#define DEBUG_CONN_HANDLE   0

Assumed Connection handle during debug.

Definition at line 42 of file pru_debug.c.

#define DEBUG_ALERT_VALUE_HANDLE   0x0f

Assumed handle of Alert characteristic value during debug.

Definition at line 43 of file pru_debug.c.

Function Documentation

void wpts_test_evt_handle_cb ( ble_wpts_t p_wpts,
ble_wpts_evt_t p_evt 
)

Override weak wpts_test_evt_handle_cb()

Parameters
p_wptsWPT Service
p_evtWPT Service event

Definition at line 77 of file pru_debug.c.

78 {
80  {
82  }
83 }
PRU Control structure.
Definition: wpt.h:79
ble_wpts_evt_type_t evt_type
Definition: ble_wpts.h:43
union ble_wpts_evt_t::@0 data
pru_control_t pru_control
Definition: ble_wpts.h:46
static pru_control_t m_latest_control_data_received
Definition: pru_debug.c:63
bool pru_process_command_board ( uint8_t  cmd)

Function for processing board specific 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 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.

Return values
trueThere is a character available for reading
falsethere is not a character available for reading

Definition at line 104 of file pru_debug.c.

105 {
106  return (NRF_UART0->EVENTS_RXDRDY == 1);
107 }
static void m_send_mode_transition_alert ( uint8_t  mode_trans_val,
bool  include_address 
)
static

Send Mode Transition alert to PTU.

Parameters
include_addressShould the address be non-null?
mode_trans_valValue to use in Mode Transition Value field of PRU alert.

Definition at line 114 of file pru_debug.c.

115 {
116  uint32_t err_code;
117  uint8_t alert[BLE_WPTS_PRU_ALERT_MAX_LEN];
118  uint16_t hvx_len = BLE_WPTS_PRU_ALERT_MAX_LEN;
119  ble_gatts_hvx_params_t params;
120 
121  memset(alert, 0, BLE_WPTS_PRU_ALERT_MAX_LEN);
122  params.offset = 0;
123  params.handle = DEBUG_ALERT_VALUE_HANDLE;
124  params.type = BLE_GATT_HVX_INDICATION;
125  params.p_data = alert;
126  params.p_len = &hvx_len;
127 
131  mode_trans_val);
132 
133  if(include_address)
134  {
135  memcpy(&params.p_data[1], MODE_TRANS_ADDRESS.addr, 6); // Copy mode transition address
136 
137  err_code = sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &MODE_TRANS_ADDRESS);
138  APP_ERROR_CHECK(err_code);
139  }
140 
141 
142  else
143  memset(&params.p_data[1], 0, 6);
144 
145 
146  err_code = sd_ble_gatts_hvx(DEBUG_CONN_HANDLE, &params);
147  APP_ERROR_CHECK(err_code);
148 }
#define BLE_WPTS_PRU_ALERT_POS
PRU alert characteristic byte positions.
#define BLE_WPTS_PRU_ALERT_MODE_TRANSITION_BITPOS
#define BLE_WPTS_PRU_ALERT_MAX_LEN
#define BLE_WPTS_BITFIELD_WRITE(bitfield, msk, pos, val)
Write bitfield.
#define BLE_WPTS_PRU_ALERT_MODE_TRANSITION_BITMSK
static const ble_gap_addr_t MODE_TRANS_ADDRESS
Definition: pru_debug.c:66
#define DEBUG_CONN_HANDLE
Definition: pru_debug.c:42
#define DEBUG_ALERT_VALUE_HANDLE
Definition: pru_debug.c:43
static bool m_pru_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 159 of file pru_debug.c.

160 {
161  bool b_cmd_processed = true;
162  uint32_t err_code;
163 
165 
166  switch (cmd)
167  {
168  case 'q':
169  print_string("Command: Power adjusted bit set");
171  print_string("\n\r");
172  break;
173 
174  case 'a':
175  print_string("Command: Power adjusted bit cleared");
177  print_string("\n\r");
178  break;
179 
180  case 'w':
181  print_string("Command: Inc IOUT -> ");
184  print_string("\n\r");
185  break;
186 
187  case 's':
188  print_string("Command: Dec IOUT -> ");
191  print_string("\n\r");
192  break;
193 
194  case 'r':
195  print_string("Command: Inc VOUT -> ");
198  print_string("\n\r");
199  break;
200 
201  case 'f':
202  print_string("Command: Dec VOUT -> ");
205  print_string("\n\r");
206  break;
207 
208  case 't':
209  print_string("Command: Wired charger on\n\r");
211  break;
212 
213  case 'g':
214  print_string("Command: Wired charger off\n\r");
216  break;
217 
218  case 'y':
219  print_string("Command: Inc VRECT -> ");
222  print_string("\n\r");
223  break;
224 
225  case 'h':
226  print_string("Command: Dec VRECT -> ");
229  print_string("\n\r");
230  break;
231 
232  case 'u':
233  print_string("Command: Inc IRECT -> ");
236  print_string("\n\r");
237  break;
238 
239  case 'j':
240  print_string("Command: Dec IRECT -> ");
243  print_string("\n\r");
244  break;
245 
246  case 'i':
247  print_string("Command: Inc TEMP -> ");
250  print_string("\n\r");
251  break;
252 
253  case 'k':
254  print_string("Command: Dec TEMP -> ");
257  print_string("\n\r");
258  break;
259 
260  case 'o':
261  print_string("Command: Test-mode enabled\n\r");
262  tmux_get_select(TMUX_SELECT_OVERRIDE);
263  tmux_set_select(TMUX_SELECT_OVERRIDE);
264  break;
265 
266  case 'p':
267  print_string("SDK Version ");
269  print_string("\n\r");
270  break;
271 
272  case 'l':
273  print_string("Command: Test-mode disabled\n\r");
274  tmux_get_select(TMUX_SELECT_NO_OVERRIDE);
275  tmux_set_select(TMUX_SELECT_NO_OVERRIDE);
276  break;
277 
278  case 'z':
279  print_string("Command: Reset\r\n");
280  NVIC_SystemReset();
281  break;
282 
283  case 'x':
284  print_string("Command: Send Slave Security Request\n\r");
285 
286  ble_gap_sec_params_t sec_params;
287  memset(&sec_params, 0, sizeof(ble_gap_sec_params_t));
288  sec_params.mitm = 0;
289  sec_params.bond = 0;
290 
291  err_code = sd_ble_gap_authenticate(DEBUG_CONN_HANDLE, &sec_params);
292  APP_ERROR_CHECK(err_code);
293 
294  break;
295 
296  case 'n':
297  print_string("Command: Charge complete\n\r");
299  break;
300 
301  case 'm':
302  print_string("Command: Charge incomplete\n\r");
304  break;
305 
306  case '1':
307  print_string("Command: Vrect to PRU_VRECT_UVLO\n\r");
309  break;
310 
311  case '2':
312  print_string("Command: Vrect to PRU_VRECT_BOOT\n\r");
314  break;
315 
316  case '3':
317  print_string("Command: Vrect to PRU_VRECT_MIN\n\r");
319  break;
320 
321  case '4':
322  print_string("Command: Vrect to PRU_VRECT_HIGH\n\r");
324  break;
325 
326  case '5':
327  print_string("Command: Vrect to PRU_VRECT_MAX\n\r");
329  break;
330 
331  case 'e':
333  break;
334 
335  case 'd':
337  break;
338 
339  case 'E':
341  break;
342 
343  case 'D':
345  break;
346 
347  case 'v':
349  {
350  print_string("Command: Stop packet transmission\n\r");
351  m_transmission_stopped = !m_transmission_stopped; // Need to do this before disabling the softdevice
352  (void)sd_softdevice_disable();
353  }
354 
355  else
356  {
357  print_string("Command: Start packet transmission\n\r");
359  nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC;
360  SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL);
361  }
362  break;
363  case '6':
364  print_string("\n\rpru\n\r");
365  break;
366  case '7':
367  case '8':
368  #ifdef DFU_SUPPORT
369  print_string("Entering DFU mode ...\n\r");
370  dfu_start();
371  break;
372  #endif // DFU_SUPPORT
373 
374  case '9':
375  case 'c':
376  case '0':
377  case ',':
378  case '.':
379  default:
380  b_cmd_processed = false;
381  break;
382  }
383 
385 
386  return b_cmd_processed;
387 }
#define IRECT_INCREMENT
Definition: pru_debug.c:37
#define PRU_VRECT_MIN
Definition: pru_hw_config.h:25
static bool m_transmission_stopped
Definition: pru_debug.c:65
#define PRU_VRECT_HIGH
Definition: pru_hw_config.h:27
#define VRECT_INCREMENT
Definition: pru_debug.c:36
#define VOUT_INCREMENT
Definition: pru_debug.c:38
#define IOUT_INCREMENT
Definition: pru_debug.c:39
#define PRU_VRECT_BOOT
Definition: pru_hw_config.h:23
#define BLE_WPTS_PRU_ALERT_MODE_6S_MODE_TRANSITION_VAL
void pru_tmux_override_values_set(pru_tmux_override_t *values)
Set override values.
Definition: pru_test_mux.c:146
#define PRU_VRECT_MAX
Definition: pru_hw_config.h:28
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
#define BLE_WPTS_PRU_ALERT_MODE_3S_MODE_TRANSITION_VAL
static pru_tmux_override_t override_values
Definition: pru_debug.c:62
void print_string(char *char_array)
Decomposes char array and send chars through UART TX.
Definition: app_print.c:25
#define PRU_VRECT_UVLO
Definition: pru_hw_config.h:24
void pru_tmux_override_values_get(pru_tmux_override_t *values)
Get ovveride values.
Definition: pru_test_mux.c:152
void tmux_get_select(tmux_select_t select)
Select source for all **tmux**get() functions.
Definition: pru_test_mux.c:135
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 SDK_VERSION
Definition: wpt.h:30
#define DEBUG_CONN_HANDLE
Definition: pru_debug.c:42
#define TEMPERATURE_INCREMENT
Definition: pru_debug.c:40
static void m_send_mode_transition_alert(uint8_t mode_trans_val, bool include_address)
Send Mode Transition alert to PTU.
Definition: pru_debug.c:114
static bool debug_output_changed ( void  )
static

Update m_debug_output variable.

Returns
true if debug output has changed.

Definition at line 433 of file pru_debug.c.

434 {
435  const pru_sensor_data_t * sensor_data;
436  bool changed = false;
437 
438  sensor_data = pru_sensors_data_get();
439 
441  {
442  changed = true;
444  }
445 
446  if(memcmp(&m_debug_output.sensor_data, sensor_data, sizeof(pru_sensor_data_t)) != 0)
447  {
448  changed = true;
449  memcpy(&m_debug_output.sensor_data, sensor_data, sizeof(pru_sensor_data_t));
450  }
451 
452  if(memcmp(&m_debug_output.control_data, &m_latest_control_data_received, sizeof(pru_control_t)))
453  {
454  changed = true;
455  memcpy(&m_debug_output.control_data, &m_latest_control_data_received, sizeof(pru_control_t));
456  }
457 
458  if(strlen(m_evt_evt_string) > 0)
459  {
460  changed = true;
461  }
462 
463  return changed;
464 }
PRU Control structure.
Definition: wpt.h:79
const pru_sensor_data_t * pru_sensors_data_get(void)
Get the latest data from the PRU sensors.
Definition: pru_sensors.c:146
Definition of PRU reporting data.
Definition: pru_sensors.h:26
static char m_evt_evt_string[EVT_STRING_LENGTH_MAX]
Definition: pru_debug.c:61
static pru_control_t m_latest_control_data_received
Definition: pru_debug.c:63
pru_sm_state_t pru_sm_execute(pru_sm_signal_type_t sig, pru_sm_state_vars_t *p_state)
Execute state machine.
Definition: pru_sm.c:71
static debug_output_t m_debug_output
Definition: pru_debug.c:60
static char* m_state_string_get ( pru_sm_state_t  state)
static

Convert state enum to string.

Parameters
stateThe state enum to convert
Returns
Pointer to char.

Definition at line 470 of file pru_debug.c.

471 {
472  switch(state)
473  {
474  case PRU_SM_STATE_NULL:
475  return "PRU Null";
476  case PRU_SM_STATE_BOOT:
477  return "PRU Boot";
478  case PRU_SM_STATE_PRU_ON:
479  return "PRU On";
480  case PRU_SM_STATE_SYSTEM_ERROR:
481  return "PRU System Er-or";
482  default:
483  return "Invalid state";
484  }
485 
486 }
static void debug_output_print ( void  )
static

Print contents of m_debug_output to UART.

< Debug output collection

Definition at line 490 of file pru_debug.c.

491 {
494  // Status
495  line_print();
496  print_string("StatusStart\r\n");
497  line_print();
498  print_string("State\r\n");
499  sprintf(temp_string, "%s\r\n", m_state_string_get(m_debug_output.state));
500  print_string(temp_string);
501 
502  // Dynamic parameters
503  line_print();
504  print_string("Dynamic parameters\r\n");
505  line_print();
506  print_string("Vrect\tIrect\tVout\tIout\tTemp\tCharged\tP_adj\r\n");
507  sprintf(temp_string, "%u\t%u\t%u\t%u\t%d\t%u\t%u\r\n",
508  m_debug_output.sensor_data.vrect,
509  m_debug_output.sensor_data.irect,
510  m_debug_output.sensor_data.vout,
511  m_debug_output.sensor_data.iout,
512  m_debug_output.sensor_data.temperature,
513  m_debug_output.sensor_data.charge_complete,
514  m_debug_output.sensor_data.power_adjusted);
515 
516  print_string(temp_string);
517 
518  // Static parameters
519  line_print();
520  print_string("Static parameters\r\n");
521  line_print();
522  print_string("Vrset\tVrmin\tVrhigh\tTempmax\tVrmax\tPrmax\r\n");
523  sprintf(temp_string, "%u\t%u\t%u\t%u\t%u\t%u\r\n",
527  PRU_TEMP_MAX,
529  PRU_PRECT_MAX);
530  print_string(temp_string);
531 
532  // Previous received control data
533  line_print();
534  print_string("Control packet\r\n");
535  line_print();
536 
537  print_string("En_out\tEn_Chin\tAdj_pow\tPerm\tTset\r\n");
538  sprintf(temp_string, "%u\t%u\t%u\t%u\t%u\r\n",
539  m_debug_output.control_data.enable_pru_output,
540  m_debug_output.control_data.enable_pru_charge_indicator,
541  m_debug_output.control_data.adj_power,
542  m_debug_output.control_data.permissions,
543  m_debug_output.control_data.time_set);
544  print_string(temp_string);
545 
546  // Print event message
547  if(strlen(m_evt_evt_string) > 0)
548  {
549  print_string("\r\n");
551  sprintf(m_evt_evt_string, "");
552  }
553 
554  print_string("StatusEnd\r\n");
555 }
#define PRU_VRECT_MIN
Definition: pru_hw_config.h:25
#define PRU_VRECT_HIGH
Definition: pru_hw_config.h:27
static char * m_state_string_get(pru_sm_state_t state)
Convert state enum to string.
Definition: pru_debug.c:470
#define PRU_TEMP_MAX
Definition: pru_hw_config.h:30
static char m_evt_evt_string[EVT_STRING_LENGTH_MAX]
Definition: pru_debug.c:61
#define PRU_VRECT_SET
Definition: pru_hw_config.h:26
#define PRU_VRECT_MAX
Definition: pru_hw_config.h:28
#define PRU_PRECT_MAX
Definition: pru_hw_config.h:43
#define DEBUG_LINE_LENGTH
Definition: pru_debug.c:35
void print_string(char *char_array)
Decomposes char array and send chars through UART TX.
Definition: app_print.c:25
static void line_print(void)
Print a DEBUG_LINE_LENGTH character long "horizontal line" to UART.
Definition: pru_debug.c:419
static char temp_string[DEBUG_LINE_LENGTH]
static debug_output_t m_debug_output
Definition: pru_debug.c:60

Variable Documentation

debug_output_t m_debug_output
static

Debug output collection

Definition at line 60 of file pru_debug.c.

char m_evt_evt_string[EVT_STRING_LENGTH_MAX] = ""
static

Event message string

Definition at line 61 of file pru_debug.c.

pru_tmux_override_t override_values
static

Local copy of override values

Definition at line 62 of file pru_debug.c.

pru_control_t m_latest_control_data_received
static

Local latest received control data

Definition at line 63 of file pru_debug.c.

bool m_uart_initialized = false
static

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

Definition at line 64 of file pru_debug.c.

bool m_transmission_stopped = false
static

Is transmission stopped?

Definition at line 65 of file pru_debug.c.

const ble_gap_addr_t MODE_TRANS_ADDRESS
static
Initial value:
=
{.addr_type = BLE_GAP_ADDR_TYPE_RANDOM_STATIC,
.addr = {0xee, 0xee, 0xff, 0xff, 0xee, 0xee}}

Address to use in Mode Transition if address is non-0

Definition at line 66 of file pru_debug.c.