2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2018-08-14 03:11:07 -04:00
|
|
|
* Copyright (c) 2013-2018 Damien P. George
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-04-19 19:16:30 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-01-01 16:06:20 -05:00
|
|
|
#include "py/runtime.h"
|
2015-10-30 19:03:58 -04:00
|
|
|
#include "py/mphal.h"
|
2014-04-19 19:16:30 -04:00
|
|
|
#include "spi.h"
|
|
|
|
|
2015-06-10 08:06:48 -04:00
|
|
|
// Possible DMA configurations for SPI busses:
|
|
|
|
// SPI1_TX: DMA2_Stream3.CHANNEL_3 or DMA2_Stream5.CHANNEL_3
|
|
|
|
// SPI1_RX: DMA2_Stream0.CHANNEL_3 or DMA2_Stream2.CHANNEL_3
|
|
|
|
// SPI2_TX: DMA1_Stream4.CHANNEL_0
|
|
|
|
// SPI2_RX: DMA1_Stream3.CHANNEL_0
|
|
|
|
// SPI3_TX: DMA1_Stream5.CHANNEL_0 or DMA1_Stream7.CHANNEL_0
|
|
|
|
// SPI3_RX: DMA1_Stream0.CHANNEL_0 or DMA1_Stream2.CHANNEL_0
|
2015-12-08 09:41:47 -05:00
|
|
|
// SPI4_TX: DMA2_Stream4.CHANNEL_5 or DMA2_Stream1.CHANNEL_4
|
|
|
|
// SPI4_RX: DMA2_Stream3.CHANNEL_5 or DMA2_Stream0.CHANNEL_4
|
|
|
|
// SPI5_TX: DMA2_Stream4.CHANNEL_2 or DMA2_Stream6.CHANNEL_7
|
|
|
|
// SPI5_RX: DMA2_Stream3.CHANNEL_2 or DMA2_Stream5.CHANNEL_7
|
|
|
|
// SPI6_TX: DMA2_Stream5.CHANNEL_1
|
|
|
|
// SPI6_RX: DMA2_Stream6.CHANNEL_1
|
2015-06-10 08:06:48 -04:00
|
|
|
|
2015-12-12 10:02:02 -05:00
|
|
|
#if defined(MICROPY_HW_SPI1_SCK)
|
2014-04-19 19:16:30 -04:00
|
|
|
SPI_HandleTypeDef SPIHandle1 = {.Instance = NULL};
|
2014-04-20 14:06:15 -04:00
|
|
|
#endif
|
2015-12-12 10:02:02 -05:00
|
|
|
#if defined(MICROPY_HW_SPI2_SCK)
|
2014-04-19 19:16:30 -04:00
|
|
|
SPI_HandleTypeDef SPIHandle2 = {.Instance = NULL};
|
2015-01-16 00:45:21 -05:00
|
|
|
#endif
|
2015-12-12 10:02:02 -05:00
|
|
|
#if defined(MICROPY_HW_SPI3_SCK)
|
2014-04-19 19:16:30 -04:00
|
|
|
SPI_HandleTypeDef SPIHandle3 = {.Instance = NULL};
|
2014-04-20 14:06:15 -04:00
|
|
|
#endif
|
2015-12-08 09:41:47 -05:00
|
|
|
#if defined(MICROPY_HW_SPI4_SCK)
|
|
|
|
SPI_HandleTypeDef SPIHandle4 = {.Instance = NULL};
|
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI5_SCK)
|
|
|
|
SPI_HandleTypeDef SPIHandle5 = {.Instance = NULL};
|
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI6_SCK)
|
|
|
|
SPI_HandleTypeDef SPIHandle6 = {.Instance = NULL};
|
|
|
|
#endif
|
2014-04-19 19:16:30 -04:00
|
|
|
|
2018-02-04 21:44:31 -05:00
|
|
|
const spi_t spi_obj[6] = {
|
2015-12-12 10:02:02 -05:00
|
|
|
#if defined(MICROPY_HW_SPI1_SCK)
|
2018-02-04 21:44:31 -05:00
|
|
|
{&SPIHandle1, &dma_SPI_1_TX, &dma_SPI_1_RX},
|
2015-12-12 10:02:02 -05:00
|
|
|
#else
|
2018-02-04 21:44:31 -05:00
|
|
|
{NULL, NULL, NULL},
|
2015-12-12 10:02:02 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI2_SCK)
|
2018-02-04 21:44:31 -05:00
|
|
|
{&SPIHandle2, &dma_SPI_2_TX, &dma_SPI_2_RX},
|
2015-12-12 10:02:02 -05:00
|
|
|
#else
|
2018-02-04 21:44:31 -05:00
|
|
|
{NULL, NULL, NULL},
|
2015-12-12 10:02:02 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI3_SCK)
|
2018-02-04 21:44:31 -05:00
|
|
|
{&SPIHandle3, &dma_SPI_3_TX, &dma_SPI_3_RX},
|
2015-12-12 10:02:02 -05:00
|
|
|
#else
|
2018-02-04 21:44:31 -05:00
|
|
|
{NULL, NULL, NULL},
|
2015-12-12 10:02:02 -05:00
|
|
|
#endif
|
2015-12-08 09:41:47 -05:00
|
|
|
#if defined(MICROPY_HW_SPI4_SCK)
|
2018-02-04 21:44:31 -05:00
|
|
|
{&SPIHandle4, &dma_SPI_4_TX, &dma_SPI_4_RX},
|
2015-12-08 09:41:47 -05:00
|
|
|
#else
|
2018-02-04 21:44:31 -05:00
|
|
|
{NULL, NULL, NULL},
|
2015-12-08 09:41:47 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI5_SCK)
|
2018-02-04 21:44:31 -05:00
|
|
|
{&SPIHandle5, &dma_SPI_5_TX, &dma_SPI_5_RX},
|
2015-12-08 09:41:47 -05:00
|
|
|
#else
|
2018-02-04 21:44:31 -05:00
|
|
|
{NULL, NULL, NULL},
|
2015-12-08 09:41:47 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI6_SCK)
|
2018-02-04 21:44:31 -05:00
|
|
|
{&SPIHandle6, &dma_SPI_6_TX, &dma_SPI_6_RX},
|
2015-12-08 09:41:47 -05:00
|
|
|
#else
|
2018-02-04 21:44:31 -05:00
|
|
|
{NULL, NULL, NULL},
|
2015-12-08 09:41:47 -05:00
|
|
|
#endif
|
2015-06-10 08:06:48 -04:00
|
|
|
};
|
2014-12-02 18:41:30 -05:00
|
|
|
|
2014-04-19 19:16:30 -04:00
|
|
|
void spi_init0(void) {
|
2018-02-02 03:01:11 -05:00
|
|
|
// Initialise the SPI handles.
|
|
|
|
// The structs live on the BSS so all other fields will be zero after a reset.
|
2015-12-12 10:02:02 -05:00
|
|
|
#if defined(MICROPY_HW_SPI1_SCK)
|
2014-04-19 19:16:30 -04:00
|
|
|
SPIHandle1.Instance = SPI1;
|
2015-12-12 10:02:02 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI2_SCK)
|
2014-04-19 19:16:30 -04:00
|
|
|
SPIHandle2.Instance = SPI2;
|
2015-12-12 10:02:02 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI3_SCK)
|
2014-04-19 19:16:30 -04:00
|
|
|
SPIHandle3.Instance = SPI3;
|
2015-12-12 10:02:02 -05:00
|
|
|
#endif
|
2015-12-08 09:41:47 -05:00
|
|
|
#if defined(MICROPY_HW_SPI4_SCK)
|
|
|
|
SPIHandle4.Instance = SPI4;
|
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI5_SCK)
|
|
|
|
SPIHandle5.Instance = SPI5;
|
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI6_SCK)
|
|
|
|
SPIHandle6.Instance = SPI6;
|
|
|
|
#endif
|
2014-04-19 19:16:30 -04:00
|
|
|
}
|
|
|
|
|
2018-08-14 03:11:07 -04:00
|
|
|
int spi_find_index(mp_obj_t id) {
|
2016-10-03 01:45:46 -04:00
|
|
|
if (MP_OBJ_IS_STR(id)) {
|
|
|
|
// given a string id
|
|
|
|
const char *port = mp_obj_str_get_str(id);
|
|
|
|
if (0) {
|
|
|
|
#ifdef MICROPY_HW_SPI1_NAME
|
|
|
|
} else if (strcmp(port, MICROPY_HW_SPI1_NAME) == 0) {
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
#ifdef MICROPY_HW_SPI2_NAME
|
|
|
|
} else if (strcmp(port, MICROPY_HW_SPI2_NAME) == 0) {
|
|
|
|
return 2;
|
|
|
|
#endif
|
|
|
|
#ifdef MICROPY_HW_SPI3_NAME
|
|
|
|
} else if (strcmp(port, MICROPY_HW_SPI3_NAME) == 0) {
|
|
|
|
return 3;
|
|
|
|
#endif
|
2018-02-02 01:44:05 -05:00
|
|
|
#ifdef MICROPY_HW_SPI4_NAME
|
|
|
|
} else if (strcmp(port, MICROPY_HW_SPI4_NAME) == 0) {
|
|
|
|
return 4;
|
|
|
|
#endif
|
|
|
|
#ifdef MICROPY_HW_SPI5_NAME
|
|
|
|
} else if (strcmp(port, MICROPY_HW_SPI5_NAME) == 0) {
|
|
|
|
return 5;
|
|
|
|
#endif
|
|
|
|
#ifdef MICROPY_HW_SPI6_NAME
|
|
|
|
} else if (strcmp(port, MICROPY_HW_SPI6_NAME) == 0) {
|
|
|
|
return 6;
|
|
|
|
#endif
|
2016-10-03 01:45:46 -04:00
|
|
|
}
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
|
2017-06-14 22:02:14 -04:00
|
|
|
"SPI(%s) doesn't exist", port));
|
2016-10-03 01:45:46 -04:00
|
|
|
} else {
|
|
|
|
// given an integer id
|
|
|
|
int spi_id = mp_obj_get_int(id);
|
2018-02-04 21:44:31 -05:00
|
|
|
if (spi_id >= 1 && spi_id <= MP_ARRAY_SIZE(spi_obj)
|
|
|
|
&& spi_obj[spi_id - 1].spi != NULL) {
|
2016-10-03 01:45:46 -04:00
|
|
|
return spi_id;
|
|
|
|
}
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
|
2017-06-14 22:02:14 -04:00
|
|
|
"SPI(%d) doesn't exist", spi_id));
|
2016-10-03 01:45:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sets the parameters in the SPI_InitTypeDef struct
|
|
|
|
// if an argument is -1 then the corresponding parameter is not changed
|
2018-08-14 03:11:07 -04:00
|
|
|
void spi_set_params(const spi_t *spi_obj, uint32_t prescale, int32_t baudrate,
|
2016-10-03 01:45:46 -04:00
|
|
|
int32_t polarity, int32_t phase, int32_t bits, int32_t firstbit) {
|
2018-02-04 21:44:31 -05:00
|
|
|
SPI_HandleTypeDef *spi = spi_obj->spi;
|
2016-10-03 01:45:46 -04:00
|
|
|
SPI_InitTypeDef *init = &spi->Init;
|
|
|
|
|
|
|
|
if (prescale != 0xffffffff || baudrate != -1) {
|
|
|
|
if (prescale == 0xffffffff) {
|
|
|
|
// prescaler not given, so select one that yields at most the requested baudrate
|
|
|
|
mp_uint_t spi_clock;
|
2018-05-28 04:10:53 -04:00
|
|
|
#if defined(STM32F0)
|
|
|
|
spi_clock = HAL_RCC_GetPCLK1Freq();
|
|
|
|
#else
|
2016-10-03 01:45:46 -04:00
|
|
|
if (spi->Instance == SPI2 || spi->Instance == SPI3) {
|
|
|
|
// SPI2 and SPI3 are on APB1
|
|
|
|
spi_clock = HAL_RCC_GetPCLK1Freq();
|
|
|
|
} else {
|
|
|
|
// SPI1, SPI4, SPI5 and SPI6 are on APB2
|
|
|
|
spi_clock = HAL_RCC_GetPCLK2Freq();
|
|
|
|
}
|
2018-05-28 04:10:53 -04:00
|
|
|
#endif
|
2018-08-10 02:39:47 -04:00
|
|
|
prescale = (spi_clock + baudrate - 1) / baudrate;
|
2016-10-03 01:45:46 -04:00
|
|
|
}
|
|
|
|
if (prescale <= 2) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; }
|
|
|
|
else if (prescale <= 4) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; }
|
|
|
|
else if (prescale <= 8) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; }
|
|
|
|
else if (prescale <= 16) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; }
|
|
|
|
else if (prescale <= 32) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; }
|
|
|
|
else if (prescale <= 64) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; }
|
|
|
|
else if (prescale <= 128) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128; }
|
|
|
|
else { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256; }
|
|
|
|
}
|
|
|
|
|
|
|
|
if (polarity != -1) {
|
|
|
|
init->CLKPolarity = polarity == 0 ? SPI_POLARITY_LOW : SPI_POLARITY_HIGH;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (phase != -1) {
|
|
|
|
init->CLKPhase = phase == 0 ? SPI_PHASE_1EDGE : SPI_PHASE_2EDGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bits != -1) {
|
|
|
|
init->DataSize = (bits == 16) ? SPI_DATASIZE_16BIT : SPI_DATASIZE_8BIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (firstbit != -1) {
|
|
|
|
init->FirstBit = firstbit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-19 19:16:30 -04:00
|
|
|
// TODO allow to take a list of pins to use
|
2018-02-04 21:44:31 -05:00
|
|
|
void spi_init(const spi_t *self, bool enable_nss_pin) {
|
|
|
|
SPI_HandleTypeDef *spi = self->spi;
|
2017-12-20 10:39:30 -05:00
|
|
|
const pin_obj_t *pins[4] = { NULL, NULL, NULL, NULL };
|
2015-06-10 08:06:48 -04:00
|
|
|
|
2014-04-20 14:06:15 -04:00
|
|
|
if (0) {
|
2015-12-12 10:02:02 -05:00
|
|
|
#if defined(MICROPY_HW_SPI1_SCK)
|
2014-04-20 14:06:15 -04:00
|
|
|
} else if (spi->Instance == SPI1) {
|
2016-07-15 19:44:26 -04:00
|
|
|
#if defined(MICROPY_HW_SPI1_NSS)
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[0] = MICROPY_HW_SPI1_NSS;
|
2016-07-15 19:44:26 -04:00
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[1] = MICROPY_HW_SPI1_SCK;
|
2017-12-20 10:39:30 -05:00
|
|
|
#if defined(MICROPY_HW_SPI1_MISO)
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[2] = MICROPY_HW_SPI1_MISO;
|
2017-12-20 10:39:30 -05:00
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[3] = MICROPY_HW_SPI1_MOSI;
|
2014-04-20 14:06:15 -04:00
|
|
|
// enable the SPI clock
|
2018-02-12 23:53:08 -05:00
|
|
|
__HAL_RCC_SPI1_CLK_ENABLE();
|
2015-12-12 10:02:02 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI2_SCK)
|
2014-04-20 14:06:15 -04:00
|
|
|
} else if (spi->Instance == SPI2) {
|
2016-07-15 19:44:26 -04:00
|
|
|
#if defined(MICROPY_HW_SPI2_NSS)
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[0] = MICROPY_HW_SPI2_NSS;
|
2016-07-15 19:44:26 -04:00
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[1] = MICROPY_HW_SPI2_SCK;
|
2017-12-20 10:39:30 -05:00
|
|
|
#if defined(MICROPY_HW_SPI2_MISO)
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[2] = MICROPY_HW_SPI2_MISO;
|
2017-12-20 10:39:30 -05:00
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[3] = MICROPY_HW_SPI2_MOSI;
|
2014-04-20 14:06:15 -04:00
|
|
|
// enable the SPI clock
|
2018-02-12 23:53:08 -05:00
|
|
|
__HAL_RCC_SPI2_CLK_ENABLE();
|
2015-12-12 10:02:02 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI3_SCK)
|
2014-04-20 14:06:15 -04:00
|
|
|
} else if (spi->Instance == SPI3) {
|
2016-07-15 19:44:26 -04:00
|
|
|
#if defined(MICROPY_HW_SPI3_NSS)
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[0] = MICROPY_HW_SPI3_NSS;
|
2016-07-15 19:44:26 -04:00
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[1] = MICROPY_HW_SPI3_SCK;
|
2017-12-20 10:39:30 -05:00
|
|
|
#if defined(MICROPY_HW_SPI3_MISO)
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[2] = MICROPY_HW_SPI3_MISO;
|
2017-12-20 10:39:30 -05:00
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[3] = MICROPY_HW_SPI3_MOSI;
|
2014-04-20 14:06:15 -04:00
|
|
|
// enable the SPI clock
|
2018-02-12 23:53:08 -05:00
|
|
|
__HAL_RCC_SPI3_CLK_ENABLE();
|
2015-12-12 10:02:02 -05:00
|
|
|
#endif
|
2015-12-08 09:41:47 -05:00
|
|
|
#if defined(MICROPY_HW_SPI4_SCK)
|
|
|
|
} else if (spi->Instance == SPI4) {
|
2016-07-15 19:44:26 -04:00
|
|
|
#if defined(MICROPY_HW_SPI4_NSS)
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[0] = MICROPY_HW_SPI4_NSS;
|
2016-07-15 19:44:26 -04:00
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[1] = MICROPY_HW_SPI4_SCK;
|
2017-12-20 10:39:30 -05:00
|
|
|
#if defined(MICROPY_HW_SPI4_MISO)
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[2] = MICROPY_HW_SPI4_MISO;
|
2017-12-20 10:39:30 -05:00
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[3] = MICROPY_HW_SPI4_MOSI;
|
2015-12-08 09:41:47 -05:00
|
|
|
// enable the SPI clock
|
2018-02-12 23:53:08 -05:00
|
|
|
__HAL_RCC_SPI4_CLK_ENABLE();
|
2015-12-08 09:41:47 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI5_SCK)
|
|
|
|
} else if (spi->Instance == SPI5) {
|
2016-07-15 19:44:26 -04:00
|
|
|
#if defined(MICROPY_HW_SPI5_NSS)
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[0] = MICROPY_HW_SPI5_NSS;
|
2016-07-15 19:44:26 -04:00
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[1] = MICROPY_HW_SPI5_SCK;
|
2017-12-20 10:39:30 -05:00
|
|
|
#if defined(MICROPY_HW_SPI5_MISO)
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[2] = MICROPY_HW_SPI5_MISO;
|
2017-12-20 10:39:30 -05:00
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[3] = MICROPY_HW_SPI5_MOSI;
|
2015-12-08 09:41:47 -05:00
|
|
|
// enable the SPI clock
|
2018-02-12 23:53:08 -05:00
|
|
|
__HAL_RCC_SPI5_CLK_ENABLE();
|
2015-12-08 09:41:47 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI6_SCK)
|
|
|
|
} else if (spi->Instance == SPI6) {
|
2016-07-15 19:44:26 -04:00
|
|
|
#if defined(MICROPY_HW_SPI6_NSS)
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[0] = MICROPY_HW_SPI6_NSS;
|
2016-07-15 19:44:26 -04:00
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[1] = MICROPY_HW_SPI6_SCK;
|
2017-12-20 10:39:30 -05:00
|
|
|
#if defined(MICROPY_HW_SPI6_MISO)
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[2] = MICROPY_HW_SPI6_MISO;
|
2017-12-20 10:39:30 -05:00
|
|
|
#endif
|
2018-03-28 01:13:21 -04:00
|
|
|
pins[3] = MICROPY_HW_SPI6_MOSI;
|
2015-12-08 09:41:47 -05:00
|
|
|
// enable the SPI clock
|
2018-02-12 23:53:08 -05:00
|
|
|
__HAL_RCC_SPI6_CLK_ENABLE();
|
2015-12-08 09:41:47 -05:00
|
|
|
#endif
|
2014-04-20 14:06:15 -04:00
|
|
|
} else {
|
|
|
|
// SPI does not exist for this board (shouldn't get here, should be checked by caller)
|
|
|
|
return;
|
2014-04-19 19:16:30 -04:00
|
|
|
}
|
|
|
|
|
2016-11-11 01:53:45 -05:00
|
|
|
// init the GPIO lines
|
|
|
|
uint32_t mode = MP_HAL_PIN_MODE_ALT;
|
|
|
|
uint32_t pull = spi->Init.CLKPolarity == SPI_POLARITY_LOW ? MP_HAL_PIN_PULL_DOWN : MP_HAL_PIN_PULL_UP;
|
2017-12-20 10:39:30 -05:00
|
|
|
for (uint i = (enable_nss_pin ? 0 : 1); i < 4; i++) {
|
|
|
|
if (pins[i] == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-02-04 21:44:31 -05:00
|
|
|
mp_hal_pin_config_alt(pins[i], mode, pull, AF_FN_SPI, (self - &spi_obj[0]) + 1);
|
2014-04-19 19:16:30 -04:00
|
|
|
}
|
|
|
|
|
2014-04-20 14:06:15 -04:00
|
|
|
// init the SPI device
|
2014-04-19 19:16:30 -04:00
|
|
|
if (HAL_SPI_Init(spi) != HAL_OK) {
|
|
|
|
// init error
|
|
|
|
// TODO should raise an exception, but this function is not necessarily going to be
|
|
|
|
// called via Python, so may not be properly wrapped in an NLR handler
|
2014-10-23 09:25:32 -04:00
|
|
|
printf("OSError: HAL_SPI_Init failed\n");
|
2014-04-19 19:16:30 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-12-02 18:41:30 -05:00
|
|
|
|
2015-06-10 08:06:48 -04:00
|
|
|
// After calling HAL_SPI_Init() it seems that the DMA gets disconnected if
|
|
|
|
// it was previously configured. So we invalidate the DMA channel to force
|
|
|
|
// an initialisation the next time we use it.
|
2016-03-22 06:28:35 -04:00
|
|
|
dma_invalidate_channel(self->tx_dma_descr);
|
|
|
|
dma_invalidate_channel(self->rx_dma_descr);
|
2014-04-19 19:16:30 -04:00
|
|
|
}
|
|
|
|
|
2018-02-04 21:44:31 -05:00
|
|
|
void spi_deinit(const spi_t *spi_obj) {
|
|
|
|
SPI_HandleTypeDef *spi = spi_obj->spi;
|
2014-04-19 19:16:30 -04:00
|
|
|
HAL_SPI_DeInit(spi);
|
2014-04-20 14:06:15 -04:00
|
|
|
if (0) {
|
2015-12-12 10:02:02 -05:00
|
|
|
#if defined(MICROPY_HW_SPI1_SCK)
|
2014-04-20 14:06:15 -04:00
|
|
|
} else if (spi->Instance == SPI1) {
|
2018-02-12 23:53:08 -05:00
|
|
|
__HAL_RCC_SPI1_FORCE_RESET();
|
|
|
|
__HAL_RCC_SPI1_RELEASE_RESET();
|
|
|
|
__HAL_RCC_SPI1_CLK_DISABLE();
|
2015-12-12 10:02:02 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI2_SCK)
|
2014-04-19 19:16:30 -04:00
|
|
|
} else if (spi->Instance == SPI2) {
|
2018-02-12 23:53:08 -05:00
|
|
|
__HAL_RCC_SPI2_FORCE_RESET();
|
|
|
|
__HAL_RCC_SPI2_RELEASE_RESET();
|
|
|
|
__HAL_RCC_SPI2_CLK_DISABLE();
|
2015-12-12 10:02:02 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI3_SCK)
|
2014-04-20 03:06:03 -04:00
|
|
|
} else if (spi->Instance == SPI3) {
|
2018-02-12 23:53:08 -05:00
|
|
|
__HAL_RCC_SPI3_FORCE_RESET();
|
|
|
|
__HAL_RCC_SPI3_RELEASE_RESET();
|
|
|
|
__HAL_RCC_SPI3_CLK_DISABLE();
|
2015-12-12 10:02:02 -05:00
|
|
|
#endif
|
2015-12-08 09:41:47 -05:00
|
|
|
#if defined(MICROPY_HW_SPI4_SCK)
|
|
|
|
} else if (spi->Instance == SPI4) {
|
2018-02-12 23:53:08 -05:00
|
|
|
__HAL_RCC_SPI4_FORCE_RESET();
|
|
|
|
__HAL_RCC_SPI4_RELEASE_RESET();
|
|
|
|
__HAL_RCC_SPI4_CLK_DISABLE();
|
2015-12-08 09:41:47 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI5_SCK)
|
|
|
|
} else if (spi->Instance == SPI5) {
|
2018-02-12 23:53:08 -05:00
|
|
|
__HAL_RCC_SPI5_FORCE_RESET();
|
|
|
|
__HAL_RCC_SPI5_RELEASE_RESET();
|
|
|
|
__HAL_RCC_SPI5_CLK_DISABLE();
|
2015-12-08 09:41:47 -05:00
|
|
|
#endif
|
|
|
|
#if defined(MICROPY_HW_SPI6_SCK)
|
|
|
|
} else if (spi->Instance == SPI6) {
|
2018-02-12 23:53:08 -05:00
|
|
|
__HAL_RCC_SPI6_FORCE_RESET();
|
|
|
|
__HAL_RCC_SPI6_RELEASE_RESET();
|
|
|
|
__HAL_RCC_SPI6_CLK_DISABLE();
|
2015-12-08 09:41:47 -05:00
|
|
|
#endif
|
2014-04-19 19:16:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 03:36:52 -04:00
|
|
|
STATIC HAL_StatusTypeDef spi_wait_dma_finished(const spi_t *spi, uint32_t t_start, uint32_t timeout) {
|
2018-02-04 21:44:31 -05:00
|
|
|
volatile HAL_SPI_StateTypeDef *state = &spi->spi->State;
|
2018-01-31 19:45:29 -05:00
|
|
|
for (;;) {
|
|
|
|
// Do an atomic check of the state; WFI will exit even if IRQs are disabled
|
|
|
|
uint32_t irq_state = disable_irq();
|
2018-02-04 21:44:31 -05:00
|
|
|
if (*state == HAL_SPI_STATE_READY) {
|
2018-01-31 19:45:29 -05:00
|
|
|
enable_irq(irq_state);
|
|
|
|
return HAL_OK;
|
|
|
|
}
|
|
|
|
__WFI();
|
|
|
|
enable_irq(irq_state);
|
2018-06-18 03:36:52 -04:00
|
|
|
if (HAL_GetTick() - t_start >= timeout) {
|
2014-12-02 18:41:30 -05:00
|
|
|
return HAL_TIMEOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return HAL_OK;
|
|
|
|
}
|
|
|
|
|
2018-08-14 03:11:07 -04:00
|
|
|
void spi_transfer(const spi_t *self, size_t len, const uint8_t *src, uint8_t *dest, uint32_t timeout) {
|
2016-09-01 02:36:41 -04:00
|
|
|
// Note: there seems to be a problem sending 1 byte using DMA the first
|
|
|
|
// time directly after the SPI/DMA is initialised. The cause of this is
|
|
|
|
// unknown but we sidestep the issue by using polling for 1 byte transfer.
|
|
|
|
|
2018-06-18 03:36:52 -04:00
|
|
|
// Note: DMA transfers are limited to 65535 bytes at a time.
|
|
|
|
|
2016-09-01 02:36:41 -04:00
|
|
|
HAL_StatusTypeDef status;
|
|
|
|
|
2016-10-02 21:47:56 -04:00
|
|
|
if (dest == NULL) {
|
2016-09-01 02:36:41 -04:00
|
|
|
// send only
|
2016-10-02 21:47:56 -04:00
|
|
|
if (len == 1 || query_irq() == IRQ_STATE_DISABLED) {
|
|
|
|
status = HAL_SPI_Transmit(self->spi, (uint8_t*)src, len, timeout);
|
2016-09-01 02:36:41 -04:00
|
|
|
} else {
|
|
|
|
DMA_HandleTypeDef tx_dma;
|
2018-09-11 03:18:06 -04:00
|
|
|
dma_init(&tx_dma, self->tx_dma_descr, DMA_MEMORY_TO_PERIPH, self->spi);
|
2016-09-01 02:36:41 -04:00
|
|
|
self->spi->hdmatx = &tx_dma;
|
|
|
|
self->spi->hdmarx = NULL;
|
2017-03-27 21:54:01 -04:00
|
|
|
MP_HAL_CLEAN_DCACHE(src, len);
|
2018-06-18 03:36:52 -04:00
|
|
|
uint32_t t_start = HAL_GetTick();
|
|
|
|
do {
|
|
|
|
uint32_t l = MIN(len, 65535);
|
|
|
|
status = HAL_SPI_Transmit_DMA(self->spi, (uint8_t*)src, l);
|
|
|
|
if (status != HAL_OK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
status = spi_wait_dma_finished(self, t_start, timeout);
|
|
|
|
if (status != HAL_OK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len -= l;
|
|
|
|
src += l;
|
|
|
|
} while (len);
|
2016-09-01 02:36:41 -04:00
|
|
|
dma_deinit(self->tx_dma_descr);
|
|
|
|
}
|
2016-10-02 21:47:56 -04:00
|
|
|
} else if (src == NULL) {
|
2016-09-01 02:36:41 -04:00
|
|
|
// receive only
|
2016-10-02 21:47:56 -04:00
|
|
|
if (len == 1 || query_irq() == IRQ_STATE_DISABLED) {
|
|
|
|
status = HAL_SPI_Receive(self->spi, dest, len, timeout);
|
2016-09-01 02:36:41 -04:00
|
|
|
} else {
|
|
|
|
DMA_HandleTypeDef tx_dma, rx_dma;
|
|
|
|
if (self->spi->Init.Mode == SPI_MODE_MASTER) {
|
|
|
|
// in master mode the HAL actually does a TransmitReceive call
|
2018-09-11 03:18:06 -04:00
|
|
|
dma_init(&tx_dma, self->tx_dma_descr, DMA_MEMORY_TO_PERIPH, self->spi);
|
2016-09-01 02:36:41 -04:00
|
|
|
self->spi->hdmatx = &tx_dma;
|
|
|
|
} else {
|
|
|
|
self->spi->hdmatx = NULL;
|
|
|
|
}
|
2018-09-11 03:18:06 -04:00
|
|
|
dma_init(&rx_dma, self->rx_dma_descr, DMA_PERIPH_TO_MEMORY, self->spi);
|
2016-09-01 02:36:41 -04:00
|
|
|
self->spi->hdmarx = &rx_dma;
|
2017-03-27 21:54:01 -04:00
|
|
|
MP_HAL_CLEANINVALIDATE_DCACHE(dest, len);
|
2018-06-18 03:36:52 -04:00
|
|
|
uint32_t t_start = HAL_GetTick();
|
|
|
|
do {
|
|
|
|
uint32_t l = MIN(len, 65535);
|
|
|
|
status = HAL_SPI_Receive_DMA(self->spi, dest, l);
|
|
|
|
if (status != HAL_OK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
status = spi_wait_dma_finished(self, t_start, timeout);
|
|
|
|
if (status != HAL_OK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len -= l;
|
|
|
|
dest += l;
|
|
|
|
} while (len);
|
2016-09-01 02:36:41 -04:00
|
|
|
if (self->spi->hdmatx != NULL) {
|
|
|
|
dma_deinit(self->tx_dma_descr);
|
|
|
|
}
|
|
|
|
dma_deinit(self->rx_dma_descr);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// send and receive
|
2016-10-02 21:47:56 -04:00
|
|
|
if (len == 1 || query_irq() == IRQ_STATE_DISABLED) {
|
|
|
|
status = HAL_SPI_TransmitReceive(self->spi, (uint8_t*)src, dest, len, timeout);
|
2016-09-01 02:36:41 -04:00
|
|
|
} else {
|
|
|
|
DMA_HandleTypeDef tx_dma, rx_dma;
|
2018-09-11 03:18:06 -04:00
|
|
|
dma_init(&tx_dma, self->tx_dma_descr, DMA_MEMORY_TO_PERIPH, self->spi);
|
2016-09-01 02:36:41 -04:00
|
|
|
self->spi->hdmatx = &tx_dma;
|
2018-09-11 03:18:06 -04:00
|
|
|
dma_init(&rx_dma, self->rx_dma_descr, DMA_PERIPH_TO_MEMORY, self->spi);
|
2016-09-01 02:36:41 -04:00
|
|
|
self->spi->hdmarx = &rx_dma;
|
2017-03-27 21:54:01 -04:00
|
|
|
MP_HAL_CLEAN_DCACHE(src, len);
|
|
|
|
MP_HAL_CLEANINVALIDATE_DCACHE(dest, len);
|
2018-06-18 03:36:52 -04:00
|
|
|
uint32_t t_start = HAL_GetTick();
|
|
|
|
do {
|
|
|
|
uint32_t l = MIN(len, 65535);
|
|
|
|
status = HAL_SPI_TransmitReceive_DMA(self->spi, (uint8_t*)src, dest, l);
|
|
|
|
if (status != HAL_OK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
status = spi_wait_dma_finished(self, t_start, timeout);
|
|
|
|
if (status != HAL_OK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len -= l;
|
|
|
|
src += l;
|
|
|
|
dest += l;
|
|
|
|
} while (len);
|
2016-09-01 02:36:41 -04:00
|
|
|
dma_deinit(self->tx_dma_descr);
|
|
|
|
dma_deinit(self->rx_dma_descr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status != HAL_OK) {
|
|
|
|
mp_hal_raise(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 03:11:07 -04:00
|
|
|
void spi_print(const mp_print_t *print, const spi_t *spi_obj, bool legacy) {
|
2018-02-04 21:44:31 -05:00
|
|
|
SPI_HandleTypeDef *spi = spi_obj->spi;
|
|
|
|
|
2015-12-08 09:41:47 -05:00
|
|
|
uint spi_num = 1; // default to SPI1
|
2016-10-03 01:45:46 -04:00
|
|
|
if (spi->Instance == SPI2) { spi_num = 2; }
|
2018-05-28 04:10:53 -04:00
|
|
|
#if defined(SPI3)
|
2016-10-03 01:45:46 -04:00
|
|
|
else if (spi->Instance == SPI3) { spi_num = 3; }
|
2018-05-28 04:10:53 -04:00
|
|
|
#endif
|
2015-12-08 09:41:47 -05:00
|
|
|
#if defined(SPI4)
|
2016-10-03 01:45:46 -04:00
|
|
|
else if (spi->Instance == SPI4) { spi_num = 4; }
|
2015-12-08 09:41:47 -05:00
|
|
|
#endif
|
|
|
|
#if defined(SPI5)
|
2016-10-03 01:45:46 -04:00
|
|
|
else if (spi->Instance == SPI5) { spi_num = 5; }
|
2015-12-08 09:41:47 -05:00
|
|
|
#endif
|
|
|
|
#if defined(SPI6)
|
2016-10-03 01:45:46 -04:00
|
|
|
else if (spi->Instance == SPI6) { spi_num = 6; }
|
2015-12-08 09:41:47 -05:00
|
|
|
#endif
|
2014-04-19 19:16:30 -04:00
|
|
|
|
2016-10-03 01:45:46 -04:00
|
|
|
mp_printf(print, "SPI(%u", spi_num);
|
|
|
|
if (spi->State != HAL_SPI_STATE_RESET) {
|
|
|
|
if (spi->Init.Mode == SPI_MODE_MASTER) {
|
2014-04-19 19:16:30 -04:00
|
|
|
// compute baudrate
|
|
|
|
uint spi_clock;
|
2018-05-28 04:10:53 -04:00
|
|
|
#if defined(STM32F0)
|
|
|
|
spi_clock = HAL_RCC_GetPCLK1Freq();
|
|
|
|
#else
|
2016-10-03 01:45:46 -04:00
|
|
|
if (spi->Instance == SPI2 || spi->Instance == SPI3) {
|
2014-04-19 19:16:30 -04:00
|
|
|
// SPI2 and SPI3 are on APB1
|
|
|
|
spi_clock = HAL_RCC_GetPCLK1Freq();
|
2015-12-08 09:41:47 -05:00
|
|
|
} else {
|
|
|
|
// SPI1, SPI4, SPI5 and SPI6 are on APB2
|
|
|
|
spi_clock = HAL_RCC_GetPCLK2Freq();
|
2014-04-19 19:16:30 -04:00
|
|
|
}
|
2018-05-28 04:10:53 -04:00
|
|
|
#endif
|
2016-10-03 01:45:46 -04:00
|
|
|
uint log_prescaler = (spi->Init.BaudRatePrescaler >> 3) + 1;
|
2014-12-08 16:34:07 -05:00
|
|
|
uint baudrate = spi_clock >> log_prescaler;
|
2016-10-03 01:45:46 -04:00
|
|
|
if (legacy) {
|
|
|
|
mp_printf(print, ", SPI.MASTER");
|
|
|
|
}
|
|
|
|
mp_printf(print, ", baudrate=%u", baudrate);
|
|
|
|
if (legacy) {
|
|
|
|
mp_printf(print, ", prescaler=%u", 1 << log_prescaler);
|
|
|
|
}
|
2014-04-19 19:16:30 -04:00
|
|
|
} else {
|
2016-10-03 01:45:46 -04:00
|
|
|
mp_printf(print, ", SPI.SLAVE");
|
2014-04-19 19:16:30 -04:00
|
|
|
}
|
2016-10-03 01:45:46 -04:00
|
|
|
mp_printf(print, ", polarity=%u, phase=%u, bits=%u", spi->Init.CLKPolarity == SPI_POLARITY_LOW ? 0 : 1, spi->Init.CLKPhase == SPI_PHASE_1EDGE ? 0 : 1, spi->Init.DataSize == SPI_DATASIZE_8BIT ? 8 : 16);
|
2018-02-12 23:37:35 -05:00
|
|
|
if (spi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) {
|
2016-10-03 01:45:46 -04:00
|
|
|
mp_printf(print, ", crc=0x%x", spi->Init.CRCPolynomial);
|
2014-04-20 19:10:04 -04:00
|
|
|
}
|
2014-04-19 19:16:30 -04:00
|
|
|
}
|
2016-10-03 01:45:46 -04:00
|
|
|
mp_print_str(print, ")");
|
|
|
|
}
|
|
|
|
|
2018-02-04 21:51:27 -05:00
|
|
|
const spi_t *spi_from_mp_obj(mp_obj_t o) {
|
|
|
|
if (MP_OBJ_IS_TYPE(o, &pyb_spi_type)) {
|
2018-07-08 09:25:11 -04:00
|
|
|
pyb_spi_obj_t *self = MP_OBJ_TO_PTR(o);
|
2018-02-04 21:51:27 -05:00
|
|
|
return self->spi;
|
|
|
|
} else if (MP_OBJ_IS_TYPE(o, &machine_hard_spi_type)) {
|
2018-07-08 09:25:11 -04:00
|
|
|
machine_hard_spi_obj_t *self = MP_OBJ_TO_PTR(o);
|
2018-02-04 21:51:27 -05:00
|
|
|
return self->spi;
|
|
|
|
} else {
|
|
|
|
mp_raise_TypeError("expecting an SPI object");
|
|
|
|
}
|
|
|
|
}
|
stm32/spi: Add implementation of low-level SPI protocol.
Can be used, for example, to configure external SPI flash using a hardware
SPI interface (code to be put in a board's bdev.c file):
STATIC const spi_proto_cfg_t hard_spi_bus = {
.spi = &spi_obj[5],
.baudrate = 10000000,
.polarity = 0,
.phase = 0,
.bits = 8,
.firstbit = SPI_FIRSTBIT_MSB,
};
STATIC mp_spiflash_cache_t spi_bdev_cache;
const mp_spiflash_config_t spiflash_config = {
.bus_kind = MP_SPIFLASH_BUS_SPI,
.bus.u_spi.cs = pin_A0,
.bus.u_spi.data = (void*)&hard_spi_bus,
.bus.u_spi.proto = &spi_proto,
.cache = &spi_bdev_cache,
};
spi_bdev_t spi_bdev;
2018-08-14 08:05:20 -04:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// Implementation of low-level SPI C protocol
|
|
|
|
|
|
|
|
STATIC int spi_proto_ioctl(void *self_in, uint32_t cmd) {
|
|
|
|
spi_proto_cfg_t *self = (spi_proto_cfg_t*)self_in;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case MP_SPI_IOCTL_INIT:
|
|
|
|
spi_set_params(self->spi, 0xffffffff, self->baudrate,
|
|
|
|
self->polarity, self->phase, self->bits, self->firstbit);
|
|
|
|
spi_init(self->spi, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_SPI_IOCTL_DEINIT:
|
|
|
|
spi_deinit(self->spi);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void spi_proto_transfer(void *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
|
|
|
|
spi_proto_cfg_t *self = (spi_proto_cfg_t*)self_in;
|
|
|
|
spi_transfer(self->spi, len, src, dest, SPI_TRANSFER_TIMEOUT(len));
|
|
|
|
}
|
|
|
|
|
|
|
|
const mp_spi_proto_t spi_proto = {
|
|
|
|
.ioctl = spi_proto_ioctl,
|
|
|
|
.transfer = spi_proto_transfer,
|
|
|
|
};
|