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.
 
 
 
 
 
 

158 lines
3.0 KiB

  1. #include <usbd_cdc_if.h>
  2. #include <strobe_rng_init.h>
  3. #include <board.h>
  4. #include <misc.h>
  5. #include <radio.h>
  6. #include <delay.h>
  7. void
  8. hexdump(uint8_t *ptr, size_t len)
  9. {
  10. int i;
  11. for (i = 0; i < len; i++)
  12. usb_printf("%02x", ptr[i]);
  13. }
  14. void
  15. txdone(void)
  16. {
  17. usb_printf("txdone\r\n");
  18. }
  19. void
  20. txtimeout(void)
  21. {
  22. usb_printf("txtimeout\r\n");
  23. }
  24. void
  25. rxdone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
  26. {
  27. usb_printf("rxdone: size: %hu, rssi: %hd, snr: %d\r\ndata: ", size, rssi, snr);
  28. hexdump(payload, size);
  29. usb_printf("\r\n");
  30. }
  31. void
  32. rxtimeout(void)
  33. {
  34. usb_printf("rxtimeout\r\n");
  35. }
  36. void
  37. rxerr(void)
  38. {
  39. usb_printf("rxerr\r\n");
  40. }
  41. RadioEvents_t revents = {
  42. .TxDone = txdone,
  43. .TxTimeout = txtimeout,
  44. .RxDone = rxdone,
  45. .RxTimeout = rxtimeout,
  46. .RxError = rxerr,
  47. };
  48. int
  49. main(void)
  50. {
  51. uint8_t bytes[8];
  52. strobe_rng_init();
  53. BoardInitMcu();
  54. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
  55. #if 1
  56. wait_for_vcp();
  57. usb_printf("starting...\r\n");
  58. bare_strobe_randomize(bytes, sizeof bytes);
  59. hexdump(bytes, sizeof bytes);
  60. usb_printf("\r\n");
  61. #endif
  62. Radio.Init(&revents);
  63. usb_printf("foo\r\n");
  64. Radio.ReadBuffer(0x00, bytes, sizeof bytes);
  65. usb_printf("regs 0x00: ");
  66. hexdump(bytes, sizeof bytes);
  67. usb_printf("\r\n");
  68. Radio.ReadBuffer(0x40, bytes, sizeof bytes);
  69. usb_printf("regs 0x40: ");
  70. hexdump(bytes, sizeof bytes);
  71. usb_printf("\r\n");
  72. uint32_t v;
  73. v = Radio.Random();
  74. usb_printf("rr: %#x\r\n", v);
  75. v = Radio.Random();
  76. usb_printf("rr: %#x\r\n", v);
  77. v = Radio.Random();
  78. usb_printf("rr: %#x\r\n", v);
  79. usb_printf("gs: %#x\r\n", Radio.GetStatus());
  80. usb_printf("set modem\r\n", v);
  81. Radio.SetModem(MODEM_LORA);
  82. usb_printf("check rffreq: %d\r\n", (int)Radio.CheckRfFrequency(914350 * 1000));
  83. usb_printf("set channel\r\n", v);
  84. Radio.SetChannel(914350 * 1000);
  85. v = Radio.Random();
  86. usb_printf("rr: %#x\r\n", v);
  87. usb_printf("rssi: %#hx\r\n", Radio.Rssi(MODEM_LORA));
  88. /* RX/TX parameters */
  89. const uint8_t modem = MODEM_LORA;
  90. const uint8_t bandwidth = 0 /* 128 kHz */;
  91. const uint8_t datarate = 7 /* 128 chips */;
  92. const uint8_t coderate = 1 /* 4/5 */;
  93. const uint8_t preambleLen = 4 /* symbols */;
  94. const uint8_t fixLen = 0 /* variable */;
  95. const uint8_t crcOn = 1 /* on */;
  96. const uint8_t freqHopOn = 0 /* off */;
  97. const bool iqInverted = false /* not inverted */;
  98. Radio.SetRxConfig(modem, bandwidth, datarate, coderate, 0/*afc*/,
  99. preambleLen, 5/*symTimeout*/, fixLen, 0/*payloadlen*/, crcOn,
  100. freqHopOn, 0/*hopPeriod*/, iqInverted, true/*rxcont*/);
  101. Radio.SetTxConfig(modem, 11/*power*/, 0/*fdev*/, bandwidth, datarate,
  102. coderate, preambleLen, fixLen, crcOn, freqHopOn, 0/*hopPeriod*/,
  103. iqInverted, 1000/*timeout*/);
  104. uint8_t sendmsg[] = "testing lora123";
  105. usb_printf("sending...\r\n");
  106. Radio.Send(sendmsg, sizeof sendmsg);
  107. DelayMs(200);
  108. Radio.Send(sendmsg, sizeof sendmsg);
  109. DelayMs(200);
  110. Radio.Send(sendmsg, sizeof sendmsg);
  111. DelayMs(200);
  112. usb_printf("rx(0)...\r\n");
  113. Radio.Rx(0);
  114. loop:
  115. BoardLowPowerHandler();
  116. if (Radio.IrqProcess != NULL)
  117. Radio.IrqProcess();
  118. goto loop;
  119. }