Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
ptu_power_sharing.c File Reference
#include <stdint.h>
#include <string.h>
#include <math.h>
#include "nrf_assert.h"
#include "ptu_conn_man.h"
#include "ptu_power_sharing.h"

Go to the source code of this file.

Functions

API implementation.
uint32_t ptu_power_sharing_init (ptu_sm_handler_t sm_handler)
 Initialize power sharing module. More...
 
void ptu_power_sharing_temperature_warning_set (bool warning)
 Inform power control module that PTU is having a high temperature warning. When this occurs the power control module will instruct PRUs to lower their power consumption. More...
 
void ptu_power_sharing_on_wpt_service_evt (ble_wpts_c_t *p_wpts_c, ble_wpts_c_evt_t *const p_wpts_c_evt)
 Handle service events. More...
 

Static funcitons and variables.

#define REG_DEVICE_CAN_ADJUST_POWER(DEV)
 Check if a device is capable of receiving power adjust commands. More...
 
static ptu_pctl_status_t m_pctl_status
 
static ptu_sm_handler_t m_sm_handler
 
static void m_ble_wpts_c_send_pru_control_wait_buffer (ptu_reg_item_t *reg_item_p)
 Sends the control packet in prev_ctl to a PRU. More...
 
static uint8_t m_get_prect (uint8_t prect_max, ctl_adj_power_t power_adjust)
 Get truncated value for adjusted power. More...
 
static uint8_t m_prect_max_get (ptu_reg_item_t *reg_item)
 Get the maximum Prect in [100 mW] for a PRU. More...
 
static uint8_t m_prect_diff_after_inc_get (ptu_reg_item_t *reg_item)
 Get the maximum increased Prect if we step up the allowed Prect for the PRU with one step. More...
 
static void m_send_power_adjust_command (ptu_reg_item_t *reg_item)
 Transmits a PRU control packet, and performs all required bookkeeping related to performing the power ajust process. More...
 
static bool m_pru_decrease_power ()
 Search for PRUs having "adjust power" capability and send control packet instructing the PRU to reduce power. More...
 
static bool m_pru_increase_power ()
 Search for PRUs having "adjust power" capability and send control packet allowing the PRU to increase power. More...
 
static bool m_pru_deny_power ()
 Search for PRUs being permitted to enable charge output and send control packet denying the PRU to do so. More...
 
static bool m_max_number_of_devices_is_being_charged (void)
 Check if the maximum number of PRUs is being charged. More...
 
static bool m_pru_allow_power ()
 Search for PRUs not being permitted to enable charge output and send control packet allowing the PRU to do so if the PTU can tolerate this increased power draw. More...
 
static ctl_adj_power_t m_attempt_to_get_prect_within_available_resources (ptu_reg_item_t *new_reg_item_p)
 See if it is possible to adjust power drain from a not yet registerred PRU so that it fits within the current remaining power of the PTU. More...
 
static void m_status_update (void)
 Collect status information from all PRUs and update m_pctl_status.
 
static void m_charge_disable_all ()
 Disable PRU output for all connected PRUs.
 
static void m_charge_complete_update (ptu_reg_item_t *p_reg_item, ble_wpts_c_evt_t *const p_wpts_c_evt)
 Check if all PRUs are done charging, in which case all will be disabled.
 
static bool m_adjusted_power_sensed (ptu_reg_item_t *item)
 See if PRU has adjusted power by looking at the I_RECT and V_RECT values from PRU dynamic params. More...
 
static void m_update_pru_p_adj_status (void)
 Update cooldown values for all PRUs which has received a power adjust command recently. Also checks for PRUs that does not respond with power adjust bit correctly after being sent a power adjust command.
 
static void m_allow_pru_to_charge (ptu_reg_item_t *reg_item_p)
 Update registry item and issue control packet for allowing PRU to charge. More...
 
static void m_deny (ptu_reg_item_t *reg_item_p, ctl_perm_t permissions)
 Update registry item and issue control packet for denying PRU charge. Will remove device from registry. More...
 
static void m_permit_with_waiting_time_and_adjust (ptu_reg_item_t *reg_item_p)
 Allow PRU to charge with adjusted power draw. Update registry item and issue control packet indicating the required adjustment. More...
 
