Date:2016-03-30 22:08:00 (7 years 8 months ago)
Author:Stefan Schmidt
Commit:968721c335ce5ce562484f060dc6c70d8e96fc57
Message: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.
Files: atusb/fw/board_app.c (1 diff)
atusb/fw/board_atusb.c (2 diffs)
atusb/fw/board_atusb.h (1 diff)
atusb/fw/board_rzusb.c (2 diffs)
atusb/fw/board_rzusb.h (1 diff)
atusb/fw/spi.c (2 diffs)
atusb/fw/usb/atu2.c (2 diffs)
atusb/fw/usb/usb.h (1 diff)

Change Details

atusb/fw/board_app.c
171171        usb_send(&eps[1], &irq_serial, 1, done, NULL);
172172    }
173173}
174
175#ifdef ATUSB
176void board_app_init(void)
177{
178    /* enable INT0, trigger on rising edge */
179    EICRA = 1 << ISC01 | 1 << ISC00;
180    EIMSK = 1 << 0;
181}
182#endif
183#ifdef RZUSB
184void board_app_init(void)
185{
186    /* enable timer input capture 1, trigger on rising edge */
187    TCCR1B = (1 << ICES1);
188    TIFR1 = (1 << ICF1);
189    TIMSK1 = (1 << ICIE1);
190}
191#endif
atusb/fw/board_atusb.c
2525#include "at86rf230.h"
2626#include "board.h"
2727#include "spi.h"
28#include "usb/usb.h"
29
30static bool spi_initialized = 0;
2831
2932void set_clkm(void)
3033{
...... 
5861
5962    get_sernum();
6063}
64
65void spi_begin(void)
66{
67    if (!spi_initialized)
68        spi_init();
69    CLR(nSS);
70}
71
72void spi_off(void)
73{
74    spi_initialized = 0;
75    UCSR1B = 0;
76}
77
78void spi_init(void)
79{
80    SET(nSS);
81    OUT(SCLK);
82    OUT(MOSI);
83    OUT(nSS);
84    IN(MISO);
85
86    UBRR1 = 0; /* set bit rate to zero to begin */
87    UCSR1C = 1 << UMSEL11 | 1 << UMSEL10;
88            /* set MSPI, MSB first, SPI data mode 0 */
89    UCSR1B = 1 << RXEN1 | 1 << TXEN1;
90            /* enable receiver and transmitter */
91    UBRR1 = 0; /* reconfirm the bit rate */
92
93    spi_initialized = 1;
94}
95
96void usb_init(void)
97{
98    USBCON |= 1 << FRZCLK; /* freeze the clock */
99
100    /* enable the PLL and wait for it to lock */
101    PLLCSR &= ~(1 << PLLP2 | 1 << PLLP1 | 1 << PLLP0);
102    PLLCSR |= 1 << PLLE;
103    while (!(PLLCSR & (1 << PLOCK)));
104
105    USBCON &= ~(1 << USBE); /* reset the controller */
106    USBCON |= 1 << USBE;
107
108    USBCON &= ~(1 << FRZCLK); /* thaw the clock */
109
110    UDCON &= ~(1 << DETACH); /* attach the pull-up */
111    UDIEN = 1 << EORSTE; /* enable device interrupts */
112// UDCON |= 1 << RSTCPU; /* reset CPU on bus reset */
113
114    ep_init();
115}
116
117void board_app_init(void)
118{
119    /* enable INT0, trigger on rising edge */
120    EICRA = 1 << ISC01 | 1 << ISC00;
121    EIMSK = 1 << 0;
122}
atusb/fw/board_atusb.h
4141void set_clkm(void);
4242void board_init(void);
4343
44void spi_begin(void);
45void spi_off(void);
46void spi_init(void);
47
4448#endif /* !BOARD_H */
atusb/fw/board_rzusb.c
2525#include "at86rf230.h"
2626#include "board.h"
2727#include "spi.h"
28#include "usb/usb.h"
29
30static bool spi_initialized = 0;
2831
2932void set_clkm(void)
3033{
...... 
6467
6568    get_sernum();
6669}
70
71void spi_begin(void)
72{
73    if (!spi_initialized)
74        spi_init();
75    CLR(nSS);
76}
77
78void spi_off(void)
79{
80    spi_initialized = 0;
81    SPCR &= ~(1 << SPE);
82}
83
84void spi_init(void)
85{
86    SET(nSS);
87    OUT(SCLK);
88    OUT(MOSI);
89    OUT(nSS);
90    IN(MISO);
91
92    SPCR = (1 << SPE) | (1 << MSTR);
93    SPSR = (1 << SPI2X);
94
95    spi_initialized = 1;
96}
97
98void usb_init(void)
99{
100    USBCON |= 1 << FRZCLK; /* freeze the clock */
101
102    /* enable the PLL and wait for it to lock */
103    /* TODO sheet page 50 For Atmel AT90USB128x only. Do not use with Atmel AT90USB64x. */
104    /* FOR 8 XTAL Mhz only!!! */
105    PLLCSR = ((1 << PLLP1) | (1 << PLLP0));
106    PLLCSR |= 1 << PLLE;
107    while (!(PLLCSR & (1 << PLOCK)));
108
109    UHWCON |= (1 << UVREGE);
110
111    USBCON &= ~((1 << USBE) | (1 << OTGPADE)); /* reset the controller */
112    USBCON |= ((1 << USBE) | (1 << OTGPADE));
113
114    USBCON &= ~(1 << FRZCLK); /* thaw the clock */
115
116    UDCON &= ~(1 << DETACH); /* attach the pull-up */
117    UDIEN = 1 << EORSTE; /* enable device interrupts */
118// UDCON |= 1 << RSTCPU; /* reset CPU on bus reset */
119
120    ep_init();
121}
122
123void board_app_init(void)
124{
125    /* enable timer input capture 1, trigger on rising edge */
126    TCCR1B = (1 << ICES1);
127    TIFR1 = (1 << ICF1);
128    TIMSK1 = (1 << ICIE1);
129}
atusb/fw/board_rzusb.h
4141void set_clkm(void);
4242void board_init(void);
4343
44void spi_begin(void);
45void spi_off(void);
46void spi_init(void);
47
4448#endif /* !BOARD_H */
atusb/fw/spi.c
2020#include "spi.h"
2121
2222
23static bool spi_initialized = 0;
24
25
26void spi_begin(void)
27{
28    if (!spi_initialized)
29        spi_init();
30    CLR(nSS);
31}
32
33
3423uint8_t spi_io(uint8_t v)
3524{
3625// while (!(UCSR1A & 1 << UDRE1));
...... 
6049    SPI_WAIT_DONE();
6150    *buf++ = SPI_DATA;
6251}
63
64
65void spi_off(void)
66{
67    spi_initialized = 0;
68    #ifdef ATUSB
69        UCSR1B = 0;
70    #endif
71    #ifdef RZUSB
72        SPCR &= ~(1 << SPE);
73    #endif
74}
75
76
77void spi_init(void)
78{
79    SET(nSS);
80    OUT(SCLK);
81    OUT(MOSI);
82    OUT(nSS);
83    IN(MISO);
84
85#ifdef ATUSB
86    UBRR1 = 0; /* set bit rate to zero to begin */
87    UCSR1C = 1 << UMSEL11 | 1 << UMSEL10;
88            /* set MSPI, MSB first, SPI data mode 0 */
89    UCSR1B = 1 << RXEN1 | 1 << TXEN1;
90            /* enable receiver and transmitter */
91    UBRR1 = 0; /* reconfirm the bit rate */
92#endif
93#ifdef RZUSB
94    SPCR = (1 << SPE) | (1 << MSTR);
95    SPSR = (1 << SPI2X);
96#endif
97
98    spi_initialized = 1;
99}
atusb/fw/usb/atu2.c
180180}
181181
182182
183static void ep_init(void)
183void ep_init(void)
184184{
185185    UENUM = 0;
186186    UECONX = (1 << RSTDT) | (1 << EPEN); /* enable */
...... 
245245    UDCON |= 1 << DETACH; /* detach the pull-up */
246246    _delay_ms(1);
247247}
248
249
250void usb_init(void)
251{
252    USBCON |= 1 << FRZCLK; /* freeze the clock */
253
254    /* enable the PLL and wait for it to lock */
255#ifdef ATUSB
256    PLLCSR &= ~(1 << PLLP2 | 1 << PLLP1 | 1 << PLLP0);
257#endif
258#ifdef RZUSB
259    /* TODO sheet page 50 For Atmel AT90USB128x only. Do not use with Atmel AT90USB64x. */
260    /* FOR 8 XTAL Mhz only!!! */
261    PLLCSR = ((1 << PLLP1) | (1 << PLLP0));
262#endif
263    PLLCSR |= 1 << PLLE;
264    while (!(PLLCSR & (1 << PLOCK)));
265
266#ifdef ATUSB
267    USBCON &= ~(1 << USBE); /* reset the controller */
268    USBCON |= 1 << USBE;
269#endif
270#ifdef RZUSB
271    UHWCON |= (1 << UVREGE);
272
273    USBCON &= ~((1 << USBE) | (1 << OTGPADE)); /* reset the controller */
274    USBCON |= ((1 << USBE) | (1 << OTGPADE));
275#endif
276
277    USBCON &= ~(1 << FRZCLK); /* thaw the clock */
278
279    UDCON &= ~(1 << DETACH); /* attach the pull-up */
280    UDIEN = 1 << EORSTE; /* enable device interrupts */
281// UDCON |= 1 << RSTCPU; /* reset CPU on bus reset */
282
283    ep_init();
284}
atusb/fw/usb/usb.h
184184void usb_reset(void);
185185void usb_init(void);
186186
187void ep_init(void);
188
187189#endif /* !USB_H */

Archive Download the corresponding diff file



interactive