Initial commit

This commit is contained in:
Вячеслав Штейбезандт
2024-10-31 11:46:35 +03:00
committed by Tenocha
parent 02b570a41c
commit 3173e999aa
903 changed files with 435151 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
// BOOTLOADER defines
// RCC defines
#define __RCC_UART_BOOT_CLK_ENABLE() __HAL_RCC_USART3_CLK_ENABLE()
#define __RCC_UART_PORT_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define __RCC_DMA_UART_CLK_ENABLE() __HAL_RCC_DMA1_CLK_ENABLE()
#define __RCC_DMA_SDIO_CLK_ENABLE() __HAL_RCC_DMA2_CLK_ENABLE()
#define __RCC_TIM_BOOT_CLK_ENABLE() __HAL_RCC_TIM7_CLK_ENABLE()
// USART defines
#define UART_BOOT USART3 // usart
#define UART_SPEED 256000
#define UART_BOOT_IRQn USART3_IRQn
#define UART_PORT GPIOB // usart port
#define UART_PIN_TX GPIO_PIN_10
#define UART_PIN_RX GPIO_PIN_11
// DMA defines
#define DMA_UART_Channel DMA1_Channel3
#define DMA_SDIO_Channel DMA2_Channel4
#define DMA_UART_IRQn DMA1_Channel3_IRQn
#define DMA_SDIO_IRQn DMA2_Channel4_5_IRQn
// TIM defines
#define TIM_BOOT TIM7
#define TIM_BOOT_Prescaler 64000 // set up for 1 tick - 1 ms (no decrement needed)
#define TIM_BOOT_IRQn TIM7_IRQn
// SDIO defines
#define SDIO_SDCard_In_PORT GPIOB
#define SDIO_SDCard_In_PIN GPIO_PIN_5
#define SDIO_SDCard_In_IRQn EXTI9_5_IRQn

90
Core/Inc/bootloader.h Normal file
View File

@@ -0,0 +1,90 @@
#include "main.h"
#include "usart.h"
#include "dma.h"
#include "tim.h"
#include "custom_flash.h"
#include "gpio.h"
#include "can.h"
#include "sdio.h"
#include "fatfs.h"
// KEY defines
#define BOOTLOADER_KEY_ADR (uint32_t)0x08009800
#define BOOTLOADER_KEY_PAGE 20
#define BL_KEY_APP_WRITTEN 0xAAAA5555
// MAIN APP defines
#define MAIN_APP_START_ADR (uint32_t)0x0800C000
#define MAIN_APP_PAGE 21
#define MAIN_APP_NUM_OF_PAGE 250-MAIN_APP_PAGE
// ERROR DEFINES
struct flag
{
unsigned reInitMCU:1; // флаг перепрошивки (очишает память перед прошивкой новой)
unsigned InitOrWait:1; // флаг активной прошивки (1) или ожидания комманды UART/CAN (0)
unsigned StartInit:1; // флаг что необходимо начать прием прошивки/прошивку
unsigned InitBootPeriph:1;// флаг старта инициализации перифирии для прошивки
unsigned BootloaderCMD:3; // комманда: интерфейс для прошивки
unsigned InitMCUReady:1; // окончание прошивки
unsigned HalfOfWrite:1; // какая половина из dma буфера не записана
unsigned SDCardIn:1;
};
struct Bootloader_Errors
{
unsigned FLASH_ERASE:1;
unsigned FLASH_WRITE:1;
unsigned CMD_INVALID:1;
unsigned USART_RECEIVE:1;
#ifdef __CAN_H__
unsigned CAN:1;
#endif
#ifdef __SDIO_H__
unsigned SD_CARD_NOT_INSERTED:1;
unsigned SD_CARD_READ:1;
#endif
};
struct Bootloader_Errors_Counters
{
unsigned FLASH_ERASE;
unsigned FLASH_WRITE;
unsigned CMD_INVALID;
unsigned USART_RECEIVE;
#ifdef __CAN_H__
unsigned CAN_RECEIVE;
#endif
#ifdef __SDIO_H__
unsigned SD_CARD_NOT_INSERTED;
unsigned SD_CARD_READ;
#endif
};
void Bootloader_main(void);
void SD_Programming(FRESULT *res_fresult, HAL_StatusTypeDef *res_hal);
void Bootloader_TIM_Handler(void); // handler for "WD" Timer
void Check_CMD_USART(uint8_t *DataUART);
void SetKey(void);
uint32_t ReadKey(void);
void ResetKey(void);
void Boot_SystemClock_Config(void);

52
Core/Inc/can.h Normal file
View 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) 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 */
/* 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__ */

9
Core/Inc/custom_flash.h Normal file
View File

@@ -0,0 +1,9 @@
#include "main.h"
#define PAGE_SIZE 2048
HAL_StatusTypeDef FLASH_Write_Page(uint32_t *Address, uint8_t *Data, int Data_size);
HAL_StatusTypeDef FLASH_Write_Word(uint32_t Address, uint64_t Data);
HAL_StatusTypeDef FLASH_Erase_App(void);
uint8_t *FLASH_Read(uint32_t add);

View File

@@ -0,0 +1,20 @@
#include "main.h"
#include "ff.h"
//#include "usart.h"
#define sizebuff 16
struct flags
{
unsigned writting:1;
};
extern struct flags flags_UART_FATFS;
FRESULT CreateAndOpenMessageFile(FIL *MessageFile, const TCHAR* path);
FRESULT OpenMessageFile(FIL *MessageFile, const TCHAR* path);
FRESULT WriteMessage(FIL *MessageFile, char *Message, UINT SizeOfMessage);
FRESULT CloseMessageFile(FIL *MessageFile);
void WriteFile(char *Data, int Num_of_Data);
void Check_USART(void);

20
Core/Inc/custom_usart.h Normal file
View File

@@ -0,0 +1,20 @@
#include "main.h"
#define __USER_LINKDMA(__HANDLE__, __PPP_DMA_FIELD__, __DMA_HANDLE__) \
do{ \
(__HANDLE__)->__PPP_DMA_FIELD__ = (__DMA_HANDLE__); \
(__DMA_HANDLE__)->Parent = (__HANDLE__);} while(0U)
struct UARTSettings
{
USART_TypeDef *UARTx;
uint32_t UART_Speed;
GPIO_TypeDef *GPIOx;
uint16_t GPIO_PIN_RX;
uint16_t GPIO_PIN_TX;
DMA_Channel_TypeDef *DMAChannel; // DMAChannel = 0 if doesnt need
};
void User_UART_Init(UART_HandleTypeDef* huart, DMA_HandleTypeDef *DMAhuart, struct UARTSettings *uuart);
void UART_GPIO_Init(GPIO_TypeDef *GPIOx, uint16_t GPIO_PIN_RX, uint16_t GPIO_PIN_TX);
void UART_DMA_Init(UART_HandleTypeDef *huart, DMA_HandleTypeDef *hdma_rx, DMA_Channel_TypeDef *DMAhuart);

52
Core/Inc/dma.h Normal file
View File

@@ -0,0 +1,52 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file dma.h
* @brief This file contains all the function prototypes for
* the dma.c file
******************************************************************************
* @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 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __DMA_H__
#define __DMA_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* DMA memory to memory transfer handles -------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_DMA_Init(void);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /* __DMA_H__ */

49
Core/Inc/gpio.h Normal file
View File

@@ -0,0 +1,49 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file gpio.h
* @brief This file contains all the function prototypes for
* the gpio.c file
******************************************************************************
* @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 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __GPIO_H__
#define __GPIO_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_GPIO_Init(void);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /*__ GPIO_H__ */

71
Core/Inc/main.h Normal file
View File

@@ -0,0 +1,71 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.h
* @brief : Header for main.c file.
* This file contains the common defines of the application.
******************************************************************************
* @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 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx_hal.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "boot_project_setup.h"
#include "requester.h"
/* USER CODE END Includes */
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
/* USER CODE END ET */
/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
/* USER CODE END EC */
/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
/* USER CODE END EM */
/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);
void SystemClock_Config(void);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */
/* Private defines -----------------------------------------------------------*/
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H */

224
Core/Inc/requester.h Normal file
View File

@@ -0,0 +1,224 @@
#ifndef _requester
#define _requester
#include "main.h"
#include "can.h"
#include "rtc.h"
#include "tim.h"
#define _DEMO
#define ONLINE 1;
#define OFFLINE 0;
#define TIM_REQUESTER_Prescaler 64000
//CAN Filter
#define ID_MAIN_DEVICE 0x000
#define CURRENT_ID_DEVICE 0x002
#define CAN_IDE_32 0b00000100 // Для 32-х битного масштаба
#define CAN_DATA_TYPE_FILTER 0x1F000000
#define CAN_DEVICE_ID_FILTER 0x000000FF
#define CAN_SENSOR_TYPE_FILTER 0x00F80000
#define CAN_SENSOR_ID_FILTER 0x0007FF00
void REQUESTER_Init(void);
void REQUESTER_MainWhile(void);
void Boot_SystemClock_Config(void);
void REQUESTER_AnalogProcessing(void);
void REQUESTER_BroadcastProcessing(void);
void REQUESTER_DiscreticProcessing(void);
void REQUESTER_ModbusProcessing(void);
void REQUESTER_RTC_SYNC(uint8_t *data);
void REQUESTER_Pulse_TIM_Handler(void);
void Boot_SystemClock_Config(void);
void REQUESTER_CAN_FILTERS(void);
union Byte{
struct bitfield{
unsigned bit0:1;
unsigned bit1:1;
unsigned bit2:1;
unsigned bit3:1;
unsigned bit4:1;
unsigned bit5:1;
unsigned bit6:1;
unsigned bit7:1;
}Bitfield;
char AllBit;
};
_Bool IsLeapYear(uint8_t year);
#define COUNT_OF_MODBUS_SECTIONS 1
#define MODBUS_SECTION_MASK 0
#define MODBUS_SECTION_FILTER 1
#define SensorToModbusRegister(SensorType, SensorID) SensorType << 11 | SensorID
#define HighByteOfWord(WORD) (WORD>>8)&0xFF
#define LowByteOfWord(WORD) WORD&0xFF
//Route mode
#define ROUTE_MASTER 0
#define ROUTE_SLAVE 1
//Data Types
#define DATA_TYPE_BROADCAST 0b0000
#define DATA_TYPE_DISCRETE 0b0001
#define DATA_TYPE_ANALOG 0b0010
#define DATA_TYPE_MODBUS_COIL 0b0011
#define DATA_TYPE_MODBUS_DISCRETE 0b0100
#define DATA_TYPE_MODBUS_HOLDING 0b0101
#define DATA_TYPE_MODBUS_INPUT 0b0110
#define DATA_TYPE_ERROR 0b0111
#define DATA_TYPE_PULSE 0b1111
//Sensor Types for DATA_TYPE_BROADCAST
#define SENSOR_TYPE_STATUS 0b00000
#define SENSOR_TYPE_ONOFF 0b00001
#define SENSOR_TYPE_RTCSETUP 0b00010
//Sensor Types for DATA_TYPE_DISCRETE
#define SENSOR_TYPE_ACCIDENT 0b00000
#define SENSOR_TYPE_WARNING 0b00001
#define SENSOR_TYPE_CONTROL_SIGNALS 0b00010
#define SENSOR_TYPE_FLAGS 0b00011
#define SENSOR_TYPE_RESET 0b00100
#define SENSOR_TYPE_CHANGE_MODE 0b00101
#define SENSOR_TYPE_REQUEST_LIST_OF_PARAMETERS 0b00110
//Sensor Types for DATA_TYPE_ANALOG
#define SENSOR_TYPE_UNIVERSAL 0b00000
#define SENSOR_TYPE_U 0b00001
#define SENSOR_TYPE_I 0b00010
#define SENSOR_TYPE_T 0b00011
//Error Code
#define NONEXISTENT_ELEMENT 0x01
#define HighIdFilter(x) x<<24
struct controlflags{
unsigned IsPulse:1;
unsigned IsRtrMode:1;
};
//Device settings
union ext_ID{
struct ext_ID_fields{
unsigned DeviceID:8;
unsigned SensorID:11;
unsigned SensorType:5;
unsigned DataType:4;
unsigned Route:1;
}Fields;
unsigned int BitAll:29;
};
union ext_ID_Modbus{
struct ext_ID_Modbus_fields{
unsigned DeviceID:8;
unsigned CountReg:8;
unsigned StrAdr:8;
unsigned DataType:4;
unsigned Route;
}Fields;
unsigned int BitAll:29;
};
struct device
{
unsigned Status:1;
union ext_ID ExtID;
unsigned TimeFromLastPulse;
unsigned LastPulseStep;
};
struct data
{
struct T_sens{
char info[256];
unsigned SensID;
unsigned AvrgValue;
unsigned LastTenValues[10];
}TS;
struct U_sens{
char info[256];
unsigned SensID;
unsigned AvrgValue;
unsigned LastTenValues[10];
}Uu, Uv, Uw;
struct I_sens{
char info[256];
unsigned SensID;
unsigned AvrgValue;
unsigned LastTenValues[10];
}Iu, Iv, Iw;
};
struct received_request{
union ext_ID RequestedExtID;
unsigned RequestedDLC;
uint8_t RxData[8];
//DATA_TYPE_DISCRETIC
union discreticflags{
struct discretictype{
unsigned Request_Accident:1;
unsigned Request_Warning:1;
unsigned Request_Control_Signals:1;
unsigned Request_Flags:1;
unsigned Request_Reset:1;
unsigned Request_List_of_Parameters:1;
}DiscreticType;
unsigned AllFlags:5;
}DiscreticFlags;
//DATA_TYPE_BROADCAST
union broadcastflags{
struct broadcasttype{
unsigned Request_Status:1;
unsigned Request_OnOff:1;
unsigned Request_RTC_Setup:1;
}BroadcastType;
unsigned AllFlags:3;
}BroadcastFlags;
//DATA_TYPE_ANALOG
union analogflags{
struct analogtype{
unsigned Request_Universal_Sens:1;
unsigned Request_U_Sens:1;
unsigned Request_I_Sens:1;
unsigned Request_T_Sens:1;
}AnalogType;
unsigned AllFlags:4;
}AnalogFlags;
//DATA_TYPE_MODBUS
union modbusflags{
struct modbustype{
unsigned Coil:1;
unsigned Discrete:1;
unsigned Holding:1;
unsigned Input:1;
}ModbusType;
unsigned AllFlags:4;
}ModbusFlags;
union sensor_To_Modbus{
struct modbus{
unsigned Count:8;
unsigned StrAdr:8;
}Modbus;
unsigned Sensor:16;
}SensorToModbus;
unsigned ModbusFlag:1;
};
#endif