static void m_handle_new_device (ptu_reg_item_t *reg_item_p)
 Handle the power sharing of a new PRU. More...
 

Macro Definition Documentation

#define REG_DEVICE_CAN_ADJUST_POWER (   DEV)
Value:
(DEV->prev_pru_static.info_adj_power_capability && \
!DEV->p_adj_disabled && \
!DEV->active_p_adj_cooldown)

Check if a device is capable of receiving power adjust commands.

Definition at line 29 of file ptu_power_sharing.c.

Function Documentation

static void m_ble_wpts_c_send_pru_control_wait_buffer ( ptu_reg_item_t reg_item_p)
static

Sends the control packet in prev_ctl to a PRU.

Parameters
reg_item_pregistry entry for PRU to which to send control packet.

Definition at line 53 of file ptu_power_sharing.c.

54 {
55  uint32_t err_code;
56 
58  {
59  reg_item_p -> prev_ctl.time_set = BLE_WPTS_TIME_SET_BITVAL;
60  }
61 
62  else
63  {
64  reg_item_p -> prev_ctl.time_set = CTL_TIME_SET_DENIED;
65  }
66 
67  do{
68  reg_item_p->pending_ctl_write = true;
69  err_code = ble_wpts_c_send_pru_control(&reg_item_p->ble_wpts_c, &reg_item_p->prev_ctl);
70  } while(err_code == BLE_ERROR_NO_TX_PACKETS);
71 }
pru_control_t prev_ctl
Definition: ptu_registry.h:55
ble_wpts_c_t ble_wpts_c
Definition: ptu_registry.h:52
uint8_t adv_flags
Definition: ptu_registry.h:50
#define BLE_WPTS_TIME_SET_BITVAL
Definition: ptu_config.h:278
uint8_t pending_ctl_write
Definition: ptu_registry.h:65
#define BLE_WPTS_BITFIELD_READ(bitfield, msk, pos)
Read bitfield.
uint32_t ble_wpts_c_send_pru_control(ble_wpts_c_t *p_wpts_c, pru_control_t *p_wpts_pru_control)
Send a PRU Control message to the PRU server.
Definition: ble_wpts_c.c:366
#define BLE_WPTS_ADV_FLAG_TIME_SET_SUPPORT_BITMSK
#define BLE_WPTS_ADV_FLAG_TIME_SET_SUPPORT_BITPOS
static uint8_t m_get_prect ( uint8_t  prect_max,
ctl_adj_power_t  power_adjust 
)
static

Get truncated value for adjusted power.

Parameters
prect_maxThe PRUs PRECT_MAX value.
power_adjustThe power adjustment to apply.
Return values
Truncatedvalue of the resulting PRECT.

Definition at line 81 of file ptu_power_sharing.c.

82 {
83  uint8_t retval = 0;
84 
85  switch(power_adjust)
86  {
88  retval = prect_max;
89  break;
91  retval = (uint8_t)ceil(prect_max * 0.66);
92  break;
94  retval = (uint8_t)ceil(prect_max * 0.33);
95  break;
97  retval = 25;
98  break;
99  default:
100  ASSERT(false);
101  break;
102  }
103  return retval;
104 }
static uint8_t m_prect_max_get ( ptu_reg_item_t reg_item)
static

Get the maximum Prect in [100 mW] for a PRU.

Parameters
reg_itemregistry item for the PRU.
Return values
Themaximum Prect in [100 mW] for the PRU.

Definition at line 112 of file ptu_power_sharing.c.

113 {
114  ASSERT(reg_item != NULL);
115  uint8_t retval = 0;
116 
118  retval = m_get_prect(reg_item->prev_pru_static.prect_max, reg_item->prev_ctl.adj_power);
119  else
120  retval = reg_item->prev_pru_static.prect_max;
121 
122  return retval;
123 }
pru_control_t prev_ctl
Definition: ptu_registry.h:55
uint8_t info_adj_power_capability
Definition: wpt.h:104
pru_static_t prev_pru_static
Definition: ptu_registry.h:53
static uint8_t m_get_prect(uint8_t prect_max, ctl_adj_power_t power_adjust)
Get truncated value for adjusted power.
ctl_adj_power_t adj_power
Definition: wpt.h:83
uint8_t prect_max
Definition: wpt.h:95
static uint8_t m_prect_diff_after_inc_get ( ptu_reg_item_t reg_item)
static

