style edits

This commit is contained in:
Hierophect 2020-01-08 10:53:53 -05:00
parent d645ea222d
commit 3d5528b88f

View File

@ -219,8 +219,12 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
self->bits = 8; self->bits = 8;
claim_pin(sck); claim_pin(sck);
if (self->mosi != NULL) claim_pin(mosi); if (self->mosi != NULL) {
if (self->miso != NULL) claim_pin(miso); claim_pin(mosi);
}
if (self->miso != NULL) {
claim_pin(miso);
}
} }
void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) { void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
@ -228,8 +232,12 @@ void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
if (mcu_spi_banks[i] == self->handle.Instance) { if (mcu_spi_banks[i] == self->handle.Instance) {
never_reset_spi[i] = true; never_reset_spi[i] = true;
never_reset_pin_number(self->sck->pin->port, self->sck->pin->number); never_reset_pin_number(self->sck->pin->port, self->sck->pin->number);
if (self->mosi != NULL) never_reset_pin_number(self->mosi->pin->port, self->mosi->pin->number); if (self->mosi != NULL) {
if (self->miso != NULL) never_reset_pin_number(self->miso->pin->port, self->miso->pin->number); never_reset_pin_number(self->mosi->pin->port, self->mosi->pin->number);
}
if (self->miso != NULL) {
never_reset_pin_number(self->miso->pin->port, self->miso->pin->number);
}
break; break;
} }
} }
@ -245,8 +253,12 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
never_reset_spi[self->sck->spi_index - 1] = false; never_reset_spi[self->sck->spi_index - 1] = false;
reset_pin_number(self->sck->pin->port,self->sck->pin->number); reset_pin_number(self->sck->pin->port,self->sck->pin->number);
if (self->mosi != NULL) reset_pin_number(self->mosi->pin->port,self->mosi->pin->number); if (self->mosi != NULL) {
if (self->miso != NULL) reset_pin_number(self->miso->pin->port,self->miso->pin->number); reset_pin_number(self->mosi->pin->port,self->mosi->pin->number);
}
if (self->miso != NULL) {
reset_pin_number(self->miso->pin->port,self->miso->pin->number);
}
self->sck = mp_const_none; self->sck = mp_const_none;
self->mosi = mp_const_none; self->mosi = mp_const_none;
self->miso = mp_const_none; self->miso = mp_const_none;
@ -282,7 +294,9 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) { uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {
//This resets the SPI, so check before updating it redundantly //This resets the SPI, so check before updating it redundantly
if (baudrate == self->baudrate && polarity== self->polarity if (baudrate == self->baudrate && polarity== self->polarity
&& phase == self->phase && bits == self->bits) return true; && phase == self->phase && bits == self->bits) {
return true;
}
//Deinit SPI //Deinit SPI
HAL_SPI_DeInit(&self->handle); HAL_SPI_DeInit(&self->handle);
@ -335,22 +349,27 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
bool common_hal_busio_spi_write(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")); if (self->mosi == NULL) {
mp_raise_ValueError(translate("No MOSI Pin"));
}
HAL_StatusTypeDef result = HAL_SPI_Transmit (&self->handle, (uint8_t *)data, (uint16_t)len, HAL_MAX_DELAY); HAL_StatusTypeDef result = HAL_SPI_Transmit (&self->handle, (uint8_t *)data, (uint16_t)len, HAL_MAX_DELAY);
return result == HAL_OK; return result == HAL_OK;
} }
bool common_hal_busio_spi_read(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")); if (self->miso == NULL) {
mp_raise_ValueError(translate("No MISO Pin"));
}
HAL_StatusTypeDef result = HAL_SPI_Receive (&self->handle, data, (uint16_t)len, HAL_MAX_DELAY); HAL_StatusTypeDef result = HAL_SPI_Receive (&self->handle, data, (uint16_t)len, HAL_MAX_DELAY);
return result == HAL_OK; return result == HAL_OK;
} }
bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, bool common_hal_busio_spi_transfer(busio_spi_obj_t *self,
uint8_t *data_out, uint8_t *data_in, size_t len) { uint8_t *data_out, uint8_t *data_in, size_t len) {
if (self->miso == NULL) mp_raise_ValueError(translate("No MISO Pin")); if (self->miso == NULL || self->mosi == NULL) {
if (self->mosi == NULL) mp_raise_ValueError(translate("No MOSI Pin")); mp_raise_ValueError(translate("Missing MISO or MOSI Pin"));
}
HAL_StatusTypeDef result = HAL_SPI_TransmitReceive (&self->handle, HAL_StatusTypeDef result = HAL_SPI_TransmitReceive (&self->handle,
data_out, data_in, (uint16_t)len,HAL_MAX_DELAY); data_out, data_in, (uint16_t)len,HAL_MAX_DELAY);
return result == HAL_OK; return result == HAL_OK;