sx1250_usb.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. a/*
  2. / _____) _ | |
  3. ( (____ _____ ____ _| |_ _____ ____| |__
  4. \____ \| ___ | (_ _) ___ |/ ___) _ \
  5. _____) ) ____| | | || |_| ____( (___| | | |
  6. (______/|_____)_|_|_| \__)_____)\____)_| |_|
  7. (C)2019 Semtech
  8. Description:
  9. Functions used to handle LoRa concentrator SX1250 radios.
  10. License: Revised BSD License, see LICENSE.TXT file include in the project
  11. */
  12. /* -------------------------------------------------------------------------- */
  13. /* --- DEPENDANCIES --------------------------------------------------------- */
  14. #include <stdint.h> /* C99 types */
  15. #include <stdio.h> /* printf fprintf */
  16. #include <string.h> /* memcmp */
  17. #include "loragw_aux.h"
  18. #include "loragw_mcu.h"
  19. #include "sx1250_usb.h"
  20. /* -------------------------------------------------------------------------- */
  21. /* --- PRIVATE MACROS ------------------------------------------------------- */
  22. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  23. #if DEBUG_RAD == 1
  24. #define DEBUG_MSG(str) fprintf(stdout, str)
  25. #define DEBUG_PRINTF(fmt, args...) fprintf(stdout,"%s:%d: "fmt, __FUNCTION__, __LINE__, args)
  26. #define CHECK_NULL(a) if(a==NULL){fprintf(stderr,"%s:%d: ERROR: NULL POINTER AS ARGUMENT\n", __FUNCTION__, __LINE__);return -1;}
  27. #else
  28. #define DEBUG_MSG(str)
  29. #define DEBUG_PRINTF(fmt, args...)
  30. #define CHECK_NULL(a) if(a==NULL){return -1;}
  31. #endif
  32. /* -------------------------------------------------------------------------- */
  33. /* --- PRIVATE CONSTANTS ---------------------------------------------------- */
  34. #define WAIT_BUSY_SX1250_MS 1
  35. /* -------------------------------------------------------------------------- */
  36. /* --- PUBLIC FUNCTIONS DEFINITION ------------------------------------------ */
  37. int sx1250_usb_w(void *com_target, uint8_t spi_mux_target, sx1250_op_code_t op_code, uint8_t *data, uint16_t size) {
  38. int usb_device;
  39. uint8_t command_size = size + 7; /* 5 bytes: REQ metadata, 2 bytes: RAW SPI frame */
  40. uint8_t in_out_buf[command_size];
  41. int a;
  42. int i;
  43. /* check input variables */
  44. CHECK_NULL(com_target);
  45. CHECK_NULL(data);
  46. usb_device = *(int *)com_target;
  47. /* wait BUSY */
  48. wait_ms(WAIT_BUSY_SX1250_MS);
  49. /* prepare command */
  50. /* Request metadata */
  51. in_out_buf[0] = 0; /* Req ID */
  52. in_out_buf[1] = MCU_SPI_REQ_TYPE_READ_WRITE; /* Req type */
  53. in_out_buf[2] = MCU_SPI_TARGET_SX1302; /* MCU -> SX1302 */
  54. in_out_buf[3] = (uint8_t)((size + 2) >> 8); /* payload size + spi_mux_target + op_code */
  55. in_out_buf[4] = (uint8_t)((size + 2) >> 0); /* payload size + spi_mux_target + op_code */
  56. /* RAW SPI frame */
  57. in_out_buf[5] = spi_mux_target; /* SX1302 -> RADIO_A or RADIO_B */
  58. in_out_buf[6] = (uint8_t)op_code;
  59. for (i = 0; i < size; i++) {
  60. in_out_buf[i + 7] = data[i];
  61. }
  62. a = mcu_spi_write(usb_device, in_out_buf, command_size);
  63. /* determine return code */
  64. if (a != 0) {
  65. DEBUG_MSG("ERROR: USB SX1250 WRITE FAILURE\n");
  66. return -1;
  67. } else {
  68. DEBUG_MSG("Note: USB SX1250 write success\n");
  69. return 0;
  70. }
  71. }
  72. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  73. int sx1250_usb_r(void *com_target, uint8_t spi_mux_target, sx1250_op_code_t op_code, uint8_t *data, uint16_t size) {
  74. int usb_device;
  75. uint8_t command_size = size + 7; /* 5 bytes: REQ metadata, 2 bytes: RAW SPI frame */
  76. uint8_t in_out_buf[command_size];
  77. int a;
  78. int i;
  79. /* check input variables */
  80. CHECK_NULL(com_target);
  81. CHECK_NULL(data);
  82. usb_device = *(int *)com_target;
  83. /* wait BUSY */
  84. wait_ms(WAIT_BUSY_SX1250_MS);
  85. /* prepare command */
  86. /* Request metadata */
  87. in_out_buf[0] = 0; /* Req ID */
  88. in_out_buf[1] = MCU_SPI_REQ_TYPE_READ_WRITE; /* Req type */
  89. in_out_buf[2] = MCU_SPI_TARGET_SX1302; /* MCU -> SX1302 */
  90. in_out_buf[3] = (uint8_t)((size + 2) >> 8); /* payload size + spi_mux_target + op_code */
  91. in_out_buf[4] = (uint8_t)((size + 2) >> 0); /* payload size + spi_mux_target + op_code */
  92. /* RAW SPI frame */
  93. in_out_buf[5] = spi_mux_target; /* SX1302 -> RADIO_A or RADIO_B */
  94. in_out_buf[6] = (uint8_t)op_code;
  95. for (i = 0; i < size; i++) {
  96. in_out_buf[i + 7] = data[i];
  97. }
  98. a = mcu_spi_write(usb_device, in_out_buf, command_size);
  99. /* determine return code */
  100. if (a != 0) {
  101. DEBUG_MSG("ERROR: USB SX1250 READ FAILURE\n");
  102. return -1;
  103. } else {
  104. DEBUG_MSG("Note: USB SX1250 read success\n");
  105. memcpy(data, in_out_buf + 7, size); /* remove the first bytes, keep only the payload */
  106. return 0;
  107. }
  108. }
  109. /* --- EOF ------------------------------------------------------------------ */