Get the maximum increased Prect if we step up the allowed Prect for the PRU with one step.

Parameters
reg_itemregistry item for the PRU.
Return values
Increasein Prect in [100 mW].

Definition at line 132 of file ptu_power_sharing.c.

133 {
134  ASSERT(reg_item != NULL);
135  ctl_adj_power_t incremented_power_level = reg_item->prev_ctl.adj_power;
136  switch(reg_item->prev_ctl.adj_power)
137  {
139  return 0;
141  incremented_power_level = CTL_ADJ_POWER_MAX_VAL;
142  break;
144  incremented_power_level = CTL_ADJ_POWER_66_PCT_VAL;
145  break;
147  incremented_power_level = CTL_ADJ_POWER_33_PCT_VAL;
148  break;
149  default:
150  ASSERT(false);
151  }
152 
153  return m_get_prect(reg_item->prev_pru_static.prect_max, incremented_power_level)
154  - m_get_prect(reg_item->prev_pru_static.prect_max, reg_item->prev_ctl.adj_power);
155 }
pru_control_t prev_ctl
Definition: ptu_registry.h:55
pru_static_t prev_pru_static
Definition: ptu_registry.h:53
ctl_adj_power_t
Power adjust settings in control packet.
Definition: wpt.h:42
static uint8_t m_get_prect(uint8_t prect_max, ctl_adj_power_t power_adjust)
Get truncated value for adjusted power.
ctl_adj_power_t adj_power
Definition: wpt.h:83
uint8_t prect_max
Definition: wpt.h:95
static void m_send_power_adjust_command ( ptu_reg_item_t reg_item)
static

Transmits a PRU control packet, and performs all required bookkeeping related to performing the power ajust process.

Parameters
reg_itemregistry item for the PRU.

Definition at line 165 of file ptu_power_sharing.c.

166 {
167  uint32_t err_code;
168 
170  reg_item->active_p_adj_cooldown = true;
171  err_code = app_timer_cnt_get(&(reg_item->p_adj_time));
172  APP_ERROR_CHECK(err_code);
173  reg_item->adjusted_power = false;
174  reg_item->p_rect_before_p_adj = reg_item->prev_pru_dynamic.vrect * reg_item->prev_pru_dynamic.irect;
175 }
uint16_t irect
Definition: wpt.h:168
static void m_ble_wpts_c_send_pru_control_wait_buffer(ptu_reg_item_t *reg_item_p)
Sends the control packet in prev_ctl to a PRU.
uint16_t vrect
Definition: wpt.h:167
int32_t p_rect_before_p_adj
Definition: ptu_registry.h:61
uint32_t p_adj_time
Definition: ptu_registry.h:60
pru_dynamic_t prev_pru_dynamic
Definition: ptu_registry.h:54
uint8_t active_p_adj_cooldown
Definition: ptu_registry.h:62
uint8_t adjusted_power
Definition: ptu_registry.h:63
static bool m_pru_decrease_power ( )
static

Search for PRUs having "adjust power" capability and send control packet instructing the PRU to reduce power.

Return values
trueif a PRU was instructed to reduce power.

Definition at line 182 of file ptu_power_sharing.c.

183 {
184  ptu_reg_item_t* match_reg_item;
185  ptu_reg_item_t* registered_devices[PTU_MAX_CONNECTIONS];
186  uint8_t n_reg_devices = ptu_reg_registered_devices_get(registered_devices);
187 
188  match_reg_item = NULL;
189 
190  for(uint8_t i = 0; i < n_reg_devices; i++)
191  {
192 
193  if(REG_DEVICE_CAN_ADJUST_POWER(registered_devices[i]) && \
194  (registered_devices[i]->prev_ctl.adj_power == CTL_ADJ_POWER_MAX_VAL))
195  {
196  match_reg_item = registered_devices[i];
197  match_reg_item->prev_adj_power = CTL_ADJ_POWER_MAX_VAL;
198  match_reg_item->prev_ctl.adj_power = CTL_ADJ_POWER_66_PCT_VAL;
199  break;
200  }
201  }
202 
203  if(match_reg_item == NULL)
204  {
205  for(uint8_t i = 0; i < n_reg_devices; i++)
206  {
207  if(REG_DEVICE_CAN_ADJUST_POWER(registered_devices[i]) && \
208  (registered_devices[i]->prev_ctl.adj_power == CTL_ADJ_POWER_66_PCT_VAL))
209  {
210  match_reg_item = registered_devices[i];
211  match_reg_item->prev_adj_power = CTL_ADJ_POWER_66_PCT_VAL;
212  match_reg_item->prev_ctl.adj_power = CTL_ADJ_POWER_33_PCT_VAL;
213  break;
214  }
215  }
216  }
217 
218  if(match_reg_item == NULL)
219  {
220  for(uint8_t i = 0; i < n_reg_devices; i++)
221  {
222  if(REG_DEVICE_CAN_ADJUST_POWER(registered_devices[i]) && \
223  (registered_devices[i]->prev_ctl.adj_power == CTL_ADJ_POWER_33_PCT_VAL))
224  {
225  match_reg_item = registered_devices[i];
226  match_reg_item->prev_adj_power = CTL_ADJ_POWER_33_PCT_VAL;
227  match_reg_item->prev_ctl.adj_power = CTL_ADJ_POWER_2_5_W_VAL;
228  break;
229  }
230  }
231  }
232 
233  if(match_reg_item != NULL)
234  {
235  m_send_power_adjust_command(match_reg_item);
236  return true;
237  }
238  else
239  {
240  return false;
241  }
242 }
pru_control_t prev_ctl
Definition: ptu_registry.h:55
Registry item.
Definition: ptu_registry.h:47
uint8_t ptu_reg_registered_devices_get(ptu_reg_item_t **registered_devices)
Get handles for all registered devices.
Definition: ptu_registry.c:254
ctl_adj_power_t prev_adj_power
Definition: ptu_registry.h:57
#define REG_DEVICE_CAN_ADJUST_POWER(DEV)
Check if a device is capable of receiving power adjust commands.
static void m_send_power_adjust_command(ptu_reg_item_t *reg_item)
Transmits a PRU control packet, and performs all required bookkeeping related to performing the power...
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
ctl_adj_power_t adj_power
Definition: wpt.h:83
static bool m_pru_increase_power ( )
static

Search for PRUs having "adjust power" capability and send control packet allowing the PRU to increase power.

Return values
trueif a PRU was allowed to increase power.

Definition at line 249 of file ptu_power_sharing.c.

