добавлен iwdg
обновлена терминалка
This commit is contained in:
@@ -27,6 +27,16 @@ void CANEmu_Init(CANEmu_HandleTypeDef *canemu, uint32_t bitrate) {
|
||||
// GPIOA->CRL &= ~(0xF << (CANEMU_TX_GPIO_PIN * 4));
|
||||
// GPIOA->CRL |= (0x1 << (CANEMU_TX_GPIO_PIN * 4)); // Output push-pull, 10 MHz
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
/*Configure GPIO pin : PB0 */
|
||||
GPIO_InitStruct.Pin = (1<<CANEMU_TX_GPIO_PIN);
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(CANEMU_TX_GPIO_PORT, &GPIO_InitStruct);
|
||||
|
||||
|
||||
// Настройка таймера:
|
||||
// TIM2->PSC = 0; // Предделитель
|
||||
// TIM2->ARR = 0xFFFF;
|
||||
|
||||
@@ -10,7 +10,6 @@ uint32_t can_bits_len = 0;
|
||||
uint32_t err_indices[MAX_ERR_BITS];
|
||||
uint8_t err_indices_count = 0;
|
||||
|
||||
#define FLIP_RAW_BITS
|
||||
|
||||
void append_bit(uint8_t *buf, uint32_t *len, uint8_t bit) {
|
||||
if (*len < MAX_BITS) {
|
||||
@@ -64,13 +63,6 @@ void apply_bit_stuffing_with_error(const uint8_t *src, uint32_t src_len, uint8_t
|
||||
|
||||
// Вспомогательная функция для инверсии бита
|
||||
void flip_bit(uint32_t bit_index) {
|
||||
if (bit_index < raw_len) {
|
||||
can_bits[bit_index] = !can_bits[bit_index];
|
||||
}
|
||||
}
|
||||
|
||||
// Вспомогательная функция для инверсии бита в raw_bits
|
||||
void flip_raw_bit(uint32_t bit_index) {
|
||||
if (bit_index < raw_len) {
|
||||
raw_bits[bit_index] = !raw_bits[bit_index];
|
||||
}
|
||||
@@ -149,7 +141,10 @@ void form_CAN_bitstream_full(const CAN_TxHeaderTypeDef *header, const uint8_t *d
|
||||
// 4. Data field (если не RTR)
|
||||
if (!header->RTR) {
|
||||
uint8_t dlc = header->DLC & 0xF;
|
||||
data_idx = raw_len;
|
||||
|
||||
if(dlc)
|
||||
data_idx = raw_len;
|
||||
|
||||
for (uint8_t i = 0; i < dlc && i < 8; i++) {
|
||||
uint8_t byte_val = data[i];
|
||||
// Time-stamp override on last 2 bytes
|
||||
@@ -173,18 +168,15 @@ void form_CAN_bitstream_full(const CAN_TxHeaderTypeDef *header, const uint8_t *d
|
||||
// 6. CRC Delimiter (recessive)
|
||||
append_bit(raw_bits, &raw_len, 1);
|
||||
|
||||
#ifdef FLIP_RAW_BITS
|
||||
// Ошибки
|
||||
if (errors.FF_SRS) flip_raw_bit(srr_idx);
|
||||
if (errors.FF_IDE) flip_raw_bit(ide_idx);
|
||||
if (errors.FF_RTR) flip_raw_bit(rtr_idx);
|
||||
if (errors.FF_R1) flip_raw_bit(r1_idx);
|
||||
if (errors.FF_R0) flip_raw_bit(r0_idx);
|
||||
if (errors.DATA) flip_raw_bit(data_idx);
|
||||
if (errors.FF_SRS) flip_bit(srr_idx);
|
||||
if (errors.FF_IDE) flip_bit(ide_idx);
|
||||
if (errors.FF_RTR) flip_bit(rtr_idx);
|
||||
if (errors.FF_R1) flip_bit(r1_idx);
|
||||
if (errors.FF_R0) flip_bit(r0_idx);
|
||||
if (errors.DATA) flip_bit(data_idx);
|
||||
// MSGID_ERR — переворачиваем 1-й бит ID (MSB ID[10]) для примера
|
||||
if (errors.MSGID) flip_raw_bit(id_start);
|
||||
|
||||
#endif
|
||||
if (errors.MSGID) flip_bit(id_start);
|
||||
|
||||
// 10. Apply bit stuffing, пропускаем вставку stuff bits если ошибка ERR_STUFF_BITS
|
||||
apply_bit_stuffing_with_error(raw_bits, raw_len, can_bits, &can_bits_len, errors.STUFF_BITS);
|
||||
@@ -200,18 +192,4 @@ void form_CAN_bitstream_full(const CAN_TxHeaderTypeDef *header, const uint8_t *d
|
||||
append_bit(can_bits, &can_bits_len, 1);
|
||||
}
|
||||
|
||||
|
||||
// Ошибки
|
||||
#ifndef FLIP_RAW_BITS
|
||||
if (errors.FF_SRS) flip_bit(srr_idx);
|
||||
if (errors.FF_IDE) flip_bit(ide_idx);
|
||||
if (errors.FF_RTR) flip_bit(rtr_idx);
|
||||
if (errors.FF_R1) flip_bit(r1_idx);
|
||||
if (errors.FF_R0) flip_bit(r0_idx);
|
||||
if (errors.DATA) flip_bit(data_idx);
|
||||
// MSGID_ERR — переворачиваем 1-й бит ID (MSB ID[10]) для примера
|
||||
if (errors.MSGID) flip_bit(id_start);
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
52
Core/Inc/can.h
Normal file
52
Core/Inc/can.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file can.h
|
||||
* @brief This file contains all the function prototypes for
|
||||
* the can.c file
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __CAN_H__
|
||||
#define __CAN_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
extern CAN_HandleTypeDef hcan;
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
|
||||
/* USER CODE END Private defines */
|
||||
|
||||
void MX_CAN_Init(void);
|
||||
|
||||
/* USER CODE BEGIN Prototypes */
|
||||
|
||||
/* USER CODE END Prototypes */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __CAN_H__ */
|
||||
|
||||
52
Core/Inc/iwdg.h
Normal file
52
Core/Inc/iwdg.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file iwdg.h
|
||||
* @brief This file contains all the function prototypes for
|
||||
* the iwdg.c file
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __IWDG_H__
|
||||
#define __IWDG_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
extern IWDG_HandleTypeDef hiwdg;
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
|
||||
/* USER CODE END Private defines */
|
||||
|
||||
void MX_IWDG_Init(void);
|
||||
|
||||
/* USER CODE BEGIN Prototypes */
|
||||
|
||||
/* USER CODE END Prototypes */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __IWDG_H__ */
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
#define HAL_MODULE_ENABLED
|
||||
/*#define HAL_ADC_MODULE_ENABLED */
|
||||
/*#define HAL_CRYP_MODULE_ENABLED */
|
||||
/*#define HAL_CAN_MODULE_ENABLED */
|
||||
#define HAL_CAN_MODULE_ENABLED
|
||||
/*#define HAL_CAN_LEGACY_MODULE_ENABLED */
|
||||
/*#define HAL_CEC_MODULE_ENABLED */
|
||||
/*#define HAL_CORTEX_MODULE_ENABLED */
|
||||
@@ -49,7 +49,7 @@
|
||||
/*#define HAL_I2C_MODULE_ENABLED */
|
||||
/*#define HAL_I2S_MODULE_ENABLED */
|
||||
/*#define HAL_IRDA_MODULE_ENABLED */
|
||||
/*#define HAL_IWDG_MODULE_ENABLED */
|
||||
#define HAL_IWDG_MODULE_ENABLED
|
||||
/*#define HAL_NOR_MODULE_ENABLED */
|
||||
/*#define HAL_NAND_MODULE_ENABLED */
|
||||
/*#define HAL_PCCARD_MODULE_ENABLED */
|
||||
|
||||
@@ -59,7 +59,7 @@ typedef struct //MB_DataInRegsTypeDef
|
||||
uint16_t CAN_ID_LO; // 3
|
||||
uint16_t CAN_DLC; // 4
|
||||
|
||||
uint16_t reserved[11]; // ...
|
||||
uint16_t reserved[11]; // ... 5-15
|
||||
|
||||
uint16_t CAN_DATA_0; // 16
|
||||
uint16_t CAN_DATA_1; // 17
|
||||
@@ -69,6 +69,12 @@ typedef struct //MB_DataInRegsTypeDef
|
||||
uint16_t CAN_DATA_5; // 21
|
||||
uint16_t CAN_DATA_6; // 22
|
||||
uint16_t CAN_DATA_7; // 23
|
||||
|
||||
uint16_t reserved2[8]; // ... 24-31
|
||||
uint16_t reserved3[16*2]; // ... 32-63
|
||||
|
||||
uint16_t DATA_UNIX_HI; // 64
|
||||
uint16_t DATA_UNIX_LO; // 65
|
||||
}MB_DataHoldRegsTypeDef;
|
||||
|
||||
|
||||
|
||||
120
Core/Src/can.c
Normal file
120
Core/Src/can.c
Normal file
@@ -0,0 +1,120 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file can.c
|
||||
* @brief This file provides code for the configuration
|
||||
* of the CAN instances.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "can.h"
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
CAN_HandleTypeDef hcan;
|
||||
|
||||
/* CAN init function */
|
||||
void MX_CAN_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN CAN_Init 0 */
|
||||
|
||||
/* USER CODE END CAN_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN CAN_Init 1 */
|
||||
|
||||
/* USER CODE END CAN_Init 1 */
|
||||
hcan.Instance = CAN1;
|
||||
hcan.Init.Prescaler = 16;
|
||||
hcan.Init.Mode = CAN_MODE_NORMAL;
|
||||
hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
|
||||
hcan.Init.TimeSeg1 = CAN_BS1_1TQ;
|
||||
hcan.Init.TimeSeg2 = CAN_BS2_1TQ;
|
||||
hcan.Init.TimeTriggeredMode = DISABLE;
|
||||
hcan.Init.AutoBusOff = DISABLE;
|
||||
hcan.Init.AutoWakeUp = DISABLE;
|
||||
hcan.Init.AutoRetransmission = DISABLE;
|
||||
hcan.Init.ReceiveFifoLocked = DISABLE;
|
||||
hcan.Init.TransmitFifoPriority = DISABLE;
|
||||
if (HAL_CAN_Init(&hcan) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN CAN_Init 2 */
|
||||
|
||||
/* USER CODE END CAN_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
|
||||
{
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if(canHandle->Instance==CAN1)
|
||||
{
|
||||
/* USER CODE BEGIN CAN1_MspInit 0 */
|
||||
|
||||
/* USER CODE END CAN1_MspInit 0 */
|
||||
/* CAN1 clock enable */
|
||||
__HAL_RCC_CAN1_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
/**CAN GPIO Configuration
|
||||
PA11 ------> CAN_RX
|
||||
PA12 ------> CAN_TX
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_11;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_12;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN CAN1_MspInit 1 */
|
||||
|
||||
/* USER CODE END CAN1_MspInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
|
||||
{
|
||||
|
||||
if(canHandle->Instance==CAN1)
|
||||
{
|
||||
/* USER CODE BEGIN CAN1_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END CAN1_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_CAN1_CLK_DISABLE();
|
||||
|
||||
/**CAN GPIO Configuration
|
||||
PA11 ------> CAN_RX
|
||||
PA12 ------> CAN_TX
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
|
||||
|
||||
/* USER CODE BEGIN CAN1_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END CAN1_MspDeInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
55
Core/Src/iwdg.c
Normal file
55
Core/Src/iwdg.c
Normal file
@@ -0,0 +1,55 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file iwdg.c
|
||||
* @brief This file provides code for the configuration
|
||||
* of the IWDG instances.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2025 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "iwdg.h"
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
IWDG_HandleTypeDef hiwdg;
|
||||
|
||||
/* IWDG init function */
|
||||
void MX_IWDG_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN IWDG_Init 0 */
|
||||
|
||||
/* USER CODE END IWDG_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN IWDG_Init 1 */
|
||||
|
||||
/* USER CODE END IWDG_Init 1 */
|
||||
hiwdg.Instance = IWDG;
|
||||
hiwdg.Init.Prescaler = IWDG_PRESCALER_4;
|
||||
hiwdg.Init.Reload = 4095;
|
||||
if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN IWDG_Init 2 */
|
||||
|
||||
/* USER CODE END IWDG_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
@@ -18,6 +18,8 @@
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
#include "can.h"
|
||||
#include "iwdg.h"
|
||||
#include "tim.h"
|
||||
#include "usart.h"
|
||||
#include "gpio.h"
|
||||
@@ -91,6 +93,8 @@ int main(void)
|
||||
MX_TIM2_Init();
|
||||
MX_TIM3_Init();
|
||||
MX_USART1_UART_Init();
|
||||
MX_CAN_Init();
|
||||
MX_IWDG_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
CANEmu_Init(&hcanemu, 125000);
|
||||
hcanemu.header.StdId = 0x123;
|
||||
@@ -122,10 +126,11 @@ void SystemClock_Config(void)
|
||||
/** Initializes the RCC Oscillators according to the specified parameters
|
||||
* in the RCC_OscInitTypeDef structure.
|
||||
*/
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
|
||||
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
|
||||
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
|
||||
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
||||
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
||||
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
|
||||
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "rs_message.h"
|
||||
#include "iwdg.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@@ -189,7 +190,8 @@ void SysTick_Handler(void)
|
||||
/* USER CODE END SysTick_IRQn 0 */
|
||||
HAL_IncTick();
|
||||
/* USER CODE BEGIN SysTick_IRQn 1 */
|
||||
|
||||
if(hiwdg.Instance != NULL)
|
||||
HAL_IWDG_Refresh(&hiwdg);
|
||||
/* USER CODE END SysTick_IRQn 1 */
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user