178 lines
4.7 KiB
C
178 lines
4.7 KiB
C
//
|
|
// Created by cfif on 17.03.2024.
|
|
//
|
|
#include "CanSerialPorts.h"
|
|
#include "SerialPorts.h"
|
|
|
|
tCanSerialPorts CAN_SERIAL_PORTS;
|
|
|
|
void CanHWControlSet(void) {
|
|
GPIO_InitType GPIO_InitStructure;
|
|
GPIO_InitStruct(&GPIO_InitStructure);
|
|
RCC_EnableAPB2PeriphClk( RCC_APB2_PERIPH_GPIOC | RCC_APB2_PERIPH_GPIOD, ENABLE);
|
|
|
|
// STB
|
|
GPIO_InitStructure.Pin = GPIO_PIN_2;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
|
GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);
|
|
GPIO_ResetBits(GPIOC, GPIO_PIN_2);
|
|
|
|
// STB
|
|
GPIO_InitStructure.Pin = GPIO_PIN_9;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
|
GPIO_InitPeripheral(GPIOD, &GPIO_InitStructure);
|
|
GPIO_ResetBits(GPIOD, GPIO_PIN_9);
|
|
}
|
|
|
|
|
|
void vCanSerialPorts_InitRCC() {
|
|
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_CAN1 | RCC_APB1_PERIPH_CAN2, ENABLE);
|
|
}
|
|
|
|
void CanPort_SetSpeed(CAN_Module *CANx, eCanBaudRate canBaudRate) {
|
|
|
|
if (CANx == CAN1) {
|
|
vCanSerialPortFrameInit(
|
|
&SERIAL_PORTS.SerialPortFrameCan1,
|
|
CAN1,
|
|
canBaudRate,
|
|
0x57,
|
|
CAN_STD_ID,
|
|
0,
|
|
20,
|
|
0,
|
|
20,
|
|
0
|
|
);
|
|
}
|
|
|
|
if (CANx == CAN2) {
|
|
vCanSerialPortFrameInit(
|
|
&SERIAL_PORTS.SerialPortFrameCan2,
|
|
CAN2,
|
|
canBaudRate,
|
|
0x57,
|
|
CAN_STD_ID,
|
|
0,
|
|
20,
|
|
0,
|
|
20,
|
|
0
|
|
);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
void USB_LP_CAN1_RX0_IRQHandler(void) {
|
|
CanSerialPortFrameIrqRxProcessing0(&SERIAL_PORTS.SerialPortFrameCan1);
|
|
}
|
|
|
|
void CAN1_RX1_IRQHandler(void) {
|
|
CanSerialPortFrameIrqRxProcessing1(&SERIAL_PORTS.SerialPortFrameCan1);
|
|
}
|
|
|
|
void CAN1_SCE_IRQHandler(void) {
|
|
|
|
__IO uint32_t err_index = 0;
|
|
if (CAN_GetFlagSTS(CAN1, CAN_FLAG_LEC) != RESET) {
|
|
err_index = CAN1->ESTS & 0x70;
|
|
CAN_ClearFlag(CAN1, CAN_FLAG_LEC);
|
|
if (err_index == 0x00000010) {
|
|
CAN_DeInit(CAN1);
|
|
CanPort_SetSpeed(CAN1, CAN_SERIAL_PORTS.canFrame1.canBaudRate);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void vCanSerialPort_InitCAN1(tCanSerialPortFrameNation *env) {
|
|
GPIO_InitType GPIO_InitStructure;
|
|
GPIO_InitStruct(&GPIO_InitStructure);
|
|
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO | RCC_APB2_PERIPH_GPIOB, ENABLE);
|
|
|
|
// Configure CAN1 RX pin
|
|
GPIO_InitStructure.Pin = GPIO_PIN_8;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
|
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
|
|
// Configure CAN1 TX pin
|
|
GPIO_InitStructure.Pin = GPIO_PIN_9;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
|
|
|
|
GPIO_ConfigPinRemap(GPIO_RMP2_CAN1, ENABLE);
|
|
|
|
|
|
|
|
}
|
|
|
|
void CAN2_RX0_IRQHandler(void) {
|
|
CanSerialPortFrameIrqRxProcessing0(&SERIAL_PORTS.SerialPortFrameCan2);
|
|
}
|
|
|
|
void CAN2_RX1_IRQHandler(void) {
|
|
CanSerialPortFrameIrqRxProcessing1(&SERIAL_PORTS.SerialPortFrameCan2);
|
|
}
|
|
|
|
void CAN2_SCE_IRQHandler(void) {
|
|
|
|
__IO uint32_t err_index = CAN_GetFlagSTS(CAN2, CAN_FLAG_LEC);
|
|
if (err_index != RESET) {
|
|
err_index = CAN2->ESTS & 0x70;
|
|
CAN_ClearFlag(CAN2, CAN_FLAG_LEC);
|
|
if (err_index == 0x00000010) {
|
|
CAN_DeInit(CAN2);
|
|
CanPort_SetSpeed(CAN2, CAN_SERIAL_PORTS.canFrame2.canBaudRate);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
void vCanSerialPort_InitCAN2(tCanSerialPortFrameNation *env) {
|
|
GPIO_InitType GPIO_InitStructure;
|
|
GPIO_InitStruct(&GPIO_InitStructure);
|
|
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO | RCC_APB2_PERIPH_GPIOD, ENABLE);
|
|
|
|
GPIO_InitStructure.Pin = GPIO_PIN_10;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
|
GPIO_InitPeripheral(GPIOD, &GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.Pin = GPIO_PIN_11;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitPeripheral(GPIOD, &GPIO_InitStructure);
|
|
|
|
GPIO_ConfigPinRemap(GPIO_RMP3_CAN2, ENABLE);
|
|
|
|
vCanSerialPortFrameInit(
|
|
env,
|
|
CAN2,
|
|
CAN_BAUD_RATE_500K,
|
|
0x57,
|
|
CAN_STD_ID,
|
|
0,
|
|
20,
|
|
0,
|
|
20,
|
|
0
|
|
);
|
|
|
|
|
|
}
|
|
|
|
void CanSerialPorts_Init() {
|
|
tCanSerialPorts *env = &CAN_SERIAL_PORTS;
|
|
|
|
vCanSerialPorts_InitRCC();
|
|
CanHWControlSet();
|
|
|
|
vCanSerialPort_InitCAN1(&env->canFrame1);
|
|
env->canFrame1IO = CanPortFrame_GetIo(&env->canFrame1);
|
|
|
|
vCanSerialPort_InitCAN2(&env->canFrame2);
|
|
env->canFrame2IO = CanPortFrame_GetIo(&env->canFrame2);
|
|
|
|
} |