Root/atusb/fw/board.c

Source at commit 1dcc83391e51890c76fc8e11aeb24300a99412a2 created 8 years 10 months ago.
By Werner Almesberger, atusb/fw/Makefile: put -mmcu into CFLAGS so that DEPEND uses it, too
1/*
2 * fw/board.c - Board-specific functions
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdint.h>
15
16#include <avr/io.h>
17#include <avr/interrupt.h>
18
19#define F_CPU 8000000UL
20#include <util/delay.h>
21
22#include "at86rf230.h"
23#include "board.h"
24#include "spi.h"
25
26
27static void set_clkm(void)
28{
29    /* switch CLKM to 8 MHz */
30
31    /*
32     * @@@ Note: Atmel advise against changing the external clock in
33     * mid-flight. We should therefore switch to the RC clock first, then
34     * crank up the external clock, and finally switch back to the external
35     * clock. The clock switching procedure is described in the ATmega32U2
36     * data sheet in secton 8.2.2.
37     */
38
39    spi_begin();
40    spi_send(AT86RF230_REG_WRITE | REG_TRX_CTRL_0);
41    spi_send(CLKM_CTRL_8MHz);
42    spi_end();
43}
44
45
46void reset_rf(void)
47{
48    /* AT86RF231 data sheet, 12.4.13, reset pulse width: 625 ns (min) */
49
50    CLR(nRST_RF);
51    _delay_us(1);
52    SET(nRST_RF);
53
54    /* 12.4.14: SPI access latency after reset: 625 ns (min) */
55
56    _delay_us(1);
57
58    /* we must restore TRX_CTRL_0 after each reset (9.6.4) */
59
60    set_clkm();
61}
62
63
64uint8_t read_irq(void)
65{
66    return PIN(IRQ_RF);
67}
68
69
70void led(int on)
71{
72    if (on)
73        SET(LED);
74    else
75        CLR(LED);
76}
77
78
79void panic(void)
80{
81    cli();
82    while (1) {
83        SET(LED);
84        _delay_ms(100);
85        CLR(LED);
86        _delay_ms(100);
87    }
88}
89
90
91void board_init(void)
92{
93    /* We start with a 1 MHz/8 clock. Disable the prescaler. */
94
95    CLKPR = 1 << CLKPCE;
96    CLKPR = 0;
97
98    /* set up all the outputs; default port value is 0 */
99
100    OUT(LED);
101    OUT(nRST_RF); /* resets the transceiver */
102    OUT(SLP_TR);
103}
104

Archive Download this file



interactive