Пример #1
0
// read various radio status and information registers
void radio_debug(void)
{
	radio_get_int_status(0, 0, 0);
	radio_get_chip_status(0);
	radio_get_modem_status(0);
	radio_part_info();
	radio_func_info();
	radio_request_device_state();
}
Пример #2
0
int main(void)
{
	// configure WDT
	WDTCTL = WDTPW | WDTHOLD;				// stop watch dog timer
	_25mhz();


#ifdef TEST
	// when compiled in test mode, use different main
	// disconnect radio when testing to avoid damage!
	test_main();
#endif

	// configure LED1 and turn it off, we'll use that for error and other stuff
	P1DIR |= LED1;
	LED1_OFF;

	P4DIR |= LED2;
	LED2_ON;

	// setup uart
	uart_init();

#ifdef DEBUG_MESSAGES
	uart_send_string("Hola mundo!\r\n");
#endif

	// setup packet handler
	ph_setup();

	// setup an configure radio
	radio_setup();
	radio_configure();

	// self-calibrate image rejection
	radio_calibrate_ir();

	// verify that radio configuration was successful
	radio_get_chip_status(0);
	if (radio_buffer.chip_status.chip_status & RADIO_CMD_ERROR) {	// check for command error
		uart_send_string("Error inicializando radio!!!\r\n");
		while (1) {
			LED1_TOGGLE;
			_delay_cycles(8000000);			// blink LED if there was an error
		}
	}


	// start packet receiving
	ph_start();

#ifdef DEBUG_MESSAGES
	uart_send_string("dAISy 0.2 started\r\n");
	LED2_OFF;
#endif

	while (1) {

		LPM0;	// deep sleep until something worthwhile happens

		__no_operation();

		ph_loop();	// packet handler house-keeping, e.g. channel hopping

#ifdef DEBUG_MESSAGES
		uint8_t channel;
		int16_t rssi;
		// debug code to monitor signal strength (RSSI)
		if (ph_get_state() == PH_STATE_PREFETCH) {											// found preamble and start flag
			// record current channel and signal strength
			channel = ph_get_radio_channel();												// read current channel
			rssi = ph_get_radio_rssi();														// read current RSSI
		}
#endif

		// retrieve last packet handler error
		uint8_t error = ph_get_last_error();
#ifdef DEBUG_MESSAGES
		// report error if packet handler failed
		if (error != PH_ERROR_NONE)	{
			dec_to_str(str_output_buffer, 3, rssi);											// convert to decimal string (reuse radio buffer)
			str_output_buffer[4] = 0;														// terminate string
			uart_send_string("sync ");														// send debug message to UART
			uart_send_byte(channel + 'A');
			uart_send_string(" RSSI=");
			uart_send_string(str_output_buffer);
			uart_send_string("dBm\r\n");
			uart_send_string("error: ");
			switch (error) {
			case PH_ERROR_NOEND:
				uart_send_string("no end flag");
				break;
			case PH_ERROR_STUFFBIT:
				uart_send_string("invalid stuff bit");
				break;
			case PH_ERROR_CRC:
				uart_send_string("CRC error");
				break;
			case PH_ERROR_RSSI_DROP:
				uart_send_string("RSSI drop");
				break;
			}
			uart_send_string("\r\n");
			ph_loop();							// house keeping, sending over UART takes time
		}
#else
		// toggle LED if packet handler failed after finding preamble and start flag
		if (error == PH_ERROR_NOEND || error == PH_ERROR_STUFFBIT || error == PH_ERROR_CRC)
			LED1_TOGGLE;
#endif

		// check if a new valid packet arrived
		uint16_t size = fifo_get_packet();
		if (size > 0) {								// if so, process packet

#ifdef DEBUG_MESSAGES
			dec_to_str(str_output_buffer, 3, rssi);											// convert to decimal string (reuse radio buffer)
			str_output_buffer[4] = 0;														// terminate string
			uart_send_string("sync ");														// send debug message to UART
			uart_send_byte(channel + 'A');
			uart_send_string(" RSSI=");
			uart_send_string(str_output_buffer);
			uart_send_string("dBm\r\n");
#endif
			LED2_ON;
			nmea_process_packet();					// process packet (NMEA message will be sent over UART)
			fifo_remove_packet();					// remove processed packet from FIFO
			LED2_OFF;
		}

		// enter low power mode LPM0 (everything off)
		// TODO: wait for UART to complete transmission

		// TODO: suspend UART
	}
}