Init
This commit is contained in:
commit
fcd6ee1708
|
|
@ -0,0 +1,223 @@
|
|||
//
|
||||
// Created by cfif on 03.06.2024.
|
||||
//
|
||||
#include "CanPorts.h"
|
||||
#include "string.h"
|
||||
|
||||
tCanPorts CAN_PORTS;
|
||||
|
||||
void CanPort_SetSpeed(can_type *CANx, eCanBaudRate canBaudRate) {
|
||||
|
||||
if (CANx == CAN1) {
|
||||
vCanSerialPortFrameInit(
|
||||
&CAN_PORTS.Can1,
|
||||
CAN1,
|
||||
canBaudRate,
|
||||
0x57,
|
||||
CAN_STD_ID,
|
||||
0,
|
||||
20,
|
||||
0,
|
||||
20,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
if (CANx == CAN2) {
|
||||
vCanSerialPortFrameInit(
|
||||
&CAN_PORTS.Can2,
|
||||
CAN2,
|
||||
canBaudRate,
|
||||
0x57,
|
||||
CAN_STD_ID,
|
||||
0,
|
||||
20,
|
||||
0,
|
||||
20,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void CanPort_GetSpeed(can_type *CANx, eCanBaudRate *canBaudRate) {
|
||||
if (CANx == CAN1) {
|
||||
*canBaudRate = CAN_PORTS.Can1.canBaudRate;
|
||||
}
|
||||
|
||||
if (CANx == CAN2) {
|
||||
*canBaudRate = CAN_PORTS.Can2.canBaudRate;
|
||||
}
|
||||
|
||||
*canBaudRate = CAN_BAUD_RATE_500K;
|
||||
}
|
||||
|
||||
void CanPort_SetFilter(can_type *CANx,
|
||||
can_filter_init_type *can_filter0_init_struct,
|
||||
can_filter_init_type *can_filter1_init_struct) {
|
||||
|
||||
if (CANx == CAN1) {
|
||||
memcpy(&CAN_PORTS.Can1.can_filter0_init_struct, can_filter0_init_struct, sizeof(can_filter_init_type));
|
||||
memcpy(&CAN_PORTS.Can1.can_filter1_init_struct, can_filter1_init_struct, sizeof(can_filter_init_type));
|
||||
can_filter_init(CAN1, &CAN_PORTS.Can1.can_filter0_init_struct);
|
||||
can_filter_init(CAN1, &CAN_PORTS.Can1.can_filter1_init_struct);
|
||||
//CanPort_SetSpeed(CAN1, CAN_PORTS.Can1.canBaudRate);
|
||||
}
|
||||
|
||||
if (CANx == CAN2) {
|
||||
memcpy(&CAN_PORTS.Can2.can_filter0_init_struct, can_filter0_init_struct, sizeof(can_filter_init_type));
|
||||
memcpy(&CAN_PORTS.Can2.can_filter1_init_struct, can_filter1_init_struct, sizeof(can_filter_init_type));
|
||||
can_filter_init(CAN2, &CAN_PORTS.Can2.can_filter0_init_struct);
|
||||
can_filter_init(CAN2, &CAN_PORTS.Can2.can_filter1_init_struct);
|
||||
//CanPort_SetSpeed(CAN2, CAN_PORTS.Can2.canBaudRate);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void vCanPort_InitCAN1RxTxPin() {
|
||||
gpio_init_type gpio_init_struct;
|
||||
|
||||
// enable the gpio clock
|
||||
crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
|
||||
|
||||
gpio_default_para_init(&gpio_init_struct);
|
||||
|
||||
// configure the can tx, rx pin
|
||||
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
|
||||
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
|
||||
gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init(GPIOD, &gpio_init_struct);
|
||||
|
||||
gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE0, GPIO_MUX_9);
|
||||
gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE1, GPIO_MUX_9);
|
||||
}
|
||||
|
||||
void CAN1_RX0_IRQHandler(void) {
|
||||
CanSerialPortFrameIrqRxProcessing0(&CAN_PORTS.Can1);
|
||||
}
|
||||
|
||||
void CAN1_RX1_IRQHandler(void) {
|
||||
CanSerialPortFrameIrqRxProcessing1(&CAN_PORTS.Can1);
|
||||
}
|
||||
|
||||
static void vCanPort_InitCan1() {
|
||||
vCanPort_InitCAN1RxTxPin();
|
||||
|
||||
CAN_PORTS.Can1.can_filter0_init_struct.filter_activate_enable = TRUE;
|
||||
CAN_PORTS.Can1.can_filter0_init_struct.filter_mode = CAN_FILTER_MODE_ID_MASK;
|
||||
CAN_PORTS.Can1.can_filter0_init_struct.filter_fifo = CAN_FILTER_FIFO0;
|
||||
CAN_PORTS.Can1.can_filter0_init_struct.filter_number = 0;
|
||||
CAN_PORTS.Can1.can_filter0_init_struct.filter_bit = CAN_FILTER_32BIT;
|
||||
CAN_PORTS.Can1.can_filter0_init_struct.filter_id_high = 0;
|
||||
CAN_PORTS.Can1.can_filter0_init_struct.filter_id_low = 0;
|
||||
CAN_PORTS.Can1.can_filter0_init_struct.filter_mask_high = 0;
|
||||
CAN_PORTS.Can1.can_filter0_init_struct.filter_mask_low = 0;
|
||||
|
||||
CAN_PORTS.Can1.can_filter1_init_struct.filter_activate_enable = TRUE;
|
||||
CAN_PORTS.Can1.can_filter1_init_struct.filter_mode = CAN_FILTER_MODE_ID_MASK;
|
||||
CAN_PORTS.Can1.can_filter1_init_struct.filter_fifo = CAN_FILTER_FIFO1;
|
||||
CAN_PORTS.Can1.can_filter1_init_struct.filter_number = 1;
|
||||
CAN_PORTS.Can1.can_filter1_init_struct.filter_bit = CAN_FILTER_32BIT;
|
||||
CAN_PORTS.Can1.can_filter1_init_struct.filter_id_high = 0;
|
||||
CAN_PORTS.Can1.can_filter1_init_struct.filter_id_low = 0;
|
||||
CAN_PORTS.Can1.can_filter1_init_struct.filter_mask_high = 0;
|
||||
CAN_PORTS.Can1.can_filter1_init_struct.filter_mask_low = 0;
|
||||
|
||||
CanPort_SetSpeed(CAN1, CAN_BAUD_RATE_500K);
|
||||
}
|
||||
|
||||
void CAN1_SE_IRQHandler(void) {
|
||||
__IO uint32_t err_index = 0;
|
||||
if (can_flag_get(CAN1, CAN_ETR_FLAG) != RESET) {
|
||||
err_index = CAN1->ests & 0x70;
|
||||
can_flag_clear(CAN1, CAN_ETR_FLAG);
|
||||
if (err_index == 0x00000010) {
|
||||
can_reset(CAN1);
|
||||
CanPort_SetSpeed(CAN1, CAN_PORTS.Can1.canBaudRate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void vCanPort_InitCAN2RxTxPin() {
|
||||
gpio_init_type gpio_init_struct;
|
||||
|
||||
// enable the gpio clock
|
||||
crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
|
||||
|
||||
gpio_default_para_init(&gpio_init_struct);
|
||||
|
||||
// configure the can tx, rx pin
|
||||
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
|
||||
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
|
||||
gpio_init_struct.gpio_pins = GPIO_PINS_5 | GPIO_PINS_6;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init(GPIOB, &gpio_init_struct);
|
||||
|
||||
gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE5, GPIO_MUX_9);
|
||||
gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE6, GPIO_MUX_9);
|
||||
}
|
||||
|
||||
void CAN2_RX0_IRQHandler(void) {
|
||||
CanSerialPortFrameIrqRxProcessing0(&CAN_PORTS.Can2);
|
||||
}
|
||||
|
||||
void CAN2_RX1_IRQHandler(void) {
|
||||
CanSerialPortFrameIrqRxProcessing1(&CAN_PORTS.Can2);
|
||||
}
|
||||
|
||||
static void vCanPort_InitCan2() {
|
||||
vCanPort_InitCAN2RxTxPin();
|
||||
|
||||
CAN_PORTS.Can2.can_filter0_init_struct.filter_activate_enable = TRUE;
|
||||
CAN_PORTS.Can2.can_filter0_init_struct.filter_mode = CAN_FILTER_MODE_ID_MASK;
|
||||
CAN_PORTS.Can2.can_filter0_init_struct.filter_fifo = CAN_FILTER_FIFO0;
|
||||
CAN_PORTS.Can2.can_filter0_init_struct.filter_number = 0;
|
||||
CAN_PORTS.Can2.can_filter0_init_struct.filter_bit = CAN_FILTER_32BIT;
|
||||
CAN_PORTS.Can2.can_filter0_init_struct.filter_id_high = 0;
|
||||
CAN_PORTS.Can2.can_filter0_init_struct.filter_id_low = 0;
|
||||
CAN_PORTS.Can2.can_filter0_init_struct.filter_mask_high = 0;
|
||||
CAN_PORTS.Can2.can_filter0_init_struct.filter_mask_low = 0;
|
||||
|
||||
CAN_PORTS.Can2.can_filter1_init_struct.filter_activate_enable = TRUE;
|
||||
CAN_PORTS.Can2.can_filter1_init_struct.filter_mode = CAN_FILTER_MODE_ID_MASK;
|
||||
CAN_PORTS.Can2.can_filter1_init_struct.filter_fifo = CAN_FILTER_FIFO1;
|
||||
CAN_PORTS.Can2.can_filter1_init_struct.filter_number = 1;
|
||||
CAN_PORTS.Can2.can_filter1_init_struct.filter_bit = CAN_FILTER_32BIT;
|
||||
CAN_PORTS.Can2.can_filter1_init_struct.filter_id_high = 0;
|
||||
CAN_PORTS.Can2.can_filter1_init_struct.filter_id_low = 0;
|
||||
CAN_PORTS.Can2.can_filter1_init_struct.filter_mask_high = 0;
|
||||
CAN_PORTS.Can2.can_filter1_init_struct.filter_mask_low = 0;
|
||||
|
||||
CanPort_SetSpeed(CAN2, CAN_BAUD_RATE_500K);
|
||||
}
|
||||
|
||||
|
||||
void CAN2_SE_IRQHandler(void) {
|
||||
__IO uint32_t err_index = 0;
|
||||
if (can_flag_get(CAN2, CAN_ETR_FLAG) != RESET) {
|
||||
err_index = CAN2->ests & 0x70;
|
||||
can_flag_clear(CAN2, CAN_ETR_FLAG);
|
||||
|
||||
if (err_index == 0x00000010) {
|
||||
// gpio_bits_set(GPIOB, GPIO_PINS_7);
|
||||
// gpio_bits_reset(GPIOB, GPIO_PINS_7);
|
||||
|
||||
can_reset(CAN2);
|
||||
CanPort_SetSpeed(CAN2, CAN_PORTS.Can2.canBaudRate);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CanPorts_Init() {
|
||||
CAN_PORTS.Can1.reInit = false;
|
||||
vCanPort_InitCan1();
|
||||
CAN_PORTS.Can1_IO = CanPortFrame_GetIo(&CAN_PORTS.Can1);
|
||||
|
||||
CAN_PORTS.Can2.reInit = false;
|
||||
vCanPort_InitCan2();
|
||||
CAN_PORTS.Can2_IO = CanPortFrame_GetIo(&CAN_PORTS.Can2);
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
//
|
||||
// Created by cfif on 03.06.2024.
|
||||
//
|
||||
|
||||
#ifndef SMART_COMPONENTS_CANPORTS_H
|
||||
#define SMART_COMPONENTS_CANPORTS_H
|
||||
|
||||
#include "CanSerialPortFrame.h"
|
||||
|
||||
typedef struct {
|
||||
tCanSerialPortFrameArtery Can1;
|
||||
tSerialPortFrameIO Can1_IO;
|
||||
|
||||
tCanSerialPortFrameArtery Can2;
|
||||
tSerialPortFrameIO Can2_IO;
|
||||
} tCanPorts;
|
||||
|
||||
extern tCanPorts CAN_PORTS;
|
||||
|
||||
void CanPorts_Init();
|
||||
|
||||
void CanPort_SetSpeed(can_type *CANx, eCanBaudRate canBaudRate);
|
||||
|
||||
void CanPort_GetSpeed(can_type *CANx, eCanBaudRate *canBaudRate);
|
||||
|
||||
void CanPort_SetFilter(can_type *CANx,
|
||||
can_filter_init_type *can_filter0_init_struct,
|
||||
can_filter_init_type *can_filter1_init_struct);
|
||||
|
||||
#endif //SMART_COMPONENTS_CANPORTS_H
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"cmake": {
|
||||
"inc_dirs": [
|
||||
"./"
|
||||
],
|
||||
"srcs": [
|
||||
"./**.c"
|
||||
]
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue