This commit is contained in:
Hierophect 2019-09-26 11:35:08 -04:00
parent a926d01509
commit f08127bfcc
3 changed files with 224 additions and 23 deletions

View File

@ -23,55 +23,248 @@
* 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 "common-hal/microcontroller/Pin.h"
//mp_raise_NotImplementedError(translate("SPI not yet supported"));
STATIC bool reserved_spi[6];
void spi_reset(void) {
//Note: I2Cs are also forcibly reset in construct, due to silicon error
#ifdef SPI1
reserved_spi[0] = false;
__HAL_RCC_SPI1_CLK_DISABLE();
#endif
#ifdef SPI2
reserved_spi[1] = false;
__HAL_RCC_SPI2_CLK_DISABLE();
#endif
#ifdef SPI3
reserved_spi[2] = false;
__HAL_RCC_SPI3_CLK_DISABLE();
#endif
#ifdef SPI4
reserved_spi[3] = false;
__HAL_RCC_SPI4_CLK_DISABLE();
#endif
#ifdef SPI5
reserved_spi[4] = false;
__HAL_RCC_SPI5_CLK_DISABLE();
#endif
#ifdef SPI6
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 * miso) {
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
//match pins to I2C 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);
//sck
for(uint i=0; i<sck_len;i++) {
if (mcu_spi_sck_list[i].pin == clock) {
//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)
&& (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)) {
//everything needs the same index
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->clk!=NULL && self->mosi!=NULL && self->miso!=NULL ) {
SPIx = mcu_spi_banks[self->clk->spi_index-1];
} else {
mp_raise_RuntimeError(translate("Invalid SPI pin selection"));
}
if(reserved_spi[self->sck->spi_index-1]) {
mp_raise_RuntimeError(translate("Hardware busy, try alternative pins"));
}
//Start GPIO for each pin
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin_mask(sck->number);
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
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_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
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_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = self->miso->altfn_index;
HAL_GPIO_Init(pin_port(miso->port), &GPIO_InitStruct);
//Keep separate so above hack can be cleanly replaced
#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_2;
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"));
}
claim_pin(sck);
claim_pin(mosi);
claim_pin(miso);
}
void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
}
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;
}
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) {
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;
}

View File

@ -33,10 +33,14 @@
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;
} busio_spi_obj_t;
void spi_reset(void);
#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_BUSIO_SPI_H

View File

@ -34,6 +34,8 @@
#include "stm32f4/pins.h"
// 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)
@ -63,6 +65,8 @@ typedef struct {
}
// 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