找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2198|回复: 0
打印 上一主题 下一主题
收起左侧

LoRa官方驱动程序 SX12xxDrivers

[复制链接]
跳转到指定楼层
楼主
ID:342496 发表于 2019-10-7 19:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
官方驱动源程序如下:
  1. /*!
  2. * \file       main.c
  3. * \brief      Ping-Pong example application on how to use Semtech's Radio
  4. *             drivers.
  5. *
  6. * \version    2.0
  7. * \date       Nov 21 2012
  8. * \author     Miguel Luis
  9. */
  10. #include <string.h>
  11. #include <stdint.h>
  12. #include <stdbool.h>
  13. #include "platform.h"
  14. #include "led.h"

  15. #if USE_UART
  16. #include "uart.h"
  17. #endif

  18. #include "radio.h"


  19. #define BUFFER_SIZE                                 9 // Define the payload size here


  20. static uint16_t BufferSize = BUFFER_SIZE;                        // RF buffer size
  21. static uint8_t Buffer[BUFFER_SIZE];                                        // RF buffer

  22. static uint8_t EnableMaster = true;                                 // Master/Slave selection

  23. tRadioDriver *Radio = NULL;

  24. const uint8_t PingMsg[] = "PING";
  25. const uint8_t PongMsg[] = "PONG";

  26. /*
  27. * Manages the master operation
  28. */
  29. void OnMaster( void )
  30. {
  31.     uint8_t i;
  32.    
  33.     switch( Radio->Process( ) )
  34.     {
  35.     case RF_RX_TIMEOUT:
  36.         // Send the next PING frame
  37.         Buffer[0] = 'P';
  38.         Buffer[1] = 'I';
  39.         Buffer[2] = 'N';
  40.         Buffer[3] = 'G';
  41.         for( i = 4; i < BufferSize; i++ )
  42.         {
  43.             Buffer[i] = i - 4;
  44.         }
  45.         Radio->SetTxPacket( Buffer, BufferSize );
  46.         break;
  47.     case RF_RX_DONE:
  48.         Radio->GetRxPacket( Buffer, ( uint16_t* )&BufferSize );
  49.    
  50.         if( BufferSize > 0 )
  51.         {
  52.             if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 )
  53.             {
  54.                 // Indicates on a LED that the received frame is a PONG
  55.                 LedToggle( LED_GREEN );

  56.                 // Send the next PING frame            
  57.                 Buffer[0] = 'P';
  58.                 Buffer[1] = 'I';
  59.                 Buffer[2] = 'N';
  60.                 Buffer[3] = 'G';
  61.                 // We fill the buffer with numbers for the payload
  62.                 for( i = 4; i < BufferSize; i++ )
  63.                 {
  64.                     Buffer[i] = i - 4;
  65.                 }
  66.                 Radio->SetTxPacket( Buffer, BufferSize );
  67.             }
  68.             else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
  69.             { // A master already exists then become a slave
  70.                 EnableMaster = false;
  71.                 LedOff( LED_RED );
  72.             }
  73.         }            
  74.         break;
  75.     case RF_TX_DONE:
  76.         // Indicates on a LED that we have sent a PING
  77.         LedToggle( LED_RED );
  78.         Radio->StartRx( );
  79.         break;
  80.     default:
  81.         break;
  82.     }
  83. }

  84. /*
  85. * Manages the slave operation
  86. */
  87. void OnSlave( void )
  88. {
  89.     uint8_t i;

  90.     switch( Radio->Process( ) )
  91.     {
  92.     case RF_RX_DONE:
  93.         Radio->GetRxPacket( Buffer, ( uint16_t* )&BufferSize );
  94.    
  95.         if( BufferSize > 0 )
  96.         {
  97.             if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
  98.             {
  99.                 // Indicates on a LED that the received frame is a PING
  100.                 LedToggle( LED_GREEN );

  101.                // Send the reply to the PONG string
  102.                 Buffer[0] = 'P';
  103.                 Buffer[1] = 'O';
  104.                 Buffer[2] = 'N';
  105.                 Buffer[3] = 'G';
  106.                 // We fill the buffer with numbers for the payload
  107.                 for( i = 4; i < BufferSize; i++ )
  108.                 {
  109.                     Buffer[i] = i - 4;
  110.                 }

  111.                 Radio->SetTxPacket( Buffer, BufferSize );
  112.             }
  113.         }
  114.         break;
  115.     case RF_TX_DONE:
  116.         // Indicates on a LED that we have sent a PONG
  117.         LedToggle( LED_RED );
  118.         Radio->StartRx( );
  119.         break;
  120.     default:
  121.         break;
  122.     }
  123. }


  124. /*
  125. * Main application entry point.
  126. */
  127. int main( void )
  128. {
  129.     BoardInit( );
  130.    
  131.     Radio = RadioDriverInit( );
  132.    
  133.     Radio->Init( );

  134.     Radio->StartRx( );
  135.    
  136.     while( 1 )
  137.     {
  138.         if( EnableMaster == true )
  139.         {
  140.             OnMaster( );
  141.         }
  142.         else
  143.         {
  144.             OnSlave( );
  145.         }   
  146. #if( PLATFORM == SX12xxEiger ) && ( USE_UART == 1 )

  147.         UartProcess( );
  148.         
  149.         {
  150.             uint8_t data = 0;
  151.             if( UartGetChar( &data ) == UART_OK )
  152.             {
  153.                 UartPutChar( data );
  154.             }
  155.         }
  156. #endif        
  157.     }
  158. #ifdef __GNUC__
  159.     return 0;
  160. #endif
  161. }
复制代码

所有资料51hei提供下载:
SX12xxDrivers-V2.1.0.7z (820.95 KB, 下载次数: 46)



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表