333 lines
7.3 KiB
C
333 lines
7.3 KiB
C
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file : main.c
|
|
* @brief : Main program body
|
|
******************************************************************************
|
|
* @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 "main.h"
|
|
#include "can.h"
|
|
#include "tim.h"
|
|
#include "usart.h"
|
|
#include "gpio.h"
|
|
|
|
/* Private includes ----------------------------------------------------------*/
|
|
/* USER CODE BEGIN Includes */
|
|
|
|
#include "usart_tools.h"
|
|
|
|
/* USER CODE END Includes */
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
/* USER CODE BEGIN PTD */
|
|
|
|
/* USER CODE END PTD */
|
|
|
|
/* Private define ------------------------------------------------------------*/
|
|
/* USER CODE BEGIN PD */
|
|
/* USER CODE END PD */
|
|
|
|
/* Private macro -------------------------------------------------------------*/
|
|
/* USER CODE BEGIN PM */
|
|
|
|
/* USER CODE END PM */
|
|
|
|
/* Private variables ---------------------------------------------------------*/
|
|
|
|
/* USER CODE BEGIN PV */
|
|
|
|
|
|
uint16_t Mode=8;
|
|
uint8_t pissa[4]={0,0,0,0};
|
|
|
|
int fif0_cownt=0;
|
|
int eror_cownt=0;
|
|
int late_cownt=0;
|
|
int trai_cownt=0;
|
|
|
|
|
|
|
|
/* USER CODE END PV */
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
void SystemClock_Config(void);
|
|
/* USER CODE BEGIN PFP */
|
|
|
|
/* USER CODE END PFP */
|
|
|
|
/* Private user code ---------------------------------------------------------*/
|
|
/* USER CODE BEGIN 0 */
|
|
|
|
/* USER CODE END 0 */
|
|
|
|
/**
|
|
* @brief The application entry point.
|
|
* @retval int
|
|
*/
|
|
int main(void)
|
|
{
|
|
/* USER CODE BEGIN 1 */
|
|
|
|
UART_HandleTypeDef * uart;
|
|
|
|
uint16_t kakka[4]={0x12,0x34,0x56,0x78};
|
|
uint16_t poppa[4][4];
|
|
|
|
uint16_t i,j;
|
|
|
|
modbus[0]=100;
|
|
|
|
|
|
XPEH xpen;
|
|
|
|
/* USER CODE END 1 */
|
|
|
|
/* MCU Configuration--------------------------------------------------------*/
|
|
|
|
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
|
HAL_Init();
|
|
|
|
/* USER CODE BEGIN Init */
|
|
|
|
/* USER CODE END Init */
|
|
|
|
/* Configure the system clock */
|
|
SystemClock_Config();
|
|
|
|
/* USER CODE BEGIN SysInit */
|
|
|
|
/* USER CODE END SysInit */
|
|
|
|
/* Initialize all configured peripherals */
|
|
MX_GPIO_Init();
|
|
MX_USART3_UART_Init();
|
|
MX_USART2_UART_Init();
|
|
MX_CAN2_Init();
|
|
MX_TIM1_Init();
|
|
/* USER CODE BEGIN 2 */
|
|
|
|
if(HAL_CAN_Start(&hcan2) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
HAL_TIM_Base_Start(&htim1);
|
|
|
|
HAL_UART_user_setup();
|
|
|
|
/* USER CODE END 2 */
|
|
|
|
/* Infinite loop */
|
|
/* USER CODE BEGIN WHILE */
|
|
while (1)
|
|
{
|
|
/* USER CODE END WHILE */
|
|
|
|
/* USER CODE BEGIN 3 */
|
|
|
|
pissa[0]++; if(pissa[0]<0)pissa[0]=0x7F; if(pissa[0]>0x7F)pissa[0]=0;
|
|
pissa[1]--; if(pissa[1]>0x7F)pissa[1]=0x7F; if(pissa[1]>0x7F)pissa[1]=0;
|
|
|
|
// HAL_UART_Transmit_IT(&huart3,pissa,4);
|
|
|
|
|
|
// if (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_RXNE))
|
|
|
|
// if(HAL_UART_Receive_IT(&huart3, poppa, 1) == HAL_OK )
|
|
|
|
// if(HAL_UART_Receive_IT_user_handler())
|
|
|
|
// for(i=0;i<8;i++)
|
|
|
|
/*
|
|
if(UART_get_command(&huart3) == CMD_PEEK)
|
|
{
|
|
peek(&huart3);
|
|
}
|
|
*/
|
|
|
|
// if(!pissa[0]) Led_0_TGL;
|
|
|
|
|
|
for(i=0;i<2;i++)
|
|
{
|
|
if(i)uart = &huart2;
|
|
else uart = &huart3;
|
|
|
|
j = UART_get_command(uart);
|
|
|
|
if(j)
|
|
{
|
|
if(i)Led_2_TGL;
|
|
else Led_3_TGL;
|
|
switch(j)
|
|
{
|
|
case CMD_INITLOAD: initload(uart); break;
|
|
case CMD_LOAD: load(uart); break;
|
|
case CMD_PEEK: peek(uart); break;
|
|
case CMD_POKE: poke(uart); break;
|
|
case CMD_EXTEND: extendbios(uart); break;
|
|
case CMD_MODBUS_3: ReceiveCommandModbus3(uart);break;
|
|
case CMD_MODBUS_6: ReceiveCommandModbus6(uart);break;
|
|
|
|
default: break;
|
|
} } }
|
|
|
|
/*
|
|
HAL_UART_Transmit_IT(&huart2,pissa,4);
|
|
HAL_UART_Transmit_IT(&huart3,pissa,4);
|
|
|
|
CAN_send((uint16_t *)pissa, 0);
|
|
|
|
Led_0_TGL;
|
|
RS485_out;
|
|
|
|
HAL_Delay(1000);
|
|
*/
|
|
|
|
}
|
|
/* USER CODE END 3 */
|
|
}
|
|
|
|
/**
|
|
* @brief System Clock Configuration
|
|
* @retval None
|
|
*/
|
|
void SystemClock_Config(void)
|
|
{
|
|
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
|
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
|
|
|
/** Configure the main internal regulator output voltage
|
|
*/
|
|
__HAL_RCC_PWR_CLK_ENABLE();
|
|
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
|
|
|
|
/** Initializes the RCC Oscillators according to the specified parameters
|
|
* in the RCC_OscInitTypeDef structure.
|
|
*/
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
|
|
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
|
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
|
|
RCC_OscInitStruct.PLL.PLLM = 12;
|
|
RCC_OscInitStruct.PLL.PLLN = 160;
|
|
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
|
RCC_OscInitStruct.PLL.PLLQ = 4;
|
|
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/** Initializes the CPU, AHB and APB buses clocks
|
|
*/
|
|
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
|
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
|
|
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
|
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
|
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
|
|
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
|
|
|
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
}
|
|
|
|
/* USER CODE BEGIN 4 */
|
|
|
|
void Millisecond()
|
|
{
|
|
static int powse=500;
|
|
static int last=0;
|
|
|
|
|
|
if( hcan2.ErrorCode & (HAL_CAN_ERROR_TX_ALST0|HAL_CAN_ERROR_TX_TERR0))
|
|
if ((hcan2.Instance->TSR) & CAN_TSR_TME0)
|
|
{
|
|
late_cownt++;
|
|
CAN_send((uint16_t *)pissa, 0);
|
|
modbus[26] = late_cownt;
|
|
|
|
}
|
|
|
|
if(powse) powse--;
|
|
else
|
|
{
|
|
powse=modbus[0];
|
|
|
|
if(fif0_cownt)
|
|
{
|
|
modbus[27] = fif0_cownt;
|
|
|
|
eror_cownt++;
|
|
|
|
if(fif0_cownt == last)
|
|
{
|
|
eror_cownt++;
|
|
fif0_cownt=0;
|
|
}
|
|
modbus[25] = eror_cownt;
|
|
}
|
|
|
|
if(!fif0_cownt)
|
|
{
|
|
fif0_cownt=25;
|
|
CAN_send((uint16_t *)pissa, 0);
|
|
trai_cownt++;
|
|
modbus[24] = trai_cownt;
|
|
fif0_cownt--;
|
|
Led_0_TGL;
|
|
|
|
}
|
|
|
|
last=fif0_cownt;
|
|
|
|
} }
|
|
|
|
/* USER CODE END 4 */
|
|
|
|
/**
|
|
* @brief This function is executed in case of error occurrence.
|
|
* @retval None
|
|
*/
|
|
void Error_Handler(void)
|
|
{
|
|
/* USER CODE BEGIN Error_Handler_Debug */
|
|
/* User can add his own implementation to report the HAL error return state */
|
|
__disable_irq();
|
|
while (1)
|
|
{
|
|
}
|
|
/* USER CODE END Error_Handler_Debug */
|
|
}
|
|
|
|
#ifdef USE_FULL_ASSERT
|
|
/**
|
|
* @brief Reports the name of the source file and the source line number
|
|
* where the assert_param error has occurred.
|
|
* @param file: pointer to the source file name
|
|
* @param line: assert_param error line source number
|
|
* @retval None
|
|
*/
|
|
void assert_failed(uint8_t *file, uint32_t line)
|
|
{
|
|
/* USER CODE BEGIN 6 */
|
|
/* User can add his own implementation to report the file name and line number,
|
|
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
|
/* USER CODE END 6 */
|
|
}
|
|
#endif /* USE_FULL_ASSERT */
|