1 /*
2  * Copyright 2018-2021, NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef __SRTM_I2C_SERVICE_H__
10 #define __SRTM_I2C_SERVICE_H__
11 
12 #include "srtm_service.h"
13 
14 /*!
15  * @addtogroup srtm_service
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 /** @brief Switch to disable I2C service debugging messages. */
23 #ifndef SRTM_I2C_SERVICE_DEBUG_OFF
24 #define SRTM_I2C_SERVICE_DEBUG_OFF (0)
25 #endif
26 
27 #if SRTM_I2C_SERVICE_DEBUG_OFF
28 #undef SRTM_DEBUG_VERBOSE_LEVEL
29 #define SRTM_DEBUG_VERBOSE_LEVEL SRTM_DEBUG_VERBOSE_NONE
30 #endif
31 
32 /* Protocol definition */
33 #define SRTM_I2C_FLAG_NEED_STOP (0x200U)
34 
35 /* I2C Service Notification Command definition */
36 
37 typedef enum
38 {
39     /* I2C Service Request Command definition */
40     SRTM_I2C_CMD_READ = 0U,
41     SRTM_I2C_CMD_WRITE,
42 } srtm_i2c_cmd_t;
43 
44 /**
45  * @brief SRTM I2C payload structure
46  */
47 SRTM_ANON_DEC_BEGIN
48 SRTM_PACKED_BEGIN struct _srtm_i2c_payload
49 {
50     uint8_t busID;
51     union
52     {
53         uint8_t reserved; /* used in request packet */
54         uint8_t retCode;  /* used in response packet */
55     };
56     uint16_t slaveAddr;
57     uint16_t flags;
58     uint16_t len;
59     uint8_t data[1]; /* data size is decided by uint16_t len */
60 } SRTM_PACKED_END;
61 SRTM_ANON_DEC_END
62 
63 typedef struct _srtm_i2c_payload *srtm_i2c_payload_t;
64 
65 typedef enum
66 {
67     SRTM_I2C_TYPE_LPI2C = 0U,
68     SRTM_I2C_TYPE_I2C
69 } srtm_i2c_type_t;
70 
71 typedef enum
72 {
73     SRTM_I2C_SWITCH_CHANNEL0 = 0U,
74     SRTM_I2C_SWITCH_CHANNEL1,
75     SRTM_I2C_SWITCH_CHANNEL2,
76     SRTM_I2C_SWITCH_CHANNEL3,
77     SRTM_I2C_SWITCH_CHANNEL_UNSPECIFIED
78 } srtm_i2c_switch_channel;
79 
80 typedef struct _i2c_bus
81 {
82     uint8_t bus_id;
83     uint32_t base_addr;
84     srtm_i2c_type_t type;
85     uint8_t switch_idx;
86     srtm_i2c_switch_channel switch_channel;
87 } *i2c_bus_t;
88 
89 typedef struct _i2c_switch
90 {
91     uint16_t slaveAddr;
92     srtm_i2c_switch_channel cur_channel;
93 } *i2c_switch_t;
94 
95 typedef struct _i2c_bus_structure
96 {
97     i2c_bus_t buses;
98     uint8_t bus_num;
99     i2c_switch_t switches;
100     uint8_t switch_num;
101 } i2c_bus_structure_t;
102 
103 /**
104  * @brief SRTM I2C adapter structure pointer.
105  */
106 typedef struct _srtm_i2c_adapter *srtm_i2c_adapter_t;
107 
108 /**
109  * @brief SRTM I2C adapter structure
110  */
111 struct _srtm_i2c_adapter
112 {
113     /* Bound service */
114     srtm_service_t service;
115 
116     i2c_bus_structure_t bus_structure;
117 
118     /* Interfaces implemented by I2C adapter */
119     srtm_status_t (*read)(srtm_i2c_adapter_t adapter,
120                           uint32_t base_addr,
121                           srtm_i2c_type_t type,
122                           uint16_t slaveAddr,
123                           uint8_t *buf,
124                           uint16_t len,
125                           uint16_t flags);
126     srtm_status_t (*write)(srtm_i2c_adapter_t adapter,
127                            uint32_t base_addr,
128                            srtm_i2c_type_t type,
129                            uint16_t slaveAddr,
130                            uint8_t *buf,
131                            uint16_t len,
132                            uint16_t flags);
133     srtm_status_t (*switchchannel)(srtm_i2c_adapter_t adapter,
134                                    uint32_t base_addr,
135                                    srtm_i2c_type_t type,
136                                    uint16_t slaveAddr,
137                                    srtm_i2c_switch_channel channel);
138 };
139 /*******************************************************************************
140  * API
141  ******************************************************************************/
142 #ifdef __cplusplus
143 extern "C" {
144 #endif
145 /*!
146  * @brief Create I2C service.
147  *
148  * @param adapter I2C adapter to provide real I2C features.
149  * @return SRTM service handle on success and NULL on failure.
150  */
151 srtm_service_t SRTM_I2CService_Create(srtm_i2c_adapter_t adapter);
152 
153 /*!
154  * @brief Destroy I2C service.
155  *
156  * @param service SRTM service to destroy.
157  */
158 void SRTM_I2CService_Destroy(srtm_service_t service);
159 
160 /*!
161  * @brief Reset I2C service. This is used to stop sending events and return to initial state.
162  *
163  * @param service SRTM service to reset.
164  * @param core Identify which core is to be reset.
165  */
166 void SRTM_I2CService_Reset(srtm_service_t service, srtm_peercore_t core);
167 
168 /*!
169  * @brief Perfrom a local read of I2C bus
170  */
171 srtm_status_t SRTM_I2C_RequestBusRead(
172     srtm_service_t service, uint8_t busID, uint16_t slaveAddr, uint8_t *buf, uint16_t len);
173 
174 /*!
175  * @brief Perfrom a local write of I2C bus
176  */
177 srtm_status_t SRTM_I2C_RequestBusWrite(
178     srtm_service_t service, uint8_t busID, uint16_t slaveAddr, uint8_t *buf, uint16_t len, uint8_t needStop);
179 #ifdef __cplusplus
180 }
181 #endif
182 
183 /*! @} */
184 
185 #endif /* __SRTM_I2C_SERVICE_H__ */
186