anchor.diff:    ( u ) 6 lshift RF:FRF 2+ rf!
     ;
   
  -: rf-group ( u -- ) RF:SYN2 rf! ;  \ set the net group (1..250)
  +: rf-correct ( -- ) \ correct the freq based on the AFC measurement of the last packet
  +  rf.afc @ 16 lshift 16 arshift 61 *         \ AFC correction applied in Hz
  +  2 arshift                                  \ apply 1/4 of measured offset as correction
  +  5000 over 0< if negate max else min then   \ don't apply more than 5khz
  +  rf.freq @ + dup rf.freq ! rf-freq          \ apply correction
  +  ;
  +
  +: rf-group ( u -- ) RF:SYN3 rf! ;  \ set the net group (1..250)
   
   : rf-check ( b -- )  \ check that the register can be accessed over SPI
     begin  dup RF:SYN1 rf!  RF:SYN1 rf@  over = until
  
positive.code: 
  create rf:init  \ initialise the radio, each 16-bit word is <reg#,val>
  hex
    0100 h, \ opmode: sleep
    0200 h, \ packet mode, fsk
    0302 h, 048A h, \ bit rate 49,261 hz
    0505 h, 06C3 h, \ 90.3kHzFdev -> modulation index = 2
  
negative.code:   0 do  dup c@ RF:FIFO rf! 1+  loop drop ;
  
  : rf-parity ( -- u )  \ calculate group parity bits
    RF:SYN3 rf@ dup 4 lshift xor dup 2 lshift xor $C0 and ;
  
  \ this is the intended public API for the RF69 driver
  
  : rf-power ( n -- )  \ change TX power level (0..31)
    RF:PA rf@ $E0 and or RF:PA rf! ;
  
  : rf-sleep ( -- ) RF:M_SLEEP rf!mode ;  \ put radio module to sleep
  
  : rf-recv ( -- b )  \ check whether a packet has been received, return #bytes
    rf.mode @ RF:M_RX <> if
      0 rf.rssi !  0 rf.afc !
      RF:M_RX rf!mode
    else rf-rssi rf-status then
    RF:IRQ2 rf@  RF:IRQ2_CRCOK and if
      RF:FIFO rf@ 66 min \ fetch length and limit
      rf.buf over rf-n@spi
    else 0 then ;
  
  : rf-ack? ( ms -- b ) \ waits ms milliseconds for an ACK and returns #bytes recv'd
    0 rf.rssi !  0 rf.afc !
    RF:M_RX rf!mode
    0 do
      rf-status \ capture rssi, afc etc.
      RF:IRQ2 rf@  RF:IRQ2_CRCOK and if
        RF:FIFO rf@ 66 min \ fetch length and limit
        rf.buf over rf-n@spi
        unloop exit
      then
      1 ms
    loop
    RF:M_STDBY rf!mode \ kill RX
    0 ;
  
  : rf-send ( addr count hdr -- )  \ send out one packet
    RF:M_STDBY rf!mode
  