diff options
author | Uros Majstorovic <majstor@majstor.org> | 2022-08-09 22:23:08 +0200 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2022-08-09 22:23:08 +0200 |
commit | 3f913efda03fd840cd526ef72e6f397c7da61bd7 (patch) | |
tree | 08f62c93e0e0660fdb7beba32276ff1ceb7a8a3c /fw/fe310/eos/net/pwr.c | |
parent | 810dde21ee65653c15606917b19566cfbaaf165e (diff) |
code layout
Diffstat (limited to 'fw/fe310/eos/net/pwr.c')
-rw-r--r-- | fw/fe310/eos/net/pwr.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/fw/fe310/eos/net/pwr.c b/fw/fe310/eos/net/pwr.c new file mode 100644 index 0000000..734e3cd --- /dev/null +++ b/fw/fe310/eos/net/pwr.c @@ -0,0 +1,69 @@ +#include <stdlib.h> +#include <stdint.h> + +#include "eos.h" +#include "event.h" +#include "dev/net.h" + +#include "soc/pwr.h" +#include "soc/spi.h" +#include "dev/spi.h" +#include "eve/eve.h" + +#include "pwr.h" + +static eos_evt_handler_t evt_handler[EOS_PWR_MAX_MTYPE]; +static unsigned char power_btn_down; + +static void pwr_handle_msg(unsigned char type, unsigned char *buffer, uint16_t len) { + unsigned char mtype; + + if ((buffer == NULL) || (len < 1)) { + eos_net_bad_handler(type, buffer, len); + return; + } + + mtype = buffer[0]; + if ((mtype < EOS_PWR_MAX_MTYPE) && evt_handler[mtype]) { + evt_handler[mtype](mtype, buffer, len); + } else { + eos_net_bad_handler(type, buffer, len); + } +} + +static void pwr_handle_btn(unsigned char type, unsigned char *buffer, uint16_t len) { + int rv; + unsigned char level = buffer[1]; + + eos_net_free(buffer, 0); + if (!level) { + power_btn_down = 1; + return; + } + if (!power_btn_down) return; + + eos_spi_select(EOS_SPI_DEV_EVE); + eve_pwr_sleep(); + eos_spi_deselect(); + + rv = eos_pwr_sleep(); +} + +void eos_pwr_net_init(void) { + int i; + + for (i=0; i<EOS_PWR_MAX_MTYPE; i++) { + evt_handler[i] = NULL; + } + eos_net_set_handler(EOS_NET_MTYPE_POWER, pwr_handle_msg); + eos_pwr_set_handler(EOS_PWR_MTYPE_BUTTON, pwr_handle_btn); +} + +void eos_pwr_set_handler(unsigned char mtype, eos_evt_handler_t handler) { + if (mtype < EOS_PWR_MAX_MTYPE) evt_handler[mtype] = handler; +} + +eos_evt_handler_t eos_pwr_get_handler(unsigned char mtype) { + if (mtype < EOS_PWR_MAX_MTYPE) return evt_handler[mtype]; + return NULL; +} |