52
Core/Inc/rtc.h Normal file
View File

@@ -0,0 +1,52 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file rtc.h
* @brief This file contains all the function prototypes for
* the rtc.c file
******************************************************************************
* @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 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __RTC_H__
#define __RTC_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
extern RTC_HandleTypeDef hrtc;
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_RTC_Init(void);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /* __RTC_H__ */

52
Core/Inc/sdio.h Normal file
View File

@@ -0,0 +1,52 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file sdio.h
* @brief This file contains all the function prototypes for
* the sdio.c file
******************************************************************************
* @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 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SDIO_H__
#define __SDIO_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
extern SD_HandleTypeDef hsd;
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_SDIO_SD_Init(void);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /* __SDIO_H__ */

View File

@@ -0,0 +1,391 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file stm32f1xx_hal_conf.h
* @brief HAL configuration file.
******************************************************************************
* @attention
*
* Copyright (c) 2017 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 __STM32F1xx_HAL_CONF_H
#define __STM32F1xx_HAL_CONF_H
#ifdef __cplusplus
extern "C" {
#endif
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* ########################## Module Selection ############################## */
/**
* @brief This is the list of modules to be used in the HAL driver
*/
#define HAL_MODULE_ENABLED
/*#define HAL_ADC_MODULE_ENABLED */
/*#define HAL_CRYP_MODULE_ENABLED */
#define HAL_CAN_MODULE_ENABLED
/*#define HAL_CAN_LEGACY_MODULE_ENABLED */
/*#define HAL_CEC_MODULE_ENABLED */
/*#define HAL_CORTEX_MODULE_ENABLED */
/*#define HAL_CRC_MODULE_ENABLED */
/*#define HAL_DAC_MODULE_ENABLED */
#define HAL_DMA_MODULE_ENABLED
/*#define HAL_ETH_MODULE_ENABLED */
/*#define HAL_FLASH_MODULE_ENABLED */
#define HAL_GPIO_MODULE_ENABLED
/*#define HAL_I2C_MODULE_ENABLED */
/*#define HAL_I2S_MODULE_ENABLED */
/*#define HAL_IRDA_MODULE_ENABLED */
/*#define HAL_IWDG_MODULE_ENABLED */
/*#define HAL_NOR_MODULE_ENABLED */
/*#define HAL_NAND_MODULE_ENABLED */
/*#define HAL_PCCARD_MODULE_ENABLED */
/*#define HAL_PCD_MODULE_ENABLED */
/*#define HAL_HCD_MODULE_ENABLED */
/*#define HAL_PWR_MODULE_ENABLED */
/*#define HAL_RCC_MODULE_ENABLED */
#define HAL_RTC_MODULE_ENABLED
#define HAL_SD_MODULE_ENABLED
/*#define HAL_MMC_MODULE_ENABLED */
/*#define HAL_SDRAM_MODULE_ENABLED */
/*#define HAL_SMARTCARD_MODULE_ENABLED */
/*#define HAL_SPI_MODULE_ENABLED */
/*#define HAL_SRAM_MODULE_ENABLED */
#define HAL_TIM_MODULE_ENABLED
#define HAL_UART_MODULE_ENABLED
/*#define HAL_USART_MODULE_ENABLED */
/*#define HAL_WWDG_MODULE_ENABLED */
#define HAL_CORTEX_MODULE_ENABLED
#define HAL_DMA_MODULE_ENABLED
#define HAL_FLASH_MODULE_ENABLED
#define HAL_EXTI_MODULE_ENABLED
#define HAL_GPIO_MODULE_ENABLED
#define HAL_PWR_MODULE_ENABLED
#define HAL_RCC_MODULE_ENABLED
/* ########################## Oscillator Values adaptation ####################*/
/**
* @brief Adjust the value of External High Speed oscillator (HSE) used in your application.
* This value is used by the RCC HAL module to compute the system frequency
* (when HSE is used as system clock source, directly or through the PLL).
*/
#if !defined (HSE_VALUE)
#define HSE_VALUE 8000000U /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */
#if !defined (HSE_STARTUP_TIMEOUT)
#define HSE_STARTUP_TIMEOUT 100U /*!< Time out for HSE start up, in ms */
#endif /* HSE_STARTUP_TIMEOUT */
/**
* @brief Internal High Speed oscillator (HSI) value.
* This value is used by the RCC HAL module to compute the system frequency
* (when HSI is used as system clock source, directly or through the PLL).
*/
#if !defined (HSI_VALUE)
#define HSI_VALUE 8000000U /*!< Value of the Internal oscillator in Hz*/
#endif /* HSI_VALUE */
/**
* @brief Internal Low Speed oscillator (LSI) value.
*/
#if !defined (LSI_VALUE)
#define LSI_VALUE 40000U /*!< LSI Typical Value in Hz */
#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz
The real value may vary depending on the variations
in voltage and temperature. */
/**
* @brief External Low Speed oscillator (LSE) value.
* This value is used by the UART, RTC HAL module to compute the system frequency
*/
#if !defined (LSE_VALUE)
#define LSE_VALUE 32768U /*!< Value of the External oscillator in Hz*/
#endif /* LSE_VALUE */
#if !defined (LSE_STARTUP_TIMEOUT)
#define LSE_STARTUP_TIMEOUT 5000U /*!< Time out for LSE start up, in ms */
#endif /* LSE_STARTUP_TIMEOUT */
/* Tip: To avoid modifying this file each time you need to use different HSE,
=== you can define the HSE value in your toolchain compiler preprocessor. */
/* ########################### System Configuration ######################### */
/**
* @brief This is the HAL system configuration section
*/
#define VDD_VALUE 3300U /*!< Value of VDD in mv */
#define TICK_INT_PRIORITY 15U /*!< tick interrupt priority (lowest by default) */
#define USE_RTOS 0U
#define PREFETCH_ENABLE 1U
#define USE_HAL_ADC_REGISTER_CALLBACKS 0U /* ADC register callback disabled */
#define USE_HAL_CAN_REGISTER_CALLBACKS 0U /* CAN register callback disabled */
#define USE_HAL_CEC_REGISTER_CALLBACKS 0U /* CEC register callback disabled */
#define USE_HAL_DAC_REGISTER_CALLBACKS 0U /* DAC register callback disabled */
#define USE_HAL_ETH_REGISTER_CALLBACKS 0U /* ETH register callback disabled */
#define USE_HAL_HCD_REGISTER_CALLBACKS 0U /* HCD register callback disabled */
#define USE_HAL_I2C_REGISTER_CALLBACKS 0U /* I2C register callback disabled */
#define USE_HAL_I2S_REGISTER_CALLBACKS 0U /* I2S register callback disabled */
#define USE_HAL_MMC_REGISTER_CALLBACKS 0U /* MMC register callback disabled */
#define USE_HAL_NAND_REGISTER_CALLBACKS 0U /* NAND register callback disabled */
#define USE_HAL_NOR_REGISTER_CALLBACKS 0U /* NOR register callback disabled */
#define USE_HAL_PCCARD_REGISTER_CALLBACKS 0U /* PCCARD register callback disabled */
#define USE_HAL_PCD_REGISTER_CALLBACKS 0U /* PCD register callback disabled */
#define USE_HAL_RTC_REGISTER_CALLBACKS 0U /* RTC register callback disabled */
#define USE_HAL_SD_REGISTER_CALLBACKS 0U /* SD register callback disabled */
#define USE_HAL_SMARTCARD_REGISTER_CALLBACKS 0U /* SMARTCARD register callback disabled */
#define USE_HAL_IRDA_REGISTER_CALLBACKS 0U /* IRDA register callback disabled */
#define USE_HAL_SRAM_REGISTER_CALLBACKS 0U /* SRAM register callback disabled */
#define USE_HAL_SPI_REGISTER_CALLBACKS 0U /* SPI register callback disabled */
#define USE_HAL_TIM_REGISTER_CALLBACKS 0U /* TIM register callback disabled */
#define USE_HAL_UART_REGISTER_CALLBACKS 0U /* UART register callback disabled */
#define USE_HAL_USART_REGISTER_CALLBACKS 0U /* USART register callback disabled */
#define USE_HAL_WWDG_REGISTER_CALLBACKS 0U /* WWDG register callback disabled */
/* ########################## Assert Selection ############################## */
/**
* @brief Uncomment the line below to expanse the "assert_param" macro in the
* HAL drivers code
*/
/* #define USE_FULL_ASSERT 1U */
/* ################## Ethernet peripheral configuration ##################### */
/* Section 1 : Ethernet peripheral configuration */
/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */
#define MAC_ADDR0 2U
#define MAC_ADDR1 0U
#define MAC_ADDR2 0U
#define MAC_ADDR3 0U
#define MAC_ADDR4 0U
#define MAC_ADDR5 0U
/* Definition of the Ethernet driver buffers size and count */
#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */
#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */
#define ETH_RXBUFNB 8U /* 4 Rx buffers of size ETH_RX_BUF_SIZE */
#define ETH_TXBUFNB 4U /* 4 Tx buffers of size ETH_TX_BUF_SIZE */
/* Section 2: PHY configuration section */
/* DP83848_PHY_ADDRESS Address*/
#define DP83848_PHY_ADDRESS 0x01U
/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/
#define PHY_RESET_DELAY 0x000000FFU
/* PHY Configuration delay */
#define PHY_CONFIG_DELAY 0x00000FFFU
#define PHY_READ_TO 0x0000FFFFU
#define PHY_WRITE_TO 0x0000FFFFU
/* Section 3: Common PHY Registers */
#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */
#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */
#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */
#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */
#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */
#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */
#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */
#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */
#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */
#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */
#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */
#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */
#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */
#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */
#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */
/* Section 4: Extended PHY Registers */
#define PHY_SR ((uint16_t)0x10U) /*!< PHY status register Offset */
#define PHY_SPEED_STATUS ((uint16_t)0x0002U) /*!< PHY Speed mask */
#define PHY_DUPLEX_STATUS ((uint16_t)0x0004U) /*!< PHY Duplex mask */
/* ################## SPI peripheral configuration ########################## */
/* CRC FEATURE: Use to activate CRC feature inside HAL SPI Driver
* Activated: CRC code is present inside driver
* Deactivated: CRC code cleaned from driver
*/
#define USE_SPI_CRC 0U
/* Includes ------------------------------------------------------------------*/
/**
* @brief Include module's header file
*/
#ifdef HAL_RCC_MODULE_ENABLED
#include "stm32f1xx_hal_rcc.h"
#endif /* HAL_RCC_MODULE_ENABLED */
#ifdef HAL_GPIO_MODULE_ENABLED
#include "stm32f1xx_hal_gpio.h"
#endif /* HAL_GPIO_MODULE_ENABLED */
#ifdef HAL_EXTI_MODULE_ENABLED
#include "stm32f1xx_hal_exti.h"
#endif /* HAL_EXTI_MODULE_ENABLED */
#ifdef HAL_DMA_MODULE_ENABLED
#include "stm32f1xx_hal_dma.h"
#endif /* HAL_DMA_MODULE_ENABLED */
#ifdef HAL_ETH_MODULE_ENABLED
#include "stm32f1xx_hal_eth.h"
#endif /* HAL_ETH_MODULE_ENABLED */
#ifdef HAL_CAN_MODULE_ENABLED
#include "stm32f1xx_hal_can.h"
#endif /* HAL_CAN_MODULE_ENABLED */
#ifdef HAL_CAN_LEGACY_MODULE_ENABLED
#include "Legacy/stm32f1xx_hal_can_legacy.h"
#endif /* HAL_CAN_LEGACY_MODULE_ENABLED */
#ifdef HAL_CEC_MODULE_ENABLED
#include "stm32f1xx_hal_cec.h"
#endif /* HAL_CEC_MODULE_ENABLED */
#ifdef HAL_CORTEX_MODULE_ENABLED
#include "stm32f1xx_hal_cortex.h"
#endif /* HAL_CORTEX_MODULE_ENABLED */
#ifdef HAL_ADC_MODULE_ENABLED
#include "stm32f1xx_hal_adc.h"
#endif /* HAL_ADC_MODULE_ENABLED */
#ifdef HAL_CRC_MODULE_ENABLED
#include "stm32f1xx_hal_crc.h"
#endif /* HAL_CRC_MODULE_ENABLED */
#ifdef HAL_DAC_MODULE_ENABLED
#include "stm32f1xx_hal_dac.h"
#endif /* HAL_DAC_MODULE_ENABLED */
#ifdef HAL_FLASH_MODULE_ENABLED
#include "stm32f1xx_hal_flash.h"
#endif /* HAL_FLASH_MODULE_ENABLED */
#ifdef HAL_SRAM_MODULE_ENABLED
#include "stm32f1xx_hal_sram.h"
#endif /* HAL_SRAM_MODULE_ENABLED */
#ifdef HAL_NOR_MODULE_ENABLED
#include "stm32f1xx_hal_nor.h"
#endif /* HAL_NOR_MODULE_ENABLED */
#ifdef HAL_I2C_MODULE_ENABLED
#include "stm32f1xx_hal_i2c.h"
#endif /* HAL_I2C_MODULE_ENABLED */
#ifdef HAL_I2S_MODULE_ENABLED
#include "stm32f1xx_hal_i2s.h"
#endif /* HAL_I2S_MODULE_ENABLED */
#ifdef HAL_IWDG_MODULE_ENABLED
#include "stm32f1xx_hal_iwdg.h"
#endif /* HAL_IWDG_MODULE_ENABLED */
#ifdef HAL_PWR_MODULE_ENABLED
#include "stm32f1xx_hal_pwr.h"
#endif /* HAL_PWR_MODULE_ENABLED */
#ifdef HAL_RTC_MODULE_ENABLED
#include "stm32f1xx_hal_rtc.h"
#endif /* HAL_RTC_MODULE_ENABLED */
#ifdef HAL_PCCARD_MODULE_ENABLED
#include "stm32f1xx_hal_pccard.h"
#endif /* HAL_PCCARD_MODULE_ENABLED */
#ifdef HAL_SD_MODULE_ENABLED
#include "stm32f1xx_hal_sd.h"
#endif /* HAL_SD_MODULE_ENABLED */
#ifdef HAL_NAND_MODULE_ENABLED
#include "stm32f1xx_hal_nand.h"
#endif /* HAL_NAND_MODULE_ENABLED */
#ifdef HAL_SPI_MODULE_ENABLED
#include "stm32f1xx_hal_spi.h"
#endif /* HAL_SPI_MODULE_ENABLED */
#ifdef HAL_TIM_MODULE_ENABLED
#include "stm32f1xx_hal_tim.h"
#endif /* HAL_TIM_MODULE_ENABLED */
#ifdef HAL_UART_MODULE_ENABLED
#include "stm32f1xx_hal_uart.h"
#endif /* HAL_UART_MODULE_ENABLED */
#ifdef HAL_USART_MODULE_ENABLED
#include "stm32f1xx_hal_usart.h"
#endif /* HAL_USART_MODULE_ENABLED */
#ifdef HAL_IRDA_MODULE_ENABLED
#include "stm32f1xx_hal_irda.h"
#endif /* HAL_IRDA_MODULE_ENABLED */
#ifdef HAL_SMARTCARD_MODULE_ENABLED
#include "stm32f1xx_hal_smartcard.h"
#endif /* HAL_SMARTCARD_MODULE_ENABLED */
#ifdef HAL_WWDG_MODULE_ENABLED
#include "stm32f1xx_hal_wwdg.h"
#endif /* HAL_WWDG_MODULE_ENABLED */
#ifdef HAL_PCD_MODULE_ENABLED
#include "stm32f1xx_hal_pcd.h"
#endif /* HAL_PCD_MODULE_ENABLED */
#ifdef HAL_HCD_MODULE_ENABLED
#include "stm32f1xx_hal_hcd.h"
#endif /* HAL_HCD_MODULE_ENABLED */
#ifdef HAL_MMC_MODULE_ENABLED
#include "stm32f1xx_hal_mmc.h"
#endif /* HAL_MMC_MODULE_ENABLED */
/* Exported macro ------------------------------------------------------------*/
#ifdef USE_FULL_ASSERT
/**
* @brief The assert_param macro is used for function's parameters check.
* @param expr If expr is false, it calls assert_failed function
* which reports the name of the source file and the source
* line number of the call that failed.
* If expr is true, it returns no value.
* @retval None
*/
#define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__))
/* Exported functions ------------------------------------------------------- */
void assert_failed(uint8_t* file, uint32_t line);
#else
#define assert_param(expr) ((void)0U)
#endif /* USE_FULL_ASSERT */
#ifdef __cplusplus
}
#endif
#endif /* __STM32F1xx_HAL_CONF_H */

