PU2CLR RDA5807 Arduino Library  1.0.1
Arduino Library for RDA5807 Devices - By Ricardo Lima Caratti
RDA5807.h
Go to the documentation of this file.
1 
2 /**
3  * @mainpage PU2CLR rda Arduino Library
4  * @brief PU2CLR rda Arduino Library implementation. <br>
5  * @details This library works with I2C protocol and can provide an easier interface for controlling the RDA5807M devices.<br>
6  * @details This is an Arduino library was implemented based on RDA5807M - SINGLE-CHIP BROADCAST FMRADIO TUNER document from RDA microeletronics.
7  *
8  * @details IMPORTANT:
9  * @details The RDA5807M can be accessed by using two I2C bus address.
10  * @details If you need to access a specific register, use the 0x11 buss adress.
11  * @details If you need to access a set o register, use the 0x10 buss adress.
12  *
13  * @see RDA5807M - SINGLE-CHIP BROADCAST FMRADIO TUNER; page 5.
14  * @see the methods setRegister, setAllRegisters, getRegister and getStatusRegisters
15  *
16  * This library can be freely distributed using the MIT Free Software model.
17  * Copyright(c) 2020 Ricardo Lima Caratti.
18  * Contact : pu2clr @gmail.com
19  *
20  */
21 
22 #include <Arduino.h>
23 #include <Wire.h>
24 
25 #define MAX_DELAY_AFTER_OSCILLATOR 500 // Max delay after the crystal oscilator becomes active
26 
27 
28 #define I2C_ADDR_DIRECT_ACCESS 0x11 //!< Can be used to access a given register at a time.
29 #define I2C_ADDR_FULL_ACCESS 0x10 //!< Can be used to access a set of register at a time.
30 
31 
32 #define OSCILLATOR_TYPE_CRYSTAL 0 //!< Crystal
33 #define OSCILLATOR_TYPE_REFCLK 1 //!< Reference clock
34 
35 #define CLOCK_32K 0 //!< 32.768kHz
36 #define CLOCK_12M 1 //!< 12Mhz
37 #define CLOCK_13M 2 //!< 13Mhz
38 #define CLOCK_19_2M 3 //!< 19.2Mhz
39 #define CLOCK_24M 5 //!< 24Mhz
40 #define CLOCK_26M 6 //!< 26Mhz
41 #define CLOCK_38_4M 7 //!< 38.4Mhz
42 
43 #define RDS_STANDARD 0 //!< RDS Mode.
44 #define RDS_VERBOSE 1 //!< RDS Mode.
45 
46 #define RDA_FM_BAND_USA_EU 0 //!< 87.5–108 MHz (US / Europe, Default)
47 #define RDA_FM_BAND_JAPAN_WIDE 1 //!< 76–91 MHz (Japan wide band)
48 #define RDA_FM_BAND_WORLD 2 //!< 76–108 MHz (world wide)
49 #define RDA_FM_BAND_SPECIAL 3 //!< 65 –76 MHz(East Europe) or 50 - 65MHz(see bit 9 of gegister 0x06)
50 
51 #define RDA_SEEK_WRAP 0 //!< Wrap at the upper or lower band limit and continue seeking
52 #define RDA_SEEK_STOP 1 //!< Stop seeking at the upper or lower band limit
53 #define RDA_SEEK_DOWN 0 //!< Seek Up
54 #define RDA_SEEK_UP 1 //!< Seek Down
55 
56 #define REG00 0x00
57 #define REG02 0x02
58 #define REG03 0x03
59 #define REG04 0x04
60 #define REG05 0x05
61 #define REG06 0x06
62 #define REG07 0x07
63 #define REG0A 0x0A
64 #define REG0B 0x0B
65 #define REG0C 0x0C
66 #define REG0D 0x0D
67 #define REG0E 0x0E
68 #define REG0F 0x0F
69 
70 #define SH_REG0A 0 // Shadow array position for register 0x0A
71 #define SH_REG0B 1 // Shadow array position for register 0x0B
72 #define SH_REG0C 2 // Shadow array position for register 0x0C
73 #define SH_REG0D 3 // Shadow array position for register 0x0D
74 #define SH_REG0E 5 // Shadow array position for register 0x0E
75 #define SH_REG0F 5 // Shadow array position for register 0x0F
76 
77 /**
78  * @defgroup GA01 Union, Structure and Defined Data Types
79  * @brief rda Defined Data Types
80  * @details Defined Data Types is a way to represent the rda registers information
81  * @details The information shown here was extracted from Datasheet:
82  * @details rda stereo FM digital tuning radio documentation.
83  */
84 
85 /**
86  * @ingroup GA01
87  * @brief Register 0x00
88  *
89  */
90  typedef union {
91  struct {
92  uint8_t CHIPID: 8; //!< Chip ifdef
93  uint8_t DUMMY: 8;
94  } refined;
96 } rda_reg00;
97 
98 /**
99  * @ingroup GA01
100  * @brief Register 0x01 - Dummy
101  * @details It is not documented by the RDA.
102  */
103 typedef union
104 {
105  struct
106  {
107  uint8_t lowByte;
108  uint8_t highByte;
109  } refined;
111 } rda_reg01;
112 
113 /**
114  * @ingroup GA01
115  * @brief Register 0x02 - Basic setup: RESET configuration;
116  * @details Clock type configuration; seek operation; Sterio/Mono; Bass; and Audio configuration
117  * @details The RDA5807M is RESET itself when power up. You also can use soft reset by triggering SOFT_RESET bit to 1.
118  * @details Seek begins in the direction specified by SEEKUP and ends when a channel is found, or the entire band has been searched.
119  * @details The SEEK bit is set low and the STC bit is set high when the seek operation completes.
120  * @details RCLK NON-CALIBRATE MODE. if 0=RCLK clock is always supply; if 1=RCLK clock is not always supply when FM work ( when 1, RDA5807M can’t directly support -20 °C ~70 °C temperature.
121  * @details Only suppory ±20°C temperature swing from tune point).
122  *
123  * Clock CLK_MODE table
124  *
125  * | Value | Clock |
126  * | ----- | --------- |
127  * | 000 | 32.768kHz |
128  * | 001 | 12Mhz |
129  * | 101 | 24Mhz |
130  * | 010 | 13Mhz |
131  * | 110 | 26Mhz |
132  * | 011 | 19.2Mhz |
133  * | 111 | 38.4Mhz |
134  */
135 typedef union {
136  struct
137  {
138  uint8_t ENABLE : 1; //!< Power Up Enable; 0 = Disabled; 1 = Enabled
139  uint8_t SOFT_RESET : 1; //!< Soft reset; If 0, not reset; If 1, reset.
140  uint8_t NEW_METHOD : 1; //!< New Demodulate Method Enable, can improve 0 the receive sensitivity about 1dB.
141  uint8_t RDS_EN : 1; //!< RDS/RBDS enable; If 1, rds/rbds enable
142  uint8_t CLK_MODE : 3; //!< See table above
143  uint8_t SKMODE : 1; //!< Seek Mode; 0 = wrap at the upper or lower band limit and continue seeking; 1 = stop seeking at the upper or lower band limit
144  uint8_t SEEK : 1; //!< Seek; 0 = Disable stop seek; 1 = Enable;
145  uint8_t SEEKUP : 1; //!< Seek Up; 0 = Seek down; 1 = Seek up
146  uint8_t RCLK_DIRECT_IN : 1; //!< RCLK clock use the directly input mode. 1 = enable
147  uint8_t NON_CALIBRATE : 1; //!< 0=RCLK clock is always supply; 1=RCLK clock is not always supply when FM work
148  uint8_t BASS : 1; //!< Bass Boost; 0 = Disabled; 1 = Bass boost enabled
149  uint8_t MONO : 1; //!< Mono Select; 0 = Stereo; 1 = Force mono
150  uint8_t DMUTE : 1; //!< Mute Disable; 0 = Mute; 1 = Normal operation
151  uint8_t DHIZ : 1; //!< Audio Output High-Z Disable; 0 = High impedance; 1 = Normal operation
152  } refined;
154 } rda_reg02;
155 
156 /**
157  * @ingroup GA01
158  * @brief Register 0x03
159  * @details Receiver configuratio
160  * @details The tune operation begins when the TUNE bit is set high. The STC bit is set high when the tune operation completes.
161  * @details The tune bit is reset to low automatically when the tune operation completes
162  *
163  * Channel space table
164  *
165  * | Value | Description |
166  * | ----- | ----------- |
167  * | 00 | 100KHz |
168  * | 01 | 200KHz |
169  * | 10 | 50KHz |
170  * | 11 | 25KHz |
171  *
172  *
173  * FM band table
174  *
175  * | Value | Description |
176  * | ----- | --------------------------- |
177  * | 00 | 87–108 MHz (US/Europe) |
178  * | 01 | 76–91 MHz (Japan) |
179  * | 10 | 76–108 MHz (world wide) |
180  * | 11 | 65 –76 MHz (East Europe) or 50-65MHz (see bit 9 of gegister 0x06) |
181  *
182  * Channel select table
183  *
184  * | BAND | Description |
185  * | ------ | -------------------------------------------------- |
186  * | 0 | Frequency = Channel Spacing (kHz) x CHAN+ 87.0 MHz |
187  * | 1 or 2 | Frequency = Channel Spacing (kHz) x CHAN + 76.0 MHz |
188  * | 3 | Frequency = Channel Spacing (kHz) x CHAN + 65.0 MHz |
189  * IMPORTANT: CHAN is updated after a seek operation.
190  *
191  */
192 typedef union {
193  struct
194  {
195  uint16_t SPACE: 2; //!< See Channel space table above
196  uint16_t BAND: 2; //!< Seet band table above
197  uint16_t TUNE : 1; //!< Tune; 0 = Disable; 1 = Enable
198  uint16_t DIRECT_MODE : 1; //!< Directly Control Mode, Only used when test
199  uint16_t CHAN : 10; //!< Channel Select.
200  } refined;
202 } rda_reg03;
203 
204 /**
205  * @ingroup GA01
206  * @brief Register 0x04
207  * @details Receiver properties
208  * @details Volume scale is logarithmic When 0000, output mute and output impedance is very large
209  * @details Setting STCIEN = 1 will generate a low pulse on GPIO2 when the interrupt occurs.
210  */
211 typedef union {
212  struct
213  {
214  uint8_t GPIO1 : 2; //!< General Purpose I/O 1. when gpio_sel=01; 00 = High impedance; 01 = Reserved; 10 = Low; 11 = High
215  uint8_t GPIO2 : 2; //!< General Purpose I/O 2. when gpio_sel=01; 00 = High impedance; 01 = Reserved; 10 = Low; 11 = High
216  uint8_t GPIO3 : 2; //!< General Purpose I/O 1. when gpio_sel=01; 00 = High impedance; 01 = Mono/Stereo indicator (ST); 10 = Low; 11 = High
217  uint8_t I2S_ENABLE : 1; //!< I2S enable; 0 = disabled; 1 = enabled.
218  uint8_t RSVD1 : 1;
219  uint8_t AFCD : 1; //!< AFC disable; If 0, afc work; If 1, afc disabled.
220  uint8_t SOFTMUTE_EN : 1; //!< If 1, softmute enable.
221  uint8_t RDS_FIFO_CLR : 1; //!< 1 = clear RDS fifo
222  uint8_t DE : 1; //!< De-emphasis; 0 = 75 μs; 1 = 50 μs
223  uint8_t RDS_FIFO_EN : 1; //!< 1 = RDS fifo mode enable.
224  uint8_t RBDS : 1; //!< 1 = RBDS mode enable; 0 = RDS mode only
225  uint8_t STCIEN : 1; //!< Seek/Tune Complete Interrupt Enable; 0 = Disable Interrupt; 1 = Enable Interrupt;
226  uint8_t RSVD2 : 1;
227  } refined;
229 } rda_reg04;
230 
231 /**
232  * @ingroup GA01
233  * @brief Register 0x05
234  */
235 typedef union {
236  struct
237  {
238  uint8_t VOLUME : 4; //!< DAC Gain Control Bits (Volume); 0000 = min volume; 1111 = max volume.
239  uint8_t LNA_ICSEL_BIT : 2; //!< Lna working current bit: 00=1.8mA; 01=2.1mA; 10=2.5mA; 11=3.0mA.
240  uint8_t LNA_PORT_SEL : 2; //!< LNA input port selection bit: 00: no input; 01: LNAN; 10: LNAP; 11: dual port input
241  uint8_t SEEKTH : 4; //!< Seek SNR Threshold value
242  uint8_t RSVD2 : 1;
243  uint8_t SEEK_MODE : 2; //!< Default value is 00; When = 10, will add the RSSI seek mode
244  uint8_t INT_MODE : 1; //!< If 0, generate 5ms interrupt; If 1, interrupt last until read reg0CH action occurs.
245  } refined;
247 } rda_reg05;
248 
249 /**
250  * @ingroup GA01
251  * @brief Register 0x06
252  *
253  * I2S_SW_CNT values table
254  *
255  * | Value | Description |
256  * | ------- | ------------------ |
257  * | 0b1000 | WS_STEP_48 |
258  * | 0b0111 | WS_STEP=44.1kbps |
259  * | 0b0110 | WS_STEP=32kbps |
260  * | 0b0101 | WS_STEP=24kbps |
261  * | 0b0100 | WS_STEP=22.05kbps |
262  * | 0b0011 | WS_STEP=16kbps |
263  * | 0b0010 | WS_STEP=12kbps |
264  * | 0b0001 | WS_STEP=11.025kbps |
265  * | 0b0000 | WS_STEP=8kbps |
266  */
267 typedef union {
268  struct
269  {
270  uint8_t R_DELY : 1; //!< If 1, R channel data delay 1T.
271  uint8_t L_DELY : 1; //!< If 1, L channel data delay 1T.
272  uint8_t SCLK_O_EDGE : 1; //!< If 1, invert sclk output when as master.
273  uint8_t SW_O_EDGE : 1; //!< If 1, invert ws output when as master.
274  uint8_t I2S_SW_CNT : 4; //!< Only valid in master mode. See table above
275  uint8_t WS_I_EDGE : 1; //!< If 0, use normal ws internally; If 1, inverte ws internally.
276  uint8_t DATA_SIGNED : 1; //!< If 0, I2S output unsigned 16-bit audio data. If 1, I2S output signed 16-bit audio data.
277  uint8_t SCLK_I_EDGE : 1; //!< If 0, use normal sclk internally;If 1, inverte sclk internally.
278  uint8_t WS_LR : 1; //!< Ws relation to l/r channel; If 0, ws=0 ->r, ws=1 ->l; If 1, ws=0 ->l, ws=1 ->r.
279  uint8_t SLAVE_MASTER : 1; //!< I2S slave or master; 1 = slave; 0 = master.
280  uint8_t OPEN_MODE : 2; //!< Open reserved register mode; 11=open behind registers writing function others: only open behind registers reading function.
281  uint8_t RSVD : 1;
282  } refined;
284 } rda_reg06;
285 
286 /**
287  * @ingroup GA01
288  * @brief Register 0x07
289  */
290 typedef union {
291  struct
292  {
293  uint8_t FREQ_MODE : 1; //!< If 1, then freq setting changed. Freq = 76000(or 87000) kHz + freq_direct (08H) kHz.
294  uint8_t SOFTBLEND_EN : 1; //!< If 1, Softblend enable
295  uint8_t SEEK_TH_OLD : 6; //!< Seek threshold for old seek mode, Valid when Seek_Mode=001
296  uint8_t RSVD1 : 1;
297  uint8_t MODE_50_60 : 1; //!< 1 = 65~76 MHz; 0 = 50~76MHz
298  uint8_t TH_SOFRBLEND : 5; //!< Threshold for noise soft blend setting, unit 2dB (default 0b10000).
299  uint8_t RSVD2 : 1;
300  } refined;
302 } rda_reg07;
303 
304 /**
305  * @ingroup GA01
306  * @brief Register 0x08 - Direct Frequency
307  * @details Valid when freq_mode = 1
308  * @details Freq = 7600(or 8700) kHz + freq_direct (08H) kHz.
309  * @details Value to be stores is frequency - 7600 or 8700
310  */
311 typedef union
312 {
313  struct
314  {
315  uint8_t lowByte;
316  uint8_t highByte;
317  } refined;
319 } rda_reg08;
320 
321 
322 /**
323  * @ingroup GA01
324  * @brief Register 0x0A - Device current status
325  * @details The seek fail flag (SF) is set when the seek operation fails to find a channel with an RSSI level greater than SEEKTH[5:0].
326  * @details The seek/tune complete (STC) flag is set when the seek or tune operation completes.
327  *
328  *
329  * Channel table
330  *
331  * | BAND | Description |
332  * | ------ | -------------------------------------------------- |
333  * | 0 | Frequency = Channel Spacing (kHz) x CHAN+ 87.0 MHz |
334  * | 1 or 2 | Frequency = Channel Spacing (kHz) x CHAN + 76.0 MHz |
335  * | 3 | Frequency = Channel Spacing (kHz) x CHAN + 65.0 MHz |
336  *
337  */
338 typedef union {
339  struct
340  {
341  uint16_t READCHAN : 10; //!< See Channel table . See table above
342  uint16_t ST : 1; //!< Stereo Indicator; 0 = Mono; 1 = Stereo
343  uint16_t BLK_E : 1; //!< When RDS enable: 1 = Block E has been found; 0 = no Block E has been found
344  uint16_t RDSS : 1; //!< RDS Synchronization; 0 = RDS decoder not synchronized(default); 1 = RDS decoder synchronized; Available only in RDS Verbose mode
345  uint16_t SF : 1; //!< Seek Fail. 0 = Seek successful; 1 = Seek failure;
346  uint16_t STC : 1; //!< Seek/Tune Complete. 0 = Not complete; 1 = Complete;
347  uint16_t RDSR : 1; //!< RDS ready; 0 = No RDS/RBDS group ready(default); 1 = New RDS/RBDS group ready.
348  } refined;
350 } rda_reg0a;
351 
352 /**
353  * @ingroup GA01
354  * @brief Register 0x0B
355  * @details Current RDS and device status
356  *
357  * Errors Level table (Block Errors Level of RDS_DATA_0 and RDS_DATA_1.
358  * Always read as Errors Level of RDS BLOCKs A and B (in RDS mode ) or E (in RBDS mode when ABCD_E flag is 1).
359  *
360  * | Value | Description |
361  * | ----- | ----------- |
362  * | 00 | 0 errors requiring correction |
363  * | 01 | 1~2 errors requiring correction |
364  * | 10 | 3~5 errors requiring correction |
365  * | 10 | 6+ errors or error in checkword, correction not possible |
366  *
367  */
368 typedef union {
369  struct
370  {
371  uint8_t BLERB : 2; //!< Block Errors Level of RDS_DATA_1
372  uint8_t BLERA : 2; //!< Block Errors Level of RDS_DATA_0
373  uint8_t ABCD_E : 1; //!< 1 = the block id of register 0cH,0dH,0eH,0fH is E; 0 = the block id of register 0cH, 0dH, 0eH,0fH is A, B, C, D
374  uint8_t RSVD1 : 2;
375  uint8_t FM_READY : 1; //!< 1=ready; 0=not ready.
376  uint8_t FM_TRUE : 1; //!< 1 = the current channel is a station; 0 = the current channel is not a station.
377  uint8_t RSSI : 7; //!< RSSI; 000000 = min; 111111 = max; RSSI scale is logarithmic.
378  } refined;
380 } rda_reg0b;
381 
382 
383 /**
384  * @ingroup GA01
385  * @brief Register 0x0C
386  * @details BLOCK A ( in RDS mode) or BLOCK E (in RBDS mode when ABCD_E flag is 1)
387  *
388  */
389 typedef union {
390  struct {
391  uint8_t lowByte;
392  uint8_t highByte;
393  } refined;
394  uint16_t RDSA; //!< BLOCK A ( in RDS mode) or BLOCK E (in RBDS mode when ABCD_E flag is 1)
395 } rda_reg0c;
396 
397 /**
398  * @ingroup GA01
399  * @brief Register 0x0D
400  * @details BLOCK B ( in RDS mode) or BLOCK E (in RBDS mode when ABCD_E flag is 1)
401  *
402  */
403 typedef union
404 {
405  struct
406  {
407  uint8_t lowByte;
408  uint8_t highByte;
409  } refined;
411 } rda_reg0d;
412 
413 
414 /**
415  * @ingroup GA01
416  * @brief Register 0x0E
417  * @details BLOCK C ( in RDS mode) or BLOCK E (in RBDS mode when ABCD_E flag is 1)
418  *
419  */
420 typedef union
421 {
422  struct
423  {
424  uint8_t lowByte;
425  uint8_t highByte;
426  } refined;
428 } rda_reg0e;
429 
430 /**
431  * @ingroup GA01
432  * @brief Register 0x0F
433  * @details BLOCK D ( in RDS mode) or BLOCK E (in RBDS mode when ABCD_E flag is 1)
434  *
435  */
436 typedef union
437 {
438  struct
439  {
440  uint8_t lowByte;
441  uint8_t highByte;
442  } refined;
444 } rda_reg0f;
445 
446 /**
447  * @ingroup GA01
448  * @brief RDS Block B data type
449  *
450  * @details For GCC on System-V ABI on 386-compatible (32-bit processors), the following stands:
451  *
452  * 1) Bit-fields are allocated from right to left (least to most significant).
453  * 2) A bit-field must entirely reside in a storage unit appropriate for its declared type.
454  * Thus a bit-field never crosses its unit boundary.
455  * 3) Bit-fields may share a storage unit with other struct/union members, including members that are not bit-fields.
456  * Of course, struct members occupy different parts of the storage unit.
457  * 4) Unnamed bit-fields' types do not affect the alignment of a structure or union, although individual
458  * bit-fields' member offsets obey the alignment constraints.
459  *
460  * @see also https://en.wikipedia.org/wiki/Radio_Data_System
461  */
462 typedef union {
463  struct
464  {
465  uint8_t address : 2; // Depends on Group Type and Version codes. If 0A or 0B it is the Text Segment Address.
466  uint8_t DI : 1; // Decoder Controll bit
467  uint8_t MS : 1; // Music/Speech
468  uint8_t TA : 1; // Traffic Announcement
469  uint8_t programType : 5; // PTY (Program Type) code
470  uint8_t trafficProgramCode : 1; // (TP) => 0 = No Traffic Alerts; 1 = Station gives Traffic Alerts
471  uint8_t versionCode : 1; // (B0) => 0=A; 1=B
472  uint8_t groupType : 4; // Group Type code.
473  } group0;
474  struct
475  {
476  uint8_t address : 4; // Depends on Group Type and Version codes. If 2A or 2B it is the Text Segment Address.
477  uint8_t textABFlag : 1; // Do something if it chanhes from binary "0" to binary "1" or vice-versa
478  uint8_t programType : 5; // PTY (Program Type) code
479  uint8_t trafficProgramCode : 1; // (TP) => 0 = No Traffic Alerts; 1 = Station gives Traffic Alerts
480  uint8_t versionCode : 1; // (B0) => 0=A; 1=B
481  uint8_t groupType : 4; // Group Type code.
482  } group2;
483  struct
484  {
485  uint8_t content : 4; // Depends on Group Type and Version codes.
486  uint8_t textABFlag : 1; // Do something if it chanhes from binary "0" to binary "1" or vice-versa
487  uint8_t programType : 5; // PTY (Program Type) code
488  uint8_t trafficProgramCode : 1; // (TP) => 0 = No Traffic Alerts; 1 = Station gives Traffic Alerts
489  uint8_t versionCode : 1; // (B0) => 0=A; 1=B
490  uint8_t groupType : 4; // Group Type code.
491  } refined;
493 } rds_blockb;
494 
495 /**
496  * @ingroup GA01
497  * Group RDS type 4A ( RDS Date and Time)
498  * When group type 4A is used by the station, it shall be transmitted every minute according to EN 50067.
499  * This Structure uses blocks 2,3 and 5 (B,C,D)
500  *
501  * ATTENTION:
502  * To make it compatible with 8, 16 and 32 bits platforms and avoid Crosses boundary, it was necessary to
503  * split minute and hour representation.
504  */
505 typedef union {
506  struct
507  {
508  uint8_t offset : 5; // Local Time Offset
509  uint8_t offset_sense : 1; // Local Offset Sign ( 0 = + , 1 = - )
510  uint8_t minute1 : 2; // UTC Minutes - 2 bits less significant (void “Crosses boundary”).
511  uint8_t minute2 : 4; // UTC Minutes - 4 bits more significant (void “Crosses boundary”)
512  uint8_t hour1 : 4; // UTC Hours - 4 bits less significant (void “Crosses boundary”)
513  uint8_t hour2 : 1; // UTC Hours - 4 bits more significant (void “Crosses boundary”)
514  uint32_t mjd : 17; // Modified Julian Day Code
515  } refined;
517 } rds_date_time;
518 
519 /**
520  * @ingroup GA01
521  * @brief Converts 16 bits word to two bytes
522  */
523 typedef union {
524  struct
525  {
526  uint8_t lowByte;
527  uint8_t highByte;
528  } refined;
530 } word16_to_bytes;
531 
532 /**
533  * @ingroup GA01
534  * @brief KT0915 Class
535  * @details This class implements all functions that will help you to control the KT0915 devices.
536  *
537  * @author PU2CLR - Ricardo Lima Caratti
538  */
539 class RDA5807 {
540 
541  private:
542 
543  uint16_t shadowStatusRegisters[6]; //!< shadow status registers
544  uint16_t shadowRegisters[9]; //!< shadow configuration registers
545 
546  // Shadow device configuration register references (writable registers)
547  rda_reg00 *reg00 = (rda_reg00 *)&shadowRegisters[0]; // REG01;
548  rda_reg01 *reg01 = (rda_reg01 *)&shadowRegisters[1]; // REG01;
549  rda_reg02 *reg02 = (rda_reg02 *)&shadowRegisters[2]; // REG02;
550  rda_reg03 *reg03 = (rda_reg03 *)&shadowRegisters[3]; // REG03;
551  rda_reg04 *reg04 = (rda_reg04 *)&shadowRegisters[4]; // REG04;
552  rda_reg05 *reg05 = (rda_reg05 *)&shadowRegisters[5]; // REG05;
553  rda_reg06 *reg06 = (rda_reg06 *)&shadowRegisters[6]; // REG06;
554  rda_reg07 *reg07 = (rda_reg07 *)&shadowRegisters[7]; // REG07;
555  rda_reg08 *reg08 = (rda_reg08 *)&shadowRegisters[8]; // REG08;
556 
557  // Shadow device status register references (read only registers)
558  rda_reg0a *reg0a = (rda_reg0a *)&shadowStatusRegisters[0]; // SH_REG0A;
559  rda_reg0b *reg0b = (rda_reg0b *)&shadowStatusRegisters[1]; // SH_REG0B;
560  rda_reg0c *reg0c = (rda_reg0c *)&shadowStatusRegisters[2]; // SH_REG0C;
561  rda_reg0d *reg0d = (rda_reg0d *)&shadowStatusRegisters[3]; // SH_REG0D;
562  rda_reg0e *reg0e = (rda_reg0e *)&shadowStatusRegisters[4]; // SH_REG0E;
563  rda_reg0f *reg0f = (rda_reg0f *)&shadowStatusRegisters[5]; // SH_REG0F;
564 
565 
566  uint16_t startBand[4] = {8700, 7600, 7600, 6500};
567  uint16_t endBand[4] = {10800, 9100, 10800, 7600}; //!< End FM band limit
568  uint16_t fmSpace[4] = {100, 200, 50, 25};
569 
570  char rds_buffer2A[65]; //!< RDS Radio Text buffer - Program Information
571  char rds_buffer2B[33]; //!< RDS Radio Text buffer - Station Informaation
572  char rds_buffer0A[9]; //!< RDS Basic tuning and switching information (Type 0 groups)
573  char rds_time[20]; //!< RDS date time received information
574 
575 
576  protected:
579 
580  int resetPin;
585 
586  int gpio1Control = -1; //!< Can be used to add control to the system via GPIO1 pin
587  int gpio2Control = -1; //!< Can be used to add control to the system via GPIO2 pin
588  int gpio3Control = -1; //!< Can be used to add control to the system via GPIO3 pin
589 
592 
594 
595  public :
596 
597  /**
598  * @brief Set the Delay After Crystal On (default 500ms)
599  *
600  * @param ms_value Value in milliseconds
601  */
602  inline void setDelayAfterCrystalOn(uint8_t ms_value) { maxDelayAftarCrystalOn = ms_value; };
603 
604  /**
605  * @ingroup GA03
606  * @brief Sets alternatives I2C bus address
607  * @details You do not need use this function on RDA5807M
608  * @param directAccess
609  * @param fullAccess
610  */
611  inline void setI2CBusAddrs(int directAccess, int fullAccess) {
612  this->deviceAddressDirectAccess = directAccess;
613  this->deviceAddressFullAccess = fullAccess;
614  }
615 
616  void getStatusRegisters();
617  void *getStatus(uint8_t reg);
618  void setAllRegisters();
619  void setRegister(uint8_t reg, uint16_t value);
620 
621  void setGpio(uint8_t gpioPin, uint8_t gpioSetup = 0, int mcuPin = -1);
622 
623  void waitAndFinishTune();
624 
625  void softReset();
626 
627  void powerUp();
628  void powerDown();
629 
630  void setup(uint8_t clock_type = CLOCK_32K, uint8_t oscillator_type = OSCILLATOR_TYPE_CRYSTAL);
631 
632  void setFrequency(uint16_t frequency);
633 
634 
636  void setFrequencyUp();
637  void setFrequencyDown();
638 
641  void setChannel(uint16_t channel);
642  void seek(uint8_t seek_mode, uint8_t direction);
643  void setSeekThreshold(uint8_t value);
644 
645  void setBand(uint8_t band = 1);
646  void setSpace(uint8_t space = 0);
647  int getRssi();
648 
649  void setSoftmute(bool value);
650 
651  void setMono(bool value);
652  bool isStereo();
653 
655 
656  void setMute(bool value);
657  void setVolume(uint8_t value);
658  uint8_t getVolume();
659  void setVolumeUp();
660  void setVolumeDown();
661 
662  void setFmDeemphasis(uint8_t de);
663 
664  //******** RDS methods
665  void setRDS(bool value);
666  void setRBDS(bool value);
667  void clearRdsFifo();
668 
669  bool getRdsReady();
670  uint8_t getRdsFlagAB(void);
674  void getNext2Block(char *c);
675  void getNext4Block(char *c);
676  char *getRdsText(void);
677  char *getRdsText0A(void);
678  char *getRdsText2A(void);
679  char *getRdsText2B(void);
680  char *getRdsTime();
681  bool getRdsSync();
682 };
RDA5807::setRDS
void setRDS(bool value)
Sets the RDS operation.
Definition: RDA5807.cpp:572
RDA5807::powerUp
void powerUp()
Powers the receiver on.
Definition: RDA5807.cpp:178
RDA5807::waitAndFinishTune
void waitAndFinishTune()
Waits for Seek or Tune finish.
Definition: RDA5807.cpp:155
RDA5807::getRdsReady
bool getRdsReady()
Returns true if RDS Ready.
Definition: RDA5807.cpp:602
RDA5807::getStatusRegisters
void getStatusRegisters()
Gets all current device status and RDS information registers (From 0x0A to 0x0F)
Definition: RDA5807.cpp:63
rda_reg0f::RDSD
uint16_t RDSD
Definition: RDA5807.h:443
RDA5807::setRegister
void setRegister(uint8_t reg, uint16_t value)
Sets a given value to a specific device register.
Definition: RDA5807.cpp:137
rds_date_time::raw
uint8_t raw[6]
Definition: RDA5807.h:516
RDA5807::getRdsText0A
char * getRdsText0A(void)
Gets the station name and other messages.
Definition: RDA5807.cpp:772
RDA5807::setSoftmute
void setSoftmute(bool value)
Sets Soft Mute Enable or disable.
Definition: RDA5807.cpp:426
rda_reg04::raw
uint16_t raw
Definition: RDA5807.h:228
RDA5807::setVolume
void setVolume(uint8_t value)
Sets the audio volume level.
Definition: RDA5807.cpp:487
RDA5807::currentFrequency
uint16_t currentFrequency
Definition: RDA5807.h:581
RDA5807::getNext2Block
void getNext2Block(char *c)
Process data received from group 2B.
Definition: RDA5807.cpp:670
RDA5807::getRdsText
char * getRdsText(void)
Gets the RDS Text when the message is of the Group Type 2 version A.
Definition: RDA5807.cpp:746
RDA5807::setup
void setup(uint8_t clock_type=CLOCK_32K, uint8_t oscillator_type=OSCILLATOR_TYPE_CRYSTAL)
Starts the device.
Definition: RDA5807.cpp:219
RDA5807::gpio3Control
int gpio3Control
Can be used to add control to the system via GPIO3 pin.
Definition: RDA5807.h:588
REG04
#define REG04
Definition: RDA5807.h:59
RDA5807::setDelayAfterCrystalOn
void setDelayAfterCrystalOn(uint8_t ms_value)
Set the Delay After Crystal On (default 500ms)
Definition: RDA5807.h:602
word16_to_bytes::raw
uint16_t raw
Definition: RDA5807.h:529
RDA5807::getRdsTime
char * getRdsTime()
Gets the RDS time and date when the Group type is 4.
Definition: RDA5807.cpp:855
RDA5807::gpio2Control
int gpio2Control
Can be used to add control to the system via GPIO2 pin.
Definition: RDA5807.h:587
RDA5807::getNext4Block
void getNext4Block(char *c)
Process data received from group 2A.
Definition: RDA5807.cpp:707
RDA5807::getRdsText2B
char * getRdsText2B(void)
Gets the Text processed for the 2B group.
Definition: RDA5807.cpp:830
RDA5807::getRdsSync
bool getRdsSync()
Get the Rds Sync.
Definition: RDA5807.cpp:913
RDA5807::clearRdsFifo
void clearRdsFifo()
Clears RDS/RBDS FIFO.
Definition: RDA5807.cpp:474
REG05
#define REG05
Definition: RDA5807.h:60
RDA5807::setMono
void setMono(bool value)
Sets audio Mono or stereo.
Definition: RDA5807.cpp:451
RDA5807::isStereo
bool isStereo()
Gets the current Stereo status.
Definition: RDA5807.cpp:463
RDA5807::currentVolume
uint8_t currentVolume
Definition: RDA5807.h:584
RDA5807::getRdsGroupType
uint16_t getRdsGroupType()
Return the group type.
Definition: RDA5807.cpp:628
rda_reg02::raw
uint16_t raw
Definition: RDA5807.h:153
RDA5807::setFrequencyUp
void setFrequencyUp()
Increments the current frequency.
Definition: RDA5807.cpp:270
RDA5807::setSeekThreshold
void setSeekThreshold(uint8_t value)
Sets RSSI Seek Threshold.
Definition: RDA5807.cpp:360
RDA5807::getRealFrequency
uint16_t getRealFrequency()
Gets the current frequency bases on the current channel.
Definition: RDA5807.cpp:336
rda_reg0e::RDSC
uint16_t RDSC
Definition: RDA5807.h:427
RDA5807::resetPin
int resetPin
Definition: RDA5807.h:580
rda_reg08::raw
uint16_t raw
Definition: RDA5807.h:318
rda_reg03::raw
uint16_t raw
Definition: RDA5807.h:201
RDA5807::powerDown
void powerDown()
Power the receiver off.
Definition: RDA5807.cpp:207
RDA5807::getRealChannel
uint16_t getRealChannel()
Gets the current channel stored in 0x0A status register.
Definition: RDA5807.cpp:314
RDA5807::seek
void seek(uint8_t seek_mode, uint8_t direction)
Seek function.
Definition: RDA5807.cpp:347
RDA5807::setI2CBusAddrs
void setI2CBusAddrs(int directAccess, int fullAccess)
Sets alternatives I2C bus address.
Definition: RDA5807.h:611
RDA5807::currentFMSpace
uint8_t currentFMSpace
Definition: RDA5807.h:583
SH_REG0D
#define SH_REG0D
Definition: RDA5807.h:73
REG0B
#define REG0B
Definition: RDA5807.h:64
I2C_ADDR_DIRECT_ACCESS
#define I2C_ADDR_DIRECT_ACCESS
Can be used to access a given register at a time.
Definition: RDA5807.h:28
rda_reg06::raw
uint16_t raw
Definition: RDA5807.h:283
RDA5807::clockType
uint8_t clockType
Definition: RDA5807.h:590
I2C_ADDR_FULL_ACCESS
#define I2C_ADDR_FULL_ACCESS
Can be used to access a set of register at a time.
Definition: RDA5807.h:29
RDA5807::setFrequency
void setFrequency(uint16_t frequency)
Sets the frequency.
Definition: RDA5807.cpp:258
RDA5807::getFrequency
uint16_t getFrequency()
Gets the current frequency.
Definition: RDA5807.cpp:300
RDA5807::deviceAddressFullAccess
int deviceAddressFullAccess
Definition: RDA5807.h:578
RDA5807::getRssi
int getRssi()
Gets the current Rssi.
Definition: RDA5807.cpp:415
RDA5807::setVolumeUp
void setVolumeUp()
Increments the audio volume.
Definition: RDA5807.cpp:512
SH_REG0F
#define SH_REG0F
Definition: RDA5807.h:75
RDA5807::setMute
void setMute(bool value)
Sets Audio mute or unmute.
Definition: RDA5807.cpp:439
RDA5807::maxDelayAftarCrystalOn
uint16_t maxDelayAftarCrystalOn
Definition: RDA5807.h:593
RDA5807::getVolume
uint8_t getVolume()
Gets the current audio volume level.
Definition: RDA5807.cpp:502
SH_REG0E
#define SH_REG0E
Definition: RDA5807.h:74
RDA5807::deviceAddressDirectAccess
int deviceAddressDirectAccess
Definition: RDA5807.h:577
RDA5807::setAllRegisters
void setAllRegisters()
Sets values to the device registers from 0x02 to 0x07.
Definition: RDA5807.cpp:113
RDA5807::softReset
void softReset()
Resets the device.
Definition: RDA5807.cpp:168
rda_reg05::raw
uint16_t raw
Definition: RDA5807.h:246
REG0A
#define REG0A
Definition: RDA5807.h:63
REG03
#define REG03
Definition: RDA5807.h:58
RDA5807::gpio1Control
int gpio1Control
Can be used to add control to the system via GPIO1 pin.
Definition: RDA5807.h:586
RDA5807::setBand
void setBand(uint8_t band=1)
Sets the FM band. See table below.
Definition: RDA5807.cpp:381
rda_reg0a::raw
uint16_t raw
Definition: RDA5807.h:349
RDA5807::setFmDeemphasis
void setFmDeemphasis(uint8_t de)
Sets De-emphasis.
Definition: RDA5807.cpp:554
RDA5807::getStatus
void * getStatus(uint8_t reg)
Gets the register content of a given status register (from 0x0A to 0x0F)
Definition: RDA5807.cpp:85
RDA5807::setRBDS
void setRBDS(bool value)
Sets the RBDS operation.
Definition: RDA5807.cpp:585
RDA5807::getRdsVersionCode
uint8_t getRdsVersionCode(void)
Gets the version code (extracted from the Block B)
Definition: RDA5807.cpp:642
rda_reg0b::raw
uint16_t raw
Definition: RDA5807.h:379
rda_reg07::raw
uint16_t raw
Definition: RDA5807.h:301
REG0F
#define REG0F
Definition: RDA5807.h:68
RDA5807::getDeviceId
uint8_t getDeviceId()
Gets the Device identification.
Definition: RDA5807.cpp:542
RDA5807::setFrequencyDown
void setFrequencyDown()
Decrements the current frequency.
Definition: RDA5807.cpp:285
RDA5807::setVolumeDown
void setVolumeDown()
Decrements the audio volume.
Definition: RDA5807.cpp:526
MAX_DELAY_AFTER_OSCILLATOR
#define MAX_DELAY_AFTER_OSCILLATOR
Definition: RDA5807.h:25
RDA5807::getRdsFlagAB
uint8_t getRdsFlagAB(void)
Returns the current Text Flag A/B
Definition: RDA5807.cpp:614
REG0E
#define REG0E
Definition: RDA5807.h:67
RDA5807::oscillatorType
uint8_t oscillatorType
Definition: RDA5807.h:591
rda_reg0d::RDSB
uint16_t RDSB
Definition: RDA5807.h:410
RDA5807::getRdsProgramType
uint8_t getRdsProgramType(void)
Returns the Program Type (extracted from the Block B)
Definition: RDA5807.cpp:656
RDA5807::getRdsText2A
char * getRdsText2A(void)
Gets the Text processed for the 2A group.
Definition: RDA5807.cpp:801
RDA5807::setChannel
void setChannel(uint16_t channel)
Sets the channel.
Definition: RDA5807.cpp:242
CLOCK_32K
#define CLOCK_32K
32.768kHz
Definition: RDA5807.h:35
rds_blockb::blockB
uint16_t blockB
Definition: RDA5807.h:492
REG02
#define REG02
Definition: RDA5807.h:57
rda_reg0c::RDSA
uint16_t RDSA
BLOCK A ( in RDS mode) or BLOCK E (in RBDS mode when ABCD_E flag is 1)
Definition: RDA5807.h:394
RDA5807::setSpace
void setSpace(uint8_t space=0)
Sets the FM channel space.
Definition: RDA5807.cpp:402
RDA5807::currentFMBand
uint8_t currentFMBand
Definition: RDA5807.h:582
RDA5807::setGpio
void setGpio(uint8_t gpioPin, uint8_t gpioSetup=0, int mcuPin=-1)
Set the Device GPIO pins.
Definition: RDA5807.cpp:33
rda_reg00::raw
uint16_t raw
Definition: RDA5807.h:95
OSCILLATOR_TYPE_CRYSTAL
#define OSCILLATOR_TYPE_CRYSTAL
Crystal.
Definition: RDA5807.h:32
rda_reg01::raw
uint16_t raw
Definition: RDA5807.h:110