1 /*!
2 * @file apm32f10x_sci2c.c
3 *
4 * @brief This file contains all the functions for the SCI2C peripheral
5 *
6 * @version V1.0.4
7 *
8 * @date 2022-12-01
9 *
10 * @attention
11 *
12 * Copyright (C) 2020-2022 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 "apm32f10x_sci2c.h"
27 #include "apm32f10x_rcm.h"
28
29 /** @addtogroup APM32F10x_StdPeriphDriver
30 @{
31 */
32
33 /** @addtogroup SCI2C_Driver 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 Configures 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 Configures 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 Configures 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 Configures 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 */
SCI2C_RxData(SCI2C_T * i2c)589 uint8_t SCI2C_RxData(SCI2C_T* i2c)
590 {
591 return (uint8_t)(i2c->DATA & 0XFF);
592 }
593
594 /*!
595 * @brief Configures data register
596 *
597 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
598 *
599 * @param stop: Enable or disable generate stop condition
600 *
601 * @param dataDir: Data direction. Read or write
602 * @arg SCI2C_DATA_DIR_WRITE: Write data
603 * @arg SCI2C_DATA_DIR_READ: Read data
604 *
605 * @param data: Data to be transmited
606 *
607 * @retval None
608 */
SCI2C_ConfigDataRegister(SCI2C_T * i2c,SCI2C_STOP_T stop,SCI2C_DATA_DIR_T dataDir,uint8_t data)609 void SCI2C_ConfigDataRegister(SCI2C_T* i2c, SCI2C_STOP_T stop, SCI2C_DATA_DIR_T dataDir, uint8_t data)
610 {
611 i2c->DATA = (uint32_t)((stop << 9) | (dataDir << 8) | data);
612 }
613
614 /*!
615 * @brief Read Rx FIFO data number
616 *
617 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
618 *
619 * @retval None
620 */
SCI2C_ReadRxFifoDataCnt(SCI2C_T * i2c)621 uint8_t SCI2C_ReadRxFifoDataCnt(SCI2C_T* i2c)
622 {
623 return (uint8_t)i2c->RFL;
624 }
625
626 /*!
627 * @brief Read Tx FIFO data number
628 *
629 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
630 *
631 * @retval None
632 */
SCI2C_ReadTxFifoDataCnt(SCI2C_T * i2c)633 uint8_t SCI2C_ReadTxFifoDataCnt(SCI2C_T* i2c)
634 {
635 return (uint8_t)i2c->TFL;
636 }
637
638 /*!
639 * @brief Configures Rx FIFO threshold
640 *
641 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
642 *
643 * @param threshold: FIFO threshold
644 *
645 * @retval None
646 */
SCI2C_ConfigRxFifoThreshold(SCI2C_T * i2c,uint8_t threshold)647 void SCI2C_ConfigRxFifoThreshold(SCI2C_T* i2c, uint8_t threshold)
648 {
649 i2c->RFT = threshold;
650 }
651
652 /*!
653 * @brief Configures Tx FIFO threshold
654 *
655 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
656 *
657 * @param threshold: FIFO threshold
658 *
659 * @retval None
660 */
SCI2C_ConfigTxFifoThreshold(SCI2C_T * i2c,uint8_t threshold)661 void SCI2C_ConfigTxFifoThreshold(SCI2C_T* i2c, uint8_t threshold)
662 {
663 i2c->TFT = threshold;
664 }
665
666 /*!
667 * @brief Enable I2C peripheral
668 *
669 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
670 *
671 * @retval None
672
673 */
SCI2C_Enable(SCI2C_T * i2c)674 void SCI2C_Enable(SCI2C_T* i2c)
675 {
676 i2c->CTRL2_B.I2CEN = BIT_SET;
677 }
678
679 /*!
680 * @brief Disable I2C peripheral
681 *
682 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
683 *
684 * @retval None
685 */
SCI2C_Disable(SCI2C_T * i2c)686 void SCI2C_Disable(SCI2C_T* i2c)
687 {
688 i2c->CTRL2_B.I2CEN = BIT_RESET;
689 }
690
691 /*!
692 * @brief Abort I2C transmit
693 *
694 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
695 *
696 * @retval None
697 */
SCI2C_Abort(SCI2C_T * i2c)698 void SCI2C_Abort(SCI2C_T* i2c)
699 {
700 i2c->CTRL2_B.ABR = BIT_SET;
701 }
702
703 /*!
704 * @brief Tx command block
705 *
706 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
707 *
708 * @param enable: ENABLE or DISABLE
709 *
710 * @retval None
711 */
SCI2C_BlockTxCmd(SCI2C_T * i2c,uint8_t enable)712 void SCI2C_BlockTxCmd(SCI2C_T* i2c, uint8_t enable)
713 {
714 i2c->CTRL2_B.TCB = enable;
715 }
716
717 /*!
718 * @brief Configures SCL high and low period
719 *
720 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
721 *
722 * @param speed: Specifies the speed.
723 * @arg SCI2C_SPEED_STANDARD: Standard speed.
724 * @arg SCI2C_SPEED_FAST: Fast speed.
725 * @arg SCI2C_SPEED_HIGH: High speed.
726 *
727 * @param highPeriod: SCL high period
728 *
729 * @param lowPeriod: SCL low period
730 *
731 * @retval None
732 */
SCI2C_ConfigClkPeriod(SCI2C_T * i2c,SCI2C_SPEED_T speed,uint16_t highPeriod,uint16_t lowPeriod)733 void SCI2C_ConfigClkPeriod(SCI2C_T* i2c, SCI2C_SPEED_T speed, uint16_t highPeriod, uint16_t lowPeriod)
734 {
735 if (speed == SCI2C_SPEED_STANDARD)
736 {
737 i2c->SSCLC = lowPeriod;
738 i2c->SSCHC = highPeriod;
739 }
740 else if (speed == SCI2C_SPEED_FAST)
741 {
742 i2c->FSCLC = lowPeriod;
743 i2c->FSCHC = highPeriod;
744 }
745 else if (speed == SCI2C_SPEED_HIGH)
746 {
747 i2c->HSCLC = lowPeriod;
748 i2c->HSCHC = highPeriod;
749 }
750 }
751
752 /*!
753 * @brief Configures SDA hold time length
754 *
755 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
756 *
757 * @param txHold: Tx SDA hold time length
758 *
759 * @param rxHold: Rx SDA hold time length
760 *
761 * @retval None
762 */
SCI2C_ConfigSDAHoldTime(SCI2C_T * i2c,uint16_t txHold,uint8_t rxHold)763 void SCI2C_ConfigSDAHoldTime(SCI2C_T* i2c, uint16_t txHold, uint8_t rxHold)
764 {
765 i2c->SDAHOLD_B.TXHOLD = txHold;
766 i2c->SDAHOLD_B.RXHOLD = rxHold;
767 }
768
769 /*!
770 * @brief Configures SDA delay time
771 *
772 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
773 *
774 * @param delay: SDA delay time
775 *
776 * @retval None
777 */
SCI2C_ConfigSDADelayTime(SCI2C_T * i2c,uint8_t delay)778 void SCI2C_ConfigSDADelayTime(SCI2C_T* i2c, uint8_t delay)
779 {
780 i2c->SDADLY = delay;
781 }
782
783 /*!
784 * @brief Enable or disable generate gernal call ack
785 *
786 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
787 *
788 * @param enable: SDA delay time
789 *
790 * @retval None
791 */
SCI2C_GernalCallAck(SCI2C_T * i2c,uint8_t enable)792 void SCI2C_GernalCallAck(SCI2C_T* i2c, uint8_t enable)
793 {
794 i2c->GCA = enable;
795 }
796
797 /*!
798 * @brief When received data no ack generated in slave mode.
799 *
800 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
801 *
802 * @param enable: ENABLE or DISABLE
803 *
804 * @retval None
805 */
SCI2C_SlaveDataNackOnly(SCI2C_T * i2c,uint8_t enable)806 void SCI2C_SlaveDataNackOnly(SCI2C_T* i2c, uint8_t enable)
807 {
808 i2c->SDNO = enable;
809 }
810
811 /*!
812 * @brief Read Tx abort source
813 *
814 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
815 *
816 * @retval Return Tx abort source
817 */
SCI2C_ReadTxAbortSource(SCI2C_T * i2c)818 uint32_t SCI2C_ReadTxAbortSource(SCI2C_T* i2c)
819 {
820 return (uint32_t)i2c->TAS;
821 }
822
823 /*!
824 * @brief Enable DMA
825 *
826 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
827 *
828 * @param dma: DMA requst source
829 * @arg SCI2C_DMA_RX: DMA RX channel
830 * @arg SCI2C_DMA_TX: DMA TX channel
831 *
832 * @retval None
833 */
SCI2C_EnableDMA(SCI2C_T * i2c,SCI2C_DMA_T dma)834 void SCI2C_EnableDMA(SCI2C_T* i2c, SCI2C_DMA_T dma)
835 {
836 i2c->DMACTRL |= dma;
837 }
838
839 /*!
840 * @brief Disable DMA
841 *
842 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
843 *
844 * @param dma: DMA requst source
845 * @arg SCI2C_DMA_RX: DMA RX channel
846 * @arg SCI2C_DMA_TX: DMA TX channel
847 *
848 * @retval None
849 */
SCI2C_DisableDMA(SCI2C_T * i2c,SCI2C_DMA_T dma)850 void SCI2C_DisableDMA(SCI2C_T* i2c, SCI2C_DMA_T dma)
851 {
852 i2c->DMACTRL &= (uint32_t)~dma;
853 }
854
855 /*!
856 * @brief Configures DMA Tx data level
857 *
858 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
859 *
860 * @param cnt: DMA Tx data level
861 *
862 * @retval None
863 */
SCI2C_ConfigDMATxDataLevel(SCI2C_T * i2c,uint8_t cnt)864 void SCI2C_ConfigDMATxDataLevel(SCI2C_T* i2c, uint8_t cnt)
865 {
866 i2c->DTDL = cnt;
867 }
868
869 /*!
870 * @brief Configures DMA Rx data level
871 *
872 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
873 *
874 * @param cnt: DMA Rx data level
875 *
876 * @retval None
877 */
SCI2C_ConfigDMARxDataLevel(SCI2C_T * i2c,uint8_t cnt)878 void SCI2C_ConfigDMARxDataLevel(SCI2C_T* i2c, uint8_t cnt)
879 {
880 i2c->DRDL = cnt;
881 }
882
883 /*!
884 * @brief Configures spike suppressio limit
885 *
886 * @param i2c: Select the the I2C peripheral. It can be I2C3 or I2C4
887 *
888 * @param speed: I2C speed mode
889 * @arg SCI2C_SPEED_STANDARD: Standard speed.
890 * @arg SCI2C_SPEED_FAST: Fast speed.
891 * @arg SCI2C_SPEED_HIGH: High speed.
892 *
893 * @param limit: Spike suppressio limit value
894 *
895 * @retval None
896 */
SCI2C_ConfigSpikeSuppressionLimit(SCI2C_T * i2c,SCI2C_SPEED_T speed,uint8_t limit)897 void SCI2C_ConfigSpikeSuppressionLimit(SCI2C_T* i2c, SCI2C_SPEED_T speed, uint8_t limit)
898 {
899 if (speed == SCI2C_SPEED_HIGH)
900 {
901 i2c->HSSSL = limit;
902 }
903 else
904 {
905 i2c->LSSSL = limit;
906 }
907 }
908
909 /**@} end of group SCI2C_Functions */
910 /**@} end of group SCI2C_Driver */
911 /**@} end of group APM32F10x_StdPeriphDriver */
912