74
Core/Inc/stm32f1xx_it.h Normal file
View File

@@ -0,0 +1,74 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file stm32f1xx_it.h
* @brief This file contains the headers of the interrupt handlers.
******************************************************************************
* @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 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM32F1xx_IT_H
#define __STM32F1xx_IT_H
#ifdef __cplusplus
extern "C" {
#endif
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
/* USER CODE END ET */
/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
/* USER CODE END EC */
/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
/* USER CODE END EM */
/* Exported functions prototypes ---------------------------------------------*/
void NMI_Handler(void);
void HardFault_Handler(void);
void MemManage_Handler(void);
void BusFault_Handler(void);
void UsageFault_Handler(void);
void SVC_Handler(void);
void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
void DMA1_Channel3_IRQHandler(void);
void USB_HP_CAN1_TX_IRQHandler(void);
void USB_LP_CAN1_RX0_IRQHandler(void);
void CAN1_SCE_IRQHandler(void);
void USART3_IRQHandler(void);
void TIM8_UP_TIM13_IRQHandler(void);
void TIM7_IRQHandler(void);
void DMA2_Channel4_5_IRQHandler(void);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */
#ifdef __cplusplus
}
#endif
#endif /* __STM32F1xx_IT_H */

52
Core/Inc/tim.h Normal file
View File

@@ -0,0 +1,52 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file tim.h
* @brief This file contains all the function prototypes for
* the tim.c file
******************************************************************************
* @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 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __TIM_H__
#define __TIM_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
extern TIM_HandleTypeDef htim7;
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_TIM7_Init(void);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /* __TIM_H__ */

52
Core/Inc/usart.h Normal file
View File

@@ -0,0 +1,52 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usart.h
* @brief This file contains all the function prototypes for
* the usart.c file
******************************************************************************
* @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 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USART_H__
#define __USART_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
//#include "custom_usart.h"
/* USER CODE END Includes */
extern UART_HandleTypeDef huart3;
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_USART3_UART_Init(void);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /* __USART_H__ */

614
Core/Src/bootloader.c Normal file
View File

