SMBusLibrary  1.0
smbus.h
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2023, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #ifndef __SMBUS_H__
33 #define __SMBUS_H__
34 
35 //*****************************************************************************
36 //
39 //
40 //*****************************************************************************
41 
42 //*****************************************************************************
43 //
44 // If building with a C++ compiler, make all of the definitions in this header
45 // have a C binding.
46 //
47 //*****************************************************************************
48 #ifdef __cplusplus
49 extern "C"
50 {
51 #endif
52 
53 //
54 // Include files
55 //
56 #include <stdlib.h>
57 #include <stdint.h>
58 #include <stdbool.h>
59 
60 #include <ti/devices/msp/msp.h>
61 #include <ti/driverlib/driverlib.h>
62 #include <ti/driverlib/m0p/dl_core.h>
63 
64 //*****************************************************************************
65 // defines
66 //*****************************************************************************
67 
68 
69 //*****************************************************************************
70 //
76 // FIXME delete when HW PEC is implemented
77 //*****************************************************************************
78 #define SMB_CRC8_USE_LOOKUP_TABLE (1)
79 
80 //*****************************************************************************
81 //
83 //
84 //*****************************************************************************
85 #define SMB_MAX_PAYLOAD_SIZE (255)
86 
87 //*****************************************************************************
88 //
90 //
91 //*****************************************************************************
92 #define SMB_MAX_PACKET_SIZE (SMB_MAX_PAYLOAD_SIZE + 3)
93 
94 //*****************************************************************************
95 //
97 //
98 //*****************************************************************************
99 #define SMB_HOST_ALERT_PACKET_SIZE (3)
100 
101 //*****************************************************************************
102 //
104 //
105 //*****************************************************************************
106 #define SMB_HOST_DEFAULT_ADDRESS (0x08)
107 
108 //*****************************************************************************
109 //
111 //
112 //*****************************************************************************
113 #define RESPONSE_NTR 0x00
114 
115 //*****************************************************************************
116 //
118 //
119 //*****************************************************************************
120 #define SMBUS_RET_OK (1)
121 
122 //*****************************************************************************
123 //
125 //
126 //*****************************************************************************
127 #define SMBUS_RET_ERROR (-1)
128 
129 //*****************************************************************************
130 // typedefs
131 //*****************************************************************************
132 
133 //*****************************************************************************
134 //
136 //
137 //*****************************************************************************
138 typedef enum
139 {
140  SMBus_Stop_After_Transfer = 0,
141  SMBus_No_Stop_After_Transfer
142 } SMBus_Stop;
143 
144 //*****************************************************************************
145 //
147 //
148 //*****************************************************************************
149 typedef enum
150 {
151  SMBus_Start_Before_Transfer = 0,
152  SMBus_No_Start_Before_Transfer
153 } SMBus_Start;
154 
155 //*****************************************************************************
156 //
158 //
159 //*****************************************************************************
160 typedef enum
161 {
162  SMBus_Auto_Ack_Last_Byte = 0,
163  SMBus_No_Auto_Ack_Last_Byte
165 
166 //*****************************************************************************
167 //
169 //
170 //*****************************************************************************
171 typedef union
172 {
173  //*****************************************************************************
174  //
176  //
177  //*****************************************************************************
178  struct
179  {
181  uint8_t pecEn : 1;
183  uint8_t hostNotifyEn : 1;
185  uint8_t swackEn : 1;
187  uint8_t intEn : 1;
189  uint8_t phyEn : 1;
191  uint8_t controller : 1;
193  uint8_t reserved2 : 2;
194  } bits;
195  //*****************************************************************************
196  //
198  //
199  //*****************************************************************************
200  struct
201  {
203  uint8_t writeBits : 2;
205  uint8_t reserved : 6;
206  } writeableBits;
208  uint8_t u8byte;
209 } SMBus_Ctrl;
210 
211 //*****************************************************************************
212 //
214 //
215 //*****************************************************************************
216 typedef struct
217 {
219  I2C_Regs* SMBus_Phy_i2cBase;
221  SMBus_Stop SMBus_Phy_stop;
222 } SMBus_Phy;
223 
224 //*****************************************************************************
225 //
227 //
228 //*****************************************************************************
229 typedef enum
230 {
254 
255 /****************************************************************************** */
256 
257 //
259 //
260 //*****************************************************************************
261 typedef struct
262 {
264  volatile SMBus_NwkState eState;
266  uint8_t currentAddr;
268  uint8_t currentCmd;
270  uint16_t rxIndex;
272  uint16_t rxLen;
274  uint16_t rxSize;
276  uint8_t *rxBuffPtr;
278  uint16_t txIndex;
280  uint16_t txLen;
282  uint8_t *txBuffPtr;
284  uint8_t *recByteTxPtr;
286  uint16_t txSize;
288  uint8_t pec;
291 } SMBus_Nwk;
292 
293 //*****************************************************************************
294 //
296 //
297 //*****************************************************************************
298 typedef enum
299 {
307 
308 //*****************************************************************************
309 //
311 //
312 //*****************************************************************************
313 typedef enum
314 {
347 } SMBus_State;
348 
349 /****************************************************************************** */
350 
351 //
353 //
354 //*****************************************************************************
355 typedef union
356 {
357  //*****************************************************************************
358  //
360  //
361  //*****************************************************************************
362  struct
363  {
365  uint8_t pecErr : 1;
367  uint8_t toErr : 1;
369  uint8_t packErr : 1;
371  uint8_t packOvrErr : 1;
373  uint8_t byteOvrErr : 1;
375  uint8_t cmdErr : 1;
377  uint8_t reserved : 2;
378  } bits;
380  uint8_t u8byte;
381 } SMBus_Status;
382 
383 //*****************************************************************************
384 //
386 //
387 //*****************************************************************************
388 typedef struct
389 {
399  SMBus_State state;
401  uint8_t ownTargetAddr;
402 } SMBus;
403 
404 //*****************************************************************************
405 // globals
406 //*****************************************************************************
407 
408 //*****************************************************************************
409 // Public functions called by applications
410 //*****************************************************************************
411 //*****************************************************************************
412 //
414 //
417 //
419 //
421 //
422 //*****************************************************************************
423 extern void SMBus_processDone(SMBus *smbus);
424 
425 //*****************************************************************************
426 //
428 //
430 //
432 //
433 //*****************************************************************************
434 extern uint16_t SMBus_getRxPayloadAvailable(SMBus *smbus);
435 
436 //*****************************************************************************
437 //
439 //
441 //
443 //
444 //*****************************************************************************
445 extern SMBus_State SMBus_getState(SMBus *smbus);
446 
447 //*****************************************************************************
448 //
450 //
452 //
454 //
455 //*****************************************************************************
456 extern void SMBus_enablePEC(SMBus *smbus);
457 //*****************************************************************************
458 //
460 //
462 //
464 //
465 //*****************************************************************************
466 extern void SMBus_disablePEC(SMBus *smbus);
467 
468 //*****************************************************************************
469 //
471 //
473 //
476 //
478 //
479 //*****************************************************************************
480 extern void SMBus_targetInit(SMBus *smbus, I2C_Regs *i2cAddr);
481 
482 //*****************************************************************************
483 //
485 //
487 //
489 //
491 //
492 //*****************************************************************************
493 extern void SMBus_targetEnableInt(SMBus *smbus);
494 
495 //*****************************************************************************
496 //
498 //
501 //
503 //
513 //
514 //*****************************************************************************
515 extern SMBus_State SMBus_targetProcessInt(SMBus *smbus);
516 
517 //*****************************************************************************
518 //
520 //
522 //
524 //
526 //
527 //*****************************************************************************
528 extern void SMBus_controllerReset(SMBus *smbus);
529 
530 //*****************************************************************************
531 //
533 //
536 //
538 //
539 //*****************************************************************************
540 extern void SMBus_targetSetAddress(SMBus *smbus,
541  uint8_t targetAddr);
542 
543 //*****************************************************************************
544 //
546 //
550 //
552 //
553 //*****************************************************************************
554 extern void SMBus_targetSetRxBuffer(SMBus *smbus,
555  uint8_t *data,
556  uint16_t size);
557 
558 //*****************************************************************************
559 //
561 //
565 //
567 //
568 //*****************************************************************************
569 extern void SMBus_targetSetTxBuffer(SMBus *smbus,
570  uint8_t *data,
571  uint16_t size);
572 
573 //*****************************************************************************
574 //
576 //
578 //
581 //
583 //
584 //*****************************************************************************
585 extern void SMBus_targetReportError(SMBus *smbus,
586  SMBus_ErrorCode errorCode);
587 
588 //*****************************************************************************
589 //
591 //
593 //
595 //
596 //*****************************************************************************
597 extern uint8_t SMBus_targetGetCommand(SMBus *smbus);
598 
599 //*****************************************************************************
600 //
602 //
605 //
607 //
608 //*****************************************************************************
609 extern uint8_t SMBus_targetClearStatusReg(SMBus *smbus,
610  uint8_t val);
611 
612 //*****************************************************************************
613 //
615 //
618 //
620 //
621 //*****************************************************************************
622 extern uint8_t SMBus_targetWriteCtrlReg(SMBus *smbus,
623  uint8_t val);
624 
625 //*****************************************************************************
626 //
628 //
648 //
652 //
654 //
655 //*****************************************************************************
656 extern int8_t SMBus_targetHostAlert(SMBus *smbus,
657  uint8_t deviceAddress,
658  uint8_t *txData);
659 
660 //*****************************************************************************
661 //
663 //
665 //
669 //
670 // \return None
671 //
672 //*****************************************************************************
673 extern void SMBus_controllerInit(SMBus *smbus,
674  I2C_Regs *i2cAddr,
675  uint32_t busClk);
676 
677 //*****************************************************************************
678 //
680 //
683 //
685 //
687 //
688 //*****************************************************************************
689 extern void SMBus_controllerEnableInt(SMBus *smbus);
690 
691 //*****************************************************************************
692 //
694 //
697 //
699 //
701 //
702 //*****************************************************************************
703 extern SMBus_State SMBus_controllerProcessInt(SMBus *smbus);
704 
705 
706 //*****************************************************************************
707 //
709 //
746 //
752 //
754 //
755 //*****************************************************************************
756 extern int8_t SMBus_controllerProcessCall(SMBus *smbus,
757  uint8_t targetAddr,
758  uint8_t command,
759  uint8_t *txData,
760  uint8_t *rxData);
761 
762 //*****************************************************************************
763 //
765 //
769 //
821 //
828 //
830 //
831 //*****************************************************************************
832 extern int8_t SMBus_controllerProcessCallBlock(SMBus *smbus,
833  uint8_t targetAddr,
834  uint8_t command,
835  uint8_t *txData,
836  uint8_t txSize,
837  uint8_t *rxData);
838 
839 //*****************************************************************************
840 //
842 //
866 //
870 //
872 //
873 //*****************************************************************************
874 extern int8_t SMBus_controllerSendByte(SMBus *smbus,
875  uint8_t targetAddr,
876  uint8_t txData);
877 
878 //*****************************************************************************
879 //
881 //
904 //
908 //
910 //
911 //*****************************************************************************
912 extern int8_t SMBus_controllerReceiveByte(SMBus *smbus,
913  uint8_t targetAddr,
914  uint8_t *rxData);
915 
916 //*****************************************************************************
917 //
919 //
923 //
963 //
968 //
970 //
971 //*****************************************************************************
972 extern int8_t SMBus_controllerReadBlock(SMBus *smbus,
973  uint8_t targetAddr,
974  uint8_t command,
975  uint8_t *rxData);
976 
977 //*****************************************************************************
978 //
980 //
983 //
1017 //
1023 //
1025 //
1026 //*****************************************************************************
1027 extern int8_t SMBus_controllerWriteBlock(SMBus *smbus,
1028  uint8_t targetAddr,
1029  uint8_t command,
1030  uint8_t *txData,
1031  uint16_t txSize);
1032 
1033 //*****************************************************************************
1034 //
1036 //
1037 //
1086 //
1092 //
1094 //
1095 //*****************************************************************************
1096 extern int8_t SMBus_controllerReadByteWord(SMBus *smbus,
1097  uint8_t targetAddr,
1098  uint8_t command,
1099  uint8_t *rxData,
1100  uint8_t rxSize);
1101 
1102 //*****************************************************************************
1103 //
1141 //
1147 //
1149 //
1150 //*****************************************************************************
1151 extern int8_t SMBus_controllerWriteByteWord(SMBus *smbus,
1152  uint8_t targetAddr,
1153  uint8_t command,
1154  uint8_t *txData,
1155  uint8_t txSize);
1156 
1157 //*****************************************************************************
1158 //
1160 //
1176 //
1180 //
1182 //
1183 //*****************************************************************************
1184 extern int8_t SMBus_controllerQuickCommand(SMBus *smbus,
1185  uint8_t targetAddr,
1186  bool write);
1187 
1188 //*****************************************************************************
1189 //
1191 //
1194 //
1196 //
1197 //*****************************************************************************
1198 extern int8_t SMBus_controllerWaitUntilDone(SMBus *smbus,
1199  int32_t timeout);
1200 
1201 //*****************************************************************************
1202 //
1204 //
1207 //
1210 //
1212 //
1213 //*****************************************************************************
1214 extern void SMBus_controllerEnableHostNotify(SMBus *smbus, uint8_t *hostAlertBuffer);
1215 
1216 //*****************************************************************************
1217 //
1219 //
1222 //
1224 //
1226 //
1227 //*****************************************************************************
1228 extern void SMBus_controllerDisableHostNotify(SMBus *smbus);
1229 
1230 //*****************************************************************************
1231 //
1232 // Mark the end of the C bindings section for C++ compilers.
1233 //
1234 //*****************************************************************************
1235 #ifdef __cplusplus
1236 }
1237 #endif
1238 
1239 //*****************************************************************************
1240 //
1241 // Close the Doxygen group.
1243 //
1244 //*****************************************************************************
1245 
1246 #endif //__SMBUS_H__
Definition: smbus.h:342
uint8_t ownTargetAddr
Definition: smbus.h:401
SMBus_Auto_Ack
List of auto ack codes used within the NWK and PHY layers.
Definition: smbus.h:160
void SMBus_controllerReset(SMBus *smbus)
Force reset to SMBus controller interface.
Definition: smbus.c:228
Definition: smbus.h:324
uint8_t SMBus_targetClearStatusReg(SMBus *smbus, uint8_t val)
Clear the target&#39;s status register.
Definition: smbus.c:158
Definition: smbus.h:236
uint16_t rxLen
Definition: smbus.h:272
int8_t SMBus_controllerProcessCallBlock(SMBus *smbus, uint8_t targetAddr, uint8_t command, uint8_t *txData, uint8_t txSize, uint8_t *rxData)
Sends a block write-block read process call.
Definition: smbus.c:257
Definition: smbus.h:332
int8_t SMBus_controllerQuickCommand(SMBus *smbus, uint8_t targetAddr, bool write)
Send a SMBus "quick command".
Definition: smbus.c:431
uint8_t currentCmd
Definition: smbus.h:268
SMBus_State SMBus_controllerProcessInt(SMBus *smbus)
I2C Interrupt Service routine for a controller.
Definition: smbus.c:223
Definition: smbus.h:318
SMBus_Nwk nwk
Definition: smbus.h:393
uint16_t txIndex
Definition: smbus.h:278
Definition: smbus.h:322
Definition: smbus.h:344
SMBus_Ctrl ctrl
Definition: smbus.h:395
SMBus_Stop SMBus_Phy_stop
Definition: smbus.h:221
SMBus_Phy phy
Definition: smbus.h:391
SMBus_ErrorCode
List of error codes used by the application to indicate an error to the library.
Definition: smbus.h:298
uint8_t u8byte
Definition: smbus.h:380
Definition: smbus.h:326
void SMBus_targetReportError(SMBus *smbus, SMBus_ErrorCode errorCode)
Reports an error to SMBus driver from the target.
Definition: smbus.c:135
SMBus control register.
Definition: smbus.h:171
Definition: smbus.h:301
void SMBus_controllerDisableHostNotify(SMBus *smbus)
Disable support for Host Notify Protocol.
Definition: smbus.c:483
volatile SMBus_NwkState eState
Definition: smbus.h:264
uint8_t * txBuffPtr
Definition: smbus.h:282
uint8_t * hostNotifyRxBuffPtr
Definition: smbus.h:290
SMBus_State SMBus_targetProcessInt(SMBus *smbus)
I2C Interrupt Service routine for a target.
Definition: smbus.c:97
Definition: smbus.h:346
SMBus_State state
Definition: smbus.h:399
void SMBus_controllerInit(SMBus *smbus, I2C_Regs *i2cAddr, uint32_t busClk)
Initialize the SMBus Interface for a controller.
Definition: smbus.c:194
Definition: smbus.h:334
Definition: smbus.h:336
void SMBus_disablePEC(SMBus *smbus)
Disables PEC support.
Definition: smbus.c:63
void SMBus_targetSetRxBuffer(SMBus *smbus, uint8_t *data, uint16_t size)
Initialize the reception buffer for target.
Definition: smbus.c:117
int8_t SMBus_controllerSendByte(SMBus *smbus, uint8_t targetAddr, uint8_t txData)
Sends byte to the target.
Definition: smbus.c:287
uint16_t rxIndex
Definition: smbus.h:270
Definition: smbus.h:246
Definition: smbus.h:240
void SMBus_processDone(SMBus *smbus)
Clears the current state of SMBus.
Definition: smbus.c:41
Definition: smbus.h:340
int8_t SMBus_controllerReadByteWord(SMBus *smbus, uint8_t targetAddr, uint8_t command, uint8_t *rxData, uint8_t rxSize)
Receive a byte from the target.
Definition: smbus.c:375
int8_t SMBus_controllerWaitUntilDone(SMBus *smbus, int32_t timeout)
Wait until the previous SMBus command is executed.
Definition: smbus.c:458
int8_t SMBus_controllerProcessCall(SMBus *smbus, uint8_t targetAddr, uint8_t command, uint8_t *txData, uint8_t *rxData)
Sends a process call to a target.
Definition: smbus.c:233
Definition: smbus.h:238
void SMBus_targetSetTxBuffer(SMBus *smbus, uint8_t *data, uint16_t size)
Initialize the transmission buffer for target.
Definition: smbus.c:126
uint8_t u8byte
Definition: smbus.h:208
void SMBus_enablePEC(SMBus *smbus)
Enables PEC support.
Definition: smbus.c:58
Definition: smbus.h:328
uint16_t txSize
Definition: smbus.h:286
uint8_t * recByteTxPtr
Definition: smbus.h:284
SMBus_State SMBus_getState(SMBus *smbus)
Returns the state of the SMBus module.
Definition: smbus.c:52
uint8_t SMBus_targetGetCommand(SMBus *smbus)
Return the current command (Rxbuffer[0]) received by the target.
Definition: smbus.c:153
Definition: smbus.h:320
void SMBus_controllerEnableInt(SMBus *smbus)
Enables the I2C interrupts for a controller.
Definition: smbus.c:218
Definition: smbus.h:242
SMBus_NwkState
SMBus network layer states.
Definition: smbus.h:229
uint8_t * rxBuffPtr
Definition: smbus.h:276
SMBus_Status status
Definition: smbus.h:397
SMBus_State
SMBus state sent to application layer.
Definition: smbus.h:313
int8_t SMBus_targetHostAlert(SMBus *smbus, uint8_t deviceAddress, uint8_t *txData)
Send a Host Alert from the target.
Definition: smbus.c:173
void SMBus_targetSetAddress(SMBus *smbus, uint8_t targetAddr)
Set the target&#39;s own I2C address.
Definition: smbus.c:110
Physical and Data Link Layer object.
Definition: smbus.h:216
Definition: smbus.h:338
uint8_t currentAddr
Definition: smbus.h:266
Definition: smbus.h:248
void SMBus_targetInit(SMBus *smbus, I2C_Regs *i2cAddr)
Initialize the SMBus interface as a target.
Definition: smbus.c:69
Definition: smbus.h:305
Definition: smbus.h:232
Main SMBus object.
Definition: smbus.h:388
void SMBus_targetEnableInt(SMBus *smbus)
Enables the I2C interrupts for a target.
Definition: smbus.c:92
uint8_t pec
Definition: smbus.h:288
Definition: smbus.h:234
SMBus_Stop
List of stop codes used within the NWK and PHY layers.
Definition: smbus.h:138
I2C_Regs * SMBus_Phy_i2cBase
Definition: smbus.h:219
Definition: smbus.h:244
int8_t SMBus_controllerWriteByteWord(SMBus *smbus, uint8_t targetAddr, uint8_t command, uint8_t *txData, uint8_t txSize)
Send a command transmitting a byte or word of data from the target.
Definition: smbus.c:403
uint8_t SMBus_targetWriteCtrlReg(SMBus *smbus, uint8_t val)
Write a value to the target&#39;s control register.
Definition: smbus.c:165
uint16_t txLen
Definition: smbus.h:280
SMBus Status Register.
Definition: smbus.h:355
Definition: smbus.h:316
Definition: smbus.h:250
Definition of SMBus Network structure.
Definition: smbus.h:261
void SMBus_controllerEnableHostNotify(SMBus *smbus, uint8_t *hostAlertBuffer)
Enable support for Host Notify Protocol.
Definition: smbus.c:476
Definition: smbus.h:303
uint16_t rxSize
Definition: smbus.h:274
Definition: smbus.h:330
Definition: smbus.h:252
SMBus_Start
List of start codes used within the NWK and PHY layers.
Definition: smbus.h:149
uint16_t SMBus_getRxPayloadAvailable(SMBus *smbus)
Returns the number of received bytes from last transaction.
Definition: smbus.c:47
© Copyright 1995-2023, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale