M480 BSP V3.05.006
The Board Support Package for M480 Series
usci_i2c.c
Go to the documentation of this file.
1/****************************************************************************/
9#include "NuMicro.h"
10
34uint32_t UI2C_Open(UI2C_T *ui2c, uint32_t u32BusClock)
35{
36 uint32_t u32ClkDiv;
37 uint32_t u32Pclk;
38
39 if( ui2c == UI2C0 )
40 {
41 u32Pclk = CLK_GetPCLK0Freq();
42 }
43 else
44 {
45 u32Pclk = CLK_GetPCLK1Freq();
46 }
47
48 u32ClkDiv = (uint32_t) ((((((u32Pclk/2U)*10U)/(u32BusClock))+5U)/10U)-1U); /* Compute proper divider for USCI_I2C clock */
49
50 /* Enable USCI_I2C protocol */
51 ui2c->CTL &= ~UI2C_CTL_FUNMODE_Msk;
52 ui2c->CTL = 4U << UI2C_CTL_FUNMODE_Pos;
53
54 /* Data format configuration */
55 /* 8 bit data length */
56 ui2c->LINECTL &= ~UI2C_LINECTL_DWIDTH_Msk;
57 ui2c->LINECTL |= 8U << UI2C_LINECTL_DWIDTH_Pos;
58
59 /* MSB data format */
60 ui2c->LINECTL &= ~UI2C_LINECTL_LSB_Msk;
61
62 /* Set USCI_I2C bus clock */
63 ui2c->BRGEN &= ~UI2C_BRGEN_CLKDIV_Msk;
64 ui2c->BRGEN |= (u32ClkDiv << UI2C_BRGEN_CLKDIV_Pos);
66
67 return ( u32Pclk / ((u32ClkDiv+1U)<<1U) );
68}
69
79void UI2C_Close(UI2C_T *ui2c)
80{
81 /* Disable USCI_I2C function */
82 ui2c->CTL &= ~UI2C_CTL_FUNMODE_Msk;
83}
84
95{
97}
98
112void UI2C_Trigger(UI2C_T *ui2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Ptrg, uint8_t u8Ack)
113{
114 uint32_t u32Reg = 0U;
116
117 if (u8Start)
118 {
119 u32Reg |= UI2C_PROTCTL_STA_Msk;
120 }
121 if (u8Stop)
122 {
123 u32Reg |= UI2C_PROTCTL_STO_Msk;
124 }
125 if (u8Ptrg)
126 {
127 u32Reg |= UI2C_PROTCTL_PTRG_Msk;
128 }
129 if (u8Ack)
130 {
131 u32Reg |= UI2C_PROTCTL_AA_Msk;
132 }
133
134 ui2c->PROTCTL = u32Val | u32Reg;
135}
136
156void UI2C_DisableInt(UI2C_T *ui2c, uint32_t u32Mask)
157{
158 /* Disable time-out interrupt flag */
159 if((u32Mask & UI2C_TO_INT_MASK) == UI2C_TO_INT_MASK)
160 {
161 ui2c->PROTIEN &= ~UI2C_PROTIEN_TOIEN_Msk;
162 }
163
164 /* Disable start condition received interrupt flag */
165 if((u32Mask & UI2C_STAR_INT_MASK) == UI2C_STAR_INT_MASK)
166 {
167 ui2c->PROTIEN &= ~UI2C_PROTIEN_STARIEN_Msk;
168 }
169
170 /* Disable stop condition received interrupt flag */
171 if((u32Mask & UI2C_STOR_INT_MASK) == UI2C_STOR_INT_MASK)
172 {
173 ui2c->PROTIEN &= ~UI2C_PROTIEN_STORIEN_Msk;
174 }
175
176 /* Disable non-acknowledge interrupt flag */
177 if((u32Mask & UI2C_NACK_INT_MASK) == UI2C_NACK_INT_MASK)
178 {
179 ui2c->PROTIEN &= ~UI2C_PROTIEN_NACKIEN_Msk;
180 }
181
182 /* Disable arbitration lost interrupt flag */
184 {
185 ui2c->PROTIEN &= ~UI2C_PROTIEN_ARBLOIEN_Msk;
186 }
187
188 /* Disable error interrupt flag */
189 if((u32Mask & UI2C_ERR_INT_MASK) == UI2C_ERR_INT_MASK)
190 {
191 ui2c->PROTIEN &= ~UI2C_PROTIEN_ERRIEN_Msk;
192 }
193
194 /* Disable acknowledge interrupt flag */
195 if((u32Mask & UI2C_ACK_INT_MASK) == UI2C_ACK_INT_MASK)
196 {
197 ui2c->PROTIEN &= ~UI2C_PROTIEN_ACKIEN_Msk;
198 }
199}
200
218void UI2C_EnableInt(UI2C_T *ui2c, uint32_t u32Mask)
219{
220 /* Enable time-out interrupt flag */
221 if((u32Mask & UI2C_TO_INT_MASK) == UI2C_TO_INT_MASK)
222 {
224 }
225
226 /* Enable start condition received interrupt flag */
227 if((u32Mask & UI2C_STAR_INT_MASK) == UI2C_STAR_INT_MASK)
228 {
230 }
231
232 /* Enable stop condition received interrupt flag */
233 if((u32Mask & UI2C_STOR_INT_MASK) == UI2C_STOR_INT_MASK)
234 {
236 }
237
238 /* Enable non-acknowledge interrupt flag */
239 if((u32Mask & UI2C_NACK_INT_MASK) == UI2C_NACK_INT_MASK)
240 {
242 }
243
244 /* Enable arbitration lost interrupt flag */
246 {
248 }
249
250 /* Enable error interrupt flag */
251 if((u32Mask & UI2C_ERR_INT_MASK) == UI2C_ERR_INT_MASK)
252 {
254 }
255
256 /* Enable acknowledge interrupt flag */
257 if((u32Mask & UI2C_ACK_INT_MASK) == UI2C_ACK_INT_MASK)
258 {
260 }
261}
262
273{
274 uint32_t u32Divider;
275 uint32_t u32Pclk;
276
277 if (ui2c == UI2C0)
278 {
279 u32Pclk = CLK_GetPCLK0Freq();
280 }
281 else
282 {
283 u32Pclk = CLK_GetPCLK1Freq();
284 }
285
286 u32Divider = (ui2c->BRGEN & UI2C_BRGEN_CLKDIV_Msk) >> UI2C_BRGEN_CLKDIV_Pos;
287
288 return ( u32Pclk / ((u32Divider+1U)<<1U) );
289}
290
301uint32_t UI2C_SetBusClockFreq(UI2C_T *ui2c, uint32_t u32BusClock)
302{
303 uint32_t u32ClkDiv;
304 uint32_t u32Pclk;
305
306 if( ui2c == UI2C0 )
307 {
308 u32Pclk = CLK_GetPCLK0Freq();
309 }
310 else
311 {
312 u32Pclk = CLK_GetPCLK1Freq();
313 }
314
315 u32ClkDiv = (uint32_t) ((((((u32Pclk/2U)*10U)/(u32BusClock))+5U)/10U)-1U); /* Compute proper divider for USCI_I2C clock */
316
317 /* Set USCI_I2C bus clock */
318 ui2c->BRGEN &= ~UI2C_BRGEN_CLKDIV_Msk;
319 ui2c->BRGEN |= (u32ClkDiv << UI2C_BRGEN_CLKDIV_Pos);
320
321 return ( u32Pclk / ((u32ClkDiv+1U)<<1U) );
322}
323
343uint32_t UI2C_GetIntFlag(UI2C_T *ui2c, uint32_t u32Mask)
344{
345 uint32_t u32IntFlag = 0U;
346 uint32_t u32TmpValue;
347
348 u32TmpValue = ui2c->PROTSTS & UI2C_PROTSTS_TOIF_Msk;
349 /* Check Time-out Interrupt Flag */
350 if((u32Mask & UI2C_TO_INT_MASK) && (u32TmpValue))
351 {
352 u32IntFlag |= UI2C_TO_INT_MASK;
353 }
354
355 u32TmpValue = ui2c->PROTSTS & UI2C_PROTSTS_STARIF_Msk;
356 /* Check Start Condition Received Interrupt Flag */
357 if((u32Mask & UI2C_STAR_INT_MASK) && (u32TmpValue))
358 {
359 u32IntFlag |= UI2C_STAR_INT_MASK;
360 }
361
362 u32TmpValue = ui2c->PROTSTS & UI2C_PROTSTS_STORIF_Msk;
363 /* Check Stop Condition Received Interrupt Flag */
364 if((u32Mask & UI2C_STOR_INT_MASK) && (u32TmpValue))
365 {
366 u32IntFlag |= UI2C_STOR_INT_MASK;
367 }
368
369 u32TmpValue = ui2c->PROTSTS & UI2C_PROTSTS_NACKIF_Msk;
370 /* Check Non-Acknowledge Interrupt Flag */
371 if((u32Mask & UI2C_NACK_INT_MASK) && (u32TmpValue))
372 {
373 u32IntFlag |= UI2C_NACK_INT_MASK;
374 }
375
376 u32TmpValue = ui2c->PROTSTS & UI2C_PROTSTS_ARBLOIF_Msk;
377 /* Check Arbitration Lost Interrupt Flag */
378 if((u32Mask & UI2C_ARBLO_INT_MASK) && (u32TmpValue))
379 {
380 u32IntFlag |= UI2C_ARBLO_INT_MASK;
381 }
382
383 u32TmpValue = ui2c->PROTSTS & UI2C_PROTSTS_ERRIF_Msk;
384 /* Check Error Interrupt Flag */
385 if((u32Mask & UI2C_ERR_INT_MASK) && (u32TmpValue))
386 {
387 u32IntFlag |= UI2C_ERR_INT_MASK;
388 }
389
390 u32TmpValue = ui2c->PROTSTS & UI2C_PROTSTS_ACKIF_Msk;
391 /* Check Acknowledge Interrupt Flag */
392 if((u32Mask & UI2C_ACK_INT_MASK) && (u32TmpValue))
393 {
394 u32IntFlag |= UI2C_ACK_INT_MASK;
395 }
396
397 return u32IntFlag;
398}
399
418void UI2C_ClearIntFlag(UI2C_T *ui2c, uint32_t u32Mask)
419{
420 /* Clear Time-out Interrupt Flag */
421 if(u32Mask & UI2C_TO_INT_MASK)
422 {
424 }
425
426 /* Clear Start Condition Received Interrupt Flag */
427 if(u32Mask & UI2C_STAR_INT_MASK)
428 {
430 }
431
432 /* Clear Stop Condition Received Interrupt Flag */
433 if(u32Mask & UI2C_STOR_INT_MASK)
434 {
436 }
437
438 /* Clear Non-Acknowledge Interrupt Flag */
439 if(u32Mask & UI2C_NACK_INT_MASK)
440 {
442 }
443
444 /* Clear Arbitration Lost Interrupt Flag */
445 if(u32Mask & UI2C_ARBLO_INT_MASK)
446 {
448 }
449
450 /* Clear Error Interrupt Flag */
451 if(u32Mask & UI2C_ERR_INT_MASK)
452 {
454 }
455
456 /* Clear Acknowledge Interrupt Flag */
457 if(u32Mask & UI2C_ACK_INT_MASK)
458 {
460 }
461}
462
472uint32_t UI2C_GetData(UI2C_T *ui2c)
473{
474 return ( ui2c->RXDAT );
475}
476
487void UI2C_SetData(UI2C_T *ui2c, uint8_t u8Data)
488{
489 ui2c->TXDAT = u8Data;
490}
491
506void UI2C_SetSlaveAddr(UI2C_T *ui2c, uint8_t u8SlaveNo, uint16_t u16SlaveAddr, uint8_t u8GCMode)
507{
508 if(u8SlaveNo)
509 {
510 ui2c->DEVADDR1 = u16SlaveAddr;
511 }
512 else
513 {
514 ui2c->DEVADDR0 = u16SlaveAddr;
515 }
516
517 ui2c->PROTCTL = (ui2c->PROTCTL & ~UI2C_PROTCTL_GCFUNC_Msk) |u8GCMode;
518}
519
532void UI2C_SetSlaveAddrMask(UI2C_T *ui2c, uint8_t u8SlaveNo, uint16_t u16SlaveAddrMask)
533{
534 if(u8SlaveNo)
535 {
536 ui2c->ADDRMSK1 = u16SlaveAddrMask;
537 }
538 else
539 {
540 ui2c->ADDRMSK0 = u16SlaveAddrMask;
541 }
542}
543
554void UI2C_EnableTimeout(UI2C_T *ui2c, uint32_t u32TimeoutCnt)
555{
556 ui2c->PROTCTL = (ui2c->PROTCTL & ~UI2C_PROTCTL_TOCNT_Msk) | (u32TimeoutCnt << UI2C_PROTCTL_TOCNT_Pos);
557 ui2c->BRGEN = (ui2c->BRGEN & ~UI2C_BRGEN_TMCNTSRC_Msk) | UI2C_BRGEN_TMCNTEN_Msk;
558}
559
570{
571 ui2c->PROTCTL &= ~UI2C_PROTCTL_TOCNT_Msk;
572 ui2c->BRGEN &= ~UI2C_BRGEN_TMCNTEN_Msk;
573}
574
587void UI2C_EnableWakeup(UI2C_T *ui2c, uint8_t u8WakeupMode)
588{
589 ui2c->WKCTL = (ui2c->WKCTL & ~UI2C_WKCTL_WKADDREN_Msk) | (u8WakeupMode | UI2C_WKCTL_WKEN_Msk);
590}
591
602{
603 ui2c->WKCTL &= ~UI2C_WKCTL_WKEN_Msk;
604}
605
620uint8_t UI2C_WriteByte(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t data)
621{
622 uint8_t u8Xfering = 1U, u8Err = 0U, u8Ctrl = 0U;
624
625 UI2C_START(ui2c); /* Send START */
626
627 while (u8Xfering)
628 {
629 while (!(UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)); /* Wait UI2C new status occur */
630
631 switch (UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)
632 {
634 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STARIF_Msk); /* Clear START INT Flag */
635 UI2C_SET_DATA(ui2c, (u8SlaveAddr << 1U) | 0x00U); /* Write SLA+W to Register UI2C_TXDAT */
636 eEvent = MASTER_SEND_ADDRESS;
637 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
638 break;
639
641 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_ACKIF_Msk); /* Clear ACK INT Flag */
642
643 if (eEvent == MASTER_SEND_ADDRESS)
644 {
645 UI2C_SET_DATA(ui2c, data); /* Write data to UI2C_TXDAT */
646 eEvent = MASTER_SEND_DATA;
647 }
648 else
649 {
650 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
651 }
652
653 break;
654
656 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_NACKIF_Msk); /* Clear NACK INT Flag */
657 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
658 u8Err = 1U;
659 break;
660
662 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STORIF_Msk); /* Clear STOP INT Flag */
663 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
664 u8Xfering = 0U;
665 break;
666
667 case UI2C_PROTSTS_ARBLOIF_Msk: /* Arbitration Lost */
668 default: /* Unknow status */
669 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
670 u8Err = 1U;
671 break;
672 }
673
674 UI2C_SET_CONTROL_REG(ui2c, u8Ctrl); /* Write controlbit to UI2C_PROTCTL register */
675 }
676
677 return (u8Err | u8Xfering); /* return (Success)/(Fail) status */
678}
679
694uint32_t UI2C_WriteMultiBytes(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t *data, uint32_t u32wLen)
695{
696 uint8_t u8Xfering = 1U, u8Ctrl = 0U;
697 uint32_t u32txLen = 0U;
698
699 UI2C_START(ui2c); /* Send START */
700
701 while (u8Xfering)
702 {
703 while (!(UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)); /* Wait UI2C new status occur */
704
705 switch (UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)
706 {
708 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STARIF_Msk); /* Clear START INT Flag */
709 UI2C_SET_DATA(ui2c, (u8SlaveAddr << 1U) | 0x00U); /* Write SLA+W to Register UI2C_TXDAT */
710 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
711 break;
712
714 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_ACKIF_Msk); /* Clear ACK INT Flag */
715
716 if (u32txLen < u32wLen)
717 UI2C_SET_DATA(ui2c, data[u32txLen++]); /* Write data to UI2C_TXDAT */
718 else
719 {
720 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
721 }
722
723 break;
724
726 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_NACKIF_Msk); /* Clear NACK INT Flag */
727 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
728 break;
729
731 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STORIF_Msk); /* Clear STOP INT Flag */
732 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
733 u8Xfering = 0U;
734 break;
735
736 case UI2C_PROTSTS_ARBLOIF_Msk: /* Arbitration Lost */
737 default: /* Unknow status */
738 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
739 break;
740 }
741
742 UI2C_SET_CONTROL_REG(ui2c, u8Ctrl); /* Write controlbit to UI2C_CTL register */
743 }
744
745 return u32txLen; /* Return bytes length that have been transmitted */
746}
747
763uint8_t UI2C_WriteByteOneReg(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t data)
764{
765 uint8_t u8Xfering = 1U, u8Err = 0U, u8Ctrl = 0U;
766 uint32_t u32txLen = 0U;
767
768 UI2C_START(ui2c); /* Send START */
769
770 while (u8Xfering)
771 {
772 while (!(UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)); /* Wait UI2C new status occur */
773
774 switch (UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)
775 {
777 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STARIF_Msk); /* Clear START INT Flag */
778 UI2C_SET_DATA(ui2c, (u8SlaveAddr << 1U) | 0x00U); /* Write SLA+W to Register UI2C_TXDAT */
779 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
780 break;
781
783 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_ACKIF_Msk); /* Clear ACK INT Flag */
784
785 if (u32txLen == 0U)
786 {
787 UI2C_SET_DATA(ui2c, u8DataAddr); /* Write data address to UI2C_TXDAT */
788 u32txLen++;
789 }
790 else if (u32txLen == 1U)
791 {
792 UI2C_SET_DATA(ui2c, data); /* Write data to UI2C_TXDAT */
793 u32txLen++;
794 }
795 else
796 {
797 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
798 }
799
800 break;
801
803 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_NACKIF_Msk); /* Clear NACK INT Flag */
804 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
805 u8Err = 1U;
806 break;
807
809 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STORIF_Msk); /* Clear STOP INT Flag */
810 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
811 u8Xfering = 0U;
812 break;
813
814 case UI2C_PROTSTS_ARBLOIF_Msk: /* Arbitration Lost */
815 default: /* Unknow status */
816 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
817 u8Err = 1U;
818 break;
819 }
820
821 UI2C_SET_CONTROL_REG(ui2c, u8Ctrl); /* Write controlbit to UI2C_CTL register */
822 }
823
824 return (u8Err | u8Xfering); /* return (Success)/(Fail) status */
825}
826
827
843uint32_t UI2C_WriteMultiBytesOneReg(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t *data, uint32_t u32wLen)
844{
845 uint8_t u8Xfering = 1U, u8Ctrl = 0U;
846 uint32_t u32txLen = 0U;
848
849 UI2C_START(ui2c); /* Send START */
850
851 while (u8Xfering)
852 {
853 while (!(UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)); /* Wait UI2C new status occur */
854
855 switch (UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)
856 {
858 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STARIF_Msk); /* Clear START INT Flag */
859 UI2C_SET_DATA(ui2c, (u8SlaveAddr << 1U) | 0x00U); /* Write SLA+W to Register UI2C_TXDAT */
860 eEvent = MASTER_SEND_ADDRESS;
861 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
862 break;
863
865 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_ACKIF_Msk); /* Clear ACK INT Flag */
866
867 if (eEvent == MASTER_SEND_ADDRESS)
868 {
869 UI2C_SET_DATA(ui2c, u8DataAddr); /* Write data address to UI2C_TXDAT */
870 eEvent = MASTER_SEND_DATA;
871 }
872 else
873 {
874 if (u32txLen < u32wLen)
875 UI2C_SET_DATA(ui2c, data[u32txLen++]); /* Write data to UI2C_TXDAT */
876 else
877 {
878 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
879 }
880 }
881
882 break;
883
885 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_NACKIF_Msk); /* Clear NACK INT Flag */
886 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
887 break;
888
890 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STORIF_Msk); /* Clear STOP INT Flag */
891 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
892 u8Xfering = 0U;
893 break;
894
895 case UI2C_PROTSTS_ARBLOIF_Msk: /* Arbitration Lost */
896 default: /* Unknow status */
897 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
898 break;
899 }
900
901 UI2C_SET_CONTROL_REG(ui2c, u8Ctrl); /* Write controlbit to UI2C_CTL register */
902 }
903
904 return u32txLen; /* Return bytes length that have been transmitted */
905}
906
922uint8_t UI2C_WriteByteTwoRegs(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t data)
923{
924 uint8_t u8Xfering = 1U, u8Err = 0U, u8Ctrl = 0U;
925 uint32_t u32txLen = 0U;
926
927 UI2C_START(ui2c); /* Send START */
928
929 while (u8Xfering)
930 {
931 while (!(UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)); /* Wait UI2C new status occur */
932
933 switch (UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)
934 {
936 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STARIF_Msk); /* Clear START INT Flag */
937 UI2C_SET_DATA(ui2c, (u8SlaveAddr << 1U) | 0x00U); /* Write SLA+W to Register UI2C_TXDAT */
938 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
939 break;
940
942 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_ACKIF_Msk); /* Clear ACK INT Flag */
943
944 if (u32txLen == 0U)
945 {
946 UI2C_SET_DATA(ui2c, (uint8_t)((u16DataAddr & 0xFF00U) >> 8U)); /* Write Hi byte data address to UI2C_TXDAT */
947 u32txLen++;
948 }
949 else if (u32txLen == 1U)
950 {
951 UI2C_SET_DATA(ui2c, (uint8_t)(u16DataAddr & 0xFFU)); /* Write Lo byte data address to UI2C_TXDAT */
952 u32txLen++;
953 }
954 else if (u32txLen == 2U)
955 {
956 UI2C_SET_DATA(ui2c, data); /* Write data to UI2C_TXDAT */
957 u32txLen++;
958 }
959 else
960 {
961 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
962 }
963
964 break;
965
967 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_NACKIF_Msk); /* Clear NACK INT Flag */
968 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
969 u8Err = 1U;
970 break;
971
973 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STORIF_Msk); /* Clear STOP INT Flag */
974 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
975 u8Xfering = 0U;
976 break;
977
978 case UI2C_PROTSTS_ARBLOIF_Msk: /* Arbitration Lost */
979 default: /* Unknow status */
980 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
981 u8Err = 1U;
982 break;
983 }
984
985 UI2C_SET_CONTROL_REG(ui2c, u8Ctrl); /* Write controlbit to UI2C_CTL register */
986 }
987
988 return (u8Err | u8Xfering);
989}
990
991
1007uint32_t UI2C_WriteMultiBytesTwoRegs(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t *data, uint32_t u32wLen)
1008{
1009 uint8_t u8Xfering = 1U, u8Addr = 1U, u8Ctrl = 0U;
1010 uint32_t u32txLen = 0U;
1012
1013 UI2C_START(ui2c); /* Send START */
1014
1015 while (u8Xfering)
1016 {
1017 while (!(UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)); /* Wait UI2C new status occur */
1018
1019 switch (UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)
1020 {
1022 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STARIF_Msk); /* Clear START INT Flag */
1023 UI2C_SET_DATA(ui2c, (u8SlaveAddr << 1U) | 0x00U); /* Write SLA+W to Register UI2C_TXDAT */
1024 eEvent = MASTER_SEND_ADDRESS;
1025 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
1026 break;
1027
1029 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_ACKIF_Msk); /* Clear ACK INT Flag */
1030
1031 if (eEvent == MASTER_SEND_ADDRESS)
1032 {
1033 UI2C_SET_DATA(ui2c, (uint8_t)((u16DataAddr & 0xFF00U) >> 8U)); /* Write Hi byte data address to UI2C_TXDAT */
1034 eEvent = MASTER_SEND_DATA;
1035 }
1036 else if (eEvent == MASTER_SEND_DATA)
1037 {
1038 if (u8Addr)
1039 {
1040 UI2C_SET_DATA(ui2c, (uint8_t)(u16DataAddr & 0xFFU)); /* Write Lo byte data address to UI2C_TXDAT */
1041 u8Addr = 0;
1042 }
1043 else
1044 {
1045 if (u32txLen < u32wLen)
1046 {
1047 UI2C_SET_DATA(ui2c, data[u32txLen++]); /* Write data to UI2C_TXDAT */
1048 }
1049 else
1050 {
1051 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
1052 }
1053 }
1054 }
1055
1056 break;
1057
1059 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_NACKIF_Msk); /* Clear NACK INT Flag */
1060 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
1061 break;
1062
1064 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STORIF_Msk); /* Clear STOP INT Flag */
1065 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
1066 u8Xfering = 0U;
1067 break;
1068
1069 case UI2C_PROTSTS_ARBLOIF_Msk: /* Arbitration Lost */
1070 default: /* Unknow status */
1071 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
1072 break;
1073 }
1074
1075 UI2C_SET_CONTROL_REG(ui2c, u8Ctrl); /* Write controlbit to UI2C_CTL register */
1076 }
1077
1078 return u32txLen; /* Return bytes length that have been transmitted */
1079}
1080
1092uint8_t UI2C_ReadByte(UI2C_T *ui2c, uint8_t u8SlaveAddr)
1093{
1094 uint8_t u8Xfering = 1U, u8Err = 0U, rdata = 0U, u8Ctrl = 0U;
1096
1097 UI2C_START(ui2c); /* Send START */
1098
1099 while (u8Xfering)
1100 {
1101 while (!(UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)); /* Wait UI2C new status occur */
1102
1103 switch (UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)
1104 {
1106 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STARIF_Msk); /* Clear START INT Flag */
1107 UI2C_SET_DATA(ui2c, (u8SlaveAddr << 1U) | 0x01U); /* Write SLA+R to Register UI2C_TXDAT */
1108 eEvent = MASTER_SEND_H_RD_ADDRESS;
1109 u8Ctrl = UI2C_CTL_PTRG;
1110 break;
1111
1113 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_ACKIF_Msk); /* Clear ACK INT Flag */
1114 eEvent = MASTER_READ_DATA;
1115 break;
1116
1118 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_NACKIF_Msk); /* Clear NACK INT Flag */
1119
1120 if (eEvent == MASTER_SEND_H_RD_ADDRESS)
1121 {
1122 u8Err = 1U;
1123 }
1124 else
1125 {
1126 rdata = (unsigned char) UI2C_GET_DATA(ui2c); /* Receive Data */
1127 }
1128
1129 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
1130
1131 break;
1132
1134 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STORIF_Msk); /* Clear STOP INT Flag */
1135 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
1136 u8Xfering = 0U;
1137 break;
1138
1139 case UI2C_PROTSTS_ARBLOIF_Msk: /* Arbitration Lost */
1140 default: /* Unknow status */
1141 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
1142 u8Err = 1U;
1143 break;
1144 }
1145
1146 UI2C_SET_CONTROL_REG(ui2c, u8Ctrl); /* Write controlbit to UI2C_PROTCTL register */
1147 }
1148
1149 if (u8Err)
1150 rdata = 0U;
1151
1152 return rdata; /* Return read data */
1153}
1154
1155
1170uint32_t UI2C_ReadMultiBytes(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t *rdata, uint32_t u32rLen)
1171{
1172 uint8_t u8Xfering = 1U, u8Ctrl = 0U;
1173 uint32_t u32rxLen = 0U;
1175
1176 UI2C_START(ui2c); /* Send START */
1177
1178 while (u8Xfering)
1179 {
1180 while (!(UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)); /* Wait UI2C new status occur */
1181
1182 switch (UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)
1183 {
1185 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STARIF_Msk); /* Clear START INT Flag */
1186 UI2C_SET_DATA(ui2c, (u8SlaveAddr << 1U) | 0x01U); /* Write SLA+R to Register UI2C_TXDAT */
1187 eEvent = MASTER_SEND_H_RD_ADDRESS;
1188 u8Ctrl = UI2C_CTL_PTRG;
1189 break;
1190
1192 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_ACKIF_Msk); /* Clear ACK INT Flag */
1193
1194 if (eEvent == MASTER_SEND_H_RD_ADDRESS)
1195 {
1196 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_AA);
1197 eEvent = MASTER_READ_DATA;
1198 }
1199 else
1200 {
1201 rdata[u32rxLen++] = (unsigned char) UI2C_GET_DATA(ui2c); /* Receive Data */
1202
1203 if (u32rxLen < (u32rLen - 1U))
1204 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_AA);
1205 else
1206 u8Ctrl = UI2C_CTL_PTRG;
1207 }
1208
1209 break;
1210
1212 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_NACKIF_Msk); /* Clear NACK INT Flag */
1213
1214 if (eEvent == MASTER_READ_DATA)
1215 rdata[u32rxLen++] = (unsigned char) UI2C_GET_DATA(ui2c); /* Receive Data */
1216
1217 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
1218
1219 break;
1220
1222 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STORIF_Msk); /* Clear STOP INT Flag */
1223 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
1224 u8Xfering = 0U;
1225 break;
1226
1227 case UI2C_PROTSTS_ARBLOIF_Msk: /* Arbitration Lost */
1228 default: /* Unknow status */
1229 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
1230 break;
1231 }
1232
1233 UI2C_SET_CONTROL_REG(ui2c, u8Ctrl); /* Write controlbit to UI2C_PROTCTL register */
1234 }
1235
1236 return u32rxLen; /* Return bytes length that have been received */
1237}
1238
1239
1253uint8_t UI2C_ReadByteOneReg(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr)
1254{
1255 uint8_t u8Xfering = 1U, u8Err = 0U, rdata = 0U, u8Ctrl = 0U;
1257
1258 UI2C_START(ui2c); /* Send START */
1259
1260 while (u8Xfering)
1261 {
1262 while (!(UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)); /* Wait UI2C new status occur */
1263
1264 switch (UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)
1265 {
1267 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STARIF_Msk); /* Clear START INT Flag */
1268
1269 if (eEvent == MASTER_SEND_START)
1270 {
1271 UI2C_SET_DATA(ui2c, (u8SlaveAddr << 1U) | 0x00U); /* Write SLA+W to Register UI2C_TXDAT */
1272 eEvent = MASTER_SEND_ADDRESS;
1273 }
1274 else if (eEvent == MASTER_SEND_REPEAT_START)
1275 {
1276 UI2C_SET_DATA(ui2c, (u8SlaveAddr << 1U) | 0x01U); /* Write SLA+R to Register TXDAT */
1277 eEvent = MASTER_SEND_H_RD_ADDRESS;
1278 }
1279
1280 u8Ctrl = UI2C_CTL_PTRG;
1281 break;
1282
1284 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_ACKIF_Msk); /* Clear ACK INT Flag */
1285
1286 if (eEvent == MASTER_SEND_ADDRESS)
1287 {
1288 UI2C_SET_DATA(ui2c, u8DataAddr); /* Write data address of register */
1289 u8Ctrl = UI2C_CTL_PTRG;
1290 eEvent = MASTER_SEND_DATA;
1291 }
1292 else if (eEvent == MASTER_SEND_DATA)
1293 {
1294 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STA); /* Send repeat START signal */
1295 eEvent = MASTER_SEND_REPEAT_START;
1296 }
1297 else
1298 {
1299 /* SLA+R ACK */
1300 u8Ctrl = UI2C_CTL_PTRG;
1301 eEvent = MASTER_READ_DATA;
1302 }
1303
1304 break;
1305
1307 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_NACKIF_Msk); /* Clear NACK INT Flag */
1308
1309 if (eEvent == MASTER_READ_DATA)
1310 {
1311 rdata = (uint8_t) UI2C_GET_DATA(ui2c); /* Receive Data */
1312 }
1313 else
1314 {
1315 u8Err = 1U;
1316 }
1317
1318 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
1319
1320 break;
1321
1323 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STORIF_Msk); /* Clear STOP INT Flag */
1324 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
1325 u8Xfering = 0U;
1326 break;
1327
1328 case UI2C_PROTSTS_ARBLOIF_Msk: /* Arbitration Lost */
1329 default: /* Unknow status */
1330 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
1331 u8Err = 1U;
1332 break;
1333 }
1334
1335 UI2C_SET_CONTROL_REG(ui2c, u8Ctrl); /* Write controlbit to UI2C_PROTCTL register */
1336 }
1337
1338 if (u8Err)
1339 rdata = 0U; /* If occurs error, return 0 */
1340
1341 return rdata; /* Return read data */
1342}
1343
1359uint32_t UI2C_ReadMultiBytesOneReg(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t *rdata, uint32_t u32rLen)
1360{
1361 uint8_t u8Xfering = 1U, u8Ctrl = 0U;
1362 uint32_t u32rxLen = 0U;
1364
1365 UI2C_START(ui2c); /* Send START */
1366
1367 while (u8Xfering)
1368 {
1369 while (!(UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)); /* Wait UI2C new status occur */
1370
1371 switch (UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)
1372 {
1374 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STARIF_Msk); /* Clear START INT Flag */
1375
1376 if (eEvent == MASTER_SEND_START)
1377 {
1378 UI2C_SET_DATA(ui2c, (u8SlaveAddr << 1U) | 0x00U); /* Write SLA+W to Register UI2C_TXDAT */
1379 eEvent = MASTER_SEND_ADDRESS;
1380 }
1381 else if (eEvent == MASTER_SEND_REPEAT_START)
1382 {
1383 UI2C_SET_DATA(ui2c, (u8SlaveAddr << 1U) | 0x01U); /* Write SLA+R to Register TXDAT */
1384 eEvent = MASTER_SEND_H_RD_ADDRESS;
1385 }
1386
1387 u8Ctrl = UI2C_CTL_PTRG;
1388 break;
1389
1391 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_ACKIF_Msk); /* Clear ACK INT Flag */
1392
1393 if (eEvent == MASTER_SEND_ADDRESS)
1394 {
1395 UI2C_SET_DATA(ui2c, u8DataAddr); /* Write data address of register */
1396 u8Ctrl = UI2C_CTL_PTRG;
1397 eEvent = MASTER_SEND_DATA;
1398 }
1399 else if (eEvent == MASTER_SEND_DATA)
1400 {
1401 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STA); /* Send repeat START signal */
1402 eEvent = MASTER_SEND_REPEAT_START;
1403 }
1404 else if (eEvent == MASTER_SEND_H_RD_ADDRESS)
1405 {
1406 /* SLA+R ACK */
1407 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_AA);
1408 eEvent = MASTER_READ_DATA;
1409 }
1410 else
1411 {
1412 rdata[u32rxLen++] = (uint8_t) UI2C_GET_DATA(ui2c); /* Receive Data */
1413
1414 if (u32rxLen < u32rLen - 1U)
1415 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_AA);
1416 else
1417 u8Ctrl = UI2C_CTL_PTRG;
1418 }
1419
1420 break;
1421
1423 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_NACKIF_Msk); /* Clear NACK INT Flag */
1424
1425 if (eEvent == MASTER_READ_DATA)
1426 rdata[u32rxLen++] = (uint8_t) UI2C_GET_DATA(ui2c); /* Receive Data */
1427
1428 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
1429
1430 break;
1431
1433 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STORIF_Msk); /* Clear STOP INT Flag */
1434 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
1435 u8Xfering = 0U;
1436 break;
1437
1438 case UI2C_PROTSTS_ARBLOIF_Msk: /* Arbitration Lost */
1439 default: /* Unknow status */
1440 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
1441 break;
1442 }
1443
1444 UI2C_SET_CONTROL_REG(ui2c, u8Ctrl); /* Write controlbit to UI2C_PROTCTL register */
1445 }
1446
1447 return u32rxLen; /* Return bytes length that have been received */
1448}
1449
1463uint8_t UI2C_ReadByteTwoRegs(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr)
1464{
1465 uint8_t u8Xfering = 1U, u8Err = 0U, rdata = 0U, u8Addr = 1U, u8Ctrl = 0U;
1467
1468 UI2C_START(ui2c); /* Send START */
1469
1470 while (u8Xfering)
1471 {
1472 while (!(UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)); /* Wait UI2C new status occur */
1473
1474 switch (UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)
1475 {
1477 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STARIF_Msk); /* Clear START INT Flag */
1478
1479 if (eEvent == MASTER_SEND_START)
1480 {
1481 UI2C_SET_DATA(ui2c, (u8SlaveAddr << 1U) | 0x00U); /* Write SLA+W to Register UI2C_TXDAT */
1482 eEvent = MASTER_SEND_ADDRESS;
1483 }
1484 else if (eEvent == MASTER_SEND_REPEAT_START)
1485 {
1486 UI2C_SET_DATA(ui2c, (u8SlaveAddr << 1U) | 0x01U); /* Write SLA+R to Register TXDAT */
1487 eEvent = MASTER_SEND_H_RD_ADDRESS;
1488 }
1489
1490 u8Ctrl = UI2C_CTL_PTRG;
1491 break;
1492
1494 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_ACKIF_Msk); /* Clear ACK INT Flag */
1495
1496 if (eEvent == MASTER_SEND_ADDRESS)
1497 {
1498 UI2C_SET_DATA(ui2c, (uint8_t)((u16DataAddr & 0xFF00U) >> 8U)); /* Write Hi byte address of register */
1499 eEvent = MASTER_SEND_DATA;
1500 }
1501 else if (eEvent == MASTER_SEND_DATA)
1502 {
1503 if (u8Addr)
1504 {
1505 UI2C_SET_DATA(ui2c, (uint8_t)(u16DataAddr & 0xFFU)); /* Write Lo byte address of register */
1506 u8Addr = 0;
1507 }
1508 else
1509 {
1510 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STA); /* Send repeat START signal */
1511 eEvent = MASTER_SEND_REPEAT_START;
1512 }
1513 }
1514 else
1515 {
1516 /* SLA+R ACK */
1517 u8Ctrl = UI2C_CTL_PTRG;
1518 eEvent = MASTER_READ_DATA;
1519 }
1520
1521 break;
1522
1524 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_NACKIF_Msk); /* Clear NACK INT Flag */
1525
1526 if (eEvent == MASTER_READ_DATA)
1527 {
1528 rdata = (uint8_t) UI2C_GET_DATA(ui2c); /* Receive Data */
1529 }
1530 else
1531 {
1532 u8Err = 1U;
1533 }
1534
1535 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
1536
1537 break;
1538
1540 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STORIF_Msk); /* Clear STOP INT Flag */
1541 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
1542 u8Xfering = 0U;
1543 break;
1544
1545 case UI2C_PROTSTS_ARBLOIF_Msk: /* Arbitration Lost */
1546 default: /* Unknow status */
1547 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
1548 u8Err = 1U;
1549 break;
1550 }
1551
1552 UI2C_SET_CONTROL_REG(ui2c, u8Ctrl); /* Write controlbit to UI2C_PROTCTL register */
1553 }
1554
1555 if (u8Err)
1556 rdata = 0U; /* If occurs error, return 0 */
1557
1558 return rdata; /* Return read data */
1559}
1560
1576uint32_t UI2C_ReadMultiBytesTwoRegs(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t *rdata, uint32_t u32rLen)
1577{
1578 uint8_t u8Xfering = 1U, u8Addr = 1U, u8Ctrl = 0U;
1579 uint32_t u32rxLen = 0U;
1581
1582 UI2C_START(ui2c); /* Send START */
1583
1584 while (u8Xfering)
1585 {
1586 while (!(UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)); /* Wait UI2C new status occur */
1587
1588 switch (UI2C_GET_PROT_STATUS(ui2c) & 0x3F00U)
1589 {
1591 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STARIF_Msk); /* Clear START INT Flag */
1592
1593 if (eEvent == MASTER_SEND_START)
1594 {
1595 UI2C_SET_DATA(ui2c, (u8SlaveAddr << 1U) | 0x00U); /* Write SLA+W to Register UI2C_TXDAT */
1596 eEvent = MASTER_SEND_ADDRESS;
1597 }
1598 else if (eEvent == MASTER_SEND_REPEAT_START)
1599 {
1600 UI2C_SET_DATA(ui2c, (u8SlaveAddr << 1U) | 0x01U); /* Write SLA+R to Register TXDAT */
1601 eEvent = MASTER_SEND_H_RD_ADDRESS;
1602 }
1603
1604 u8Ctrl = UI2C_CTL_PTRG;
1605 break;
1606
1608 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_ACKIF_Msk); /* Clear ACK INT Flag */
1609
1610 if (eEvent == MASTER_SEND_ADDRESS)
1611 {
1612 UI2C_SET_DATA(ui2c, (uint8_t)((u16DataAddr & 0xFF00U) >> 8U)); /* Write Hi byte address of register */
1613 eEvent = MASTER_SEND_DATA;
1614 }
1615 else if (eEvent == MASTER_SEND_DATA)
1616 {
1617 if (u8Addr)
1618 {
1619 UI2C_SET_DATA(ui2c, (uint8_t)(u16DataAddr & 0xFFU)); /* Write Lo byte address of register */
1620 u8Addr = 0;
1621 }
1622 else
1623 {
1624 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STA); /* Send repeat START signal */
1625 eEvent = MASTER_SEND_REPEAT_START;
1626 }
1627 }
1628 else if (eEvent == MASTER_SEND_H_RD_ADDRESS)
1629 {
1630 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_AA);
1631 eEvent = MASTER_READ_DATA;
1632 }
1633 else
1634 {
1635 rdata[u32rxLen++] = (uint8_t) UI2C_GET_DATA(ui2c); /* Receive Data */
1636
1637 if (u32rxLen < u32rLen - 1U)
1638 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_AA);
1639 else
1640 u8Ctrl = UI2C_CTL_PTRG;
1641 }
1642
1643 break;
1644
1646 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_NACKIF_Msk); /* Clear NACK INT Flag */
1647
1648 if (eEvent == MASTER_READ_DATA)
1649 rdata[u32rxLen++] = (uint8_t) UI2C_GET_DATA(ui2c); /* Receive Data */
1650
1651 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
1652
1653 break;
1654
1656 UI2C_CLR_PROT_INT_FLAG(ui2c, UI2C_PROTSTS_STORIF_Msk); /* Clear STOP INT Flag */
1657 u8Ctrl = UI2C_CTL_PTRG; /* Clear SI */
1658 u8Xfering = 0U;
1659 break;
1660
1661 case UI2C_PROTSTS_ARBLOIF_Msk: /* Arbitration Lost */
1662 default: /* Unknow status */
1663 u8Ctrl = (UI2C_CTL_PTRG | UI2C_CTL_STO); /* Clear SI and send STOP */
1664 break;
1665 }
1666
1667 UI2C_SET_CONTROL_REG(ui2c, u8Ctrl); /* Write controlbit to UI2C_PROTCTL register */
1668 }
1669
1670 return u32rxLen; /* Return bytes length that have been received */
1671}
1672 /* end of group USCI_I2C_EXPORTED_FUNCTIONS */
1674 /* end of group USCI_I2C_Driver */
1676 /* end of group Standard_Driver */
1678
1679/*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
NuMicro peripheral access layer header file.
uint32_t CLK_GetPCLK1Freq(void)
Get PCLK1 frequency.
Definition: clk.c:206
uint32_t CLK_GetPCLK0Freq(void)
Get PCLK0 frequency.
Definition: clk.c:166
#define UI2C0
Definition: M480.h:454
#define UI2C_LINECTL_DWIDTH_Pos
Definition: ui2c_reg.h:886
#define UI2C_BRGEN_TMCNTEN_Msk
Definition: ui2c_reg.h:869
#define UI2C_BRGEN_CLKDIV_Msk
Definition: ui2c_reg.h:881
#define UI2C_PROTIEN_STARIEN_Msk
Definition: ui2c_reg.h:950
#define UI2C_PROTSTS_STARIF_Msk
Definition: ui2c_reg.h:974
#define UI2C_PROTCTL_STA_Msk
Definition: ui2c_reg.h:926
#define UI2C_PROTSTS_NACKIF_Msk
Definition: ui2c_reg.h:980
#define UI2C_PROTSTS_ARBLOIF_Msk
Definition: ui2c_reg.h:983
#define UI2C_PROTCTL_STO_Msk
Definition: ui2c_reg.h:923
#define UI2C_PROTIEN_ACKIEN_Msk
Definition: ui2c_reg.h:965
#define UI2C_WKCTL_WKEN_Msk
Definition: ui2c_reg.h:908
#define UI2C_BRGEN_CLKDIV_Pos
Definition: ui2c_reg.h:880
#define UI2C_PROTIEN_TOIEN_Msk
Definition: ui2c_reg.h:947
#define UI2C_PROTSTS_ACKIF_Msk
Definition: ui2c_reg.h:989
#define UI2C_PROTCTL_PTRG_Msk
Definition: ui2c_reg.h:932
#define UI2C_PROTIEN_ARBLOIEN_Msk
Definition: ui2c_reg.h:959
#define UI2C_PROTIEN_STORIEN_Msk
Definition: ui2c_reg.h:953
#define UI2C_PROTSTS_TOIF_Msk
Definition: ui2c_reg.h:968
#define UI2C_PROTCTL_AA_Msk
Definition: ui2c_reg.h:920
#define UI2C_PROTCTL_PROTEN_Msk
Definition: ui2c_reg.h:944
#define UI2C_PROTIEN_NACKIEN_Msk
Definition: ui2c_reg.h:956
#define UI2C_PROTSTS_ERRIF_Msk
Definition: ui2c_reg.h:986
#define UI2C_PROTIEN_ERRIEN_Msk
Definition: ui2c_reg.h:962
#define UI2C_PROTSTS_STORIF_Msk
Definition: ui2c_reg.h:977
#define UI2C_CTL_FUNMODE_Pos
Definition: ui2c_reg.h:856
#define UI2C_PROTCTL_TOCNT_Pos
Definition: ui2c_reg.h:940
#define UI2C_TO_INT_MASK
Definition: usci_i2c.h:83
#define UI2C_NACK_INT_MASK
Definition: usci_i2c.h:86
#define UI2C_CTL_AA
Definition: usci_i2c.h:66
#define UI2C_ACK_INT_MASK
Definition: usci_i2c.h:89
#define UI2C_STAR_INT_MASK
Definition: usci_i2c.h:84
#define UI2C_ERR_INT_MASK
Definition: usci_i2c.h:88
#define UI2C_CTL_STO
Definition: usci_i2c.h:65
UI2C_MASTER_EVENT
Definition: usci_i2c.h:34
#define UI2C_CTL_STA
Definition: usci_i2c.h:64
#define UI2C_STOR_INT_MASK
Definition: usci_i2c.h:85
#define UI2C_CTL_PTRG
Definition: usci_i2c.h:63
#define UI2C_ARBLO_INT_MASK
Definition: usci_i2c.h:87
@ MASTER_SEND_ADDRESS
Definition: usci_i2c.h:35
@ MASTER_SEND_START
Definition: usci_i2c.h:43
@ MASTER_SEND_H_RD_ADDRESS
Definition: usci_i2c.h:37
@ MASTER_READ_DATA
Definition: usci_i2c.h:41
@ MASTER_SEND_DATA
Definition: usci_i2c.h:39
@ MASTER_SEND_REPEAT_START
Definition: usci_i2c.h:40
void UI2C_EnableInt(UI2C_T *ui2c, uint32_t u32Mask)
This function enables the interrupt of USCI_I2C module.
Definition: usci_i2c.c:218
#define UI2C_START(ui2c)
This macro only set START bit to protocol control register of USCI_I2C module.
Definition: usci_i2c.h:121
uint32_t UI2C_GetIntFlag(UI2C_T *ui2c, uint32_t u32Mask)
This function gets the interrupt flag of USCI_I2C module.
Definition: usci_i2c.c:343
uint32_t UI2C_Open(UI2C_T *ui2c, uint32_t u32BusClock)
This function makes USCI_I2C module be ready and set the wanted bus clock.
Definition: usci_i2c.c:34
void UI2C_DisableWakeup(UI2C_T *ui2c)
This function disables the wakeup function of USCI_I2C module.
Definition: usci_i2c.c:601
uint32_t UI2C_GetData(UI2C_T *ui2c)
This function returns the data stored in data register of USCI_I2C module.
Definition: usci_i2c.c:472
void UI2C_SetSlaveAddr(UI2C_T *ui2c, uint8_t u8SlaveNo, uint16_t u16SlaveAddr, uint8_t u8GCMode)
Configure slave address and enable GC mode.
Definition: usci_i2c.c:506
uint8_t UI2C_WriteByte(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t data)
Write a byte to Slave.
Definition: usci_i2c.c:620
uint32_t UI2C_WriteMultiBytesTwoRegs(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t *data, uint32_t u32wLen)
Specify two bytes register address and write multi bytes to Slave.
Definition: usci_i2c.c:1007
void UI2C_DisableTimeout(UI2C_T *ui2c)
This function disables time-out function.
Definition: usci_i2c.c:569
uint8_t UI2C_ReadByteTwoRegs(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr)
Specify two bytes register address and read a byte from Slave.
Definition: usci_i2c.c:1463
void UI2C_SetData(UI2C_T *ui2c, uint8_t u8Data)
This function writes a byte data to data register of USCI_I2C module.
Definition: usci_i2c.c:487
uint8_t UI2C_ReadByteOneReg(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr)
Specify a byte register address and read a byte from Slave.
Definition: usci_i2c.c:1253
uint32_t UI2C_WriteMultiBytesOneReg(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t *data, uint32_t u32wLen)
Specify a byte register address and write multi bytes to Slave.
Definition: usci_i2c.c:843
void UI2C_EnableTimeout(UI2C_T *ui2c, uint32_t u32TimeoutCnt)
This function enables time-out function and configures timeout counter.
Definition: usci_i2c.c:554
void UI2C_EnableWakeup(UI2C_T *ui2c, uint8_t u8WakeupMode)
This function enables the wakeup function of USCI_I2C module.
Definition: usci_i2c.c:587
uint32_t UI2C_WriteMultiBytes(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t *data, uint32_t u32wLen)
Write multi bytes to Slave.
Definition: usci_i2c.c:694
void UI2C_DisableInt(UI2C_T *ui2c, uint32_t u32Mask)
This function disables the interrupt of USCI_I2C module.
Definition: usci_i2c.c:156
void UI2C_ClearIntFlag(UI2C_T *ui2c, uint32_t u32Mask)
This function clears the interrupt flag of USCI_I2C module.
Definition: usci_i2c.c:418
uint8_t UI2C_WriteByteTwoRegs(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t data)
Specify two bytes register address and Write a byte to Slave.
Definition: usci_i2c.c:922
uint32_t UI2C_ReadMultiBytesOneReg(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t *rdata, uint32_t u32rLen)
Specify a byte register address and read multi bytes from Slave.
Definition: usci_i2c.c:1359
#define UI2C_GET_PROT_STATUS(ui2c)
This macro gets USCI_I2C protocol interrupt flag or bus status.
Definition: usci_i2c.h:232
#define UI2C_SET_CONTROL_REG(ui2c, u8Ctrl)
This macro sets the USCI_I2C protocol control register at one time.
Definition: usci_i2c.h:109
void UI2C_Trigger(UI2C_T *ui2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Ptrg, uint8_t u8Ack)
This function sets the control bit of the USCI_I2C module.
Definition: usci_i2c.c:112
#define UI2C_CLR_PROT_INT_FLAG(ui2c, u32IntTypeFlag)
This macro clears specified protocol interrupt flag.
Definition: usci_i2c.h:250
uint8_t UI2C_ReadByte(UI2C_T *ui2c, uint8_t u8SlaveAddr)
Read a byte from Slave.
Definition: usci_i2c.c:1092
uint32_t UI2C_GetBusClockFreq(UI2C_T *ui2c)
This function returns the real bus clock of USCI_I2C module.
Definition: usci_i2c.c:272
void UI2C_ClearTimeoutFlag(UI2C_T *ui2c)
This function clears the time-out flag.
Definition: usci_i2c.c:94
uint32_t UI2C_ReadMultiBytesTwoRegs(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t *rdata, uint32_t u32rLen)
Specify two bytes register address and read multi bytes from Slave.
Definition: usci_i2c.c:1576
#define UI2C_SET_DATA(ui2c, u8Data)
This macro writes the data to data register of USCI_I2C module.
Definition: usci_i2c.h:158
void UI2C_Close(UI2C_T *ui2c)
This function closes the USCI_I2C module.
Definition: usci_i2c.c:79
uint8_t UI2C_WriteByteOneReg(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t data)
Specify a byte register address and write a byte to Slave.
Definition: usci_i2c.c:763
#define UI2C_GET_DATA(ui2c)
This macro returns the data stored in data register of USCI_I2C module.
Definition: usci_i2c.h:145
void UI2C_SetSlaveAddrMask(UI2C_T *ui2c, uint8_t u8SlaveNo, uint16_t u16SlaveAddrMask)
Configure the mask bit of slave address.
Definition: usci_i2c.c:532
uint32_t UI2C_SetBusClockFreq(UI2C_T *ui2c, uint32_t u32BusClock)
This function sets bus clock frequency of USCI_I2C module.
Definition: usci_i2c.c:301
uint32_t UI2C_ReadMultiBytes(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t *rdata, uint32_t u32rLen)
Read multi bytes from Slave.
Definition: usci_i2c.c:1170
__IO uint32_t DEVADDR1
Definition: ui2c_reg.h:835
__IO uint32_t PROTSTS
Definition: ui2c_reg.h:842
__IO uint32_t WKCTL
Definition: ui2c_reg.h:838
__IO uint32_t ADDRMSK0
Definition: ui2c_reg.h:836
__I uint32_t RXDAT
Definition: ui2c_reg.h:830
__IO uint32_t PROTCTL
Definition: ui2c_reg.h:840
__IO uint32_t LINECTL
Definition: ui2c_reg.h:828
__IO uint32_t DEVADDR0
Definition: ui2c_reg.h:834
__IO uint32_t BRGEN
Definition: ui2c_reg.h:824
__IO uint32_t ADDRMSK1
Definition: ui2c_reg.h:837
__IO uint32_t CTL
Definition: ui2c_reg.h:820
__IO uint32_t PROTIEN
Definition: ui2c_reg.h:841
__O uint32_t TXDAT
Definition: ui2c_reg.h:829