@@ -0,0 +1,614 @@
#include "bootloader.h"
#include "custom_receive_and_write.h"
extern FLASH_EraseInitTypeDef EraseInitStruct;
struct flag FLAGS;
struct Bootloader_Errors_Counters BOOTLOADER_ERRORS_COUNTERS;
struct Bootloader_Errors BOOTLOADER_ERRORS;
uint32_t app_current_add; // address for writing pages
int cnt_tim_reset = 0; // WD Tim counter
uint8_t ReceiveDataUART[4]; // buffer for CMD
uint8_t Data2Write[PAGE_SIZE*2]; // DMA buffer for application
uint8_t CAN_Data[8];
CAN_TxHeaderTypeDef TxHeader;
CAN_RxHeaderTypeDef RxHeader;
uint32_t TxMailBox = 0; // num of used mail
extern UART_HandleTypeDef huart_boot; // uart handler for boot uart
extern DMA_HandleTypeDef hdma_usart_boot_rx; // dma handler for boot uart
struct UARTSettings UARTSet; // settings for uart
void Bootloader_Init(void)
{
HAL_Init();
Boot_SystemClock_Config();
MX_DMA_Init();
//Если используется ЮСАРТ - инициализация
#ifdef __USART_H__
UARTSet.UARTx = UART_BOOT;
UARTSet.UART_Speed = UART_SPEED;
UARTSet.GPIOx = UART_PORT;
UARTSet.GPIO_PIN_RX = UART_PIN_RX;
UARTSet.GPIO_PIN_TX = UART_PIN_TX;
UARTSet.DMAChannel = DMA_UART_Channel;
User_UART_Init(&huart_boot, &hdma_usart_boot_rx, &UARTSet);
#endif
//Если используется SD-карта - инициализация
#ifdef __SDIO_H__
BSP_SD_ITConfig();
#endif
#ifdef __CAN_H__
MX_CAN_Init();
#endif
//Инициализация светодиодов
MX_GPIO_Init();
MX_TIM7_Init();
}
HAL_StatusTypeDef res_hal;
void Bootloader_main(void) // main function, that defines bootloader behaviour
{
/*
// Настройка доступной переферии с помощью define
Bootloader_Init();
*/
/* START APPLICATION */
if (ReadKey() == BL_KEY_APP_WRITTEN)
{
// jump to main app
//Задаётся адрес программы со смещением от начала вектора прерываний
uint32_t app_jump_adr;
app_jump_adr=*((volatile uint32_t*)(MAIN_APP_START_ADR+4));
void(*GoToApp)(void);
//Деинициализация HAL
HAL_DeInit();
GoToApp = (void (*) (void)) app_jump_adr;
//Перенос вектора прерываний на начало зашитой программы
__disable_irq();
__set_MSP(*((volatile uint32_t*)MAIN_APP_START_ADR));
__enable_irq();
//Переход к выполнению зашитой программы
GoToApp();
}
else /* START PROGRAMMING MCU */
{
/* MCU Configuration for bootloader-------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash BootloaderCMD and the Systick. */
Bootloader_Init();
FLAGS.InitOrWait = 0;
// waif for commant for programming
do{
res_hal=HAL_UART_Receive_IT(&huart_boot, ReceiveDataUART, sizeof(ReceiveDataUART));
if(res_hal!=HAL_OK){
BOOTLOADER_ERRORS_COUNTERS.USART_RECEIVE++;
BOOTLOADER_ERRORS.USART_RECEIVE=1;
}
}while(res_hal!=HAL_OK); // if usart err - try start receive again
HAL_CAN_Start(&hcan);
HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
CAN_FilterTypeDef canFilterConfig;
canFilterConfig.FilterBank = 0;
canFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
canFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
canFilterConfig.FilterIdHigh = 0x0000;
canFilterConfig.FilterIdLow = 0x0000;
canFilterConfig.FilterMaskIdHigh = 0x0000;
canFilterConfig.FilterMaskIdLow = 0x0000;
canFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
canFilterConfig.FilterActivation = ENABLE;
canFilterConfig.SlaveStartFilterBank = 14;
HAL_CAN_ConfigFilter(&hcan, &canFilterConfig);
// FLAGS.StartInit=1;
// FLAGS.reInitMCU=1;
// FLAGS.BootloaderCMD = 2;
app_current_add = MAIN_APP_START_ADR;
// HAL_CAN_Start(&hcan);
/* Infinite loop */
while (1)
{
// HAL_Delay(10);
// HAL_UART_Transmit(&huart_boot, (uint8_t *)"000000", 6, HAL_MAX_DELAY);
// choose interface for programming
if (FLAGS.StartInit)
{
if (FLAGS.reInitMCU) // if its reInit request - erase outdate app
{
GPIOB->ODR^=0x2000;
res_hal=FLASH_Erase_App();
if(res_hal!=HAL_OK)
{
FLAGS.StartInit=1;
BOOTLOADER_ERRORS.FLASH_ERASE = 1;
continue;
}
BOOTLOADER_ERRORS.FLASH_ERASE = 0;
GPIOB->ODR^=0x2000;
FLAGS.reInitMCU = 0;
}
FLAGS.StartInit = 0;
HAL_UART_AbortReceive_IT(&huart_boot);
HAL_CAN_Stop(&hcan);
HAL_CAN_DeactivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
switch ((int)FLAGS.BootloaderCMD)
{
// UART
#ifdef __USART_H__
case 1:/* UART */
FLAGS.InitBootPeriph = 1;
// start receiving app.bin
res_hal = HAL_UART_Receive_DMA(&huart_boot, Data2Write, PAGE_SIZE*2);
if(res_hal!=HAL_OK)
{
FLAGS.StartInit=1;
BOOTLOADER_ERRORS_COUNTERS.USART_RECEIVE++;
BOOTLOADER_ERRORS.USART_RECEIVE = 1;
break;
}
// code of writing app in flash in callbacks functions:
// HAL_UART_RxHalfCpltCallback and HAL_UART_RxCpltCallback
break;
#endif
#ifdef __CAN_H__
// CAN
case 2: /* CAN */
// activate can and start receiving app.bin
FLAGS.InitOrWait=1;
res_hal = HAL_CAN_Start(&hcan);
res_hal = HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING | CAN_IT_ERROR | CAN_IT_BUSOFF | CAN_IT_LAST_ERROR_CODE);
// code of writing app in flash in callback functions:
// HAL_CAN_RxFifo0MsgPendingCallback
break;
#endif
// SD
#ifdef __SDIO_H__
case 3: /* SD */
if (FLAGS.SDCardIn==0) // wait for sd-card
{
FLAGS.StartInit=1; // if there is no card, exit programmator and go in again
BOOTLOADER_ERRORS.SD_CARD_NOT_INSERTED = 1;
//Ошибка SD карты. Начать приём новой cmd
break;
}
HAL_Delay(100); // wait for inputting SD Card
if (FLAGS.SDCardIn==0)
{
FLAGS.StartInit=1;
BOOTLOADER_ERRORS.SD_CARD_NOT_INSERTED = 1;
break;
}
FRESULT res_fresult;
SD_Programming(&res_fresult, &res_hal); // programming from sd card
// res_fresual - status for sd card read // res_hal - status for flash write
//Если всё удачно - выполнить код ниже. Если нет - StartInit = 1, break
if(res_fresult!=FR_OK || res_hal!=HAL_OK)
{
FLAGS.StartInit=1;
if(res_fresult!=FR_OK){
BOOTLOADER_ERRORS_COUNTERS.SD_CARD_READ++;
BOOTLOADER_ERRORS.SD_CARD_READ = 1;
}
if(res_hal!=HAL_OK){
BOOTLOADER_ERRORS_COUNTERS.FLASH_WRITE++;
BOOTLOADER_ERRORS.FLASH_WRITE = 1;
}
break;
}
// write Key: application in Flash, and reset MCU
{
ResetKey();
SetKey();
NVIC_SystemReset();
}// reset mcu
break;
#endif
default: // if command is incorrect - wait CMD
BOOTLOADER_ERRORS_COUNTERS.CMD_INVALID++; // uncorrect command
BOOTLOADER_ERRORS.CMD_INVALID=1;
do
{
res_hal=HAL_UART_Receive_IT(&huart_boot, ReceiveDataUART, sizeof(ReceiveDataUART));
if(res_hal!=HAL_OK){
BOOTLOADER_ERRORS_COUNTERS.USART_RECEIVE++; // err when initialize uart
BOOTLOADER_ERRORS.USART_RECEIVE=1;}
}while(res_hal!=HAL_OK);
HAL_CAN_Start(&hcan);
HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
}
}
// TxHeader.StdId = 0x200; // ID OF MESSAGE
// TxHeader.ExtId = 0; // STANDART FRAME (NOT EXTENTED)
// TxHeader.RTR = CAN_RTR_DATA; // TRANSMIT DATA OR
// TxHeader.IDE = CAN_ID_STD; // STANDART FRAME
// TxHeader.DLC = 8; // DATA SIZE
// TxHeader.TransmitGlobalTime = DISABLE; //THIS MODE IS NOT USED, SO DISABLE
// uint8_t asd[8] = "ABCDEFGL";
// while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0); // wait for free mail box
// res_hal = HAL_CAN_AddTxMessage(&hcan, &TxHeader, asd, &TxMailBox); // add to mail for transmit
// HAL_Delay(1000);
}
}
}
void SD_Programming(FRESULT *res_fresult, HAL_StatusTypeDef *res_hal)
{
// init peripth
MX_FATFS_Init();
MX_SDIO_SD_Init();
FIL MFile;
int SizeApp;
int AppCount;
// mount disk
uint8_t MOUNT=1;
*res_fresult = f_mount(&SDFatFS, (TCHAR const*)SDPath, 1);
if(*res_fresult != FR_OK) return;
// name of the application file
static char path[8] = "app.bin";
path[7] = '\0';
// OPEN AND READ
*res_fresult = f_open(&MFile, (const TCHAR*)path, FA_READ);
if (*res_fresult == FR_OK)
{
SizeApp = MFile.fsize;
AppCount = 0; // counter of written bytes
unsigned int bytesRead;
// reading and writing two pages
while(SizeApp - AppCount >= PAGE_SIZE*2) // read while count of rest bytes more than size of two pages
{
*res_fresult = f_read(&MFile, Data2Write, PAGE_SIZE*2, &bytesRead);
if(*res_fresult != FR_OK) return;
AppCount += PAGE_SIZE*2;
*res_hal = FLASH_Write_Page(&app_current_add, Data2Write, PAGE_SIZE*2);
if(*res_hal != HAL_OK) return;
GPIOB->ODR^=0x2000; // indicate written two pages
}
// reading and writing rest bytes (less than two pages)
if(SizeApp != AppCount)
{
int NumOfLastBytes = SizeApp - AppCount;
*res_fresult = f_read(&MFile, Data2Write, NumOfLastBytes, &bytesRead);
if(*res_fresult != FR_OK) return;
AppCount += PAGE_SIZE*2;
*res_hal = FLASH_Write_Page(&app_current_add, Data2Write, NumOfLastBytes);
if(*res_hal != HAL_OK) return;
GPIOB->ODR^=0x2000;
}
*res_fresult = f_close(&MFile); // indicate written two pages
}
}
void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart) // writing first half of dma buffer (1 page)
{
if (huart->Instance == UART_BOOT)
{
if (FLAGS.InitOrWait)
{
HAL_StatusTypeDef res_hal;
if (FLAGS.InitBootPeriph) // if its first received page
{
//HAL_TIM_Base_Start_IT(&htim_boot); // start "wd" timer
FLAGS.InitBootPeriph = 0; // 5 sec silent on RX - mcu reset
}
res_hal = FLASH_Write_Page(&app_current_add, Data2Write, PAGE_SIZE);
if (res_hal != HAL_OK)
{
BOOTLOADER_ERRORS_COUNTERS.FLASH_WRITE++; // err when initialize uart
BOOTLOADER_ERRORS.FLASH_WRITE=1;
}
// HAL_UART_DMAStop(&huart_boot);
// HAL_UART_AbortReceive_IT(&huart_boot);
// HAL_UART_AbortReceive(&huart_boot);
FLAGS.HalfOfWrite = 1; // switch unwritten half of DMA buffer
cnt_tim_reset = 0; // upd WD Tim
GPIOB->ODR^=0x2000; // indicate written page
}
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) // writing second half of dma buffer (1 page)
{
if (huart->Instance == UART_BOOT)
{
if (FLAGS.InitOrWait == 0) // if its wait mode (not writting app)
{
// set Init mode, start WDTim after receiving first page
Check_CMD_USART(ReceiveDataUART); // check received uart data
FLAGS.InitOrWait = 1;
FLAGS.StartInit = 1;
FLAGS.InitBootPeriph = 1;
app_current_add = MAIN_APP_START_ADR; // set adress for app
}
else
{
HAL_StatusTypeDef res_hal;
res_hal = FLASH_Write_Page(&app_current_add, Data2Write+PAGE_SIZE, PAGE_SIZE);
if (res_hal != HAL_OK)
{
BOOTLOADER_ERRORS_COUNTERS.FLASH_WRITE++; // err when initialize uart
BOOTLOADER_ERRORS.FLASH_WRITE=1;
}
FLAGS.HalfOfWrite = 0; // switch unwritten half of DMA buffer
cnt_tim_reset = 0; // upd WD Tim
GPIOB->ODR^=0x2000; // indicate written page
}
}
}
uint32_t temp_app_cur_add;
int app_size;
/*void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
if(HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, CAN_Data) == HAL_OK)
{
#ifdef _CAN_PUSH_ID_TO_ADDR_
if(FLAGS.InitOrWait)
{ // if its 1 - Init: writting app.bin in flash
temp_app_cur_add = app_current_add+(RxHeader.ExtId&(0x0000FFFF));
FLASH_Write_Page(&temp_app_cur_add, CAN_Data, 8);
app_size-=8; // decrease app_size
if (app_size<=0) // when its gone - reset system and go to the app
{
__ASM("");
NVIC_SystemReset();
}
}
else
{ // if its 0 - Wait: parsing address to writting and size of app.bin
app_current_add = Data2Write[0]<<24 | Data2Write[1]<<16 | Data2Write[2]<<8 | Data2Write[3];
app_size = Data2Write[4]<<24 | Data2Write[5]<<16 | Data2Write[6]<<8 | Data2Write[7];
FLAGS.InitOrWait = 1; // switch to firmware (init)
}
#endif
if(FLAGS.InitOrWait)
{
if (FLAGS.InitBootPeriph) // if its first received page
{
HAL_TIM_Base_Start_IT(&htim_boot); // start "wd" timer
FLAGS.InitBootPeriph = 0; // 5 sec silent on RX - mcu reset
}
FLASH_Write_Page(&app_current_add, CAN_Data, 8);
cnt_tim_reset=0;
}
else
{
app_current_add = MAIN_APP_START_ADR; // set adress for app
Check_CMD_USART(CAN_Data);
FLAGS.InitBootPeriph=1;
FLAGS.InitOrWait = 1;
FLAGS.StartInit = 1;
}
}
}
*/
void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
{
uint32_t er;
er = HAL_CAN_GetError(hcan);
__ASM("");
}
void Bootloader_TIM_Handler(void) // reset mcu after writing application is done
{ // add this to the TIM handler that is used (TIMx is seting up in project_setup.h)
HAL_StatusTypeDef res_fresult;
cnt_tim_reset++;
if (cnt_tim_reset > 5) // writing is done if there is silence on Rx for 5 second
{
if(FLAGS.BootloaderCMD==1)
{
FLAGS.InitMCUReady = 1;
ResetKey();
SetKey(); // write key for going to application
int NumOfLastBytes = (PAGE_SIZE - DMA1_Channel3->CNDTR%PAGE_SIZE); // read size of new data in dma buffer
if (NumOfLastBytes != 0)
{
res_fresult = FLASH_Write_Page(&app_current_add, Data2Write+PAGE_SIZE*FLAGS.HalfOfWrite, NumOfLastBytes); // writing last data
}
NVIC_SystemReset(); // reset mcu
}
else if(FLAGS.BootloaderCMD==2)
{
FLAGS.InitMCUReady = 1;
ResetKey();
SetKey();
NVIC_SystemReset();
}
}
}
void Check_CMD_USART(uint8_t *DataUART) // choose CMD (interface)
{ // 4 byte: 0xFF - erase app, else - just write app
//USART
if ((DataUART[0]|
DataUART[1]<<8|
DataUART[2]<<16) == 0xFFFFFF)
{
FLAGS.BootloaderCMD = 1;
}
//CAN
else if ((DataUART[0]|
DataUART[1]<<8|
DataUART[2]<<16) == 0xAAAFFF)
{
FLAGS.BootloaderCMD = 2;
}
//SDIO
else if ((DataUART[0]|
DataUART[1]<<8|
DataUART[2]<<16) == 0xAAFAFF)
{
FLAGS.BootloaderCMD = 3;
BSP_SD_DetectIT();
}
if (DataUART[3]== 0xFF)
{
FLAGS.reInitMCU = 1;
}
else
{
FLAGS.reInitMCU = 0;
}
}
// reset/set key function
void SetKey(void)
{
HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, BOOTLOADER_KEY_ADR, BL_KEY_APP_WRITTEN);
HAL_FLASH_Lock();
}
uint32_t ReadKey(void)
{
return (*(__IO uint32_t*)BOOTLOADER_KEY_ADR);
}
void ResetKey(void)
{
HAL_FLASH_Unlock();
uint32_t PageError = 0x00;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;// erase pages
EraseInitStruct.PageAddress = BOOTLOADER_KEY_ADR; //address
EraseInitStruct.NbPages = 0x01;// num of erased pages
HAL_FLASHEx_Erase(&EraseInitStruct, &PageError);
HAL_FLASH_Lock();
}
HAL_StatusTypeDef BSP_SD_ITConfig(void)
{
/* Code to be updated by the user or replaced by one from the FW pack (in a stmxxxx_sd.c file) */
GPIO_InitTypeDef gpio_init_structure;
/* Configure Interrupt mode for SD detection pin */
gpio_init_structure.Pin = SDIO_SDCard_In_PIN;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Speed = GPIO_SPEED_HIGH;
gpio_init_structure.Mode = GPIO_MODE_IT_RISING_FALLING;
HAL_GPIO_Init(SDIO_SDCard_In_PORT, &gpio_init_structure);
/* Enable and set SD detect EXTI Interrupt to the lowest priority */
HAL_NVIC_SetPriority((SDIO_SDCard_In_IRQn), 0x00, 0x00);
HAL_NVIC_EnableIRQ((SDIO_SDCard_In_IRQn));
return HAL_OK;
// return (uint8_t)0;
}
void BSP_SD_DetectIT(void)
{ // add this to the EXTI handler that is used (EXTIx (GPIO_PIN_x) is seting up in project_setup.h)
if(SDIO_SDCard_In_PORT->IDR&SDIO_SDCard_In_PIN)
FLAGS.SDCardIn = 0;
else
FLAGS.SDCardIn = 1;
}
void Boot_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_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
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();
}
HAL_RCC_MCOConfig(RCC_MCO, RCC_MCO1SOURCE_PLLCLK, RCC_MCODIV_1);
}

131
Core/Src/can.c Normal file
View File

@@ -0,0 +1,131 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file can.c
* @brief This file provides code for the configuration
* of the CAN instances.
******************************************************************************
* @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 "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 = 2;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan.Init.TimeSeg1 = CAN_BS1_13TQ;
hcan.Init.TimeSeg2 = CAN_BS2_2TQ;
hcan.Init.TimeTriggeredMode = DISABLE;
hcan.Init.AutoBusOff = ENABLE;
hcan.Init.AutoWakeUp = DISABLE;
hcan.Init.AutoRetransmission = DISABLE;
hcan.Init.ReceiveFifoLocked = DISABLE;
hcan.Init.TransmitFifoPriority = ENABLE;
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);
/* CAN1 interrupt Init */
HAL_NVIC_SetPriority(USB_HP_CAN1_TX_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USB_HP_CAN1_TX_IRQn);
HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
HAL_NVIC_SetPriority(CAN1_SCE_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(CAN1_SCE_IRQn);
/* 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);
/* CAN1 interrupt Deinit */
HAL_NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn);
HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
HAL_NVIC_DisableIRQ(CAN1_SCE_IRQn);
/* USER CODE BEGIN CAN1_MspDeInit 1 */
/* USER CODE END CAN1_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

94
Core/Src/custom_flash.c Normal file
View File

@@ -0,0 +1,94 @@
#include "custom_flash.h"
#include "bootloader.h"
FLASH_EraseInitTypeDef EraseInitStruct;
//uint32_t PAGE_OFFSET = ((uint32_t)((4-1) * 0x0400));
uint32_t PAGE_NUMB = 127;
uint8_t *FLASH_Read(uint32_t add)
{
return (uint8_t *)add;
}
HAL_StatusTypeDef FLASH_Write_Word(uint32_t Address, uint64_t Data) //Куда записывать
{
HAL_StatusTypeDef res;
res = HAL_FLASH_Unlock();
if (res != HAL_OK) return res;
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, (uint32_t)(Data));
if (res != HAL_OK) return res;
res = HAL_FLASH_Lock();
return res;
}
uint32_t word_data;
HAL_StatusTypeDef FLASH_Write_Page(uint32_t *Address, uint8_t *Data, int Data_size)
{
//GPIOB->ODR^=(0x2000);
// GPIOB->ODR|=0x4000;
HAL_StatusTypeDef res;
int data_cnt = 0;
uint32_t adr;
res = HAL_FLASH_Unlock();
if (res != HAL_OK) return res;
for (adr = *Address; adr < *Address + Data_size; adr = adr+4)
{
word_data = (
Data[data_cnt]|
Data[data_cnt+1]<<8|
Data[data_cnt+2]<<16|
Data[data_cnt+3]<<24);
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, adr, word_data);
if (res != HAL_OK) return res;
data_cnt +=4;
}
*Address += Data_size;
res = HAL_FLASH_Lock();
return res;
}
HAL_StatusTypeDef FLASH_Erase_App(void) //
{
HAL_StatusTypeDef res;
uint32_t PageError = 0x00;
res = HAL_FLASH_Unlock();
if (res != HAL_OK) return res;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;// erase pages
EraseInitStruct.Banks = 1;
EraseInitStruct.PageAddress = MAIN_APP_START_ADR; //address
EraseInitStruct.NbPages = MAIN_APP_NUM_OF_PAGE;// num of erased pages
res = HAL_FLASHEx_Erase(&EraseInitStruct, &PageError);
if (res != HAL_OK) return res;
res = HAL_FLASH_Lock();
return res;
}

View File

@@ -0,0 +1,105 @@
#include "custom_receive_and_write.h"
extern UART_HandleTypeDef huart3;
struct flags flags_UART_FATFS;
extern FIL SDFile;
uint8_t bytescnt;
char Char_Shift = 0x41;
FIL MFile;
uint8_t Message[sizebuff] = "";
uint8_t path_uart[5] = "A.txt";
//void MountSD(void)
//{
// if((f_mount(&SDFatFS, (TCHAR const*)SDPath, MOUNT)) ==FR_OK)
// {
// for (int i=0;i<5;i++)
// {
// GPIOB->ODR^=1<<14;
// HAL_Delay(500);
// }
// GPIOB->ODR&=~(1<<14); \
// }
//}
FRESULT CreateAndOpenMessageFile(FIL *MessageFile, const TCHAR* Path)
{
return f_open(MessageFile, Path, FA_WRITE | FA_CREATE_ALWAYS);
}
FRESULT OpenMessageFile(FIL *MessageFile, const TCHAR* Path)
{
return f_open(MessageFile, Path, FA_READ);
}
FRESULT WriteMessage(FIL *MessageFile, char *Message, UINT SizeOfMessage)
{
FRESULT ResultOfOperation;
uint32_t WrittenBytes;
ResultOfOperation = f_lseek(MessageFile,f_size(MessageFile));
ResultOfOperation = f_write(MessageFile, Message, SizeOfMessage, &WrittenBytes);
if (WrittenBytes!=SizeOfMessage) return FR_INVALID_PARAMETER;
//GPIOB->ODR^=0xf000;
return ResultOfOperation;
}
FRESULT CloseMessageFile(FIL *MessageFile)
{
return f_close(MessageFile);
}
void WriteFile(char *Data, int Num_of_Data)
{
static int FileOpen = 0;
OpenMessageFile(&MFile, (const TCHAR*)path_uart);
WriteMessage(&MFile, Data, Num_of_Data);
CloseMessageFile(&MFile);
// Char_Shift++;
// path_uart[0] = (char)Char_Shift;
}
void Check_USART(void)
{
uint8_t res;
static FIL MessageFile;
if (flags_UART_FATFS.writting==0)
{ switch((int)Message[0])
{
case 1:
flags_UART_FATFS.writting = 1;
Message[bytescnt] = '\0';
res = CreateAndOpenMessageFile(&MessageFile, (const TCHAR*)(Message+1));
break;
case 2:
flags_UART_FATFS.writting = 1;
res = OpenMessageFile(&MessageFile, (const TCHAR*)path_uart);
break;
}
}
else
{
if (Message[0] == 0x03)
{
flags_UART_FATFS.writting = 0;
res = CloseMessageFile(&MessageFile);
}
else
{
res = WriteMessage(&MessageFile, (char *)Message, bytescnt);
}
}
HAL_UART_AbortReceive_IT(&huart3);
HAL_UART_Receive_IT(&huart3, Message, sizeof(Message));
bytescnt = 0;
for(int i = 0; i<sizebuff; i++)
{
Message[i] = 0x00;
}
}

125
Core/Src/custom_usart.c Normal file
View File

@@ -0,0 +1,125 @@
#ifndef _CUSTOM_USART_
#define _CUSTOM_USART_
#include "custom_usart.h"
UART_HandleTypeDef huart_boot;
DMA_HandleTypeDef hdma_usart_boot_rx;
// CUSTOM UART INIT
// DMA INIT (RCC, NVIC) SHOULD BE ADDED IN DMA.C
void User_UART_Init(UART_HandleTypeDef* huart, DMA_HandleTypeDef *DMAhuart, struct UARTSettings *uuart)
{ // function takes uart handler, dma handler and setting structure for init
// get setting for uart from UARTSettings structure
huart->Instance = uuart->UARTx;
huart->Init.BaudRate = uuart->UART_Speed;
// everything else is default (for now, maybe this settings would be added in UARTSettings structure later)
huart->Init.WordLength = UART_WORDLENGTH_8B;
huart->Init.StopBits = UART_STOPBITS_1;
huart->Init.Parity = UART_PARITY_NONE;
huart->Init.Mode = UART_MODE_TX_RX;
huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart->Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(huart) != HAL_OK)
{
Error_Handler();
}
// init gpio from UARTSettings structure
UART_GPIO_Init(uuart->GPIOx, uuart->GPIO_PIN_RX, uuart->GPIO_PIN_TX);
// init dma from UARTSettings structure if need
if (uuart->DMAChannel != 0)
UART_DMA_Init(huart, DMAhuart, uuart->DMAChannel);
}
void UART_GPIO_Init(GPIO_TypeDef *GPIOx, uint16_t GPIO_PIN_RX, uint16_t GPIO_PIN_TX)
{ // function takes port and pins (for rx and tx)
GPIO_InitTypeDef GPIO_InitStruct = {0};
// choose port for enable clock
if (GPIOx==GPIOA)
__HAL_RCC_GPIOA_CLK_ENABLE();
else if (GPIOx==GPIOB)
__HAL_RCC_GPIOB_CLK_ENABLE();
else if (GPIOx==GPIOC)
__HAL_RCC_GPIOC_CLK_ENABLE();
else if (GPIOx==GPIOD)
__HAL_RCC_GPIOD_CLK_ENABLE();
// USART3 GPIO Configuration
//GPIO_PIN_TX ------> USART_TX
GPIO_InitStruct.Pin = GPIO_PIN_TX;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
// GPIO_PIN_RX ------> USART_RX
GPIO_InitStruct.Pin = GPIO_PIN_RX;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
}
void UART_DMA_Init(UART_HandleTypeDef *huart, DMA_HandleTypeDef *hdma_rx, DMA_Channel_TypeDef *DMAhuart)
{ // function takes uart and dma handlers and dmachannel for uart
// for now only dma rx is supported, tx maybe later if needed
/* USART3 DMA Init */
/* USART3_RX Init */
hdma_rx->Instance = DMAhuart;
hdma_rx->Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_rx->Init.PeriphInc = DMA_PINC_DISABLE;
hdma_rx->Init.MemInc = DMA_MINC_ENABLE;
hdma_rx->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_rx->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_rx->Init.Mode = DMA_CIRCULAR;
hdma_rx->Init.Priority = DMA_PRIORITY_LOW;
if (HAL_DMA_Init(hdma_rx) != HAL_OK)
{
Error_Handler();
}
__USER_LINKDMA(huart,hdmarx,hdma_rx);
// __USER_LINKDMA is need because __HAL_LINKDMA is written for global defined hdma_rx
// so you get error because hal uses . insted of ->
}
void HAL_UART_MspInit(UART_HandleTypeDef* huart) // redefine hal function
{ // left only rcc and interrupt init for USART_1,2,3 (maybe UART_4,5 need to be added)
// GPIO and DMA init was move to their own functions
if(huart->Instance==USART3)
{
/* USART3 clock enable */
__HAL_RCC_USART3_CLK_ENABLE();
/* USART3 interrupt Init */
HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART3_IRQn);
}
else if(huart->Instance==USART2)
{
/* USART3 clock enable */
__HAL_RCC_USART2_CLK_ENABLE();
/* USART3 interrupt Init */
HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART2_IRQn);
}
else if(huart->Instance==USART1)
{
/* USART3 clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
/* USART3 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
}
}
#endif

59
Core/Src/dma.c Normal file
View File

@@ -0,0 +1,59 @@
/* 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) 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 "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_DMA2_CLK_ENABLE();
__HAL_RCC_DMA1_CLK_ENABLE();
/* DMA interrupt init */
/* DMA1_Channel3_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel3_IRQn);
/* DMA2_Channel4_5_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA2_Channel4_5_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA2_Channel4_5_IRQn);
}
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */

75
Core/Src/gpio.c Normal file
View File

@@ -0,0 +1,75 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file gpio.c
* @brief This file provides code for the configuration
* of all used GPIO pins.
******************************************************************************
* @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 "gpio.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/*----------------------------------------------------------------------------*/
/* Configure GPIO */
/*----------------------------------------------------------------------------*/
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/** Configure pins
PA8 ------> RCC_MCO
*/
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
/*Configure GPIO pins : PB12 PB13 PB14 PB15 */
GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pin : PA8 */
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : PB5 */
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */

233
Core/Src/main.c Normal file
View File

@@ -0,0 +1,233 @@
/* 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 "fatfs.h"
#include "rtc.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "custom_flash.h"
#include "gpio.h"
//#include "bootloader.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
int friman=0;
/* 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 */
/* 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 */
//uint32_t programBytes2Read;
//uint32_t programBytesCounter;
//uint32_t currentAddress;
//UINT readBytes;
//FRESULT result;
//extern uint8_t ReceiveDataUART[4];
//extern uint8_t Data2Write[PAGE_SIZE*2];
//extern struct flag FLAGS;
//uint8_t *FLASH_Read(uint32_t add)
//{
// return (uint8_t *)add;
//}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
SystemClock_Config();
//Bootloader_main();
REQUESTER_Init();
REQUESTER_MainWhile();
/* 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 */
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_RTC_Init();
/* USER CODE BEGIN 2 */
// MX_USER_UART_Init();
// HAL_UART_Receive_IT(&huart_boot, ReceiveDataUART, sizeof(ReceiveDataUART));
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* if (friman)
{
SetKey();
HAL_NVIC_SystemReset();
}*/
// if (FLAGS.reInitMCU == 1)
// {
// HAL_UART_Receive_DMA(&huart_boot, Data2Write, PAGE_SIZE*2);
// }
//
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
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();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
HAL_RCC_MCOConfig(RCC_MCO, RCC_MCO1SOURCE_PLLCLK, RCC_MCODIV_1);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief Period elapsed callback in non blocking mode
* @note This function is called when TIM8 interrupt took place, inside
* HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
* a global variable "uwTick" used as application time base.
* @param htim : TIM handle
* @retval None
*/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
/* USER CODE BEGIN Callback 0 */
/* USER CODE END Callback 0 */
if (htim->Instance == TIM8) {
HAL_IncTick();
}
/* USER CODE BEGIN Callback 1 */
/* USER CODE END Callback 1 */
}
/**
* @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 */

619
Core/Src/requester.c Normal file
View File

