打印
[uCOS/RTOS]

[RTOS]+Rhtread基于Lora开关控制

[复制链接]
1390|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本帖最后由 publicpeople 于 2020-6-23 19:26 编辑

RT-Thread使用情况概述
主控器使用RT-Thread作为操作系统,进行任务调度。RT-thread使用的版本是3.1.2,直接使用KEIL添加到工程中。



由于直接采用KEIL添加的方式,与我使用的ES32_SDK_V1.07部分函数冲突。需要将SDK中iqr.c中的voidSysTick_Handler(void)、void PendSV_Handler(void)、void HardFault_Handler(void)进行注释。
硬件框架

本产品主要是Lora 通信接口的移植。本方案主要采用口模拟行的SPI通信。

主控器通过按键获取不同机井的控制状态,通过Lora 向周围的分控器发送指令。完成对终端的控制。
软件框架说明
软件模块说明
/**
  *********************************************************************************
  *
  * @file    main.c
  * @brief   Main file for DEMO
  *
  * @version V1.0
  * @date    17 Nov 2017
  * @author  AE Team
  * @NOTE   
  *
  * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  *
  *********************************************************************************
  */
#include <string.h>
#include "main.h"
#include "ald_gpio.h"
#include "ald_cmu.h"
#include "ald_rtc.h"

#include  "dido.h"
#include "timer.h"

//lora start
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
#include <string.h>
#include "radio.h"
#include "sx126x.h"
#include "sx126x-board.h"

#include "SPI_IO.h"
#define HAL_MAX_DELAY      0xFFFFFFFFU
#if defined( USE_BAND_868 )
#define RF_FREQUENCY                                868000000 // Hz
#elif defined( USE_BAND_915 )
#define RF_FREQUENCY                                915000000 // Hz
#elif defined( USE_BAND_433 )
#define RF_FREQUENCY                                433000000 // Hz
#else
    #error "Please define a frequency band in the compiler options."
#endif
#define TX_OUTPUT_POWER                             14        // dBm
#define LORA_BANDWIDTH                              2         // [0: 125 kHz,
                                                              //  1: 250 kHz,
                                                              //  2: 500 kHz,
                                                              //  3: Reserved]
#define LORA_SPREADING_FACTOR                       7         // [SF7..SF12]
#define LORA_CODINGRATE                             1         // [1: 4/5,
                                                              //  2: 4/6,
                                                              //  3: 4/7,
                                                              //  4: 4/8]
#define LORA_PREAMBLE_LENGTH                        108       // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT                         100       // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON                  false
#define LORA_IQ_INVERSION_ON                        false
#define LORA_FIX_LENGTH_PAYLOAD_LEN                 19
typedef enum
{
    LOWPOWER,
    RX,
    RX_TIMEOUT,
    RX_ERROR,
    TX,
    TX_TIMEOUT,
    START_CAD,
}States_t;
typedef enum
{
    CAD_FAIL,
    CAD_SUCCESS,
    PENDING,
}CadRx_t;
#define RX_TIMEOUT_VALUE                            4000
#define BUFFER_SIZE                                 64 // Define the payload size here
uint16_t BufferSize = BUFFER_SIZE;
uint8_t Buffer[BUFFER_SIZE];
States_t State = LOWPOWER;
int8_t RssiValue = 0;
int8_t SnrValue = 0;
/*!
* CAD performance evaluation's parameters
*/
#define RX_FW       1
#define TX_FW       0   //TX_FW is only for test
#define FULL_DBG    1   //Active all traces
#if(RX_FW == TX_FW)
    #error "Please define only one firmware."
#endif
CadRx_t CadRx = CAD_FAIL;
bool PacketReceived = false;
bool RxTimeoutTimerIrqFlag = false;
uint16_t channelActivityDetectedCnt = 0;
uint16_t RxCorrectCnt = 0;
uint16_t RxErrorCnt = 0;
uint16_t RxTimeoutCnt = 0;
uint16_t SymbTimeoutCnt = 0;
int16_t RssiMoy = 0;
int8_t SnrMoy = 0;

