3.24_433_RX版本:封装RF433模块,完成开机进入TX/RX模式并在开发板验证成功

This commit is contained in:
2026-03-24 16:59:20 +08:00
commit e439dd465e
1311 changed files with 692196 additions and 0 deletions

139
Core/Inc/e32_demo.h Normal file
View File

@ -0,0 +1,139 @@
#ifndef _E32_DEMO_H_
#define _E32_DEMO_H_
#include "e32_hal.h"
typedef enum
{
OFF = 0x00,
ON = 0x01,
}on_off_t;
typedef enum
{
RADIO_RATE_2400 = 0x02,
RADIO_RATE_4800 = 0x03,
RADIO_RATE_9600 = 0x04,
RADIO_RATE_19200 = 0x05,
RADIO_RATE_38400 = 0x06,
RADIO_RATE_62500 = 0x07,
}radio_rate_t;
typedef enum
{
UART_8N1 = 0x00,
UART_8O1 = 0x01,
UART_8E1 = 0x02,
}uart_parity_t;
typedef enum
{
UART_RATE_1200 = 0x00,
UART_RATE_2400 = 0x01,
UART_RATE_4800 = 0x02,
UART_RATE_9600 = 0x03,
UART_RATE_19200 = 0x04 ,
UART_RATE_38400 = 0x05,
UART_RATE_57600 = 0x06,
UART_RATE_115200= 0x07,
}uart_rate_t;
typedef enum
{
WOR_PERIOD_250MS = 0x00,
WOR_PERIOD_500MS = 0x01,
WOR_PERIOD_750MS = 0x02,
WOR_PERIOD_1000MS = 0x03,
WOR_PERIOD_1250MS = 0x04,
WOR_PERIOD_1500MS = 0x05,
WOR_PERIOD_1750MS = 0x06,
WOR_PERIOD_2000MS = 0x07,
}wor_period_t;
typedef enum
{
TX_POWER_DBM_30 = 0x00,
TX_POWER_DBM_27 = 0x01,
TX_POWER_DBM_24 = 0x02,
TX_POWER_DBM_21 = 0x03,
}transmit_power_t;
typedef struct
{
/* 00H<30>ǹ̶<C7B9><CCB6><EFBFBD>HEAD<41><44><EFBFBD>޷<EFBFBD><DEB7><EFBFBD><EFBFBD><EFBFBD> */
/* ======== <20>û<EFBFBD><C3BB><EFBFBD><EFBFBD>üĴ<C3BC><C4B4><EFBFBD> 01H ======== */
struct
{
uint8_t address_h; /* ģ<><C4A3><EFBFBD><EFBFBD>ַ (<28>û<EFBFBD><C3BB><EFBFBD><EFBFBD>üĴ<C3BC><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ: 01H<31><48>02H) <20><>ͬ<EFBFBD><CDAC>ַ<EFBFBD><D6B7>ģ<EFBFBD><C4A3><EFBFBD>޷<EFBFBD>ֱ<EFBFBD>ӻ<EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>(<28><EFBFBD><E3B2A5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>); 65535Ϊ<35><EFBFBD><E3B2A5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱥ<EFBFBD><C8BA><EFBFBD><EFBFBD>Ϣ */
}register_1;
/* ======== <20>û<EFBFBD><C3BB><EFBFBD><EFBFBD>üĴ<C3BC><C4B4><EFBFBD> 02H ======== */
struct
{
uint8_t address_l;
}register_2;
/* ======== <20>û<EFBFBD><C3BB><EFBFBD><EFBFBD>üĴ<C3BC><C4B4><EFBFBD> 03H ======== */
union {
uint8_t value;
struct
{
radio_rate_t radio_rate : 3; /* <20><><EFBFBD>߿<EFBFBD><DFBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (<28>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ: 03H Bit2-0) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD>ߣ<EFBFBD><DFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD><ECA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD><C8BB>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊͨ<CEAA>ž<EFBFBD><C5BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
uart_rate_t uart_baud_rate : 3; /* <20><><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD> (<28>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ: 03H Bit7-5) <20><><EFBFBD><EFBFBD>ģʽʽ2)ǿ<>ƹ̶<C6B9><CCB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ9600<30><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E0B4AB>ģʽʱΪ<CAB1>û<EFBFBD><C3BB><EFBFBD><EFBFBD>ô<EFBFBD><C3B4>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD> */
uart_parity_t uart_parity : 2; /* <20><><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (<28>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ: 03H Bit4-3) */
}field;
}register_3;
/* ======== <20>û<EFBFBD><C3BB><EFBFBD><EFBFBD>üĴ<C3BC><C4B4><EFBFBD> 04H ======== */
struct
{
uint8_t channel; /* <20>ŵ<EFBFBD><C5B5><EFBFBD><EFBFBD><EFBFBD> (<28>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ: 04H) <20><><EFBFBD><EFBFBD><EFBFBD>ز<EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>йأ<D0B9><D8A3>ز<EFBFBD>Ƶ<EFBFBD><C6B5> = <20>ŵ<EFBFBD>0<EFBFBD><30>ʼƵ<CABC><C6B5> + (1MHz x <20>ŵ<EFBFBD><C5B5><EFBFBD><EFBFBD><EFBFBD>) */
}register_4;
/* ======== <20>û<EFBFBD><C3BB><EFBFBD><EFBFBD>üĴ<C3BC><C4B4><EFBFBD> 05H ======== */
union {
uint8_t value;
struct
{
transmit_power_t tx_power : 2; /* <20><><EFBFBD><EFBFBD><E4B9A6> (<28>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ: 05H Bit1-0) <20><>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD>Ĺ<EFBFBD><C4B9>ʷֵ<CAB7><D6B5><EFBFBD>һ<EFBFBD>£<EFBFBD><C2A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>Ҫ<EFBFBD>ο<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD>ֲ<EFBFBD> */
on_off_t packet_fec : 1; /* ǰ<><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FEC (<28>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ: 05H Bit2 ) ˫<><CBAB>˫<EFBFBD>˱<EFBFBD><CBB1><EFBFBD>һ<EFBFBD>£<EFBFBD><C2A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޷<EFBFBD>ͨ<EFBFBD>š<EFBFBD><C5A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󣬾<EFBFBD><F3A3ACBE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿ(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>Ҳ<EFBFBD><D2B2><EFBFBD>ӳ<EFBFBD> */
wor_period_t wake_on_radio_period : 3; /* <20><><EFBFBD>л<EFBFBD><D0BB><EFBFBD>WOR<4F><52><EFBFBD><EFBFBD> (<28>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ: 05H Bit2-0) <20>շ<EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><E8B6A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׳<EFBFBD><D7B3><EFBFBD><EFBFBD>޷<EFBFBD><DEB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
on_off_t reserve : 1; /* <20><><EFBFBD><EFBFBD> IO<49><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ*/
on_off_t specify_target : 1; /* ָ<><D6B8>Ŀ<EFBFBD><EFBFBD>䣬Ҳ<E4A3AC>ж<EFBFBD><D0B6><EFBFBD>ģʽ (<28>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ: 05H Bit7 ) <20><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ŵ<EFBFBD> */
}field;
}register_5;
}e32_register_t;
typedef enum
{
REQUEST_CMD_CONFIG = 0x00,
REQUEST_CMD_NAME ,
REQUEST_CMD_VERSION ,
}request_cmd_t;
typedef struct
{
uint8_t address_h;
uint8_t address_l;
uint8_t channel;
uint8_t data[237];//<2F><><EFBFBD><EFBFBD><E3B4AB><><D6B8>Ŀ<EFBFBD><C4BF>)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݲ<EFBFBD><DDB2>ó<EFBFBD><C3B3><EFBFBD>237<33>ֽڣ<D6BD><DAA3><EFBFBD><EFBFBD>򳬳<EFBFBD><F2B3ACB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻᶪʧ
}e32_specify_target_buffer_t;
typedef struct
{
uint8_t command;
uint8_t config[20];
}e32_hex_cmd_buffer_t;
typedef union
{
uint8_t opt_buffer[1024];
e32_hex_cmd_buffer_t hex_cmd;
e32_specify_target_buffer_t target;
}e32_opt_buffer_t;
#endif

21
Core/Inc/e32_hal.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef _E32_HAL_H_
#define _E32_HAL_H_
#define E32_USE_GPIO_AUX 1 // 0:<3A><>ʹ<EFBFBD>û򲻼<C3BB><F2B2BBBC><EFBFBD>AUX״̬<D7B4><CCAC><EFBFBD><EFBFBD> 1:<3A><><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>AUX<55><58><EFBFBD><EFBFBD>
#include <stdint.h>
typedef enum
{
WORK_MODE_TRANSPARENT = 0x00,
WORK_MODE_WAKE_ON_RADIO_MASTER = 0x01,
WORK_MODE_WAKE_ON_RADIO_SLAVE = 0x02,
WORK_MODE_CONFIG_AND_SLEEP = 0x03,
}work_mode_t;
void e32_hal_uart_tx( uint8_t *buffer , uint16_t length );
void e32_hal_aux_wait(void);
void e32_hal_reset(void);
void e32_hal_work_mode( work_mode_t mode);
#endif

31
Core/Inc/fifo.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef _FIFO_H_
#define _FIFO_H_
#include <stdint.h>
typedef enum
{
FIFO_OK = 0x00,
FIFO_ERROR_NULL,
FIFO_ERROR_LENGTH,
FIFO_ERROR_FULL,
FIFO_ERROR_EMPTY,
}fifo_error_t;
typedef struct
{
uint32_t in;
uint32_t out;
uint32_t size;
uint8_t* buffer;
} fifo_t;
fifo_error_t fifo_create( fifo_t *fifo , uint8_t *buffer, uint32_t size );
fifo_error_t fifo_clear( fifo_t *fifo );
fifo_error_t fifo_write( fifo_t *fifo, uint8_t *buffer, uint32_t length );
fifo_error_t fifo_read( fifo_t *fifo, uint8_t *buffer, uint32_t length );
fifo_error_t fifo_get_length( fifo_t *fifo , uint32_t *length);
fifo_error_t fifo_get_remain_length( fifo_t *fifo , uint32_t *length);
#endif

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__ */

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

