spacing changes and text fix

This commit is contained in:
Hierophect 2020-01-09 16:17:57 -05:00
parent e509115630
commit 4ec588bb37
4 changed files with 79 additions and 67 deletions

View File

@ -46,11 +46,11 @@ STATIC void i2c_clock_disable(uint8_t mask);
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 {
never_reset_mask |= 1<<i;
never_reset_mask |= 1 << i;
}
}
i2c_clock_disable(ALL_CLOCKS & ~(never_reset_mask));
@ -61,17 +61,17 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
//match pins to I2C objects
I2C_TypeDef * I2Cx;
uint8_t sda_len = sizeof(mcu_i2c_sda_list)/sizeof(*mcu_i2c_sda_list);
uint8_t scl_len = sizeof(mcu_i2c_scl_list)/sizeof(*mcu_i2c_scl_list);
uint8_t sda_len = sizeof(mcu_i2c_sda_list) / sizeof(*mcu_i2c_sda_list);
uint8_t scl_len = sizeof(mcu_i2c_scl_list) / sizeof(*mcu_i2c_scl_list);
bool i2c_taken = false;
for(uint i=0; i<sda_len;i++) {
for (uint i = 0; i < sda_len; i++) {
if (mcu_i2c_sda_list[i].pin == sda) {
for(uint j=0; j<scl_len;j++) {
for (uint j = 0; j < scl_len; j++) {
if ((mcu_i2c_scl_list[j].pin == scl)
&& (mcu_i2c_scl_list[j].i2c_index == mcu_i2c_sda_list[i].i2c_index)) {
//keep looking if the I2C is taken, could be another SCL that works
if(reserved_i2c[mcu_i2c_scl_list[i].i2c_index-1]) {
if (reserved_i2c[mcu_i2c_scl_list[i].i2c_index - 1]) {
i2c_taken = true;
continue;
}
@ -84,8 +84,8 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
}
//handle typedef selection, errors
if(self->sda!=NULL && self->scl!=NULL ) {
I2Cx = mcu_i2c_banks[self->sda->i2c_index-1];
if (self->sda != NULL && self->scl != NULL ) {
I2Cx = mcu_i2c_banks[self->sda->i2c_index - 1];
} else {
if (i2c_taken) {
mp_raise_ValueError(translate("Hardware busy, try alternative pins"));
@ -111,7 +111,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
HAL_GPIO_Init(pin_port(scl->port), &GPIO_InitStruct);
//Note: due to I2C soft reboot issue, do not relocate clock init.
i2c_clock_enable(1<<(self->sda->i2c_index - 1));
i2c_clock_enable(1 << (self->sda->i2c_index - 1));
reserved_i2c[self->sda->i2c_index - 1] = true;
self->handle.Instance = I2Cx;
@ -124,7 +124,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
self->handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
self->handle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
self->handle.State = HAL_I2C_STATE_RESET;
if(HAL_I2C_Init(&(self->handle)) != HAL_OK) {
if (HAL_I2C_Init(&(self->handle)) != HAL_OK) {
mp_raise_RuntimeError(translate("I2C Init Error"));
}
claim_pin(sda);
@ -132,7 +132,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
}
void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {
for (size_t i = 0 ; i < MP_ARRAY_SIZE(mcu_i2c_banks); i++) {
for (size_t i = 0; i < MP_ARRAY_SIZE(mcu_i2c_banks); i++) {
if (self->handle.Instance == mcu_i2c_banks[i]) {
never_reset_i2c[i] = true;
@ -152,7 +152,7 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
return;
}
i2c_clock_disable(1<<(self->sda->i2c_index - 1));
i2c_clock_disable(1 << (self->sda->i2c_index - 1));
reserved_i2c[self->sda->i2c_index - 1] = false;
never_reset_i2c[self->sda->i2c_index - 1] = false;
@ -163,7 +163,7 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
}
bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
return HAL_I2C_IsDeviceReady(&(self->handle), (uint16_t)(addr<<1),2,2) == HAL_OK;
return HAL_I2C_IsDeviceReady(&(self->handle), (uint16_t)(addr << 1), 2, 2) == HAL_OK;
}
bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) {
@ -195,13 +195,15 @@ 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) {
HAL_StatusTypeDef result = HAL_I2C_Master_Transmit(&(self->handle), (uint16_t)(addr<<1), (uint8_t *)data, (uint16_t)len, 500);
HAL_StatusTypeDef result = HAL_I2C_Master_Transmit(&(self->handle), (uint16_t)(addr << 1),
(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) {
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;
}
STATIC void i2c_clock_enable(uint8_t mask) {
@ -231,12 +233,18 @@ STATIC void i2c_clock_enable(uint8_t mask) {
STATIC void i2c_clock_disable(uint8_t mask) {
#ifdef I2C1
if (mask & 1<<0) __HAL_RCC_I2C1_CLK_DISABLE();
if (mask & 1<<0) {
__HAL_RCC_I2C1_CLK_DISABLE();
}
#endif
#ifdef I2C2
if (mask & 1<<1) __HAL_RCC_I2C2_CLK_DISABLE();
if (mask & 1<<1) {
__HAL_RCC_I2C2_CLK_DISABLE();
}
#endif
#ifdef I2C3
if (mask & 1<<2) __HAL_RCC_I2C3_CLK_DISABLE();
if (mask & 1<<2) {
__HAL_RCC_I2C3_CLK_DISABLE();
}
#endif
}

View File

@ -87,11 +87,11 @@ STATIC uint32_t stm32_baud_to_spi_div(uint32_t baudrate, uint16_t * prescaler, u
void spi_reset(void) {
uint16_t never_reset_mask = 0x00;
for(int i=0;i<MAX_SPI;i++) {
for (int i = 0; i < MAX_SPI; i++) {
if (!never_reset_spi[i]) {
reserved_spi[i] = false;
} else {
never_reset_mask |= 1<<i;
never_reset_mask |= 1 << i;
}
}
spi_clock_disable(ALL_CLOCKS & ~(never_reset_mask));
@ -110,20 +110,20 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
bool spi_taken = false;
//SCK is not optional. MOSI and MISO are
for (uint i=0; i<sck_len;i++) {
for (uint i = 0; i < sck_len; i++) {
if (mcu_spi_sck_list[i].pin == sck) {
//if both MOSI and MISO exist, loop search normally
if ((mosi != mp_const_none) && (miso != mp_const_none)) {
//MOSI
for (uint j=0; j<mosi_len;j++) {
for (uint j = 0; j < mosi_len; j++) {
if (mcu_spi_mosi_list[j].pin == mosi) {
//MISO
for (uint k=0; k<miso_len;k++) {
for (uint k = 0; k < miso_len; k++) {
if ((mcu_spi_miso_list[k].pin == miso) //everything needs the same index
&& (mcu_spi_sck_list[i].spi_index == mcu_spi_mosi_list[j].spi_index)
&& (mcu_spi_sck_list[i].spi_index == mcu_spi_miso_list[k].spi_index)) {
//keep looking if the SPI is taken, edge case
if (reserved_spi[mcu_spi_sck_list[i].spi_index-1]) {
if (reserved_spi[mcu_spi_sck_list[i].spi_index - 1]) {
spi_taken = true;
continue;
}
@ -138,11 +138,11 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
}
// if just MISO, reduce search
} else if (miso != mp_const_none) {
for (uint j=0; j<miso_len;j++) {
for (uint j = 0; j < miso_len; j++) {
if ((mcu_spi_miso_list[j].pin == miso) //only SCK and MISO need the same index
&& (mcu_spi_sck_list[i].spi_index == mcu_spi_miso_list[j].spi_index)) {
//keep looking if the SPI is taken, edge case
if (reserved_spi[mcu_spi_sck_list[i].spi_index-1]) {
if (reserved_spi[mcu_spi_sck_list[i].spi_index - 1]) {
spi_taken = true;
continue;
}
@ -155,11 +155,11 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
}
// if just MOSI, reduce search
} else if (mosi != mp_const_none) {
for (uint j=0; j<mosi_len;j++) {
for (uint j = 0; j < mosi_len; j++) {
if ((mcu_spi_mosi_list[j].pin == mosi) //only SCK and MOSI need the same index
&& (mcu_spi_sck_list[i].spi_index == mcu_spi_mosi_list[j].spi_index)) {
//keep looking if the SPI is taken, edge case
if (reserved_spi[mcu_spi_sck_list[i].spi_index-1]) {
if (reserved_spi[mcu_spi_sck_list[i].spi_index - 1]) {
spi_taken = true;
continue;
}
@ -178,10 +178,10 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
}
//handle typedef selection, errors
if ( (self->sck!=NULL && self->mosi!=NULL && self->miso != NULL) ||
(self->sck!=NULL && self->mosi!=NULL && miso == mp_const_none) ||
(self->sck!=NULL && self->miso!=NULL && mosi == mp_const_none)) {
SPIx = mcu_spi_banks[self->sck->spi_index-1];
if ( (self->sck != NULL && self->mosi != NULL && self->miso != NULL) ||
(self->sck != NULL && self->mosi != NULL && miso == mp_const_none) ||
(self->sck != NULL && self->miso != NULL && mosi == mp_const_none)) {
SPIx = mcu_spi_banks[self->sck->spi_index - 1];
} else {
if (spi_taken) {
mp_raise_ValueError(translate("Hardware busy, try alternative pins"));
@ -217,7 +217,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
HAL_GPIO_Init(pin_port(miso->port), &GPIO_InitStruct);
}
spi_clock_enable(1<<(self->sck->spi_index - 1));
spi_clock_enable(1 << (self->sck->spi_index - 1));
reserved_spi[self->sck->spi_index - 1] = true;
self->handle.Instance = SPIx;
@ -237,7 +237,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
{
mp_raise_ValueError(translate("SPI Init Error"));
}
self->baudrate = (get_busclock(SPIx)/16);
self->baudrate = (get_busclock(SPIx) / 16);
self->prescaler = 16;
self->polarity = 0;
self->phase = 0;
@ -253,7 +253,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
}
void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
for (size_t i = 0 ; i < MP_ARRAY_SIZE(mcu_spi_banks); i++) {
for (size_t i = 0; i < MP_ARRAY_SIZE(mcu_spi_banks); i++) {
if (mcu_spi_banks[i] == self->handle.Instance) {
never_reset_spi[i] = true;
never_reset_pin_number(self->sck->pin->port, self->sck->pin->number);

View File

@ -79,8 +79,8 @@ void common_hal_busio_uart_construct(busio_uart_obj_t* self,
//match pins to UART objects
USART_TypeDef * USARTx;
uint8_t tx_len = sizeof(mcu_uart_tx_list)/sizeof(*mcu_uart_tx_list);
uint8_t rx_len = sizeof(mcu_uart_rx_list)/sizeof(*mcu_uart_rx_list);
uint8_t tx_len = sizeof(mcu_uart_tx_list) / sizeof(*mcu_uart_tx_list);
uint8_t rx_len = sizeof(mcu_uart_rx_list) / sizeof(*mcu_uart_rx_list);
bool uart_taken = false;
uint8_t uart_index = 0; //origin 0 corrected

View File

@ -69,15 +69,15 @@ STATIC uint32_t timer_get_source_freq(uint32_t tim_id) {
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);
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
for (int i=0; i<(1 << 16);i++) {
*period = source_freq/(i*frequency);
if (*period < (1 << 16) && *period>=2) {
for (int i = 0; i < (1 << 16); i++) {
*period = source_freq / (i * frequency);
if (*period < (1 << 16) && *period >= 2) {
*prescaler = i;
break;
}
@ -89,12 +89,12 @@ STATIC void timer_get_optimal_divisors(uint32_t*period, uint32_t*prescaler,
void pwmout_reset(void) {
uint16_t never_reset_mask = 0x00;
for(int i=0;i<TIM_BANK_ARRAY_LEN;i++) {
for (int i = 0; i < TIM_BANK_ARRAY_LEN; i++) {
if (!never_reset_tim[i]) {
reserved_tim[i] = 0x00;
tim_frequencies[i] = 0x00;
} else {
never_reset_mask |= 1<<i;
never_reset_mask |= 1 << i;
}
}
tim_clock_disable(ALL_CLOCKS & ~(never_reset_mask));
@ -106,23 +106,23 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
uint32_t frequency,
bool variable_frequency) {
TIM_TypeDef * TIMx;
uint8_t tim_num = sizeof(mcu_tim_pin_list)/sizeof(*mcu_tim_pin_list);
uint8_t tim_num = sizeof(mcu_tim_pin_list) / sizeof(*mcu_tim_pin_list);
bool tim_chan_taken = false;
bool tim_taken_f_mismatch = false;
bool var_freq_mismatch = false;
bool first_time_setup = true;
for(uint i = 0; i < tim_num; i++) {
for (uint i = 0; i < tim_num; i++) {
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;
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 (l_tim.pin == pin) {
//check if the timer has a channel active
if (reserved_tim[l_tim_index] != 0) {
//is it the same channel? (or all channels reserved by a var-freq)
if (reserved_tim[l_tim_index] & 1<<(l_tim_channel)) {
if (reserved_tim[l_tim_index] & 1 << (l_tim_channel)) {
tim_chan_taken = true;
continue; //keep looking, might be another viable option
}
@ -145,21 +145,21 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
}
//handle valid/invalid timer instance
if (self->tim!=NULL) {
if (self->tim != NULL) {
//create instance
TIMx = mcu_tim_banks[self->tim->tim_index-1];
TIMx = mcu_tim_banks[self->tim->tim_index - 1];
//reserve timer/channel
if (variable_frequency) {
reserved_tim[self->tim->tim_index-1] = 0x0F;
reserved_tim[self->tim->tim_index - 1] = 0x0F;
} else {
reserved_tim[self->tim->tim_index-1] |= 1<<(self->tim->channel_index-1);
reserved_tim[self->tim->tim_index - 1] |= 1 << (self->tim->channel_index - 1);
}
tim_frequencies[self->tim->tim_index-1] = frequency;
tim_frequencies[self->tim->tim_index - 1] = frequency;
} else { //no match found
if (tim_chan_taken) {
mp_raise_ValueError(translate("No more timers available on this pin."));
} else if (tim_taken_f_mismatch) {
mp_raise_ValueError(translate("Frequency must be the same as as the existing PWMOut using this timer"));
mp_raise_ValueError(translate("Frequency must match existing PWMOut using this timer"));
} else if (var_freq_mismatch) {
mp_raise_ValueError(translate("Cannot vary frequency on a timer that is already in use"));
} else {
@ -175,14 +175,15 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
GPIO_InitStruct.Alternate = self->tim->altfn_index;
HAL_GPIO_Init(pin_port(pin->port), &GPIO_InitStruct);
tim_clock_enable(1<<(self->tim->tim_index - 1));
tim_clock_enable(1 << (self->tim->tim_index - 1));
//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
timer_get_optimal_divisors(&period, &prescaler,frequency,timer_get_source_freq(self->tim->tim_index));
timer_get_optimal_divisors(&period, &prescaler, frequency,
timer_get_source_freq(self->tim->tim_index));
//Timer init
self->handle.Instance = TIMx;
@ -223,7 +224,7 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
}
void common_hal_pulseio_pwmout_never_reset(pulseio_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] = true;
never_reset_pin_number(self->tim->pin->port, self->tim->pin->number);
@ -233,7 +234,7 @@ void common_hal_pulseio_pwmout_never_reset(pulseio_pwmout_obj_t *self) {
}
void common_hal_pulseio_pwmout_reset_ok(pulseio_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;
@ -251,18 +252,18 @@ void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) {
}
//var freq shuts down entire timer, others just their channel
if (self->variable_frequency) {
reserved_tim[self->tim->tim_index-1] = 0x00;
reserved_tim[self->tim->tim_index - 1] = 0x00;
} else {
reserved_tim[self->tim->tim_index-1] &= ~(1<<self->tim->channel_index);
reserved_tim[self->tim->tim_index - 1] &= ~(1 << self->tim->channel_index);
HAL_TIM_PWM_Stop(&self->handle, self->channel);
}
reset_pin_number(self->tim->pin->port,self->tim->pin->number);
self->tim = mp_const_none;
//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;
tim_clock_disable(1<<(self->tim->tim_index-1));
if (!reserved_tim[self->tim->tim_index - 1]) {
tim_frequencies[self->tim->tim_index - 1] = 0x00;
tim_clock_disable(1 << (self->tim->tim_index - 1));
}
}
@ -278,11 +279,14 @@ uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t* self) {
void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_t frequency) {
//don't halt setup for the same frequency
if (frequency == self->frequency) return;
if (frequency == self->frequency) {
return;
}
uint32_t prescaler = 0;
uint32_t period = 0;
timer_get_optimal_divisors(&period, &prescaler,frequency,timer_get_source_freq(self->tim->tim_index));
timer_get_optimal_divisors(&period, &prescaler, frequency,
timer_get_source_freq(self->tim->tim_index));
//shut down
HAL_TIM_PWM_Stop(&self->handle, self->channel);
@ -305,7 +309,7 @@ void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_
mp_raise_ValueError(translate("Could not restart PWM"));
}
tim_frequencies[self->tim->tim_index-1] = frequency;
tim_frequencies[self->tim->tim_index - 1] = frequency;
self->frequency = frequency;
self->period = period;
}