Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
ptu_latching_fault.c
Go to the documentation of this file.
1 /* Copyright (c) Nordic Semiconductor. All Rights Reserved.
2  *
3  * The information contained herein is property of Nordic Semiconductor ASA.
4  * Terms and conditions of usage are described in detail in NORDIC
5  * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
6  *
7  * Licensees are granted free, non-transferable use of the information. NO
8  * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
9  * the file.
10  *
11  */
12 
15 #include <stdint.h>
16 #include <stdbool.h>
17 #include <string.h>
18 
19 #include "ptu_latching_fault.h"
20 #include "ptu_config.h"
21 #include "ptu_sensors.h"
22 #include "ptu_power_ctl.h"
23 #include "ptu_test_mux.h"
24 
25 /******************************************************************************/
28 /******************************************************************************/
29 
34 typedef enum
35 {
39 
44 typedef struct
45 {
46  lf_timer_state_t timer_state;
47  uint32_t successive_faults;
48  bool persistent;
49 } lf_status_t;
50 
51 
53 APP_TIMER_DEF(m_clear_latching_fault_timer_id);
54 APP_TIMER_DEF(m_short_beacon_timer_id);
55 APP_TIMER_DEF(m_reset_nof_consecutive_latching_fault_count_timer_id);
56 static lf_status_t m_lf_status;
64 {
65  m_lf_status.successive_faults = 0;
66  m_lf_status.persistent = false;
67 }
68 
74 static void m_latch_fault_attempt_to_clear_timer_handler(void * p_context)
75 {
76  m_ptu_sm_execute(PTU_SM_SIGNAL_LATCHING_FAULT_CLEARED_BY_USER);
77 }
78 
79 
86 static bool m_no_devices_on_pad(const ptu_sensor_data_t * p_sensor_data)
87 {
88  static uint8_t load_var_detect_count = 0;
89 
90  if((p_sensor_data->res_impedance < PTU_Z_TX_IN_NO_LOAD))
91  {
92  load_var_detect_count = 0;
93  return false;
94  }
95 
97  {
98  load_var_detect_count = 0;
99  return true;
100  }
101 
102  return false;
103 }
104 
109 static void m_latch_fault_short_beacon_timer_handler(void * p_context)
110 {
111  uint32_t err_code;
112  const ptu_sensor_data_t * p_sensor_data;
113 
114 
115  if(PTU_SM_CURRENT_STATE() != PTU_SM_STATE_LATCH_FAULT)
116  {
117  return;
118  }
119 
120  switch(m_lf_status.timer_state)
121  {
122  case LF_TIMER_STATE_BEACON_OFF: // start short beacon
123 
125  APP_ERROR_CHECK(err_code);
126 
127  err_code = ptu_tmux_poweramp_enable_set(true);
128  APP_ERROR_CHECK(err_code);
129 
130  err_code = TIMER_START(m_short_beacon_timer_id, PTU_BEACON_DURATION_SHORT_MS, NULL);
131  APP_ERROR_CHECK(err_code);
132 
134  break;
135 
136  case LF_TIMER_STATE_BEACON_SHORT: // end short beacon
137  // sensors needs to be read before disabling poweramp in order for load variation detection to be performed.
138  err_code = ptu_sensors_read();
139  APP_ERROR_CHECK(err_code);
140 
141  err_code = ptu_tmux_poweramp_enable_set(false);
142  APP_ERROR_CHECK(err_code);
143 
144  err_code = TIMER_START(m_short_beacon_timer_id, (PTU_BEACON_CYCLE_PERIOD_MS - PTU_BEACON_DURATION_SHORT_MS), NULL);
145  APP_ERROR_CHECK(err_code);
146 
148 
149  err_code = ptu_sensors_data_get(&p_sensor_data);
150  APP_ERROR_CHECK(err_code);
151 
152  // Non-persistent latching fault has been cleared by seeing a load variation
153  if(!m_lf_status.persistent && p_sensor_data->load_var_detected)
154  {
155  m_ptu_sm_execute(PTU_SM_SIGNAL_LATCHING_FAULT_CLEARED_BY_USER);
156  }
157 
158  // User has cleared persistent latching fault by removing all devices from pad
159  else if(m_lf_status.persistent && m_no_devices_on_pad(p_sensor_data))
160  {
161  m_lf_status.successive_faults = 0;
162  m_lf_status.persistent = false;
163  m_ptu_sm_execute(PTU_SM_SIGNAL_LATCHING_FAULT_CLEARED_BY_USER);
164  }
165  break;
166 
167  default:
168  break;
169  }
170 }
171 
174 /******************************************************************************/
177 /******************************************************************************/
178 
180 {
181  uint32_t err_code;
182 
183  if(PTU_SM_CURRENT_STATE() != PTU_SM_STATE_LATCH_FAULT)
184  {
185  return NRF_ERROR_INVALID_STATE;
186  }
187 
189 
190  err_code = TIMER_START(m_short_beacon_timer_id, PTU_LATCH_FAULT_ENTRY_DELAY_MS, NULL);
191  APP_ERROR_CHECK(err_code);
192 
193  err_code = TIMER_START(m_reset_nof_consecutive_latching_fault_count_timer_id, PTU_LATCH_FAULT_NO_ERR_CLR_TIMEOUT_MS, NULL);
194  APP_ERROR_CHECK(err_code);
195 
196  if(m_lf_status.successive_faults ++ < PTU_LATCH_FAULT_MAX_AUTO_CLR_ATTEMPTS)
197  {
198  err_code = TIMER_START(m_clear_latching_fault_timer_id, PTU_LATCH_FAULT_EXIT_ATTEMPT_DELAY_MS, NULL);
199  APP_ERROR_CHECK(err_code);
200  }
201 
202  else
203  {
204  // Count of successive latching faults indicates that PTU is now in persistent latching fault
205  m_lf_status.persistent = true;
206  }
207 
208  return NRF_SUCCESS;
209 }
210 
211 
213 {
214  uint32_t err_code;
215 
216  if(sm_handler == NULL)
217  {
218  return NRF_ERROR_INVALID_PARAM;
219  }
220 
221  m_ptu_sm_execute = sm_handler;
222 
223  memset(&m_lf_status, 0, sizeof(lf_status_t));
224 
225  err_code = app_timer_create(&m_clear_latching_fault_timer_id,
226  APP_TIMER_MODE_SINGLE_SHOT,
228  APP_ERROR_CHECK(err_code);
229 
230  err_code = app_timer_create(&m_short_beacon_timer_id,
231  APP_TIMER_MODE_SINGLE_SHOT,
233  APP_ERROR_CHECK(err_code);
234 
235  err_code = app_timer_create(&m_reset_nof_consecutive_latching_fault_count_timer_id,
236  APP_TIMER_MODE_SINGLE_SHOT,
238  APP_ERROR_CHECK(err_code);
239 
240  return NRF_SUCCESS;
241 }
242 
#define PTU_BEACON_DURATION_SHORT_MS
Definition: ptu_config.h:184
void(* ptu_sm_handler_t)(ptu_sm_signal_type_t signal)
Definition: ptu.h:30
uint32_t ptu_sensors_read(void)
Read sensors. This function should typically be called regularly by a timer and can generate events b...
Definition: ptu_sensors.c:303
#define PTU_BEACON_CYCLE_PERIOD_MS
Definition: ptu_config.h:182
#define PTU_LATCH_FAULT_NO_LOAD_VAR_NO_DEVICES_MS
Definition: ptu_config.h:210
#define PTU_POWERAMP_INPUT_BEACON_SHORT
Definition: ptu_hw_config.h:59
static lf_status_t m_lf_status
uint32_t ptu_tmux_poweramp_enable_set(bool enable)
Test wrapper for ptu_hal_poweramp_enable_set().
Definition: ptu_test_mux.c:69
APP_TIMER_DEF(m_clear_latching_fault_timer_id)
uint32_t ptu_power_ctrl_set_poweramp_input(uint16_t level)
Perform power control adjustments.
static ptu_sm_handler_t m_ptu_sm_execute
lf_timer_state_t
Enumerator describing what m_short_beacon_timer_id is being used for.
uint16_t res_impedance
static bool m_no_devices_on_pad(const ptu_sensor_data_t *p_sensor_data)
Check if no load variation has been seen for PTU_PRU_NO_LOAD_VAR_NO_DEVICES_MS milliseconds, implying that no devices are on the pad.
uint32_t ptu_latching_fault_init(ptu_sm_handler_t sm_handler)
Initialize latching fault module.
ptu_sm_state_t
PTU states.
Definition: ptu_sm.h:35
#define PTU_LATCH_FAULT_ENTRY_DELAY_MS
Definition: ptu_config.h:205
static void m_reset_nof_consecutive_latching_fault_count_timer_handler(void *p_context)
Clear number of consecutive latching faults timer handler.
#define TIMER_START(timer_id, ms, p_ctx)
Definition: wpt.h:32
#define PTU_LATCH_FAULT_NO_ERR_CLR_TIMEOUT_MS
Definition: ptu_config.h:209
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
#define PTU_LATCH_FAULT_EXIT_ATTEMPT_DELAY_MS
Definition: ptu_config.h:206
#define PTU_Z_TX_IN_NO_LOAD
Definition: ptu_config.h:71
static void m_latch_fault_attempt_to_clear_timer_handler(void *p_context)
Latch fault entry handler.
#define PTU_SM_CURRENT_STATE()
Definition: ptu.h:36
#define PTU_LATCH_FAULT_MAX_AUTO_CLR_ATTEMPTS
Definition: ptu_config.h:207
uint32_t ptu_latching_fault_entered(ptu_sm_state_t previous)
Handle the entry of latching fault state.
static void m_latch_fault_short_beacon_timer_handler(void *p_context)
Latch fault short beacon timer handler.
Definition of PTU sensor data.
Definition: ptu_hw_config.h:99