Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
ptu_power_ctl.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 <math.h>
16 
17 #include "ptu_power_ctl.h"
18 #include "ptu_test_mux.h"
19 #include "ptu_registry.h"
20 #include "nrf_assert.h"
21 
22 /******************************************************************************/
25 /******************************************************************************/
26 
28 typedef enum
29 {
34 
35 
36 static bool m_itx_adj_disabled = false;
40 static uint16_t m_conn_handle_dominant;
49 static void m_find_vrect_high_and_min(ptu_reg_item_t const * p_reg_item, uint16_t * p_vrect_high, uint16_t * p_vrect_min)
50 {
51  bool has_dynamic_vrect_min = BLE_WPTS_BITFIELD_READ(p_reg_item->prev_pru_dynamic.optional_fields,
54 
55  bool has_dynamic_vrect_high = BLE_WPTS_BITFIELD_READ(p_reg_item->prev_pru_dynamic.optional_fields,
58 
59  *p_vrect_high = has_dynamic_vrect_high ?
60  p_reg_item->prev_pru_dynamic.vrect_high_dyn :
62 
63  *p_vrect_min = has_dynamic_vrect_min ?
64  p_reg_item->prev_pru_dynamic.vrect_min_dyn :
66 }
67 
75  bool * p_reduced_positive_step_size,
76  bool * p_reduced_negative_step_size)
77 {
78  uint16_t vrect_high;
79  uint16_t vrect_min;
80 
81  m_find_vrect_high_and_min(p_reg_item, &vrect_high, &vrect_min);
82 
84  {
85  *p_reduced_positive_step_size = true;
86  }
87 
89  {
90  *p_reduced_negative_step_size = true;
91  }
92 }
93 
94 
102  bool * p_reduced_positive_step_size,
103  bool * p_reduced_negative_step_size)
104 {
105  uint16_t vrect = p_dominant_pru_reg_item->prev_pru_dynamic.vrect;
106  uint16_t vrect_set = p_dominant_pru_reg_item->prev_pru_static.vrect_set;
107 
108  // If V_RECT qualifies PTU for step size reduction
111  {
112  *p_reduced_negative_step_size = true;
113  *p_reduced_positive_step_size = true;
114  }
115 }
116 
117 
129 static void m_pru_scan_all(bool * p_reduced_positive_step_size, bool * p_reduced_negative_step_size, bool * p_increased_step_size)
130 {
131  ptu_reg_item_t* registered_devices[PTU_MAX_CONNECTIONS];
132  uint8_t n_reg_devices = ptu_reg_registered_devices_get(registered_devices);
133  bool high_voltage_f = false;
134  bool low_voltage_f = false;
135  uint8_t l_putil_max = 0;
136  int8_t dominant_pru_index = -1;
137 
138  *p_reduced_positive_step_size = false;
139  *p_reduced_negative_step_size = false;
140  *p_increased_step_size = true;
141 
142  for(uint8_t i = 0; i < n_reg_devices; ++i)
143  {
144  m_update_power_amplifier_adjustment_step_size(registered_devices[i], p_reduced_positive_step_size, p_reduced_negative_step_size);
145 
146  if(registered_devices[i]->prev_pru_dynamic.putil > l_putil_max)
147  {
148  l_putil_max = registered_devices[i]->prev_pru_dynamic.putil;
149  dominant_pru_index = i;
150  }
151 
152  if(registered_devices[i]->prev_pru_static.pru_category < BLE_WPTS_PRU_CATEGORY_4)
153  {
154  *p_increased_step_size = false;
155  }
156 
157  // Note that the PRU Static bitfields vrect_min_static and vrect_high_static
158  // are updated to values from the last read PRU Dynamic characteristic if the
159  // respective optional fields are enabled.
160  if(registered_devices[i]->prev_pru_dynamic.vrect > registered_devices[i]->prev_pru_static.vrect_high_static - PTU_VRECT_HIGH_OFFSET)
161  {
162  high_voltage_f = true;
163  }
164 
165  else if(registered_devices[i]->prev_pru_dynamic.vrect < registered_devices[i]->prev_pru_static.vrect_min_static)
166  {
167  low_voltage_f = true;
168  }
169  }
170 
171  if(high_voltage_f)
172  {
174  }
175  else if(low_voltage_f)
176  {
178  }
179  else
180  {
182  }
183 
184  if(dominant_pru_index >= 0)
185  {
186  m_conn_handle_dominant = registered_devices[dominant_pru_index]->ble_wpts_c.conn_handle;
187 
188  m_update_power_amplifier_adjustment_step_size_for_dominant_pru(registered_devices[dominant_pru_index],
189  p_reduced_positive_step_size,
190  p_reduced_negative_step_size);
191  }
192 
193 }
194 
200 static uint32_t m_get_step_size(bool reduced_step_size, bool increased_step_size)
201 {
202  if(reduced_step_size)
203  {
205  }
206 
207  else if(increased_step_size)
208  {
210  }
211 
213 }
214 
215 
221 static uint32_t m_poweramp_input_step_up(bool reduced_positive_step_size, bool increased_step_size)
222 {
223  uint32_t err_code;
224  uint16_t level_to_set;
225  uint16_t current_level;
226  uint16_t step_size;
227 
228  err_code = ptu_tmux_poweramp_level_get(&current_level);
229 
230  if(err_code != NRF_SUCCESS)
231  {
232  return err_code;
233  }
234 
235  step_size = m_get_step_size(reduced_positive_step_size, increased_step_size);
236 
237  if (current_level + step_size > PTU_POWERAMP_INPUT_MAX)
238  {
239  level_to_set = PTU_POWERAMP_INPUT_MAX;
240  }
241 
242  else
243  {
244  level_to_set = current_level + step_size;
245  }
246 
247  return ptu_power_ctrl_set_poweramp_input(level_to_set);
248 }
249 
255 static uint32_t m_poweramp_input_step_down(bool reduced_negative_step_size, bool increased_step_size)
256 {
257  uint32_t err_code;
258  uint16_t level_to_set;
259  uint16_t current_level;
260  uint16_t step_size;
261 
262  err_code = ptu_tmux_poweramp_level_get(&current_level);
263 
264  if(err_code != NRF_SUCCESS)
265  {
266  return err_code;
267  }
268 
269  step_size = m_get_step_size(reduced_negative_step_size, increased_step_size);
270 
271  if (((int32_t)current_level - (int32_t)step_size) < PTU_POWERAMP_INPUT_NOMINAL)
272  {
273  level_to_set = PTU_POWERAMP_INPUT_NOMINAL;
274  }
275 
276  else
277  {
278  level_to_set = current_level - step_size;
279  }
280 
281  return ptu_power_ctrl_set_poweramp_input(level_to_set);
282 }
283 
286 /******************************************************************************/
289 /******************************************************************************/
290 
291 
293 {
294  uint32_t err_code;
295  bool reduced_positive_step_size;
296  bool reduced_negative_step_size;
297  bool increased_step_size;
298 
299  m_pru_scan_all(&reduced_positive_step_size, &reduced_negative_step_size, &increased_step_size);
300 
303 
304  switch(m_substate)
305  {
307  if(l_pru_dynamic -> vrect < (l_pru_static -> vrect_set - (PTU_POWER_TRANSFER_HYSTERISIS / 2)))
308  {
309  err_code = m_poweramp_input_step_up(reduced_positive_step_size, increased_step_size);
310  APP_ERROR_CHECK(err_code);
311  }
312  else if(l_pru_dynamic -> vrect > (l_pru_static -> vrect_set + (PTU_POWER_TRANSFER_HYSTERISIS / 2)))
313  {
314  err_code = m_poweramp_input_step_down(reduced_negative_step_size, increased_step_size);
315  APP_ERROR_CHECK(err_code);
316  }
317  break;
318 
320  // false as argument since modified step size is not permitted in substate 2
321  err_code = m_poweramp_input_step_up(false, false);
322  APP_ERROR_CHECK(err_code);
323  break;
324 
326  // false as argument since modified step size is not permitted in substate 3
327  err_code = m_poweramp_input_step_down(false, false);
328  APP_ERROR_CHECK(err_code);
329  break;
330 
331  default:
332  break;
333  }
334 }
335 
336 uint32_t ptu_power_ctrl_set_poweramp_input(uint16_t level)
337 {
338  if(!m_itx_adj_disabled)
339  return ptu_tmux_poweramp_level_set(level);
340  return NRF_SUCCESS;
341 }
342 
344 {
345  m_itx_adj_disabled = val;
346 }
347 
349 {
350  uint16_t tmp_itx;
351 
352  switch(cmd)
353  {
355  break;
356 
358  APP_ERROR_CHECK(ptu_tmux_poweramp_level_get(&tmp_itx));
360  break;
361 
363  APP_ERROR_CHECK(ptu_tmux_poweramp_level_get(&tmp_itx));
365  break;
366 
368  break;
369 
370  default:
371  ASSERT(false);
372  break;
373  }
374 }
375 
#define PTU_POWERAMP_INPUT_STEP_SIZE_INCREASED
Definition: ptu_hw_config.h:79
#define PTU_POWERAMP_INPUT_TEST_STEP_SIZE
Definition: ptu_hw_config.h:58
static void m_find_vrect_high_and_min(ptu_reg_item_t const *p_reg_item, uint16_t *p_vrect_high, uint16_t *p_vrect_min)
Inspect registry item and find the current value of VRECT_MIN and VRECT_HIGH.
Definition: ptu_power_ctl.c:49
Registry item.
Definition: ptu_registry.h:47
uint8_t putil
Definition: wpt.h:179
#define PTU_DOMINANT_PRU_LOWER_LIMIT_VRECTSET_MULTIPLIER_FOR_REDUCTION_IN_STEP_SIZE
Definition: ptu_config.h:60
#define PTU_POWERAMP_INPUT_MAX
Definition: ptu_hw_config.h:73
uint16_t conn_handle
Definition: ble_wpts_c.h:66
uint8_t ptu_reg_registered_devices_get(ptu_reg_item_t **registered_devices)
Get handles for all registered devices.
Definition: ptu_registry.c:254
ble_wpts_c_t ble_wpts_c
Definition: ptu_registry.h:52
void ptu_power_ctrl_adjust(void)
Perform power control adjustments.
power_transfer_substates_t
Definition of power transfer substates.
Definition: ptu_power_ctl.c:28
#define BLE_WPTS_PRU_DYNAMIC_OPTION_VRECT_MIN_DYN_BITPOS
#define PTU_POWERAMP_INPUT_STEP_SIZE_REDUCED
Definition: ptu_hw_config.h:76
#define BLE_WPTS_PRU_CATEGORY_4
uint16_t vrect
Definition: wpt.h:167
static uint32_t m_get_step_size(bool reduced_step_size, bool increased_step_size)
Find step size to be used given permissions to use reduced and/or increased step size.
static power_transfer_substates_t m_substate
Definition: ptu_power_ctl.c:38
uint16_t vrect_high_dyn
Definition: wpt.h:174
static uint32_t m_poweramp_input_step_down(bool reduced_negative_step_size, bool increased_step_size)
Decrease PTU poweramp output.
uint32_t ptu_power_ctrl_set_poweramp_input(uint16_t level)
Perform power control adjustments.
#define PTU_POWERAMP_INPUT_NOMINAL
Definition: ptu_hw_config.h:72
uint16_t vrect_min_dyn
Definition: wpt.h:172
uint32_t ptu_tmux_poweramp_level_set(uint16_t level)
Test wrapper for ptu_hal_poweramp_level_set().
Definition: ptu_test_mux.c:74
#define PTU_VRECT_HIGH_OFFSET
Definition: ptu_hw_config.h:80
void ptu_power_ctrl_set_disable_power_amplifier_adjustments(bool val)
Enable/Disable adjustments of power amplifier (I_TX_COIL).
uint16_t vrect_high_static
Definition: wpt.h:97
#define BLE_WPTS_PRU_DYNAMIC_OPTION_VRECT_HIGH_DYN_BITPOS
PRU Static Parameter structure.
Definition: wpt.h:89
#define PTU_ALLOWED_REDUCED_POSITIVE_ITX_COIL_ADJUSTMENT_MULTIPLIER
Definition: ptu_config.h:49
pru_static_t prev_pru_static
Definition: ptu_registry.h:53
#define BLE_WPTS_PRU_DYNAMIC_OPTION_VRECT_MIN_DYN_BITMSK
static void m_update_power_amplifier_adjustment_step_size_for_dominant_pru(ptu_reg_item_t const *p_dominant_pru_reg_item, bool *p_reduced_positive_step_size, bool *p_reduced_negative_step_size)
Inspect registry item for dominant PRU and see if reduced step size is allowed for negative and posit...
ptu_reg_item_t * ptu_reg_item_get_from_conn_handle(uint16_t conn_handle)
Get registry item from connection handle.
Definition: ptu_registry.c:137
uint16_t vrect_min_static
Definition: wpt.h:96
uint8_t optional_fields
Definition: wpt.h:166
uint16_t vrect_set
Definition: wpt.h:98
pru_dynamic_t prev_pru_dynamic
Definition: ptu_registry.h:54
#define PTU_ALLOWED_REDUCED_NEGATIVE_ITX_COIL_ADJUSTMENT_MULTIPLIER
Definition: ptu_config.h:52
static void m_update_power_amplifier_adjustment_step_size(ptu_reg_item_t const *p_reg_item, bool *p_reduced_positive_step_size, bool *p_reduced_negative_step_size)
Inspect registry item and see if reduced step size is allowed for increase and/or decrease...
Definition: ptu_power_ctl.c:74
#define PTU_POWER_TRANSFER_HYSTERISIS
Definition: ptu_config.h:43
static uint16_t m_conn_handle_dominant
Definition: ptu_power_ctl.c:40
ptu_tester_command_t
PTU Tester commands.
Definition: wpt.h:155
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
static bool m_itx_adj_disabled
Definition: ptu_power_ctl.c:36
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
#define BLE_WPTS_PRU_DYNAMIC_OPTION_VRECT_HIGH_DYN_BITMSK
#define BLE_WPTS_BITFIELD_READ(bitfield, msk, pos)
Read bitfield.
void ptu_power_ctrl_set_itx_val(ptu_tester_command_t cmd)
Handle PTU tester power adjustment command.
static uint32_t m_poweramp_input_step_up(bool reduced_positive_step_size, bool increased_step_size)
Increase PTU poweramp output.
#define PTU_POWERAMP_INPUT_STEP_SIZE
Definition: ptu_hw_config.h:75
PRU Dynamic Parameter structure.
Definition: wpt.h:164
#define PTU_DOMINANT_PRU_UPPER_LIMIT_VRECTSET_MULTIPLIER_FOR_REDUCTION_IN_STEP_SIZE
Definition: ptu_config.h:55
static void m_pru_scan_all(bool *p_reduced_positive_step_size, bool *p_reduced_negative_step_size, bool *p_increased_step_size)
Scan through dynamic and static parameters received from all connected PRUs.