From 968721c335ce5ce562484f060dc6c70d8e96fc57 Mon Sep 17 00:00:00 2001 From: Stefan Schmidt Date: Wed, 30 Mar 2016 22:08:00 +0200 Subject: [PATCH] atusb: fw: re-factor SPI, USB and board_app code for board specifics Move board specific code form spi, usb and board_app into the new board specific files to avoid to many ifdefs. --- atusb/fw/board_app.c | 18 ------------ atusb/fw/board_atusb.c | 62 +++++++++++++++++++++++++++++++++++++++++ atusb/fw/board_atusb.h | 4 +++ atusb/fw/board_rzusb.c | 63 ++++++++++++++++++++++++++++++++++++++++++ atusb/fw/board_rzusb.h | 4 +++ atusb/fw/spi.c | 48 -------------------------------- atusb/fw/usb/atu2.c | 39 +------------------------- atusb/fw/usb/usb.h | 2 ++ 8 files changed, 136 insertions(+), 104 deletions(-) diff --git a/atusb/fw/board_app.c b/atusb/fw/board_app.c index 22bae19..815f4da 100644 --- a/atusb/fw/board_app.c +++ b/atusb/fw/board_app.c @@ -171,21 +171,3 @@ ISR(TIMER1_CAPT_vect) usb_send(&eps[1], &irq_serial, 1, done, NULL); } } - -#ifdef ATUSB -void board_app_init(void) -{ - /* enable INT0, trigger on rising edge */ - EICRA = 1 << ISC01 | 1 << ISC00; - EIMSK = 1 << 0; -} -#endif -#ifdef RZUSB -void board_app_init(void) -{ - /* enable timer input capture 1, trigger on rising edge */ - TCCR1B = (1 << ICES1); - TIFR1 = (1 << ICF1); - TIMSK1 = (1 << ICIE1); -} -#endif diff --git a/atusb/fw/board_atusb.c b/atusb/fw/board_atusb.c index 485f537..518fe78 100644 --- a/atusb/fw/board_atusb.c +++ b/atusb/fw/board_atusb.c @@ -25,6 +25,9 @@ #include "at86rf230.h" #include "board.h" #include "spi.h" +#include "usb/usb.h" + +static bool spi_initialized = 0; void set_clkm(void) { @@ -58,3 +61,62 @@ void board_init(void) get_sernum(); } + +void spi_begin(void) +{ + if (!spi_initialized) + spi_init(); + CLR(nSS); +} + +void spi_off(void) +{ + spi_initialized = 0; + UCSR1B = 0; +} + +void spi_init(void) +{ + SET(nSS); + 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 */ + + spi_initialized = 1; +} + +void usb_init(void) +{ + USBCON |= 1 << FRZCLK; /* freeze the clock */ + + /* enable the PLL and wait for it to lock */ + PLLCSR &= ~(1 << PLLP2 | 1 << PLLP1 | 1 << PLLP0); + PLLCSR |= 1 << PLLE; + while (!(PLLCSR & (1 << PLOCK))); + + USBCON &= ~(1 << USBE); /* reset the controller */ + USBCON |= 1 << USBE; + + USBCON &= ~(1 << FRZCLK); /* thaw the clock */ + + UDCON &= ~(1 << DETACH); /* attach the pull-up */ + UDIEN = 1 << EORSTE; /* enable device interrupts */ +// UDCON |= 1 << RSTCPU; /* reset CPU on bus reset */ + + ep_init(); +} + +void board_app_init(void) +{ + /* enable INT0, trigger on rising edge */ + EICRA = 1 << ISC01 | 1 << ISC00; + EIMSK = 1 << 0; +} diff --git a/atusb/fw/board_atusb.h b/atusb/fw/board_atusb.h index 1ba0722..e5974c7 100644 --- a/atusb/fw/board_atusb.h +++ b/atusb/fw/board_atusb.h @@ -41,4 +41,8 @@ void set_clkm(void); void board_init(void); +void spi_begin(void); +void spi_off(void); +void spi_init(void); + #endif /* !BOARD_H */ diff --git a/atusb/fw/board_rzusb.c b/atusb/fw/board_rzusb.c index fe0b037..83333f8 100644 --- a/atusb/fw/board_rzusb.c +++ b/atusb/fw/board_rzusb.c @@ -25,6 +25,9 @@ #include "at86rf230.h" #include "board.h" #include "spi.h" +#include "usb/usb.h" + +static bool spi_initialized = 0; void set_clkm(void) { @@ -64,3 +67,63 @@ void board_init(void) get_sernum(); } + +void spi_begin(void) +{ + if (!spi_initialized) + spi_init(); + CLR(nSS); +} + +void spi_off(void) +{ + spi_initialized = 0; + SPCR &= ~(1 << SPE); +} + +void spi_init(void) +{ + SET(nSS); + OUT(SCLK); + OUT(MOSI); + OUT(nSS); + IN(MISO); + + SPCR = (1 << SPE) | (1 << MSTR); + SPSR = (1 << SPI2X); + + spi_initialized = 1; +} + +void usb_init(void) +{ + USBCON |= 1 << FRZCLK; /* freeze the clock */ + + /* enable the PLL and wait for it to lock */ + /* TODO sheet page 50 For Atmel AT90USB128x only. Do not use with Atmel AT90USB64x. */ + /* FOR 8 XTAL Mhz only!!! */ + PLLCSR = ((1 << PLLP1) | (1 << PLLP0)); + PLLCSR |= 1 << PLLE; + while (!(PLLCSR & (1 << PLOCK))); + + UHWCON |= (1 << UVREGE); + + USBCON &= ~((1 << USBE) | (1 << OTGPADE)); /* reset the controller */ + USBCON |= ((1 << USBE) | (1 << OTGPADE)); + + USBCON &= ~(1 << FRZCLK); /* thaw the clock */ + + UDCON &= ~(1 << DETACH); /* attach the pull-up */ + UDIEN = 1 << EORSTE; /* enable device interrupts */ +// UDCON |= 1 << RSTCPU; /* reset CPU on bus reset */ + + ep_init(); +} + +void board_app_init(void) +{ + /* enable timer input capture 1, trigger on rising edge */ + TCCR1B = (1 << ICES1); + TIFR1 = (1 << ICF1); + TIMSK1 = (1 << ICIE1); +} diff --git a/atusb/fw/board_rzusb.h b/atusb/fw/board_rzusb.h index 2e22747..c2e518f 100644 --- a/atusb/fw/board_rzusb.h +++ b/atusb/fw/board_rzusb.h @@ -41,4 +41,8 @@ void set_clkm(void); void board_init(void); +void spi_begin(void); +void spi_off(void); +void spi_init(void); + #endif /* !BOARD_H */ diff --git a/atusb/fw/spi.c b/atusb/fw/spi.c index 359e99d..3fa5715 100644 --- a/atusb/fw/spi.c +++ b/atusb/fw/spi.c @@ -20,17 +20,6 @@ #include "spi.h" -static bool spi_initialized = 0; - - -void spi_begin(void) -{ - if (!spi_initialized) - spi_init(); - CLR(nSS); -} - - uint8_t spi_io(uint8_t v) { // while (!(UCSR1A & 1 << UDRE1)); @@ -60,40 +49,3 @@ void spi_recv_block(uint8_t *buf, uint8_t n) SPI_WAIT_DONE(); *buf++ = SPI_DATA; } - - -void spi_off(void) -{ - spi_initialized = 0; - #ifdef ATUSB - UCSR1B = 0; - #endif - #ifdef RZUSB - SPCR &= ~(1 << SPE); - #endif -} - - -void spi_init(void) -{ - SET(nSS); - OUT(SCLK); - OUT(MOSI); - OUT(nSS); - IN(MISO); - -#ifdef ATUSB - 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 */ -#endif -#ifdef RZUSB - SPCR = (1 << SPE) | (1 << MSTR); - SPSR = (1 << SPI2X); -#endif - - spi_initialized = 1; -} diff --git a/atusb/fw/usb/atu2.c b/atusb/fw/usb/atu2.c index bc1118b..98158bf 100644 --- a/atusb/fw/usb/atu2.c +++ b/atusb/fw/usb/atu2.c @@ -180,7 +180,7 @@ stall: } -static void ep_init(void) +void ep_init(void) { UENUM = 0; UECONX = (1 << RSTDT) | (1 << EPEN); /* enable */ @@ -245,40 +245,3 @@ void usb_reset(void) UDCON |= 1 << DETACH; /* detach the pull-up */ _delay_ms(1); } - - -void usb_init(void) -{ - USBCON |= 1 << FRZCLK; /* freeze the clock */ - - /* enable the PLL and wait for it to lock */ -#ifdef ATUSB - PLLCSR &= ~(1 << PLLP2 | 1 << PLLP1 | 1 << PLLP0); -#endif -#ifdef RZUSB - /* TODO sheet page 50 For Atmel AT90USB128x only. Do not use with Atmel AT90USB64x. */ - /* FOR 8 XTAL Mhz only!!! */ - PLLCSR = ((1 << PLLP1) | (1 << PLLP0)); -#endif - PLLCSR |= 1 << PLLE; - while (!(PLLCSR & (1 << PLOCK))); - -#ifdef ATUSB - USBCON &= ~(1 << USBE); /* reset the controller */ - USBCON |= 1 << USBE; -#endif -#ifdef RZUSB - UHWCON |= (1 << UVREGE); - - USBCON &= ~((1 << USBE) | (1 << OTGPADE)); /* reset the controller */ - USBCON |= ((1 << USBE) | (1 << OTGPADE)); -#endif - - USBCON &= ~(1 << FRZCLK); /* thaw the clock */ - - UDCON &= ~(1 << DETACH); /* attach the pull-up */ - UDIEN = 1 << EORSTE; /* enable device interrupts */ -// UDCON |= 1 << RSTCPU; /* reset CPU on bus reset */ - - ep_init(); -} diff --git a/atusb/fw/usb/usb.h b/atusb/fw/usb/usb.h index 6428ccd..cb40f9e 100644 --- a/atusb/fw/usb/usb.h +++ b/atusb/fw/usb/usb.h @@ -184,4 +184,6 @@ void usb_ep_change(struct ep_descr *ep); void usb_reset(void); void usb_init(void); +void ep_init(void); + #endif /* !USB_H */