19 #include "nrf_assert.h"
29 #define REG_DEVICE_CAN_ADJUST_POWER(DEV) \
30 (DEV->prev_pru_static.info_adj_power_capability && \
31 !DEV->p_adj_disabled && \
32 !DEV->active_p_adj_cooldown)
39 uint32_t granted_prect_sum;
40 int32_t available_power;
41 bool pending_power_adjust;
43 bool temperature_warning;
46 static ptu_pctl_status_t m_pctl_status;
70 }
while(err_code == BLE_ERROR_NO_TX_PACKETS);
91 retval = (uint8_t)ceil(prect_max * 0.66);
94 retval = (uint8_t)ceil(prect_max * 0.33);
114 ASSERT(reg_item != NULL);
134 ASSERT(reg_item != NULL);
171 err_code = app_timer_cnt_get(&(reg_item->
p_adj_time));
172 APP_ERROR_CHECK(err_code);
188 match_reg_item = NULL;
190 for(uint8_t i = 0; i < n_reg_devices; i++)
196 match_reg_item = registered_devices[i];
203 if(match_reg_item == NULL)
205 for(uint8_t i = 0; i < n_reg_devices; i++)
210 match_reg_item = registered_devices[i];
218 if(match_reg_item == NULL)
220 for(uint8_t i = 0; i < n_reg_devices; i++)
225 match_reg_item = registered_devices[i];
233 if(match_reg_item != NULL)
255 match_reg_item = NULL;
257 for(uint8_t i = 0; i < n_reg_devices; i++)
263 match_reg_item = registered_devices[i];
270 if(match_reg_item == NULL)
272 for(uint8_t i = 0; i < n_reg_devices; i++)
278 match_reg_item = registered_devices[i];
286 if(match_reg_item == NULL)
288 for(uint8_t i = 0; i < n_reg_devices; i++)
294 match_reg_item = registered_devices[i];
302 if(match_reg_item != NULL)
324 for(uint8_t i = 0; i < n_reg_devices; i++)
328 match_reg_item = registered_devices[i];
348 uint8_t nof_devices_being_charged = 0;
350 for(uint8_t i = 0; i < nof_devices; ++i)
352 if(registered_devices[i]->prev_ctl.enable_pru_output == 1 || registered_devices[i]->
pending_ctl_write)
353 nof_devices_being_charged++;
373 for(uint8_t i = 0; i < n_reg_devices; i++)
376 m_prect_max_get(registered_devices[i]) <= m_pctl_status.available_power && \
379 match_reg_item = registered_devices[i];
407 uint8_t current_adjust_val = 0;
409 uint8_t prect_66_percent = prect_33_percent * 2;
411 if(prect_66_percent <= m_pctl_status.available_power)
413 current_adjust_val = prect_66_percent;
417 else if(prect_33_percent <= m_pctl_status.available_power)
419 current_adjust_val = prect_33_percent;
425 if(current_adjust_val < 25 && m_pctl_status.available_power >= 25)
438 m_pctl_status.granted_prect_sum = 0;
439 m_pctl_status.pending_power_adjust =
false;
440 m_pctl_status.prus_waiting =
false;
442 for(uint8_t i = 0; i < n_reg_devices; i++)
444 if(registered_devices[i]->prev_pru_static.info_adj_power_capability &&
449 m_pctl_status.pending_power_adjust =
true;
453 if(registered_devices[i]->pending_ctl_write)
455 m_pctl_status.pending_power_adjust =
true;
461 m_pctl_status.prus_waiting =
true;
466 m_pctl_status.granted_prect_sum +=
m_prect_max_get(registered_devices[i]);
474 if(m_pctl_status.temperature_warning)
477 m_pctl_status.available_power /= 100;
480 m_pctl_status.available_power -= m_pctl_status.granted_prect_sum;
491 for(uint8_t i = 0; i < n_reg_devices ; i++)
520 int32_t diff, required_diff;
528 diff == required_diff;
536 uint32_t err_code, diff;
539 uint32_t current_time;
541 err_code = app_timer_cnt_get(¤t_time);
542 APP_ERROR_CHECK(err_code);
544 for(uint8_t i = 0; i < n_reg_devices; ++i)
546 if(registered_devices[i] != NULL)
548 diff =
ticks_diff(current_time, registered_devices[i]->p_adj_time);
551 if(registered_devices[i]->active_p_adj_cooldown && \
576 reg_item_p -> prev_ctl.enable_pru_output = 1;
577 reg_item_p -> prev_ctl.enable_pru_charge_indicator = 1;
591 reg_item_p -> prev_ctl.enable_pru_output = 0;
595 reg_item_p -> prev_ctl.enable_pru_charge_indicator = 0;
597 reg_item_p -> prev_ctl.enable_pru_charge_indicator = 1;
599 reg_item_p -> prev_ctl.permissions = permissions;
611 reg_item_p -> prev_ctl.enable_pru_output = 0;
612 reg_item_p -> prev_ctl.enable_pru_charge_indicator = 1;
623 if(reg_item_p -> prev_pru_static.pru_category > (
PTU_CLASS + 2))
629 else if(m_pctl_status.available_power < reg_item_p->
prev_pru_static.
prect_max || m_pctl_status.pending_power_adjust)
652 if(sm_handler == NULL)
653 return NRF_ERROR_INVALID_PARAM;
656 memset(&m_pctl_status, 0,
sizeof(ptu_pctl_status_t));
663 m_pctl_status.temperature_warning = warning;
672 if (new_reg_item_p != NULL)
686 switch(p_wpts_c_evt->
type)
698 if(!m_pctl_status.pending_power_adjust)
701 if(m_pctl_status.available_power < 0)
711 else if(m_pctl_status.prus_waiting)
uint8_t info_adj_power_capability
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...
#define BLE_WPTS_PERMISSION_PERMITTED
PRU Control characteristic Permission field values.
union ble_wpts_c_evt_t::@1 data
WPT Service Client structure. This contains various status information for the service.
void(* ptu_sm_handler_t)(ptu_sm_signal_type_t signal)
uint8_t ptu_reg_registered_devices_get(ptu_reg_item_t **registered_devices)
Get handles for all registered devices.
uint8_t adjust_power_response
uint32_t ticks_diff(uint32_t ticks_now, uint32_t ticks_old)
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
#define BLE_WPTS_PRU_DYNAMIC_OPTION_IOUT_BITPOS
ctl_perm_t
Permissions fields in control packet.
static void m_handle_new_device(ptu_reg_item_t *reg_item_p)
Handle the power sharing of a new PRU.
#define PTU_PTX_IN_TEMP_WARNING_DEC
#define PTU_POWER_EFFICIENCY
ctl_adj_power_t prev_adj_power
#define BLE_WPTS_TIME_SET_BITVAL
uint8_t pending_ctl_write
static bool m_max_number_of_devices_is_being_charged(void)
Check if the maximum number of PRUs is being charged.
static void m_charge_disable_all()
Disable PRU output for all connected PRUs.
static bool m_pru_decrease_power()
Search for PRUs having "adjust power" capability and send control packet instructing the PRU to reduc...
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...
#define BLE_WPTS_PRU_DYNAMIC_OPTION_VOUT_BITPOS
PRU Dynamic Parameter characteristic Option field.
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...
int32_t p_rect_before_p_adj
static bool m_pru_deny_power()
Search for PRUs being permitted to enable charge output and send control packet denying the PRU to do...
ble_wpts_c_evt_type_t type
#define PTU_ADJUSTED_POWER_BIT_TIMEOUT_MS
static bool m_pru_increase_power()
Search for PRUs having "adjust power" capability and send control packet allowing the PRU to increase...
static bool m_pru_allow_power()
Search for PRUs not being permitted to enable charge output and send control packet allowing the PRU ...
pru_static_t prev_pru_static
bool ptu_reg_all_charged(void)
Function returning true if all items in registry are charged or there are not items in registry...
WPT Service Client event.
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...
#define REG_DEVICE_CAN_ADJUST_POWER(DEV)
Check if a device is capable of receiving power adjust commands.
#define APP_TIMER_PRESCALER
uint8_t pending_charge_disable
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...
ptu_reg_item_t * ptu_reg_item_get_from_conn_handle(uint16_t conn_handle)
Get registry item from connection handle.
static ptu_sm_handler_t m_sm_handler
State machine event handler.
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.
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...
ctl_adj_power_t
Power adjust settings in control packet.
uint8_t enable_pru_charge_indicator
pru_dynamic_t prev_pru_dynamic
void ptu_cm_remove_device(ptu_reg_item_t *reg_item_p)
Disconnect and/or unregister any device.
uint8_t active_p_adj_cooldown
pru_dynamic_t pru_dynamic
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.
#define BLE_WPTS_PERMISSION_DENIED_CLASS
static uint8_t m_get_prect(uint8_t prect_max, ctl_adj_power_t power_adjust)
Get truncated value for adjusted power.
#define PTU_MAX_CONNECTIONS
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 void m_status_update(void)
Collect status information from all PRUs and update m_pctl_status.
uint32_t ptu_power_sharing_init(ptu_sm_handler_t sm_handler)
Initialize power sharing module.
#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.
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...
ptu_reg_item_state_t state
#define PTU_P_ADJ_CMD_COOLDOWN_MS
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.
ctl_adj_power_t adj_power
static uint8_t m_prect_max_get(ptu_reg_item_t *reg_item)
Get the maximum Prect in [100 mW] for a PRU.
#define BLE_WPTS_ADV_FLAG_TIME_SET_SUPPORT_BITMSK
#define BLE_WPTS_ADV_FLAG_TIME_SET_SUPPORT_BITPOS