amfm2  2.1.0.0
amfm2.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef AMFM2_H
29 #define AMFM2_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_i2c_master.h"
52 
77 #define AMFM2_CMD_POWER_UP 0x01
78 #define AMFM2_CMD_GET_REV 0x10
79 #define AMFM2_CMD_POWER_DOWN 0x11
80 #define AMFM2_CMD_SET_PROPERTY 0x12
81 #define AMFM2_CMD_GET_PROPERTY 0x13
82 #define AMFM2_CMD_GET_INT_STATUS 0x14
83 #define AMFM2_CMD_PATCH_ARGS 0x15
84 #define AMFM2_CMD_PATCH_DATA 0x16
85 #define AMFM2_CMD_TX_TUNE_FREQ 0x30
86 #define AMFM2_CMD_TX_TUNE_POWER 0x31
87 #define AMFM2_CMD_TX_TUNE_MEASURE 0x32
88 #define AMFM2_CMD_TX_TUNE_STATUS 0x33
89 #define AMFM2_CMD_TX_ASQ_STATUS 0x34
90 #define AMFM2_CMD_TX_RDS_BUFF 0x35
91 #define AMFM2_CMD_TX_TX_RDS_PS 0x36
92 #define AMFM2_CMD_GPIO_CTL 0x80
93 #define AMFM2_CMD_GPIO_SET 0x81
94 
99 #define AMFM2_CMD_AM_TUNE_FREQ 0x40
100 #define AMFM2_CMD_AM_SEEK_START 0x41
101 #define AMFM2_CMD_AM_TUNE_STATUS 0x42
102 #define AMFM2_CMD_AM_RSQ_STATUS 0x43
103 #define AMFM2_CMD_AM_AGC_STATUS 0x47
104 #define AMFM2_CMD_AM_AGC_OVERRIDE 0x48
105 
110 #define AMFM2_CMD_FM_TUNE_FREQ 0x20
111 #define AMFM2_CMD_FM_SEEK_START 0x21
112 #define AMFM2_CMD_FM_TUNE_STATUS 0x22
113 #define AMFM2_CMD_FM_RSQ_STATUS 0x23
114 #define AMFM2_CMD_FM_RDS_STATUS 0x24
115 #define AMFM2_CMD_FM_AGC_STATUS 0x27
116 #define AMFM2_CMD_FM_AGC_OVERRIDE 0x28
117  // amfm2_cmd
119 
134 #define AMFM2_RSP_STATUS_CTS 0x80
135 #define AMFM2_RSP_STATUS_ERROR 0x40
136 #define AMFM2_RSP_STATUS_RDSINT 0x04
137 #define AMFM2_RSP_STATUS_ASQINT 0x02
138 #define AMFM2_RSP_STATUS_STCINT 0x01
139 
144 #define AMFM2_PWRUP_ARG_CTSIEN 0x80
145 #define AMFM2_PWRUP_ARG_GPO2OEN 0x40
146 #define AMFM2_PWRUP_ARG_PATCH 0x20
147 #define AMFM2_PWRUP_ARG_XOSCEN 0x10
148 #define AMFM2_PWRUP_ARG_FUNC_FMRECEIVE 0x00
149 #define AMFM2_PWRUP_ARG_FUNC_QUERYLIBID 0x0F
150 #define AMFM2_PWRUP_ARG_OPMODE_ANALOGOUT 0x05
151 #define AMFM2_PWRUP_ARG_OPMODE_DIGITALOUT 0x0B
152 #define AMFM2_PWRUP_ARG_OPMODE_DIGITALOUTS 0xB0
153 #define AMFM2_FMTUNEFREQ_ARG_FREEZE 0x02
154 #define AMFM2_FMTUNEFREQ_ARG_FAST 0x01
155 #define AMFM2_FMSEEKSTART_ARG_SEEKUP 0x08
156 #define AMFM2_FMSEEKSTART_ARG_WRAP 0x04
157 #define AMFM2_FMTUNESTATUS_ARG_CANCEL 0x02
158 #define AMFM2_FMTUNESTATUS_ARG_INTACK 0x01
159 #define AMFM2_FMRSQSTATUS_ARG_INTACK 0x01
160 #define AMFM2_FMRDSSTATUS_ARG_STATUSONLY 0x04
161 #define AMFM2_FMRDSSTATUS_ARG_MTFIFO 0x02
162 #define AMFM2_FMRDSSTATUS_ARG_INTACK 0x01
163 #define AMFM2_FMAGCOVERRIDE_ARG_RFAGCDIS 0x01
164 #define AMFM2_GPIOCTL_ARG_GPO3OEN 0x08
165 #define AMFM2_GPIOCTL_ARG_GPO2OEN 0x04
166 #define AMFM2_GPIOCTL_ARG_GPO1OEN 0x02
167 #define AMFM2_GPIOSET_ARG_GPO3LEVEL 0x08
168 #define AMFM2_GPIOSET_ARG_GPO2LEVEL 0x04
169 #define AMFM2_GPIOSET_ARG_GPO1LEVEL 0x02
170 
175 #define AMFM2_SET_VOLUME_MUTE 0
176 #define AMFM2_SET_VOLUME_MAX 63
177 
182 #define AMFM2_PROP_GPOIEN_H 0x00
183 #define AMFM2_PROP_GPOIEN_L 0x01
184 #define AMFM2_PROP_DIGITALOUTFORMAT_H 0x01
185 #define AMFM2_PROP_DIGITALOUTFORMAT_L 0x02
186 #define AMFM2_PROP_DIGITALOUTSAMPLERATE_H 0x01
187 #define AMFM2_PROP_DIGITALOUTSAMPLERATE_L 0x04
188 #define AMFM2_PROP_REFCLKFREQ_H 0x02
189 #define AMFM2_PROP_REFCLKFREQ_L 0x01
190 #define AMFM2_PROP_REFCLKPRESCALE_H 0x02
191 #define AMFM2_PROP_REFCLKPRESCALE_L 0x02
192 #define AMFM2_PROP_RXVOLUME_H 0x40
193 #define AMFM2_PROP_RXVOLUME_L 0x00
194 #define AMFM2_PROP_RXHMUTE_H 0x40
195 #define AMFM2_PROP_RXHMUTE_L 0x01
196 
201 #define AMFM2_PROP_FMDEEMPHASIS_H 0x11
202 #define AMFM2_PROP_FMDEEMPHASIS_L 0x00
203 #define AMFM2_PROP_AMDEEMPHASIS_H 0x31
204 #define AMFM2_PROP_AMDEEMPHASIS_L 0x00
205 #define AMFM2_PROP_FMCHANFILTER_H 0x11
206 #define AMFM2_PROP_FMCHANFILTER_L 0x02
207 #define AMFM2_PROP_AMCHANFILTER_H 0x31
208 #define AMFM2_PROP_AMCHANFILTER_L 0x02
209 #define AMFM2_PROP_AMRSQINTS_H 0x32
210 #define AMFM2_PROP_AMRSQINTS_L 0x00
211 #define AMFM2_PROP_AMNBRATE_H 0x39
212 #define AMFM2_PROP_AMNBRATE_L 0x02
213 #define AMFM2_PROP_AMNBIIRFILTER_H 0x39
214 #define AMFM2_PROP_AMNBIIRFILTER_L 0x03
215 #define AMFM2_PROP_AMNBDELAY_H 0x39
216 #define AMFM2_PROP_AMNBDELAY_L 0x04
217 
222 #define AMFM2_PROP_FMBLENDSTEREOTHS_H 0x11
223 #define AMFM2_PROP_FMBLENDSTEREOTHS_L 0x05
224 #define AMFM2_PROP_FMBLENDMONOTHS_H 0x11
225 #define AMFM2_PROP_FMBLENDMONOTHS_L 0x06
226 #define AMFM2_PROP_FMNBDETECTTHS_H 0x19
227 #define AMFM2_PROP_FMNBDETECTTHS_L 0x00
228 #define AMFM2_PROP_FMNBINTERVAL_H 0x19
229 #define AMFM2_PROP_FMNBINTERVAL_L 0x01
230 #define AMFM2_PROP_FMNBRATE_H 0x19
231 #define AMFM2_PROP_FMNBRATE_L 0x02
232 #define AMFM2_PROP_FMNBIIRFILTER_H 0x19
233 #define AMFM2_PROP_FMNBIIRFILTER_L 0x03
234 #define AMFM2_PROP_FMNBDELAY_H 0x19
235 #define AMFM2_PROP_FMNBDELAY_L 0x04
236 
241 #define AMFM2_PROPVALH_RSQREP 0x08
242 #define AMFM2_PROPVALH_RDSREP 0x04
243 #define AMFM2_PROPVALH_STCREP 0x01
244 #define AMFM2_PROPVALL_CTSIEN 0x80
245 #define AMFM2_PROPVALL_ERRIEN 0x40
246 #define AMFM2_PROPVALL_RSQIEN 0x08
247 #define AMFM2_PROPVALL_RDSIEN 0x04
248 #define AMFM2_PROPVALL_STCIEN 0x01
249 #define AMFM2_PROPVALL_OFALL 0x80
250 #define AMFM2_PROPVALL_LMUTE 0x02
251 #define AMFM2_PROPVALL_RMUTE 0x01
252 
258 #define AMFM2_DEVICE_ADDRESS_GND 0x11
259 #define AMFM2_DEVICE_ADDRESS_VCC 0x63
260  // amfm2_set
262 
277 #define AMFM2_MAP_MIKROBUS( cfg, mikrobus ) \
278  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
279  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
280  cfg.ema = MIKROBUS( mikrobus, MIKROBUS_AN ); \
281  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
282  cfg.gp1 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
283  cfg.gp2 = MIKROBUS( mikrobus, MIKROBUS_INT )
284  // amfm2_map // amfm2
287 
292 typedef struct
293 {
294  // Output pins
295  digital_out_t ema;
296  digital_out_t rst;
298  // Input pins
299  digital_in_t gp1;
300  digital_in_t gp2;
302  // Modules
303  i2c_master_t i2c;
305  // I2C slave address
306  uint8_t slave_address;
308 } amfm2_t;
309 
314 typedef struct
315 {
316  pin_name_t scl;
317  pin_name_t sda;
319  pin_name_t ema;
320  pin_name_t rst;
321  pin_name_t gp1;
322  pin_name_t gp2;
324  uint32_t i2c_speed;
325  uint8_t i2c_address;
327 } amfm2_cfg_t;
328 
333 typedef struct
334 {
335  uint8_t pn;
336  uint8_t fwmajor;
337  uint8_t fwminor;
338  uint16_t patch;
339  uint8_t cmpmajor;
340  uint8_t cmpminor;
341  uint8_t chiprev;
344 
349 typedef enum
350 {
351  AMFM2_OK = 0,
352  AMFM2_ERROR = -1
353 
355 
372 
386 err_t amfm2_init ( amfm2_t *ctx, amfm2_cfg_t *cfg );
387 
400 err_t amfm2_default_cfg ( amfm2_t *ctx );
401 
416 err_t amfm2_write_data ( amfm2_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
417 
432 err_t amfm2_generic_read ( amfm2_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
433 
444 void amfm2_hw_reset ( amfm2_t *ctx );
445 
457 
469 
481 uint8_t amfm2_get_gp1 ( amfm2_t *ctx );
482 
494 uint8_t amfm2_get_gp2 ( amfm2_t *ctx );
495 
507 void amfm2_set_rst ( amfm2_t *ctx, uint8_t pin_state );
508 
521 err_t amfm2_wait_rsp ( amfm2_t *ctx );
522 
540 err_t amfm2_set_command ( amfm2_t *ctx, uint8_t cmd, uint8_t *arg,
541  uint8_t arg_len, uint8_t *rsp, uint8_t rsp_len );
542 
557 err_t amfm2_get_response ( amfm2_t *ctx, uint8_t *rsp, uint8_t rsp_len );
558 
574 
588 err_t amfm2_seek_station ( amfm2_t *ctx, uint8_t *rsp );
589 
604 err_t amfm2_set_volume ( amfm2_t *ctx, uint8_t volume, uint8_t *rsp );
605 
619 err_t amfm2_sound_off ( amfm2_t *ctx, uint8_t *rsp );
620 
634 err_t amfm2_sound_on ( amfm2_t *ctx, uint8_t *rsp );
635 
650 err_t amfm2_tuning_freq_up ( amfm2_t *ctx, uint8_t *rsp );
651 
666 err_t amfm2_tuning_freq_down ( amfm2_t *ctx, uint8_t *rsp );
667 
682 err_t amfm2_tuning_freq ( amfm2_t *ctx, float frequency, uint8_t *rsp );
683 
698 err_t amfm2_get_tuning_freq ( amfm2_t *ctx, float *frequency );
699 
713 
727 
741 
755 
756 #ifdef __cplusplus
757 }
758 #endif
759 #endif // AMFM2_H
760  // amfm2
762 
763 // ------------------------------------------------------------------------ END
amfm2_device_info_t::chiprev
uint8_t chiprev
Definition: amfm2.h:341
amfm2_get_rev
err_t amfm2_get_rev(amfm2_t *ctx, amfm2_device_info_t *rev)
AM/FM 2 get revision information function.
amfm2_device_info_t
AM/FM 2 Click device information object.
Definition: amfm2.h:334
amfm2_tuning_freq
err_t amfm2_tuning_freq(amfm2_t *ctx, float frequency, uint8_t *rsp)
AM/FM 2 tuning frequency function.
amfm2_sound_on
err_t amfm2_sound_on(amfm2_t *ctx, uint8_t *rsp)
AM/FM 2 sound on function.
amfm2_device_info_t::fwminor
uint8_t fwminor
Definition: amfm2.h:337
AMFM2_OK
@ AMFM2_OK
Definition: amfm2.h:351
amfm2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: amfm2.h:324
amfm2_headphone_amp_disable
void amfm2_headphone_amp_disable(amfm2_t *ctx)
AM/FM 2 disable headphone amp function.
amfm2_wait_rsp
err_t amfm2_wait_rsp(amfm2_t *ctx)
AM/FM 2 wait response function.
amfm2_headphone_amp_enable
void amfm2_headphone_amp_enable(amfm2_t *ctx)
AM/FM 2 enable headphone amp function.
amfm2_cfg_t
AM/FM 2 Click configuration object.
Definition: amfm2.h:315
amfm2_return_value_t
amfm2_return_value_t
AM/FM 2 Click return value data.
Definition: amfm2.h:350
amfm2_get_response
err_t amfm2_get_response(amfm2_t *ctx, uint8_t *rsp, uint8_t rsp_len)
AM/FM 2 get response function.
amfm2_device_info_t::cmpmajor
uint8_t cmpmajor
Definition: amfm2.h:339
amfm2_cfg_t::ema
pin_name_t ema
Definition: amfm2.h:319
amfm2_device_info_t::fwmajor
uint8_t fwmajor
Definition: amfm2.h:336
amfm2_cfg_t::gp2
pin_name_t gp2
Definition: amfm2.h:322
amfm2_check_seek_tune_complete
err_t amfm2_check_seek_tune_complete(amfm2_t *ctx)
AM/FM 2 check CTS function.
amfm2_t::rst
digital_out_t rst
Definition: amfm2.h:296
amfm2_cfg_t::scl
pin_name_t scl
Definition: amfm2.h:316
amfm2_t
AM/FM 2 Click context object.
Definition: amfm2.h:293
amfm2_default_cfg
err_t amfm2_default_cfg(amfm2_t *ctx)
AM/FM 2 default configuration function.
amfm2_cfg_t::gp1
pin_name_t gp1
Definition: amfm2.h:321
amfm2_t::i2c
i2c_master_t i2c
Definition: amfm2.h:303
amfm2_hw_reset
void amfm2_hw_reset(amfm2_t *ctx)
AM/FM 2 hardware reset function.
amfm2_write_data
err_t amfm2_write_data(amfm2_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
AM/FM 2 I2C writing function.
amfm2_device_info_t::pn
uint8_t pn
Definition: amfm2.h:335
amfm2_t::slave_address
uint8_t slave_address
Definition: amfm2.h:306
amfm2_tuning_freq_down
err_t amfm2_tuning_freq_down(amfm2_t *ctx, uint8_t *rsp)
AM/FM 2 tuning frequency down function.
amfm2_device_info_t::cmpminor
uint8_t cmpminor
Definition: amfm2.h:340
amfm2_get_tuning_freq
err_t amfm2_get_tuning_freq(amfm2_t *ctx, float *frequency)
AM/FM 2 get tuning frequency function.
amfm2_check_rds_interrupt
err_t amfm2_check_rds_interrupt(amfm2_t *ctx)
AM/FM 2 check RDS interrupt function.
amfm2_sound_off
err_t amfm2_sound_off(amfm2_t *ctx, uint8_t *rsp)
AM/FM 2 sound off function.
amfm2_set_rst
void amfm2_set_rst(amfm2_t *ctx, uint8_t pin_state)
AM/FM 2 set state of RST pin function.
amfm2_get_gp2
uint8_t amfm2_get_gp2(amfm2_t *ctx)
AM/FM 2 get state of GP2 pin function.
AMFM2_ERROR
@ AMFM2_ERROR
Definition: amfm2.h:352
amfm2_cfg_setup
void amfm2_cfg_setup(amfm2_cfg_t *cfg)
AM/FM 2 configuration object setup function.
amfm2_seek_station
err_t amfm2_seek_station(amfm2_t *ctx, uint8_t *rsp)
AM/FM 2 seek station function.
amfm2_check_status_error
err_t amfm2_check_status_error(amfm2_t *ctx)
AM/FM 2 check status error function.
amfm2_init
err_t amfm2_init(amfm2_t *ctx, amfm2_cfg_t *cfg)
AM/FM 2 initialization function.
amfm2_device_info_t::patch
uint16_t patch
Definition: amfm2.h:338
amfm2_cfg_t::sda
pin_name_t sda
Definition: amfm2.h:317
amfm2_cfg_t::i2c_address
uint8_t i2c_address
Definition: amfm2.h:325
amfm2_t::gp1
digital_in_t gp1
Definition: amfm2.h:299
amfm2_cfg_t::rst
pin_name_t rst
Definition: amfm2.h:320
amfm2_tuning_freq_up
err_t amfm2_tuning_freq_up(amfm2_t *ctx, uint8_t *rsp)
AM/FM 2 tuning frequency up function.
amfm2_check_clear_to_send
err_t amfm2_check_clear_to_send(amfm2_t *ctx)
AM/FM 2 check CTS function.
amfm2_t::ema
digital_out_t ema
Definition: amfm2.h:295
amfm2_set_command
err_t amfm2_set_command(amfm2_t *ctx, uint8_t cmd, uint8_t *arg, uint8_t arg_len, uint8_t *rsp, uint8_t rsp_len)
AM/FM 2 set command function.
amfm2_get_gp1
uint8_t amfm2_get_gp1(amfm2_t *ctx)
AM/FM 2 get state of GP1 pin function.
amfm2_set_volume
err_t amfm2_set_volume(amfm2_t *ctx, uint8_t volume, uint8_t *rsp)
AM/FM 2 set volume function.
amfm2_t::gp2
digital_in_t gp2
Definition: amfm2.h:300
amfm2_generic_read
err_t amfm2_generic_read(amfm2_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
AM/FM 2 I2C reading function.