usbcsink2 2.1.0.0
usbcsink2.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 USBCSINK2_H
29#define USBCSINK2_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_analog_in.h"
52#include "drv_i2c_master.h"
53
74#define USBCSINK2_CMD_SRCPDO 0x00
75#define USBCSINK2_CMD_PDONUM 0x1C
76#define USBCSINK2_CMD_STATUS 0x1D
77#define USBCSINK2_CMD_MASK 0x1E
78#define USBCSINK2_CMD_VOLTAGE 0x20
79#define USBCSINK2_CMD_CURRENT 0x21
80#define USBCSINK2_CMD_TEMP 0x22
81#define USBCSINK2_CMD_OCPTHR 0x23
82#define USBCSINK2_CMD_OTPTHR 0x24
83#define USBCSINK2_CMD_DRTHR 0x25
84#define USBCSINK2_CMD_TR25 0x28
85#define USBCSINK2_CMD_TR50 0x2A
86#define USBCSINK2_CMD_TR75 0x2C
87#define USBCSINK2_CMD_TR100 0x2E
88#define USBCSINK2_CMD_RDO 0x30
89
90 // usbcsink2_reg
91
106#define USBCSINK2_SRCPDO_LENGTH 28
107#define USBCSINK2_RDO_LENGTH 4
108
113#define USBCSINK2_RDO_REQ_SUCCESS 0x03
114
119#define USBCSINK2_SENS_VTG_MV 80.0f
120#define USBCSINK2_SENS_CURRENT_MA 24.0f
121
126#define USBCSINK2_ADC_RESOLUTION 0x0FFF
127#define USBCSINK2_VREF_3V3 3.3
128#define USBCSINK2_VREF_5V 5.0
129
135#define USBCSINK2_SET_DEV_ADDR 0x51
136
137 // usbcsink2_set
138
153#define USBCSINK2_MAP_MIKROBUS( cfg, mikrobus ) \
154 cfg.an = MIKROBUS( mikrobus, MIKROBUS_AN ); \
155 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
156 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
157 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
158
159 // usbcsink2_map
160 // usbcsink2
161
166typedef struct
167{
168 digital_in_t int_pin;
170 analog_in_t adc;
171 i2c_master_t i2c;
174 float vref;
177 uint8_t pdo_data[ 28 ];
180
185typedef struct
186{
187 pin_name_t an;
188 pin_name_t scl;
189 pin_name_t sda;
191 pin_name_t int_pin;
193 analog_in_resolution_t resolution;
194 float vref;
196 uint32_t i2c_speed;
197 uint8_t i2c_address;
200
211
228
243
257
273err_t usbcsink2_generic_write ( usbcsink2_t *ctx, uint8_t cmd, uint8_t *data_in, uint8_t len );
274
290err_t usbcsink2_generic_read ( usbcsink2_t *ctx, uint8_t cmd, uint8_t *data_out, uint8_t len );
291
303err_t usbcsink2_read_raw_adc ( usbcsink2_t *ctx, uint16_t *raw_adc );
304
316err_t usbcsink2_read_voltage ( usbcsink2_t *ctx, float *voltage );
317
329err_t usbcsink2_set_vref ( usbcsink2_t *ctx, float vref );
330
345err_t usbcsink2_read_pdo ( usbcsink2_t *ctx, uint8_t *pdo );
346
360err_t usbcsink2_get_pdo_num ( usbcsink2_t *ctx, uint8_t *pdo_num );
361
375err_t usbcsink2_read_status ( usbcsink2_t *ctx, uint8_t *status );
376
391
405err_t usbcsink2_get_pdo_voltage ( usbcsink2_t *ctx, float *voltage_mv );
406
420err_t usbcsink2_get_pdo_current ( usbcsink2_t *ctx, float *current_ma );
421
435err_t usbcsink2_get_temperature ( usbcsink2_t *ctx, uint8_t *temperature );
436
451err_t usbcsink2_write_rdo ( usbcsink2_t *ctx, uint8_t *rdo );
452
467
481
482#ifdef __cplusplus
483}
484#endif
485#endif // USBCSINK2_H
486
487 // usbcsink2
488
489// ------------------------------------------------------------------------ END
err_t usbcsink2_read_status(usbcsink2_t *ctx, uint8_t *status)
USB-C Sink 2 status reading function.
err_t usbcsink2_get_temperature(usbcsink2_t *ctx, uint8_t *temperature)
USB-C Sink 2 get the temperature function.
void usbcsink2_cfg_setup(usbcsink2_cfg_t *cfg)
USB-C Sink 2 configuration object setup function.
err_t usbcsink2_read_raw_adc(usbcsink2_t *ctx, uint16_t *raw_adc)
USB-C Sink 2 read raw ADC value function.
err_t usbcsink2_set_vref(usbcsink2_t *ctx, float vref)
USB-C Sink 2 set vref function.
err_t usbcsink2_get_pdo_current(usbcsink2_t *ctx, float *current_ma)
USB-C Sink 2 get the current function.
err_t usbcsink2_write_rdo(usbcsink2_t *ctx, uint8_t *rdo)
USB-C Sink 2 write the RDO function.
err_t usbcsink2_read_voltage(usbcsink2_t *ctx, float *voltage)
USB-C Sink 2 read voltage level function.
err_t usbcsink2_read_pdo(usbcsink2_t *ctx, uint8_t *pdo)
USB-C Sink 2 PDO data reading function.
err_t usbcsink2_get_pdo_num(usbcsink2_t *ctx, uint8_t *pdo_num)
USB-C Sink 2 PDO number reading function.
err_t usbcsink2_default_cfg(usbcsink2_t *ctx)
USB-C Sink 2 default configuration function.
err_t usbcsink2_reset(usbcsink2_t *ctx)
USB-C Sink 2 hard reset the RDO function.
err_t usbcsink2_init(usbcsink2_t *ctx, usbcsink2_cfg_t *cfg)
USB-C Sink 2 initialization function.
err_t usbcsink2_wait_rdo_req_success(usbcsink2_t *ctx)
USB-C Sink 2 wait RDO request function.
err_t usbcsink2_generic_write(usbcsink2_t *ctx, uint8_t cmd, uint8_t *data_in, uint8_t len)
USB-C Sink 2 data writing function.
err_t usbcsink2_get_pdo_voltage(usbcsink2_t *ctx, float *voltage_mv)
USB-C Sink 2 get the voltage function.
err_t usbcsink2_generic_read(usbcsink2_t *ctx, uint8_t cmd, uint8_t *data_out, uint8_t len)
USB-C Sink 2 data reading function.
uint8_t usbcsink2_get_interrupt(usbcsink2_t *ctx)
USB-C Sink 2 get interrupt function.
USB-C Sink 2 Click configuration object.
Definition usbcsink2.h:186
analog_in_resolution_t resolution
Definition usbcsink2.h:193
float vref
Definition usbcsink2.h:194
uint32_t i2c_speed
Definition usbcsink2.h:196
pin_name_t scl
Definition usbcsink2.h:188
pin_name_t int_pin
Definition usbcsink2.h:191
pin_name_t sda
Definition usbcsink2.h:189
pin_name_t an
Definition usbcsink2.h:187
uint8_t i2c_address
Definition usbcsink2.h:197
USB-C Sink 2 Click context object.
Definition usbcsink2.h:167
float vref
Definition usbcsink2.h:174
digital_in_t int_pin
Definition usbcsink2.h:168
i2c_master_t i2c
Definition usbcsink2.h:171
uint8_t slave_address
Definition usbcsink2.h:173
uint8_t number_of_valid_pdo
Definition usbcsink2.h:176
analog_in_t adc
Definition usbcsink2.h:170
usbcsink2_return_value_t
USB-C Sink 2 Click return value data.
Definition usbcsink2.h:206
@ USBCSINK2_ERROR
Definition usbcsink2.h:208
@ USBCSINK2_OK
Definition usbcsink2.h:207