tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/sx126x/radiocontrol_pins.go (about)

     1  //go:build !stm32wlx
     2  
     3  package sx126x
     4  
     5  import (
     6  	"machine"
     7  
     8  	"time"
     9  )
    10  
    11  // RadioControl for boards that are connected using normal pins.
    12  type RadioControl struct {
    13  	nssPin, busyPin, dio1Pin   machine.Pin
    14  	rxPin, txLowPin, txHighPin machine.Pin
    15  }
    16  
    17  func NewRadioControl(nssPin, busyPin, dio1Pin,
    18  	rxPin, txLowPin, txHighPin machine.Pin) *RadioControl {
    19  	return &RadioControl{
    20  		nssPin:    nssPin,
    21  		busyPin:   busyPin,
    22  		dio1Pin:   dio1Pin,
    23  		rxPin:     rxPin,
    24  		txLowPin:  txLowPin,
    25  		txHighPin: txHighPin,
    26  	}
    27  }
    28  
    29  // SetNss sets the NSS line aka chip select for SPI.
    30  func (rc *RadioControl) SetNss(state bool) error {
    31  	rc.nssPin.Set(state)
    32  	return nil
    33  }
    34  
    35  // WaitWhileBusy wait until the radio is no longer busy
    36  func (rc *RadioControl) WaitWhileBusy() error {
    37  	count := 100
    38  	for count > 0 {
    39  		if !rc.busyPin.Get() {
    40  			return nil
    41  		}
    42  		count--
    43  		time.Sleep(time.Millisecond)
    44  	}
    45  	return errWaitWhileBusyTimeout
    46  }
    47  
    48  // Init() configures whatever needed for sx126x radio control
    49  func (rc *RadioControl) Init() error {
    50  	rc.nssPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
    51  	rc.busyPin.Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
    52  	return nil
    53  }
    54  
    55  // add interrupt handler for Radio IRQs for pins
    56  func (rc *RadioControl) SetupInterrupts(handler func()) error {
    57  	irqHandler = handler
    58  
    59  	rc.dio1Pin.Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
    60  	if err := rc.dio1Pin.SetInterrupt(machine.PinRising, handleInterrupt); err != nil {
    61  		return errRadioNotFound
    62  	}
    63  
    64  	return nil
    65  }
    66  
    67  var irqHandler func()
    68  
    69  func handleInterrupt(machine.Pin) {
    70  	irqHandler()
    71  }
    72  
    73  func (rc *RadioControl) SetRfSwitchMode(mode int) error {
    74  	switch mode {
    75  
    76  	case RFSWITCH_RX:
    77  		rc.rxPin.Set(true)
    78  		rc.txLowPin.Set(false)
    79  		rc.txHighPin.Set(false)
    80  	case RFSWITCH_TX_LP:
    81  		rc.rxPin.Set(false)
    82  		rc.txLowPin.Set(true)
    83  		rc.txHighPin.Set(false)
    84  	case RFSWITCH_TX_HP:
    85  		rc.rxPin.Set(false)
    86  		rc.txLowPin.Set(false)
    87  		rc.txHighPin.Set(true)
    88  	}
    89  
    90  	return nil
    91  }