wayne roberts / Mbed 2 deprecated hid_test

Dependencies:   SX127x USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "USBHID.h"
00003 #include "sx127x.h"
00004 
00005 //#define _DEBUG_ 
00006 
00007 //           mosi, miso, sclk,   cs,  rst,  dio0, dio1
00008 SX127x radio(D11,   D12, D13,    D10,  A0,   D2,   D3); // sx1276 arduino shield
00009 #ifdef _DEBUG_
00010     #include "sx127x_lora.h"
00011     SX127x_lora lora(radio);
00012 #endif /* _DEBUG_ */
00013 
00014 DigitalInOut rfsw(A4);
00015 //We declare a USBHID device. By default input and output reports are 64 bytes long.
00016 USBHID hid(64, 64, 0x47a, 0x0b);
00017 //USBHID (uint8_t output_report_length=64, uint8_t input_report_length=64, uint16_t vendor_id=0x1234, uint16_t product_id=0x0006, uint16_t product_release=0x0001, bool connect=true) 
00018  
00019 Serial pc(USBTX, USBRX);
00020  
00021 //This report will contain data to be sent
00022 HID_REPORT send_report;
00023 HID_REPORT recv_report;
00024  
00025 DigitalOut l1(LED1);
00026 
00027 #define FW_VERSION                                  "3.1.0"
00028 #define SK_NAME                                     "mbed"
00029 
00030 #define HID_SK_RESET                                0x00
00031 #define HID_SK_GET_VERSION                          0x01
00032 #define HID_SK_GET_NAME                             0x02
00033 #define HID_SK_GET_PIN                              0x10
00034 #define HID_SK_SET_PIN                              0x11
00035 #define HID_SK_GET_PINS                             0x14
00036 #define HID_DEVICE_READ                             0x80
00037 #define HID_DEVICE_WRITE                            0x81
00038 #define HID_DEVICE_INIT                             0x88
00039 #define HID_DEVICE_RESET                            0x89
00040 #define HID_SK_CMD_NONE                             0xFF
00041 
00042 typedef struct sHidCommand
00043 {
00044     uint8_t Cmd;
00045     uint8_t CmdOpt;
00046     uint8_t CmdDataSize;
00047     uint8_t *CmdData;
00048 } tHidCommand;
00049 
00050 typedef enum
00051 {
00052     SX_OK,
00053     SX_ERROR,
00054     SX_BUSY,
00055     SX_EMPTY,
00056     SX_DONE,
00057     SX_TIMEOUT,
00058     SX_UNSUPPORTED,
00059     SX_WAIT,
00060     SX_CLOSE,
00061     SX_YES,
00062     SX_NO,          
00063 } tReturnCodes;
00064 
00065 #ifdef _DEBUG_
00066 char verbose = 0;
00067 #endif /* _DEBUG_ */
00068 
00069 void rfsw_callback()
00070 {
00071     if (radio.RegOpMode.bits.Mode == RF_OPMODE_TRANSMITTER)
00072         rfsw = 1;
00073     else
00074         rfsw = 0;
00075 }
00076 
00077 void HidDecodeCommand( uint8_t *hidReport, tHidCommand *cmd )
00078 {
00079     cmd->Cmd = hidReport[0];
00080     cmd->CmdOpt = hidReport[1];
00081     cmd->CmdDataSize = hidReport[2];
00082     cmd->CmdData = hidReport + 3;
00083 }
00084 
00085 void HidEncodeCommandAns( uint8_t cmd, uint8_t stat, uint8_t dataSize, uint8_t *data )
00086 {
00087     send_report.data[0] =  cmd;
00088     send_report.data[1] =  stat;
00089 
00090     // TimeStamp
00091     memset( send_report.data + 2, 0, 8 );
00092 
00093     send_report.data[10] =  dataSize;
00094     memcpy( send_report.data + 11, ( const void* )data, dataSize );
00095     
00096     //send_report.length = 11 + dataSize;
00097     send_report.length = 64;
00098     hid.send(&send_report);
00099 }
00100 
00101 void HidCmdProcess(void)
00102 {
00103     uint8_t stat = SX_OK;
00104     uint8_t size = 0;
00105     uint8_t dataBuffer[64];
00106     tHidCommand cmd = { HID_SK_CMD_NONE, 0, 0, NULL };
00107     #ifdef _DEBUG_
00108     int i;
00109     #endif /* _DEBUG_ */
00110 
00111     HidDecodeCommand(recv_report.data, &cmd);
00112     
00113     switch (cmd.Cmd) {
00114         case HID_DEVICE_RESET:
00115             radio.hw_reset();
00116             break;
00117         case HID_SK_RESET:
00118         case HID_DEVICE_INIT:
00119             radio.hw_reset();
00120             #ifdef _DEBUG_
00121             if (verbose)
00122                 printf("reset-init\r\n");
00123             #endif /* _DEBUG_ */
00124             radio.init();   //SX1272Init( );
00125             // Set FSK modem ON
00126             radio.set_opmode(RF_OPMODE_SLEEP);  
00127             radio.RegOpMode.bits.LongRangeMode = 0;
00128             radio.write_reg(REG_OPMODE, radio.RegOpMode.octet);            
00129             //radio.SetLoRaOn( false ); //SX1272SetLoRaOn( false ); // Default radio setting
00130             // Default answer settings
00131             break;
00132         case HID_SK_GET_VERSION:
00133             strcpy( ( char* )dataBuffer, FW_VERSION );
00134             size = strlen( FW_VERSION );
00135             break;
00136         case HID_DEVICE_READ:
00137             // cmd.CmdData[0] = size
00138             // cmd.CmdData[1] = address
00139             size = cmd.CmdData[0];
00140             radio.ReadBuffer( cmd.CmdData[1], dataBuffer, size );
00141             #ifdef _DEBUG_
00142             if (verbose) {
00143                 pc.printf("read %d bytes from %02x: ", size, cmd.CmdData[1]);
00144                 for (i = 0; i < size; i++)
00145                     pc.printf("%02x ", dataBuffer[i]);
00146                 pc.printf("\r\n");
00147             }
00148             #endif /* _DEBUG_ */
00149             stat = SX_OK;
00150             break;
00151         case HID_SK_GET_PINS:
00152             dataBuffer[0] = 0;
00153             if (radio.dio0)
00154                 dataBuffer[0] |= 0x01;
00155             if (radio.dio1)
00156                 dataBuffer[0] |= 0x02;
00157             #ifdef _DEBUG_
00158             if (verbose && dataBuffer[0] != 0)
00159                 printf("HID_SK_GET_PINS:%02x\r\n", dataBuffer[0]);
00160             #endif /* _DEBUG_ */
00161             /*dataBuffer[0] |= DIO1 << 1;
00162             dataBuffer[0] |= DIO2 << 2;
00163             dataBuffer[0] |= DIO3 << 3;
00164             dataBuffer[0] |= DIO4 << 4;
00165             dataBuffer[0] |= DIO5 << 5;*/
00166             size = 1;
00167             break;
00168         case HID_SK_GET_PIN:
00169             // cmd.CmdData[0] = Pin id
00170             switch( cmd.CmdData[0] ) {
00171                 case 11:    // FEM_CPS_PIN
00172                     // not existing on shield board -- dataBuffer[0] = radio.femcps;
00173                     #ifdef _DEBUG_
00174                     if (verbose)
00175                         printf("HID_SK_GET_PIN femcps:%02x\r\n", dataBuffer[0]);
00176                     #endif /* _DEBUG_ */
00177                     break;
00178                 case 12:    // FEM_CTX_PIN
00179                     // not existing on shield board --  dataBuffer[0] = radio.femctx;
00180                     #ifdef _DEBUG_
00181                     if (verbose)
00182                         printf("HID_SK_GET_PIN femctx:%02x\r\n", dataBuffer[0]);
00183                     #endif /* _DEBUG_ */
00184                     break;
00185                 default:
00186                     dataBuffer[0] = 0xFF; // Signal ID error
00187                     #ifdef _DEBUG_
00188                     printf("HID_SK_GET_PIN %d\r\n", cmd.CmdData[0]);
00189                     #endif /* _DEBUG_ */
00190                     break;
00191             } // ...switch( cmd.CmdData[0] )
00192             break;
00193         case HID_SK_SET_PIN:
00194             // cmd.CmdData[0] = Pin id
00195             // cmd.CmdData[1] = Pin state
00196             switch( cmd.CmdData[0] ) {
00197                 case 6:
00198                 case 7:
00199                 case 8:
00200                     // ignore LEDs
00201                     break;
00202                 case 11:    // FEM_CPS_PIN
00203                     // not existing on shield board -- radio.femcps = cmd.CmdData[1];
00204                     #ifdef _DEBUG_
00205                     if (verbose)
00206                         printf("HID_SK_SET_PIN femcps:%d\r\n", (int)radio.femcps);
00207                     #endif /* _DEBUG_ */
00208                     break;                    
00209                 case 12:    // FEM_CTX_PIN
00210                     // not existing on shield board -- radio.femctx = cmd.CmdData[1];
00211                     #ifdef _DEBUG_
00212                     if (verbose)
00213                         printf("HID_SK_SET_PIN femctx:%d\r\n", (int)radio.femctx); 
00214                     #endif /* _DEBUG_ */
00215                     break;
00216                 default:
00217                     stat = SX_UNSUPPORTED;
00218                     #ifdef _DEBUG_
00219                     pc.printf("HID_SK_SET_PIN %d %d\r\n", cmd.CmdData[0], cmd.CmdData[1]);
00220                     #endif /* _DEBUG_ */
00221                     break;
00222             } // ...switch( cmd.CmdData[0] )
00223             
00224             break;
00225         case HID_DEVICE_WRITE:
00226             // cmd.CmdData[0] = size
00227             // cmd.CmdData[1] = address
00228             // cmd.CmdData[2] = Buffer first byte
00229             // cmd.CmdData[2+(size-1)] = Buffer last byte
00230             radio.WriteBuffer( cmd.CmdData[1], cmd.CmdData + 2, cmd.CmdData[0] );
00231             #ifdef _DEBUG_
00232             if (verbose) {
00233                 pc.printf("write %d bytes to %02x: ", cmd.CmdData[0], cmd.CmdData[1]);
00234                 for (i = 0; i < cmd.CmdData[0]; i++)
00235                     pc.printf("%02x ", cmd.CmdData[2+i]);
00236                 pc.printf("\r\n");
00237             }
00238             #endif /* _DEBUG_ */
00239             stat = SX_OK;
00240             break;
00241         case HID_SK_GET_NAME:
00242             strcpy( ( char* )dataBuffer, SK_NAME );
00243             size = strlen( SK_NAME );
00244             break;            
00245         default:
00246             pc.printf("%d: ", recv_report.length);
00247             for(int i = 0; i < recv_report.length; i++) {
00248                 pc.printf("%02x ", recv_report.data[i]);
00249             }
00250             pc.printf("\r\n");
00251             stat = SX_UNSUPPORTED;
00252         break;
00253     } // ...switch (cmd.Cmd)
00254     
00255     HidEncodeCommandAns( cmd.Cmd, stat, size, dataBuffer);
00256 }
00257 
00258 #ifdef _DEBUG_
00259 void printOpMode()
00260 {
00261     radio.RegOpMode.octet = radio.read_reg(REG_OPMODE);
00262     switch (radio.RegOpMode.bits.Mode) {
00263         case RF_OPMODE_SLEEP: printf("sleep"); break;
00264         case RF_OPMODE_STANDBY: printf("stby"); break;
00265         case RF_OPMODE_SYNTHESIZER_TX: printf("fstx"); break;
00266         case RF_OPMODE_TRANSMITTER: printf("tx"); break;
00267         case RF_OPMODE_SYNTHESIZER_RX: printf("fsrx"); break;
00268         case RF_OPMODE_RECEIVER: printf("rx"); break;
00269         case 6:
00270             if (radio.RegOpMode.bits.LongRangeMode)
00271                 printf("rxs");
00272             else
00273                 printf("-6-");
00274             break;  // todo: different lora/fsk
00275         case 7:
00276             if (radio.RegOpMode.bits.LongRangeMode)
00277                 printf("cad");
00278             else
00279                 printf("-7-");
00280             break;  // todo: different lora/fsk
00281     }
00282 }
00283 #endif /* _DEBUG_ */
00284 
00285 #ifdef _DEBUG_
00286 void
00287 printPa()
00288 {
00289     radio.RegPaConfig.octet = radio.read_reg(REG_PACONFIG);
00290     if (radio.RegPaConfig.bits.PaSelect) {
00291         float output_dBm = 17 - (15-radio.RegPaConfig.bits.OutputPower);
00292         printf(" PABOOST OutputPower=%.1fdBm", output_dBm);
00293     } else {
00294         float pmax = (0.6*radio.RegPaConfig.bits.MaxPower) + 10.8;
00295         float output_dBm = pmax - (15-radio.RegPaConfig.bits.OutputPower);
00296         printf(" RFO pmax=%.1fdBm OutputPower=%.1fdBm", pmax, output_dBm);
00297     }
00298 }
00299 #endif /* _DEBUG_ */
00300 
00301 #ifdef _DEBUG_
00302 void /* things always present, whether lora or fsk */
00303 common_print_status()
00304 {
00305     printf("version:0x%02x %.3fMHz ", radio.read_reg(REG_VERSION), radio.get_frf_MHz());
00306     printOpMode();
00307 
00308     printPa();
00309 
00310     radio.RegOcp.octet = radio.read_reg(REG_OCP);
00311     if (radio.RegOcp.bits.OcpOn) {
00312         int imax = 0;
00313         if (radio.RegOcp.bits.OcpTrim < 16)
00314             imax = 45 + (5 * radio.RegOcp.bits.OcpTrim);
00315         else if (radio.RegOcp.bits.OcpTrim < 28)
00316             imax = -30 + (10 * radio.RegOcp.bits.OcpTrim);
00317         else
00318             imax = 240;
00319         printf(" OcpOn %dmA ", imax);
00320     } else
00321         printf(" OcpOFF ");
00322 
00323     printf("\r\n");
00324 
00325 }
00326 #endif /* _DEBUG_ */
00327 
00328 #ifdef _DEBUG_
00329 void lora_print_dio()
00330 {
00331    radio.RegDioMapping2.octet = radio.read_reg(REG_DIOMAPPING2);
00332     printf("DIO5:");
00333     switch (radio.RegDioMapping2.bits.Dio5Mapping) {
00334         case 0: printf("ModeReady"); break;
00335         case 1: printf("ClkOut"); break;
00336         case 2: printf("ClkOut"); break;
00337     }
00338     printf(" DIO4:");
00339     switch (radio.RegDioMapping2.bits.Dio4Mapping) {
00340         case 0: printf("CadDetected"); break;
00341         case 1: printf("PllLock"); break;
00342         case 2: printf("PllLock"); break;
00343     }    
00344     radio.RegDioMapping1.octet = radio.read_reg(REG_DIOMAPPING1);
00345     printf(" DIO3:");
00346     switch (radio.RegDioMapping1.bits.Dio3Mapping) {
00347         case 0: printf("CadDone"); break;
00348         case 1: printf("ValidHeader"); break;
00349         case 2: printf("PayloadCrcError"); break;
00350     }    
00351     printf(" DIO2:");
00352     switch (radio.RegDioMapping1.bits.Dio2Mapping) {
00353         case 0:
00354         case 1:
00355         case 2:
00356             printf("FhssChangeChannel");
00357             break;
00358     }    
00359     printf(" DIO1:");
00360     switch (radio.RegDioMapping1.bits.Dio1Mapping) {
00361         case 0: printf("RxTimeout"); break;
00362         case 1: printf("FhssChangeChannel"); break;
00363         case 2: printf("CadDetected"); break;
00364     }    
00365     printf(" DIO0:");
00366     switch (radio.RegDioMapping1.bits.Dio0Mapping) {
00367         case 0: printf("RxDone"); break;
00368         case 1: printf("TxDone"); break;
00369         case 2: printf("CadDone"); break;
00370     }    
00371     
00372     printf("\r\n"); 
00373 }
00374 #endif /* _DEBUG_ */
00375 
00376 #ifdef _DEBUG_
00377 void
00378 printCodingRate(bool from_rx)
00379 {
00380     uint8_t d = lora.getCodingRate(from_rx);
00381     printf("CodingRate:");
00382     switch (d) {
00383         case 1: printf("4/5 "); break;
00384         case 2: printf("4/6 "); break;
00385         case 3: printf("4/7 "); break;
00386         case 4: printf("4/8 "); break;
00387         default:
00388             printf("%d ", d);
00389             break;
00390     }
00391 }
00392 #endif /* _DEBUG_ */
00393 
00394 #ifdef _DEBUG_
00395 void printHeaderMode()
00396 {
00397     if (lora.getHeaderMode())
00398         printf("implicit ");
00399     else
00400         printf("explicit ");
00401 }
00402 #endif /* _DEBUG_ */
00403 
00404 #ifdef _DEBUG_
00405 void printBw()
00406 {
00407     uint8_t bw = lora.getBw();
00408     
00409     printf("Bw:");
00410     if (radio.type == SX1276) {
00411         switch (lora.RegModemConfig.sx1276bits.Bw) {
00412             case 0: printf("7.8KHz "); break;
00413             case 1: printf("10.4KHz "); break;
00414             case 2: printf("15.6KHz "); break;
00415             case 3: printf("20.8KHz "); break;
00416             case 4: printf("31.25KHz "); break;
00417             case 5: printf("41.7KHz "); break;
00418             case 6: printf("62.5KHz "); break;
00419             case 7: printf("125KHz "); break;
00420             case 8: printf("250KHz "); break;
00421             case 9: printf("500KHz "); break;
00422             default: printf("%x ", lora.RegModemConfig.sx1276bits.Bw); break;
00423         }
00424     } else if (radio.type == SX1272) {
00425         switch (lora.RegModemConfig.sx1272bits.Bw) {
00426             case 0: printf("125KHz "); break;
00427             case 1: printf("250KHz "); break;
00428             case 2: printf("500KHz "); break;
00429             case 3: printf("11b "); break;
00430         }
00431     }
00432 }
00433 #endif /* _DEBUG_ */
00434 
00435 #ifdef _DEBUG_
00436 void printSf()
00437 {
00438     // spreading factor same between sx127[26]
00439     printf("sf:%d ", lora.getSf());
00440 }
00441 #endif /* _DEBUG_ */
00442 
00443 #ifdef _DEBUG_
00444 void printTxContinuousMode()
00445 {
00446     printf("TxContinuousMode:%d ", lora.RegModemConfig2.sx1276bits.TxContinuousMode);    // same for sx1272 and sx1276
00447 }
00448 #endif /* _DEBUG_ */
00449 
00450 #ifdef _DEBUG_
00451 void printAgcAutoOn()
00452 {
00453     printf("AgcAutoOn:%d", lora.getAgcAutoOn());
00454 }
00455 #endif /* _DEBUG_ */
00456 
00457 #ifdef _DEBUG_
00458 void printRxPayloadCrcOn()
00459 {
00460     bool on = lora.getRxPayloadCrcOn();
00461     //printf("RxPayloadCrcOn:%s ", on ? "on" : "off");
00462     if (on)
00463         printf("RxPayloadCrcOn:1 = Tx CRC Enabled\r\n");
00464     else
00465         printf("RxPayloadCrcOn:1 = no Tx CRC\r\n");
00466 }
00467 #endif /* _DEBUG_ */
00468 
00469 #ifdef _DEBUG_
00470 void printLoraIrqs_(bool clear)
00471 {
00472     //in radio class -- RegIrqFlags_t RegIrqFlags;
00473 
00474     //already read RegIrqFlags.octet = radio.read_reg(REG_LR_IRQFLAGS);
00475     printf("\r\nIrqFlags:");
00476     if (lora.RegIrqFlags.bits.CadDetected)
00477         printf("CadDetected ");
00478     if (lora.RegIrqFlags.bits.FhssChangeChannel) {
00479         //radio.RegHopChannel.octet = radio.read_reg(REG_LR_HOPCHANNEL);
00480         printf("FhssChangeChannel:%d ", lora.RegHopChannel.bits.FhssPresentChannel);
00481     }
00482     if (lora.RegIrqFlags.bits.CadDone)
00483         printf("CadDone ");
00484     if (lora.RegIrqFlags.bits.TxDone)
00485         printf("TxDone ");
00486     if (lora.RegIrqFlags.bits.ValidHeader)
00487         printf("ValidHeader ");
00488     if (lora.RegIrqFlags.bits.PayloadCrcError)
00489         printf("PayloadCrcError ");
00490     if (lora.RegIrqFlags.bits.RxDone)
00491         printf("RxDone ");  
00492     if (lora.RegIrqFlags.bits.RxTimeout)
00493         printf("RxTimeout ");
00494 
00495     printf("\r\n");
00496 
00497     if (clear)
00498         radio.write_reg(REG_LR_IRQFLAGS, lora.RegIrqFlags.octet);
00499 
00500 }
00501 #endif /* _DEBUG_ */
00502 
00503 #ifdef _DEBUG_
00504 void lora_print_status()
00505 {
00506     uint8_t d;
00507     
00508     if (radio.type == SX1276)
00509         printf("\r\nSX1276 ");
00510     else if (radio.type == SX1272)
00511         printf("\r\nSX1272 ");
00512     
00513     radio.RegOpMode.octet = radio.read_reg(REG_OPMODE);
00514     if (!radio.RegOpMode.bits.LongRangeMode) {
00515         printf("FSK\r\n");
00516         return;
00517     }
00518     
00519     lora_print_dio();
00520     printf("LoRa ");
00521     
00522     // printing LoRa registers at 0x0d -> 0x3f
00523 
00524     lora.RegModemConfig.octet = radio.read_reg(REG_LR_MODEMCONFIG);
00525     lora.RegModemConfig2.octet = radio.read_reg(REG_LR_MODEMCONFIG2);
00526 
00527     printCodingRate(false); // false: transmitted coding rate
00528     printHeaderMode();
00529     printBw();
00530     printSf();
00531     printRxPayloadCrcOn();
00532     // RegModemStat
00533     printf("ModemStat:0x%02x\r\n", radio.read_reg(REG_LR_MODEMSTAT));
00534 
00535     // fifo ptrs:
00536     lora.RegPayloadLength = radio.read_reg(REG_LR_PAYLOADLENGTH);
00537     lora.RegRxMaxPayloadLength = radio.read_reg(REG_LR_RX_MAX_PAYLOADLENGTH);
00538     printf("fifoptr=0x%02x txbase=0x%02x rxbase=0x%02x payloadLength=0x%02x maxlen=0x%02x",
00539         radio.read_reg(REG_LR_FIFOADDRPTR),
00540         radio.read_reg(REG_LR_FIFOTXBASEADDR),
00541         radio.read_reg(REG_LR_FIFORXBASEADDR),
00542         lora.RegPayloadLength,
00543         lora.RegRxMaxPayloadLength
00544     );
00545 
00546     lora.RegIrqFlags.octet = radio.read_reg(REG_LR_IRQFLAGS);
00547     printLoraIrqs_(false);
00548 
00549     lora.RegHopPeriod = radio.read_reg(REG_LR_HOPPERIOD);
00550     if (lora.RegHopPeriod != 0) {
00551         printf("\r\nHopPeriod:0x%02x\r\n", lora.RegHopPeriod);
00552     }
00553 
00554     printf("SymbTimeout:0x%03x ", radio.read_u16(REG_LR_MODEMCONFIG2) & 0x3ff);
00555 
00556     lora.RegPreamble = radio.read_u16(REG_LR_PREAMBLEMSB);
00557     printf("PreambleLength:0x%03x ", lora.RegPreamble);
00558     
00559 
00560     if (radio.RegOpMode.bits.Mode == RF_OPMODE_RECEIVER || radio.RegOpMode.bits.Mode == RF_OPMODE_RECEIVER_SINGLE) {
00561         d = radio.read_reg(REG_LR_RSSIVALUE);
00562         printf("rssi:%ddBm ", d-120);
00563     }
00564 
00565     printTxContinuousMode();
00566 
00567     printf("\r\n");
00568     printAgcAutoOn();
00569     if (radio.type == SX1272) {
00570         printf(" LowDataRateOptimize:%d\r\n", lora.RegModemConfig.sx1272bits.LowDataRateOptimize);
00571     }
00572 
00573     printf("\r\nHeaderCount:%d PacketCount:%d, ",
00574         radio.read_u16(REG_LR_RXHEADERCNTVALUE_MSB), radio.read_u16(REG_LR_RXPACKETCNTVALUE_MSB));
00575 
00576     printf("Lora detection threshold:%02x\r\n", radio.read_reg(REG_LR_DETECTION_THRESHOLD));
00577     lora.RegTest31.octet = radio.read_reg(REG_LR_TEST31);
00578     printf("detect_trig_same_peaks_nb:%d\r\n", lora.RegTest31.bits.detect_trig_same_peaks_nb);
00579 
00580     if (radio.type == SX1272) {
00581         lora.RegModemConfig.octet = radio.read_reg(REG_LR_MODEMCONFIG);
00582         printf("LowDataRateOptimize:%d\r\n", lora.RegModemConfig.sx1272bits.LowDataRateOptimize);
00583     } else if (radio.type == SX1276) {
00584         lora.RegModemConfig3.octet = radio.read_reg(REG_LR_MODEMCONFIG3);
00585         printf("LowDataRateOptimize:%d\r\n", lora.RegModemConfig3.sx1276bits.LowDataRateOptimize);        
00586     }
00587     
00588     printf("\r\n");
00589     //printf("A %02x\r\n", radio.RegModemConfig2.octet);
00590 }
00591 #endif /* _DEBUG_ */
00592 
00593 /*void
00594 service_radio()
00595 {
00596     service_action_e act = radio.service();
00597     
00598     switch (act) {
00599         case SERVICE_READ_FIFO:
00600             printf("SERVICE_READ_FIFO\r\n");
00601             // clear Irq flags
00602             radio.write_reg(REG_LR_IRQFLAGS, radio.RegIrqFlags.octet);        
00603             break;
00604         case SERVICE_TX_DONE:
00605             printf("SERVICE_TX_DONE\r\n");
00606             break;
00607         case SERVICE_ERROR:
00608             printf("error\r\n");
00609             break;
00610     } // ...switch (act)
00611 }*/
00612         
00613 int
00614 main(void)
00615 {
00616     #ifdef _DEBUG_ 
00617     pc.baud(57600);
00618     pc.printf("\r\nstart\r\n");
00619     #endif /* _DEBUG_ */
00620     
00621     radio.rf_switch.attach(rfsw_callback);
00622     
00623     while (1) { 
00624         //try to read a msg
00625         if (hid.readNB(&recv_report)) {
00626             HidCmdProcess();
00627         }
00628 
00629         #ifdef _DEBUG_   
00630         if (pc.readable()) {
00631             char c = pc.getc();
00632             if (c == 'v') {
00633                 pc.printf("verbose ");
00634                 if (verbose) {
00635                     verbose = 0;
00636                     pc.printf("off");
00637                 } else {
00638                     verbose = 1;
00639                     pc.printf("on");
00640                 }
00641                 pc.printf("\r\n");
00642             } else if (c == '.') {
00643                 common_print_status();
00644                 if (radio.RegOpMode.bits.LongRangeMode)
00645                     lora_print_status();
00646                 else
00647                     printf("FSK\r\n");
00648             } else if (c == 't') {
00649                 int i;
00650                 printf("tx\r\n");
00651                 radio.set_opmode(RF_OPMODE_TRANSMITTER);
00652                 for (i = 0; i < 20; i++) {
00653                     radio.RegOpMode.octet = radio.read_reg(REG_OPMODE);
00654                     printf("opmode:%02x\r\n", radio.RegOpMode.octet);
00655                 }
00656             } else if (c == 'T') {
00657                 printf("start_tx\r\n");
00658                 lora.RegPayloadLength = 8;
00659                 radio.write_reg(REG_LR_PAYLOADLENGTH, lora.RegPayloadLength);
00660                 lora.start_tx(8);
00661             } /*else if (c == 'e') {
00662                 printf("service_radio\r\n");
00663                 service_radio();
00664             }*/ else if (c == 's') {
00665                 radio.set_opmode(RF_OPMODE_STANDBY);
00666                 printf("standby\r\n");
00667             } else if (c == 'h') {
00668                 printf("hwreset\r\n");
00669                 radio.hw_reset();
00670                 radio.init();   //SX1272Init( );
00671             } /*else if (c == 'l') {
00672                 radio.SetLoRaOn(!radio.RegOpMode.bits.LongRangeMode);
00673                 printf("LongRangeMode:%d\r\n", radio.RegOpMode.bits.LongRangeMode);
00674             }*/ else if (c == '?') {
00675                 printf("s   standby\r\n");
00676                 printf("T   lora_start_tx(8)\r\n");
00677                 printf(".   print status\r\n");
00678                 printf("v   toggle verbose\r\n");
00679                 printf("t   tx mode test\r\n");
00680                 printf("e   manualy service radio once\r\n");
00681                 printf("h   hwreset, init\r\n");
00682                 printf("l   toggle lora mode\r\n");
00683             }
00684         } // ...if (pc.readable())
00685         #endif /* _DEBUG_ */
00686         
00687     } // ...while (1)
00688 }