【STM32WLE5之LoRa#系列3、易智联LM401评估板PingPong应⽤移植】STM32WLE5之LoRa# 系列3、易智联LM401 评估板PingPong应⽤移植
前⾔
前⾯我们已经做CubeMX和CubeIDE的⼯程移植,下⾯我们就将具体的应⽤程序进⾏修改
⼀、系统初始化SYS_APP.c⽂件
这个⽂件是系统应⽤的初始化,包括RTC、UART、低功耗等的初始化与配置。这⾥我们屏蔽掉DBG_Disable()和DBG_ProbesInit()两个函数。如下所⽰:
void SystemApp_Init(void)
{
/* USER CODE BEGIN SystemApp_Init_1 */
/* USER CODE END SystemApp_Init_1 */
/* Ensure that MSI is wake-up system clock */
__HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_MSI);
/*Initialize timer and RTC*/
UTIL_TIMER_Init();
/* Debug config : disable serial wires and DbgMcu pins settings */
//DBG_Disable();
/* Initializes the SW probes pins and the monitor RF pins via Alternate Function */
//DBG_ProbesInit();
/*Initialize the terminal */
UTIL_ADV_TRACE_Init();
UTIL_ADV_TRACE_RegisterTimeStampFunction(TimestampNow);
⼆、中断stm32wlxx_it.c⽂件
在⽂件的后⾯增加按键的中断函数
/* USER CODE BEGIN 1 */
void EXTI0_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);;
}
void EXTI1_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1);
}
void EXTI4_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_4);
}
/* USER CODE END 1 */
三、PingPong的应⽤程序
将PingPong的应⽤程序覆盖subghz_phy_app.c的内容
/*!
* \file      subghz_phy_app.c
*
* \brief    Ping-Pong implementation
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*                ______                              _
*              / _____)            _              | |
*              ( (____  _____ ____ _| |_ _____  ____| |__
*              \____ \| ___ |    (_  _) ___ |/ ___)  _ \
*              _____) ) ____| | | || |_| ____( (___| | | |
*              (______/|_____)_|_|_| \__)_____)\____)_| |_|
*              (C)2013-2017 Semtech
*
* \endcode
*
* \author    Miguel Luis ( Semtech )
*
* \author    Gregory Cristian ( Semtech )
*/
/**
******************************************************************************
*
*          Portions COPYRIGHT 2020 STMicroelectronics
*
* @file    subghz_phy_app.c
* @author  MCD Application Team
* @brief  Application of the SubGHz_Phy Middleware
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/ #include"platform.h"
#include"sys_app.h"
#include"subghz_phy_app.h"
#include"radio.h"
#include"app_version.h"
/* USER CODE BEGIN Includes */
#include"stm32_timer.h"
#include"stm32_seq.h"
#include"utilities_def.h"
/* USER CODE END Includes */
/* External variables ---------------------------------------------------------*/ /* USER CODE BEGIN EV */
/* USER CODE END EV */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
typedef enum
{
RX,
RX_TIMEOUT,
RX_ERROR,
TX,
TX_TIMEOUT,
} States_t;
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */
/* Configurations */
/*Timeout*/
#define RX_TIMEOUT_VALUE              5000
#define TX_TIMEOUT_VALUE              3000
#define TX_TIMEOUT_VALUE              3000
/* PING string*/
#define PING "PING"
/* PONG string*/
#define PONG "PONG"
/*Size of the payload to be sent*/
/* Size must be greater of equal the PING and PONG*/
#define MAX_APP_BUFFER_SIZE          255
#if(PAYLOAD_LEN > MAX_APP_BUFFER_SIZE)
#error PAYLOAD_LEN must be less or equal than MAX_APP_BUFFER_SIZE
#endif/* (PAYLOAD_LEN > MAX_APP_BUFFER_SIZE) */
/
* wait for remote to be in Rx, before sending a Tx frame*/
#define RX_TIME_MARGIN                200
/* Afc bandwidth in Hz */
#define FSK_AFC_BANDWIDTH            83333
/* LED blink Period*/
#define LED_PERIOD_MS                200
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/
* Radio events function pointer */
static RadioEvents_t RadioEvents;
/* USER CODE BEGIN PV */
/*Ping Pong FSM states */
static States_t State = RX;
/* App Rx Buffer*/
static uint8_t BufferRx[MAX_APP_BUFFER_SIZE];
/* App Tx Buffer*/
timeout on t2 timerstatic uint8_t BufferTx[MAX_APP_BUFFER_SIZE];
/* Last  Received Buffer Size*/
uint16_t RxBufferSize =0;
/
* Last  Received packer Rssi*/
int8_t RssiValue =0;
/* Last  Received packer SNR (in Lora modulation)*/
int8_t SnrValue =0;
/* Led Timers objects*/
static UTIL_TIMER_Object_t timerLed;
/* device state. Master: true, Slave: false*/
bool isMaster = true;
/* random delay to make sure 2 devices will sync*/
/* the closest the random delays are, the longer it will
take for the devices to sync when started simultaneously*/
static int32_t random_delay;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/*!
* @brief Function to be executed on Radio Tx Done event
*/
static void OnTxDone(void);
/**
* @brief Function to be executed on Radio Rx Done event
* @param  payload ptr of buffer received
* @param  size buffer size
* @param  rssi
* @param  LoraSnr_FskCfo
*/
static void OnRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t LoraSnr_FskCfo); /**
* @brief Function executed on Radio Tx Timeout event
* @brief Function executed on Radio Tx Timeout event
*/
static void OnTxTimeout(void);
/**
* @brief Function executed on Radio Rx Timeout event
*/
static void OnRxTimeout(void);
/**
* @brief Function executed on Radio Rx Error event
*/
static void OnRxError(void);
/* USER CODE BEGIN PFP */
/**
* @brief  Function executed on when led timer elapses
* @param  context ptr of LED context
*/
static void OnledEvent(void*context);
/
**
* @brief PingPong state machine implementation
*/
static void PingPong_Process(void);
/* USER CODE END PFP */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
switch(GPIO_Pin)
{
case  BUTTON_SW1_PIN:
BSP_LED_Toggle(LED_BLUE);
HAL_Delay(20);
BSP_LED_Toggle(LED_BLUE);
APP_PRINTF("BUTTON SW1\r\n");
break;
case  BUTTON_SW2_PIN:
BSP_LED_Toggle(LED_GREEN);
HAL_Delay(20);
BSP_LED_Toggle(LED_GREEN);
APP_PRINTF("BUTTON SW2\r\n");
break;
case  BUTTON_SW3_PIN:
BSP_LED_Toggle(LED_RED);
HAL_Delay(20);
BSP_LED_Toggle(LED_RED);
APP_PRINTF("BUTTON SW3\r\n");
break;
default:
APP_PRINTF("Unkonw Button\r\n");
break;
}
}
/* Exported functions ---------------------------------------------------------*/
void SubghzApp_Init(void)
{
/* USER CODE BEGIN SubghzApp_Init_1 */
APP_LOG(TS_OFF, VLEVEL_M,"\n\rPING PONG\n\r");
/* Print APP version*/
APP_LOG(TS_OFF, VLEVEL_M,"APP_VERSION= V%X.%X.%X\r\n",
(uint8_t)(__APP_VERSION >> __APP_VERSION_MAIN_SHIFT),
(uint8_t)(__APP_VERSION >> __APP_VERSION_SUB1_SHIFT),
(uint8_t)(__APP_VERSION >> __APP_VERSION_SUB2_SHIFT));
/* Led Timers*/
UTIL_TIMER_Create(&timerLed,0xFFFFFFFFU, UTIL_TIMER_ONESHOT, OnledEvent,NULL); UTIL_TIMER_SetPeriod(&timerLed, LED_PERIOD_MS);
UTIL_TIMER_SetPeriod(&timerLed, LED_PERIOD_MS);
UTIL_TIMER_Start(&timerLed);
/* USER CODE END SubghzApp_Init_1 */
/* Radio initialization */
RadioEvents.TxDone = OnTxDone;
RadioEvents.RxDone = OnRxDone;
RadioEvents.TxTimeout = OnTxTimeout;
RadioEvents.RxTimeout = OnRxTimeout;
RadioEvents.RxError = OnRxError;
Radio.Init(&RadioEvents);
/* USER CODE BEGIN SubghzApp_Init_2 */
/
* Radio Set frequency */
Radio.SetChannel(RF_FREQUENCY);
/* Radio configuration */
#if((USE_MODEM_LORA == 1)&&(USE_MODEM_FSK == 0))
APP_LOG(TS_OFF, VLEVEL_M,"---------------\n\r");
APP_LOG(TS_OFF, VLEVEL_M,"LORA_MODULATION\n\r");
APP_LOG(TS_OFF, VLEVEL_M,"LORA_BW=%d kHz\n\r",(1<< LORA_BANDWIDTH)*125); APP_LOG(TS_OFF, VLEVEL_M,"LORA_SF=%d\n\r", LORA_SPREADING_FACTOR);
Radio.SetTxConfig(MODEM_LORA, TX_OUTPUT_POWER,0, LORA_BANDWIDTH,
LORA_SPREADING_FACTOR, LORA_CODINGRATE,
LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
true,0,0, LORA_IQ_INVERSION_ON, TX_TIMEOUT_VALUE);
Radio.SetRxConfig(MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
LORA_CODINGRATE,0, LORA_PREAMBLE_LENGTH,
LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
0, true,0,0, LORA_IQ_INVERSION_ON, true);
Radio.SetMaxPayloadLength(MODEM_LORA, MAX_APP_BUFFER_SIZE);
#elif((USE_MODEM_LORA == 0)&&(USE_MODEM_FSK == 1))
APP_LOG(TS_OFF, VLEVEL_M,"---------------\n\r");
APP_LOG(TS_OFF, VLEVEL_M,"FSK_MODULATION\n\r");
APP_LOG(TS_OFF, VLEVEL_M,"FSK_BW=%d Hz\n\r", FSK_BANDWIDTH);
APP_LOG(TS_OFF, VLEVEL_M,"FSK_DR=%d bits/s\n\r", FSK_DATARATE);
Radio.SetTxConfig(MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV,0,
FSK_DATARATE,0,
FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
true,0,0,0, TX_TIMEOUT_VALUE);
Radio.SetRxConfig(MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
0, FSK_FIX_LENGTH_PAYLOAD_ON,0, true,
0,0, false, true);
Radio.SetMaxPayloadLength(MODEM_FSK, MAX_APP_BUFFER_SIZE);
#else
#error"Please define a modulation in the subghz_phy_app.h file."
#endif/* USE_MODEM_LORA | USE_MODEM_FSK */
/
* LED initialization*/
BSP_LED_Init(LED_GREEN);
BSP_LED_Init(LED_RED);
BSP_LED_Init(LED_BLUE);
BSP_PB_Init(BUTTON_SW1, BUTTON_MODE_EXTI);
BSP_PB_Init(BUTTON_SW2, BUTTON_MODE_EXTI);
BSP_PB_Init(BUTTON_SW3, BUTTON_MODE_EXTI);
/*calculate random delay for synchronization*/

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。