Skyward boardcore
Loading...
Searching...
No Matches
Xbee.h
Go to the documentation of this file.
1/* Copyright (c) 2021 Skyward Experimental Rocketry
2 * Author: Luca Erbetta
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
27#include <miosix.h>
28#include <radio/Transceiver.h>
30
31#include <functional>
32
33#include "APIFrameParser.h"
34#include "XbeeStatus.h"
35
36using miosix::FastMutex;
37
38#ifndef USE_MOCK_PERIPHERALS
39using GpioType = miosix::GpioPin;
40#else
41#include <utils/TestUtils/MockGpioPin.h>
42using GpioType = Boardcore::MockGpioPin;
43#endif
44
45namespace Boardcore
46{
47
48namespace Xbee
49{
51
52static constexpr unsigned int FRAME_POLL_INTERVAL = 10; // ms
53static constexpr unsigned int DEFAULT_TX_TIMEOUT = 5000; // ms
54
55// Size (in frames) of the receive circular buffer
56static constexpr unsigned int RX_FRAMES_BUF_SIZE = 3;
57
58class Xbee : public Transceiver
59{
60public:
61 using OnFrameReceivedListener = std::function<void(APIFrame& frame)>;
62
72 Xbee(SPIBusInterface& bus, GpioType cs, GpioType attn, GpioType rst,
73 long long txTimeout = DEFAULT_TX_TIMEOUT);
74
86 Xbee(SPIBusInterface& bus, SPIBusConfig config, GpioType cs, GpioType attn,
87 GpioType rst, long long txTimeout = DEFAULT_TX_TIMEOUT);
88
89 ~Xbee();
90
102 bool send(uint8_t* pkt, size_t packetLength) override;
103
111 ssize_t receive(uint8_t* buf, size_t bufferMaxSize) override;
112
116 void reset();
117
124 void handleATTNInterrupt();
125
132 void wakeReceiver(bool forceReturn = false);
133
142 void sendATCommand(const char* cmd, uint8_t* params = nullptr,
143 size_t paramsLength = 0);
144
156 bool sendATCommand(const char* cmd, ATCommandResponseFrame* response,
157 uint8_t* params = nullptr, size_t paramsLength = 0,
158 unsigned int timeout = 1000);
159
167
169
170private:
176 void handleFrame(APIFrame& frame);
177
183 uint8_t sendATCommandInternal(const char* cmd, uint8_t* params = nullptr,
184 size_t paramsLength = 0);
185
191 uint8_t sendATCommandInternal(uint8_t frameId, const char* cmd,
192 uint8_t* params = nullptr,
193 size_t paramsLength = 0);
194
195 uint8_t buildTXRequestFrame(TXRequestFrame& txReq, uint8_t* pkt,
196 size_t packetLength);
197
201 size_t fillReceiveBuf(uint8_t* buf, size_t bufferMaxSize);
202
208 ParseResult readOneFrame();
209
220 bool readRXFrame();
221
227 void writeFrame(APIFrame& frame);
228
229 uint8_t getNewFrameID();
230
245 bool waitForFrame(uint8_t frameType, unsigned int pollInterval,
246 long long timeoutTick);
247
248 // Synchronizes all communications to the xbee, as they may happen on
249 // multiple threads
250 FastMutex mutexXbeeCommunication;
251
252 APIFrameParser parser;
253 // Frame being parsed. Only valid if last call to
254 // parser.parse() returned ParseResult::SUCCESS
255 APIFrame parsingApiFrame;
256
257 // Temporary storage for RX packet frames, waiting to be returned by
258 // receive()
259 CircularBuffer<RXPacketFrame, RX_FRAMES_BUF_SIZE> rxFramesBuffer;
260 FastMutex mutexRxFrames;
261 // RX Packet currently being returned by receive()
262 RXPacketFrame currRxFrame;
263 // Index of the first byte of the payload of currRxFrame that will be
264 // returned on the next call to receive()
265 int currRxPayloadPointer = -1;
266
267 // SPI defs
268 SPISlave spiXbee;
269 GpioType attn;
270 GpioType rst;
271
272 // How long to wait for a TX Status
273 long long txTimeout;
274
275 OnFrameReceivedListener frameListener;
276
277 // Used to generate a unique frame id
278 uint8_t frameIdCounter = 1;
279
280 // Forces receive() to return even if there is no new data
281 bool forceRcvReturn = false;
282
283 // Status structs
284 XbeeStatus status;
285 Stats timeToSendStats;
286
287 // Waiting thread to be woken when something has been received.
288 miosix::Thread* receiveThread = 0;
289
290 PrintLogger logger = Logging::getLogger("xbee");
291};
292
293} // namespace Xbee
294
295} // namespace Boardcore
miosix::GpioPin GpioType
static PrintLogger getLogger(const string &name)
Interface for low level access of a SPI bus as a master.
Computes on-line statistics of a dataset.
Definition Stats.h:54
Parses a byte sequence into an Xbee APIFrame.
ParseResult
Result of the last parse operation.
void wakeReceiver(bool forceReturn=false)
Wakes the receive function without needing an interrupt.
Definition Xbee.cpp:208
void setOnFrameReceivedListener(OnFrameReceivedListener listener)
Set the frame received listener, called each time a new APIFrame is received from the device.
Definition Xbee.cpp:508
ssize_t receive(uint8_t *buf, size_t bufferMaxSize) override
Waits until a new packet is received.
Definition Xbee.cpp:116
void handleATTNInterrupt()
Signals the receive() function that there is new data available. Call this from the ATTN pin interrup...
Definition Xbee.cpp:220
std::function< void(APIFrame &frame)> OnFrameReceivedListener
Definition Xbee.h:61
void reset()
Hardware resets the Xbee.
Definition Xbee.cpp:170
void sendATCommand(const char *cmd, uint8_t *params=nullptr, size_t paramsLength=0)
Sends an AT Command to the Xbee (see datasheet) without waiting for a response.
Definition Xbee.cpp:370
XbeeStatus getStatus()
Definition Xbee.cpp:163
bool send(uint8_t *pkt, size_t packetLength) override
Sends a packet. The function blocks until the packet is sent to the peripheral, but does not wait for...
Definition Xbee.cpp:59
This file includes all the types the logdecoder script will decode.
SPI Bus configuration for a specific slave.
Contains information about a single SPI slave device.