Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STM32: DMA Module and SPI Peripheral #3619

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ports/stm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ SRC_C += \
boards/$(BOARD)/board.c \
boards/$(BOARD)/pins.c \
peripherals/timers.c \
peripherals/dma.c \
peripherals/stm32$(MCU_SERIES_LOWER)/clocks.c \
peripherals/stm32$(MCU_SERIES_LOWER)/$(MCU_VARIANT_LOWER)/pins.c \
peripherals/stm32$(MCU_SERIES_LOWER)/$(MCU_VARIANT_LOWER)/gpio.c \
Expand Down
24 changes: 7 additions & 17 deletions ports/stm/common-hal/busio/SPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,6 @@
// 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
#define MAX_SPI 6

STATIC bool reserved_spi[MAX_SPI];
STATIC bool never_reset_spi[MAX_SPI];

#define ALL_CLOCKS 0xFF
STATIC void spi_clock_enable(uint8_t mask);
STATIC void spi_clock_disable(uint8_t mask);

STATIC uint32_t get_busclock(SPI_TypeDef * instance) {
#if (CPY_STM32H7)
if (instance == SPI1 || instance == SPI2 || instance == SPI3) {
Expand Down Expand Up @@ -108,7 +98,7 @@ void spi_reset(void) {
spi_clock_disable(ALL_CLOCKS & ~(never_reset_mask));
}

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) {
const mcu_periph_obj_t *spi_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 ) {
return table;
Expand All @@ -118,7 +108,7 @@ STATIC const mcu_periph_obj_t *find_pin_function(const mcu_periph_obj_t *table,
}

//match pins to SPI objects
STATIC int check_pins(busio_spi_obj_t *self,
int spi_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) {
bool spi_taken = false;
Expand All @@ -137,12 +127,12 @@ STATIC int check_pins(busio_spi_obj_t *self,
int periph_index = mcu_spi_sck->periph_index;

const mcu_periph_obj_t *mcu_spi_miso = NULL;
if (miso && !(mcu_spi_miso = find_pin_function(mcu_spi_miso_list, miso_len, miso, periph_index))) {
if (miso && !(mcu_spi_miso = spi_find_pin_function(mcu_spi_miso_list, miso_len, miso, periph_index))) {
continue;
}

const mcu_periph_obj_t *mcu_spi_mosi = NULL;
if (mosi && !(mcu_spi_mosi = find_pin_function(mcu_spi_mosi_list, mosi_len, mosi, periph_index))) {
if (mosi && !(mcu_spi_mosi = spi_find_pin_function(mcu_spi_mosi_list, mosi_len, mosi, periph_index))) {
continue;
}

Expand All @@ -169,7 +159,7 @@ 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) {

int periph_index = check_pins(self, sck, mosi, miso);
int periph_index = spi_check_pins(self, sck, mosi, miso);
SPI_TypeDef * SPIx = mcu_spi_banks[periph_index - 1];

//Start GPIO for each pin
Expand Down Expand Up @@ -375,7 +365,7 @@ uint8_t common_hal_busio_spi_get_polarity(busio_spi_obj_t* self) {
return self->polarity;
}

STATIC void spi_clock_enable(uint8_t mask) {
void spi_clock_enable(uint8_t mask) {
#ifdef SPI1
if (mask & (1 << 0)) {
__HAL_RCC_SPI1_CLK_ENABLE();
Expand Down Expand Up @@ -408,7 +398,7 @@ STATIC void spi_clock_enable(uint8_t mask) {
#endif
}

STATIC void spi_clock_disable(uint8_t mask) {
void spi_clock_disable(uint8_t mask) {
#ifdef SPI1
if (mask & (1 << 0)) {
__HAL_RCC_SPI1_CLK_DISABLE();
Expand Down
17 changes: 16 additions & 1 deletion ports/stm/common-hal/busio/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,33 @@
typedef struct {
mp_obj_base_t base;
SPI_HandleTypeDef handle;
IRQn_Type irq;
bool has_lock;
const mcu_periph_obj_t *sck;
const mcu_periph_obj_t *mosi;
const mcu_periph_obj_t *miso;
const mcu_periph_obj_t *nss;
const mcu_pin_obj_t *cs_pin;
uint32_t baudrate;
uint16_t prescaler;
uint8_t polarity;
uint8_t phase;
uint8_t bits;
} busio_spi_obj_t;

//arrays use 0 based numbering: SPI1 is stored at index 0
#define MAX_SPI 6
#define ALL_CLOCKS 0xFF

bool reserved_spi[MAX_SPI];
bool never_reset_spi[MAX_SPI];

const mcu_periph_obj_t *spi_find_pin_function(const mcu_periph_obj_t *table, size_t sz,
const mcu_pin_obj_t *pin, int periph_index);
int spi_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);
void spi_reset(void);
void spi_clock_enable(uint8_t mask);
void spi_clock_disable(uint8_t mask);

#endif // MICROPY_INCLUDED_STM32_COMMON_HAL_BUSIO_SPI_H
Loading