nRF24L01 driver
Dependents: Nucleo_IOT1 wireless
Diff: nRF24L01P_PTX.cpp
- Revision:
- 5:bb28775120e7
- Parent:
- 4:8f612189af31
- Child:
- 6:952996e3abdb
diff -r 8f612189af31 -r bb28775120e7 nRF24L01P_PTX.cpp --- a/nRF24L01P_PTX.cpp Sat Sep 03 12:24:04 2016 +0000 +++ b/nRF24L01P_PTX.cpp Tue Sep 13 13:21:59 2016 +0000 @@ -35,7 +35,7 @@ Device.reset(); Device.set_ptx_mode(); Status = STATUS_POWER_DOWN; - Int.fall(this, &nRF24L01P_PTX::IntHandler); + Int.fall(NULL); } void @@ -106,11 +106,13 @@ { error("nRF24L01P_PTX::TransmitPacket(): error: device power must be on!"); } - // Wait until the device is ready to transmit - while (Status != STATUS_STANDBY) + + // if we're not ready to send, then bug out + if (Status != STATUS_STANDBY) { - wait_us(1); + return -1; } + Device.write_tx_payload(Buf, Size); Status = STATUS_TRANSMITTING; CE = 1; @@ -151,9 +153,80 @@ return -1; } +int +nRF24L01P_PTX::TransmitPacketNonBlocking(char* Buf, int Size) +{ + // If the device isn't powered up then there is a logic error + if (Status == STATUS_UNDEFINED || Status == STATUS_READY_INIT || Status == STATUS_POWER_DOWN) + { + error("nRF24L01P_PTX::TransmitPacketNonBlocking(): error: device power must be on!"); + } + + // if we're not ready to send, then bug out + if (Status != STATUS_STANDBY) + { + return -1; + } + + // Set up the interrupt handler + Int.fall(this, &nRF24L01P_PTX::IntHandler); + + // write the payload + Device.write_tx_payload(Buf, Size); + Status = STATUS_TRANSMITTING; + CE = 1; + wait_us(Thce_us); + CE = 0; + + return 0; +} + +bool +nRF24L01P_PTX::IsTransmitFinished() +{ + return Status != STATUS_TRANSMITTING; +} + +int +nRF24L01P_PTX::TransmitComplete() +{ + // wait until the packet is transmitted (or some error) + while (Status == STATUS_TRANSMITTING) + { + wait_us(1); + } + + if (Status == STATUS_PACKET_OK) + { + Status = STATUS_STANDBY; + return 0; + } + else if (Status == STATUS_PACKET_MAX_RT) + { + Status = STATUS_STANDBY; + Device.flush_tx_fifo(); + return -1; + } + // this shouldn't happen + return -2; +} + void nRF24L01P_PTX::IntHandler() { + Int.fall(NULL); + if (Device.is_tx_sent()) + { + // we successfully sent a packet + Status = STATUS_PACKET_OK; + Device.clear_tx_sent(); + } + else if (Device.is_max_rt()) + { + // we failed to send the packet + Status = STATUS_PACKET_MAX_RT; + Device.clear_max_rt(); + } } void