@ -0,0 +1,52 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file i2c.h
* @brief This file contains all the function prototypes for
* the i2c.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 __I2C_H__
#define __I2C_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
extern I2C_HandleTypeDef hi2c2;
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_I2C2_Init(void);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /* __I2C_H__ */

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

@ -0,0 +1,129 @@
/* 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 <stdbool.h>
#include <stdio.h>
#include "application.h"
/* USER CODE END Includes */
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
extern uint8_t my_usb_rx_data[ ];
extern uint16_t my_usb_rx_num ;
typedef enum
{
KEY_NAME_UP = 0,
KEY_NAME_DOWN ,
KEY_NAME_ENTER,
}key_name_t;
/* 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);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */
/* Private defines -----------------------------------------------------------*/
#define RESET_Pin GPIO_PIN_3
#define RESET_GPIO_Port GPIOA
#define M0_Pin GPIO_PIN_7
#define M0_GPIO_Port GPIOA
#define M1_Pin GPIO_PIN_0
#define M1_GPIO_Port GPIOB
#define AUX_Pin GPIO_PIN_1
#define AUX_GPIO_Port GPIOB
#define LED_TX_Pin GPIO_PIN_15
#define LED_TX_GPIO_Port GPIOA
#define BUZZER_PWM_Pin GPIO_PIN_3
#define BUZZER_PWM_GPIO_Port GPIOB
#define KEY_UP_Pin GPIO_PIN_4
#define KEY_UP_GPIO_Port GPIOB
#define USB_CTRL_Pin GPIO_PIN_5
#define USB_CTRL_GPIO_Port GPIOB
#define LED_RX_Pin GPIO_PIN_6
#define LED_RX_GPIO_Port GPIOB
#define KEY_ENTER_Pin GPIO_PIN_7
#define KEY_ENTER_GPIO_Port GPIOB
#define KEY_DOWN_Pin GPIO_PIN_9
#define KEY_DOWN_GPIO_Port GPIOB
/* USER CODE BEGIN Private defines */
void usb_printf(const char *format, ...);
void gpio_usb_ctrl_on(void);
void gpio_usb_ctrl_off(void);
void gpio_led_tx_on(void);
void gpio_led_tx_off(void);
void gpio_led_rx_on(void);
void gpio_led_rx_off(void);
void buzzer_on(void);
void buzzer_off(void);
void buzzer_button_press(void);
bool key_check_press( key_name_t name );
void key_set_continue( key_name_t name , bool enable );
void key_timer_1ms_interrupt_callback(void);
void systick_interrupt_1ms_callback(void);
void systick_set_user_timeout( uint32_t time_ms );
uint32_t systick_get_user_timeout(void);
void uart1_reconfig( uint32_t rate );
void uart1_rx_timeout_1ms_callback(void);
void uart1_wait_response_blocked( uint8_t * buffer, uint16_t *length );
bool uart1_check_rx_done( uint8_t *buffer , uint32_t *length );
void e32_demo_read_device_name( char *buffer , uint8_t *length );
void e32_demo_read_fireware_version( char *buffer , uint8_t *length);
void e32_demo_menu_config( menu_config_t *config );
void e32_demo_transmit( uint8_t *buffer , uint16_t length );
/* USER CODE END Private defines */
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H */

119
Core/Inc/rf433_rx_app.h Normal file
View File

@ -0,0 +1,119 @@
/**
******************************************************************************
* @file rf433_rx_app.h
* @brief RF433 RX应用层接口
******************************************************************************
*/
#ifndef __RF433_RX_APP_H__
#define __RF433_RX_APP_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "rf433.h"
#include <stdint.h>
#include <stdbool.h>
/* ============================================================================
* 数据类型定义
* ============================================================================ */
/**
* @brief RX状态机状态
*/
typedef enum {
RX_STATE_INIT = 0, /**< 初始化状态 */
RX_STATE_RECEIVING, /**< 接收中状态 */
RX_STATE_IDLE, /**< 空闲状态 */
} rx_state_t;
/**
* @brief RX统计结构体
*/
typedef struct {
uint32_t total_received; /**< 总接收次数 */
uint32_t total_expected; /**< 总期望次数 */
uint32_t lost_packets; /**< 丢包数 */
uint8_t lost_percent; /**< 丢包率(%) */
} rf433_rx_stats_t;
/**
* @brief RX应用结构体
*/
typedef struct {
rx_state_t state; /**< 当前状态 */
rf433_rx_stats_t stats; /**< 统计信息 */
uint32_t tx_current_number; /**< TX当前序号 */
uint32_t tx_total_number; /**< TX总次数 */
uint32_t tx_number_record; /**< TX序号记录 */
rf433_register_t config; /**< RF433配置 */
bool is_running; /**< 是否正在运行 */
} rf433_rx_app_t;
/* ============================================================================
* API函数声明
* ============================================================================ */
/**
* @brief 初始化RX应用层
* @param config RF433配置参数
* @retval RF433_OK 成功
* @retval RF433_ERROR 参数错误
*/
rf433_error_t rf433_rx_app_init(const rf433_register_t *config);
/**
* @brief 启动RX自动接收
* @retval RF433_OK 成功
*/
rf433_error_t rf433_rx_app_start(void);
/**
* @brief 停止RX自动接收
* @retval RF433_OK 成功
*/
rf433_error_t rf433_rx_app_stop(void);
/**
* @brief RX任务函数在主循环中调用
*/
void rf433_rx_app_task(void);
/**
* @brief 获取RX状态
* @return 当前RX状态
*/
rx_state_t rf433_rx_app_get_state(void);
/**
* @brief 获取RX统计信息
* @param stats 统计信息结构体指针
* @retval RF433_OK 成功
* @retval RF433_ERROR 参数错误
*/
rf433_error_t rf433_rx_app_get_stats(rf433_rx_stats_t *stats);
/**
* @brief 重置RX统计信息
*/
void rf433_rx_app_reset_stats(void);
/**
* @brief 解析数据包
* @param data 数据包指针
* @param length 数据包长度
* @param tx_total 输出TX总次数
* @param tx_current 输出TX当前序号
* @retval RF433_OK 解析成功
* @retval RF433_ERROR 数据包格式错误
*/
rf433_error_t rf433_rx_app_parse_packet(const uint8_t *data, uint16_t length,
uint32_t *tx_total, uint32_t *tx_current);
#ifdef __cplusplus
}
#endif
#endif /* __RF433_RX_APP_H__ */

115
Core/Inc/rf433_tx_app.h Normal file
View File

@ -0,0 +1,115 @@
/**
******************************************************************************
* @file rf433_tx_app.h
* @brief RF433 TX应用层接口
******************************************************************************
*/
#ifndef __RF433_TX_APP_H__
#define __RF433_TX_APP_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "rf433.h"
#include <stdint.h>
#include <stdbool.h>
/* ============================================================================
* 数据类型定义
* ============================================================================ */
/**
* @brief TX状态机状态
*/
typedef enum {
TX_STATE_INIT = 0, /**< 初始化状态 */
TX_STATE_SENDING, /**< 发送中状态 */
TX_STATE_WAITING, /**< 等待状态 */
TX_STATE_IDLE, /**< 空闲状态 */
} tx_state_t;
/**
* @brief TX应用结构体
*/
typedef struct {
tx_state_t state; /**< 当前状态 */
uint32_t send_count; /**< 已发送次数 */
uint32_t total_count; /**< 总发送次数 */
uint32_t send_interval_ms; /**< 发送间隔(ms) */
uint32_t last_send_time; /**< 上次发送时间 */
rf433_register_t config; /**< RF433配置 */
bool is_running; /**< 是否正在运行 */
} rf433_tx_app_t;
/* ============================================================================
* API函数声明
* ============================================================================ */
/**
* @brief 初始化TX应用层
* @param config RF433配置参数
* @retval RF433_OK 成功
* @retval RF433_ERROR 参数错误
*/
rf433_error_t rf433_tx_app_init(const rf433_register_t *config);
/**
* @brief 启动TX自动发送
* @param count 总发送次数
* @param interval_ms 发送间隔(ms)
* @retval RF433_OK 成功
* @retval RF433_ERROR 参数错误
*/
rf433_error_t rf433_tx_app_start(uint32_t count, uint32_t interval_ms);
/**
* @brief 停止TX自动发送
* @retval RF433_OK 成功
*/
rf433_error_t rf433_tx_app_stop(void);
/**
* @brief 手动发送数据
* @param data 数据指针
* @param length 数据长度
* @retval RF433_OK 成功
* @retval RF433_ERROR 参数错误
* @retval RF433_ERROR_BUSY 模块忙
*/
rf433_error_t rf433_tx_app_manual_send(uint8_t *data, uint16_t length);
/**
* @brief TX任务函数在主循环中调用
*/
void rf433_tx_app_task(void);
/**
* @brief 获取TX状态
* @return 当前TX状态
*/
tx_state_t rf433_tx_app_get_state(void);
/**
* @brief 获取已发送次数
* @return 已发送次数
*/
uint32_t rf433_tx_app_get_send_count(void);
/**
* @brief 获取总发送次数
* @return 总发送次数
*/
uint32_t rf433_tx_app_get_total_count(void);
/**
* @brief 重置TX计数器
*/
void rf433_tx_app_reset_count(void);
#ifdef __cplusplus
}
#endif
#endif /* __RF433_TX_APP_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 */

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

@ -0,0 +1,68 @@
/* 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 USB_LP_CAN1_RX0_IRQHandler(void);
void USART1_IRQHandler(void);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */
#ifdef __cplusplus
}
#endif
#endif /* __STM32F1xx_IT_H */

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

@ -0,0 +1,54 @@
/* 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 htim2;
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_TIM2_Init(void);
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /* __TIM_H__ */

17
Core/Inc/u8g2_hal.h Normal file
View File

