Claim USB pins at startup to prevent overwrites

This commit is contained in:
Lucian Copeland 2020-07-22 14:11:00 -04:00
parent e232ec10ce
commit c1f731d62e
13 changed files with 35 additions and 32 deletions

View File

@ -29,7 +29,7 @@
#include "py/runtime.h"
#include "supervisor/shared/translate.h"
#include "common-hal/microcontroller/Pin.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "stm32f4xx_hal.h"
#include "stm32f4xx_ll_gpio.h"
@ -57,7 +57,7 @@ void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self,
} else {
mp_raise_ValueError(translate("Invalid ADC Unit value"));
}
claim_pin(pin);
common_hal_mcu_pin_claim(pin);
self->pin = pin;
}

View File

@ -86,7 +86,7 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
dac_on[self->dac_index] = true;
self->pin = pin;
claim_pin(pin);
common_hal_mcu_pin_claim(pin);
#endif
}

View File

@ -32,7 +32,7 @@
#include "shared-bindings/microcontroller/__init__.h"
#include "supervisor/shared/translate.h"
#include "common-hal/microcontroller/Pin.h"
#include "shared-bindings/microcontroller/Pin.h"
// I2C timing specs for the H7 and F7
// Configured for maximum possible clock settings for the family
@ -161,8 +161,8 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
if (HAL_I2C_Init(&(self->handle)) != HAL_OK) {
mp_raise_RuntimeError(translate("I2C Init Error"));
}
claim_pin(sda);
claim_pin(scl);
common_hal_mcu_pin_claim(sda);
common_hal_mcu_pin_claim(scl);
}
void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {

View File

@ -33,7 +33,7 @@
#include "shared-bindings/microcontroller/__init__.h"
#include "boards/board.h"
#include "supervisor/shared/translate.h"
#include "common-hal/microcontroller/Pin.h"
#include "shared-bindings/microcontroller/Pin.h"
// Note that any bugs introduced in this file can cause crashes at startup
// for chips using external SPI flash.
@ -233,12 +233,12 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
self->phase = 0;
self->bits = 8;
claim_pin(sck);
common_hal_mcu_pin_claim(sck);
if (self->mosi != NULL) {
claim_pin(mosi);
common_hal_mcu_pin_claim(mosi);
}
if (self->miso != NULL) {
claim_pin(miso);
common_hal_mcu_pin_claim(miso);
}
}

View File

@ -25,6 +25,7 @@
*/
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/busio/UART.h"
#include "mpconfigport.h"
@ -224,10 +225,10 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
mp_raise_ValueError(translate("UART Buffer allocation error"));
}
}
claim_pin(rx);
common_hal_mcu_pin_claim(rx);
}
if (self->tx != NULL) {
claim_pin(tx);
common_hal_mcu_pin_claim(tx);
}
self->baudrate = baudrate;
self->timeout_ms = timeout * 1000;

View File

@ -26,6 +26,7 @@
*/
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "py/runtime.h"
#include "supervisor/shared/translate.h"
@ -46,7 +47,7 @@ void common_hal_digitalio_digitalinout_never_reset(
digitalinout_result_t common_hal_digitalio_digitalinout_construct(
digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) {
claim_pin(pin);
common_hal_mcu_pin_claim(pin);
self->pin = pin;
GPIO_InitTypeDef GPIO_InitStruct = {0};

View File

@ -101,25 +101,15 @@ void common_hal_reset_pin(const mcu_pin_obj_t* pin) {
reset_pin_number(pin->port, pin->number);
}
void claim_pin(const mcu_pin_obj_t* pin) {
void claim_pin(uint8_t pin_port, uint8_t pin_number) {
// Set bit in claimed_pins bitmask.
claimed_pins[pin->port] |= 1<<pin->number;
#ifdef MICROPY_HW_NEOPIXEL
if (pin == MICROPY_HW_NEOPIXEL) {
neopixel_in_use = true;
}
#endif
claimed_pins[pin_port] |= 1<<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_resettable(uint8_t pin_port, uint8_t pin_number) {
return !(never_reset_pins[pin_port] & 1<<pin_number);
}
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {
#ifdef MICROPY_HW_NEOPIXEL
if (pin == MICROPY_HW_NEOPIXEL) {
@ -143,7 +133,12 @@ uint8_t common_hal_mcu_pin_number(const mcu_pin_obj_t* pin) {
}
void common_hal_mcu_pin_claim(const mcu_pin_obj_t* pin) {
claim_pin(pin);
claim_pin(pin->port, pin->number);
#ifdef MICROPY_HW_NEOPIXEL
if (pin == MICROPY_HW_NEOPIXEL) {
neopixel_in_use = true;
}
#endif
}
void common_hal_mcu_pin_reset_number(uint8_t pin_no) {

View File

@ -43,9 +43,8 @@ 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_number);
void claim_pin(const mcu_pin_obj_t* pin);
void claim_pin(uint8_t pin_port, uint8_t pin_number);
bool pin_number_is_free(uint8_t pin_port, uint8_t pin_number);
bool pin_number_is_resettable(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);

View File

@ -33,7 +33,7 @@
#include "shared-bindings/microcontroller/__init__.h"
#include STM32_HAL_H
#include "common-hal/microcontroller/Pin.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "timers.h"

View File

@ -31,6 +31,7 @@
#include "py/gc.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/pulseio/PulseIn.h"
#include "timers.h"
@ -174,7 +175,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu
// Interrupt starts immediately
assign_EXTI_Interrupt(self, pin->number);
HAL_NVIC_EnableIRQ(self->irq);
claim_pin(pin);
common_hal_mcu_pin_claim(pin);
}
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) {

View File

@ -36,7 +36,7 @@
#include "supervisor/shared/translate.h"
#include STM32_HAL_H
#include "common-hal/microcontroller/Pin.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "timers.h"
// A single timer is shared amongst all PulseOut objects under the assumption that

View File

@ -86,6 +86,8 @@ void init_usb_hardware(void) {
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
never_reset_pin_number(0, 11);
never_reset_pin_number(0, 12);
claim_pin(0, 11);
claim_pin(0, 12);
/* Configure VBUS Pin */
#if !(BOARD_NO_VBUS_SENSE)
@ -94,6 +96,7 @@ void init_usb_hardware(void) {
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
never_reset_pin_number(0, 9);
claim_pin(0, 9);
#endif
/* This for ID line debug */
@ -108,6 +111,7 @@ void init_usb_hardware(void) {
#endif
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
never_reset_pin_number(0, 10);
claim_pin(0, 10);
#ifdef STM32F412Zx
/* Configure POWER_SWITCH IO pin (F412 ONLY)*/
@ -116,6 +120,7 @@ void init_usb_hardware(void) {
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
never_reset_pin_number(0, 8);
claim_pin(0, 8);
#endif
#if CPY_STM32H7

View File

@ -31,6 +31,7 @@
#include "py/runtime.h"
#include "py/mphal.h"
#include "shared-bindings/touchio/TouchIn.h"
#include "shared-bindings/microcontroller/Pin.h"
// This is a capacitive touch sensing routine using a single digital
// pin. The pin should be connected to the sensing pad, and to ground
@ -67,7 +68,7 @@ static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
}
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, const mcu_pin_obj_t *pin) {
claim_pin(pin);
common_hal_mcu_pin_claim(pin);
self->digitalinout = m_new_obj(digitalio_digitalinout_obj_t);
self->digitalinout->base.type = &digitalio_digitalinout_type;