diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2020-11-24 14:03:19 -0700 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2020-11-24 14:03:19 -0700 |
commit | 351ff7059a5bacb322664412a8c62ee4640b33bf (patch) | |
tree | 53ef3fb16c5742c2edc45e633d80f6e16896f864 /src/kern/gpio/gpio_manager.c | |
parent | 6a1e0acc14b62c00317ac61c6ad6d8ffe441be4f (diff) | |
download | stm32l4-351ff7059a5bacb322664412a8c62ee4640b33bf.tar.gz stm32l4-351ff7059a5bacb322664412a8c62ee4640b33bf.tar.bz2 stm32l4-351ff7059a5bacb322664412a8c62ee4640b33bf.zip |
Update .clang-format and run it on filse
Diffstat (limited to 'src/kern/gpio/gpio_manager.c')
-rw-r--r-- | src/kern/gpio/gpio_manager.c | 156 |
1 files changed, 72 insertions, 84 deletions
diff --git a/src/kern/gpio/gpio_manager.c b/src/kern/gpio/gpio_manager.c index 82dd0ba..749ae79 100644 --- a/src/kern/gpio/gpio_manager.c +++ b/src/kern/gpio/gpio_manager.c @@ -17,7 +17,7 @@ struct gpio_afn_and_pin { * * This function will use afn_number = -1 as the terminal. * - * Note that EVENTOUT is a special case because all pins have an event out + * Note that EVENTOUT is a special case because all pins have an event out * at afn=15 and should be assumed by other logic and thus is not handled * by this function. */ @@ -25,42 +25,38 @@ static void get_ports_and_pins_for_alternate_function( gpio_alternate_function_t afn, struct gpio_afn_and_pin* out) { switch (afn) { -#define AFN1(fn, ...) \ - static_assert(false, "Unable to parse afn_table at " #fn); -#define AFN3(fn, ...) \ - static_assert(false, "Unable to parse afn_table at " #fn); -#define AFN5(fn, ...) \ - static_assert(false, "Unable to parse afn_table at " #fn); -#define AFN7(fn, ...) \ - static_assert(false, "Unable to parse afn_table at " #fn); +#define AFN1(fn, ...) static_assert(false, "Unable to parse afn_table at " #fn); +#define AFN3(fn, ...) static_assert(false, "Unable to parse afn_table at " #fn); +#define AFN5(fn, ...) static_assert(false, "Unable to parse afn_table at " #fn); +#define AFN7(fn, ...) static_assert(false, "Unable to parse afn_table at " #fn); #define AFN2(fn, afn, pin) \ - out[0].afn_number = afn; \ - out[0].gpio_pin = GPIO_PIN_ ## pin + out[0].afn_number = afn; \ + out[0].gpio_pin = GPIO_PIN_##pin #define AFN4(fn, afn0, pin0, afn1, pin1) \ - AFN2(fn, afn0, pin0); \ - out[1].afn_number = afn1; \ - out[1].gpio_pin = GPIO_PIN_ ## pin1 + AFN2(fn, afn0, pin0); \ + out[1].afn_number = afn1; \ + out[1].gpio_pin = GPIO_PIN_##pin1 #define AFN6(fn, afn0, pin0, afn1, pin1, afn2, pin2) \ - AFN4(fn, afn0, pin0, afn1, pin1); \ - out[2].afn_number = afn2; \ - out[2].gpio_pin = GPIO_PIN_ ## pin2 + AFN4(fn, afn0, pin0, afn1, pin1); \ + out[2].afn_number = afn2; \ + out[2].gpio_pin = GPIO_PIN_##pin2 #define AFN8(fn, afn0, pin0, afn1, pin1, afn2, pin2, afn3, pin3) \ - AFN6(fn, afn0, pin0, afn1, pin1, afn2, pin2); \ - out[2].afn_number = afn3; \ - out[2].gpio_pin = GPIO_PIN_ ## pin3 + AFN6(fn, afn0, pin0, afn1, pin1, afn2, pin2); \ + out[2].afn_number = afn3; \ + out[2].gpio_pin = GPIO_PIN_##pin3 #define GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME #define GET_N(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME -#define AFN(fn, ...) \ - case GPIO_ALTERNATE_FUNCTION_ ## fn: \ - GET_MACRO(__VA_ARGS__, AFN8, AFN7, AFN6, AFN5, AFN4, AFN3, AFN2, AFN1)\ - (fn, __VA_ARGS__); \ - out[GET_N(__VA_ARGS__, 4, 4, 3, 3, 2, 2, 1, 1)] = \ - (struct gpio_afn_and_pin){-1, -1}; \ +#define AFN(fn, ...) \ + case GPIO_ALTERNATE_FUNCTION_##fn: \ + GET_MACRO(__VA_ARGS__, AFN8, AFN7, AFN6, AFN5, AFN4, AFN3, AFN2, AFN1) \ + (fn, __VA_ARGS__); \ + out[GET_N(__VA_ARGS__, 4, 4, 3, 3, 2, 2, 1, 1)] = \ + (struct gpio_afn_and_pin){-1, -1}; \ break; #include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/afn_table.inc" @@ -72,11 +68,13 @@ static void get_ports_and_pins_for_alternate_function( static inline int offset_for_gpio_pin(gpio_pin_t pin) { switch (pin) { -#define PORT(p, pn) \ - case GPIO_PIN_P ## p ## pn: return pn; +#define PORT(p, pn) \ + case GPIO_PIN_P##p##pn: \ + return pn; #include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc" #undef PORT - case N_GPIO_PINS: return -1; + case N_GPIO_PINS: + return -1; } /* Should be unreachable. */ @@ -98,16 +96,14 @@ inline bool gpio_pin_in_use(gpio_pin_t pin) #define H(...) #define I(...) #define SELECT_MACRO(PORT) PORT -#define PORT(port, pin) \ - SELECT_MACRO(port)(GPIO_PIN_P ## port ## pin, pin) +#define PORT(port, pin) SELECT_MACRO(port)(GPIO_PIN_P##port##pin, pin) static int gc_port_a() { return 0 #undef A -#define A(abspin, relpin) \ - | (gpio_pin_in_use(abspin) << (relpin)) +#define A(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin)) #include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc" - ; + ; #undef A #define A(...) } @@ -116,10 +112,9 @@ static int gc_port_b() { return 0 #undef B -#define B(abspin, relpin) \ - | (gpio_pin_in_use(abspin) << (relpin)) +#define B(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin)) #include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc" - ; + ; #undef B #define B(...) } @@ -128,10 +123,9 @@ static int gc_port_c() { return 0 #undef C -#define C(abspin, relpin) \ - | (gpio_pin_in_use(abspin) << (relpin)) +#define C(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin)) #include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc" - ; + ; #undef C #define C(...) } @@ -140,10 +134,9 @@ static int gc_port_d() { return 0 #undef D -#define D(abspin, relpin) \ - | (gpio_pin_in_use(abspin) << (relpin)) +#define D(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin)) #include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc" - ; + ; #undef D #define D(...) } @@ -152,10 +145,9 @@ static int gc_port_e() { return 0 #undef E -#define E(abspin, relpin) \ - | (gpio_pin_in_use(abspin) << (relpin)) +#define E(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin)) #include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc" - ; + ; #undef E #define E(...) } @@ -164,10 +156,9 @@ static int gc_port_f() { return 0 #undef F -#define F(abspin, relpin) \ - | (gpio_pin_in_use(abspin) << (relpin)) +#define F(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin)) #include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc" - ; + ; #undef F #define F(...) } @@ -176,10 +167,9 @@ static int gc_port_g() { return 0 #undef G -#define G(abspin, relpin) \ - | (gpio_pin_in_use(abspin) << (relpin)) +#define G(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin)) #include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc" - ; + ; #undef G #define G(...) } @@ -188,10 +178,9 @@ static int gc_port_h() { return 0 #undef H -#define H(abspin, relpin) \ - | (gpio_pin_in_use(abspin) << (relpin)) +#define H(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin)) #include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc" - ; + ; #undef H #define H(...) } @@ -200,30 +189,32 @@ static int gc_port_i() { return 0 #undef I -#define I(abspin, relpin) \ - | (gpio_pin_in_use(abspin) << (relpin)) +#define I(abspin, relpin) | (gpio_pin_in_use(abspin) << (relpin)) #include "arch/stm32l4xxx/peripherals/tables/stm32l432xx/gpio/port_table.inc" - ; + ; #undef I #define I(...) } - static inline bool gpio_pin_try_reserve(gpio_pin_t pin) { - int in_use = __sync_fetch_and_or( - &pins_inuse[pin / 32], 1 << (pin % 32)); + int in_use = __sync_fetch_and_or(&pins_inuse[pin / 32], 1 << (pin % 32)); return !(in_use & (1 << (pin % 32))); } inline static gpio_port_config_t* get_gpio_port_config(gpio_port_t port) { - switch(port) { - case GPIO_PORT_A: return (gpio_port_config_t*) GPIOA_BASE; - case GPIO_PORT_B: return (gpio_port_config_t*) GPIOB_BASE; - case GPIO_PORT_C: return (gpio_port_config_t*) GPIOC_BASE; - case GPIO_PORT_H: return (gpio_port_config_t*) GPIOH_BASE; - default: return NULL; + switch (port) { + case GPIO_PORT_A: + return (gpio_port_config_t*)GPIOA_BASE; + case GPIO_PORT_B: + return (gpio_port_config_t*)GPIOB_BASE; + case GPIO_PORT_C: + return (gpio_port_config_t*)GPIOC_BASE; + case GPIO_PORT_H: + return (gpio_port_config_t*)GPIOH_BASE; + default: + return NULL; } } @@ -239,7 +230,7 @@ gpio_reserved_pin_t reserve_gpio_pin( *error_out = 0; if (!gpio_pin_try_reserve(pin)) { *error_out = GPIO_ERROR_IN_USE; - return (gpio_reserved_pin_t) { .v_ = -1 }; + return (gpio_reserved_pin_t){.v_ = -1}; } gpio_port_t port = get_port_for_pin(pin); @@ -247,18 +238,18 @@ gpio_reserved_pin_t reserve_gpio_pin( gpio_port_config_t* port_config = get_gpio_port_config(port); - int off = offset_for_gpio_pin(pin); regset(port_config->mode_r, gpio_mode_n(off), opts->mode); regset(port_config->pupd_r, gpio_pupd_n(off), opts->pull_dir); - switch(opts->mode) { + switch (opts->mode) { case GPIO_MODE_INPUT: break; case GPIO_MODE_OUTPUT: - regset(port_config->ospeed_r, gpio_ospeed_n(off), opts->output_opts.speed); + regset( + port_config->ospeed_r, gpio_ospeed_n(off), opts->output_opts.speed); regset(port_config->otype_r, gpio_otype_n(off), opts->output_opts.type); break; @@ -281,13 +272,11 @@ gpio_reserved_pin_t reserve_gpio_pin( break; } - return (gpio_reserved_pin_t) { .v_ = pin }; + return (gpio_reserved_pin_t){.v_ = pin}; } gpio_reserved_pin_t gpio_enable_alternate_function( - gpio_alternate_function_t fn, - gpio_pin_t hint, - int* error_out) + gpio_alternate_function_t fn, gpio_pin_t hint, int* error_out) { int i = 0; gpio_pin_opts_t opts; @@ -295,7 +284,7 @@ gpio_reserved_pin_t gpio_enable_alternate_function( if (gpio_pin_out_of_range(hint) && hint != -1) { *error_out = GPIO_ERROR_INVALID_PIN; - return (gpio_reserved_pin_t) { .v_ = -1 }; + return (gpio_reserved_pin_t){.v_ = -1}; } opts.mode = GPIO_MODE_ALTERNATE; @@ -307,21 +296,20 @@ gpio_reserved_pin_t gpio_enable_alternate_function( } afn_and_pin[i].gpio_pin = hint; } else { - get_ports_and_pins_for_alternate_function(fn, afn_and_pin); + get_ports_and_pins_for_alternate_function(fn, afn_and_pin); if (hint == -1) { hint = afn_and_pin[0].gpio_pin; } - for(i = 0; - i < 5 - && afn_and_pin[i].gpio_pin != hint - && afn_and_pin[i].gpio_pin != -1; - ++ i); + for (i = 0; i < 5 && afn_and_pin[i].gpio_pin != hint && + afn_and_pin[i].gpio_pin != -1; + ++i) + ; if (afn_and_pin[i].gpio_pin == -1 || i == 5) { *error_out = GPIO_ERROR_INVALID_PIN_FOR_ALTERNATE_FUNCTION; - return (gpio_reserved_pin_t) { .v_ = -1 }; + return (gpio_reserved_pin_t){.v_ = -1}; } } @@ -337,7 +325,7 @@ void release_gpio_pin(gpio_reserved_pin_t rpin) pins_inuse[pin / 32] &= ~(1 << (pin % 32)); int used; - switch(port) { + switch (port) { case GPIO_PORT_A: used = gc_port_a(); break; |