acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
SX1280.cpp (5166B)
1 #include "SX1280.h" 2 #if !defined(RADIOLIB_EXCLUDE_SX128X) 3 4 SX1280::SX1280(Module* mod) : SX1281(mod) { 5 6 } 7 8 int16_t SX1280::range(bool master, uint32_t addr) { 9 // start ranging 10 int16_t state = startRanging(master, addr); 11 RADIOLIB_ASSERT(state); 12 13 // wait until ranging is finished 14 uint32_t start = _mod->millis(); 15 while(!_mod->digitalRead(_mod->getIrq())) { 16 _mod->yield(); 17 if(_mod->millis() - start > 10000) { 18 clearIrqStatus(); 19 standby(); 20 return(RADIOLIB_ERR_RANGING_TIMEOUT); 21 } 22 } 23 24 // clear interrupt flags 25 state = clearIrqStatus(); 26 RADIOLIB_ASSERT(state); 27 28 // set mode to standby 29 state = standby(); 30 31 return(state); 32 } 33 34 int16_t SX1280::startRanging(bool master, uint32_t addr) { 35 // check active modem 36 uint8_t modem = getPacketType(); 37 if(!((modem == RADIOLIB_SX128X_PACKET_TYPE_LORA) || (modem == RADIOLIB_SX128X_PACKET_TYPE_RANGING))) { 38 return(RADIOLIB_ERR_WRONG_MODEM); 39 } 40 41 // set mode to standby 42 int16_t state = standby(); 43 RADIOLIB_ASSERT(state); 44 45 // ensure modem is set to ranging 46 if(modem == RADIOLIB_SX128X_PACKET_TYPE_LORA) { 47 state = setPacketType(RADIOLIB_SX128X_PACKET_TYPE_RANGING); 48 RADIOLIB_ASSERT(state); 49 } 50 51 // set modulation parameters 52 state = setModulationParams(_sf, _bw, _cr); 53 RADIOLIB_ASSERT(state); 54 55 // set packet parameters 56 state = setPacketParamsLoRa(_preambleLengthLoRa, _headerType, _payloadLen, _crcLoRa); 57 RADIOLIB_ASSERT(state); 58 59 // check all address bits 60 uint8_t regValue; 61 state = readRegister(RADIOLIB_SX128X_REG_SLAVE_RANGING_ADDRESS_WIDTH, ®Value, 1); 62 RADIOLIB_ASSERT(state); 63 regValue &= 0b00111111; 64 regValue |= 0b11000000; 65 state = writeRegister(RADIOLIB_SX128X_REG_SLAVE_RANGING_ADDRESS_WIDTH, ®Value, 1); 66 RADIOLIB_ASSERT(state); 67 68 // set remaining parameter values 69 uint32_t addrReg = RADIOLIB_SX128X_REG_SLAVE_RANGING_ADDRESS_BYTE_3; 70 uint32_t irqMask = RADIOLIB_SX128X_IRQ_RANGING_SLAVE_RESP_DONE | RADIOLIB_SX128X_IRQ_RANGING_SLAVE_REQ_DISCARD; 71 uint32_t irqDio1 = RADIOLIB_SX128X_IRQ_RANGING_SLAVE_RESP_DONE; 72 if(master) { 73 addrReg = RADIOLIB_SX128X_REG_MASTER_RANGING_ADDRESS_BYTE_3; 74 irqMask = RADIOLIB_SX128X_IRQ_RANGING_MASTER_RES_VALID | RADIOLIB_SX128X_IRQ_RANGING_MASTER_TIMEOUT; 75 irqDio1 = RADIOLIB_SX128X_IRQ_RANGING_MASTER_RES_VALID; 76 } 77 78 // set ranging address 79 uint8_t addrBuff[] = { (uint8_t)((addr >> 24) & 0xFF), (uint8_t)((addr >> 16) & 0xFF), (uint8_t)((addr >> 8) & 0xFF), (uint8_t)(addr & 0xFF) }; 80 state = writeRegister(addrReg, addrBuff, 4); 81 RADIOLIB_ASSERT(state); 82 83 // set DIO mapping 84 state = setDioIrqParams(irqMask, irqDio1); 85 RADIOLIB_ASSERT(state); 86 87 // set calibration values 88 uint8_t index = (_sf >> 4) - 5; 89 static const uint16_t calTable[3][6] = { 90 { 10299, 10271, 10244, 10242, 10230, 10246 }, 91 { 11486, 11474, 11453, 11426, 11417, 11401 }, 92 { 13308, 13493, 13528, 13515, 13430, 13376 } 93 }; 94 uint16_t val = 0; 95 switch(_bw) { 96 case(RADIOLIB_SX128X_LORA_BW_406_25): 97 val = calTable[0][index]; 98 break; 99 case(RADIOLIB_SX128X_LORA_BW_812_50): 100 val = calTable[1][index]; 101 break; 102 case(RADIOLIB_SX128X_LORA_BW_1625_00): 103 val = calTable[2][index]; 104 break; 105 default: 106 return(RADIOLIB_ERR_INVALID_BANDWIDTH); 107 } 108 uint8_t calBuff[] = { (uint8_t)((val >> 8) & 0xFF), (uint8_t)(val & 0xFF) }; 109 state = writeRegister(RADIOLIB_SX128X_REG_RANGING_CALIBRATION_MSB, calBuff, 2); 110 RADIOLIB_ASSERT(state); 111 112 // set role and start ranging 113 if(master) { 114 state = setRangingRole(RADIOLIB_SX128X_RANGING_ROLE_MASTER); 115 RADIOLIB_ASSERT(state); 116 117 state = setTx(RADIOLIB_SX128X_TX_TIMEOUT_NONE); 118 RADIOLIB_ASSERT(state); 119 120 } else { 121 state = setRangingRole(RADIOLIB_SX128X_RANGING_ROLE_SLAVE); 122 RADIOLIB_ASSERT(state); 123 124 state = setRx(RADIOLIB_SX128X_RX_TIMEOUT_INF); 125 RADIOLIB_ASSERT(state); 126 127 } 128 129 return(state); 130 } 131 132 float SX1280::getRangingResult() { 133 // set mode to standby XOSC 134 int16_t state = standby(RADIOLIB_SX128X_STANDBY_XOSC); 135 RADIOLIB_ASSERT(state); 136 137 // enable clock 138 uint8_t data[4]; 139 state = readRegister(RADIOLIB_SX128X_REG_RANGING_LORA_CLOCK_ENABLE, data, 1); 140 RADIOLIB_ASSERT(state); 141 142 data[0] |= (1 << 1); 143 state = writeRegister(RADIOLIB_SX128X_REG_RANGING_LORA_CLOCK_ENABLE, data, 1); 144 RADIOLIB_ASSERT(state); 145 146 // set result type to filtered 147 state = readRegister(RADIOLIB_SX128X_REG_RANGING_TYPE, data, 1); 148 RADIOLIB_ASSERT(state); 149 150 data[0] &= 0xCF; 151 data[0] |= (1 << 4); 152 state = writeRegister(RADIOLIB_SX128X_REG_RANGING_TYPE, data, 1); 153 RADIOLIB_ASSERT(state); 154 155 // read the register values 156 state = readRegister(RADIOLIB_SX128X_REG_RANGING_RESULT_MSB, &data[0], 1); 157 RADIOLIB_ASSERT(state); 158 state = readRegister(RADIOLIB_SX128X_REG_RANGING_RESULT_MID, &data[1], 1); 159 RADIOLIB_ASSERT(state); 160 state = readRegister(RADIOLIB_SX128X_REG_RANGING_RESULT_LSB, &data[2], 1); 161 RADIOLIB_ASSERT(state); 162 163 // set mode to standby RC 164 state = standby(); 165 RADIOLIB_ASSERT(state); 166 167 // calculate the real result 168 uint32_t raw = ((uint32_t)data[0] << 16) | ((uint32_t)data[1] << 8) | data[2]; 169 return((float)raw * 150.0 / (4.096 * _bwKhz)); 170 } 171 172 #endif