2024-04-19 18:59:54 +00:00
|
|
|
#ifndef BMC_HANDLER_H
|
|
|
|
#define BMC_HANDLER_H
|
2024-02-14 05:05:01 +00:00
|
|
|
|
2024-02-16 22:45:10 +00:00
|
|
|
#define PW_SWITCH_PIN 0
|
|
|
|
#define PW_SWITCH_DELAY_MS 100
|
2024-02-18 19:55:43 +00:00
|
|
|
#define PW_SWITCH_INV 0
|
2024-02-16 22:45:10 +00:00
|
|
|
#define PW_STATE_PIN 1
|
|
|
|
#define PW_STATE_UPDATE_REPEAT_DELAY_MS 100
|
2024-02-18 19:55:43 +00:00
|
|
|
#define PW_STATE_INV 1
|
2024-02-14 05:05:01 +00:00
|
|
|
|
2024-02-29 06:56:22 +00:00
|
|
|
typedef struct CURRENT_STATE_T_ {
|
|
|
|
float voltage;
|
|
|
|
float tempC;
|
|
|
|
bool power_state;
|
|
|
|
} CURRENT_STATE_T;
|
|
|
|
|
|
|
|
struct repeating_timer * state_update_timer = NULL;
|
|
|
|
|
|
|
|
CURRENT_STATE_T current_state;
|
2024-02-14 05:05:01 +00:00
|
|
|
|
2024-02-16 22:45:10 +00:00
|
|
|
// handler fn to set the power switch pin to an active state
|
2024-02-14 05:05:01 +00:00
|
|
|
int64_t pw_sw_on_async (alarm_id_t id, void * user_data) {
|
2024-02-16 22:45:10 +00:00
|
|
|
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
|
2024-02-18 19:55:43 +00:00
|
|
|
gpio_put(PW_SWITCH_PIN, 1 ^ PW_SWITCH_INV);
|
2024-02-14 05:05:01 +00:00
|
|
|
return 0; // do not reschedule alarm
|
|
|
|
}
|
|
|
|
|
2024-02-16 22:45:10 +00:00
|
|
|
// handler fn to set the power switch pin to an inactive state
|
2024-02-14 05:05:01 +00:00
|
|
|
int64_t pw_sw_off_async (alarm_id_t id, void * user_data) {
|
2024-02-16 22:45:10 +00:00
|
|
|
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
|
2024-02-18 19:55:43 +00:00
|
|
|
gpio_put(PW_SWITCH_PIN, 0 ^ PW_SWITCH_INV);
|
2024-02-14 05:05:01 +00:00
|
|
|
return 0; // do not reschedule alarm
|
|
|
|
}
|
|
|
|
|
2024-02-16 22:45:10 +00:00
|
|
|
// hander fn to read from the power state
|
2024-02-14 05:05:01 +00:00
|
|
|
bool update_current_state_async (repeating_timer_t * rt) {
|
2024-02-29 06:56:22 +00:00
|
|
|
current_state.voltage = adc_read() * 3.3f / (1 << 12);
|
2024-03-05 05:07:18 +00:00
|
|
|
current_state.tempC = 27.0f - (current_state.voltage - 0.706f) / 0.001721f;
|
2024-02-29 06:56:22 +00:00
|
|
|
current_state.power_state = gpio_get(PW_STATE_PIN) ^ PW_STATE_INV;
|
2024-02-14 05:05:01 +00:00
|
|
|
return true; // continue repeating alarm
|
|
|
|
}
|
|
|
|
|
2024-02-16 22:45:10 +00:00
|
|
|
// handler fn called to attempt to set the power state to the requested state
|
2024-02-29 06:56:22 +00:00
|
|
|
void bmc_power_handler (bool requested_power_state) {
|
|
|
|
if (requested_power_state != current_state.power_state) {
|
2024-02-14 05:05:01 +00:00
|
|
|
add_alarm_in_ms(0, pw_sw_on_async, NULL, true);
|
2024-02-16 22:45:10 +00:00
|
|
|
add_alarm_in_ms(PW_SWITCH_DELAY_MS, pw_sw_off_async, NULL, true);
|
2024-02-14 05:05:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-14 21:33:20 +00:00
|
|
|
void bmc_handler_init () {
|
2024-02-16 22:45:10 +00:00
|
|
|
// init power switch pin as output
|
|
|
|
gpio_init(PW_SWITCH_PIN);
|
|
|
|
gpio_set_dir(PW_SWITCH_DELAY_MS, GPIO_OUT);
|
|
|
|
|
|
|
|
// init power state pin as input
|
|
|
|
gpio_init(PW_STATE_PIN);
|
|
|
|
gpio_set_dir(PW_STATE_PIN, GPIO_IN);
|
|
|
|
|
2024-02-29 06:56:22 +00:00
|
|
|
// init adc input
|
|
|
|
adc_init();
|
|
|
|
adc_set_temp_sensor_enabled(true);
|
|
|
|
adc_select_input(4);
|
|
|
|
|
2024-02-16 22:45:10 +00:00
|
|
|
// set repeating timer for power state update
|
2024-02-14 21:33:20 +00:00
|
|
|
state_update_timer = malloc(sizeof(struct repeating_timer));
|
2024-02-16 22:45:10 +00:00
|
|
|
add_repeating_timer_ms(PW_STATE_UPDATE_REPEAT_DELAY_MS, update_current_state_async, NULL, state_update_timer);
|
2024-02-29 06:56:22 +00:00
|
|
|
|
|
|
|
DEBUG_printf("[BMC ] [OK ] BMC initialized successfully\n");
|
2024-02-14 21:33:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bmc_handler_deinit () {
|
|
|
|
cancel_repeating_timer(state_update_timer);
|
|
|
|
free(state_update_timer);
|
|
|
|
state_update_timer = NULL;
|
2024-02-14 05:05:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|