int radio_init() { // Reset delay_ms(50); radio_command(SRES); delay_ms(50); if (!radio_check_version()) { failures |= Fail_Radio_Interface; return 0; } // Test GDO2 high radio_write(IOCFG2, 0x2f | GDOx_INVERT); if (!radio_gdo2()) { // Interrupt line is stuck low failures |= Fail_Radio_Int_Low; return 0; } // Test GDO2 low radio_write(IOCFG2, 0x2f); if (radio_gdo2()) { // Interrupt line is stuck high failures |= Fail_Radio_Int_High; return 0; } return 1; }
void Cfg::parseFile() { char line[128]; char* ret; RegOpMode_t test_regopmode; SX127x.RegOpMode.bits.Mode = RF_OPMODE_SLEEP; radio_write(REG_OPMODE, SX127x.RegOpMode.octet); do { test_regopmode.octet = radio_read(REG_OPMODE); } while (test_regopmode.bits.Mode != RF_OPMODE_SLEEP); while ((ret = fgets(line, sizeof(line), f)) != NULL) { char* tok = strtok(line, "\t"); if (!strcmp(tok, "REG")) { char *addr, *value; tok = strtok(NULL, "\t"); // reg name addr = strtok(NULL, "\t"); value = strtok(NULL, "\t"); if (addr != REG_FIFO) { int a, d, r; sscanf(addr, "%x", &a); sscanf(value, "%x", &d); if (a == REG_OPMODE) { RegOpMode_t new_regopmode; new_regopmode.octet = d; if (new_regopmode.bits.LongRangeMode != test_regopmode.bits.LongRangeMode) { new_regopmode.bits.Mode = RF_OPMODE_SLEEP; radio_write(REG_OPMODE, new_regopmode.octet); do { new_regopmode.octet = radio_read(REG_OPMODE); } while (new_regopmode.bits.Mode != RF_OPMODE_SLEEP); } } radio_write(a, d); r = radio_read(a); if (d != r) fprintf(stderr, "at %02x: wrote %02x, read back %02x\n", a, d, r); } } else if (!strcmp(tok, "PKT")) { tok = strtok(NULL, "\t"); // TODO } else if (!strcmp(tok, "XTAL")) { tok = strtok(NULL, "\t"); sscanf(tok, "%u", &xtal_hz); } else if (tok[0] == '#') { // ignore comment line } else { fprintf(stderr, "tok:\"%s\"\n", tok); tok = strtok(NULL, "\t"); fprintf(stderr, "2nd call:\"%s\"\n", tok); tok = strtok(NULL, "\t"); fprintf(stderr, "3rd call:\"%s\"\n", tok); } } }
int radio_cmd(const char* cmd, int tries) { int i; for(i = 0; i < tries; ++i) { char buf[200]; int n; radio_write(cmd); n = radio_read(buf, sizeof(buf) - 1); if(n == 0) continue; if(strstr(buf, "\nOK\r") != NULL) break; else if(strstr(buf, "\rOK\r") != NULL) break; } if(i == tries) return FALSE; else return TRUE; }
int radio_setup_2g() { gpio_pin_output(RADIO_GPIO_BB_MUX_SEL, OFF); gpio_pulldown_configure(RADIO_BB_PULLDOWN, GPIOPDDown); gpio_pin_output(RADIO_GPIO_BB_ON, OFF); udelay(100000); gpio_pin_output(RADIO_GPIO_RADIO_ON, ON); udelay(100000); gpio_pin_output(RADIO_GPIO_BB_RESET, ON); udelay(100000); gpio_pin_output(RADIO_GPIO_BB_RESET, OFF); udelay(100000); gpio_pin_use_as_input(RADIO_GPIO_BB_DETECT); if(gpio_pin_state(RADIO_GPIO_BB_DETECT) != 0) { bufferPrintf("radio: comm board not present, powered on, or at+xdrv=10,2 had been issued.\r\n"); return -1; } bufferPrintf("radio: comm board detected.\r\n"); if(!radio_wait_for_ok(10)) { bufferPrintf("radio: no response from baseband!\r\n"); return -1; } bufferPrintf("radio: setting speed to 750000 baud.\r\n"); radio_write("at+ipr=750000\r\n"); // wait a millisecond for the command to totally clear uart // I wasn't able to detect this condition with uart registers (looking at FIFO count, transmitter empty) udelay(1000); uart_set_baud_rate(RADIO_UART, 750000); if(!radio_wait_for_ok(10)) { bufferPrintf("radio: no response from baseband!\r\n"); return -1; } RadioAvailable = TRUE; bufferPrintf("radio: ready.\r\n"); speaker_setup(); return 0; }
void vibrator_off() { char buf[100]; // write the command radio_write("at+xdrv=4,0,0,0,0,0\r\n"); // clear the response radio_read(buf, sizeof(buf)); }
void vibrator_once(int frequency, int time) { char buf[100]; sprintf(buf, "at+xdrv=4,0,1,%d,%d,%d\r\n", frequency, time + 1, time); // write the command radio_write(buf); // clear the response radio_read(buf, sizeof(buf)); }
void vibrator_loop(int frequency, int period, int timeOn) { char buf[100]; sprintf(buf, "at+xdrv=4,0,2,%d,%d,%d\r\n", frequency, period, timeOn); // write the command radio_write(buf); // clear the response radio_read(buf, sizeof(buf)); }
void cmd_radio_send(int argc, char** argv) { if(argc < 2) { bufferPrintf("Usage: %s <command>\r\n", argv[0]); return; } radio_write(argv[1]); radio_write("\r\n"); char buf[100]; int c = radio_read(buf, sizeof(buf)); printf("radio reply: %s", buf); while(c == (sizeof(buf) - 1)) { c = radio_read(buf, sizeof(buf)); printf("%s", buf); } printf("\n"); }
static int rx_finished() { radio_write(FSCTRL0, radio_read(FREQEST)); uint8_t bytes = radio_read(RXBYTES); if (bytes < 3) { // Bad CRC, so the packet was flushed (or the radio got misconfigured). radio_command(SFRX); radio_command(SRX); return 0; } // Read the packet from the radio radio_select(); spi_xfer(RXFIFO | CC_READ | CC_BURST); radio_rx_len = spi_xfer(SNOP); if (radio_rx_len > sizeof(radio_rx_buf)) { // Either PKTLEN in the radio configuration is wrong or we lost data in the FIFO and this wasn't really a length byte. radio_deselect(); radio_command(SFRX); radio_command(SRX); radio_rx_len = 0; return 0; } for (int i = 0; i < radio_rx_len; ++i) { radio_rx_buf[i] = spi_xfer(SNOP); } // Read status bytes last_rssi = (int8_t)spi_xfer(SNOP); uint8_t status = spi_xfer(SNOP); radio_deselect(); if (!(status & 0x80)) { // Bad CRC // // Autoflush is supposed to be on so this should never happen. // If we get here and autoflush is on, this means some bytes have been lost // and the status byte isn't really the status byte. radio_command(SFRX); radio_command(SRX); return 0; } return 1; }
void cmd_radio_send(int argc, char** argv) { if(argc < 2) { bufferPrintf("Usage: %s <command>\r\n", argv[0]); return; } radio_write(argv[1]); radio_write("\r\n"); char* buf = malloc(0x1000); int c = radio_read(buf, 0x1000); printf("radio reply: %s", buf); while(c == (0x1000 - 1)) { c = radio_read(buf, 0x1000); printf("%s", buf); } printf("\n"); free(buf); }
void radio_configure() { radio_in_tx = 0; radio_command(SIDLE); radio_select(); for (int i = 0; i < sizeof(cc1101_regs); ++i) { spi_xfer(cc1101_regs[i]); } radio_deselect(); radio_write(IOCFG2, 6 | GDOx_INVERT); radio_channel(current_channel); radio_command(SFRX); radio_command(SRX); }
int radio_register(int timeout) { char buf[256]; // enable auto registration radio_cmd("at+cops=0\r\n", 10); uint64_t startTime = timer_get_system_microtime(); while(TRUE) { if(has_elapsed(startTime, timeout * 1000)) return -1; char* pos; radio_write("at+cops?\r\n"); radio_read(buf, sizeof(buf)); pos = buf; while(memcmp(pos, "+COPS: ", sizeof("+COPS: ") - 1) != 0) ++pos; if(pos[7] != '0' || pos[8] != ',') { radio_cmd("at+cops=0\r\n", 10); continue; } char* name = &pos[12]; char* lastQuote = name; while(*lastQuote != '\"') ++lastQuote; *lastQuote = '\0'; bufferPrintf("radio: Registered with %s\r\n", name); return 0; } }
static int radio_nvram_read_idx(int idx, char** res) { char cmd[20]; char* curBuf; char* resultStart; int curBufSize; int curPos; int c; int searchLen; sprintf(cmd, "at+xdrv=9,1,%d\r\n", idx); radio_write(cmd); curPos = 0; curBufSize = 100; curBuf = malloc(curBufSize); curPos = radio_read(curBuf, curBufSize); while(curPos == (curBufSize - 1)) { curBufSize += 100; curBuf = realloc(curBuf, curBufSize); c = radio_read(curBuf + curPos, curBufSize - curPos); curPos += c; } sprintf(cmd, "+XDRV: 9,1,0,%d,", idx); searchLen = strlen(cmd); resultStart = curBuf; while((resultStart - curBuf) <= (curPos - searchLen) && memcmp(resultStart, cmd, searchLen) != 0) ++resultStart; if(memcmp(resultStart, cmd, searchLen) != 0) { free(curBuf); return 0; } resultStart += searchLen; if(memcmp(resultStart, "NULL", sizeof("NULL")) == 0) { free(curBuf); return 0; } c = 0; while(*resultStart != '\r' && *resultStart != '\n' && *resultStart != '\0') { cmd[0] = resultStart[0]; cmd[1] = resultStart[1]; cmd[2] = '\0'; curBuf[c++] = strtoul(cmd, NULL, 16); resultStart += 2; } *res = curBuf; return c; }
void radio_channel(int n) { current_channel = n; radio_write(CHANNR, n); }