add pin claiming to devices

This commit is contained in:
Dan Halbert 2018-08-31 17:46:03 -04:00
parent 585597a252
commit 9ea809bef7
36 changed files with 140 additions and 100 deletions

2
main.c
View File

@ -451,7 +451,7 @@ void NORETURN __fatal_error(const char *msg) {
#ifndef NDEBUG
void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {
printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);
mp_printf(&mp_plat_print, "Assertion '%s' failed, at file %s:%d\n", expr, file, line);
__fatal_error("Assertion failed");
}
#endif

View File

@ -48,8 +48,8 @@ bool board_requests_safe_mode(void) {
gpio_set_pin_pull_mode(PIN_PA28, GPIO_PULL_DOWN);
bool safe_mode = gpio_get_pin_level(PIN_PA14) &&
gpio_get_pin_level(PIN_PA28);
reset_pin(PIN_PA14);
reset_pin(PIN_PA28);
reset_pin_number(PIN_PA14);
reset_pin_number(PIN_PA28);
return safe_mode;
}

View File

@ -48,8 +48,8 @@ bool board_requests_safe_mode(void) {
gpio_set_pin_pull_mode(PIN_PA28, GPIO_PULL_DOWN);
bool safe_mode = gpio_get_pin_level(PIN_PA14) &&
gpio_get_pin_level(PIN_PA28);
reset_pin(PIN_PA14);
reset_pin(PIN_PA28);
reset_pin_number(PIN_PA14);
reset_pin_number(PIN_PA28);
return safe_mode;
}

View File

@ -80,7 +80,7 @@ void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) {
if (common_hal_analogio_analogin_deinited(self)) {
return;
}
reset_pin(self->pin->number);
reset_pin_number(self->pin->number);
self->pin = mp_const_none;
}

View File

@ -113,7 +113,7 @@ void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
return;
}
dac_sync_disable_channel(&self->descriptor, self->channel);
reset_pin(PIN_PA02);
reset_pin_number(PIN_PA02);
// Only deinit the DAC on the SAMD51 if both outputs are free.
#ifdef SAMD51
if (common_hal_mcu_pin_is_free(&pin_PA02) && common_hal_mcu_pin_is_free(&pin_PA05)) {

View File

@ -212,11 +212,11 @@ void common_hal_audiobusio_i2sout_deinit(audiobusio_i2sout_obj_t* self) {
return;
}
reset_pin(self->bit_clock->number);
reset_pin_number(self->bit_clock->number);
self->bit_clock = mp_const_none;
reset_pin(self->word_select->number);
reset_pin_number(self->word_select->number);
self->word_select = mp_const_none;
reset_pin(self->data->number);
reset_pin_number(self->data->number);
self->data = mp_const_none;
}

View File

@ -235,8 +235,8 @@ void common_hal_audiobusio_pdmin_deinit(audiobusio_pdmin_obj_t* self) {
disconnect_gclk_from_peripheral(self->gclk, I2S_GCLK_ID_0 + self->clock_unit);
disable_clock_generator(self->gclk);
reset_pin(self->clock_pin->number);
reset_pin(self->data_pin->number);
reset_pin_number(self->clock_pin->number);
reset_pin_number(self->data_pin->number);
self->clock_pin = mp_const_none;
self->data_pin = mp_const_none;
}

View File

@ -248,10 +248,10 @@ void common_hal_audioio_audioout_deinit(audioio_audioout_obj_t* self) {
tc_set_enable(tc_insts[self->tc_index], false);
reset_pin(self->left_channel->number);
reset_pin_number(self->left_channel->number);
self->left_channel = mp_const_none;
#ifdef SAMD51
reset_pin(self->right_channel->number);
reset_pin_number(self->right_channel->number);
self->right_channel = mp_const_none;
#endif
}

View File

