2016-11-08 18:26:58 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014-2016 Freie Universität Berlin
|
|
|
|
* 2015 Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
* 2015 FreshTemp, LLC.
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-06-22 15:43:17 +02:00
|
|
|
* @ingroup cpu_sam0_common
|
|
|
|
* @ingroup drivers_periph_spi
|
2016-11-08 18:26:58 +01:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Low-level SPI driver implementation
|
|
|
|
*
|
|
|
|
* @author Thomas Eichinger <thomas.eichinger@fu-berlin.de>
|
|
|
|
* @author Troels Hoffmeyer <troels.d.hoffmeyer@gmail.com>
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
|
|
* @author Joakim Nohlgård <joakim.nohlgard@eistec.se>
|
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "mutex.h"
|
|
|
|
#include "assert.h"
|
|
|
|
#include "periph/spi.h"
|
2020-06-11 11:07:06 +02:00
|
|
|
#include "pm_layered.h"
|
2016-11-08 18:26:58 +01:00
|
|
|
|
2020-10-22 11:34:00 +02:00
|
|
|
#define ENABLE_DEBUG 0
|
2016-11-08 18:26:58 +01:00
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Array holding one pre-initialized mutex for each SPI device
|
|
|
|
*/
|
|
|
|
static mutex_t locks[SPI_NUMOF];
|
|
|
|
|
2020-06-11 11:07:06 +02:00
|
|
|
#ifdef MODULE_PERIPH_DMA
|
|
|
|
struct dma_state {
|
|
|
|
dma_t tx_dma;
|
|
|
|
dma_t rx_dma;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct dma_state _dma_state[SPI_NUMOF];
|
|
|
|
|
|
|
|
static DmacDescriptor DMA_DESCRIPTOR_ATTRS tx_desc[SPI_NUMOF];
|
|
|
|
static DmacDescriptor DMA_DESCRIPTOR_ATTRS rx_desc[SPI_NUMOF];
|
|
|
|
#endif
|
|
|
|
|
2016-11-08 18:26:58 +01:00
|
|
|
/**
|
|
|
|
* @brief Shortcut for accessing the used SPI SERCOM device
|
|
|
|
*/
|
|
|
|
static inline SercomSpi *dev(spi_t bus)
|
|
|
|
{
|
|
|
|
return spi_config[bus].dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void poweron(spi_t bus)
|
|
|
|
{
|
2019-03-29 13:39:34 +01:00
|
|
|
sercom_clk_en(dev(bus));
|
2016-11-08 18:26:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void poweroff(spi_t bus)
|
|
|
|
{
|
2019-03-29 13:39:34 +01:00
|
|
|
sercom_clk_dis(dev(bus));
|
2016-11-08 18:26:58 +01:00
|
|
|
}
|
|
|
|
|
2020-06-17 17:59:48 +02:00
|
|
|
static void _reset(SercomSpi *dev)
|
|
|
|
{
|
|
|
|
dev->CTRLA.reg |= SERCOM_SPI_CTRLA_SWRST;
|
|
|
|
while (dev->CTRLA.reg & SERCOM_SPI_CTRLA_SWRST) {}
|
|
|
|
|
|
|
|
#ifdef SERCOM_SPI_STATUS_SYNCBUSY
|
|
|
|
while (dev->STATUS.bit.SYNCBUSY) {}
|
|
|
|
#else
|
|
|
|
while (dev->SYNCBUSY.bit.SWRST) {}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void _disable(SercomSpi *dev)
|
|
|
|
{
|
|
|
|
dev->CTRLA.reg = 0;
|
|
|
|
|
|
|
|
#ifdef SERCOM_SPI_STATUS_SYNCBUSY
|
|
|
|
while (dev->STATUS.bit.SYNCBUSY) {}
|
|
|
|
#else
|
|
|
|
while (dev->SYNCBUSY.reg) {}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void _enable(SercomSpi *dev)
|
|
|
|
{
|
|
|
|
dev->CTRLA.bit.ENABLE = 1;
|
|
|
|
|
|
|
|
#ifdef SERCOM_SPI_STATUS_SYNCBUSY
|
|
|
|
while (dev->STATUS.bit.SYNCBUSY) {}
|
|
|
|
#else
|
|
|
|
while (dev->SYNCBUSY.reg) {}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-06-11 11:07:06 +02:00
|
|
|
static inline bool _use_dma(spi_t bus)
|
|
|
|
{
|
|
|
|
#ifdef MODULE_PERIPH_DMA
|
|
|
|
return (spi_config[bus].tx_trigger != DMA_TRIGGER_DISABLED) &&
|
|
|
|
(spi_config[bus].rx_trigger != DMA_TRIGGER_DISABLED);
|
|
|
|
#else
|
|
|
|
(void)bus;
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-01 22:03:08 +01:00
|
|
|
static inline void _init_dma(spi_t bus, volatile void *reg_rx, volatile void *reg_tx)
|
|
|
|
{
|
|
|
|
if (!_use_dma(bus)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MODULE_PERIPH_DMA
|
|
|
|
_dma_state[bus].rx_dma = dma_acquire_channel();
|
|
|
|
_dma_state[bus].tx_dma = dma_acquire_channel();
|
|
|
|
|
|
|
|
dma_setup(_dma_state[bus].tx_dma,
|
|
|
|
spi_config[bus].tx_trigger, 0, false);
|
|
|
|
dma_setup(_dma_state[bus].rx_dma,
|
|
|
|
spi_config[bus].rx_trigger, 1, true);
|
|
|
|
|
|
|
|
dma_prepare(_dma_state[bus].rx_dma, DMAC_BTCTRL_BEATSIZE_BYTE_Val,
|
|
|
|
reg_rx, NULL, 1, 0);
|
|
|
|
dma_prepare(_dma_state[bus].tx_dma, DMAC_BTCTRL_BEATSIZE_BYTE_Val,
|
|
|
|
NULL, reg_tx, 0, 0);
|
|
|
|
#else
|
|
|
|
(void)reg_rx;
|
|
|
|
(void)reg_tx;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-11-08 18:26:58 +01:00
|
|
|
void spi_init(spi_t bus)
|
|
|
|
{
|
|
|
|
/* make sure given bus is good */
|
|
|
|
assert(bus < SPI_NUMOF);
|
|
|
|
|
|
|
|
/* initialize the device lock */
|
|
|
|
mutex_init(&locks[bus]);
|
|
|
|
|
|
|
|
/* configure pins and their muxes */
|
|
|
|
spi_init_pins(bus);
|
|
|
|
|
|
|
|
/* wake up device */
|
|
|
|
poweron(bus);
|
|
|
|
|
|
|
|
/* reset all device configuration */
|
2020-06-17 17:59:48 +02:00
|
|
|
_reset(dev(bus));
|
2016-11-08 18:26:58 +01:00
|
|
|
|
2020-11-01 22:03:08 +01:00
|
|
|
/* configure base clock */
|
2019-12-16 19:40:23 +01:00
|
|
|
sercom_set_gen(dev(bus), spi_config[bus].gclk_src);
|
2016-11-08 18:26:58 +01:00
|
|
|
|
|
|
|
/* enable receiver and configure character size to 8-bit
|
|
|
|
* no synchronization needed, as SERCOM device is not enabled */
|
|
|
|
dev(bus)->CTRLB.reg = (SERCOM_SPI_CTRLB_CHSIZE(0) | SERCOM_SPI_CTRLB_RXEN);
|
|
|
|
|
2020-11-01 22:03:08 +01:00
|
|
|
/* set up DMA channels */
|
|
|
|
_init_dma(bus, &dev(bus)->DATA.reg, &dev(bus)->DATA.reg);
|
|
|
|
|
2016-11-08 18:26:58 +01:00
|
|
|
/* put device back to sleep */
|
|
|
|
poweroff(bus);
|
2020-06-11 11:07:06 +02:00
|
|
|
|
2016-11-08 18:26:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void spi_init_pins(spi_t bus)
|
|
|
|
{
|
2017-02-24 21:10:42 +01:00
|
|
|
/* MISO must always have PD/PU, see #5968. This is a ~65uA difference */
|
|
|
|
gpio_init(spi_config[bus].miso_pin, GPIO_IN_PD);
|
2016-11-08 18:26:58 +01:00
|
|
|
gpio_init(spi_config[bus].mosi_pin, GPIO_OUT);
|
|
|
|
gpio_init(spi_config[bus].clk_pin, GPIO_OUT);
|
|
|
|
gpio_init_mux(spi_config[bus].miso_pin, spi_config[bus].miso_mux);
|
|
|
|
gpio_init_mux(spi_config[bus].mosi_pin, spi_config[bus].mosi_mux);
|
2020-05-20 17:16:17 +02:00
|
|
|
/* clk_pin will be muxed during acquire / release */
|
2020-03-13 17:33:43 +01:00
|
|
|
|
|
|
|
mutex_unlock(&locks[bus]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void spi_deinit_pins(spi_t bus)
|
|
|
|
{
|
|
|
|
mutex_lock(&locks[bus]);
|
|
|
|
|
|
|
|
gpio_disable_mux(spi_config[bus].miso_pin);
|
|
|
|
gpio_disable_mux(spi_config[bus].mosi_pin);
|
2016-11-08 18:26:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
|
|
|
|
{
|
2019-11-22 14:44:42 +01:00
|
|
|
(void)cs;
|
2017-02-24 21:10:42 +01:00
|
|
|
|
2016-11-08 18:26:58 +01:00
|
|
|
/* configure bus clock, in synchronous mode its calculated from
|
|
|
|
* BAUD.reg = (f_ref / (2 * f_bus) - 1)
|
2020-03-03 12:24:42 +01:00
|
|
|
* with f_ref := CLOCK_CORECLOCK as defined by the board
|
|
|
|
* to mitigate the rounding error due to integer arithmetic, the
|
|
|
|
* equation is modified to
|
|
|
|
* BAUD.reg = ((f_ref + f_bus) / (2 * f_bus) - 1) */
|
|
|
|
const uint8_t baud = ((sam0_gclk_freq(spi_config[bus].gclk_src) + clk) / (2 * clk) - 1);
|
2016-11-08 18:26:58 +01:00
|
|
|
|
|
|
|
/* configure device to be master and set mode and pads,
|
|
|
|
*
|
|
|
|
* NOTE: we could configure the pads already during spi_init, but for
|
|
|
|
* efficiency reason we do that here, so we can do all in one single write
|
|
|
|
* to the CTRLA register */
|
2019-10-08 13:58:54 +02:00
|
|
|
const uint32_t ctrla = SERCOM_SPI_CTRLA_MODE(0x3) /* 0x3 -> master */
|
2020-11-01 22:03:08 +01:00
|
|
|
| SERCOM_SPI_CTRLA_DOPO(spi_config[bus].mosi_pad)
|
|
|
|
| SERCOM_SPI_CTRLA_DIPO(spi_config[bus].miso_pad)
|
|
|
|
| (mode << SERCOM_SPI_CTRLA_CPHA_Pos);
|
2019-10-08 13:58:54 +02:00
|
|
|
|
|
|
|
/* get exclusive access to the device */
|
|
|
|
mutex_lock(&locks[bus]);
|
|
|
|
|
|
|
|
/* power on the device */
|
|
|
|
poweron(bus);
|
|
|
|
|
2019-11-22 14:22:36 +01:00
|
|
|
/* first configuration or reconfiguration after altered device usage */
|
|
|
|
if (dev(bus)->BAUD.reg != baud || dev(bus)->CTRLA.reg != ctrla) {
|
|
|
|
/* disable the device */
|
2020-06-17 17:59:48 +02:00
|
|
|
_disable(dev(bus));
|
2019-11-22 14:22:36 +01:00
|
|
|
|
|
|
|
dev(bus)->BAUD.reg = baud;
|
|
|
|
dev(bus)->CTRLA.reg = ctrla;
|
|
|
|
/* no synchronization needed here, the enable synchronization below
|
|
|
|
* acts as a write-synchronization for both registers */
|
2019-10-08 13:58:54 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 18:26:58 +01:00
|
|
|
/* finally enable the device */
|
2020-06-17 17:59:48 +02:00
|
|
|
_enable(dev(bus));
|
2016-11-08 18:26:58 +01:00
|
|
|
|
2020-05-20 17:16:17 +02:00
|
|
|
/* mux clk_pin to SPI peripheral */
|
|
|
|
gpio_init_mux(spi_config[bus].clk_pin, spi_config[bus].clk_mux);
|
|
|
|
|
2016-11-08 18:26:58 +01:00
|
|
|
return SPI_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void spi_release(spi_t bus)
|
|
|
|
{
|
2020-05-20 17:16:17 +02:00
|
|
|
/* Demux clk_pin back to GPIO_OUT function. Otherwise it will get HIGH-Z
|
|
|
|
* and lead to unexpected current draw by SPI salves. */
|
|
|
|
gpio_disable_mux(spi_config[bus].clk_pin);
|
|
|
|
|
2019-11-22 14:22:36 +01:00
|
|
|
/* disable the device */
|
2020-06-17 17:59:48 +02:00
|
|
|
_disable(dev(bus));
|
2019-11-22 14:22:36 +01:00
|
|
|
|
|
|
|
/* power off the device */
|
|
|
|
poweroff(bus);
|
|
|
|
|
2016-11-08 18:26:58 +01:00
|
|
|
/* release access to the device */
|
|
|
|
mutex_unlock(&locks[bus]);
|
|
|
|
}
|
|
|
|
|
2020-06-11 11:07:06 +02:00
|
|
|
static void _blocking_transfer(spi_t bus, const void *out, void *in, size_t len)
|
2016-11-08 18:26:58 +01:00
|
|
|
{
|
2017-06-29 13:16:58 +02:00
|
|
|
const uint8_t *out_buf = out;
|
|
|
|
uint8_t *in_buf = in;
|
2016-11-08 18:26:58 +01:00
|
|
|
|
2020-11-01 22:03:08 +01:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
2016-11-08 18:26:58 +01:00
|
|
|
uint8_t tmp = (out_buf) ? out_buf[i] : 0;
|
2020-11-01 22:03:08 +01:00
|
|
|
|
|
|
|
/* transmit byte on MOSI */
|
2016-11-08 18:26:58 +01:00
|
|
|
dev(bus)->DATA.reg = tmp;
|
2020-11-01 22:03:08 +01:00
|
|
|
|
|
|
|
/* wait until byte has been sampled on MISO */
|
|
|
|
while (dev(bus)->INTFLAG.bit.RXC == 0) {}
|
|
|
|
|
|
|
|
/* consume the byte */
|
|
|
|
tmp = dev(bus)->DATA.reg;
|
|
|
|
|
2016-11-08 18:26:58 +01:00
|
|
|
if (in_buf) {
|
|
|
|
in_buf[i] = tmp;
|
|
|
|
}
|
|
|
|
}
|
2020-06-11 11:07:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MODULE_PERIPH_DMA
|
|
|
|
|
|
|
|
static void _dma_execute(spi_t bus)
|
|
|
|
{
|
2020-08-21 12:06:42 +02:00
|
|
|
#if defined(CPU_COMMON_SAMD21)
|
2020-06-11 11:07:06 +02:00
|
|
|
pm_block(SAMD21_PM_IDLE_1);
|
|
|
|
#endif
|
|
|
|
dma_start(_dma_state[bus].rx_dma);
|
|
|
|
dma_start(_dma_state[bus].tx_dma);
|
|
|
|
|
|
|
|
dma_wait(_dma_state[bus].rx_dma);
|
2020-08-21 12:06:42 +02:00
|
|
|
#if defined(CPU_COMMON_SAMD21)
|
2020-06-11 11:07:06 +02:00
|
|
|
pm_unblock(SAMD21_PM_IDLE_1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _dma_transfer(spi_t bus, const uint8_t *out, uint8_t *in,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
uint8_t tmp = 0;
|
|
|
|
const uint8_t *out_addr = out ? out + len : &tmp;
|
|
|
|
uint8_t *in_addr = in ? in + len : &tmp;
|
|
|
|
dma_prepare_dst(_dma_state[bus].rx_dma, in_addr, len, in ? true : false);
|
|
|
|
dma_prepare_src(_dma_state[bus].tx_dma, out_addr, len, out ? true : false);
|
|
|
|
_dma_execute(bus);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _dma_transfer_regs(spi_t bus, uint8_t reg, const uint8_t *out,
|
|
|
|
uint8_t *in, size_t len)
|
|
|
|
{
|
|
|
|
uint8_t tmp;
|
|
|
|
const uint8_t *out_addr = out ? out + len : &tmp;
|
|
|
|
uint8_t *in_addr = in ? in + len : &tmp;
|
|
|
|
|
|
|
|
dma_prepare_dst(_dma_state[bus].rx_dma, &tmp, 1, false);
|
|
|
|
dma_prepare_src(_dma_state[bus].tx_dma, ®, 1, false);
|
|
|
|
|
|
|
|
dma_append_dst(_dma_state[bus].rx_dma, &rx_desc[bus], in_addr,
|
|
|
|
len, in ? true : false);
|
|
|
|
dma_append_src(_dma_state[bus].tx_dma, &tx_desc[bus], out_addr,
|
|
|
|
len, out ? true : false);
|
|
|
|
|
|
|
|
_dma_execute(bus);
|
|
|
|
}
|
2020-11-01 22:03:08 +01:00
|
|
|
|
2020-06-11 11:07:06 +02:00
|
|
|
void spi_transfer_regs(spi_t bus, spi_cs_t cs,
|
|
|
|
uint8_t reg, const void *out, void *in, size_t len)
|
|
|
|
{
|
|
|
|
if (cs != SPI_CS_UNDEF) {
|
|
|
|
gpio_clear((gpio_t)cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_use_dma(bus)) {
|
|
|
|
/* The DMA promises not to modify the const out data */
|
|
|
|
_dma_transfer_regs(bus, reg, out, in, len);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_blocking_transfer(bus, ®, NULL, 1);
|
|
|
|
_blocking_transfer(bus, out, in, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cs != SPI_CS_UNDEF) {
|
|
|
|
gpio_set((gpio_t)cs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t spi_transfer_reg(spi_t bus, spi_cs_t cs, uint8_t reg, uint8_t out)
|
|
|
|
{
|
|
|
|
uint8_t res;
|
|
|
|
spi_transfer_regs(bus, cs, reg, &out, &res, 1);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MODULE_PERIPH_DMA */
|
|
|
|
|
|
|
|
|
|
|
|
void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont,
|
|
|
|
const void *out, void *in, size_t len)
|
|
|
|
{
|
|
|
|
assert(out || in);
|
|
|
|
|
|
|
|
if (cs != SPI_CS_UNDEF) {
|
|
|
|
gpio_clear((gpio_t)cs);
|
|
|
|
}
|
2020-11-01 22:03:08 +01:00
|
|
|
|
2020-06-11 11:07:06 +02:00
|
|
|
if (_use_dma(bus)) {
|
|
|
|
#ifdef MODULE_PERIPH_DMA
|
|
|
|
/* The DMA promises not to modify the const out data */
|
|
|
|
_dma_transfer(bus, out, in, len);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_blocking_transfer(bus, out, in, len);
|
|
|
|
}
|
2016-11-08 18:26:58 +01:00
|
|
|
|
|
|
|
if ((!cont) && (cs != SPI_CS_UNDEF)) {
|
|
|
|
gpio_set((gpio_t)cs);
|
|
|
|
}
|
|
|
|
}
|