1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/cpu/lpc2387/periph/spi.c

192 lines
4.1 KiB
C
Raw Normal View History

2015-06-19 11:45:06 +02:00
/*
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
* 2016 Freie Universität Berlin
2015-06-19 11:45:06 +02: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
* @ingroup drivers_periph_spi
2015-06-19 11:45:06 +02:00
* @{
*
* @file
* @brief Low-level SPI driver implementation
*
* 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>
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
2015-06-19 11:45:06 +02:00
*
* @}
*/
#include "cpu.h"
#include "mutex.h"
#include "assert.h"
2015-06-19 11:45:06 +02:00
#include "periph/spi.h"
2015-06-19 11:45:06 +02:00
#define ENABLE_DEBUG (0)
#include "debug.h"
/**
* @brief Get the pointer to the base register of the given SPI device
*
* @param[in] dev SPI device identifier
*
* @return base register address
*/
static inline lpc23xx_spi_t *get_dev(spi_t dev)
{
return spi_config[dev].dev;
}
2015-06-19 11:45:06 +02:00
/**
* @brief Array holding one pre-initialized mutex for each SPI device
*/
static mutex_t lock[SPI_NUMOF];
static void _power_off(spi_t bus)
{
switch ((uint32_t) get_dev(bus)) {
case SSP0_BASE_ADDR:
PCONP &= ~PCSSP0;
break;
case SSP1_BASE_ADDR:
PCONP &= ~PCSSP1;
break;
}
}
static void _power_on(spi_t bus)
{
switch ((uint32_t) get_dev(bus)) {
case SSP0_BASE_ADDR:
PCONP |= PCSSP0;
break;
case SSP1_BASE_ADDR:
PCONP |= PCSSP1;
break;
}
}
void spi_init(spi_t bus)
2015-06-19 11:45:06 +02:00
{
assert(bus < SPI_NUMOF);
2015-06-19 11:45:06 +02:00
/* configure pins */
spi_init_pins(bus);
/* power off the bus (default is on) */
_power_off(bus);
}
2015-06-19 11:45:06 +02:00
void spi_init_pins(spi_t bus)
{
const spi_conf_t *cfg = &spi_config[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
*(&PINSEL0 + cfg->pinsel_mosi) |= cfg->pinsel_msk_mosi;
*(&PINSEL0 + cfg->pinsel_miso) |= cfg->pinsel_msk_miso;
*(&PINSEL0 + cfg->pinsel_clk) |= cfg->pinsel_msk_clk;
}
2015-06-19 11:45:06 +02: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) cs;
uint32_t pclksel;
uint32_t cpsr;
2015-06-19 11:45:06 +02:00
lpc23xx_spi_t *dev = get_dev(bus);
/* only support for mode 0 at the moment */
if (mode != SPI_MODE_0) {
return SPI_NOMODE;
}
2015-06-19 11:45:06 +02:00
/* lock bus */
mutex_lock(&lock[bus]);
/* power on */
_power_on(bus);
/* interface setup */
dev->CR0 = 7;
2015-06-19 11:45:06 +02:00
/* configure bus clock */
lpc2387_pclk_scale(CLOCK_CORECLOCK / 1000, (uint32_t)clk, &pclksel, &cpsr);
switch ((uint32_t)dev) {
case SSP0_BASE_ADDR:
PCLKSEL1 &= ~(BIT10 | BIT11); /* CCLK to PCLK divider*/
PCLKSEL1 |= pclksel << 10;
break;
case SSP1_BASE_ADDR:
PCLKSEL0 &= ~(BIT20 | BIT21); /* CCLK to PCLK divider*/
PCLKSEL0 |= pclksel << 20;
break;
}
dev->CPSR = cpsr;
2015-06-19 11:45:06 +02:00
/* enable the bus */
dev->CR1 |= BIT1;
2015-06-19 11:45:06 +02:00
/* clear RxFIFO */
while (dev->SR & SSPSR_RNE) { /* while RNE (Receive FIFO Not Empty)...*/
dev->DR; /* read data*/
2015-06-19 11:45:06 +02:00
}
return SPI_OK;
2015-06-19 11:45:06 +02:00
}
void spi_release(spi_t bus)
2015-06-19 11:45:06 +02:00
{
lpc23xx_spi_t *dev = get_dev(bus);
/* disable, power off, and release the bus */
dev->CR1 &= ~BIT1;
_power_off(bus);
mutex_unlock(&lock[bus]);
2015-06-19 11:45:06 +02: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
{
const uint8_t *out_buf = out;
uint8_t *in_buf = in;
2015-06-19 11:45:06 +02:00
lpc23xx_spi_t *dev = get_dev(bus);
assert(out_buf || in_buf);
2015-06-19 11:45:06 +02:00
if (cs != SPI_CS_UNDEF) {
gpio_clear((gpio_t)cs);
2015-06-19 11:45:06 +02:00
}
for (size_t i = 0; i < len; i++) {
uint8_t tmp = (out_buf) ? out_buf[i] : 0;
/* wait for TX buffer empty */
while (!(dev->SR & SSPSR_TFE)) {}
dev->DR = tmp;
while (dev->SR & SSPSR_BSY) {}
/* wait for RX not empty */
while (!(dev->SR & SSPSR_RNE)) {}
tmp = (uint8_t)dev->DR;
if (in_buf) {
in_buf[i] = tmp;
}
2015-06-19 11:45:06 +02:00
}
if ((!cont) && cs != SPI_CS_UNDEF) {
gpio_set((gpio_t)cs);
2015-09-11 11:13:57 +02:00
}
}