2015-09-02 12:43:21 +02:00
|
|
|
/*
|
2016-11-08 18:20:38 +01:00
|
|
|
* Copyright (C) 2015-2016 Freie Universität Berlin
|
2015-09-02 12:43:21 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2023-06-13 15:56:24 +02:00
|
|
|
* @ingroup cpu_msp430_f2xx_g2xx
|
2017-06-22 15:43:17 +02:00
|
|
|
* @ingroup drivers_periph_spi
|
2015-09-02 12:43:21 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Low-level SPI driver implementation
|
|
|
|
*
|
|
|
|
* This SPI driver implementation does only support one single SPI device for
|
|
|
|
* now. This is sufficient, as most MSP430 CPU's only support two serial
|
|
|
|
* devices - one used as UART and one as SPI.
|
|
|
|
*
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2021-02-01 14:05:16 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
2015-09-02 12:43:21 +02:00
|
|
|
#include "cpu.h"
|
|
|
|
#include "mutex.h"
|
|
|
|
#include "periph/spi.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Mutex for locking the SPI device
|
|
|
|
*/
|
|
|
|
static mutex_t spi_lock = MUTEX_INIT;
|
|
|
|
|
2016-11-08 18:20:38 +01:00
|
|
|
void spi_init(spi_t bus)
|
2015-09-02 12:43:21 +02:00
|
|
|
{
|
2021-02-01 14:05:16 +01:00
|
|
|
assert((unsigned)bus < SPI_NUMOF);
|
2016-03-24 09:40:28 +01:00
|
|
|
|
2016-11-08 18:20:38 +01:00
|
|
|
/* reset SPI device */
|
2023-06-14 16:18:15 +02:00
|
|
|
SPI_BASE->CTL1 = UCSWRST;
|
|
|
|
SPI_BASE->CTL1 |= UCSSEL_SMCLK;
|
2016-11-08 18:20:38 +01:00
|
|
|
|
|
|
|
/* trigger the pin configuration */
|
|
|
|
spi_init_pins(bus);
|
2015-09-02 12:43:21 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 18:20:38 +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:20:38 +01:00
|
|
|
gpio_periph_mode(SPI_PIN_MISO, true);
|
|
|
|
gpio_periph_mode(SPI_PIN_MOSI, true);
|
|
|
|
gpio_periph_mode(SPI_PIN_CLK, true);
|
|
|
|
}
|
2015-09-02 12:43:21 +02:00
|
|
|
|
2021-02-01 14:05:16 +01:00
|
|
|
void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
|
2015-09-02 12:43:21 +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;
|
|
|
|
(void)cs;
|
2021-02-01 14:05:16 +01:00
|
|
|
assert((unsigned)bus < SPI_NUMOF);
|
|
|
|
assert(clk != SPI_CLK_10MHZ);
|
2015-09-02 12:43:21 +02:00
|
|
|
|
2016-11-08 18:20:38 +01:00
|
|
|
/* lock the bus */
|
|
|
|
mutex_lock(&spi_lock);
|
2016-03-24 09:40:28 +01:00
|
|
|
|
2016-11-08 18:20:38 +01:00
|
|
|
/* calculate baudrate */
|
2023-06-13 15:56:24 +02:00
|
|
|
uint32_t br = msp430_submain_clock_freq() / clk;
|
2016-03-24 09:40:28 +01:00
|
|
|
/* make sure the is not smaller then 2 */
|
|
|
|
if (br < 2) {
|
|
|
|
br = 2;
|
|
|
|
}
|
2016-11-08 18:20:38 +01:00
|
|
|
SPI_BASE->BR0 = (uint8_t)br;
|
|
|
|
SPI_BASE->BR1 = (uint8_t)(br >> 8);
|
2016-03-24 09:40:28 +01:00
|
|
|
|
2016-11-08 18:20:38 +01:00
|
|
|
/* configure bus mode */
|
|
|
|
/* configure mode */
|
2023-06-14 16:18:15 +02:00
|
|
|
SPI_BASE->CTL0 = (UCSYNC | UCMST | UCMODE_0 | UCMSB | mode);
|
2016-11-08 18:20:38 +01:00
|
|
|
/* release from software reset */
|
2023-06-14 16:18:15 +02:00
|
|
|
SPI_BASE->CTL1 &= ~(UCSWRST);
|
2015-09-02 12:43:21 +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 spi_release(spi_t bus)
|
2015-09-02 12:43:21 +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:20:38 +01:00
|
|
|
/* put SPI device back in reset state */
|
2023-06-14 16:18:15 +02:00
|
|
|
SPI_BASE->CTL1 |= UCSWRST;
|
2015-09-02 12:43:21 +02:00
|
|
|
|
2016-11-08 18:20:38 +01:00
|
|
|
/* release the bus */
|
2015-09-02 12:43:21 +02:00
|
|
|
mutex_unlock(&spi_lock);
|
|
|
|
}
|
|
|
|
|
2016-11-08 18:20:38 +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-02 12:43:21 +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;
|
2016-11-08 18:20:38 +01:00
|
|
|
|
|
|
|
assert(out_buf || in_buf);
|
2016-09-28 20:05:06 +02:00
|
|
|
|
2016-11-08 18:20:38 +01:00
|
|
|
if (cs != SPI_CS_UNDEF) {
|
|
|
|
gpio_clear((gpio_t)cs);
|
|
|
|
}
|
2016-09-28 20:05:06 +02:00
|
|
|
|
|
|
|
/* if we only send out data, we do this the fast way... */
|
2016-11-08 18:20:38 +01:00
|
|
|
if (!in_buf) {
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
2016-09-28 20:05:06 +02:00
|
|
|
while (!(SPI_IF & SPI_IE_TX_BIT)) {}
|
2017-06-29 13:16:58 +02:00
|
|
|
SPI_BASE->TXBUF = out_buf[i];
|
2016-09-28 20:05:06 +02:00
|
|
|
}
|
|
|
|
/* finally we need to wait, until all transfers are complete */
|
2023-06-14 16:18:15 +02:00
|
|
|
while (SPI_BASE->STAT & UCBUSY) {}
|
2016-11-08 18:20:38 +01:00
|
|
|
SPI_BASE->RXBUF;
|
2015-09-02 12:43:21 +02:00
|
|
|
}
|
2016-11-08 18:20:38 +01:00
|
|
|
else if (!out_buf) {
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
SPI_BASE->TXBUF = 0;
|
2016-09-28 20:05:06 +02:00
|
|
|
while (!(SPI_IF & SPI_IE_RX_BIT)) {}
|
2016-11-08 18:20:38 +01:00
|
|
|
in_buf[i] = (char)SPI_BASE->RXBUF;
|
2016-09-28 20:05:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2016-11-08 18:20:38 +01:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
2016-09-28 20:05:06 +02:00
|
|
|
while (!(SPI_IF & SPI_IE_TX_BIT)) {}
|
2016-11-08 18:20:38 +01:00
|
|
|
SPI_BASE->TXBUF = out_buf[i];
|
2016-09-28 20:05:06 +02:00
|
|
|
while (!(SPI_IF & SPI_IE_RX_BIT)) {}
|
2016-11-08 18:20:38 +01:00
|
|
|
in_buf[i] = (char)SPI_BASE->RXBUF;
|
2016-09-28 20:05:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 18:20:38 +01:00
|
|
|
if ((!cont) && (cs != SPI_CS_UNDEF)) {
|
|
|
|
gpio_set((gpio_t)cs);
|
|
|
|
}
|
2015-09-02 12:43:21 +02:00
|
|
|
}
|