From 167de72ea4be187cb218355a17c7dbe9fffaddf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=A8?= =?UTF-8?q?=D1=82=D0=B5=D0=B9=D0=B1=D0=B5=D0=B7=D0=B0=D0=BD=D0=B4=D1=82?= Date: Wed, 20 Nov 2024 14:22:35 +0300 Subject: [PATCH] Can define settings --- Core/Inc/requester.h | 12 +++++++++++- Core/Src/can.c | 2 +- Core/Src/requester.c | 28 ++++++++++++++-------------- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/Core/Inc/requester.h b/Core/Inc/requester.h index 4a7b84c..223b65e 100644 --- a/Core/Inc/requester.h +++ b/Core/Inc/requester.h @@ -10,10 +10,20 @@ #define ONLINE 1; #define OFFLINE 0; - // TIM defines #define TIM_REQUESTER_Prescaler 64000 +//CAN Settings +#define _HCAN hcan + +//CAN Speed +#define CAN_SPEED_PRSC_1000KB 2 +#define CAN_SPEED_PRSC_500KB 4 +#define CAN_SPEED_PRSC_250KB 8 +#define CAN_SPEED_PRSC_125KB 16 +#define CAN_SPEED_PRSC_50KB 40 +#define CAN_CURRENT_SPEED CAN_SPEED_PRSC_250KB + //CAN Filter #define ID_MAIN_DEVICE 0x000 #define CURRENT_ID_DEVICE 0x002 diff --git a/Core/Src/can.c b/Core/Src/can.c index 176c579..89624a2 100644 --- a/Core/Src/can.c +++ b/Core/Src/can.c @@ -38,7 +38,7 @@ void MX_CAN_Init(void) /* USER CODE END CAN_Init 1 */ hcan.Instance = CAN1; - hcan.Init.Prescaler = 2; + hcan.Init.Prescaler = CAN_CURRENT_SPEED; hcan.Init.Mode = CAN_MODE_NORMAL; hcan.Init.SyncJumpWidth = CAN_SJW_1TQ; hcan.Init.TimeSeg1 = CAN_BS1_13TQ; diff --git a/Core/Src/requester.c b/Core/Src/requester.c index 834aa24..9661711 100644 --- a/Core/Src/requester.c +++ b/Core/Src/requester.c @@ -44,9 +44,9 @@ void REQUESTER_Init(void) { HAL_Init(); MX_CAN_Init(); - HAL_CAN_Start(&hcan); + HAL_CAN_Start(&_HCAN); REQUESTER_CAN_FILTERS(); - HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING); + HAL_CAN_ActivateNotification(&_HCAN, CAN_IT_RX_FIFO0_MSG_PENDING); ControlFlags.IsPulse = 1; MX_TIM7_Init(); MX_RTC_Init(); @@ -200,7 +200,7 @@ __weak HAL_StatusTypeDef CanRequestToAnalogUniversal(struct RXMsg _rxMsg) data[3] = 'V'; data[4] = 'E'; data[5] = 'R'; - return HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox); + return HAL_CAN_AddTxMessage(&_HCAN, &TxHeader, data, &TxMailBox); } __weak HAL_StatusTypeDef CanRequestToAnalogUSTAVKI(struct RXMsg _rxMsg) @@ -223,7 +223,7 @@ __weak HAL_StatusTypeDef CanRequestToAnalogUSTAVKI(struct RXMsg _rxMsg) data[4] = 'V'; data[5] = 'K'; data[6] = 'I'; - return HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox); + return HAL_CAN_AddTxMessage(&_HCAN, &TxHeader, data, &TxMailBox); } __weak HAL_StatusTypeDef CanRequestToAnalogUSens(struct RXMsg _rxMsg) @@ -245,7 +245,7 @@ __weak HAL_StatusTypeDef CanRequestToAnalogUSens(struct RXMsg _rxMsg) data[3] = 'e'; data[4] = 'n'; data[5] = 's'; - return HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox); + return HAL_CAN_AddTxMessage(&_HCAN, &TxHeader, data, &TxMailBox); } __weak HAL_StatusTypeDef CanRequestToAnalogISens(struct RXMsg _rxMsg) @@ -267,7 +267,7 @@ __weak HAL_StatusTypeDef CanRequestToAnalogISens(struct RXMsg _rxMsg) data[3] = 'e'; data[4] = 'n'; data[5] = 's'; - return HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox); + return HAL_CAN_AddTxMessage(&_HCAN, &TxHeader, data, &TxMailBox); } __weak HAL_StatusTypeDef CanRequestToAnalogTSens(struct RXMsg _rxMsg) @@ -289,7 +289,7 @@ __weak HAL_StatusTypeDef CanRequestToAnalogTSens(struct RXMsg _rxMsg) data[3] = 'e'; data[4] = 'n'; data[5] = 's'; - return HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox); + return HAL_CAN_AddTxMessage(&_HCAN, &TxHeader, data, &TxMailBox); } /** @@ -358,7 +358,7 @@ __weak HAL_StatusTypeDef CanRequestToBroadcastStatus(struct RXMsg _rxMsg) data[5] = DateToUpdate.Date; data[6] = DateToUpdate.WeekDay; - return HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox); + return HAL_CAN_AddTxMessage(&_HCAN, &TxHeader, data, &TxMailBox); } __weak void CanRequestToBroadcastOnOff(struct RXMsg _rxMsg) @@ -713,7 +713,7 @@ void REQUESTER_Pulse_TIM_Handler() PulseStage = 0; } data[0] = PulseStage++; - HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox); + HAL_CAN_AddTxMessage(&_HCAN, &TxHeader, data, &TxMailBox); } } @@ -768,7 +768,7 @@ void REQUESTER_CAN_FILTERS() canFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0; canFilterConfig.FilterActivation = ENABLE; canFilterConfig.SlaveStartFilterBank = 14; - if(HAL_CAN_ConfigFilter(&hcan, &canFilterConfig) != HAL_OK) + if(HAL_CAN_ConfigFilter(&_HCAN, &canFilterConfig) != HAL_OK) { Error_Handler(); } @@ -782,7 +782,7 @@ void REQUESTER_CAN_FILTERS() /*Маска 1.1111.1110.<...>. Нули - любые символы. Единицы - точное соответствие фильтру выше.*/ canFilterConfig.FilterMaskIdHigh = (uint16_t)(CAN_DEVICE_ID_FILTER>>13); canFilterConfig.FilterMaskIdLow = (uint16_t)(CAN_DEVICE_ID_FILTER<<3) | CAN_IDE_32; - if(HAL_CAN_ConfigFilter(&hcan, &canFilterConfig) != HAL_OK) + if(HAL_CAN_ConfigFilter(&_HCAN, &canFilterConfig) != HAL_OK) { Error_Handler(); } @@ -793,7 +793,7 @@ void REQUESTER_CAN_FILTERS() canFilterConfig.FilterIdLow = (uint16_t)(0x03000000<<5) | CAN_IDE_32; canFilterConfig.FilterMaskIdHigh = (uint16_t)(CAN_DATA_TYPE_FILTER>>13); // we're checking only high 13 bits, that contained "key" canFilterConfig.FilterMaskIdLow = (uint16_t)(CAN_DATA_TYPE_FILTER<<3) | CAN_IDE_32; // 1<<2 - set IDE bit - if(HAL_CAN_ConfigFilter(&hcan, &canFilterConfig) != HAL_OK) + if(HAL_CAN_ConfigFilter(&_HCAN, &canFilterConfig) != HAL_OK) { Error_Handler(); } @@ -805,7 +805,7 @@ void REQUESTER_CAN_FILTERS() canFilterConfig.FilterIdLow = (uint16_t)(HighIdFilter(DATA_TYPE_PULSE)<<5) | CAN_IDE_32; canFilterConfig.FilterMaskIdHigh = (uint16_t)(CAN_DATA_TYPE_FILTER>>13); // we're checking only high 13 bits, that contained "key" canFilterConfig.FilterMaskIdLow = (uint16_t)(CAN_DATA_TYPE_FILTER<<3) | CAN_IDE_32; // 1<<2 - set IDE bit - if(HAL_CAN_ConfigFilter(&hcan, &canFilterConfig) != HAL_OK) + if(HAL_CAN_ConfigFilter(&_HCAN, &canFilterConfig) != HAL_OK) { Error_Handler(); } @@ -816,7 +816,7 @@ void REQUESTER_CAN_FILTERS() canFilterConfig.FilterIdLow = (uint16_t)(HighIdFilter(0)<<5) | CAN_IDE_32; canFilterConfig.FilterMaskIdHigh = (uint16_t)(0>>13); // we're checking only high 13 bits, that contained "key" canFilterConfig.FilterMaskIdLow = (uint16_t)(0<<3) | CAN_IDE_32; // 1<<2 - set IDE bit - if(HAL_CAN_ConfigFilter(&hcan, &canFilterConfig) != HAL_OK) + if(HAL_CAN_ConfigFilter(&_HCAN, &canFilterConfig) != HAL_OK) { Error_Handler(); }