@@ -0,0 +1,619 @@
#include "requester.h"
static union Byte byte[2048][8];
#define byte_modbusadr(x) byte[x/64][(x%64)/8]
#define _GET_MODBUS_BIT(x) byte[x/64][(x%64)/8].AllBit>>((x%64)%8)
#define _GET_MODBUS_ADR(SensorId, RequestedBytePosition, RequestedBitPosition) SensorId*64 + RequestedBytePosition*8 + RequestedBitPosition
uint16_t ModbusFilters[COUNT_OF_MODBUS_SECTIONS][2];
struct device CurrentDevice;
struct device Device_on_the_Network[32];
struct data Data;
struct controlflags ControlFlags;
struct received_request ReceivedRequest;
#define MAX_NUM_OF_DEVICES_PER_LINE 4
#define MAX_NUM_OF_REGISTERS_IN_DEVICE 255
//Регистр это слово (16 бит). uint16
uint16_t ModbusDemonstration[MAX_NUM_OF_DEVICES_PER_LINE][MAX_NUM_OF_REGISTERS_IN_DEVICE];
uint16_t ModbusAlternativeTable[MAX_NUM_OF_DEVICES_PER_LINE*MAX_NUM_OF_REGISTERS_IN_DEVICE];
_Bool IsLeapYear(uint8_t year)
{
year+=2000;
return (year%400==0)||((year%4==0)&&(year%100!=0));
}
/**
* @brief Инициализация переферии
* @details Инициализация HAL, CAN, TIM7, RTC.
* @note Фильтры CAN описаны в разделе REQUESTER_CAN_FILTERS().
*/
void REQUESTER_Init(void)
{
HAL_Init();
MX_CAN_Init();
HAL_CAN_Start(&hcan);
REQUESTER_CAN_FILTERS();
HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
ControlFlags.IsPulse = 1;
MX_TIM7_Init();
MX_RTC_Init();
#ifdef _DEMO
int Reg_AltModbusTable;
//Тестовые значения регистров. Для отладки/демонстрации
for(int x = 0; x < MAX_NUM_OF_DEVICES_PER_LINE; x++)
{
for(int y = 0; y < MAX_NUM_OF_REGISTERS_IN_DEVICE; y++)
{
ModbusDemonstration[x][y] = x<<8 | y;
Reg_AltModbusTable = x*MAX_NUM_OF_REGISTERS_IN_DEVICE+y;
ModbusAlternativeTable[Reg_AltModbusTable] = x<<8 | y;
}
}
#endif
}
int ProverkaDefinaResult;
int ProverkaDefinaItem = 0;
int ProverkaArbitors = 0;
/**
* @brief Функция с обработкой полученных запросов
* @details В бесконечном цикле функция ожидает выставление флага о полученном запросе.
* Обработка запроса аналоговых значений - REQUESTER_AnalogProcessing().
* Обработка широковещательных запросов - REQUESTER_BroadcastProcessing().
* Обработка запроса дискретных значений - REQUESTER_DiscreticProcessing().
* Обработка Modbus - REQUESTER_ModbusProcessing().
* @note
*/
void REQUESTER_MainWhile(void)
{
CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailBox = 0;
uint8_t data[8];
union ext_ID eID;
eID.Fields.DeviceID = 16;
eID.Fields.DataType = DATA_TYPE_ANALOG;
eID.Fields.SensorType = 0x1F;
eID.Fields.SensorID = 0;
eID.Fields.Route = ROUTE_SLAVE;
TxHeader.TransmitGlobalTime = DISABLE;
TxHeader.RTR = CAN_RTR_DATA;
int TxTest = 0;
HAL_TIM_Base_Start_IT(&htim7);
while(1)
{
if(ReceivedRequest.AnalogFlags.AllFlags)
{
REQUESTER_AnalogProcessing();
}
if(ReceivedRequest.BroadcastFlags.AllFlags)
{
REQUESTER_BroadcastProcessing();
}
if(ReceivedRequest.DiscreticFlags.AllFlags)
{
REQUESTER_DiscreticProcessing();
}
if(ReceivedRequest.ModbusFlags.AllFlags)
{
REQUESTER_ModbusProcessing();
}
while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0);
TxHeader.ExtId = eID.BitAll;
if(HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox)!= HAL_OK)
{
ProverkaArbitors++;
}
eID.Fields.SensorID++;
if(eID.Fields.SensorID>100)
{
eID.Fields.SensorID = 0;
}
}
}
/**
* @brief Функция обработки аналоговых запросов.
* @details Функция, формирующая и отправляющая ответ на запросы. Типы запросов: Универсальный, Уставки, Напряжение, Ток, Температура
*/
void REQUESTER_AnalogProcessing()
{
CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailBox = 0;
uint8_t data[8];
if(ReceivedRequest.AnalogFlags.AnalogType.Request_Universal_Sens)
{
ReceivedRequest.AnalogFlags.AnalogType.Request_Universal_Sens = 0;
}
if(ReceivedRequest.AnalogFlags.AnalogType.Request_U_Sens)
{
//Запрос на данные датчика напряжения.
//В дальнейшем реализовать отправку настоящих данных.
//А пока - тестовое сообщение, нужное для отладки.
//Расширенный ID
TxHeader.IDE=CAN_ID_EXT;
//Ответ на запрос осуществляется по тому-же ID,
//с которым был отправлен запрос.
union ext_ID eID;
eID.BitAll = ReceivedRequest.RequestedExtID.BitAll;
eID.Fields.Route = ROUTE_SLAVE;
TxHeader.ExtId = eID.BitAll;
//Выставляется количество передаваемых байтов. (Макс. 8)
TxHeader.DLC = 6;
data[0] = 'U';
data[1] = ' ';
data[2] = 's';
data[3] = 'e';
data[4] = 'n';
data[5] = 's';
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
ReceivedRequest.AnalogFlags.AnalogType.Request_U_Sens=0;
}
if(ReceivedRequest.AnalogFlags.AnalogType.Request_I_Sens)
{
TxHeader.IDE=CAN_ID_EXT;
union ext_ID eID;
eID.BitAll = ReceivedRequest.RequestedExtID.BitAll;
eID.Fields.Route = ROUTE_SLAVE;
TxHeader.ExtId = eID.BitAll;
TxHeader.DLC = 6;
data[0] = 'I';
data[1] = ' ';
data[2] = 's';
data[3] = 'e';
data[4] = 'n';
data[5] = 's';
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
ReceivedRequest.AnalogFlags.AnalogType.Request_I_Sens=0;
}
if(ReceivedRequest.AnalogFlags.AnalogType.Request_T_Sens)
{
TxHeader.IDE=CAN_ID_EXT;
union ext_ID eID;
eID.BitAll = ReceivedRequest.RequestedExtID.BitAll;
eID.Fields.Route = ROUTE_SLAVE;
TxHeader.ExtId = eID.BitAll;
TxHeader.DLC = 6;
data[0] = 'T';
data[1] = ' ';
data[2] = 's';
data[3] = 'e';
data[4] = 'n';
data[5] = 's';
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
ReceivedRequest.AnalogFlags.AnalogType.Request_T_Sens=0;
}
}
/**
* @brief Функция обработки широковещательных запросов.
* @details Функция, выполняющая команды, переданные в широковещательном формате с головного (master) устройства. Типы команд: Запрос статуса, запрос на включение или выключение, рестарт устройств, установка времени.
*/
void REQUESTER_BroadcastProcessing()
{
if(ReceivedRequest.BroadcastFlags.BroadcastType.Request_OnOff)
{
//Обработка запроса на вкл/выкл
ControlFlags.IsPulse = !ControlFlags.IsPulse;
ReceivedRequest.BroadcastFlags.BroadcastType.Request_OnOff = 0;
}
if(ReceivedRequest.BroadcastFlags.BroadcastType.Request_RTC_Setup)
{
//Обработка запроса на синхронизацию времени
//С головным устройством
if(ReceivedRequest.RequestedDLC > 7)
{
//ERROR
}
else
{
int DaysCount_Normal[2][12] = {{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
if (ReceivedRequest.RxData[0]>23 ||
ReceivedRequest.RxData[1]>59 ||
ReceivedRequest.RxData[2]>59 ||
ReceivedRequest.RxData[3]>99 ||
ReceivedRequest.RxData[4]>12 ||
ReceivedRequest.RxData[5] > DaysCount_Normal[IsLeapYear(ReceivedRequest.RxData[3])][ReceivedRequest.RxData[4]] ||
ReceivedRequest.RxData[6]>6)
{
//ERROR
}
else
{
REQUESTER_RTC_SYNC(ReceivedRequest.RxData);
}
}
ReceivedRequest.BroadcastFlags.BroadcastType.Request_RTC_Setup = 0;
}
if(ReceivedRequest.BroadcastFlags.BroadcastType.Request_Status)
{
//Обработка запроса статуса устройства
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef DateToUpdate = {0};
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailBox = 0;
uint8_t data[8];
TxHeader.IDE=CAN_ID_EXT;
union ext_ID eID;
eID.BitAll = ReceivedRequest.RequestedExtID.BitAll;
eID.Fields.Route = ROUTE_SLAVE;
TxHeader.ExtId = eID.BitAll;
TxHeader.DLC = 7;
data[0] = sTime.Hours;
data[1] = sTime.Minutes;
data[2] = sTime.Seconds;
HAL_RTC_GetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN);
data[3] = DateToUpdate.Year;
data[4] = DateToUpdate.Month;
data[5] = DateToUpdate.Date;
data[6] = DateToUpdate.WeekDay;
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
ReceivedRequest.AnalogFlags.AnalogType.Request_T_Sens=0;
ReceivedRequest.BroadcastFlags.BroadcastType.Request_Status = 0;
}
}
/**
* @brief Функция обработки дискретных запросов.
* @details Функция, формирующая и отправляющая ответ на запросы. Типы запросов: Аварии, Предупреждения, Управляющие сигналы, Флаги, Рестарт устройства, Изменение режима работы устройства, Запрос на устройство.
* @note Запрос на устройство. Головное (master) устройство запрашивает некоторое колличество параметров. В Data - 64 битовых адресса параметров, тип которых задаётся в Sensor ID. Имеется возможность запрашивать непоследовательные параметры.
*/
void REQUESTER_DiscreticProcessing()
{
if(ReceivedRequest.DiscreticFlags.DiscreticType.Request_Accident)
{
ReceivedRequest.DiscreticFlags.DiscreticType.Request_Accident = 0;
}
if(ReceivedRequest.DiscreticFlags.DiscreticType.Request_Control_Signals)
{
ReceivedRequest.DiscreticFlags.DiscreticType.Request_Control_Signals = 0;
}
if(ReceivedRequest.DiscreticFlags.DiscreticType.Request_Flags)
{
ReceivedRequest.DiscreticFlags.DiscreticType.Request_Flags = 0;
}
if(ReceivedRequest.DiscreticFlags.DiscreticType.Request_Warning)
{
ReceivedRequest.DiscreticFlags.DiscreticType.Request_Warning = 0;
}
if(ReceivedRequest.DiscreticFlags.DiscreticType.Request_Reset)
{
ReceivedRequest.DiscreticFlags.DiscreticType.Request_Reset = 0;
NVIC_SystemReset();
}
if(ReceivedRequest.DiscreticFlags.DiscreticType.Request_List_of_Parameters)
{
ReceivedRequest.DiscreticFlags.DiscreticType.Request_List_of_Parameters = 0;
for(int Current_byte = 0; Current_byte < 8; Current_byte++)
{
for(int Current_bit = 0; Current_bit < 8; Current_bit++)
{
if((ReceivedRequest.RxData[Current_byte]>>Current_bit)&0b1)
{
_GET_MODBUS_ADR(ReceivedRequest.RequestedExtID.Fields.SensorID, Current_byte, Current_bit);
}
}
}
}
}
/**
* @brief Функция обработки Modbus запросов.
* @details Функция, формирующая и отправляющая ответ на запросы.
*/
void REQUESTER_ModbusProcessing()
{
if((ReceivedRequest.SensorToModbus.Modbus.StrAdr>=0) && (ReceivedRequest.SensorToModbus.Modbus.StrAdr<=127))
{
//Обращение к существующему в устройстве модбас регистру
CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailBox = 0;
uint8_t data[8];
TxHeader.IDE = CAN_ID_EXT;
TxHeader.DLC = 8;
TxHeader.RTR = CAN_RTR_DATA;
int RequestFromDLC;
for(RequestFromDLC = ReceivedRequest.SensorToModbus.Modbus.StrAdr;
(RequestFromDLC<MAX_NUM_OF_REGISTERS_IN_DEVICE) &&
((RequestFromDLC-ReceivedRequest.SensorToModbus.Modbus.StrAdr)<ReceivedRequest.SensorToModbus.Modbus.Count);
// RequestFromDLC++
)
{
TxHeader.DLC = 0;
union ext_ID eID;
eID.BitAll = ReceivedRequest.RequestedExtID.BitAll;
eID.Fields.Route = ROUTE_SLAVE;
eID.Fields.SensorType = RequestFromDLC>>11;
eID.Fields.SensorID = RequestFromDLC;
TxHeader.ExtId = eID.BitAll;
for(int DataFor = 0; DataFor < 8; DataFor+=2)
{
data[DataFor] = HighByteOfWord(ModbusDemonstration[CURRENT_ID_DEVICE][RequestFromDLC]);
data[DataFor+1] = LowByteOfWord(ModbusDemonstration[CURRENT_ID_DEVICE][RequestFromDLC]);
RequestFromDLC++;
TxHeader.DLC +=2;
if(!((RequestFromDLC<MAX_NUM_OF_REGISTERS_IN_DEVICE) &&
((RequestFromDLC-ReceivedRequest.SensorToModbus.Modbus.StrAdr)<ReceivedRequest.SensorToModbus.Modbus.Count)))
{
break;
}
}
while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0);
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
}
union ext_ID_Modbus extID;
extID.Fields.DeviceID = CURRENT_ID_DEVICE;
extID.Fields.DataType = DATA_TYPE_ERROR;
extID.Fields.CountReg = NONEXISTENT_ELEMENT;
extID.Fields.Route = ROUTE_SLAVE;
TxHeader.DLC = 0;
for(;(RequestFromDLC-ReceivedRequest.SensorToModbus.Modbus.StrAdr)<ReceivedRequest.SensorToModbus.Modbus.Count; RequestFromDLC++)
{
extID.Fields.StrAdr = RequestFromDLC;
TxHeader.ExtId = extID.BitAll;
while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0);
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
}
ReceivedRequest.ModbusFlags.AllFlags = 0;
return;
}
ReceivedRequest.ModbusFlags.AllFlags = 0;
}
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
CAN_RxHeaderTypeDef RxHeader;
uint8_t RCAN_Data[8];
HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RCAN_Data);
//Расширенный ID
if(RxHeader.IDE == CAN_ID_EXT)
{
union ext_ID ExtID_Of_RX_MSG;
ExtID_Of_RX_MSG.BitAll = RxHeader.ExtId;
//Полученное сообщение - широковещательное
if (ExtID_Of_RX_MSG.Fields.DeviceID == ID_MAIN_DEVICE)
{
if(ExtID_Of_RX_MSG.Fields.DataType == DATA_TYPE_BROADCAST)
{
switch(ExtID_Of_RX_MSG.Fields.SensorType)
{
case SENSOR_TYPE_STATUS:
ReceivedRequest.BroadcastFlags.BroadcastType.Request_Status = 1;
break;
case SENSOR_TYPE_ONOFF:
ReceivedRequest.BroadcastFlags.BroadcastType.Request_OnOff = 1;
break;
case SENSOR_TYPE_RTCSETUP:
ReceivedRequest.RequestedDLC = RxHeader.DLC;
ReceivedRequest.RxData[0] = RCAN_Data[0];
ReceivedRequest.RxData[1] = RCAN_Data[1];
ReceivedRequest.RxData[2] = RCAN_Data[2];
ReceivedRequest.RxData[3] = RCAN_Data[3];
ReceivedRequest.RxData[4] = RCAN_Data[4];
ReceivedRequest.RxData[5] = RCAN_Data[5];
ReceivedRequest.RxData[6] = RCAN_Data[6];
ReceivedRequest.BroadcastFlags.BroadcastType.Request_RTC_Setup = 1;
break;
default:
break;
}
return;
}
}
//Если ID запроса соответствует ID устройства
if(ExtID_Of_RX_MSG.Fields.DeviceID == (CURRENT_ID_DEVICE))
{
if(ExtID_Of_RX_MSG.Fields.DataType == DATA_TYPE_DISCRETE)
{
switch(ExtID_Of_RX_MSG.Fields.SensorType)
{
case SENSOR_TYPE_ACCIDENT:
break;
case SENSOR_TYPE_WARNING:
break;
case SENSOR_TYPE_CONTROL_SIGNALS:
break;
case SENSOR_TYPE_FLAGS:
break;
case SENSOR_TYPE_RESET:
ReceivedRequest.DiscreticFlags.DiscreticType.Request_Reset = 1;
break;
case SENSOR_TYPE_CHANGE_MODE:
ControlFlags.IsRtrMode = !ControlFlags.IsRtrMode;
break;
case SENSOR_TYPE_REQUEST_LIST_OF_PARAMETERS:
ReceivedRequest.DiscreticFlags.DiscreticType.Request_List_of_Parameters = 1;
break;
}
}
if(ExtID_Of_RX_MSG.Fields.DataType == DATA_TYPE_ANALOG)
{
//Является ли полученное сообщение - запросом
if(RxHeader.RTR)
{
ReceivedRequest.RequestedExtID.BitAll = ExtID_Of_RX_MSG.BitAll;
ReceivedRequest.RequestedDLC = RxHeader.DLC;
//Определяется запрашиваемая информация
switch(ExtID_Of_RX_MSG.Fields.SensorType)
{
case SENSOR_TYPE_UNIVERSAL:
{
ReceivedRequest.AnalogFlags.AnalogType.Request_Universal_Sens=1;
break;
}
case SENSOR_TYPE_U:
{
ReceivedRequest.AnalogFlags.AnalogType.Request_U_Sens=1;
break;
}
case SENSOR_TYPE_I:
{
ReceivedRequest.AnalogFlags.AnalogType.Request_I_Sens=1;
break;
}
case SENSOR_TYPE_T:
{
ReceivedRequest.AnalogFlags.AnalogType.Request_T_Sens=1;
break;
}
}
}
}
if(ExtID_Of_RX_MSG.Fields.DataType == DATA_TYPE_MODBUS_COIL)
{
ReceivedRequest.RequestedExtID.BitAll = ExtID_Of_RX_MSG.BitAll;
ReceivedRequest.SensorToModbus.Sensor = SensorToModbusRegister(ReceivedRequest.RequestedExtID.Fields.SensorType, ReceivedRequest.RequestedExtID.Fields.SensorID);
ReceivedRequest.ModbusFlags.ModbusType.Coil = 1;
}
if(ExtID_Of_RX_MSG.Fields.DataType == DATA_TYPE_MODBUS_DISCRETE)
{
ReceivedRequest.RequestedExtID.BitAll = ExtID_Of_RX_MSG.BitAll;
ReceivedRequest.SensorToModbus.Sensor = SensorToModbusRegister(ReceivedRequest.RequestedExtID.Fields.SensorType, ReceivedRequest.RequestedExtID.Fields.SensorID);
ReceivedRequest.ModbusFlags.ModbusType.Discrete = 1;
}
if(ExtID_Of_RX_MSG.Fields.DataType == DATA_TYPE_MODBUS_HOLDING)
{
ReceivedRequest.RequestedExtID.BitAll = ExtID_Of_RX_MSG.BitAll;
ReceivedRequest.SensorToModbus.Sensor = SensorToModbusRegister(ReceivedRequest.RequestedExtID.Fields.SensorType, ReceivedRequest.RequestedExtID.Fields.SensorID);
ReceivedRequest.ModbusFlags.ModbusType.Holding = 1;
}
if(ExtID_Of_RX_MSG.Fields.DataType == DATA_TYPE_MODBUS_INPUT)
{
ReceivedRequest.RequestedExtID.BitAll = ExtID_Of_RX_MSG.BitAll;
ReceivedRequest.SensorToModbus.Sensor = SensorToModbusRegister(ReceivedRequest.RequestedExtID.Fields.SensorType, ReceivedRequest.RequestedExtID.Fields.SensorID);
ReceivedRequest.ModbusFlags.ModbusType.Input = 1;
}
}
//Полученное сообщение - пульс устройств в сети
if (ExtID_Of_RX_MSG.Fields.DataType == DATA_TYPE_PULSE)
{
Device_on_the_Network[ExtID_Of_RX_MSG.Fields.DeviceID].Status = ONLINE;
Device_on_the_Network[ExtID_Of_RX_MSG.Fields.DeviceID].TimeFromLastPulse = 0;
}
}
}
void REQUESTER_Pulse_TIM_Handler()
{
if(ControlFlags.IsPulse)
{
static unsigned PulseStage = 0;
CAN_TxHeaderTypeDef TxHeader;
uint32_t TxMailBox = 0;
union ext_ID currentID;
currentID.BitAll = 0;
currentID.Fields.DeviceID = CURRENT_ID_DEVICE;
currentID.Fields.DataType = DATA_TYPE_PULSE;
currentID.Fields.Route = ROUTE_SLAVE;
TxHeader.ExtId = currentID.BitAll;
uint8_t data[8];
TxHeader.IDE = CAN_ID_EXT;
TxHeader.TransmitGlobalTime = DISABLE;
TxHeader.RTR = CAN_RTR_DATA;
TxHeader.DLC = 1;
if(PulseStage > 0xFF)
{
PulseStage = 0;
}
data[0] = PulseStage++;
HAL_CAN_AddTxMessage(&hcan, &TxHeader, data, &TxMailBox);
}
}
void REQUESTER_RTC_SYNC(uint8_t *data)
{
__HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc);
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef DateToUpdate = {0};
sTime.Hours = data[0];
sTime.Minutes = data[1];
sTime.Seconds = data[2];
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
{
Error_Handler();
}
DateToUpdate.Year = data[3];
DateToUpdate.Month = data[4];
DateToUpdate.Date = data[5];
DateToUpdate.WeekDay = data[6];
if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN) != HAL_OK)
{
Error_Handler();
}
__HAL_RTC_WRITEPROTECTION_ENABLE(&hrtc);
}
void REQUESTER_CAN_FILTERS()
{
//MAIN DEVICE
CAN_FilterTypeDef canFilterConfig;
canFilterConfig.FilterBank = 0;
canFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
canFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
/*Для ID устройства используются восемь младших битов.
Макс значение 0 устройство - 0x000, 1 - 0x002, 2 - 0x004, 3 - 0x006*/
canFilterConfig.FilterIdHigh = (uint16_t)(ID_MAIN_DEVICE>>13);
canFilterConfig.FilterIdLow = (uint16_t)(ID_MAIN_DEVICE<<5) | CAN_IDE_32;
/*Маска 1.1111.1110.<...>. Нули - любые символы. Единицы - точное соответствие фильтру выше.*/
canFilterConfig.FilterMaskIdHigh = (uint16_t)(CAN_DEVICE_ID_FILTER>>13);
canFilterConfig.FilterMaskIdLow = (uint16_t)(CAN_DEVICE_ID_FILTER<<3) | CAN_IDE_32;
canFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
canFilterConfig.FilterActivation = ENABLE;
canFilterConfig.SlaveStartFilterBank = 14;
if(HAL_CAN_ConfigFilter(&hcan, &canFilterConfig) != HAL_OK)
{
Error_Handler();
}
//CURRENT DEVICE
canFilterConfig.FilterBank = 1;
/*Для ID устройства используются восемь младших битов.
Макс значение 0 устройство - 0x000, 1 - 0x002, 2 - 0x004, 3 - 0x006*/
canFilterConfig.FilterIdHigh = (uint16_t)(CURRENT_ID_DEVICE>>13);
canFilterConfig.FilterIdLow = (uint16_t)(CURRENT_ID_DEVICE<<5) | CAN_IDE_32;
/*Маска 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)
{
Error_Handler();
}
//MODBUS
canFilterConfig.FilterBank = 2;
canFilterConfig.FilterIdHigh = (uint16_t)(0x03000000>>13);
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)
{
Error_Handler();
}
//PULSE
canFilterConfig.FilterBank = 3;
canFilterConfig.FilterIdHigh = (uint16_t)(HighIdFilter(DATA_TYPE_PULSE)>>13);
//canFilterConfig.FilterIdHigh = (uint16_t)(0x1F000000>>13);
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)
{
Error_Handler();
}
}

121
Core/Src/rtc.c Normal file
View File

@@ -0,0 +1,121 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file rtc.c
* @brief This file provides code for the configuration
* of the RTC instances.
******************************************************************************
* @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 "rtc.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
RTC_HandleTypeDef hrtc;
/* RTC init function */
void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef DateToUpdate = {0};
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
/** Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
hrtc.Init.OutPut = RTC_OUTPUTSOURCE_ALARM;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN Check_RTC_BKUP */
// Время отсчитывается от 12.03.2024 в секундах (регистры RTC)
/* USER CODE END Check_RTC_BKUP */
/** Initialize RTC and set the Time and Date
*/
sTime.Hours = 0;
sTime.Minutes = 0;
sTime.Seconds = 0;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
{
Error_Handler();
}
DateToUpdate.WeekDay = RTC_WEEKDAY_MONDAY;
DateToUpdate.Month = RTC_MONTH_JANUARY;
DateToUpdate.Date = 1;
DateToUpdate.Year = 0;
if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN RTC_Init 2 */
/* USER CODE END RTC_Init 2 */
}
void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
{
if(rtcHandle->Instance==RTC)
{
/* USER CODE BEGIN RTC_MspInit 0 */
/* USER CODE END RTC_MspInit 0 */
HAL_PWR_EnableBkUpAccess();
/* Enable BKP CLK enable for backup registers */
__HAL_RCC_BKP_CLK_ENABLE();
/* RTC clock enable */
__HAL_RCC_RTC_ENABLE();
/* USER CODE BEGIN RTC_MspInit 1 */
/* USER CODE END RTC_MspInit 1 */
}
}
void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle)
{
if(rtcHandle->Instance==RTC)
{
/* USER CODE BEGIN RTC_MspDeInit 0 */
/* USER CODE END RTC_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_RTC_DISABLE();
/* USER CODE BEGIN RTC_MspDeInit 1 */
/* USER CODE END RTC_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

151
Core/Src/sdio.c Normal file
View File

@@ -0,0 +1,151 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file sdio.c
* @brief This file provides code for the configuration
* of the SDIO instances.
******************************************************************************
* @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 "sdio.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
SD_HandleTypeDef hsd;
DMA_HandleTypeDef hdma_sdio;
/* SDIO init function */
void MX_SDIO_SD_Init(void)
{
/* USER CODE BEGIN SDIO_Init 0 */
/* USER CODE END SDIO_Init 0 */
/* USER CODE BEGIN SDIO_Init 1 */
/* USER CODE END SDIO_Init 1 */
hsd.Instance = SDIO;
hsd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
hsd.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
hsd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
hsd.Init.BusWide = SDIO_BUS_WIDE_4B;
hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
hsd.Init.ClockDiv = 4;
/* USER CODE BEGIN SDIO_Init 2 */
/* USER CODE END SDIO_Init 2 */
}
void HAL_SD_MspInit(SD_HandleTypeDef* sdHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(sdHandle->Instance==SDIO)
{
/* USER CODE BEGIN SDIO_MspInit 0 */
/* USER CODE END SDIO_MspInit 0 */
/* SDIO clock enable */
__HAL_RCC_SDIO_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/**SDIO GPIO Configuration
PC8 ------> SDIO_D0
PC9 ------> SDIO_D1
PC10 ------> SDIO_D2
PC11 ------> SDIO_D3
PC12 ------> SDIO_CK
PD2 ------> SDIO_CMD
*/
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11
|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/* SDIO DMA Init */
/* SDIO Init */
hdma_sdio.Instance = DMA2_Channel4;
hdma_sdio.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_sdio.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_sdio.Init.MemInc = DMA_MINC_ENABLE;
hdma_sdio.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_sdio.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_sdio.Init.Mode = DMA_NORMAL;
hdma_sdio.Init.Priority = DMA_PRIORITY_LOW;
if (HAL_DMA_Init(&hdma_sdio) != HAL_OK)
{
Error_Handler();
}
/* Several peripheral DMA handle pointers point to the same DMA handle.
Be aware that there is only one channel to perform all the requested DMAs. */
/* Be sure to change transfer direction before calling
HAL_SD_ReadBlocks_DMA or HAL_SD_WriteBlocks_DMA. */
__HAL_LINKDMA(sdHandle,hdmarx,hdma_sdio);
__HAL_LINKDMA(sdHandle,hdmatx,hdma_sdio);
/* USER CODE BEGIN SDIO_MspInit 1 */
/* USER CODE END SDIO_MspInit 1 */
}
}
void HAL_SD_MspDeInit(SD_HandleTypeDef* sdHandle)
{
if(sdHandle->Instance==SDIO)
{
/* USER CODE BEGIN SDIO_MspDeInit 0 */
/* USER CODE END SDIO_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_SDIO_CLK_DISABLE();
/**SDIO GPIO Configuration
PC8 ------> SDIO_D0
PC9 ------> SDIO_D1
PC10 ------> SDIO_D2
PC11 ------> SDIO_D3
PC12 ------> SDIO_CK
PD2 ------> SDIO_CMD
*/
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11
|GPIO_PIN_12);
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_2);
/* SDIO DMA DeInit */
HAL_DMA_DeInit(sdHandle->hdmarx);
HAL_DMA_DeInit(sdHandle->hdmatx);
/* USER CODE BEGIN SDIO_MspDeInit 1 */
/* USER CODE END SDIO_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

View File

@@ -0,0 +1,81 @@
/* 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*/
/* USER CODE BEGIN MspInit 1 */
/* USER CODE END MspInit 1 */
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

View File

@@ -0,0 +1,129 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file stm32f1xx_hal_timebase_TIM.c
* @brief HAL time base based on the hardware TIM.
******************************************************************************
* @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 "stm32f1xx_hal.h"
#include "stm32f1xx_hal_tim.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim8;
/* Private function prototypes -----------------------------------------------*/
void TIM8_IRQHandler(void);
/* Private functions ---------------------------------------------------------*/
/**
* @brief This function configures the TIM8 as a time base source.
* The time source is configured to have 1ms time base with a dedicated
* Tick interrupt priority.
* @note This function is called automatically at the beginning of program after
* reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
* @param TickPriority: Tick interrupt priority.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
RCC_ClkInitTypeDef clkconfig;
uint32_t uwTimclock = 0U;
uint32_t uwPrescalerValue = 0U;
uint32_t pFLatency;
HAL_StatusTypeDef status = HAL_OK;
/* Enable TIM8 clock */
__HAL_RCC_TIM8_CLK_ENABLE();
/* Get clock configuration */
HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
/* Compute TIM8 clock */
uwTimclock = HAL_RCC_GetPCLK2Freq();
/* Compute the prescaler value to have TIM8 counter clock equal to 1MHz */
uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
/* Initialize TIM8 */
htim8.Instance = TIM8;
/* Initialize TIMx peripheral as follow:
+ Period = [(TIM8CLK/1000) - 1]. to have a (1/1000) s time base.
+ Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
+ ClockDivision = 0
+ Counter direction = Up
*/
htim8.Init.Period = (1000000U / 1000U) - 1U;
htim8.Init.Prescaler = uwPrescalerValue;
htim8.Init.ClockDivision = 0;
htim8.Init.CounterMode = TIM_COUNTERMODE_UP;
htim8.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
status = HAL_TIM_Base_Init(&htim8);
if (status == HAL_OK)
{
/* Start the TIM time Base generation in interrupt mode */
status = HAL_TIM_Base_Start_IT(&htim8);
if (status == HAL_OK)
{
/* Enable the TIM8 global Interrupt */
HAL_NVIC_EnableIRQ(TIM8_UP_TIM13_IRQn);
/* Configure the SysTick IRQ priority */
if (TickPriority < (1UL << __NVIC_PRIO_BITS))
{
/* Configure the TIM IRQ priority */
HAL_NVIC_SetPriority(TIM8_UP_TIM13_IRQn, TickPriority, 0U);
uwTickPrio = TickPriority;
}
else
{
status = HAL_ERROR;
}
}
}
/* Return function status */
return status;
}
/**
* @brief Suspend Tick increment.
* @note Disable the tick increment by disabling TIM8 update interrupt.
* @param None
* @retval None
*/
void HAL_SuspendTick(void)
{
/* Disable TIM8 update Interrupt */
__HAL_TIM_DISABLE_IT(&htim8, TIM_IT_UPDATE);
}
/**
* @brief Resume Tick increment.
* @note Enable the tick increment by Enabling TIM8 update interrupt.
* @param None
* @retval None
*/
void HAL_ResumeTick(void)
{
/* Enable TIM8 Update interrupt */
__HAL_TIM_ENABLE_IT(&htim8, TIM_IT_UPDATE);
}

336
Core/Src/stm32f1xx_it.c Normal file
View File

@@ -0,0 +1,336 @@
/* 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 */
#include "custom_flash.h"
#include "requester.h"
/* 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 CAN_HandleTypeDef hcan;
extern DMA_HandleTypeDef hdma_sdio;
extern TIM_HandleTypeDef htim7;
extern DMA_HandleTypeDef hdma_usart3_rx;
extern UART_HandleTypeDef huart3;
extern TIM_HandleTypeDef htim8;
/* USER CODE BEGIN EV */
extern uint8_t Data2Write[PAGE_SIZE*2];
extern uint8_t ReceiveDataUART[4];
extern struct flag FLAGS;
extern int cnt_tim_reset;
/* 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 */
/* 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 */
/* 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 channel3 global interrupt.
*/
void DMA1_Channel3_IRQHandler(void)
{
/* USER CODE BEGIN DMA1_Channel3_IRQn 0 */
/* USER CODE END DMA1_Channel3_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_usart3_rx);
/* USER CODE BEGIN DMA1_Channel3_IRQn 1 */
/* USER CODE END DMA1_Channel3_IRQn 1 */
}
/**
* @brief This function handles USB high priority or CAN TX interrupts.
*/
void USB_HP_CAN1_TX_IRQHandler(void)
{
/* USER CODE BEGIN USB_HP_CAN1_TX_IRQn 0 */
/* USER CODE END USB_HP_CAN1_TX_IRQn 0 */
HAL_CAN_IRQHandler(&hcan);
/* USER CODE BEGIN USB_HP_CAN1_TX_IRQn 1 */
/* USER CODE END USB_HP_CAN1_TX_IRQn 1 */
}
/**
* @brief This function handles USB low priority or CAN RX0 interrupts.
*/
void USB_LP_CAN1_RX0_IRQHandler(void)
{
/* USER CODE BEGIN USB_LP_CAN1_RX0_IRQn 0 */
/* USER CODE END USB_LP_CAN1_RX0_IRQn 0 */
HAL_CAN_IRQHandler(&hcan);
/* USER CODE BEGIN USB_LP_CAN1_RX0_IRQn 1 */
/* USER CODE END USB_LP_CAN1_RX0_IRQn 1 */
}
/**
* @brief This function handles CAN SCE interrupt.
*/
void CAN1_SCE_IRQHandler(void)
{
/* USER CODE BEGIN CAN1_SCE_IRQn 0 */
/* USER CODE END CAN1_SCE_IRQn 0 */
HAL_CAN_IRQHandler(&hcan);
/* USER CODE BEGIN CAN1_SCE_IRQn 1 */
/* USER CODE END CAN1_SCE_IRQn 1 */
}
/**
* @brief This function handles USART3 global interrupt.
*/
void USART3_IRQHandler(void)
{
/* USER CODE BEGIN USART3_IRQn 0 */
// HAL_Delay(5);
/* USER CODE END USART3_IRQn 0 */
HAL_UART_IRQHandler(&huart3);
/* USER CODE BEGIN USART3_IRQn 1 */
/* USER CODE END USART3_IRQn 1 */
}
/**
* @brief This function handles TIM8 update interrupt and TIM13 global interrupt.
*/
void TIM8_UP_TIM13_IRQHandler(void)
{
/* USER CODE BEGIN TIM8_UP_TIM13_IRQn 0 */
/* USER CODE END TIM8_UP_TIM13_IRQn 0 */
HAL_TIM_IRQHandler(&htim8);
/* USER CODE BEGIN TIM8_UP_TIM13_IRQn 1 */
/* USER CODE END TIM8_UP_TIM13_IRQn 1 */
}
/**
* @brief This function handles TIM7 global interrupt.
*/
void TIM7_IRQHandler(void)
{
/* USER CODE BEGIN TIM7_IRQn 0 */
REQUESTER_Pulse_TIM_Handler();
/* USER CODE END TIM7_IRQn 0 */
HAL_TIM_IRQHandler(&htim7);
/* USER CODE BEGIN TIM7_IRQn 1 */
/* USER CODE END TIM7_IRQn 1 */
}
/**
* @brief This function handles DMA2 channel4 and channel5 global interrupts.
*/
void DMA2_Channel4_5_IRQHandler(void)
{
/* USER CODE BEGIN DMA2_Channel4_5_IRQn 0 */
/* USER CODE END DMA2_Channel4_5_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_sdio);
/* USER CODE BEGIN DMA2_Channel4_5_IRQn 1 */
/* USER CODE END DMA2_Channel4_5_IRQn 1 */
}
/* USER CODE BEGIN 1 */
void EXTI9_5_IRQHandler(void)
{
/* USER CODE BEGIN DMA2_Channel4_5_IRQn 0 */
/* USER CODE END DMA2_Channel4_5_IRQn 0 */
//HAL_GPIO_EXTI_IRQHandler(SD_DETECT_PIN);
/* USER CODE BEGIN DMA2_Channel4_5_IRQn 1 */
//BSP_SD_DetectIT();
/* USER CODE END DMA2_Channel4_5_IRQn 1 */
}
/* USER CODE END 1 */

408
Core/Src/system_stm32f1xx.c Normal file
View File

@@ -0,0 +1,408 @@
/**
******************************************************************************
* @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
*
* <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/** @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 0x0000a000U /*!< 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 = 16000000;
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 */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

104
Core/Src/tim.c Normal file
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) 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 "tim.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
TIM_HandleTypeDef htim7;
/* TIM7 init function */
void MX_TIM7_Init(void)
{
/* USER CODE BEGIN TIM7_Init 0 */
/* USER CODE END TIM7_Init 0 */
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM7_Init 1 */
/* USER CODE END TIM7_Init 1 */
htim7.Instance = TIM7;
htim7.Init.Prescaler = TIM_REQUESTER_Prescaler-1;
htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
htim7.Init.Period = 1000;
htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim7) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM7_Init 2 */
/* USER CODE END TIM7_Init 2 */
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM7)
{
/* USER CODE BEGIN TIM7_MspInit 0 */
/* USER CODE END TIM7_MspInit 0 */
/* TIM7 clock enable */
__HAL_RCC_TIM7_CLK_ENABLE();
/* TIM7 interrupt Init */
HAL_NVIC_SetPriority(TIM7_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM7_IRQn);
/* USER CODE BEGIN TIM7_MspInit 1 */
/* USER CODE END TIM7_MspInit 1 */
}
}
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM7)
{
/* USER CODE BEGIN TIM7_MspDeInit 0 */
/* USER CODE END TIM7_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM7_CLK_DISABLE();
/* TIM7 interrupt Deinit */
HAL_NVIC_DisableIRQ(TIM7_IRQn);
/* USER CODE BEGIN TIM7_MspDeInit 1 */
/* USER CODE END TIM7_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

258
Core/Src/usart.c Normal file
View File

@@ -0,0 +1,258 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usart.c
* @brief This file provides code for the configuration
* of the USART instances.
******************************************************************************
* @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 "usart.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
UART_HandleTypeDef huart3;
DMA_HandleTypeDef hdma_usart3_rx;
/* USART3 init function */
void MX_USART3_UART_Init(void)
{
/* USER CODE BEGIN USART3_Init 0 */
/* USER CODE END USART3_Init 0 */
/* USER CODE BEGIN USART3_Init 1 */
//256000*4
/* USER CODE END USART3_Init 1 */
huart3.Instance = USART3;
huart3.Init.BaudRate = 9600*2;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart3) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART3_Init 2 */
/* USER CODE END USART3_Init 2 */
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(uartHandle->Instance==USART3)
{
/* USER CODE BEGIN USART3_MspInit 0 */
/* USER CODE END USART3_MspInit 0 */
/* USART3 clock enable */
__HAL_RCC_USART3_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**USART3 GPIO Configuration
PB10 ------> USART3_TX
PB11 ------> USART3_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USART3 DMA Init */
/* USART3_RX Init */
hdma_usart3_rx.Instance = DMA1_Channel3;
hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_usart3_rx.Init.Mode = DMA_CIRCULAR;
hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW;
if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(uartHandle,hdmarx,hdma_usart3_rx);
/* USART3 interrupt Init */
HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART3_IRQn);
/* USER CODE BEGIN USART3_MspInit 1 */
/* USER CODE END USART3_MspInit 1 */
}
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
if(uartHandle->Instance==USART3)
{
/* USER CODE BEGIN USART3_MspDeInit 0 */
/* USER CODE END USART3_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_USART3_CLK_DISABLE();
/**USART3 GPIO Configuration
PB10 ------> USART3_TX
PB11 ------> USART3_RX
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11);
/* USART3 DMA DeInit */
HAL_DMA_DeInit(uartHandle->hdmarx);
/* USART3 interrupt Deinit */
HAL_NVIC_DisableIRQ(USART3_IRQn);
/* USER CODE BEGIN USART3_MspDeInit 1 */
/* USER CODE END USART3_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
// CUSTOM UART INIT
//void User_UART_Init(UART_HandleTypeDef* huart, DMA_HandleTypeDef *DMAhuart, struct UARTSettings *uuart)
//{ // function takes uart handler, dma handler and setting structure for init
//
// // get setting for uart from UARTSettings structure
// huart->Instance = uuart->UARTx;
// huart->Init.BaudRate = uuart->UART_Speed*2;
//
// // everything else is default (for now, maybe this settings would be added in UARTSettings structure later)
// huart->Init.WordLength = UART_WORDLENGTH_8B;
// huart->Init.StopBits = UART_STOPBITS_1;
// huart->Init.Parity = UART_PARITY_NONE;
// huart->Init.Mode = UART_MODE_TX_RX;
// huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
// huart->Init.OverSampling = UART_OVERSAMPLING_16;
// if (HAL_UART_Init(huart) != HAL_OK)
// {
// Error_Handler();
// }
//
// // init gpio from UARTSettings structure
// UART_GPIO_Init(uuart->GPIOx, uuart->GPIO_PIN_RX, uuart->GPIO_PIN_TX);
// // init dma from UARTSettings structure if need
// if (uuart->DMAChannel != 0)
// UART_DMA_Init(huart, DMAhuart, uuart->DMAChannel);
//}
//void UART_GPIO_Init(GPIO_TypeDef *GPIOx, uint16_t GPIO_PIN_RX, uint16_t GPIO_PIN_TX)
//{ // function takes port and pins (for rx and tx)
// GPIO_InitTypeDef GPIO_InitStruct = {0};
//
// // choose port for enable clock
// if (GPIOx==GPIOA)
// __HAL_RCC_GPIOA_CLK_ENABLE();
// else if (GPIOx==GPIOB)
// __HAL_RCC_GPIOB_CLK_ENABLE();
// else if (GPIOx==GPIOC)
// __HAL_RCC_GPIOC_CLK_ENABLE();
// else if (GPIOx==GPIOD)
// __HAL_RCC_GPIOD_CLK_ENABLE();
//
//// USART3 GPIO Configuration
////GPIO_PIN_TX ------> USART_TX
// GPIO_InitStruct.Pin = GPIO_PIN_TX;
// GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
// GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
// HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
//// GPIO_PIN_RX ------> USART_RX
// GPIO_InitStruct.Pin = GPIO_PIN_RX;
// GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
// GPIO_InitStruct.Pull = GPIO_NOPULL;
// HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
//}
//void UART_DMA_Init(UART_HandleTypeDef *huart, DMA_HandleTypeDef *hdma_rx, DMA_Channel_TypeDef *DMAhuart)
//{ // function takes uart and dma handlers and dmachannel for uart
// // for now only dma rx is supported, tx maybe later if needed
//
// /* USART3 DMA Init */
// /* USART3_RX Init */
// hdma_rx->Instance = DMAhuart;
// hdma_rx->Init.Direction = DMA_PERIPH_TO_MEMORY;
// hdma_rx->Init.PeriphInc = DMA_PINC_DISABLE;
// hdma_rx->Init.MemInc = DMA_MINC_ENABLE;
// hdma_rx->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
// hdma_rx->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
// hdma_rx->Init.Mode = DMA_CIRCULAR;
// hdma_rx->Init.Priority = DMA_PRIORITY_LOW;
// if (HAL_DMA_Init(hdma_rx) != HAL_OK)
// {
// Error_Handler();
// }
// __USER_LINKDMA(huart,hdmarx,hdma_rx);
// // __USER_LINKDMA is need because __HAL_LINKDMA is written for global defined hdma_rx
// // so you get error because hal uses . insted of ->
//}
//void HAL_UART_MspInit(UART_HandleTypeDef* huart) // redefine hal function
//{ // left only rcc and interrupt init for USART_1,2,3 (maybe UART_4,5 need to be added)
// // GPIO and DMA init was move to their own functions
// if(huart->Instance==USART3)
// {
// /* USART3 clock enable */
// __HAL_RCC_USART3_CLK_ENABLE();
//
// /* USART3 interrupt Init */
// HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
// HAL_NVIC_EnableIRQ(USART3_IRQn);
// }
// else if(huart->Instance==USART2)
// {
// /* USART3 clock enable */
// __HAL_RCC_USART2_CLK_ENABLE();
//
// /* USART3 interrupt Init */
// HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
// HAL_NVIC_EnableIRQ(USART2_IRQn);
// }
// else if(huart->Instance==USART1)
// {
// /* USART3 clock enable */
// __HAL_RCC_USART1_CLK_ENABLE();
//
// /* USART3 interrupt Init */
// HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
// HAL_NVIC_EnableIRQ(USART1_IRQn);
// }
//}
/* USER CODE END 1 */