1
0
mirror of git://projects.qi-hardware.com/ben-wpan.git synced 2024-11-25 03:44:40 +02:00

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.
This commit is contained in:
Stefan Schmidt 2016-03-30 22:08:00 +02:00
parent 65912c2acb
commit 968721c335
8 changed files with 136 additions and 104 deletions

View File

@ -171,21 +171,3 @@ ISR(TIMER1_CAPT_vect)
usb_send(&eps[1], &irq_serial, 1, done, NULL); 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

View File

@ -25,6 +25,9 @@
#include "at86rf230.h" #include "at86rf230.h"
#include "board.h" #include "board.h"
#include "spi.h" #include "spi.h"
#include "usb/usb.h"
static bool spi_initialized = 0;
void set_clkm(void) void set_clkm(void)
{ {
@ -58,3 +61,62 @@ void board_init(void)
get_sernum(); 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;
}

View File

@ -41,4 +41,8 @@
void set_clkm(void); void set_clkm(void);
void board_init(void); void board_init(void);
void spi_begin(void);
void spi_off(void);
void spi_init(void);
#endif /* !BOARD_H */ #endif /* !BOARD_H */

View File

@ -25,6 +25,9 @@
#include "at86rf230.h" #include "at86rf230.h"
#include "board.h" #include "board.h"
#include "spi.h" #include "spi.h"
#include "usb/usb.h"
static bool spi_initialized = 0;
void set_clkm(void) void set_clkm(void)
{ {
@ -64,3 +67,63 @@ void board_init(void)
get_sernum(); 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);
}

View File

@ -41,4 +41,8 @@
void set_clkm(void); void set_clkm(void);
void board_init(void); void board_init(void);
void spi_begin(void);
void spi_off(void);
void spi_init(void);
#endif /* !BOARD_H */ #endif /* !BOARD_H */

View File

@ -20,17 +20,6 @@
#include "spi.h" #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) uint8_t spi_io(uint8_t v)
{ {
// while (!(UCSR1A & 1 << UDRE1)); // while (!(UCSR1A & 1 << UDRE1));
@ -60,40 +49,3 @@ void spi_recv_block(uint8_t *buf, uint8_t n)
SPI_WAIT_DONE(); SPI_WAIT_DONE();
*buf++ = SPI_DATA; *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;
}

View File

@ -180,7 +180,7 @@ stall:
} }
static void ep_init(void) void ep_init(void)
{ {
UENUM = 0; UENUM = 0;
UECONX = (1 << RSTDT) | (1 << EPEN); /* enable */ UECONX = (1 << RSTDT) | (1 << EPEN); /* enable */
@ -245,40 +245,3 @@ void usb_reset(void)
UDCON |= 1 << DETACH; /* detach the pull-up */ UDCON |= 1 << DETACH; /* detach the pull-up */
_delay_ms(1); _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();
}

View File

@ -184,4 +184,6 @@ void usb_ep_change(struct ep_descr *ep);
void usb_reset(void); void usb_reset(void);
void usb_init(void); void usb_init(void);
void ep_init(void);
#endif /* !USB_H */ #endif /* !USB_H */