1 /*************************************************************************************
2 * Copyright (C) 2017, Huada Semiconductor Co.,Ltd All rights reserved.
3 *
4 * This software is owned and published by:
5 * Huada Semiconductor Co.,Ltd ("HDSC").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with HDSC
11 * components. This software is licensed by HDSC to be adapted only
12 * for use in systems utilizing HDSC components. HDSC shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. HDSC is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * Disclaimer:
19 * HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
20 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
21 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
22 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
23 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
24 * WARRANTY OF NONINFRINGEMENT.
25 * HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
26 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
27 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
28 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
29 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
31 * SAVINGS OR PROFITS,
32 * EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
34 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
35 * FROM, THE SOFTWARE.
36 *
37 * This software may be replicated in part or whole for the licensed use,
38 * with the restriction that this Disclaimer and Copyright notice must be
39 * included with each copy of this software, whether used in part or whole,
40 * at all times.
41 */
42 /******************************************************************************/
43 /** \file I2C.c
44 **
45 ** WDT function driver API.
46 ** @link SampleGroup Some description @endlink
47 **
48 ** - 2018-03-13 1.0 CJ First version for Device Driver Library of Module.
49 **
50 ******************************************************************************/
51
52 /******************************************************************************/
53 /* Include files */
54 /******************************************************************************/
55 #include "hc32l196_i2c.h"
56
57 /**
58 *******************************************************************************
59 ** \addtogroup I2cGroup
60 ******************************************************************************/
61 //@{
62
63 /******************************************************************************/
64 /* Local function prototypes ('static') */
65 /******************************************************************************/
66
67 /**
68 ******************************************************************************
69 ** \brief I2C设置波特率配置寄存器
70 **
71 ** \param [in] u8Tm 波特率配置值
72 **
73 ** \retval enRet 成功或失败
74 **
75 ******************************************************************************/
I2C_SetBaud(M0P_I2C_TypeDef * I2Cx,uint8_t u8Tm)76 en_result_t I2C_SetBaud(M0P_I2C_TypeDef* I2Cx, uint8_t u8Tm)
77 {
78 en_result_t enRet = Error;
79
80 I2Cx->TM = u8Tm;
81
82 enRet = Ok;
83 return enRet;
84 }
85 /**
86 ******************************************************************************
87 ** \brief I2C功能设置相关函数
88 **
89 ** \param [in] enFunc功能参数
90 **
91 ** \retval enRet 成功或失败
92 **
93 ******************************************************************************/
I2C_SetFunc(M0P_I2C_TypeDef * I2Cx,en_i2c_func_t enFunc)94 en_result_t I2C_SetFunc(M0P_I2C_TypeDef* I2Cx, en_i2c_func_t enFunc)
95 {
96 en_result_t enRet = Error;
97
98 SetBit((uint32_t)&I2Cx->CR, enFunc, TRUE);
99
100 enRet = Ok;
101 return enRet;
102 }
103 /**
104 ******************************************************************************
105 ** \brief I2C功能清除相关函数
106 **
107 ** \param [in] enFunc功能参数
108 **
109 ** \retval enRet 成功或失败
110 **
111 ******************************************************************************/
I2C_ClearFunc(M0P_I2C_TypeDef * I2Cx,en_i2c_func_t enFunc)112 en_result_t I2C_ClearFunc(M0P_I2C_TypeDef* I2Cx, en_i2c_func_t enFunc)
113 {
114 en_result_t enRet = Error;
115
116 SetBit((uint32_t)&I2Cx->CR, enFunc, FALSE);
117
118 enRet = Ok;
119 return enRet;
120 }
121 /**
122 ******************************************************************************
123 ** \brief I2C获取中断标记函数
124 **
125 ** \param 无
126 **
127 ** \retval bIrq中断标记
128 **
129 ******************************************************************************/
I2C_GetIrq(M0P_I2C_TypeDef * I2Cx)130 boolean_t I2C_GetIrq(M0P_I2C_TypeDef* I2Cx)
131 {
132 if(I2Cx->CR&0x8)
133 {
134 return TRUE;
135 }
136 else
137 {
138 return FALSE;
139 }
140 }
141 /**
142 ******************************************************************************
143 ** \brief I2C清除中断标记函数
144 **
145 ** \param 无
146 **
147 ** \retval bIrq中断标记
148 **
149 ******************************************************************************/
I2C_ClearIrq(M0P_I2C_TypeDef * I2Cx)150 en_result_t I2C_ClearIrq(M0P_I2C_TypeDef* I2Cx)
151 {
152 en_result_t enRet = Error;
153
154 I2Cx->CR &= ~0x8u;
155
156 enRet = Ok;
157 return enRet;
158 }
159 /**
160 ******************************************************************************
161 ** \brief I2C获取相关状态
162 **
163 ** \param 无
164 **
165 ** \retval I2C状态
166 **
167 ******************************************************************************/
I2C_GetState(M0P_I2C_TypeDef * I2Cx)168 uint8_t I2C_GetState(M0P_I2C_TypeDef* I2Cx)
169 {
170 uint8_t u8State = 0;
171
172 u8State = I2Cx->STAT;
173
174 return u8State;
175 }
176
177 /**
178 ******************************************************************************
179 ** \brief 字节数据写函数
180 **
181 ** \param u8Data写数据
182 **
183 ** \retval 写数据是否成功
184 **
185 ******************************************************************************/
I2C_WriteByte(M0P_I2C_TypeDef * I2Cx,uint8_t u8Data)186 en_result_t I2C_WriteByte(M0P_I2C_TypeDef* I2Cx, uint8_t u8Data)
187 {
188 en_result_t enRet = Error;
189
190 I2Cx->DATA = u8Data;
191
192 enRet = Ok;
193 return enRet;
194 }
195 /**
196 ******************************************************************************
197 ** \brief 字节数据读函数
198 **
199 ** \param 无
200 **
201 ** \retval 读取数据
202 **
203 ******************************************************************************/
I2C_ReadByte(M0P_I2C_TypeDef * I2Cx)204 uint8_t I2C_ReadByte(M0P_I2C_TypeDef* I2Cx)
205 {
206 uint8_t u8Data = 0;
207
208 u8Data = I2Cx->DATA;
209
210 return u8Data;
211 }
212
213 /**
214 ******************************************************************************
215 ** \brief I2C模块初始化
216 **
217 ** \param pstcI2CCfg初始化配置结构体
218 **
219 ** \retval 初始化是否成功
220 **
221 ******************************************************************************/
I2C_Init(M0P_I2C_TypeDef * I2Cx,stc_i2c_cfg_t * pstcI2CCfg)222 en_result_t I2C_Init(M0P_I2C_TypeDef* I2Cx, stc_i2c_cfg_t *pstcI2CCfg)
223 {
224 en_result_t enRet = Error;
225 uint8_t u8Tm;
226
227 if(M0P_I2C0 == I2Cx)
228 {
229 M0P_RESET->PERI_RESET0 &= ~(uint32_t)0x10u;
230 M0P_RESET->PERI_RESET0 |= (uint32_t)0x10u;
231 }
232 else
233 {
234 M0P_RESET->PERI_RESET0 &= ~(uint32_t)0x20u;
235 M0P_RESET->PERI_RESET0 |= (uint32_t)0x20u;
236 }
237
238 I2Cx->CR = 0;
239 I2Cx->CR = pstcI2CCfg->enMode;
240
241 if((pstcI2CCfg->u32Baud<<4) > pstcI2CCfg->u32Pclk)
242 {
243 return Error;
244 }
245
246 if(I2cMasterMode == pstcI2CCfg->enMode)
247 {
248 I2Cx->TMRUN = TRUE;
249 ///< Fsck = Fpclk/8*(Tm+1)
250 u8Tm = ((pstcI2CCfg->u32Pclk / pstcI2CCfg->u32Baud) >> 3) - 1;
251 if(9 > u8Tm)
252 {
253 I2C_SetFunc(I2Cx,I2cHlm_En);
254 }
255 enRet = I2C_SetBaud(I2Cx, u8Tm);
256 }
257 else
258 {
259 I2Cx->TMRUN = FALSE;
260 pstcI2CCfg->u8SlaveAddr = (uint8_t)(((uint32_t)pstcI2CCfg->u8SlaveAddr<<1)|(pstcI2CCfg->bGc));
261 I2Cx->ADDR = pstcI2CCfg->u8SlaveAddr;
262 }
263
264 return enRet;
265 }
266
267 //@} // I2cGroup
268