From ec7c93789ad1bc71b9c7f2539f112891bbf1588e Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 9 Jun 2011 14:02:26 -0300 Subject: [PATCH] atusb/fw/: added "lazy enabling" of UART-SPI after ATUSB_GPIO - spi.h (spi_off), spi.c: disable the UART - spi.c (spi_initialized, spi_begin, spi_init): initialize the UART if necessary - board_app.c (gpio): call spi_off to disable the UART instead of open-coding the operation - board_app.c (gpio): don't explicitly re-enable the UART but defer it to the next communication --- atusb/fw/board_app.c | 4 +--- atusb/fw/spi.c | 14 ++++++++++++++ atusb/fw/spi.h | 1 + 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/atusb/fw/board_app.c b/atusb/fw/board_app.c index 6c64291..520f2fe 100644 --- a/atusb/fw/board_app.c +++ b/atusb/fw/board_app.c @@ -94,7 +94,7 @@ int gpio(uint8_t port, uint8_t data, uint8_t dir, uint8_t mask, uint8_t *res) } /* disable the UART so that we can meddle with these pins as well. */ - UCSR1B = 0; + spi_off(); _delay_ms(1); switch (port) { @@ -115,7 +115,5 @@ int gpio(uint8_t port, uint8_t data, uint8_t dir, uint8_t mask, uint8_t *res) break; } - spi_init(); - return 1; } diff --git a/atusb/fw/spi.c b/atusb/fw/spi.c index 30345d8..047523f 100644 --- a/atusb/fw/spi.c +++ b/atusb/fw/spi.c @@ -19,8 +19,13 @@ #include "spi.h" +static int spi_initialized = 0; + + void spi_begin(void) { + if (!spi_initialized) + spi_init(); CLR(nSS); } @@ -41,6 +46,13 @@ void spi_end(void) } +void spi_off(void) +{ + spi_initialized = 0; + UCSR1B = 0; +} + + void spi_init(void) { SET(nSS); @@ -55,4 +67,6 @@ void spi_init(void) UCSR1B = 1 << RXEN1 | 1 << TXEN1; /* enable receiver and transmitter */ UBRR1 = 0; /* reconfirm the bit rate */ + + spi_initialized = 1; } diff --git a/atusb/fw/spi.h b/atusb/fw/spi.h index 51726f4..ff9d005 100644 --- a/atusb/fw/spi.h +++ b/atusb/fw/spi.h @@ -19,6 +19,7 @@ void spi_begin(void); uint8_t spi_io(uint8_t v); void spi_end(void); +void spi_off(void); void spi_init(void); #define spi_send(v) (void) spi_io(v)