250 {
251  ptu_reg_item_t* match_reg_item;
252  ptu_reg_item_t* registered_devices[PTU_MAX_CONNECTIONS];
253  uint8_t n_reg_devices = ptu_reg_registered_devices_get(registered_devices);
254 
255  match_reg_item = NULL;
256 
257  for(uint8_t i = 0; i < n_reg_devices; i++)
258  {
259  if(REG_DEVICE_CAN_ADJUST_POWER(registered_devices[i]) && \
260  (registered_devices[i]->prev_ctl.adj_power == CTL_ADJ_POWER_2_5_W_VAL) && \
261  (m_prect_diff_after_inc_get(registered_devices[i]) <= m_pctl_status.available_power))
262  {
263  match_reg_item = registered_devices[i];
264  match_reg_item->prev_adj_power = CTL_ADJ_POWER_2_5_W_VAL;
265  match_reg_item->prev_ctl.adj_power = CTL_ADJ_POWER_33_PCT_VAL;
266  break;
267  }
268  }
269 
270  if(match_reg_item == NULL)
271  {
272  for(uint8_t i = 0; i < n_reg_devices; i++)
273  {
274  if(REG_DEVICE_CAN_ADJUST_POWER(registered_devices[i]) && \
275  (registered_devices[i]->prev_ctl.adj_power == CTL_ADJ_POWER_33_PCT_VAL) && \
276  (m_prect_diff_after_inc_get(registered_devices[i]) <= m_pctl_status.available_power))
277  {
278  match_reg_item = registered_devices[i];
279  match_reg_item->prev_adj_power = CTL_ADJ_POWER_33_PCT_VAL;
280  match_reg_item->prev_ctl.adj_power = CTL_ADJ_POWER_66_PCT_VAL;
281  break;
282  }
283  }
284  }
285 
286  if(match_reg_item == NULL)
287  {
288  for(uint8_t i = 0; i < n_reg_devices; i++)
289  {
290  if(REG_DEVICE_CAN_ADJUST_POWER(registered_devices[i]) && \
291  (registered_devices[i]->prev_ctl.adj_power == CTL_ADJ_POWER_66_PCT_VAL) &&\
292  (m_prect_diff_after_inc_get(registered_devices[i]) <= m_pctl_status.available_power))
293  {
294  match_reg_item = registered_devices[i];
295  match_reg_item->prev_adj_power = CTL_ADJ_POWER_66_PCT_VAL;
296  match_reg_item->prev_ctl.adj_power = CTL_ADJ_POWER_MAX_VAL;
297  break;
298  }
299  }
300  }
301 
302  if(match_reg_item != NULL)
303  {
304  m_send_power_adjust_command(match_reg_item);
305  return true;
306  }
307  else
308  {
309  return false;
310  }
311 }
pru_control_t prev_ctl
Definition: ptu_registry.h:55
Registry item.
Definition: ptu_registry.h:47
uint8_t ptu_reg_registered_devices_get(ptu_reg_item_t **registered_devices)
Get handles for all registered devices.
Definition: ptu_registry.c:254
ctl_adj_power_t prev_adj_power
Definition: ptu_registry.h:57
#define REG_DEVICE_CAN_ADJUST_POWER(DEV)
Check if a device is capable of receiving power adjust commands.
static void m_send_power_adjust_command(ptu_reg_item_t *reg_item)
Transmits a PRU control packet, and performs all required bookkeeping related to performing the power...
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
static uint8_t m_prect_diff_after_inc_get(ptu_reg_item_t *reg_item)
Get the maximum increased Prect if we step up the allowed Prect for the PRU with one step...
ctl_adj_power_t adj_power
Definition: wpt.h:83
static bool m_pru_deny_power ( )
static

Search for PRUs being permitted to enable charge output and send control packet denying the PRU to do so.

Return values
trueif a PRU was instructed disable charge output.

Definition at line 318 of file ptu_power_sharing.c.

319 {
320  ptu_reg_item_t * match_reg_item;
321  ptu_reg_item_t* registered_devices[PTU_MAX_CONNECTIONS];
322  uint8_t n_reg_devices = ptu_reg_registered_devices_get(registered_devices);
323 
324  for(uint8_t i = 0; i < n_reg_devices; i++)
325  {
326  if(registered_devices[i]->prev_ctl.permissions == CTL_PERMISSION_PERMITTED)
327  {
328  match_reg_item = registered_devices[i];
329  match_reg_item->prev_ctl.enable_pru_output = 0;
330  match_reg_item->prev_ctl.enable_pru_charge_indicator = 0;
331  match_reg_item->prev_ctl.permissions = m_pctl_status.temperature_warning ? CTL_PERMISSION_DENIED_HIGH_TEMP : CTL_PERMISSION_DENIED_POWER;
333  return true;
334  }
335  }
336 
337  return false;
338 }
pru_control_t prev_ctl
Definition: ptu_registry.h:55
Registry item.
Definition: ptu_registry.h:47
uint8_t ptu_reg_registered_devices_get(ptu_reg_item_t **registered_devices)
Get handles for all registered devices.
Definition: ptu_registry.c:254
static void m_ble_wpts_c_send_pru_control_wait_buffer(ptu_reg_item_t *reg_item_p)
Sends the control packet in prev_ctl to a PRU.
uint8_t enable_pru_output
Definition: wpt.h:81
ctl_perm_t permissions
Definition: wpt.h:84
uint8_t enable_pru_charge_indicator
Definition: wpt.h:82
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
static bool m_max_number_of_devices_is_being_charged ( void  )
static

Check if the maximum number of PRUs is being charged.

Return values
trueif a PRU was instructed disable charge output.

Definition at line 344 of file ptu_power_sharing.c.

345 {
346  ptu_reg_item_t* registered_devices[PTU_MAX_CONNECTIONS];
347  uint8_t nof_devices = ptu_reg_registered_devices_get(registered_devices);
348  uint8_t nof_devices_being_charged = 0;
349 
350  for(uint8_t i = 0; i < nof_devices; ++i)
351  {
352  if(registered_devices[i]->prev_ctl.enable_pru_output == 1 || registered_devices[i]->pending_ctl_write)
353  nof_devices_being_charged++;
354  }
355 
356  ASSERT(nof_devices_being_charged <= PTU_MAX_DEVICES);
357 
358  return nof_devices_being_charged == PTU_MAX_DEVICES;
359 }
#define PTU_MAX_DEVICES
Definition: ptu_config.h:30
Registry item.
Definition: ptu_registry.h:47
uint8_t ptu_reg_registered_devices_get(ptu_reg_item_t **registered_devices)
Get handles for all registered devices.
Definition: ptu_registry.c:254
uint8_t pending_ctl_write
Definition: ptu_registry.h:65
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
static bool m_pru_allow_power ( )
static

Search for PRUs not being permitted to enable charge output and send control packet allowing the PRU to do so if the PTU can tolerate this increased power draw.

Return values
trueif a PRU was allowed to enable charge output.

Definition at line 367 of file ptu_power_sharing.c.

368 {
369  ptu_reg_item_t* registered_devices[PTU_MAX_CONNECTIONS];
370  ptu_reg_item_t * match_reg_item;
371  uint8_t n_reg_devices = ptu_reg_registered_devices_get(registered_devices);
372 
373  for(uint8_t i = 0; i < n_reg_devices; i++)
374  {
375  if(registered_devices[i]->prev_ctl.permissions != CTL_PERMISSION_PERMITTED && \
376  m_prect_max_get(registered_devices[i]) <= m_pctl_status.available_power && \
377  registered_devices[i]->adjusted_power)
378  {
379  match_reg_item = registered_devices[i];
380  match_reg_item->prev_ctl.enable_pru_output = 1;
381  match_reg_item->prev_ctl.enable_pru_charge_indicator = 1;
384  return true;
385  }
386  }
387 
388  return false;
389 }
pru_control_t prev_ctl
Definition: ptu_registry.h:55
Registry item.
Definition: ptu_registry.h:47
uint8_t ptu_reg_registered_devices_get(ptu_reg_item_t **registered_devices)
Get handles for all registered devices.
Definition: ptu_registry.c:254
static void m_ble_wpts_c_send_pru_control_wait_buffer(ptu_reg_item_t *reg_item_p)
Sends the control packet in prev_ctl to a PRU.
uint8_t enable_pru_output
Definition: wpt.h:81
ctl_perm_t permissions
Definition: wpt.h:84
uint8_t enable_pru_charge_indicator
Definition: wpt.h:82
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
static uint8_t m_prect_max_get(ptu_reg_item_t *reg_item)
Get the maximum Prect in [100 mW] for a PRU.
uint8_t adjusted_power
Definition: ptu_registry.h:63
static ctl_adj_power_t m_attempt_to_get_prect_within_available_resources ( ptu_reg_item_t new_reg_item_p)
static

See if it is possible to adjust power drain from a not yet registerred PRU so that it fits within the current remaining power of the PTU.

Parameters
new_reg_item_pregistry item for the PRU.
Return values
Thepower adjustment required for PRU to be within power limit. CTL_ADJ_POWER_MAX_VAL if it is not possible to get PRU within limit.

Definition at line 402 of file ptu_power_sharing.c.

403 {
404  // If we can not adjust power on new device (of any reason), return MAX_VAL, and regular Power Sharing between
405  // already connected PRUs will take place.
406  ctl_adj_power_t power_adjust = CTL_ADJ_POWER_MAX_VAL;
407  uint8_t current_adjust_val = 0;
408  uint8_t prect_33_percent = (uint8_t)(new_reg_item_p->prev_pru_static.prect_max * 0.33);
409  uint8_t prect_66_percent = prect_33_percent * 2;
410  // See wether PTU has enough power for 66% or 33% of P_RECT_MAX
411  if(prect_66_percent <= m_pctl_status.available_power)
412  {
413  current_adjust_val = prect_66_percent;
414  power_adjust = CTL_ADJ_POWER_66_PCT_VAL;
415  }
416 
417  else if(prect_33_percent <= m_pctl_status.available_power)
418  {
419  current_adjust_val = prect_33_percent;
420  power_adjust = CTL_ADJ_POWER_33_PCT_VAL;
421  }
422 
423  // What is biggest, our fraction of the P_RECT_MAX, or 2.5W ?
424  // In any case, make sure PTU has enough to power left
425  if(current_adjust_val < 25 && m_pctl_status.available_power >= 25)
426  power_adjust = CTL_ADJ_POWER_2_5_W_VAL;
427 
428  return power_adjust;
429 }
pru_static_t prev_pru_static
Definition: ptu_registry.h:53
ctl_adj_power_t
Power adjust settings in control packet.
Definition: wpt.h:42
uint8_t prect_max
Definition: wpt.h:95
static bool m_adjusted_power_sensed ( ptu_reg_item_t item)
static

See if PRU has adjusted power by looking at the I_RECT and V_RECT values from PRU dynamic params.

Parameters
itemThe PRU item to check.
Return values
HasPRU adjusted power.

Definition at line 518 of file ptu_power_sharing.c.

519 {
520  int32_t diff, required_diff;
521 
522  required_diff = ((int32_t)m_get_prect(item->prev_pru_static.prect_max, item->prev_adj_power)) \
523  - ((int32_t)m_get_prect(item->prev_pru_static.prect_max, item->prev_ctl.adj_power));
524  diff = (int32_t)(item->prev_pru_dynamic.vout * item->prev_pru_dynamic.iout) - item->p_rect_before_p_adj;
525 
528  diff == required_diff;
529 }
pru_control_t prev_ctl
Definition: ptu_registry.h:55
uint16_t vout
Definition: wpt.h:169
#define BLE_WPTS_PRU_DYNAMIC_OPTION_IOUT_BITPOS
uint16_t iout
Definition: wpt.h:170
ctl_adj_power_t prev_adj_power
Definition: ptu_registry.h:57
#define BLE_WPTS_PRU_DYNAMIC_OPTION_VOUT_BITPOS
PRU Dynamic Parameter characteristic Option field.
int32_t p_rect_before_p_adj
Definition: ptu_registry.h:61
pru_static_t prev_pru_static
Definition: ptu_registry.h:53
uint8_t optional_fields
Definition: wpt.h:166
pru_dynamic_t prev_pru_dynamic
Definition: ptu_registry.h:54
static uint8_t m_get_prect(uint8_t prect_max, ctl_adj_power_t power_adjust)
Get truncated value for adjusted power.
ctl_adj_power_t adj_power
Definition: wpt.h:83
uint8_t prect_max
Definition: wpt.h:95
static void m_allow_pru_to_charge ( ptu_reg_item_t reg_item_p)
static

Update registry item and issue control packet for allowing PRU to charge.

Parameters
[in]reg_item_pPointer to registry item that should be allowed to charge.

Definition at line 574 of file ptu_power_sharing.c.

575 {
576  reg_item_p -> prev_ctl.enable_pru_output = 1;
577  reg_item_p -> prev_ctl.enable_pru_charge_indicator = 1;
578  reg_item_p -> prev_ctl.adj_power = CTL_ADJ_POWER_MAX_VAL;
579  reg_item_p -> prev_ctl.permissions = CTL_PERMISSION_PERMITTED;
581 }
static void m_ble_wpts_c_send_pru_control_wait_buffer(ptu_reg_item_t *reg_item_p)
Sends the control packet in prev_ctl to a PRU.
static void m_deny ( ptu_reg_item_t reg_item_p,
ctl_perm_t  permissions 
)
static

Update registry item and issue control packet for denying PRU charge. Will remove device from registry.

Parameters
[in]reg_item_pPointer to registry item that should be denied charge.
permissionsReason for being denied.

Definition at line 589 of file ptu_power_sharing.c.

