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

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