From fcfefc47d6380fc23694bae9665c22cd02bba31a Mon Sep 17 00:00:00 2001 From: Freak Date: Thu, 11 Jun 2026 09:09:33 +0200 Subject: [PATCH 1/2] added (opinionated) nrf24l01 module code --- nrf24l01/const_bandwidth.go | 9 ++++ nrf24l01/const_commands.go | 17 ++++++++ nrf24l01/const_powerOutput.go | 10 +++++ nrf24l01/const_registers.go | 34 +++++++++++++++ nrf24l01/func_Module.go | 65 ++++++++++++++++++++++++++++ nrf24l01/interface_NRF24L01.go | 15 +++++++ nrf24l01/interface_Pin.go | 7 +++ nrf24l01/interface_SPI.go | 6 +++ nrf24l01/private_rfunc_down.go | 5 +++ nrf24l01/private_rfunc_getBuffer.go | 8 ++++ nrf24l01/private_rfunc_getRFSetup.go | 37 ++++++++++++++++ nrf24l01/private_rfunc_up.go | 5 +++ nrf24l01/rfunc_FlushRX.go | 10 +++++ nrf24l01/rfunc_FlushTX.go | 10 +++++ nrf24l01/rfunc_HasData.go | 5 +++ nrf24l01/rfunc_HasIRQ.go | 5 +++ nrf24l01/rfunc_ReadMultiRegister.go | 18 ++++++++ nrf24l01/rfunc_ReadPayload.go | 23 ++++++++++ nrf24l01/rfunc_ReadRegister.go | 15 +++++++ nrf24l01/rfunc_Send.go | 46 ++++++++++++++++++++ nrf24l01/rfunc_WriteMultiRegister.go | 16 +++++++ nrf24l01/rfunc_WritePayload.go | 19 ++++++++ nrf24l01/rfunc_WriteRegister.go | 14 ++++++ nrf24l01/struct_Config.go | 9 ++++ nrf24l01/struct_SPIConfig.go | 25 +++++++++++ nrf24l01/struct_nrf24l01.go | 18 ++++++++ 26 files changed, 451 insertions(+) create mode 100644 nrf24l01/const_bandwidth.go create mode 100644 nrf24l01/const_commands.go create mode 100644 nrf24l01/const_powerOutput.go create mode 100644 nrf24l01/const_registers.go create mode 100644 nrf24l01/func_Module.go create mode 100644 nrf24l01/interface_NRF24L01.go create mode 100644 nrf24l01/interface_Pin.go create mode 100644 nrf24l01/interface_SPI.go create mode 100644 nrf24l01/private_rfunc_down.go create mode 100644 nrf24l01/private_rfunc_getBuffer.go create mode 100644 nrf24l01/private_rfunc_getRFSetup.go create mode 100644 nrf24l01/private_rfunc_up.go create mode 100644 nrf24l01/rfunc_FlushRX.go create mode 100644 nrf24l01/rfunc_FlushTX.go create mode 100644 nrf24l01/rfunc_HasData.go create mode 100644 nrf24l01/rfunc_HasIRQ.go create mode 100644 nrf24l01/rfunc_ReadMultiRegister.go create mode 100644 nrf24l01/rfunc_ReadPayload.go create mode 100644 nrf24l01/rfunc_ReadRegister.go create mode 100644 nrf24l01/rfunc_Send.go create mode 100644 nrf24l01/rfunc_WriteMultiRegister.go create mode 100644 nrf24l01/rfunc_WritePayload.go create mode 100644 nrf24l01/rfunc_WriteRegister.go create mode 100644 nrf24l01/struct_Config.go create mode 100644 nrf24l01/struct_SPIConfig.go create mode 100644 nrf24l01/struct_nrf24l01.go diff --git a/nrf24l01/const_bandwidth.go b/nrf24l01/const_bandwidth.go new file mode 100644 index 000000000..5b4caaf61 --- /dev/null +++ b/nrf24l01/const_bandwidth.go @@ -0,0 +1,9 @@ +package nrf24l01 + +type bandWidth uint8 + +const ( + Bandwidth_256kbps bandWidth = iota + Bandwidth_1Mbps bandWidth = iota + Bandwidth_2Mbps bandWidth = iota +) diff --git a/nrf24l01/const_commands.go b/nrf24l01/const_commands.go new file mode 100644 index 000000000..c18140ffd --- /dev/null +++ b/nrf24l01/const_commands.go @@ -0,0 +1,17 @@ +package nrf24l01 + +const ( + R_REGISTER uint8 = 0x00 + W_REGISTER uint8 = 0x20 + + R_RX_PL_WID uint8 = 0x60 + R_RX_PAYLOAD uint8 = 0x61 + W_TX_PAYLOAD uint8 = 0xA0 + + W_TX_PAYLOAD_NOACK uint8 = 0xB0 + + FLUSH_TX uint8 = 0xE1 + FLUSH_RX uint8 = 0xE2 + + NOP uint8 = 0xFF +) diff --git a/nrf24l01/const_powerOutput.go b/nrf24l01/const_powerOutput.go new file mode 100644 index 000000000..79713542c --- /dev/null +++ b/nrf24l01/const_powerOutput.go @@ -0,0 +1,10 @@ +package nrf24l01 + +type powerOutput uint8 + +const ( + PowerOutput_n18dBm powerOutput = iota + PowerOutput_n12dBm powerOutput = iota + PowerOutput_n6dBm powerOutput = iota + PowerOutput_0dBm powerOutput = iota +) diff --git a/nrf24l01/const_registers.go b/nrf24l01/const_registers.go new file mode 100644 index 000000000..6c504ce64 --- /dev/null +++ b/nrf24l01/const_registers.go @@ -0,0 +1,34 @@ +package nrf24l01 + +const ( + CONFIG uint8 = 0x00 + EN_AA uint8 = 0x01 + EN_RXADDR uint8 = 0x02 + SETUP_AW uint8 = 0x03 + SETUP_RETR uint8 = 0x04 + RF_CH uint8 = 0x05 + RF_SETUP uint8 = 0x06 + STATUS uint8 = 0x07 + OBSERVE_TX uint8 = 0x08 + + RX_ADDR_P0 uint8 = 0x0A + RX_ADDR_P1 uint8 = 0x0B + RX_ADDR_P2 uint8 = 0x0C + RX_ADDR_P3 uint8 = 0x0D + RX_ADDR_P4 uint8 = 0x0E + RX_ADDR_P5 uint8 = 0x0F + + TX_ADDR uint8 = 0x10 + + RX_PW_P0 uint8 = 0x11 + RX_PW_P1 uint8 = 0x12 + RX_PW_P2 uint8 = 0x13 + RX_PW_P3 uint8 = 0x14 + RX_PW_P4 uint8 = 0x15 + RX_PW_P5 uint8 = 0x16 + + FIFO_STATUS uint8 = 0x17 + + DYNPD uint8 = 0x1C + FEATURE uint8 = 0x1D +) diff --git a/nrf24l01/func_Module.go b/nrf24l01/func_Module.go new file mode 100644 index 000000000..923d9e4a5 --- /dev/null +++ b/nrf24l01/func_Module.go @@ -0,0 +1,65 @@ +package nrf24l01 + +import ( + "errors" + "time" +) + +// initialize a new NRF24L01 Module +func Module(config Config) (NRF24L01, error) { + if len(config.SPIConfig.SenderAddress) != 5 || len(config.SPIConfig.ListenerAddress) != 5 { + return nil, errors.New("error: len(address) != 5") + } + + s := nrf24l01{ + packageSize: int(config.SPIConfig.PackageSize), + channel: config.SPIConfig.Channel, + senderAddress: config.SPIConfig.SenderAddress, + listenerAddress: config.SPIConfig.ListenerAddress, + addressWidth: 5, + txCMD: make([]uint8, 2), + rxCMD: make([]uint8, 2), + tx: make([]uint8, int(config.SPIConfig.PackageSize)+1), + rx: make([]uint8, int(config.SPIConfig.PackageSize)+1), + spi: config.SPI, + ce: config.CE, + csn: config.CSN, + irq: config.IRQ, + } + + s.ce.Low() + s.csn.High() + // RESET + s.WriteRegister(CONFIG, 0x00) + time.Sleep(time.Millisecond * 10) + // CLEAR + s.WriteRegister(STATUS, 0x70) + s.FlushTX() + s.FlushRX() + // BASIC CONFIG + s.WriteRegister(SETUP_AW, 0x03) + if config.SPIConfig.ACK { + s.WriteRegister(EN_AA, 0x01) + } else { + s.WriteRegister(EN_AA, 0x00) + } + s.WriteRegister(EN_RXADDR, 0x01) + s.WriteRegister(RF_SETUP, s.getRFSetup(config.SPIConfig)) + s.WriteRegister(RF_CH, s.channel) + s.WriteRegister(RX_PW_P0, config.SPIConfig.PackageSize) + s.WriteRegister(DYNPD, 0x00) + s.WriteRegister(CONFIG, 0x3E) + if config.SPIConfig.Sender { + s.WriteRegister(CONFIG, 0x0A) + time.Sleep(time.Millisecond * 10) + s.WriteMultiRegister(TX_ADDR, s.senderAddress) + s.WriteMultiRegister(RX_ADDR_P0, s.listenerAddress) + } else { + s.WriteRegister(CONFIG, 0x0B) + time.Sleep(time.Millisecond * 10) + s.WriteMultiRegister(TX_ADDR, s.listenerAddress) + s.WriteMultiRegister(RX_ADDR_P0, s.senderAddress) + s.ce.High() + } + return &s, nil +} diff --git a/nrf24l01/interface_NRF24L01.go b/nrf24l01/interface_NRF24L01.go new file mode 100644 index 000000000..8273cc963 --- /dev/null +++ b/nrf24l01/interface_NRF24L01.go @@ -0,0 +1,15 @@ +package nrf24l01 + +type NRF24L01 interface { + FlushRX() error + FlushTX() error + HasIRQ() bool + HasData() bool + ReadMultiRegister(uint8, int) []uint8 + ReadPayload() ([]uint8, error) + ReadRegister(uint8) uint8 + Send([]uint8) error + WriteMultiRegister(uint8, []uint8) error + WritePayload([]uint8) error + WriteRegister(uint8, uint8) error +} diff --git a/nrf24l01/interface_Pin.go b/nrf24l01/interface_Pin.go new file mode 100644 index 000000000..6b6de5019 --- /dev/null +++ b/nrf24l01/interface_Pin.go @@ -0,0 +1,7 @@ +package nrf24l01 + +type Pin interface { + High() + Low() + Get() bool +} diff --git a/nrf24l01/interface_SPI.go b/nrf24l01/interface_SPI.go new file mode 100644 index 000000000..816a15ba5 --- /dev/null +++ b/nrf24l01/interface_SPI.go @@ -0,0 +1,6 @@ +package nrf24l01 + +type SPI interface { + Tx([]uint8, []uint8) error + Transfer(uint8) (uint8, error) +} diff --git a/nrf24l01/private_rfunc_down.go b/nrf24l01/private_rfunc_down.go new file mode 100644 index 000000000..c7ef3d9e4 --- /dev/null +++ b/nrf24l01/private_rfunc_down.go @@ -0,0 +1,5 @@ +package nrf24l01 + +func (s *nrf24l01) down() { + s.csn.High() +} diff --git a/nrf24l01/private_rfunc_getBuffer.go b/nrf24l01/private_rfunc_getBuffer.go new file mode 100644 index 000000000..89bd64824 --- /dev/null +++ b/nrf24l01/private_rfunc_getBuffer.go @@ -0,0 +1,8 @@ +package nrf24l01 + +func (s *nrf24l01) getBuffer(b []uint8) []uint8 { + for i, _ := range b { + b[i] = 0x00 + } + return b +} diff --git a/nrf24l01/private_rfunc_getRFSetup.go b/nrf24l01/private_rfunc_getRFSetup.go new file mode 100644 index 000000000..bafcd7810 --- /dev/null +++ b/nrf24l01/private_rfunc_getRFSetup.go @@ -0,0 +1,37 @@ +package nrf24l01 + +func (s *nrf24l01) getRFSetup(config SPIConfig) uint8 { + var rfSetup uint8 = 0x00 + + // LNA_HCURR + rfSetup |= 1 << 1 + + // Bandwidth + switch config.Bandwidth { + case Bandwidth_256kbps: + rfSetup |= 1 << 6 + break + case Bandwidth_1Mbps: + break + case Bandwidth_2Mbps: + rfSetup |= 1 << 4 + break + } + + // PowerOutput + switch config.PowerOutput { + case PowerOutput_n18dBm: + break + case PowerOutput_n12dBm: + rfSetup |= 1 << 2 + break + case PowerOutput_n6dBm: + rfSetup |= 1 << 3 + break + case PowerOutput_0dBm: + rfSetup |= 1 << 2 + rfSetup |= 1 << 3 + break + } + return rfSetup +} diff --git a/nrf24l01/private_rfunc_up.go b/nrf24l01/private_rfunc_up.go new file mode 100644 index 000000000..febfdb90d --- /dev/null +++ b/nrf24l01/private_rfunc_up.go @@ -0,0 +1,5 @@ +package nrf24l01 + +func (s *nrf24l01) up() { + s.csn.Low() +} diff --git a/nrf24l01/rfunc_FlushRX.go b/nrf24l01/rfunc_FlushRX.go new file mode 100644 index 000000000..ca8d6a20d --- /dev/null +++ b/nrf24l01/rfunc_FlushRX.go @@ -0,0 +1,10 @@ +package nrf24l01 + +func (s *nrf24l01) FlushRX() error { + s.up() + defer s.down() + tx := s.getBuffer(s.txCMD) + tx[0] = FLUSH_RX + txErr := s.spi.Tx(tx, nil) + return txErr +} diff --git a/nrf24l01/rfunc_FlushTX.go b/nrf24l01/rfunc_FlushTX.go new file mode 100644 index 000000000..e21838583 --- /dev/null +++ b/nrf24l01/rfunc_FlushTX.go @@ -0,0 +1,10 @@ +package nrf24l01 + +func (s *nrf24l01) FlushTX() error { + s.up() + defer s.down() + tx := s.getBuffer(s.txCMD) + tx[0] = FLUSH_TX + txErr := s.spi.Tx(tx, nil) + return txErr +} diff --git a/nrf24l01/rfunc_HasData.go b/nrf24l01/rfunc_HasData.go new file mode 100644 index 000000000..271f5ed75 --- /dev/null +++ b/nrf24l01/rfunc_HasData.go @@ -0,0 +1,5 @@ +package nrf24l01 + +func (s *nrf24l01) HasData() bool { + return s.ReadRegister(FIFO_STATUS)&0x01 == 0 +} diff --git a/nrf24l01/rfunc_HasIRQ.go b/nrf24l01/rfunc_HasIRQ.go new file mode 100644 index 000000000..8aa1601f3 --- /dev/null +++ b/nrf24l01/rfunc_HasIRQ.go @@ -0,0 +1,5 @@ +package nrf24l01 + +func (s *nrf24l01) HasIRQ() bool { + return !s.irq.Get() +} diff --git a/nrf24l01/rfunc_ReadMultiRegister.go b/nrf24l01/rfunc_ReadMultiRegister.go new file mode 100644 index 000000000..77d75a10a --- /dev/null +++ b/nrf24l01/rfunc_ReadMultiRegister.go @@ -0,0 +1,18 @@ +package nrf24l01 + +func (s *nrf24l01) ReadMultiRegister(reg uint8, length int) []uint8 { + s.up() + defer s.down() + + tx := s.getBuffer(s.tx) + rx := s.getBuffer(s.rx) + tx[0] = reg + for i := 1; i < length+1; i++ { + tx[i] = NOP + } + txErr := s.spi.Tx(tx, rx) + if txErr != nil { + println("SPI error: ", txErr.Error()) + } + return rx +} diff --git a/nrf24l01/rfunc_ReadPayload.go b/nrf24l01/rfunc_ReadPayload.go new file mode 100644 index 000000000..00aa1679a --- /dev/null +++ b/nrf24l01/rfunc_ReadPayload.go @@ -0,0 +1,23 @@ +package nrf24l01 + +import ( + "errors" + "strconv" +) + +func (s *nrf24l01) ReadPayload() ([]uint8, error) { + s.ce.Low() + defer s.ce.High() + payload := s.ReadMultiRegister(R_RX_PAYLOAD, s.packageSize) + payloadLength := payload[1] + if payloadLength == 0 || payloadLength > uint8(s.packageSize) { + s := string(payload) + " out of bounds " + strconv.Itoa(int(payloadLength)) + return payload, errors.New(s) + } + if len(payload) < int(payloadLength)+2 { + s := "payload to small:" + strconv.Itoa(len(payload)) + return payload, errors.New(s) + } + message := payload[1 : payloadLength+2] + return message, nil +} diff --git a/nrf24l01/rfunc_ReadRegister.go b/nrf24l01/rfunc_ReadRegister.go new file mode 100644 index 000000000..187089b8a --- /dev/null +++ b/nrf24l01/rfunc_ReadRegister.go @@ -0,0 +1,15 @@ +package nrf24l01 + +func (s *nrf24l01) ReadRegister(reg uint8) uint8 { + s.up() + defer s.down() + tx := s.getBuffer(s.txCMD) + tx[0] = R_REGISTER | reg + tx[1] = NOP + rx := s.getBuffer(s.rxCMD) + txErr := s.spi.Tx(tx, rx) + if txErr != nil { + println("ERROR: SPI: ", txErr.Error()) + } + return rx[1] +} diff --git a/nrf24l01/rfunc_Send.go b/nrf24l01/rfunc_Send.go new file mode 100644 index 000000000..d136ace09 --- /dev/null +++ b/nrf24l01/rfunc_Send.go @@ -0,0 +1,46 @@ +package nrf24l01 + +import ( + "errors" + "time" +) + +// Opinionated send method with the first bit indicating the data length. +// Fist bit is the length of data, therefore data must be smaller than set package size +func (s *nrf24l01) Send(data []uint8) error { + if len(data) > s.packageSize-1 { + return errors.New("package size too big") + } + fifoStatus := s.ReadRegister(FIFO_STATUS) + if fifoStatus&0x20 != 0 { //TX_FULL + return errors.New("TX_FULL") + } + s.ce.Low() + s.WriteRegister(STATUS, 0x70) + tx := s.getBuffer(s.tx) + tx[0] = W_TX_PAYLOAD + tx[1] = uint8(len(data)) + for i, d := range data { + tx[i+2] = d + } + writeErr := s.WritePayload(tx) + if writeErr != nil { + return writeErr + } + s.ce.High() + time.Sleep(time.Microsecond * 15) + s.ce.Low() + // wait for TX_DS or MAX_RT + for { + status := s.ReadRegister(STATUS) + if status&0x20 != 0 { // TX_DS + s.WriteRegister(STATUS, 0x20) + return nil + } + if status&0x10 != 0 { // MAX_RT + s.WriteRegister(STATUS, 0x10) + s.FlushTX() + return errors.New("MAX_RT") + } + } +} diff --git a/nrf24l01/rfunc_WriteMultiRegister.go b/nrf24l01/rfunc_WriteMultiRegister.go new file mode 100644 index 000000000..f09955107 --- /dev/null +++ b/nrf24l01/rfunc_WriteMultiRegister.go @@ -0,0 +1,16 @@ +package nrf24l01 + +func (s *nrf24l01) WriteMultiRegister(reg uint8, values []uint8) error { + s.up() + defer s.down() + tx := s.getBuffer(s.tx) + tx[0] = W_REGISTER | reg + for i, v := range values { + tx[i+1] = v + } + txErr := s.spi.Tx(tx, nil) + if txErr != nil { + return txErr + } + return nil +} diff --git a/nrf24l01/rfunc_WritePayload.go b/nrf24l01/rfunc_WritePayload.go new file mode 100644 index 000000000..a7a48daaf --- /dev/null +++ b/nrf24l01/rfunc_WritePayload.go @@ -0,0 +1,19 @@ +package nrf24l01 + +import ( + "errors" +) + +func (s *nrf24l01) WritePayload(data []uint8) error { + if len(data) > s.packageSize+1 { + return errors.New("packagesize too big") + } + s.up() + defer s.down() + data[0] = W_TX_PAYLOAD + txErr := s.spi.Tx(data, nil) + if txErr != nil { + return txErr + } + return nil +} diff --git a/nrf24l01/rfunc_WriteRegister.go b/nrf24l01/rfunc_WriteRegister.go new file mode 100644 index 000000000..0a351b077 --- /dev/null +++ b/nrf24l01/rfunc_WriteRegister.go @@ -0,0 +1,14 @@ +package nrf24l01 + +func (s *nrf24l01) WriteRegister(reg, value uint8) error { + s.up() + defer s.down() + tx := s.getBuffer(s.txCMD) + tx[0] = W_REGISTER | reg + tx[1] = value + txErr := s.spi.Tx(tx, nil) + if txErr != nil { + return txErr + } + return nil +} diff --git a/nrf24l01/struct_Config.go b/nrf24l01/struct_Config.go new file mode 100644 index 000000000..e9fe6705f --- /dev/null +++ b/nrf24l01/struct_Config.go @@ -0,0 +1,9 @@ +package nrf24l01 + +type Config struct { + SPI SPI + CE Pin + CSN Pin + IRQ Pin + SPIConfig SPIConfig +} diff --git a/nrf24l01/struct_SPIConfig.go b/nrf24l01/struct_SPIConfig.go new file mode 100644 index 000000000..841a22be1 --- /dev/null +++ b/nrf24l01/struct_SPIConfig.go @@ -0,0 +1,25 @@ +package nrf24l01 + +type SPIConfig struct { + Channel uint8 + SenderAddress []uint8 + ListenerAddress []uint8 + Sender bool + Bandwidth bandWidth + PowerOutput powerOutput + PackageSize uint8 + ACK bool +} + +func SPIConfigDefault() SPIConfig { + return SPIConfig{ + Channel: 76, + SenderAddress: []uint8{0, 0, 0, 0, 0}, + ListenerAddress: []uint8{0, 0, 0, 0, 0}, + Sender: false, + Bandwidth: Bandwidth_256kbps, + PowerOutput: PowerOutput_0dBm, + PackageSize: 32, + ACK: false, + } +} diff --git a/nrf24l01/struct_nrf24l01.go b/nrf24l01/struct_nrf24l01.go new file mode 100644 index 000000000..722f36584 --- /dev/null +++ b/nrf24l01/struct_nrf24l01.go @@ -0,0 +1,18 @@ +package nrf24l01 + +type nrf24l01 struct { + packageSize int + channel uint8 + senderAddress []uint8 + listenerAddress []uint8 + addressWidth uint8 + txCMD []uint8 + rxCMD []uint8 + tx []uint8 + rx []uint8 + + spi SPI + ce Pin + csn Pin + irq Pin +} From de0b53f44a19d048868925d83ed3c6596b15ad71 Mon Sep 17 00:00:00 2001 From: Freak Date: Thu, 11 Jun 2026 10:23:19 +0200 Subject: [PATCH 2/2] renamed constructor function and added a few comments --- nrf24l01/const_registers.go | 14 +++++++------- nrf24l01/{func_Module.go => func_New.go} | 12 ++++++++---- nrf24l01/rfunc_ReadPayload.go | 3 +++ nrf24l01/rfunc_Send.go | 3 ++- 4 files changed, 20 insertions(+), 12 deletions(-) rename nrf24l01/{func_Module.go => func_New.go} (88%) diff --git a/nrf24l01/const_registers.go b/nrf24l01/const_registers.go index 6c504ce64..8daf1df67 100644 --- a/nrf24l01/const_registers.go +++ b/nrf24l01/const_registers.go @@ -2,25 +2,25 @@ package nrf24l01 const ( CONFIG uint8 = 0x00 - EN_AA uint8 = 0x01 + EN_AA uint8 = 0x01 //ACK EN_RXADDR uint8 = 0x02 SETUP_AW uint8 = 0x03 - SETUP_RETR uint8 = 0x04 - RF_CH uint8 = 0x05 + SETUP_RETR uint8 = 0x04 // RETRY + RF_CH uint8 = 0x05 //CHANNEL RF_SETUP uint8 = 0x06 STATUS uint8 = 0x07 OBSERVE_TX uint8 = 0x08 - RX_ADDR_P0 uint8 = 0x0A + RX_ADDR_P0 uint8 = 0x0A // RECEIVER ADDRESS RX_ADDR_P1 uint8 = 0x0B RX_ADDR_P2 uint8 = 0x0C RX_ADDR_P3 uint8 = 0x0D RX_ADDR_P4 uint8 = 0x0E RX_ADDR_P5 uint8 = 0x0F - TX_ADDR uint8 = 0x10 + TX_ADDR uint8 = 0x10 // SENDER ADDRESS - RX_PW_P0 uint8 = 0x11 + RX_PW_P0 uint8 = 0x11 // PACKAGE WIDTH (SIZE) RX_PW_P1 uint8 = 0x12 RX_PW_P2 uint8 = 0x13 RX_PW_P3 uint8 = 0x14 @@ -29,6 +29,6 @@ const ( FIFO_STATUS uint8 = 0x17 - DYNPD uint8 = 0x1C + DYNPD uint8 = 0x1C // DYNAMIC PACKAGE SIZE FEATURE uint8 = 0x1D ) diff --git a/nrf24l01/func_Module.go b/nrf24l01/func_New.go similarity index 88% rename from nrf24l01/func_Module.go rename to nrf24l01/func_New.go index 923d9e4a5..bbca71fd4 100644 --- a/nrf24l01/func_Module.go +++ b/nrf24l01/func_New.go @@ -5,12 +5,15 @@ import ( "time" ) -// initialize a new NRF24L01 Module -func Module(config Config) (NRF24L01, error) { +// initialize a new NRF24L01 New +func New(config Config) (NRF24L01, error) { if len(config.SPIConfig.SenderAddress) != 5 || len(config.SPIConfig.ListenerAddress) != 5 { return nil, errors.New("error: len(address) != 5") } - + // make sure that adresses can be written + if config.SPIConfig.PackageSize < 5 { + config.SPIConfig.PackageSize = 5 + } s := nrf24l01{ packageSize: int(config.SPIConfig.PackageSize), channel: config.SPIConfig.Channel, @@ -28,7 +31,8 @@ func Module(config Config) (NRF24L01, error) { } s.ce.Low() - s.csn.High() + s.up() + defer s.down() // RESET s.WriteRegister(CONFIG, 0x00) time.Sleep(time.Millisecond * 10) diff --git a/nrf24l01/rfunc_ReadPayload.go b/nrf24l01/rfunc_ReadPayload.go index 00aa1679a..4e099de3c 100644 --- a/nrf24l01/rfunc_ReadPayload.go +++ b/nrf24l01/rfunc_ReadPayload.go @@ -5,6 +5,9 @@ import ( "strconv" ) +// Opinionated read method. +// Fist bit is the length of data, therefore data is smaller than set package size +// Use ReadMultiRegister() for custom framing func (s *nrf24l01) ReadPayload() ([]uint8, error) { s.ce.Low() defer s.ce.High() diff --git a/nrf24l01/rfunc_Send.go b/nrf24l01/rfunc_Send.go index d136ace09..976f53f8c 100644 --- a/nrf24l01/rfunc_Send.go +++ b/nrf24l01/rfunc_Send.go @@ -5,8 +5,9 @@ import ( "time" ) -// Opinionated send method with the first bit indicating the data length. +// Opinionated send method. // Fist bit is the length of data, therefore data must be smaller than set package size +// Use WritePayload() for custom framing func (s *nrf24l01) Send(data []uint8) error { if len(data) > s.packageSize-1 { return errors.New("package size too big")