Date:2011-02-09 01:32:24 (8 years 9 months ago)
Author:Werner Almesberger
Commit:1c004ee2d21c841f9bb7533dbd7a222b6e517537
Message: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
Files: atusb/fw2/Makefile (1 diff)
atusb/fw2/atusb.c (3 diffs)
atusb/fw2/spi.c (1 diff)
atusb/fw2/spi.h (1 diff)

Change Details

atusb/fw2/Makefile
1616  $(FreakUSB)/usb/usb_buf.o \
1717  $(FreakUSB)/hw/at90usbxx2/ep.o $(FreakUSB)/hw/at90usbxx2/hw.o \
1818  $(FreakUSB)/hw/at90usbxx2/isr.o
19OBJS = atusb.o descr.o $(USB_OBJS)
19OBJS = atusb.o spi.o descr.o $(USB_OBJS)
2020
2121CFLAGS += -I../fw/include \
2222      -I$(FreakUSB)/usb -I$(FreakUSB)/hw/at90usbxx2 \
atusb/fw2/atusb.c
77
88#include "freakusb.h"
99
10#include "io.h"
1110#include "at86rf230.h"
11#include "io.h"
12#include "spi.h"
1213
1314
14static void spi_begin(void)
15{
16    CLR(nSS);
17}
15void reset_rf(void);
1816
1917
20static uint8_t spi(uint8_t v)
18void reset_rf(void)
2119{
22// while (!(UCSR1A & 1 << UDRE1));
23    UDR1 = v;
24    while (!(UCSR1A & 1 << RXC1));
25    return UDR1;
26}
20    /* AT86RF231 data sheet, 12.4.13, reset pulse width: 625 ns (min) */
2721
22    CLR(nRST_RF);
23    _delay_us(1);
24    SET(nRST_RF);
2825
29static void spi_end(void)
30{
31// while (!(UCSR1A & 1 << TXC1));
32    SET(nSS);
26    /* 12.4.14: SPI access latency after reset: 625 ns (min) */
27
28    _delay_us(1);
3329}
3430
3531
...... 
4339    /* set up all the outputs; default port value is 0 */
4440
4541    OUT(LED);
46    OUT(nRST_RF); /* reset the transceiver */
42    OUT(nRST_RF); /* resets the transceiver */
4743    OUT(SLP_TR);
48    OUT(SCLK);
49    OUT(MOSI);
50    OUT(nSS);
5144
52    /* set up UART-SPI */
45    spi_init();
5346
54    UCSR1C = 1 << UMSEL11 | 1 << UMSEL10;
55                /* set MSPI, MSB first, SPI data mode 0 */
56    UCSR1B = 1 << RXEN1 | 1 << TXEN1;
57                /* enable receiver and transmitter */
58    UBRR1 = 0; /* reconfirm the bit rate */
59
60    /* bring the transceiver out of reset */
61
62    /*
63     * AT86RF231 data sheet, 12.4.13, reset pulse with: 625 ns (min).
64     * We spend a lot more time getting here, so no extra wait is needed.
65     */
66    SET(nRST_RF);
67
68    /*
69     * 12.4.14: SPI access latency after reset: 625 ns
70     */
71    _delay_us(1);
47    reset_rf();
7248
7349    /* switch CLKM to 8 MHz */
7450
...... 
8157     */
8258
8359    spi_begin();
84    spi(AT86RF230_REG_WRITE | REG_TRX_CTRL_0);
85    spi(CLKM_CTRL_8MHz);
60    spi_send(AT86RF230_REG_WRITE | REG_TRX_CTRL_0);
61    spi_send(CLKM_CTRL_8MHz);
8662    spi_end();
8763
8864    /* now we should be at 8 MHz */
atusb/fw2/spi.c
1#include <stdint.h>
2
3#include <avr/io.h>
4
5#include "io.h"
6#include "spi.h"
7
8
9void spi_begin(void)
10{
11    CLR(nSS);
12}
13
14
15uint8_t spi_io(uint8_t v)
16{
17// while (!(UCSR1A & 1 << UDRE1));
18        UDR1 = v;
19        while (!(UCSR1A & 1 << RXC1));
20        return UDR1;
21}
22
23
24void spi_end(void)
25{
26// while (!(UCSR1A & 1 << TXC1));
27    SET(nSS);
28}
29
30
31void spi_init(void)
32{
33    OUT(SCLK);
34    OUT(MOSI);
35    OUT(nSS);
36    IN(MISO);
37
38    UBRR1 = 0; /* set bit rate to zero to begin */
39    UCSR1C = 1 << UMSEL11 | 1 << UMSEL10;
40            /* set MSPI, MSB first, SPI data mode 0 */
41    UCSR1B = 1 << RXEN1 | 1 << TXEN1;
42            /* enable receiver and transmitter */
43    UBRR1 = 0; /* reconfirm the bit rate */
44}
atusb/fw2/spi.h
1#ifndef SPI_H
2#define SPI_H
3
4#include <stdint.h>
5
6void spi_begin(void);
7uint8_t spi_io(uint8_t v);
8void spi_end(void);
9void spi_init(void);
10
11#define spi_send(v) (void) spi_io(v)
12#define spi_recv(v) spi_io(0)
13
14#endif /* !SPI_H */

Archive Download the corresponding diff file



interactive