Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

178 lines
5.1 KiB

  1. /*!
  2. * \file main.c
  3. *
  4. * \brief Radio sensitivity test
  5. *
  6. * \remark When LED1 stops blinking LoRa packets aren't received any more and
  7. * the sensitivity level has been reached.
  8. * By reading the RF generator output power we can estimate the board
  9. * sensitivity
  10. *
  11. * \copyright Revised BSD License, see section \ref LICENSE.
  12. *
  13. * \code
  14. * ______ _
  15. * / _____) _ | |
  16. * ( (____ _____ ____ _| |_ _____ ____| |__
  17. * \____ \| ___ | (_ _) ___ |/ ___) _ \
  18. * _____) ) ____| | | || |_| ____( (___| | | |
  19. * (______/|_____)_|_|_| \__)_____)\____)_| |_|
  20. * (C)2013-2017 Semtech
  21. *
  22. * \endcode
  23. *
  24. * \author Miguel Luis ( Semtech )
  25. *
  26. * \author Gregory Cristian ( Semtech )
  27. */
  28. #include "board.h"
  29. #include "gpio.h"
  30. #include "timer.h"
  31. #include "radio.h"
  32. #if defined( REGION_AS923 )
  33. #define RF_FREQUENCY 923000000 // Hz
  34. #elif defined( REGION_AU915 )
  35. #define RF_FREQUENCY 915000000 // Hz
  36. #elif defined( REGION_CN470 )
  37. #define RF_FREQUENCY 470000000 // Hz
  38. #elif defined( REGION_CN779 )
  39. #define RF_FREQUENCY 779000000 // Hz
  40. #elif defined( REGION_EU433 )
  41. #define RF_FREQUENCY 433000000 // Hz
  42. #elif defined( REGION_EU868 )
  43. #define RF_FREQUENCY 868000000 // Hz
  44. #elif defined( REGION_KR920 )
  45. #define RF_FREQUENCY 920000000 // Hz
  46. #elif defined( REGION_IN865 )
  47. #define RF_FREQUENCY 865000000 // Hz
  48. #elif defined( REGION_US915 )
  49. #define RF_FREQUENCY 915000000 // Hz
  50. #elif defined( REGION_RU864 )
  51. #define RF_FREQUENCY 864000000 // Hz
  52. #else
  53. #error "Please define a frequency band in the compiler options."
  54. #endif
  55. #if defined( USE_MODEM_LORA )
  56. #define LORA_BANDWIDTH 0 // [0: 125 kHz,
  57. // 1: 250 kHz,
  58. // 2: 500 kHz,
  59. // 3: Reserved]
  60. #define LORA_SPREADING_FACTOR 10 // [SF7..SF12]
  61. #define LORA_CODINGRATE 1 // [1: 4/5,
  62. // 2: 4/6,
  63. // 3: 4/7,
  64. // 4: 4/8]
  65. #define LORA_SYMBOL_TIMEOUT 5 // Symbols
  66. #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
  67. #define LORA_FIX_LENGTH_PAYLOAD_ON false
  68. #define LORA_IQ_INVERSION_ON false
  69. #elif defined( USE_MODEM_FSK )
  70. #define FSK_DATARATE 50000 // bps
  71. #define FSK_BANDWIDTH 50000 // Hz
  72. #define FSK_AFC_BANDWIDTH 83333 // Hz
  73. #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
  74. #define FSK_FIX_LENGTH_PAYLOAD_ON false
  75. #else
  76. #error "Please define a modem in the compiler options."
  77. #endif
  78. /*!
  79. * Radio events function pointer
  80. */
  81. static RadioEvents_t RadioEvents;
  82. /*!
  83. * LED GPIO pins objects
  84. */
  85. extern Gpio_t Led1;
  86. /*!
  87. * \brief Function to be executed on Radio Rx Done event
  88. */
  89. void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
  90. /*!
  91. * Main application entry point.
  92. */
  93. int main( void )
  94. {
  95. // Target board initialization
  96. BoardInitMcu( );
  97. BoardInitPeriph( );
  98. // Radio initialization
  99. RadioEvents.RxDone = OnRxDone;
  100. Radio.Init( &RadioEvents );
  101. Radio.SetChannel( RF_FREQUENCY );
  102. #if defined( USE_MODEM_LORA )
  103. Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
  104. LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
  105. LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
  106. 0, true, 0, 0, LORA_IQ_INVERSION_ON, true );
  107. Radio.SetMaxPayloadLength( MODEM_LORA, 255 );
  108. #elif defined( USE_MODEM_FSK )
  109. Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
  110. 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
  111. 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, true,
  112. 0, 0, false, true );
  113. Radio.SetMaxPayloadLength( MODEM_FSK, 255 );
  114. #else
  115. #error "Please define a frequency band in the compiler options."
  116. #endif
  117. Radio.Rx( 0 ); // Continuous Rx
  118. while( 1 )
  119. {
  120. BoardLowPowerHandler( );
  121. // Process Radio IRQ
  122. if( Radio.IrqProcess != NULL )
  123. {
  124. Radio.IrqProcess( );
  125. }
  126. }
  127. }
  128. void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
  129. {
  130. static uint8_t ledState = 1;
  131. // Toggle LED 1
  132. ledState ^= 1;
  133. GpioWrite( &Led1, ledState );
  134. }