Si4463 Radio Library for AVR and Arduino  1.0
Si446x.h
Go to the documentation of this file.
1 /*
2  * Project: Si4463 Library for AVR and Arduino
3  * Author: Zak Kemble, contact@zakkemble.co.uk
4  * Copyright: (C) 2017 by Zak Kemble
5  * License: GNU GPL v3 (see License.txt)
6  * Web: http://blog.zakkemble.co.uk/si4463-radio-library-avr-arduino/
7  */
8 
9 #ifndef SI443X_H_
10 #define SI443X_H_
11 
12 #ifdef ARDUINO
13 #include <Arduino.h>
14 #endif
15 
16 #include <stdint.h>
17 
18 #include "Si446x_config.h"
19 
20 // Address matching doesnt really work very well as the FIFO still needs to be
21 // manually cleared after receiving a packet, so the MCU still needs to wakeup and
22 // do stuff instead of the radio doing things automatically :/
23 #if !DOXYGEN
24 #define SI446X_ENABLE_ADDRMATCHING 0
25 #endif
26 
27 #define SI446X_MAX_PACKET_LEN 128
28 
29 #define SI446X_MAX_TX_POWER 127
30 
31 #define SI446X_WUT_RUN 1
32 #define SI446X_WUT_BATT 2
33 #define SI446X_WUT_RX 4
34 
35 #define SI446X_GPIO_PULL_EN 0x40
36 #define SI446X_GPIO_PULL_DIS 0x00
37 #define SI446X_NIRQ_PULL_EN 0x40
38 #define SI446X_NIRQ_PULL_DIS 0x00
39 #define SI446X_SDO_PULL_EN 0x40
40 #define SI446X_SDO_PULL_DIS 0x00
41 #define SI446X_PIN_PULL_EN 0x40
42 #define SI446X_PIN_PULL_DIS 0x00
43 
44 #define SI446X_GPIO_DRV_HIGH 0x00
45 #define SI446X_GPIO_DRV_MED_HIGH 0x20
46 #define SI446X_GPIO_DRV_MED_LOW 0x40
47 #define SI446X_GPIO_DRV_LOW 0x60
48 
49 #define SI446X_PROP_GROUP_GLOBAL 0x00
50 #define SI446X_PROP_GROUP_INT 0x01
51 #define SI446X_PROP_GROUP_FRR 0x02
52 #define SI446X_PROP_GROUP_PREAMBLE 0x10
53 #define SI446X_PROP_GROUP_SYNC 0x11
54 #define SI446X_PROP_GROUP_PKT 0x12
55 #define SI446X_PROP_GROUP_MODEM 0x20
56 #define SI446X_PROP_GROUP_MODEM_CHFLT 0x21
57 #define SI446X_PROP_GROUP_PA 0x22
58 #define SI446X_PROP_GROUP_SYNTH 0x23
59 #define SI446X_PROP_GROUP_MATCH 0x30
60 #define SI446X_PROP_GROUP_FREQ_CONTROL 0x40
61 #define SI446X_PROP_GROUP_RX_HOP 0x50
62 #define SI446X_PROP_GROUP_PTI 0xF0
63 
64 
67 typedef enum
68 {
99 // Nothing for 0x1E (30)
110 
114 typedef enum
115 {
121 // SI446X_NIRQ_MODE_32K_CLK = 0x05,
122 // SI446X_NIRQ_MODE_BOOT_CLK = 0x06,
125 // SI446X_NIRQ_MODE_INV_CTS = 0x09,
126 // SI446X_NIRQ_MODE_CMD_OVERLAP = 0x0A,
129 // SI446X_NIRQ_MODE_CAL_WUT = 0x0D,
130 // SI446X_NIRQ_MODE_WUT = 0x0E,
144 // SI446X_NIRQ_MODE_IN_SLEEP = 0x1C,
146 // Nothing for 0x1E (30)
148 // SI446X_NIRQ_MODE_TX_STATE = 0x20,
149 // SI446X_NIRQ_MODE_RX_STATE = 0x21,
150 // SI446X_NIRQ_MODE_RX_FIFO_FULL = 0x22,
151 // SI446X_NIRQ_MODE_TX_FIFO_EMPTY = 0x23,
152 // SI446X_NIRQ_MODE_LOW_BATT = 0x24,
153 // SI446X_NIRQ_MODE_CCA_LATCH = 0x25,
154 // SI446X_NIRQ_MODE_HOPPED = 0x26,
157 
161 typedef enum
162 {
169 // SI446X_SDO_MODE_BOOT_CLK = 0x06,
172 // SI446X_SDO_MODE_INV_CTS = 0x09,
173 // SI446X_SDO_MODE_CMD_OVERLAP = 0x0A,
176 // SI446X_SDO_MODE_CAL_WUT = 0x0D,
191 // SI446X_SDO_MODE_IN_SLEEP = 0x1C,
192 // SI446X_SDO_MODE_PKT_TRACE = 0x1D,
193 // Nothing for 0x1E (30)
194 // SI446X_SDO_MODE_TX_RX_DATA_CLK = 0x1F,
195 // SI446X_SDO_MODE_TX_STATE = 0x20,
196 // SI446X_SDO_MODE_RX_STATE = 0x21,
197 // SI446X_SDO_MODE_RX_FIFO_FULL = 0x22,
198 // SI446X_SDO_MODE_TX_FIFO_EMPTY = 0x23,
199 // SI446X_SDO_MODE_LOW_BATT = 0x24,
200 // SI446X_SDO_MODE_CCA_LATCH = 0x25,
201 // SI446X_SDO_MODE_HOPPED = 0x26,
202 // SI446X_SDO_MODE_HOP_TABLE_WRAP = 0x27
204 
208 typedef struct {
209  uint8_t chipRev;
210  uint16_t part;
211  uint8_t partBuild;
212  uint16_t id;
213  uint8_t customer;
214  uint8_t romId;
215 
216  uint8_t revExternal;
217  uint8_t revBranch;
218  uint8_t revInternal;
219  uint16_t patch;
220  uint8_t func;
221 } si446x_info_t;
222 
226 typedef enum
227 {
234 } si446x_gpio_t;
235 
239 typedef enum
240 {
251 
252 #if SI446X_ENABLE_ADDRMATCHING
253 /*-*
254 * @brief Address modes (NOT SUPPORTED)
255 */
256 typedef enum
257 {
258  SI446X_ADDRMODE_DISABLE,
259  SI446X_ADDRMODE_ADDR,
260  SI446X_ADDRMODE_ADDRBROADCAST
261 } si446x_addrMode_t;
262 
263 #define SI446X_CBS_ADDRMATCH _BV(7+8)
264 #define SI446X_CBS_ADDRMISS _BV(6+8)
265 #endif
266 
267 #define SI446X_CBS_SENT _BV(5+8)
268 //#define SI446X_CBS_RXCOMPLETE _BV(4+8)
269 //#define SI446X_CBS_RXINVALID _BV(3+8)
270 #define SI446X_CBS_RXBEGIN _BV(0)
271 //#define SI446X_CBS_INVALIDSYNC _BV(5)
272 
273 #if defined(__cplusplus)
274 extern "C" {
275 #endif
276 
282 void Si446x_init(void);
283 
291 void Si446x_getInfo(si446x_info_t* info);
292 
298 int16_t Si446x_getRSSI(void);
299 
313 void Si446x_setTxPower(uint8_t pwr);
314 
322 void Si446x_setupCallback(uint16_t callbacks, uint8_t state);
323 
331 void Si446x_read(void* buff, uint8_t len);
332 
342 uint8_t Si446x_TX(void* packet, uint8_t len, uint8_t channel, si446x_state_t onTxFinish);
343 
352 void Si446x_RX(uint8_t channel);
353 
354 /*-*
355 * @brief Changes will be applied next time the radio enters RX mode (NOT SUPPORTED)
356 *
357 * @param [mode] TODO
358 * @param [address] TODO
359 * @return (none)
360 */
361 //void Si446x_setAddress(si446x_addrMode_t mode, uint8_t address);
362 
371 void Si446x_setLowBatt(uint16_t voltage);
372 
392 void Si446x_setupWUT(uint8_t r, uint16_t m, uint8_t ldc, uint8_t config);
393 
399 void Si446x_disableWUT(void);
400 
411 uint8_t Si446x_sleep(void);
412 
420 
427 uint16_t Si446x_adc_gpio(uint8_t pin);
428 
434 uint16_t Si446x_adc_battery(void);
435 
441 float Si446x_adc_temperature(void);
442 
452 void Si446x_writeGPIO(si446x_gpio_t pin, uint8_t value);
453 
459 uint8_t Si446x_readGPIO(void);
460 
468 uint8_t Si446x_dump(void* buff, uint8_t group);
469 
475 #if DOXYGEN || defined(ARDUINO) || SI446X_INTERRUPTS == 0
476 void Si446x_SERVICE(void);
477 #else
478 #define Si446x_SERVICE() ((void)(0))
479 #endif
480 
481 #if SI446X_ENABLE_ADDRMATCHING
482 /*-*
483 * @brief Set the address in the first byte of the payload TODO
484 *
485 * @return (none)
486 */
487 //inline void Si446x_setPacketAddress(uint8_t* data, uint8_t addr)
488 //{
489 // data[0] = addr;
490 //}
491 
492 /*-*
493 * @brief Set the MSB in the 2nd byte of the payload to signify that this is a broadcast transmission TODO
494 *
495 * @return (none)
496 */
497 //inline void Si446x_setBroadcast(uint8_t* data)
498 //{
499 // data[1] |= 0x80;
500 //}
501 
502 /*-*
503 * @brief TODO (NOT SUPPORTED)
504 *
505 * @return (none)
506 */
507 //inline void Si446x_clearBroadcast(uint8_t* data)
508 //{
509 // data[1] &= ~(0x80);
510 //}
511 
512 /*-*
513 * @brief TODO (NOT SUPPORTED)
514 *
515 * @return TODO
516 */
517 //inline uint8_t Si446x_isBroadcast(uint8_t* data)
518 //{
519 // return !!(data[1] & 0x80);
520 //}
521 
522 /*-*
523 * @brief TODO (NOT SUPPORTED)
524 *
525 * @return The destination address of the packet
526 */
527 //inline uint8_t Si446x_getPacketAddress(uint8_t* data)
528 //{
529 // return data[0];
530 //}
531 #endif
532 
541 uint8_t Si446x_irq_off(void);
542 
552 void Si446x_irq_on(uint8_t origVal);
553 
554 #if DOXYGEN || SI446X_INTERRUPTS != 0
555 
556 #if !defined(DOXYGEN)
557 static inline void _Si446x_iRestore(const uint8_t *__s)
558 {
559  Si446x_irq_on(*__s);
560  __asm__ volatile ("" ::: "memory");
561 }
562 #endif
563 
578 #define SI446X_NO_INTERRUPT() \
579  for(uint8_t si446x_irq __attribute__((__cleanup__(_Si446x_iRestore))) = Si446x_irq_off(), \
580  si446x_tmp = 1; si446x_tmp ; si446x_tmp = 0)
581 
582 #else
583 #define SI446X_NO_INTERRUPT() ((void)(0));
584 #endif
585 
586 #if defined(__cplusplus)
587 }
588 #endif
589 
590 #endif /* SI443X_H_ */
Definition: Si446x.h:248
Definition: Si446x.h:135
Definition: Si446x.h:140
uint16_t patch
Patch.
Definition: Si446x.h:219
NIRQ.
Definition: Si446x.h:232
Definition: Si446x.h:88
Definition: Si446x.h:107
uint8_t revInternal
Revision internal.
Definition: Si446x.h:218
void Si446x_irq_on(uint8_t origVal)
When using interrupts use this to re-enable them for the Si446x.
Definition: Si446x.h:164
Definition: Si446x.h:127
Definition: Si446x.h:76
void Si446x_setTxPower(uint8_t pwr)
Set the transmit power. The output power does not follow the pwr value, see the Si446x datasheet for ...
uint8_t chipRev
Chip revision.
Definition: Si446x.h:209
Definition: Si446x.h:165
GPIO 3.
Definition: Si446x.h:230
void Si446x_setupCallback(uint16_t callbacks, uint8_t state)
Enable or disable callbacks. This is mainly to configure what events should wake the microcontroller ...
Definition: Si446x.h:123
Definition: Si446x.h:86
Definition: Si446x.h:72
This will never be returned since SPI activity will wake the radio into SI446X_STATE_SPI_ACTIVE.
Definition: Si446x.h:242
Definition: Si446x.h:95
void Si446x_writeGPIO(si446x_gpio_t pin, uint8_t value)
Configure GPIO/NIRQ/SDO pin.
Definition: Si446x.h:168
Definition: Si446x.h:87
uint16_t part
Part ID.
Definition: Si446x.h:210
uint8_t Si446x_dump(void *buff, uint8_t group)
Get all values of a property group.
Definition: Si446x.h:180
Definition: Si446x.h:143
Definition: Si446x.h:89
Definition: Si446x.h:98
si446x_nirq_mode_t
NIRQ pin modes (see the Si446x API docs for what they all mean)
Definition: Si446x.h:114
Definition: Si446x.h:78
si446x_sdo_mode_t
SDO pin modes (see the Si446x API docs for what they all mean)
Definition: Si446x.h:161
Definition: Si446x.h:91
Definition: Si446x.h:81
uint16_t Si446x_adc_gpio(uint8_t pin)
Read pin ADC value.
Definition: Si446x.h:249
Definition: Si446x.h:79
uint16_t Si446x_adc_battery(void)
Read supply voltage.
Definition: Si446x.h:101
Data structure for storing chip info from Si446x_getInfo()
Definition: Si446x.h:208
Definition: Si446x.h:128
Definition: Si446x.h:188
Definition: Si446x.h:73
void Si446x_read(void *buff, uint8_t len)
Read received data from FIFO.
uint8_t customer
Customer.
Definition: Si446x.h:213
Definition: Si446x.h:93
Definition: Si446x.h:105
uint8_t partBuild
Part build.
Definition: Si446x.h:211
Definition: Si446x.h:77
Definition: Si446x.h:94
Definition: Si446x.h:70
Definition: Si446x.h:145
Definition: Si446x.h:137
Definition: Si446x.h:102
Definition: Si446x.h:141
si446x_gpio_mode_t
GPIO pin modes (see the Si446x API docs for what they all mean)
Definition: Si446x.h:67
Will return as SI446X_STATE_TX.
Definition: Si446x.h:246
Definition: Si446x.h:96
Definition: Si446x.h:138
si446x_state_t Si446x_getState(void)
Get the radio status.
si446x_state_t
Radio states, returned from Si446x_getState()
Definition: Si446x.h:239
Definition: Si446x.h:187
Definition: Si446x.h:186
void Si446x_RX(uint8_t channel)
Enter receive mode.
Definition: Si446x.h:147
GPIO 1.
Definition: Si446x.h:228
uint8_t func
Function.
Definition: Si446x.h:220
Definition: Si446x.h:82
Definition: Si446x.h:118
uint8_t Si446x_sleep(void)
Enter sleep mode.
int16_t Si446x_getRSSI(void)
Get the current RSSI, the chip needs to be in receive mode for this to work.
void Si446x_init(void)
Initialise, must be called before anything else!
Definition: Si446x.h:166
uint8_t Si446x_irq_off(void)
When using interrupts use this to disable them for the Si446x.
Definition: Si446x.h:74
GPIO 2.
Definition: Si446x.h:229
Definition: Si446x.h:119
Definition: Si446x.h:178
Definition: Si446x.h:108
Definition: Si446x.h:139
Definition: Si446x.h:163
Definition: Si446x.h:175
uint8_t revBranch
Revision branch.
Definition: Si446x.h:217
void Si446x_disableWUT(void)
Disable the wake up timer.
si446x_gpio_t
GPIOs for passing to Si446x_writeGPIO(), or for masking when reading from Si446x_readGPIO() ...
Definition: Si446x.h:226
Definition: Si446x.h:84
Definition: Si446x.h:167
Definition: Si446x.h:134
SDO.
Definition: Si446x.h:233
Definition: Si446x.h:174
Definition: Si446x.h:177
Definition: Si446x.h:116
Definition: Si446x.h:181
Definition: Si446x.h:85
void Si446x_setLowBatt(uint16_t voltage)
Set the low battery voltage alarm.
Definition: Si446x.h:179
Definition: Si446x.h:120
Definition: Si446x.h:124
void Si446x_getInfo(si446x_info_t *info)
Get chip info, see si446x_info_t.
Definition: Si446x.h:83
Definition: Si446x.h:142
Will return as SI446X_STATE_READY.
Definition: Si446x.h:245
void Si446x_SERVICE(void)
If interrupts are disabled (::SI446X_INTERRUPTS in Si446x_config.h) then this function should be call...
Definition: Si446x.h:97
Definition: Si446x.h:71
Definition: Si446x.h:190
uint8_t revExternal
Revision external.
Definition: Si446x.h:216
Definition: Si446x.h:184
Definition: Si446x.h:182
Definition: Si446x.h:69
Definition: Si446x.h:243
GPIO 4.
Definition: Si446x.h:231
void Si446x_setupWUT(uint8_t r, uint16_t m, uint8_t ldc, uint8_t config)
Configure the wake up timer.
uint8_t Si446x_TX(void *packet, uint8_t len, uint8_t channel, si446x_state_t onTxFinish)
Transmit a packet.
Definition: Si446x.h:170
Definition: Si446x.h:244
Definition: Si446x.h:80
Definition: Si446x.h:100
Definition: Si446x.h:136
uint8_t romId
ROM ID (3 = revB1B, 6 = revC2A)
Definition: Si446x.h:214
Will return as SI446X_STATE_RX.
Definition: Si446x.h:247
Definition: Si446x.h:155
Definition: Si446x.h:189
Definition: Si446x.h:103
Definition: Si446x.h:75
Definition: Si446x.h:183
Definition: Si446x.h:185
Definition: Si446x.h:171
Definition: Si446x.h:131
Definition: Si446x.h:133
Definition: Si446x.h:104
uint8_t Si446x_readGPIO(void)
Read GPIO pin states.
Definition: Si446x.h:106
uint16_t id
ID.
Definition: Si446x.h:212
Definition: Si446x.h:117
Definition: Si446x.h:241
Definition: Si446x.h:90
Definition: Si446x.h:92
Definition: Si446x.h:132
float Si446x_adc_temperature(void)
Read temperature.