@ -0,0 +1,17 @@
/*
* @Author: JFeng 2834294740@qq.com
* @Date: 2023-06-30 12:08:24
* @LastEditors: JFeng 2834294740@qq.com
* @LastEditTime: 2023-07-14 20:12:55
* @FilePath: \MY_GUI\HARDWARE\stm32_u8g2.h
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
#ifndef __STM32_U8G2_H
#define __STM32_U8G2_H
#include "main.h"
#include "u8g2.h"
void u8g2Init(u8g2_t *u8g2);
#endif

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

@ -0,0 +1,58 @@
/* 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 */
typedef enum {
FUNC_NONE = 0,
FUNC_FEATURE1,
FUNC_FEATURE2
} uart_feature;
extern uart_feature current_feature;
/* USER CODE END Includes */
extern UART_HandleTypeDef huart1;
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_USART1_UART_Init(void);
/* USER CODE BEGIN Prototypes */
void usb_receive_to_tx_send( void );
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /* __USART_H__ */

292
Core/Src/e32_demo.c Normal file
View File

@ -0,0 +1,292 @@
#include "e32_demo.h"
#include "main.h"
#include "application.h"
#include <string.h>
/**
* ģ<><C4A3><EFBFBD><EFBFBD><EFBFBD>ݲ<EFBFBD><DDB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
static e32_opt_buffer_t e32_buffer;
/**
* ģ<><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡָ<C8A1><D6B8>
*/
static const uint8_t request_config[3]={0xC1,0xC1,0xC1};
/**
* ATָ<54><D6B8> <20><>ȡ<EFBFBD><EFBFBD><E8B1B8>
*/
static const char request_name[]="AT+DEVTYPE=?";
/**
* ATָ<54><D6B8> <20><>ȡ<EFBFBD>̼<EFBFBD><CCBC>
*/
static const char request_version[]="AT+FWCODE=?";
/**
* E32-433T30S Ĭ<><C4AC><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD>
*/
static const e32_register_t register_default =
{
.register_1 = {
.address_h = 0x00,
},
.register_2 = {
.address_l = 0x00,
},
.register_3.field = {
.radio_rate = RADIO_RATE_2400,
.uart_baud_rate = UART_RATE_9600,
.uart_parity = UART_8N1,
},
.register_4 = {
.channel = 0x17,
},
.register_5.field = {
.tx_power = TX_POWER_DBM_30,
.packet_fec = ON,
.wake_on_radio_period = WOR_PERIOD_250MS,
.reserve = OFF,
.specify_target = OFF,
},
};
/**
* @brief <20><>ģ<EFBFBD><C4A3>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD>ָ<EFBFBD><D6B8>
*
* @param config <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
*/
static void e32_send_config_command( const e32_register_t *config )
{
/* <20><>1<EFBFBD>ֽڣ<D6BD><DAA3><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
C0<43><30><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
e32_buffer.hex_cmd.command = 0xC0;
/* <20><>2~6<>ֽڿ<D6BD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD> */
memcpy( e32_buffer.hex_cmd.config , (uint8_t*)config , sizeof(e32_register_t));
/* <20><><EFBFBD><EFBFBD>д<EFBFBD><D0B4> */
e32_hal_uart_tx( (uint8_t*)&e32_buffer, sizeof(e32_register_t) + 1);
}
/**
* @brief <20><>ģ<EFBFBD><C4A3>д<EFBFBD><D0B4><EFBFBD><EFBFBD>ѯָ<D1AF><D6B8>
*
* @param cmd <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>
*/
static void e32_send_request_command( request_cmd_t cmd )
{
switch( cmd )
{
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD>ò<EFBFBD><C3B2><EFBFBD> */
case REQUEST_CMD_CONFIG:
e32_hal_uart_tx( (uint8_t*)request_config, sizeof(request_config));
break;
/* <20><>ȡ<EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD> */
case REQUEST_CMD_NAME:
e32_hal_uart_tx( (uint8_t*)request_name, strlen(request_name));
break;
/* <20><>ȡ<EFBFBD>̼<EFBFBD><CCBC>汾 */
case REQUEST_CMD_VERSION:
e32_hal_uart_tx( (uint8_t*)request_version, strlen(request_version));
break;
}
}
/**
* @brief (<28><>ѯָ<D1AF><D6B8><EFBFBD><EFBFBD><><C4A3>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>ݼ<EFBFBD><DDBC><EFBFBD>
*
* @param cmd <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>
* @param buffer ָ<><D6B8>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD>
* @param length Ӧ<><D3A6><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
* @return bool <20><>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>true; <20><><EFBFBD>򷵻<EFBFBD>false<73><65>
*/
static bool e32_response_command_check( request_cmd_t cmd , uint8_t *buffer , uint8_t length )
{
bool ret = false;
switch( cmd )
{
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD>ò<EFBFBD><C3B2><EFBFBD> */
case REQUEST_CMD_CONFIG:
/* <20><><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>6<EFBFBD>ֽ<EFBFBD>*/
if( length == 6 )
{
/* <20><><EFBFBD>ݼ<EFBFBD><DDBC><EFBFBD> ֡ͷ<D6A1><CDB7><EFBFBD><EFBFBD>Ϊ0xC1 */
if( buffer[0] == 0xC1)
{
ret = true;
}
}
break;
/* <20><>ȡ<EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD> */
case REQUEST_CMD_NAME:
/* <20><><EFBFBD>ݼ<EFBFBD><DDBC><EFBFBD> */
if( strncmp( "DEVTYPE=", (char*)buffer, 8) == 0 )
{
ret = true;
}
break;
/* <20><>ȡ<EFBFBD>̼<EFBFBD><CCBC>汾 */
case REQUEST_CMD_VERSION:
/* <20><><EFBFBD>ݼ<EFBFBD><DDBC><EFBFBD> */
if( strncmp( "FWCODE=", (char*)buffer, 7) == 0 )
{
ret = true;
}
break;
}
return ret;
}
/**
* @brief <20><>ģ<EFBFBD><EFBFBD><E9B4AE>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @note <20><>ע<EFBFBD><D7A2>ģ<EFBFBD><EFBFBD><E9B9A4>ģʽ
* @param buffer ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD>
* @param length д<><EFBFBD><EBB3A4>
*/
void e32_demo_transmit( uint8_t *buffer , uint16_t length )
{
/* <20><><EFBFBD><EFBFBD>д<EFBFBD><D0B4> */
e32_hal_uart_tx( buffer, length);
}
/**
* @brief (<28><>ѡ) ʾ<><CABE><EFBFBD><EFBFBD><EFBFBD>ζ<EFBFBD>ȡ<EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
*
* @note ģ<><C4A3>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
* @param buffer ģ<><C4A3>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param length ģ<><C4A3>Ӧ<EFBFBD>𳤶<EFBFBD>
*/
void e32_demo_read_device_name( char *buffer , uint8_t *length )
{
uint16_t opt_length;
/* <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD>֧<EFBFBD>ִ<EFBFBD><D6B4><EFBFBD>9600<30><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
uart1_reconfig(9600);
/* <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ */
e32_hal_work_mode( WORK_MODE_CONFIG_AND_SLEEP );
/* <20><><EFBFBD>Ͳ<EFBFBD>ѯָ<D1AF><D6B8> <20><>ȡ<EFBFBD><EFBFBD><E8B1B8> */
e32_send_request_command( REQUEST_CMD_NAME );
/* <20>ȴ<EFBFBD>ģ<EFBFBD><C4A3>Ӧ<EFBFBD><D3A6> */
uart1_wait_response_blocked( (uint8_t*)buffer , &opt_length );
/* Ӧ<><D3A6><EFBFBD><EFBFBD><EFBFBD>ݼ<EFBFBD><DDBC><EFBFBD> */
if( e32_response_command_check( REQUEST_CMD_NAME, (uint8_t*)buffer , opt_length ) != true )
{
/* Ӧ<><D3A6><EFBFBD><EFBFBD><EFBFBD>ݲ<EFBFBD><DDB2><EFBFBD>ȷ */
while(1);
}
*length = opt_length;
}
/**
* @brief (<28><>ѡ) ʾ<><CABE><EFBFBD><EFBFBD><EFBFBD>ζ<EFBFBD>ȡ<EFBFBD><EFBFBD>̼<EFBFBD><CCBC>
*
* @note ģ<><C4A3>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
* @param buffer ģ<><C4A3>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param length ģ<><C4A3>Ӧ<EFBFBD>𳤶<EFBFBD>
*/
void e32_demo_read_fireware_version( char *buffer , uint8_t *length )
{
uint16_t opt_length;
/* <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD>֧<EFBFBD>ִ<EFBFBD><D6B4><EFBFBD>9600<30><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
uart1_reconfig(9600);
/* <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ */
e32_hal_work_mode( WORK_MODE_CONFIG_AND_SLEEP );
/* <20><><EFBFBD>Ͳ<EFBFBD>ѯָ<D1AF><D6B8> <20><>ȡ<EFBFBD><EFBFBD><E8B1B8> */
e32_send_request_command( REQUEST_CMD_VERSION );
/* <20>ȴ<EFBFBD>ģ<EFBFBD><C4A3>Ӧ<EFBFBD><D3A6> */
uart1_wait_response_blocked( (uint8_t*)buffer , &opt_length );
/* Ӧ<><D3A6><EFBFBD><EFBFBD><EFBFBD>ݼ<EFBFBD><DDBC><EFBFBD> */
if( e32_response_command_check( REQUEST_CMD_VERSION, (uint8_t*)buffer , opt_length ) != true )
{
/* Ӧ<><D3A6><EFBFBD><EFBFBD><EFBFBD>ݲ<EFBFBD><DDB2><EFBFBD>ȷ */
while(1);
}
*length = opt_length;
}
/**
* @brief ʹ<><CAB9><EFBFBD><EFBFBD>ʾ<EFBFBD>˵<EFBFBD><CBB5>û<EFBFBD><C3BB><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>
*
* @param config <20>˵<EFBFBD><CBB5><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>Ϣ
*/
void e32_demo_menu_config( menu_config_t *config )
{
uint16_t opt_length;
e32_register_t register_write;
transmit_power_t power_select;
/* <20><><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϼĴ<CFBC><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
memcpy( (uint8_t*)&register_write , (uint8_t*)&register_default , sizeof(e32_register_t) );
/* <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA> */
switch( config->tx_power )
{
case 27:
power_select = TX_POWER_DBM_27;
break;
case 24:
power_select = TX_POWER_DBM_24;
break;
case 21:
power_select = TX_POWER_DBM_21;
break;
default:
power_select = TX_POWER_DBM_30;
break;
}
/* <20>޸<EFBFBD><DEB8>û<EFBFBD><C3BB><EFBFBD><EFBFBD>õIJ<C3B5><C4B2><EFBFBD> */
register_write.register_5.field.tx_power = power_select; //<2F><><EFBFBD><EFBFBD>
register_write.register_3.field.radio_rate = (radio_rate_t)config->rate_mode; //<2F><><EFBFBD><EFBFBD>
register_write.register_4.channel = config->channel; //<2F>ŵ<EFBFBD>
/* <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD>֧<EFBFBD>ִ<EFBFBD><D6B4><EFBFBD>9600<30><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
uart1_reconfig(9600);
/* <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ */
e32_hal_work_mode( WORK_MODE_CONFIG_AND_SLEEP );
/* <20><>ģ<EFBFBD><EFBFBD><E9B4AE>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
e32_send_config_command( &register_write );
/* <20>ȴ<EFBFBD>ģ<EFBFBD><C4A3>Ӧ<EFBFBD><D3A6> */
uart1_wait_response_blocked( e32_buffer.opt_buffer , &opt_length );
// /* Ӧ<><D3A6><EFBFBD><EFBFBD><EFBFBD>ݼ<EFBFBD><DDBC><EFBFBD> ָ<><D6B8>ͷ<EFBFBD><CDB7>ΪC1 */
// if( e32_buffer.opt_buffer[0] != 0xC1 )
// {
// /* ģ<><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ش<EFBFBD>FF FF FF <20><>ʾָ<CABE><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
// while(1);
// }
///@todo <20><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD>
/* <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD>õ<EFBFBD>͸<EFBFBD><CDB8>ģʽ<C4A3><CABD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD> */
uart1_reconfig(9600);
/* <20>ص<EFBFBD>͸<EFBFBD><CDB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ */
e32_hal_work_mode( WORK_MODE_TRANSPARENT );
}

123
Core/Src/e32_hal.c Normal file
View File

@ -0,0 +1,123 @@
#include "e32_hal.h"
/**
* <20>뵥Ƭ<EBB5A5><C6AC>ƽ̨<C6BD>й<EFBFBD>
*/
#include "main.h"
#include "gpio.h"
#include "usart.h"
/**
* @brief <20><><EFBFBD>ڷ<EFBFBD><DAB7>ͽӿ<CDBD>
*
* @param buffer <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param length <20><><EFBFBD>ͳ<EFBFBD><CDB3><EFBFBD>
*/
void e32_hal_uart_tx( uint8_t *buffer , uint16_t length )
{
HAL_UART_Transmit( &huart1, buffer, length, 0xFFFF);
}
/**
* @brief ģ<><C4A3>æ״̬<D7B4>ȴ<EFBFBD>
*/
void e32_hal_aux_wait(void)
{
if( HAL_GPIO_ReadPin( AUX_GPIO_Port , AUX_Pin ) == GPIO_PIN_RESET )
{
/* <20>ȵ<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD> AUX<55><58><EFBFBD>ŵ<EFBFBD>Ϊæ<CEAA><C3A6><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD> */
while( HAL_GPIO_ReadPin( AUX_GPIO_Port , AUX_Pin ) == GPIO_PIN_RESET )
{
///@todo <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>߼<EFBFBD>
}
/* <20><><EFBFBD>Ǽ<EFBFBD><C7BC><EFBFBD><EFBFBD>ԣ<EFBFBD>AUX<55><58><EFBFBD>к<EFBFBD><D0BA><EFBFBD><EFBFBD>Ե<EFBFBD>1~2ms */
HAL_Delay(2);
}
}
/**
* @brief ģ<>鸴λ
*/
void e32_hal_reset(void)
{
HAL_GPIO_WritePin( RESET_GPIO_Port, RESET_Pin, GPIO_PIN_RESET );
HAL_Delay(1);
HAL_GPIO_WritePin( RESET_GPIO_Port, RESET_Pin, GPIO_PIN_SET );
#if E32_USE_GPIO_AUX
/* ע<><D7A2> Ӳ<><D3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AUX<55><58><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ȴ<EFBFBD>һ<EFBFBD><D2BB>ʱ<EFBFBD><CAB1><EFBFBD>ٿ<EFBFBD>ʼAUX<55><58><EFBFBD><EFBFBD> */
HAL_Delay(10);
e32_hal_aux_wait();
#else
// /* E32-V2 (V8.1) <20><EFBFBD><E6B1BE><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ϳ<EFBFBD>
// <20><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>AT+FWCODE=?<3F><>ȡ<EFBFBD><EFBFBD><E6B1BE>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7> 7393-x-xx */
// e32_delay_ms(1200);
/* E32-V2 (V8.2) <20>
<20><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>AT+FWCODE=?<3F><>ȡ<EFBFBD><EFBFBD><E6B1BE>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7> 7459-x-xx */
e32_delay_ms(30);
#endif
}
/**
* @brief ģ<><C4A3>ģʽ<C4A3>л<EFBFBD>
*
* @param mode <20><><EFBFBD><EFBFBD>ģʽ
*/
void e32_hal_work_mode( work_mode_t mode)
{
#if E32_USE_GPIO_AUX
e32_hal_aux_wait();
#endif
switch(mode)
{
/* ģʽ0<CABD><30> һ<><D2BB>ģʽ (M0=0 M1=0) */
case WORK_MODE_TRANSPARENT:
HAL_GPIO_WritePin( M0_GPIO_Port, M0_Pin, GPIO_PIN_RESET );
HAL_GPIO_WritePin( M1_GPIO_Port, M1_Pin, GPIO_PIN_RESET );
break;
/* ģʽ1<CABD><31> <20><><EFBFBD><EFBFBD>ģʽ (M0=1 M1=0) */
case WORK_MODE_WAKE_ON_RADIO_MASTER:
HAL_GPIO_WritePin( M0_GPIO_Port, M0_Pin, GPIO_PIN_SET );
HAL_GPIO_WritePin( M1_GPIO_Port, M1_Pin, GPIO_PIN_RESET );
break;
/* ģʽ2<CABD><32> ʡ<><CAA1>ģʽ (M0=0 M1=1) */
case WORK_MODE_WAKE_ON_RADIO_SLAVE:
HAL_GPIO_WritePin( M0_GPIO_Port, M0_Pin, GPIO_PIN_RESET );
HAL_GPIO_WritePin( M1_GPIO_Port, M1_Pin, GPIO_PIN_SET );
break;
/* ģʽ3<CABD><33> <20><><EFBFBD><EFBFBD>ģʽ (M0=1 M1=1) */
case WORK_MODE_CONFIG_AND_SLEEP:
HAL_GPIO_WritePin( M0_GPIO_Port, M0_Pin, GPIO_PIN_SET );
HAL_GPIO_WritePin( M1_GPIO_Port, M1_Pin, GPIO_PIN_SET );
break;
default:
while(1); //ģʽ<C4A3><CABD><EFBFBD><EFBFBD>
}
#if E32_USE_GPIO_AUX
/* <20><><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD> ģ<><C4A3><EFBFBD><EFBFBD>ʱAUX<55><58><EFBFBD>Ų<EFBFBD><C5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD>Ҫ<EFBFBD>Ե<EFBFBD><D4B5>ٽ<EFBFBD><D9BD>м<EFBFBD><D0BC><EFBFBD> */
HAL_Delay(5);
e32_hal_aux_wait();
#else
/* <20><><EFBFBD><EFBFBD><EFB1A3>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ȷ<EFBFBD><C8B7>ģʽ<C4A3>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD> */
e32_delay_ms(50);
#endif
}

168
Core/Src/fifo.c Normal file
View File

