2015-06-19 11:45:06 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
|
2016-11-08 18:25:21 +01:00
|
|
|
* 2016 Freie Universität Berlin
|
2015-06-19 11:45:06 +02:00
|
|
|
*
|
2016-11-08 18:25:21 +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-06-19 11:45:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup cpu_lpc2387
|
2017-06-22 15:43:17 +02:00
|
|
|
* @ingroup drivers_periph_spi
|
2015-06-19 11:45:06 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Low-level SPI driver implementation
|
|
|
|
*
|
2016-11-08 18:25:21 +01:00
|
|
|
* This implementation is very basic and only supports a single SPI device with
|
|
|
|
* limited configuration options.
|
|
|
|
*
|
|
|
|
* @todo This implementation needs a major rework
|
|
|
|
*
|
2015-06-19 11:45:06 +02:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2016-11-08 18:25:21 +01:00
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2015-06-19 11:45:06 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "mutex.h"
|
2016-11-08 18:25:21 +01:00
|
|
|
#include "assert.h"
|
2015-06-19 11:45:06 +02:00
|
|
|
#include "periph/spi.h"
|
2016-11-08 18:25:21 +01:00
|
|
|
|
2015-06-19 11:45:06 +02:00
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
#define SPI_TX_EMPTY (SSP0SR & SSPSR_TFE)
|
|
|
|
#define SPI_BUSY (SSP0SR & SSPSR_BSY)
|
|
|
|
#define SPI_RX_AVAIL (SSP0SR & SSPSR_RNE)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Array holding one pre-initialized mutex for each SPI device
|
|
|
|
*/
|
2016-11-08 18:25:21 +01:00
|
|
|
static mutex_t lock = MUTEX_INIT;
|
|
|
|
|
|
|
|
void spi_init(spi_t bus)
|
2015-06-19 11:45:06 +02:00
|
|
|
{
|
2016-11-08 18:25:21 +01:00
|
|
|
assert(bus == SPI_DEV(0));
|
2015-06-19 11:45:06 +02:00
|
|
|
|
2016-11-08 18:25:21 +01:00
|
|
|
/* configure pins */
|
|
|
|
spi_init_pins(bus);
|
|
|
|
/* power off the bus (default is on) */
|
|
|
|
PCONP &= ~(PCSSP0);
|
|
|
|
}
|
2015-06-19 11:45:06 +02:00
|
|
|
|
2016-11-08 18:25:21 +01:00
|
|
|
void spi_init_pins(spi_t 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) bus;
|
|
|
|
|
2016-11-08 18:25:21 +01:00
|
|
|
PINSEL3 |= (BIT8 | BIT9); /* SCLK */
|
|
|
|
PINSEL3 |= (BIT14 | BIT15); /* MISO */
|
|
|
|
PINSEL3 |= (BIT16 | BIT17); /* MOSI */
|
|
|
|
}
|
2015-06-19 11:45:06 +02:00
|
|
|
|
2016-11-08 18:25:21 +01:00
|
|
|
int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
|
|
|
|
{
|
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) bus;
|
|
|
|
(void) cs;
|
|
|
|
|
2016-11-08 18:25:21 +01:00
|
|
|
uint32_t pclksel;
|
|
|
|
uint32_t cpsr;
|
2015-06-19 11:45:06 +02:00
|
|
|
|
2016-11-08 18:25:21 +01:00
|
|
|
/* only support for mode 0 at the moment */
|
|
|
|
if (mode != SPI_MODE_0) {
|
|
|
|
return SPI_NOMODE;
|
|
|
|
}
|
2015-06-19 11:45:06 +02:00
|
|
|
|
2016-11-08 18:25:21 +01:00
|
|
|
/* lock bus */
|
|
|
|
mutex_lock(&lock);
|
|
|
|
/* power on */
|
|
|
|
PCONP |= (PCSSP0);
|
2018-05-19 18:12:25 +02:00
|
|
|
/* interface setup */
|
|
|
|
SSP0CR0 = 7;
|
2015-06-19 11:45:06 +02:00
|
|
|
|
2016-11-08 18:25:21 +01:00
|
|
|
/* configure bus clock */
|
|
|
|
lpc2387_pclk_scale(CLOCK_CORECLOCK / 1000, (uint32_t)clk, &pclksel, &cpsr);
|
2015-06-19 11:45:06 +02:00
|
|
|
PCLKSEL1 &= ~(BIT10 | BIT11); /* CCLK to PCLK divider*/
|
|
|
|
PCLKSEL1 |= pclksel << 10;
|
|
|
|
SSP0CPSR = cpsr;
|
|
|
|
|
2016-11-08 18:25:21 +01:00
|
|
|
/* enable the bus */
|
|
|
|
SSP0CR1 |= BIT1;
|
2015-06-19 11:45:06 +02:00
|
|
|
|
2016-11-08 18:25:21 +01:00
|
|
|
/* clear RxFIFO */
|
|
|
|
int dummy;
|
|
|
|
while (SPI_RX_AVAIL) { /* while RNE (Receive FIFO Not Empty)...*/
|
|
|
|
dummy = SSP0DR; /* read data*/
|
2015-06-19 11:45:06 +02:00
|
|
|
}
|
2016-11-08 18:25:21 +01:00
|
|
|
(void) dummy; /* to suppress unused-but-set-variable */
|
2015-06-19 11:45:06 +02:00
|
|
|
|
2016-11-08 18:25:21 +01:00
|
|
|
return SPI_OK;
|
2015-06-19 11:45:06 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 18:25:21 +01:00
|
|
|
void spi_release(spi_t bus)
|
2015-06-19 11:45:06 +02:00
|
|
|
{
|
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) bus;
|
2016-11-08 18:25:21 +01:00
|
|
|
/* disable, power off, and release the bus */
|
|
|
|
SSP0CR1 &= ~(BIT1);
|
|
|
|
PCONP &= ~(PCSSP0);
|
|
|
|
mutex_unlock(&lock);
|
2015-06-19 11:45:06 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 18:25:21 +01:00
|
|
|
void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont,
|
|
|
|
const void *out, void *in, size_t len)
|
2015-06-19 11:45:06 +02:00
|
|
|
{
|
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) bus;
|
|
|
|
|
2017-06-29 13:16:58 +02:00
|
|
|
const uint8_t *out_buf = out;
|
|
|
|
uint8_t *in_buf = in;
|
2015-06-19 11:45:06 +02:00
|
|
|
|
2016-11-08 18:25:21 +01:00
|
|
|
assert(out_buf || in_buf);
|
2015-06-19 11:45:06 +02:00
|
|
|
|
2016-11-08 18:25:21 +01:00
|
|
|
if (cs != SPI_CS_UNDEF) {
|
|
|
|
gpio_clear((gpio_t)cs);
|
2015-06-19 11:45:06 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 18:25:21 +01:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
uint8_t tmp = (out_buf) ? out_buf[i] : 0;
|
|
|
|
while (!SPI_TX_EMPTY) {}
|
|
|
|
SSP0DR = tmp;
|
|
|
|
while (SPI_BUSY) {}
|
|
|
|
while (!SPI_RX_AVAIL) {}
|
|
|
|
tmp = (uint8_t)SSP0DR;
|
|
|
|
if (in_buf) {
|
|
|
|
in_buf[i] = tmp;
|
|
|
|
}
|
2015-06-19 11:45:06 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 18:25:21 +01:00
|
|
|
if ((!cont) && cs != SPI_CS_UNDEF) {
|
|
|
|
gpio_set((gpio_t)cs);
|
2015-09-11 11:13:57 +02:00
|
|
|
}
|
|
|
|
}
|