590 {
591  reg_item_p -> prev_ctl.enable_pru_output = 0;
592 
593  // It is only in the case where PRU is too large Category that PRU charge could NEVER happen
594  if(permissions == BLE_WPTS_PERMISSION_DENIED_CLASS)
595  reg_item_p -> prev_ctl.enable_pru_charge_indicator = 0;
596  else
597  reg_item_p -> prev_ctl.enable_pru_charge_indicator = 1;
598 
599  reg_item_p -> prev_ctl.permissions = permissions;
601  ptu_cm_remove_device(reg_item_p);
602 }
static void m_ble_wpts_c_send_pru_control_wait_buffer(ptu_reg_item_t *reg_item_p)
Sends the control packet in prev_ctl to a PRU.
void ptu_cm_remove_device(ptu_reg_item_t *reg_item_p)
Disconnect and/or unregister any device.
Definition: ptu_conn_man.c:836
#define BLE_WPTS_PERMISSION_DENIED_CLASS
static void m_permit_with_waiting_time_and_adjust ( ptu_reg_item_t reg_item_p)
static

Allow PRU to charge with adjusted power draw. Update registry item and issue control packet indicating the required adjustment.

Parameters
[in]reg_item_pPointer to registry item that should be allowed adjusted charge.

Definition at line 609 of file ptu_power_sharing.c.

610 {
611  reg_item_p -> prev_ctl.enable_pru_output = 0;
612  reg_item_p -> prev_ctl.enable_pru_charge_indicator = 1;
613  reg_item_p -> prev_ctl.permissions = CTL_PERMISSION_PERMITTED_WAITING;
614  reg_item_p -> prev_ctl.adj_power = m_attempt_to_get_prect_within_available_resources(reg_item_p);
615  m_send_power_adjust_command(reg_item_p);
616 }
static ctl_adj_power_t m_attempt_to_get_prect_within_available_resources(ptu_reg_item_t *new_reg_item_p)
See if it is possible to adjust power drain from a not yet registerred PRU so that it fits within the...
static void m_send_power_adjust_command(ptu_reg_item_t *reg_item)
Transmits a PRU control packet, and performs all required bookkeeping related to performing the power...
static void m_handle_new_device ( ptu_reg_item_t reg_item_p)
static

Handle the power sharing of a new PRU.

Parameters
[in]reg_item_pPointer to registry item for new PRU.

Definition at line 621 of file ptu_power_sharing.c.

622 {
623  if(reg_item_p -> prev_pru_static.pru_category > (PTU_CLASS + 2)) // PTU Class 2 = 1, Class 2 supports Category 3 PRU, hence " + 2 "
624  m_deny(reg_item_p, CTL_PERMISSION_DENIED_CLASS);
625 
628 
629  else if(m_pctl_status.available_power < reg_item_p->prev_pru_static.prect_max || m_pctl_status.pending_power_adjust)
630  {
631  // If new device supports power adjustment, make an attempt to adjust its power.
632  if(reg_item_p->prev_pru_static.info_adj_power_capability == 1)
634 
635  // Otherwise, adjust PTU can never fulfill the PRUs power demand, and it should be rejected.
636  else
637  m_deny(reg_item_p, CTL_PERMISSION_DENIED_POWER);
638  }
639 
640  else
641  m_allow_pru_to_charge(reg_item_p);
642 }
uint8_t info_adj_power_capability
Definition: wpt.h:104
static bool m_max_number_of_devices_is_being_charged(void)
Check if the maximum number of PRUs is being charged.
#define PTU_CLASS
Definition: ptu_hw_config.h:30
static void m_deny(ptu_reg_item_t *reg_item_p, ctl_perm_t permissions)
Update registry item and issue control packet for denying PRU charge. Will remove device from registr...
pru_static_t prev_pru_static
Definition: ptu_registry.h:53
static void m_permit_with_waiting_time_and_adjust(ptu_reg_item_t *reg_item_p)
Allow PRU to charge with adjusted power draw. Update registry item and issue control packet indicatin...
static void m_allow_pru_to_charge(ptu_reg_item_t *reg_item_p)
Update registry item and issue control packet for allowing PRU to charge.
uint8_t prect_max
Definition: wpt.h:95