@ -96,8 +96,8 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
common_hal_mcu_delay_us(3);
if (!gpio_get_pin_level(sda->number) || !gpio_get_pin_level(scl->number)) {
reset_pin(sda->number);
reset_pin(scl->number);
reset_pin_number(sda->number);
reset_pin_number(scl->number);
mp_raise_RuntimeError(translate("SDA or SCL needs a pull up"));
}
gpio_set_pin_function(sda->number, sda_pinmux);
@ -107,8 +107,8 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
samd_peripherals_sercom_clock_init(sercom, sercom_index);
if (i2c_m_sync_init(&self->i2c_desc, sercom) != ERR_NONE) {
reset_pin(sda->number);
reset_pin(scl->number);
reset_pin_number(sda->number);
reset_pin_number(scl->number);
mp_raise_OSError(MP_EIO);
}
@ -116,8 +116,8 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
// Frequency must be set before the I2C device is enabled.
if (i2c_m_sync_set_baudrate(&self->i2c_desc, 0, frequency / 1000) != ERR_NONE) {
reset_pin(sda->number);
reset_pin(scl->number);
reset_pin_number(sda->number);
reset_pin_number(scl->number);
mp_raise_ValueError(translate("Unsupported baudrate"));
}
@ -144,8 +144,8 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
i2c_m_sync_disable(&self->i2c_desc);
i2c_m_sync_deinit(&self->i2c_desc);
reset_pin(self->sda_pin);
reset_pin(self->scl_pin);
reset_pin_number(self->sda_pin);
reset_pin_number(self->scl_pin);
self->sda_pin = NO_PIN;
self->scl_pin = NO_PIN;
}

View File

@ -36,7 +36,7 @@
typedef struct {
mp_obj_base_t base;
struct i2c_m_sync_desc i2c_desc;
volatile bool has_lock;
bool has_lock;
uint8_t scl_pin;
uint8_t sda_pin;
} busio_i2c_obj_t;

View File

@ -196,9 +196,9 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
}
spi_m_sync_disable(&self->spi_desc);
spi_m_sync_deinit(&self->spi_desc);
reset_pin(self->clock_pin);
reset_pin(self->MOSI_pin);
reset_pin(self->MISO_pin);
reset_pin_number(self->clock_pin);
reset_pin_number(self->MOSI_pin);
reset_pin_number(self->MISO_pin);
self->clock_pin = NO_PIN;
}

View File

@ -222,8 +222,8 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
struct usart_async_descriptor * const usart_desc_p = (struct usart_async_descriptor * const) &self->usart_desc;
usart_async_disable(usart_desc_p);
usart_async_deinit(usart_desc_p);
reset_pin(self->rx_pin);
reset_pin(self->tx_pin);
reset_pin_number(self->rx_pin);
reset_pin_number(self->tx_pin);
self->rx_pin = NO_PIN;
self->tx_pin = NO_PIN;
}

View File

@ -40,6 +40,8 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct(
digitalio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin) {
claim_pin(pin);
self->pin = pin;
self->output = false;
self->open_drain = false;
// Must set pull after setting direction.
gpio_set_pin_direction(pin->number, GPIO_DIRECTION_IN);
@ -55,7 +57,7 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self
if (common_hal_digitalio_digitalinout_deinited(self)) {
return;
}
reset_pin(self->pin->number);
reset_pin_number(self->pin->number);
self->pin = mp_const_none;
}
@ -83,7 +85,7 @@ void common_hal_digitalio_digitalinout_switch_to_output(
digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
digitalio_digitalinout_obj_t* self) {
return self->output? DIRECTION_OUTPUT : DIRECTION_INPUT;
return self->output ? DIRECTION_OUTPUT : DIRECTION_INPUT;
}
void common_hal_digitalio_digitalinout_set_value(

View File

@ -105,8 +105,8 @@ void common_hal_i2cslave_i2c_slave_deinit(i2cslave_i2c_slave_obj_t *self) {
self->sercom->I2CS.CTRLA.bit.ENABLE = 0;
reset_pin(self->sda_pin);
reset_pin(self->scl_pin);
reset_pin_number(self->sda_pin);
reset_pin_number(self->scl_pin);
self->sda_pin = NO_PIN;
self->scl_pin = NO_PIN;
}

View File

@ -91,23 +91,23 @@ void reset_all_pins(void) {
#endif
}
void reset_pin(uint8_t pin) {
if (pin >= PORT_BITS) {
void reset_pin_number(uint8_t pin_number) {
if (pin_number >= PORT_BITS) {
return;
}
#ifdef MICROPY_HW_NEOPIXEL
if (pin == MICROPY_HW_NEOPIXEL->number) {
if (pin_number == MICROPY_HW_NEOPIXEL->number) {
neopixel_in_use = false;
rgb_led_status_init();
return;
}
#endif
#ifdef MICROPY_HW_APA102_MOSI
if (pin == MICROPY_HW_APA102_MOSI->number ||
pin == MICROPY_HW_APA102_SCK->number) {
apa102_mosi_in_use = apa102_mosi_in_use && pin != MICROPY_HW_APA102_MOSI->number;
apa102_sck_in_use = apa102_sck_in_use && pin != MICROPY_HW_APA102_SCK->number;
if (pin_number == MICROPY_HW_APA102_MOSI->number ||
pin_number == MICROPY_HW_APA102_SCK->number) {
apa102_mosi_in_use = apa102_mosi_in_use && pin_number != MICROPY_HW_APA102_MOSI->number;
apa102_sck_in_use = apa102_sck_in_use && pin_number != MICROPY_HW_APA102_SCK->number;
if (!apa102_sck_in_use && !apa102_mosi_in_use) {
rgb_led_status_init();
}
@ -115,24 +115,24 @@ void reset_pin(uint8_t pin) {
}
#endif
if (pin == PIN_PA30
if (pin_number == PIN_PA30
#ifdef SAMD51
) {
gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_H);
gpio_set_pin_function(pin_number, GPIO_PIN_FUNCTION_H);
#endif
#ifdef SAMD21
|| pin == PIN_PA31) {
gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_G);
|| pin_number == PIN_PA31) {
gpio_set_pin_function(pin_number, GPIO_PIN_FUNCTION_G);
#endif
} else {
gpio_set_pin_direction(pin, GPIO_DIRECTION_OFF);
gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF);
gpio_set_pin_direction(pin_number, GPIO_DIRECTION_OFF);
gpio_set_pin_function(pin_number, GPIO_PIN_FUNCTION_OFF);
}
#ifdef SPEAKER_ENABLE_PIN
if (pin == SPEAKER_ENABLE_PIN->number) {
if (pin_number == SPEAKER_ENABLE_PIN->number) {
speaker_enable_in_use = false;
gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF);
gpio_set_pin_function(pin_number, GPIO_PIN_FUNCTION_OFF);
gpio_set_pin_direction(SPEAKER_ENABLE_PIN->number, GPIO_DIRECTION_OUT);
gpio_set_pin_level(SPEAKER_ENABLE_PIN->number, false);
}

View File

@ -40,9 +40,9 @@ extern bool apa102_mosi_in_use;
#endif
void reset_all_pins(void);
// reset_pin takes the pin number instead of the pointer so that objects don't
// 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(uint8_t pin);
void reset_pin_number(uint8_t pin_number);
void claim_pin(const mcu_pin_obj_t* pin);
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_MICROCONTROLLER_PIN_H

View File

@ -285,7 +285,7 @@ void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) {
}
}
}
reset_pin(self->pin->number);
reset_pin_number(self->pin->number);
self->pin = mp_const_none;
}

View File

@ -139,6 +139,8 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
turn_on_cpu_interrupt(self->channel);
claim_pin(pin);
// Set config will enable the EIC.
pulsein_set_config(self, true);
}
@ -152,7 +154,7 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) {
return;
}
turn_off_eic_channel(self->channel);
reset_pin(self->pin);
reset_pin_number(self->pin);
self->pin = NO_PIN;
}

View File

@ -73,6 +73,9 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode
((uint8_t) gpio_get_pin_level(self->pin_a) << 1) |
(uint8_t) gpio_get_pin_level(self->pin_b);
claim_pin(pin_a);
claim_pin(pin_b);
turn_on_eic_channel(self->eic_channel_a, EIC_CONFIG_SENSE0_BOTH_Val, EIC_HANDLER_INCREMENTAL_ENCODER);
turn_on_eic_channel(self->eic_channel_b, EIC_CONFIG_SENSE0_BOTH_Val, EIC_HANDLER_INCREMENTAL_ENCODER);
}
@ -87,9 +90,9 @@ void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_o
}
turn_off_eic_channel(self->eic_channel_a);
turn_off_eic_channel(self->eic_channel_b);
reset_pin(self->pin_a);
reset_pin_number(self->pin_a);
self->pin_a = NO_PIN;
reset_pin(self->pin_b);
reset_pin_number(self->pin_b);
self->pin_b = NO_PIN;
}

View File

@ -110,7 +110,7 @@ void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self) {
}
// We leave the clocks running because they may be in use by others.
reset_pin(self->config.pin);
reset_pin_number(self->config.pin);
self->config.pin = NO_PIN;
}

@ -1 +1 @@
Subproject commit 156279784cb3d22f7f8a8d93b4bb55e2d912a5f8
Subproject commit b099a172dfb6345a8dac30633df4f6267489c38e

View File

@ -52,8 +52,8 @@ bool board_requests_safe_mode(void) {
// gpio_set_pin_pull_mode(PIN_PA28, GPIO_PULL_DOWN);
// bool safe_mode = gpio_get_pin_level(PIN_PA14) &&
// gpio_get_pin_level(PIN_PA28);
// reset_pin(PIN_PA14);
// reset_pin(PIN_PA28);
// reset_pin_number(PIN_PA14);
// reset_pin_number(PIN_PA28);
// return safe_mode;
return false;

View File

@ -40,6 +40,7 @@ void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const
nrf_gpio_cfg_default(pin->number);
claim_pin(pin);
self->pin = pin;
}
@ -53,6 +54,7 @@ void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) {
nrf_gpio_cfg_default(self->pin->number);
reset_pin_number(self->pin->number);
self->pin = mp_const_none;
}

View File

@ -42,9 +42,7 @@ bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {
}
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) {
}

View File

@ -71,6 +71,11 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t *
config.frequency = NRF_TWIM_FREQ_250K;
}
self->scl_pin_number = scl->number;
self->sda_pin_number = sda->number;
claim_pin(sda);
claim_pin(scl);
nrfx_err_t err = nrfx_twim_init(&self->twim, &config, NULL, NULL);
// A soft reset doesn't uninit the driver so we might end up with a invalid state
@ -79,14 +84,15 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t *
err = nrfx_twim_init(&self->twim, &config, NULL, NULL);
}
if (err != NRFX_SUCCESS)
if (err != NRFX_SUCCESS) {
common_hal_busio_i2c_deinit(self);
mp_raise_OSError(MP_EIO);
}
self->inited = true;
}
bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) {
return !self->inited;
return self->sda_pin_number == NO_PIN;
}
void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
@ -95,7 +101,10 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
nrfx_twim_uninit(&self->twim);
self->inited = false;
reset_pin_number(self->sda_pin_number);
reset_pin_number(self->scl_pin_number);
self->sda_pin_number = NO_PIN;
self->scl_pin_number = NO_PIN;
}
// nrfx_twim_tx doesn't support 0-length data so we fall back to the hal API

View File

@ -34,8 +34,9 @@
typedef struct {
mp_obj_base_t base;
nrfx_twim_t twim;
bool inited;
bool has_lock;
uint8_t scl_pin_number;
uint8_t sda_pin_number;
} busio_i2c_obj_t;
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BUSIO_I2C_H

View File

@ -78,12 +78,24 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t *
config.frequency = NRF_SPIM_FREQ_8M;
config.sck_pin = clock->number;
self->clock_pin_number = clock->number;
claim_pin(clock);
if (mosi != (mcu_pin_obj_t*)&mp_const_none_obj)
if (mosi != (mcu_pin_obj_t*)&mp_const_none_obj) {
config.mosi_pin = mosi->number;
self->MOSI_pin_number = mosi->number;
claim_pin(mosi);
} else {
self->MOSI_pin_number = NO_PIN;
}
if (miso != (mcu_pin_obj_t*)&mp_const_none_obj)
if (miso != (mcu_pin_obj_t*)&mp_const_none_obj) {
config.miso_pin = miso->number;
self->MISO_pin_number = mosi->number;
claim_pin(miso);
} else {
self->MISO_pin_number = NO_PIN;
}
nrfx_err_t err = nrfx_spim_init(&self->spim, &config, NULL, NULL);
@ -93,14 +105,14 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t *
err = nrfx_spim_init(&self->spim, &config, NULL, NULL);
}
if (err != NRFX_SUCCESS)
if (err != NRFX_SUCCESS) {
common_hal_busio_spi_deinit(self);
mp_raise_OSError(MP_EIO);
self->inited = true;
}
}
bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) {
return !self->inited;
return self->clock_pin_number == NO_PIN;
}
void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
@ -109,7 +121,9 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
nrfx_spim_uninit(&self->spim);
self->inited = false;
reset_pin_number(self->clock_pin_number);
reset_pin_number(self->MOSI_pin_number);
reset_pin_number(self->MISO_pin_number);
}
bool common_hal_busio_spi_configure(busio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {

View File

@ -33,8 +33,10 @@
typedef struct {
mp_obj_base_t base;
nrfx_spim_t spim;
bool inited;
bool has_lock;
uint8_t clock_pin_number;
uint8_t MOSI_pin_number;
uint8_t MISO_pin_number;
} busio_spi_obj_t;
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BUSIO_SPI_H

View File

@ -32,7 +32,10 @@
digitalinout_result_t common_hal_digitalio_digitalinout_construct(
digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) {
claim_pin(pin);
self->pin = pin;
self->output = false;
self->open_drain = false;
nrf_gpio_cfg_input(pin->number, NRF_GPIO_PIN_NOPULL);
@ -49,11 +52,13 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self
}
nrf_gpio_cfg_default(self->pin->number);
reset_pin_number(self->pin->number);
self->pin = mp_const_none;
}
void common_hal_digitalio_digitalinout_switch_to_input(
digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) {
self->output = false;
nrf_gpio_cfg_input(self->pin->number, NRF_GPIO_PIN_NOPULL);
common_hal_digitalio_digitalinout_set_pull(self, pull);
}
@ -61,6 +66,7 @@ void common_hal_digitalio_digitalinout_switch_to_input(
void common_hal_digitalio_digitalinout_switch_to_output(
digitalio_digitalinout_obj_t *self, bool value,
digitalio_drive_mode_t drive_mode) {
self->output = true;
self->open_drain = (drive_mode == DRIVE_MODE_OPEN_DRAIN);
nrf_gpio_cfg_input(self->pin->number, NRF_GPIO_PIN_NOPULL);
@ -70,8 +76,7 @@ void common_hal_digitalio_digitalinout_switch_to_output(
digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
digitalio_digitalinout_obj_t *self) {
return (nrf_gpio_pin_dir_get(self->pin->number) == NRF_GPIO_PIN_DIR_OUTPUT)
? DIRECTION_OUTPUT : DIRECTION_INPUT;
return self->output ? DIRECTION_OUTPUT : DIRECTION_INPUT;
}
void common_hal_digitalio_digitalinout_set_value(
@ -87,8 +92,9 @@ void common_hal_digitalio_digitalinout_set_value(
bool common_hal_digitalio_digitalinout_get_value(
digitalio_digitalinout_obj_t *self) {
if (nrf_gpio_pin_dir_get(self->pin->number) == NRF_GPIO_PIN_DIR_INPUT) {
if (self->open_drain)
if (self->open_drain) {
return true;
}
return nrf_gpio_pin_read(self->pin->number);
}
@ -104,14 +110,16 @@ void common_hal_digitalio_digitalinout_set_drive_mode(
// True is implemented differently between modes so reset the value to make
// sure its correct for the new mode.
if (value)
if (value) {
common_hal_digitalio_digitalinout_set_value(self, value);
}
}
digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(
digitalio_digitalinout_obj_t *self) {
if (self->open_drain)
if (self->open_drain) {
return DRIVE_MODE_OPEN_DRAIN;
}
return DRIVE_MODE_PUSH_PULL;
}
@ -141,7 +149,7 @@ digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
// Changes pin to be a relative pin number in port.
NRF_GPIO_Type *reg = nrf_gpio_pin_port_decode(&pin);
if (nrf_gpio_pin_dir_get(pin) == NRF_GPIO_PIN_DIR_OUTPUT) {
if (nrf_gpio_pin_dir_get(self->pin->number) == NRF_GPIO_PIN_DIR_OUTPUT) {
mp_raise_AttributeError(translate("Cannot get pull while in output mode"));
return PULL_NONE;
}

View File

@ -32,6 +32,7 @@
typedef struct {
mp_obj_base_t base;
const mcu_pin_obj_t *pin;
bool output;
bool open_drain;
} digitalio_digitalinout_obj_t;

View File

@ -44,7 +44,7 @@ bool speaker_enable_in_use;
#endif
// Bit mask of claimed pins on each of up to two ports. nrf52832 has one port; nrf52840 has two.
STATIC bool claimed_pins[2];
STATIC uint32_t claimed_pins[2];
void reset_all_pins(void) {
claimed_pins[0] = 0;
@ -70,17 +70,16 @@ void reset_all_pins(void) {
}
// Mark pin as free and return it to a quiescent state.
void reset_pin(uint8_t pin) {
// Ignore out-of-bound pin numbers. NUMBER_OF_PINS is from nrf_gpio.h.
if (pin >= NUMBER_OF_PINS) {
void reset_pin_number(uint8_t pin_number) {
if (pin_number == NO_PIN) {
return;
}
// Clear claimed bit.
claimed_pins[nrf_pin_port(pin)] &= ~(1 << nrf_relative_pin_number(pin));
claimed_pins[nrf_pin_port(pin_number)] &= ~(1 << nrf_relative_pin_number(pin_number));
#ifdef MICROPY_HW_NEOPIXEL
if (pin == MICROPY_HW_NEOPIXEL->number) {
if (pin_number == MICROPY_HW_NEOPIXEL->number) {
neopixel_in_use = false;
rgb_led_status_init();
return;
@ -89,8 +88,8 @@ void reset_pin(uint8_t pin) {
#ifdef MICROPY_HW_APA102_MOSI
if (pin == MICROPY_HW_APA102_MOSI->number ||
pin == MICROPY_HW_APA102_SCK->number) {
apa102_mosi_in_use = apa102_mosi_in_use && pin != MICROPY_HW_APA102_MOSI->number;
apa102_sck_in_use = apa102_sck_in_use && pin != MICROPY_HW_APA102_SCK->number;
apa102_mosi_in_use = apa102_mosi_in_use && pin_number != MICROPY_HW_APA102_MOSI->number;
apa102_sck_in_use = apa102_sck_in_use && pin_number != MICROPY_HW_APA102_SCK->number;
if (!apa102_sck_in_use && !apa102_mosi_in_use) {
rgb_led_status_init();
}
@ -99,11 +98,11 @@ void reset_pin(uint8_t pin) {
#endif
#ifdef SPEAKER_ENABLE_PIN
if (pin == SPEAKER_ENABLE_PIN->number) {
if (pin_number == SPEAKER_ENABLE_PIN->number) {
speaker_enable_in_use = false;
common_hal_digitalio_digitalinout_switch_to_output(
nrf_gpio_pin_dir_set(pin, NRF_GPIO_PIN_DIR_OUTPUT);
nrf_gpio_pin_write(pin, false);
nrf_gpio_pin_dir_set(pin_number, NRF_GPIO_PIN_DIR_OUTPUT);
nrf_gpio_pin_write(pin_number, false);
}
#endif
}
@ -154,5 +153,5 @@ bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {
}
#endif
return !(claimed_pins[nrf_pin_port(pin->number)] & (nrf_relative_pin_number(pin->number)));
return !(claimed_pins[nrf_pin_port(pin->number)] & (1 << nrf_relative_pin_number(pin->number)));
}

View File

@ -40,9 +40,9 @@ extern bool apa102_mosi_in_use;
#endif
void reset_all_pins(void);
// reset_pin takes the pin number instead of the pointer so that objects don't
// 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(uint8_t pin);
void reset_pin_number(uint8_t pin);
void claim_pin(const mcu_pin_obj_t* pin);
// Lower 5 bits of a pin number are the pin number in a port.

View File

@ -27,8 +27,6 @@
#ifndef MICROPY_INCLUDED_NRF_PERIPHERALS_NRF52832_PINS_H
#define MICROPY_INCLUDED_NRF_PERIPHERALS_NRF52832_PINS_H
void reset_pin(uint8_t pin);
extern const mcu_pin_obj_t pin_P0_00;
extern const mcu_pin_obj_t pin_P0_01;
extern const mcu_pin_obj_t pin_P0_02;

View File

@ -27,8 +27,6 @@
#ifndef MICROPY_INCLUDED_NRF_PERIPHERALS_NRF52840_PINS_H
#define MICROPY_INCLUDED_NRF_PERIPHERALS_NRF52840_PINS_H
void reset_pin(uint8_t pin);
extern const mcu_pin_obj_t pin_P0_00;
extern const mcu_pin_obj_t pin_P0_01;
extern const mcu_pin_obj_t pin_P0_02;

View File

@ -55,6 +55,9 @@ extern const mp_obj_type_t mcu_pin_type;
.adc_channel = (p_adc_channel), \
}
// Use illegal pin value to mark unassigned pins.
#define NO_PIN 0xff
// Choose based on chip, but not specifically revision (e.g., not NRF52832_XXAA)
#ifdef NRF52832
#include "nrf52832/pins.h"

View File

@ -71,7 +71,7 @@ void rgb_led_status_init() {
#else
if (!common_hal_busio_spi_deinited(&status_apa102)) {
// Don't use spi_deinit because that leads to infinite
// recursion because reset_pin may call
// recursion because reset_pin_number may call
// rgb_led_status_init.
spi_m_sync_disable(&status_apa102.spi_desc);
}
@ -103,11 +103,11 @@ void rgb_led_status_init() {
void reset_status_led() {
#ifdef MICROPY_HW_NEOPIXEL
reset_pin(MICROPY_HW_NEOPIXEL->number);
reset_pin_number(MICROPY_HW_NEOPIXEL->number);
#endif
#if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)
reset_pin(MICROPY_HW_APA102_MOSI->number);
reset_pin(MICROPY_HW_APA102_SCK->number);
reset_pin_number(MICROPY_HW_APA102_MOSI->number);
reset_pin_number(MICROPY_HW_APA102_SCK->number);
#endif
}