Bar Logo 3.8/7.6 kw Totem pole Demonstration Application (Part-No. (not specified))
 
Content
     
Loading...
Searching...
No Matches
drv_i2c.c
Go to the documentation of this file.
1
11/*
12 (c) 2022 Microchip Technology Inc. and its subsidiaries. You may use this
13 software and any derivatives exclusively with Microchip products.
14
15 THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
16 EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
17 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
18 PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
19 WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
20
21 IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
22 INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
23 WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
24 BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
25 FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
26 ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
27 THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
28
29 MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
30 TERMS.
31 */
32
33#include "xc.h"
34#include "drv_i2c.h"
35#include "driver/can/drv_can.h"
36
37// MCC header files
38#include "i2c2.h"
39#include "system/pins.h"
40
41// create I2C write object, initialize each member to be in fault state
42static I2C_t i2c_write = {
44 .frame.bytes.aux_fault_flags.bits._12v = 1,
45 .frame.bytes.aux_fault_flags.bits._24v = 1,
46 .frame.bytes.sic_status_flags.bits.sic1 = 3,
47 .frame.bytes.sic_status_flags.bits.sic2 = 3,
48 .frame.bytes.sic_status_flags.bits.sic3 = 3,
49 .frame.bytes.sic_status_flags.bits.sic4 = 3
50};
51
52static uint8_t i2c2_slaveWriteData = 0xAA; // holds byte written to slave
53
54//------------------------------------------------------------------------------
55// local function declarations
56//------------------------------------------------------------------------------
57static void i2c_crc_update(uint8_t data, struct I2C_s* i2c_obj);
58
59
69{
70 return (i2c_write);
71}
72
73
82bool I2C2_StatusCallback(I2C2_SLAVE_DRIVER_STATUS status)
83{
84 static uint16_t byteCount;
85
86 switch (status)
87 {
88 case I2C2_SLAVE_TRANSMIT_REQUEST_DETECTED:
89 // set up the slave driver buffer transmit pointer
90 I2C2_ReadPointerSet(&i2c_write.frame.array[byteCount++]);
91 if (byteCount >= I2C_WRITE_BUFFER_SIZE)
92 {
93 byteCount = 0;
94 }
95 break;
96
97 case I2C2_SLAVE_RECEIVE_REQUEST_DETECTED:
98 // address of slave matches address in I2C message transmitted
99 // from I2C master
100 byteCount = 0;
101
102 // clear status flags
104
105 // clear timeout flag and counter
108
109 // set up the slave driver buffer receive pointer
110 I2C2_WritePointerSet(&i2c2_slaveWriteData);
111
112 // start CRC calculation
113 // first clear CRC byte
114 i2c_write.crc = 0;
115
116 // read first byte written from I2C master
117 // this should be the address, left shifted by 1 bit, plus the
118 // read/write bit in bit position 0 (read/write bit = 0) in this
119 // case as I2C master is writing to I2C slave
120 volatile uint8_t i2c_rcv = (uint8_t) I2C2RCV;
121 i2c_crc_update(i2c_rcv, &i2c_write);
122
123 break;
124
125 case I2C2_SLAVE_RECEIVED_DATA_DETECTED:
126
127 // receiving non-address data
128 // update CRC
130
131 // check if command byte is valid
132 // (cmd byte = first byte received after address byte)
133 if (byteCount == 0)
134 {
135 // first byte should be a valid command byte
137 {
138 // error, command byte is not correct
140 }
141 else
142 {
143 // command byte is as expected
145 }
146 }
147
148 // fill buffer with received data
149 if (byteCount < I2C_WRITE_BUFFER_SIZE)
150 {
152 }
153 byteCount++;
154
155 break;
156
157 case I2C2_SLAVE_10BIT_RECEIVE_REQUEST_DETECTED:
158 // 10-bit address is detected
159 break;
160
161 case I2C2_SLAVE_STOP_BIT_DETECTED:
162
163 // finished processing this message
165
166 // CRC check, all going well the CRC should be zero at this point
167 if (i2c_write.crc == 0)
168 {
170 }
171 else
172 {
174 }
175
176 break;
177
178 default:
179 break;
180
181 }
182
183 return true;
184}
185
186//------------------------------------------------------------------------------
187// look up table for CRC8 with polynomial 0x07 (used for SMBus PEC)
188//------------------------------------------------------------------------------
189const uint8_t crc_lut[256] = {0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
190 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
191 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
192 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD,
193 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA,
194 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,
195 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A,
196 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A,
197 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,
198 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4,
199 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44,
200 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,
201 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63,
202 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,
203 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
204 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3};
205
206
215static void i2c_crc_update(uint8_t data, struct I2C_s* i2c_obj)
216{
217 // xor byte with crc
218 uint8_t xor_data_crc = (uint8_t) (data ^ i2c_obj->crc);
219
220 // calculate CRC using LUT
221 i2c_obj->crc = (uint8_t) (crc_lut[xor_data_crc]);
222}
223
224
236{
239 {
241 i2c_write.status.bits.timeout = true; // set timeout flag
242 }
243}
header of the CAN Driver
void Drv_I2C_Timeout(void)
Definition drv_i2c.c:235
I2C_t Drv_I2C_Get_Write_Data(void)
Definition drv_i2c.c:68
const uint8_t crc_lut[256]
Definition drv_i2c.c:189
static I2C_t i2c_write
Definition drv_i2c.c:42
bool I2C2_StatusCallback(I2C2_SLAVE_DRIVER_STATUS status)
Definition drv_i2c.c:82
static void i2c_crc_update(uint8_t data, struct I2C_s *i2c_obj)
Definition drv_i2c.c:215
static uint8_t i2c2_slaveWriteData
Definition drv_i2c.c:52
This is the driver header file for DMA driver.
#define CMD_SEND_COMBINED_STATUS_DATA
Definition drv_i2c.h:48
#define I2C_WRITE_BUFFER_SIZE
Definition drv_i2c.h:45
#define I2C_TIMEOUT_IN_100us_TICKS
Definition drv_i2c.h:54
size_t status
Definition uart1.c:99
volatile bool done
Bit #2: flag indicating that I2C module is finished processing this message.
Definition drv_i2c.h:67
volatile bool cmd_ok
Bit #0: flag indicating the validity of the command byte.
Definition drv_i2c.h:65
struct I2C_STATUS_s::@3::@5 bits
volatile bool timeout
Bit #3: flag indicating that I2C message has not been received withing timeout period.
Definition drv_i2c.h:68
volatile bool pec_ok
Bit #1: flag indicating PEC error (CRC error in datastream)
Definition drv_i2c.h:66
struct AUX_FLAGS_s::@9::@11 bits
volatile uint8_t _5v
Bit 0:1 -> 0 = no fault
Definition drv_i2c.h:100
uint8_t array[I2C_WRITE_BUFFER_SIZE]
Definition drv_i2c.h:126
struct SIC_CLIENT_RECEIVE_FRAME_s::@12::@14 bytes
AUX_FLAGS_t aux_fault_flags
Definition drv_i2c.h:121
SIC_CLIENT_RECEIVE_FRAME_t frame
buffer to store I2C data
Definition drv_i2c.h:133
I2C_STATUS_t status
status flags
Definition drv_i2c.h:132
uint8_t crc
running crc
Definition drv_i2c.h:134
uint16_t timeout_counter
Definition drv_i2c.h:135