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 
35 #include "drv_digital_out.h"
36 #include "drv_digital_in.h"
37 #include "drv_i2c_master.h"
38 
59 #define USBCSINK_REG_BCD_TYPEC_REV_LOW 0x06
60 #define USBCSINK_REG_BCD_TYPEC_REV_HIGH 0x07
61 #define USBCSINK_REG_BCD_USBPD_REV_LOW 0x08
62 #define USBCSINK_REG_BCD_USBPD_REV_HIGH 0x09
63 #define USBCSINK_REG_DEVICE_CAPAB_HIGH 0x0A
64 #define USBCSINK_REG_ALERT_STAT_1 0x0B
65 #define USBCSINK_REG_ALERT_STAT_1_MASK 0x0C
66 #define USBCSINK_REG_PORT_STAT_0 0x0D
67 #define USBCSINK_REG_PORT_STAT_1 0x0E
68 #define USBCSINK_REG_TYPEC_MON_STAT_0 0x0F
69 #define USBCSINK_REG_TYPEC_MON_STAT_1 0x10
70 #define USBCSINK_REG_CC_STAT 0x11
71 #define USBCSINK_REG_CC_HW_FAULT_STAT_0 0x12
72 #define USBCSINK_REG_CC_HW_FAULT_STAT_1 0x13
73 #define USBCSINK_REG_PD_TYPEC_STAT 0x14
74 #define USBCSINK_REG_TYPEC_STAT 0x15
75 #define USBCSINK_REG_PRT_STAT 0x16
76 #define USBCSINK_REG_PD_CMD_CTL 0x1A
77 #define USBCSINK_REG_MON_CTL_0 0x20
78 #define USBCSINK_REG_MON_CTL_2 0x22
79 #define USBCSINK_REG_RESET_CTL 0x23
80 #define USBCSINK_REG_VBUS_DISCH_TIME_CTL 0x25
81 #define USBCSINK_REG_VBUS_DISCH_CTL 0x26
82 #define USBCSINK_REG_VBUS_CTL 0x27
83 #define USBCSINK_REG_GPIO3_SW_GPIO 0x2D
84 #define USBCSINK_REG_DEV_ID 0x2F
85 #define USBCSINK_REG_RX_HEADER_LOW 0x31
86 #define USBCSINK_REG_RX_HEADER_HIGH 0x32
87 #define USBCSINK_REG_RX_DATA_OBJ1_0 0x33
88 #define USBCSINK_REG_RX_DATA_OBJ1_1 0x34
89 #define USBCSINK_REG_RX_DATA_OBJ1_2 0x35
90 #define USBCSINK_REG_RX_DATA_OBJ1_3 0x36
91 #define USBCSINK_REG_RX_DATA_OBJ2_0 0x37
92 #define USBCSINK_REG_RX_DATA_OBJ2_1 0x38
93 #define USBCSINK_REG_RX_DATA_OBJ2_2 0x39
94 #define USBCSINK_REG_RX_DATA_OBJ2_3 0x3A
95 #define USBCSINK_REG_RX_DATA_OBJ3_0 0x3B
96 #define USBCSINK_REG_RX_DATA_OBJ3_1 0x3C
97 #define USBCSINK_REG_RX_DATA_OBJ3_2 0x3D
98 #define USBCSINK_REG_RX_DATA_OBJ3_3 0x3E
99 #define USBCSINK_REG_RX_DATA_OBJ4_0 0x3F
100 #define USBCSINK_REG_RX_DATA_OBJ4_1 0x40
101 #define USBCSINK_REG_RX_DATA_OBJ4_2 0x41
102 #define USBCSINK_REG_RX_DATA_OBJ4_3 0x42
103 #define USBCSINK_REG_RX_DATA_OBJ5_0 0x43
104 #define USBCSINK_REG_RX_DATA_OBJ5_1 0x44
105 #define USBCSINK_REG_RX_DATA_OBJ5_2 0x45
106 #define USBCSINK_REG_RX_DATA_OBJ5_3 0x46
107 #define USBCSINK_REG_RX_DATA_OBJ6_0 0x47
108 #define USBCSINK_REG_RX_DATA_OBJ6_1 0x48
109 #define USBCSINK_REG_RX_DATA_OBJ6_2 0x49
110 #define USBCSINK_REG_RX_DATA_OBJ6_3 0x4A
111 #define USBCSINK_REG_RX_DATA_OBJ7_0 0x4B
112 #define USBCSINK_REG_RX_DATA_OBJ7_1 0x4C
113 #define USBCSINK_REG_RX_DATA_OBJ7_2 0x4D
114 #define USBCSINK_REG_RX_DATA_OBJ7_3 0x4E
115 #define USBCSINK_REG_TX_HEADER_LOW 0x51
116 #define USBCSINK_REG_TX_HEADER_HIGH 0x52
117 #define USBCSINK_REG_DPM_PDO_NUMB 0x70
118 #define USBCSINK_REG_DPM_SNK_PDO1_0 0x85
119 #define USBCSINK_REG_DPM_SNK_PDO1_1 0x86
120 #define USBCSINK_REG_DPM_SNK_PDO1_2 0x87
121 #define USBCSINK_REG_DPM_SNK_PDO1_3 0x88
122 #define USBCSINK_REG_DPM_SNK_PDO2_0 0x89
123 #define USBCSINK_REG_DPM_SNK_PDO2_1 0x8A
124 #define USBCSINK_REG_DPM_SNK_PDO2_2 0x8B
125 #define USBCSINK_REG_DPM_SNK_PDO2_3 0x8C
126 #define USBCSINK_REG_DPM_SNK_PDO3_0 0x8D
127 #define USBCSINK_REG_DPM_SNK_PDO3_1 0x8E
128 #define USBCSINK_REG_DPM_SNK_PDO3_2 0x8F
129 #define USBCSINK_REG_DPM_SNK_PDO3_3 0x90
130 #define USBCSINK_REG_DPM_REQ_RDO3_0 0x91
131 #define USBCSINK_REG_DPM_REQ_RDO3_1 0x92
132 #define USBCSINK_REG_DPM_REQ_RDO3_2 0x93
133 #define USBCSINK_REG_DPM_REQ_RDO3_3 0x94
134 #define USBCSINK_REG_DEF 0xFF
135 #define USBCSINK_REG_FTP_CUST_PWD_REG 0x95
136 #define USBCSINK_REG_FTP_CTL_0 0x96
137 #define USBCSINK_REG_FTP_CTL_1 0x97
138 #define USBCSINK_REG_RW_BUF 0x53
139 
140 #define USBCSINK_FTP_CUST_PWR 0x80
141 #define USBCSINK_FTP_CUST_RST_N 0x40
142 #define USBCSINK_FTP_CUST_REQ 0x10
143 #define USBCSINK_FTP_CUST_SECT 0x07
144 
145 #define USBCSINK_FTP_CUST_SER 0xF8
146 #define USBCSINK_FTP_CUST_OPCODE 0x07
147 
148 #define USBCSINK_SEC_0 0x01
149 #define USBCSINK_SEC_1 0x02
150 #define USBCSINK_SEC_2 0x04
151 #define USBCSINK_SEC_3 0x08
152 #define USBCSINK_SEC_4 0x10
153  // usbcsink_reg
155 
175 #define USBCSINK_FTP_CUST_PWD 0x47
176 
181 #define USBCSINK_SW_RESET_ON 0x01
182 #define USBCSINK_SW_RESET_OFF 0x00
183 
188 #define USBCSINK_READ 0x00
189 #define USBCSINK_WRITE_PL 0x01
190 #define USBCSINK_WRITE_SER 0x02
191 #define USBCSINK_ERASE_SEC 0x05
192 #define USBCSINK_PROG_SEC 0x06
193 #define USBCSINK_SOFT_PROG_SEC 0x07
194 
199 #define USBCSINK_SET_PDO_1 0x01
200 #define USBCSINK_SET_PDO_2 0x02
201 #define USBCSINK_SET_PDO_3 0x03
202 
203 #define USBCSINK_UPLOAD_NEW_DATA_VAL 0x00
204 #define USBCSINK_UPLOAD_DEFAULT 0x01
205 
211 #define USBCSINK_SLAVE_ADR_0 0x28
212 #define USBCSINK_SLAVE_ADR_1 0x29
213 #define USBCSINK_SLAVE_ADR_2 0x2A
214 #define USBCSINK_SLAVE_ADR_3 0x2B
215  // usbcsink_set
217 
232 #define USBCSINK_MAP_MIKROBUS( cfg, mikrobus ) \
233  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
234  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
235  cfg.po3 = MIKROBUS( mikrobus, MIKROBUS_CS ); \
236  cfg.po2 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
237  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
238  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
239  // usbcsink_map // usbcsink
242 
247 typedef struct
248 {
249  // Output pins
250  digital_out_t rst;
252  // Input pins
253  digital_in_t po3;
254  digital_in_t po2;
255  digital_in_t int_pin;
257  // Modules
258  i2c_master_t i2c;
260  // I2C slave address
261  uint8_t slave_address;
263 } usbcsink_t;
264 
269 typedef struct
270 {
271  pin_name_t scl;
272  pin_name_t sda;
274  pin_name_t po3;
275  pin_name_t po2;
276  pin_name_t rst;
277  pin_name_t int_pin;
279  uint32_t i2c_speed;
280  uint8_t i2c_address;
283 
288 typedef enum
289 {
291  USBCSINK_ERROR = -1
292 
294 
311 
327 
343 err_t usbcsink_generic_write ( usbcsink_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len );
344 
360 err_t usbcsink_generic_read ( usbcsink_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len );
361 
372 
382 uint8_t usbcsink_get_pdo2 ( usbcsink_t *ctx );
383 
393 uint8_t usbcsink_get_pdo3 ( usbcsink_t *ctx );
394 
405 
406 
419 void usbcsink_write_byte ( usbcsink_t *ctx, uint8_t reg, uint8_t tx_data );
420 
431 uint8_t usbcsink_read_byte ( usbcsink_t *ctx, uint8_t reg );
432 
442 uint8_t usbcsink_dev_chk ( usbcsink_t *ctx );
443 
459 err_t usbcsink_cust_wr_sec ( usbcsink_t *ctx, uint8_t sec_num, uint8_t *sec_data );
460 
474 err_t usbcsink_cust_ent_wr_mode ( usbcsink_t *ctx, uint8_t er_sec );
475 
489 
500 
512 void usbcsink_upload_new_data ( usbcsink_t *ctx, uint8_t def_vals );
513 
524 void usbcsink_set_voltage ( uint8_t pdo_numb, float volt );
525 
535 float usbcsink_get_voltage ( uint8_t pdo_numb );
536 
547 void usbcsink_set_current ( uint8_t pdo_numb, float curr );
548 
562 float usbcsink_get_current ( uint8_t pdo_numb );
563 
577 void usbcsink_set_lo_volt_limit ( uint8_t pdo_numb, uint8_t value );
578 
588 uint8_t usbcsink_get_lo_volt_limit ( uint8_t pdo_numb );
589 
602 void usbcsink_set_up_volt_limit ( uint8_t pdo_numb, uint8_t value );
603 
613 uint8_t usbcsink_get_up_volt_limit ( uint8_t pdo_numb );
614 
623 void usbcsink_set_flx_curr ( float value );
624 
632 float usbcsink_get_flx_curr ( void );
633 
642 void usbcsink_set_pdo_num ( uint8_t pdo_numb );
643 
651 uint8_t usbcsink_get_pdo_num ( void );
652 
663 void usbcsink_set_ext_pow ( uint8_t value );
664 
674 uint8_t usbcsink_get_ext_pow ( void );
675 
685 void usbcsink_set_usb_com_capa ( uint8_t value );
686 
695 uint8_t usbcsink_get_usb_com_cap ( void );
696 
706 void usbcsink_set_cfg_ok_gpio ( uint8_t value );
707 
716 uint8_t usbcsink_get_cfg_ok_gpio ( void );
717 
727 void usbcsink_set_gpio_ctl ( uint8_t value );
728 
737 uint8_t usbcsink_get_gpio_ctl ( void );
738 
748 void usbcsink_set_pow_above_5v_only ( uint8_t value );
749 
759 
771 void usbcsink_set_req_src_current ( uint8_t value );
772 
783 uint8_t usbcsink_get_req_src_curr ( void );
784 
785 #ifdef __cplusplus
786 }
787 #endif
788 #endif // USBCSINK_H
789  // usbcsink
791 
792 // ------------------------------------------------------------------------ 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:274
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:275
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:277
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:261
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:276
usbcsink_t::rst
digital_out_t rst
Definition: usbcsink.h:250
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:279
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:270
usbcsink_t::po2
digital_in_t po2
Definition: usbcsink.h:254
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:248
usbcsink_cfg_t::i2c_address
uint8_t i2c_address
Definition: usbcsink.h:280
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:271
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:291
usbcsink_get_pdo2
uint8_t usbcsink_get_pdo2(usbcsink_t *ctx)
Get PO2 pin state function.
USBCSINK_OK
@ USBCSINK_OK
Definition: usbcsink.h:290
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:272
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:255
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:253
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:258
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:289
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.