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.
 
 
 
 
 
 

143 lines
2.7 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. Radio.SetRxConfig(MODEM_LORA, 0/*bandwidth*/, 7/*datarate*/, 1/*coderate*/, 0/*afc*/, 8/*preamblen*/, 5/*symTimeout*/, 0/*fixLen*/, 0/*payloadlen*/, 1/*crcOn*/, 0/*freqHop*/, 0/*hopPeriod*/, false/*iqInverted*/, true/*rxcont*/);
  89. Radio.SetTxConfig(MODEM_LORA, 11/*power*/, 0/*fdev*/, 0/*bandwidth*/, 7/*datarate*/, 1/*coderate*/, 8/*preamblen*/, 0/*fixLen*/, 1/*crcOn*/, 0/*freqHop*/, 0/*hopPeriod*/, false/*iqInverted*/, 1000/*timeout*/);
  90. uint8_t sendmsg[] = "testing lora123";
  91. usb_printf("sending...\r\n");
  92. Radio.Send(sendmsg, sizeof sendmsg);
  93. DelayMs(200);
  94. Radio.Send(sendmsg, sizeof sendmsg);
  95. DelayMs(200);
  96. Radio.Send(sendmsg, sizeof sendmsg);
  97. DelayMs(200);
  98. usb_printf("rx(0)...\r\n");
  99. Radio.Rx(0);
  100. loop:
  101. BoardLowPowerHandler();
  102. if (Radio.IrqProcess != NULL)
  103. Radio.IrqProcess();
  104. goto loop;
  105. }