void radioInit(enum radioMode_e mode) { int i; // Clock the radio and enable the radio SPI RFCON = 0x06; RFCTL = 0x10; //SPI enable @8MHz switch (mode) { case RADIO_MODE_PTX: // Energize the radio in PTX mode. Interrupts disable radioWriteReg(REG_CONFIG, 0x7E); break; case RADIO_MODE_PRX: // Energize the radio in PRX mode. Interrupts disable radioWriteReg(REG_CONFIG, 0x3F); break; } //Wait a little while for the radio to be rdy for(i=0;i<1000;i++); //Enable dynamic packet size, ack payload, and NOACK command radioWriteReg(REG_FEATURE, 0x07); radioWriteReg(REG_DYNPD, 0x01); //Set the default radio parameters radioUpdateRfSetup(); radioUpdateRetr(); }
void radioSetMode(enum radioMode_e mode) { switch (mode) { case RADIO_MODE_PTX: // Energize the radio in PTX mode. Interrupts disable radioWriteReg(REG_CONFIG, 0x7E); break; case RADIO_MODE_PRX: // Energize the radio in PRX mode. Interrupts disable radioWriteReg(REG_CONFIG, 0x7F); // start receiving RADIO_EN_CE(); break; } }
//Send a packet and receive the ACK //Return true in case of success. //Polling implementation unsigned char radioSendPacket(__xdata char *payload, char len, __xdata char *ackPayload, char *ackLen) { char status = 0; //Send the packet radioTxPacket(payload, len); //Wait for something to happen while(((status=radioNop())&0x70) == 0); // Clear the flags radioWriteReg(REG_STATUS, 0x70); //Return FALSE if the packet has not been transmited if (status&BIT_MAX_RT) { radioFlushTx(); return 0; } //Receive the ackPayload if any has been received if (status&BIT_RX_DR) *ackLen = radioRxPacket(ackPayload); else *ackLen = 0; radioFlushRx(); return status&BIT_TX_DS; }
void radioDeinit() { //Deenergise the radio radioWriteReg(REG_CONFIG, 0x00); //Unclock the radio and SPI RFCON = 0x00; //Radio unclocked RFCTL = 0x00; //SPI disable }
void radioUpdateRfSetup() { unsigned char setup=0; setup = setupDataRate[radioConf.dataRate]; setup |= radioConf.power<<1; if (radioConf.contCarrier) setup |= 0x90; radioWriteReg(REG_RF_SETUP, setup); }
//Set the radio channel. void radioSetChannel(char channel) { //Test the input if(channel<0 || channel>125) return; //Change the channel RADIO_DIS_CE(); radioWriteReg(REG_RF_CH, channel); //CE is continously activated if in continous carrier mode if(radioConf.contCarrier) RADIO_EN_CE(); }
//Raw registers update (for internal use) void radioUpdateRetr() { char ard=0; unsigned char nbytes; if (radioConf.ard & ARD_PLOAD) { nbytes = ((radioConf.ard&0x7F)>32)?32:(radioConf.ard&0x7F); for (ard=0; ardStep[radioConf.dataRate][ard]<nbytes; ard++) continue; } else ard = radioConf.ard & 0x0F; radioWriteReg(REG_SETUP_RETR, (ard<<4) | (radioConf.arc&0x0F)); }
int do_ra(CmdTableT *cmdtp, int argc, char *argv[]) { int len; switch (argc) { case 2: len = strlen(argv[1]); if (strncmp(argv[1], "reset", len) == 0) { printf("Reset radio\n"); radioReset(); } if (strncmp(argv[1], "dump", len) == 0) { radioDumpReg(); } break; case 3: len = strlen(argv[1]); if (strncmp(argv[1], "read", len) == 0) { uint8_t reg = strtoul(argv[2], NULL, 0); printf("Read radio reg %x: %x\n", reg, radioReadReg(reg)); } break; case 4: len = strlen(argv[1]); if (strncmp(argv[1], "read", len) == 0) { uint8_t reg = strtoul(argv[2], NULL, 0); uint8_t val = strtoul(argv[3], NULL, 0); radioWriteReg(reg, val); printf("Write radio reg %x: %x [%x]\n", reg, val, radioReadReg(reg)); } break; default: cmdUsage(cmdtp); break; } return 0; }