1 /*
2  * Copyright  2021-2022 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef _FSL_SX1502_H_
9 #define _FSL_SX1502_H_
10 
11 #include "fsl_common.h"
12 #include "fsl_adapter_i2c.h"
13 
14 /*!
15  * @addtogroup SX1502
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 /*! @name Driver version */
23 /*@{*/
24 /*! @brief SX1502 driver version 2.0.0. */
25 #define FSL_SX1502_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
26 /*@}*/
27 
28 /*! @brief SX1502 handle size */
29 #ifndef SX1502_I2C_HANDLER_SIZE
30 #define SX1502_I2C_HANDLER_SIZE HAL_I2C_MASTER_HANDLE_SIZE
31 #endif
32 
33 /*! @brief SX1502 register map*/
34 #define SX1502_REGDATA            (0x00U)
35 #define SX1502_REGDIR             (0x01U)
36 #define SX1502_REGPULLUP          (0x02U)
37 #define SX1502_REGPULLDOWN        (0x03U)
38 #define SX1502_REGINTERRUPTMASK   (0x05U)
39 #define SX1502_REGSENSEHIGH       (0x06U)
40 #define SX1502_REGSENSELOW        (0x07U)
41 #define SX1502_REGINTERRUPTSOURCE (0x08U)
42 #define SX1502_REGEVENTSTATUS     (0x09U)
43 #define SX1502_REGPLDMODE         (0x10U)
44 #define SX1502_REGPLDTABLE0       (0x11U)
45 #define SX1502_REGPLDTABLE1       (0x12U)
46 #define SX1502_REGPLDTABLE2       (0x13U)
47 #define SX1502_REGPLDTABLE3       (0x14U)
48 #define SX1502_REGPLDTABLE4       (0x15U)
49 #define SX1502_REGADVANCED        (0xABU)
50 
51 /*! @brief SX1502 I2C address. */
52 #define SX1502_I2C_ADDRESS (0x20U)
53 
54 /*! @brief SX1502 I2C bit rate. */
55 #define SX1502_I2C_BITRATE (400000U)
56 
57 /*! @brief SX1502 IO output status.*/
58 typedef enum _sx1502_io_status
59 {
60     kSX1502_IO_Low  = 0x0, /*!< Output low */
61     kSX1502_IO_High = 0x1, /*!< Output high */
62 } sx1502_io_status_t;
63 
64 /*! @brief SX1502 IO direction.*/
65 typedef enum _sx1502_io_dir
66 {
67     kSX1502_IO_Output = 0, /*!< Output */
68     kSX1502_IO_Input  = 1, /*!< Input */
69 } sx1502_io_dir_t;
70 
71 /*! @brief SX1502 IO index.*/
72 typedef enum _sx1502_io_index
73 {
74     kSX1502_IO0    = 0x01U, /*!< IO_0 */
75     kSX1502_IO1    = 0x02U, /*!< IO_1 */
76     kSX1502_IO2    = 0x04U, /*!< IO_2 */
77     kSX1502_IO3    = 0x08U, /*!< IO_3 */
78     kSX1502_IO4    = 0x10U, /*!< IO_4 */
79     kSX1502_IO5    = 0x20U, /*!< IO_5 */
80     kSX1502_IO6    = 0x40U, /*!< IO_6 */
81     kSX1502_IO7    = 0x80U, /*!< IO_7 */
82     kSX1502_IO_All = 0xFFU, /*!< IO_All */
83 } sx1502_io_index_t;
84 
85 /*! @brief Configuration structure of SX1502.*/
86 typedef struct _sx1502_config
87 {
88     uint8_t initRegDataValue;      /*!< RegData register init value */
89     uint8_t initRegDirValue;       /*!< RegDir register init value */
90     uint32_t sx1502I2CInstance;    /*!< i2c bus instance */
91     uint32_t sx1502I2CSourceClock; /*!< i2c bus source clock frequency */
92 } sx1502_config_t;
93 
94 /*! @brief SX1502 handler
95  */
96 typedef struct _sx1502_handle
97 {
98     uint8_t slaveAddress;                       /*!< code device slave address */
99     uint8_t i2cHandle[SX1502_I2C_HANDLER_SIZE]; /*!< i2c handle */
100 } sx1502_handle_t;
101 
102 /*******************************************************************************
103  * API
104  ******************************************************************************/
105 #if defined(__cplusplus)
106 extern "C" {
107 #endif
108 
109 /*!
110  * @brief SX1502 write register.
111  *
112  * @param handle SX1502 handle structure.
113  * @param reg register address.
114  * @param value value to write.
115  * @return kStatus_Success, else failed.
116  */
117 status_t SX1502_WriteRegister(sx1502_handle_t *handle, uint8_t reg, uint8_t value);
118 
119 /*!
120  * @brief SX1502 read register.
121  *
122  * @param handle SX1502 handle structure.
123  * @param reg register address.
124  * @param value value to read.
125  * @return kStatus_Success, else failed.
126  */
127 status_t SX1502_ReadRegister(sx1502_handle_t *handle, uint8_t reg, uint8_t *value);
128 
129 /*!
130  * @brief SX1502 modify register.
131  *
132  * @param handle SX1502 handle structure.
133  * @param reg register address.
134  * @param mask register bits mask.
135  * @param value value to write.
136  * @return kStatus_Success, else failed.
137  */
138 status_t SX1502_ModifyRegister(sx1502_handle_t *handle, uint8_t reg, uint8_t mask, uint8_t value);
139 
140 /*!
141  * @brief Initializes SX1502.
142  *
143  * @param handle SX1502 handle structure.
144  * @param sx1502Config SX1502 configuration structure.
145  */
146 status_t SX1502_Init(sx1502_handle_t *handle, const sx1502_config_t *sx1502Config);
147 
148 /*!
149  * @brief Deinitializes the SX1502 codec.
150  *
151  * This function resets SX1502.
152  *
153  * @param handle SX1502 handle structure.
154  *
155  * @return kStatus_Success if successful, different code otherwise.
156  */
157 status_t SX1502_Deinit(sx1502_handle_t *handle);
158 
159 /*!
160  * @brief SX1502 IO Set.
161  *
162  * @param handle SX1502 handle structure.
163  * @param ioMask bits to be set,it can be a single IO or multiple IO,control multiple IO with or operation.
164  * eg.Set IO1~IO3,ioMask is kSX1502_IO1|kSX1502_IO2|kSX1502_IO3.
165  * @return kStatus_Success, else failed.
166  */
167 status_t SX1502_IO_Set(sx1502_handle_t *handle, uint8_t ioMask);
168 
169 /*!
170  * @brief SX1502 IO Clear.
171  *
172  * @param handle SX1502 handle structure.
173  * @param ioMask bits to be set,it can be a single IO or multiple IO,control multiple IO with or-operation.
174  * eg.Clear IO1~IO3,ioMask is kSX1502_IO1|kSX1502_IO2|kSX1502_IO3.
175  * @return kStatus_Success, else failed.
176  */
177 status_t SX1502_IO_Clear(sx1502_handle_t *handle, uint8_t ioMask);
178 
179 /*!
180  * @brief SX1502 IO Direction to input or output.
181  *
182  * This API is used to set the direction of any IO combination.For example,set the direction of IO0~IO3 as output.
183  * SX1502_IO_SetDirection(handle, kSX1502_IO0|kSX1502_IO1|kSX1502_IO2|kSX1502_IO3, kSX1502_IO_Output);
184  *
185  * @param handle SX1502 handle structure.
186  * @param ioMask bits to be set,it can be a single IO or multiple IO,control multiple IO with or-operation.
187  * @param ioDirection set IO direction to input or output.
188  * @return kStatus_Success, else failed.
189  */
190 status_t SX1502_IO_SetDirection(sx1502_handle_t *handle, uint8_t ioMask, sx1502_io_dir_t ioDirection);
191 
192 /*!
193  * @brief SX1502 IO output status in a pattern.
194  *
195  * This API is used to set any IO combination and output in any pattern.
196  * For example,set IO0~IO3 as output and iopattern as 0101,iopattern is 0x05U.
197  * SX1502_IO_OutputControl(handle, kSX1502_IO0|kSX1502_IO1|kSX1502_IO2|kSX1502_IO3, 0x05U);
198  *
199  * @param handle SX1502 handle structure.
200  * @param ioMask bits to be set,it can be a single IO or multiple IO,control multiple IO with or operation.
201  * @param ioPattern ioPattern is the pattern of IO status to set.0-output low,1-output high.
202  * @return kStatus_Success, else failed.
203  */
204 status_t SX1502_IO_OutputControl(sx1502_handle_t *handle, uint8_t ioMask, uint8_t ioPattern);
205 
206 #if defined(__cplusplus)
207 }
208 #endif
209 
210 #endif /* _FSL_SX1502_H_ */
211