2015-09-17 21:11:42 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Loci Controls Inc.
|
2016-11-08 18:17:17 +01:00
|
|
|
* 2016 Freie Universität Berlin
|
2015-09-17 21:11:42 +02:00
|
|
|
*
|
2016-11-08 18:17:17 +01:00
|
|
|
* 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.
|
2015-09-17 21:11:42 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-06-22 15:43:17 +02:00
|
|
|
* @ingroup cpu_cc2538
|
|
|
|
* @ingroup drivers_periph_spi
|
2015-09-17 21:11:42 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Low-level SPI driver implementation
|
|
|
|
*
|
|
|
|
* @author Ian Martin <ian@locicontrols.com>
|
2016-11-08 18:17:17 +01:00
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2015-09-17 21:11:42 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2018-08-03 16:05:53 +02:00
|
|
|
#include "vendor/hw_memmap.h"
|
|
|
|
#include "vendor/hw_ssi.h"
|
|
|
|
|
2015-09-17 21:11:42 +02:00
|
|
|
#include "cpu.h"
|
|
|
|
#include "mutex.h"
|
2016-11-08 18:17:17 +01:00
|
|
|
#include "assert.h"
|
2015-09-17 21:11:42 +02:00
|
|
|
#include "periph/spi.h"
|
|
|
|
|
2020-10-22 11:34:00 +02:00
|
|
|
#define ENABLE_DEBUG 0
|
2018-08-07 08:29:35 +02:00
|
|
|
#include "debug.h"
|
|
|
|
|
2015-09-17 21:11:42 +02:00
|
|
|
/**
|
2016-11-08 18:17:17 +01:00
|
|
|
* @brief Array holding one pre-initialized mutex for each SPI device
|
2015-09-17 21:11:42 +02:00
|
|
|
*/
|
2016-11-08 18:17:17 +01:00
|
|
|
static mutex_t locks[SPI_NUMOF];
|
2015-09-17 21:11:42 +02:00
|
|
|
|
2016-11-08 18:17:17 +01:00
|
|
|
static inline cc2538_ssi_t *dev(spi_t bus)
|
2015-09-17 21:11:42 +02:00
|
|
|
{
|
2018-08-03 16:05:53 +02:00
|
|
|
/* .num is either 0 or 1, return respective base address */
|
|
|
|
return (spi_config[bus].num) ? (cc2538_ssi_t *)SSI1_BASE : (cc2538_ssi_t *)SSI0_BASE;
|
2016-11-08 18:17:17 +01:00
|
|
|
}
|
2015-09-17 21:11:42 +02:00
|
|
|
|
2016-11-08 18:17:17 +01:00
|
|
|
static inline void poweron(spi_t bus)
|
|
|
|
{
|
2018-08-03 16:05:53 +02:00
|
|
|
SYS_CTRL_RCGCSSI |= (1 << spi_config[bus].num);
|
|
|
|
SYS_CTRL_SCGCSSI |= (1 << spi_config[bus].num);
|
|
|
|
SYS_CTRL_DCGCSSI |= (1 << spi_config[bus].num);
|
2015-09-17 21:11:42 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 18:17:17 +01:00
|
|
|
static inline void poweroff(spi_t bus)
|
2015-09-17 21:11:42 +02:00
|
|
|
{
|
2018-08-03 16:05:53 +02:00
|
|
|
SYS_CTRL_RCGCSSI &= ~(1 << spi_config[bus].num);
|
|
|
|
SYS_CTRL_SCGCSSI &= ~(1 << spi_config[bus].num);
|
|
|
|
SYS_CTRL_DCGCSSI &= ~(1 << spi_config[bus].num);
|
2015-09-17 21:11:42 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 18:17:17 +01:00
|
|
|
void spi_init(spi_t bus)
|
2015-09-17 21:11:42 +02:00
|
|
|
{
|
2018-08-07 08:29:35 +02:00
|
|
|
DEBUG("%s: bus=%u\n", __FUNCTION__, bus);
|
|
|
|
|
2018-08-03 16:05:53 +02:00
|
|
|
assert(bus < SPI_NUMOF);
|
2016-11-08 18:17:17 +01:00
|
|
|
|
2018-08-03 16:05:53 +02:00
|
|
|
/* init mutex for given bus */
|
|
|
|
mutex_init(&locks[bus]);
|
2016-11-08 18:17:17 +01:00
|
|
|
/* temporarily power on the device */
|
|
|
|
poweron(bus);
|
|
|
|
/* configure device to be a master and disable SSI operation mode */
|
|
|
|
dev(bus)->CR1 = 0;
|
|
|
|
/* configure system clock as SSI clock source */
|
2018-08-03 16:05:53 +02:00
|
|
|
dev(bus)->CC = SSI_CC_CS_IODIV;
|
2016-11-08 18:17:17 +01:00
|
|
|
/* and power off the bus again */
|
|
|
|
poweroff(bus);
|
|
|
|
|
|
|
|
/* trigger SPI pin configuration */
|
|
|
|
spi_init_pins(bus);
|
|
|
|
}
|
2015-09-17 21:11:42 +02:00
|
|
|
|
2016-11-08 18:17:17 +01:00
|
|
|
void spi_init_pins(spi_t bus)
|
|
|
|
{
|
2018-08-07 08:29:35 +02:00
|
|
|
DEBUG("%s: bus=%u\n", __FUNCTION__, bus);
|
2018-08-03 16:05:53 +02:00
|
|
|
/* select values according to SPI device */
|
|
|
|
cc2538_ioc_sel_t txd = spi_config[bus].num ? SSI1_TXD : SSI0_TXD;
|
|
|
|
cc2538_ioc_sel_t clk = spi_config[bus].num ? SSI1_CLK_OUT : SSI0_CLK_OUT;
|
|
|
|
cc2538_ioc_sel_t fss = spi_config[bus].num ? SSI1_FSS_OUT : SSI0_FSS_OUT;
|
|
|
|
cc2538_ioc_pin_t rxd = spi_config[bus].num ? SSI1_RXD : SSI0_RXD;
|
|
|
|
/* init pin functions and multiplexing */
|
|
|
|
gpio_init_mux(spi_config[bus].mosi_pin, OVERRIDE_ENABLE, txd, GPIO_MUX_NONE);
|
|
|
|
gpio_init_mux(spi_config[bus].sck_pin, OVERRIDE_ENABLE, clk, GPIO_MUX_NONE);
|
|
|
|
gpio_init_mux(spi_config[bus].cs_pin, OVERRIDE_ENABLE, fss, GPIO_MUX_NONE);
|
|
|
|
gpio_init_mux(spi_config[bus].miso_pin, OVERRIDE_DISABLE, GPIO_MUX_NONE, rxd);
|
2015-09-17 21:11:42 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 18:17:17 +01:00
|
|
|
int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
|
2015-09-17 21:11:42 +02:00
|
|
|
{
|
2018-08-07 08:29:35 +02:00
|
|
|
DEBUG("%s: bus=%u\n", __FUNCTION__, bus);
|
build: fix unused parameter errors
cpu, sam0_common: fix unused parameter in periph/spi
cpu, kinetis_common: fix unused parameter in periph/spi
cpu, cc2538: fix unused param in periph/i2c
cpu, cc2538: fix unused param in periph/spi
cpu, sam3: fix unused param in periph/spi
cpu, stm32_common: fix unused param in periph/pm
cpu, stm32f3: fix unused params in periph/i2c
cpu, nrf5x_common: fix unused param in periph/gpio
cpu, nrf5x_common: fix unused param in periph/spi
cpu, lpc2387: fix unused params in periph/spi
cpu, cc2538: fix unused params in radio/netdev
cpu, cc2650: fix unused params in periph/uart
cpu, lm4f120: fix unused param in periph/spi
cpu, lm4f120: fix unused params in periph/timer
cpu, lm4f120: fix unused params in periph/uart
cpu, stm32_common: fix unused params in periph/dac
cpu, stm32l0: fix unused params in periph/i2c
cpu, msp430fxyz: fix unused params in periph/uart
cpu, mips: fix unused params
cpu, cc430: fix unused-params in periph/timer
cpu, msp430fxyz: fix unused params in periph/spi
drivers, cc2420: fix unused param
cpu, mips32r2_common: fix unused params in periph/timer
cpu, cc2538: fix unused-param in periph/i2c
cpu, mips32r2_common: fix unused-param in periph/timer
cpu, msp430fxyz: fix unused params in periph/timer
cpu, atmega_common: fix unused params in periph/spi
driver, nrfmin: fix unused params
cpu, cc2538_rf: fix unused params
driver, netdev_ieee802514: fix unused param
cpu, mip_pic32m: fix unused params
cpu, lpc2387: fix unused params in periph/pwm
tests/driver_sdcard_spi: fix unused params
cpu, sam3: fix unused param in periph/pwm
tests/driver_dynamixel: fix unused params, and style issues
cpu, cc430: fix unused param in periph/rtc
cpu, atmega_common: fix unused params in periph/i2c
2017-10-31 12:09:11 +01:00
|
|
|
(void) cs;
|
2016-11-08 18:17:17 +01:00
|
|
|
/* lock the bus */
|
|
|
|
mutex_lock(&locks[bus]);
|
|
|
|
/* power on device */
|
|
|
|
poweron(bus);
|
|
|
|
/* configure SCR clock field, data-width and mode */
|
|
|
|
dev(bus)->CR0 = 0;
|
|
|
|
dev(bus)->CPSR = (spi_clk_config[clk].cpsr);
|
|
|
|
dev(bus)->CR0 = ((spi_clk_config[clk].scr << 8) | mode | SSI_CR0_DSS(8));
|
|
|
|
/* enable SSI device */
|
|
|
|
dev(bus)->CR1 = SSI_CR1_SSE;
|
|
|
|
|
|
|
|
return SPI_OK;
|
2015-09-17 21:11:42 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 18:17:17 +01:00
|
|
|
void spi_release(spi_t bus)
|
2015-09-17 21:11:42 +02:00
|
|
|
{
|
2018-08-07 08:29:35 +02:00
|
|
|
DEBUG("%s: bus=%u\n", __FUNCTION__, bus);
|
2016-11-08 18:17:17 +01:00
|
|
|
/* disable and power off device */
|
|
|
|
dev(bus)->CR1 = 0;
|
|
|
|
poweroff(bus);
|
|
|
|
/* and release lock... */
|
|
|
|
mutex_unlock(&locks[bus]);
|
2015-09-17 21:11:42 +02:00
|
|
|
}
|
|
|
|
|
2020-03-09 16:30:43 +01:00
|
|
|
static uint8_t _trx(cc2538_ssi_t *dev, uint8_t in)
|
|
|
|
{
|
|
|
|
while (!(dev->SR & SSI_SR_TNF)) {}
|
|
|
|
dev->DR = in;
|
|
|
|
while (!(dev->SR & SSI_SR_RNE)) {}
|
|
|
|
return dev->DR;
|
|
|
|
}
|
|
|
|
|
2016-11-08 18:17:17 +01:00
|
|
|
void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont,
|
|
|
|
const void *out, void *in, size_t len)
|
2015-09-17 21:11:42 +02:00
|
|
|
{
|
2018-08-07 08:29:35 +02:00
|
|
|
DEBUG("%s: bus=%u, len=%u\n", __FUNCTION__, bus, (unsigned)len);
|
|
|
|
|
2017-06-29 13:16:58 +02:00
|
|
|
const uint8_t *out_buf = out;
|
|
|
|
uint8_t *in_buf = in;
|
2015-09-17 21:11:42 +02:00
|
|
|
|
2016-11-08 18:17:17 +01:00
|
|
|
assert(out_buf || in_buf);
|
2015-09-17 21:11:42 +02:00
|
|
|
|
2016-11-08 18:17:17 +01:00
|
|
|
if (cs != SPI_CS_UNDEF) {
|
|
|
|
gpio_clear((gpio_t)cs);
|
2015-09-17 21:11:42 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 18:17:17 +01:00
|
|
|
if (!in_buf) {
|
2020-03-09 16:30:43 +01:00
|
|
|
for (const void *end = out_buf + len; out_buf != end; ++out_buf) {
|
|
|
|
_trx(dev(bus), *out_buf);
|
2015-09-17 21:11:42 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-30 13:11:11 +02:00
|
|
|
else if (!out_buf) {
|
2020-03-09 16:30:43 +01:00
|
|
|
for (void *end = in_buf + len; in_buf != end; ++in_buf) {
|
|
|
|
*in_buf = _trx(dev(bus), 0);
|
2016-11-08 18:17:17 +01:00
|
|
|
}
|
2015-09-17 21:11:42 +02:00
|
|
|
}
|
2016-11-08 18:17:17 +01:00
|
|
|
else {
|
2020-03-09 16:30:43 +01:00
|
|
|
for (void *end = in_buf + len; in_buf != end; ++in_buf, ++out_buf) {
|
|
|
|
*in_buf = _trx(dev(bus), *out_buf);
|
2016-11-08 18:17:17 +01:00
|
|
|
}
|
2015-09-17 21:11:42 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 18:17:17 +01:00
|
|
|
if ((!cont) && (cs != SPI_CS_UNDEF)) {
|
|
|
|
gpio_set((gpio_t)cs);
|
2015-09-17 21:11:42 +02:00
|
|
|
}
|
|
|
|
}
|