vumeter  2.0.0.0
vumeter.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 VUMETER_H
29 #define VUMETER_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_spi_master.h"
52 #include "spi_specifics.h"
53 #include "drv_analog_in.h"
54 
75 #define VUMETER_GAIN_LEVEL_MAX 0xFF
76 #define VUMETER_GAIN_LEVEL_MID 0x80
77 #define VUMETER_GAIN_LEVEL_MIN 0x00
78 
83 #define VUMETER_DBU_VREF 0.7746
84 #define VUMETER_DBU_TO_VU 4.0
85 #define VUMETER_RMS_FOR_20VU 0.123
86 #define VUMETER_MIN_VU -20.0
87 #define VUMETER_VCC_3V3 3.3
88 #define VUMETER_VCC_5V 5.0
89 
98 #define VUMETER_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
99 #define VUMETER_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
100  // vumeter_set
102 
117 #define VUMETER_MAP_MIKROBUS( cfg, mikrobus ) \
118  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
119  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
120  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
121  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
122  cfg.an = MIKROBUS( mikrobus, MIKROBUS_AN )
123  // vumeter_map // vumeter
126 
131 typedef struct
132 {
133  // Modules
134  spi_master_t spi;
135  analog_in_t adc;
137  pin_name_t chip_select;
139 } vumeter_t;
140 
145 typedef struct
146 {
147  // Communication gpio pins
148  pin_name_t miso;
149  pin_name_t mosi;
150  pin_name_t sck;
151  pin_name_t cs;
152  pin_name_t an;
154  // static variable
155  uint32_t spi_speed;
156  spi_master_mode_t spi_mode;
157  spi_master_chip_select_polarity_t cs_polarity;
159  analog_in_resolution_t resolution;
160  float vref;
162 } vumeter_cfg_t;
163 
168 typedef enum
169 {
171  VUMETER_ERROR = -1
172 
174 
191 
206 err_t vumeter_init ( vumeter_t *ctx, vumeter_cfg_t *cfg );
207 
220 err_t vumeter_read_an_pin_value ( vumeter_t *ctx, uint16_t *data_out );
221 
236 err_t vumeter_read_an_pin_voltage ( vumeter_t *ctx, float *data_out );
237 
250 err_t vumeter_set_gain_level ( vumeter_t *ctx, uint8_t gain_lvl );
251 
263 float vumeter_calculate_vu_level ( vumeter_t *ctx, uint16_t sample_rate_ms );
264 
265 #ifdef __cplusplus
266 }
267 #endif
268 #endif // VUMETER_H
269  // vumeter
271 
272 // ------------------------------------------------------------------------ END
vumeter_cfg_setup
void vumeter_cfg_setup(vumeter_cfg_t *cfg)
VU Meter configuration object setup function.
vumeter_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: vumeter.h:156
vumeter_read_an_pin_value
err_t vumeter_read_an_pin_value(vumeter_t *ctx, uint16_t *data_out)
VU Meter read AN pin value function.
vumeter_init
err_t vumeter_init(vumeter_t *ctx, vumeter_cfg_t *cfg)
VU Meter initialization function.
vumeter_t::spi
spi_master_t spi
Definition: vumeter.h:134
vumeter_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: vumeter.h:157
spi_specifics.h
This file contains SPI specific macros, functions, etc.
vumeter_return_value_t
vumeter_return_value_t
VU Meter Click return value data.
Definition: vumeter.h:169
VUMETER_OK
@ VUMETER_OK
Definition: vumeter.h:170
vumeter_t
VU Meter Click context object.
Definition: vumeter.h:132
vumeter_cfg_t::sck
pin_name_t sck
Definition: vumeter.h:150
vumeter_cfg_t::spi_speed
uint32_t spi_speed
Definition: vumeter.h:155
VUMETER_ERROR
@ VUMETER_ERROR
Definition: vumeter.h:171
vumeter_cfg_t::resolution
analog_in_resolution_t resolution
Definition: vumeter.h:159
vumeter_cfg_t::vref
float vref
Definition: vumeter.h:160
vumeter_t::chip_select
pin_name_t chip_select
Definition: vumeter.h:137
vumeter_calculate_vu_level
float vumeter_calculate_vu_level(vumeter_t *ctx, uint16_t sample_rate_ms)
VU Meter calculate VU level function.
vumeter_cfg_t::mosi
pin_name_t mosi
Definition: vumeter.h:149
vumeter_cfg_t::miso
pin_name_t miso
Definition: vumeter.h:148
vumeter_cfg_t::cs
pin_name_t cs
Definition: vumeter.h:151
vumeter_t::adc
analog_in_t adc
Definition: vumeter.h:135
vumeter_cfg_t::an
pin_name_t an
Definition: vumeter.h:152
vumeter_read_an_pin_voltage
err_t vumeter_read_an_pin_voltage(vumeter_t *ctx, float *data_out)
VU Meter read AN pin voltage level function.
vumeter_cfg_t
VU Meter Click configuration object.
Definition: vumeter.h:146
vumeter_set_gain_level
err_t vumeter_set_gain_level(vumeter_t *ctx, uint8_t gain_lvl)
VU Meter set gain level function.