From ba45f9807332663f295024b8d8bba659dd1c1fd4 Mon Sep 17 00:00:00 2001 From: Hierophect Date: Fri, 6 Sep 2019 11:26:20 -0400 Subject: [PATCH 01/11] Add basic analogio files --- ports/stm32f4/common-hal/analogio/AnalogIn.c | 112 ++++++++++++++++++ ports/stm32f4/common-hal/analogio/AnalogIn.h | 39 ++++++ ports/stm32f4/common-hal/analogio/AnalogOut.c | 48 ++++++++ ports/stm32f4/common-hal/analogio/AnalogOut.h | 36 ++++++ ports/stm32f4/common-hal/analogio/__init__.c | 1 + 5 files changed, 236 insertions(+) create mode 100644 ports/stm32f4/common-hal/analogio/AnalogIn.c create mode 100644 ports/stm32f4/common-hal/analogio/AnalogIn.h create mode 100644 ports/stm32f4/common-hal/analogio/AnalogOut.c create mode 100644 ports/stm32f4/common-hal/analogio/AnalogOut.h create mode 100644 ports/stm32f4/common-hal/analogio/__init__.c diff --git a/ports/stm32f4/common-hal/analogio/AnalogIn.c b/ports/stm32f4/common-hal/analogio/AnalogIn.c new file mode 100644 index 0000000000..f20802ac98 --- /dev/null +++ b/ports/stm32f4/common-hal/analogio/AnalogIn.c @@ -0,0 +1,112 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Dan Halbert for Adafruit Industries + * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "common-hal/analogio/AnalogIn.h" +#include "py/runtime.h" +#include "supervisor/shared/translate.h" + +#include "nrfx_saadc.h" +#include "nrf_gpio.h" + +#define CHANNEL_NO 0 + +void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) { + if (pin->adc_channel == 0) + mp_raise_ValueError(translate("Pin does not have ADC capabilities")); + + nrf_gpio_cfg_default(pin->number); + + claim_pin(pin); + self->pin = pin; +} + +bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t *self) { + return self->pin == mp_const_none; +} + +void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) { + if (common_hal_analogio_analogin_deinited(self)) + return; + + nrf_gpio_cfg_default(self->pin->number); + + reset_pin_number(self->pin->number); + self->pin = mp_const_none; +} + +uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { + // Something else might have used the ADC in a different way, + // so we completely re-initialize it. + + nrf_saadc_value_t value; + + const nrf_saadc_channel_config_t config = { + .resistor_p = NRF_SAADC_RESISTOR_DISABLED, + .resistor_n = NRF_SAADC_RESISTOR_DISABLED, + .gain = NRF_SAADC_GAIN1_6, + .reference = NRF_SAADC_REFERENCE_INTERNAL, + .acq_time = NRF_SAADC_ACQTIME_3US, + .mode = NRF_SAADC_MODE_SINGLE_ENDED, + .burst = NRF_SAADC_BURST_DISABLED, + .pin_p = self->pin->adc_channel, + .pin_n = self->pin->adc_channel, + }; + + nrf_saadc_resolution_set(NRF_SAADC_RESOLUTION_14BIT); + nrf_saadc_oversample_set(NRF_SAADC_OVERSAMPLE_DISABLED); + nrf_saadc_enable(); + + for (uint32_t i = 0; i < NRF_SAADC_CHANNEL_COUNT; i++) + nrf_saadc_channel_input_set(i, NRF_SAADC_INPUT_DISABLED, NRF_SAADC_INPUT_DISABLED); + + nrf_saadc_channel_init(CHANNEL_NO, &config); + nrf_saadc_buffer_init(&value, 1); + + nrf_saadc_task_trigger(NRF_SAADC_TASK_START); + while (nrf_saadc_event_check(NRF_SAADC_EVENT_STARTED) == 0); + nrf_saadc_event_clear(NRF_SAADC_EVENT_STARTED); + + nrf_saadc_task_trigger(NRF_SAADC_TASK_SAMPLE); + while (nrf_saadc_event_check(NRF_SAADC_EVENT_END) == 0); + nrf_saadc_event_clear(NRF_SAADC_EVENT_END); + + nrf_saadc_task_trigger(NRF_SAADC_TASK_STOP); + while (nrf_saadc_event_check(NRF_SAADC_EVENT_STOPPED) == 0); + nrf_saadc_event_clear(NRF_SAADC_EVENT_STOPPED); + + nrf_saadc_disable(); + + if (value < 0) + value = 0; + + // Map value to from 14 to 16 bits + return (value << 2); +} + +float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) { + return 3.3f; +} diff --git a/ports/stm32f4/common-hal/analogio/AnalogIn.h b/ports/stm32f4/common-hal/analogio/AnalogIn.h new file mode 100644 index 0000000000..e0e95bad4c --- /dev/null +++ b/ports/stm32f4/common-hal/analogio/AnalogIn.h @@ -0,0 +1,39 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_ANALOGIO_ANALOGIN_H +#define MICROPY_INCLUDED_NRF_COMMON_HAL_ANALOGIO_ANALOGIN_H + +#include "common-hal/microcontroller/Pin.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t * pin; +} analogio_analogin_obj_t; + +#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_ANALOGIO_ANALOGIN_H diff --git a/ports/stm32f4/common-hal/analogio/AnalogOut.c b/ports/stm32f4/common-hal/analogio/AnalogOut.c new file mode 100644 index 0000000000..adafa15d5c --- /dev/null +++ b/ports/stm32f4/common-hal/analogio/AnalogOut.c @@ -0,0 +1,48 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Dan Halbert for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/analogio/AnalogOut.h" + +#include +#include + +#include "py/mperrno.h" +#include "py/runtime.h" +#include "supervisor/shared/translate.h" + +void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, const mcu_pin_obj_t *pin) { + mp_raise_RuntimeError(translate("AnalogOut functionality not supported")); +} + +bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) { + return true; +} + +void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) { +} + +void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self, uint16_t value) { +} diff --git a/ports/stm32f4/common-hal/analogio/AnalogOut.h b/ports/stm32f4/common-hal/analogio/AnalogOut.h new file mode 100644 index 0000000000..3244ee33b8 --- /dev/null +++ b/ports/stm32f4/common-hal/analogio/AnalogOut.h @@ -0,0 +1,36 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_ANALOGIO_ANALOGOUT_H +#define MICROPY_INCLUDED_NRF_COMMON_HAL_ANALOGIO_ANALOGOUT_H + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; +} analogio_analogout_obj_t; + +#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_ANALOGIO_ANALOGOUT_H diff --git a/ports/stm32f4/common-hal/analogio/__init__.c b/ports/stm32f4/common-hal/analogio/__init__.c new file mode 100644 index 0000000000..eea58c77d6 --- /dev/null +++ b/ports/stm32f4/common-hal/analogio/__init__.c @@ -0,0 +1 @@ +// No analogio module functions. From 8c0be5fbe2bc76d438bb78ebc1dc13d6d84c73ba Mon Sep 17 00:00:00 2001 From: Hierophect Date: Fri, 6 Sep 2019 17:30:52 -0400 Subject: [PATCH 02/11] Pack pin numbers, add ADC to pin structure, support DigitalIO --- .../common-hal/digitalio/DigitalInOut.c | 36 +-- .../stm32f4/common-hal/microcontroller/Pin.c | 30 ++- .../stm32f4/common-hal/microcontroller/Pin.h | 17 +- ports/stm32f4/peripherals/stm32f4/pins.h | 34 ++- .../peripherals/stm32f4/stm32f411xe/pins.c | 158 ++++++------ .../peripherals/stm32f4/stm32f412zx/pins.c | 224 +++++++++--------- 6 files changed, 262 insertions(+), 237 deletions(-) diff --git a/ports/stm32f4/common-hal/digitalio/DigitalInOut.c b/ports/stm32f4/common-hal/digitalio/DigitalInOut.c index 0805779f43..9410e7f415 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->port_number, self->pin->number); + never_reset_pin_number(self->pin->number); } digitalinout_result_t common_hal_digitalio_digitalinout_construct( @@ -43,11 +43,11 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct( self->pin = pin; GPIO_InitTypeDef GPIO_InitStruct = {0}; - GPIO_InitStruct.Pin = (1 << pin->number); + GPIO_InitStruct.Pin = (pin_mask(pin->number)); GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(pin->port, &GPIO_InitStruct); + HAL_GPIO_Init(pin_port(pin->number), &GPIO_InitStruct); return DIGITALINOUT_OK; } @@ -61,7 +61,7 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self return; } - reset_pin_number(self->pin->port_number, self->pin->number); + reset_pin_number(self->pin->number); self->pin = mp_const_none; } @@ -69,11 +69,11 @@ 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 = (1 << self->pin->number); + GPIO_InitStruct.Pin = pin_mask(pin->number); GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(self->pin->port, &GPIO_InitStruct); + HAL_GPIO_Init(pin_port(pin->number), &GPIO_InitStruct); common_hal_digitalio_digitalinout_set_pull(self, pull); } @@ -89,38 +89,38 @@ 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(self->pin->port, (1 << self->pin->number)) + return (LL_GPIO_GetPinMode(pin_port(pin->number), pin_mask(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(self->pin->port, 1 << self->pin->number, value); + HAL_GPIO_WritePin(pin_port(pin->number), 1 << self->pin->number, value); } bool common_hal_digitalio_digitalinout_get_value( digitalio_digitalinout_obj_t *self) { - return (LL_GPIO_GetPinMode(self->pin->port, (1 << self->pin->number)) == LL_GPIO_MODE_INPUT) - ? HAL_GPIO_ReadPin(self->pin->port, (1 << self->pin->number)) - : LL_GPIO_IsOutputPinSet(self->pin->port, (1 << self->pin->number)); + return (LL_GPIO_GetPinMode(pin_port(pin->number), pin_mask(pin->number)) == LL_GPIO_MODE_INPUT) + ? HAL_GPIO_ReadPin(pin_port(pin->number), pin_mask(pin->number)) + : LL_GPIO_IsOutputPinSet(pin_port(pin->number), pin_mask(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 = (1 << self->pin->number); + GPIO_InitStruct.Pin = pin_mask(pin->number); GPIO_InitStruct.Mode = (drive_mode == DRIVE_MODE_OPEN_DRAIN ? GPIO_MODE_OUTPUT_OD : GPIO_MODE_OUTPUT_PP); GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(self->pin->port, &GPIO_InitStruct); + HAL_GPIO_Init(pin_port(pin->number), &GPIO_InitStruct); } digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( digitalio_digitalinout_obj_t *self) { - return LL_GPIO_GetPinOutputType(self->pin->port, (1 << self->pin->port_number)) + return LL_GPIO_GetPinOutputType(pin_port(pin->number),pin_mask(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(self->pin->port,(1 << self->pin->number),LL_GPIO_PULL_UP); + LL_GPIO_SetPinPull(pin_port(pin->number),pin_mask(pin->number),LL_GPIO_PULL_UP); break; case PULL_DOWN: - LL_GPIO_SetPinPull(self->pin->port,(1 << self->pin->number),LL_GPIO_PULL_DOWN); + LL_GPIO_SetPinPull(pin_port(pin->number),pin_mask(pin->number),LL_GPIO_PULL_DOWN); break; case PULL_NONE: - LL_GPIO_SetPinPull(self->pin->port,(1 << self->pin->number),LL_GPIO_PULL_NO); + LL_GPIO_SetPinPull(pin_port(pin->number),pin_mask(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(self->pin->port,(1 << self->pin->number))) { + switch (LL_GPIO_GetPinPull(pin_port(pin->number),pin_mask(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 3450c935b6..735445ccba 100644 --- a/ports/stm32f4/common-hal/microcontroller/Pin.c +++ b/ports/stm32f4/common-hal/microcontroller/Pin.c @@ -53,34 +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_port, uint8_t pin_number) { - if (pin_number == NO_PIN) { +void reset_pin_number(uint8_t pin) { + if (pin == NO_PIN) { return; } // Clear claimed bit. - claimed_pins[pin_port] &= ~(1 << pin_number); + claimed_pins[gpio_port_num(pin)] &= ~(gpio_mask(pin)); // Reset the pin - HAL_GPIO_DeInit(ports[pin_port], (1 << pin_number)); + HAL_GPIO_DeInit(ports[gpio_port_num(pin)], gpio_mask(pin)); } -void never_reset_pin_number(uint8_t pin_port, uint8_t pin_number) { - never_reset_pins[pin_port] |= 1 << pin_number; +void never_reset_pin_number(uint8_t pin) { + never_reset_pins[gpio_port_num(pin)] |= gpio_mask(pin); } void claim_pin(const mcu_pin_obj_t* pin) { // Set bit in claimed_pins bitmask. - claimed_pins[pin->port_number] |= 1 << pin->number; + claimed_pins[gpio_port_num(pin->number)] |= gpio_mask(pin->number); } -bool pin_number_is_free(uint8_t pin_port, uint8_t pin_number) { - return !(claimed_pins[pin_port] & (1 << pin_number)); +bool pin_number_is_free(uint8_t pin) { + return !(claimed_pins[gpio_port_num(pin)] & gpio_mask(pin)); } bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) { - - return pin_number_is_free(pin->port_number, pin->number); - + return pin_number_is_free(pin->number); +} + +void* pin_port(uint8_t pin) { + return ports[gpio_port_num(pin)]; +} + +uint16_t pin_mask(uint8_t pin) { + return gpio_mask(pin); } diff --git a/ports/stm32f4/common-hal/microcontroller/Pin.h b/ports/stm32f4/common-hal/microcontroller/Pin.h index e5b1b9ceb0..ca83ef5cf1 100644 --- a/ports/stm32f4/common-hal/microcontroller/Pin.h +++ b/ports/stm32f4/common-hal/microcontroller/Pin.h @@ -34,9 +34,20 @@ void reset_all_pins(void); // reset_pin_number takes the pin number instead of the pointer so that objects don't // need to store a full pointer. -void reset_pin_number(uint8_t pin_port, uint8_t pin); +void reset_pin_number(uint8_t pin); void claim_pin(const mcu_pin_obj_t* 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); +bool pin_number_is_free(uint8_t pin); +void never_reset_pin_number(uint8_t pin); +void* pin_port(uint8_t pin); +uint16_t pin_mask(uint8_t pin); + +//helper functions for unpacking the pin object "number" into hal-usable addr/mask +static inline uint8_t gpio_port_num(uint8_t packed_pin) { + return packed_pin >> 4; +} + +static inline uint16_t gpio_mask(uint8_t packed_pin) { + return 1 << (0x0F & packed_pin); +} #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 bf5abd5edc..0f3146c9b7 100644 --- a/ports/stm32f4/peripherals/stm32f4/pins.h +++ b/ports/stm32f4/peripherals/stm32f4/pins.h @@ -38,28 +38,36 @@ typedef struct { mp_obj_base_t base; - //uint8_t number; //(3)port,(5)pin - uint8_t port_number; - GPIO_TypeDef * port; uint8_t number; + uint8_t adc_num_input; //(3)mask () } mcu_pin_obj_t; -// extern GPIO_TypeDef *port_lookup_table[]; + // //uint8_t number; //(3)port,(5)pin + // uint8_t port_number; + // GPIO_TypeDef * port; -// static inline GPIO_TypeDef * get_GPIO_ptr(const mcu_pin_obj_t *p) -// { -// return port_lookup_table[0x7&( (p->number) >> 5)]; -// } +#define ADC_1 1 +#define ADC_123 7 +#define ADC_12 3 +#define ADC_3 4 + +//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. +#define ADC_INPUT(mask, number) \ + .adc_num_input = (((mask) << 5) | ((number) & 0x1F)), + +#define NO_ADC \ + .adc_num_input = 0xff, extern const mp_obj_type_t mcu_pin_type; -// Used in device-specific pins.c -#define PIN(p_port_num, p_port, p_number) \ +// STM32 can have up to 9 ports, each restricted to 16 pins +// We split the pin/port evenly, in contrast to nrf. +#define PIN(p_port, p_number, p_adc) \ { \ { &mcu_pin_type }, \ - .port_number = (p_port_num), \ - .port = (p_port), \ - .number = (p_number), \ + .number = (((mask) << 4) | ((number) & 0x0F)), \ + p_adc \ } // Use illegal pin value to mark unassigned pins. diff --git a/ports/stm32f4/peripherals/stm32f4/stm32f411xe/pins.c b/ports/stm32f4/peripherals/stm32f4/stm32f411xe/pins.c index 0f773961db..7670f9aebb 100644 --- a/ports/stm32f4/peripherals/stm32f4/stm32f411xe/pins.c +++ b/ports/stm32f4/peripherals/stm32f4/stm32f411xe/pins.c @@ -28,96 +28,96 @@ #include "py/mphal.h" #include "stm32f4/pins.h" -const mcu_pin_obj_t pin_PE02 = PIN(4, GPIOE, 2); -const mcu_pin_obj_t pin_PE03 = PIN(4, GPIOE, 3); -const mcu_pin_obj_t pin_PE04 = PIN(4, GPIOE, 4); -const mcu_pin_obj_t pin_PE05 = PIN(4, GPIOE, 5); -const mcu_pin_obj_t pin_PE06 = PIN(4, GPIOE, 6); +const mcu_pin_obj_t pin_PE02 = PIN(4, 2, NO_ADC); +const mcu_pin_obj_t pin_PE03 = PIN(4, 3, NO_ADC); +const mcu_pin_obj_t pin_PE04 = PIN(4, 4, NO_ADC); +const mcu_pin_obj_t pin_PE05 = PIN(4, 5, NO_ADC); +const mcu_pin_obj_t pin_PE06 = PIN(4, 6, NO_ADC); -const mcu_pin_obj_t pin_PC13 = PIN(2, GPIOC, 13); //anti-tamp -const mcu_pin_obj_t pin_PC14 = PIN(2, GPIOC, 14); //OSC32_IN -const mcu_pin_obj_t pin_PC15 = PIN(2, GPIOC, 15); //OSC32_OUT +const mcu_pin_obj_t pin_PC13 = PIN(2, 13, NO_ADC); //anti-tamp +const mcu_pin_obj_t pin_PC14 = PIN(2, 14, NO_ADC); //OSC32_IN +const mcu_pin_obj_t pin_PC15 = PIN(2, 15, NO_ADC); //OSC32_OUT -const mcu_pin_obj_t pin_PC00 = PIN(2, GPIOC, 0); -const mcu_pin_obj_t pin_PC01 = PIN(2, GPIOC, 1); -const mcu_pin_obj_t pin_PC02 = PIN(2, GPIOC, 2); -const mcu_pin_obj_t pin_PC03 = PIN(2, GPIOC, 3); +const mcu_pin_obj_t pin_PC00 = PIN(2, 0, ADC_INPUT(ADC_1,10)); +const mcu_pin_obj_t pin_PC01 = PIN(2, 1, ADC_INPUT(ADC_1,11)); +const mcu_pin_obj_t pin_PC02 = PIN(2, 2, ADC_INPUT(ADC_1,12)); +const mcu_pin_obj_t pin_PC03 = PIN(2, 3, ADC_INPUT(ADC_1,13)); -const mcu_pin_obj_t pin_PA00 = PIN(0, GPIOA, 0); -const mcu_pin_obj_t pin_PA01 = PIN(0, GPIOA, 1); -const mcu_pin_obj_t pin_PA02 = PIN(0, GPIOA, 2); -const mcu_pin_obj_t pin_PA03 = PIN(0, GPIOA, 3); -const mcu_pin_obj_t pin_PA04 = PIN(0, GPIOA, 4); -const mcu_pin_obj_t pin_PA05 = PIN(0, GPIOA, 5); -const mcu_pin_obj_t pin_PA06 = PIN(0, GPIOA, 6); -const mcu_pin_obj_t pin_PA07 = PIN(0, GPIOA, 7); +const mcu_pin_obj_t pin_PA00 = PIN(0, 0, ADC_INPUT(ADC_1,0)); +const mcu_pin_obj_t pin_PA01 = PIN(0, 1, ADC_INPUT(ADC_1,1)); +const mcu_pin_obj_t pin_PA02 = PIN(0, 2, ADC_INPUT(ADC_1,2)); +const mcu_pin_obj_t pin_PA03 = PIN(0, 3, ADC_INPUT(ADC_1,3)); +const mcu_pin_obj_t pin_PA04 = PIN(0, 4, ADC_INPUT(ADC_1,4)); +const mcu_pin_obj_t pin_PA05 = PIN(0, 5, ADC_INPUT(ADC_1,5)); +const mcu_pin_obj_t pin_PA06 = PIN(0, 6, ADC_INPUT(ADC_1,6)); +const mcu_pin_obj_t pin_PA07 = PIN(0, 7, ADC_INPUT(ADC_1,7)); -const mcu_pin_obj_t pin_PC04 = PIN(2, GPIOC, 4); -const mcu_pin_obj_t pin_PC05 = PIN(2, GPIOC, 5); +const mcu_pin_obj_t pin_PC04 = PIN(2, 4, ADC_INPUT(ADC_1,14)); +const mcu_pin_obj_t pin_PC05 = PIN(2, 5, ADC_INPUT(ADC_1,15)); -const mcu_pin_obj_t pin_PB00 = PIN(1, GPIOB, 0); -const mcu_pin_obj_t pin_PB01 = PIN(1, GPIOB, 1); -const mcu_pin_obj_t pin_PB02 = PIN(1, GPIOB, 2); +const mcu_pin_obj_t pin_PB00 = PIN(1, 0, ADC_INPUT(ADC_1,8)); +const mcu_pin_obj_t pin_PB01 = PIN(1, 1, ADC_INPUT(ADC_1,9)); +const mcu_pin_obj_t pin_PB02 = PIN(1, 2, NO_ADC); -const mcu_pin_obj_t pin_PE07 = PIN(4, GPIOE, 7); -const mcu_pin_obj_t pin_PE08 = PIN(4, GPIOE, 8); -const mcu_pin_obj_t pin_PE09 = PIN(4, GPIOE, 9); -const mcu_pin_obj_t pin_PE10 = PIN(4, GPIOE, 10); -const mcu_pin_obj_t pin_PE11 = PIN(4, GPIOE, 11); -const mcu_pin_obj_t pin_PE12 = PIN(4, GPIOE, 12); -const mcu_pin_obj_t pin_PE13 = PIN(4, GPIOE, 13); -const mcu_pin_obj_t pin_PE14 = PIN(4, GPIOE, 14); -const mcu_pin_obj_t pin_PE15 = PIN(4, GPIOE, 15); +const mcu_pin_obj_t pin_PE07 = PIN(4, 7, NO_ADC); +const mcu_pin_obj_t pin_PE08 = PIN(4, 8, NO_ADC); +const mcu_pin_obj_t pin_PE09 = PIN(4, 9, NO_ADC); +const mcu_pin_obj_t pin_PE10 = PIN(4, 10, NO_ADC); +const mcu_pin_obj_t pin_PE11 = PIN(4, 11, NO_ADC); +const mcu_pin_obj_t pin_PE12 = PIN(4, 12, NO_ADC); +const mcu_pin_obj_t pin_PE13 = PIN(4, 13, NO_ADC); +const mcu_pin_obj_t pin_PE14 = PIN(4, 14, NO_ADC); +const mcu_pin_obj_t pin_PE15 = PIN(4, 15, NO_ADC); -const mcu_pin_obj_t pin_PB10 = PIN(1, GPIOB, 10); -const mcu_pin_obj_t pin_PB12 = PIN(1, GPIOB, 12); -const mcu_pin_obj_t pin_PB13 = PIN(1, GPIOB, 13); -const mcu_pin_obj_t pin_PB14 = PIN(1, GPIOB, 14); -const mcu_pin_obj_t pin_PB15 = PIN(1, GPIOB, 15); +const mcu_pin_obj_t pin_PB10 = PIN(1, 10, NO_ADC); +const mcu_pin_obj_t pin_PB12 = PIN(1, 12, NO_ADC); +const mcu_pin_obj_t pin_PB13 = PIN(1, 13, NO_ADC); +const mcu_pin_obj_t pin_PB14 = PIN(1, 14, NO_ADC); +const mcu_pin_obj_t pin_PB15 = PIN(1, 15, NO_ADC); -const mcu_pin_obj_t pin_PD08 = PIN(3, GPIOD, 8); -const mcu_pin_obj_t pin_PD09 = PIN(3, GPIOD, 9); -const mcu_pin_obj_t pin_PD10 = PIN(3, GPIOD, 10); -const mcu_pin_obj_t pin_PD11 = PIN(3, GPIOD, 11); -const mcu_pin_obj_t pin_PD12 = PIN(3, GPIOD, 12); -const mcu_pin_obj_t pin_PD13 = PIN(3, GPIOD, 13); -const mcu_pin_obj_t pin_PD14 = PIN(3, GPIOD, 14); -const mcu_pin_obj_t pin_PD15 = PIN(3, GPIOD, 15); +const mcu_pin_obj_t pin_PD08 = PIN(3, 8, NO_ADC); +const mcu_pin_obj_t pin_PD09 = PIN(3, 9, NO_ADC); +const mcu_pin_obj_t pin_PD10 = PIN(3, 10, NO_ADC); +const mcu_pin_obj_t pin_PD11 = PIN(3, 11, NO_ADC); +const mcu_pin_obj_t pin_PD12 = PIN(3, 12, NO_ADC); +const mcu_pin_obj_t pin_PD13 = PIN(3, 13, NO_ADC); +const mcu_pin_obj_t pin_PD14 = PIN(3, 14, NO_ADC); +const mcu_pin_obj_t pin_PD15 = PIN(3, 15, NO_ADC); -const mcu_pin_obj_t pin_PC06 = PIN(2, GPIOC, 6); -const mcu_pin_obj_t pin_PC07 = PIN(2, GPIOC, 7); -const mcu_pin_obj_t pin_PC08 = PIN(2, GPIOC, 8); -const mcu_pin_obj_t pin_PC09 = PIN(2, GPIOC, 9); +const mcu_pin_obj_t pin_PC06 = PIN(2, 6, NO_ADC); +const mcu_pin_obj_t pin_PC07 = PIN(2, 7, NO_ADC); +const mcu_pin_obj_t pin_PC08 = PIN(2, 8, NO_ADC); +const mcu_pin_obj_t pin_PC09 = PIN(2, 9, NO_ADC); -const mcu_pin_obj_t pin_PA08 = PIN(0, GPIOA, 8); -const mcu_pin_obj_t pin_PA09 = PIN(0, GPIOA, 9); -const mcu_pin_obj_t pin_PA10 = PIN(0, GPIOA, 10); -const mcu_pin_obj_t pin_PA11 = PIN(0, GPIOA, 11); -const mcu_pin_obj_t pin_PA12 = PIN(0, GPIOA, 12); -const mcu_pin_obj_t pin_PA13 = PIN(0, GPIOA, 13); -const mcu_pin_obj_t pin_PA14 = PIN(0, GPIOA, 14); -const mcu_pin_obj_t pin_PA15 = PIN(0, GPIOA, 15); +const mcu_pin_obj_t pin_PA08 = PIN(0, 8, NO_ADC); +const mcu_pin_obj_t pin_PA09 = PIN(0, 9, NO_ADC); +const mcu_pin_obj_t pin_PA10 = PIN(0, 10, NO_ADC); +const mcu_pin_obj_t pin_PA11 = PIN(0, 11, NO_ADC); +const mcu_pin_obj_t pin_PA12 = PIN(0, 12, NO_ADC); +const mcu_pin_obj_t pin_PA13 = PIN(0, 13, NO_ADC); +const mcu_pin_obj_t pin_PA14 = PIN(0, 14, NO_ADC); +const mcu_pin_obj_t pin_PA15 = PIN(0, 15, NO_ADC); -const mcu_pin_obj_t pin_PC10 = PIN(2, GPIOC, 10); -const mcu_pin_obj_t pin_PC11 = PIN(2, GPIOC, 11); -const mcu_pin_obj_t pin_PC12 = PIN(2, GPIOC, 12); +const mcu_pin_obj_t pin_PC10 = PIN(2, 10, NO_ADC); +const mcu_pin_obj_t pin_PC11 = PIN(2, 11, NO_ADC); +const mcu_pin_obj_t pin_PC12 = PIN(2, 12, NO_ADC); -const mcu_pin_obj_t pin_PD00 = PIN(3, GPIOD, 0); -const mcu_pin_obj_t pin_PD01 = PIN(3, GPIOD, 1); -const mcu_pin_obj_t pin_PD02 = PIN(3, GPIOD, 2); -const mcu_pin_obj_t pin_PD03 = PIN(3, GPIOD, 3); -const mcu_pin_obj_t pin_PD04 = PIN(3, GPIOD, 4); -const mcu_pin_obj_t pin_PD05 = PIN(3, GPIOD, 5); -const mcu_pin_obj_t pin_PD06 = PIN(3, GPIOD, 6); -const mcu_pin_obj_t pin_PD07 = PIN(3, GPIOD, 7); +const mcu_pin_obj_t pin_PD00 = PIN(3, 0, NO_ADC); +const mcu_pin_obj_t pin_PD01 = PIN(3, 1, NO_ADC); +const mcu_pin_obj_t pin_PD02 = PIN(3, 2, NO_ADC); +const mcu_pin_obj_t pin_PD03 = PIN(3, 3, NO_ADC); +const mcu_pin_obj_t pin_PD04 = PIN(3, 4, NO_ADC); +const mcu_pin_obj_t pin_PD05 = PIN(3, 5, NO_ADC); +const mcu_pin_obj_t pin_PD06 = PIN(3, 6, NO_ADC); +const mcu_pin_obj_t pin_PD07 = PIN(3, 7, NO_ADC); -const mcu_pin_obj_t pin_PB03 = PIN(1, GPIOB, 3); -const mcu_pin_obj_t pin_PB04 = PIN(1, GPIOB, 4); -const mcu_pin_obj_t pin_PB05 = PIN(1, GPIOB, 5); -const mcu_pin_obj_t pin_PB06 = PIN(1, GPIOB, 6); -const mcu_pin_obj_t pin_PB07 = PIN(1, GPIOB, 7); -const mcu_pin_obj_t pin_PB08 = PIN(1, GPIOB, 8); -const mcu_pin_obj_t pin_PB09 = PIN(1, GPIOB, 9); +const mcu_pin_obj_t pin_PB03 = PIN(1, 3, NO_ADC); +const mcu_pin_obj_t pin_PB04 = PIN(1, 4, NO_ADC); +const mcu_pin_obj_t pin_PB05 = PIN(1, 5, NO_ADC); +const mcu_pin_obj_t pin_PB06 = PIN(1, 6, NO_ADC); +const mcu_pin_obj_t pin_PB07 = PIN(1, 7, NO_ADC); +const mcu_pin_obj_t pin_PB08 = PIN(1, 8, NO_ADC); +const mcu_pin_obj_t pin_PB09 = PIN(1, 9, NO_ADC); -const mcu_pin_obj_t pin_PE00 = PIN(4, GPIOE, 0); -const mcu_pin_obj_t pin_PE01 = PIN(4, GPIOE, 1); \ No newline at end of file +const mcu_pin_obj_t pin_PE00 = PIN(4, 0, NO_ADC); +const mcu_pin_obj_t pin_PE01 = PIN(4, 1, NO_ADC); \ No newline at end of file diff --git a/ports/stm32f4/peripherals/stm32f4/stm32f412zx/pins.c b/ports/stm32f4/peripherals/stm32f4/stm32f412zx/pins.c index 1a556e1008..c56de5a295 100644 --- a/ports/stm32f4/peripherals/stm32f4/stm32f412zx/pins.c +++ b/ports/stm32f4/peripherals/stm32f4/stm32f412zx/pins.c @@ -28,134 +28,134 @@ #include "py/mphal.h" #include "stm32f4/pins.h" -const mcu_pin_obj_t pin_PE02 = PIN(4, GPIOE, 2); -const mcu_pin_obj_t pin_PE03 = PIN(4, GPIOE, 3); -const mcu_pin_obj_t pin_PE04 = PIN(4, GPIOE, 4); -const mcu_pin_obj_t pin_PE05 = PIN(4, GPIOE, 5); -const mcu_pin_obj_t pin_PE06 = PIN(4, GPIOE, 6); +const mcu_pin_obj_t pin_PE02 = PIN(4, 2, NO_ADC); +const mcu_pin_obj_t pin_PE03 = PIN(4, 3, NO_ADC); +const mcu_pin_obj_t pin_PE04 = PIN(4, 4, NO_ADC); +const mcu_pin_obj_t pin_PE05 = PIN(4, 5, NO_ADC); +const mcu_pin_obj_t pin_PE06 = PIN(4, 6, NO_ADC); -const mcu_pin_obj_t pin_PC13 = PIN(2, GPIOC, 13); -const mcu_pin_obj_t pin_PC14 = PIN(2, GPIOC, 14); //OSC32_IN -const mcu_pin_obj_t pin_PC15 = PIN(2, GPIOC, 15); //OSC32_OUT +const mcu_pin_obj_t pin_PC13 = PIN(2, 13, NO_ADC); //anti-tamp +const mcu_pin_obj_t pin_PC14 = PIN(2, 14, NO_ADC); //OSC32_IN +const mcu_pin_obj_t pin_PC15 = PIN(2, 15, NO_ADC); //OSC32_OUT -const mcu_pin_obj_t pin_PF00 = PIN(5, GPIOF, 0); // 144 only -const mcu_pin_obj_t pin_PF01 = PIN(5, GPIOF, 1); // 144 only -const mcu_pin_obj_t pin_PF02 = PIN(5, GPIOF, 2); // 144 only -const mcu_pin_obj_t pin_PF03 = PIN(5, GPIOF, 3); // 144 only -const mcu_pin_obj_t pin_PF04 = PIN(5, GPIOF, 4); // 144 only -const mcu_pin_obj_t pin_PF05 = PIN(5, GPIOF, 5); // 144 only -const mcu_pin_obj_t pin_PF06 = PIN(5, GPIOF, 6); // 144 only -const mcu_pin_obj_t pin_PF07 = PIN(5, GPIOF, 7); // 144 only -const mcu_pin_obj_t pin_PF08 = PIN(5, GPIOF, 8); // 144 only -const mcu_pin_obj_t pin_PF09 = PIN(5, GPIOF, 9); // 144 only -const mcu_pin_obj_t pin_PF10 = PIN(5, GPIOF, 10); // 144 only +const mcu_pin_obj_t pin_PF00 = PIN(5, 0, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PF01 = PIN(5, 1, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PF02 = PIN(5, 2, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PF03 = PIN(5, 3, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PF04 = PIN(5, 4, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PF05 = PIN(5, 5, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PF06 = PIN(5, 6, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PF07 = PIN(5, 7, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PF08 = PIN(5, 8, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PF09 = PIN(5, 9, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PF10 = PIN(5, 10, NO_ADC); // 144 only -const mcu_pin_obj_t pin_PC00 = PIN(2, GPIOC, 0); -const mcu_pin_obj_t pin_PC01 = PIN(2, GPIOC, 1); -const mcu_pin_obj_t pin_PC02 = PIN(2, GPIOC, 2); -const mcu_pin_obj_t pin_PC03 = PIN(2, GPIOC, 3); +const mcu_pin_obj_t pin_PC00 = PIN(2, 0, ADC_INPUT(ADC_1,10)); +const mcu_pin_obj_t pin_PC01 = PIN(2, 1, ADC_INPUT(ADC_1,11)); +const mcu_pin_obj_t pin_PC02 = PIN(2, 2, ADC_INPUT(ADC_1,12)); +const mcu_pin_obj_t pin_PC03 = PIN(2, 3, ADC_INPUT(ADC_1,13)); -const mcu_pin_obj_t pin_PA00 = PIN(0, GPIOA, 0); -const mcu_pin_obj_t pin_PA01 = PIN(0, GPIOA, 1); -const mcu_pin_obj_t pin_PA02 = PIN(0, GPIOA, 2); -const mcu_pin_obj_t pin_PA03 = PIN(0, GPIOA, 3); -const mcu_pin_obj_t pin_PA04 = PIN(0, GPIOA, 4); -const mcu_pin_obj_t pin_PA05 = PIN(0, GPIOA, 5); -const mcu_pin_obj_t pin_PA06 = PIN(0, GPIOA, 6); -const mcu_pin_obj_t pin_PA07 = PIN(0, GPIOA, 7); +const mcu_pin_obj_t pin_PA00 = PIN(0, 0, ADC_INPUT(ADC_1,0)); +const mcu_pin_obj_t pin_PA01 = PIN(0, 1, ADC_INPUT(ADC_1,1)); +const mcu_pin_obj_t pin_PA02 = PIN(0, 2, ADC_INPUT(ADC_1,2)); +const mcu_pin_obj_t pin_PA03 = PIN(0, 3, ADC_INPUT(ADC_1,3)); +const mcu_pin_obj_t pin_PA04 = PIN(0, 4, ADC_INPUT(ADC_1,4)); +const mcu_pin_obj_t pin_PA05 = PIN(0, 5, ADC_INPUT(ADC_1,5)); +const mcu_pin_obj_t pin_PA06 = PIN(0, 6, ADC_INPUT(ADC_1,6)); +const mcu_pin_obj_t pin_PA07 = PIN(0, 7, ADC_INPUT(ADC_1,7)); -const mcu_pin_obj_t pin_PC04 = PIN(2, GPIOC, 4); -const mcu_pin_obj_t pin_PC05 = PIN(2, GPIOC, 5); +const mcu_pin_obj_t pin_PC04 = PIN(2, 4, ADC_INPUT(ADC_1,14)); +const mcu_pin_obj_t pin_PC05 = PIN(2, 5, ADC_INPUT(ADC_1,15)); -const mcu_pin_obj_t pin_PB00 = PIN(1, GPIOB, 0); -const mcu_pin_obj_t pin_PB01 = PIN(1, GPIOB, 1); -const mcu_pin_obj_t pin_PB02 = PIN(1, GPIOB, 2); +const mcu_pin_obj_t pin_PB00 = PIN(1, 0, ADC_INPUT(ADC_1,8)); +const mcu_pin_obj_t pin_PB01 = PIN(1, 1, ADC_INPUT(ADC_1,9)); +const mcu_pin_obj_t pin_PB02 = PIN(1, 2, NO_ADC); -const mcu_pin_obj_t pin_PF11 = PIN(5, GPIOF, 11); // 144 only -const mcu_pin_obj_t pin_PF12 = PIN(5, GPIOF, 12); // 144 only -const mcu_pin_obj_t pin_PF13 = PIN(5, GPIOF, 13); // 144 only -const mcu_pin_obj_t pin_PF14 = PIN(5, GPIOF, 14); // 144 only -const mcu_pin_obj_t pin_PF15 = PIN(5, GPIOF, 15); // 144 only +const mcu_pin_obj_t pin_PF11 = PIN(5, 11, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PF12 = PIN(5, 12, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PF13 = PIN(5, 13, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PF14 = PIN(5, 14, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PF15 = PIN(5, 15, NO_ADC); // 144 only -const mcu_pin_obj_t pin_PG00 = PIN(6, GPIOG, 0); // 144 only -const mcu_pin_obj_t pin_PG01 = PIN(6, GPIOG, 1); // 144 only +const mcu_pin_obj_t pin_PG00 = PIN(6, 0, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PG01 = PIN(6, 1, NO_ADC); // 144 only -const mcu_pin_obj_t pin_PE07 = PIN(4, GPIOE, 7); -const mcu_pin_obj_t pin_PE08 = PIN(4, GPIOE, 8); -const mcu_pin_obj_t pin_PE09 = PIN(4, GPIOE, 9); -const mcu_pin_obj_t pin_PE10 = PIN(4, GPIOE, 10); -const mcu_pin_obj_t pin_PE11 = PIN(4, GPIOE, 11); -const mcu_pin_obj_t pin_PE12 = PIN(4, GPIOE, 12); -const mcu_pin_obj_t pin_PE13 = PIN(4, GPIOE, 13); -const mcu_pin_obj_t pin_PE14 = PIN(4, GPIOE, 14); -const mcu_pin_obj_t pin_PE15 = PIN(4, GPIOE, 15); +const mcu_pin_obj_t pin_PE07 = PIN(4, 7, NO_ADC); +const mcu_pin_obj_t pin_PE08 = PIN(4, 8, NO_ADC); +const mcu_pin_obj_t pin_PE09 = PIN(4, 9, NO_ADC); +const mcu_pin_obj_t pin_PE10 = PIN(4, 10, NO_ADC); +const mcu_pin_obj_t pin_PE11 = PIN(4, 11, NO_ADC); +const mcu_pin_obj_t pin_PE12 = PIN(4, 12, NO_ADC); +const mcu_pin_obj_t pin_PE13 = PIN(4, 13, NO_ADC); +const mcu_pin_obj_t pin_PE14 = PIN(4, 14, NO_ADC); +const mcu_pin_obj_t pin_PE15 = PIN(4, 15, NO_ADC); -const mcu_pin_obj_t pin_PB10 = PIN(1, GPIOB, 10); -const mcu_pin_obj_t pin_PB12 = PIN(1, GPIOB, 12); -const mcu_pin_obj_t pin_PB11 = PIN(1, GPIOB, 11); // 144 only -const mcu_pin_obj_t pin_PB13 = PIN(1, GPIOB, 13); -const mcu_pin_obj_t pin_PB14 = PIN(1, GPIOB, 14); -const mcu_pin_obj_t pin_PB15 = PIN(1, GPIOB, 15); +const mcu_pin_obj_t pin_PB10 = PIN(1, 10, NO_ADC); +const mcu_pin_obj_t pin_PB11 = PIN(1, 11, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PB12 = PIN(1, 12, NO_ADC); +const mcu_pin_obj_t pin_PB13 = PIN(1, 13, NO_ADC); +const mcu_pin_obj_t pin_PB14 = PIN(1, 14, NO_ADC); +const mcu_pin_obj_t pin_PB15 = PIN(1, 15, NO_ADC); -const mcu_pin_obj_t pin_PD08 = PIN(3, GPIOD, 8); -const mcu_pin_obj_t pin_PD09 = PIN(3, GPIOD, 9); -const mcu_pin_obj_t pin_PD10 = PIN(3, GPIOD, 10); -const mcu_pin_obj_t pin_PD11 = PIN(3, GPIOD, 11); -const mcu_pin_obj_t pin_PD12 = PIN(3, GPIOD, 12); -const mcu_pin_obj_t pin_PD13 = PIN(3, GPIOD, 13); -const mcu_pin_obj_t pin_PD14 = PIN(3, GPIOD, 14); -const mcu_pin_obj_t pin_PD15 = PIN(3, GPIOD, 15); +const mcu_pin_obj_t pin_PD08 = PIN(3, 8, NO_ADC); +const mcu_pin_obj_t pin_PD09 = PIN(3, 9, NO_ADC); +const mcu_pin_obj_t pin_PD10 = PIN(3, 10, NO_ADC); +const mcu_pin_obj_t pin_PD11 = PIN(3, 11, NO_ADC); +const mcu_pin_obj_t pin_PD12 = PIN(3, 12, NO_ADC); +const mcu_pin_obj_t pin_PD13 = PIN(3, 13, NO_ADC); +const mcu_pin_obj_t pin_PD14 = PIN(3, 14, NO_ADC); +const mcu_pin_obj_t pin_PD15 = PIN(3, 15, NO_ADC); -const mcu_pin_obj_t pin_PG02 = PIN(6, GPIOG, 2); // 144 only -const mcu_pin_obj_t pin_PG03 = PIN(6, GPIOG, 3); // 144 only -const mcu_pin_obj_t pin_PG04 = PIN(6, GPIOG, 4); // 144 only -const mcu_pin_obj_t pin_PG05 = PIN(6, GPIOG, 5); // 144 only -const mcu_pin_obj_t pin_PG06 = PIN(6, GPIOG, 6); // 144 only -const mcu_pin_obj_t pin_PG07 = PIN(6, GPIOG, 7); // 144 only -const mcu_pin_obj_t pin_PG08 = PIN(6, GPIOG, 8); // 144 only +const mcu_pin_obj_t pin_PG02 = PIN(6, 2, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PG03 = PIN(6, 3, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PG04 = PIN(6, 4, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PG05 = PIN(6, 5, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PG06 = PIN(6, 6, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PG07 = PIN(6, 7, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PG08 = PIN(6, 8, NO_ADC); // 144 only -const mcu_pin_obj_t pin_PC06 = PIN(2, GPIOC, 6); -const mcu_pin_obj_t pin_PC07 = PIN(2, GPIOC, 7); -const mcu_pin_obj_t pin_PC08 = PIN(2, GPIOC, 8); -const mcu_pin_obj_t pin_PC09 = PIN(2, GPIOC, 9); +const mcu_pin_obj_t pin_PC06 = PIN(2, 6, NO_ADC); +const mcu_pin_obj_t pin_PC07 = PIN(2, 7, NO_ADC); +const mcu_pin_obj_t pin_PC08 = PIN(2, 8, NO_ADC); +const mcu_pin_obj_t pin_PC09 = PIN(2, 9, NO_ADC); -const mcu_pin_obj_t pin_PA08 = PIN(0, GPIOA, 8); -const mcu_pin_obj_t pin_PA09 = PIN(0, GPIOA, 9); -const mcu_pin_obj_t pin_PA10 = PIN(0, GPIOA, 10); -const mcu_pin_obj_t pin_PA11 = PIN(0, GPIOA, 11); -const mcu_pin_obj_t pin_PA12 = PIN(0, GPIOA, 12); -const mcu_pin_obj_t pin_PA13 = PIN(0, GPIOA, 13); -const mcu_pin_obj_t pin_PA14 = PIN(0, GPIOA, 14); -const mcu_pin_obj_t pin_PA15 = PIN(0, GPIOA, 15); +const mcu_pin_obj_t pin_PA08 = PIN(0, 8, NO_ADC); +const mcu_pin_obj_t pin_PA09 = PIN(0, 9, NO_ADC); +const mcu_pin_obj_t pin_PA10 = PIN(0, 10, NO_ADC); +const mcu_pin_obj_t pin_PA11 = PIN(0, 11, NO_ADC); +const mcu_pin_obj_t pin_PA12 = PIN(0, 12, NO_ADC); +const mcu_pin_obj_t pin_PA13 = PIN(0, 13, NO_ADC); +const mcu_pin_obj_t pin_PA14 = PIN(0, 14, NO_ADC); +const mcu_pin_obj_t pin_PA15 = PIN(0, 15, NO_ADC); -const mcu_pin_obj_t pin_PC10 = PIN(2, GPIOC, 10); -const mcu_pin_obj_t pin_PC11 = PIN(2, GPIOC, 11); -const mcu_pin_obj_t pin_PC12 = PIN(2, GPIOC, 12); +const mcu_pin_obj_t pin_PC10 = PIN(2, 10, NO_ADC); +const mcu_pin_obj_t pin_PC11 = PIN(2, 11, NO_ADC); +const mcu_pin_obj_t pin_PC12 = PIN(2, 12, NO_ADC); -const mcu_pin_obj_t pin_PD00 = PIN(3, GPIOD, 0); -const mcu_pin_obj_t pin_PD01 = PIN(3, GPIOD, 1); -const mcu_pin_obj_t pin_PD02 = PIN(3, GPIOD, 2); -const mcu_pin_obj_t pin_PD03 = PIN(3, GPIOD, 3); -const mcu_pin_obj_t pin_PD04 = PIN(3, GPIOD, 4); -const mcu_pin_obj_t pin_PD05 = PIN(3, GPIOD, 5); -const mcu_pin_obj_t pin_PD06 = PIN(3, GPIOD, 6); -const mcu_pin_obj_t pin_PD07 = PIN(3, GPIOD, 7); +const mcu_pin_obj_t pin_PD00 = PIN(3, 0, NO_ADC); +const mcu_pin_obj_t pin_PD01 = PIN(3, 1, NO_ADC); +const mcu_pin_obj_t pin_PD02 = PIN(3, 2, NO_ADC); +const mcu_pin_obj_t pin_PD03 = PIN(3, 3, NO_ADC); +const mcu_pin_obj_t pin_PD04 = PIN(3, 4, NO_ADC); +const mcu_pin_obj_t pin_PD05 = PIN(3, 5, NO_ADC); +const mcu_pin_obj_t pin_PD06 = PIN(3, 6, NO_ADC); +const mcu_pin_obj_t pin_PD07 = PIN(3, 7, NO_ADC); -const mcu_pin_obj_t pin_PG09 = PIN(6, GPIOG, 9); // 144 only -const mcu_pin_obj_t pin_PG10 = PIN(6, GPIOG, 10); // 144 only -const mcu_pin_obj_t pin_PG11 = PIN(6, GPIOG, 11); // 144 only -const mcu_pin_obj_t pin_PG12 = PIN(6, GPIOG, 12); // 144 only -const mcu_pin_obj_t pin_PG13 = PIN(6, GPIOG, 13); // 144 only -const mcu_pin_obj_t pin_PG14 = PIN(6, GPIOG, 14); // 144 only -const mcu_pin_obj_t pin_PG15 = PIN(6, GPIOG, 15); // 144 only +const mcu_pin_obj_t pin_PG09 = PIN(6, 9, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PG10 = PIN(6, 10, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PG11 = PIN(6, 11, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PG12 = PIN(6, 12, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PG13 = PIN(6, 13, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PG14 = PIN(6, 14, NO_ADC); // 144 only +const mcu_pin_obj_t pin_PG15 = PIN(6, 15, NO_ADC); // 144 only -const mcu_pin_obj_t pin_PB03 = PIN(1, GPIOB, 3); -const mcu_pin_obj_t pin_PB04 = PIN(1, GPIOB, 4); -const mcu_pin_obj_t pin_PB05 = PIN(1, GPIOB, 5); -const mcu_pin_obj_t pin_PB06 = PIN(1, GPIOB, 6); -const mcu_pin_obj_t pin_PB07 = PIN(1, GPIOB, 7); -const mcu_pin_obj_t pin_PB08 = PIN(1, GPIOB, 8); -const mcu_pin_obj_t pin_PB09 = PIN(1, GPIOB, 9); +const mcu_pin_obj_t pin_PB03 = PIN(1, 3, NO_ADC); +const mcu_pin_obj_t pin_PB04 = PIN(1, 4, NO_ADC); +const mcu_pin_obj_t pin_PB05 = PIN(1, 5, NO_ADC); +const mcu_pin_obj_t pin_PB06 = PIN(1, 6, NO_ADC); +const mcu_pin_obj_t pin_PB07 = PIN(1, 7, NO_ADC); +const mcu_pin_obj_t pin_PB08 = PIN(1, 8, NO_ADC); +const mcu_pin_obj_t pin_PB09 = PIN(1, 9, NO_ADC); -const mcu_pin_obj_t pin_PE00 = PIN(4, GPIOE, 0); -const mcu_pin_obj_t pin_PE01 = PIN(4, GPIOE, 1); \ No newline at end of file +const mcu_pin_obj_t pin_PE00 = PIN(4, 0, NO_ADC); +const mcu_pin_obj_t pin_PE01 = PIN(4, 1, NO_ADC); From a932d2c4a1bb4fa88667d3fc0bce054fc568b874 Mon Sep 17 00:00:00 2001 From: Hierophect Date: Fri, 6 Sep 2019 17:40:45 -0400 Subject: [PATCH 03/11] Minor fixes --- .../common-hal/digitalio/DigitalInOut.c | 32 +++++++++---------- ports/stm32f4/peripherals/stm32f4/pins.h | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/ports/stm32f4/common-hal/digitalio/DigitalInOut.c b/ports/stm32f4/common-hal/digitalio/DigitalInOut.c index 9410e7f415..ad30d33121 100644 --- a/ports/stm32f4/common-hal/digitalio/DigitalInOut.c +++ b/ports/stm32f4/common-hal/digitalio/DigitalInOut.c @@ -43,11 +43,11 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct( self->pin = pin; GPIO_InitTypeDef GPIO_InitStruct = {0}; - GPIO_InitStruct.Pin = (pin_mask(pin->number)); + GPIO_InitStruct.Pin = (pin_mask(self->pin->number)); GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(pin_port(pin->number), &GPIO_InitStruct); + HAL_GPIO_Init(pin_port(self->pin->number), &GPIO_InitStruct); return DIGITALINOUT_OK; } @@ -69,11 +69,11 @@ 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(pin->number); + GPIO_InitStruct.Pin = pin_mask(self->pin->number); GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(pin_port(pin->number), &GPIO_InitStruct); + HAL_GPIO_Init(pin_port(self->pin->number), &GPIO_InitStruct); common_hal_digitalio_digitalinout_set_pull(self, pull); } @@ -89,38 +89,38 @@ 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(pin->number), pin_mask(pin->number)) + return (LL_GPIO_GetPinMode(pin_port(self->pin->number), pin_mask(self->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(pin->number), 1 << self->pin->number, value); + HAL_GPIO_WritePin(pin_port(self->pin->number), 1 << self->pin->number, value); } bool common_hal_digitalio_digitalinout_get_value( digitalio_digitalinout_obj_t *self) { - return (LL_GPIO_GetPinMode(pin_port(pin->number), pin_mask(pin->number)) == LL_GPIO_MODE_INPUT) - ? HAL_GPIO_ReadPin(pin_port(pin->number), pin_mask(pin->number)) - : LL_GPIO_IsOutputPinSet(pin_port(pin->number), pin_mask(pin->number)); + 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)); } 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(pin->number); + GPIO_InitStruct.Pin = pin_mask(self->pin->number); GPIO_InitStruct.Mode = (drive_mode == DRIVE_MODE_OPEN_DRAIN ? GPIO_MODE_OUTPUT_OD : GPIO_MODE_OUTPUT_PP); GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(pin_port(pin->number), &GPIO_InitStruct); + HAL_GPIO_Init(pin_port(self->pin->number), &GPIO_InitStruct); } digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( digitalio_digitalinout_obj_t *self) { - return LL_GPIO_GetPinOutputType(pin_port(pin->number),pin_mask(pin->number)) + return LL_GPIO_GetPinOutputType(pin_port(self->pin->number), pin_mask(self->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(pin->number),pin_mask(pin->number),LL_GPIO_PULL_UP); + LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number),LL_GPIO_PULL_UP); break; case PULL_DOWN: - LL_GPIO_SetPinPull(pin_port(pin->number),pin_mask(pin->number),LL_GPIO_PULL_DOWN); + LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number),LL_GPIO_PULL_DOWN); break; case PULL_NONE: - LL_GPIO_SetPinPull(pin_port(pin->number),pin_mask(pin->number),LL_GPIO_PULL_NO); + LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->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(pin->number),pin_mask(pin->number))) { + switch (LL_GPIO_GetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number))) { case LL_GPIO_PULL_UP: return PULL_UP; case LL_GPIO_PULL_DOWN: diff --git a/ports/stm32f4/peripherals/stm32f4/pins.h b/ports/stm32f4/peripherals/stm32f4/pins.h index 0f3146c9b7..bb1e91c12d 100644 --- a/ports/stm32f4/peripherals/stm32f4/pins.h +++ b/ports/stm32f4/peripherals/stm32f4/pins.h @@ -66,7 +66,7 @@ extern const mp_obj_type_t mcu_pin_type; #define PIN(p_port, p_number, p_adc) \ { \ { &mcu_pin_type }, \ - .number = (((mask) << 4) | ((number) & 0x0F)), \ + .number = (((p_port) << 4) | ((p_number) & 0x0F)), \ p_adc \ } From a3ed5ec27b37b69b2d146e2a5dcbe6dff80c3d06 Mon Sep 17 00:00:00 2001 From: Hierophect Date: Fri, 6 Sep 2019 18:02:57 -0400 Subject: [PATCH 04/11] Add latest USB support --- ports/stm32f4/boards/stm32f411ve_discovery/mpconfigboard.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32f4/boards/stm32f411ve_discovery/mpconfigboard.mk b/ports/stm32f4/boards/stm32f411ve_discovery/mpconfigboard.mk index 725a12cf66..8a688b3d78 100644 --- a/ports/stm32f4/boards/stm32f411ve_discovery/mpconfigboard.mk +++ b/ports/stm32f4/boards/stm32f411ve_discovery/mpconfigboard.mk @@ -2,7 +2,7 @@ USB_VID = 0x239A USB_PID = 0x802A USB_PRODUCT = "STM32F411VE Discovery Board - CPy" USB_MANUFACTURER = "STMicroelectronics" -USB_CDC_AND_MSC_ONLY = 1 +USB_DEVICES = "CDC,MSC" INTERNAL_FLASH_FILESYSTEM = 1 LONGINT_IMPL = NONE From 4490fb196d81312ce8462e5548dd7ea2ff3d663d Mon Sep 17 00:00:00 2001 From: Hierophect Date: Fri, 6 Sep 2019 19:42:03 -0400 Subject: [PATCH 05/11] delete analogio for now --- ports/stm32f4/common-hal/analogio/AnalogIn.c | 112 ------------------ ports/stm32f4/common-hal/analogio/AnalogIn.h | 39 ------ ports/stm32f4/common-hal/analogio/AnalogOut.c | 48 -------- ports/stm32f4/common-hal/analogio/AnalogOut.h | 36 ------ ports/stm32f4/common-hal/analogio/__init__.c | 1 - 5 files changed, 236 deletions(-) delete mode 100644 ports/stm32f4/common-hal/analogio/AnalogIn.c delete mode 100644 ports/stm32f4/common-hal/analogio/AnalogIn.h delete mode 100644 ports/stm32f4/common-hal/analogio/AnalogOut.c delete mode 100644 ports/stm32f4/common-hal/analogio/AnalogOut.h delete mode 100644 ports/stm32f4/common-hal/analogio/__init__.c diff --git a/ports/stm32f4/common-hal/analogio/AnalogIn.c b/ports/stm32f4/common-hal/analogio/AnalogIn.c deleted file mode 100644 index f20802ac98..0000000000 --- a/ports/stm32f4/common-hal/analogio/AnalogIn.c +++ /dev/null @@ -1,112 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2019 Dan Halbert for Adafruit Industries - * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "common-hal/analogio/AnalogIn.h" -#include "py/runtime.h" -#include "supervisor/shared/translate.h" - -#include "nrfx_saadc.h" -#include "nrf_gpio.h" - -#define CHANNEL_NO 0 - -void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) { - if (pin->adc_channel == 0) - mp_raise_ValueError(translate("Pin does not have ADC capabilities")); - - nrf_gpio_cfg_default(pin->number); - - claim_pin(pin); - self->pin = pin; -} - -bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t *self) { - return self->pin == mp_const_none; -} - -void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) { - if (common_hal_analogio_analogin_deinited(self)) - return; - - nrf_gpio_cfg_default(self->pin->number); - - reset_pin_number(self->pin->number); - self->pin = mp_const_none; -} - -uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { - // Something else might have used the ADC in a different way, - // so we completely re-initialize it. - - nrf_saadc_value_t value; - - const nrf_saadc_channel_config_t config = { - .resistor_p = NRF_SAADC_RESISTOR_DISABLED, - .resistor_n = NRF_SAADC_RESISTOR_DISABLED, - .gain = NRF_SAADC_GAIN1_6, - .reference = NRF_SAADC_REFERENCE_INTERNAL, - .acq_time = NRF_SAADC_ACQTIME_3US, - .mode = NRF_SAADC_MODE_SINGLE_ENDED, - .burst = NRF_SAADC_BURST_DISABLED, - .pin_p = self->pin->adc_channel, - .pin_n = self->pin->adc_channel, - }; - - nrf_saadc_resolution_set(NRF_SAADC_RESOLUTION_14BIT); - nrf_saadc_oversample_set(NRF_SAADC_OVERSAMPLE_DISABLED); - nrf_saadc_enable(); - - for (uint32_t i = 0; i < NRF_SAADC_CHANNEL_COUNT; i++) - nrf_saadc_channel_input_set(i, NRF_SAADC_INPUT_DISABLED, NRF_SAADC_INPUT_DISABLED); - - nrf_saadc_channel_init(CHANNEL_NO, &config); - nrf_saadc_buffer_init(&value, 1); - - nrf_saadc_task_trigger(NRF_SAADC_TASK_START); - while (nrf_saadc_event_check(NRF_SAADC_EVENT_STARTED) == 0); - nrf_saadc_event_clear(NRF_SAADC_EVENT_STARTED); - - nrf_saadc_task_trigger(NRF_SAADC_TASK_SAMPLE); - while (nrf_saadc_event_check(NRF_SAADC_EVENT_END) == 0); - nrf_saadc_event_clear(NRF_SAADC_EVENT_END); - - nrf_saadc_task_trigger(NRF_SAADC_TASK_STOP); - while (nrf_saadc_event_check(NRF_SAADC_EVENT_STOPPED) == 0); - nrf_saadc_event_clear(NRF_SAADC_EVENT_STOPPED); - - nrf_saadc_disable(); - - if (value < 0) - value = 0; - - // Map value to from 14 to 16 bits - return (value << 2); -} - -float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) { - return 3.3f; -} diff --git a/ports/stm32f4/common-hal/analogio/AnalogIn.h b/ports/stm32f4/common-hal/analogio/AnalogIn.h deleted file mode 100644 index e0e95bad4c..0000000000 --- a/ports/stm32f4/common-hal/analogio/AnalogIn.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Scott Shawcroft - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_ANALOGIO_ANALOGIN_H -#define MICROPY_INCLUDED_NRF_COMMON_HAL_ANALOGIO_ANALOGIN_H - -#include "common-hal/microcontroller/Pin.h" - -#include "py/obj.h" - -typedef struct { - mp_obj_base_t base; - const mcu_pin_obj_t * pin; -} analogio_analogin_obj_t; - -#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_ANALOGIO_ANALOGIN_H diff --git a/ports/stm32f4/common-hal/analogio/AnalogOut.c b/ports/stm32f4/common-hal/analogio/AnalogOut.c deleted file mode 100644 index adafa15d5c..0000000000 --- a/ports/stm32f4/common-hal/analogio/AnalogOut.c +++ /dev/null @@ -1,48 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2018 Dan Halbert for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "shared-bindings/analogio/AnalogOut.h" - -#include -#include - -#include "py/mperrno.h" -#include "py/runtime.h" -#include "supervisor/shared/translate.h" - -void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, const mcu_pin_obj_t *pin) { - mp_raise_RuntimeError(translate("AnalogOut functionality not supported")); -} - -bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) { - return true; -} - -void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) { -} - -void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self, uint16_t value) { -} diff --git a/ports/stm32f4/common-hal/analogio/AnalogOut.h b/ports/stm32f4/common-hal/analogio/AnalogOut.h deleted file mode 100644 index 3244ee33b8..0000000000 --- a/ports/stm32f4/common-hal/analogio/AnalogOut.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Scott Shawcroft - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_ANALOGIO_ANALOGOUT_H -#define MICROPY_INCLUDED_NRF_COMMON_HAL_ANALOGIO_ANALOGOUT_H - -#include "py/obj.h" - -typedef struct { - mp_obj_base_t base; -} analogio_analogout_obj_t; - -#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_ANALOGIO_ANALOGOUT_H diff --git a/ports/stm32f4/common-hal/analogio/__init__.c b/ports/stm32f4/common-hal/analogio/__init__.c deleted file mode 100644 index eea58c77d6..0000000000 --- a/ports/stm32f4/common-hal/analogio/__init__.c +++ /dev/null @@ -1 +0,0 @@ -// No analogio module functions. From 642f4535f4893c39c483964a0834af58a94c52ce Mon Sep 17 00:00:00 2001 From: Hierophect Date: Fri, 6 Sep 2019 19:44:57 -0400 Subject: [PATCH 06/11] text fixes --- ports/stm32f4/peripherals/stm32f4/pins.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ports/stm32f4/peripherals/stm32f4/pins.h b/ports/stm32f4/peripherals/stm32f4/pins.h index bb1e91c12d..ee11cbe832 100644 --- a/ports/stm32f4/peripherals/stm32f4/pins.h +++ b/ports/stm32f4/peripherals/stm32f4/pins.h @@ -42,10 +42,6 @@ typedef struct { uint8_t adc_num_input; //(3)mask () } mcu_pin_obj_t; - // //uint8_t number; //(3)port,(5)pin - // uint8_t port_number; - // GPIO_TypeDef * port; - #define ADC_1 1 #define ADC_123 7 #define ADC_12 3 From beb40a62a7928e25a0241a03f4ec234d848c3a0c Mon Sep 17 00:00:00 2001 From: Hierophect Date: Sat, 7 Sep 2019 13:19:50 -0400 Subject: [PATCH 07/11] Fix missed shift replacement --- ports/stm32f4/common-hal/digitalio/DigitalInOut.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32f4/common-hal/digitalio/DigitalInOut.c b/ports/stm32f4/common-hal/digitalio/DigitalInOut.c index ad30d33121..e19988a7f7 100644 --- a/ports/stm32f4/common-hal/digitalio/DigitalInOut.c +++ b/ports/stm32f4/common-hal/digitalio/DigitalInOut.c @@ -95,7 +95,7 @@ digitalio_direction_t common_hal_digitalio_digitalinout_get_direction( void common_hal_digitalio_digitalinout_set_value( digitalio_digitalinout_obj_t *self, bool value) { - HAL_GPIO_WritePin(pin_port(self->pin->number), 1 << self->pin->number, value); + HAL_GPIO_WritePin(pin_port(self->pin->number), pin_mask(self->pin->number), value); } bool common_hal_digitalio_digitalinout_get_value( From 49b04f4b77513bedb45e002aad1bb98a05b66824 Mon Sep 17 00:00:00 2001 From: Hierophect Date: Tue, 10 Sep 2019 17:52:07 -0400 Subject: [PATCH 08/11] Minor text fix --- ports/stm32f4/common-hal/microcontroller/Pin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32f4/common-hal/microcontroller/Pin.c b/ports/stm32f4/common-hal/microcontroller/Pin.c index 735445ccba..2e0da9ccbe 100644 --- a/ports/stm32f4/common-hal/microcontroller/Pin.c +++ b/ports/stm32f4/common-hal/microcontroller/Pin.c @@ -83,7 +83,7 @@ bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) { return pin_number_is_free(pin->number); } -void* pin_port(uint8_t pin) { +GPIO_TypeDef * pin_port(uint8_t pin) { return ports[gpio_port_num(pin)]; } From 9f8c8c6504f1dfbf63b877499bd6597146fa29fc Mon Sep 17 00:00:00 2001 From: Hierophect Date: Wed, 11 Sep 2019 13:12:49 -0400 Subject: [PATCH 09/11] Compiler level bit packing --- .../common-hal/digitalio/DigitalInOut.c | 30 +++++++++---------- .../stm32f4/common-hal/microcontroller/Pin.c | 30 +++++++++---------- .../stm32f4/common-hal/microcontroller/Pin.h | 19 ++++-------- ports/stm32f4/peripherals/stm32f4/pins.h | 16 ++++++---- 4 files changed, 46 insertions(+), 49 deletions(-) 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 \ } From d888922db659241828e12ce0568c547aa56cc2e6 Mon Sep 17 00:00:00 2001 From: Hierophect Date: Wed, 11 Sep 2019 13:55:48 -0400 Subject: [PATCH 10/11] minor text fix --- ports/stm32f4/common-hal/microcontroller/Pin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32f4/common-hal/microcontroller/Pin.c b/ports/stm32f4/common-hal/microcontroller/Pin.c index 3a2dd01c58..e9693e18c6 100644 --- a/ports/stm32f4/common-hal/microcontroller/Pin.c +++ b/ports/stm32f4/common-hal/microcontroller/Pin.c @@ -54,7 +54,7 @@ void reset_all_pins(void) { // Mark pin as free and return it to a quiescent state. void reset_pin_number(uint8_t pin_port, uint8_t pin_number) { - if (pin_port == 0x00) { + if (pin_port == 0x0F) { return; } From 6e007f4e8c0a305a04f96a4730a73eec23c8f07d Mon Sep 17 00:00:00 2001 From: Hierophect Date: Wed, 11 Sep 2019 14:09:57 -0400 Subject: [PATCH 11/11] text fixes --- .../common-hal/digitalio/DigitalInOut.c | 26 +++++++++---------- .../stm32f4/common-hal/microcontroller/Pin.c | 1 - 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/ports/stm32f4/common-hal/digitalio/DigitalInOut.c b/ports/stm32f4/common-hal/digitalio/DigitalInOut.c index 6f2a0976e3..394000ba8e 100644 --- a/ports/stm32f4/common-hal/digitalio/DigitalInOut.c +++ b/ports/stm32f4/common-hal/digitalio/DigitalInOut.c @@ -43,7 +43,7 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct( self->pin = pin; GPIO_InitTypeDef GPIO_InitStruct = {0}; - GPIO_InitStruct.Pin = 1<pin->number; + GPIO_InitStruct.Pin = pin_mask(self->pin->number); GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; @@ -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 = 1<pin->number; + GPIO_InitStruct.Pin = pin_mask(self->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), 1<pin->number) + return (LL_GPIO_GetPinMode(pin_port(self->pin->number), pin_mask(self->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), 1<pin->number, value); + HAL_GPIO_WritePin(pin_port(self->pin->number), pin_mask(self->pin->number), value); } bool common_hal_digitalio_digitalinout_get_value( digitalio_digitalinout_obj_t *self) { - 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); + 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)); } 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 = 1<pin->number; + GPIO_InitStruct.Pin = pin_mask(self->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), 1<pin->number) + return LL_GPIO_GetPinOutputType(pin_port(self->pin->number), pin_mask(self->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), 1<pin->number,LL_GPIO_PULL_UP); + LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number),LL_GPIO_PULL_UP); break; case PULL_DOWN: - LL_GPIO_SetPinPull(pin_port(self->pin->number), 1<pin->number,LL_GPIO_PULL_DOWN); + LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number),LL_GPIO_PULL_DOWN); break; case PULL_NONE: - LL_GPIO_SetPinPull(pin_port(self->pin->number), 1<pin->number,LL_GPIO_PULL_NO); + LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->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), 1<pin->number)) { + switch (LL_GPIO_GetPinPull(pin_port(self->pin->number), pin_mask(self->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 e9693e18c6..69a6796a5e 100644 --- a/ports/stm32f4/common-hal/microcontroller/Pin.c +++ b/ports/stm32f4/common-hal/microcontroller/Pin.c @@ -86,7 +86,6 @@ GPIO_TypeDef * pin_port(uint8_t pin_port) { return ports[pin_port]; } -//TODO: replace with macro? uint16_t pin_mask(uint8_t pin_number) { return 1<