//CAD parameters
#define CAD_SYMBOL_NUM          LORA_CAD_02_SYMBOL
#define CAD_DET_PEAK            22
#define CAD_DET_MIN             10
#define CAD_TIMEOUT_MS          2000
#define NB_TRY                  10
/*!
* Radio events function pointer
*/
static RadioEvents_t RadioEvents;
/*!
* \brief Function to be executed on Radio Tx Done event
*/
void OnTxDone( void );
/*!
* \brief Function to be executed on Radio Rx Done event
*/
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
/*!
* \brief Function executed on Radio Tx Timeout event
*/
void OnTxTimeout( void );
/*!
* \brief Function executed on Radio Rx Timeout event
*/
void OnRxTimeout( void );
/*!
* \brief Function executed on Radio Rx Error event
*/
void OnRxError( void );
/*!
* \brief Function executed on Radio CAD Done event
*/
void OnCadDone( bool channelActivityDetected);
/*!
* \brief Function configuring CAD parameters
* \param [in]  cadSymbolNum   The number of symbol to use for CAD operations
*                             [LORA_CAD_01_SYMBOL, LORA_CAD_02_SYMBOL,
*                              LORA_CAD_04_SYMBOL, LORA_CAD_08_SYMBOL,
*                              LORA_CAD_16_SYMBOL]
* \param [in]  cadDetPeak     Limit for detection of SNR peak used in the CAD
* \param [in]  cadDetMin      Set the minimum symbol recognition for CAD
* \param [in]  cadTimeout     Defines the timeout value to abort the CAD activity
*/
void SX126xConfigureCad( RadioLoRaCadSymbols_t cadSymbolNum, uint8_t cadDetPeak, uint8_t cadDetMin , uint32_t cadTimeout);
/*!
* \brief CAD timeout timer callback
*/
static void CADTimeoutTimeoutIrq( void );
/*!
* \brief Rx timeout timer callback
*/
static void RxTimeoutTimerIrq( void );
/*!
* \brief Average the collected RSSIs during CAD
*/
int8_t AverageCadRssi( void );
/*!
* \brief Get the last good RSSI during CAD
*/
int8_t GetLastCadRssi( void );
/*!
* \brief Display collected RSSIs each ms during CAD
*/
void DisplayCadRssivsTime( void );
uint32_t uwTick;
__weak void HAL_IncTick(void)
{
  uwTick += 1;
}
/**
  * @brief Provides a tick value in millisecond.
  * @NOTE  This function is declared as __weak to be overwritten in case of other
  *       implementations in user file.
  * @retval tick value
  */
__weak uint32_t HAL_GetTick(void)
{
  return uwTick;
}
__weak void HAL_Delay(uint32_t Delay)//?óê±oá??
{
  uint32_t tickstart = HAL_GetTick();
  uint32_t wait = Delay;
  /* Add a period to guaranty minimum wait */
  if (wait < HAL_MAX_DELAY)
  {
    wait++;
  }
  while((HAL_GetTick() - tickstart) < wait)
  {
  }
}
extern void RadioSetRxDutyCycle( uint32_t rxTime, uint32_t sleepTime );
/** @addtogroup Projects_Examples_ALD
  * @{
  */
/** @addtogroup Examples
  * @{
  */
/**
  * @brief  Test main function
  * @retval Status.
  */
//rt-thread
#include <rtthread.h>
/* 定义线程控制块 */
static struct rt_thread LORA_thread;
static struct rt_thread LED_RUN;
static struct rt_thread Button_RUN;
/* 定义线程控栈时要求 RT_ALIGN_SIZE 个字节对齐 */
ALIGN(RT_ALIGN_SIZE)
/* 定义线程栈 */
static rt_uint8_t LORA_entry_stack[256];
static rt_uint8_t LED_RUN_entry_stack[128];
static rt_uint8_t Button_entry_stack[128];
static void LORA_entry(void *Parameter)
{
while(1)
{
  

  // SX126xSetRx( 0x2fffff );
  Radio.IrqProcess( );
  rt_thread_mdelay(300);
  
}
}
static void LED_RUN_entry(void *Parameter)
{

while(1)
{
  

    ald_gpio_toggle_pin(GPIOC, GPIO_PIN_9);
  rt_thread_mdelay(1000);
  
}

}
static void Button_entry(void *Parameter)
{

unsigned char button_buf[2]={0x11,0x22};
while(1)
{
  

  if(ald_gpio_read_pin(GPIOF, GPIO_PIN_0)==0)  
  {
   ald_gpio_write_pin(GPIOC, GPIO_PIN_9,0);
   Radio.Send(button_buf,1);
  
  }
  
  if(ald_gpio_read_pin(GPIOF, GPIO_PIN_1)==0)  
  {
   ald_gpio_write_pin(GPIOC, GPIO_PIN_9,0);
   Radio.Send(&button_buf[1],1);
  
  }
   rt_thread_mdelay(100);
}

}
int main()
{

uint8_t buffer[2]={0,22};
  uint8_t *payloadLength=0;
  uint8_t *rxStartBufferPointer=0;
  uint16_t PacketCnt = 0;
/* Initialize */
// ald_cmu_init();
// ald_cmu_clock_config(CMU_CLOCK_HOSC,8000000);
rt_err_t result;
Dido_Init();
SPI_Init_IO();
  
  //******************************************************启动LORA_thread 线程
  result = rt_thread_init(&LORA_thread, /* 线程控制块 */
      "LORA_entry", /* 线程名字 */
    LORA_entry, /* 线程入口函数 */
    RT_NULL, /* 线程入口函数参数 */
    &LORA_entry_stack[0], /* 线程栈起始地址 */
  sizeof(LORA_entry_stack), /* 线程栈大小 */
                6, /* 线程的优先级 */
         20); /* 线程时间片 */
    rt_thread_startup(&LORA_thread); /* 启动线程,开启调度 */
  
  
//******************************************************启动LED_RUN 线程
  result = rt_thread_init(&LED_RUN, /* 线程控制块 */
      "LED_RUN_entry", /* 线程名字 */
    LED_RUN_entry, /* 线程入口函数 */
    RT_NULL, /* 线程入口函数参数 */
    &LED_RUN_entry_stack[0], /* 线程栈起始地址 */
  sizeof(LED_RUN_entry_stack), /* 线程栈大小 */
                5, /* 线程的优先级 */
         20); /* 线程时间片 */
   rt_thread_startup(&LED_RUN);/* 启动线程,开启调度 */
  
  
  //******************************************************启动Button_entry 线程
   result = rt_thread_init(&Button_RUN, /* 线程控制块 */
      "Button_entry", /* 线程名字 */
    Button_entry, /* 线程入口函数 */
    RT_NULL, /* 线程入口函数参数 */
    &Button_entry_stack[0], /* 线程栈起始地址 */
   sizeof(Button_entry_stack), /* 线程栈大小 */
                3, /* 线程的优先级 */
         20); /* 线程时间片 */
   rt_thread_startup(&Button_RUN);/* 启动线程,开启调度 */
  //******************************************************启动 线程
  
}

void OnTxDone( void )
{
    Radio.Standby( );
    State = TX;
}
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
{
    Radio.Standby( );
    BufferSize = size;
    memcpy( Buffer, payload, BufferSize );
    RssiValue = rssi;
    SnrValue = snr;
    PacketReceived = true;
    RssiMoy = (((RssiMoy * RxCorrectCnt) + RssiValue) / (RxCorrectCnt + 1));
    SnrMoy = (((SnrMoy * RxCorrectCnt) + SnrValue) / (RxCorrectCnt + 1));
    State = RX;

ald_gpio_toggle_pin(GPIOC, GPIO_PIN_8);
}
void OnTxTimeout( void )
{
    Radio.Standby( );
    State = TX_TIMEOUT;
}
void OnRxTimeout( void )
{
    Radio.Standby( );
    if( RxTimeoutTimerIrqFlag )
    {
        State = RX_TIMEOUT;
    }
    else
    {
        Radio.Rx( RX_TIMEOUT_VALUE );   //  Restart Rx
        SymbTimeoutCnt++;               //  if we pass here because of Symbol Timeout
        State = LOWPOWER;
    }
}
void OnRxError( void )
{
    Radio.Standby( );
    State = RX_ERROR;
}
void OnCadDone( bool channelActivityDetected)
{
    Radio.Standby( );
    if( channelActivityDetected == true )
    {
        CadRx = CAD_SUCCESS;
    }
    else
    {
        CadRx = CAD_FAIL;
    }
    State = RX;
}
void SX126xConfigureCad( RadioLoRaCadSymbols_t cadSymbolNum, uint8_t cadDetPeak, uint8_t cadDetMin , uint32_t cadTimeout)
{
//    SX126xSetDioIrqParams(  IRQ_CAD_DONE | IRQ_CAD_ACTIVITY_DETECTED, IRQ_CAD_DONE | IRQ_CAD_ACTIVITY_DETECTED,
//                            IRQ_RADIO_NONE, IRQ_RADIO_NONE );
//    SX126xSetCadParams( cadSymbolNum, cadDetPeak, cadDetMin, LORA_CAD_ONLY, ((cadTimeout * 1000) / 15.625 ));
  
    SX126xSetDioIrqParams( IRQ_RX_DONE|IRQ_CRC_ERROR|IRQ_RX_TX_TIMEOUT,
                           IRQ_RX_DONE,
                           IRQ_RADIO_NONE,
                           IRQ_RADIO_NONE );  //

SX126xSetRx( 0x2fffff ); //
}
static void CADTimeoutTimeoutIrq( void )
{
    Radio.Standby( );
    State = START_CAD;
}
static void RxTimeoutTimerIrq( void )
{
    RxTimeoutTimerIrqFlag = true;
}
/**
  * @}
  */
/**
  * @}
  */
演示效果


演示地址

https://www.bilibili.com/video/BV1cC4y1Y7wz/

代码地址
https://gitee.com/publicpeople/codes/aipgclne51w70d4vu2bqt41



  

RT-Thread应用创新设计大赛作品模板-publicpeople.zip

3.12 MB

使用特权

评论回复

相关帖子

沙发
zeshoufx| | 2020-6-23 08:32 | 只看该作者
谢谢分享,,,,,,

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

261

帖子

2

粉丝