// Set the channel fast and discard the first wrong readings void my_set_channel(int ch) { int k, temp; SPI_SETCHANNEL_SUPERFAST(357+((ch-11)*5)); // Approx. 292 us // Discard first 16 bad readings CC2420_SPI_ENABLE(); for (k=0; k<=16; k++) { MY_FASTSPI_GETRSSI(temp); } clock_delay(1); CC2420_SPI_DISABLE(); }
PROCESS_THREAD(scanning, ev, data) { PROCESS_BEGIN(); // Initial operations leds_off(LEDS_ALL); watchdog_stop(); // Avoiding wrong RSSI readings unsigned temp; CC2420_READ_REG(CC2420_AGCTST1, temp); CC2420_WRITE_REG(CC2420_AGCTST1, (temp + (1 << 8) + (1 << 13))); // Selecting the channel SPI_SETCHANNEL_SUPERFAST(357+((CHANNEL-11)*5)); // Avoiding the initial wrong readings by discarding the wrong readings CC2420_SPI_ENABLE(); unsigned long k=0; for (k=0; k<=15; k++) {MY_FASTSPI_GETRSSI(temp);} CC2420_SPI_DISABLE(); static struct etimer et; while(1){ #if VERBOSE printf("#START (dBm: occurrencies)\n"); #endif // Resetting everything for(k=0;k<BUFFER_SIZE;k++){ buffer0[k] = 0; } dint(); // Disable interrupts boost_cpu(); // Temporarily boost CPU speed CC2420_SPI_ENABLE(); // Enable SPI // Actual scanning static signed char rssi; for(k=0; k<MAX_VALUE; k++){ MY_FASTSPI_GETRSSI(rssi); buffer0[rssi+55]++; } CC2420_SPI_DISABLE(); // Disable SPI restore_cpu(); // Restore CPU speed eint(); // Re-enable interrupts // Printing the stored values in compressed form unsigned long sum_cca = 0; unsigned long max = 0, max_value = 0; for(temp=0; temp<BUFFER_SIZE; temp++) { sum_cca += (temp * buffer0[temp]); if(buffer0[temp] > max){ max = buffer0[temp]; max_value = temp; } } // Printing the results of the CCA float f_cca = (((float) sum_cca*1.0000) / MAX_VALUE)-100.0000; #if VERBOSE printf("Average noise: %ld.%04u\nStatistic Mode noise: %ld\n", (long) f_cca, (unsigned)((f_cca-floor(f_cca))*10000), max_value-100); #else printf("%ld.%04u\n", (long) f_cca, (unsigned)((f_cca-floor(f_cca))*10000)); #endif #if VERBOSE printf("#END\n"); #endif // Waiting for timer etimer_set(&et, PERIOD_TIME); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); } PROCESS_WAIT_EVENT(); PROCESS_END(); }
PROCESS_THREAD(scanning, ev, data) { PROCESS_BEGIN(); // Initial operations leds_off(LEDS_ALL); watchdog_stop(); // Avoiding wrong RSSI readings unsigned temp; CC2420_READ_REG(CC2420_AGCTST1, temp); CC2420_WRITE_REG(CC2420_AGCTST1, (temp + (1 << 8) + (1 << 13))); // Selecting the channel SPI_SETCHANNEL_SUPERFAST(357+((CHANNEL-11)*5)); // Avoiding the initial wrong readings by discarding the wrong readings CC2420_SPI_ENABLE(); int k=0; for (k=0; k<=15; k++) { MY_FASTSPI_GETRSSI(temp); } CC2420_SPI_DISABLE(); static struct etimer et; while(1) { // Resetting everything for(k=0; k<(BUFFER_SIZE/2); k++) { buffer1[k] = 0; buffer0[k] = 0; } printf("#START [dBm: occurrencies]\n"); dint(); // Disable interrupts boost_cpu(); // Temporarily boost CPU speed CC2420_SPI_ENABLE(); // Enable SPI // Actual scanning static signed char rssi; int current = 0; int previous = 0; int cnt = 1; for(k=0; k<(BUFFER_SIZE/2);) { // Sample the RSSI fast MY_FASTSPI_GETRSSI(rssi); current = rssi + 55; if((current == previous)&&(cnt<255)) { cnt++; } else { buffer0[k] = previous; buffer1[k++] = cnt; cnt = 1; previous = current; } } CC2420_SPI_DISABLE(); // Disable SPI restore_cpu(); // Restore CPU speed eint(); // Re-enable interrupts // Printing the stored values in compressed form for(temp=0; temp<(BUFFER_SIZE/2); temp++) { printf("%d: %d\n",buffer0[temp] - 100, buffer1[temp]); clock_delay(30000); } printf("#END\n"); // Waiting for timer etimer_set(&et, PERIOD_TIME); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); } PROCESS_WAIT_EVENT(); PROCESS_END(); }