2 * Copyright Droids Corporation (2008)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Author : Julien LE GUEN - jlg@jleguen.info
25 #include <aversive/pgmspace.h>
26 #include <aversive/error.h>
27 #include <aversive/errno.h>
28 #include <aversive/wait.h>
29 #include <avr/interrupt.h>
38 #include <cc2420_arch.h>
42 #include "firefly2_2.h"
50 * When an unexpected interrupt occurs
55 EMERG(EFAULT, "Bad interrupt vector !");
57 /* Loop and blink the leds */
78 void mylog(struct error * e, ...)
83 uint16_t stream_flags = stdout->flags;
84 volatile time_ext_t time = time_ext_get();
89 case ERROR_SEVERITY_EMERG: severity = "[EMERG]"; break;
90 case ERROR_SEVERITY_ERROR: severity = "[ERROR]"; break;
91 case ERROR_SEVERITY_WARNING: severity = "[WARNING]"; break;
92 case ERROR_SEVERITY_NOTICE: severity = "[NOTICE]"; break;
93 case ERROR_SEVERITY_DEBUG: severity = "[DEBUG]"; break;
94 default: severity = "[DEFAULT]"; break;
99 case E_SPI: module = "[SPI] "; break;
100 case E_CC2420: module = "[CC2420]"; break;
101 case E_TIME_EXT: module = "[TIME] "; break;
102 default: module = ""; break;
106 //vfprintf_P(stdout, severity, ap);
107 //vfprintf_P(stdout, module, ap);
108 printf_P(PSTR("(%lu %lu) "), time.sec, time.nano);
109 printf_P(PSTR("%s"), severity);
110 printf_P(PSTR("%s "), module);
111 vfprintf_P(stdout, e->text, ap);
112 printf_P(PSTR("\r\n"));
114 stdout->flags = stream_flags;
118 void init_ports(void)
120 /* No pull-up resistor */
124 LED_DDR = _BV(LED_ORANGE) | _BV(LED_BLUE) | _BV(LED_GREEN) | _BV(LED_RED);
126 /* Button -> output */
127 cbi(BUTTON_DDR, BUTTON_PIN);
138 /* Initialize Time module */
143 /* Initialize firefly ports */
146 /* Initialize uart1 to send debug messages */
148 fdevopen(uart1_dev_send, uart1_dev_recv);
150 /* register log function */
151 error_register_emerg(mylog);
152 error_register_error(mylog);
153 error_register_warning(mylog);
154 error_register_notice(mylog);
155 error_register_debug(mylog);
157 NOTICE(ESUCCESS, "\r\n\r\n\r\n\r\n\r\n\r\n **************************************");
158 NOTICE(ESUCCESS, "MCUSR = 0x%x", MCUSR);
167 /* Light up yellow led */
173 NOTICE(ESUCCESS, "Ready !");
176 /* Test SPI link: reading CC2420 registers */
177 // MAIN - default = 0xF800
178 reg = cc2420_read_register(MAIN);
180 NOTICE(ESUCCESS, "MAIN read [OK]");
182 ERROR(42, "MAIN should be 0xF800, read 0x%x", reg);
185 NOTICE(ESUCCESS, "MANFIDH = 0x%x", cc2420_read_register(MANFIDH));
186 NOTICE(ESUCCESS, "MANFIDL = 0x%x", cc2420_read_register(MANFIDL));
187 NOTICE(ESUCCESS, "SYNCWORD = 0x%x", cc2420_read_register(SYNCWORD));
188 cc2420_write_register(SYNCWORD, 0xdead);
189 NOTICE(ESUCCESS, "SYNCWORD = 0x%x", cc2420_read_register(SYNCWORD));
190 NOTICE(ESUCCESS, "STAT = 0x%x", cc2420_get_status());
194 cc2420_read_ram(RAM_IEEEADR, &tmp, 1);
195 NOTICE(ESUCCESS, "RAM_IEEEADR = 0x%x", tmp);
196 NOTICE(ESUCCESS, "STAT = 0x%x", cc2420_get_status());
198 cc2420_write_ram(RAM_IEEEADR, &tmp, 1);
199 NOTICE(ESUCCESS, "STAT = 0x%x", cc2420_get_status());
200 cc2420_read_ram(RAM_IEEEADR, &tmp, 1);
201 NOTICE(ESUCCESS, "RAM_IEEEADR = 0x%x", tmp);
202 NOTICE(ESUCCESS, "STAT = 0x%x", cc2420_get_status());
206 /* test TXFIFO and RAM access */
207 for(i = 0; i < 128; i++)
209 NOTICE(ESUCCESS, "Writing to RXFIFO...");
210 cc2420_write_rxfifo(buffer, 128);
212 NOTICE(ESUCCESS, "Reading RXFIFO...");
213 cc2420_read_rxfifo(buffer, 128);
215 /* Dump buffer contents */
216 NOTICE(ESUCCESS, "RAM content after RXFIFO write");
217 for(i = 0; i < 23; i++)
219 printf("0x%x \t| ", i*16);
220 for(j = 0; j < 16; j++)
221 printf("%x ", buffer[i*16+j]);
226 /* Write and Read RAM */
227 for(i = 0; i < 128; i++)
229 NOTICE(ESUCCESS, "Writing to RAM...");
230 cc2420_write_ram(RAM_TXFIFO, buffer, 128);
232 NOTICE(ESUCCESS, "Reading RAM...");
233 cc2420_read_ram(RAM_TXFIFO, buffer, 368);
235 /* Dump buffer contents */
236 NOTICE(ESUCCESS, "RAM content after write to RAM");
237 for(i = 0; i < 23; i++)
239 printf("0x%x \t| ", i*16);
240 for(j = 0; j < 16; j++)
241 printf("%x ", buffer[i*16+j]);
248 * TEST for PORT and DDR and stuff
250 NOTICE(ESUCCESS, "&PORTC = 0x%x | PORTC = 0x%x | *(&PORTC) = 0x%x",
251 &PORTC, PORTC, *(&PORTC));
252 spi_display_ss_lines();
258 if(!TST(BUTTON_PORT, BUTTON_PIN))
260 time_ext_t local_time = time_ext_get();
262 NOTICE(ESUCCESS, "Button event at %ld %ld",
263 local_time.sec, local_time.nano);
264 NOTICE(ESUCCESS, "STAT = 0x%x", cc2420_get_status());
265 NOTICE(ESUCCESS, "DDRC = 0x%x | PORTC = 0x%x", DDRC, PORTC);
266 while(!TST(BUTTON_PORT, BUTTON_PIN))
268 NOTICE(ESUCCESS, "STAT = 0x%x", cc2420_get_status());