commit
d6e987e587
@ -137,6 +137,7 @@ SRC_STM32 = \
|
||||
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_fsmc.c \
|
||||
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sram.c \
|
||||
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c \
|
||||
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c \
|
||||
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_i2c.c \
|
||||
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_dma.c \
|
||||
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c \
|
||||
|
@ -60,7 +60,7 @@
|
||||
/* #define HAL_SAI_MODULE_ENABLED */
|
||||
#define HAL_SD_MODULE_ENABLED
|
||||
/* #define HAL_MMC_MODULE_ENABLED */
|
||||
/* #define HAL_SPI_MODULE_ENABLED */
|
||||
#define HAL_SPI_MODULE_ENABLED
|
||||
/* #define HAL_TIM_MODULE_ENABLED */
|
||||
#define HAL_UART_MODULE_ENABLED
|
||||
/* #define HAL_USART_MODULE_ENABLED */
|
||||
@ -70,7 +70,6 @@
|
||||
#define HAL_PCD_MODULE_ENABLED
|
||||
/* #define HAL_HCD_MODULE_ENABLED */
|
||||
/* #define HAL_DSI_MODULE_ENABLED */
|
||||
/* #define HAL_QSPI_MODULE_ENABLED */
|
||||
#define HAL_QSPI_MODULE_ENABLED
|
||||
/* #define HAL_CEC_MODULE_ENABLED */
|
||||
/* #define HAL_FMPI2C_MODULE_ENABLED */
|
||||
|
@ -4,6 +4,7 @@
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 Scott Shawcroft
|
||||
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@ -23,37 +24,352 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "shared-bindings/busio/SPI.h"
|
||||
#include "py/mperrno.h"
|
||||
#include "py/runtime.h"
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
#include "shared-bindings/microcontroller/__init__.h"
|
||||
#include "boards/board.h"
|
||||
#include "supervisor/shared/translate.h"
|
||||
#include "common-hal/microcontroller/Pin.h"
|
||||
|
||||
STATIC bool reserved_spi[6];
|
||||
STATIC bool never_reset_spi[6];
|
||||
|
||||
void spi_reset(void) {
|
||||
#ifdef SPI1
|
||||
if(!never_reset_spi[0]) {
|
||||
reserved_spi[0] = false;
|
||||
__HAL_RCC_SPI1_CLK_DISABLE();
|
||||
}
|
||||
#endif
|
||||
#ifdef SPI2
|
||||
if(!never_reset_spi[1]) {
|
||||
reserved_spi[1] = false;
|
||||
__HAL_RCC_SPI2_CLK_DISABLE();
|
||||
}
|
||||
#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();
|
||||
}
|
||||
#endif
|
||||
#ifdef SPI6
|
||||
if(!never_reset_spi[5]) {
|
||||
reserved_spi[5] = false;
|
||||
__HAL_RCC_SPI6_CLK_DISABLE();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
|
||||
const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi,
|
||||
const mcu_pin_obj_t * sck, const mcu_pin_obj_t * mosi,
|
||||
const mcu_pin_obj_t * miso) {
|
||||
mp_raise_NotImplementedError(translate("SPI not yet supported"));
|
||||
|
||||
//match pins to SPI objects
|
||||
SPI_TypeDef * SPIx;
|
||||
|
||||
uint8_t sck_len = sizeof(mcu_spi_sck_list)/sizeof(*mcu_spi_sck_list);
|
||||
uint8_t mosi_len = sizeof(mcu_spi_mosi_list)/sizeof(*mcu_spi_mosi_list);
|
||||
uint8_t miso_len = sizeof(mcu_spi_miso_list)/sizeof(*mcu_spi_miso_list);
|
||||
|
||||
bool spi_taken = false;
|
||||
//sck
|
||||
for(uint i=0; i<sck_len;i++) {
|
||||
if (mcu_spi_sck_list[i].pin == sck) {
|
||||
//mosi
|
||||
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++) {
|
||||
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]) {
|
||||
spi_taken = true;
|
||||
continue;
|
||||
}
|
||||
//store pins if not
|
||||
self->sck = &mcu_spi_sck_list[j];
|
||||
self->mosi = &mcu_spi_mosi_list[j];
|
||||
self->miso = &mcu_spi_miso_list[k];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//handle typedef selection, errors
|
||||
if(self->sck!=NULL && self->mosi!=NULL && self->miso!=NULL ) {
|
||||
SPIx = mcu_spi_banks[self->sck->spi_index-1];
|
||||
} else {
|
||||
if (spi_taken) {
|
||||
mp_raise_RuntimeError(translate("Hardware busy, try alternative pins"));
|
||||
} else {
|
||||
mp_raise_RuntimeError(translate("Invalid SPI pin selection"));
|
||||
}
|
||||
}
|
||||
|
||||
//Start GPIO for each pin
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_InitStruct.Pin = pin_mask(sck->number);
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = self->sck->altfn_index;
|
||||
HAL_GPIO_Init(pin_port(sck->port), &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = pin_mask(mosi->number);
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = self->mosi->altfn_index;
|
||||
HAL_GPIO_Init(pin_port(mosi->port), &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = pin_mask(miso->number);
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
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
|
||||
|
||||
self->handle.Instance = SPIx;
|
||||
self->handle.Init.Mode = SPI_MODE_MASTER;
|
||||
self->handle.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
self->handle.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
self->handle.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
self->handle.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||
self->handle.Init.NSS = SPI_NSS_SOFT;
|
||||
self->handle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
|
||||
self->handle.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
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)
|
||||
{
|
||||
mp_raise_RuntimeError(translate("SPI Init Error"));
|
||||
}
|
||||
self->baudrate = (HAL_RCC_GetPCLK2Freq()/16);
|
||||
self->prescaler = 16;
|
||||
self->polarity = 0;
|
||||
self->phase = 1;
|
||||
self->bits = 8;
|
||||
|
||||
claim_pin(sck);
|
||||
claim_pin(mosi);
|
||||
claim_pin(miso);
|
||||
}
|
||||
|
||||
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++) {
|
||||
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);
|
||||
never_reset_pin_number(self->mosi->pin->port, self->mosi->pin->number);
|
||||
never_reset_pin_number(self->miso->pin->port, self->miso->pin->number);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) {
|
||||
return 0;
|
||||
return self->sck->pin == mp_const_none;
|
||||
}
|
||||
|
||||
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
|
||||
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);
|
||||
self->sck = mp_const_none;
|
||||
self->mosi = mp_const_none;
|
||||
self->miso = mp_const_none;
|
||||
}
|
||||
|
||||
static uint32_t stm32_baud_to_spi_div(uint32_t baudrate, uint16_t * prescaler) {
|
||||
static const uint32_t baud_map[8][2] = {
|
||||
{2,SPI_BAUDRATEPRESCALER_2},
|
||||
{4,SPI_BAUDRATEPRESCALER_4},
|
||||
{8,SPI_BAUDRATEPRESCALER_8},
|
||||
{16,SPI_BAUDRATEPRESCALER_16},
|
||||
{32,SPI_BAUDRATEPRESCALER_32},
|
||||
{64,SPI_BAUDRATEPRESCALER_64},
|
||||
{128,SPI_BAUDRATEPRESCALER_128},
|
||||
{256,SPI_BAUDRATEPRESCALER_256}
|
||||
};
|
||||
size_t i = 0;
|
||||
uint16_t divisor;
|
||||
do {
|
||||
divisor = baud_map[i][0];
|
||||
if (baudrate >= (HAL_RCC_GetPCLK2Freq()/divisor)) {
|
||||
*prescaler = divisor;
|
||||
return baud_map[i][1];
|
||||
}
|
||||
i++;
|
||||
} while (divisor != 256);
|
||||
//only gets here if requested baud is lower than minimum
|
||||
*prescaler = 256;
|
||||
return SPI_BAUDRATEPRESCALER_256;
|
||||
}
|
||||
|
||||
bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
|
||||
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {
|
||||
return true;
|
||||
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
|
||||
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.Mode = SPI_MODE_MASTER;
|
||||
self->handle.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
self->handle.Init.NSS = SPI_NSS_SOFT;
|
||||
self->handle.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
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)
|
||||
{
|
||||
mp_raise_RuntimeError(translate("SPI Re-initialization error"));
|
||||
}
|
||||
|
||||
self->baudrate = baudrate;
|
||||
self->polarity = polarity;
|
||||
self->phase = phase;
|
||||
self->bits = bits;
|
||||
return true;
|
||||
}
|
||||
|
||||
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.
|
||||
// uint32_t store_primask = __get_PRIMASK();
|
||||
// __disable_irq();
|
||||
// __DMB();
|
||||
|
||||
if (!self->has_lock) {
|
||||
grabbed_lock = true;
|
||||
self->has_lock = true;
|
||||
}
|
||||
|
||||
// __DMB();
|
||||
// __set_PRIMASK(store_primask);
|
||||
|
||||
return grabbed_lock;
|
||||
}
|
||||
|
||||
@ -67,26 +383,33 @@ 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) {
|
||||
return 0;
|
||||
HAL_StatusTypeDef result = HAL_SPI_Transmit (&self->handle, (uint8_t *)data, (uint16_t)len, 2);
|
||||
return result == HAL_OK ? 1 : 0;
|
||||
}
|
||||
|
||||
bool common_hal_busio_spi_read(busio_spi_obj_t *self,
|
||||
uint8_t *data, size_t len, uint8_t write_value) {
|
||||
return 0;
|
||||
HAL_StatusTypeDef result = HAL_SPI_Receive (&self->handle, data, (uint16_t)len, 2);
|
||||
return result == HAL_OK ? 1 : 0;
|
||||
}
|
||||
|
||||
bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, uint8_t *data_out, uint8_t *data_in, size_t len) {
|
||||
return 0;
|
||||
bool common_hal_busio_spi_transfer(busio_spi_obj_t *self,
|
||||
uint8_t *data_out, uint8_t *data_in, size_t len) {
|
||||
HAL_StatusTypeDef result = HAL_SPI_TransmitReceive (&self->handle,
|
||||
data_out, data_in, (uint16_t)len,2);
|
||||
return result == HAL_OK ? 1 : 0;
|
||||
}
|
||||
|
||||
uint32_t common_hal_busio_spi_get_frequency(busio_spi_obj_t* self) {
|
||||
return 0;
|
||||
//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) {
|
||||
return 0;
|
||||
return self->phase;
|
||||
}
|
||||
|
||||
uint8_t common_hal_busio_spi_get_polarity(busio_spi_obj_t* self) {
|
||||
return 0;
|
||||
return self->polarity;
|
||||
}
|
@ -4,6 +4,7 @@
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 Scott Shawcroft
|
||||
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@ -29,14 +30,26 @@
|
||||
|
||||
#include "common-hal/microcontroller/Pin.h"
|
||||
|
||||
#include "stm32f4xx_hal.h"
|
||||
#include "stm32f4/periph.h"
|
||||
|
||||
#include "py/obj.h"
|
||||
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
SPI_HandleTypeDef handle;
|
||||
bool has_lock;
|
||||
uint8_t clock_pin;
|
||||
uint8_t MOSI_pin;
|
||||
uint8_t MISO_pin;
|
||||
const mcu_spi_sck_obj_t *sck;
|
||||
const mcu_spi_mosi_obj_t *mosi;
|
||||
const mcu_spi_miso_obj_t *miso;
|
||||
const mcu_spi_nss_obj_t *nss;
|
||||
uint32_t baudrate;
|
||||
uint16_t prescaler;
|
||||
uint8_t polarity;
|
||||
uint8_t phase;
|
||||
uint8_t bits;
|
||||
} busio_spi_obj_t;
|
||||
|
||||
void spi_reset(void);
|
||||
|
||||
#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_BUSIO_SPI_H
|
||||
|
@ -23,6 +23,7 @@ CIRCUITPY_BUSIO = 1
|
||||
CIRCUITPY_TIME = 1
|
||||
CIRCUITPY_OS = 1
|
||||
CIRCUITPY_STRUCT = 1
|
||||
CIRCUITPY_MATH = 1
|
||||
|
||||
#ifeq ($(MCU_SUB_VARIANT), stm32f412zx)
|
||||
#endif
|
||||
|
@ -33,17 +33,19 @@
|
||||
#include "stm32f4xx_hal.h"
|
||||
#include "stm32f4/pins.h"
|
||||
|
||||
// Address Version
|
||||
// I2C
|
||||
//TODO: these objects should be condensed into a single 'periph_pin' unless we
|
||||
//find a compelling reason to store more unique data in them.
|
||||
|
||||
typedef struct {
|
||||
uint8_t i2c_index:4; // Index of the I2C unit (1 to 3)
|
||||
uint8_t altfn_index:4; //Index of the altfn for this pin (0 to 15)
|
||||
const mcu_pin_obj_t * pin;
|
||||
} mcu_i2c_sda_obj_t;
|
||||
|
||||
// Address Version
|
||||
typedef struct {
|
||||
uint8_t i2c_index:4; // Index of the I2C unit (1 to 3)
|
||||
uint8_t altfn_index:4; //Index of the altfn for this pin (0 to 15)
|
||||
uint8_t i2c_index:4;
|
||||
uint8_t altfn_index:4;
|
||||
const mcu_pin_obj_t * pin;
|
||||
} mcu_i2c_scl_obj_t;
|
||||
|
||||
@ -62,6 +64,41 @@ typedef struct {
|
||||
.pin = scl_pin, \
|
||||
}
|
||||
|
||||
// SPI
|
||||
//TODO: these objects should be condensed into a single 'periph_pin' unless we
|
||||
//find a compelling reason to store more unique data in them.
|
||||
|
||||
typedef struct {
|
||||
uint8_t spi_index:4; //Up to 6 SPI units
|
||||
uint8_t altfn_index:4; //Up to 15 alt channels
|
||||
const mcu_pin_obj_t * pin;
|
||||
} mcu_spi_sck_obj_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t spi_index:4;
|
||||
uint8_t altfn_index:4;
|
||||
const mcu_pin_obj_t * pin;
|
||||
} mcu_spi_mosi_obj_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t spi_index:4;
|
||||
uint8_t altfn_index:4;
|
||||
const mcu_pin_obj_t * pin;
|
||||
} mcu_spi_miso_obj_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t spi_index:4;
|
||||
uint8_t altfn_index:4;
|
||||
const mcu_pin_obj_t * pin;
|
||||
} mcu_spi_nss_obj_t;
|
||||
|
||||
#define SPI(index, alt, spi_pin) \
|
||||
{ \
|
||||
.spi_index = index, \
|
||||
.altfn_index = alt, \
|
||||
.pin = spi_pin, \
|
||||
}
|
||||
|
||||
// TODO: SPI, UART, etc
|
||||
|
||||
// Choose based on chip
|
||||
|
@ -46,4 +46,43 @@ const mcu_i2c_scl_obj_t mcu_i2c_scl_list[4] = {
|
||||
I2C_SCL(2, 4, &pin_PB10),
|
||||
I2C_SCL(3, 4, &pin_PA08)
|
||||
};
|
||||
//SPI, UART, Etc
|
||||
|
||||
SPI_TypeDef * mcu_spi_banks[3] = {SPI1, SPI2, SPI3};
|
||||
|
||||
const mcu_spi_sck_obj_t mcu_spi_sck_list[7] = {
|
||||
SPI(1, 5, &pin_PA05),
|
||||
SPI(1, 5, &pin_PB03),
|
||||
SPI(2, 5, &pin_PB10),
|
||||
SPI(2, 5, &pin_PB13),
|
||||
SPI(2, 5, &pin_PC07),
|
||||
SPI(3, 6, &pin_PB03),
|
||||
SPI(3, 6, &pin_PC10),
|
||||
};
|
||||
|
||||
const mcu_spi_mosi_obj_t mcu_spi_mosi_list[6] = {
|
||||
SPI(1, 5, &pin_PA07),
|
||||
SPI(1, 5, &pin_PB05),
|
||||
SPI(2, 5, &pin_PB15),
|
||||
SPI(2, 5, &pin_PC03),
|
||||
SPI(3, 6, &pin_PB05),
|
||||
SPI(3, 6, &pin_PC12),
|
||||
};
|
||||
|
||||
const mcu_spi_miso_obj_t mcu_spi_miso_list[6] = {
|
||||
SPI(1, 5, &pin_PA06),
|
||||
SPI(1, 5, &pin_PB04),
|
||||
SPI(2, 5, &pin_PB14),
|
||||
SPI(2, 5, &pin_PC02),
|
||||
SPI(3, 6, &pin_PB04),
|
||||
SPI(3, 6, &pin_PC11),
|
||||
};
|
||||
|
||||
const mcu_spi_nss_obj_t mcu_spi_nss_list[6] = {
|
||||
SPI(1, 5, &pin_PA04),
|
||||
SPI(1, 5, &pin_PA15),
|
||||
SPI(2, 5, &pin_PB09),
|
||||
SPI(2, 5, &pin_PB12),
|
||||
SPI(3, 6, &pin_PA04),
|
||||
SPI(3, 6, &pin_PA15),
|
||||
};
|
||||
//UART, Etc
|
||||
|
@ -24,8 +24,8 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H
|
||||
#define MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H
|
||||
#ifndef MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F405XX_PERIPH_H
|
||||
#define MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F405XX_PERIPH_H
|
||||
|
||||
//I2C
|
||||
extern I2C_TypeDef * mcu_i2c_banks[3];
|
||||
@ -33,5 +33,13 @@ extern I2C_TypeDef * mcu_i2c_banks[3];
|
||||
extern const mcu_i2c_sda_obj_t mcu_i2c_sda_list[4];
|
||||
extern const mcu_i2c_scl_obj_t mcu_i2c_scl_list[4];
|
||||
|
||||
//SPI
|
||||
extern SPI_TypeDef * mcu_spi_banks[3];
|
||||
|
||||
#endif // MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H
|
||||
extern const mcu_spi_sck_obj_t mcu_spi_sck_list[7];
|
||||
extern const mcu_spi_mosi_obj_t mcu_spi_mosi_list[6];
|
||||
extern const mcu_spi_miso_obj_t mcu_spi_miso_list[6];
|
||||
extern const mcu_spi_nss_obj_t mcu_spi_nss_list[6];
|
||||
|
||||
|
||||
#endif // MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F405XX_PERIPH_H
|
@ -49,4 +49,74 @@ const mcu_i2c_scl_obj_t mcu_i2c_scl_list[4] = {
|
||||
I2C_SCL(2, 4, &pin_PB10),
|
||||
I2C_SCL(3, 4, &pin_PA08)
|
||||
};
|
||||
//SPI, UART, Etc
|
||||
|
||||
// SPI
|
||||
|
||||
SPI_TypeDef * mcu_spi_banks[5] = {SPI1, SPI2, SPI3, SPI4, SPI5};
|
||||
|
||||
const mcu_spi_sck_obj_t mcu_spi_sck_list[15] = {
|
||||
SPI(1, 5, &pin_PA05),
|
||||
SPI(1, 5, &pin_PB03),
|
||||
SPI(2, 5, &pin_PB10),
|
||||
SPI(2, 5, &pin_PB13),
|
||||
SPI(2, 5, &pin_PC07),
|
||||
SPI(2, 5, &pin_PD03),
|
||||
SPI(3, 6, &pin_PB03),
|
||||
SPI(3, 7, &pin_PB12),
|
||||
SPI(3, 6, &pin_PC10),
|
||||
SPI(4, 6, &pin_PB13),
|
||||
SPI(4, 5, &pin_PE02),
|
||||
SPI(4, 5, &pin_PE12),
|
||||
SPI(5, 6, &pin_PB00),
|
||||
SPI(5, 6, &pin_PE02),
|
||||
SPI(5, 6, &pin_PE12)
|
||||
};
|
||||
|
||||
const mcu_spi_mosi_obj_t mcu_spi_mosi_list[14] = {
|
||||
SPI(1, 5, &pin_PA07),
|
||||
SPI(1, 5, &pin_PB05),
|
||||
SPI(2, 5, &pin_PB15),
|
||||
SPI(2, 5, &pin_PC03),
|
||||
SPI(3, 6, &pin_PB05),
|
||||
SPI(3, 6, &pin_PC12),
|
||||
SPI(3, 5, &pin_PD06),
|
||||
SPI(4, 5, &pin_PA01),
|
||||
SPI(4, 5, &pin_PE06),
|
||||
SPI(4, 5, &pin_PE14),
|
||||
SPI(5, 6, &pin_PA10),
|
||||
SPI(5, 6, &pin_PB08),
|
||||
SPI(5, 6, &pin_PE06),
|
||||
SPI(5, 6, &pin_PE14)
|
||||
};
|
||||
|
||||
const mcu_spi_miso_obj_t mcu_spi_miso_list[12] = {
|
||||
SPI(1, 5, &pin_PA06),
|
||||
SPI(1, 5, &pin_PB04),
|
||||
SPI(2, 5, &pin_PB14),
|
||||
SPI(2, 5, &pin_PC02),
|
||||
SPI(3, 6, &pin_PB04),
|
||||
SPI(3, 6, &pin_PC11),
|
||||
SPI(4, 6, &pin_PA11),
|
||||
SPI(4, 5, &pin_PE05),
|
||||
SPI(4, 5, &pin_PE13),
|
||||
SPI(5, 6, &pin_PA12),
|
||||
SPI(5, 6, &pin_PE05),
|
||||
SPI(5, 6, &pin_PE13)
|
||||
};
|
||||
|
||||
const mcu_spi_nss_obj_t mcu_spi_nss_list[12] = {
|
||||
SPI(1, 5, &pin_PA04),
|
||||
SPI(1, 5, &pin_PA15),
|
||||
SPI(2, 5, &pin_PB09),
|
||||
SPI(2, 5, &pin_PB12),
|
||||
SPI(3, 6, &pin_PA04),
|
||||
SPI(3, 6, &pin_PA15),
|
||||
SPI(4, 6, &pin_PB12),
|
||||
SPI(4, 5, &pin_PE04),
|
||||
SPI(4, 5, &pin_PE11),
|
||||
SPI(5, 6, &pin_PB01),
|
||||
SPI(5, 6, &pin_PE04),
|
||||
SPI(5, 6, &pin_PE11)
|
||||
};
|
||||
|
||||
//UART, Etc
|
||||
|
@ -33,4 +33,12 @@ extern I2C_TypeDef * mcu_i2c_banks[3];
|
||||
extern const mcu_i2c_sda_obj_t mcu_i2c_sda_list[7];
|
||||
extern const mcu_i2c_scl_obj_t mcu_i2c_scl_list[4];
|
||||
|
||||
//SPI
|
||||
extern SPI_TypeDef * mcu_spi_banks[5];
|
||||
|
||||
extern const mcu_spi_sck_obj_t mcu_spi_sck_list[15];
|
||||
extern const mcu_spi_mosi_obj_t mcu_spi_mosi_list[14];
|
||||
extern const mcu_spi_miso_obj_t mcu_spi_miso_list[12];
|
||||
extern const mcu_spi_nss_obj_t mcu_spi_nss_list[12];
|
||||
|
||||
#endif // MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H
|
@ -50,4 +50,74 @@ const mcu_i2c_scl_obj_t mcu_i2c_scl_list[4] = {
|
||||
I2C_SCL(2, 4, &pin_PB10),
|
||||
I2C_SCL(3, 4, &pin_PA08)
|
||||
};
|
||||
//SPI, UART, Etc
|
||||
|
||||
// SPI
|
||||
|
||||
SPI_TypeDef * mcu_spi_banks[5] = {SPI1, SPI2, SPI3, SPI4, SPI5};
|
||||
|
||||
const mcu_spi_sck_obj_t mcu_spi_sck_list[15] = {
|
||||
SPI(1, 5, &pin_PA05),
|
||||
SPI(1, 5, &pin_PB03),
|
||||
SPI(2, 5, &pin_PB10),
|
||||
SPI(2, 5, &pin_PB13),
|
||||
SPI(2, 5, &pin_PC07),
|
||||
SPI(2, 5, &pin_PD03),
|
||||
SPI(3, 6, &pin_PB03),
|
||||
SPI(3, 7, &pin_PB12),
|
||||
SPI(3, 6, &pin_PC10),
|
||||
SPI(4, 6, &pin_PB13),
|
||||
SPI(4, 5, &pin_PE02),
|
||||
SPI(4, 5, &pin_PE12),
|
||||
SPI(5, 6, &pin_PB00),
|
||||
SPI(5, 6, &pin_PE02),
|
||||
SPI(5, 6, &pin_PE12)
|
||||
};
|
||||
|
||||
const mcu_spi_mosi_obj_t mcu_spi_mosi_list[14] = {
|
||||
SPI(1, 5, &pin_PA07),
|
||||
SPI(1, 5, &pin_PB05),
|
||||
SPI(2, 5, &pin_PB15),
|
||||
SPI(2, 5, &pin_PC03),
|
||||
SPI(3, 6, &pin_PB05),
|
||||
SPI(3, 6, &pin_PC12),
|
||||
SPI(3, 5, &pin_PD06),
|
||||
SPI(4, 5, &pin_PA01),
|
||||
SPI(4, 5, &pin_PE06),
|
||||
SPI(4, 5, &pin_PE14),
|
||||
SPI(5, 6, &pin_PA10),
|
||||
SPI(5, 6, &pin_PB08),
|
||||
SPI(5, 6, &pin_PE06),
|
||||
SPI(5, 6, &pin_PE14)
|
||||
};
|
||||
|
||||
const mcu_spi_miso_obj_t mcu_spi_miso_list[12] = {
|
||||
SPI(1, 5, &pin_PA06),
|
||||
SPI(1, 5, &pin_PB04),
|
||||
SPI(2, 5, &pin_PB14),
|
||||
SPI(2, 5, &pin_PC02),
|
||||
SPI(3, 6, &pin_PB04),
|
||||
SPI(3, 6, &pin_PC11),
|
||||
SPI(4, 6, &pin_PA11),
|
||||
SPI(4, 5, &pin_PE05),
|
||||
SPI(4, 5, &pin_PE13),
|
||||
SPI(5, 6, &pin_PA12),
|
||||
SPI(5, 6, &pin_PE05),
|
||||
SPI(5, 6, &pin_PE13)
|
||||
};
|
||||
|
||||
const mcu_spi_nss_obj_t mcu_spi_nss_list[12] = {
|
||||
SPI(1, 5, &pin_PA04),
|
||||
SPI(1, 5, &pin_PA15),
|
||||
SPI(2, 5, &pin_PB09),
|
||||
SPI(2, 5, &pin_PB12),
|
||||
SPI(3, 6, &pin_PA04),
|
||||
SPI(3, 6, &pin_PA15),
|
||||
SPI(4, 6, &pin_PB12),
|
||||
SPI(4, 5, &pin_PE04),
|
||||
SPI(4, 5, &pin_PE11),
|
||||
SPI(5, 6, &pin_PB01),
|
||||
SPI(5, 6, &pin_PE04),
|
||||
SPI(5, 6, &pin_PE11)
|
||||
};
|
||||
|
||||
//UART, Etc
|
||||
|
@ -34,4 +34,12 @@ extern const mcu_i2c_sda_obj_t mcu_i2c_sda_list[8];
|
||||
extern const mcu_i2c_scl_obj_t mcu_i2c_scl_list[4];
|
||||
|
||||
|
||||
//SPI
|
||||
extern SPI_TypeDef * mcu_spi_banks[5];
|
||||
|
||||
extern const mcu_spi_sck_obj_t mcu_spi_sck_list[15];
|
||||
extern const mcu_spi_mosi_obj_t mcu_spi_mosi_list[14];
|
||||
extern const mcu_spi_miso_obj_t mcu_spi_miso_list[12];
|
||||
extern const mcu_spi_nss_obj_t mcu_spi_nss_list[12];
|
||||
|
||||
#endif // MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H
|
@ -32,6 +32,7 @@
|
||||
|
||||
#include "common-hal/microcontroller/Pin.h"
|
||||
#include "common-hal/busio/I2C.h"
|
||||
#include "common-hal/busio/SPI.h"
|
||||
|
||||
#include "stm32f4/clocks.h"
|
||||
#include "stm32f4/gpio.h"
|
||||
@ -55,6 +56,7 @@ safe_mode_t port_init(void) {
|
||||
void reset_port(void) {
|
||||
reset_all_pins();
|
||||
i2c_reset();
|
||||
spi_reset();
|
||||
}
|
||||
|
||||
void reset_to_bootloader(void) {
|
||||
|
Loading…
Reference in New Issue
Block a user