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.
 
 
 
 
 
 

165 lines
4.8 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_CN779 )
  37. #define RF_FREQUENCY 779000000 // Hz
  38. #elif defined( REGION_EU868 )
  39. #define RF_FREQUENCY 868000000 // Hz
  40. #elif defined( REGION_KR920 )
  41. #define RF_FREQUENCY 920000000 // Hz
  42. #elif defined( REGION_IN865 )
  43. #define RF_FREQUENCY 865000000 // Hz
  44. #elif defined( REGION_US915 )
  45. #define RF_FREQUENCY 915000000 // Hz
  46. #elif defined( REGION_RU864 )
  47. #define RF_FREQUENCY 864000000 // Hz
  48. #else
  49. #error "Please define a frequency band in the compiler options."
  50. #endif
  51. #if defined( USE_MODEM_LORA )
  52. #define LORA_BANDWIDTH 0 // [0: 125 kHz,
  53. // 1: 250 kHz,
  54. // 2: 500 kHz,
  55. // 3: Reserved]
  56. #define LORA_SPREADING_FACTOR 10 // [SF7..SF12]
  57. #define LORA_CODINGRATE 1 // [1: 4/5,
  58. // 2: 4/6,
  59. // 3: 4/7,
  60. // 4: 4/8]
  61. #define LORA_SYMBOL_TIMEOUT 5 // Symbols
  62. #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
  63. #define LORA_FIX_LENGTH_PAYLOAD_ON false
  64. #define LORA_IQ_INVERSION_ON false
  65. #elif defined( USE_MODEM_FSK )
  66. #define FSK_DATARATE 50000 // bps
  67. #define FSK_BANDWIDTH 50000 // Hz
  68. #define FSK_AFC_BANDWIDTH 83333 // Hz
  69. #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
  70. #define FSK_FIX_LENGTH_PAYLOAD_ON false
  71. #else
  72. #error "Please define a modem in the compiler options."
  73. #endif
  74. /*!
  75. * Radio events function pointer
  76. */
  77. static RadioEvents_t RadioEvents;
  78. /*!
  79. * LED GPIO pins objects
  80. */
  81. extern Gpio_t Led4;
  82. /*!
  83. * \brief Function to be executed on Radio Rx Done event
  84. */
  85. void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
  86. /*!
  87. * Main application entry point.
  88. */
  89. int main( void )
  90. {
  91. // Target board initialization
  92. BoardInitMcu( );
  93. BoardInitPeriph( );
  94. // Radio initialization
  95. RadioEvents.RxDone = OnRxDone;
  96. Radio.Init( &RadioEvents );
  97. Radio.SetChannel( RF_FREQUENCY );
  98. #if defined( USE_MODEM_LORA )
  99. Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
  100. LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
  101. LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
  102. 0, true, 0, 0, LORA_IQ_INVERSION_ON, true );
  103. Radio.SetMaxPayloadLength( MODEM_LORA, 255 );
  104. #elif defined( USE_MODEM_FSK )
  105. Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
  106. 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
  107. 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, true,
  108. 0, 0, false, true );
  109. Radio.SetMaxPayloadLength( MODEM_FSK, 255 );
  110. #else
  111. #error "Please define a frequency band in the compiler options."
  112. #endif
  113. Radio.Rx( 0 ); // Continuous Rx
  114. while( 1 )
  115. {
  116. BoardLowPowerHandler( );
  117. }
  118. }
  119. void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
  120. {
  121. static uint8_t ledState = 1;
  122. // Toggle LED 4
  123. ledState ^= 1;
  124. GpioWrite( &Led4, ledState );
  125. }