usbcsink  2.0.0.0
usbcsink.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 USBCSINK_H
29 #define USBCSINK_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 
73 #define USBCSINK_REG_BCD_TYPEC_REV_LOW 0x06
74 #define USBCSINK_REG_BCD_TYPEC_REV_HIGH 0x07
75 #define USBCSINK_REG_BCD_USBPD_REV_LOW 0x08
76 #define USBCSINK_REG_BCD_USBPD_REV_HIGH 0x09
77 #define USBCSINK_REG_DEVICE_CAPAB_HIGH 0x0A
78 #define USBCSINK_REG_ALERT_STAT_1 0x0B
79 #define USBCSINK_REG_ALERT_STAT_1_MASK 0x0C
80 #define USBCSINK_REG_PORT_STAT_0 0x0D
81 #define USBCSINK_REG_PORT_STAT_1 0x0E
82 #define USBCSINK_REG_TYPEC_MON_STAT_0 0x0F
83 #define USBCSINK_REG_TYPEC_MON_STAT_1 0x10
84 #define USBCSINK_REG_CC_STAT 0x11
85 #define USBCSINK_REG_CC_HW_FAULT_STAT_0 0x12
86 #define USBCSINK_REG_CC_HW_FAULT_STAT_1 0x13
87 #define USBCSINK_REG_PD_TYPEC_STAT 0x14
88 #define USBCSINK_REG_TYPEC_STAT 0x15
89 #define USBCSINK_REG_PRT_STAT 0x16
90 #define USBCSINK_REG_PD_CMD_CTL 0x1A
91 #define USBCSINK_REG_MON_CTL_0 0x20
92 #define USBCSINK_REG_MON_CTL_2 0x22
93 #define USBCSINK_REG_RESET_CTL 0x23
94 #define USBCSINK_REG_VBUS_DISCH_TIME_CTL 0x25
95 #define USBCSINK_REG_VBUS_DISCH_CTL 0x26
96 #define USBCSINK_REG_VBUS_CTL 0x27
97 #define USBCSINK_REG_GPIO3_SW_GPIO 0x2D
98 #define USBCSINK_REG_DEV_ID 0x2F
99 #define USBCSINK_REG_RX_HEADER_LOW 0x31
100 #define USBCSINK_REG_RX_HEADER_HIGH 0x32
101 #define USBCSINK_REG_RX_DATA_OBJ1_0 0x33
102 #define USBCSINK_REG_RX_DATA_OBJ1_1 0x34
103 #define USBCSINK_REG_RX_DATA_OBJ1_2 0x35
104 #define USBCSINK_REG_RX_DATA_OBJ1_3 0x36
105 #define USBCSINK_REG_RX_DATA_OBJ2_0 0x37
106 #define USBCSINK_REG_RX_DATA_OBJ2_1 0x38
107 #define USBCSINK_REG_RX_DATA_OBJ2_2 0x39
108 #define USBCSINK_REG_RX_DATA_OBJ2_3 0x3A
109 #define USBCSINK_REG_RX_DATA_OBJ3_0 0x3B
110 #define USBCSINK_REG_RX_DATA_OBJ3_1 0x3C
111 #define USBCSINK_REG_RX_DATA_OBJ3_2 0x3D
112 #define USBCSINK_REG_RX_DATA_OBJ3_3 0x3E
113 #define USBCSINK_REG_RX_DATA_OBJ4_0 0x3F
114 #define USBCSINK_REG_RX_DATA_OBJ4_1 0x40
115 #define USBCSINK_REG_RX_DATA_OBJ4_2 0x41
116 #define USBCSINK_REG_RX_DATA_OBJ4_3 0x42
117 #define USBCSINK_REG_RX_DATA_OBJ5_0 0x43
118 #define USBCSINK_REG_RX_DATA_OBJ5_1 0x44
119 #define USBCSINK_REG_RX_DATA_OBJ5_2 0x45
120 #define USBCSINK_REG_RX_DATA_OBJ5_3 0x46
121 #define USBCSINK_REG_RX_DATA_OBJ6_0 0x47
122 #define USBCSINK_REG_RX_DATA_OBJ6_1 0x48
123 #define USBCSINK_REG_RX_DATA_OBJ6_2 0x49
124 #define USBCSINK_REG_RX_DATA_OBJ6_3 0x4A
125 #define USBCSINK_REG_RX_DATA_OBJ7_0 0x4B
126 #define USBCSINK_REG_RX_DATA_OBJ7_1 0x4C
127 #define USBCSINK_REG_RX_DATA_OBJ7_2 0x4D
128 #define USBCSINK_REG_RX_DATA_OBJ7_3 0x4E
129 #define USBCSINK_REG_TX_HEADER_LOW 0x51
130 #define USBCSINK_REG_TX_HEADER_HIGH 0x52
131 #define USBCSINK_REG_DPM_PDO_NUMB 0x70
132 #define USBCSINK_REG_DPM_SNK_PDO1_0 0x85
133 #define USBCSINK_REG_DPM_SNK_PDO1_1 0x86
134 #define USBCSINK_REG_DPM_SNK_PDO1_2 0x87
135 #define USBCSINK_REG_DPM_SNK_PDO1_3 0x88
136 #define USBCSINK_REG_DPM_SNK_PDO2_0 0x89
137 #define USBCSINK_REG_DPM_SNK_PDO2_1 0x8A
138 #define USBCSINK_REG_DPM_SNK_PDO2_2 0x8B
139 #define USBCSINK_REG_DPM_SNK_PDO2_3 0x8C
140 #define USBCSINK_REG_DPM_SNK_PDO3_0 0x8D
141 #define USBCSINK_REG_DPM_SNK_PDO3_1 0x8E
142 #define USBCSINK_REG_DPM_SNK_PDO3_2 0x8F
143 #define USBCSINK_REG_DPM_SNK_PDO3_3 0x90
144 #define USBCSINK_REG_DPM_REQ_RDO3_0 0x91
145 #define USBCSINK_REG_DPM_REQ_RDO3_1 0x92
146 #define USBCSINK_REG_DPM_REQ_RDO3_2 0x93
147 #define USBCSINK_REG_DPM_REQ_RDO3_3 0x94
148 #define USBCSINK_REG_DEF 0xFF
149 #define USBCSINK_REG_FTP_CUST_PWD_REG 0x95
150 #define USBCSINK_REG_FTP_CTL_0 0x96
151 #define USBCSINK_REG_FTP_CTL_1 0x97
152 #define USBCSINK_REG_RW_BUF 0x53
153 
154 #define USBCSINK_FTP_CUST_PWR 0x80
155 #define USBCSINK_FTP_CUST_RST_N 0x40
156 #define USBCSINK_FTP_CUST_REQ 0x10
157 #define USBCSINK_FTP_CUST_SECT 0x07
158 
159 #define USBCSINK_FTP_CUST_SER 0xF8
160 #define USBCSINK_FTP_CUST_OPCODE 0x07
161 
162 #define USBCSINK_SEC_0 0x01
163 #define USBCSINK_SEC_1 0x02
164 #define USBCSINK_SEC_2 0x04
165 #define USBCSINK_SEC_3 0x08
166 #define USBCSINK_SEC_4 0x10
167  // usbcsink_reg
169 
189 #define USBCSINK_FTP_CUST_PWD 0x47
190 
195 #define USBCSINK_SW_RESET_ON 0x01
196 #define USBCSINK_SW_RESET_OFF 0x00
197 
202 #define USBCSINK_READ 0x00
203 #define USBCSINK_WRITE_PL 0x01
204 #define USBCSINK_WRITE_SER 0x02
205 #define USBCSINK_ERASE_SEC 0x05
206 #define USBCSINK_PROG_SEC 0x06
207 #define USBCSINK_SOFT_PROG_SEC 0x07
208 
213 #define USBCSINK_SET_PDO_1 0x01
214 #define USBCSINK_SET_PDO_2 0x02
215 #define USBCSINK_SET_PDO_3 0x03
216 
217 #define USBCSINK_UPLOAD_NEW_DATA_VAL 0x00
218 #define USBCSINK_UPLOAD_DEFAULT 0x01
219 
225 #define USBCSINK_SLAVE_ADR_0 0x28
226 #define USBCSINK_SLAVE_ADR_1 0x29
227 #define USBCSINK_SLAVE_ADR_2 0x2A
228 #define USBCSINK_SLAVE_ADR_3 0x2B
229  // usbcsink_set
231 
246 #define USBCSINK_MAP_MIKROBUS( cfg, mikrobus ) \
247  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
248  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
249  cfg.po3 = MIKROBUS( mikrobus, MIKROBUS_CS ); \
250  cfg.po2 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
251  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
252  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
253  // usbcsink_map // usbcsink
256 
261 typedef struct
262 {
263  // Output pins
264  digital_out_t rst;
266  // Input pins
267  digital_in_t po3;
268  digital_in_t po2;
269  digital_in_t int_pin;
271  // Modules
272  i2c_master_t i2c;
274  // I2C slave address
275  uint8_t slave_address;
277 } usbcsink_t;
278 
283 typedef struct
284 {
285  pin_name_t scl;
286  pin_name_t sda;
288  pin_name_t po3;
289  pin_name_t po2;
290  pin_name_t rst;
291  pin_name_t int_pin;
293  uint32_t i2c_speed;
294  uint8_t i2c_address;
297 
302 typedef enum
303 {
305  USBCSINK_ERROR = -1
306 
308 
325 
341 
357 err_t usbcsink_generic_write ( usbcsink_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len );
358 
374 err_t usbcsink_generic_read ( usbcsink_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len );
375 
386 
396 uint8_t usbcsink_get_pdo2 ( usbcsink_t *ctx );
397 
407 uint8_t usbcsink_get_pdo3 ( usbcsink_t *ctx );
408 
419 
420 
433 void usbcsink_write_byte ( usbcsink_t *ctx, uint8_t reg, uint8_t tx_data );
434 
445 uint8_t usbcsink_read_byte ( usbcsink_t *ctx, uint8_t reg );
446 
456 uint8_t usbcsink_dev_chk ( usbcsink_t *ctx );
457 
473 err_t usbcsink_cust_wr_sec ( usbcsink_t *ctx, uint8_t sec_num, uint8_t *sec_data );
474 
488 err_t usbcsink_cust_ent_wr_mode ( usbcsink_t *ctx, uint8_t er_sec );
489 
503 
514 
526 void usbcsink_upload_new_data ( usbcsink_t *ctx, uint8_t def_vals );
527 
538 void usbcsink_set_voltage ( uint8_t pdo_numb, float volt );
539 
549 float usbcsink_get_voltage ( uint8_t pdo_numb );
550 
561 void usbcsink_set_current ( uint8_t pdo_numb, float curr );
562 
576 float usbcsink_get_current ( uint8_t pdo_numb );
577 
591 void usbcsink_set_lo_volt_limit ( uint8_t pdo_numb, uint8_t value );
592 
602 uint8_t usbcsink_get_lo_volt_limit ( uint8_t pdo_numb );
603 
616 void usbcsink_set_up_volt_limit ( uint8_t pdo_numb, uint8_t value );
617 
627 uint8_t usbcsink_get_up_volt_limit ( uint8_t pdo_numb );
628 
637 void usbcsink_set_flx_curr ( float value );
638 
646 float usbcsink_get_flx_curr ( void );
647 
656 void usbcsink_set_pdo_num ( uint8_t pdo_numb );
657 
665 uint8_t usbcsink_get_pdo_num ( void );
666 
677 void usbcsink_set_ext_pow ( uint8_t value );
678 
688 uint8_t usbcsink_get_ext_pow ( void );
689 
699 void usbcsink_set_usb_com_capa ( uint8_t value );
700 
709 uint8_t usbcsink_get_usb_com_cap ( void );
710 
720 void usbcsink_set_cfg_ok_gpio ( uint8_t value );
721 
730 uint8_t usbcsink_get_cfg_ok_gpio ( void );
731 
741 void usbcsink_set_gpio_ctl ( uint8_t value );
742 
751 uint8_t usbcsink_get_gpio_ctl ( void );
752 
762 void usbcsink_set_pow_above_5v_only ( uint8_t value );
763 
773 
785 void usbcsink_set_req_src_current ( uint8_t value );
786 
797 uint8_t usbcsink_get_req_src_curr ( void );
798 
799 #ifdef __cplusplus
800 }
801 #endif
802 #endif // USBCSINK_H
803  // usbcsink
805 
806 // ------------------------------------------------------------------------ END
usbcsink_write_byte
void usbcsink_write_byte(usbcsink_t *ctx, uint8_t reg, uint8_t tx_data)
Write byte function.
usbcsink_cfg_t::po3
pin_name_t po3
Definition: usbcsink.h:288
usbcsink_cfg_setup
void usbcsink_cfg_setup(usbcsink_cfg_t *cfg)
USB-C Sink configuration object setup function.
usbcsink_get_pdo3
uint8_t usbcsink_get_pdo3(usbcsink_t *ctx)
Get PO3 pin state function.
usbcsink_cfg_t::po2
pin_name_t po2
Definition: usbcsink.h:289
usbcsink_set_current
void usbcsink_set_current(uint8_t pdo_numb, float curr)
Set the current function.
usbcsink_set_usb_com_capa
void usbcsink_set_usb_com_capa(uint8_t value)
Set the USB capable function.
usbcsink_get_cfg_ok_gpio
uint8_t usbcsink_get_cfg_ok_gpio(void)
Get the config OK GPIO function.
usbcsink_cust_wr_sec
err_t usbcsink_cust_wr_sec(usbcsink_t *ctx, uint8_t sec_num, uint8_t *sec_data)
Write sector function.
usbcsink_generic_read
err_t usbcsink_generic_read(usbcsink_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len)
USB-C Sink I2C reading function.
usbcsink_cfg_t::int_pin
pin_name_t int_pin
Definition: usbcsink.h:291
usbcsink_set_ext_pow
void usbcsink_set_ext_pow(uint8_t value)
Set the external source of power function.
usbcsink_load_data
void usbcsink_load_data(usbcsink_t *ctx)
Load data function.
usbcsink_t::slave_address
uint8_t slave_address
Definition: usbcsink.h:275
usbcsink_init
err_t usbcsink_init(usbcsink_t *ctx, usbcsink_cfg_t *cfg)
USB-C Sink initialization function.
usbcsink_set_voltage
void usbcsink_set_voltage(uint8_t pdo_numb, float volt)
Set the voltage function.
usbcsink_cfg_t::rst
pin_name_t rst
Definition: usbcsink.h:290
usbcsink_t::rst
digital_out_t rst
Definition: usbcsink.h:264
usbcsink_read_byte
uint8_t usbcsink_read_byte(usbcsink_t *ctx, uint8_t reg)
Read byte function.
usbcsink_set_flx_curr
void usbcsink_set_flx_curr(float value)
Set the flexible current function.
usbcsink_get_up_volt_limit
uint8_t usbcsink_get_up_volt_limit(uint8_t pdo_numb)
Get the over voltage limit function.
usbcsink_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: usbcsink.h:293
usbcsink_set_pow_above_5v_only
void usbcsink_set_pow_above_5v_only(uint8_t value)
Set the power only above 5V function.
usbcsink_get_interrupt
uint8_t usbcsink_get_interrupt(usbcsink_t *ctx)
Get INT pin state function.
usbcsink_get_flx_curr
float usbcsink_get_flx_curr(void)
Get the flexible current function.
usbcsink_set_lo_volt_limit
void usbcsink_set_lo_volt_limit(uint8_t pdo_numb, uint8_t value)
Set the lower voltage limit function.
usbcsink_generic_write
err_t usbcsink_generic_write(usbcsink_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len)
USB-C Sink I2C writing function.
usbcsink_cfg_t
USB-C Sink Click configuration object.
Definition: usbcsink.h:284
usbcsink_t::po2
digital_in_t po2
Definition: usbcsink.h:268
usbcsink_get_ext_pow
uint8_t usbcsink_get_ext_pow(void)
Get the external source of power function.
usbcsink_t
USB-C Sink Click context object.
Definition: usbcsink.h:262
usbcsink_cfg_t::i2c_address
uint8_t i2c_address
Definition: usbcsink.h:294
usbcsink_get_req_src_curr
uint8_t usbcsink_get_req_src_curr(void)
Get the request operating current function.
usbcsink_upload_new_data
void usbcsink_upload_new_data(usbcsink_t *ctx, uint8_t def_vals)
Upload data function.
usbcsink_hw_reset
void usbcsink_hw_reset(usbcsink_t *ctx)
HW reset function.
usbcsink_get_pow_above_5v_only
uint8_t usbcsink_get_pow_above_5v_only(void)
Get the power only above 5V function.
usbcsink_get_lo_volt_limit
uint8_t usbcsink_get_lo_volt_limit(uint8_t pdo_numb)
Get the lower voltage limit function.
usbcsink_dev_chk
uint8_t usbcsink_dev_chk(usbcsink_t *ctx)
Check device communication function.
usbcsink_cfg_t::scl
pin_name_t scl
Definition: usbcsink.h:285
usbcsink_get_usb_com_cap
uint8_t usbcsink_get_usb_com_cap(void)
Get the USB capable function.
USBCSINK_ERROR
@ USBCSINK_ERROR
Definition: usbcsink.h:305
usbcsink_get_pdo2
uint8_t usbcsink_get_pdo2(usbcsink_t *ctx)
Get PO2 pin state function.
USBCSINK_OK
@ USBCSINK_OK
Definition: usbcsink.h:304
usbcsink_cust_ent_wr_mode
err_t usbcsink_cust_ent_wr_mode(usbcsink_t *ctx, uint8_t er_sec)
Enter the write mode function.
usbcsink_get_current
float usbcsink_get_current(uint8_t pdo_numb)
Get the current function.
usbcsink_get_gpio_ctl
uint8_t usbcsink_get_gpio_ctl(void)
Get the config CTRL GPIO function.
usbcsink_set_gpio_ctl
void usbcsink_set_gpio_ctl(uint8_t value)
Set the config CTRL GPIO function.
usbcsink_cfg_t::sda
pin_name_t sda
Definition: usbcsink.h:286
usbcsink_get_pdo_num
uint8_t usbcsink_get_pdo_num(void)
Get the number of sink function.
usbcsink_t::int_pin
digital_in_t int_pin
Definition: usbcsink.h:269
usbcsink_set_req_src_current
void usbcsink_set_req_src_current(uint8_t value)
Set the request operating current function.
usbcsink_t::po3
digital_in_t po3
Definition: usbcsink.h:267
usbcsink_set_pdo_num
void usbcsink_set_pdo_num(uint8_t pdo_numb)
Set the number of sink function.
usbcsink_t::i2c
i2c_master_t i2c
Definition: usbcsink.h:272
usbcsink_set_cfg_ok_gpio
void usbcsink_set_cfg_ok_gpio(uint8_t value)
Set the config OK GPIO function.
usbcsink_get_voltage
float usbcsink_get_voltage(uint8_t pdo_numb)
Get the voltage function.
usbcsink_return_value_t
usbcsink_return_value_t
USB-C Sink Click return value data.
Definition: usbcsink.h:303
usbcsink_cust_ext_test_mode
err_t usbcsink_cust_ext_test_mode(usbcsink_t *ctx)
Exit write mode function.
usbcsink_set_up_volt_limit
void usbcsink_set_up_volt_limit(uint8_t pdo_numb, uint8_t value)
Set the upper voltage limit function.