1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2015 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <string>
27 #include <iostream>
28 
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <termios.h>
36 #include <sys/time.h>
37 #include <sys/select.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 
41 #include <mraa/uart.h>
42 
43 #define HMTRP_DEFAULT_UART 0
44 
45 namespace upm {
46 /**
47  * @brief HMTRP Serial RF Pro library
48  * @defgroup hmtrp libupm-hmtrp
49  * @ingroup seeed uart wifi
50  */
51 
52 /**
53  * @library hmtrp
54  * @sensor hmtrp
55  * @comname Grove Serial RF Pro
56  * @altname HMTRP-433 HMTRP-470 HMTRP-868 HMTRP-915
57  * @type wifi
58  * @man seeed
59  * @con uart
60  *
61  * @brief API for the HM-TRP Serial RF Pro transceiver
62  *
63  * UPM support for the HM-TRP Serial RF Pro transceiver. This was tested
64  * specifically with the Grove Serial RF Pro transceiver. In theory,
65  * this class should work with the following devices:
66  *
67  * HM-TRP-433: 414000000-454000000Hz
68  * HM-TRP-470: 450000000-490000000Hz
69  * HM-TRP-868: 849000000-889000000Hz
70  * HM-TRP-915: 895000000-935000000Hz
71  *
72  * The only difference is the transmit and receive frequencies
73  * supported.
74  *
75  * By default, the device simply sends and receives any data
76  * presented on its UART interface. It can be put into a
77  * configuration mode by grounding the CONFIG pin on the transceiver.
78  *
79  * @image html hmtrp.jpg
80  * @snippet hmtrp.cxx Interesting
81  */
82   class HMTRP {
83   public:
84 
85     // HMTRP opcodes
86     typedef enum { RESET               = 0xf0,
87                    GET_CONFIG          = 0xe1,
88                    SET_FREQUENCY       = 0xd2,
89                    SET_RF_DATARATE     = 0xc3, // 1200-115200 (baud)
90                    SET_RX_BW           = 0xb4, // 30-620 (Khz)
91                    SET_FREQ_MODULATION = 0xa5, // 10-160 (KHz)
92                    SET_TX_POWER        = 0x96, // 0-7
93                    SET_UART_SPEED      = 0x1e, // recommended not to change
94                    GET_RF_SIGNAL_STR   = 0xa7,
95                    GET_MOD_SIGNAL_STR  = 0x78
96     } HMTRP_OPCODE_T;
97 
98     /**
99      * HMTRP Serial RF Pro transceiver constructor
100      *
101      * @param uart Default UART to use (0 or 1)
102      */
103     HMTRP(int uart=HMTRP_DEFAULT_UART);
104 
105     /**
106      * HMTRP destructor
107      */
108     ~HMTRP();
109 
110     /**
111      * Checks to see if there is data available for reading
112      *
113      * @param millis Number of milliseconds to wait; 0 means no waiting (default).
114      * @return True if there is data available for reading
115      */
116     bool dataAvailable(unsigned int millis=0);
117 
118     /**
119      * Reads any available data in a user-supplied buffer
120      *
121      * @param buffer Buffer to hold the data read
122      * @param len Length of the buffer
123      * @param millis Maximum time in milliseconds to wait for input. -1 means
124      * waiting forever (default).
125      * @return Number of bytes read; 0 if timed out and millis is >= 0
126      */
127     int readData(char *buffer, int len, int millis=-1);
128 
129     /**
130      * Writes the data in the buffer to the device
131      *
132      * @param buffer Buffer to hold the data read
133      * @param len Length of the buffer
134      * @return Number of bytes written
135      */
136     int writeData(char *buffer, int len);
137 
138     /**
139      * Sets up proper tty I/O modes and the baud rate. The default
140      * baud rate is 9,600 (B9600).
141      *
142      * @param baud Desired baud rate.
143      * @return True if successful
144      */
145     bool setupTty(speed_t baud=B9600);
146 
147     /**
148      * Looks for and verifies an OK response. This looks like "OK\r\n"
149      *
150      * @return True if OK received
151      */
152     bool checkOK();
153 
154     /**
155      * Resets the device to default parameters, except for the UART baud rate
156      *
157      * @return True if successful
158      */
159     bool reset();
160 
161     /**
162      * Queries the radio to determine its configuration
163      *
164      * @param freq Operating frequency
165      * @param dataRate TX/RX bit rate
166      * @param rxBandwidth Receiving bandwidth in Khz
167      * @param modulation Modulation frequency in Khz
168      * @param txPower Transmission power (1-7)
169      * @param uartBaud UART baud rate
170      * @return True if successful
171      */
172     bool getConfig(uint32_t *freq, uint32_t *dataRate, uint16_t *rxBandwidth,
173                    uint8_t *modulation, uint8_t *txPower, uint32_t *uartBaud);
174 
175     /**
176      * Sets the frequency. Note: this is limited depending on which
177      * HM-TRP device you are using. Consult the datasheet.
178      *
179      * @param freq Operating frequency
180      * @return True if successful
181      */
182     bool setFrequency(uint32_t freq);
183 
184     /**
185      * Sets the RF data transmission rate. Valid values are between
186      * 1,200 and 115,200.
187      *
188      * @param rate Radio transmission rate in baud (1,200-115,200)
189      * @return True if successful
190      */
191     bool setRFDataRate(uint32_t rate);
192 
193     /**
194      * Sets the RX bandwidth. Valid values are between 30 and 620 (in Khz)
195      *
196      * @param rxBand RX bandwidth in Khz (30-620)
197      * @return True if successful
198      */
199     bool setRXBandwidth(uint16_t rxBand);
200 
201     /**
202      * Sets the frequency modulation. Valid values are between 10 and 160 (in Khz)
203      *
204      * @param modulation Frequency modulation to use, in Khz (10-160)
205      * @return True if successful
206      */
207     bool setFrequencyModulation(uint8_t modulation);
208 
209     /**
210      * Sets the transmit power level. Valid values are between 0 and 7,
211      * 7 being the maximum power.
212      *
213      * @param power Power level to use during transmission. Valid values
214      * are between 0 and 7.
215      * @return True if successful
216      */
217     bool setTransmitPower(uint8_t power);
218 
219     /**
220      * Sets the configured baud rate of the UART. It is strongly
221      * recommended you do not change this, or you may lose the
222      * ability to communicate with the transceiver. Valid values are 1,200-115,200.
223      *
224      * @param speed Desired baud rate to configure the device to use
225      * Valid values are between 1,200 and 115,200.
226      * @return True if successful
227      */
228     bool setUARTSpeed(uint32_t speed);
229 
230     /**
231      * Gets the RF signal strength
232      *
233      * @param strength Returned strength
234      * @return True if successful
235      */
236     bool getRFSignalStrength(uint8_t *strength);
237 
238     /**
239      * Gets the modulation signal strength.
240      *
241      * @param strength Returned strength
242      * @return True if successful
243      */
244     bool getModSignalStrength(uint8_t *strength);
245 
246 
247   private:
248     mraa_uart_context m_uart;
249     int m_ttyFd;
250   };
251 }
252 
253 
254