|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "machine" |
| 6 | + "runtime" |
| 7 | + "time" |
| 8 | + |
| 9 | + "tinygo.org/x/drivers/sx128x" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + // pin mapping specific to the lilygo t3s3, change as needed for your board |
| 14 | + sdoPin = machine.GPIO6 |
| 15 | + sdiPin = machine.GPIO3 |
| 16 | + sckPin = machine.GPIO5 |
| 17 | + nssPin = machine.GPIO7 |
| 18 | + busyPin = machine.GPIO36 |
| 19 | + resetPin = machine.GPIO8 |
| 20 | + dio1Pin = machine.GPIO9 |
| 21 | +) |
| 22 | + |
| 23 | +func setupPins() { |
| 24 | + nssPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) |
| 25 | + nssPin.Set(true) |
| 26 | + |
| 27 | + resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) |
| 28 | + resetPin.Set(true) |
| 29 | + |
| 30 | + busyPin.Configure(machine.PinConfig{Mode: machine.PinInput}) |
| 31 | + dio1Pin.Configure(machine.PinConfig{Mode: machine.PinInput}) |
| 32 | +} |
| 33 | + |
| 34 | +func main() { |
| 35 | + setupPins() |
| 36 | + |
| 37 | + spi := machine.SPI0 |
| 38 | + spi.Configure(machine.SPIConfig{ |
| 39 | + Mode: 0, |
| 40 | + Frequency: 8 * 1e6, |
| 41 | + SDO: sdoPin, |
| 42 | + SDI: sdiPin, |
| 43 | + SCK: sckPin, |
| 44 | + }) |
| 45 | + |
| 46 | + radio := sx128x.New( |
| 47 | + spi, |
| 48 | + nssPin, |
| 49 | + resetPin, |
| 50 | + busyPin, |
| 51 | + ) |
| 52 | + |
| 53 | + radio.WaitWhileBusy(time.Second) |
| 54 | + SetupLora(radio) |
| 55 | + |
| 56 | + for { |
| 57 | + data, err := Rx(radio) |
| 58 | + if err != nil { |
| 59 | + println("failed to receive:", err) |
| 60 | + } else { |
| 61 | + println("received:", string(data)) |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func SetupLora(radio *sx128x.Device) { |
| 67 | + radio.SetStandby(sx128x.STANDBY_RC) |
| 68 | + radio.SetPacketType(sx128x.PACKET_TYPE_LORA) |
| 69 | + radio.SetRegulatorMode(sx128x.REGULATOR_DC_DC) |
| 70 | + |
| 71 | + radio.SetRfFrequency(2400000000) // 2.4Ghz |
| 72 | + radio.SetModulationParamsLoRa(sx128x.LORA_SF_9, sx128x.LORA_BW_1600, sx128x.LORA_CR_4_7) |
| 73 | + |
| 74 | + // section 14.4.1 shows required register setting for setting up LoRa operations. These depend on the chosen spreading factor. |
| 75 | + radio.WriteRegister(0x925, []byte{0x32}) |
| 76 | + radio.WriteRegister(0x93C, []byte{0x01}) |
| 77 | + |
| 78 | + radio.SetTxParams(13, sx128x.RADIO_RAMP_02_US) |
| 79 | + radio.SetPacketParamsLoRa(12, sx128x.LORA_HEADER_EXPLICIT, 0xFF, sx128x.LORA_CRC_DISABLE, sx128x.LORA_IQ_STD) |
| 80 | + radio.WriteRegister(sx128x.REG_LORA_SYNC_WORD_MSB, []byte{0x14, 0x24}) // full sync word is 0x1424 |
| 81 | + |
| 82 | +} |
| 83 | + |
| 84 | +func Rx(radio *sx128x.Device) ([]byte, error) { |
| 85 | + radio.SetStandby(sx128x.STANDBY_RC) |
| 86 | + radio.SetDioIrqParams(sx128x.IRQ_RX_DONE_MASK|sx128x.IRQ_RX_TX_TIMEOUT_MASK, sx128x.IRQ_RX_DONE_MASK|sx128x.IRQ_RX_TX_TIMEOUT_MASK, 0x00, 0x00) |
| 87 | + radio.SetBufferBaseAddress(0, 0) |
| 88 | + radio.ClearIrqStatus(sx128x.IRQ_ALL_MASK) |
| 89 | + radio.SetRx(sx128x.PERIOD_BASE_4_MS, 250) // 4ms * 250 = 1s |
| 90 | + // busy wait for IRQ indication |
| 91 | + for dio1Pin.Get() == false { |
| 92 | + runtime.Gosched() |
| 93 | + } |
| 94 | + irqStatus, _ := radio.GetIrqStatus() |
| 95 | + if irqStatus&sx128x.IRQ_RX_DONE_MASK != 0 { |
| 96 | + payloadLength, bufferOffset, err := radio.GetRxBufferStatus() |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + data, err := radio.ReadBuffer(bufferOffset, payloadLength) |
| 101 | + return data, nil |
| 102 | + } else if irqStatus&sx128x.IRQ_RX_TX_TIMEOUT_MASK != 0 { |
| 103 | + return nil, errors.New("rx timeout") |
| 104 | + } |
| 105 | + return nil, errors.New("unexpected IRQ status") |
| 106 | +} |
0 commit comments