Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: LoRaWAN-SanJose_Bootcamp LoRaWAN-grove-cayenne LoRaWAN-classC-demo LoRaWAN-grove-cayenne ... more
radio/radio.cpp@0:6b3ac9c5a042, 2018-02-28 (annotated)
- Committer:
- Wayne Roberts
- Date:
- Wed Feb 28 10:48:11 2018 -0800
- Revision:
- 0:6b3ac9c5a042
- Child:
- 4:e4bfe9183f94
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Wayne Roberts |
0:6b3ac9c5a042 | 1 | #include "board.h" |
Wayne Roberts |
0:6b3ac9c5a042 | 2 | #include "radio.h" |
Wayne Roberts |
0:6b3ac9c5a042 | 3 | |
Wayne Roberts |
0:6b3ac9c5a042 | 4 | LowPowerTimer Radio::lpt; |
Wayne Roberts |
0:6b3ac9c5a042 | 5 | |
Wayne Roberts |
0:6b3ac9c5a042 | 6 | void Radio::Sleep() |
Wayne Roberts |
0:6b3ac9c5a042 | 7 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 8 | radio.set_opmode(RF_OPMODE_SLEEP); |
Wayne Roberts |
0:6b3ac9c5a042 | 9 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 10 | |
Wayne Roberts |
0:6b3ac9c5a042 | 11 | void Radio::Standby() |
Wayne Roberts |
0:6b3ac9c5a042 | 12 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 13 | radio.set_opmode(RF_OPMODE_STANDBY); |
Wayne Roberts |
0:6b3ac9c5a042 | 14 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 15 | |
Wayne Roberts |
0:6b3ac9c5a042 | 16 | bool Radio::CheckRfFrequency(unsigned hz) |
Wayne Roberts |
0:6b3ac9c5a042 | 17 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 18 | return true; |
Wayne Roberts |
0:6b3ac9c5a042 | 19 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 20 | |
Wayne Roberts |
0:6b3ac9c5a042 | 21 | void Radio::SetChannel(unsigned hz) |
Wayne Roberts |
0:6b3ac9c5a042 | 22 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 23 | radio.set_frf_MHz(hz / 1000000.0); |
Wayne Roberts |
0:6b3ac9c5a042 | 24 | MAC_PRINTF(" %uhz ", hz); |
Wayne Roberts |
0:6b3ac9c5a042 | 25 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 26 | |
Wayne Roberts |
0:6b3ac9c5a042 | 27 | LowPowerTimeout TxTimeoutEvent; |
Wayne Roberts |
0:6b3ac9c5a042 | 28 | |
Wayne Roberts |
0:6b3ac9c5a042 | 29 | void SX1272OnTimeoutIrq( void ) |
Wayne Roberts |
0:6b3ac9c5a042 | 30 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 31 | Radio::radio.set_opmode(RF_OPMODE_STANDBY); |
Wayne Roberts |
0:6b3ac9c5a042 | 32 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 33 | |
Wayne Roberts |
0:6b3ac9c5a042 | 34 | void Radio::SetTxContinuousWave(unsigned hz, int8_t dbm, unsigned timeout_us) |
Wayne Roberts |
0:6b3ac9c5a042 | 35 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 36 | Radio::SetChannel(hz); |
Wayne Roberts |
0:6b3ac9c5a042 | 37 | /* TODO: fsk enable, set regPacketConfig2.datamode */ |
Wayne Roberts |
0:6b3ac9c5a042 | 38 | set_tx_dbm(dbm); |
Wayne Roberts |
0:6b3ac9c5a042 | 39 | TxTimeoutEvent.attach_us(SX1272OnTimeoutIrq, timeout_us); |
Wayne Roberts |
0:6b3ac9c5a042 | 40 | radio.set_opmode(RF_OPMODE_TRANSMITTER); |
Wayne Roberts |
0:6b3ac9c5a042 | 41 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 42 | |
Wayne Roberts |
0:6b3ac9c5a042 | 43 | #define LORA_MAC_PRIVATE_SYNCWORD 0x12 |
Wayne Roberts |
0:6b3ac9c5a042 | 44 | #define LORA_MAC_PUBLIC_SYNCWORD 0x34 |
Wayne Roberts |
0:6b3ac9c5a042 | 45 | void Radio::SetPublicNetwork(bool en) |
Wayne Roberts |
0:6b3ac9c5a042 | 46 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 47 | radio.write_reg(REG_LR_SYNC_BYTE, en ? LORA_MAC_PUBLIC_SYNCWORD : LORA_MAC_PRIVATE_SYNCWORD); |
Wayne Roberts |
0:6b3ac9c5a042 | 48 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 49 | |
Wayne Roberts |
0:6b3ac9c5a042 | 50 | uint32_t Radio::Random(void) |
Wayne Roberts |
0:6b3ac9c5a042 | 51 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 52 | uint32_t ret = 0; |
Wayne Roberts |
0:6b3ac9c5a042 | 53 | unsigned i; |
Wayne Roberts |
0:6b3ac9c5a042 | 54 | |
Wayne Roberts |
0:6b3ac9c5a042 | 55 | radio.set_opmode(RF_OPMODE_RECEIVER); |
Wayne Roberts |
0:6b3ac9c5a042 | 56 | for (i = 0; i < 32; i++) { |
Wayne Roberts |
0:6b3ac9c5a042 | 57 | uint32_t r; |
Wayne Roberts |
0:6b3ac9c5a042 | 58 | wait_us(3000); |
Wayne Roberts |
0:6b3ac9c5a042 | 59 | r = radio.read_reg(REG_LR_WIDEBAND_RSSI); |
Wayne Roberts |
0:6b3ac9c5a042 | 60 | r <<= ((i & 7) << 2); |
Wayne Roberts |
0:6b3ac9c5a042 | 61 | ret ^= r; |
Wayne Roberts |
0:6b3ac9c5a042 | 62 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 63 | |
Wayne Roberts |
0:6b3ac9c5a042 | 64 | return ret; |
Wayne Roberts |
0:6b3ac9c5a042 | 65 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 66 | |
Wayne Roberts |
0:6b3ac9c5a042 | 67 | void Radio::LoRaConfig(uint32_t bandwidth, uint8_t datarate, uint8_t coderate, uint16_t preambleLen, bool fixLen, bool crcOn) |
Wayne Roberts |
0:6b3ac9c5a042 | 68 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 69 | float sp; |
Wayne Roberts |
0:6b3ac9c5a042 | 70 | |
Wayne Roberts |
0:6b3ac9c5a042 | 71 | if (!radio.RegOpMode.bits.LongRangeMode) { |
Wayne Roberts |
0:6b3ac9c5a042 | 72 | lora.enable(); |
Wayne Roberts |
0:6b3ac9c5a042 | 73 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 74 | radio.write_reg(REG_LR_IRQFLAGSMASK, 0); |
Wayne Roberts |
0:6b3ac9c5a042 | 75 | |
Wayne Roberts |
0:6b3ac9c5a042 | 76 | MAC_PRINTF("sf%u bw%u ", datarate, bandwidth); |
Wayne Roberts |
0:6b3ac9c5a042 | 77 | lora.RegModemConfig2.sx1276bits.SpreadingFactor = datarate; |
Wayne Roberts |
0:6b3ac9c5a042 | 78 | if (radio.type == SX1276) { |
Wayne Roberts |
0:6b3ac9c5a042 | 79 | MAC_PRINTF("sx1276 "); |
Wayne Roberts |
0:6b3ac9c5a042 | 80 | lora.RegModemConfig2.sx1276bits.RxPayloadCrcOn = crcOn; |
Wayne Roberts |
0:6b3ac9c5a042 | 81 | lora.RegModemConfig.sx1276bits.ImplicitHeaderModeOn = fixLen; |
Wayne Roberts |
0:6b3ac9c5a042 | 82 | lora.RegModemConfig.sx1276bits.CodingRate = coderate; |
Wayne Roberts |
0:6b3ac9c5a042 | 83 | lora.RegModemConfig.sx1276bits.Bw = bandwidth + 7; |
Wayne Roberts |
0:6b3ac9c5a042 | 84 | lora.RegModemConfig3.octet = radio.read_reg(REG_LR_MODEMCONFIG3); |
Wayne Roberts |
0:6b3ac9c5a042 | 85 | sp = lora.get_symbol_period(); |
Wayne Roberts |
0:6b3ac9c5a042 | 86 | if (sp > 16) |
Wayne Roberts |
0:6b3ac9c5a042 | 87 | lora.RegModemConfig3.sx1276bits.LowDataRateOptimize = 1; |
Wayne Roberts |
0:6b3ac9c5a042 | 88 | else |
Wayne Roberts |
0:6b3ac9c5a042 | 89 | lora.RegModemConfig3.sx1276bits.LowDataRateOptimize = 0; |
Wayne Roberts |
0:6b3ac9c5a042 | 90 | radio.write_reg(REG_LR_MODEMCONFIG3, lora.RegModemConfig3.octet); |
Wayne Roberts |
0:6b3ac9c5a042 | 91 | } else if (radio.type == SX1272) { |
Wayne Roberts |
0:6b3ac9c5a042 | 92 | lora.RegModemConfig.sx1272bits.RxPayloadCrcOn = crcOn; |
Wayne Roberts |
0:6b3ac9c5a042 | 93 | lora.RegModemConfig.sx1272bits.ImplicitHeaderModeOn = fixLen; |
Wayne Roberts |
0:6b3ac9c5a042 | 94 | lora.RegModemConfig.sx1272bits.CodingRate = coderate; |
Wayne Roberts |
0:6b3ac9c5a042 | 95 | lora.RegModemConfig.sx1272bits.Bw = bandwidth; |
Wayne Roberts |
0:6b3ac9c5a042 | 96 | if (lora.get_symbol_period() > 16) |
Wayne Roberts |
0:6b3ac9c5a042 | 97 | lora.RegModemConfig.sx1272bits.LowDataRateOptimize = 1; |
Wayne Roberts |
0:6b3ac9c5a042 | 98 | else |
Wayne Roberts |
0:6b3ac9c5a042 | 99 | lora.RegModemConfig.sx1272bits.LowDataRateOptimize = 0; |
Wayne Roberts |
0:6b3ac9c5a042 | 100 | MAC_PRINTF("sx1272 "); |
Wayne Roberts |
0:6b3ac9c5a042 | 101 | } else { |
Wayne Roberts |
0:6b3ac9c5a042 | 102 | MAC_PRINTF("\e[31msx127?\e[0m "); |
Wayne Roberts |
0:6b3ac9c5a042 | 103 | return; |
Wayne Roberts |
0:6b3ac9c5a042 | 104 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 105 | |
Wayne Roberts |
0:6b3ac9c5a042 | 106 | //printf(" {%02x %02x} ", lora.RegModemConfig.octet, lora.RegModemConfig2.octet); |
Wayne Roberts |
0:6b3ac9c5a042 | 107 | radio.write_reg(REG_LR_MODEMCONFIG, lora.RegModemConfig.octet); |
Wayne Roberts |
0:6b3ac9c5a042 | 108 | radio.write_reg(REG_LR_MODEMCONFIG2, lora.RegModemConfig2.octet); |
Wayne Roberts |
0:6b3ac9c5a042 | 109 | |
Wayne Roberts |
0:6b3ac9c5a042 | 110 | lora.RegPreamble = preambleLen; |
Wayne Roberts |
0:6b3ac9c5a042 | 111 | radio.write_u16(REG_LR_PREAMBLEMSB, lora.RegPreamble); |
Wayne Roberts |
0:6b3ac9c5a042 | 112 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 113 | |
Wayne Roberts |
0:6b3ac9c5a042 | 114 | void Radio::SetRxConfig( |
Wayne Roberts |
0:6b3ac9c5a042 | 115 | RadioModems_t modem, |
Wayne Roberts |
0:6b3ac9c5a042 | 116 | uint32_t bandwidth, |
Wayne Roberts |
0:6b3ac9c5a042 | 117 | uint32_t datarate, |
Wayne Roberts |
0:6b3ac9c5a042 | 118 | uint8_t coderate, |
Wayne Roberts |
0:6b3ac9c5a042 | 119 | uint32_t bandwidthAfc, |
Wayne Roberts |
0:6b3ac9c5a042 | 120 | uint16_t preambleLen, |
Wayne Roberts |
0:6b3ac9c5a042 | 121 | uint16_t symbTimeout, |
Wayne Roberts |
0:6b3ac9c5a042 | 122 | bool fixLen, |
Wayne Roberts |
0:6b3ac9c5a042 | 123 | uint8_t payloadLen, |
Wayne Roberts |
0:6b3ac9c5a042 | 124 | bool crcOn, |
Wayne Roberts |
0:6b3ac9c5a042 | 125 | bool iqInverted) |
Wayne Roberts |
0:6b3ac9c5a042 | 126 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 127 | if (modem == MODEM_FSK) { |
Wayne Roberts |
0:6b3ac9c5a042 | 128 | if (radio.RegOpMode.bits.LongRangeMode) |
Wayne Roberts |
0:6b3ac9c5a042 | 129 | fsk.enable(false); |
Wayne Roberts |
0:6b3ac9c5a042 | 130 | /* TODO */ |
Wayne Roberts |
0:6b3ac9c5a042 | 131 | //uint32_t bandwidthAfc, |
Wayne Roberts |
0:6b3ac9c5a042 | 132 | } else if (modem == MODEM_LORA) { |
Wayne Roberts |
0:6b3ac9c5a042 | 133 | uint16_t reg_u16; |
Wayne Roberts |
0:6b3ac9c5a042 | 134 | |
Wayne Roberts |
0:6b3ac9c5a042 | 135 | LoRaConfig(bandwidth, datarate, coderate, preambleLen, fixLen, crcOn); |
Wayne Roberts |
0:6b3ac9c5a042 | 136 | |
Wayne Roberts |
0:6b3ac9c5a042 | 137 | reg_u16 = radio.read_u16(REG_LR_MODEMCONFIG2); |
Wayne Roberts |
0:6b3ac9c5a042 | 138 | reg_u16 &= 0xfc00; |
Wayne Roberts |
0:6b3ac9c5a042 | 139 | reg_u16 |= symbTimeout; |
Wayne Roberts |
0:6b3ac9c5a042 | 140 | radio.write_u16(REG_LR_MODEMCONFIG2, reg_u16); |
Wayne Roberts |
0:6b3ac9c5a042 | 141 | //MAC_PRINTF("rxconf symbt %02x %02x mask:%02x\r\n", radio.read_reg(REG_LR_MODEMCONFIG2), radio.read_reg(REG_LR_MODEMCONFIG2+1), radio.read_reg(REG_LR_IRQFLAGSMASK)); |
Wayne Roberts |
0:6b3ac9c5a042 | 142 | |
Wayne Roberts |
0:6b3ac9c5a042 | 143 | lora.invert_rx(iqInverted); |
Wayne Roberts |
0:6b3ac9c5a042 | 144 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 145 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 146 | |
Wayne Roberts |
0:6b3ac9c5a042 | 147 | void Radio::SetTxConfig( |
Wayne Roberts |
0:6b3ac9c5a042 | 148 | RadioModems_t modem, |
Wayne Roberts |
0:6b3ac9c5a042 | 149 | int8_t dbm, |
Wayne Roberts |
0:6b3ac9c5a042 | 150 | uint32_t fdev, |
Wayne Roberts |
0:6b3ac9c5a042 | 151 | uint32_t bandwidth, |
Wayne Roberts |
0:6b3ac9c5a042 | 152 | uint32_t datarate, |
Wayne Roberts |
0:6b3ac9c5a042 | 153 | uint8_t coderate, |
Wayne Roberts |
0:6b3ac9c5a042 | 154 | uint16_t preambleLen, |
Wayne Roberts |
0:6b3ac9c5a042 | 155 | bool fixLen, |
Wayne Roberts |
0:6b3ac9c5a042 | 156 | bool crcOn, |
Wayne Roberts |
0:6b3ac9c5a042 | 157 | bool iqInverted) |
Wayne Roberts |
0:6b3ac9c5a042 | 158 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 159 | set_tx_dbm(dbm); |
Wayne Roberts |
0:6b3ac9c5a042 | 160 | |
Wayne Roberts |
0:6b3ac9c5a042 | 161 | if (modem == MODEM_FSK) { |
Wayne Roberts |
0:6b3ac9c5a042 | 162 | if (radio.RegOpMode.bits.LongRangeMode) |
Wayne Roberts |
0:6b3ac9c5a042 | 163 | fsk.enable(false); |
Wayne Roberts |
0:6b3ac9c5a042 | 164 | /* TODO */ |
Wayne Roberts |
0:6b3ac9c5a042 | 165 | } else if (modem == MODEM_LORA) { |
Wayne Roberts |
0:6b3ac9c5a042 | 166 | LoRaConfig(bandwidth, datarate, coderate, preambleLen, fixLen, crcOn); |
Wayne Roberts |
0:6b3ac9c5a042 | 167 | |
Wayne Roberts |
0:6b3ac9c5a042 | 168 | lora.invert_tx(iqInverted); |
Wayne Roberts |
0:6b3ac9c5a042 | 169 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 170 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 171 | |
Wayne Roberts |
0:6b3ac9c5a042 | 172 | void Radio::SetRxMaxPayloadLength(RadioModems_t modem, uint8_t max) |
Wayne Roberts |
0:6b3ac9c5a042 | 173 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 174 | /* TODO fsk */ |
Wayne Roberts |
0:6b3ac9c5a042 | 175 | radio.write_reg(REG_LR_RX_MAX_PAYLOADLENGTH, max); |
Wayne Roberts |
0:6b3ac9c5a042 | 176 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 177 | |
Wayne Roberts |
0:6b3ac9c5a042 | 178 | const RadioEvents_t* RadioEvents; |
Wayne Roberts |
0:6b3ac9c5a042 | 179 | |
Wayne Roberts |
0:6b3ac9c5a042 | 180 | |
Wayne Roberts |
0:6b3ac9c5a042 | 181 | #if 0 |
Wayne Roberts |
0:6b3ac9c5a042 | 182 | void dumpregs() |
Wayne Roberts |
0:6b3ac9c5a042 | 183 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 184 | unsigned i; |
Wayne Roberts |
0:6b3ac9c5a042 | 185 | //MAC_PRINTF("%02x) %02x\r\n", i, radio.read_reg(i)); |
Wayne Roberts |
0:6b3ac9c5a042 | 186 | for (i = 0; i < 0x70; i++) { |
Wayne Roberts |
0:6b3ac9c5a042 | 187 | MAC_PRINTF("%02x) %02x\r\n", i, radio.read_reg(i)); |
Wayne Roberts |
0:6b3ac9c5a042 | 188 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 189 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 190 | #endif /* if 0 */ |
Wayne Roberts |
0:6b3ac9c5a042 | 191 | |
Wayne Roberts |
0:6b3ac9c5a042 | 192 | //volatile bool fooEnable = false; |
Wayne Roberts |
0:6b3ac9c5a042 | 193 | |
Wayne Roberts |
0:6b3ac9c5a042 | 194 | void Radio::dio0callback() |
Wayne Roberts |
0:6b3ac9c5a042 | 195 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 196 | us_timestamp_t now = lpt.read_us(); |
Wayne Roberts |
0:6b3ac9c5a042 | 197 | |
Wayne Roberts |
0:6b3ac9c5a042 | 198 | lora.RegIrqFlags.octet = radio.read_reg(REG_LR_IRQFLAGS); |
Wayne Roberts |
0:6b3ac9c5a042 | 199 | |
Wayne Roberts |
0:6b3ac9c5a042 | 200 | if (radio.RegOpMode.bits.Mode == RF_OPMODE_RECEIVER || |
Wayne Roberts |
0:6b3ac9c5a042 | 201 | radio.RegOpMode.bits.Mode == RF_OPMODE_RECEIVER_SINGLE) |
Wayne Roberts |
0:6b3ac9c5a042 | 202 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 203 | lora.service(); |
Wayne Roberts |
0:6b3ac9c5a042 | 204 | if (!lora.RegIrqFlags.bits.RxDone) { |
Wayne Roberts |
0:6b3ac9c5a042 | 205 | MAC_PRINTF("not-rxdone\r\n"); |
Wayne Roberts |
0:6b3ac9c5a042 | 206 | } else if (RadioEvents->RxDone) { |
Wayne Roberts |
0:6b3ac9c5a042 | 207 | int8_t rssi; |
Wayne Roberts |
0:6b3ac9c5a042 | 208 | float snr = lora.RegPktSnrValue / 4.0; |
Wayne Roberts |
0:6b3ac9c5a042 | 209 | |
Wayne Roberts |
0:6b3ac9c5a042 | 210 | rssi = lora.get_pkt_rssi(); |
Wayne Roberts |
0:6b3ac9c5a042 | 211 | if (snr < 0) |
Wayne Roberts |
0:6b3ac9c5a042 | 212 | rssi += snr; |
Wayne Roberts |
0:6b3ac9c5a042 | 213 | //MAC_PRINTF("rxdone snr:%.2f, rssi:%d\r\n", snr, rssi); |
Wayne Roberts |
0:6b3ac9c5a042 | 214 | //fooEnable = true; |
Wayne Roberts |
0:6b3ac9c5a042 | 215 | RadioEvents->RxDone(radio.rx_buf, lora.RegRxNbBytes, rssi, snr, now); |
Wayne Roberts |
0:6b3ac9c5a042 | 216 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 217 | } else if (radio.RegOpMode.bits.Mode == RF_OPMODE_TRANSMITTER) { |
Wayne Roberts |
0:6b3ac9c5a042 | 218 | if (!lora.RegIrqFlags.bits.TxDone) { |
Wayne Roberts |
0:6b3ac9c5a042 | 219 | MAC_PRINTF("not-txdone\r\n"); |
Wayne Roberts |
0:6b3ac9c5a042 | 220 | } else if (RadioEvents->TxDone) |
Wayne Roberts |
0:6b3ac9c5a042 | 221 | RadioEvents->TxDone(now); |
Wayne Roberts |
0:6b3ac9c5a042 | 222 | lora.service(); |
Wayne Roberts |
0:6b3ac9c5a042 | 223 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 224 | } // ..dio0callback() |
Wayne Roberts |
0:6b3ac9c5a042 | 225 | |
Wayne Roberts |
0:6b3ac9c5a042 | 226 | void Radio::dio1callback() |
Wayne Roberts |
0:6b3ac9c5a042 | 227 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 228 | lora.RegIrqFlags.octet = radio.read_reg(REG_LR_IRQFLAGS); |
Wayne Roberts |
0:6b3ac9c5a042 | 229 | |
Wayne Roberts |
0:6b3ac9c5a042 | 230 | MAC_PRINTF("\e[7mdio1:%02x\e[0m\r\n", lora.RegIrqFlags.octet); |
Wayne Roberts |
0:6b3ac9c5a042 | 231 | if (!lora.RegIrqFlags.bits.RxTimeout) { |
Wayne Roberts |
0:6b3ac9c5a042 | 232 | MAC_PRINTF("dio1-not-rxtimeout\r\n"); |
Wayne Roberts |
0:6b3ac9c5a042 | 233 | } else if (RadioEvents->RxTimeout) |
Wayne Roberts |
0:6b3ac9c5a042 | 234 | RadioEvents->RxTimeout(); |
Wayne Roberts |
0:6b3ac9c5a042 | 235 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 236 | |
Wayne Roberts |
0:6b3ac9c5a042 | 237 | void Radio::Init(const RadioEvents_t* e) |
Wayne Roberts |
0:6b3ac9c5a042 | 238 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 239 | dio0.rise(dio0callback); |
Wayne Roberts |
0:6b3ac9c5a042 | 240 | dio1.rise(dio1callback); |
Wayne Roberts |
0:6b3ac9c5a042 | 241 | |
Wayne Roberts |
0:6b3ac9c5a042 | 242 | radio.rf_switch = rfsw_callback; |
Wayne Roberts |
0:6b3ac9c5a042 | 243 | boardInit(); |
Wayne Roberts |
0:6b3ac9c5a042 | 244 | |
Wayne Roberts |
0:6b3ac9c5a042 | 245 | RadioEvents = e; |
Wayne Roberts |
0:6b3ac9c5a042 | 246 | lpt.start(); |
Wayne Roberts |
0:6b3ac9c5a042 | 247 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 248 | |
Wayne Roberts |
0:6b3ac9c5a042 | 249 | void Radio::Send(uint8_t size) |
Wayne Roberts |
0:6b3ac9c5a042 | 250 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 251 | if (radio.RegOpMode.bits.Mode == RF_OPMODE_SLEEP) { |
Wayne Roberts |
0:6b3ac9c5a042 | 252 | radio.set_opmode(RF_OPMODE_STANDBY); |
Wayne Roberts |
0:6b3ac9c5a042 | 253 | wait_us(1000); |
Wayne Roberts |
0:6b3ac9c5a042 | 254 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 255 | //MAC_PRINTF("Radio::Send() dio:%u:%u opmode:%02x ", radio.dio0.read(), radio.dio1.read(), radio.read_reg(REG_OPMODE)); |
Wayne Roberts |
0:6b3ac9c5a042 | 256 | lora.RegPayloadLength = size; |
Wayne Roberts |
0:6b3ac9c5a042 | 257 | radio.write_reg(REG_LR_PAYLOADLENGTH, lora.RegPayloadLength); |
Wayne Roberts |
0:6b3ac9c5a042 | 258 | lora.start_tx(size); |
Wayne Roberts |
0:6b3ac9c5a042 | 259 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 260 | |
Wayne Roberts |
0:6b3ac9c5a042 | 261 | /*volatile RadioState_t state; |
Wayne Roberts |
0:6b3ac9c5a042 | 262 | |
Wayne Roberts |
0:6b3ac9c5a042 | 263 | RadioState_t Radio::GetStatus() |
Wayne Roberts |
0:6b3ac9c5a042 | 264 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 265 | return state; |
Wayne Roberts |
0:6b3ac9c5a042 | 266 | }*/ |
Wayne Roberts |
0:6b3ac9c5a042 | 267 | |
Wayne Roberts |
0:6b3ac9c5a042 | 268 | void Radio::Rx(unsigned timeout) |
Wayne Roberts |
0:6b3ac9c5a042 | 269 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 270 | if (timeout == 0) |
Wayne Roberts |
0:6b3ac9c5a042 | 271 | lora.start_rx(RF_OPMODE_RECEIVER); |
Wayne Roberts |
0:6b3ac9c5a042 | 272 | else |
Wayne Roberts |
0:6b3ac9c5a042 | 273 | lora.start_rx(RF_OPMODE_RECEIVER_SINGLE); |
Wayne Roberts |
0:6b3ac9c5a042 | 274 | |
Wayne Roberts |
0:6b3ac9c5a042 | 275 | MAC_PRINTF("start_rx "); |
Wayne Roberts |
0:6b3ac9c5a042 | 276 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 277 | |
Wayne Roberts |
0:6b3ac9c5a042 | 278 | |
Wayne Roberts |
0:6b3ac9c5a042 | 279 | void Radio::ocp(uint8_t ma) |
Wayne Roberts |
0:6b3ac9c5a042 | 280 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 281 | if (ma < 130) |
Wayne Roberts |
0:6b3ac9c5a042 | 282 | radio.RegOcp.bits.OcpTrim = (ma - 45) / 5; |
Wayne Roberts |
0:6b3ac9c5a042 | 283 | else |
Wayne Roberts |
0:6b3ac9c5a042 | 284 | radio.RegOcp.bits.OcpTrim = (ma + 30) / 10; |
Wayne Roberts |
0:6b3ac9c5a042 | 285 | radio.write_reg(REG_OCP, radio.RegOcp.octet); |
Wayne Roberts |
0:6b3ac9c5a042 | 286 | |
Wayne Roberts |
0:6b3ac9c5a042 | 287 | radio.RegOcp.octet = radio.read_reg(REG_OCP); |
Wayne Roberts |
0:6b3ac9c5a042 | 288 | if (radio.RegOcp.bits.OcpTrim < 16) |
Wayne Roberts |
0:6b3ac9c5a042 | 289 | ma = 45 + (5 * radio.RegOcp.bits.OcpTrim); |
Wayne Roberts |
0:6b3ac9c5a042 | 290 | else if (radio.RegOcp.bits.OcpTrim < 28) |
Wayne Roberts |
0:6b3ac9c5a042 | 291 | ma = (10 * radio.RegOcp.bits.OcpTrim) - 30; |
Wayne Roberts |
0:6b3ac9c5a042 | 292 | else |
Wayne Roberts |
0:6b3ac9c5a042 | 293 | ma = 240; |
Wayne Roberts |
0:6b3ac9c5a042 | 294 | //MAC_PRINTF("Ocp: %dmA\r\n", ma); |
Wayne Roberts |
0:6b3ac9c5a042 | 295 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 296 | |
Wayne Roberts |
0:6b3ac9c5a042 | 297 | |
Wayne Roberts |
0:6b3ac9c5a042 | 298 | void Radio::PrintStatus() |
Wayne Roberts |
0:6b3ac9c5a042 | 299 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 300 | MAC_PRINTF("dio:%u:%u opmode:%02x ", radio.dio0.read(), radio.dio1.read(), radio.read_reg(REG_OPMODE)); |
Wayne Roberts |
0:6b3ac9c5a042 | 301 | lora.RegIrqFlags.octet = radio.read_reg(REG_LR_IRQFLAGS); |
Wayne Roberts |
0:6b3ac9c5a042 | 302 | MAC_PRINTF("irqFlags:%02x\r\n", lora.RegIrqFlags.octet); |
Wayne Roberts |
0:6b3ac9c5a042 | 303 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 304 | |
Wayne Roberts |
0:6b3ac9c5a042 | 305 | #ifdef DUTY_ENABLE |
Wayne Roberts |
0:6b3ac9c5a042 | 306 | us_timestamp_t |
Wayne Roberts |
0:6b3ac9c5a042 | 307 | Radio::TimeOnAir(RadioModems_t m, uint8_t pktLen) |
Wayne Roberts |
0:6b3ac9c5a042 | 308 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 309 | uint32_t airTime = 0; |
Wayne Roberts |
0:6b3ac9c5a042 | 310 | |
Wayne Roberts |
0:6b3ac9c5a042 | 311 | switch (m) |
Wayne Roberts |
0:6b3ac9c5a042 | 312 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 313 | case MODEM_FSK: |
Wayne Roberts |
0:6b3ac9c5a042 | 314 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 315 | /* TODO |
Wayne Roberts |
0:6b3ac9c5a042 | 316 | airTime = round( ( 8 * ( SX1272.Settings.Fsk.PreambleLen + |
Wayne Roberts |
0:6b3ac9c5a042 | 317 | ( ( SX1272Read( REG_SYNCCONFIG ) & ~RF_SYNCCONFIG_SYNCSIZE_MASK ) + 1 ) + |
Wayne Roberts |
0:6b3ac9c5a042 | 318 | ( ( SX1272.Settings.Fsk.FixLen == 0x01 ) ? 0.0 : 1.0 ) + |
Wayne Roberts |
0:6b3ac9c5a042 | 319 | ( ( ( SX1272Read( REG_PACKETCONFIG1 ) & ~RF_PACKETCONFIG1_ADDRSFILTERING_MASK ) != 0x00 ) ? 1.0 : 0 ) + |
Wayne Roberts |
0:6b3ac9c5a042 | 320 | pktLen + |
Wayne Roberts |
0:6b3ac9c5a042 | 321 | ( ( SX1272.Settings.Fsk.CrcOn == 0x01 ) ? 2.0 : 0 ) ) / |
Wayne Roberts |
0:6b3ac9c5a042 | 322 | SX1272.Settings.Fsk.Datarate ) * 1e3 ); |
Wayne Roberts |
0:6b3ac9c5a042 | 323 | */ |
Wayne Roberts |
0:6b3ac9c5a042 | 324 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 325 | break; |
Wayne Roberts |
0:6b3ac9c5a042 | 326 | case MODEM_LORA: |
Wayne Roberts |
0:6b3ac9c5a042 | 327 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 328 | double bw = 0.0; |
Wayne Roberts |
0:6b3ac9c5a042 | 329 | uint8_t fixLen, bandwidth, LowDatarateOptimize, coderate, crcOn ; |
Wayne Roberts |
0:6b3ac9c5a042 | 330 | if (radio.type == SX1276) { |
Wayne Roberts |
0:6b3ac9c5a042 | 331 | coderate = lora.RegModemConfig.sx1276bits.CodingRate; |
Wayne Roberts |
0:6b3ac9c5a042 | 332 | LowDatarateOptimize = lora.RegModemConfig3.sx1276bits.LowDataRateOptimize; |
Wayne Roberts |
0:6b3ac9c5a042 | 333 | fixLen = lora.RegModemConfig.sx1276bits.ImplicitHeaderModeOn; |
Wayne Roberts |
0:6b3ac9c5a042 | 334 | bandwidth = lora.RegModemConfig.sx1276bits.Bw - 7; |
Wayne Roberts |
0:6b3ac9c5a042 | 335 | crcOn = lora.RegModemConfig2.sx1276bits.RxPayloadCrcOn; |
Wayne Roberts |
0:6b3ac9c5a042 | 336 | } else if (radio.type == SX1272) { |
Wayne Roberts |
0:6b3ac9c5a042 | 337 | coderate = lora.RegModemConfig.sx1272bits.CodingRate; |
Wayne Roberts |
0:6b3ac9c5a042 | 338 | LowDatarateOptimize = lora.RegModemConfig.sx1272bits.LowDataRateOptimize; |
Wayne Roberts |
0:6b3ac9c5a042 | 339 | fixLen = lora.RegModemConfig.sx1272bits.ImplicitHeaderModeOn; |
Wayne Roberts |
0:6b3ac9c5a042 | 340 | bandwidth = lora.RegModemConfig.sx1272bits.Bw; |
Wayne Roberts |
0:6b3ac9c5a042 | 341 | crcOn = lora.RegModemConfig.sx1272bits.RxPayloadCrcOn; |
Wayne Roberts |
0:6b3ac9c5a042 | 342 | } else |
Wayne Roberts |
0:6b3ac9c5a042 | 343 | return 0; |
Wayne Roberts |
0:6b3ac9c5a042 | 344 | |
Wayne Roberts |
0:6b3ac9c5a042 | 345 | switch( bandwidth ) |
Wayne Roberts |
0:6b3ac9c5a042 | 346 | { |
Wayne Roberts |
0:6b3ac9c5a042 | 347 | case 0: // 125 kHz |
Wayne Roberts |
0:6b3ac9c5a042 | 348 | bw = 125;//ms: 125e3; |
Wayne Roberts |
0:6b3ac9c5a042 | 349 | break; |
Wayne Roberts |
0:6b3ac9c5a042 | 350 | case 1: // 250 kHz |
Wayne Roberts |
0:6b3ac9c5a042 | 351 | bw = 250;//ms:250e3; |
Wayne Roberts |
0:6b3ac9c5a042 | 352 | break; |
Wayne Roberts |
0:6b3ac9c5a042 | 353 | case 2: // 500 kHz |
Wayne Roberts |
0:6b3ac9c5a042 | 354 | bw = 500;//ms:500e3; |
Wayne Roberts |
0:6b3ac9c5a042 | 355 | break; |
Wayne Roberts |
0:6b3ac9c5a042 | 356 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 357 | |
Wayne Roberts |
0:6b3ac9c5a042 | 358 | // Symbol rate : time for one symbol (secs) |
Wayne Roberts |
0:6b3ac9c5a042 | 359 | double rs = bw / ( 1 << lora.RegModemConfig2.sx1276bits.SpreadingFactor ); |
Wayne Roberts |
0:6b3ac9c5a042 | 360 | double ts = 1 / rs; |
Wayne Roberts |
0:6b3ac9c5a042 | 361 | // time of preamble |
Wayne Roberts |
0:6b3ac9c5a042 | 362 | double tPreamble; |
Wayne Roberts |
0:6b3ac9c5a042 | 363 | tPreamble = (lora.RegPreamble + 4.25 ) * ts; |
Wayne Roberts |
0:6b3ac9c5a042 | 364 | // Symbol length of payload and time |
Wayne Roberts |
0:6b3ac9c5a042 | 365 | double tmp = ceil( ( 8 * pktLen - 4 * lora.RegModemConfig2.sx1276bits.SpreadingFactor + |
Wayne Roberts |
0:6b3ac9c5a042 | 366 | 28 + 16 * crcOn - |
Wayne Roberts |
0:6b3ac9c5a042 | 367 | ( fixLen ? 20 : 0 ) ) / |
Wayne Roberts |
0:6b3ac9c5a042 | 368 | ( double )( 4 * ( lora.RegModemConfig2.sx1276bits.SpreadingFactor - |
Wayne Roberts |
0:6b3ac9c5a042 | 369 | ( ( LowDatarateOptimize > 0 ) ? 2 : 0 ) ) ) ) * |
Wayne Roberts |
0:6b3ac9c5a042 | 370 | ( coderate + 4 ); |
Wayne Roberts |
0:6b3ac9c5a042 | 371 | double nPayload = 8 + ( ( tmp > 0 ) ? tmp : 0 ); |
Wayne Roberts |
0:6b3ac9c5a042 | 372 | double tPayload = nPayload * ts; |
Wayne Roberts |
0:6b3ac9c5a042 | 373 | // Time on air |
Wayne Roberts |
0:6b3ac9c5a042 | 374 | double tOnAir = tPreamble + tPayload; |
Wayne Roberts |
0:6b3ac9c5a042 | 375 | // return ms secs |
Wayne Roberts |
0:6b3ac9c5a042 | 376 | airTime = floor( tOnAir * 1e3 + 0.999 ); |
Wayne Roberts |
0:6b3ac9c5a042 | 377 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 378 | break; |
Wayne Roberts |
0:6b3ac9c5a042 | 379 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 380 | return airTime; |
Wayne Roberts |
0:6b3ac9c5a042 | 381 | |
Wayne Roberts |
0:6b3ac9c5a042 | 382 | } |
Wayne Roberts |
0:6b3ac9c5a042 | 383 | #endif /* DUTY_ENABLE */ |
Wayne Roberts |
0:6b3ac9c5a042 | 384 |