IEEE 802.15.4 subsystem
Sign in or create your account | Project List | Help
IEEE 802.15.4 subsystem Git Source Tree
Root/
Source at commit 862b554e2d5d3b00c9d2669ac57484c0cf3e4982 created 12 years 8 months ago. By Werner Almesberger, libatrf: added HardMAC functions | |
---|---|
1 | /* |
2 | * fw/board_app.c - Board-specific functions (for the application) |
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 <stddef.h> |
15 | #include <stdint.h> |
16 | |
17 | #include <avr/io.h> |
18 | #include <avr/interrupt.h> |
19 | |
20 | #define F_CPU 8000000UL |
21 | #include <util/delay.h> |
22 | |
23 | #include "usb.h" |
24 | #include "at86rf230.h" |
25 | #include "spi.h" |
26 | #include "mac.h" |
27 | #include "board.h" |
28 | |
29 | |
30 | static volatile uint32_t timer_h = 0; /* 2^(16+32) / 8 MHz = ~1.1 years */ |
31 | |
32 | |
33 | void reset_cpu(void) |
34 | { |
35 | WDTCSR = 1 << WDE; |
36 | } |
37 | |
38 | |
39 | uint8_t read_irq(void) |
40 | { |
41 | return PIN(IRQ_RF); |
42 | } |
43 | |
44 | |
45 | void slp_tr(void) |
46 | { |
47 | SET(SLP_TR); |
48 | CLR(SLP_TR); |
49 | } |
50 | |
51 | |
52 | ISR(TIMER1_OVF_vect) |
53 | { |
54 | timer_h++; |
55 | } |
56 | |
57 | |
58 | uint64_t timer_read(void) |
59 | { |
60 | uint32_t high; |
61 | uint8_t low, mid; |
62 | |
63 | do { |
64 | if (TIFR1 & (1 << TOV1)) { |
65 | TIFR1 = 1 << TOV1; |
66 | timer_h++; |
67 | } |
68 | high = timer_h; |
69 | low = TCNT1L; |
70 | mid = TCNT1H; |
71 | } |
72 | while (TIFR1 & (1 << TOV1)); |
73 | |
74 | /* |
75 | * We need all these casts because the intermediate results are handled |
76 | * as if they were signed and thus get sign-expanded. Sounds wrong-ish. |
77 | */ |
78 | return (uint64_t) high << 16 | (uint64_t) mid << 8 | (uint64_t) low; |
79 | } |
80 | |
81 | |
82 | void timer_init(void) |
83 | { |
84 | /* configure timer 1 as a free-running CLK counter */ |
85 | |
86 | TCCR1A = 0; |
87 | TCCR1B = 1 << CS10; |
88 | |
89 | /* enable timer overflow interrupt */ |
90 | |
91 | TIMSK1 = 1 << TOIE1; |
92 | } |
93 | |
94 | |
95 | int gpio(uint8_t port, uint8_t data, uint8_t dir, uint8_t mask, uint8_t *res) |
96 | { |
97 | EIMSK = 0; /* recover INT_RF to ATUSB_GPIO_CLEANUP or an MCU reset */ |
98 | |
99 | switch (port) { |
100 | case 1: |
101 | DDRB = (DDRB & ~mask) | dir; |
102 | PORTB = (PORTB & ~mask) | data; |
103 | break; |
104 | case 2: |
105 | DDRC = (DDRC & ~mask) | dir; |
106 | PORTC = (PORTC & ~mask) | data; |
107 | break; |
108 | case 3: |
109 | DDRD = (DDRD & ~mask) | dir; |
110 | PORTD = (PORTD & ~mask) | data; |
111 | break; |
112 | default: |
113 | return 0; |
114 | } |
115 | |
116 | /* disable the UART so that we can meddle with these pins as well. */ |
117 | spi_off(); |
118 | _delay_ms(1); |
119 | |
120 | switch (port) { |
121 | case 1: |
122 | res[0] = PINB; |
123 | res[1] = PORTB; |
124 | res[2] = DDRB; |
125 | break; |
126 | case 2: |
127 | res[0] = PINC; |
128 | res[1] = PORTC; |
129 | res[2] = DDRC; |
130 | break; |
131 | case 3: |
132 | res[0] = PIND; |
133 | res[1] = PORTD; |
134 | res[2] = DDRD; |
135 | break; |
136 | } |
137 | |
138 | return 1; |
139 | } |
140 | |
141 | |
142 | void gpio_cleanup(void) |
143 | { |
144 | EIMSK = 1 << 0; |
145 | } |
146 | |
147 | |
148 | static void done(void *user) |
149 | { |
150 | led(0); |
151 | } |
152 | |
153 | |
154 | uint8_t irq_serial; |
155 | |
156 | |
157 | ISR(INT0_vect) |
158 | { |
159 | if (mac_irq) { |
160 | if (mac_irq()) |
161 | return; |
162 | } |
163 | if (eps[1].state == EP_IDLE) { |
164 | led(1); |
165 | irq_serial = (irq_serial+1) | 0x80; |
166 | usb_send(&eps[1], &irq_serial, 1, done, NULL); |
167 | } |
168 | } |
169 | |
170 | |
171 | void board_app_init(void) |
172 | { |
173 | /* enable INT0, trigger on rising edge */ |
174 | EICRA = 1 << ISC01 | 1 << ISC00; |
175 | EIMSK = 1 << 0; |
176 | } |
177 |