找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3787|回复: 2
收起左侧

STM32单片机+SX1278官方源码分享

[复制链接]
ID:234051 发表于 2019-5-17 16:52 | 显示全部楼层 |阅读模式
自带收发功能,支持SX1272/1276/1278

  1. #include <string.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include "platform.h"
  5. #include "led.h"

  6. #if USE_UART
  7. #include "uart.h"
  8. #endif

  9. #include "radio.h"


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


  11. static uint16_t BufferSize = BUFFER_SIZE;                        // RF buffer size
  12. static uint8_t Buffer[BUFFER_SIZE];                                        // RF buffer

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

  14. tRadioDriver *Radio = NULL;

  15. const uint8_t PingMsg[] = "PING";
  16. const uint8_t PongMsg[] = "PONG";

  17. /*
  18. * Manages the master operation
  19. */
  20. void OnMaster( void )
  21. {
  22.     uint8_t i;

  23.     switch( Radio->Process( ) )
  24.     {
  25.     case RF_RX_TIMEOUT:
  26.         // Send the next PING frame
  27.         Buffer[0] = 'P';
  28.         Buffer[1] = 'I';
  29.         Buffer[2] = 'N';
  30.         Buffer[3] = 'G';
  31.         for( i = 4; i < BufferSize; i++ )
  32.         {
  33.             Buffer[i] = i - 4;
  34.         }
  35.         Radio->SetTxPacket( Buffer, BufferSize );
  36.         break;
  37.     case RF_RX_DONE:
  38.         Radio->GetRxPacket( Buffer, ( uint16_t* )&BufferSize );

  39.         if( BufferSize > 0 )
  40.         {
  41.             if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 )
  42.             {
  43.                 // Indicates on a LED that the received frame is a PONG
  44.                 LedToggle( LED_GREEN );

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

  73. /*
  74. * Manages the slave operation
  75. */
  76. void OnSlave( void )
  77. {
  78.     uint8_t i;

  79.     switch( Radio->Process( ) )
  80.     {
  81.     case RF_RX_DONE:
  82.         Radio->GetRxPacket( Buffer, ( uint16_t* )&BufferSize );

  83.         if( BufferSize > 0 )
  84.         {
  85.             if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
  86.             {
  87.                 // Indicates on a LED that the received frame is a PING
  88.                 LedToggle( LED_GREEN );

  89.                // Send the reply to the PONG string
  90.                 Buffer[0] = 'P';
  91.                 Buffer[1] = 'O';
  92.                 Buffer[2] = 'N';
  93.                 Buffer[3] = 'G';
  94.                 // We fill the buffer with numbers for the payload
  95.                 for( i = 4; i < BufferSize; i++ )
  96.                 {
  97.                     Buffer[i] = i - 4;
  98.                 }

  99.                 Radio->SetTxPacket( Buffer, BufferSize );
  100.             }
  101.         }
  102.         break;
  103.     case RF_TX_DONE:
  104.         // Indicates on a LED that we have sent a PONG
  105.         LedToggle( LED_RED );
  106.         Radio->StartRx( );
  107.         break;
  108.     default:
  109.         break;
  110.     }
  111. }


  112. /*
  113. * Main application entry point.
  114. */
  115. int main( void )
  116. {
  117.     BoardInit( );

  118.     Radio = RadioDriverInit( );

  119.     Radio->Init( );

  120.     Radio->StartRx( );

  121.     while( 1 )
  122.     {
  123.         if( EnableMaster == true )
  124.         {
  125.             OnMaster( );
  126.         }
  127.         else
  128.         {
  129.             OnSlave( );
  130.         }   
  131. #if( PLATFORM == SX12xxEiger ) && ( USE_UART == 1 )

  132.         UartProcess( );

  133.         {
  134.             uint8_t data = 0;
  135.             if( UartGetChar( &data ) == UART_OK )
  136.             {
  137.                 UartPutChar( data );
  138.             }
  139.         }
  140. #endif        
  141.     }
  142. #ifdef __GNUC__
  143.     return 0;
  144. #endif
  145. }
复制代码
全部资料51hei下载地址:
semtech驱动源码SX12xxDrivers-2.0.0.7z (577.25 KB, 下载次数: 108)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:453198 发表于 2020-12-17 11:08 | 显示全部楼层
这是stm32的程序
回复

使用道具 举报

ID:246744 发表于 2021-11-3 00:04 | 显示全部楼层
十分感谢分享代码,学习了~~~
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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