/*
 * j1939.h
 *
 *  Created on: May 3, 2024
 *      Author: colorbass
 */

#ifndef INC_J1939_H_
#define INC_J1939_H_

#define J_ID_SE 0x56
#define J_ID_EV 0xF4

#include "main.h"

extern CAN_HandleTypeDef hcan1;

typedef struct{
	uint8_t data[256]; 	//data array
	uint32_t PGN; 			//received PGN
	uint16_t size;
	uint8_t packets;
	uint8_t packet;
	uint8_t step;
	uint8_t step_cts_remain;
	uint8_t state; //(0 = standby, 1= receiving)
	uint32_t tick;
}j_receive_t;

typedef struct{
	uint32_t ExtId;
	uint8_t DLC;
	uint8_t data[8];
	uint32_t tick;
}j1939_rx_frame_t;

typedef struct{
	uint32_t ExtId;
	uint8_t DLC;
	uint8_t data[8];
}j1939_tx_frame_t;

extern j_receive_t j_rx;


void J_SendCTS(j_receive_t rx);
void J_SendACK(j_receive_t rx);
void J1939_ExchangeRxBuffer(void);
void J1939_ExchangeTxBuffer(void);
void J1939_InitBuffers(void);
uint16_t J1939_GetRxBufferCount(void);
uint16_t J1939_GetTxBufferCount(void);
uint32_t J1939_GetRxOverflowCount(void);
uint32_t J1939_GetTxOverflowCount(void);


void GBT_CAN_ReInit();
void GBT_CAN_FilterInit();

#endif /* INC_J1939_H_ */
