2020-03-11 18:13:06 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include <stdbool.h>
|
2020-09-16 14:05:15 -04:00
|
|
|
#include <string.h>
|
2020-03-11 18:13:06 -04:00
|
|
|
|
|
|
|
#include "shared-bindings/busio/SPI.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
2022-05-27 15:59:54 -04:00
|
|
|
#include "supervisor/shared/translate/translate.h"
|
2020-07-22 14:11:00 -04:00
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
2020-03-11 18:13:06 -04:00
|
|
|
|
|
|
|
// Note that any bugs introduced in this file can cause crashes at startup
|
|
|
|
// for chips using external SPI flash.
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
// arrays use 0 based numbering: SPI1 is stored at index 0
|
2020-03-11 18:13:06 -04:00
|
|
|
#define MAX_SPI 6
|
2020-04-06 19:13:55 -04:00
|
|
|
|
2020-03-11 18:13:06 -04:00
|
|
|
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);
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC uint32_t get_busclock(SPI_TypeDef *instance) {
|
2020-04-06 19:13:55 -04:00
|
|
|
#if (CPY_STM32H7)
|
2021-03-15 09:57:36 -04:00
|
|
|
if (instance == SPI1 || instance == SPI2 || instance == SPI3) {
|
|
|
|
return HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SPI123);
|
|
|
|
} else if (instance == SPI4 || instance == SPI5) {
|
|
|
|
return HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SPI45);
|
|
|
|
} else {
|
|
|
|
return HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SPI6);
|
|
|
|
}
|
feat: add Blues Swan R5 support
complete pin mapping for Feather pins
stubbed out files needed for complilation. still to be modified
0 out all CPY modules in mpconfigboard.mk until we get the build running
add csv for pin generation for STM32L4R5
add F4R5 references in peripherals files
refactored out board files BECAUSE I AM AN IDIOT; add L4 series system clocks file from CubeMX
took a guess at the number of USB endpoint pairs to get the build done
guess was close, but wrong. It is 8
clean up peripheral DEFs
Fixes build error:
```
In file included from ../../py/mpstate.h:33,
from ../../py/mpstate.c:27:
../../py/misc.h: In function 'vstr_str':
../../py/misc.h:196:1: sorry, unimplemented: Thumb-1 hard-float VFP ABI
static inline char *vstr_str(vstr_t *vstr) {
^~~~~~
```
Sleuthing steps:
* verify that the feather_stm32f4_express board builds correctly
* put a `#error` at the bottom of the `mpstate.c` file.
* build for the feather and swan boards, with V=2 to capture the build command for that file.
* use a differencing tool to inspect the differences between the two invocations
* inspecting the differences, I saw a missing `-mcpu=cortex-m4` I tested by adding that to the Swan build command. The file built fine (stopping at the hard error, but no other warnings.)
A grep through the sources revealed where this flag was being set for the stm ports.
With this commit, the build gets further, but does not complete. The next exciting episode in this unfolding coding saga is just a commit away!
working build with minimal set of modules for the Blues Swan r5
chore:change header copyright name to Blues Wireless Contributors
USB operational. Fixed up clocks to be hardwired for LSE no HSE case. (Trying to combine HSE in there made the code much more complex, and I don't have a board to test it out on.)
USART working
adds support for `ENABLE_3V3` and `DISCHARGE_3V3` pins. I am surprised that pin definitions are quite low-level and don't include default direction and state, so the code currently has to initialize `ENABLE_3V3` pin as output. The LED takes over a second to discharge, so I wonder if the board startup code is not having the desired affect.
short circuit implementation of backup memory for the STM32L4
all the ports
remove company name from board name to be consistent with the Arduino board definition.
add default pins for I2C, SPI and UART, so that `board.I2C` et al. works as expected. Confirmed I2C timing.
fix board name
fix incorrect pin definition. add test to allow manual check of each output pin
analog IO
code changes for WebUSB. Doesn't appear to work, will revisit later.
ensure that `sys.platform` is available
checkin missing file
feat: make room for a larger filesystem so the sensor tutorial will fit on the device.
fix:(stm32l4r5zi.csv): merged AF0-7 and AF8-15 into single lines and removed extraneous headers mixed in with the data.
fix(parse_af_csv.py): pin index in the csv is 0 not 1, and AF index made 1 larger
chore(Swan R5): update peripherals pins from `parse_af_csv.py` output
optimize flash sector access
2021-07-29 18:06:31 -04:00
|
|
|
#elif (CPY_STM32F4 || CPY_STM32F7 || CPY_STM32L4)
|
2021-03-15 09:57:36 -04:00
|
|
|
// SPI2 and 3 are on PCLK1, if they exist.
|
|
|
|
#ifdef SPI2
|
|
|
|
if (instance == SPI2) {
|
|
|
|
return HAL_RCC_GetPCLK1Freq();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef SPI3
|
|
|
|
if (instance == SPI3) {
|
|
|
|
return HAL_RCC_GetPCLK1Freq();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return HAL_RCC_GetPCLK2Freq();
|
2020-03-11 18:13:06 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC uint32_t stm32_baud_to_spi_div(uint32_t baudrate, uint16_t *prescaler, uint32_t busclock) {
|
2020-03-11 18:13:06 -04:00
|
|
|
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];
|
2021-03-15 09:57:36 -04:00
|
|
|
if (baudrate >= (busclock / divisor)) {
|
2020-03-11 18:13:06 -04:00
|
|
|
*prescaler = divisor;
|
|
|
|
return baud_map[i][1];
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
} while (divisor != 256);
|
2021-03-15 09:57:36 -04:00
|
|
|
// only gets here if requested baud is lower than minimum
|
2020-03-11 18:13:06 -04:00
|
|
|
*prescaler = 256;
|
|
|
|
return SPI_BAUDRATEPRESCALER_256;
|
|
|
|
}
|
|
|
|
|
|
|
|
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] = false;
|
|
|
|
} else {
|
|
|
|
never_reset_mask |= 1 << i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spi_clock_disable(ALL_CLOCKS & ~(never_reset_mask));
|
|
|
|
}
|
|
|
|
|
2020-06-24 12:28:28 -04:00
|
|
|
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) {
|
2021-03-15 09:57:36 -04:00
|
|
|
for (size_t i = 0; i < sz; i++, table++) {
|
|
|
|
if (periph_index == table->periph_index && pin == table->pin) {
|
2020-06-24 12:28:28 -04:00
|
|
|
return table;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
// match pins to SPI objects
|
2020-06-24 12:28:28 -04:00
|
|
|
STATIC int check_pins(busio_spi_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
const mcu_pin_obj_t *sck, const mcu_pin_obj_t *mosi,
|
|
|
|
const mcu_pin_obj_t *miso) {
|
2020-06-24 12:28:28 -04:00
|
|
|
bool spi_taken = false;
|
2020-03-11 18:13:06 -04:00
|
|
|
|
|
|
|
uint8_t sck_len = MP_ARRAY_SIZE(mcu_spi_sck_list);
|
|
|
|
uint8_t mosi_len = MP_ARRAY_SIZE(mcu_spi_mosi_list);
|
|
|
|
uint8_t miso_len = MP_ARRAY_SIZE(mcu_spi_miso_list);
|
|
|
|
|
2020-06-24 12:28:28 -04:00
|
|
|
// Loop over each possibility for SCK. Check whether MISO and/or MOSI can be used on the same peripheral
|
2020-03-11 18:13:06 -04:00
|
|
|
for (uint i = 0; i < sck_len; i++) {
|
2020-06-24 12:28:28 -04:00
|
|
|
const mcu_periph_obj_t *mcu_spi_sck = &mcu_spi_sck_list[i];
|
|
|
|
if (mcu_spi_sck->pin != sck) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
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))) {
|
|
|
|
continue;
|
2020-03-11 18:13:06 -04:00
|
|
|
}
|
2020-06-24 12:28:28 -04:00
|
|
|
|
|
|
|
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))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
if (reserved_spi[periph_index - 1]) {
|
2020-06-24 12:28:28 -04:00
|
|
|
spi_taken = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->sck = mcu_spi_sck;
|
|
|
|
self->mosi = mcu_spi_mosi;
|
|
|
|
self->miso = mcu_spi_miso;
|
|
|
|
|
|
|
|
return periph_index;
|
2020-03-11 18:13:06 -04:00
|
|
|
}
|
|
|
|
|
2020-06-24 12:28:28 -04:00
|
|
|
if (spi_taken) {
|
2023-07-20 14:16:31 -04:00
|
|
|
mp_raise_ValueError(translate("Hardware in use, try alternative pins"));
|
2020-03-11 18:13:06 -04:00
|
|
|
} else {
|
2022-05-13 15:33:43 -04:00
|
|
|
raise_ValueError_invalid_pin();
|
2020-03-11 18:13:06 -04:00
|
|
|
}
|
2020-06-24 12:28:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
const mcu_pin_obj_t *sck, const mcu_pin_obj_t *mosi,
|
2022-02-08 21:37:38 -05:00
|
|
|
const mcu_pin_obj_t *miso, bool half_duplex) {
|
2020-06-24 12:28:28 -04:00
|
|
|
|
|
|
|
int periph_index = check_pins(self, sck, mosi, miso);
|
2021-03-15 09:57:36 -04:00
|
|
|
SPI_TypeDef *SPIx = mcu_spi_banks[periph_index - 1];
|
2020-03-11 18:13:06 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
// Start GPIO for each pin
|
2020-03-11 18:13:06 -04:00
|
|
|
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);
|
|
|
|
|
|
|
|
if (self->mosi != NULL) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->miso != NULL) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:26:13 -04:00
|
|
|
spi_clock_enable(1 << (self->sck->periph_index - 1));
|
|
|
|
reserved_spi[self->sck->periph_index - 1] = true;
|
2020-03-11 18:13:06 -04:00
|
|
|
|
2022-05-29 10:13:38 -04:00
|
|
|
// Always start at 250khz which is what SD cards need. They are sensitive to
|
|
|
|
// SPI bus noise before they are put into SPI mode.
|
|
|
|
const uint32_t default_baudrate = 250000UL;
|
|
|
|
|
2020-03-11 18:13:06 -04:00
|
|
|
self->handle.Instance = SPIx;
|
|
|
|
self->handle.Init.Mode = SPI_MODE_MASTER;
|
2022-02-08 21:37:38 -05:00
|
|
|
// Direction change only required for RX-only, see RefMan RM0090:884
|
2022-02-08 23:13:22 -05:00
|
|
|
if (half_duplex) {
|
2022-02-07 21:08:44 -05:00
|
|
|
self->handle.Init.Direction = SPI_DIRECTION_1LINE;
|
2022-02-08 21:37:38 -05:00
|
|
|
} else {
|
|
|
|
self->handle.Init.Direction = (self->mosi == NULL) ? SPI_DIRECTION_2LINES_RXONLY : SPI_DIRECTION_2LINES;
|
2022-02-07 21:08:44 -05:00
|
|
|
}
|
2020-03-11 18:13:06 -04:00
|
|
|
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;
|
2022-05-29 10:13:38 -04:00
|
|
|
self->handle.Init.BaudRatePrescaler = stm32_baud_to_spi_div(default_baudrate, &self->prescaler, get_busclock(self->handle.Instance));
|
2020-03-11 18:13:06 -04:00
|
|
|
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;
|
2021-03-15 09:57:36 -04:00
|
|
|
if (HAL_SPI_Init(&self->handle) != HAL_OK) {
|
2022-05-13 15:33:43 -04:00
|
|
|
mp_raise_ValueError(translate("SPI init error"));
|
2020-03-11 18:13:06 -04:00
|
|
|
}
|
2022-05-29 10:13:38 -04:00
|
|
|
self->baudrate = default_baudrate;
|
|
|
|
// self->prescaler = 16; // Initialised above by stm32_baud_to_spi_div
|
2022-02-08 21:37:38 -05:00
|
|
|
self->half_duplex = half_duplex;
|
2020-03-11 18:13:06 -04:00
|
|
|
self->polarity = 0;
|
|
|
|
self->phase = 0;
|
|
|
|
self->bits = 8;
|
|
|
|
|
2020-07-22 14:11:00 -04:00
|
|
|
common_hal_mcu_pin_claim(sck);
|
2020-03-11 18:13:06 -04:00
|
|
|
if (self->mosi != NULL) {
|
2020-07-22 14:11:00 -04:00
|
|
|
common_hal_mcu_pin_claim(mosi);
|
2020-03-11 18:13:06 -04:00
|
|
|
}
|
|
|
|
if (self->miso != NULL) {
|
2020-07-22 14:11:00 -04:00
|
|
|
common_hal_mcu_pin_claim(miso);
|
2020-03-11 18:13:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
|
2020-05-27 11:45:15 -04:00
|
|
|
|
|
|
|
never_reset_spi[self->sck->periph_index - 1] = true;
|
|
|
|
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->miso != NULL) {
|
|
|
|
never_reset_pin_number(self->miso->pin->port, self->miso->pin->number);
|
2020-03-11 18:13:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) {
|
2022-01-26 11:46:42 -05:00
|
|
|
return self->sck == NULL;
|
2020-03-11 18:13:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
|
|
|
|
if (common_hal_busio_spi_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
spi_clock_disable(1 << (self->sck->periph_index - 1));
|
2020-03-17 18:26:13 -04:00
|
|
|
reserved_spi[self->sck->periph_index - 1] = false;
|
|
|
|
never_reset_spi[self->sck->periph_index - 1] = false;
|
2020-03-11 18:13:06 -04:00
|
|
|
|
|
|
|
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->miso != NULL) {
|
|
|
|
reset_pin_number(self->miso->pin->port,self->miso->pin->number);
|
|
|
|
}
|
|
|
|
self->sck = NULL;
|
|
|
|
self->mosi = NULL;
|
|
|
|
self->miso = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
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
|
2020-03-11 18:13:06 -04:00
|
|
|
&& phase == self->phase && bits == self->bits) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
// Deinit SPI
|
2020-03-11 18:13:06 -04:00
|
|
|
HAL_SPI_DeInit(&self->handle);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2022-02-19 08:46:02 -05:00
|
|
|
// Set SCK pull up or down based on SPI CLK Polarity
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
GPIO_InitStruct.Pin = pin_mask(self->sck->pin->number);
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
|
|
GPIO_InitStruct.Pull = (polarity) ? GPIO_PULLUP : GPIO_PULLDOWN;
|
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
|
|
GPIO_InitStruct.Alternate = self->sck->altfn_index;
|
|
|
|
HAL_GPIO_Init(pin_port(self->sck->pin->port), &GPIO_InitStruct);
|
|
|
|
|
2020-03-11 18:13:06 -04:00
|
|
|
self->handle.Init.BaudRatePrescaler = stm32_baud_to_spi_div(baudrate, &self->prescaler,
|
2021-03-15 09:57:36 -04:00
|
|
|
get_busclock(self->handle.Instance));
|
2020-03-11 18:13:06 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
if (HAL_SPI_Init(&self->handle) != HAL_OK) {
|
2022-05-20 10:10:55 -04:00
|
|
|
mp_raise_RuntimeError(translate("SPI re-init"));
|
2020-03-11 18:13:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
// Critical section code that may be required at some point.
|
2020-03-11 18:13:06 -04:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_spi_has_lock(busio_spi_obj_t *self) {
|
|
|
|
return self->has_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
|
|
|
|
self->has_lock = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_spi_write(busio_spi_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
const uint8_t *data, size_t len) {
|
2020-03-11 18:13:06 -04:00
|
|
|
if (self->mosi == NULL) {
|
2023-07-20 14:16:31 -04:00
|
|
|
mp_raise_ValueError_varg(translate("No %q pin"), MP_QSTR_mosi);
|
2020-03-11 18:13:06 -04:00
|
|
|
}
|
2020-09-16 14:05:15 -04:00
|
|
|
HAL_StatusTypeDef result = HAL_SPI_Transmit(&self->handle, (uint8_t *)data, (uint16_t)len, HAL_MAX_DELAY);
|
2020-03-11 18:13:06 -04:00
|
|
|
return result == HAL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_spi_read(busio_spi_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
uint8_t *data, size_t len, uint8_t write_value) {
|
2022-02-08 23:13:22 -05:00
|
|
|
if (self->miso == NULL && !self->half_duplex) {
|
2023-07-20 14:16:31 -04:00
|
|
|
mp_raise_ValueError_varg(translate("No %q pin"), MP_QSTR_miso);
|
2022-02-08 23:13:22 -05:00
|
|
|
} else if (self->half_duplex && self->mosi == NULL) {
|
2023-07-20 14:16:31 -04:00
|
|
|
mp_raise_ValueError_varg(translate("No %q pin"), MP_QSTR_mosi);
|
2022-02-08 21:37:38 -05:00
|
|
|
}
|
2020-09-16 14:05:15 -04:00
|
|
|
HAL_StatusTypeDef result = HAL_OK;
|
2022-02-08 23:13:22 -05:00
|
|
|
if ((!self->half_duplex && self->mosi == NULL) || (self->half_duplex && self->mosi != NULL && self->miso == NULL)) {
|
2020-09-16 14:05:15 -04:00
|
|
|
result = HAL_SPI_Receive(&self->handle, data, (uint16_t)len, HAL_MAX_DELAY);
|
2022-02-08 21:37:38 -05:00
|
|
|
} else {
|
2020-09-16 14:05:15 -04:00
|
|
|
memset(data, write_value, len);
|
|
|
|
result = HAL_SPI_TransmitReceive(&self->handle, data, data, (uint16_t)len, HAL_MAX_DELAY);
|
|
|
|
}
|
2020-03-11 18:13:06 -04:00
|
|
|
return result == HAL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_busio_spi_transfer(busio_spi_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
const uint8_t *data_out, uint8_t *data_in, size_t len) {
|
2023-07-20 14:16:31 -04:00
|
|
|
if (self->mosi == NULL && data_out != NULL) {
|
|
|
|
mp_raise_ValueError_varg(translate("No %q pin"), MP_QSTR_mosi);
|
|
|
|
}
|
|
|
|
if (self->miso == NULL && data_in != NULL) {
|
|
|
|
mp_raise_ValueError_varg(translate("No %q pin"), MP_QSTR_miso);
|
2020-03-11 18:13:06 -04:00
|
|
|
}
|
2020-09-16 14:05:15 -04:00
|
|
|
HAL_StatusTypeDef result = HAL_SPI_TransmitReceive(&self->handle,
|
2021-03-15 09:57:36 -04:00
|
|
|
(uint8_t *)data_out, data_in, (uint16_t)len,HAL_MAX_DELAY);
|
2020-03-11 18:13:06 -04:00
|
|
|
return result == HAL_OK;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint32_t common_hal_busio_spi_get_frequency(busio_spi_obj_t *self) {
|
|
|
|
// returns actual frequency
|
2022-05-29 10:13:38 -04:00
|
|
|
uint32_t result = get_busclock(self->handle.Instance) / self->prescaler;
|
2020-03-11 18:13:06 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint8_t common_hal_busio_spi_get_phase(busio_spi_obj_t *self) {
|
2020-03-11 18:13:06 -04:00
|
|
|
return self->phase;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint8_t common_hal_busio_spi_get_polarity(busio_spi_obj_t *self) {
|
2020-03-11 18:13:06 -04:00
|
|
|
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();
|
|
|
|
__HAL_RCC_SPI1_FORCE_RESET();
|
|
|
|
__HAL_RCC_SPI1_RELEASE_RESET();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef SPI2
|
|
|
|
if (mask & (1 << 1)) {
|
|
|
|
__HAL_RCC_SPI2_CLK_DISABLE();
|
|
|
|
__HAL_RCC_SPI2_FORCE_RESET();
|
|
|
|
__HAL_RCC_SPI2_RELEASE_RESET();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef SPI3
|
|
|
|
if (mask & (1 << 2)) {
|
|
|
|
__HAL_RCC_SPI3_CLK_DISABLE();
|
|
|
|
__HAL_RCC_SPI3_FORCE_RESET();
|
|
|
|
__HAL_RCC_SPI3_RELEASE_RESET();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef SPI4
|
|
|
|
if (mask & (1 << 3)) {
|
|
|
|
__HAL_RCC_SPI4_CLK_DISABLE();
|
|
|
|
__HAL_RCC_SPI4_FORCE_RESET();
|
|
|
|
__HAL_RCC_SPI4_RELEASE_RESET();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef SPI5
|
|
|
|
if (mask & (1 << 4)) {
|
|
|
|
__HAL_RCC_SPI5_CLK_DISABLE();
|
|
|
|
__HAL_RCC_SPI5_FORCE_RESET();
|
|
|
|
__HAL_RCC_SPI5_RELEASE_RESET();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef SPI6
|
|
|
|
if (mask & (1 << 5)) {
|
|
|
|
__HAL_RCC_SPI6_CLK_DISABLE();
|
|
|
|
__HAL_RCC_SPI6_FORCE_RESET();
|
|
|
|
__HAL_RCC_SPI6_RELEASE_RESET();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|