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 //
71 //
72 //*****************************************************************************
73 #define SMB_PEC_SUPPORTED (1)
74 
75 
76 //*****************************************************************************
77 //
83 //
84 //*****************************************************************************
85 #define SMB_CRC8_USE_LOOKUP_TABLE (1)
86 
87 //*****************************************************************************
88 //
90 //
91 //*****************************************************************************
92 #define SMB_MAX_PAYLOAD_SIZE (255)
93 
94 //*****************************************************************************
95 //
97 //
98 //*****************************************************************************
99 #define SMB_MAX_PACKET_SIZE (SMB_MAX_PAYLOAD_SIZE + 3)
100 
101 //*****************************************************************************
102 //
104 //
105 //*****************************************************************************
106 #define RESPONSE_NTR 0x00
107 
108 //*****************************************************************************
109 //
111 //
112 //*****************************************************************************
113 #define SMBUS_RET_OK (1)
114 
115 //*****************************************************************************
116 //
118 //
119 //*****************************************************************************
120 #define SMBUS_RET_ERROR (-1)
121 
122 //*****************************************************************************
123 // typedefs
124 //*****************************************************************************
125 
126 //*****************************************************************************
127 //
129 //
130 //*****************************************************************************
131 typedef enum
132 {
133  SMBus_Stop_After_Transfer = 0,
134  SMBus_No_Stop_After_Transfer
135 } SMBus_Stop;
136 
137 //*****************************************************************************
138 //
140 //
141 //*****************************************************************************
142 typedef enum
143 {
144  SMBus_Start_Before_Transfer = 0,
145  SMBus_No_Start_Before_Transfer
146 } SMBus_Start;
147 
148 //*****************************************************************************
149 //
151 //
152 //*****************************************************************************
153 typedef enum
154 {
155  SMBus_Auto_Ack_Last_Byte = 0,
156  SMBus_No_Auto_Ack_Last_Byte
158 
159 //*****************************************************************************
160 //
162 //
163 //*****************************************************************************
164 typedef union
165 {
166  //*****************************************************************************
167  //
169  //
170  //*****************************************************************************
171  struct
172  {
174  uint8_t pecEn : 1;
176  uint8_t swackEn : 1;
178  uint8_t intEn : 1;
180  uint8_t phyEn : 1;
182  uint8_t controller : 1;
184  uint8_t reserved2 : 3;
185  } bits;
186  //*****************************************************************************
187  //
189  //
190  //*****************************************************************************
191  struct
192  {
194  uint8_t writeBits : 1;
196  uint8_t reserved : 7;
197  } writeableBits;
199  uint8_t u8byte;
200 } SMBus_Ctrl;
201 
202 //*****************************************************************************
203 //
205 //
206 //*****************************************************************************
207 typedef struct
208 {
210  I2C_Regs* SMBus_Phy_i2cBase;
212  SMBus_Stop SMBus_Phy_stop;
213 } SMBus_Phy;
214 
215 //*****************************************************************************
216 //
218 //
219 //*****************************************************************************
220 typedef enum
221 {
243 
244 /****************************************************************************** */
245 
246 //
248 //
249 //*****************************************************************************
250 typedef struct
251 {
253  volatile SMBus_NwkState eState;
255  uint8_t currentAddr;
257  uint8_t currentCmd;
259  uint16_t rxIndex;
261  uint16_t rxLen;
263  uint16_t rxSize;
265  uint8_t *rxBuffPtr;
267  uint16_t txIndex;
269  uint16_t txLen;
271  uint8_t *txBuffPtr;
273  uint8_t *recByteTxPtr;
275  uint16_t txSize;
276 #if (SMB_PEC_SUPPORTED == 1)
277 
278  uint8_t pec;
279 #endif
280 } SMBus_Nwk;
281 
282 //*****************************************************************************
283 //
285 //
286 //*****************************************************************************
287 typedef enum
288 {
296 
297 //*****************************************************************************
298 //
300 //
301 //*****************************************************************************
302 typedef enum
303 {
334 } SMBus_State;
335 
336 /****************************************************************************** */
337 
338 //
340 //
341 //*****************************************************************************
342 typedef union
343 {
344  //*****************************************************************************
345  //
347  //
348  //*****************************************************************************
349  struct
350  {
352  uint8_t pecErr : 1;
354  uint8_t toErr : 1;
356  uint8_t packErr : 1;
358  uint8_t packOvrErr : 1;
360  uint8_t byteOvrErr : 1;
362  uint8_t cmdErr : 1;
364  uint8_t reserved : 2;
365  } bits;
367  uint8_t u8byte;
368 } SMBus_Status;
369 
370 //*****************************************************************************
371 //
373 //
374 //*****************************************************************************
375 typedef struct
376 {
386  SMBus_State state;
388  uint8_t ownTargetAddr;
389 } SMBus;
390 
391 //*****************************************************************************
392 // globals
393 //*****************************************************************************
394 
395 //*****************************************************************************
396 // Public functions called by applications
397 //*****************************************************************************
398 //*****************************************************************************
399 //
401 //
404 //
406 //
408 //
409 //*****************************************************************************
410 extern void SMBus_processDone(SMBus *smbus);
411 
412 //*****************************************************************************
413 //
415 //
417 //
419 //
420 //*****************************************************************************
421 extern uint16_t SMBus_getRxPayloadAvailable(SMBus *smbus);
422 
423 //*****************************************************************************
424 //
426 //
428 //
430 //
431 //*****************************************************************************
432 extern SMBus_State SMBus_getState(SMBus *smbus);
433 
434 #if (SMB_PEC_SUPPORTED == 1)
435 //*****************************************************************************
436 //
438 //
440 //
442 //
443 //*****************************************************************************
444 extern void SMBus_enablePEC(SMBus *smbus);
445 //*****************************************************************************
446 //
448 //
450 //
452 //
453 //*****************************************************************************
454 extern void SMBus_disablePEC(SMBus *smbus);
455 #endif
456 
457 //*****************************************************************************
458 //
460 //
462 //
465 //
467 //
468 //*****************************************************************************
469 extern void SMBus_targetInit(SMBus *smbus, I2C_Regs *i2cAddr);
470 
471 //*****************************************************************************
472 //
474 //
476 //
478 //
480 //
481 //*****************************************************************************
482 extern void SMBus_targetEnableInt(SMBus *smbus);
483 
484 //*****************************************************************************
485 //
487 //
490 //
492 //
502 //
503 //*****************************************************************************
504 extern SMBus_State SMBus_targetProcessInt(SMBus *smbus);
505 
506 //*****************************************************************************
507 //
509 //
511 //
513 //
515 //
516 //*****************************************************************************
517 extern void SMBus_controllerReset(SMBus *smbus);
518 
519 //*****************************************************************************
520 //
522 //
525 //
527 //
528 //*****************************************************************************
529 extern void SMBus_targetSetAddress(SMBus *smbus,
530  uint8_t targetAddr);
531 
532 //*****************************************************************************
533 //
535 //
539 //
541 //
542 //*****************************************************************************
543 extern void SMBus_targetSetRxBuffer(SMBus *smbus,
544  uint8_t *data,
545  uint16_t size);
546 
547 //*****************************************************************************
548 //
550 //
554 //
556 //
557 //*****************************************************************************
558 extern void SMBus_targetSetTxBuffer(SMBus *smbus,
559  uint8_t *data,
560  uint16_t size);
561 
562 //*****************************************************************************
563 //
565 //
567 //
570 //
572 //
573 //*****************************************************************************
574 extern void SMBus_targetReportError(SMBus *smbus,
575  SMBus_ErrorCode errorCode);
576 
577 //*****************************************************************************
578 //
580 //
582 //
584 //
585 //*****************************************************************************
586 extern uint8_t SMBus_targetGetCommand(SMBus *smbus);
587 
588 //*****************************************************************************
589 //
591 //
594 //
596 //
597 //*****************************************************************************
598 extern uint8_t SMBus_targetClearStatusReg(SMBus *smbus,
599  uint8_t val);
600 
601 //*****************************************************************************
602 //
604 //
607 //
609 //
610 //*****************************************************************************
611 extern uint8_t SMBus_targetWriteCtrlReg(SMBus *smbus,
612  uint8_t val);
613 
614 
615 //*****************************************************************************
616 //
618 //
620 //
624 //
625 // \return None
626 //
627 //*****************************************************************************
628 extern void SMBus_controllerInit(SMBus *smbus,
629  I2C_Regs *i2cAddr,
630  uint32_t busClk);
631 
632 //*****************************************************************************
633 //
635 //
638 //
640 //
642 //
643 //*****************************************************************************
644 extern void SMBus_controllerEnableInt(SMBus *smbus);
645 
646 //*****************************************************************************
647 //
649 //
652 //
654 //
656 //
657 //*****************************************************************************
658 extern SMBus_State SMBus_controllerProcessInt(SMBus *smbus);
659 
660 
661 //*****************************************************************************
662 //
664 //
701 //
707 //
709 //
710 //*****************************************************************************
711 extern int8_t SMBus_controllerProcessCall(SMBus *smbus,
712  uint8_t targetAddr,
713  uint8_t command,
714  uint8_t *txData,
715  uint8_t *rxData);
716 
717 //*****************************************************************************
718 //
720 //
724 //
776 //
783 //
785 //
786 //*****************************************************************************
787 extern int8_t SMBus_controllerProcessCallBlock(SMBus *smbus,
788  uint8_t targetAddr,
789  uint8_t command,
790  uint8_t *txData,
791  uint8_t txSize,
792  uint8_t *rxData);
793 
794 //*****************************************************************************
795 //
797 //
821 //
825 //
827 //
828 //*****************************************************************************
829 extern int8_t SMBus_controllerSendByte(SMBus *smbus,
830  uint8_t targetAddr,
831  uint8_t txData);
832 
833 //*****************************************************************************
834 //
836 //
859 //
863 //
865 //
866 //*****************************************************************************
867 extern int8_t SMBus_controllerReceiveByte(SMBus *smbus,
868  uint8_t targetAddr,
869  uint8_t *rxData);
870 
871 //*****************************************************************************
872 //
874 //
878 //
918 //
923 //
925 //
926 //*****************************************************************************
927 extern int8_t SMBus_controllerReadBlock(SMBus *smbus,
928  uint8_t targetAddr,
929  uint8_t command,
930  uint8_t *rxData);
931 
932 //*****************************************************************************
933 //
935 //
938 //
972 //
978 //
980 //
981 //*****************************************************************************
982 extern int8_t SMBus_controllerWriteBlock(SMBus *smbus,
983  uint8_t targetAddr,
984  uint8_t command,
985  uint8_t *txData,
986  uint16_t txSize);
987 
988 //*****************************************************************************
989 //
991 //
992 //
1041 //
1047 //
1049 //
1050 //*****************************************************************************
1051 extern int8_t SMBus_controllerReadByteWord(SMBus *smbus,
1052  uint8_t targetAddr,
1053  uint8_t command,
1054  uint8_t *rxData,
1055  uint8_t rxSize);
1056 
1057 //*****************************************************************************
1058 //
1096 //
1102 //
1104 //
1105 //*****************************************************************************
1106 extern int8_t SMBus_controllerWriteByteWord(SMBus *smbus,
1107  uint8_t targetAddr,
1108  uint8_t command,
1109  uint8_t *txData,
1110  uint8_t txSize);
1111 
1112 //*****************************************************************************
1113 //
1115 //
1131 //
1135 //
1137 //
1138 //*****************************************************************************
1139 extern int8_t SMBus_controllerQuickCommand(SMBus *smbus,
1140  uint8_t targetAddr,
1141  bool write);
1142 
1143 //*****************************************************************************
1144 //
1146 //
1149 //
1151 //
1152 //*****************************************************************************
1153 extern int8_t SMBus_controllerWaitUntilDone(SMBus *smbus,
1154  int32_t timeout);
1155 
1156 //*****************************************************************************
1157 //
1158 // Mark the end of the C bindings section for C++ compilers.
1159 //
1160 //*****************************************************************************
1161 #ifdef __cplusplus
1162 }
1163 #endif
1164 
1165 //*****************************************************************************
1166 //
1167 // Close the Doxygen group.
1169 //
1170 //*****************************************************************************
1171 
1172 #endif //__SMBUS_H__
Definition: smbus.h:331
uint8_t ownTargetAddr
Definition: smbus.h:388
SMBus_Auto_Ack
List of auto ack codes used within the NWK and PHY layers.
Definition: smbus.h:153
void SMBus_controllerReset(SMBus *smbus)
Force reset to SMBus controller interface.
Definition: smbus.c:211
Definition: smbus.h:313
uint8_t SMBus_targetClearStatusReg(SMBus *smbus, uint8_t val)
Clear the target&#39;s status register.
Definition: smbus.c:160
Definition: smbus.h:227
uint16_t rxLen
Definition: smbus.h:261
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:240
Definition: smbus.h:321
int8_t SMBus_controllerQuickCommand(SMBus *smbus, uint8_t targetAddr, bool write)
Send a SMBus "quick command".
Definition: smbus.c:414
uint8_t currentCmd
Definition: smbus.h:257
SMBus_State SMBus_controllerProcessInt(SMBus *smbus)
I2C Interrupt Service routine for a controller.
Definition: smbus.c:206
Definition: smbus.h:307
SMBus_Nwk nwk
Definition: smbus.h:380
uint16_t txIndex
Definition: smbus.h:267
Definition: smbus.h:311
SMBus_Ctrl ctrl
Definition: smbus.h:382
SMBus_Stop SMBus_Phy_stop
Definition: smbus.h:212
SMBus_Phy phy
Definition: smbus.h:378
SMBus_ErrorCode
List of error codes used by the application to indicate an error to the library.
Definition: smbus.h:287
uint8_t u8byte
Definition: smbus.h:367
Definition: smbus.h:315
void SMBus_targetReportError(SMBus *smbus, SMBus_ErrorCode errorCode)
Reports an error to SMBus driver from the target.
Definition: smbus.c:137
SMBus control register.
Definition: smbus.h:164
Definition: smbus.h:290
volatile SMBus_NwkState eState
Definition: smbus.h:253
uint8_t * txBuffPtr
Definition: smbus.h:271
SMBus_State SMBus_targetProcessInt(SMBus *smbus)
I2C Interrupt Service routine for a target.
Definition: smbus.c:99
Definition: smbus.h:333
SMBus_State state
Definition: smbus.h:386
void SMBus_controllerInit(SMBus *smbus, I2C_Regs *i2cAddr, uint32_t busClk)
Initialize the SMBus Interface for a controller.
Definition: smbus.c:175
Definition: smbus.h:323
Definition: smbus.h:325
void SMBus_disablePEC(SMBus *smbus)
Disables PEC support.
Definition: smbus.c:62
void SMBus_targetSetRxBuffer(SMBus *smbus, uint8_t *data, uint16_t size)
Initialize the reception buffer for target.
Definition: smbus.c:119
int8_t SMBus_controllerSendByte(SMBus *smbus, uint8_t targetAddr, uint8_t txData)
Sends byte to the target.
Definition: smbus.c:270
uint16_t rxIndex
Definition: smbus.h:259
Definition: smbus.h:235
Definition: smbus.h:229
void SMBus_processDone(SMBus *smbus)
Clears the current state of SMBus.
Definition: smbus.c:40
Definition: smbus.h:329
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:358
int8_t SMBus_controllerWaitUntilDone(SMBus *smbus, int32_t timeout)
Wait until the previous SMBus command is executed.
Definition: smbus.c:441
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:216
void SMBus_targetSetTxBuffer(SMBus *smbus, uint8_t *data, uint16_t size)
Initialize the transmission buffer for target.
Definition: smbus.c:128
uint8_t u8byte
Definition: smbus.h:199
void SMBus_enablePEC(SMBus *smbus)
Enables PEC support.
Definition: smbus.c:57
Definition: smbus.h:317
uint16_t txSize
Definition: smbus.h:275
uint8_t * recByteTxPtr
Definition: smbus.h:273
SMBus_State SMBus_getState(SMBus *smbus)
Returns the state of the SMBus module.
Definition: smbus.c:51
uint8_t SMBus_targetGetCommand(SMBus *smbus)
Return the current command (Rxbuffer[0]) received by the target.
Definition: smbus.c:155
Definition: smbus.h:309
void SMBus_controllerEnableInt(SMBus *smbus)
Enables the I2C interrupts for a controller.
Definition: smbus.c:201
Definition: smbus.h:231
SMBus_NwkState
SMBus network layer states.
Definition: smbus.h:220
uint8_t * rxBuffPtr
Definition: smbus.h:265
SMBus_Status status
Definition: smbus.h:384
SMBus_State
SMBus state sent to application layer.
Definition: smbus.h:302
void SMBus_targetSetAddress(SMBus *smbus, uint8_t targetAddr)
Set the target&#39;s own I2C address.
Definition: smbus.c:112
Physical and Data Link Layer object.
Definition: smbus.h:207
Definition: smbus.h:327
uint8_t currentAddr
Definition: smbus.h:255
Definition: smbus.h:237
void SMBus_targetInit(SMBus *smbus, I2C_Regs *i2cAddr)
Initialize the SMBus interface as a target.
Definition: smbus.c:69
Definition: smbus.h:294
Definition: smbus.h:223
Main SMBus object.
Definition: smbus.h:375
void SMBus_targetEnableInt(SMBus *smbus)
Enables the I2C interrupts for a target.
Definition: smbus.c:94
uint8_t pec
Definition: smbus.h:278
Definition: smbus.h:225
SMBus_Stop
List of stop codes used within the NWK and PHY layers.
Definition: smbus.h:131
I2C_Regs * SMBus_Phy_i2cBase
Definition: smbus.h:210
Definition: smbus.h:233
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:386
uint8_t SMBus_targetWriteCtrlReg(SMBus *smbus, uint8_t val)
Write a value to the target&#39;s control register.
Definition: smbus.c:167
uint16_t txLen
Definition: smbus.h:269
SMBus Status Register.
Definition: smbus.h:342
Definition: smbus.h:305
Definition: smbus.h:239
Definition of SMBus Network structure.
Definition: smbus.h:250
Definition: smbus.h:292
uint16_t rxSize
Definition: smbus.h:263
Definition: smbus.h:319
Definition: smbus.h:241
SMBus_Start
List of start codes used within the NWK and PHY layers.
Definition: smbus.h:142
uint16_t SMBus_getRxPayloadAvailable(SMBus *smbus)
Returns the number of received bytes from last transaction.
Definition: smbus.c:46
© Copyright 1995-2023, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale