samples: mesh_demo: rework error and device handling
The board specific interface in this sample is not handling errors. Fix that by making board_init() return a negative error code or zero. Rework the bbc_microbit specific board file by handling errors appropriately and avoiding device_get_binding() to move some device configuration errors to build time. Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
parent
d36c6a996e
commit
0d9da124cd
3 changed files with 52 additions and 24 deletions
|
@ -15,15 +15,16 @@ uint16_t board_set_target(void);
|
|||
void board_play(const char *str);
|
||||
|
||||
#if defined(CONFIG_BOARD_BBC_MICROBIT)
|
||||
void board_init(uint16_t *addr);
|
||||
int board_init(uint16_t *addr);
|
||||
void board_play_tune(const char *str);
|
||||
void board_heartbeat(uint8_t hops, uint16_t feat);
|
||||
void board_other_dev_pressed(uint16_t addr);
|
||||
void board_attention(bool attention);
|
||||
#else
|
||||
static inline void board_init(uint16_t *addr)
|
||||
static inline int board_init(uint16_t *addr)
|
||||
{
|
||||
*addr = NODE_ADDR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline void board_play_tune(const char *str)
|
||||
|
|
|
@ -268,7 +268,11 @@ void main(void)
|
|||
|
||||
printk("Initializing...\n");
|
||||
|
||||
board_init(&addr);
|
||||
err = board_init(&addr);
|
||||
if (err) {
|
||||
printk("Board initialization failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printk("Unicast address: 0x%04x\n", addr);
|
||||
|
||||
|
@ -276,6 +280,7 @@ void main(void)
|
|||
err = bt_enable(bt_ready);
|
||||
if (err) {
|
||||
printk("Bluetooth init failed (err %d)\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
|
|
|
@ -29,9 +29,13 @@
|
|||
#define SEQ_PAGE (NRF_FICR->CODEPAGESIZE * (NRF_FICR->CODESIZE - 1))
|
||||
#define SEQ_MAX (NRF_FICR->CODEPAGESIZE * 8 * SEQ_PER_BIT)
|
||||
|
||||
static const struct device *gpio;
|
||||
static const struct device *nvm;
|
||||
static const struct device *pwm;
|
||||
static const struct gpio_dt_spec button_a =
|
||||
GPIO_DT_SPEC_GET(DT_NODELABEL(buttona), gpios);
|
||||
static const struct gpio_dt_spec button_b =
|
||||
GPIO_DT_SPEC_GET(DT_NODELABEL(buttonb), gpios);
|
||||
static const struct device *nvm =
|
||||
DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller));
|
||||
static const struct device *pwm = DEVICE_DT_GET_ANY(nordic_nrf_sw_pwm);
|
||||
|
||||
static struct k_work button_work;
|
||||
|
||||
|
@ -46,7 +50,7 @@ static void button_pressed(const struct device *dev, struct gpio_callback *cb,
|
|||
{
|
||||
struct mb_display *disp = mb_display_get();
|
||||
|
||||
if (pins & BIT(DT_GPIO_PIN(DT_ALIAS(sw0), gpios))) {
|
||||
if (pins & BIT(button_a.pin)) {
|
||||
k_work_submit(&button_work);
|
||||
} else {
|
||||
uint16_t target = board_set_target();
|
||||
|
@ -222,36 +226,54 @@ void board_attention(bool attention)
|
|||
}
|
||||
}
|
||||
|
||||
static void configure_button(void)
|
||||
static int configure_button(const struct gpio_dt_spec *button)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = gpio_pin_configure_dt(button, GPIO_INPUT);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
return gpio_pin_interrupt_configure_dt(button, GPIO_INT_EDGE_TO_ACTIVE);
|
||||
}
|
||||
|
||||
static int configure_buttons(void)
|
||||
{
|
||||
static struct gpio_callback button_cb;
|
||||
int err;
|
||||
|
||||
k_work_init(&button_work, button_send_pressed);
|
||||
|
||||
gpio = device_get_binding(DT_GPIO_LABEL(DT_ALIAS(sw0), gpios));
|
||||
err = configure_button(&button_a);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
gpio_pin_configure(gpio, DT_GPIO_PIN(DT_ALIAS(sw0), gpios),
|
||||
GPIO_INPUT | DT_GPIO_FLAGS(DT_ALIAS(sw0), gpios));
|
||||
gpio_pin_interrupt_configure(gpio, DT_GPIO_PIN(DT_ALIAS(sw0), gpios),
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
err = configure_button(&button_b);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
gpio_pin_configure(gpio, DT_GPIO_PIN(DT_ALIAS(sw1), gpios),
|
||||
GPIO_INPUT | DT_GPIO_FLAGS(DT_ALIAS(sw1), gpios));
|
||||
gpio_pin_interrupt_configure(gpio, DT_GPIO_PIN(DT_ALIAS(sw1), gpios),
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
if (button_a.port != button_b.port) {
|
||||
/* These should be the same device on this board. */
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
gpio_init_callback(&button_cb, button_pressed,
|
||||
BIT(DT_GPIO_PIN(DT_ALIAS(sw0), gpios)) |
|
||||
BIT(DT_GPIO_PIN(DT_ALIAS(sw1), gpios)));
|
||||
gpio_add_callback(gpio, &button_cb);
|
||||
BIT(button_a.pin) | BIT(button_b.pin));
|
||||
return gpio_add_callback(button_a.port, &button_cb);
|
||||
}
|
||||
|
||||
void board_init(uint16_t *addr)
|
||||
int board_init(uint16_t *addr)
|
||||
{
|
||||
struct mb_display *disp = mb_display_get();
|
||||
|
||||
nvm = device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL);
|
||||
pwm = device_get_binding(DT_LABEL(DT_INST(0, nordic_nrf_sw_pwm)));
|
||||
if (!(device_is_ready(nvm) && device_is_ready(pwm) &&
|
||||
device_is_ready(button_a.port) &&
|
||||
device_is_ready(button_b.port))) {
|
||||
printk("One or more devices are not ready\n");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
*addr = NRF_UICR->CUSTOMER[0];
|
||||
if (!*addr || *addr == 0xffff) {
|
||||
|
@ -265,5 +287,5 @@ void board_init(uint16_t *addr)
|
|||
mb_display_print(disp, MB_DISPLAY_MODE_DEFAULT, SCROLL_SPEED,
|
||||
"0x%04x", *addr);
|
||||
|
||||
configure_button();
|
||||
return configure_buttons();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue