diff options
Diffstat (limited to 'ports/stm/common-hal')
30 files changed, 450 insertions, 457 deletions
diff --git a/ports/stm/common-hal/analogio/AnalogIn.c b/ports/stm/common-hal/analogio/AnalogIn.c index 588d687ee..35010ac7c 100644 --- a/ports/stm/common-hal/analogio/AnalogIn.c +++ b/ports/stm/common-hal/analogio/AnalogIn.c @@ -36,8 +36,8 @@ #include "stm32f4xx_ll_adc.h" #include "stm32f4xx_ll_bus.h" -void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self, - const mcu_pin_obj_t *pin) { +void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, + const mcu_pin_obj_t *pin) { // No ADC function on pin if (pin->adc_unit == 0x00) { @@ -76,9 +76,9 @@ void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) { 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. - ADC_TypeDef * ADCx; + ADC_TypeDef *ADCx; - if(self->pin->adc_unit & 0x01) { + if (self->pin->adc_unit & 0x01) { ADCx = ADC1; } else if (self->pin->adc_unit == 0x04) { #ifdef ADC3 @@ -89,9 +89,9 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { } LL_GPIO_SetPinMode(pin_port(self->pin->port), (uint32_t)pin_mask(self->pin->number), LL_GPIO_MODE_ANALOG); - //LL_GPIO_PIN_0 + // LL_GPIO_PIN_0 - //HAL Implementation + // HAL Implementation ADC_HandleTypeDef AdcHandle; ADC_ChannelConfTypeDef sConfig; @@ -110,9 +110,9 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; HAL_ADC_Init(&AdcHandle); - sConfig.Channel = (uint32_t)self->pin->adc_channel; //ADC_CHANNEL_0 <-normal iteration, not mask + sConfig.Channel = (uint32_t)self->pin->adc_channel; // ADC_CHANNEL_0 <-normal iteration, not mask sConfig.Rank = 1; - sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES; //Taken from micropython + sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES; // Taken from micropython HAL_ADC_ConfigChannel(&AdcHandle, &sConfig); HAL_ADC_Start(&AdcHandle); diff --git a/ports/stm/common-hal/analogio/AnalogIn.h b/ports/stm/common-hal/analogio/AnalogIn.h index 910092897..c8e3e0868 100644 --- a/ports/stm/common-hal/analogio/AnalogIn.h +++ b/ports/stm/common-hal/analogio/AnalogIn.h @@ -34,7 +34,7 @@ typedef struct { mp_obj_base_t base; - const mcu_pin_obj_t * pin; + const mcu_pin_obj_t *pin; } analogio_analogin_obj_t; static inline uint8_t stm32_adc_units(uint8_t adc_packed) { diff --git a/ports/stm/common-hal/analogio/AnalogOut.c b/ports/stm/common-hal/analogio/AnalogOut.c index 69c1f3e04..1a3817a35 100644 --- a/ports/stm/common-hal/analogio/AnalogOut.c +++ b/ports/stm/common-hal/analogio/AnalogOut.c @@ -39,15 +39,15 @@ #include "stm32f4xx_hal.h" -//DAC is shared between both channels. +// DAC is shared between both channels. #if HAS_DAC DAC_HandleTypeDef handle; #endif STATIC bool dac_on[2]; -void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, - const mcu_pin_obj_t *pin) { +void common_hal_analogio_analogout_construct(analogio_analogout_obj_t *self, + const mcu_pin_obj_t *pin) { #if !(HAS_DAC) mp_raise_ValueError(translate("No DAC on chip")); #else @@ -61,17 +61,16 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, mp_raise_ValueError(translate("Invalid DAC pin supplied")); } - //Only init if the shared DAC is empty or reset + // Only init if the shared DAC is empty or reset if (handle.Instance == NULL || handle.State == HAL_DAC_STATE_RESET) { __HAL_RCC_DAC_CLK_ENABLE(); handle.Instance = DAC; - if (HAL_DAC_Init(&handle) != HAL_OK) - { + if (HAL_DAC_Init(&handle) != HAL_OK) { mp_raise_ValueError(translate("DAC Device Init Error")); } } - //init channel specific pin + // init channel specific pin GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Pin = pin_mask(pin->number); GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; @@ -100,8 +99,8 @@ void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) { self->pin = NULL; dac_on[self->dac_index] = false; - //turn off the DAC if both channels are off - if(dac_on[0] == false && dac_on[1] == false) { + // turn off the DAC if both channels are off + if (dac_on[0] == false && dac_on[1] == false) { __HAL_RCC_DAC_CLK_DISABLE(); HAL_DAC_DeInit(&handle); } @@ -109,7 +108,7 @@ 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) { + uint16_t value) { #if HAS_DAC HAL_DAC_SetValue(&handle, self->channel, DAC_ALIGN_12B_R, value >> 4); HAL_DAC_Start(&handle, self->channel); diff --git a/ports/stm/common-hal/analogio/AnalogOut.h b/ports/stm/common-hal/analogio/AnalogOut.h index 46312b460..3c208a611 100644 --- a/ports/stm/common-hal/analogio/AnalogOut.h +++ b/ports/stm/common-hal/analogio/AnalogOut.h @@ -36,12 +36,12 @@ typedef struct { mp_obj_base_t base; -#if HAS_DAC + #if HAS_DAC DAC_ChannelConfTypeDef ch_handle; -#endif - const mcu_pin_obj_t * pin; + #endif + const mcu_pin_obj_t *pin; uint8_t channel; - uint8_t dac_index:1; + uint8_t dac_index : 1; } analogio_analogout_obj_t; void analogout_reset(void); diff --git a/ports/stm/common-hal/busio/I2C.c b/ports/stm/common-hal/busio/I2C.c index 7726b5e87..db6d86c34 100644 --- a/ports/stm/common-hal/busio/I2C.c +++ b/ports/stm/common-hal/busio/I2C.c @@ -61,11 +61,11 @@ STATIC bool never_reset_i2c[MAX_I2C]; #define ALL_CLOCKS 0xFF STATIC void i2c_clock_enable(uint8_t mask); STATIC void i2c_clock_disable(uint8_t mask); -STATIC void i2c_assign_irq(busio_i2c_obj_t *self, I2C_TypeDef * I2Cx); +STATIC void i2c_assign_irq(busio_i2c_obj_t *self, I2C_TypeDef *I2Cx); void i2c_reset(void) { uint16_t never_reset_mask = 0x00; - for(int i = 0; i < MAX_I2C; i++) { + for (int i = 0; i < MAX_I2C; i++) { if (!never_reset_i2c[i]) { reserved_i2c[i] = false; } else { @@ -76,10 +76,10 @@ void i2c_reset(void) { } void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, - const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t frequency, uint32_t timeout) { + const mcu_pin_obj_t *scl, const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout) { // Match pins to I2C objects - I2C_TypeDef * I2Cx; + I2C_TypeDef *I2Cx; uint8_t sda_len = MP_ARRAY_SIZE(mcu_i2c_sda_list); uint8_t scl_len = MP_ARRAY_SIZE(mcu_i2c_scl_list); bool i2c_taken = false; @@ -107,7 +107,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, } // Handle typedef selection, errors - if (self->sda != NULL && self->scl != NULL ) { + if (self->sda != NULL && self->scl != NULL) { I2Cx = mcu_i2c_banks[self->sda->periph_index - 1]; } else { if (i2c_taken) { @@ -171,8 +171,8 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, self->frame_in_prog = false; - //start the receive interrupt chain - HAL_NVIC_DisableIRQ(self->irq); //prevent handle lock contention + // start the receive interrupt chain + HAL_NVIC_DisableIRQ(self->irq); // prevent handle lock contention HAL_NVIC_SetPriority(self->irq, 1, 0); HAL_NVIC_EnableIRQ(self->irq); } @@ -215,15 +215,15 @@ bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) { bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) { bool grabbed_lock = false; - //Critical section code that may be required at some point. + // Critical section code that may be required at some point. // uint32_t store_primask = __get_PRIMASK(); // __disable_irq(); // __DMB(); - if (!self->has_lock) { - grabbed_lock = true; - self->has_lock = true; - } + if (!self->has_lock) { + grabbed_lock = true; + self->has_lock = true; + } // __DMB(); // __set_PRIMASK(store_primask); @@ -240,7 +240,7 @@ void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) { } uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr, - const uint8_t *data, size_t len, bool transmit_stop_bit) { + const uint8_t *data, size_t len, bool transmit_stop_bit) { HAL_StatusTypeDef result; if (!transmit_stop_bit) { uint32_t xfer_opt; @@ -251,31 +251,29 @@ uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr, xfer_opt = I2C_NEXT_FRAME; } result = HAL_I2C_Master_Seq_Transmit_IT(&(self->handle), - (uint16_t)(addr << 1), (uint8_t *)data, - (uint16_t)len, xfer_opt); - while (HAL_I2C_GetState(&(self->handle)) != HAL_I2C_STATE_READY) - { + (uint16_t)(addr << 1), (uint8_t *)data, + (uint16_t)len, xfer_opt); + while (HAL_I2C_GetState(&(self->handle)) != HAL_I2C_STATE_READY) { RUN_BACKGROUND_TASKS; } self->frame_in_prog = true; } else { result = HAL_I2C_Master_Transmit(&(self->handle), (uint16_t)(addr << 1), - (uint8_t *)data, (uint16_t)len, 500); + (uint8_t *)data, (uint16_t)len, 500); } return result == HAL_OK ? 0 : MP_EIO; } uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr, - uint8_t *data, size_t len) { + uint8_t *data, size_t len) { if (!self->frame_in_prog) { - return HAL_I2C_Master_Receive(&(self->handle), (uint16_t)(addr<<1), data, (uint16_t)len, 500) - == HAL_OK ? 0 : MP_EIO; + return HAL_I2C_Master_Receive(&(self->handle), (uint16_t)(addr << 1), data, (uint16_t)len, 500) + == HAL_OK ? 0 : MP_EIO; } else { HAL_StatusTypeDef result = HAL_I2C_Master_Seq_Receive_IT(&(self->handle), - (uint16_t)(addr << 1), (uint8_t *)data, - (uint16_t)len, I2C_LAST_FRAME); - while (HAL_I2C_GetState(&(self->handle)) != HAL_I2C_STATE_READY) - { + (uint16_t)(addr << 1), (uint8_t *)data, + (uint16_t)len, I2C_LAST_FRAME); + while (HAL_I2C_GetState(&(self->handle)) != HAL_I2C_STATE_READY) { RUN_BACKGROUND_TASKS; } self->frame_in_prog = false; @@ -338,7 +336,7 @@ STATIC void i2c_clock_disable(uint8_t mask) { #endif } -STATIC void i2c_assign_irq(busio_i2c_obj_t *self, I2C_TypeDef * I2Cx) { +STATIC void i2c_assign_irq(busio_i2c_obj_t *self, I2C_TypeDef *I2Cx) { #ifdef I2C1 if (I2Cx == I2C1) { self->irq = I2C1_EV_IRQn; @@ -362,8 +360,8 @@ STATIC void i2c_assign_irq(busio_i2c_obj_t *self, I2C_TypeDef * I2Cx) { } STATIC void call_hal_irq(int i2c_num) { - //Create casted context pointer - busio_i2c_obj_t * context = (busio_i2c_obj_t*)MP_STATE_PORT(cpy_i2c_obj_all)[i2c_num - 1]; + // Create casted context pointer + busio_i2c_obj_t *context = (busio_i2c_obj_t *)MP_STATE_PORT(cpy_i2c_obj_all)[i2c_num - 1]; if (context != NULL) { HAL_NVIC_ClearPendingIRQ(context->irq); HAL_I2C_EV_IRQHandler(&context->handle); diff --git a/ports/stm/common-hal/busio/SPI.c b/ports/stm/common-hal/busio/SPI.c index 20ee0f6e1..2f26a01cb 100644 --- a/ports/stm/common-hal/busio/SPI.c +++ b/ports/stm/common-hal/busio/SPI.c @@ -39,7 +39,7 @@ // Note that any bugs introduced in this file can cause crashes at startup // for chips using external SPI flash. -//arrays use 0 based numbering: SPI1 is stored at index 0 +// arrays use 0 based numbering: SPI1 is stored at index 0 #define MAX_SPI 6 STATIC bool reserved_spi[MAX_SPI]; @@ -49,28 +49,32 @@ STATIC bool never_reset_spi[MAX_SPI]; STATIC void spi_clock_enable(uint8_t mask); STATIC void spi_clock_disable(uint8_t mask); -STATIC uint32_t get_busclock(SPI_TypeDef * instance) { +STATIC uint32_t get_busclock(SPI_TypeDef *instance) { #if (CPY_STM32H7) - if (instance == SPI1 || instance == SPI2 || instance == SPI3) { - return HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SPI123); - } else if (instance == SPI4 || instance == SPI5) { - return HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SPI45); - } else { - return HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SPI6); - } + if (instance == SPI1 || instance == SPI2 || instance == SPI3) { + return HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SPI123); + } else if (instance == SPI4 || instance == SPI5) { + return HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SPI45); + } else { + return HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SPI6); + } #elif (CPY_STM32F4 || CPY_STM32F7) - //SPI2 and 3 are on PCLK1, if they exist. - #ifdef SPI2 - if (instance == SPI2) return HAL_RCC_GetPCLK1Freq(); - #endif - #ifdef SPI3 - if (instance == SPI3) return HAL_RCC_GetPCLK1Freq(); - #endif - return HAL_RCC_GetPCLK2Freq(); + // SPI2 and 3 are on PCLK1, if they exist. + #ifdef SPI2 + if (instance == SPI2) { + return HAL_RCC_GetPCLK1Freq(); + } + #endif + #ifdef SPI3 + if (instance == SPI3) { + return HAL_RCC_GetPCLK1Freq(); + } + #endif + return HAL_RCC_GetPCLK2Freq(); #endif } -STATIC uint32_t stm32_baud_to_spi_div(uint32_t baudrate, uint16_t * prescaler, uint32_t busclock) { +STATIC uint32_t stm32_baud_to_spi_div(uint32_t baudrate, uint16_t *prescaler, uint32_t busclock) { static const uint32_t baud_map[8][2] = { {2,SPI_BAUDRATEPRESCALER_2}, {4,SPI_BAUDRATEPRESCALER_4}, @@ -85,13 +89,13 @@ STATIC uint32_t stm32_baud_to_spi_div(uint32_t baudrate, uint16_t * prescaler, u uint16_t divisor; do { divisor = baud_map[i][0]; - if (baudrate >= (busclock/divisor)) { + if (baudrate >= (busclock / divisor)) { *prescaler = divisor; return baud_map[i][1]; } i++; } while (divisor != 256); - //only gets here if requested baud is lower than minimum + // only gets here if requested baud is lower than minimum *prescaler = 256; return SPI_BAUDRATEPRESCALER_256; } @@ -109,18 +113,18 @@ void spi_reset(void) { } STATIC const mcu_periph_obj_t *find_pin_function(const mcu_periph_obj_t *table, size_t sz, const mcu_pin_obj_t *pin, int periph_index) { - for(size_t i = 0; i<sz; i++, table++) { - if(periph_index == table->periph_index && pin == table->pin ) { + for (size_t i = 0; i < sz; i++, table++) { + if (periph_index == table->periph_index && pin == table->pin) { return table; } } return NULL; } -//match pins to SPI objects +// match pins to SPI objects STATIC int check_pins(busio_spi_obj_t *self, - const mcu_pin_obj_t * sck, const mcu_pin_obj_t * mosi, - const mcu_pin_obj_t * miso) { + const mcu_pin_obj_t *sck, const mcu_pin_obj_t *mosi, + const mcu_pin_obj_t *miso) { bool spi_taken = false; uint8_t sck_len = MP_ARRAY_SIZE(mcu_spi_sck_list); @@ -146,7 +150,7 @@ STATIC int check_pins(busio_spi_obj_t *self, continue; } - if (reserved_spi[periph_index-1]) { + if (reserved_spi[periph_index - 1]) { spi_taken = true; continue; } @@ -166,13 +170,13 @@ STATIC int check_pins(busio_spi_obj_t *self, } void common_hal_busio_spi_construct(busio_spi_obj_t *self, - const mcu_pin_obj_t * sck, const mcu_pin_obj_t * mosi, - const mcu_pin_obj_t * miso) { + const mcu_pin_obj_t *sck, const mcu_pin_obj_t *mosi, + const mcu_pin_obj_t *miso) { int periph_index = check_pins(self, sck, mosi, miso); - SPI_TypeDef * SPIx = mcu_spi_banks[periph_index - 1]; + SPI_TypeDef *SPIx = mcu_spi_banks[periph_index - 1]; - //Start GPIO for each pin + // Start GPIO for each pin GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Pin = pin_mask(sck->number); GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; @@ -215,8 +219,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self, self->handle.Init.TIMode = SPI_TIMODE_DISABLE; self->handle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; self->handle.Init.CRCPolynomial = 10; - if (HAL_SPI_Init(&self->handle) != HAL_OK) - { + if (HAL_SPI_Init(&self->handle) != HAL_OK) { mp_raise_ValueError(translate("SPI Init Error")); } self->baudrate = (get_busclock(SPIx) / 16); @@ -254,7 +257,7 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) { if (common_hal_busio_spi_deinited(self)) { return; } - spi_clock_disable(1<<(self->sck->periph_index - 1)); + spi_clock_disable(1 << (self->sck->periph_index - 1)); reserved_spi[self->sck->periph_index - 1] = false; never_reset_spi[self->sck->periph_index - 1] = false; @@ -271,14 +274,14 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) { } bool common_hal_busio_spi_configure(busio_spi_obj_t *self, - uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) { - //This resets the SPI, so check before updating it redundantly - if (baudrate == self->baudrate && polarity== self->polarity + uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) { + // This resets the SPI, so check before updating it redundantly + if (baudrate == self->baudrate && polarity == self->polarity && phase == self->phase && bits == self->bits) { return true; } - //Deinit SPI + // Deinit SPI HAL_SPI_DeInit(&self->handle); self->handle.Init.DataSize = (bits == 16) ? SPI_DATASIZE_16BIT : SPI_DATASIZE_8BIT; @@ -286,10 +289,9 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self, self->handle.Init.CLKPhase = (phase) ? SPI_PHASE_2EDGE : SPI_PHASE_1EDGE; self->handle.Init.BaudRatePrescaler = stm32_baud_to_spi_div(baudrate, &self->prescaler, - get_busclock(self->handle.Instance)); + get_busclock(self->handle.Instance)); - if (HAL_SPI_Init(&self->handle) != HAL_OK) - { + if (HAL_SPI_Init(&self->handle) != HAL_OK) { mp_raise_ValueError(translate("SPI Re-initialization error")); } @@ -303,7 +305,7 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self, bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) { bool grabbed_lock = false; - //Critical section code that may be required at some point. + // Critical section code that may be required at some point. // uint32_t store_primask = __get_PRIMASK(); // __disable_irq(); // __DMB(); @@ -328,7 +330,7 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) { } bool common_hal_busio_spi_write(busio_spi_obj_t *self, - const uint8_t *data, size_t len) { + const uint8_t *data, size_t len) { if (self->mosi == NULL) { mp_raise_ValueError(translate("No MOSI Pin")); } @@ -337,7 +339,7 @@ bool common_hal_busio_spi_write(busio_spi_obj_t *self, } bool common_hal_busio_spi_read(busio_spi_obj_t *self, - uint8_t *data, size_t len, uint8_t write_value) { + uint8_t *data, size_t len, uint8_t write_value) { if (self->miso == NULL) { mp_raise_ValueError(translate("No MISO Pin")); } @@ -352,26 +354,26 @@ bool common_hal_busio_spi_read(busio_spi_obj_t *self, } bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, - const uint8_t *data_out, uint8_t *data_in, size_t len) { + const uint8_t *data_out, uint8_t *data_in, size_t len) { if (self->miso == NULL || self->mosi == NULL) { mp_raise_ValueError(translate("Missing MISO or MOSI Pin")); } HAL_StatusTypeDef result = HAL_SPI_TransmitReceive(&self->handle, - (uint8_t *) data_out, data_in, (uint16_t)len,HAL_MAX_DELAY); + (uint8_t *)data_out, data_in, (uint16_t)len,HAL_MAX_DELAY); return result == HAL_OK; } -uint32_t common_hal_busio_spi_get_frequency(busio_spi_obj_t* self) { - //returns actual frequency - uint32_t result = HAL_RCC_GetPCLK2Freq()/self->prescaler; +uint32_t common_hal_busio_spi_get_frequency(busio_spi_obj_t *self) { + // returns actual frequency + uint32_t result = HAL_RCC_GetPCLK2Freq() / self->prescaler; return result; } -uint8_t common_hal_busio_spi_get_phase(busio_spi_obj_t* self) { +uint8_t common_hal_busio_spi_get_phase(busio_spi_obj_t *self) { return self->phase; } -uint8_t common_hal_busio_spi_get_polarity(busio_spi_obj_t* self) { +uint8_t common_hal_busio_spi_get_polarity(busio_spi_obj_t *self) { return self->polarity; } diff --git a/ports/stm/common-hal/busio/UART.c b/ports/stm/common-hal/busio/UART.c index b4794a31b..ecaee84d3 100644 --- a/ports/stm/common-hal/busio/UART.c +++ b/ports/stm/common-hal/busio/UART.c @@ -39,19 +39,19 @@ #define ALL_UARTS 0xFFFF -//arrays use 0 based numbering: UART1 is stored at index 0 +// arrays use 0 based numbering: UART1 is stored at index 0 STATIC bool reserved_uart[MAX_UART]; STATIC bool never_reset_uart[MAX_UART]; -int errflag; //Used to restart read halts +int errflag; // Used to restart read halts STATIC void uart_clock_enable(uint16_t mask); STATIC void uart_clock_disable(uint16_t mask); -STATIC void uart_assign_irq(busio_uart_obj_t* self, USART_TypeDef* USARTx); +STATIC void uart_assign_irq(busio_uart_obj_t *self, USART_TypeDef *USARTx); -STATIC USART_TypeDef * assign_uart_or_throw(busio_uart_obj_t* self, bool pin_eval, - int periph_index, bool uart_taken) { +STATIC USART_TypeDef *assign_uart_or_throw(busio_uart_obj_t *self, bool pin_eval, + int periph_index, bool uart_taken) { if (pin_eval) { - //assign a root pointer pointer for IRQ + // assign a root pointer pointer for IRQ MP_STATE_PORT(cpy_uart_obj_all)[periph_index] = self; return mcu_uart_banks[periph_index]; } else { @@ -77,40 +77,40 @@ void uart_reset(void) { } void common_hal_busio_uart_construct(busio_uart_obj_t *self, - const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, - const mcu_pin_obj_t * rts, const mcu_pin_obj_t * cts, - const mcu_pin_obj_t * rs485_dir, bool rs485_invert, + const mcu_pin_obj_t *tx, const mcu_pin_obj_t *rx, + const mcu_pin_obj_t *rts, const mcu_pin_obj_t *cts, + const mcu_pin_obj_t *rs485_dir, bool rs485_invert, uint32_t baudrate, uint8_t bits, busio_uart_parity_t parity, uint8_t stop, - mp_float_t timeout, uint16_t receiver_buffer_size, byte* receiver_buffer, + mp_float_t timeout, uint16_t receiver_buffer_size, byte *receiver_buffer, bool sigint_enabled) { - //match pins to UART objects - USART_TypeDef * USARTx; + // match pins to UART objects + USART_TypeDef *USARTx; uint8_t tx_len = MP_ARRAY_SIZE(mcu_uart_tx_list); uint8_t rx_len = MP_ARRAY_SIZE(mcu_uart_rx_list); bool uart_taken = false; - uint8_t periph_index = 0; //origin 0 corrected + uint8_t periph_index = 0; // origin 0 corrected if ((rts != NULL) || (cts != NULL) || (rs485_dir != NULL) || (rs485_invert == true)) { mp_raise_ValueError(translate("RTS/CTS/RS485 Not yet supported on this device")); } - //Can have both pins, or either + // Can have both pins, or either if ((tx != NULL) && (rx != NULL)) { - //normal find loop if both pins exist + // normal find loop if both pins exist for (uint i = 0; i < tx_len; i++) { if (mcu_uart_tx_list[i].pin == tx) { - //rx + // rx for (uint j = 0; j < rx_len; j++) { if (mcu_uart_rx_list[j].pin == rx && mcu_uart_rx_list[j].periph_index == mcu_uart_tx_list[i].periph_index) { - //keep looking if the UART is taken, edge case + // keep looking if the UART is taken, edge case if (reserved_uart[mcu_uart_tx_list[i].periph_index - 1]) { uart_taken = true; continue; } - //store pins if not + // store pins if not self->tx = &mcu_uart_tx_list[i]; self->rx = &mcu_uart_rx_list[j]; break; @@ -125,15 +125,15 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self, USARTx = assign_uart_or_throw(self, (self->tx != NULL && self->rx != NULL), periph_index, uart_taken); } else if (tx == NULL) { - //If there is no tx, run only rx + // If there is no tx, run only rx for (uint i = 0; i < rx_len; i++) { if (mcu_uart_rx_list[i].pin == rx) { - //keep looking if the UART is taken, edge case + // keep looking if the UART is taken, edge case if (reserved_uart[mcu_uart_rx_list[i].periph_index - 1]) { uart_taken = true; continue; } - //store pins if not + // store pins if not self->rx = &mcu_uart_rx_list[i]; break; } @@ -142,15 +142,15 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self, USARTx = assign_uart_or_throw(self, (self->rx != NULL), periph_index, uart_taken); } else if (rx == NULL) { - //If there is no rx, run only tx + // If there is no rx, run only tx for (uint i = 0; i < tx_len; i++) { if (mcu_uart_tx_list[i].pin == tx) { - //keep looking if the UART is taken, edge case + // keep looking if the UART is taken, edge case if (reserved_uart[mcu_uart_tx_list[i].periph_index - 1]) { uart_taken = true; continue; } - //store pins if not + // store pins if not self->tx = &mcu_uart_tx_list[i]; break; } @@ -159,22 +159,22 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self, USARTx = assign_uart_or_throw(self, (self->tx != NULL), periph_index, uart_taken); } else { - //both pins cannot be empty + // both pins cannot be empty mp_raise_ValueError(translate("Supply at least one UART pin")); } - //Other errors - if ( receiver_buffer_size == 0 ) { + // Other errors + if (receiver_buffer_size == 0) { mp_raise_ValueError(translate("Invalid buffer size")); } - if ( bits != 8 && bits != 9 ) { + if (bits != 8 && bits != 9) { mp_raise_ValueError(translate("Invalid word/bit length")); } - if ( USARTx == NULL) { //this can only be hit if the periph file is wrong + if (USARTx == NULL) { // this can only be hit if the periph file is wrong mp_raise_ValueError(translate("Internal define error")); } - //GPIO Init + // GPIO Init GPIO_InitTypeDef GPIO_InitStruct = {0}; if (self->tx != NULL) { GPIO_InitStruct.Pin = pin_mask(tx->number); @@ -193,7 +193,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self, HAL_GPIO_Init(pin_port(rx->port), &GPIO_InitStruct); } - //reserve uart and enable the peripheral + // reserve uart and enable the peripheral reserved_uart[periph_index] = true; uart_clock_enable(1 << (periph_index)); uart_assign_irq(self, USARTx); @@ -203,15 +203,14 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self, self->handle.Init.WordLength = (bits == 9) ? UART_WORDLENGTH_9B : UART_WORDLENGTH_8B; self->handle.Init.StopBits = (stop > 1) ? UART_STOPBITS_2 : UART_STOPBITS_1; self->handle.Init.Parity = (parity == BUSIO_UART_PARITY_ODD) ? UART_PARITY_ODD : - (parity == BUSIO_UART_PARITY_EVEN) ? UART_PARITY_EVEN : - UART_PARITY_NONE; + (parity == BUSIO_UART_PARITY_EVEN) ? UART_PARITY_EVEN : + UART_PARITY_NONE; self->handle.Init.Mode = (self->tx != NULL && self->rx != NULL) ? UART_MODE_TX_RX : - (self->tx != NULL) ? UART_MODE_TX : - UART_MODE_RX; + (self->tx != NULL) ? UART_MODE_TX : + UART_MODE_RX; self->handle.Init.HwFlowCtl = UART_HWCONTROL_NONE; self->handle.Init.OverSampling = UART_OVERSAMPLING_16; - if (HAL_UART_Init(&self->handle) != HAL_OK) - { + if (HAL_UART_Init(&self->handle) != HAL_OK) { mp_raise_ValueError(translate("UART Init Error")); } @@ -219,7 +218,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self, // Init buffer for rx and claim pins if (self->rx != NULL) { if (receiver_buffer != NULL) { - self->ringbuf = (ringbuf_t){ receiver_buffer, receiver_buffer_size }; + self->ringbuf = (ringbuf_t) { receiver_buffer, receiver_buffer_size }; } else { if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) { mp_raise_ValueError(translate("UART Buffer allocation error")); @@ -234,13 +233,13 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self, self->timeout_ms = timeout * 1000; self->sigint_enabled = sigint_enabled; - //start the interrupt series + // start the interrupt series if ((HAL_UART_GetState(&self->handle) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX) { mp_raise_ValueError(translate("Could not start interrupt, RX busy")); } - //start the receive interrupt chain - HAL_NVIC_DisableIRQ(self->irq); //prevent handle lock contention + // start the receive interrupt chain + HAL_NVIC_DisableIRQ(self->irq); // prevent handle lock contention HAL_UART_Receive_IT(&self->handle, &self->rx_char, 1); HAL_NVIC_SetPriority(self->irq, UART_IRQPRI, UART_IRQSUB_PRI); HAL_NVIC_EnableIRQ(self->irq); @@ -260,11 +259,13 @@ void common_hal_busio_uart_never_reset(busio_uart_obj_t *self) { } bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) { - return (self->tx->pin == NULL && self->rx->pin == NULL); + return self->tx->pin == NULL && self->rx->pin == NULL; } void common_hal_busio_uart_deinit(busio_uart_obj_t *self) { - if (common_hal_busio_uart_deinited(self)) return; + if (common_hal_busio_uart_deinited(self)) { + return; + } for (size_t i = 0; i < MP_ARRAY_SIZE(mcu_uart_banks); i++) { if (mcu_uart_banks[i] == self->handle.Instance) { @@ -294,14 +295,14 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t uint64_t start_ticks = supervisor_ticks_ms64(); // Wait for all bytes received or timeout, same as nrf - while ( (ringbuf_num_filled(&self->ringbuf) < len) && (supervisor_ticks_ms64() - start_ticks < self->timeout_ms) ) { + while ((ringbuf_num_filled(&self->ringbuf) < len) && (supervisor_ticks_ms64() - start_ticks < self->timeout_ms)) { RUN_BACKGROUND_TASKS; - //restart if it failed in the callback + // restart if it failed in the callback if (errflag != HAL_OK) { errflag = HAL_UART_Receive_IT(&self->handle, &self->rx_char, 1); } // Allow user to break out of a timeout with a KeyboardInterrupt. - if ( mp_hal_is_interrupted() ) { + if (mp_hal_is_interrupted()) { return 0; } } @@ -324,10 +325,10 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, if (self->tx == NULL) { mp_raise_ValueError(translate("No TX pin")); } - bool write_err = false; //write error shouldn't disable interrupts + bool write_err = false; // write error shouldn't disable interrupts HAL_NVIC_DisableIRQ(self->irq); - HAL_StatusTypeDef ret = HAL_UART_Transmit(&self->handle, (uint8_t*)data, len, HAL_MAX_DELAY); + HAL_StatusTypeDef ret = HAL_UART_Transmit(&self->handle, (uint8_t *)data, len, HAL_MAX_DELAY); if (ret != HAL_OK) { write_err = true; } @@ -340,13 +341,12 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, return len; } -void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handle) -{ +void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handle) { for (int i = 0; i < 7; i++) { - //get context pointer and cast it as struct pointer - busio_uart_obj_t * context = (busio_uart_obj_t*)MP_STATE_PORT(cpy_uart_obj_all)[i]; + // get context pointer and cast it as struct pointer + busio_uart_obj_t *context = (busio_uart_obj_t *)MP_STATE_PORT(cpy_uart_obj_all)[i]; if (handle == &context->handle) { - //check if transaction is ongoing + // check if transaction is ongoing if ((HAL_UART_GetState(handle) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX) { return; } @@ -364,8 +364,7 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handle) } } -void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle) -{ +void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle) { if (__HAL_UART_GET_FLAG(UartHandle, UART_FLAG_PE) != RESET) { __HAL_UART_CLEAR_PEFLAG(UartHandle); } else if (__HAL_UART_GET_FLAG(UartHandle, UART_FLAG_FE) != RESET) { @@ -375,9 +374,9 @@ void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle) } else if (__HAL_UART_GET_FLAG(UartHandle, UART_FLAG_ORE) != RESET) { __HAL_UART_CLEAR_OREFLAG(UartHandle); } - //restart serial read after an error + // restart serial read after an error for (int i = 0; i < 7; i++) { - busio_uart_obj_t * context = (busio_uart_obj_t *)MP_STATE_PORT(cpy_uart_obj_all)[i]; + busio_uart_obj_t *context = (busio_uart_obj_t *)MP_STATE_PORT(cpy_uart_obj_all)[i]; if (UartHandle == &context->handle) { HAL_UART_Receive_IT(UartHandle, &context->rx_char, 1); return; @@ -391,10 +390,12 @@ uint32_t common_hal_busio_uart_get_baudrate(busio_uart_obj_t *self) { } void common_hal_busio_uart_set_baudrate(busio_uart_obj_t *self, uint32_t baudrate) { - //Don't reset if it's the same value - if (baudrate == self->baudrate) return; + // Don't reset if it's the same value + if (baudrate == self->baudrate) { + return; + } - //Otherwise de-init and set new rate + // Otherwise de-init and set new rate if (HAL_UART_DeInit(&self->handle) != HAL_OK) { mp_raise_ValueError(translate("UART De-init error")); } @@ -407,7 +408,7 @@ void common_hal_busio_uart_set_baudrate(busio_uart_obj_t *self, uint32_t baudrat } mp_float_t common_hal_busio_uart_get_timeout(busio_uart_obj_t *self) { - return (mp_float_t) (self->timeout_ms / 1000.0f); + return (mp_float_t)(self->timeout_ms / 1000.0f); } void common_hal_busio_uart_set_timeout(busio_uart_obj_t *self, mp_float_t timeout) { @@ -430,8 +431,8 @@ bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self) { } STATIC void call_hal_irq(int uart_num) { - //Create casted context pointer - busio_uart_obj_t * context = (busio_uart_obj_t*)MP_STATE_PORT(cpy_uart_obj_all)[uart_num - 1]; + // Create casted context pointer + busio_uart_obj_t *context = (busio_uart_obj_t *)MP_STATE_PORT(cpy_uart_obj_all)[uart_num - 1]; if (context != NULL) { HAL_NVIC_ClearPendingIRQ(context->irq); HAL_UART_IRQHandler(&context->handle); @@ -609,7 +610,7 @@ STATIC void uart_clock_disable(uint16_t mask) { #endif } -STATIC void uart_assign_irq(busio_uart_obj_t *self, USART_TypeDef * USARTx) { +STATIC void uart_assign_irq(busio_uart_obj_t *self, USART_TypeDef *USARTx) { #ifdef USART1 if (USARTx == USART1) { self->irq = USART1_IRQn; diff --git a/ports/stm/common-hal/canio/CAN.c b/ports/stm/common-hal/canio/CAN.c index 52d5cad1f..091421fd1 100644 --- a/ports/stm/common-hal/canio/CAN.c +++ b/ports/stm/common-hal/canio/CAN.c @@ -38,7 +38,7 @@ STATIC bool reserved_can[MP_ARRAY_SIZE(mcu_can_banks)]; STATIC const mcu_periph_obj_t *find_pin_function(const mcu_periph_obj_t *table, size_t sz, const mcu_pin_obj_t *pin, int periph_index) { - for(size_t i = 0; i<sz; i++, table++) { + for (size_t i = 0; i < sz; i++, table++) { if (periph_index != -1 && periph_index != table->periph_index) { continue; } @@ -50,9 +50,8 @@ STATIC const mcu_periph_obj_t *find_pin_function(const mcu_periph_obj_t *table, } -void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *tx, mcu_pin_obj_t *rx, int baudrate, bool loopback, bool silent) -{ -#define DIV_ROUND(a, b) (((a) + (b)/2) / (b)) +void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *tx, mcu_pin_obj_t *rx, int baudrate, bool loopback, bool silent) { +#define DIV_ROUND(a, b) (((a) + (b) / 2) / (b)) #define DIV_ROUND_UP(a, b) (((a) + (b) - 1) / (b)) const uint8_t can_tx_len = MP_ARRAY_SIZE(mcu_can_tx_list); @@ -110,7 +109,7 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *tx, mc __HAL_RCC_CAN1_CLK_ENABLE(); - if(hw == CAN2) { + if (hw == CAN2) { __HAL_RCC_CAN2_CLK_ENABLE(); self->start_filter_bank = 14; self->end_filter_bank = 28; @@ -126,9 +125,9 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *tx, mc .AutoBusOff = ENABLE, .Prescaler = divisor, .Mode = (loopback ? CAN_MODE_LOOPBACK : 0) | (silent ? CAN_MODE_SILENT_LOOPBACK : 0), - .SyncJumpWidth = (sjw-1) << CAN_BTR_SJW_Pos, - .TimeSeg1 = (tq_to_sample-2) << CAN_BTR_TS1_Pos, - .TimeSeg2 = (tq_after_sample-1) << CAN_BTR_TS2_Pos, + .SyncJumpWidth = (sjw - 1) << CAN_BTR_SJW_Pos, + .TimeSeg1 = (tq_to_sample - 2) << CAN_BTR_TS1_Pos, + .TimeSeg2 = (tq_after_sample - 1) << CAN_BTR_TS2_Pos, }; self->periph_index = periph_index; @@ -149,7 +148,7 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *tx, mc // Clear every filter enable bit for this can HW uint32_t fa1r = self->filter_hw->FA1R; - for (int i = self->start_filter_bank; i<self->end_filter_bank; i++) { + for (int i = self->start_filter_bank; i < self->end_filter_bank; i++) { fa1r &= ~(1 << i); } self->filter_hw->FA1R = fa1r; @@ -160,23 +159,19 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *tx, mc reserved_can[periph_index] = true; } -bool common_hal_canio_can_loopback_get(canio_can_obj_t *self) -{ +bool common_hal_canio_can_loopback_get(canio_can_obj_t *self) { return self->loopback; } -int common_hal_canio_can_baudrate_get(canio_can_obj_t *self) -{ +int common_hal_canio_can_baudrate_get(canio_can_obj_t *self) { return self->baudrate; } -int common_hal_canio_can_transmit_error_count_get(canio_can_obj_t *self) -{ +int common_hal_canio_can_transmit_error_count_get(canio_can_obj_t *self) { return (self->handle.Instance->ESR & CAN_ESR_TEC) >> CAN_ESR_TEC_Pos; } -int common_hal_canio_can_receive_error_count_get(canio_can_obj_t *self) -{ +int common_hal_canio_can_receive_error_count_get(canio_can_obj_t *self) { return (self->handle.Instance->ESR & CAN_ESR_REC) >> CAN_ESR_REC_Pos; } @@ -213,15 +208,14 @@ bool common_hal_canio_can_auto_restart_get(canio_can_obj_t *self) { } void common_hal_canio_can_auto_restart_set(canio_can_obj_t *self, bool value) { - if(value) { + if (value) { SET_BIT(self->handle.Instance->MCR, CAN_MCR_ABOM); } else { CLEAR_BIT(self->handle.Instance->MCR, CAN_MCR_ABOM); } } -void common_hal_canio_can_send(canio_can_obj_t *self, mp_obj_t message_in) -{ +void common_hal_canio_can_send(canio_can_obj_t *self, mp_obj_t message_in) { canio_message_obj_t *message = message_in; uint32_t mailbox; bool rtr = message->base.type == &canio_remote_transmission_request_type; @@ -278,8 +272,7 @@ void common_hal_canio_can_check_for_deinit(canio_can_obj_t *self) { } } -void common_hal_canio_can_deinit(canio_can_obj_t *self) -{ +void common_hal_canio_can_deinit(canio_can_obj_t *self) { if (self->handle.Instance) { SET_BIT(self->handle.Instance->MCR, CAN_MCR_RESET); while (READ_BIT(self->handle.Instance->MCR, CAN_MCR_RESET)) { @@ -290,7 +283,7 @@ void common_hal_canio_can_deinit(canio_can_obj_t *self) } void common_hal_canio_reset(void) { - for (size_t i=0; i<MP_ARRAY_SIZE(mcu_can_banks); i++) { + for (size_t i = 0; i < MP_ARRAY_SIZE(mcu_can_banks); i++) { SET_BIT(mcu_can_banks[i]->MCR, CAN_MCR_RESET); reserved_can[i] = 0; } diff --git a/ports/stm/common-hal/canio/CAN.h b/ports/stm/common-hal/canio/CAN.h index 3157d0a03..16bb8fd1e 100644 --- a/ports/stm/common-hal/canio/CAN.h +++ b/ports/stm/common-hal/canio/CAN.h @@ -45,12 +45,12 @@ typedef struct canio_can_obj { int baudrate; const mcu_pin_obj_t *rx_pin; const mcu_pin_obj_t *tx_pin; - bool loopback:1; - bool silent:1; - bool auto_restart:1; - bool fifo0_in_use:1; - bool fifo1_in_use:1; - uint8_t periph_index:2; + bool loopback : 1; + bool silent : 1; + bool auto_restart : 1; + bool fifo0_in_use : 1; + bool fifo1_in_use : 1; + uint8_t periph_index : 2; uint8_t cancel_mailbox; uint8_t start_filter_bank; uint8_t end_filter_bank; diff --git a/ports/stm/common-hal/canio/Listener.c b/ports/stm/common-hal/canio/Listener.c index 09456d39d..23634eba6 100644 --- a/ports/stm/common-hal/canio/Listener.c +++ b/ports/stm/common-hal/canio/Listener.c @@ -46,7 +46,7 @@ STATIC void prevent_filter_change(canio_can_obj_t *can) { } STATIC bool filter_in_use(canio_can_obj_t *can, int idx) { - return can->filter_hw->FA1R & (1<<idx); + return can->filter_hw->FA1R & (1 << idx); } // One filter bank can hold: @@ -62,19 +62,19 @@ STATIC size_t num_filters_needed(size_t nmatch, canio_match_obj_t **matches) { } size_t num_extended_mask = 0; size_t num_standard_mask = 1; - for(size_t i=0; i<nmatch; i++) { + for (size_t i = 0; i < nmatch; i++) { if (matches[i]->extended) { num_extended_mask += 1; } else { num_standard_mask += 1; } } - return num_extended_mask + num_standard_mask/2; + return num_extended_mask + num_standard_mask / 2; } STATIC size_t num_filters_available(canio_can_obj_t *can) { size_t available = 0; - for(size_t i = can->start_filter_bank; i < can->end_filter_bank; i++) { + for (size_t i = can->start_filter_bank; i < can->end_filter_bank; i++) { if (!filter_in_use(can, i)) { available++; } @@ -87,9 +87,9 @@ STATIC void clear_filters(canio_listener_obj_t *self) { allow_filter_change(can); uint32_t fa1r = can->filter_hw->FA1R; - for(size_t i = can->start_filter_bank; i < can->end_filter_bank; i++) { + for (size_t i = can->start_filter_bank; i < can->end_filter_bank; i++) { if (((can->filter_hw->FFA1R >> i) & 1) == self->fifo_idx) { - fa1r &= ~(1<<i); + fa1r &= ~(1 << i); } } can->filter_hw->FA1R = fa1r; @@ -98,8 +98,8 @@ STATIC void clear_filters(canio_listener_obj_t *self) { STATIC int next_filter(canio_can_obj_t *can) { uint32_t fa1r = can->filter_hw->FA1R; - for(size_t i = can->start_filter_bank; i < can->end_filter_bank; i++) { - if (!(fa1r & (1<<i))) { + for (size_t i = can->start_filter_bank; i < can->end_filter_bank; i++) { + if (!(fa1r & (1 << i))) { return i; } } @@ -109,8 +109,8 @@ STATIC int next_filter(canio_can_obj_t *can) { // IDE = "extended ID" flag of packet header. We always add this bit to the // mask because a match is always for just one kind of address length -#define FILTER16_IDE (1<<3) -#define FILTER32_IDE (1<<2) +#define FILTER16_IDE (1 << 3) +#define FILTER32_IDE (1 << 2) STATIC void install_standard_filter(canio_listener_obj_t *self, canio_match_obj_t *match1, canio_match_obj_t *match2) { int bank = next_filter(self->can); @@ -201,8 +201,8 @@ void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t ** install_all_match_filter(self); } else { canio_match_obj_t *first_match = NULL; - for(size_t i = 0; i<nmatch; i++) { - if(matches[i]->extended) { + for (size_t i = 0; i < nmatch; i++) { + if (matches[i]->extended) { install_extended_filter(self, matches[i]); } else { if (first_match) { diff --git a/ports/stm/common-hal/digitalio/DigitalInOut.c b/ports/stm/common-hal/digitalio/DigitalInOut.c index a676aeb15..fbefdb79a 100644 --- a/ports/stm/common-hal/digitalio/DigitalInOut.c +++ b/ports/stm/common-hal/digitalio/DigitalInOut.c @@ -40,12 +40,12 @@ #endif void common_hal_digitalio_digitalinout_never_reset( - digitalio_digitalinout_obj_t *self) { + digitalio_digitalinout_obj_t *self) { never_reset_pin_number(self->pin->port, self->pin->number); } digitalinout_result_t common_hal_digitalio_digitalinout_construct( - digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) { + digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) { common_hal_mcu_pin_claim(pin); self->pin = pin; @@ -74,7 +74,7 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self } void common_hal_digitalio_digitalinout_switch_to_input( - digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { + digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Pin = pin_mask(self->pin->number); @@ -87,8 +87,8 @@ void common_hal_digitalio_digitalinout_switch_to_input( } digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output( - digitalio_digitalinout_obj_t *self, bool value, - digitalio_drive_mode_t drive_mode) { + digitalio_digitalinout_obj_t *self, bool value, + digitalio_drive_mode_t drive_mode) { common_hal_digitalio_digitalinout_set_drive_mode(self, drive_mode); common_hal_digitalio_digitalinout_set_value(self, value); @@ -96,27 +96,27 @@ digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output( } digitalio_direction_t common_hal_digitalio_digitalinout_get_direction( - digitalio_digitalinout_obj_t *self) { + digitalio_digitalinout_obj_t *self) { return (LL_GPIO_GetPinMode(pin_port(self->pin->port), 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) { + digitalio_digitalinout_obj_t *self, bool value) { HAL_GPIO_WritePin(pin_port(self->pin->port), pin_mask(self->pin->number), value); } bool common_hal_digitalio_digitalinout_get_value( - digitalio_digitalinout_obj_t *self) { + digitalio_digitalinout_obj_t *self) { return (LL_GPIO_GetPinMode(pin_port(self->pin->port), pin_mask(self->pin->number)) == LL_GPIO_MODE_INPUT) ? HAL_GPIO_ReadPin(pin_port(self->pin->port), pin_mask(self->pin->number)) : LL_GPIO_IsOutputPinSet(pin_port(self->pin->port), pin_mask(self->pin->number)); } digitalinout_result_t common_hal_digitalio_digitalinout_set_drive_mode( - digitalio_digitalinout_obj_t *self, - digitalio_drive_mode_t 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.Mode = (drive_mode == DRIVE_MODE_OPEN_DRAIN ? @@ -128,14 +128,14 @@ digitalinout_result_t common_hal_digitalio_digitalinout_set_drive_mode( } digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( - digitalio_digitalinout_obj_t *self) { + digitalio_digitalinout_obj_t *self) { return LL_GPIO_GetPinOutputType(pin_port(self->pin->port), pin_mask(self->pin->number)) - == LL_GPIO_OUTPUT_OPENDRAIN ? DRIVE_MODE_OPEN_DRAIN : DRIVE_MODE_PUSH_PULL; + == LL_GPIO_OUTPUT_OPENDRAIN ? DRIVE_MODE_OPEN_DRAIN : DRIVE_MODE_PUSH_PULL; } void common_hal_digitalio_digitalinout_set_pull( - digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { + digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { switch (pull) { case PULL_UP: @@ -153,7 +153,7 @@ void common_hal_digitalio_digitalinout_set_pull( } digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( - digitalio_digitalinout_obj_t *self) { + digitalio_digitalinout_obj_t *self) { switch (LL_GPIO_GetPinPull(pin_port(self->pin->port), pin_mask(self->pin->number))) { case LL_GPIO_PULL_UP: diff --git a/ports/stm/common-hal/displayio/ParallelBus.c b/ports/stm/common-hal/displayio/ParallelBus.c index fd07d38af..4788deb9b 100644 --- a/ports/stm/common-hal/displayio/ParallelBus.c +++ b/ports/stm/common-hal/displayio/ParallelBus.c @@ -33,19 +33,19 @@ #include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-bindings/microcontroller/__init__.h" -void common_hal_displayio_parallelbus_construct(displayio_parallelbus_obj_t* self, - const mcu_pin_obj_t* data0, const mcu_pin_obj_t* command, const mcu_pin_obj_t* chip_select, - const mcu_pin_obj_t* write, const mcu_pin_obj_t* read, const mcu_pin_obj_t* reset) { +void common_hal_displayio_parallelbus_construct(displayio_parallelbus_obj_t *self, + const mcu_pin_obj_t *data0, const mcu_pin_obj_t *command, const mcu_pin_obj_t *chip_select, + const mcu_pin_obj_t *write, const mcu_pin_obj_t *read, const mcu_pin_obj_t *reset) { mp_raise_NotImplementedError(translate("ParallelBus not yet supported")); } -void common_hal_displayio_parallelbus_deinit(displayio_parallelbus_obj_t* self) { +void common_hal_displayio_parallelbus_deinit(displayio_parallelbus_obj_t *self) { } bool common_hal_displayio_parallelbus_reset(mp_obj_t obj) { - return false; + return false; } bool common_hal_displayio_parallelbus_bus_free(mp_obj_t obj) { diff --git a/ports/stm/common-hal/microcontroller/Pin.c b/ports/stm/common-hal/microcontroller/Pin.c index 37c202d86..80cb9defc 100644 --- a/ports/stm/common-hal/microcontroller/Pin.c +++ b/ports/stm/common-hal/microcontroller/Pin.c @@ -41,15 +41,15 @@ bool apa102_mosi_in_use; #endif #if defined(TFBGA216) - GPIO_TypeDef * ports[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH, GPIOI, GPIOJ, GPIOK}; +GPIO_TypeDef *ports[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH, GPIOI, GPIOJ, GPIOK}; #elif defined(LQFP144) - GPIO_TypeDef * ports[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG}; +GPIO_TypeDef *ports[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG}; #elif defined(LQFP100_f4) || (LQFP100_x7) - GPIO_TypeDef * ports[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE}; +GPIO_TypeDef *ports[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE}; #elif defined(LQFP64) - GPIO_TypeDef * ports[] = {GPIOA, GPIOB, GPIOC, GPIOD}; +GPIO_TypeDef *ports[] = {GPIOA, GPIOB, GPIOC, GPIOD}; #elif defined(UFQFPN48) - GPIO_TypeDef * ports[] = {GPIOA, GPIOB, GPIOC}; +GPIO_TypeDef *ports[] = {GPIOA, GPIOB, GPIOC}; #endif @@ -78,7 +78,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_number == NO_PIN ) { + if (pin_number == NO_PIN) { return; } @@ -86,9 +86,9 @@ void reset_pin_number(uint8_t pin_port, uint8_t pin_number) { return; } // Clear claimed bit & reset - claimed_pins[pin_port] &= ~(1<<pin_number); - never_reset_pins[pin_port] &= ~(1<<pin_number); - HAL_GPIO_DeInit(ports[pin_port], 1<<pin_number); + claimed_pins[pin_port] &= ~(1 << pin_number); + never_reset_pins[pin_port] &= ~(1 << pin_number); + HAL_GPIO_DeInit(ports[pin_port], 1 << pin_number); #ifdef MICROPY_HW_NEOPIXEL if (pin_port == MICROPY_HW_NEOPIXEL->port && pin_number == MICROPY_HW_NEOPIXEL->number) { @@ -101,8 +101,7 @@ void reset_pin_number(uint8_t pin_port, uint8_t pin_number) { if ( (pin_port == MICROPY_HW_APA102_MOSI->port && pin_number == MICROPY_HW_APA102_MOSI->number) || (pin_port == MICROPY_HW_APA102_SCK->port && pin_number == MICROPY_HW_APA102_MOSI->number) - ) - { + ) { apa102_mosi_in_use = false; apa102_sck_in_use = false; rgb_led_status_init(); @@ -112,19 +111,19 @@ void reset_pin_number(uint8_t pin_port, uint8_t pin_number) { } void never_reset_pin_number(uint8_t pin_port, uint8_t pin_number) { - if ( pin_number == NO_PIN ) { + if (pin_number == NO_PIN) { return; } - never_reset_pins[pin_port] |= 1<<pin_number; + never_reset_pins[pin_port] |= 1 << pin_number; // Make sure never reset pins are also always claimed - claimed_pins[pin_port] |= 1<<pin_number; + claimed_pins[pin_port] |= 1 << pin_number; } -void common_hal_never_reset_pin(const mcu_pin_obj_t* pin) { +void common_hal_never_reset_pin(const mcu_pin_obj_t *pin) { never_reset_pin_number(pin->port, pin->number); } -void common_hal_reset_pin(const mcu_pin_obj_t* pin) { +void common_hal_reset_pin(const mcu_pin_obj_t *pin) { if (pin == NULL) { return; } @@ -133,11 +132,11 @@ void common_hal_reset_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; + 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); + return !(claimed_pins[pin_port] & 1 << pin_number); } bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) { @@ -147,12 +146,10 @@ bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) { } #endif #if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK) - if (pin == MICROPY_HW_APA102_MOSI) - { + if (pin == MICROPY_HW_APA102_MOSI) { return !apa102_mosi_in_use; } - if (pin == MICROPY_HW_APA102_SCK) - { + if (pin == MICROPY_HW_APA102_SCK) { return !apa102_sck_in_use; } #endif @@ -160,19 +157,19 @@ bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) { return pin_number_is_free(pin->port, pin->number); } -GPIO_TypeDef * pin_port(uint8_t pin_port) { +GPIO_TypeDef *pin_port(uint8_t pin_port) { return ports[pin_port]; } uint16_t pin_mask(uint8_t pin_number) { - return 1<<pin_number; + return 1 << pin_number; } -uint8_t common_hal_mcu_pin_number(const mcu_pin_obj_t* pin) { +uint8_t common_hal_mcu_pin_number(const mcu_pin_obj_t *pin) { return pin->port * 16 + pin->number; } -void common_hal_mcu_pin_claim(const mcu_pin_obj_t* pin) { +void common_hal_mcu_pin_claim(const mcu_pin_obj_t *pin) { claim_pin(pin->port, pin->number); #ifdef MICROPY_HW_NEOPIXEL if (pin == MICROPY_HW_NEOPIXEL) { @@ -180,12 +177,10 @@ void common_hal_mcu_pin_claim(const mcu_pin_obj_t* pin) { } #endif #if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK) - if (pin == MICROPY_HW_APA102_MOSI) - { + if (pin == MICROPY_HW_APA102_MOSI) { apa102_mosi_in_use = true; } - if (pin == MICROPY_HW_APA102_SCK) - { + if (pin == MICROPY_HW_APA102_SCK) { apa102_sck_in_use = true; } #endif diff --git a/ports/stm/common-hal/microcontroller/Pin.h b/ports/stm/common-hal/microcontroller/Pin.h index f461d3813..4a3397942 100644 --- a/ports/stm/common-hal/microcontroller/Pin.h +++ b/ports/stm/common-hal/microcontroller/Pin.h @@ -46,7 +46,7 @@ void reset_pin_number(uint8_t pin_port, uint8_t pin_number); void claim_pin(uint8_t pin_port, uint8_t pin_number); 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); +GPIO_TypeDef *pin_port(uint8_t pin_port); uint16_t pin_mask(uint8_t pin_number); #endif // MICROPY_INCLUDED_STM32_COMMON_HAL_MICROCONTROLLER_PIN_H diff --git a/ports/stm/common-hal/microcontroller/Processor.c b/ports/stm/common-hal/microcontroller/Processor.c index dd04c56dc..b33d96e73 100644 --- a/ports/stm/common-hal/microcontroller/Processor.c +++ b/ports/stm/common-hal/microcontroller/Processor.c @@ -37,10 +37,10 @@ #define STM32_UUID ((uint32_t *)0x1FFF7A10) -//Factory calibration locations +// Factory calibration locations #define ADC_CAL_ADDRESS (0x1fff7a2a) -#define ADC_CAL1 ((uint16_t*)(ADC_CAL_ADDRESS + 2)) -#define ADC_CAL2 ((uint16_t*)(ADC_CAL_ADDRESS + 4)) +#define ADC_CAL1 ((uint16_t *)(ADC_CAL_ADDRESS + 2)) +#define ADC_CAL2 ((uint16_t *)(ADC_CAL_ADDRESS + 4)) #define VREFIN_CAL ((uint16_t *)ADC_CAL_ADDRESS) // correction factor for reference value @@ -68,7 +68,7 @@ float common_hal_mcu_processor_get_temperature(void) { #if CPY_STM32F4 __HAL_RCC_ADC1_CLK_ENABLE(); - //HAL Implementation + // HAL Implementation ADC_HandleTypeDef AdcHandle; ADC_ChannelConfTypeDef sConfig; set_adc_params(&AdcHandle); @@ -77,7 +77,7 @@ float common_hal_mcu_processor_get_temperature(void) { ADC->CCR |= ADC_CCR_TSVREFE; ADC->CCR &= ~ADC_CCR_VBATE; // If this somehow got turned on, it'll return bad values. - sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; //either 16 or 18, depending on chip + sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; // either 16 or 18, depending on chip sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES; // Temp sensor likes 10us minimum HAL_ADC_ConfigChannel(&AdcHandle, &sConfig); @@ -89,7 +89,7 @@ float common_hal_mcu_processor_get_temperature(void) { uint32_t value = (uint32_t)HAL_ADC_GetValue(&AdcHandle); HAL_ADC_Stop(&AdcHandle); - //There's no F4 specific appnote for this but it works the same as the L1 in AN3964 + // There's no F4 specific appnote for this but it works the same as the L1 in AN3964 float core_temp_avg_slope = (*ADC_CAL2 - *ADC_CAL1) / 80.0; return (((float)value * adc_refcor - *ADC_CAL1) / core_temp_avg_slope) + 30.0f; #else @@ -101,7 +101,7 @@ float common_hal_mcu_processor_get_voltage(void) { #if CPY_STM32F4 __HAL_RCC_ADC1_CLK_ENABLE(); - //HAL Implementation + // HAL Implementation ADC_HandleTypeDef AdcHandle; ADC_ChannelConfTypeDef sConfig; set_adc_params(&AdcHandle); @@ -121,7 +121,7 @@ float common_hal_mcu_processor_get_voltage(void) { uint32_t value = (uint32_t)HAL_ADC_GetValue(&AdcHandle); HAL_ADC_Stop(&AdcHandle); - //This value could be used to actively correct ADC values. + // This value could be used to actively correct ADC values. adc_refcor = ((float)(*VREFIN_CAL)) / ((float)value); return adc_refcor * 3.3f; @@ -136,8 +136,8 @@ uint32_t common_hal_mcu_processor_get_frequency(void) { void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) { #if CPY_STM32F4 - for (int i=0; i<3; i++) { - ((uint32_t*) raw_id)[i] = STM32_UUID[i]; + for (int i = 0; i < 3; i++) { + ((uint32_t *)raw_id)[i] = STM32_UUID[i]; } #endif } diff --git a/ports/stm/common-hal/microcontroller/__init__.c b/ports/stm/common-hal/microcontroller/__init__.c index a827399cc..28e06c913 100644 --- a/ports/stm/common-hal/microcontroller/__init__.c +++ b/ports/stm/common-hal/microcontroller/__init__.c @@ -41,11 +41,12 @@ #include "supervisor/shared/safe_mode.h" void common_hal_mcu_delay_us(uint32_t delay) { - uint32_t ticks_per_us = HAL_RCC_GetSysClockFreq()/1000000; + uint32_t ticks_per_us = HAL_RCC_GetSysClockFreq() / 1000000; delay *= ticks_per_us; SysTick->LOAD = delay; SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk; - while ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0) {} + while ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0) { + } SysTick->CTRL = 0; } @@ -61,7 +62,7 @@ void common_hal_mcu_enable_interrupts(void) { if (nesting_count == 0) { // This is very very bad because it means there was mismatched disable/enables so we // "HardFault". - asm("bkpt"); + asm ("bkpt"); } nesting_count--; if (nesting_count > 0) { @@ -72,12 +73,13 @@ void common_hal_mcu_enable_interrupts(void) { } void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) { - if(runmode == RUNMODE_SAFE_MODE) - safe_mode_on_next_reset(PROGRAMMATIC_SAFE_MODE); + if (runmode == RUNMODE_SAFE_MODE) { + safe_mode_on_next_reset(PROGRAMMATIC_SAFE_MODE); + } } void common_hal_mcu_reset(void) { - filesystem_flush(); //TODO: implement as part of flash improvements + filesystem_flush(); // TODO: implement as part of flash improvements NVIC_SystemReset(); } @@ -96,6 +98,6 @@ const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = { .type = &nvm_bytearray_type, }, .len = NVM_BYTEARRAY_BUFFER_SIZE, - .start_address = (uint8_t*) (CIRCUITPY_INTERNAL_NVM_START_ADDR) + .start_address = (uint8_t *)(CIRCUITPY_INTERNAL_NVM_START_ADDR) }; #endif diff --git a/ports/stm/common-hal/neopixel_write/__init__.c b/ports/stm/common-hal/neopixel_write/__init__.c index c8852403b..f5dabe7f9 100644 --- a/ports/stm/common-hal/neopixel_write/__init__.c +++ b/ports/stm/common-hal/neopixel_write/__init__.c @@ -36,7 +36,7 @@ uint64_t next_start_raw_ticks = 0; -//sysclock divisors +// sysclock divisors #define MAGIC_800_INT 900000 // ~1.11 us -> 1.2 field #define MAGIC_800_T0H 2800000 // ~0.36 us -> 0.44 field #define MAGIC_800_T1H 1350000 // ~0.74 us -> 0.84 field @@ -44,25 +44,26 @@ uint64_t next_start_raw_ticks = 0; #pragma GCC push_options #pragma GCC optimize ("Os") -void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout, uint8_t *pixels, - uint32_t numBytes) { +void common_hal_neopixel_write(const digitalio_digitalinout_obj_t *digitalinout, uint8_t *pixels, + uint32_t numBytes) { uint8_t *p = pixels, *end = p + numBytes, pix = *p++, mask = 0x80; uint32_t start = 0; uint32_t cyc = 0; - //assumes 800_000Hz frequency - //Theoretical values here are 800_000 -> 1.25us, 2500000->0.4us, 1250000->0.8us - //TODO: try to get dynamic weighting working again + // assumes 800_000Hz frequency + // Theoretical values here are 800_000 -> 1.25us, 2500000->0.4us, 1250000->0.8us + // TODO: try to get dynamic weighting working again uint32_t sys_freq = HAL_RCC_GetSysClockFreq(); - uint32_t interval = sys_freq/MAGIC_800_INT; - uint32_t t0 = (sys_freq/MAGIC_800_T0H); - uint32_t t1 = (sys_freq/MAGIC_800_T1H); + uint32_t interval = sys_freq / MAGIC_800_INT; + uint32_t t0 = (sys_freq / MAGIC_800_T0H); + uint32_t t1 = (sys_freq / MAGIC_800_T1H); // Wait to make sure we don't append onto the last transmission. This should only be a tick or // two. - while (port_get_raw_ticks(NULL) < next_start_raw_ticks) {} + while (port_get_raw_ticks(NULL) < next_start_raw_ticks) { + } - GPIO_TypeDef * p_port = pin_port(digitalinout->pin->port); + GPIO_TypeDef *p_port = pin_port(digitalinout->pin->port); uint32_t p_mask = pin_mask(digitalinout->pin->number); __disable_irq(); @@ -71,16 +72,22 @@ void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; DWT->CYCCNT = 0; - for(;;) { + for (;;) { cyc = (pix & mask) ? t1 : t0; start = DWT->CYCCNT; LL_GPIO_SetOutputPin(p_port, p_mask); - while((DWT->CYCCNT - start) < cyc); + while ((DWT->CYCCNT - start) < cyc) { + ; + } LL_GPIO_ResetOutputPin(p_port, p_mask); - while((DWT->CYCCNT - start) < interval); - if(!(mask >>= 1)) { - if(p >= end) break; - pix = *p++; + while ((DWT->CYCCNT - start) < interval) { + ; + } + if (!(mask >>= 1)) { + if (p >= end) { + break; + } + pix = *p++; mask = 0x80; } } diff --git a/ports/stm/common-hal/nvm/ByteArray.c b/ports/stm/common-hal/nvm/ByteArray.c index b7a1ff035..6ef2eb392 100644 --- a/ports/stm/common-hal/nvm/ByteArray.c +++ b/ports/stm/common-hal/nvm/ByteArray.c @@ -38,7 +38,7 @@ uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) { } bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self, - uint32_t start_index, uint8_t* values, uint32_t len) { + uint32_t start_index, uint8_t *values, uint32_t len) { // Copy flash to buffer uint8_t buffer[self->len]; memcpy(buffer, self->start_address, self->len); @@ -48,7 +48,7 @@ bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self, // Erase flash sector HAL_FLASH_Unlock(); - __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR ); + __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); FLASH_Erase_Sector(CIRCUITPY_INTERNAL_NVM_SECTOR, VOLTAGE_RANGE_3); // Write bytes to flash @@ -68,6 +68,6 @@ bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self, // NVM memory is memory mapped so reading it is easy. void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self, - uint32_t start_index, uint32_t len, uint8_t* values) { + uint32_t start_index, uint32_t len, uint8_t *values) { memcpy(values, self->start_address + start_index, len); } diff --git a/ports/stm/common-hal/nvm/ByteArray.h b/ports/stm/common-hal/nvm/ByteArray.h index b6ea36de3..b88d91976 100644 --- a/ports/stm/common-hal/nvm/ByteArray.h +++ b/ports/stm/common-hal/nvm/ByteArray.h @@ -38,7 +38,7 @@ typedef struct { mp_obj_base_t base; - uint8_t* start_address; + uint8_t *start_address; uint32_t len; } nvm_bytearray_obj_t; diff --git a/ports/stm/common-hal/os/__init__.c b/ports/stm/common-hal/os/__init__.c index 8da7243a3..66dfd9f76 100644 --- a/ports/stm/common-hal/os/__init__.c +++ b/ports/stm/common-hal/os/__init__.c @@ -55,7 +55,7 @@ STATIC MP_DEFINE_ATTRTUPLE( (mp_obj_t)&os_uname_info_release_obj, (mp_obj_t)&os_uname_info_version_obj, (mp_obj_t)&os_uname_info_machine_obj -); + ); mp_obj_t common_hal_os_uname(void) { return (mp_obj_t)&os_uname_info_obj; @@ -65,19 +65,23 @@ mp_obj_t common_hal_os_uname(void) { bool common_hal_os_urandom(uint8_t *buffer, uint32_t length) { #if (HAS_TRNG) - //init the RNG + // init the RNG __HAL_RCC_RNG_CLK_ENABLE(); RNG_HandleTypeDef handle; handle.Instance = RNG; - if (HAL_RNG_Init(&handle) != HAL_OK) mp_raise_ValueError(translate("RNG Init Error")); + if (HAL_RNG_Init(&handle) != HAL_OK) { + mp_raise_ValueError(translate("RNG Init Error")); + } - //Assign bytes + // Assign bytes uint32_t i = 0; while (i < length) { uint32_t new_random; uint32_t start = HAL_GetTick(); - //the HAL function has a timeout, but it isn't long enough, and isn't adjustable - while(!(__HAL_RNG_GET_FLAG(&handle,RNG_FLAG_DRDY)) && ((HAL_GetTick() - start) < RNG_TIMEOUT)); + // the HAL function has a timeout, but it isn't long enough, and isn't adjustable + while (!(__HAL_RNG_GET_FLAG(&handle,RNG_FLAG_DRDY)) && ((HAL_GetTick() - start) < RNG_TIMEOUT)) { + ; + } if (HAL_RNG_GenerateRandomNumber(&handle, &new_random) != HAL_OK) { mp_raise_ValueError(translate("Random number generation error")); } @@ -88,8 +92,10 @@ bool common_hal_os_urandom(uint8_t *buffer, uint32_t length) { } } - //shut down the peripheral - if (HAL_RNG_DeInit(&handle) != HAL_OK) mp_raise_ValueError(translate("RNG DeInit Error")); + // shut down the peripheral + if (HAL_RNG_DeInit(&handle) != HAL_OK) { + mp_raise_ValueError(translate("RNG DeInit Error")); + } __HAL_RCC_RNG_CLK_DISABLE(); return true; diff --git a/ports/stm/common-hal/pulseio/PulseIn.c b/ports/stm/common-hal/pulseio/PulseIn.c index 015ee8536..ece02f66a 100644 --- a/ports/stm/common-hal/pulseio/PulseIn.c +++ b/ports/stm/common-hal/pulseio/PulseIn.c @@ -38,21 +38,18 @@ #include STM32_HAL_H #define STM32_GPIO_PORT_SIZE 16 -static pulseio_pulsein_obj_t* _objs[STM32_GPIO_PORT_SIZE]; +static pulseio_pulsein_obj_t *_objs[STM32_GPIO_PORT_SIZE]; STATIC TIM_HandleTypeDef tim_handle; static uint32_t overflow_count = 0; STATIC uint8_t refcount = 0; -static void assign_EXTI_Interrupt(pulseio_pulsein_obj_t* self, uint8_t num); +static void assign_EXTI_Interrupt(pulseio_pulsein_obj_t *self, uint8_t num); -void pulsein_timer_event_handler(void) -{ +void pulsein_timer_event_handler(void) { // Detect TIM Update event - if (__HAL_TIM_GET_FLAG(&tim_handle, TIM_FLAG_UPDATE) != RESET) - { - if (__HAL_TIM_GET_IT_SOURCE(&tim_handle, TIM_IT_UPDATE) != RESET) - { + if (__HAL_TIM_GET_FLAG(&tim_handle, TIM_FLAG_UPDATE) != RESET) { + if (__HAL_TIM_GET_IT_SOURCE(&tim_handle, TIM_IT_UPDATE) != RESET) { __HAL_TIM_CLEAR_IT(&tim_handle, TIM_IT_UPDATE); overflow_count++; } @@ -67,13 +64,15 @@ static void pulsein_exti_event_handler(uint8_t num) { // Interrupt register must be cleared manually EXTI->PR = 1 << num; - pulseio_pulsein_obj_t* self = _objs[num]; - if ( !self ) return; + pulseio_pulsein_obj_t *self = _objs[num]; + if (!self) { + return; + } if (self->first_edge) { // first pulse is opposite state from idle bool state = HAL_GPIO_ReadPin(pin_port(self->pin->port), pin_mask(self->pin->number)); - if ( self->idle_state != state ) { + if (self->idle_state != state) { self->first_edge = false; } } else { @@ -109,20 +108,20 @@ void pulsein_reset(void) { refcount = 0; } -void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu_pin_obj_t* pin, - uint16_t maxlen, bool idle_state) { +void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu_pin_obj_t *pin, + uint16_t maxlen, bool idle_state) { // STM32 has one shared EXTI for each pin number, 0-15 uint8_t p_num = pin->number; - if(_objs[p_num]) { + if (_objs[p_num]) { mp_raise_ValueError(translate("Pin number already reserved by EXTI")); } _objs[p_num] = self; // Allocate pulse buffer - self->buffer = (uint16_t *) m_malloc(maxlen * sizeof(uint16_t), false); + self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t), false); if (self->buffer == NULL) { mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), - maxlen * sizeof(uint16_t)); + maxlen * sizeof(uint16_t)); } // Set internal variables @@ -138,12 +137,12 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu if (HAL_TIM_Base_GetState(&tim_handle) == HAL_TIM_STATE_RESET) { // Find a suitable timer - TIM_TypeDef * tim_instance = stm_peripherals_find_timer(); + TIM_TypeDef *tim_instance = stm_peripherals_find_timer(); stm_peripherals_timer_reserve(tim_instance); // Set ticks to 1us uint32_t source = stm_peripherals_timer_get_source_freq(tim_instance); - uint32_t prescaler = source/1000000; + uint32_t prescaler = source / 1000000; // Enable clocks and IRQ, set callback stm_peripherals_timer_preinit(tim_instance, 4, pulsein_timer_event_handler); @@ -151,7 +150,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu // Set the new period tim_handle.Instance = tim_instance; tim_handle.Init.Prescaler = prescaler - 1; - tim_handle.Init.Period = 0x10000 - 1; //65 ms period (maximum) + tim_handle.Init.Period = 0x10000 - 1; // 65 ms period (maximum) HAL_TIM_Base_Init(&tim_handle); // Set registers manually @@ -178,15 +177,15 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu common_hal_mcu_pin_claim(pin); } -bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) { - return (self->pin == NULL); +bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t *self) { + return self->pin == NULL; } -void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) { +void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) { if (common_hal_pulseio_pulsein_deinited(self)) { return; } - //Remove pulsein slot from shared array + // Remove pulsein slot from shared array _objs[self->pin->number] = NULL; reset_pin_number(self->pin->port, self->pin->number); self->pin = NULL; @@ -197,14 +196,14 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) { } } -void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) { +void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t *self) { HAL_NVIC_DisableIRQ(self->irq); self->paused = true; } -void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration) { +void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self, uint16_t trigger_duration) { // Make sure we're paused. - if ( !self->paused ) { + if (!self->paused) { common_hal_pulseio_pulsein_pause(self); } @@ -235,14 +234,14 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t tri HAL_NVIC_EnableIRQ(self->irq); } -void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) { +void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t *self) { HAL_NVIC_DisableIRQ(self->irq); self->start = 0; self->len = 0; HAL_NVIC_EnableIRQ(self->irq); } -uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_t index) { +uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t *self, int16_t index) { HAL_NVIC_DisableIRQ(self->irq); if (index < 0) { index += self->len; @@ -256,7 +255,7 @@ uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_ return value; } -uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) { +uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) { if (self->len == 0) { mp_raise_IndexError_varg(translate("pop from empty %q"), MP_QSTR_PulseIn); } @@ -269,19 +268,19 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) { return value; } -uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) { +uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t *self) { return self->maxlen; } -bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t* self) { +bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t *self) { return self->paused; } -uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) { +uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t *self) { return self->len; } -static void assign_EXTI_Interrupt(pulseio_pulsein_obj_t* self, uint8_t num) { +static void assign_EXTI_Interrupt(pulseio_pulsein_obj_t *self, uint8_t num) { if (num == 0) { self->irq = EXTI0_IRQn; } else if (num == 1) { @@ -292,49 +291,42 @@ static void assign_EXTI_Interrupt(pulseio_pulsein_obj_t* self, uint8_t num) { self->irq = EXTI3_IRQn; } else if (num == 4) { self->irq = EXTI4_IRQn; - } else if (num >= 5 && num <= 9 ) { + } else if (num >= 5 && num <= 9) { self->irq = EXTI9_5_IRQn; } else if (num >= 10 && num <= 15) { self->irq = EXTI15_10_IRQn; } } -void EXTI0_IRQHandler(void) -{ +void EXTI0_IRQHandler(void) { pulsein_exti_event_handler(0); } -void EXTI1_IRQHandler(void) -{ +void EXTI1_IRQHandler(void) { pulsein_exti_event_handler(1); } -void EXTI2_IRQHandler(void) -{ +void EXTI2_IRQHandler(void) { pulsein_exti_event_handler(2); } -void EXTI3_IRQHandler(void) -{ +void EXTI3_IRQHandler(void) { pulsein_exti_event_handler(3); } -void EXTI4_IRQHandler(void) -{ +void EXTI4_IRQHandler(void) { pulsein_exti_event_handler(4); } -void EXTI9_5_IRQHandler(void) -{ +void EXTI9_5_IRQHandler(void) { uint32_t pending = EXTI->PR; for (uint i = 5; i <= 9; i++) { - if(pending & (1 << i)) { + if (pending & (1 << i)) { pulsein_exti_event_handler(i); } } } -void EXTI15_10_IRQHandler(void) -{ +void EXTI15_10_IRQHandler(void) { uint32_t pending = EXTI->PR; for (uint i = 10; i <= 15; i++) { - if(pending & (1 << i)) { + if (pending & (1 << i)) { pulsein_exti_event_handler(i); } } diff --git a/ports/stm/common-hal/pulseio/PulseIn.h b/ports/stm/common-hal/pulseio/PulseIn.h index 34d0cc731..8de60b9cd 100644 --- a/ports/stm/common-hal/pulseio/PulseIn.h +++ b/ports/stm/common-hal/pulseio/PulseIn.h @@ -34,13 +34,13 @@ typedef struct { mp_obj_base_t base; - const mcu_pin_obj_t* pin; + const mcu_pin_obj_t *pin; IRQn_Type irq; bool idle_state; bool paused; volatile bool first_edge; - uint16_t* buffer; + uint16_t *buffer; uint16_t maxlen; volatile uint16_t start; diff --git a/ports/stm/common-hal/pulseio/PulseOut.c b/ports/stm/common-hal/pulseio/PulseOut.c index 38027606f..9e28002a6 100644 --- a/ports/stm/common-hal/pulseio/PulseOut.c +++ b/ports/stm/common-hal/pulseio/PulseOut.c @@ -45,7 +45,7 @@ STATIC uint8_t refcount = 0; STATIC uint16_t *pulse_array = NULL; STATIC volatile uint16_t pulse_array_index = 0; STATIC uint16_t pulse_array_length; -//Timer is shared, must be accessible by interrupt +// Timer is shared, must be accessible by interrupt STATIC TIM_HandleTypeDef tim_handle; STATIC pulseio_pulseout_obj_t *curr_pulseout = NULL; @@ -60,7 +60,7 @@ STATIC void turn_off(pulseio_pulseout_obj_t *pulseout) { HAL_TIM_PWM_Stop(&(pulseout->pwmout->handle), pulseout->pwmout->channel); // Make sure pin is low. HAL_GPIO_WritePin(pin_port(pulseout->pwmout->tim->pin->port), - pin_mask(pulseout->pwmout->tim->pin->number), 0); + pin_mask(pulseout->pwmout->tim->pin->number), 0); } STATIC void start_timer(void) { @@ -77,13 +77,11 @@ STATIC void start_timer(void) { STATIC void pulseout_event_handler(void) { // Detect TIM Update event - if (__HAL_TIM_GET_FLAG(&tim_handle, TIM_FLAG_UPDATE) != RESET) - { - if (__HAL_TIM_GET_IT_SOURCE(&tim_handle, TIM_IT_UPDATE) != RESET) - { + if (__HAL_TIM_GET_FLAG(&tim_handle, TIM_FLAG_UPDATE) != RESET) { + if (__HAL_TIM_GET_IT_SOURCE(&tim_handle, TIM_IT_UPDATE) != RESET) { __HAL_TIM_CLEAR_IT(&tim_handle, TIM_IT_UPDATE); if (curr_pulseout->pwmout == NULL) { - return; //invalid interrupt + return; // invalid interrupt } pulse_array_index++; @@ -112,27 +110,27 @@ void pulseout_reset() { refcount = 0; } -void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, - const pwmio_pwmout_obj_t* carrier, - const mcu_pin_obj_t* pin, - uint32_t frequency, - uint16_t duty_cycle) { +void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self, + const pwmio_pwmout_obj_t *carrier, + const mcu_pin_obj_t *pin, + uint32_t frequency, + uint16_t duty_cycle) { if (!carrier || pin || frequency) { mp_raise_NotImplementedError(translate("Port does not accept pins or frequency. Construct and pass a PWMOut Carrier instead")); } // Add to active PulseOuts refcount++; - TIM_TypeDef * tim_instance = stm_peripherals_find_timer(); + TIM_TypeDef *tim_instance = stm_peripherals_find_timer(); stm_peripherals_timer_reserve(tim_instance); - //calculate a 1ms period + // calculate a 1ms period uint32_t source = stm_peripherals_timer_get_source_freq(tim_instance); - uint32_t prescaler = source/1000000; //1us intervals + uint32_t prescaler = source / 1000000; // 1us intervals stm_peripherals_timer_preinit(tim_instance, 4, pulseout_event_handler); tim_handle.Instance = tim_instance; - tim_handle.Init.Period = 100; //immediately replaced. + tim_handle.Init.Period = 100; // immediately replaced. tim_handle.Init.Prescaler = prescaler - 1; tim_handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; tim_handle.Init.CounterMode = TIM_COUNTERMODE_UP; @@ -142,15 +140,15 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, tim_handle.Instance->SR = 0; // The HAL can't work with const, recast required. - self->pwmout = (pwmio_pwmout_obj_t*)carrier; + self->pwmout = (pwmio_pwmout_obj_t *)carrier; turn_off(self); } -bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self) { +bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t *self) { return self->pwmout == NULL; } -void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) { +void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t *self) { if (common_hal_pulseio_pulseout_deinited(self)) { return; } @@ -163,7 +161,7 @@ void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) { } } -void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) { +void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t *self, uint16_t *pulses, uint16_t length) { pulse_array = pulses; pulse_array_index = 0; pulse_array_length = length; @@ -177,7 +175,7 @@ void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pu // Use when debugging, or issues are irrecoverable // uint32_t starttime = supervisor_ticks_ms64(); // uint32_t timeout = 10000; - while(pulse_array_index < length) { + while (pulse_array_index < length) { // Do other things while we wait. The interrupts will handle sending the // signal. RUN_BACKGROUND_TASKS; @@ -187,6 +185,6 @@ void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pu // mp_raise_RuntimeError(translate("Error: Send Timeout")); // } } - //turn off timer counter. + // turn off timer counter. tim_handle.Instance->CR1 &= ~TIM_CR1_CEN; } diff --git a/ports/stm/common-hal/pwmio/PWMOut.c b/ports/stm/common-hal/pwmio/PWMOut.c index 85427185e..c463cac43 100644 --- a/ports/stm/common-hal/pwmio/PWMOut.c +++ b/ports/stm/common-hal/pwmio/PWMOut.c @@ -44,13 +44,13 @@ STATIC uint32_t tim_frequencies[TIM_BANK_ARRAY_LEN]; STATIC bool never_reset_tim[TIM_BANK_ARRAY_LEN]; STATIC uint32_t timer_get_internal_duty(uint16_t duty, uint32_t period) { - //duty cycle is duty/0xFFFF fraction x (number of pulses per period) - return (duty*period) / ((1 << 16) - 1); + // duty cycle is duty/0xFFFF fraction x (number of pulses per period) + return (duty * period) / ((1 << 16) - 1); } -STATIC void timer_get_optimal_divisors(uint32_t*period, uint32_t*prescaler, - uint32_t frequency, uint32_t source_freq) { - //Find the largest possible period supported by this frequency +STATIC void timer_get_optimal_divisors(uint32_t *period, uint32_t *prescaler, + uint32_t frequency, uint32_t source_freq) { + // Find the largest possible period supported by this frequency for (int i = 0; i < (1 << 16); i++) { *period = source_freq / (i * frequency); if (*period < (1 << 16) && *period >= 2) { @@ -75,12 +75,12 @@ void pwmout_reset(void) { } } -pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self, - const mcu_pin_obj_t* pin, - uint16_t duty, - uint32_t frequency, - bool variable_frequency) { - TIM_TypeDef * TIMx; +pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self, + const mcu_pin_obj_t *pin, + uint16_t duty, + uint32_t frequency, + bool variable_frequency) { + TIM_TypeDef *TIMx; uint8_t tim_num = MP_ARRAY_SIZE(mcu_tim_pin_list); bool tim_taken_internal = false; bool tim_chan_taken = false; @@ -89,47 +89,47 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self, bool first_time_setup = true; for (uint i = 0; i < tim_num; i++) { - const mcu_tim_pin_obj_t * l_tim = &mcu_tim_pin_list[i]; + const mcu_tim_pin_obj_t *l_tim = &mcu_tim_pin_list[i]; uint8_t l_tim_index = l_tim->tim_index - 1; uint8_t l_tim_channel = l_tim->channel_index - 1; - //if pin is same + // if pin is same if (l_tim->pin == pin) { - //check if the timer has a channel active, or is reserved by main timer system + // check if the timer has a channel active, or is reserved by main timer system if (l_tim_index < TIM_BANK_ARRAY_LEN && reserved_tim[l_tim_index] != 0) { // Timer has already been reserved by an internal module if (stm_peripherals_timer_is_reserved(mcu_tim_banks[l_tim_index])) { tim_taken_internal = true; - continue; //keep looking + continue; // keep looking } - //is it the same channel? (or all channels reserved by a var-freq) + // is it the same channel? (or all channels reserved by a var-freq) if (reserved_tim[l_tim_index] & 1 << (l_tim_channel)) { tim_chan_taken = true; - continue; //keep looking, might be another viable option + continue; // keep looking, might be another viable option } - //If the frequencies are the same it's ok + // If the frequencies are the same it's ok if (tim_frequencies[l_tim_index] != frequency) { tim_taken_f_mismatch = true; - continue; //keep looking + continue; // keep looking } - //you can't put a variable frequency on a partially reserved timer + // you can't put a variable frequency on a partially reserved timer if (variable_frequency) { var_freq_mismatch = true; - continue; //keep looking + continue; // keep looking } - first_time_setup = false; //skip setting up the timer + first_time_setup = false; // skip setting up the timer } - //No problems taken, so set it up + // No problems taken, so set it up self->tim = l_tim; break; } } - //handle valid/invalid timer instance + // handle valid/invalid timer instance if (self->tim != NULL) { - //create instance + // create instance TIMx = mcu_tim_banks[self->tim->tim_index - 1]; - //reserve timer/channel + // reserve timer/channel if (variable_frequency) { reserved_tim[self->tim->tim_index - 1] = 0x0F; } else { @@ -137,7 +137,7 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self, } tim_frequencies[self->tim->tim_index - 1] = frequency; stm_peripherals_timer_reserve(TIMx); - } else { //no match found + } else { // no match found if (tim_chan_taken) { mp_raise_ValueError(translate("No more timers available on this pin.")); } else if (tim_taken_internal) { @@ -161,15 +161,15 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self, tim_clock_enable(1 << (self->tim->tim_index - 1)); - //translate channel into handle value + // translate channel into handle value self->channel = 4 * (self->tim->channel_index - 1); - uint32_t prescaler = 0; //prescaler is 15 bit - uint32_t period = 0; //period is 16 bit + uint32_t prescaler = 0; // prescaler is 15 bit + uint32_t period = 0; // period is 16 bit uint32_t source_freq = stm_peripherals_timer_get_source_freq(TIMx); timer_get_optimal_divisors(&period, &prescaler, frequency, source_freq); - //Timer init + // Timer init self->handle.Instance = TIMx; self->handle.Init.Period = period - 1; self->handle.Init.Prescaler = prescaler - 1; @@ -177,14 +177,14 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self, self->handle.Init.CounterMode = TIM_COUNTERMODE_UP; self->handle.Init.RepetitionCounter = 0; - //only run init if this is the first instance of this timer + // only run init if this is the first instance of this timer if (first_time_setup) { if (HAL_TIM_PWM_Init(&self->handle) != HAL_OK) { mp_raise_ValueError(translate("Could not initialize timer")); } } - //Channel/PWM init + // Channel/PWM init self->chan_handle.OCMode = TIM_OCMODE_PWM1; self->chan_handle.Pulse = timer_get_internal_duty(duty, period); self->chan_handle.OCPolarity = TIM_OCPOLARITY_HIGH; @@ -215,7 +215,7 @@ void common_hal_pwmio_pwmout_never_reset(pwmio_pwmout_obj_t *self) { } void common_hal_pwmio_pwmout_reset_ok(pwmio_pwmout_obj_t *self) { - for(size_t i = 0; i < TIM_BANK_ARRAY_LEN; i++) { + for (size_t i = 0; i < TIM_BANK_ARRAY_LEN; i++) { if (mcu_tim_banks[i] == self->handle.Instance) { never_reset_tim[i] = false; break; @@ -223,15 +223,15 @@ void common_hal_pwmio_pwmout_reset_ok(pwmio_pwmout_obj_t *self) { } } -bool common_hal_pwmio_pwmout_deinited(pwmio_pwmout_obj_t* self) { +bool common_hal_pwmio_pwmout_deinited(pwmio_pwmout_obj_t *self) { return self->tim == NULL; } -void common_hal_pwmio_pwmout_deinit(pwmio_pwmout_obj_t* self) { +void common_hal_pwmio_pwmout_deinit(pwmio_pwmout_obj_t *self) { if (common_hal_pwmio_pwmout_deinited(self)) { return; } - //var freq shuts down entire timer, others just their channel + // var freq shuts down entire timer, others just their channel if (self->variable_frequency) { reserved_tim[self->tim->tim_index - 1] = 0x00; } else { @@ -240,7 +240,7 @@ void common_hal_pwmio_pwmout_deinit(pwmio_pwmout_obj_t* self) { } reset_pin_number(self->tim->pin->port,self->tim->pin->number); - //if reserved timer has no active channels, we can disable it + // if reserved timer has no active channels, we can disable it if (!reserved_tim[self->tim->tim_index - 1]) { tim_frequencies[self->tim->tim_index - 1] = 0x00; stm_peripherals_timer_free(self->handle.Instance); @@ -249,18 +249,18 @@ void common_hal_pwmio_pwmout_deinit(pwmio_pwmout_obj_t* self) { self->tim = NULL; } -void common_hal_pwmio_pwmout_set_duty_cycle(pwmio_pwmout_obj_t* self, uint16_t duty) { +void common_hal_pwmio_pwmout_set_duty_cycle(pwmio_pwmout_obj_t *self, uint16_t duty) { uint32_t internal_duty_cycle = timer_get_internal_duty(duty, self->period); __HAL_TIM_SET_COMPARE(&self->handle, self->channel, internal_duty_cycle); self->duty_cycle = duty; } -uint16_t common_hal_pwmio_pwmout_get_duty_cycle(pwmio_pwmout_obj_t* self) { +uint16_t common_hal_pwmio_pwmout_get_duty_cycle(pwmio_pwmout_obj_t *self) { return self->duty_cycle; } -void common_hal_pwmio_pwmout_set_frequency(pwmio_pwmout_obj_t* self, uint32_t frequency) { - //don't halt setup for the same frequency +void common_hal_pwmio_pwmout_set_frequency(pwmio_pwmout_obj_t *self, uint32_t frequency) { + // don't halt setup for the same frequency if (frequency == self->frequency) { return; } @@ -270,14 +270,14 @@ void common_hal_pwmio_pwmout_set_frequency(pwmio_pwmout_obj_t* self, uint32_t fr uint32_t source_freq = stm_peripherals_timer_get_source_freq(self->handle.Instance); timer_get_optimal_divisors(&period, &prescaler, frequency, source_freq); - //shut down + // shut down HAL_TIM_PWM_Stop(&self->handle, self->channel); - //Only change altered values + // Only change altered values self->handle.Init.Period = period - 1; self->handle.Init.Prescaler = prescaler - 1; - //restart everything, adjusting for new speed + // restart everything, adjusting for new speed if (HAL_TIM_PWM_Init(&self->handle) != HAL_OK) { mp_raise_ValueError(translate("Could not re-init timer")); } @@ -296,10 +296,10 @@ void common_hal_pwmio_pwmout_set_frequency(pwmio_pwmout_obj_t* self, uint32_t fr self->period = period; } -uint32_t common_hal_pwmio_pwmout_get_frequency(pwmio_pwmout_obj_t* self) { +uint32_t common_hal_pwmio_pwmout_get_frequency(pwmio_pwmout_obj_t *self) { return self->frequency; } -bool common_hal_pwmio_pwmout_get_variable_frequency(pwmio_pwmout_obj_t* self) { +bool common_hal_pwmio_pwmout_get_variable_frequency(pwmio_pwmout_obj_t *self) { return self->variable_frequency; } diff --git a/ports/stm/common-hal/pwmio/PWMOut.h b/ports/stm/common-hal/pwmio/PWMOut.h index 5d8001202..4cf2b1c67 100644 --- a/ports/stm/common-hal/pwmio/PWMOut.h +++ b/ports/stm/common-hal/pwmio/PWMOut.h @@ -39,8 +39,8 @@ typedef struct { TIM_HandleTypeDef handle; TIM_OC_InitTypeDef chan_handle; const mcu_tim_pin_obj_t *tim; - uint8_t channel: 7; - bool variable_frequency: 1; + uint8_t channel : 7; + bool variable_frequency : 1; uint16_t duty_cycle; uint32_t frequency; uint32_t period; diff --git a/ports/stm/common-hal/rgbmatrix/RGBMatrix.c b/ports/stm/common-hal/rgbmatrix/RGBMatrix.c index 36ed6d018..4c0ad94b0 100644 --- a/ports/stm/common-hal/rgbmatrix/RGBMatrix.c +++ b/ports/stm/common-hal/rgbmatrix/RGBMatrix.c @@ -34,24 +34,24 @@ extern void _PM_IRQ_HANDLER(void); void *common_hal_rgbmatrix_timer_allocate(rgbmatrix_rgbmatrix_obj_t *self) { - TIM_TypeDef * timer = stm_peripherals_find_timer(); + TIM_TypeDef *timer = stm_peripherals_find_timer(); stm_peripherals_timer_reserve(timer); stm_peripherals_timer_never_reset(timer); return timer; } -void common_hal_rgbmatrix_timer_enable(void* ptr) { - TIM_TypeDef *tim = (TIM_TypeDef*)ptr; +void common_hal_rgbmatrix_timer_enable(void *ptr) { + TIM_TypeDef *tim = (TIM_TypeDef *)ptr; HAL_NVIC_EnableIRQ(stm_peripherals_timer_get_irqnum(tim)); } -void common_hal_rgbmatrix_timer_disable(void* ptr) { - TIM_TypeDef *tim = (TIM_TypeDef*)ptr; +void common_hal_rgbmatrix_timer_disable(void *ptr) { + TIM_TypeDef *tim = (TIM_TypeDef *)ptr; tim->DIER &= ~TIM_DIER_UIE; } -void common_hal_rgbmatrix_timer_free(void* ptr) { - TIM_TypeDef *tim = (TIM_TypeDef*)ptr; +void common_hal_rgbmatrix_timer_free(void *ptr) { + TIM_TypeDef *tim = (TIM_TypeDef *)ptr; stm_peripherals_timer_free(tim); common_hal_rgbmatrix_timer_disable(ptr); } diff --git a/ports/stm/common-hal/rgbmatrix/RGBMatrix.h b/ports/stm/common-hal/rgbmatrix/RGBMatrix.h index 2debb6a76..cc4b82da0 100644 --- a/ports/stm/common-hal/rgbmatrix/RGBMatrix.h +++ b/ports/stm/common-hal/rgbmatrix/RGBMatrix.h @@ -30,8 +30,8 @@ #include "shared-module/rgbmatrix/RGBMatrix.h" void *common_hal_rgbmatrix_timer_allocate(rgbmatrix_rgbmatrix_obj_t *self); -void common_hal_rgbmatrix_timer_enable(void*); -void common_hal_rgbmatrix_timer_disable(void*); -void common_hal_rgbmatrix_timer_free(void*); +void common_hal_rgbmatrix_timer_enable(void *); +void common_hal_rgbmatrix_timer_disable(void *); +void common_hal_rgbmatrix_timer_free(void *); #endif diff --git a/ports/stm/common-hal/sdioio/SDCard.c b/ports/stm/common-hal/sdioio/SDCard.c index de0e8d111..e09e2e1b3 100644 --- a/ports/stm/common-hal/sdioio/SDCard.c +++ b/ports/stm/common-hal/sdioio/SDCard.c @@ -40,18 +40,18 @@ STATIC bool reserved_sdio[MP_ARRAY_SIZE(mcu_sdio_banks)]; STATIC bool never_reset_sdio[MP_ARRAY_SIZE(mcu_sdio_banks)]; STATIC const mcu_periph_obj_t *find_pin_function(const mcu_periph_obj_t *table, size_t sz, const mcu_pin_obj_t *pin, int periph_index) { - for(size_t i = 0; i<sz; i++, table++) { - if(periph_index == table->periph_index && pin == table->pin ) { + for (size_t i = 0; i < sz; i++, table++) { + if (periph_index == table->periph_index && pin == table->pin) { return table; } } return NULL; } -//match pins to SDIO objects +// match pins to SDIO objects STATIC int check_pins(sdioio_sdcard_obj_t *self, - const mcu_pin_obj_t * clock, const mcu_pin_obj_t * command, - uint8_t num_data, mcu_pin_obj_t ** data) { + const mcu_pin_obj_t *clock, const mcu_pin_obj_t *command, + uint8_t num_data, mcu_pin_obj_t **data) { bool sdio_taken = false; const uint8_t sdio_clock_len = MP_ARRAY_SIZE(mcu_sdio_clock_list); @@ -78,26 +78,26 @@ STATIC int check_pins(sdioio_sdcard_obj_t *self, } const mcu_periph_obj_t *mcu_sdio_data0 = NULL; - if(!(mcu_sdio_data0 = find_pin_function(mcu_sdio_data0_list, sdio_data0_len, data[0], periph_index))) { + if (!(mcu_sdio_data0 = find_pin_function(mcu_sdio_data0_list, sdio_data0_len, data[0], periph_index))) { continue; } const mcu_periph_obj_t *mcu_sdio_data1 = NULL; - if(num_data > 1 && !(mcu_sdio_data1 = find_pin_function(mcu_sdio_data1_list, sdio_data1_len, data[1], periph_index))) { + if (num_data > 1 && !(mcu_sdio_data1 = find_pin_function(mcu_sdio_data1_list, sdio_data1_len, data[1], periph_index))) { continue; } const mcu_periph_obj_t *mcu_sdio_data2 = NULL; - if(num_data > 2 && !(mcu_sdio_data2 = find_pin_function(mcu_sdio_data2_list, sdio_data2_len, data[2], periph_index))) { + if (num_data > 2 && !(mcu_sdio_data2 = find_pin_function(mcu_sdio_data2_list, sdio_data2_len, data[2], periph_index))) { continue; } const mcu_periph_obj_t *mcu_sdio_data3 = NULL; - if(num_data > 3 && !(mcu_sdio_data3 = find_pin_function(mcu_sdio_data3_list, sdio_data3_len, data[3], periph_index))) { + if (num_data > 3 && !(mcu_sdio_data3 = find_pin_function(mcu_sdio_data3_list, sdio_data3_len, data[3], periph_index))) { continue; } - if (reserved_sdio[periph_index-1]) { + if (reserved_sdio[periph_index - 1]) { sdio_taken = true; continue; } @@ -121,16 +121,16 @@ STATIC int check_pins(sdioio_sdcard_obj_t *self, void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self, - const mcu_pin_obj_t * clock, const mcu_pin_obj_t * command, - uint8_t num_data, mcu_pin_obj_t ** data, uint32_t frequency) { + const mcu_pin_obj_t *clock, const mcu_pin_obj_t *command, + uint8_t num_data, mcu_pin_obj_t **data, uint32_t frequency) { int periph_index = check_pins(self, clock, command, num_data, data); - SDIO_TypeDef * SDIOx = mcu_sdio_banks[periph_index - 1]; + SDIO_TypeDef *SDIOx = mcu_sdio_banks[periph_index - 1]; GPIO_InitTypeDef GPIO_InitStruct = {0}; /* Configure data pins */ - for (int i=0; i<num_data; i++) { + for (int i = 0; i < num_data; i++) { GPIO_InitStruct.Pin = pin_mask(data[i]->number); GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; @@ -186,7 +186,7 @@ void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self, common_hal_mcu_pin_claim(clock); common_hal_mcu_pin_claim(command); - for (int i=0; i<num_data; i++) { + for (int i = 0; i < num_data; i++) { common_hal_mcu_pin_claim(data[i]); } @@ -217,7 +217,7 @@ STATIC void wait_write_complete(sdioio_sdcard_obj_t *self) { // This waits up to 60s for programming to complete. This seems like // an extremely long time, but this is the timeout that micropython's // implementation uses - for (int i=0; i < 60000 && st == HAL_SD_CARD_PROGRAMMING; i++) { + for (int i = 0; i < 60000 && st == HAL_SD_CARD_PROGRAMMING; i++) { st = HAL_SD_GetCardState(&self->handle); HAL_Delay(1); }; @@ -293,7 +293,7 @@ void common_hal_sdioio_sdcard_deinit(sdioio_sdcard_obj_t *self) { reset_mcu_periph(self->clock); self->command = NULL; - for (size_t i=0; i<MP_ARRAY_SIZE(self->data); i++) { + for (size_t i = 0; i < MP_ARRAY_SIZE(self->data); i++) { reset_mcu_periph(self->data[i]); self->data[i] = NULL; } @@ -313,13 +313,13 @@ void common_hal_sdioio_sdcard_never_reset(sdioio_sdcard_obj_t *self) { never_reset_mcu_periph(self->command); never_reset_mcu_periph(self->clock); - for (size_t i=0; i<MP_ARRAY_SIZE(self->data); i++) { + for (size_t i = 0; i < MP_ARRAY_SIZE(self->data); i++) { never_reset_mcu_periph(self->data[i]); } } void sdioio_reset() { - for (size_t i=0; i<MP_ARRAY_SIZE(reserved_sdio); i++) { + for (size_t i = 0; i < MP_ARRAY_SIZE(reserved_sdio); i++) { if (!never_reset_sdio[i]) { reserved_sdio[i] = false; } diff --git a/ports/stm/common-hal/sdioio/SDCard.h b/ports/stm/common-hal/sdioio/SDCard.h index b737b6939..845a5aa5f 100644 --- a/ports/stm/common-hal/sdioio/SDCard.h +++ b/ports/stm/common-hal/sdioio/SDCard.h @@ -36,7 +36,7 @@ typedef struct { mp_obj_base_t base; SD_HandleTypeDef handle; - uint8_t num_data:3, state_programming:1; + uint8_t num_data : 3, state_programming : 1; bool has_lock; const mcu_periph_obj_t *command; const mcu_periph_obj_t *clock; diff --git a/ports/stm/common-hal/supervisor/Runtime.c b/ports/stm/common-hal/supervisor/Runtime.c index 974f26cec..f82765178 100755..100644 --- a/ports/stm/common-hal/supervisor/Runtime.c +++ b/ports/stm/common-hal/supervisor/Runtime.c @@ -29,9 +29,9 @@ #include "supervisor/serial.h" bool common_hal_supervisor_runtime_get_serial_connected(void) { - return (bool) serial_connected(); + return (bool)serial_connected(); } bool common_hal_supervisor_runtime_get_serial_bytes_available(void) { - return (bool) serial_bytes_available(); + return (bool)serial_bytes_available(); } |
