From 1c004ee2d21c841f9bb7533dbd7a222b6e517537 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Tue, 8 Feb 2011 21:32:24 -0300 Subject: [PATCH] atusb/fw2: moved board-specific functions (SPI, RF reset) out of main() - Makefile (OBJS): added spi.o - spi.h, spi.c, atusb.c: moved SPI functions to new file - spi.h (spi), spi.c (spi), atusb.c (main): renamed function "spi" to "spi_io" - atusb.c (reset_rf, main): moved transceiver reset to separate function --- atusb/fw2/Makefile | 2 +- atusb/fw2/atusb.c | 58 ++++++++++++++-------------------------------- atusb/fw2/spi.c | 44 +++++++++++++++++++++++++++++++++++ atusb/fw2/spi.h | 14 +++++++++++ 4 files changed, 76 insertions(+), 42 deletions(-) create mode 100644 atusb/fw2/spi.c create mode 100644 atusb/fw2/spi.h diff --git a/atusb/fw2/Makefile b/atusb/fw2/Makefile index 1414030..e6fc479 100644 --- a/atusb/fw2/Makefile +++ b/atusb/fw2/Makefile @@ -16,7 +16,7 @@ USB_OBJS = $(FreakUSB)/usb/usb.o $(FreakUSB)/usb/ctrl.o \ $(FreakUSB)/usb/usb_buf.o \ $(FreakUSB)/hw/at90usbxx2/ep.o $(FreakUSB)/hw/at90usbxx2/hw.o \ $(FreakUSB)/hw/at90usbxx2/isr.o -OBJS = atusb.o descr.o $(USB_OBJS) +OBJS = atusb.o spi.o descr.o $(USB_OBJS) CFLAGS += -I../fw/include \ -I$(FreakUSB)/usb -I$(FreakUSB)/hw/at90usbxx2 \ diff --git a/atusb/fw2/atusb.c b/atusb/fw2/atusb.c index 8cbcbd5..c2b85c7 100644 --- a/atusb/fw2/atusb.c +++ b/atusb/fw2/atusb.c @@ -7,29 +7,25 @@ #include "freakusb.h" -#include "io.h" #include "at86rf230.h" +#include "io.h" +#include "spi.h" -static void spi_begin(void) +void reset_rf(void); + + +void reset_rf(void) { - CLR(nSS); -} + /* AT86RF231 data sheet, 12.4.13, reset pulse width: 625 ns (min) */ + CLR(nRST_RF); + _delay_us(1); + SET(nRST_RF); -static uint8_t spi(uint8_t v) -{ -// while (!(UCSR1A & 1 << UDRE1)); - UDR1 = v; - while (!(UCSR1A & 1 << RXC1)); - return UDR1; -} + /* 12.4.14: SPI access latency after reset: 625 ns (min) */ - -static void spi_end(void) -{ -// while (!(UCSR1A & 1 << TXC1)); - SET(nSS); + _delay_us(1); } @@ -43,32 +39,12 @@ int main(void) /* set up all the outputs; default port value is 0 */ OUT(LED); - OUT(nRST_RF); /* reset the transceiver */ + OUT(nRST_RF); /* resets the transceiver */ OUT(SLP_TR); - OUT(SCLK); - OUT(MOSI); - OUT(nSS); - /* set up UART-SPI */ + spi_init(); - UCSR1C = 1 << UMSEL11 | 1 << UMSEL10; - /* set MSPI, MSB first, SPI data mode 0 */ - UCSR1B = 1 << RXEN1 | 1 << TXEN1; - /* enable receiver and transmitter */ - UBRR1 = 0; /* reconfirm the bit rate */ - - /* bring the transceiver out of reset */ - - /* - * AT86RF231 data sheet, 12.4.13, reset pulse with: 625 ns (min). - * We spend a lot more time getting here, so no extra wait is needed. - */ - SET(nRST_RF); - - /* - * 12.4.14: SPI access latency after reset: 625 ns - */ - _delay_us(1); + reset_rf(); /* switch CLKM to 8 MHz */ @@ -81,8 +57,8 @@ int main(void) */ spi_begin(); - spi(AT86RF230_REG_WRITE | REG_TRX_CTRL_0); - spi(CLKM_CTRL_8MHz); + spi_send(AT86RF230_REG_WRITE | REG_TRX_CTRL_0); + spi_send(CLKM_CTRL_8MHz); spi_end(); /* now we should be at 8 MHz */ diff --git a/atusb/fw2/spi.c b/atusb/fw2/spi.c new file mode 100644 index 0000000..766e70c --- /dev/null +++ b/atusb/fw2/spi.c @@ -0,0 +1,44 @@ +#include + +#include + +#include "io.h" +#include "spi.h" + + +void spi_begin(void) +{ + CLR(nSS); +} + + +uint8_t spi_io(uint8_t v) +{ +// while (!(UCSR1A & 1 << UDRE1)); + UDR1 = v; + while (!(UCSR1A & 1 << RXC1)); + return UDR1; +} + + +void spi_end(void) +{ +// while (!(UCSR1A & 1 << TXC1)); + SET(nSS); +} + + +void spi_init(void) +{ + OUT(SCLK); + OUT(MOSI); + OUT(nSS); + IN(MISO); + + UBRR1 = 0; /* set bit rate to zero to begin */ + UCSR1C = 1 << UMSEL11 | 1 << UMSEL10; + /* set MSPI, MSB first, SPI data mode 0 */ + UCSR1B = 1 << RXEN1 | 1 << TXEN1; + /* enable receiver and transmitter */ + UBRR1 = 0; /* reconfirm the bit rate */ +} diff --git a/atusb/fw2/spi.h b/atusb/fw2/spi.h new file mode 100644 index 0000000..69c5983 --- /dev/null +++ b/atusb/fw2/spi.h @@ -0,0 +1,14 @@ +#ifndef SPI_H +#define SPI_H + +#include + +void spi_begin(void); +uint8_t spi_io(uint8_t v); +void spi_end(void); +void spi_init(void); + +#define spi_send(v) (void) spi_io(v) +#define spi_recv(v) spi_io(0) + +#endif /* !SPI_H */