Skip to content

Commit

Permalink
Add STM32 support for gpio:set_int/4
Browse files Browse the repository at this point in the history
This contains the STM32 port driver code to support gpio:set_int/4, which allows
sending the `gpio_interrupt` message to the specified pid as the fourth parameter.

Signed-off-by: Winford <[email protected]>
  • Loading branch information
UncleGrumpy committed Dec 4, 2023
1 parent 8a97964 commit e5af122
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions libs/eavmlib/src/gpio.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
stop/0,
close/1
]).

-export([
init/1,
deinit/1,
Expand Down
25 changes: 24 additions & 1 deletion src/platforms/stm32/src/lib/gpio_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,8 @@ static bool gpiodriver_is_gpio_attached(struct GPIOData *gpio_data, term gpio_ba

static term gpiodriver_set_int(Context *ctx, int32_t target_pid, term cmd)
{
int32_t target_local_pid;

term gpio_tuple = term_get_tuple_element(cmd, 1);
if (UNLIKELY(!term_is_tuple(gpio_tuple))) {
AVM_LOGE(TAG, "Invalid GPIO Pin tuple, expect {Bank, Pin}.");
Expand Down Expand Up @@ -788,6 +790,27 @@ static term gpiodriver_set_int(Context *ctx, int32_t target_pid, term cmd)
return create_pair(ctx, ERROR_ATOM, globalcontext_make_atom(ctx->global, invalid_trigger_atom));
}

if (term_get_tuple_arity(cmd) == 4) {
term pid = term_get_tuple_element(cmd, 3);
if (UNLIKELY(!term_is_pid(pid) && !term_is_atom(pid))) {
AVM_LOGE(TAG, "Invalid listener parameter, must be a pid() or registered process!");
return create_pair(ctx, ERROR_ATOM, INVALID_LISTENER_ATOM);
}
if (term_is_pid(pid)) {
target_local_pid = term_to_local_process_id(pid);
} else {
int pid_atom_index = term_to_atom_index(pid);
int32_t registered_process = (int32_t) globalcontext_get_registered_process(ctx->global, pid_atom_index);
if (UNLIKELY(registered_process == 0)) {
AVM_LOGE(TAG, "Invalid listener parameter, atom() is not a registered process name!");
return create_pair(ctx, ERROR_ATOM, NOPROC_ATOM);
}
target_local_pid = registered_process;
}
} else {
target_local_pid = target_pid;
}

uint32_t exti = 1U << gpio_pin;

if (!list_is_empty(&gpio_data->gpio_listeners)) {
Expand Down Expand Up @@ -815,7 +838,7 @@ static term gpiodriver_set_int(Context *ctx, int32_t target_pid, term cmd)
AVM_ABORT();
}
list_append(&gpio_data->gpio_listeners, &data->gpio_listener_list_head);
data->target_local_pid = target_pid;
data->target_local_pid = target_local_pid;
data->bank_atom = gpio_bank_atom;
data->gpio_pin = gpio_pin;
data->exti = exti;
Expand Down

0 comments on commit e5af122

Please sign in to comment.