hostsim and cob handling
[aversive.git] / projects / firefly_example1 / main.c
1 /*  
2  *  Copyright Droids Corporation (2008)
3  * 
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.
8  *
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.
13  *
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
17  *
18  */
19
20 /*
21  * Author : Julien LE GUEN - jlg@jleguen.info
22  */
23
24 #include <aversive.h>
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>
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <math.h>
34
35 #include <uart.h>
36 #include <spi.h>
37 #include <cc2420.h>
38 #include <cc2420_arch.h>
39 #include <time_ext.h>
40
41
42 #include "firefly2_2.h"
43
44
45 uint8_t i;
46
47
48
49 /*
50  *      When an unexpected interrupt occurs
51  */
52 ISR(__vector_default)
53 {
54         /* Notify the user */
55         EMERG(EFAULT, "Bad interrupt vector !");
56
57         /* Loop and blink the leds */
58         while(TRUE)
59         {
60                 LED_SET(LED_BLUE);
61                 LED_SET(LED_GREEN);
62                 LED_SET(LED_ORANGE);
63                 LED_SET(LED_RED);
64                 wait_ms(100);
65
66                 LED_CLR(LED_BLUE);
67                 LED_CLR(LED_RED);
68                 LED_CLR(LED_ORANGE);
69                 LED_CLR(LED_GREEN);
70                 wait_ms(100);
71         }
72 }
73
74
75 /*
76  *      LOG function
77  */
78 void mylog(struct error * e, ...)
79 {
80         char *severity;
81         char *module;
82         va_list ap;
83         uint16_t stream_flags = stdout->flags;
84         volatile time_ext_t time = time_ext_get();
85
86
87         switch(e->severity)
88         {
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;
95         }
96
97         switch(e->err_num)
98         {
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;
103         }
104
105         va_start(ap, e);
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"));
113         va_end(ap);
114         stdout->flags = stream_flags;
115 }
116
117
118 void init_ports(void)
119 {
120         /* No pull-up resistor */
121         MCUCR |= (1<<PUD);
122                 
123         /* Leds -> ouputs */
124         LED_DDR = _BV(LED_ORANGE) | _BV(LED_BLUE) | _BV(LED_GREEN) | _BV(LED_RED);
125         
126         /* Button -> output */
127         cbi(BUTTON_DDR, BUTTON_PIN);
128 }
129
130
131 int main(void)
132 {
133         uint16_t reg;
134         uint8_t buffer[368];
135         uint16_t i;
136         uint8_t j;
137
138         /* Initialize Time module */
139         time_ext_init();
140         time_ext_set(0, 0);
141         //sei();
142
143         /* Initialize firefly ports */
144         init_ports();
145
146         /* Initialize uart1 to send debug messages */
147         uart_init();
148         fdevopen(uart1_dev_send, uart1_dev_recv);
149
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);
156
157         NOTICE(ESUCCESS, "\r\n\r\n\r\n\r\n\r\n\r\n **************************************");
158         NOTICE(ESUCCESS, "MCUSR = 0x%x", MCUSR);
159
160
161         /* Clear all leds*/
162         LED_CLR(LED_GREEN);
163         LED_CLR(LED_ORANGE);
164         LED_CLR(LED_RED);
165         LED_CLR(LED_BLUE);
166
167         /* Light up yellow led */
168         LED_SET(LED_ORANGE);
169
170         cc2420_init();
171
172
173         NOTICE(ESUCCESS, "Ready !");
174
175
176         /* Test SPI link: reading CC2420 registers */
177         // MAIN - default = 0xF800
178         reg = cc2420_read_register(MAIN);
179         if(reg == 0xF800)
180                 NOTICE(ESUCCESS, "MAIN read [OK]");
181         else
182                 ERROR(42, "MAIN should be 0xF800, read 0x%x", reg);
183
184
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());
191
192
193         uint8_t tmp = 0x42;
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());
197         tmp = 0x42;
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());
203
204
205 //#if 0
206         /* test TXFIFO and RAM access */
207         for(i = 0; i < 128; i++)
208                 buffer[i] = i;
209         NOTICE(ESUCCESS, "Writing to RXFIFO...");
210         cc2420_write_rxfifo(buffer, 128);
211         /* Read all RAM */
212         NOTICE(ESUCCESS, "Reading RXFIFO...");
213         cc2420_read_rxfifo(buffer, 128);
214
215         /* Dump buffer contents */
216         NOTICE(ESUCCESS, "RAM content after RXFIFO write");
217         for(i = 0; i < 23; i++)
218         {
219                 printf("0x%x \t| ", i*16);
220                 for(j = 0; j < 16; j++)
221                         printf("%x ", buffer[i*16+j]);
222                 printf("\r\n");
223         }
224
225
226         /* Write and Read RAM */
227         for(i = 0; i < 128; i++)
228                 buffer[i] = i;
229         NOTICE(ESUCCESS, "Writing to RAM...");
230         cc2420_write_ram(RAM_TXFIFO, buffer, 128);
231         /* Read all RAM */
232         NOTICE(ESUCCESS, "Reading RAM...");
233         cc2420_read_ram(RAM_TXFIFO, buffer, 368);
234
235         /* Dump buffer contents */
236         NOTICE(ESUCCESS, "RAM content after write to RAM");
237         for(i = 0; i < 23; i++)
238         {
239                 printf("0x%x \t| ", i*16);
240                 for(j = 0; j < 16; j++)
241                         printf("%x ", buffer[i*16+j]);
242                 printf("\r\n");
243         }
244 //#endif
245
246
247         /*
248          *      TEST for PORT and DDR and stuff
249          */
250         NOTICE(ESUCCESS, "&PORTC = 0x%x | PORTC = 0x%x | *(&PORTC) = 0x%x",
251                         &PORTC, PORTC, *(&PORTC));
252         spi_display_ss_lines();
253
254
255         /* Infinite loop */
256         while(1)
257         {
258                 if(!TST(BUTTON_PORT, BUTTON_PIN))
259                 {
260                         time_ext_t local_time = time_ext_get();
261                         LED_SET(LED_BLUE);
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))
267                                 ;
268                         NOTICE(ESUCCESS, "STAT = 0x%x", cc2420_get_status());
269                 }
270                 else
271                         LED_CLR(LED_BLUE);
272         }
273 }