1 /*!
2 * @file apm32e10x_sci2c.c
3 *
4 * @brief This file contains all the functions for the SCI2C peripheral
5 *
6 * @version V1.0.2
7 *
8 * @date 2022-12-31
9 *
10 * @attention
11 *
12 * Copyright (C) 2021-2023 Geehy Semiconductor
13 *
14 * You may not use this file except in compliance with the
15 * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16 *
17 * The program is only for reference, which is distributed in the hope
18 * that it will be useful and instructional for customers to develop
19 * their software. Unless required by applicable law or agreed to in
20 * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21 * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23 * and limitations under the License.
24 */
25
26 #include "apm32e10x_sci2c.h"
27 #include "apm32e10x_rcm.h"
28
29 /** @addtogroup APM32E10x_StdPeriphDriver
30 @{
31 */
32
33 /** @addtogroup SCI2C_Driver
34 * @brief SCI2C driver modules
35 @{
36 */
37
38 /** @defgroup SCI2C_Functions Functions
39 @{
40 */
41
42 /*!
43 * @brief Set I2C peripheral registers to their default reset values
44 *
45 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
46 *
47 * @retval None
48 */
SCI2C_Reset(SCI2C_T * i2c)49 void SCI2C_Reset(SCI2C_T *i2c)
50 {
51 if(i2c == I2C3)
52 {
53 RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_I2C1);
54 RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_I2C1);
55 }
56 else
57 {
58 RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_I2C2);
59 RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_I2C2);
60 }
61
62 i2c->SW = 0;
63 i2c->SW = 1;
64 i2c->INTEN = 0;
65 }
66
67 /*!
68 * @brief Config the I2C peripheral according to the specified parameters in the sci2cConfig
69 *
70 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
71 *
72 * @param sci2cConfig: pointer to a SCI2C_Config_T structure
73 *
74 * @retval None
75 */
SCI2C_Config(SCI2C_T * i2c,SCI2C_Config_T * sci2cConfig)76 void SCI2C_Config(SCI2C_T *i2c, SCI2C_Config_T *sci2cConfig)
77 {
78 i2c->SW = BIT_SET;
79
80 i2c->CTRL2_B.I2CEN = BIT_RESET;
81
82 if(sci2cConfig->mode == SCI2C_MODE_MASTER)
83 {
84 i2c->CTRL1_B.MST = BIT_SET;
85 i2c->CTRL1_B.SLADIS = BIT_SET;
86 }
87 else
88 {
89 i2c->CTRL1_B.MST = BIT_RESET;
90 }
91
92 i2c->CTRL1_B.SPD = sci2cConfig->speed;
93 i2c->CTRL1_B.RSTAEN = sci2cConfig->restart;
94
95 i2c->TFT = sci2cConfig->txFifoThreshold;
96 i2c->RFT = sci2cConfig->rxFifoThreshold;
97
98 i2c->TARADDR_B.MAM = sci2cConfig->addrMode;
99 i2c->CTRL1_B.SAM = sci2cConfig->addrMode;
100 i2c->SLAADDR = sci2cConfig->slaveAddr;
101
102 if(sci2cConfig->speed == SCI2C_SPEED_STANDARD)
103 {
104 i2c->SSCLC = sci2cConfig->clkLowPeriod;
105 i2c->SSCHC = sci2cConfig->clkHighPeriod;
106 }
107 else if(sci2cConfig->speed == SCI2C_SPEED_FAST)
108 {
109 i2c->FSCLC = sci2cConfig->clkLowPeriod;
110 i2c->FSCHC = sci2cConfig->clkHighPeriod;
111 }
112 else if(sci2cConfig->speed == SCI2C_SPEED_HIGH)
113 {
114 i2c->HSCLC = sci2cConfig->clkLowPeriod;
115 i2c->HSCHC = sci2cConfig->clkHighPeriod;
116 }
117 }
118
119 /*!
120 * @brief Fills each sci2cConfig member with its default value
121 *
122 * @param sci2cConfig: pointer to a SCI2C_Config_T structure
123 *
124 * @retval None
125 */
SCI2C_ConfigStructInit(SCI2C_Config_T * sci2cConfig)126 void SCI2C_ConfigStructInit(SCI2C_Config_T *sci2cConfig)
127 {
128 sci2cConfig->addrMode = SCI2C_ADDR_MODE_7BIT;
129 sci2cConfig->slaveAddr = 0x55;
130 sci2cConfig->clkHighPeriod = 0x3C;
131 sci2cConfig->clkLowPeriod = 0x82;
132 sci2cConfig->mode = SCI2C_MODE_MASTER;
133 sci2cConfig->restart = SCI2C_RESTART_ENABLE;
134 sci2cConfig->rxFifoThreshold = 0;
135 sci2cConfig->txFifoThreshold = 0;
136 sci2cConfig->speed = SCI2C_SPEED_FAST;
137 }
138
139 /*!
140 * @brief Read specified flag
141 *
142 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
143 *
144 * @param flag: Specifies the flag to be checked
145 * The parameter can be one of following values:
146 * @arg SCI2C_FLAG_ACT: Activity flag
147 * @arg SCI2C_FLAG_TFNF: Tx FIFO not full flag
148 * @arg SCI2C_FLAG_TFE: TX FIFO empty flag
149 * @arg SCI2C_FLAG_RFNE: Rx FIFO not empty flag
150 * @arg SCI2C_FLAG_RFF: Rx FIFO full flag
151 * @arg SCI2C_FLAG_MA: Master activity flag
152 * @arg SCI2C_FLAG_SA: Slave activity flag
153 * @arg SCI2C_FLAG_I2CEN: I2C enable flag
154 * @arg SCI2C_FLAG_SDWB: Slave disable while busy flag
155 * @arg SCI2C_FLAG_SRDL: Slave receive data lost flag
156 *
157 * @retval The new state of flag (SET or RESET)
158 */
SCI2C_ReadStatusFlag(SCI2C_T * i2c,SCI2C_FLAG_T flag)159 uint8_t SCI2C_ReadStatusFlag(SCI2C_T *i2c, SCI2C_FLAG_T flag)
160 {
161 uint8_t ret = RESET;
162
163 if(flag & BIT8)
164 {
165 ret = i2c->STS2 & flag ? SET : RESET;
166 }
167 else
168 {
169 ret = i2c->STS1 & flag ? SET : RESET;
170 }
171
172 return ret;
173 }
174
175 /*!
176 * @brief Read specified interrupt flag
177 *
178 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
179 *
180 * @param flag: Specifies the interrupt flag to be checked
181 * The parameter can be one of following values:
182 * @arg SCI2C_INT_RFU: Rx FIFO underflow interrupt flag
183 * @arg SCI2C_INT_RFO: Rx FIFO onverflow interrupt flag
184 * @arg SCI2C_INT_RFF: Rx FIFO full interrupt flag
185 * @arg SCI2C_INT_TFO: Tx FIFO onverflow interrupt flag
186 * @arg SCI2C_INT_TFE: Tx FIFO empty interrupt flag
187 * @arg SCI2C_INT_RR: Read request interrupt flag
188 * @arg SCI2C_INT_TA: Tx abort interrupt flag
189 * @arg SCI2C_INT_RD: Read done interrupt flag
190 * @arg SCI2C_INT_ACT: Activity interrupt flag
191 * @arg SCI2C_INT_STPD: Stop detect interrupt flag
192 * @arg SCI2C_INT_STAD: Start detect interrupt flag
193 * @arg SCI2C_INT_GC: Gernal call interrupt flag
194 * @arg SCI2C_INT_RSTAD: Restart detect interrupt flag
195 * @arg SCI2C_INT_MOH: Master on hold interrupt flag
196 *
197 * @retval The new state of flag (SET or RESET)
198 */
SCI2C_ReadIntFlag(SCI2C_T * i2c,SCI2C_INT_T flag)199 uint8_t SCI2C_ReadIntFlag(SCI2C_T *i2c, SCI2C_INT_T flag)
200 {
201 uint8_t ret = RESET;
202
203 ret = i2c->INTSTS & flag ? SET : RESET;
204
205 return ret;
206 }
207
208 /*!
209 * @brief Clear specified interrupt flag
210 *
211 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
212 *
213 * @param flag: Specifies the interrupt flag to be checked
214 * The parameter can be one of following values:
215 * @arg SCI2C_INT_RFU: Rx FIFO underflow interrupt flag
216 * @arg SCI2C_INT_RFO: Rx FIFO onverflow interrupt flag
217 * @arg SCI2C_INT_TFO: Tx FIFO onverflow interrupt flag
218 * @arg SCI2C_INT_RR: Read request interrupt flag
219 * @arg SCI2C_INT_TA: Tx abort interrupt flag
220 * @arg SCI2C_INT_RD: Read done interrupt flag
221 * @arg SCI2C_INT_ACT: Activity interrupt flag
222 * @arg SCI2C_INT_STPD: Stop detect interrupt flag
223 * @arg SCI2C_INT_STAD: Start detect interrupt flag
224 * @arg SCI2C_INT_GC: Gernal call interrupt flag
225 * @arg SCI2C_INT_ALL: All interrupt flag
226 * @retval The new state of flag (SET or RESET)
227 */
SCI2C_ClearIntFlag(SCI2C_T * i2c,SCI2C_INT_T flag)228 void SCI2C_ClearIntFlag(SCI2C_T *i2c, SCI2C_INT_T flag)
229 {
230 volatile uint32_t dummy = 0;
231
232 if(flag == SCI2C_INT_ALL)
233 {
234 dummy = i2c->INTCLR;
235 }
236 else if(flag == SCI2C_INT_RFU)
237 {
238 dummy = i2c->RFUIC;
239 }
240 else if(flag == SCI2C_INT_RFO)
241 {
242 dummy = i2c->RFOIC;
243 }
244 else if(flag == SCI2C_INT_TFO)
245 {
246 dummy = i2c->TFOIC;
247 }
248 else if(flag == SCI2C_INT_RR)
249 {
250 dummy = i2c->RRIC;
251 }
252 else if(flag == SCI2C_INT_TA)
253 {
254 dummy = i2c->TAIC;
255 }
256 else if(flag == SCI2C_INT_RD)
257 {
258 dummy = i2c->RDIC;
259 }
260 else if(flag == SCI2C_INT_ACT)
261 {
262 dummy = i2c->AIC;
263 }
264 else if(flag == SCI2C_INT_STPD)
265 {
266 dummy = i2c->STPDIC;
267 }
268 else if(flag == SCI2C_INT_STAD)
269 {
270 dummy = i2c->STADIC;
271 }
272 else if(flag == SCI2C_INT_GC)
273 {
274 dummy = i2c->GCIC;
275 }
276 }
277
278 /*!
279 * @brief Read specified interrupt flag(Raw register)
280 *
281 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
282 *
283 * @param flag: Specifies the interrupt flag to be checked
284 * The parameter can be one of following values:
285 * @arg SCI2C_INT_RFU: Rx FIFO underflow interrupt flag
286 * @arg SCI2C_INT_RFO: Rx FIFO onverflow interrupt flag
287 * @arg SCI2C_INT_RFF: Rx FIFO full interrupt flag
288 * @arg SCI2C_INT_TFO: Tx FIFO onverflow interrupt flag
289 * @arg SCI2C_INT_TFE: Tx FIFO empty interrupt flag
290 * @arg SCI2C_INT_RR: Read request interrupt flag
291 * @arg SCI2C_INT_TA: Tx abort interrupt flag
292 * @arg SCI2C_INT_RD: Read done interrupt flag
293 * @arg SCI2C_INT_ACT: Activity interrupt flag
294 * @arg SCI2C_INT_STPD: Stop detect interrupt flag
295 * @arg SCI2C_INT_STAD: Start detect interrupt flag
296 * @arg SCI2C_INT_GC: Gernal call interrupt flag
297 * @arg SCI2C_INT_RSTAD: Restart detect interrupt flag
298 * @arg SCI2C_INT_MOH: Master on hold interrupt flag
299 *
300 * @retval The new state of flag (SET or RESET)
301 */
SCI2C_ReadRawIntFlag(SCI2C_T * i2c,SCI2C_INT_T flag)302 uint8_t SCI2C_ReadRawIntFlag(SCI2C_T *i2c, SCI2C_INT_T flag)
303 {
304 uint8_t ret = RESET;
305
306 ret = i2c->RIS & flag ? SET : RESET;
307
308 return ret;
309 }
310
311 /*!
312 * @brief Enable the specified interrupts
313 *
314 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
315 *
316 * @param interrupt: Specifies the interrupt sources
317 * The parameter can be any combination of following values:
318 * @arg SCI2C_INT_RFU: Rx FIFO underflow interrupt
319 * @arg SCI2C_INT_RFO: Rx FIFO onverflow interrupt
320 * @arg SCI2C_INT_RFF: Rx FIFO full interrupt
321 * @arg SCI2C_INT_TFO: Tx FIFO onverflow interrupt
322 * @arg SCI2C_INT_TFE: Tx FIFO empty interrupt
323 * @arg SCI2C_INT_RR: Read request interrupt
324 * @arg SCI2C_INT_TA: Tx abort interrupt
325 * @arg SCI2C_INT_RD: Read done interrupt
326 * @arg SCI2C_INT_ACT: Activity interrupt
327 * @arg SCI2C_INT_STPD: Stop detect interrupt
328 * @arg SCI2C_INT_STAD: Start detect interrupt
329 * @arg SCI2C_INT_GC: Gernal call interrupt
330 * @arg SCI2C_INT_RSTAD: Restart detect interrupt
331 * @arg SCI2C_INT_MOH: Master on hold interrupt
332 *
333 * @retval None
334 */
SCI2C_EnableInterrupt(SCI2C_T * i2c,uint16_t interrupt)335 void SCI2C_EnableInterrupt(SCI2C_T *i2c, uint16_t interrupt)
336 {
337 i2c->INTEN |= interrupt;
338 }
339
340 /*!
341 * @brief Disable the specified interrupts
342 *
343 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
344 *
345 * @param interrupt: Specifies the interrupt sources
346 * The parameter can be any combination of following values:
347 * @arg SCI2C_INT_RFU: Rx FIFO underflow interrupt
348 * @arg SCI2C_INT_RFO: Rx FIFO onverflow interrupt
349 * @arg SCI2C_INT_RFF: Rx FIFO full interrupt
350 * @arg SCI2C_INT_TFO: Tx FIFO onverflow interrupt
351 * @arg SCI2C_INT_TFE: Tx FIFO empty interrupt
352 * @arg SCI2C_INT_RR: Read request interrupt
353 * @arg SCI2C_INT_TA: Tx abort interrupt
354 * @arg SCI2C_INT_RD: Read done interrupt
355 * @arg SCI2C_INT_ACT: Activity interrupt
356 * @arg SCI2C_INT_STPD: Stop detect interrupt
357 * @arg SCI2C_INT_STAD: Start detect interrupt
358 * @arg SCI2C_INT_GC: Gernal call interrupt
359 * @arg SCI2C_INT_RSTAD: Restart detect interrupt
360 * @arg SCI2C_INT_MOH: Master on hold interrupt
361 *
362 * @retval None
363 */
SCI2C_DisableInterrupt(SCI2C_T * i2c,uint16_t interrupt)364 void SCI2C_DisableInterrupt(SCI2C_T *i2c, uint16_t interrupt)
365 {
366 i2c->INTEN &= ~interrupt;
367 }
368
369 /*!
370 * @brief Enable stop detected only master in activity.
371 *
372 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
373 */
SCI2C_EnableStopDetectMasterActivity(SCI2C_T * i2c)374 void SCI2C_EnableStopDetectMasterActivity(SCI2C_T *i2c)
375 {
376 i2c->CTRL1_B.DSMA = BIT_SET;
377 }
378
379 /*!
380 * @brief Disable stop detected only master in activity.
381 *
382 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
383 */
SCI2C_DisableStopDetectMasterActivity(SCI2C_T * i2c)384 void SCI2C_DisableStopDetectMasterActivity(SCI2C_T *i2c)
385 {
386 i2c->CTRL1_B.DSMA = BIT_RESET;
387 }
388
389 /*!
390 * @brief Enable stop detected only address is matched in slave mode.
391 *
392 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
393 */
SCI2C_EnableStopDetectAddressed(SCI2C_T * i2c)394 void SCI2C_EnableStopDetectAddressed(SCI2C_T *i2c)
395 {
396 i2c->CTRL1_B.DSA = BIT_SET;
397 }
398
399 /*!
400 * @brief Disable stop detected only address is matched in slave mode.
401 *
402 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
403 */
SCI2C_DisableStopDetectAddressed(SCI2C_T * i2c)404 void SCI2C_DisableStopDetectAddressed(SCI2C_T *i2c)
405 {
406 i2c->CTRL1_B.DSA = BIT_RESET;
407 }
408
409 /*!
410 * @brief Enable restart
411 *
412 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
413 *
414 * @retval None
415 */
SCI2C_EnableRestart(SCI2C_T * i2c)416 void SCI2C_EnableRestart(SCI2C_T *i2c)
417 {
418 i2c->CTRL1_B.RSTAEN = BIT_SET;
419 }
420
421 /*!
422 * @brief Disable restart
423 *
424 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
425 *
426 * @retval None
427 */
SCI2C_DisableRestart(SCI2C_T * i2c)428 void SCI2C_DisableRestart(SCI2C_T *i2c)
429 {
430 i2c->CTRL1_B.RSTAEN = BIT_RESET;
431 }
432
433 /*!
434 * @brief Config speed.
435 *
436 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
437 *
438 * @param speed: Specifies the speed.
439 * @arg SCI2C_SPEED_STANDARD: Standard speed.
440 * @arg SCI2C_SPEED_FAST: Fast speed.
441 * @arg SCI2C_SPEED_HIGH: High speed.
442 *
443 * @retval None
444 */
SCI2C_ConfigSpeed(SCI2C_T * i2c,SCI2C_SPEED_T speed)445 void SCI2C_ConfigSpeed(SCI2C_T *i2c, SCI2C_SPEED_T speed)
446 {
447 i2c->CTRL1_B.SPD = speed;
448 }
449
450 /*!
451 * @brief Config master address.
452 *
453 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
454 *
455 * @param mode: Specifies the address mode.
456 * @arg SCI2C_ADDR_MODE_7BIT: 7-bit address mode.
457 * @arg SCI2C_ADDR_MODE_10BIT: 10-bit address mode.
458 *
459 * @param addr: Specifies the address.
460 *
461 * @retval None
462 */
SCI2C_ConfigMasterAddr(SCI2C_T * i2c,SCI2C_ADDR_MODE_T mode,uint16_t addr)463 void SCI2C_ConfigMasterAddr(SCI2C_T *i2c, SCI2C_ADDR_MODE_T mode, uint16_t addr)
464 {
465 i2c->TARADDR_B.MAM = mode;
466 i2c->TARADDR_B.ADDR = addr;
467 }
468
469
470 /*!
471 * @brief Config slave address.
472 *
473 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
474 *
475 * @param mode: Specifies the address mode.
476 * @arg SCI2C_ADDR_MODE_7BIT: 7-bit address mode.
477 * @arg SCI2C_ADDR_MODE_10BIT: 10-bit address mode.
478 *
479 * @param addr: Specifies the address.
480 *
481 * @retval None
482 */
SCI2C_ConfigSlaveAddr(SCI2C_T * i2c,SCI2C_ADDR_MODE_T mode,uint16_t addr)483 void SCI2C_ConfigSlaveAddr(SCI2C_T *i2c, SCI2C_ADDR_MODE_T mode, uint16_t addr)
484 {
485 i2c->CTRL1_B.SAM = mode;
486 i2c->SLAADDR = addr;
487 }
488
489 /*!
490 * @brief Enable master mode
491 *
492 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
493 *
494 * @retval None
495 */
SCI2C_EnableMasterMode(SCI2C_T * i2c)496 void SCI2C_EnableMasterMode(SCI2C_T *i2c)
497 {
498 i2c->CTRL1_B.MST = BIT_SET;
499 }
500
501 /*!
502 * @brief Disable master mode
503 *
504 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
505 *
506 * @retval None
507 */
SCI2C_DisableMasterMode(SCI2C_T * i2c)508 void SCI2C_DisableMasterMode(SCI2C_T *i2c)
509 {
510 i2c->CTRL1_B.MST = BIT_RESET;
511 }
512
513 /*!
514 * @brief Enable slave mode
515 *
516 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
517 *
518 * @retval None
519 */
SCI2C_EnableSlaveMode(SCI2C_T * i2c)520 void SCI2C_EnableSlaveMode(SCI2C_T *i2c)
521 {
522 i2c->CTRL1_B.SLADIS = BIT_RESET;
523 }
524
525 /*!
526 * @brief Disable slave mode
527 *
528 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
529 *
530 * @retval None
531 */
SCI2C_DisableSlaveMode(SCI2C_T * i2c)532 void SCI2C_DisableSlaveMode(SCI2C_T *i2c)
533 {
534 i2c->CTRL1_B.SLADIS = BIT_SET;
535 }
536
537 /*!
538 * @brief Config master code
539 *
540 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
541 *
542 * @param code: Master code
543 *
544 * @retval None
545 */
SCI2C_ConfigMasterCode(SCI2C_T * i2c,uint8_t code)546 void SCI2C_ConfigMasterCode(SCI2C_T *i2c, uint8_t code)
547 {
548 i2c->HSMC = code;
549 }
550
551 /*!
552 * @brief Config data direction
553 *
554 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
555 *
556 * @param dir: Data direction
557 * @arg SCI2C_DATA_DIR_WRITE: Write data
558 * @arg SCI2C_DATA_DIR_READ: Read data
559 *
560 * @retval None
561 */
SCI2C_ConfigDataDir(SCI2C_T * i2c,SCI2C_DATA_DIR_T dir)562 void SCI2C_ConfigDataDir(SCI2C_T *i2c, SCI2C_DATA_DIR_T dir)
563 {
564 i2c->DATA = (uint32_t)(dir << 8);
565 }
566
567 /*!
568 * @brief Transmit data
569 *
570 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
571 *
572 * @param data: Data to be transmited
573 *
574 * @retval None
575 */
SCI2C_TxData(SCI2C_T * i2c,uint8_t data)576 void SCI2C_TxData(SCI2C_T *i2c, uint8_t data)
577 {
578 i2c->DATA_B.DATA = data;
579 }
580
581 /*!
582 * @brief Returns the most recent received data
583 *
584 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
585 *
586 * @retval Received data
587 *
588 * @note
589 */
SCI2C_RxData(SCI2C_T * i2c)590 uint8_t SCI2C_RxData(SCI2C_T *i2c)
591 {
592 return (uint8_t)(i2c->DATA & 0XFF);
593 }
594
595 /*!
596 * @brief Config data register
597 *
598 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
599 *
600 * @param stop: Enable or disable generate stop condition
601 *
602 * @param dataDir: Data direction. Read or write
603 * @arg SCI2C_DATA_DIR_WRITE: Write data
604 * @arg SCI2C_DATA_DIR_READ: Read data
605 *
606 * @param data: Data to be transmited
607 *
608 * @retval None
609 */
SCI2C_ConfigDataRegister(SCI2C_T * i2c,SCI2C_STOP_T stop,SCI2C_DATA_DIR_T dataDir,uint8_t data)610 void SCI2C_ConfigDataRegister(SCI2C_T *i2c, SCI2C_STOP_T stop, SCI2C_DATA_DIR_T dataDir, uint8_t data)
611 {
612 i2c->DATA = (uint32_t)((stop << 9) | (dataDir << 8) | data);
613 }
614
615 /*!
616 * @brief Read Rx FIFO data number
617 *
618 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
619 *
620 * @retval None
621 */
SCI2C_ReadRxFifoDataCnt(SCI2C_T * i2c)622 uint8_t SCI2C_ReadRxFifoDataCnt(SCI2C_T *i2c)
623 {
624 return (uint8_t)i2c->RFL;
625 }
626
627 /*!
628 * @brief Read Tx FIFO data number
629 *
630 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
631 *
632 * @retval None
633 */
SCI2C_ReadTxFifoDataCnt(SCI2C_T * i2c)634 uint8_t SCI2C_ReadTxFifoDataCnt(SCI2C_T *i2c)
635 {
636 return (uint8_t)i2c->TFL;
637 }
638
639 /*!
640 * @brief Config Rx FIFO threshold
641 *
642 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
643 *
644 * @param threshold: FIFO threshold
645 *
646 * @retval None
647 */
SCI2C_ConfigRxFifoThreshold(SCI2C_T * i2c,uint8_t threshold)648 void SCI2C_ConfigRxFifoThreshold(SCI2C_T *i2c, uint8_t threshold)
649 {
650 i2c->RFT = threshold;
651 }
652
653 /*!
654 * @brief Config Tx FIFO threshold
655 *
656 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
657 *
658 * @param threshold: FIFO threshold
659 *
660 * @retval None
661 */
SCI2C_ConfigTxFifoThreshold(SCI2C_T * i2c,uint8_t threshold)662 void SCI2C_ConfigTxFifoThreshold(SCI2C_T *i2c, uint8_t threshold)
663 {
664 i2c->TFT = threshold;
665 }
666
667 /*!
668 * @brief Enable I2C peripheral
669 *
670 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
671 *
672 * @retval None
673
674 */
SCI2C_Enable(SCI2C_T * i2c)675 void SCI2C_Enable(SCI2C_T *i2c)
676 {
677 i2c->CTRL2_B.I2CEN = BIT_SET;
678 }
679
680 /*!
681 * @brief Disable I2C peripheral
682 *
683 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
684 *
685 * @retval None
686 */
SCI2C_Disable(SCI2C_T * i2c)687 void SCI2C_Disable(SCI2C_T *i2c)
688 {
689 i2c->CTRL2_B.I2CEN = BIT_RESET;
690 }
691
692 /*!
693 * @brief Abort I2C transmit
694 *
695 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
696 *
697 * @retval None
698 */
SCI2C_Abort(SCI2C_T * i2c)699 void SCI2C_Abort(SCI2C_T *i2c)
700 {
701 i2c->CTRL2_B.ABR = BIT_SET;
702 }
703
704 /*!
705 * @brief Tx command block
706 *
707 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
708 *
709 * @param enable: ENABLE or DISABLE
710 *
711 * @retval None
712 */
SCI2C_BlockTxCmd(SCI2C_T * i2c,uint8_t enable)713 void SCI2C_BlockTxCmd(SCI2C_T *i2c, uint8_t enable)
714 {
715 i2c->CTRL2_B.TCB = enable;
716 }
717
718 /*!
719 * @brief Config SCL high and low period
720 *
721 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
722 *
723 * @param speed: Specifies the speed.
724 * @arg SCI2C_SPEED_STANDARD: Standard speed.
725 * @arg SCI2C_SPEED_FAST: Fast speed.
726 * @arg SCI2C_SPEED_HIGH: High speed.
727 *
728 * @param highPeriod: SCL high period
729 *
730 * @param lowPeriod: SCL low period
731 *
732 * @retval None
733 */
SCI2C_ConfigClkPeriod(SCI2C_T * i2c,SCI2C_SPEED_T speed,uint16_t highPeriod,uint16_t lowPeriod)734 void SCI2C_ConfigClkPeriod(SCI2C_T *i2c, SCI2C_SPEED_T speed, uint16_t highPeriod, uint16_t lowPeriod)
735 {
736 if(speed == SCI2C_SPEED_STANDARD)
737 {
738 i2c->SSCLC = lowPeriod;
739 i2c->SSCHC = highPeriod;
740 }
741 else if(speed == SCI2C_SPEED_FAST)
742 {
743 i2c->FSCLC = lowPeriod;
744 i2c->FSCHC = highPeriod;
745 }
746 else if(speed == SCI2C_SPEED_HIGH)
747 {
748 i2c->HSCLC = lowPeriod;
749 i2c->HSCHC = highPeriod;
750 }
751 }
752
753 /*!
754 * @brief Config SDA hold time length
755 *
756 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
757 *
758 * @param txHold: Tx SDA hold time length
759 *
760 * @param rxHold: Rx SDA hold time length
761 *
762 * @retval None
763 */
SCI2C_ConfigSDAHoldTime(SCI2C_T * i2c,uint16_t txHold,uint8_t rxHold)764 void SCI2C_ConfigSDAHoldTime(SCI2C_T *i2c, uint16_t txHold, uint8_t rxHold)
765 {
766 i2c->SDAHOLD_B.TXHOLD = txHold;
767 i2c->SDAHOLD_B.RXHOLD = rxHold;
768 }
769
770 /*!
771 * @brief Config SDA delay time
772 *
773 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
774 *
775 * @param delay: SDA delay time
776 *
777 * @retval None
778 */
SCI2C_ConfigSDADelayTime(SCI2C_T * i2c,uint8_t delay)779 void SCI2C_ConfigSDADelayTime(SCI2C_T *i2c, uint8_t delay)
780 {
781 i2c->SDADLY = delay;
782 }
783
784 /*!
785 * @brief Enable or disable generate gernal call ack
786 *
787 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
788 *
789 * @param enable: SDA delay time
790 *
791 * @retval None
792 */
SCI2C_GernalCallAck(SCI2C_T * i2c,uint8_t enable)793 void SCI2C_GernalCallAck(SCI2C_T *i2c, uint8_t enable)
794 {
795 i2c->GCA = enable;
796 }
797
798 /*!
799 * @brief When received data no ack generated in slave mode.
800 *
801 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
802 *
803 * @param enable: ENABLE or DISABLE
804 *
805 * @retval None
806 */
SCI2C_SlaveDataNackOnly(SCI2C_T * i2c,uint8_t enable)807 void SCI2C_SlaveDataNackOnly(SCI2C_T *i2c, uint8_t enable)
808 {
809 i2c->SDNO = enable;
810 }
811
812 /*!
813 * @brief Read Tx abort source
814 *
815 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
816 *
817 * @retval Return Tx abort source
818 */
SCI2C_ReadTxAbortSource(SCI2C_T * i2c)819 uint32_t SCI2C_ReadTxAbortSource(SCI2C_T *i2c)
820 {
821 return (uint32_t)i2c->TAS;
822 }
823
824 /*!
825 * @brief Enable DMA
826 *
827 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
828 *
829 * @param dma: DMA requst source
830 * @arg SCI2C_DMA_RX: DMA RX channel
831 * @arg SCI2C_DMA_TX: DMA TX channel
832 *
833 * @retval None
834 */
SCI2C_EnableDMA(SCI2C_T * i2c,SCI2C_DMA_T dma)835 void SCI2C_EnableDMA(SCI2C_T *i2c, SCI2C_DMA_T dma)
836 {
837 i2c->DMACTRL |= dma;
838 }
839
840 /*!
841 * @brief Disable DMA
842 *
843 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
844 *
845 * @param dma: DMA requst source
846 * @arg SCI2C_DMA_RX: DMA RX channel
847 * @arg SCI2C_DMA_TX: DMA TX channel
848 *
849 * @retval None
850 */
SCI2C_DisableDMA(SCI2C_T * i2c,SCI2C_DMA_T dma)851 void SCI2C_DisableDMA(SCI2C_T *i2c, SCI2C_DMA_T dma)
852 {
853 i2c->DMACTRL &= (uint32_t)~dma;
854 }
855
856 /*!
857 * @brief Config DMA Tx data level
858 *
859 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
860 *
861 * @param cnt: DMA Tx data level
862 *
863 * @retval None
864 */
SCI2C_ConfigDMATxDataLevel(SCI2C_T * i2c,uint8_t cnt)865 void SCI2C_ConfigDMATxDataLevel(SCI2C_T *i2c, uint8_t cnt)
866 {
867 i2c->DTDL = cnt;
868 }
869
870 /*!
871 * @brief Config DMA Rx data level
872 *
873 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
874 *
875 * @param cnt: DMA Rx data level
876 *
877 * @retval None
878 */
SCI2C_ConfigDMARxDataLevel(SCI2C_T * i2c,uint8_t cnt)879 void SCI2C_ConfigDMARxDataLevel(SCI2C_T *i2c, uint8_t cnt)
880 {
881 i2c->DRDL = cnt;
882 }
883
884 /*!
885 * @brief Config spike suppressio limit
886 *
887 * @param i2c: Select the the I2C peripheral.It can be I2C3 or I2C4
888 *
889 * @param speed: I2C speed mode
890 * @arg SCI2C_SPEED_STANDARD: Standard speed.
891 * @arg SCI2C_SPEED_FAST: Fast speed.
892 * @arg SCI2C_SPEED_HIGH: High speed.
893 *
894 * @param limit: Spike suppressio limit value
895 *
896 * @retval None
897 */
SCI2C_ConfigSpikeSuppressionLimit(SCI2C_T * i2c,SCI2C_SPEED_T speed,uint8_t limit)898 void SCI2C_ConfigSpikeSuppressionLimit(SCI2C_T *i2c, SCI2C_SPEED_T speed, uint8_t limit)
899 {
900 if(speed == SCI2C_SPEED_HIGH)
901 {
902 i2c->HSSSL = limit;
903 }
904 else
905 {
906 i2c->LSSSL = limit;
907 }
908 }
909
910 /**@} end of group SCI2C_Functions */
911 /**@} end of group SCI2C_Driver */
912 /**@} end of group APM32E10x_StdPeriphDriver */
913
914