nRF24L01 driver
Dependents: Nucleo_IOT1 wireless
nRF24L01P_PRX.cpp@6:952996e3abdb, 2016-09-14 (annotated)
- Committer:
- ianmcc
- Date:
- Wed Sep 14 14:43:13 2016 +0000
- Revision:
- 6:952996e3abdb
- Parent:
- 0:7313e63394c3
bug fixes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ianmcc | 0:7313e63394c3 | 1 | |
ianmcc | 0:7313e63394c3 | 2 | #include "nRF24L01P_PRX.h" |
ianmcc | 0:7313e63394c3 | 3 | |
ianmcc | 0:7313e63394c3 | 4 | // status |
ianmcc | 0:7313e63394c3 | 5 | #define STATUS_UNDEFINED 0 |
ianmcc | 0:7313e63394c3 | 6 | #define STATUS_READY_INIT 1 |
ianmcc | 0:7313e63394c3 | 7 | #define STATUS_POWER_DOWN 2 |
ianmcc | 0:7313e63394c3 | 8 | #define STATUS_STARTUP_STANDBY 3 |
ianmcc | 0:7313e63394c3 | 9 | #define STATUS_STANDBY 4 |
ianmcc | 0:7313e63394c3 | 10 | #define STATUS_RECEIVE 5 |
ianmcc | 0:7313e63394c3 | 11 | |
ianmcc | 0:7313e63394c3 | 12 | nRF24L01P_PRX::nRF24L01P_PRX(nRF24L01P& Device_, PinName CE_, PinName Int_) |
ianmcc | 0:7313e63394c3 | 13 | : Device(Device_), |
ianmcc | 0:7313e63394c3 | 14 | CE(CE_), |
ianmcc | 0:7313e63394c3 | 15 | Int(Int_) |
ianmcc | 0:7313e63394c3 | 16 | { |
ianmcc | 0:7313e63394c3 | 17 | CE = 0; |
ianmcc | 0:7313e63394c3 | 18 | PayloadSize = 0; |
ianmcc | 0:7313e63394c3 | 19 | Status = STATUS_UNDEFINED; |
ianmcc | 0:7313e63394c3 | 20 | Int.mode(PullNone); |
ianmcc | 0:7313e63394c3 | 21 | Int.fall(NULL); |
ianmcc | 0:7313e63394c3 | 22 | InitializeTimer.attach_us(this, &nRF24L01P_PRX::ReadyInitialize, Tundef2pd_us); |
ianmcc | 0:7313e63394c3 | 23 | } |
ianmcc | 0:7313e63394c3 | 24 | |
ianmcc | 0:7313e63394c3 | 25 | void |
ianmcc | 0:7313e63394c3 | 26 | nRF24L01P_PRX::Initialize() |
ianmcc | 0:7313e63394c3 | 27 | { |
ianmcc | 0:7313e63394c3 | 28 | while (Status == STATUS_UNDEFINED) |
ianmcc | 0:7313e63394c3 | 29 | { |
ianmcc | 0:7313e63394c3 | 30 | wait_us(1); |
ianmcc | 0:7313e63394c3 | 31 | } |
ianmcc | 0:7313e63394c3 | 32 | PowerOnTimer.detach(); |
ianmcc | 0:7313e63394c3 | 33 | CE = 0; |
ianmcc | 0:7313e63394c3 | 34 | Device.reset(); |
ianmcc | 0:7313e63394c3 | 35 | Device.set_prx_mode(); |
ianmcc | 0:7313e63394c3 | 36 | Status = STATUS_POWER_DOWN; |
ianmcc | 0:7313e63394c3 | 37 | PayloadSize = 0; |
ianmcc | 0:7313e63394c3 | 38 | Int.fall(this, &nRF24L01P_PRX::IntHandler); |
ianmcc | 0:7313e63394c3 | 39 | } |
ianmcc | 0:7313e63394c3 | 40 | |
ianmcc | 0:7313e63394c3 | 41 | void |
ianmcc | 0:7313e63394c3 | 42 | nRF24L01P_PRX::SetChannel(int Channel) |
ianmcc | 0:7313e63394c3 | 43 | { |
ianmcc | 0:7313e63394c3 | 44 | Device.set_channel(Channel); |
ianmcc | 0:7313e63394c3 | 45 | } |
ianmcc | 0:7313e63394c3 | 46 | |
ianmcc | 0:7313e63394c3 | 47 | void |
ianmcc | 0:7313e63394c3 | 48 | nRF24L01P_PRX::SetDataRate(int Rate) |
ianmcc | 0:7313e63394c3 | 49 | { |
ianmcc | 0:7313e63394c3 | 50 | Device.set_air_data_rate(Rate); |
ianmcc | 0:7313e63394c3 | 51 | } |
ianmcc | 0:7313e63394c3 | 52 | |
ianmcc | 0:7313e63394c3 | 53 | void |
ianmcc | 0:7313e63394c3 | 54 | nRF24L01P_PRX::SetAddress(uint64_t Address) |
ianmcc | 0:7313e63394c3 | 55 | { |
ianmcc | 0:7313e63394c3 | 56 | Device.set_rx_address(0, Address); |
ianmcc | 0:7313e63394c3 | 57 | } |
ianmcc | 0:7313e63394c3 | 58 | |
ianmcc | 0:7313e63394c3 | 59 | void |
ianmcc | 0:7313e63394c3 | 60 | nRF24L01P_PRX::SetPayloadSize(int Size) |
ianmcc | 0:7313e63394c3 | 61 | { |
ianmcc | 0:7313e63394c3 | 62 | PayloadSize = Size; |
ianmcc | 0:7313e63394c3 | 63 | Device.set_rx_payload_bytes(0, Size); |
ianmcc | 0:7313e63394c3 | 64 | } |
ianmcc | 0:7313e63394c3 | 65 | |
ianmcc | 0:7313e63394c3 | 66 | void |
ianmcc | 0:7313e63394c3 | 67 | nRF24L01P_PRX::PowerUp() |
ianmcc | 0:7313e63394c3 | 68 | { |
ianmcc | 0:7313e63394c3 | 69 | if (Status != STATUS_POWER_DOWN) |
ianmcc | 0:7313e63394c3 | 70 | { |
ianmcc | 0:7313e63394c3 | 71 | error("nRF24L01P_PRX::PowerUp(): can only be called when device is powered down"); |
ianmcc | 0:7313e63394c3 | 72 | } |
ianmcc | 0:7313e63394c3 | 73 | Status = STATUS_STARTUP_STANDBY; |
ianmcc | 0:7313e63394c3 | 74 | Device.set_power_up(); |
ianmcc | 0:7313e63394c3 | 75 | PowerOnTimer.attach_us(this, &nRF24L01P_PRX::ReadyStandby, Tpd2stby_us); |
ianmcc | 0:7313e63394c3 | 76 | } |
ianmcc | 0:7313e63394c3 | 77 | |
ianmcc | 0:7313e63394c3 | 78 | void |
ianmcc | 0:7313e63394c3 | 79 | nRF24L01P_PRX::PowerDown() |
ianmcc | 0:7313e63394c3 | 80 | { |
ianmcc | 0:7313e63394c3 | 81 | if (Status == STATUS_UNDEFINED || Status == STATUS_READY_INIT || Status == STATUS_POWER_DOWN) |
ianmcc | 0:7313e63394c3 | 82 | { |
ianmcc | 0:7313e63394c3 | 83 | error("nRF24L01P_PRX::PowerDown(): error: device is not powered up!"); |
ianmcc | 0:7313e63394c3 | 84 | } |
ianmcc | 0:7313e63394c3 | 85 | PowerOnTimer.detach(); |
ianmcc | 0:7313e63394c3 | 86 | Device.set_power_down(); |
ianmcc | 0:7313e63394c3 | 87 | Status = STATUS_POWER_DOWN; |
ianmcc | 0:7313e63394c3 | 88 | } |
ianmcc | 0:7313e63394c3 | 89 | |
ianmcc | 0:7313e63394c3 | 90 | void |
ianmcc | 0:7313e63394c3 | 91 | nRF24L01P_PRX::StartReceive() |
ianmcc | 0:7313e63394c3 | 92 | { |
ianmcc | 0:7313e63394c3 | 93 | if (Status == STATUS_UNDEFINED || Status == STATUS_READY_INIT || Status == STATUS_POWER_DOWN) |
ianmcc | 0:7313e63394c3 | 94 | { |
ianmcc | 0:7313e63394c3 | 95 | error("nRF24L01P_PRX::StartReceive(): error: device is not powered up!"); |
ianmcc | 0:7313e63394c3 | 96 | } |
ianmcc | 0:7313e63394c3 | 97 | while (Status == STATUS_STARTUP_STANDBY) |
ianmcc | 0:7313e63394c3 | 98 | { |
ianmcc | 0:7313e63394c3 | 99 | wait_us(1); |
ianmcc | 0:7313e63394c3 | 100 | } |
ianmcc | 0:7313e63394c3 | 101 | CE = 1; |
ianmcc | 0:7313e63394c3 | 102 | Status = STATUS_RECEIVE; |
ianmcc | 0:7313e63394c3 | 103 | } |
ianmcc | 0:7313e63394c3 | 104 | |
ianmcc | 0:7313e63394c3 | 105 | void |
ianmcc | 0:7313e63394c3 | 106 | nRF24L01P_PRX::StopReceive() |
ianmcc | 0:7313e63394c3 | 107 | { |
ianmcc | 0:7313e63394c3 | 108 | if (Status != STATUS_RECEIVE) |
ianmcc | 0:7313e63394c3 | 109 | { |
ianmcc | 0:7313e63394c3 | 110 | error("nRF24L01P_PRX::StopReceive(): error: device is not receiving!"); |
ianmcc | 0:7313e63394c3 | 111 | } |
ianmcc | 0:7313e63394c3 | 112 | CE = 0; |
ianmcc | 0:7313e63394c3 | 113 | Status = STATUS_STANDBY; |
ianmcc | 0:7313e63394c3 | 114 | } |
ianmcc | 0:7313e63394c3 | 115 | |
ianmcc | 0:7313e63394c3 | 116 | bool |
ianmcc | 0:7313e63394c3 | 117 | nRF24L01P_PRX::IsPacketReady() |
ianmcc | 0:7313e63394c3 | 118 | { |
ianmcc | 0:7313e63394c3 | 119 | return Device.is_rx_ready(); |
ianmcc | 0:7313e63394c3 | 120 | } |
ianmcc | 0:7313e63394c3 | 121 | |
ianmcc | 0:7313e63394c3 | 122 | int |
ianmcc | 0:7313e63394c3 | 123 | nRF24L01P_PRX::ReadPacket(char* Buf) |
ianmcc | 0:7313e63394c3 | 124 | { |
ianmcc | 0:7313e63394c3 | 125 | if (!this->IsPacketReady()) |
ianmcc | 0:7313e63394c3 | 126 | { |
ianmcc | 0:7313e63394c3 | 127 | error("nRF24L01P_PRX::ReadPacket(): error: no packet to read!"); |
ianmcc | 0:7313e63394c3 | 128 | } |
ianmcc | 0:7313e63394c3 | 129 | |
ianmcc | 0:7313e63394c3 | 130 | Device.read_rx_payload(Buf); |
ianmcc | 0:7313e63394c3 | 131 | Device.clear_rx_ready(); |
ianmcc | 0:7313e63394c3 | 132 | return PayloadSize; |
ianmcc | 0:7313e63394c3 | 133 | } |
ianmcc | 0:7313e63394c3 | 134 | |
ianmcc | 0:7313e63394c3 | 135 | void |
ianmcc | 0:7313e63394c3 | 136 | nRF24L01P_PRX::IntHandler() |
ianmcc | 0:7313e63394c3 | 137 | { |
ianmcc | 0:7313e63394c3 | 138 | // here we do nothing, since we use polling instead |
ianmcc | 0:7313e63394c3 | 139 | /* |
ianmcc | 0:7313e63394c3 | 140 | while (Device.is_rx_ready()) |
ianmcc | 0:7313e63394c3 | 141 | { |
ianmcc | 0:7313e63394c3 | 142 | // read payload |
ianmcc | 0:7313e63394c3 | 143 | Device.read_rx_payload(Buf); |
ianmcc | 0:7313e63394c3 | 144 | Device.clear_rx_ready(); |
ianmcc | 0:7313e63394c3 | 145 | } |
ianmcc | 0:7313e63394c3 | 146 | */ |
ianmcc | 0:7313e63394c3 | 147 | } |
ianmcc | 0:7313e63394c3 | 148 | |
ianmcc | 0:7313e63394c3 | 149 | void |
ianmcc | 0:7313e63394c3 | 150 | nRF24L01P_PRX::ReadyInitialize() |
ianmcc | 0:7313e63394c3 | 151 | { |
ianmcc | 0:7313e63394c3 | 152 | Status = STATUS_READY_INIT; |
ianmcc | 0:7313e63394c3 | 153 | } |
ianmcc | 0:7313e63394c3 | 154 | |
ianmcc | 0:7313e63394c3 | 155 | void |
ianmcc | 0:7313e63394c3 | 156 | nRF24L01P_PRX::ReadyStandby() |
ianmcc | 0:7313e63394c3 | 157 | { |
ianmcc | 0:7313e63394c3 | 158 | if (Status == STATUS_STARTUP_STANDBY) |
ianmcc | 0:7313e63394c3 | 159 | Status = STATUS_STANDBY; |
ianmcc | 0:7313e63394c3 | 160 | } |