@ -0,0 +1,168 @@
#include "fifo.h"
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
fifo_error_t fifo_create( fifo_t *fifo , uint8_t *buffer, uint32_t size )
{
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
if( (fifo == 0) || (buffer == 0) || (size == 0) )
{
return FIFO_ERROR_NULL;
}
/* <20><><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD> (<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2^n) */
if( size & (size - 1) )
{
return FIFO_ERROR_LENGTH;
}
/* Ĭ<><C4AC> */
fifo->size = size;
fifo->buffer = buffer;
fifo->in = 0;
fifo->out = 0;
return FIFO_OK;
}
fifo_error_t fifo_clear( fifo_t *fifo )
{
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
if( fifo == 0 )
{
return FIFO_ERROR_NULL;
}
fifo->in = 0;
fifo->out = 0;
return FIFO_OK;
}
fifo_error_t fifo_write( fifo_t *fifo, uint8_t *buffer, uint32_t length )
{
uint32_t i, j;
uint32_t end_length, org_length;
uint8_t *fifo_buffer;
/* <20><>¼ԭʼ<D4AD>û<EFBFBD>д<EFBFBD><EFBFBD><EBB3A4> */
org_length = length;
/* <20><><EFBFBD><EFBFBD>FIFOʣ<4F><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
length = MIN( length, fifo->size - fifo->in + fifo->out );
/* <20><><EFBFBD><EFBFBD>FIFO<46><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĩβ<C4A9>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>㹻λ<E3B9BB><CEBB> */
end_length = MIN( length, fifo->size - ( fifo->in & ( fifo->size - 1 ) ) );
/* <20>ҵ<EFBFBD>FIFO<46><4F>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ַ */
fifo_buffer = fifo->buffer + ( fifo->in & ( fifo->size - 1 ) );
/* <20><><EFBFBD><EFBFBD>FIFOĩβ<C4A9><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD>ݸ<EFBFBD><DDB8>Ƶ<EFBFBD>FIFO<46><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĩβ */
for( i = 0; i < end_length; i++ )
{
*( fifo_buffer++ ) = *( buffer++ );
}
/* <20><><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
j = length - end_length;
/* <20><><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><EFBFBD><E0A3AC><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>FIFO<46><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>*/
if ( j > 0 )
{
fifo_buffer = fifo->buffer;
for( i = 0; i < j; i++ )
{
*( fifo_buffer++ ) = *( buffer++ );
}
}
/* <20><>¼<EFBFBD>ѽ<EFBFBD><D1BD><EFBFBD>FIFO<46>ij<EFBFBD><C4B3><EFBFBD> */
fifo->in += length;
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FIFO<46><4F><EFBFBD>ݵij<DDB5><C4B3><EFBFBD>С<EFBFBD><D0A1><EFBFBD>û<EFBFBD>ʵ<EFBFBD><CAB5>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD>ݵij<DDB5><C4B3>ȣ<EFBFBD><C8A3>Ǿ<EFBFBD><C7BE><EFBFBD>FIFO<46><4F><EFBFBD><EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
if( length < org_length )
{
return FIFO_ERROR_FULL;
}
return FIFO_OK;
}
fifo_error_t fifo_read( fifo_t *fifo, uint8_t *buffer, uint32_t length )
{
uint32_t i, j;
uint32_t end_length, org_length;
uint8_t *fifo_buffer;
/* <20><>¼ԭʼ<D4AD>û<EFBFBD><C3BB><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD> */
org_length = length;
/* <20><><EFBFBD><EFBFBD>FIFO<46><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD> */
length = MIN( length, fifo->in - fifo->out );
/* <20><><EFBFBD><EFBFBD>FIFOĩβ<C4A9><CEB2><EFBFBD>ӳ<EFBFBD><D3B3><EFBFBD> */
end_length = MIN( length, fifo->size - ( fifo->out & ( fifo->size - 1 ) ) );
/* <20>ҵ<EFBFBD>FIFOĩβ<C4A9><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ַ */
fifo_buffer = fifo->buffer + ( fifo->out & ( fifo->size - 1 ) );
/* <20><><EFBFBD><EFBFBD>FIFOĩβ<C4A9><CEB2><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD>FIFO<46>ڸ<EFBFBD><DAB8>Ƹ<EFBFBD><C6B8>û<EFBFBD><C3BB><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD> */
for( i = 0; i < end_length; i++ )
{
*( buffer++ ) = *( fifo_buffer++ );
}
/* <20><><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
j = length - end_length;
/* <20><><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><EFBFBD><E0A3AC><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>FIFO<46><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>*/
if ( j > 0 )
{
fifo_buffer = fifo->buffer;
for( i = 0; i < j; i++ )
{
*( buffer++ ) = *( fifo_buffer++ ) ;
}
}
/* <20><>¼<EFBFBD>ѳ<EFBFBD><D1B3>ӵij<D3B5><C4B3><EFBFBD> */
fifo->out += length;
/* <20><><EFBFBD><EFBFBD>FIFO<46><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>С<EFBFBD><D0A1><EFBFBD>û<EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>ݵij<DDB5><C4B3>ȣ<EFBFBD><C8A3>Ǿ<EFBFBD><C7BE><EFBFBD>FIFO<46><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
if( length < org_length )
{
return FIFO_ERROR_EMPTY;
}
return FIFO_OK;
}
fifo_error_t fifo_get_length( fifo_t *fifo , uint32_t *length)
{
if( (fifo==0) || (length==0) )
{
return FIFO_ERROR_NULL;
}
*length = fifo->in - fifo->out;
return FIFO_OK;
}
fifo_error_t fifo_get_remain_length( fifo_t *fifo , uint32_t *length)
{
if( (fifo==0) || (length==0) )
{
return FIFO_ERROR_NULL;
}
*length = fifo->size - ( fifo->in - fifo->out );
return FIFO_OK;
}

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

@ -0,0 +1,122 @@
/* 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 as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, RESET_Pin|M0_Pin|LED_TX_Pin, GPIO_PIN_SET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, M1_Pin|LED_RX_Pin, GPIO_PIN_SET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(USB_CTRL_GPIO_Port, USB_CTRL_Pin, GPIO_PIN_RESET);
/*Configure GPIO pins : PAPin PAPin PAPin */
GPIO_InitStruct.Pin = RESET_Pin|M0_Pin|LED_TX_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : PBPin PBPin PBPin */
GPIO_InitStruct.Pin = M1_Pin|USB_CTRL_Pin|LED_RX_Pin;
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 : PtPin */
GPIO_InitStruct.Pin = AUX_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(AUX_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : PBPin PBPin PBPin */
GPIO_InitStruct.Pin = KEY_UP_Pin|KEY_ENTER_Pin|KEY_DOWN_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
/* USER CODE BEGIN 2 */
void gpio_usb_ctrl_on(void)
{
HAL_GPIO_WritePin( USB_CTRL_GPIO_Port , USB_CTRL_Pin , GPIO_PIN_SET );
}
void gpio_usb_ctrl_off(void)
{
HAL_GPIO_WritePin( USB_CTRL_GPIO_Port , USB_CTRL_Pin , GPIO_PIN_RESET );
}
void gpio_led_tx_on(void)
{
HAL_GPIO_WritePin( LED_TX_GPIO_Port, LED_TX_Pin, GPIO_PIN_RESET);
}
void gpio_led_tx_off(void)
{
HAL_GPIO_WritePin( LED_TX_GPIO_Port, LED_TX_Pin, GPIO_PIN_SET);
}
void gpio_led_rx_on(void)
{
HAL_GPIO_WritePin( LED_RX_GPIO_Port, LED_RX_Pin, GPIO_PIN_RESET);
}
void gpio_led_rx_off(void)
{
HAL_GPIO_WritePin( LED_RX_GPIO_Port, LED_RX_Pin, GPIO_PIN_SET);
}
/* USER CODE END 2 */

114
Core/Src/i2c.c Normal file
View File

@ -0,0 +1,114 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file i2c.c
* @brief This file provides code for the configuration
* of the I2C 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 "i2c.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
I2C_HandleTypeDef hi2c2;
/* I2C2 init function */
void MX_I2C2_Init(void)
{
/* USER CODE BEGIN I2C2_Init 0 */
/* USER CODE END I2C2_Init 0 */
/* USER CODE BEGIN I2C2_Init 1 */
/* USER CODE END I2C2_Init 1 */
hi2c2.Instance = I2C2;
hi2c2.Init.ClockSpeed = 400000;
hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c2.Init.OwnAddress1 = 0;
hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c2.Init.OwnAddress2 = 0;
hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C2_Init 2 */
/* USER CODE END I2C2_Init 2 */
}
void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(i2cHandle->Instance==I2C2)
{
/* USER CODE BEGIN I2C2_MspInit 0 */
/* USER CODE END I2C2_MspInit 0 */
__HAL_RCC_GPIOB_CLK_ENABLE();
/**I2C2 GPIO Configuration
PB10 ------> I2C2_SCL
PB11 ------> I2C2_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* I2C2 clock enable */
__HAL_RCC_I2C2_CLK_ENABLE();
/* USER CODE BEGIN I2C2_MspInit 1 */
/* USER CODE END I2C2_MspInit 1 */
}
}
void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
{
if(i2cHandle->Instance==I2C2)
{
/* USER CODE BEGIN I2C2_MspDeInit 0 */
/* USER CODE END I2C2_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_I2C2_CLK_DISABLE();
/**I2C2 GPIO Configuration
PB10 ------> I2C2_SCL
PB11 ------> I2C2_SDA
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10);
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_11);
/* USER CODE BEGIN I2C2_MspDeInit 1 */
/* USER CODE END I2C2_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

122
Core/Src/key.c Normal file
View File

@ -0,0 +1,122 @@
#include "main.h"
#define KEY_SHORT_PRESS_TIME_MS 100
typedef struct
{
uint32_t count ;
bool continue_enable;
bool is_press;
bool is_continue;
}key_config_t;
static key_config_t key_group[3] =
{
[ KEY_NAME_UP ] = {
.count = 0,
.is_press = 0,
.continue_enable = false,
.is_continue = false,
},
[ KEY_NAME_DOWN ] = {
.count = 0,
.is_press = 0,
.continue_enable = false,
.is_continue = false,
},
[ KEY_NAME_ENTER ] = {
.count = 0,
.is_press = 0,
.continue_enable = false,
.is_continue = false,
}
};
bool key_check_press( key_name_t name )
{
bool ret = false;
/* <20>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѯ״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ״̬ */
ret = key_group[name].is_press;
/* <20><><EFBFBD><EFBFBD>flash<73><68><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD>δ<EFBFBD><CEB4><EFBFBD> */
key_group[name].is_press = false;
return ret;
}
void key_set_continue( key_name_t name , bool enable )
{
key_group[name].continue_enable = enable;
}
static inline void key_press( key_name_t name )
{
key_group[name].count ++;
/* <20><><EFBFBD><EFBFBD><EFBFBD>̰<EFBFBD><CCB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ʱ<EFBFBD><CAB1> */
if( key_group[name].count > KEY_SHORT_PRESS_TIME_MS )
{
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD> */
if( key_group[name].continue_enable == false )
{
/* <20><><EFBFBD>ڵ<EFBFBD>һ<EFBFBD>ζ̰<CEB6><CCB0>´<EFBFBD><C2B4><EFBFBD><EFBFBD>ж<EFBFBD> */
if( key_group[name].is_continue == false )
{
/* <20><>ֹ<EFBFBD>´δ<C2B4><CEB4><EFBFBD> ֱ<><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD> <20><><EFBFBD><EFBFBD>false*/
key_group[name].is_continue = true;
/* <20><>һ<EFBFBD>θ<EFBFBD><CEB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD>ʶ */
key_group[name].is_press = true;
}
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD> */
else
{
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD>ʶ */
key_group[name].is_press = true;
/* <20><><EFBFBD>¼<EFBFBD>ʱ */
key_group[name].count = 0;
}
}
}
static inline void key_release( key_name_t name )
{
key_group[ name ].count = 0;
key_group[ name ].is_continue = false;
}
void key_timer_1ms_interrupt_callback(void)
{
if( HAL_GPIO_ReadPin( KEY_UP_GPIO_Port, KEY_UP_Pin ) == RESET )
{
key_press( KEY_NAME_UP );
}
else
{
key_release( KEY_NAME_UP );
}
if( HAL_GPIO_ReadPin( KEY_DOWN_GPIO_Port, KEY_DOWN_Pin ) == RESET )
{
key_press( KEY_NAME_DOWN );
}
else
{
key_release( KEY_NAME_DOWN );
}
if( HAL_GPIO_ReadPin( KEY_ENTER_GPIO_Port, KEY_ENTER_Pin ) == RESET )
{
key_press( KEY_NAME_ENTER );
}
else
{
key_release( KEY_NAME_ENTER );
}
}

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

@ -0,0 +1,229 @@
/* 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, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "usart.h"
#include "usb_device.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "rf433.h"
#include "rf433_config.h"
#if (RF433_MODE == RF433_MODE_TX) || (RF433_MODE == RF433_MODE_BOTH)
#include "rf433_tx_app.h"
#endif
#if (RF433_MODE == RF433_MODE_RX) || (RF433_MODE == RF433_MODE_BOTH)
#include "rf433_rx_app.h"
#endif
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#include "usbd_cdc_if.h"
extern uint8_t usb_rx_data;
/* 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 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USB_DEVICE_Init();
MX_TIM2_Init();
/* USER CODE BEGIN 2 */
/* 初始化RF433模块 - 使用默认配置 */
rf433_init(NULL);
/* 启动UART接收 */
HAL_UART_Receive_IT(&huart1, &usb_rx_data, 1);
/* 根据配置模式初始化TX/RX应用层 */
#if (RF433_MODE == RF433_MODE_TX) || (RF433_MODE == RF433_MODE_BOTH)
/* TX模式初始化 */
rf433_tx_app_init(NULL);
rf433_tx_app_start(RF433_DEFAULT_TX_COUNT, RF433_DEFAULT_TX_INTERVAL);
#endif
#if (RF433_MODE == RF433_MODE_RX) || (RF433_MODE == RF433_MODE_BOTH)
/* RX模式初始化 */
rf433_rx_app_init(NULL);
rf433_rx_app_start();
#endif
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
#if (RF433_MODE == RF433_MODE_TX) || (RF433_MODE == RF433_MODE_BOTH)
/* TX任务 */
rf433_tx_app_task();
#endif
#if (RF433_MODE == RF433_MODE_RX) || (RF433_MODE == RF433_MODE_BOTH)
/* RX任务 */
rf433_rx_app_task();
#endif
/* 短延时避免CPU占用过高 */
HAL_Delay(1);
}
/* 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_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: usb_printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

238
Core/Src/rf433_rx_app.c Normal file
View File

@ -0,0 +1,238 @@
/**
******************************************************************************
* @file rf433_rx_app.c
* @brief RF433 RX应用层实现
******************************************************************************
*/
#include "rf433_config.h"
#if (RF433_MODE == RF433_MODE_RX) || (RF433_MODE == RF433_MODE_BOTH)
#include "rf433_rx_app.h"
#include "rf433.h"
#include "rf433_hal.h"
#include "main.h"
#include <stdio.h>
#include <string.h>
/* ============================================================================
* 私有变量
* ============================================================================ */
static rf433_rx_app_t g_rx_app = {0};
/* ============================================================================
* 私有函数声明
* ============================================================================ */
/**
* @brief 控制LED_RX指示灯
* @param state 0: 关闭, 1: 打开
*/
static void rx_led_control(uint8_t state);
/**
* @brief 更新丢包统计
*/
static void rx_update_stats(void);
/* ============================================================================
* 私有函数实现
* ============================================================================ */
static void rx_led_control(uint8_t state)
{
// LED_RX控制 - 使用gpio.c中定义的函数
if (state) {
gpio_led_rx_on(); // 打开LED_RX
} else {
gpio_led_rx_off(); // 关闭LED_RX
}
}
static void rx_update_stats(void)
{
// 计算丢包数
if (g_rx_app.tx_number_record > 0) {
g_rx_app.stats.lost_packets = g_rx_app.tx_number_record - g_rx_app.stats.total_received;
// 计算丢包率
if (g_rx_app.tx_number_record > 0) {
g_rx_app.stats.lost_percent = (uint8_t)((g_rx_app.stats.lost_packets * 100) / g_rx_app.tx_number_record);
} else {
g_rx_app.stats.lost_percent = 0;
}
}
}
/* ============================================================================
* 公共API实现
* ============================================================================ */
rf433_error_t rf433_rx_app_init(const rf433_register_t *config)
{
if (config == NULL) {
return RF433_ERROR;
}
// 初始化RX应用结构体
memset(&g_rx_app, 0, sizeof(rf433_rx_app_t));
g_rx_app.state = RX_STATE_INIT;
g_rx_app.is_running = false;
// 保存配置
memcpy(&g_rx_app.config, config, sizeof(rf433_register_t));
// 配置RF433模块
rf433_error_t ret = rf433_set_config(&g_rx_app.config);
if (ret != RF433_OK) {
return ret;
}
// 设置为透明传输模式
ret = rf433_set_work_mode(RF433_WORK_MODE_TRANSPARENT);
if (ret != RF433_OK) {
return ret;
}
return RF433_OK;
}
rf433_error_t rf433_rx_app_start(void)
{
// 启动RF433接收
rf433_error_t ret = rf433_rx_start();
if (ret != RF433_OK) {
return ret;
}
g_rx_app.state = RX_STATE_RECEIVING;
g_rx_app.is_running = true;
return RF433_OK;
}
rf433_error_t rf433_rx_app_stop(void)
{
// 停止RF433接收
rf433_rx_stop();
g_rx_app.is_running = false;
g_rx_app.state = RX_STATE_IDLE;
rx_led_control(0);
return RF433_OK;
}
void rf433_rx_app_task(void)
{
if (!g_rx_app.is_running) {
return;
}
switch (g_rx_app.state) {
case RX_STATE_INIT:
// 初始化完成,进入接收状态
g_rx_app.state = RX_STATE_RECEIVING;
break;
case RX_STATE_RECEIVING:
// 尝试接收数据(使用与原始代码相同的接收机制)
{
uint8_t buffer[64];
uint32_t actual_len = 0;
// 使用uart1_check_rx_done检查接收完成与原始代码一致
if (uart1_check_rx_done(buffer, &actual_len)) {
// 解析数据包
uint32_t tx_total = 0;
uint32_t tx_current = 0;
rf433_error_t ret = rf433_rx_app_parse_packet(buffer, (uint16_t)actual_len, &tx_total, &tx_current);
if (ret == RF433_OK) {
// 更新统计信息
g_rx_app.stats.total_received++;
g_rx_app.tx_total_number = tx_total;
g_rx_app.tx_current_number = tx_current;
// 更新TX序号记录
if (tx_current > g_rx_app.tx_number_record) {
g_rx_app.tx_number_record = tx_current;
}
// 更新丢包统计
rx_update_stats();
// LED闪烁指示
rx_led_control(1);
HAL_Delay(50);
rx_led_control(0);
}
}
}
break;
case RX_STATE_IDLE:
// 空闲状态,不做任何操作
break;
default:
g_rx_app.state = RX_STATE_IDLE;
break;
}
}
rx_state_t rf433_rx_app_get_state(void)
{
return g_rx_app.state;
}
rf433_error_t rf433_rx_app_get_stats(rf433_rx_stats_t *stats)
{
if (stats == NULL) {
return RF433_ERROR;
}
memcpy(stats, &g_rx_app.stats, sizeof(rf433_rx_stats_t));
return RF433_OK;
}
void rf433_rx_app_reset_stats(void)
{
memset(&g_rx_app.stats, 0, sizeof(rf433_rx_stats_t));
g_rx_app.tx_current_number = 0;
g_rx_app.tx_total_number = 0;
g_rx_app.tx_number_record = 0;
}
rf433_error_t rf433_rx_app_parse_packet(const uint8_t *packet_buf, uint16_t length,
uint32_t *tx_total, uint32_t *tx_current)
{
if (packet_buf == NULL || length == 0 || tx_total == NULL || tx_current == NULL) {
return RF433_ERROR;
}
// 数据包格式: "TX.总次数.当前序号."
// 例如: "TX.010.001." 或 "TX.100.100."
// 参考原始代码application.c中的rx_analysis函数
// 检查最小长度原始代码要求至少10字节
if (length < 10) {
return RF433_ERROR;
}
// 检查前缀 "TX"
if (packet_buf[0] != 'T' || packet_buf[1] != 'X') {
return RF433_ERROR;
}
// 使用sscanf解析数据包与原始代码一致
// 从buffer+3开始解析跳过"TX."
sscanf((char*)(packet_buf + 3), "%d.%d.", tx_total, tx_current);
return RF433_OK;
}
#endif /* (RF433_MODE == RF433_MODE_RX) || (RF433_MODE == RF433_MODE_BOTH) */

239
Core/Src/rf433_tx_app.c Normal file
View File

@ -0,0 +1,239 @@
/**
******************************************************************************
* @file rf433_tx_app.c
* @brief RF433 TX应用层实现
******************************************************************************
*/
#include "rf433_config.h"
#if (RF433_MODE == RF433_MODE_TX) || (RF433_MODE == RF433_MODE_BOTH)
#include "rf433_tx_app.h"
#include "rf433.h"
#include "main.h"
#include <stdio.h>
#include <string.h>
/* ============================================================================
* 私有变量
* ============================================================================ */
static rf433_tx_app_t g_tx_app = {0};
/* ============================================================================
* 私有函数声明
* ============================================================================ */
/**
* @brief 构造数据包
* @param buffer 数据包缓冲区
* @param buffer_size 缓冲区大小
* @param total_count 总次数
* @param current_count 当前序号
* @return 实际数据包长度
*/
static uint16_t tx_build_packet(uint8_t *buffer, uint16_t buffer_size,
uint32_t total_count, uint32_t current_count);
/**
* @brief 控制LED_TX指示灯
* @param state 0: 关闭, 1: 打开
*/
static void tx_led_control(uint8_t state);
/* ============================================================================
* 私有函数实现
* ============================================================================ */
static uint16_t tx_build_packet(uint8_t *buffer, uint16_t buffer_size,
uint32_t total_count, uint32_t current_count)
{
if (buffer == NULL || buffer_size < 16) {
return 0;
}
// 数据包格式: "TX.总次数.当前序号."
// 例如: "TX.010.001."
int len = snprintf((char *)buffer, buffer_size, "TX.%03lu.%03lu.",
total_count, current_count);
return (len > 0) ? (uint16_t)len : 0;
}
static void tx_led_control(uint8_t state)
{
// LED_TX控制 - 使用gpio.c中定义的函数
if (state) {
gpio_led_tx_on(); // 打开LED_TX
} else {
gpio_led_tx_off(); // 关闭LED_TX
}
}
/* ============================================================================
* 公共API实现
* ============================================================================ */
rf433_error_t rf433_tx_app_init(const rf433_register_t *config)
{
if (config == NULL) {
return RF433_ERROR;
}
// 初始化TX应用结构体
memset(&g_tx_app, 0, sizeof(rf433_tx_app_t));
g_tx_app.state = TX_STATE_INIT;
g_tx_app.is_running = false;
// 保存配置
memcpy(&g_tx_app.config, config, sizeof(rf433_register_t));
// 配置RF433模块
rf433_error_t ret = rf433_set_config(&g_tx_app.config);
if (ret != RF433_OK) {
return ret;
}
// 设置为透明传输模式
ret = rf433_set_work_mode(RF433_WORK_MODE_TRANSPARENT);
if (ret != RF433_OK) {
return ret;
}
return RF433_OK;
}
rf433_error_t rf433_tx_app_start(uint32_t count, uint32_t interval_ms)
{
if (count == 0 || interval_ms == 0) {
return RF433_ERROR;
}
g_tx_app.total_count = count;
g_tx_app.send_count = 0;
g_tx_app.send_interval_ms = interval_ms;
g_tx_app.last_send_time = 0;
g_tx_app.state = TX_STATE_INIT;
g_tx_app.is_running = true;
return RF433_OK;
}
rf433_error_t rf433_tx_app_stop(void)
{
g_tx_app.is_running = false;
g_tx_app.state = TX_STATE_IDLE;
tx_led_control(0);
return RF433_OK;
}
rf433_error_t rf433_tx_app_manual_send(uint8_t *data, uint16_t length)
{
if (data == NULL || length == 0) {
return RF433_ERROR;
}
// 发送数据
rf433_error_t ret = rf433_transmit(data, length);
if (ret == RF433_OK) {
// LED闪烁指示
tx_led_control(1);
HAL_Delay(50);
tx_led_control(0);
}
return ret;
}
void rf433_tx_app_task(void)
{
if (!g_tx_app.is_running) {
return;
}
uint32_t current_time = HAL_GetTick();
switch (g_tx_app.state) {
case TX_STATE_INIT:
// 初始化完成,进入发送状态
g_tx_app.state = TX_STATE_SENDING;
break;
case TX_STATE_SENDING:
// 检查是否达到发送次数
if (g_tx_app.send_count >= g_tx_app.total_count) {
g_tx_app.state = TX_STATE_IDLE;
tx_led_control(0);
break;
}
// 检查发送间隔
if (current_time - g_tx_app.last_send_time >= g_tx_app.send_interval_ms) {
// 构造数据包
uint8_t packet[32];
uint16_t packet_len = tx_build_packet(packet, sizeof(packet),
g_tx_app.total_count,
g_tx_app.send_count + 1);
if (packet_len > 0) {
// 发送数据
rf433_error_t ret = rf433_transmit(packet, packet_len);
if (ret == RF433_OK) {
g_tx_app.send_count++;
g_tx_app.last_send_time = current_time;
// LED闪烁指示
tx_led_control(1);
HAL_Delay(50);
tx_led_control(0);
}
}
g_tx_app.state = TX_STATE_WAITING;
}
break;
case TX_STATE_WAITING:
// 等待一段时间后继续发送
if (current_time - g_tx_app.last_send_time >= g_tx_app.send_interval_ms) {
g_tx_app.state = TX_STATE_SENDING;
}
break;
case TX_STATE_IDLE:
// 空闲状态,不做任何操作
break;
default:
g_tx_app.state = TX_STATE_IDLE;
break;
}
}
tx_state_t rf433_tx_app_get_state(void)
{
return g_tx_app.state;
}
uint32_t rf433_tx_app_get_send_count(void)
{
return g_tx_app.send_count;
}
uint32_t rf433_tx_app_get_total_count(void)
{
return g_tx_app.total_count;
}
void rf433_tx_app_reset_count(void)
{
g_tx_app.send_count = 0;
g_tx_app.last_send_time = 0;
if (g_tx_app.is_running) {
g_tx_app.state = TX_STATE_SENDING;
}
}
#endif /* (RF433_MODE == RF433_MODE_TX) || (RF433_MODE == RF433_MODE_BOTH) */

View File

@ -0,0 +1,86 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file stm32f1xx_hal_msp.c
* @brief This file provides code for the MSP Initialization
* and de-Initialization codes.
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
/* USER CODE END TD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN Define */
/* USER CODE END Define */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN Macro */
/* USER CODE END Macro */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* External functions --------------------------------------------------------*/
/* USER CODE BEGIN ExternalFunctions */
/* USER CODE END ExternalFunctions */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* Initializes the Global MSP.
*/
void HAL_MspInit(void)
{
/* USER CODE BEGIN MspInit 0 */
/* USER CODE END MspInit 0 */
__HAL_RCC_AFIO_CLK_ENABLE();
__HAL_RCC_PWR_CLK_ENABLE();
/* System interrupt init*/
/** NOJTAG: JTAG-DP Disabled and SW-DP Enabled
*/
__HAL_AFIO_REMAP_SWJ_NOJTAG();
/* USER CODE BEGIN MspInit 1 */
/* USER CODE END MspInit 1 */
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

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

@ -0,0 +1,232 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file stm32f1xx_it.c
* @brief Interrupt Service Routines.
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f1xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
/* USER CODE END TD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/* External variables --------------------------------------------------------*/
extern PCD_HandleTypeDef hpcd_USB_FS;
extern UART_HandleTypeDef huart1;
/* USER CODE BEGIN EV */
/* USER CODE END EV */
/******************************************************************************/
/* Cortex-M3 Processor Interruption and Exception Handlers */
/******************************************************************************/
/**
* @brief This function handles Non maskable interrupt.
*/
void NMI_Handler(void)
{
/* USER CODE BEGIN NonMaskableInt_IRQn 0 */
/* USER CODE END NonMaskableInt_IRQn 0 */
/* 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 */
key_timer_1ms_interrupt_callback();
systick_interrupt_1ms_callback();
/* USER CODE END SysTick_IRQn 0 */
HAL_IncTick();
/* USER CODE BEGIN SysTick_IRQn 1 */
/* USER CODE END SysTick_IRQn 1 */
}
/******************************************************************************/
/* STM32F1xx Peripheral Interrupt Handlers */
/* Add here the Interrupt Handlers for the used peripherals. */
/* For the available peripheral interrupt handler names, */
/* please refer to the startup file (startup_stm32f1xx.s). */
/******************************************************************************/
/**
* @brief This function handles 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_PCD_IRQHandler(&hpcd_USB_FS);
/* USER CODE BEGIN USB_LP_CAN1_RX0_IRQn 1 */
/* USER CODE END USB_LP_CAN1_RX0_IRQn 1 */
}
/**
* @brief This function handles USART1 global interrupt.
*/
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
/* USER CODE END USART1_IRQn 1 */
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

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

@ -0,0 +1,406 @@
/**
******************************************************************************
* @file system_stm32f1xx.c
* @author MCD Application Team
* @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
*
* 1. This file provides two functions and one global variable to be called from
* user application:
* - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
* factors, AHB/APBx prescalers and Flash settings).
* This function is called at startup just after reset and
* before branch to main program. This call is made inside
* the "startup_stm32f1xx_xx.s" file.
*
* - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
* by the user application to setup the SysTick
* timer or configure other parameters.
*
* - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
* be called whenever the core clock is changed
* during program execution.
*
* 2. After each device reset the HSI (8 MHz) is used as system clock source.
* Then SystemInit() function is called, in "startup_stm32f1xx_xx.s" file, to
* configure the system clock before to branch to main program.
*
* 4. The default value of HSE crystal is set to 8 MHz (or 25 MHz, depending on
* the product used), refer to "HSE_VALUE".
* When HSE is used as system clock source, directly or through PLL, and you
* are using different crystal you have to adapt the HSE value to your own
* configuration.
*
******************************************************************************
* @attention
*
* Copyright (c) 2017-2021 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/** @addtogroup CMSIS
* @{
*/
/** @addtogroup stm32f1xx_system
* @{
*/
/** @addtogroup STM32F1xx_System_Private_Includes
* @{
*/
#include "stm32f1xx.h"
/**
* @}
*/
/** @addtogroup STM32F1xx_System_Private_TypesDefinitions
* @{
*/
/**
* @}
*/
/** @addtogroup STM32F1xx_System_Private_Defines
* @{
*/
#if !defined (HSE_VALUE)
#define HSE_VALUE 8000000U /*!< Default value of the External oscillator in Hz.
This value can be provided and adapted by the user application. */
#endif /* HSE_VALUE */
#if !defined (HSI_VALUE)
#define HSI_VALUE 8000000U /*!< Default value of the Internal oscillator in Hz.
This value can be provided and adapted by the user application. */
#endif /* HSI_VALUE */
/*!< Uncomment the following line if you need to use external SRAM */
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
/* #define DATA_IN_ExtSRAM */
#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
/* Note: Following vector table addresses must be defined in line with linker
configuration. */
/*!< Uncomment the following line if you need to relocate the vector table
anywhere in Flash or Sram, else the vector table is kept at the automatic
remap of boot address selected */
/* #define USER_VECT_TAB_ADDRESS */
#if defined(USER_VECT_TAB_ADDRESS)
/*!< Uncomment the following line if you need to relocate your vector Table
in Sram else user remap will be done in Flash. */
/* #define VECT_TAB_SRAM */
#if defined(VECT_TAB_SRAM)
#define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field.
This value must be a multiple of 0x200. */
#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
This value must be a multiple of 0x200. */
#else
#define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field.
This value must be a multiple of 0x200. */
#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
This value must be a multiple of 0x200. */
#endif /* VECT_TAB_SRAM */
#endif /* USER_VECT_TAB_ADDRESS */
/******************************************************************************/
/**
* @}
*/
/** @addtogroup STM32F1xx_System_Private_Macros
* @{
*/
/**
* @}
*/
/** @addtogroup STM32F1xx_System_Private_Variables
* @{
*/
/* This variable is updated in three ways:
1) by calling CMSIS function SystemCoreClockUpdate()
2) by calling HAL API function HAL_RCC_GetHCLKFreq()
3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
Note: If you use this function to configure the system clock; then there
is no need to call the 2 first functions listed above, since SystemCoreClock
variable is updated automatically.
*/
uint32_t SystemCoreClock = 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 */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

24
Core/Src/systick.c Normal file
View File

@ -0,0 +1,24 @@
#include "main.h"
static volatile uint32_t user_timerout_ms = 0;
void systick_set_user_timeout( uint32_t time_ms )
{
user_timerout_ms = time_ms;
}
uint32_t systick_get_user_timeout(void)
{
return user_timerout_ms;
}
void systick_interrupt_1ms_callback(void)
{
uart1_rx_timeout_1ms_callback();
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ж<EFBFBD> */
if( user_timerout_ms > 0 )
{
user_timerout_ms--;
}
}

150
Core/Src/tim.c Normal file
View File

@ -0,0 +1,150 @@
/* 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 htim2;
/* TIM2 init function */
void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
/* USER CODE END TIM2_Init 0 */
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 72-1;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 300-1;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 150-1;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_Init 2 */
HAL_TIM_MspPostInit(&htim2);
}
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* tim_pwmHandle)
{
if(tim_pwmHandle->Instance==TIM2)
{
/* USER CODE BEGIN TIM2_MspInit 0 */
/* USER CODE END TIM2_MspInit 0 */
/* TIM2 clock enable */
__HAL_RCC_TIM2_CLK_ENABLE();
/* USER CODE BEGIN TIM2_MspInit 1 */
/* USER CODE END TIM2_MspInit 1 */
}
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(timHandle->Instance==TIM2)
{
/* USER CODE BEGIN TIM2_MspPostInit 0 */
/* USER CODE END TIM2_MspPostInit 0 */
__HAL_RCC_GPIOB_CLK_ENABLE();
/**TIM2 GPIO Configuration
PB3 ------> TIM2_CH2
*/
GPIO_InitStruct.Pin = BUZZER_PWM_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(BUZZER_PWM_GPIO_Port, &GPIO_InitStruct);
__HAL_AFIO_REMAP_TIM2_PARTIAL_1();
/* USER CODE BEGIN TIM2_MspPostInit 1 */
/* USER CODE END TIM2_MspPostInit 1 */
}
}
void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* tim_pwmHandle)
{
if(tim_pwmHandle->Instance==TIM2)
{
/* USER CODE BEGIN TIM2_MspDeInit 0 */
/* USER CODE END TIM2_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM2_CLK_DISABLE();
/* USER CODE BEGIN TIM2_MspDeInit 1 */
/* USER CODE END TIM2_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
void buzzer_on(void)
{
HAL_TIM_PWM_Start( &htim2 , TIM_CHANNEL_2 );
}
void buzzer_off(void)
{
HAL_TIM_PWM_Stop( &htim2 , TIM_CHANNEL_2 );
}
void buzzer_button_press(void)
{
buzzer_on();
HAL_Delay(50);
buzzer_off();
}
/* USER CODE END 1 */

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

@ -0,0 +1,121 @@
#include "u8g2_hal.h"
#include "menuConfig.h"
#include "i2c.h"
#define HARDWARE_I2C
#define OLED_I2C_DEV_ADDRESS 0x78
#ifdef HARDWARE_I2C
static uint8_t u8x8_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
uint8_t* data = (uint8_t*) arg_ptr;
switch(msg) {
case U8X8_MSG_BYTE_SEND:
while( arg_int-- > 0 ) {
///@todo <20>޸<EFBFBD>
/* ==================================== */
I2C2->DR = *data++;
while( __HAL_I2C_GET_FLAG(&hi2c2, I2C_FLAG_TXE) == RESET );
/* ==================================== */
}
break;
case U8X8_MSG_BYTE_INIT:
/* add your custom code to init i2c subsystem */
break;
case U8X8_MSG_BYTE_SET_DC:
/* ignored for i2c */
break;
case U8X8_MSG_BYTE_START_TRANSFER:
///@todo <20>޸<EFBFBD>
/* ==================================== */
/* Wait until BUSY flag is reset */
while( __HAL_I2C_GET_FLAG( &hi2c2, I2C_FLAG_BUSY ) == SET );
/* Disable Pos */
CLEAR_BIT( I2C2->CR1, I2C_CR1_POS );
/* Generate Start */
SET_BIT( I2C2->CR1, I2C_CR1_START );
/* Wait until SB flag is set */
while( __HAL_I2C_GET_FLAG( &hi2c2, I2C_FLAG_SB ) == RESET );
/* Send slave address */
I2C2->DR = I2C_7BIT_ADD_WRITE( OLED_I2C_DEV_ADDRESS );
/* Wait until ADDR flag is set */
while ( __HAL_I2C_GET_FLAG( &hi2c2, I2C_FLAG_ADDR) == RESET );
/* Clear ADDR flag */
__HAL_I2C_CLEAR_ADDRFLAG( &hi2c2 );
/* Wait until TXE flag is set */
while( __HAL_I2C_GET_FLAG(&hi2c2, I2C_FLAG_TXE) == RESET );
/* ==================================== */
break;
case U8X8_MSG_BYTE_END_TRANSFER:
///@todo <20>޸<EFBFBD>
/* ==================================== */
/* Generate Stop */
SET_BIT( I2C2->CR1, I2C_CR1_STOP );
/* ==================================== */
break;
default:
return 0;
}
return 1;
}
static uint8_t u8x8_gpio_and_delay_hw(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
switch (msg) {
case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds
break;
case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds
break;
case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
HAL_Delay(1);
break;
case U8X8_MSG_DELAY_I2C: // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
break; // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
case U8X8_MSG_GPIO_I2C_CLOCK: // arg_int=0: Output low at I2C clock pin
break; // arg_int=1: Input dir with pullup high for I2C clock pin
case U8X8_MSG_GPIO_I2C_DATA: // arg_int=0: Output low at I2C data pin
break; // arg_int=1: Input dir with pullup high for I2C data pin
case U8X8_MSG_GPIO_MENU_SELECT:
u8x8_SetGPIOResult(u8x8, /* get menu select pin state */ 0);
break;
case U8X8_MSG_GPIO_MENU_NEXT:
u8x8_SetGPIOResult(u8x8, /* get menu next pin state */ 0);
break;
case U8X8_MSG_GPIO_MENU_PREV:
u8x8_SetGPIOResult(u8x8, /* get menu prev pin state */ 0);
break;
case U8X8_MSG_GPIO_MENU_HOME:
u8x8_SetGPIOResult(u8x8, /* get menu home pin state */ 0);
break;
default:
u8x8_SetGPIOResult(u8x8, 1); // default return value
break;
}
return 1;
}
//static void HardWare_I2C2_GPIOInit(void)
//{
//}
#endif
void u8g2Init(u8g2_t *u8g2)
{
#ifdef HARDWARE_I2C
// HardWare_I2C2_GPIOInit();//<2F><><EFBFBD><EFBFBD>i2c.c<><63><EFBFBD><EFBFBD><EFBFBD>ɳ<EFBFBD>ʼ<EFBFBD><CABC>
u8g2_Setup_ssd1306_i2c_128x64_noname_f(u8g2, U8G2_R0, u8x8_byte_hw_i2c, u8x8_gpio_and_delay_hw);
#endif
u8g2_InitDisplay(u8g2);
u8g2_SetPowerSave(u8g2, 0);
u8g2_ClearBuffer(u8g2);
}

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

@ -0,0 +1,164 @@
/* 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, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "usart.h"
/* USER CODE BEGIN 0 */
#include <stdbool.h>
#include "usbd_cdc_if.h"
#include "rf433_hal.h"
uint8_t usb_rx_data;
uart_feature current_feature = FUNC_FEATURE1;
/* USER CODE END 0 */
UART_HandleTypeDef huart1;
/* USART1 init function */
void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 9600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(uartHandle->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspInit 0 */
/* USER CODE END USART1_MspInit 0 */
/* USART1 clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USART1 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspInit 1 */
/* USER CODE END USART1_MspInit 1 */
}
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
if(uartHandle->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspDeInit 0 */
/* USER CODE END USART1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_USART1_CLK_DISABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
/* USART1 interrupt Deinit */
HAL_NVIC_DisableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspDeInit 1 */
/* USER CODE END USART1_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
void uart1_reconfig( uint32_t rate )
{
/* 原串口1的初始化 */
huart1.Instance = USART1;
huart1.Init.BaudRate = rate;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1)
{
/* 调用RF433模块的UART接收回调 */
rf433_hal_uart_rxcplt_callback();
}
}
void usb_receive_to_tx_send( void )
{
HAL_Delay(1);
if(usb_rx_complete)
{
HAL_UART_Transmit(&huart1,my_usb_rx_data, my_usb_rx_num ,500);
memset(my_usb_rx_data, 0, my_usb_rx_num);
my_usb_rx_num = 0;
usb_rx_complete = 0;
}
}
/* USER CODE END 1 */