May 2021 Tests

Dependencies:   SX127x sx12xx_hal

Committer:
lightshow
Date:
Sun May 09 20:00:15 2021 +0000
Revision:
2:28982c714a50
Parent:
1:b277f5a65c1c
May 2021 Tests

Who changed what in which revision?

UserRevisionLine numberNew contents of line
LoRaToolbox 0:44fb2db84011 1 #include "radio.h"
LoRaToolbox 0:44fb2db84011 2
lightshow 2:28982c714a50 3 //InterruptIn txByte[8]={InterruptIn(D2),InterruptIn(D3),InterruptIn(D4),InterruptIn(D5),InterruptIn(D7),InterruptIn(D8),InterruptIn(D9),InterruptIn(D14)};
lightshow 2:28982c714a50 4 InterruptIn txByte0(PB_1, PullDown);//D2,A1,PC_8 didn't work, PC_5
lightshow 2:28982c714a50 5 InterruptIn txByte1(PB_15, PullDown);//D3 didn't work, A2 works
lightshow 2:28982c714a50 6 InterruptIn txByte2(D4, PullDown);
lightshow 2:28982c714a50 7 InterruptIn txByte3(D7, PullDown);
lightshow 2:28982c714a50 8 InterruptIn txByte4(D5, PullDown);
lightshow 2:28982c714a50 9 InterruptIn txByte5(PB_14, PullDown);//D8 didn't work, a3 works
lightshow 2:28982c714a50 10 InterruptIn txByte6(D9, PullDown);//D9 didn't work, a4 works
lightshow 2:28982c714a50 11 InterruptIn txByte7(PB_2, PullDown);//D14,PC_0(A6),PC_6 didn't work
lightshow 2:28982c714a50 12 EventQueue queue(10 * EVENTS_EVENT_SIZE);
lightshow 2:28982c714a50 13 Thread t;
lightshow 2:28982c714a50 14 Radio loRaRadio;
lightshow 2:28982c714a50 15
lightshow 2:28982c714a50 16 uint8_t ID_byte = 42;
lightshow 2:28982c714a50 17
lightshow 2:28982c714a50 18 void transmit( uint8_t byte);
lightshow 2:28982c714a50 19
lightshow 2:28982c714a50 20 //uint8_t old_byte = 0;
lightshow 2:28982c714a50 21 void tx_event(void)
lightshow 2:28982c714a50 22 {
lightshow 2:28982c714a50 23 printf("tx_event\n\r");
lightshow 2:28982c714a50 24 uint8_t byte = 0;
lightshow 2:28982c714a50 25
lightshow 2:28982c714a50 26 uint8_t bit0 = txByte0.read();
lightshow 2:28982c714a50 27 uint8_t bit1 = txByte1.read() << 1;
lightshow 2:28982c714a50 28 uint8_t bit2 = txByte2.read() << 2;
lightshow 2:28982c714a50 29 uint8_t bit3 = txByte3.read() << 3;
lightshow 2:28982c714a50 30 uint8_t bit4 = txByte4.read() << 4;
lightshow 2:28982c714a50 31 uint8_t bit5 = txByte5.read() << 5;
lightshow 2:28982c714a50 32 uint8_t bit6 = txByte6.read() << 6;
lightshow 2:28982c714a50 33 uint8_t bit7 = txByte7.read() << 7;
lightshow 2:28982c714a50 34 if(bit0 || bit1 || bit2 || bit3 || bit4 || bit5 || bit6 || bit7)
lightshow 2:28982c714a50 35 {
lightshow 2:28982c714a50 36 byte = bit0 + bit1 + bit2 + bit3 + bit4 + bit5 + bit6 + bit7;
lightshow 2:28982c714a50 37 transmit(byte);
lightshow 2:28982c714a50 38 }
lightshow 2:28982c714a50 39 printf("bit0 :%d,\n\r",bit0);
lightshow 2:28982c714a50 40 printf("bit1 :%d,\n\r",bit1);
lightshow 2:28982c714a50 41 printf("bit2 :%d,\n\r",bit2);
lightshow 2:28982c714a50 42 printf("bit3 :%d,\n\r",bit3);
lightshow 2:28982c714a50 43 printf("bit4 :%d,\n\r",bit4);
lightshow 2:28982c714a50 44 printf("bit5 :%d,\n\r",bit5);
lightshow 2:28982c714a50 45 printf("bit6 :%d,\n\r",bit6);
lightshow 2:28982c714a50 46 printf("bit7 :%d,\n\r",bit7);
lightshow 2:28982c714a50 47 }
lightshow 2:28982c714a50 48
lightshow 2:28982c714a50 49 void rise_handler(void) {
lightshow 2:28982c714a50 50 //printf("rise_handler in context %p\r\n", Thread::gettid());
lightshow 2:28982c714a50 51 queue.call(tx_event);
lightshow 2:28982c714a50 52
lightshow 2:28982c714a50 53 }
lightshow 2:28982c714a50 54
LoRaToolbox 1:b277f5a65c1c 55 // Semtech radio definitions for SX127x, SX126x and SX128x
LoRaToolbox 1:b277f5a65c1c 56
LoRaToolbox 0:44fb2db84011 57 #if defined(SX127x_H)
LoRaToolbox 0:44fb2db84011 58 #define BW_KHZ 500
LoRaToolbox 0:44fb2db84011 59 #define SPREADING_FACTOR 11
LoRaToolbox 0:44fb2db84011 60 #define CF_HZ 912000000
lightshow 2:28982c714a50 61 #define TX_DBM 00
LoRaToolbox 0:44fb2db84011 62 #elif defined(SX126x_H)
LoRaToolbox 0:44fb2db84011 63 #define BW_KHZ 500
LoRaToolbox 0:44fb2db84011 64 #define SPREADING_FACTOR 10
LoRaToolbox 0:44fb2db84011 65 #define CF_HZ 915000000
LoRaToolbox 0:44fb2db84011 66 #define TX_DBM (Radio::chipType == CHIP_TYPE_SX1262 ? 20 : 14)
LoRaToolbox 0:44fb2db84011 67 #elif defined(SX128x_H)
LoRaToolbox 0:44fb2db84011 68 #define BW_KHZ 200
LoRaToolbox 0:44fb2db84011 69 #define SPREADING_FACTOR 7
LoRaToolbox 0:44fb2db84011 70 #define CF_HZ 2487000000
LoRaToolbox 0:44fb2db84011 71 #define TX_DBM 6
LoRaToolbox 0:44fb2db84011 72 #endif
LoRaToolbox 0:44fb2db84011 73
LoRaToolbox 1:b277f5a65c1c 74 /******************** Setup radio transmitter ****************************/
LoRaToolbox 1:b277f5a65c1c 75
LoRaToolbox 1:b277f5a65c1c 76
LoRaToolbox 0:44fb2db84011 77 volatile bool txDone;
LoRaToolbox 0:44fb2db84011 78
LoRaToolbox 0:44fb2db84011 79 void txDoneCB()
LoRaToolbox 0:44fb2db84011 80 {
LoRaToolbox 0:44fb2db84011 81 txDone = true;
LoRaToolbox 0:44fb2db84011 82 }
LoRaToolbox 0:44fb2db84011 83
LoRaToolbox 0:44fb2db84011 84 void rxDoneCB(uint8_t size, float rssi, float snr)
LoRaToolbox 0:44fb2db84011 85 {
LoRaToolbox 0:44fb2db84011 86 }
LoRaToolbox 0:44fb2db84011 87
LoRaToolbox 1:b277f5a65c1c 88 // Define radio events for transmitter
LoRaToolbox 1:b277f5a65c1c 89
LoRaToolbox 0:44fb2db84011 90 const RadioEvents_t rev = {
LoRaToolbox 0:44fb2db84011 91 /* Dio0_top_half */ NULL,
LoRaToolbox 0:44fb2db84011 92 /* TxDone_topHalf */ NULL,
LoRaToolbox 0:44fb2db84011 93 /* TxDone_botHalf */ txDoneCB,
LoRaToolbox 0:44fb2db84011 94 /* TxTimeout */ NULL,
LoRaToolbox 0:44fb2db84011 95 /* RxDone */ rxDoneCB,
LoRaToolbox 0:44fb2db84011 96 /* RxTimeout */ NULL,
LoRaToolbox 0:44fb2db84011 97 /* RxError */ NULL,
LoRaToolbox 0:44fb2db84011 98 /* FhssChangeChannel */NULL,
LoRaToolbox 0:44fb2db84011 99 /* CadDone */ NULL
LoRaToolbox 0:44fb2db84011 100 };
LoRaToolbox 0:44fb2db84011 101
LoRaToolbox 0:44fb2db84011 102 int main()
LoRaToolbox 0:44fb2db84011 103 {
lightshow 2:28982c714a50 104 // Start the event queue
lightshow 2:28982c714a50 105 t.start(callback(&queue, &EventQueue::dispatch_forever));
lightshow 2:28982c714a50 106
lightshow 2:28982c714a50 107 // The 'rise' handler will execute in the context of thread 't'
lightshow 2:28982c714a50 108 // for(int i = 0; i < 8; i++)
lightshow 2:28982c714a50 109 // {
lightshow 2:28982c714a50 110 // txByte[i].rise(queue.event(rise_handler));
lightshow 2:28982c714a50 111 // }
lightshow 2:28982c714a50 112 txByte0.rise(rise_handler);
lightshow 2:28982c714a50 113 txByte1.rise(rise_handler);
lightshow 2:28982c714a50 114 txByte2.rise(rise_handler);
lightshow 2:28982c714a50 115 txByte3.rise(rise_handler);
lightshow 2:28982c714a50 116 txByte4.rise(rise_handler);
lightshow 2:28982c714a50 117 txByte5.rise(rise_handler);
lightshow 2:28982c714a50 118 txByte6.rise(rise_handler);
lightshow 2:28982c714a50 119 txByte7.rise(rise_handler);
lightshow 2:28982c714a50 120
lightshow 2:28982c714a50 121
LoRaToolbox 1:b277f5a65c1c 122 uint8_t seq = 0; // Set initial transmit sequence to 0
LoRaToolbox 1:b277f5a65c1c 123
LoRaToolbox 0:44fb2db84011 124 printf("\r\nreset-tx ");
LoRaToolbox 1:b277f5a65c1c 125
LoRaToolbox 1:b277f5a65c1c 126 // Start radio transmitter after POR or reset
LoRaToolbox 0:44fb2db84011 127
lightshow 2:28982c714a50 128 loRaRadio.Init(&rev);
LoRaToolbox 0:44fb2db84011 129
LoRaToolbox 1:b277f5a65c1c 130 //Set radio properties for transmitter
LoRaToolbox 1:b277f5a65c1c 131
lightshow 2:28982c714a50 132 loRaRadio.Standby();
lightshow 2:28982c714a50 133 loRaRadio.LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1);
lightshow 2:28982c714a50 134 loRaRadio.SetChannel(CF_HZ);
LoRaToolbox 0:44fb2db84011 135
LoRaToolbox 1:b277f5a65c1c 136 // Set transmitter output power
LoRaToolbox 1:b277f5a65c1c 137
lightshow 2:28982c714a50 138 loRaRadio.set_tx_dbm(TX_DBM);
LoRaToolbox 0:44fb2db84011 139
LoRaToolbox 1:b277f5a65c1c 140 // Setup transmit packet payload -> preambleLen, fixLen, crcOn, invIQ
LoRaToolbox 1:b277f5a65c1c 141
lightshow 2:28982c714a50 142 loRaRadio.LoRaPacketConfig(8, false, true, false);
LoRaToolbox 0:44fb2db84011 143
LoRaToolbox 0:44fb2db84011 144 printf("\n");
LoRaToolbox 0:44fb2db84011 145
LoRaToolbox 0:44fb2db84011 146 printf("%02d \r\n\n", seq);
LoRaToolbox 0:44fb2db84011 147
lightshow 2:28982c714a50 148 for (;;) {}
lightshow 2:28982c714a50 149
lightshow 2:28982c714a50 150 // loRaRadio.radio.tx_buf[0] = seq; // set payload
lightshow 2:28982c714a50 151 // txDone = false;
lightshow 2:28982c714a50 152 // loRaRadio.Send(10, 0, 0, 0); // begin transmission of payload
lightshow 2:28982c714a50 153
lightshow 2:28982c714a50 154 // while (!txDone) {
lightshow 2:28982c714a50 155 // loRaRadio.service();
lightshow 2:28982c714a50 156 // }
lightshow 2:28982c714a50 157
lightshow 2:28982c714a50 158 // // Transmit payload every 500mS
lightshow 2:28982c714a50 159
lightshow 2:28982c714a50 160 // wait(0.5); // throttle sending rate
lightshow 2:28982c714a50 161 // seq++; // change payload (increment sequence number)
lightshow 2:28982c714a50 162 // printf("%02d \r\n\n", seq); // Print sequence number
lightshow 2:28982c714a50 163
lightshow 2:28982c714a50 164 //}
lightshow 2:28982c714a50 165 }
lightshow 2:28982c714a50 166
lightshow 2:28982c714a50 167 void transmit( uint8_t byte)
lightshow 2:28982c714a50 168 {
lightshow 2:28982c714a50 169 //volatile bool tx_Done;
lightshow 2:28982c714a50 170 loRaRadio.radio.tx_buf[0] = ID_byte; // set payload
lightshow 2:28982c714a50 171 loRaRadio.radio.tx_buf[1] = byte; // set payload
LoRaToolbox 0:44fb2db84011 172 txDone = false;
lightshow 2:28982c714a50 173 loRaRadio.Send(2, 0, 0, 0); // begin transmission of payload
LoRaToolbox 0:44fb2db84011 174
LoRaToolbox 0:44fb2db84011 175 while (!txDone) {
lightshow 2:28982c714a50 176 loRaRadio.service();
LoRaToolbox 0:44fb2db84011 177 }
lightshow 2:28982c714a50 178 printf("transmission complete\n\r");
LoRaToolbox 0:44fb2db84011 179 }