Bar Logo 3.8/7.6 kw Totem pole Demonstration Application (Part-No. (not specified))
 
Content
     
Loading...
Searching...
No Matches
drv_can.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// MCC header files
34//#include "slave1.h"
35#include "driver/dma/drv_dma.h"
36
37// other header files
38#include "drv_can.h"
40#include "driver/spi/drv_spi.h"
41#include "driver/i2c/drv_i2c.h"
42#include "../../../revision.h"
43#include "can/can_interface.h"
44#include "system/pins.h"
45#include "can/can1.h"
46
48
49#include "../sources_common/Totempole_Application_Setup.h"
50
51//------------------------------------------------------------------------------
52// local typedefs
53//------------------------------------------------------------------------------
55
56 union
57 {
58 struct
59 {
60 volatile uint8_t sic1_5v_ok : 1;
61 volatile uint8_t sic2_5v_ok : 1;
62 volatile uint8_t sic3_5v_ok : 1;
63 volatile uint8_t sic4_5v_ok : 1;
64 volatile uint8_t sic1_comms_ok : 1;
65 volatile uint8_t sic2_comms_ok : 1;
66 volatile uint8_t sic3_comms_ok : 1;
67 volatile uint8_t sic4_comms_ok : 1;
68 volatile uint8_t aux_5v_ok : 1;
69 volatile uint8_t aux_12v_ok : 1;
70 volatile uint8_t aux_24v_ok : 1;
71 volatile uint8_t aux_comms_ok : 1;
72 volatile uint8_t spi_checksum_ok : 1;
73 volatile uint8_t spi_connection_ok : 1;
74 volatile unsigned : 1;
75 volatile unsigned : 1;
76 } __attribute__((packed)) bits; // data structure for single bit addressing operations
77 volatile uint16_t value; // buffer for 16-bit word read/write operations
78 };
79};
80
81// TODO: create module specific structure wrapping all of these variables
83
85
86static uint16_t MSIFIFOrec[32]; //intermediate buffer for S->M FIFO data
87static uint8_t can_sendbuffer[64]; //buffer for send bytes via CAN interface
88
89static uint16_t protocolAData[5];
90
91typedef struct CAN_MSG_OBJ CAN_MSG_OBJ_t; // this is necessary!
92
93static CAN_MSG_OBJ_t CANBRIDGE_MSGOBJ_TX; //struct from CAN-MCC
94static CAN_MSG_OBJ_t CANBRIDGE_MSGOBJ_RX; //struct from CAN-MCC
95
97
98static uint8_t firmware_version[32] = {'p', 'r', 'i',':',' ',
99 REV_MAJOR,'.',REV_MINOR,'.',REV_PATCH,
100 ',',' ','s','e','c',':',' ','.','.','.','.','.','.','.'};
101
102
129
130
149{
150 // sic status byte from I2C
151 // bits 0:1 -> SiC card 1
152 // bits 2:3 -> SiC card 2
153 // bits 4:5 -> SiC card 3
154 // bits 6:7 -> SiC card 4
155 //
156 // if 0: comms ok, 5V ok
157 // if 1 or 2: comms ok, 5V not ok
158 // if 3 comms not ok, 5V not ok (assume the worst as don't know)
159
160 // fetch i2c write data via API function
161 I2C_t i2c_write_data = Drv_I2C_Get_Write_Data();
162
163 //----------------------------------------------------------------------------
164 // SiC card 1
165 //----------------------------------------------------------------------------
166 if (i2c_write_data.frame.bytes.sic_status_flags.bits.sic1 == 0)
167 {
170 }
171 else if (i2c_write_data.frame.bytes.sic_status_flags.bits.sic1 == 3)
172 {
175 }
176 else
177 {
178 // 1 or 2
181 }
182
183 //----------------------------------------------------------------------------
184 // SiC card 2
185 //----------------------------------------------------------------------------
186 if (i2c_write_data.frame.bytes.sic_status_flags.bits.sic2 == 0)
187 {
190 }
191 else if (i2c_write_data.frame.bytes.sic_status_flags.bits.sic2 == 3)
192 {
195 }
196 else
197 {
198 // 1 or 2
201 }
202
203 //----------------------------------------------------------------------------
204 // SiC card 3
205 //----------------------------------------------------------------------------
206 if (i2c_write_data.frame.bytes.sic_status_flags.bits.sic3 == 0)
207 {
210 }
211 else if (i2c_write_data.frame.bytes.sic_status_flags.bits.sic3 == 3)
212 {
215 }
216 else
217 {
218 // 1 or 2
221 }
222
223 //----------------------------------------------------------------------------
224 // SiC card 4
225 //----------------------------------------------------------------------------
226 if (i2c_write_data.frame.bytes.sic_status_flags.bits.sic4 == 0)
227 {
230 }
231 else if (i2c_write_data.frame.bytes.sic_status_flags.bits.sic4 == 3)
232 {
235 }
236 else
237 {
238 // 1 or 2
241 }
242
243 //----------------------------------------------------------------------------
244 // PIC16 on main board (5V, 12V and 24V rails)
245 // this data is received over I2C
246 //----------------------------------------------------------------------------
247 if (i2c_write_data.frame.bytes.aux_fault_flags.bits._5v == 0)
248 {
250 }
251 else
252 {
254 }
255
256 if (i2c_write_data.frame.bytes.aux_fault_flags.bits._12v == 0)
257 {
259 }
260 else
261 {
263 }
264
265 if (i2c_write_data.frame.bytes.aux_fault_flags.bits._24v == 0)
266 {
268 }
269 else
270 {
272 }
273
274 // comms check
275 if ((!i2c_write_data.status.bits.cmd_ok) || (i2c_write_data.status.bits.timeout))
276 {
278 }
279 else
280 {
282 }
283
286
287}
288
289
300{
301 if (MSI1FIFOCSbits.RFFULL) // FIFO from secondary to primary core is full
302 {
303 volatile uint16_t can_word_index;
304
305 // average input voltage has been computed by AC monitor state machines
306 // this data is stored in phase1.vin.avg, phase2.vin.avg and phase3.vin.avg
307
308 uint16_t vin_avg[3];
309 uint16_t * coreBuf = DRV_DMA_getCoreBuffer();
310
311 DRV_DMA_getVinAverage(vin_avg);
312
313 // CAN word 0, 1 and 2
314 // average voltage for each phase, for display on power board visualizer GUI
315 for (can_word_index = 0; can_word_index < 3; can_word_index++)
316 {
317 can_sendbuffer[can_word_index << 1] = (uint8_t) (vin_avg[can_word_index] >> 8);
318 can_sendbuffer[(can_word_index << 1) + 1] = (uint8_t) (vin_avg[can_word_index] & 0x00FF);
319 }
320
321 // CAN words 3, 4, 5, 6
322 // word 3: phase 1 voltage loop feed-forward term
323 // word 4: phase 2 voltage loop feed-forward term
324 // word 5: phase 3 voltage loop feed-forward term
325 // word 6: Vac status word
326 // this data has already been placed in the array pri_to_sec_core_buffer[]
327 // for sending across to the secondary core
328 for (can_word_index = 3; can_word_index < 7; can_word_index++)
329 {
330 can_sendbuffer[can_word_index << 1] = (uint8_t) (coreBuf[can_word_index] >> 8);
331 can_sendbuffer[(can_word_index << 1) + 1] = (uint8_t) (coreBuf[can_word_index] & 0x00FF);
332 }
333
334 // next bunch of CAN bytes: fill them up with the data from the FIFO
335 // channel from secondary to primary
336 uint16_t fifo_word_index = 0;
337
338 while (!MSI1FIFOCSbits.RFEMPTY) // keep pulling words out of FIFO until it is empty
339 {
340 MSIFIFOrec[fifo_word_index] = MRSWFDATA; // read word from FIFO
341
342 // at present, only the first 12 words in the FIFO words contain valid data
343 // so stop when index >= 12
344 if (fifo_word_index < 12)
345 {
346 // convert word to bytes into CAN send buffer
347 can_sendbuffer[can_word_index << 1] = (uint8_t) (MSIFIFOrec[fifo_word_index] >> 8);
348 can_sendbuffer[(can_word_index << 1) + 1] = (uint8_t) (MSIFIFOrec[fifo_word_index] & 0x00FF);
349 can_word_index++;
350 }
351
352 fifo_word_index++; // keep going until FIFO is empty
353 }
354
355 // fill rest of the CAN buffer with data gathered over I2C and SPI
356 Housekeeping_Flags_Update(); // update flags for PBV GUI
357
358 // convert words to bytes for CAN
359 can_sendbuffer[can_word_index << 1] = (uint8_t) (housekeeping_status.value >> 8);
360 can_sendbuffer[(can_word_index << 1) + 1] = (uint8_t) (housekeeping_status.value & 0x00FF);
361 can_word_index++;
362
363 // bytes 2 to 5 of I2C data contain SiC card temperatures
364 // Power Board visualizer reads words (not bytes) so convert from bytes to words
365 I2C_t i2c_write_data = Drv_I2C_Get_Write_Data(); // fetch I2C write data via API function
366
367 uint16_t i2c_byte_index;
368 for (i2c_byte_index = 2; i2c_byte_index < 6; i2c_byte_index++)
369 {
370 int16_t temperature = (int16_t) ((int8_t) i2c_write_data.frame.array[i2c_byte_index] + 128); // sign extend if necessary
371
372 // convert words to bytes
373 can_sendbuffer[can_word_index << 1] = (uint8_t) (temperature >> 8);
374 can_sendbuffer[(can_word_index << 1) + 1] = (uint8_t) (temperature & 0x00FF);
375 can_word_index++;
376 }
377
378 static uint16_t transmit_firmware_version_counter = 100;
379 transmit_firmware_version_counter--;
380 if (transmit_firmware_version_counter <= 0)
381 {
382 // update secondary core firmware version information
383 firmware_version[19] = (uint16_t)((MSIFIFOrec[3] & 0xFF00)>>8); // secondary core, major revision
384 firmware_version[21] = (uint16_t)(MSIFIFOrec[3] & 0x00FF); // secondary core, minor revision
385 firmware_version[23] = (uint16_t)((MSIFIFOrec[4] & 0xFF00)>>8); // secondary core, patch revision
386 firmware_version[25] = (uint16_t)(MSIFIFOrec[4] & 0x00FF); // secondary core, high or low voltage
389 transmit_firmware_version_counter = 2000;
390 }
391 else
392 {
393 // send bytes to GUI via CAN
396 }
397 }
398}
399
400
412{
414 {
415 if (CAN1_Receive(&CANBRIDGE_MSGOBJ_RX) == true) // message received over CAN from GUI
416 {
417 // send CAN data from primary to secondary using protocol A
418 protocolAData[0] = (uint16_t) (CANBRIDGE_MSGOBJ_RX.data[0] << 8) + CANBRIDGE_MSGOBJ_RX.data[1];
419 protocolAData[1] = (uint16_t) (CANBRIDGE_MSGOBJ_RX.data[2] << 8) + CANBRIDGE_MSGOBJ_RX.data[3];
420 protocolAData[2] = (uint16_t) (CANBRIDGE_MSGOBJ_RX.data[4] << 8) + CANBRIDGE_MSGOBJ_RX.data[5];
421 protocolAData[3] = (uint16_t) (CANBRIDGE_MSGOBJ_RX.data[6] << 8) + CANBRIDGE_MSGOBJ_RX.data[7];
422 protocolAData[4] = (uint16_t) (CANBRIDGE_MSGOBJ_RX.data[8] << 8) + CANBRIDGE_MSGOBJ_RX.data[9];
424 }
425 }
426}
This is the generated driver header file for the CAN1 driver using CCL.
@ CAN1_TXQ
Definition can1.h:212
@ DLC_64
Definition can_types.h:197
@ DLC_32
Definition can_types.h:195
@ CAN_FRAME_STD
Definition can_types.h:92
@ CAN_FRAME_EXT
Definition can_types.h:93
@ CAN_FRAME_DATA
Definition can_types.h:103
@ CAN_FD_FORMAT
Definition can_types.h:115
@ CAN_BRS_MODE
Definition can_types.h:82
This is the generated driver header file for the SEC_CORE1 driver.
@ MSI1_ProtocolA
static CAN_MSG_OBJ_t CANBRIDGE_MSGOBJ_RX
Definition drv_can.c:94
static CAN_MSG_OBJ_t CANBRIDGE_FIRMWARE_VERSION_TX
Definition drv_can.c:96
void CAN_TX_Obj_Init(void)
Definition drv_can.c:111
void CAN_Receive_from_GUI(void)
Definition drv_can.c:411
static CAN_MSG_OBJ_t CANBRIDGE_MSGOBJ_TX
Definition drv_can.c:93
void CAN_Transmit_to_GUI(void)
Definition drv_can.c:299
void Housekeeping_Flags_Update(void)
Definition drv_can.c:148
static uint8_t can_sendbuffer[64]
Definition drv_can.c:87
static uint8_t firmware_version[32]
Definition drv_can.c:98
static uint16_t protocolAData[5]
Definition drv_can.c:89
static uint16_t MSIFIFOrec[32]
Definition drv_can.c:86
static HOUSEKEEPING_STATUS_t housekeeping_status
Definition drv_can.c:84
header of the CAN Driver
void DRV_DMA_getVinAverage(uint16_t *vinAvg)
Definition drv_dma.c:241
uint16_t * DRV_DMA_getCoreBuffer(void)
Definition drv_dma.c:257
This is the driver header file for DMA driver.
I2C_t Drv_I2C_Get_Write_Data(void)
Definition drv_i2c.c:68
This is the driver header file for DMA driver.
bool Drv_SPI_get_Connected_Flag(void)
Definition drv_spi.c:102
bool Drv_SPI_get_ChecksumOK_Flag(void)
Definition drv_spi.c:117
This is the driver source file for spi builds upon mcc.
header of the VAC
bool CAN1_Receive(struct CAN_MSG_OBJ *rxCanMsg)
Reads the received single message object.
Definition can1.c:661
uint8_t CAN1_ReceivedMessageCountGet(void)
Returns the number of CAN messages received in all the FIFO.
Definition can1.c:812
enum CAN_TX_MSG_REQUEST_STATUS CAN1_Transmit(const enum CAN1_TX_FIFO_CHANNELS fifoChannel, struct CAN_MSG_OBJ *txCanMsg)
Writes the CAN message object to specified transmit FIFO channel.
Definition can1.c:746
bool SEC_CORE1_ProtocolWrite(enum SEC_CORE_PROTOCOLS protocolName, uint16_t *pData)
This routine writes data to mailbox.
Definition sec_core1.c:365
unsigned int frameType
Definition can_types.h:55
unsigned int formatType
Definition can_types.h:57
unsigned int dlc
Definition can_types.h:56
unsigned int idType
Definition can_types.h:54
unsigned int brs
Definition can_types.h:58
This data structure used to configure the CAN FD message object.
Definition can_types.h:68
struct CAN_MSG_FIELD field
Definition can_types.h:70
uint32_t msgId
Definition can_types.h:69
uint8_t * data
Definition can_types.h:71
volatile uint8_t spi_connection_ok
Bit #13
Definition drv_can.c:73
volatile uint8_t aux_24v_ok
Bit #10.
Definition drv_can.c:70
volatile unsigned
Bit #14: unused
Definition drv_can.c:74
volatile uint8_t sic1_comms_ok
Bit #4.
Definition drv_can.c:64
volatile uint16_t value
Definition drv_can.c:77
struct HOUSEKEEPING_STATUS_s::@0::@2 bits
volatile uint8_t aux_5v_ok
Bit #8.
Definition drv_can.c:68
volatile uint8_t sic1_5v_ok
Bit #0.
Definition drv_can.c:60
volatile uint8_t sic2_comms_ok
Bit #5.
Definition drv_can.c:65
volatile uint8_t sic3_5v_ok
Bit #2.
Definition drv_can.c:62
volatile uint8_t aux_comms_ok
Bit #11.
Definition drv_can.c:71
volatile uint8_t aux_12v_ok
Bit #9.
Definition drv_can.c:69
volatile uint8_t sic3_comms_ok
Bit #6.
Definition drv_can.c:66
volatile uint8_t sic4_comms_ok
Bit #7.
Definition drv_can.c:67
volatile uint8_t sic4_5v_ok
Bit #3.
Definition drv_can.c:63
volatile uint8_t sic2_5v_ok
Bit #1.
Definition drv_can.c:61
volatile uint8_t spi_checksum_ok
Bit #12.
Definition drv_can.c:72
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 uint8_t sic4
Bit 6:7
Definition drv_i2c.h:88
struct SIC_FLAGS_s::@6::@8 bits
volatile uint8_t sic2
Bit 2:3
Definition drv_i2c.h:86
volatile uint8_t sic3
Bit 4:5
Definition drv_i2c.h:87
volatile uint8_t sic1
Bit 0:1
Definition drv_i2c.h:85
volatile uint8_t _24v
Bit 4:5 -> 0 = no fault.
Definition drv_i2c.h:102
struct AUX_FLAGS_s::@9::@11 bits
volatile uint8_t _5v
Bit 0:1 -> 0 = no fault
Definition drv_i2c.h:100
volatile uint8_t _12v
Bit 2:3 -> 0 = no fault.
Definition drv_i2c.h:101
uint8_t array[I2C_WRITE_BUFFER_SIZE]
Definition drv_i2c.h:126
struct SIC_CLIENT_RECEIVE_FRAME_s::@12::@14 bytes
SIC_FLAGS_t sic_status_flags
Definition drv_i2c.h:116
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