From a49391b6f46a84677ac480de148ea1989132f626 Mon Sep 17 00:00:00 2001 From: cfif Date: Wed, 4 Dec 2024 13:10:49 +0300 Subject: [PATCH] Init --- CanSerialPorts.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++ CanSerialPorts.h | 24 ++++++++++ modular.json | 17 +++++++ 3 files changed, 159 insertions(+) create mode 100644 CanSerialPorts.c create mode 100644 CanSerialPorts.h create mode 100644 modular.json diff --git a/CanSerialPorts.c b/CanSerialPorts.c new file mode 100644 index 0000000..94dcc6e --- /dev/null +++ b/CanSerialPorts.c @@ -0,0 +1,118 @@ +// +// 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 USB_LP_CAN1_RX0_IRQHandler(void) { + CanSerialPortIrqRxProcessing(&SERIAL_PORTS.SerialPortCan1); +} + +void vCanSerialPort_InitCAN1(tCanSerialPortNation *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); + + vCanSerialPortInit( + env, + CAN1, + CAN_BAUD_RATE_250K, + 0xFF, + CAN_STD_ID, + 0x400, + true, + 0x400, + 0x00, + 512, + 0 + ); + +} + +void CAN2_RX0_IRQHandler(void) { + CanSerialPortIrqRxProcessing(&SERIAL_PORTS.SerialPortCan2); +} + +void vCanSerialPort_InitCAN2(tCanSerialPortNation *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); + + vCanSerialPortInit( + env, + CAN2, + CAN_BAUD_RATE_250K, + 0xFF, + CAN_STD_ID, + 0x400, + true, + 0x400, + 0x00, + 512, + 0 + ); +} + +void CanSerialPorts_Init() { + tCanSerialPorts *env = &CAN_SERIAL_PORTS; + + vCanSerialPorts_InitRCC(); + CanHWControlSet(); + + vCanSerialPort_InitCAN1(&env->can1); + env->can1IO = CanPort_GetIo(&env->can1); + + vCanSerialPort_InitCAN2(&env->can2); + env->can2IO = CanPort_GetIo(&env->can2); + +} \ No newline at end of file diff --git a/CanSerialPorts.h b/CanSerialPorts.h new file mode 100644 index 0000000..587edbf --- /dev/null +++ b/CanSerialPorts.h @@ -0,0 +1,24 @@ +// +// Created by cfif on 17.03.2024. +// +#include "n32g45x.h" +#include "CanSerialPort.h" + +#ifndef NATION_CAN_TEST_CANSERIALPORTS_H +#define NATION_CAN_TEST_CANSERIALPORTS_H + +typedef struct { + tCanSerialPortNation can1; + tSerialPortIO can1IO; + + tCanSerialPortNation can2; + tSerialPortIO can2IO; +} tCanSerialPorts; + +void CanSerialPorts_Init(); +void vCanSerialPort_InitCAN1(tCanSerialPortNation *env); +void vCanSerialPort_InitCAN2(tCanSerialPortNation *env); +extern tCanSerialPorts CAN_SERIAL_PORTS; +void vCanSerialPorts_InitRCC(); +void CanHWControlSet(); +#endif //NATION_CAN_TEST_CANSERIALPORTS_H diff --git a/modular.json b/modular.json new file mode 100644 index 0000000..8cc4431 --- /dev/null +++ b/modular.json @@ -0,0 +1,17 @@ +{ + "dep": [ + { + "type": "git", + "provider": "NAVIGATOR_UVEOS_NATION_TELIT", + "repo": "CanSerialPort_NATION_N32G45x" + } + ], + "cmake": { + "inc_dirs": [ + "./" + ], + "srcs": [ + "*.c" + ] + } +} \ No newline at end of file