diff --git a/ports/stm32f4/common-hal/digitalio/DigitalInOut.c b/ports/stm32f4/common-hal/digitalio/DigitalInOut.c index e19988a7f7..6f2a0976e3 100644 --- a/ports/stm32f4/common-hal/digitalio/DigitalInOut.c +++ b/ports/stm32f4/common-hal/digitalio/DigitalInOut.c @@ -33,7 +33,7 @@ void common_hal_digitalio_digitalinout_never_reset( digitalio_digitalinout_obj_t *self) { - never_reset_pin_number(self->pin->number); + never_reset_pin_number(self->pin->port, self->pin->number); } digitalinout_result_t common_hal_digitalio_digitalinout_construct( @@ -43,7 +43,7 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct( self->pin = pin; GPIO_InitTypeDef GPIO_InitStruct = {0}; - GPIO_InitStruct.Pin = (pin_mask(self->pin->number)); + GPIO_InitStruct.Pin = 1<pin->number; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; @@ -61,7 +61,7 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self return; } - reset_pin_number(self->pin->number); + reset_pin_number(self->pin->port, self->pin->number); self->pin = mp_const_none; } @@ -69,7 +69,7 @@ void common_hal_digitalio_digitalinout_switch_to_input( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { GPIO_InitTypeDef GPIO_InitStruct = {0}; - GPIO_InitStruct.Pin = pin_mask(self->pin->number); + GPIO_InitStruct.Pin = 1<pin->number; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; @@ -89,27 +89,27 @@ void common_hal_digitalio_digitalinout_switch_to_output( digitalio_direction_t common_hal_digitalio_digitalinout_get_direction( digitalio_digitalinout_obj_t *self) { - return (LL_GPIO_GetPinMode(pin_port(self->pin->number), pin_mask(self->pin->number)) + return (LL_GPIO_GetPinMode(pin_port(self->pin->number), 1<pin->number) == LL_GPIO_MODE_INPUT) ? DIRECTION_INPUT : DIRECTION_OUTPUT; } void common_hal_digitalio_digitalinout_set_value( digitalio_digitalinout_obj_t *self, bool value) { - HAL_GPIO_WritePin(pin_port(self->pin->number), pin_mask(self->pin->number), value); + HAL_GPIO_WritePin(pin_port(self->pin->number), 1<pin->number, value); } bool common_hal_digitalio_digitalinout_get_value( digitalio_digitalinout_obj_t *self) { - return (LL_GPIO_GetPinMode(pin_port(self->pin->number), pin_mask(self->pin->number)) == LL_GPIO_MODE_INPUT) - ? HAL_GPIO_ReadPin(pin_port(self->pin->number), pin_mask(self->pin->number)) - : LL_GPIO_IsOutputPinSet(pin_port(self->pin->number), pin_mask(self->pin->number)); + return (LL_GPIO_GetPinMode(pin_port(self->pin->number), 1<pin->number) == LL_GPIO_MODE_INPUT) + ? HAL_GPIO_ReadPin(pin_port(self->pin->number), 1<pin->number) + : LL_GPIO_IsOutputPinSet(pin_port(self->pin->number), 1<pin->number); } void common_hal_digitalio_digitalinout_set_drive_mode( digitalio_digitalinout_obj_t *self, digitalio_drive_mode_t drive_mode) { GPIO_InitTypeDef GPIO_InitStruct = {0}; - GPIO_InitStruct.Pin = pin_mask(self->pin->number); + GPIO_InitStruct.Pin = 1<pin->number; GPIO_InitStruct.Mode = (drive_mode == DRIVE_MODE_OPEN_DRAIN ? GPIO_MODE_OUTPUT_OD : GPIO_MODE_OUTPUT_PP); GPIO_InitStruct.Pull = GPIO_NOPULL; @@ -120,7 +120,7 @@ void common_hal_digitalio_digitalinout_set_drive_mode( digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( digitalio_digitalinout_obj_t *self) { - return LL_GPIO_GetPinOutputType(pin_port(self->pin->number), pin_mask(self->pin->number)) + return LL_GPIO_GetPinOutputType(pin_port(self->pin->number), 1<pin->number) == LL_GPIO_OUTPUT_OPENDRAIN ? DRIVE_MODE_OPEN_DRAIN : DRIVE_MODE_PUSH_PULL; } @@ -129,13 +129,13 @@ void common_hal_digitalio_digitalinout_set_pull( switch (pull) { case PULL_UP: - LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number),LL_GPIO_PULL_UP); + LL_GPIO_SetPinPull(pin_port(self->pin->number), 1<pin->number,LL_GPIO_PULL_UP); break; case PULL_DOWN: - LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number),LL_GPIO_PULL_DOWN); + LL_GPIO_SetPinPull(pin_port(self->pin->number), 1<pin->number,LL_GPIO_PULL_DOWN); break; case PULL_NONE: - LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number),LL_GPIO_PULL_NO); + LL_GPIO_SetPinPull(pin_port(self->pin->number), 1<pin->number,LL_GPIO_PULL_NO); break; default: break; @@ -146,7 +146,7 @@ digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( digitalio_digitalinout_obj_t *self) { - switch (LL_GPIO_GetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number))) { + switch (LL_GPIO_GetPinPull(pin_port(self->pin->number), 1<pin->number)) { case LL_GPIO_PULL_UP: return PULL_UP; case LL_GPIO_PULL_DOWN: diff --git a/ports/stm32f4/common-hal/microcontroller/Pin.c b/ports/stm32f4/common-hal/microcontroller/Pin.c index 2e0da9ccbe..3a2dd01c58 100644 --- a/ports/stm32f4/common-hal/microcontroller/Pin.c +++ b/ports/stm32f4/common-hal/microcontroller/Pin.c @@ -53,40 +53,40 @@ void reset_all_pins(void) { } // Mark pin as free and return it to a quiescent state. -void reset_pin_number(uint8_t pin) { - if (pin == NO_PIN) { +void reset_pin_number(uint8_t pin_port, uint8_t pin_number) { + if (pin_port == 0x00) { return; } // Clear claimed bit. - claimed_pins[gpio_port_num(pin)] &= ~(gpio_mask(pin)); + claimed_pins[pin_port] &= ~(1<number)] |= gpio_mask(pin->number); + claimed_pins[pin->port] |= 1<number; } - -bool pin_number_is_free(uint8_t pin) { - return !(claimed_pins[gpio_port_num(pin)] & gpio_mask(pin)); +bool pin_number_is_free(uint8_t pin_port, uint8_t pin_number) { + return !(claimed_pins[pin_port] & 1<number); + return pin_number_is_free(pin->port, pin->number); } -GPIO_TypeDef * pin_port(uint8_t pin) { - return ports[gpio_port_num(pin)]; +GPIO_TypeDef * pin_port(uint8_t pin_port) { + return ports[pin_port]; } -uint16_t pin_mask(uint8_t pin) { - return gpio_mask(pin); +//TODO: replace with macro? +uint16_t pin_mask(uint8_t pin_number) { + return 1<> 4; -} - -static inline uint16_t gpio_mask(uint8_t packed_pin) { - return 1 << (0x0F & packed_pin); -} +bool pin_number_is_free(uint8_t pin_port, uint8_t pin_number); +void never_reset_pin_number(uint8_t pin_port, uint8_t pin_number); +GPIO_TypeDef * pin_port(uint8_t pin_port); +uint16_t pin_mask(uint8_t pin_number); #endif // MICROPY_INCLUDED_STM34F4_COMMON_HAL_MICROCONTROLLER_PIN_H diff --git a/ports/stm32f4/peripherals/stm32f4/pins.h b/ports/stm32f4/peripherals/stm32f4/pins.h index ee11cbe832..2bda672c05 100644 --- a/ports/stm32f4/peripherals/stm32f4/pins.h +++ b/ports/stm32f4/peripherals/stm32f4/pins.h @@ -38,8 +38,10 @@ typedef struct { mp_obj_base_t base; - uint8_t number; - uint8_t adc_num_input; //(3)mask () + uint8_t port:4; + uint8_t number:4; + uint8_t adc_unit:3; + uint8_t adc_channel:5; } mcu_pin_obj_t; #define ADC_1 1 @@ -49,11 +51,14 @@ typedef struct { //STM32 ADC pins can have a combination of 1, 2 or all 3 ADCs on a single pin, //but all 3 ADCs will share the same input number per pin. +//F4 family has 3 ADC max, 24 channels max. #define ADC_INPUT(mask, number) \ - .adc_num_input = (((mask) << 5) | ((number) & 0x1F)), + .adc_unit = mask, \ + .adc_channel = number, #define NO_ADC \ - .adc_num_input = 0xff, + .adc_unit = 0x00, \ + .adc_channel = 0x1f extern const mp_obj_type_t mcu_pin_type; @@ -62,7 +67,8 @@ extern const mp_obj_type_t mcu_pin_type; #define PIN(p_port, p_number, p_adc) \ { \ { &mcu_pin_type }, \ - .number = (((p_port) << 4) | ((p_number) & 0x0F)), \ + .port = p_port, \ + .number = p_number, \ p_adc \ }