Merge pull request #2307 from hierophect/stm32-spi-cleanup

STM32: SPI clock rate fix and cleanup
crypto-aes
Dan Halbert 3 years ago committed by GitHub
commit e06dc72359
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 220
      ports/stm32f4/common-hal/busio/SPI.c

@ -36,46 +36,37 @@
#include "supervisor/shared/translate.h"
#include "common-hal/microcontroller/Pin.h"
STATIC bool reserved_spi[6];
STATIC bool never_reset_spi[6];
#define MAX_SPI 6 //TODO; replace this as part of periph cleanup
#define ALL_CLOCKS 0xFF
void spi_reset(void) {
#ifdef SPI1
if(!never_reset_spi[0]) {
reserved_spi[0] = false;
__HAL_RCC_SPI1_CLK_DISABLE();
}
#endif
//arrays use 0 based numbering: SPI1 is stored at index 0
STATIC bool reserved_spi[MAX_SPI];
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) {
//SPI2 and 3 are on PCLK1, if they exist.
#ifdef SPI2
if(!never_reset_spi[1]) {
reserved_spi[1] = false;
__HAL_RCC_SPI2_CLK_DISABLE();
}
if(instance == SPI2) return HAL_RCC_GetPCLK1Freq();
#endif
#ifdef SPI3
if(!never_reset_spi[2]) {
reserved_spi[2] = false;
__HAL_RCC_SPI3_CLK_DISABLE();
}
#endif
#ifdef SPI4
if(!never_reset_spi[3]) {
reserved_spi[3] = false;
__HAL_RCC_SPI4_CLK_DISABLE();
}
#endif
#ifdef SPI5
if(!never_reset_spi[4]) {
reserved_spi[4] = false;
__HAL_RCC_SPI5_CLK_DISABLE();
}
if(instance == SPI3) return HAL_RCC_GetPCLK1Freq();
#endif
#ifdef SPI6
if(!never_reset_spi[5]) {
reserved_spi[5] = false;
__HAL_RCC_SPI6_CLK_DISABLE();
return HAL_RCC_GetPCLK2Freq();
}
void spi_reset(void) {
uint16_t never_reset_mask = 0x00;
for(int i=0;i<MAX_SPI;i++) {
if (!never_reset_spi[i]) {
reserved_spi[i] = 0x00;
} else {
never_reset_mask |= 1<<i;
}
}
#endif
spi_clock_disable(ALL_CLOCKS & ~(never_reset_mask));
}
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
@ -152,42 +143,8 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
GPIO_InitStruct.Alternate = self->miso->altfn_index;
HAL_GPIO_Init(pin_port(miso->port), &GPIO_InitStruct);
#ifdef SPI1
if(SPIx==SPI1) {
reserved_spi[0] = true;
__HAL_RCC_SPI1_CLK_ENABLE();
}
#endif
#ifdef SPI2
if(SPIx==SPI2) {
reserved_spi[1] = true;
__HAL_RCC_SPI2_CLK_ENABLE();
}
#endif
#ifdef SPI3
if(SPIx==SPI3) {
reserved_spi[2] = true;
__HAL_RCC_SPI3_CLK_ENABLE();
}
#endif
#ifdef SPI4
if(SPIx==SPI4) {
reserved_spi[3] = true;
__HAL_RCC_SPI4_CLK_ENABLE();
}
#endif
#ifdef SPI5
if(SPIx==SPI5) {
reserved_spi[4] = true;
__HAL_RCC_SPI5_CLK_ENABLE();
}
#endif
#ifdef SPI6
if(SPIx==SPI6) {
reserved_spi[5] = true;
__HAL_RCC_SPI6_CLK_ENABLE();
}
#endif
spi_clock_enable(1<<(self->sck->spi_index - 1));
reserved_spi[self->sck->spi_index - 1] = true;
self->handle.Instance = SPIx;
self->handle.Init.Mode = SPI_MODE_MASTER;
@ -205,7 +162,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
{
mp_raise_ValueError(translate("SPI Init Error"));
}
self->baudrate = (HAL_RCC_GetPCLK2Freq()/16);
self->baudrate = (get_busclock(SPIx)/16);
self->prescaler = 16;
self->polarity = 0;
self->phase = 1;
@ -233,42 +190,9 @@ bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) {
}
void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
#ifdef SPI1
if(self->handle.Instance==SPI1) {
reserved_spi[0] = false;
__HAL_RCC_SPI1_CLK_DISABLE();
}
#endif
#ifdef SPI2
if(self->handle.Instance==SPI2) {
reserved_spi[1] = false;
__HAL_RCC_SPI2_CLK_DISABLE();
}
#endif
#ifdef SPI3
if(self->handle.Instance==SPI3) {
reserved_spi[2] = false;
__HAL_RCC_SPI3_CLK_DISABLE();
}
#endif
#ifdef SPI4
if(self->handle.Instance==SPI4) {
reserved_spi[3] = false;
__HAL_RCC_SPI4_CLK_DISABLE();
}
#endif
#ifdef SPI5
if(self->handle.Instance==SPI5) {
reserved_spi[4] = false;
__HAL_RCC_SPI5_CLK_DISABLE();
}
#endif
#ifdef SPI6
if(self->handle.Instance==SPI6) {
reserved_spi[5] = false;
__HAL_RCC_SPI6_CLK_DISABLE();
}
#endif
spi_clock_disable(1<<(self->sck->spi_index - 1));
reserved_spi[self->sck->spi_index - 1] = true;
reset_pin_number(self->sck->pin->port,self->sck->pin->number);
reset_pin_number(self->mosi->pin->port,self->mosi->pin->number);
reset_pin_number(self->miso->pin->port,self->miso->pin->number);
@ -277,7 +201,7 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
self->miso = mp_const_none;
}
static uint32_t stm32_baud_to_spi_div(uint32_t baudrate, uint16_t * prescaler) {
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},
@ -292,7 +216,7 @@ static uint32_t stm32_baud_to_spi_div(uint32_t baudrate, uint16_t * prescaler) {
uint16_t divisor;
do {
divisor = baud_map[i][0];
if (baudrate >= (HAL_RCC_GetPCLK2Freq()/divisor)) {
if (baudrate >= (busclock/divisor)) {
*prescaler = divisor;
return baud_map[i][1];
}
@ -312,28 +236,12 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
//Deinit SPI
HAL_SPI_DeInit(&self->handle);
if (bits == 8) {
self->handle.Init.DataSize = SPI_DATASIZE_8BIT;
} else if (bits == 16) {
self->handle.Init.DataSize = SPI_DATASIZE_16BIT;
} else {
return false;
}
if (polarity) {
self->handle.Init.CLKPolarity = SPI_POLARITY_HIGH;
} else {
self->handle.Init.CLKPolarity = SPI_POLARITY_LOW;
}
if (phase) {
self->handle.Init.CLKPhase = SPI_PHASE_2EDGE;
} else {
self->handle.Init.CLKPhase = SPI_PHASE_1EDGE;
}
self->handle.Init.BaudRatePrescaler = stm32_baud_to_spi_div(baudrate, &self->prescaler);
self->handle.Init.DataSize = (bits == 16) ? SPI_DATASIZE_16BIT : SPI_DATASIZE_8BIT;
self->handle.Init.CLKPolarity = (polarity) ? SPI_POLARITY_HIGH : SPI_POLARITY_LOW;
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));
self->handle.Init.Mode = SPI_MODE_MASTER;
self->handle.Init.Direction = SPI_DIRECTION_2LINES;
self->handle.Init.NSS = SPI_NSS_SOFT;
@ -362,10 +270,10 @@ bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) {
// __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);
@ -412,4 +320,46 @@ uint8_t common_hal_busio_spi_get_phase(busio_spi_obj_t* self) {
uint8_t common_hal_busio_spi_get_polarity(busio_spi_obj_t* self) {
return self->polarity;
}
}
STATIC void spi_clock_enable(uint8_t mask) {
#ifdef SPI1
if (mask & 1<<0) __HAL_RCC_SPI1_CLK_ENABLE();
#endif
#ifdef SPI2
if (mask & 1<<1) __HAL_RCC_SPI2_CLK_ENABLE();
#endif
#ifdef SPI3
if (mask & 1<<2) __HAL_RCC_SPI3_CLK_ENABLE();
#endif
#ifdef SPI4
if (mask & 1<<3) __HAL_RCC_SPI4_CLK_ENABLE();
#endif
#ifdef SPI5
if (mask & 1<<4) __HAL_RCC_SPI5_CLK_ENABLE();
#endif
#ifdef SPI6
if (mask & 1<<5) __HAL_RCC_SPI6_CLK_ENABLE();
#endif
}
STATIC void spi_clock_disable(uint8_t mask) {
#ifdef SPI1
if (mask & 1<<0) __HAL_RCC_SPI1_CLK_DISABLE();
#endif
#ifdef SPI2
if (mask & 1<<1) __HAL_RCC_SPI2_CLK_DISABLE();
#endif
#ifdef SPI3
if (mask & 1<<2) __HAL_RCC_SPI3_CLK_DISABLE();
#endif
#ifdef SPI4
if (mask & 1<<3) __HAL_RCC_SPI4_CLK_DISABLE();
#endif
#ifdef SPI5
if (mask & 1<<4) __HAL_RCC_SPI5_CLK_DISABLE();
#endif
#ifdef SPI6
if (mask & 1<<5) __HAL_RCC_SPI6_CLK_DISABLE();
#endif
}

Loading…
Cancel
Save