Skip to content
Snippets Groups Projects
Unverified Commit 0288997c authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

lora32u4ii: add spi driver

parent b35dfb3f
No related branches found
No related tags found
No related merge requests found
#pragma once
class SPI {
private:
SPI(const SPI &copy);
public:
SPI () {}
signed char setup();
signed char xmit(
unsigned char tx_len, unsigned char *tx_buf,
unsigned char rx_len, unsigned char *rx_buf);
};
extern SPI spi;
......@@ -14,6 +14,10 @@ config arch_lora32u4ii_driver_adc
bool "ADC (Analog-Digital-Converter)"
select meta_driver_adc
config arch_lora32u4ii_driver_spi
bool "SPI"
select meta_driver_spi
config arch_lora32u4ii_driver_uptime
bool "Uptime Counter"
select meta_driver_uptime
......@@ -48,12 +48,20 @@ ifneq ($(findstring adc,${arch_drivers}), )
CONFIG_arch_lora32u4ii_driver_adc = y
endif
ifneq ($(findstring spi,${arch_drivers}), )
CONFIG_arch_lora32u4ii_driver_spi = y
endif
# Kconfig driver selection
ifdef CONFIG_arch_lora32u4ii_driver_adc
CXX_TARGETS += src/arch/lora32u4ii/driver/adc.cc
endif
ifdef CONFIG_arch_lora32u4ii_driver_spi
CXX_TARGETS += src/arch/lora32u4ii/driver/spi.cc
endif
ifdef CONFIG_arch_lora32u4ii_driver_uptime
COMMON_FLAGS += -DTIMER_S
CXX_TARGETS += src/arch/lora32u4ii/driver/uptime.cc
......
#include "driver/spi.h"
#include "driver/gpio.h"
#include "arch.h"
#include <avr/io.h>
signed char SPI::setup()
{
// configure SS as output to avoid unintened switches to slave mode
gpio.output(GPIO::pb0, 0);
// MISO is automatically configured as input
// Configure SCK and MOSI as output
DDRB |= _BV(PB2) | _BV(PB1);
// up to 2 MHz SPI clock
SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPR0);
}
signed char SPI::xmit(
unsigned char tx_len, unsigned char *tx_buf,
unsigned char rx_len, unsigned char *rx_buf)
{
unsigned char i = 0;
while ((i < tx_len) || (i < rx_len)) {
if (i < tx_len) {
SPDR = tx_buf[i];
} else {
SPDR = 0;
}
arch.delay_ms(1);
while (!(SPSR & _BV(SPIF))) ;
if (i < rx_len) {
rx_buf[i] = SPDR;
}
i++;
}
return 0;
}
SPI spi;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment