mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-24 16:55:36 +03:00
371 lines
9.3 KiB
C
371 lines
9.3 KiB
C
/*
|
|
* This file is part of Cleanflight.
|
|
*
|
|
* Cleanflight is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Cleanflight is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#include <platform.h>
|
|
|
|
#if defined(USE_SPI)
|
|
|
|
#include "common/utils.h"
|
|
|
|
#include "drivers/bus.h"
|
|
#include "drivers/bus_spi.h"
|
|
#include "drivers/bus_spi_impl.h"
|
|
#include "drivers/dma.h"
|
|
#include "drivers/io.h"
|
|
#include "drivers/nvic.h"
|
|
#include "drivers/rcc.h"
|
|
|
|
spiDevice_t spiDevice[SPIDEV_COUNT];
|
|
|
|
#ifndef SPI2_SCK_PIN
|
|
#define SPI2_NSS_PIN PB12
|
|
#define SPI2_SCK_PIN PB13
|
|
#define SPI2_MISO_PIN PB14
|
|
#define SPI2_MOSI_PIN PB15
|
|
#endif
|
|
|
|
#ifndef SPI3_SCK_PIN
|
|
#define SPI3_NSS_PIN PA15
|
|
#define SPI3_SCK_PIN PB3
|
|
#define SPI3_MISO_PIN PB4
|
|
#define SPI3_MOSI_PIN PB5
|
|
#endif
|
|
|
|
#ifndef SPI4_SCK_PIN
|
|
#define SPI4_NSS_PIN PA15
|
|
#define SPI4_SCK_PIN PB3
|
|
#define SPI4_MISO_PIN PB4
|
|
#define SPI4_MOSI_PIN PB5
|
|
#endif
|
|
|
|
#ifndef SPI1_NSS_PIN
|
|
#define SPI1_NSS_PIN NONE
|
|
#endif
|
|
#ifndef SPI2_NSS_PIN
|
|
#define SPI2_NSS_PIN NONE
|
|
#endif
|
|
#ifndef SPI3_NSS_PIN
|
|
#define SPI3_NSS_PIN NONE
|
|
#endif
|
|
#ifndef SPI4_NSS_PIN
|
|
#define SPI4_NSS_PIN NONE
|
|
#endif
|
|
|
|
#define SPI_DEFAULT_TIMEOUT 10
|
|
|
|
SPIDevice spiDeviceByInstance(SPI_TypeDef *instance)
|
|
{
|
|
#ifdef USE_SPI_DEVICE_1
|
|
if (instance == SPI1)
|
|
return SPIDEV_1;
|
|
#endif
|
|
|
|
#ifdef USE_SPI_DEVICE_2
|
|
if (instance == SPI2)
|
|
return SPIDEV_2;
|
|
#endif
|
|
|
|
#ifdef USE_SPI_DEVICE_3
|
|
if (instance == SPI3)
|
|
return SPIDEV_3;
|
|
#endif
|
|
|
|
#ifdef USE_SPI_DEVICE_4
|
|
if (instance == SPI4)
|
|
return SPIDEV_4;
|
|
#endif
|
|
|
|
return SPIINVALID;
|
|
}
|
|
|
|
SPI_TypeDef *spiInstanceByDevice(SPIDevice device)
|
|
{
|
|
if (device >= SPIDEV_COUNT) {
|
|
return NULL;
|
|
}
|
|
|
|
return spiDevice[device].dev;
|
|
}
|
|
|
|
void spiInitDevice(SPIDevice device)
|
|
{
|
|
spiDevice_t *spi = &(spiDevice[device]);
|
|
|
|
#ifdef SDCARD_SPI_INSTANCE
|
|
if (spi->dev == SDCARD_SPI_INSTANCE) {
|
|
spi->leadingEdge = true;
|
|
}
|
|
#endif
|
|
#ifdef RX_SPI_INSTANCE
|
|
if (spi->dev == RX_SPI_INSTANCE) {
|
|
spi->leadingEdge = true;
|
|
}
|
|
#endif
|
|
|
|
// Enable SPI clock
|
|
RCC_ClockCmd(spi->rcc, ENABLE);
|
|
RCC_ResetCmd(spi->rcc, ENABLE);
|
|
|
|
IOInit(IOGetByTag(spi->sck), OWNER_SPI_SCK, RESOURCE_INDEX(device));
|
|
IOInit(IOGetByTag(spi->miso), OWNER_SPI_MISO, RESOURCE_INDEX(device));
|
|
IOInit(IOGetByTag(spi->mosi), OWNER_SPI_MOSI, RESOURCE_INDEX(device));
|
|
|
|
if (spi->leadingEdge == true)
|
|
IOConfigGPIOAF(IOGetByTag(spi->sck), SPI_IO_AF_SCK_CFG_LOW, spi->sckAF);
|
|
else
|
|
IOConfigGPIOAF(IOGetByTag(spi->sck), SPI_IO_AF_SCK_CFG_HIGH, spi->sckAF);
|
|
IOConfigGPIOAF(IOGetByTag(spi->miso), SPI_IO_AF_MISO_CFG, spi->misoAF);
|
|
IOConfigGPIOAF(IOGetByTag(spi->mosi), SPI_IO_AF_CFG, spi->mosiAF);
|
|
|
|
LL_SPI_Disable(spi->dev);
|
|
LL_SPI_DeInit(spi->dev);
|
|
|
|
LL_SPI_InitTypeDef init =
|
|
{
|
|
.TransferDirection = SPI_DIRECTION_2LINES,
|
|
.Mode = SPI_MODE_MASTER,
|
|
.DataWidth = SPI_DATASIZE_8BIT,
|
|
.ClockPolarity = spi->leadingEdge ? SPI_POLARITY_LOW : SPI_POLARITY_HIGH,
|
|
.ClockPhase = spi->leadingEdge ? SPI_PHASE_1EDGE : SPI_PHASE_2EDGE,
|
|
.NSS = SPI_NSS_SOFT,
|
|
.BaudRate = SPI_BAUDRATEPRESCALER_8,
|
|
.BitOrder = SPI_FIRSTBIT_MSB,
|
|
.CRCPoly = 7,
|
|
.CRCCalculation = SPI_CRCCALCULATION_DISABLE,
|
|
};
|
|
LL_SPI_SetRxFIFOThreshold(spi->dev, SPI_RXFIFO_THRESHOLD_QF);
|
|
|
|
LL_SPI_Init(spi->dev, &init);
|
|
LL_SPI_Enable(spi->dev);
|
|
}
|
|
|
|
bool spiInit(SPIDevice device)
|
|
{
|
|
switch (device) {
|
|
case SPIINVALID:
|
|
return false;
|
|
case SPIDEV_1:
|
|
#if defined(USE_SPI_DEVICE_1)
|
|
spiInitDevice(device);
|
|
return true;
|
|
#else
|
|
break;
|
|
#endif
|
|
case SPIDEV_2:
|
|
#if defined(USE_SPI_DEVICE_2)
|
|
spiInitDevice(device);
|
|
return true;
|
|
#else
|
|
break;
|
|
#endif
|
|
case SPIDEV_3:
|
|
#if defined(USE_SPI_DEVICE_3)
|
|
spiInitDevice(device);
|
|
return true;
|
|
#else
|
|
break;
|
|
#endif
|
|
case SPIDEV_4:
|
|
#if defined(USE_SPI_DEVICE_4)
|
|
spiInitDevice(device);
|
|
return true;
|
|
#else
|
|
break;
|
|
#endif
|
|
}
|
|
return false;
|
|
}
|
|
|
|
uint32_t spiTimeoutUserCallback(SPI_TypeDef *instance)
|
|
{
|
|
SPIDevice device = spiDeviceByInstance(instance);
|
|
if (device == SPIINVALID) {
|
|
return -1;
|
|
}
|
|
spiDevice[device].errorCount++;
|
|
return spiDevice[device].errorCount;
|
|
}
|
|
|
|
uint8_t spiTransferByte(SPI_TypeDef *instance, uint8_t txByte)
|
|
{
|
|
uint16_t spiTimeout = 1000;
|
|
|
|
while (!LL_SPI_IsActiveFlag_TXE(instance))
|
|
if ((spiTimeout--) == 0)
|
|
return spiTimeoutUserCallback(instance);
|
|
|
|
LL_SPI_TransmitData8(instance, txByte);
|
|
|
|
spiTimeout = 1000;
|
|
while (!LL_SPI_IsActiveFlag_RXNE(instance))
|
|
if ((spiTimeout--) == 0)
|
|
return spiTimeoutUserCallback(instance);
|
|
|
|
return (uint8_t)LL_SPI_ReceiveData8(instance);
|
|
}
|
|
|
|
/**
|
|
* Return true if the bus is currently in the middle of a transmission.
|
|
*/
|
|
bool spiIsBusBusy(SPI_TypeDef *instance)
|
|
{
|
|
return LL_SPI_GetTxFIFOLevel(instance) != LL_SPI_TX_FIFO_EMPTY
|
|
|| LL_SPI_IsActiveFlag_BSY(instance);
|
|
}
|
|
|
|
bool spiTransfer(SPI_TypeDef *instance, const uint8_t *txData, uint8_t *rxData, int len)
|
|
{
|
|
// set 16-bit transfer
|
|
CLEAR_BIT(instance->CR2, SPI_RXFIFO_THRESHOLD);
|
|
while (len > 1) {
|
|
int spiTimeout = 1000;
|
|
while (!LL_SPI_IsActiveFlag_TXE(instance)) {
|
|
if ((spiTimeout--) == 0) {
|
|
return spiTimeoutUserCallback(instance);
|
|
}
|
|
}
|
|
uint16_t w;
|
|
if (txData) {
|
|
w = *((uint16_t *)txData);
|
|
txData += 2;
|
|
} else {
|
|
w = 0xFFFF;
|
|
}
|
|
LL_SPI_TransmitData16(instance, w);
|
|
|
|
spiTimeout = 1000;
|
|
while (!LL_SPI_IsActiveFlag_RXNE(instance)) {
|
|
if ((spiTimeout--) == 0) {
|
|
return spiTimeoutUserCallback(instance);
|
|
}
|
|
}
|
|
w = LL_SPI_ReceiveData16(instance);
|
|
if (rxData) {
|
|
*((uint16_t *)rxData) = w;
|
|
rxData += 2;
|
|
}
|
|
len -= 2;
|
|
}
|
|
// set 8-bit transfer
|
|
SET_BIT(instance->CR2, SPI_RXFIFO_THRESHOLD);
|
|
if (len) {
|
|
int spiTimeout = 1000;
|
|
while (!LL_SPI_IsActiveFlag_TXE(instance)) {
|
|
if ((spiTimeout--) == 0) {
|
|
return spiTimeoutUserCallback(instance);
|
|
}
|
|
}
|
|
uint8_t b = txData ? *(txData++) : 0xFF;
|
|
LL_SPI_TransmitData8(instance, b);
|
|
|
|
spiTimeout = 1000;
|
|
while (!LL_SPI_IsActiveFlag_RXNE(instance)) {
|
|
if ((spiTimeout--) == 0) {
|
|
return spiTimeoutUserCallback(instance);
|
|
}
|
|
}
|
|
b = LL_SPI_ReceiveData8(instance);
|
|
if (rxData) {
|
|
*(rxData++) = b;
|
|
}
|
|
--len;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool spiBusTransfer(const busDevice_t *bus, const uint8_t *txData, uint8_t *rxData, int length)
|
|
{
|
|
IOLo(bus->busdev_u.spi.csnPin);
|
|
spiTransfer(bus->busdev_u.spi.instance, txData, rxData, length);
|
|
IOHi(bus->busdev_u.spi.csnPin);
|
|
return true;
|
|
}
|
|
|
|
void spiSetDivisor(SPI_TypeDef *instance, uint16_t divisor)
|
|
{
|
|
#if !(defined(STM32F1) || defined(STM32F3))
|
|
// SPI2 and SPI3 are on APB1/AHB1 which PCLK is half that of APB2/AHB2.
|
|
|
|
if (instance == SPI2 || instance == SPI3) {
|
|
divisor /= 2; // Safe for divisor == 0 or 1
|
|
}
|
|
#endif
|
|
|
|
LL_SPI_Disable(instance);
|
|
LL_SPI_SetBaudRatePrescaler(instance, divisor ? (ffs(divisor | 0x100) - 2) << SPI_CR1_BR_Pos : 0);
|
|
LL_SPI_Enable(instance);
|
|
}
|
|
|
|
uint16_t spiGetErrorCounter(SPI_TypeDef *instance)
|
|
{
|
|
SPIDevice device = spiDeviceByInstance(instance);
|
|
if (device == SPIINVALID) {
|
|
return 0;
|
|
}
|
|
return spiDevice[device].errorCount;
|
|
}
|
|
|
|
void spiResetErrorCounter(SPI_TypeDef *instance)
|
|
{
|
|
SPIDevice device = spiDeviceByInstance(instance);
|
|
if (device != SPIINVALID) {
|
|
spiDevice[device].errorCount = 0;
|
|
}
|
|
}
|
|
|
|
bool spiBusWriteRegister(const busDevice_t *bus, uint8_t reg, uint8_t data)
|
|
{
|
|
IOLo(bus->busdev_u.spi.csnPin);
|
|
spiTransferByte(bus->busdev_u.spi.instance, reg);
|
|
spiTransferByte(bus->busdev_u.spi.instance, data);
|
|
IOHi(bus->busdev_u.spi.csnPin);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool spiBusReadRegisterBuffer(const busDevice_t *bus, uint8_t reg, uint8_t *data, uint8_t length)
|
|
{
|
|
IOLo(bus->busdev_u.spi.csnPin);
|
|
spiTransferByte(bus->busdev_u.spi.instance, reg | 0x80); // read transaction
|
|
spiTransfer(bus->busdev_u.spi.instance, NULL, data, length);
|
|
IOHi(bus->busdev_u.spi.csnPin);
|
|
|
|
return true;
|
|
}
|
|
|
|
uint8_t spiBusReadRegister(const busDevice_t *bus, uint8_t reg)
|
|
{
|
|
uint8_t data;
|
|
IOLo(bus->busdev_u.spi.csnPin);
|
|
spiTransferByte(bus->busdev_u.spi.instance, reg | 0x80); // read transaction
|
|
spiTransfer(bus->busdev_u.spi.instance, NULL, &data, 1);
|
|
IOHi(bus->busdev_u.spi.csnPin);
|
|
|
|
return data;
|
|
}
|
|
|
|
void spiBusSetInstance(busDevice_t *bus, SPI_TypeDef *instance)
|
|
{
|
|
bus->busdev_u.spi.instance = instance;
|
|
}
|
|
|
|
#endif
|