void Cfg::save(char* fname) { int i, v; const char **ip; const char **common_regnames_end; const char **common_regnames_start; RegOpMode_t test_regopmode; if (fname != NULL) { strncpy(path, fname, sizeof(path)); } f = fopen(path, "w"); if (f == NULL) { perror(path); return; } fprintf(f, "#Type Register Name Address[Hex] Value[Hex]\r\n"); test_regopmode.octet = radio_read(REG_OPMODE); if (test_regopmode.bits.LongRangeMode) common_regnames_start = common_regnames_start_lora; else common_regnames_start = common_regnames_start_fsk; for (i = 0; i < 0x0d; i++) { v = radio_read(i); if (i == REG_OPMODE) SX127x.RegOpMode.octet = v; fprintf(f, "REG\t%s\t0x%02x\t0x%02x\r\n", common_regnames_start[i], i, v); } if (SX127x.RegOpMode.bits.LongRangeMode) ip = &lora_regnames[0]; else ip = &fsk_regnames[0]; for (i = 0x0d; i < 0x40; i++) { v = radio_read(i); fprintf(f, "REG\t%s\t0x%02x\t0x%02x\r\n", ip[i-0x0d], i, v); } if (sx1276) common_regnames_end = common_regnames_end_1276; else common_regnames_end = common_regnames_end_1272; for (i = 0x40; i < 0x80; i++) { v = radio_read(i); fprintf(f, "REG\t%s\t0x%02x\t0x%02x\r\n", common_regnames_end[i-0x40], i, v); } fclose(f); }
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); } } }
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; }
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; }
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"); }
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); }
// Waits for the radio's version byte to have the expected value. // This verifies that the radio crystal oscillator and SPI interface are working. static int radio_check_version() { // It may take some time for the radio's oscillator to start up for (int i = 0; i < 10; ++i) { if (radio_read(VERSION) == 0x04) { return 1; } delay_ms(10); } return 0; }
int main() { int radio = config_port("/dev/ttyUSB0"); char buff = '\0'; char read_in[256]; int n = 0; getchar(); while (buff!='\n'){ buff = radio_read(radio); read_in[n] = buff; n++; } printf("%s", read_in); close(radio); return 0; }
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; } }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(cooja_radio_process, ev, data) { int len; char dupbuf[PACKETBUF_SIZE]; int duplen; PROCESS_BEGIN(); while(1) { PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL); packetbuf_clear(); len = radio_read(packetbuf_dataptr(), PACKETBUF_SIZE); memcpy(dupbuf, packetbuf_dataptr(), len); duplen = len; if(len > 0) { PRINTF("COOJA: receiving %d bytes @ %lu\n", len, clock_time()); packetbuf_set_datalen(len); NETSTACK_RDC.input(); } } PROCESS_END(); }
void radio_call(const char* number) { char buf[256]; bufferPrintf("radio: Setting up audio\r\n"); audiohw_switch_normal_call(TRUE); #ifdef CONFIG_3G radio_cmd("at+xdrv=0,8,0,0\r\n", 10); #else radio_cmd("at+xdrv=0,4\r\n", 10); radio_cmd("at+xdrv=0,20,0\r\n", 10); #endif // mute everything? radio_cmd("at+xdrv=0,1,0,0\r\n", 10); radio_cmd("at+xdrv=0,1,0,1\r\n", 10); radio_cmd("at+xdrv=0,1,0,2\r\n", 10); radio_cmd("at+xdrv=0,1,0,6\r\n", 10); // I really don't know radio_cmd("at+xdrv=0,24,1,1\r\n", 10); // note this is different from before radio_cmd("at+xdrv=0,0,1,1\r\n", 10); // microphone volume? radio_cmd("at+xdrv=0,1,100,1\r\n", 10); loudspeaker_vol(40); #ifdef CONFIG_3G radio_cmd("at+xdrv=0,8,1,0\r\n", 10); #endif speaker_vol(68); // clock // In general, lower is slower and higher is faster, but at some point it loops around. // This may mean the value is a bitset, e.g., at+xdrv=0,2,2,29 will set it to half speed radio_cmd("at+xdrv=0,2,2,10\r\n", 10); // channels? radio_cmd("at+xdrv=0,9,2\r\n", 10); // enable i2s? radio_cmd("at+xdrv=0,20,1\r\n", 10); // unmute? radio_cmd("at+xdrv=0,3,0\r\n", 10); // get notifications radio_cmd("at+xcallstat=1\r\n", 10); bufferPrintf("radio: Dialing\r\n"); sprintf(buf, "atd%s;\r\n", number); radio_cmd(buf, 10); radio_cmd("at+cmut=0\r\n", 10); #ifndef CONFIG_3G radio_cmd("at+xdrv=4,0,0,0,0,0\r\n", 10); speaker_vol(68); radio_cmd("at+xdrv=4,0,0,0,0,0\r\n", 10); #endif // we now need to wait for +XCALLSTAT to indicate 0 or active status. This code is less // complex than it seems. The whole point is just to wait until we have a line that says // +XCALLSTAT: *,0. That's it. while(TRUE) { buf[0] = '\0'; radio_read(buf, sizeof(buf)); char* pos = buf; int len = strlen(buf); int callstat = -1; while(len >= (sizeof("+XCALLSTAT: ") - 1)) { while(((int)(pos - buf)) <= (len - sizeof("+XCALLSTAT: ") + 1) && memcmp(pos, "+XCALLSTAT: ", sizeof("+XCALLSTAT: ") - 1) != 0) ++pos; if(memcmp(pos, "+XCALLSTAT: ", sizeof("+XCALLSTAT: ") - 1) != 0) break; while(*pos != ',') ++pos; ++pos; if(*pos == '0') { bufferPrintf("radio: Call answered\r\n"); callstat = 0; break; } ++pos; } if(callstat == 0) break; } #ifndef CONFIG_3G // do the rest radio_cmd("at+xdrv=4,0,0,0,0,0\r\n", 10); #endif // why the same thing again? radio_cmd("at+xdrv=0,4\r\n", 10); radio_cmd("at+xdrv=0,20,0\r\n", 10); radio_cmd("at+xcallstat=0\r\n", 10); }
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; }