2016-06-06 17:54:42 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Daniel Amkaer Sorensen
|
|
|
|
* 2016 Freie Universität Berlin
|
2017-06-21 16:10:24 +02:00
|
|
|
* 2017 Hamburg University of Applied Sciences
|
2017-07-03 19:23:20 +02:00
|
|
|
* 2017 Thomas Perrot <thomas.perrot@tupi.fr>
|
2016-06-06 17:54:42 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-06-22 15:43:17 +02:00
|
|
|
* @ingroup cpu_atmega_common
|
|
|
|
* @ingroup drivers_periph_spi
|
2016-06-06 17:54:42 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
2016-07-01 21:52:08 +02:00
|
|
|
* @brief Low-level SPI driver implementation for ATmega family
|
2016-06-06 17:54:42 +02:00
|
|
|
*
|
|
|
|
* @author Daniel Amkaer Sorensen <daniel.amkaer@gmail.com>
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2017-06-21 16:10:24 +02:00
|
|
|
* @author Dimitri Nahm <dimitri.nahm@haw-hamburg.de>
|
2017-07-03 19:23:20 +02:00
|
|
|
* @author Thomas Perrot <thomas.perrot@tupi.fr>
|
2016-06-06 17:54:42 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
2021-02-01 14:05:16 +01:00
|
|
|
#include <assert.h>
|
2016-06-06 17:54:42 +02:00
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "mutex.h"
|
|
|
|
#include "periph/spi.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Extract BR0, BR1 and SPI2X bits from speed value
|
|
|
|
* @{
|
|
|
|
*/
|
2016-11-04 18:32:28 +01:00
|
|
|
#define CLK_MASK (0x3)
|
2016-06-06 17:54:42 +02:00
|
|
|
#define S2X_SHIFT (2)
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
static mutex_t lock = MUTEX_INIT;
|
|
|
|
|
2016-11-04 18:32:28 +01:00
|
|
|
void spi_init(spi_t bus)
|
|
|
|
{
|
|
|
|
assert(bus == 0);
|
|
|
|
/* power off the SPI peripheral */
|
2017-09-14 10:35:06 +02:00
|
|
|
power_spi_disable();
|
2016-11-04 18:32:28 +01:00
|
|
|
/* trigger the pin configuration */
|
2017-01-31 19:38:41 +01:00
|
|
|
spi_init_pins(bus);
|
2016-11-04 18:32:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void spi_init_pins(spi_t bus)
|
2016-06-06 17:54:42 +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-21 16:10:24 +02:00
|
|
|
/* set SPI pins as output */
|
|
|
|
#if defined (CPU_ATMEGA2560) || defined (CPU_ATMEGA1281)
|
2016-06-06 17:54:42 +02:00
|
|
|
DDRB |= ((1 << DDB2) | (1 << DDB1) | (1 << DDB0));
|
2017-06-21 16:10:24 +02:00
|
|
|
#endif
|
2018-02-27 14:00:43 +01:00
|
|
|
#if defined (CPU_ATMEGA328P)
|
2017-06-21 16:10:24 +02:00
|
|
|
DDRB |= ((1 << DDB2) | (1 << DDB3) | (1 << DDB5));
|
|
|
|
#endif
|
2018-03-17 21:23:14 +01:00
|
|
|
#if defined (CPU_ATMEGA1284P)
|
|
|
|
DDRB |= ((1 << DDB4) | (1 << DDB5) | (1 << DDB7));
|
|
|
|
#endif
|
2019-09-28 00:02:16 +02:00
|
|
|
#if defined(CPU_ATMEGA128RFA1) || defined(CPU_ATMEGA256RFR2)
|
2018-02-27 14:00:43 +01:00
|
|
|
/* Master: PB3 MISO set to out
|
|
|
|
* PB2 MOSI set to input by hardware
|
|
|
|
* PB1 SCK set to out
|
|
|
|
* PB0 /CS kept as is, has to be configured by user. Flexibility to
|
|
|
|
* use different /CS pin.
|
|
|
|
* Only Master supported. Slave: Only MOSI has to be set as Input.
|
|
|
|
* ATmega256RFR2 data sheet p. 365
|
|
|
|
* */
|
|
|
|
DDRB |= ((1 << DDB2) | (1 << DDB1));
|
|
|
|
#endif
|
2017-07-03 19:23:20 +02:00
|
|
|
#ifdef CPU_ATMEGA32U4
|
|
|
|
DDRB |= ((1 << DDB0) | (1 << DDB1) | (1 << DDB2));
|
|
|
|
#endif
|
2016-11-04 18:32:28 +01: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)
|
2016-11-04 18:32:28 +01: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-04 18:32:28 +01:00
|
|
|
(void)cs;
|
2021-02-01 14:05:16 +01:00
|
|
|
assert(bus == SPI_DEV(0));
|
2016-06-06 17:54:42 +02:00
|
|
|
|
2016-11-04 18:32:28 +01:00
|
|
|
/* lock the bus and power on the SPI peripheral */
|
|
|
|
mutex_lock(&lock);
|
2017-09-14 10:35:06 +02:00
|
|
|
power_spi_enable();
|
2016-06-06 17:54:42 +02:00
|
|
|
|
|
|
|
/* configure as master, with given mode and clock */
|
2016-11-04 18:32:28 +01:00
|
|
|
SPSR = (clk >> S2X_SHIFT);
|
|
|
|
SPCR = ((1 << SPE) | (1 << MSTR) | mode | (clk & CLK_MASK));
|
2016-06-06 17:54:42 +02:00
|
|
|
|
2016-11-04 18:32:28 +01:00
|
|
|
/* clear interrupt flag by reading SPSR and data register by reading SPDR */
|
2016-06-06 17:54:42 +02:00
|
|
|
(void)SPSR;
|
|
|
|
(void)SPDR;
|
|
|
|
}
|
|
|
|
|
2016-11-04 18:32:28 +01:00
|
|
|
void spi_release(spi_t bus)
|
2016-06-06 17:54:42 +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-04 18:32:28 +01:00
|
|
|
/* power off and release the bus */
|
|
|
|
SPCR &= ~(1 << SPE);
|
2017-09-14 10:35:06 +02:00
|
|
|
power_spi_disable();
|
2016-06-06 17:54:42 +02:00
|
|
|
mutex_unlock(&lock);
|
|
|
|
}
|
|
|
|
|
2016-11-04 18:32:28 +01:00
|
|
|
void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont,
|
|
|
|
const void *out, void *in, size_t len)
|
2016-06-06 17:54:42 +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-06-06 17:54:42 +02:00
|
|
|
|
2016-11-04 18:32:28 +01:00
|
|
|
assert(out_buf || in_buf);
|
2016-06-06 17:54:42 +02:00
|
|
|
|
2016-11-04 18:32:28 +01:00
|
|
|
if (cs != SPI_CS_UNDEF) {
|
|
|
|
gpio_clear((gpio_t)cs);
|
|
|
|
}
|
2016-06-06 17:54:42 +02:00
|
|
|
|
2016-11-04 18:32:28 +01:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
uint8_t tmp = (out_buf) ? out_buf[i] : 0;
|
2016-06-06 17:54:42 +02:00
|
|
|
SPDR = tmp;
|
|
|
|
while (!(SPSR & (1 << SPIF))) {}
|
|
|
|
tmp = SPDR;
|
2016-11-04 18:32:28 +01:00
|
|
|
if (in_buf) {
|
|
|
|
in_buf[i] = tmp;
|
2016-06-06 17:54:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 18:32:28 +01:00
|
|
|
if ((!cont) && (cs != SPI_CS_UNDEF)) {
|
|
|
|
gpio_set((gpio_t)cs);
|
|
|
|
}
|
2016-06-06 17:54:42 +02:00
|
|
|
}
|