usbcsink3 2.1.0.0
usbcsink3.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 USBCSINK3_H
29#define USBCSINK3_H
30
31#ifdef __cplusplus
32extern "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#include "drv_spi_master.h"
53#include "spi_specifics.h"
54#include "drv_analog_in.h"
55
76#define USBCSINK3_REG_INPUT_PORT 0x00
77#define USBCSINK3_REG_OUTPUT_PORT 0x01
78#define USBCSINK3_REG_POLARITY_INV 0x02
79#define USBCSINK3_REG_CONFIG 0x03
80#define USBCSINK3_REG_SPECIAL_FUNC 0x50
81
82 // usbcsink3_reg
83
93#define USBCSINK3_CMD_SEL_NONE 0x00
94#define USBCSINK3_CMD_SEL_DATA_WRITE 0x10
95#define USBCSINK3_CMD_SEL_SHUTDOWN 0x20
96
101#define USBCSINK3_POT_SEL_DUMMY_CODE 0x00
102#define USBCSINK3_POT_SEL_DIGI_POT_1 0x01
103#define USBCSINK3_POT_SEL_DIGI_POT_2 0x02
104#define USBCSINK3_POT_SEL_DIGI_POT_BOTH 0x03
105
106 // usbcsink3_cmd
107
122#define USBCSINK3_NO_PIN_BIT_MASK 0x00
123#define USBCSINK3_PIN_0_BIT_MASK 0x01
124#define USBCSINK3_PIN_1_BIT_MASK 0x02
125#define USBCSINK3_PIN_2_BIT_MASK 0x04
126#define USBCSINK3_PIN_3_BIT_MASK 0x08
127#define USBCSINK3_ALL_PINS_BIT_MASK 0xF0
128
133#define USBCSINK3_DIRECTION_OUTPUT 0x00
134#define USBCSINK3_DIRECTION_INPUT 0x01
135
140#define USBCSINK3_VSEL0_DISABLE 0x00
141#define USBCSINK3_VSEL0_ENABLE 0x01
142#define USBCSINK3_VSEL0_PIN_BIT_MASK 0x0E
143#define USBCSINK3_VSEL1_DISABLE 0x00
144#define USBCSINK3_VSEL1_ENABLE 0x02
145#define USBCSINK3_VSEL1_PIN_BIT_MASK 0x0D
146#define USBCSINK3_VSEL2_DISABLE 0x00
147#define USBCSINK3_VSEL2_ENABLE 0x04
148#define USBCSINK3_VSEL2_PIN_BIT_MASK 0x0B
149
154#define USBCSINK3_SPEC_FUNC_P3_AS_INT 0x80
155#define USBCSINK3_SPEC_FUNC_PU_DISABLED 0x40
156
161#define USBCSINK3_VBUS_VTG_0V 0.0f
162#define USBCSINK3_VBUS_VTG_5V 5.0f
163#define USBCSINK3_VBUS_VTG_9V 9.0f
164#define USBCSINK3_VBUS_VTG_15V 15.0f
165#define USBCSINK3_VBUS_VTG_20V 20.3f
166#define USBCSINK3_VBUS_VTG_4_3V 4.3f
167#define USBCSINK3_VBUS_VTG_8_6V 8.6f
168#define USBCSINK3_VBUS_VTG_12_9V 12.9f
169#define USBCSINK3_VBUS_VTG_17_2V 17.2f
170
175#define USBCSINK3_WIPER_POS_MAX 255
176#define USBCSINK3_RES_6_8_KOHM 6.8f
177#define USBCSINK3_RES_16_KOHM 16.0f
178#define USBCSINK3_RES_27_KOHM 27.0f
179#define USBCSINK3_RES_36_KOHM 36.0f
180#define USBCSINK3_RES_45_3_KOHM 45.3f
181#define USBCSINK3_RES_56_KOHM 56.0f
182#define USBCSINK3_RES_64_9_KOHM 64.9f
183#define USBCSINK3_RES_75_KOHM 75.0f
184#define USBCSINK3_RES_82_KOHM 82.0f
185#define USBCSINK3_RES_91_KOHM 91.0f
186#define USBCSINK3_RES_100_KOHM 100.0f
187#define USBCSINK3_VBUS_R11_RES_73_2_KOHM 73.2f
188#define USBCSINK3_VBUS_R13_RES_10_KOHM 10.0f
189
194#define USBCSINK3_ADC_RESOLUTION 0x0FFF
195#define USBCSINK3_VREF_3V3 3.3
196#define USBCSINK3_VREF_5V 5.0
197
203#define USBCSINK3_DEVICE_ADDRESS 0x41
204
213#define USBCSINK3_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
214#define USBCSINK3_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
215
216 // usbcsink3_set
217
232#define USBCSINK3_MAP_MIKROBUS( cfg, mikrobus ) \
233 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
234 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
235 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
236 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
237 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
238 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
239 cfg.an = MIKROBUS( mikrobus, MIKROBUS_AN )
240
241 // usbcsink3_map
242 // usbcsink3
243
244
249typedef struct usbcsink3_s
250{
251 analog_in_t adc;
252 float vref;
254 i2c_master_t i2c;
255 spi_master_t spi;
258 pin_name_t chip_select;
261
266typedef struct
267{
268 pin_name_t scl;
269 pin_name_t sda;
270 pin_name_t miso;
271 pin_name_t mosi;
272 pin_name_t sck;
273 pin_name_t cs;
274 pin_name_t an;
276 analog_in_resolution_t resolution;
277 float vref;
279 uint32_t i2c_speed;
280 uint8_t i2c_address;
282 uint32_t spi_speed;
283 spi_master_mode_t spi_mode;
284 spi_master_chip_select_polarity_t cs_polarity;
287
298
315
334
335
352
367
381
396err_t usbcsink3_i2c_write ( usbcsink3_t *ctx, uint8_t reg, uint8_t data_in );
397
413err_t usbcsink3_i2c_read ( usbcsink3_t *ctx, uint8_t reg, uint8_t *data_out );
414
430err_t usbcsink3_spi_write ( usbcsink3_t *ctx, uint8_t reg, uint8_t data_in );
431
443err_t usbcsink3_read_raw_adc ( usbcsink3_t *ctx, uint16_t *raw_adc );
444
456err_t usbcsink3_read_voltage ( usbcsink3_t *ctx, float *voltage );
457
469err_t usbcsink3_set_vref ( usbcsink3_t *ctx, float vref );
470
483err_t usbcsink3_get_vbus ( usbcsink3_t *ctx, float *vbus );
484
498err_t usbcsink3_set_wiper ( usbcsink3_t *ctx, uint8_t wiper_pos );
499
513err_t usbcsink3_set_resistence ( usbcsink3_t *ctx, float resistence );
514
530
546
547#ifdef __cplusplus
548}
549#endif
550#endif // USBCSINK3_H
551
552 // usbcsink3
553
554// ------------------------------------------------------------------------ END
err_t usbcsink3_set_power(usbcsink3_t *ctx, usbcsink3_pwr_sel_t power)
USB-C Sink 3 set the power function.
err_t usbcsink3_set_vref(usbcsink3_t *ctx, float vref)
USB-C Sink 3 set vref function.
err_t usbcsink3_i2c_read(usbcsink3_t *ctx, uint8_t reg, uint8_t *data_out)
USB-C Sink 3 I2C reading function.
err_t usbcsink3_read_voltage(usbcsink3_t *ctx, float *voltage)
USB-C Sink 3 read voltage level function.
err_t usbcsink3_i2c_write(usbcsink3_t *ctx, uint8_t reg, uint8_t data_in)
USB-C Sink 3 I2C writing function.
err_t usbcsink3_set_resistence(usbcsink3_t *ctx, float resistence)
USB-C Sink 3 set the resistance function.
err_t usbcsink3_read_raw_adc(usbcsink3_t *ctx, uint16_t *raw_adc)
USB-C Sink 3 read raw ADC value function.
void usbcsink3_cfg_setup(usbcsink3_cfg_t *cfg)
USB-C Sink 3 configuration object setup function.
err_t usbcsink3_init(usbcsink3_t *ctx, usbcsink3_cfg_t *cfg)
USB-C Sink 3 initialization function.
err_t usbcsink3_set_wiper(usbcsink3_t *ctx, uint8_t wiper_pos)
USB-C Sink 3 set the wiper position function.
err_t usbcsink3_set_voltage(usbcsink3_t *ctx, usbcsink3_vtg_sel_t voltage)
USB-C Sink 3 set the voltage function.
err_t usbcsink3_default_cfg(usbcsink3_t *ctx)
USB-C Sink 3 default configuration function.
err_t usbcsink3_spi_write(usbcsink3_t *ctx, uint8_t reg, uint8_t data_in)
USB-C Sink 3 SPI writing function.
err_t usbcsink3_get_vbus(usbcsink3_t *ctx, float *vbus)
USB-C Sink 3 get VBUS function.
This file contains SPI specific macros, functions, etc.
USB-C Sink 3 Click configuration object.
Definition usbcsink3.h:267
analog_in_resolution_t resolution
Definition usbcsink3.h:276
float vref
Definition usbcsink3.h:277
uint32_t i2c_speed
Definition usbcsink3.h:279
spi_master_chip_select_polarity_t cs_polarity
Definition usbcsink3.h:284
pin_name_t sck
Definition usbcsink3.h:272
spi_master_mode_t spi_mode
Definition usbcsink3.h:283
pin_name_t mosi
Definition usbcsink3.h:271
uint32_t spi_speed
Definition usbcsink3.h:282
pin_name_t scl
Definition usbcsink3.h:268
pin_name_t miso
Definition usbcsink3.h:270
pin_name_t sda
Definition usbcsink3.h:269
pin_name_t an
Definition usbcsink3.h:274
pin_name_t cs
Definition usbcsink3.h:273
uint8_t i2c_address
Definition usbcsink3.h:280
USB-C Sink 3 Click context object.
Definition usbcsink3.h:250
spi_master_t spi
Definition usbcsink3.h:255
float vref
Definition usbcsink3.h:252
i2c_master_t i2c
Definition usbcsink3.h:254
uint8_t slave_address
Definition usbcsink3.h:257
pin_name_t chip_select
Definition usbcsink3.h:258
analog_in_t adc
Definition usbcsink3.h:251
usbcsink3_return_value_t
USB-C Sink 3 Click return value data.
Definition usbcsink3.h:293
@ USBCSINK3_ERROR
Definition usbcsink3.h:295
@ USBCSINK3_OK
Definition usbcsink3.h:294
struct usbcsink3_s usbcsink3_t
USB-C Sink 3 Click context object.
usbcsink3_vtg_sel_t
USB-C Sink 3 Click voltage selection value data.
Definition usbcsink3.h:304
@ USBCSINK3_VTG_SEL_9V
Definition usbcsink3.h:306
@ USBCSINK3_VTG_SEL_4_3V
Definition usbcsink3.h:309
@ USBCSINK3_VTG_SEL_15V
Definition usbcsink3.h:307
@ USBCSINK3_VTG_SEL_20V
Definition usbcsink3.h:308
@ USBCSINK3_VTG_SEL_17_2V
Definition usbcsink3.h:312
@ USBCSINK3_VTG_SEL_8_6V
Definition usbcsink3.h:310
@ USBCSINK3_VTG_SEL_5V
Definition usbcsink3.h:305
@ USBCSINK3_VTG_SEL_12_9V
Definition usbcsink3.h:311
usbcsink3_pwr_sel_t
USB-C Sink 3 Click power selection value data.
Definition usbcsink3.h:321
@ USBCSINK3_PWR_SEL_45W
Definition usbcsink3.h:328
@ USBCSINK3_PWR_SEL_100W
Definition usbcsink3.h:331
@ USBCSINK3_PWR_SEL_27W
Definition usbcsink3.h:326
@ USBCSINK3_PWR_SEL_90W
Definition usbcsink3.h:330
@ USBCSINK3_PWR_SEL_60W
Definition usbcsink3.h:329
@ USBCSINK3_PWR_SEL_36W
Definition usbcsink3.h:327
@ USBCSINK3_PWR_SEL_15W
Definition usbcsink3.h:323
@ USBCSINK3_PWR_SEL_20W
Definition usbcsink3.h:325
@ USBCSINK3_PWR_SEL_12W
Definition usbcsink3.h:322
@ USBCSINK3_PWR_SEL_18W
Definition usbcsink3.h:324