mcp73213  2.0.0.0
mcp73213.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to 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 MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef MCP73213_H
36 #define MCP73213_H
37 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_spi_master.h"
51 
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
64 #define MCP73213_MAP_MIKROBUS( cfg, mikrobus ) \
65  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
66  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
67  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
68  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS )
69 
75 #define MCP73213_RETVAL uint8_t
76 
77 #define MCP73213_OK 0x00
78 #define MCP73213_INIT_ERROR 0xFF
79 
85 #define MCP73213_SPI_CMD_READ 0x0C
86 #define MCP73213_SPI_CMD_WRITE 0x00
87 
93 #define MCP73213_VOLATILE_WIPER_0 0x00
94 #define MCP73213_VOLATILE_WIPER_1 0x01
95 #define MCP73213_NON_VOLATILE_WIPER_0 0x02
96 #define MCP73213_NON_VOLATILE_WIPER_1 0x03
97 #define MCP73213_VOLATILE_TCON_REGISTER 0x04
98 #define MCP73213_STATUS_REGISTER 0x05
99 #define MCP73213_DATA_EEPROM_6 0x06
100 #define MCP73213_DATA_EEPROM_7 0x07
101 #define MCP73213_DATA_EEPROM_8 0x08
102 #define MCP73213_DATA_EEPROM_9 0x09
103 #define MCP73213_DATA_EEPROM_A 0x0A
104 #define MCP73213_DATA_EEPROM_B 0x0B
105 #define MCP73213_DATA_EEPROM_C 0x0C
106 #define MCP73213_DATA_EEPROM_D 0x0D
107 #define MCP73213_DATA_EEPROM_E 0x0E
108 #define MCP73213_DATA_EEPROM_F 0x0F
109 //#define MCP73213_DATA_WRITE_CMD 0x00
116 #define P73213_VREF_3V3 3.30
117 #define P73213_VREF_5V 5.00
118 
124 #define MCP73213_OUTPUT_130_mA 0x00
125 #define MCP73213_OUTPUT_250_mA 0x82
126 #define MCP73213_OUTPUT_550_mA 0xCC
127 #define MCP73213_OUTPUT_1100_mA 0xE9
128  // End group macro
131 // --------------------------------------------------------------- PUBLIC TYPES
140 typedef struct
141 {
142  digital_out_t cs;
143 
144  // Modules
145 
146  spi_master_t spi;
147  pin_name_t chip_select;
148 
149 } mcp73213_t;
150 
154 typedef struct
155 {
156  // Communication gpio pins
157 
158  pin_name_t miso;
159  pin_name_t mosi;
160  pin_name_t sck;
161  pin_name_t cs;
162 
163  // static variable
164 
165  uint32_t spi_speed;
166  spi_master_mode_t spi_mode;
167  spi_master_chip_select_polarity_t cs_polarity;
168 
170  // End types group
172 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
173 
178 #ifdef __cplusplus
179 extern "C"{
180 #endif
181 
191 
200 
213 (
214  mcp73213_t *ctx,
215  uint8_t *wr_buf,
216  uint16_t wr_len,
217  uint8_t *rd_buf,
218  uint16_t rd_len
219 );
220 
231 void mcp73213_write_data ( mcp73213_t *ctx, uint8_t command, uint8_t write_data );
232 
245 uint8_t mcp73213_read_data ( mcp73213_t *ctx, uint8_t command );
246 
256 void mcp73213_set_current_output ( mcp73213_t *ctx, uint8_t out_data );
257 
270 
284 float mcp73213_convert_output ( uint8_t value_adc, float v_ref );
285 
286 #ifdef __cplusplus
287 }
288 #endif
289 #endif // _MCP73213_H_
290  // End public_function group
293 
294 // ------------------------------------------------------------------------- END
mcp73213_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: mcp73213.h:166
mcp73213_read_data
uint8_t mcp73213_read_data(mcp73213_t *ctx, uint8_t command)
Generic read 8-bit data function.
mcp73213_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: mcp73213.h:167
mcp73213_cfg_t::miso
pin_name_t miso
Definition: mcp73213.h:158
mcp73213_t::chip_select
pin_name_t chip_select
Definition: mcp73213.h:147
mcp73213_cfg_t::mosi
pin_name_t mosi
Definition: mcp73213.h:159
mcp73213_set_current_output
void mcp73213_set_current_output(mcp73213_t *ctx, uint8_t out_data)
Set values for output current function.
mcp73213_get_status
uint8_t mcp73213_get_status(mcp73213_t *ctx)
Get the status register data function.
mcp73213_generic_transfer
void mcp73213_generic_transfer(mcp73213_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
mcp73213_t
Click ctx object definition.
Definition: mcp73213.h:141
mcp73213_cfg_setup
void mcp73213_cfg_setup(mcp73213_cfg_t *cfg)
Config Object Initialization function.
mcp73213_cfg_t
Click configuration structure definition.
Definition: mcp73213.h:155
mcp73213_cfg_t::cs
pin_name_t cs
Definition: mcp73213.h:161
mcp73213_t::spi
spi_master_t spi
Definition: mcp73213.h:146
MCP73213_RETVAL
#define MCP73213_RETVAL
Definition: mcp73213.h:75
mcp73213_write_data
void mcp73213_write_data(mcp73213_t *ctx, uint8_t command, uint8_t write_data)
Generic write 8-bit data function.
mcp73213_convert_output
float mcp73213_convert_output(uint8_t value_adc, float v_ref)
Convert ADC value to volatage.
mcp73213_t::cs
digital_out_t cs
Definition: mcp73213.h:142
mcp73213_init
MCP73213_RETVAL mcp73213_init(mcp73213_t *ctx, mcp73213_cfg_t *cfg)
Initialization function.
mcp73213_cfg_t::spi_speed
uint32_t spi_speed
Definition: mcp73213.h:165
mcp73213_cfg_t::sck
pin_name_t sck
Definition: mcp73213.h:160