This commit is contained in:
claw committed 2026-03-31 13:27:23 +08:00
commit 53c85e94af
393 files changed
+141844

No files matched your search

+58
View File
@@ -0,0 +1,58 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file dma.c
* @brief This file provides code for the configuration
* of all the requested memory to memory DMA transfers.
******************************************************************************
* @attention
*
* Copyright (c) 2026 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 "dma.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/*----------------------------------------------------------------------------*/
/* Configure DMA */
/*----------------------------------------------------------------------------*/
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/**
* Enable DMA controller clock
*/
void MX_DMA_Init(void)
{
/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();
/* DMA interrupt init */
/* DMA1_Channel4_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn);
/* DMA1_Channel5_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
}
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
+172
View File
@@ -0,0 +1,172 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file gpio.c
* @brief This file provides code for the configuration
* of all used GPIO pins.
******************************************************************************
* @attention
*
* Copyright (c) 2026 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 "gpio.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/*----------------------------------------------------------------------------*/
/* Configure GPIO */
/*----------------------------------------------------------------------------*/
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, run_lamp_Pin|comm_lamp_Pin|w5500cs_Pin|re485_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(w5500rst_GPIO_Port, w5500rst_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, o24_Pin|o23_Pin|o22_Pin|o12_Pin
|o11_Pin|o10_Pin|o9_Pin|o8_Pin
|o7_Pin, GPIO_PIN_SET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOE, o21_Pin|o20_Pin|o19_Pin|o18_Pin
|o17_Pin|o16_Pin|o15_Pin|o14_Pin
|o13_Pin, GPIO_PIN_SET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, o6_Pin|o5_Pin|o4_Pin|o3_Pin
|o2_Pin|o1_Pin, GPIO_PIN_SET);
/*Configure GPIO pins : in13_Pin in14_Pin in15_Pin in2_Pin
in1_Pin */
GPIO_InitStruct.Pin = in13_Pin|in14_Pin|in15_Pin|in2_Pin
|in1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
/*Configure GPIO pins : in16_Pin in17_Pin in18_Pin in19_Pin
in20_Pin in21_Pin in22_Pin e2_Pin
c2_Pin b2_Pin a2_Pin a1_Pin */
GPIO_InitStruct.Pin = in16_Pin|in17_Pin|in18_Pin|in19_Pin
|in20_Pin|in21_Pin|in22_Pin|e2_Pin
|c2_Pin|b2_Pin|a2_Pin|a1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pins : in23_Pin in24_Pin rstbtn_Pin */
GPIO_InitStruct.Pin = in23_Pin|in24_Pin|rstbtn_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : run_lamp_Pin comm_lamp_Pin re485_Pin */
GPIO_InitStruct.Pin = run_lamp_Pin|comm_lamp_Pin|re485_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : w5500cs_Pin */
GPIO_InitStruct.Pin = w5500cs_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(w5500cs_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : w5500rst_Pin */
GPIO_InitStruct.Pin = w5500rst_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(w5500rst_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : o24_Pin o23_Pin o22_Pin o12_Pin
o11_Pin o10_Pin o9_Pin o8_Pin
o7_Pin */
GPIO_InitStruct.Pin = o24_Pin|o23_Pin|o22_Pin|o12_Pin
|o11_Pin|o10_Pin|o9_Pin|o8_Pin
|o7_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pins : o21_Pin o20_Pin o19_Pin o18_Pin
o17_Pin o16_Pin o15_Pin o14_Pin
o13_Pin */
GPIO_InitStruct.Pin = o21_Pin|o20_Pin|o19_Pin|o18_Pin
|o17_Pin|o16_Pin|o15_Pin|o14_Pin
|o13_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
/*Configure GPIO pins : o6_Pin o5_Pin o4_Pin o3_Pin
o2_Pin o1_Pin */
GPIO_InitStruct.Pin = o6_Pin|o5_Pin|o4_Pin|o3_Pin
|o2_Pin|o1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/*Configure GPIO pins : g2_Pin f2_Pin b1_Pin c1_Pin
e1_Pin f1_Pin g1_Pin in12_Pin
in11_Pin in10_Pin */
GPIO_InitStruct.Pin = g2_Pin|f2_Pin|b1_Pin|c1_Pin
|e1_Pin|f1_Pin|g1_Pin|in12_Pin
|in11_Pin|in10_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/*Configure GPIO pins : in9_Pin in8_Pin in7_Pin in6_Pin
in5_Pin in4_Pin in3_Pin */
GPIO_InitStruct.Pin = in9_Pin|in8_Pin|in7_Pin|in6_Pin
|in5_Pin|in4_Pin|in3_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
+626
View File
@@ -0,0 +1,626 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 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 "dma.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "w5500/w5500_port.h"
#include "w5500/modbusSlave.h"
#include "w5500/modbus_crc.h"
#include "w5500/loopback.h"
#include "w5500/httpParser.h"
#include "w5500/httpServer.h"
#include "w5500/webpages.h"
#include "w5500/httpUtil.h"
#include "w5500/slmpMaster.h"
#include "w5500/modbusRtuMaster.h"
#include "w5500/loopback2.h"
#include "w5500/rtuMaster.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define FLASH_LAST_PAGE_512 0x0807F800
#define IP_ADDR 20
#define run_lamp_on HAL_GPIO_WritePin(run_lamp_GPIO_Port, run_lamp_Pin, GPIO_PIN_RESET)
#define run_lamp_off HAL_GPIO_WritePin(run_lamp_GPIO_Port, run_lamp_Pin, GPIO_PIN_SET)
#define comm_lamp_on HAL_GPIO_WritePin(comm_lamp_GPIO_Port, comm_lamp_Pin, GPIO_PIN_RESET)
#define comm_lamp_off HAL_GPIO_WritePin(comm_lamp_GPIO_Port, comm_lamp_Pin, GPIO_PIN_SET)
#define RE_SET HAL_GPIO_WritePin(re485_GPIO_Port, re485_Pin, GPIO_PIN_SET)
#define RE_RESET HAL_GPIO_WritePin(re485_GPIO_Port, re485_Pin, GPIO_PIN_RESET)
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint8_t commType = 0; // 0 slmp, 1 omr
_Bool comm1, comm2;
uint16_t comm_cnt, prev_cnt;
uint16_t slmpTimeout = 0;
uint16_t slmpTimegap = 1;
uint16_t slmp_send_loop = 0;
uint8_t w5500_rx[1024];
uint8_t uart_rx[255];
uint8_t uart_tx[255];
uint8_t local_ip[4];
uint8_t slmp_ip[4];
uint16_t slmp_port;
uint16_t in1_word = 0;
uint16_t in2_word = 0;
uint16_t in3_word = 0;
uint16_t in4_word = 0;
uint16_t out_word1 = 0;
uint16_t out_word2 = 0;
////////////////////////////////////////////////////////// 新增变量 //////////////////////////
rtumST readST[10] = { { .slave_id = 0, .slave_address = 1, .plc_address = 2, .size = 3 }, { .slave_id = 4, .slave_address = 5, .plc_address = 6, .size = 7 }, {
.slave_id = 8, .slave_address = 9, .plc_address = 10, .size = 11 }, { .slave_id = 12, .slave_address = 13, .plc_address = 14, .size = 15 }, {
.slave_id = 16, .slave_address = 17, .plc_address = 18, .size = 19 }, { .slave_id = 20, .slave_address = 21, .plc_address = 22, .size = 23 }, {
.slave_id = 24, .slave_address = 25, .plc_address = 26, .size = 27 }, { .slave_id = 28, .slave_address = 29, .plc_address = 30, .size = 31 }, {
.slave_id = 32, .slave_address = 33, .plc_address = 34, .size = 35 }, { .slave_id = 36, .slave_address = 37, .plc_address = 38, .size = 39 } };
rtumST writeST[10] = { { .slave_id = 40, .slave_address = 41, .plc_address = 42, .size = 43 }, { .slave_id = 44, .slave_address = 45, .plc_address = 46, .size =
47 }, { .slave_id = 48, .slave_address = 49, .plc_address = 50, .size = 51 }, { .slave_id = 52, .slave_address = 53, .plc_address = 54, .size = 55 }, {
.slave_id = 56, .slave_address = 57, .plc_address = 58, .size = 59 }, { .slave_id = 60, .slave_address = 61, .plc_address = 62, .size = 63 }, {
.slave_id = 64, .slave_address = 65, .plc_address = 66, .size = 67 }, { .slave_id = 68, .slave_address = 69, .plc_address = 70, .size = 71 }, {
.slave_id = 72, .slave_address = 73, .plc_address = 74, .size = 75 }, { .slave_id = 76, .slave_address = 77, .plc_address = 78, .size = 79 } };
rtumST myRtumST[20];
RtuMaster master = { .localStartIdx = 1400 };
////////////////////////////////////////////////////////// 新增变量 //////////////////////////
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE {
HAL_UART_Transmit(&huart1, (uint8_t*) &ch, 1, 0xFFFF);
return ch;
}
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/**
* uint16_t data_array[] = {0x1234, 0x5678, 0x9ABC};
* write_flash_u16(data_array, 3);
*/
void write_flash_u16(uint16_t *data, uint32_t length, uint32_t user_save_flash) {
HAL_FLASH_Unlock();
FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t SectorError;
// 擦除扇区
EraseInitStruct.TypeErase = TYPEERASE_PAGES;
EraseInitStruct.PageAddress = user_save_flash;
EraseInitStruct.NbPages = 1;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
// 擦除错误处理
HAL_FLASH_Lock();
Error_Handler();
}
// 写入数据
for (uint32_t i = 0; i < length; i += 2) {
uint32_t word = 0;
if (i + 1 < length) {
// 将两�??????? u16 组合成一�??????? u32
word = ((uint32_t) data[i + 1] << 16) | data[i];
} else {
// 如果数据长度是奇数,�???????后一�??????? u16 �???????要单独处�???????
word = data[i];
}
// 写入组合好的 32 位数�???????
if (HAL_FLASH_Program(TYPEPROGRAM_WORD, user_save_flash, word) != HAL_OK) {
HAL_FLASH_Lock();
Error_Handler();
}
user_save_flash += 4; // 4 字节为一个单位,移动到下�???????个地�???????
}
HAL_FLASH_Lock(); // 写完后锁�??????? Flash
}
/**
* uint16_t data_array[3];
* read_flash_u16(data_array, 3);
*/
void read_flash_u16(uint16_t *data, uint32_t length, uint32_t user_save_flash) {
for (uint32_t i = 0; i < length; i += 2) {
uint32_t word = *(uint32_t*) user_save_flash; // �??????? Flash 中读�??????? 32 位数�???????
// 拆分 32 位数据为两个 16 位数�???????
data[i] = (uint16_t) (word & 0xFFFF); // �??????? 16 �???????
if (i + 1 < length) {
data[i + 1] = (uint16_t) ((word >> 16) & 0xFFFF); // �??????? 16 �???????
}
user_save_flash += 4; // 每次读取后,移动到下�???????�??????? 32 位地�???????
}
}
union {
uint16_t u16;
uint8_t u8[2];
} u16_u8;
void load_flash() {
uint16_t data_array[10];
read_flash_u16(data_array, 10, FLASH_LAST_PAGE_512);
set_register_range(20, 10, data_array);
}
_Bool check_ipv4(uint8_t ip[4]) {
for (int i = 0; i < 4; i++) {
if (ip[i] >= 255 || ip[i] == 0) {
return 0;
}
}
return 1;
}
//////////////////////////////////////////////////////////// ↓定时器(TIME6=1MS↓ /////////////////////////////////////////////
uint16_t timeout_beat, rtu_master_timeout, rstbtn_timeout, runbeat;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim == &htim6) {
if (master.timeout < 0xffff) {
master.timeout++;
}
if (slmpTimeout < 0xffff) {
slmpTimeout++;
}
if (HAL_GPIO_ReadPin(rstbtn_GPIO_Port, rstbtn_Pin) == GPIO_PIN_RESET) {
if (rstbtn_timeout < 0xffff) {
rstbtn_timeout++;
} else {
rstbtn_timeout = 0;
}
}
if (runbeat < 1000) { //控制运行指示灯间隔1秒闪烁
runbeat++;
} else {
runbeat = 0;
HAL_GPIO_TogglePin(run_lamp_GPIO_Port, run_lamp_Pin);
}
}
}
uint16_t flash_reset[100];
void reset_button() {
if (rstbtn_timeout > 3000) {
memset(flash_reset, 0xFF, 200);
Write_To_Flash_uint16(EDIT_FLASH_ADDRESS, flash_reset, 100);
rstbtn_timeout = 0;
}
}
//////////////////////////////////////////////////////////// ↑定时器↑ /////////////////////////////////////////////
//////////////////////////////////////////////////////////// ↓485主站↓ /////////////////////////////////////////////
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
if (huart == &huart1) {
RE_RESET;
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, master.txbuf, 512);
}
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) {
if (huart == &huart1) {
master.rxdone = 1;
master.rxsize = Size;
}
}
//////////////////////////////////////////////////////////// ↑485主站↑ /////////////////////////////////////////////
int getBit(int num, int n) {
return (num & (1 << n)) >> n;
}
void map_out(uint16_t startAddress) {
out_word1 = get_register(startAddress);
out_word2 = get_register(startAddress + 1);
(getBit(out_word1, 0)) ? HAL_GPIO_WritePin(o1_GPIO_Port, o1_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o1_GPIO_Port, o1_Pin, GPIO_PIN_SET);
(getBit(out_word1, 1)) ? HAL_GPIO_WritePin(o2_GPIO_Port, o2_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o2_GPIO_Port, o2_Pin, GPIO_PIN_SET);
(getBit(out_word1, 2)) ? HAL_GPIO_WritePin(o3_GPIO_Port, o3_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o3_GPIO_Port, o3_Pin, GPIO_PIN_SET);
(getBit(out_word1, 3)) ? HAL_GPIO_WritePin(o4_GPIO_Port, o4_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o4_GPIO_Port, o4_Pin, GPIO_PIN_SET);
(getBit(out_word1, 4)) ? HAL_GPIO_WritePin(o5_GPIO_Port, o5_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o5_GPIO_Port, o5_Pin, GPIO_PIN_SET);
(getBit(out_word1, 5)) ? HAL_GPIO_WritePin(o6_GPIO_Port, o6_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o6_GPIO_Port, o6_Pin, GPIO_PIN_SET);
(getBit(out_word1, 6)) ? HAL_GPIO_WritePin(o7_GPIO_Port, o7_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o7_GPIO_Port, o7_Pin, GPIO_PIN_SET);
(getBit(out_word1, 7)) ? HAL_GPIO_WritePin(o8_GPIO_Port, o8_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o8_GPIO_Port, o8_Pin, GPIO_PIN_SET);
(getBit(out_word1, 8)) ? HAL_GPIO_WritePin(o9_GPIO_Port, o9_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o9_GPIO_Port, o9_Pin, GPIO_PIN_SET);
(getBit(out_word1, 9)) ? HAL_GPIO_WritePin(o10_GPIO_Port, o10_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o10_GPIO_Port, o10_Pin, GPIO_PIN_SET);
(getBit(out_word1, 10)) ? HAL_GPIO_WritePin(o11_GPIO_Port, o11_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o11_GPIO_Port, o11_Pin, GPIO_PIN_SET);
(getBit(out_word1, 11)) ? HAL_GPIO_WritePin(o12_GPIO_Port, o12_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o12_GPIO_Port, o12_Pin, GPIO_PIN_SET);
(getBit(out_word1, 12)) ? HAL_GPIO_WritePin(o13_GPIO_Port, o13_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o13_GPIO_Port, o13_Pin, GPIO_PIN_SET);
(getBit(out_word1, 13)) ? HAL_GPIO_WritePin(o14_GPIO_Port, o14_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o14_GPIO_Port, o14_Pin, GPIO_PIN_SET);
(getBit(out_word1, 14)) ? HAL_GPIO_WritePin(o15_GPIO_Port, o15_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o15_GPIO_Port, o15_Pin, GPIO_PIN_SET);
(getBit(out_word1, 15)) ? HAL_GPIO_WritePin(o16_GPIO_Port, o16_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o16_GPIO_Port, o16_Pin, GPIO_PIN_SET);
(getBit(out_word2, 0)) ? HAL_GPIO_WritePin(o17_GPIO_Port, o17_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o17_GPIO_Port, o17_Pin, GPIO_PIN_SET);
(getBit(out_word2, 1)) ? HAL_GPIO_WritePin(o18_GPIO_Port, o18_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o18_GPIO_Port, o18_Pin, GPIO_PIN_SET);
(getBit(out_word2, 2)) ? HAL_GPIO_WritePin(o19_GPIO_Port, o19_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o19_GPIO_Port, o19_Pin, GPIO_PIN_SET);
(getBit(out_word2, 3)) ? HAL_GPIO_WritePin(o20_GPIO_Port, o20_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o20_GPIO_Port, o20_Pin, GPIO_PIN_SET);
(getBit(out_word2, 4)) ? HAL_GPIO_WritePin(o21_GPIO_Port, o21_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o21_GPIO_Port, o21_Pin, GPIO_PIN_SET);
(getBit(out_word2, 5)) ? HAL_GPIO_WritePin(o22_GPIO_Port, o22_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o22_GPIO_Port, o22_Pin, GPIO_PIN_SET);
(getBit(out_word2, 6)) ? HAL_GPIO_WritePin(o23_GPIO_Port, o23_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o23_GPIO_Port, o23_Pin, GPIO_PIN_SET);
(getBit(out_word2, 7)) ? HAL_GPIO_WritePin(o24_GPIO_Port, o24_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o24_GPIO_Port, o24_Pin, GPIO_PIN_SET);
}
void map_in(uint16_t startAddress) {
(HAL_GPIO_ReadPin(in1_GPIO_Port, in1_Pin) == 1) ? (in1_word &= ~(1 << 0)) : (in1_word |= (1 << 0));
(HAL_GPIO_ReadPin(in2_GPIO_Port, in2_Pin) == 1) ? (in1_word &= ~(1 << 1)) : (in1_word |= (1 << 1));
(HAL_GPIO_ReadPin(in3_GPIO_Port, in3_Pin) == 1) ? (in1_word &= ~(1 << 2)) : (in1_word |= (1 << 2));
(HAL_GPIO_ReadPin(in4_GPIO_Port, in4_Pin) == 1) ? (in1_word &= ~(1 << 3)) : (in1_word |= (1 << 3));
(HAL_GPIO_ReadPin(in5_GPIO_Port, in5_Pin) == 1) ? (in1_word &= ~(1 << 4)) : (in1_word |= (1 << 4));
(HAL_GPIO_ReadPin(in6_GPIO_Port, in6_Pin) == 1) ? (in1_word &= ~(1 << 5)) : (in1_word |= (1 << 5));
(HAL_GPIO_ReadPin(in7_GPIO_Port, in7_Pin) == 1) ? (in1_word &= ~(1 << 6)) : (in1_word |= (1 << 6));
(HAL_GPIO_ReadPin(in8_GPIO_Port, in8_Pin) == 1) ? (in1_word &= ~(1 << 7)) : (in1_word |= (1 << 7));
(HAL_GPIO_ReadPin(in9_GPIO_Port, in9_Pin) == 1) ? (in1_word &= ~(1 << 8)) : (in1_word |= (1 << 8));
(HAL_GPIO_ReadPin(in10_GPIO_Port, in10_Pin) == 1) ? (in1_word &= ~(1 << 9)) : (in1_word |= (1 << 9));
(HAL_GPIO_ReadPin(in11_GPIO_Port, in11_Pin) == 1) ? (in1_word &= ~(1 << 10)) : (in1_word |= (1 << 10));
(HAL_GPIO_ReadPin(in12_GPIO_Port, in12_Pin) == 1) ? (in1_word &= ~(1 << 11)) : (in1_word |= (1 << 11));
(HAL_GPIO_ReadPin(in13_GPIO_Port, in13_Pin) == 1) ? (in1_word &= ~(1 << 12)) : (in1_word |= (1 << 12));
(HAL_GPIO_ReadPin(in14_GPIO_Port, in14_Pin) == 1) ? (in1_word &= ~(1 << 13)) : (in1_word |= (1 << 13));
(HAL_GPIO_ReadPin(in15_GPIO_Port, in15_Pin) == 1) ? (in1_word &= ~(1 << 14)) : (in1_word |= (1 << 14));
(HAL_GPIO_ReadPin(in16_GPIO_Port, in16_Pin) == 1) ? (in1_word &= ~(1 << 15)) : (in1_word |= (1 << 15));
(HAL_GPIO_ReadPin(in17_GPIO_Port, in17_Pin) == 1) ? (in2_word &= ~(1 << 0)) : (in2_word |= (1 << 0));
(HAL_GPIO_ReadPin(in18_GPIO_Port, in18_Pin) == 1) ? (in2_word &= ~(1 << 1)) : (in2_word |= (1 << 1));
(HAL_GPIO_ReadPin(in19_GPIO_Port, in19_Pin) == 1) ? (in2_word &= ~(1 << 2)) : (in2_word |= (1 << 2));
(HAL_GPIO_ReadPin(in20_GPIO_Port, in20_Pin) == 1) ? (in2_word &= ~(1 << 3)) : (in2_word |= (1 << 3));
(HAL_GPIO_ReadPin(in21_GPIO_Port, in21_Pin) == 1) ? (in2_word &= ~(1 << 4)) : (in2_word |= (1 << 4));
(HAL_GPIO_ReadPin(in22_GPIO_Port, in22_Pin) == 1) ? (in2_word &= ~(1 << 5)) : (in2_word |= (1 << 5));
(HAL_GPIO_ReadPin(in23_GPIO_Port, in23_Pin) == 1) ? (in2_word &= ~(1 << 6)) : (in2_word |= (1 << 6));
(HAL_GPIO_ReadPin(in24_GPIO_Port, in24_Pin) == 1) ? (in2_word &= ~(1 << 7)) : (in2_word |= (1 << 7));
(HAL_GPIO_ReadPin(a1_GPIO_Port, a1_Pin) == 1) ? (in3_word &= ~(1 << 0)) : (in3_word |= (1 << 0));
(HAL_GPIO_ReadPin(b1_GPIO_Port, b1_Pin) == 1) ? (in3_word &= ~(1 << 1)) : (in3_word |= (1 << 1));
(HAL_GPIO_ReadPin(c1_GPIO_Port, c1_Pin) == 1) ? (in3_word &= ~(1 << 2)) : (in3_word |= (1 << 2));
(HAL_GPIO_ReadPin(e1_GPIO_Port, e1_Pin) == 1) ? (in3_word &= ~(1 << 3)) : (in3_word |= (1 << 3));
(HAL_GPIO_ReadPin(f1_GPIO_Port, f1_Pin) == 1) ? (in3_word &= ~(1 << 4)) : (in3_word |= (1 << 4));
(HAL_GPIO_ReadPin(g1_GPIO_Port, g1_Pin) == 1) ? (in3_word &= ~(1 << 5)) : (in3_word |= (1 << 5));
(HAL_GPIO_ReadPin(a2_GPIO_Port, a2_Pin) == 1) ? (in4_word &= ~(1 << 0)) : (in4_word |= (1 << 0));
(HAL_GPIO_ReadPin(b2_GPIO_Port, b2_Pin) == 1) ? (in4_word &= ~(1 << 1)) : (in4_word |= (1 << 1));
(HAL_GPIO_ReadPin(c2_GPIO_Port, c2_Pin) == 1) ? (in4_word &= ~(1 << 2)) : (in4_word |= (1 << 2));
(HAL_GPIO_ReadPin(e2_GPIO_Port, e2_Pin) == 1) ? (in4_word &= ~(1 << 3)) : (in4_word |= (1 << 3));
(HAL_GPIO_ReadPin(f2_GPIO_Port, f2_Pin) == 1) ? (in4_word &= ~(1 << 4)) : (in4_word |= (1 << 4));
(HAL_GPIO_ReadPin(g2_GPIO_Port, g2_Pin) == 1) ? (in4_word &= ~(1 << 5)) : (in4_word |= (1 << 5));
set_register_noSave(startAddress, in1_word);
set_register_noSave(startAddress + 1, in2_word);
set_register_noSave(startAddress + 2, in3_word);
set_register_noSave(startAddress + 3, in4_word);
}
void Read_From_Flash(uint32_t startAddress, uint16_t *buffer, uint32_t numElements) {
for (uint32_t i = 0; i < numElements; i++) {
buffer[i] = *(__IO uint16_t*) (startAddress + (i * 2));
}
}
void combineMyRtumST() {
for (int i = 0; i < 10; i++) {
myRtumST[i * 2] = readST[i];
myRtumST[i * 2 + 1] = writeST[i];
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void) {
/* USER CODE BEGIN 1 */
/* 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_DMA_Init();
MX_SPI1_Init();
// MX_USART1_UART_Init();
MX_TIM6_Init();
/* USER CODE BEGIN 2 */
// 初始化保存数据
uint16_t readDataBuffer[FLASH_SAVE_SIZE];
Read_From_Flash(EDIT_FLASH_ADDRESS, readDataBuffer, FLASH_SAVE_SIZE);
set_register_range(0, FLASH_SAVE_SIZE, readDataBuffer);
//加载参数
slmp_port = get_register(85);
slmp_ip[0] = get_register(81);
slmp_ip[1] = get_register(82);
slmp_ip[2] = get_register(83);
slmp_ip[3] = get_register(84);
if (slmp_ip[0] == 0xff || slmp_ip[1] == 0xff || slmp_ip[2] == 0xff || slmp_ip[3] == 0xff) {
slmp_ip[0] = 192;
slmp_ip[1] = 168;
slmp_ip[2] = 1;
slmp_ip[3] = 9;
set_register(81, 192);
set_register(82, 168);
set_register(83, 1);
set_register(84, 9);
}
if (slmp_port == 0xffff) {
set_register(85, 4096);
}
if (get_register(91) == 0xffff) {
set_register(91, 0);
}
commType = get_register(91);
local_ip[0] = get_register(86);
local_ip[1] = get_register(87);
local_ip[2] = get_register(88);
local_ip[3] = get_register(89);
if (local_ip[0] == 0xff || local_ip[1] == 0xff || local_ip[2] == 0xff || local_ip[3] == 0xff) {
local_ip[0] = 192;
local_ip[1] = 168;
local_ip[2] = 1;
local_ip[3] = 14;
set_register(86, 192);
set_register(87, 168);
set_register(88, 1);
set_register(89, 14);
}
if (get_register(90) == 0xffff) {
set_register(90, 120);
}
set_slaveid(get_register(90));
if (get_register(80) == 0xffff) {
set_register(80, 0);
}
uint32_t BaudRate = 0;
switch (get_register(80)) { // 80号地址波特率
case 0:
BaudRate = 9600;
break;
case 1:
BaudRate = 14400;
break;
case 2:
BaudRate = 19200;
break;
case 3:
BaudRate = 38400;
break;
case 4:
BaudRate = 56000;
break;
case 5:
BaudRate = 57600;
break;
case 6:
BaudRate = 115200;
break;
default:
BaudRate = 128000;
break;
}
for (uint16_t i = 0; i < 80; i++) {
if (get_register(i) == 0xffff) {
set_register(i, 0);
}
}
// 网页
httpServer_init();
reg_httpServer_cbfunc(NVIC_SystemReset, NULL);
reg_httpServer_webContent((uint8_t*) "index.html", (uint8_t*) rtu_master);
reg_httpServer_webContent((uint8_t*) "ajax.js", (uint8_t*) w5x00web_ajax_js);
//初始化硬件
HAL_TIM_Base_Start_IT(&htim6);
w5500_init(local_ip);
MX_USART1_UART_Init(BaudRate);
HAL_GPIO_WritePin(run_lamp_GPIO_Port, run_lamp_Pin, GPIO_PIN_RESET);
combineMyRtumST();
updateMyRtumST(myRtumST);
setupRtumST(readST, writeST);
master.localSlaveId = get_slaveid();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
set_register(109, HAL_GPIO_ReadPin(run_lamp_GPIO_Port, run_lamp_Pin) ? 1 : 0);
map_in(100);
map_out(110);
RTU_Master_Loop(&master, myRtumST);
prev_cnt = comm_cnt;
loopback_tcps_modbus(0, w5500_rx, 502, &comm_cnt, &comm1);
if (slmp_send_loop < 2) {
slmp_send_loop++;
} else {
slmp_send_loop = 0;
if (commType == 0) {
loopback_tcpc_slmp(1, slmp_ip, slmp_port, &comm_cnt, &comm2);
} else if (commType == 1) {
loopback_udpc_omrfins(1, slmp_ip, slmp_port, local_ip[3], &comm_cnt, &comm2);
}
}
if (comm_cnt > 100) {
HAL_GPIO_TogglePin(comm_lamp_GPIO_Port, comm_lamp_Pin);
comm_cnt = 0;
}
if (!comm1 && !comm2) {
HAL_GPIO_WritePin(comm_lamp_GPIO_Port, comm_lamp_Pin, GPIO_PIN_SET);
}
web_loop();
reset_button();
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };
/** 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.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
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_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
Error_Handler();
}
/** Enables the Clock Security System
*/
HAL_RCC_EnableCSS();
}
/* USER CODE BEGIN 4 */
/* 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 */
+122
View File
@@ -0,0 +1,122 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file spi.c
* @brief This file provides code for the configuration
* of the SPI instances.
******************************************************************************
* @attention
*
* Copyright (c) 2026 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 "spi.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
SPI_HandleTypeDef hspi1;
/* SPI1 init function */
void MX_SPI1_Init(void)
{
/* USER CODE BEGIN SPI1_Init 0 */
/* USER CODE END SPI1_Init 0 */
/* USER CODE BEGIN SPI1_Init 1 */
/* USER CODE END SPI1_Init 1 */
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI1_Init 2 */
/* USER CODE END SPI1_Init 2 */
}
void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(spiHandle->Instance==SPI1)
{
/* USER CODE BEGIN SPI1_MspInit 0 */
/* USER CODE END SPI1_MspInit 0 */
/* SPI1 clock enable */
__HAL_RCC_SPI1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**SPI1 GPIO Configuration
PA5 ------> SPI1_SCK
PA6 ------> SPI1_MISO
PA7 ------> SPI1_MOSI
*/
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN SPI1_MspInit 1 */
/* USER CODE END SPI1_MspInit 1 */
}
}
void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
{
if(spiHandle->Instance==SPI1)
{
/* USER CODE BEGIN SPI1_MspDeInit 0 */
/* USER CODE END SPI1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_SPI1_CLK_DISABLE();
/**SPI1 GPIO Configuration
PA5 ------> SPI1_SCK
PA6 ------> SPI1_MISO
PA7 ------> SPI1_MOSI
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
/* USER CODE BEGIN SPI1_MspDeInit 1 */
/* USER CODE END SPI1_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
+86
View File
@@ -0,0 +1,86 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file stm32f1xx_hal_msp.c
* @brief This file provides code for the MSP Initialization
* and de-Initialization codes.
******************************************************************************
* @attention
*
* Copyright (c) 2024 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"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
/* USER CODE END TD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN Define */
/* USER CODE END Define */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN Macro */
/* USER CODE END Macro */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* External functions --------------------------------------------------------*/
/* USER CODE BEGIN ExternalFunctions */
/* USER CODE END ExternalFunctions */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* Initializes the Global MSP.
*/
void HAL_MspInit(void)
{
/* USER CODE BEGIN MspInit 0 */
/* USER CODE END MspInit 0 */
__HAL_RCC_AFIO_CLK_ENABLE();
__HAL_RCC_PWR_CLK_ENABLE();
/* System interrupt init*/
/** NOJTAG: JTAG-DP Disabled and SW-DP Enabled
*/
__HAL_AFIO_REMAP_SWJ_NOJTAG();
/* USER CODE BEGIN MspInit 1 */
/* USER CODE END MspInit 1 */
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
+263
View File
@@ -0,0 +1,263 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file stm32f1xx_it.c
* @brief Interrupt Service Routines.
******************************************************************************
* @attention
*
* Copyright (c) 2024 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 "stm32f1xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
/* USER CODE END TD */
/* 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 */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/* External variables --------------------------------------------------------*/
extern TIM_HandleTypeDef htim6;
extern DMA_HandleTypeDef hdma_usart1_rx;
extern DMA_HandleTypeDef hdma_usart1_tx;
extern UART_HandleTypeDef huart1;
/* USER CODE BEGIN EV */
/* USER CODE END EV */
/******************************************************************************/
/* Cortex-M3 Processor Interruption and Exception Handlers */
/******************************************************************************/
/**
* @brief This function handles Non maskable interrupt.
*/
void NMI_Handler(void)
{
/* USER CODE BEGIN NonMaskableInt_IRQn 0 */
/* USER CODE END NonMaskableInt_IRQn 0 */
HAL_RCC_NMI_IRQHandler();
/* USER CODE BEGIN NonMaskableInt_IRQn 1 */
while (1)
{
}
/* USER CODE END NonMaskableInt_IRQn 1 */
}
/**
* @brief This function handles Hard fault interrupt.
*/
void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn 0 */
/* USER CODE END HardFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_HardFault_IRQn 0 */
/* USER CODE END W1_HardFault_IRQn 0 */
}
}
/**
* @brief This function handles Memory management fault.
*/
void MemManage_Handler(void)
{
/* USER CODE BEGIN MemoryManagement_IRQn 0 */
/* USER CODE END MemoryManagement_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
/* USER CODE END W1_MemoryManagement_IRQn 0 */
}
}
/**
* @brief This function handles Prefetch fault, memory access fault.
*/
void BusFault_Handler(void)
{
/* USER CODE BEGIN BusFault_IRQn 0 */
/* USER CODE END BusFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_BusFault_IRQn 0 */
/* USER CODE END W1_BusFault_IRQn 0 */
}
}
/**
* @brief This function handles Undefined instruction or illegal state.
*/
void UsageFault_Handler(void)
{
/* USER CODE BEGIN UsageFault_IRQn 0 */
/* USER CODE END UsageFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_UsageFault_IRQn 0 */
/* USER CODE END W1_UsageFault_IRQn 0 */
}
}
/**
* @brief This function handles System service call via SWI instruction.
*/
void SVC_Handler(void)
{
/* USER CODE BEGIN SVCall_IRQn 0 */
/* USER CODE END SVCall_IRQn 0 */
/* USER CODE BEGIN SVCall_IRQn 1 */
/* USER CODE END SVCall_IRQn 1 */
}
/**
* @brief This function handles Debug monitor.
*/
void DebugMon_Handler(void)
{
/* USER CODE BEGIN DebugMonitor_IRQn 0 */
/* USER CODE END DebugMonitor_IRQn 0 */
/* USER CODE BEGIN DebugMonitor_IRQn 1 */
/* USER CODE END DebugMonitor_IRQn 1 */
}
/**
* @brief This function handles Pendable request for system service.
*/
void PendSV_Handler(void)
{
/* USER CODE BEGIN PendSV_IRQn 0 */
/* USER CODE END PendSV_IRQn 0 */
/* USER CODE BEGIN PendSV_IRQn 1 */
/* USER CODE END PendSV_IRQn 1 */
}
/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
/* USER CODE BEGIN SysTick_IRQn 0 */
/* USER CODE END SysTick_IRQn 0 */
HAL_IncTick();
/* USER CODE BEGIN SysTick_IRQn 1 */
/* USER CODE END SysTick_IRQn 1 */
}
/******************************************************************************/
/* STM32F1xx Peripheral Interrupt Handlers */
/* Add here the Interrupt Handlers for the used peripherals. */
/* For the available peripheral interrupt handler names, */
/* please refer to the startup file (startup_stm32f1xx.s). */
/******************************************************************************/
/**
* @brief This function handles DMA1 channel4 global interrupt.
*/
void DMA1_Channel4_IRQHandler(void)
{
/* USER CODE BEGIN DMA1_Channel4_IRQn 0 */
/* USER CODE END DMA1_Channel4_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_usart1_tx);
/* USER CODE BEGIN DMA1_Channel4_IRQn 1 */
/* USER CODE END DMA1_Channel4_IRQn 1 */
}
/**
* @brief This function handles DMA1 channel5 global interrupt.
*/
void DMA1_Channel5_IRQHandler(void)
{
/* USER CODE BEGIN DMA1_Channel5_IRQn 0 */
/* USER CODE END DMA1_Channel5_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_usart1_rx);
/* USER CODE BEGIN DMA1_Channel5_IRQn 1 */
/* USER CODE END DMA1_Channel5_IRQn 1 */
}
/**
* @brief This function handles USART1 global interrupt.
*/
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
/* USER CODE END USART1_IRQn 1 */
}
/**
* @brief This function handles TIM6 global interrupt.
*/
void TIM6_IRQHandler(void)
{
/* USER CODE BEGIN TIM6_IRQn 0 */
/* USER CODE END TIM6_IRQn 0 */
HAL_TIM_IRQHandler(&htim6);
/* USER CODE BEGIN TIM6_IRQn 1 */
/* USER CODE END TIM6_IRQn 1 */
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
+176
View File
@@ -0,0 +1,176 @@
/**
******************************************************************************
* @file syscalls.c
* @author Auto-generated by STM32CubeIDE
* @brief STM32CubeIDE Minimal System calls file
*
* For more information about which c-functions
* need which of these lowlevel functions
* please consult the Newlib libc-manual
******************************************************************************
* @attention
*
* Copyright (c) 2020-2024 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.
*
******************************************************************************
*/
/* Includes */
#include <sys/stat.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>
/* Variables */
extern int __io_putchar(int ch) __attribute__((weak));
extern int __io_getchar(void) __attribute__((weak));
char *__env[1] = { 0 };
char **environ = __env;
/* Functions */
void initialise_monitor_handles()
{
}
int _getpid(void)
{
return 1;
}
int _kill(int pid, int sig)
{
(void)pid;
(void)sig;
errno = EINVAL;
return -1;
}
void _exit (int status)
{
_kill(status, -1);
while (1) {} /* Make sure we hang here */
}
__attribute__((weak)) int _read(int file, char *ptr, int len)
{
(void)file;
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
*ptr++ = __io_getchar();
}
return len;
}
__attribute__((weak)) int _write(int file, char *ptr, int len)
{
(void)file;
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
__io_putchar(*ptr++);
}
return len;
}
int _close(int file)
{
(void)file;
return -1;
}
int _fstat(int file, struct stat *st)
{
(void)file;
st->st_mode = S_IFCHR;
return 0;
}
int _isatty(int file)
{
(void)file;
return 1;
}
int _lseek(int file, int ptr, int dir)
{
(void)file;
(void)ptr;
(void)dir;
return 0;
}
int _open(char *path, int flags, ...)
{
(void)path;
(void)flags;
/* Pretend like we always fail */
return -1;
}
int _wait(int *status)
{
(void)status;
errno = ECHILD;
return -1;
}
int _unlink(char *name)
{
(void)name;
errno = ENOENT;
return -1;
}
int _times(struct tms *buf)
{
(void)buf;
return -1;
}
int _stat(char *file, struct stat *st)
{
(void)file;
st->st_mode = S_IFCHR;
return 0;
}
int _link(char *old, char *new)
{
(void)old;
(void)new;
errno = EMLINK;
return -1;
}
int _fork(void)
{
errno = EAGAIN;
return -1;
}
int _execve(char *name, char **argv, char **env)
{
(void)name;
(void)argv;
(void)env;
errno = ENOMEM;
return -1;
}
+79
View File
@@ -0,0 +1,79 @@
/**
******************************************************************************
* @file sysmem.c
* @author Generated by STM32CubeIDE
* @brief STM32CubeIDE System Memory calls file
*
* For more information about which C functions
* need which of these lowlevel functions
* please consult the newlib libc manual
******************************************************************************
* @attention
*
* Copyright (c) 2024 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.
*
******************************************************************************
*/
/* Includes */
#include <errno.h>
#include <stdint.h>
/**
* Pointer to the current high watermark of the heap usage
*/
static uint8_t *__sbrk_heap_end = NULL;
/**
* @brief _sbrk() allocates memory to the newlib heap and is used by malloc
* and others from the C library
*
* @verbatim
* ############################################################################
* # .data # .bss # newlib heap # MSP stack #
* # # # # Reserved by _Min_Stack_Size #
* ############################################################################
* ^-- RAM start ^-- _end _estack, RAM end --^
* @endverbatim
*
* This implementation starts allocating at the '_end' linker symbol
* The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack
* The implementation considers '_estack' linker symbol to be RAM end
* NOTE: If the MSP stack, at any point during execution, grows larger than the
* reserved size, please increase the '_Min_Stack_Size'.
*
* @param incr Memory size
* @return Pointer to allocated memory
*/
void *_sbrk(ptrdiff_t incr)
{
extern uint8_t _end; /* Symbol defined in the linker script */
extern uint8_t _estack; /* Symbol defined in the linker script */
extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */
const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size;
const uint8_t *max_heap = (uint8_t *)stack_limit;
uint8_t *prev_heap_end;
/* Initialize heap end at first call */
if (NULL == __sbrk_heap_end)
{
__sbrk_heap_end = &_end;
}
/* Protect heap from growing into the reserved MSP stack */
if (__sbrk_heap_end + incr > max_heap)
{
errno = ENOMEM;
return (void *)-1;
}
prev_heap_end = __sbrk_heap_end;
__sbrk_heap_end += incr;
return (void *)prev_heap_end;
}
+406
View File
@@ -0,0 +1,406 @@
/**
******************************************************************************
* @file system_stm32f1xx.c
* @author MCD Application Team
* @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
*
* 1. This file provides two functions and one global variable to be called from
* user application:
* - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
* factors, AHB/APBx prescalers and Flash settings).
* This function is called at startup just after reset and
* before branch to main program. This call is made inside
* the "startup_stm32f1xx_xx.s" file.
*
* - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
* by the user application to setup the SysTick
* timer or configure other parameters.
*
* - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
* be called whenever the core clock is changed
* during program execution.
*
* 2. After each device reset the HSI (8 MHz) is used as system clock source.
* Then SystemInit() function is called, in "startup_stm32f1xx_xx.s" file, to
* configure the system clock before to branch to main program.
*
* 4. The default value of HSE crystal is set to 8 MHz (or 25 MHz, depending on
* the product used), refer to "HSE_VALUE".
* When HSE is used as system clock source, directly or through PLL, and you
* are using different crystal you have to adapt the HSE value to your own
* configuration.
*
******************************************************************************
* @attention
*
* Copyright (c) 2017-2021 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.
*
******************************************************************************
*/
/** @addtogroup CMSIS
* @{
*/
/** @addtogroup stm32f1xx_system
* @{
*/
/** @addtogroup STM32F1xx_System_Private_Includes
* @{
*/
#include "stm32f1xx.h"
/**
* @}
*/
/** @addtogroup STM32F1xx_System_Private_TypesDefinitions
* @{
*/
/**
* @}
*/
/** @addtogroup STM32F1xx_System_Private_Defines
* @{
*/
#if !defined (HSE_VALUE)
#define HSE_VALUE 8000000U /*!< Default value of the External oscillator in Hz.
This value can be provided and adapted by the user application. */
#endif /* HSE_VALUE */
#if !defined (HSI_VALUE)
#define HSI_VALUE 8000000U /*!< Default value of the Internal oscillator in Hz.
This value can be provided and adapted by the user application. */
#endif /* HSI_VALUE */
/*!< Uncomment the following line if you need to use external SRAM */
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
/* #define DATA_IN_ExtSRAM */
#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
/* Note: Following vector table addresses must be defined in line with linker
configuration. */
/*!< Uncomment the following line if you need to relocate the vector table
anywhere in Flash or Sram, else the vector table is kept at the automatic
remap of boot address selected */
/* #define USER_VECT_TAB_ADDRESS */
#if defined(USER_VECT_TAB_ADDRESS)
/*!< Uncomment the following line if you need to relocate your vector Table
in Sram else user remap will be done in Flash. */
/* #define VECT_TAB_SRAM */
#if defined(VECT_TAB_SRAM)
#define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field.
This value must be a multiple of 0x200. */
#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
This value must be a multiple of 0x200. */
#else
#define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field.
This value must be a multiple of 0x200. */
#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
This value must be a multiple of 0x200. */
#endif /* VECT_TAB_SRAM */
#endif /* USER_VECT_TAB_ADDRESS */
/******************************************************************************/
/**
* @}
*/
/** @addtogroup STM32F1xx_System_Private_Macros
* @{
*/
/**
* @}
*/
/** @addtogroup STM32F1xx_System_Private_Variables
* @{
*/
/* This variable is updated in three ways:
1) by calling CMSIS function SystemCoreClockUpdate()
2) by calling HAL API function HAL_RCC_GetHCLKFreq()
3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
Note: If you use this function to configure the system clock; then there
is no need to call the 2 first functions listed above, since SystemCoreClock
variable is updated automatically.
*/
uint32_t SystemCoreClock = 8000000;
const uint8_t AHBPrescTable[16U] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
const uint8_t APBPrescTable[8U] = {0, 0, 0, 0, 1, 2, 3, 4};
/**
* @}
*/
/** @addtogroup STM32F1xx_System_Private_FunctionPrototypes
* @{
*/
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
#ifdef DATA_IN_ExtSRAM
static void SystemInit_ExtMemCtl(void);
#endif /* DATA_IN_ExtSRAM */
#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
/**
* @}
*/
/** @addtogroup STM32F1xx_System_Private_Functions
* @{
*/
/**
* @brief Setup the microcontroller system
* Initialize the Embedded Flash Interface, the PLL and update the
* SystemCoreClock variable.
* @note This function should be used only after reset.
* @param None
* @retval None
*/
void SystemInit (void)
{
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
#ifdef DATA_IN_ExtSRAM
SystemInit_ExtMemCtl();
#endif /* DATA_IN_ExtSRAM */
#endif
/* Configure the Vector Table location -------------------------------------*/
#if defined(USER_VECT_TAB_ADDRESS)
SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
#endif /* USER_VECT_TAB_ADDRESS */
}
/**
* @brief Update SystemCoreClock variable according to Clock Register Values.
* The SystemCoreClock variable contains the core clock (HCLK), it can
* be used by the user application to setup the SysTick timer or configure
* other parameters.
*
* @note Each time the core clock (HCLK) changes, this function must be called
* to update SystemCoreClock variable value. Otherwise, any configuration
* based on this variable will be incorrect.
*
* @note - The system frequency computed by this function is not the real
* frequency in the chip. It is calculated based on the predefined
* constant and the selected clock source:
*
* - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
*
* - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
*
* - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
* or HSI_VALUE(*) multiplied by the PLL factors.
*
* (*) HSI_VALUE is a constant defined in stm32f1xx.h file (default value
* 8 MHz) but the real value may vary depending on the variations
* in voltage and temperature.
*
* (**) HSE_VALUE is a constant defined in stm32f1xx.h file (default value
* 8 MHz or 25 MHz, depending on the product used), user has to ensure
* that HSE_VALUE is same as the real frequency of the crystal used.
* Otherwise, this function may have wrong result.
*
* - The result of this function could be not correct when using fractional
* value for HSE crystal.
* @param None
* @retval None
*/
void SystemCoreClockUpdate (void)
{
uint32_t tmp = 0U, pllmull = 0U, pllsource = 0U;
#if defined(STM32F105xC) || defined(STM32F107xC)
uint32_t prediv1source = 0U, prediv1factor = 0U, prediv2factor = 0U, pll2mull = 0U;
#endif /* STM32F105xC */
#if defined(STM32F100xB) || defined(STM32F100xE)
uint32_t prediv1factor = 0U;
#endif /* STM32F100xB or STM32F100xE */
/* Get SYSCLK source -------------------------------------------------------*/
tmp = RCC->CFGR & RCC_CFGR_SWS;
switch (tmp)
{
case 0x00U: /* HSI used as system clock */
SystemCoreClock = HSI_VALUE;
break;
case 0x04U: /* HSE used as system clock */
SystemCoreClock = HSE_VALUE;
break;
case 0x08U: /* PLL used as system clock */
/* Get PLL clock source and multiplication factor ----------------------*/
pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
#if !defined(STM32F105xC) && !defined(STM32F107xC)
pllmull = ( pllmull >> 18U) + 2U;
if (pllsource == 0x00U)
{
/* HSI oscillator clock divided by 2 selected as PLL clock entry */
SystemCoreClock = (HSI_VALUE >> 1U) * pllmull;
}
else
{
#if defined(STM32F100xB) || defined(STM32F100xE)
prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1U;
/* HSE oscillator clock selected as PREDIV1 clock entry */
SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
#else
/* HSE selected as PLL clock entry */
if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
{/* HSE oscillator clock divided by 2 */
SystemCoreClock = (HSE_VALUE >> 1U) * pllmull;
}
else
{
SystemCoreClock = HSE_VALUE * pllmull;
}
#endif
}
#else
pllmull = pllmull >> 18U;
if (pllmull != 0x0DU)
{
pllmull += 2U;
}
else
{ /* PLL multiplication factor = PLL input clock * 6.5 */
pllmull = 13U / 2U;
}
if (pllsource == 0x00U)
{
/* HSI oscillator clock divided by 2 selected as PLL clock entry */
SystemCoreClock = (HSI_VALUE >> 1U) * pllmull;
}
else
{/* PREDIV1 selected as PLL clock entry */
/* Get PREDIV1 clock source and division factor */
prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1U;
if (prediv1source == 0U)
{
/* HSE oscillator clock selected as PREDIV1 clock entry */
SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
}
else
{/* PLL2 clock selected as PREDIV1 clock entry */
/* Get PREDIV2 division factor and PLL2 multiplication factor */
prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4U) + 1U;
pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8U) + 2U;
SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;
}
}
#endif /* STM32F105xC */
break;
default:
SystemCoreClock = HSI_VALUE;
break;
}
/* Compute HCLK clock frequency ----------------*/
/* Get HCLK prescaler */
tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4U)];
/* HCLK clock frequency */
SystemCoreClock >>= tmp;
}
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
/**
* @brief Setup the external memory controller. Called in startup_stm32f1xx.s
* before jump to __main
* @param None
* @retval None
*/
#ifdef DATA_IN_ExtSRAM
/**
* @brief Setup the external memory controller.
* Called in startup_stm32f1xx_xx.s/.c before jump to main.
* This function configures the external SRAM mounted on STM3210E-EVAL
* board (STM32 High density devices). This SRAM will be used as program
* data memory (including heap and stack).
* @param None
* @retval None
*/
void SystemInit_ExtMemCtl(void)
{
__IO uint32_t tmpreg;
/*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is
required, then adjust the Register Addresses */
/* Enable FSMC clock */
RCC->AHBENR = 0x00000114U;
/* Delay after an RCC peripheral clock enabling */
tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_FSMCEN);
/* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
RCC->APB2ENR = 0x000001E0U;
/* Delay after an RCC peripheral clock enabling */
tmpreg = READ_BIT(RCC->APB2ENR, RCC_APB2ENR_IOPDEN);
(void)(tmpreg);
/* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/
/*---------------- SRAM Address lines configuration -------------------------*/
/*---------------- NOE and NWE configuration --------------------------------*/
/*---------------- NE3 configuration ----------------------------------------*/
/*---------------- NBL0, NBL1 configuration ---------------------------------*/
GPIOD->CRL = 0x44BB44BBU;
GPIOD->CRH = 0xBBBBBBBBU;
GPIOE->CRL = 0xB44444BBU;
GPIOE->CRH = 0xBBBBBBBBU;
GPIOF->CRL = 0x44BBBBBBU;
GPIOF->CRH = 0xBBBB4444U;
GPIOG->CRL = 0x44BBBBBBU;
GPIOG->CRH = 0x444B4B44U;
/*---------------- FSMC Configuration ---------------------------------------*/
/*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/
FSMC_Bank1->BTCR[4U] = 0x00001091U;
FSMC_Bank1->BTCR[5U] = 0x00110212U;
}
#endif /* DATA_IN_ExtSRAM */
#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
+104
View File
@@ -0,0 +1,104 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file tim.c
* @brief This file provides code for the configuration
* of the TIM instances.
******************************************************************************
* @attention
*
* Copyright (c) 2026 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 "tim.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
TIM_HandleTypeDef htim6;
/* TIM6 init function */
void MX_TIM6_Init(void)
{
/* USER CODE BEGIN TIM6_Init 0 */
/* USER CODE END TIM6_Init 0 */
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM6_Init 1 */
/* USER CODE END TIM6_Init 1 */
htim6.Instance = TIM6;
htim6.Init.Prescaler = 71;
htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
htim6.Init.Period = 999;
htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM6_Init 2 */
/* USER CODE END TIM6_Init 2 */
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM6)
{
/* USER CODE BEGIN TIM6_MspInit 0 */
/* USER CODE END TIM6_MspInit 0 */
/* TIM6 clock enable */
__HAL_RCC_TIM6_CLK_ENABLE();
/* TIM6 interrupt Init */
HAL_NVIC_SetPriority(TIM6_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM6_IRQn);
/* USER CODE BEGIN TIM6_MspInit 1 */
/* USER CODE END TIM6_MspInit 1 */
}
}
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM6)
{
/* USER CODE BEGIN TIM6_MspDeInit 0 */
/* USER CODE END TIM6_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM6_CLK_DISABLE();
/* TIM6 interrupt Deinit */
HAL_NVIC_DisableIRQ(TIM6_IRQn);
/* USER CODE BEGIN TIM6_MspDeInit 1 */
/* USER CODE END TIM6_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
+161
View File
@@ -0,0 +1,161 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usart.c
* @brief This file provides code for the configuration
* of the USART instances.
******************************************************************************
* @attention
*
* Copyright (c) 2026 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 "usart.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
UART_HandleTypeDef huart1;
DMA_HandleTypeDef hdma_usart1_rx;
DMA_HandleTypeDef hdma_usart1_tx;
/* USART1 init function */
void MX_USART1_UART_Init(uint32_t BaudRate)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(uartHandle->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspInit 0 */
/* USER CODE END USART1_MspInit 0 */
/* USART1 clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USART1 DMA Init */
/* USART1_RX Init */
hdma_usart1_rx.Instance = DMA1_Channel5;
hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_usart1_rx.Init.Mode = DMA_NORMAL;
hdma_usart1_rx.Init.Priority = DMA_PRIORITY_LOW;
if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(uartHandle,hdmarx,hdma_usart1_rx);
/* USART1_TX Init */
hdma_usart1_tx.Instance = DMA1_Channel4;
hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_usart1_tx.Init.Mode = DMA_NORMAL;
hdma_usart1_tx.Init.Priority = DMA_PRIORITY_LOW;
if (HAL_DMA_Init(&hdma_usart1_tx) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(uartHandle,hdmatx,hdma_usart1_tx);
/* USART1 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspInit 1 */
/* USER CODE END USART1_MspInit 1 */
}
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
if(uartHandle->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspDeInit 0 */
/* USER CODE END USART1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_USART1_CLK_DISABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
/* USART1 DMA DeInit */
HAL_DMA_DeInit(uartHandle->hdmarx);
HAL_DMA_DeInit(uartHandle->hdmatx);
/* USART1 interrupt Deinit */
HAL_NVIC_DisableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspDeInit 1 */
/* USER CODE END USART1_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */