Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

350 rindas
8.9 KiB

  1. /*
  2. / _____) _ | |
  3. ( (____ _____ ____ _| |_ _____ ____| |__
  4. \____ \| ___ | (_ _) ___ |/ ___) _ \
  5. _____) ) ____| | | || |_| ____( (___| | | |
  6. (______/|_____)_|_|_| \__)_____)\____)_| |_|
  7. (C) 2014 Semtech
  8. Description: -
  9. License: Revised BSD License, see LICENSE.TXT file include in the project
  10. Maintainers: Miguel Luis, Gregory Cristian and Nicolas Huguenin
  11. */
  12. #include "sx1276-hal.h"
  13. const RadioRegisters_t SX1276MB1xAS::RadioRegsInit[] = RADIO_INIT_REGISTERS_VALUE;
  14. SX1276MB1xAS::SX1276MB1xAS( RadioEvents_t *events,
  15. PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset,
  16. PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5,
  17. PinName antSwitch )
  18. : SX1276( events, mosi, miso, sclk, nss, reset, dio0, dio1, dio2, dio3, dio4, dio5 ),
  19. AntSwitch( antSwitch ),
  20. #if( defined ( TARGET_NUCLEO_L152RE ) )
  21. Fake( D8 )
  22. #else
  23. Fake( A3 )
  24. #endif
  25. {
  26. this->RadioEvents = events;
  27. Reset( );
  28. RxChainCalibration( );
  29. IoInit( );
  30. SetOpMode( RF_OPMODE_SLEEP );
  31. IoIrqInit( dioIrq );
  32. RadioRegistersInit( );
  33. SetModem( MODEM_FSK );
  34. this->settings.State = RF_IDLE ;
  35. }
  36. SX1276MB1xAS::SX1276MB1xAS( RadioEvents_t *events )
  37. #if defined ( TARGET_NUCLEO_L152RE )
  38. : SX1276( events, D11, D12, D13, D10, A0, D2, D3, D4, D5, A3, D9 ), // For NUCLEO L152RE dio4 is on port A3
  39. AntSwitch( A4 ),
  40. Fake( D8 )
  41. #elif defined( TARGET_LPC11U6X )
  42. : SX1276( events, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ),
  43. AntSwitch( P0_23 ),
  44. Fake( A3 )
  45. #else
  46. : SX1276( events, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ),
  47. AntSwitch( A4 ),
  48. Fake( A3 )
  49. #endif
  50. {
  51. this->RadioEvents = events;
  52. Reset( );
  53. boardConnected = UNKNOWN;
  54. DetectBoardType( );
  55. RxChainCalibration( );
  56. IoInit( );
  57. SetOpMode( RF_OPMODE_SLEEP );
  58. IoIrqInit( dioIrq );
  59. RadioRegistersInit( );
  60. SetModem( MODEM_FSK );
  61. this->settings.State = RF_IDLE ;
  62. }
  63. //-------------------------------------------------------------------------
  64. // Board relative functions
  65. //-------------------------------------------------------------------------
  66. uint8_t SX1276MB1xAS::DetectBoardType( void )
  67. {
  68. if( boardConnected == UNKNOWN )
  69. {
  70. this->AntSwitch.input( );
  71. wait_ms( 1 );
  72. if( this->AntSwitch == 1 )
  73. {
  74. boardConnected = SX1276MB1LAS;
  75. }
  76. else
  77. {
  78. boardConnected = SX1276MB1MAS;
  79. }
  80. this->AntSwitch.output( );
  81. wait_ms( 1 );
  82. }
  83. return ( boardConnected );
  84. }
  85. void SX1276MB1xAS::IoInit( void )
  86. {
  87. AntSwInit( );
  88. SpiInit( );
  89. }
  90. void SX1276MB1xAS::RadioRegistersInit( )
  91. {
  92. uint8_t i = 0;
  93. for( i = 0; i < sizeof( RadioRegsInit ) / sizeof( RadioRegisters_t ); i++ )
  94. {
  95. SetModem( RadioRegsInit[i].Modem );
  96. Write( RadioRegsInit[i].Addr, RadioRegsInit[i].Value );
  97. }
  98. }
  99. void SX1276MB1xAS::SpiInit( void )
  100. {
  101. nss = 1;
  102. spi.format( 8,0 );
  103. uint32_t frequencyToSet = 8000000;
  104. #if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_LPC11U6X ) )
  105. spi.frequency( frequencyToSet );
  106. #elif( defined ( TARGET_KL25Z ) ) //busclock frequency is halved -> double the spi frequency to compensate
  107. spi.frequency( frequencyToSet * 2 );
  108. #else
  109. #warning "Check the board's SPI frequency"
  110. #endif
  111. wait(0.1);
  112. }
  113. void SX1276MB1xAS::IoIrqInit( DioIrqHandler *irqHandlers )
  114. {
  115. #if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_LPC11U6X ) )
  116. dio0.mode( PullDown );
  117. dio1.mode( PullDown );
  118. dio2.mode( PullDown );
  119. dio3.mode( PullDown );
  120. dio4.mode( PullDown );
  121. #endif
  122. dio0.rise( mbed::callback( this, static_cast< TriggerMB1xAS > ( irqHandlers[0] ) ) );
  123. dio1.rise( mbed::callback( this, static_cast< TriggerMB1xAS > ( irqHandlers[1] ) ) );
  124. dio2.rise( mbed::callback( this, static_cast< TriggerMB1xAS > ( irqHandlers[2] ) ) );
  125. dio3.rise( mbed::callback( this, static_cast< TriggerMB1xAS > ( irqHandlers[3] ) ) );
  126. dio4.rise( mbed::callback( this, static_cast< TriggerMB1xAS > ( irqHandlers[4] ) ) );
  127. }
  128. void SX1276MB1xAS::IoDeInit( void )
  129. {
  130. //nothing
  131. }
  132. void SX1276MB1xAS::SetRfTxPower( int8_t power )
  133. {
  134. uint8_t paConfig = 0;
  135. uint8_t paDac = 0;
  136. paConfig = Read( REG_PACONFIG );
  137. paDac = Read( REG_PADAC );
  138. paConfig = ( paConfig & RF_PACONFIG_PASELECT_MASK ) | GetPaSelect( this->settings.Channel );
  139. paConfig = ( paConfig & RF_PACONFIG_MAX_POWER_MASK ) | 0x70;
  140. if( ( paConfig & RF_PACONFIG_PASELECT_PABOOST ) == RF_PACONFIG_PASELECT_PABOOST )
  141. {
  142. if( power > 17 )
  143. {
  144. paDac = ( paDac & RF_PADAC_20DBM_MASK ) | RF_PADAC_20DBM_ON;
  145. }
  146. else
  147. {
  148. paDac = ( paDac & RF_PADAC_20DBM_MASK ) | RF_PADAC_20DBM_OFF;
  149. }
  150. if( ( paDac & RF_PADAC_20DBM_ON ) == RF_PADAC_20DBM_ON )
  151. {
  152. if( power < 5 )
  153. {
  154. power = 5;
  155. }
  156. if( power > 20 )
  157. {
  158. power = 20;
  159. }
  160. paConfig = ( paConfig & RF_PACONFIG_OUTPUTPOWER_MASK ) | ( uint8_t )( ( uint16_t )( power - 5 ) & 0x0F );
  161. }
  162. else
  163. {
  164. if( power < 2 )
  165. {
  166. power = 2;
  167. }
  168. if( power > 17 )
  169. {
  170. power = 17;
  171. }
  172. paConfig = ( paConfig & RF_PACONFIG_OUTPUTPOWER_MASK ) | ( uint8_t )( ( uint16_t )( power - 2 ) & 0x0F );
  173. }
  174. }
  175. else
  176. {
  177. if( power < -1 )
  178. {
  179. power = -1;
  180. }
  181. if( power > 14 )
  182. {
  183. power = 14;
  184. }
  185. paConfig = ( paConfig & RF_PACONFIG_OUTPUTPOWER_MASK ) | ( uint8_t )( ( uint16_t )( power + 1 ) & 0x0F );
  186. }
  187. Write( REG_PACONFIG, paConfig );
  188. Write( REG_PADAC, paDac );
  189. }
  190. uint8_t SX1276MB1xAS::GetPaSelect( uint32_t channel )
  191. {
  192. if( channel > RF_MID_BAND_THRESH )
  193. {
  194. if( boardConnected == SX1276MB1LAS )
  195. {
  196. return RF_PACONFIG_PASELECT_PABOOST;
  197. }
  198. else
  199. {
  200. return RF_PACONFIG_PASELECT_RFO;
  201. }
  202. }
  203. else
  204. {
  205. return RF_PACONFIG_PASELECT_RFO;
  206. }
  207. }
  208. void SX1276MB1xAS::SetAntSwLowPower( bool status )
  209. {
  210. if( isRadioActive != status )
  211. {
  212. isRadioActive = status;
  213. if( status == false )
  214. {
  215. AntSwInit( );
  216. }
  217. else
  218. {
  219. AntSwDeInit( );
  220. }
  221. }
  222. }
  223. void SX1276MB1xAS::AntSwInit( void )
  224. {
  225. this->AntSwitch = 0;
  226. }
  227. void SX1276MB1xAS::AntSwDeInit( void )
  228. {
  229. this->AntSwitch = 0;
  230. }
  231. void SX1276MB1xAS::SetAntSw( uint8_t opMode )
  232. {
  233. switch( opMode )
  234. {
  235. case RFLR_OPMODE_TRANSMITTER:
  236. this->AntSwitch = 1;
  237. break;
  238. case RFLR_OPMODE_RECEIVER:
  239. case RFLR_OPMODE_RECEIVER_SINGLE:
  240. case RFLR_OPMODE_CAD:
  241. this->AntSwitch = 0;
  242. break;
  243. default:
  244. this->AntSwitch = 0;
  245. break;
  246. }
  247. }
  248. bool SX1276MB1xAS::CheckRfFrequency( uint32_t frequency )
  249. {
  250. // Implement check. Currently all frequencies are supported
  251. return true;
  252. }
  253. void SX1276MB1xAS::Reset( void )
  254. {
  255. reset.output( );
  256. reset = 0;
  257. wait_ms( 1 );
  258. reset.input( );
  259. wait_ms( 6 );
  260. }
  261. void SX1276MB1xAS::Write( uint8_t addr, uint8_t data )
  262. {
  263. Write( addr, &data, 1 );
  264. }
  265. uint8_t SX1276MB1xAS::Read( uint8_t addr )
  266. {
  267. uint8_t data;
  268. Read( addr, &data, 1 );
  269. return data;
  270. }
  271. void SX1276MB1xAS::Write( uint8_t addr, uint8_t *buffer, uint8_t size )
  272. {
  273. uint8_t i;
  274. nss = 0;
  275. spi.write( addr | 0x80 );
  276. for( i = 0; i < size; i++ )
  277. {
  278. spi.write( buffer[i] );
  279. }
  280. nss = 1;
  281. }
  282. void SX1276MB1xAS::Read( uint8_t addr, uint8_t *buffer, uint8_t size )
  283. {
  284. uint8_t i;
  285. nss = 0;
  286. spi.write( addr & 0x7F );
  287. for( i = 0; i < size; i++ )
  288. {
  289. buffer[i] = spi.write( 0 );
  290. }
  291. nss = 1;
  292. }
  293. void SX1276MB1xAS::WriteFifo( uint8_t *buffer, uint8_t size )
  294. {
  295. Write( 0, buffer, size );
  296. }
  297. void SX1276MB1xAS::ReadFifo( uint8_t *buffer, uint8_t size )
  298. {
  299. Read( 0, buffer, size );
  300. }