00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <aversive.h>
00025 #include <aversive/pgmspace.h>
00026 #include <aversive/error.h>
00027 #include <aversive/errno.h>
00028 #include <aversive/wait.h>
00029 #include <avr/interrupt.h>
00030
00031 #include <stdio.h>
00032 #include <string.h>
00033 #include <math.h>
00034
00035 #include <uart.h>
00036 #include <spi.h>
00037 #include <cc2420.h>
00038 #include <cc2420_arch.h>
00039 #include <time_ext.h>
00040
00041
00042 #include "firefly2_2.h"
00043
00044
00045 uint8_t i;
00046
00047
00048
00049
00050
00051
00052 ISR(__vector_default)
00053 {
00054
00055 EMERG(EFAULT, "Bad interrupt vector !");
00056
00057
00058 while(TRUE)
00059 {
00060 LED_SET(LED_BLUE);
00061 LED_SET(LED_GREEN);
00062 LED_SET(LED_ORANGE);
00063 LED_SET(LED_RED);
00064 wait_ms(100);
00065
00066 LED_CLR(LED_BLUE);
00067 LED_CLR(LED_RED);
00068 LED_CLR(LED_ORANGE);
00069 LED_CLR(LED_GREEN);
00070 wait_ms(100);
00071 }
00072 }
00073
00074
00075
00076
00077
00078 void mylog(struct error * e, ...)
00079 {
00080 char *severity;
00081 char *module;
00082 va_list ap;
00083 uint16_t stream_flags = stdout->flags;
00084 volatile time_ext_t time = time_ext_get();
00085
00086
00087 switch(e->severity)
00088 {
00089 case ERROR_SEVERITY_EMERG: severity = "[EMERG]"; break;
00090 case ERROR_SEVERITY_ERROR: severity = "[ERROR]"; break;
00091 case ERROR_SEVERITY_WARNING: severity = "[WARNING]"; break;
00092 case ERROR_SEVERITY_NOTICE: severity = "[NOTICE]"; break;
00093 case ERROR_SEVERITY_DEBUG: severity = "[DEBUG]"; break;
00094 default: severity = "[DEFAULT]"; break;
00095 }
00096
00097 switch(e->err_num)
00098 {
00099 case E_SPI: module = "[SPI] "; break;
00100 case E_CC2420: module = "[CC2420]"; break;
00101 case E_TIME_EXT: module = "[TIME] "; break;
00102 default: module = ""; break;
00103 }
00104
00105 va_start(ap, e);
00106
00107
00108 printf_P(PSTR("(%lu %lu) "), time.sec, time.nano);
00109 printf_P(PSTR("%s"), severity);
00110 printf_P(PSTR("%s "), module);
00111 vfprintf_P(stdout, e->text, ap);
00112 printf_P(PSTR("\r\n"));
00113 va_end(ap);
00114 stdout->flags = stream_flags;
00115 }
00116
00117
00118 void init_ports(void)
00119 {
00120
00121 MCUCR |= (1<<PUD);
00122
00123
00124 LED_DDR = _BV(LED_ORANGE) | _BV(LED_BLUE) | _BV(LED_GREEN) | _BV(LED_RED);
00125
00126
00127 cbi(BUTTON_DDR, BUTTON_PIN);
00128 }
00129
00130
00131 int main(void)
00132 {
00133 uint16_t reg;
00134 uint8_t buffer[368];
00135 uint16_t i;
00136 uint8_t j;
00137
00138
00139 time_ext_init();
00140 time_ext_set(0, 0);
00141
00142
00143
00144 init_ports();
00145
00146
00147 uart_init();
00148 fdevopen(uart1_dev_send, uart1_dev_recv);
00149
00150
00151 error_register_emerg(mylog);
00152 error_register_error(mylog);
00153 error_register_warning(mylog);
00154 error_register_notice(mylog);
00155 error_register_debug(mylog);
00156
00157 NOTICE(ESUCCESS, "\r\n\r\n\r\n\r\n\r\n\r\n **************************************");
00158 NOTICE(ESUCCESS, "MCUSR = 0x%x", MCUSR);
00159
00160
00161
00162 LED_CLR(LED_GREEN);
00163 LED_CLR(LED_ORANGE);
00164 LED_CLR(LED_RED);
00165 LED_CLR(LED_BLUE);
00166
00167
00168 LED_SET(LED_ORANGE);
00169
00170 cc2420_init();
00171
00172
00173 NOTICE(ESUCCESS, "Ready !");
00174
00175
00176
00177
00178 reg = cc2420_read_register(MAIN);
00179 if(reg == 0xF800)
00180 NOTICE(ESUCCESS, "MAIN read [OK]");
00181 else
00182 ERROR(42, "MAIN should be 0xF800, read 0x%x", reg);
00183
00184
00185 NOTICE(ESUCCESS, "MANFIDH = 0x%x", cc2420_read_register(MANFIDH));
00186 NOTICE(ESUCCESS, "MANFIDL = 0x%x", cc2420_read_register(MANFIDL));
00187 NOTICE(ESUCCESS, "SYNCWORD = 0x%x", cc2420_read_register(SYNCWORD));
00188 cc2420_write_register(SYNCWORD, 0xdead);
00189 NOTICE(ESUCCESS, "SYNCWORD = 0x%x", cc2420_read_register(SYNCWORD));
00190 NOTICE(ESUCCESS, "STAT = 0x%x", cc2420_get_status());
00191
00192
00193 uint8_t tmp = 0x42;
00194 cc2420_read_ram(RAM_IEEEADR, &tmp, 1);
00195 NOTICE(ESUCCESS, "RAM_IEEEADR = 0x%x", tmp);
00196 NOTICE(ESUCCESS, "STAT = 0x%x", cc2420_get_status());
00197 tmp = 0x42;
00198 cc2420_write_ram(RAM_IEEEADR, &tmp, 1);
00199 NOTICE(ESUCCESS, "STAT = 0x%x", cc2420_get_status());
00200 cc2420_read_ram(RAM_IEEEADR, &tmp, 1);
00201 NOTICE(ESUCCESS, "RAM_IEEEADR = 0x%x", tmp);
00202 NOTICE(ESUCCESS, "STAT = 0x%x", cc2420_get_status());
00203
00204
00205
00206
00207 for(i = 0; i < 128; i++)
00208 buffer[i] = i;
00209 NOTICE(ESUCCESS, "Writing to RXFIFO...");
00210 cc2420_write_rxfifo(buffer, 128);
00211
00212 NOTICE(ESUCCESS, "Reading RXFIFO...");
00213 cc2420_read_rxfifo(buffer, 128);
00214
00215
00216 NOTICE(ESUCCESS, "RAM content after RXFIFO write");
00217 for(i = 0; i < 23; i++)
00218 {
00219 printf("0x%x \t| ", i*16);
00220 for(j = 0; j < 16; j++)
00221 printf("%x ", buffer[i*16+j]);
00222 printf("\r\n");
00223 }
00224
00225
00226
00227 for(i = 0; i < 128; i++)
00228 buffer[i] = i;
00229 NOTICE(ESUCCESS, "Writing to RAM...");
00230 cc2420_write_ram(RAM_TXFIFO, buffer, 128);
00231
00232 NOTICE(ESUCCESS, "Reading RAM...");
00233 cc2420_read_ram(RAM_TXFIFO, buffer, 368);
00234
00235
00236 NOTICE(ESUCCESS, "RAM content after write to RAM");
00237 for(i = 0; i < 23; i++)
00238 {
00239 printf("0x%x \t| ", i*16);
00240 for(j = 0; j < 16; j++)
00241 printf("%x ", buffer[i*16+j]);
00242 printf("\r\n");
00243 }
00244
00245
00246
00247
00248
00249
00250 NOTICE(ESUCCESS, "&PORTC = 0x%x | PORTC = 0x%x | *(&PORTC) = 0x%x",
00251 &PORTC, PORTC, *(&PORTC));
00252 spi_display_ss_lines();
00253
00254
00255
00256 while(1)
00257 {
00258 if(!TST(BUTTON_PORT, BUTTON_PIN))
00259 {
00260 time_ext_t local_time = time_ext_get();
00261 LED_SET(LED_BLUE);
00262 NOTICE(ESUCCESS, "Button event at %ld %ld",
00263 local_time.sec, local_time.nano);
00264 NOTICE(ESUCCESS, "STAT = 0x%x", cc2420_get_status());
00265 NOTICE(ESUCCESS, "DDRC = 0x%x | PORTC = 0x%x", DDRC, PORTC);
00266 while(!TST(BUTTON_PORT, BUTTON_PIN))
00267 ;
00268 NOTICE(ESUCCESS, "STAT = 0x%x", cc2420_get_status());
00269 }
00270 else
00271 LED_CLR(LED_BLUE);
00272 }
00273 }