From 22487d95e9b9b0ada171c72df5f5fbb3f3985724 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Fri, 4 May 2018 12:27:52 -0400 Subject: spicmds: Rework SPI message transmission Improve the SPI message transmit system. Add support for bus speed and bus mode. Add support for sending SPI messages on shutdown. Signed-off-by: Petri Honkala Signed-off-by: Douglas Hammond Signed-off-by: Kevin O'Connor --- src/avr/gpio.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++------- src/avr/gpio.h | 8 +++-- 2 files changed, 91 insertions(+), 13 deletions(-) (limited to 'src/avr') diff --git a/src/avr/gpio.c b/src/avr/gpio.c index d4b784f5..0ae7e52f 100644 --- a/src/avr/gpio.c +++ b/src/avr/gpio.c @@ -352,29 +352,103 @@ gpio_adc_cancel_sample(struct gpio_adc g) ****************************************************************/ #if CONFIG_MACH_atmega168 || CONFIG_MACH_atmega328 -static const uint8_t SS = GPIO('B', 2), SCK = GPIO('B', 5), MOSI = GPIO('B', 3); +static const uint8_t SS = GPIO('B', 2), SCK = GPIO('B', 5); +static const uint8_t MOSI = GPIO('B', 3), MISO = GPIO('B', 4); #elif CONFIG_MACH_atmega644p || CONFIG_MACH_atmega1284p -static const uint8_t SS = GPIO('B', 4), SCK = GPIO('B', 7), MOSI = GPIO('B', 5); +static const uint8_t SS = GPIO('B', 4), SCK = GPIO('B', 7); +static const uint8_t MOSI = GPIO('B', 5), MISO = GPIO('B', 6); #elif CONFIG_MACH_at90usb1286 || CONFIG_MACH_at90usb646 || CONFIG_MACH_atmega1280 || CONFIG_MACH_atmega2560 -static const uint8_t SS = GPIO('B', 0), SCK = GPIO('B', 1), MOSI = GPIO('B', 2); +static const uint8_t SS = GPIO('B', 0), SCK = GPIO('B', 1); +static const uint8_t MOSI = GPIO('B', 2), MISO = GPIO('B', 3); #endif -void -spi_config(void) +static void +spi_init(void) { gpio_out_setup(SS, 1); gpio_out_setup(SCK, 0); gpio_out_setup(MOSI, 0); + gpio_in_setup(MISO, 0); + SPCR = (1< 3) + shutdown("Invalid spi_setup parameters"); + + // Make sure the SPI interface is enabled + spi_init(); + + // Setup rate + struct spi_config config = {0, 0}; + if (rate >= (CONFIG_CLOCK_FREQ / 2)) { + config.spsr = (1<= (CONFIG_CLOCK_FREQ / 4)) { + config.spcr = 0; + } else if (rate >= (CONFIG_CLOCK_FREQ / 8)) { + config.spcr = 1; + config.spsr = (1<= (CONFIG_CLOCK_FREQ / 16)) { + config.spcr = 1; + } else if (rate >= (CONFIG_CLOCK_FREQ / 32)) { + config.spcr = 2; + config.spsr = (1<= (CONFIG_CLOCK_FREQ / 64)) { + config.spcr = 2; + } else { + config.spcr = 3; + } + + // Setup mode + config.spcr |= (1<