2 #include "../firmware.h"
5 #include "driverlib/interrupt.h"
6 #include "driverlib/systick.h"
7 #include "driverlib/sysctl.h"
16 #define UART UART_CAMERA
18 void cw_tone_option(
void);
19 void tx_packets_option(
void);
20 uint8_t ui_set_sym_dev(uint8_t radio_id);
21 uint8_t ui_set_freq_power(uint8_t radio_id);
22 uint8_t ui_set_power(uint8_t radio_id);
23 uint8_t ui_set_freq(uint8_t radio_id);
24 uint8_t ui_set_rxbw(uint8_t radio_id);
25 char wait_for_response_char(
void);
26 char peek_for_response_char(
void);
27 void rx_packets_option(
void);
28 uint32_t ui_set_packet_len(uint32_t max_len, uint32_t min_len);
29 uint16_t ui_get_packet_wait(
void);
30 void tx_packets_pwrsweep_option(
void);
31 void rx_packets_stats_option(
void);
33 uint16_t wait_for_response_ln(
void);
34 double current_pwr = -500;
35 uint8_t current_pwr_reg = 0;
36 double current_freq = 0;
38 #define UART_BUFF_LEN 100
39 char uart_in_buff[UART_BUFF_LEN] = {0};
40 char uart_out_buff[UART_BUFF_LEN] = {0};
73 char res = wait_for_response_char();
86 tx_packets_pwrsweep_option();
89 rx_packets_stats_option();
107 char peek_for_response_char(
void){
115 char wait_for_response_char(
void){
122 uint16_t wait_for_response_ln(
void){
127 while((p < (UART_BUFF_LEN-1)) && (ln == 0)){
130 if ((uart_in_buff[p] ==
'\n') || (uart_in_buff[p] ==
'\r'))
135 uart_in_buff[p] =
'\0';
139 uint8_t ui_set_sym_dev(uint8_t radio_id){
140 uint32_t symrate, deviation;
145 UART_puts(
UART,
"Enter symbol rate (/sec, default: 1000 /sec): ");
146 wait_for_response_ln();
147 symrate = (uint32_t)atoi(uart_in_buff);
152 UART_puts(
UART,
"\nEnter deviation (Hz, default: 2000 Hz): ");
153 wait_for_response_ln();
154 deviation = (uint32_t)atoi(uart_in_buff);
159 r = radio_set_fsk_param(radio_id, &symrate, &deviation);
160 snprintf(uart_out_buff, UART_BUFF_LEN,
"%li, %li\n", symrate, deviation);
162 UART_puts(
UART,
"\nError in symbol rate, deviation settings entered: ");
166 UART_puts(
UART,
"\nSetting symbol rate, deviation settings to ");
172 uint8_t ui_set_freq_power(uint8_t radio_id){
174 if(ui_set_freq(radio_id))
177 if(ui_set_power(radio_id))
182 uint8_t ui_set_power(uint8_t radio_id){
190 wait_for_response_ln();
191 pwr = atof(uart_in_buff);
192 r = radio_set_pwr_f(radio_id, &pwr, &pwr_reg);
193 snprintf(uart_out_buff, UART_BUFF_LEN,
"%2.1f dBm (reg = %i)\n", pwr, pwr_reg);
202 current_pwr_reg = pwr_reg;
205 uint8_t ui_set_freq(uint8_t radio_id){
212 wait_for_response_ln();
213 freq = atof(uart_in_buff);
214 r = radio_set_freq_f(radio_id, &freq);
215 snprintf(uart_out_buff, UART_BUFF_LEN,
"%3.3f MHz\n", freq);
229 uint32_t ui_set_packet_len(uint32_t max_len, uint32_t min_len){
230 UART_puts(
UART,
"\nEnter packet length (default: 20 bytes): ");
231 wait_for_response_ln();
232 int32_t len = atoi(uart_in_buff);
235 snprintf(uart_out_buff, UART_BUFF_LEN,
"%li bytes\n", len);
236 if ((len > (int32_t)max_len) || (len < (int32_t)min_len)){
243 return (uint32_t)len;
246 uint16_t ui_get_packet_wait(
void){
247 UART_puts(
UART,
"\nEnter packet wait period (default: 300 ms): ");
248 wait_for_response_ln();
249 int32_t p = atoi(uart_in_buff);
252 snprintf(uart_out_buff, UART_BUFF_LEN,
"%li ms\n", p);
258 uint8_t ui_set_rxbw(uint8_t radio_id){
263 UART_puts(
UART,
"Enter RX bandwidth (Hz, default: 8000 Hz): ");
264 wait_for_response_ln();
265 rxbw = (uint32_t)atoi(uart_in_buff);
269 r = radio_set_rxbw_param(radio_id, &rxbw);
270 snprintf(uart_out_buff, UART_BUFF_LEN,
"%li\n", rxbw);
282 void rx_packets_stats_option(
void){
286 radio_reset_config(SPI_RADIO_RX, preferredSettings_fsk,
sizeof(preferredSettings_fsk)/
sizeof(
registerSetting_t));
288 if (ui_set_freq(SPI_RADIO_RX))
291 if (ui_set_sym_dev(SPI_RADIO_RX))
295 if (ui_set_rxbw(SPI_RADIO_RX))
298 manualCalibration(SPI_RADIO_RX);
300 uint8_t num_bytes, marcState;
303 SPI_cmd(SPI_RADIO_RX, CC112X_SRX);
305 #define CONFIG_CNT 200
306 uint16_t config_list[CONFIG_CNT] = {0xFFFF};
307 uint32_t last_id_list[CONFIG_CNT] = {0};
308 uint32_t err_cnt_list[CONFIG_CNT] = {0};
309 uint32_t suc_cnt_list[CONFIG_CNT] = {0};
310 uint16_t list_top = 0;
311 uint32_t lowest_id = 0xFFFFFFFF;
318 while(cc1125_pollGPIO(GPIO0_RADIO_RX) == 0){
319 c = peek_for_response_char();
325 while(cc1125_pollGPIO(GPIO0_RADIO_RX) > 0){
326 c = peek_for_response_char();
335 lowest_id = 0xFFFFFFFF;
345 cc112xSpiReadReg(SPI_RADIO_RX, CC112X_NUM_RXBYTES, &num_bytes);
349 cc112xSpiReadReg(SPI_RADIO_RX, CC112X_MARCSTATE, &marcState);
350 if((marcState & 0x1F) == 0x11) {
353 SPI_cmd(SPI_RADIO_RX, CC112X_SFRX);
357 cc112xSpiReadRxFifo(SPI_RADIO_RX, rxBuff, num_bytes);
365 packetid = (uint32_t)(rxBuff[2] | (rxBuff[3] << 8) | (rxBuff[4] << 16) | (rxBuff[5] << 24));
367 crc = rxBuff[num_bytes - 1] & 0x80;
370 if (lowest_id == 0xFFFFFFFF)
371 lowest_id = packetid;
376 for (i = 0; i < list_top; i++){
377 if (config_list[i] == cfg){
383 if (!(list_top >= CONFIG_CNT)){
386 config_list[i] = cfg;
387 last_id_list[i] = lowest_id;
395 if (found && (packetid > lowest_id)){
396 uint32_t missing = packetid - last_id_list[i] - 1;
397 last_id_list[i] = packetid;
398 err_cnt_list[i] += missing;
400 suc_cnt_list[i] += 1;
402 err_cnt_list[i] += 1;
403 snprintf(uart_out_buff, UART_BUFF_LEN,
"id: %li, cfg: %i, crc?: %i ", packetid,cfg,crc);
405 snprintf(uart_out_buff, UART_BUFF_LEN,
"tot pass: %li, tot fail: %li\n", suc_cnt_list[i], err_cnt_list[i]);
413 UART_puts(
UART,
"\nError, Pin asserted, but no bytes in FIFO! :/\n");
415 SPI_cmd(SPI_RADIO_RX, CC112X_SRX);
420 void rx_packets_option(
void){
424 radio_reset_config(SPI_RADIO_RX, preferredSettings_fsk,
sizeof(preferredSettings_fsk)/
sizeof(
registerSetting_t));
426 if (ui_set_freq(SPI_RADIO_RX))
429 if (ui_set_sym_dev(SPI_RADIO_RX))
433 if (ui_set_rxbw(SPI_RADIO_RX))
436 manualCalibration(SPI_RADIO_RX);
438 uint8_t num_bytes, marcState;
442 SPI_cmd(SPI_RADIO_RX, CC112X_SRX);
447 while(cc1125_pollGPIO(GPIO0_RADIO_RX) == 0){};
448 while(cc1125_pollGPIO(GPIO0_RADIO_RX) > 0){};
454 cc112xSpiReadReg(SPI_RADIO_RX, CC112X_NUM_RXBYTES, &num_bytes);
458 cc112xSpiReadReg(SPI_RADIO_RX, CC112X_MARCSTATE, &marcState);
459 if((marcState & 0x1F) == 0x11) {
462 SPI_cmd(SPI_RADIO_RX, CC112X_SFRX);
466 cc112xSpiReadRxFifo(SPI_RADIO_RX, rxBuff, num_bytes);
472 if(rxBuff[num_bytes - 1] & 0x80) {
473 snprintf(uart_out_buff, UART_BUFF_LEN,
"Rx OK (%d bytes) - ", num_bytes);
476 snprintf(uart_out_buff, UART_BUFF_LEN,
"CRC ERR (%d bytes) - ", num_bytes);
480 for (i = 0; i < num_bytes; i++){
481 snprintf(uart_out_buff, UART_BUFF_LEN,
"%02x ", rxBuff[i]);
485 for (i = 0; i < num_bytes; i++){
496 UART_puts(
UART,
"\nError, Pin asserted, but no bytes in FIFO! :/\n");
498 SPI_cmd(SPI_RADIO_RX, CC112X_SRX);
504 void tx_packets_option(
void){
509 radio_reset_config(SPI_RADIO_TX, preferredSettings_fsk,
sizeof(preferredSettings_fsk)/
sizeof(
registerSetting_t));
511 if (ui_set_freq_power(SPI_RADIO_TX))
514 if (ui_set_sym_dev(SPI_RADIO_TX))
518 uint32_t len = ui_set_packet_len(256-3, 6);
522 uint16_t wait = ui_get_packet_wait();
527 UART_puts(
UART,
"w/s - Increase/decrease power in ~0.5dB increments\n");
528 UART_puts(
UART,
"a/d - Increase/decrease frequency in 500Hz increments\n\n");
530 manualCalibration(SPI_RADIO_TX);
535 buff[0] = (uint8_t)len;
536 buff[1] = current_pwr_reg;
537 buff[2] = (uint8_t)(cnt & 0xFF);
538 buff[3] = (uint8_t)((cnt>>8) & 0xFF);
539 buff[4] = (uint8_t)((cnt>>16) & 0xFF);
540 buff[5] = (uint8_t)((cnt>>24) & 0xFF);
541 for (i = 6; i < len+1; i++)
545 cc112xSpiWriteTxFifo(SPI_RADIO_TX, buff, (uint8_t)(len+1));
547 SPI_cmd(SPI_RADIO_TX, CC112X_STX);
553 while( cc1125_pollGPIO(GPIO0_RADIO_TX)) {} ;
556 for(w2 = 0; w2 < wait; w2++){
557 for(w1 = 0; w1 < 1000; w1++) {};
560 c = peek_for_response_char();
562 SPI_cmd(SPI_RADIO_TX, CC112X_SIDLE);
563 if (radio_set_pwr_reg(SPI_RADIO_TX, (uint8_t)(current_pwr_reg+1))==0){
565 snprintf(uart_out_buff, UART_BUFF_LEN,
"Power register set to: %i (~%2.1f dBm)\n", current_pwr_reg, radio_pwr_reg_to_dbm(current_pwr_reg));
570 SPI_cmd(SPI_RADIO_TX, CC112X_SIDLE);
571 if (radio_set_pwr_reg(SPI_RADIO_TX, (uint8_t)(current_pwr_reg-1))==0){
573 snprintf(uart_out_buff, UART_BUFF_LEN,
"Power register set to: %i (~%2.1f dBm)\n", current_pwr_reg, radio_pwr_reg_to_dbm(current_pwr_reg));
578 SPI_cmd(SPI_RADIO_TX, CC112X_SIDLE);
579 current_freq += 0.0005;
580 radio_set_freq_f(SPI_RADIO_TX, ¤t_freq);
581 snprintf(uart_out_buff, UART_BUFF_LEN,
"Frequency set to: %3.4f MHz\n", current_freq);
585 SPI_cmd(SPI_RADIO_TX, CC112X_SIDLE);
586 current_freq -= 0.0005;
587 radio_set_freq_f(SPI_RADIO_TX, ¤t_freq);
588 snprintf(uart_out_buff, UART_BUFF_LEN,
"Frequency set to: %3.4f MHz\n", current_freq);
603 void tx_packets_pwrsweep_option(
void){
608 radio_reset_config(SPI_RADIO_TX, preferredSettings_fsk,
sizeof(preferredSettings_fsk)/
sizeof(
registerSetting_t));
610 if (ui_set_freq(SPI_RADIO_TX))
613 if (ui_set_sym_dev(SPI_RADIO_TX))
616 uint32_t len = ui_set_packet_len(256-3, 6);
620 uint16_t wait = ui_get_packet_wait();
628 for (pwr_reg = 3; pwr_reg < 0x3F; pwr_reg++){
629 writebyte = (uint8_t)((1<<6) | pwr_reg);
630 cc112xSpiWriteReg(SPI_RADIO_TX, CC112X_PA_CFG2, &writebyte);
632 manualCalibration(SPI_RADIO_TX);
637 buff[0] = (uint8_t)len;
639 buff[2] = (uint8_t)(cnt & 0xFF);
640 buff[3] = (uint8_t)((cnt>>8) & 0xFF);
641 buff[4] = (uint8_t)((cnt>>16) & 0xFF);
642 buff[5] = (uint8_t)((cnt>>24) & 0xFF);
643 for (i = 6; i < len+1; i++)
647 cc112xSpiWriteTxFifo(SPI_RADIO_TX, buff, (uint8_t)(len+1));
649 SPI_cmd(SPI_RADIO_TX, CC112X_STX);
655 while( cc1125_pollGPIO(GPIO0_RADIO_TX)) {} ;
658 for(w2 = 0; w2 < wait; w2++){
659 for(w1 = 0; w1 < 1000; w1++) {};
670 void cw_tone_option(
void){
675 radio_reset_config(SPI_RADIO_TX, preferredSettings_cw,
sizeof(preferredSettings_cw)/
sizeof(
registerSetting_t));
676 manualCalibration(SPI_RADIO_TX);
678 if (ui_set_freq_power(SPI_RADIO_TX))
683 SPI_cmd(SPI_RADIO_TX, CC112X_STX);
685 while(wait_for_response_char() !=
'q'){};
686 radio_reset_config(SPI_RADIO_TX, preferredSettings_cw,
sizeof(preferredSettings_cw)/
sizeof(
registerSetting_t));
void UART_init(uint8_t uart_num, uint32_t baudrate)
char UART_getc(uint8_t uart_num)
int main(void)
Runs all module tests.
bool UART_charsAvail(uint8_t uart_num)
void UART_puts(uint8_t uart_num, char *str)
uint8_t SPI_cmd(uint8_t spi_num, uint8_t cmd)
void UART_putc(uint8_t uart_num, char c)