sx126x-board.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. ______ _
  3. / _____) _ | |
  4. ( (____ _____ ____ _| |_ _____ ____| |__
  5. \____ \| ___ | (_ _) ___ |/ ___) _ \
  6. _____) ) ____| | | || |_| ____( (___| | | |
  7. (______/|_____)_|_|_| \__)_____)\____)_| |_|
  8. (C)2013 Semtech
  9. Description: SX126x driver specific target board functions implementation
  10. License: Revised BSD License, see LICENSE.TXT file include in the project
  11. Maintainer: Miguel Luis and Gregory Cristian
  12. */
  13. #include "radio.h"
  14. #include "sx126x.h"
  15. #include "sx126x-board.h"
  16. uint8_t SX126xGetIrqFired2( lora_device_t* lora_device ){
  17. return luat_gpio_get(lora_device->lora_pin_dio1);
  18. }
  19. void SX126xDelayMs2(uint32_t ms){
  20. luat_timer_mdelay(ms);
  21. }
  22. void SX126xReset2( lora_device_t* lora_device ){
  23. SX126xDelayMs2(10);
  24. luat_gpio_set( lora_device->lora_pin_rst, 0 );
  25. SX126xDelayMs2(20);
  26. luat_gpio_set( lora_device->lora_pin_rst, 1 );
  27. SX126xDelayMs2(10);
  28. }
  29. void SX126xWaitOnBusy2( lora_device_t* lora_device ){
  30. while(luat_gpio_get(lora_device->lora_pin_busy)==1){
  31. SX126xDelayMs2(1);
  32. }
  33. }
  34. void SX126xSetNss2(lora_device_t* lora_device,uint8_t lev ){
  35. luat_gpio_set( lora_device->lora_pin_cs, lev);
  36. }
  37. void lora_spi_transfer(lora_device_t* lora_device, const uint8_t* send_buf, size_t send_length, uint8_t* recv_buf, size_t recv_length){
  38. if (lora_device->lora_spi_id == 255){
  39. luat_spi_device_transfer(lora_device->lora_spi_device, (const char *)send_buf, send_length, (char *)recv_buf, recv_length);
  40. }else{
  41. SX126xSetNss2(lora_device,0);
  42. // luat_spi_transfer(lora_device->lora_spi_id, send_buf, send_length, recv_buf, recv_length);
  43. if (send_length){
  44. luat_spi_send(lora_device->lora_spi_id, (const char *)send_buf, send_length);
  45. }
  46. if (recv_length){
  47. luat_spi_recv(lora_device->lora_spi_id, (char *)recv_buf, recv_length);
  48. }
  49. SX126xSetNss2(lora_device,1);
  50. }
  51. }
  52. void SX126xWakeup2( lora_device_t* lora_device){
  53. uint8_t cmd[2] = {RADIO_GET_STATUS,0x00};
  54. lora_spi_transfer(lora_device, cmd, 2,NULL,0);
  55. // Wait for chip to be ready.
  56. SX126xWaitOnBusy2(lora_device );
  57. }
  58. void SX126xWriteCommand2( lora_device_t* lora_device,RadioCommands_t command, uint8_t *buffer, uint16_t size ){
  59. uint16_t i = 0;
  60. SX126xCheckDeviceReady2( lora_device );
  61. uint8_t cmd[1+size];
  62. cmd[0] = (uint8_t)command;
  63. memcpy(cmd+1,buffer,size);
  64. lora_spi_transfer(lora_device, cmd, 1+size,NULL,0);
  65. if( command != RADIO_SET_SLEEP ){
  66. SX126xWaitOnBusy2(lora_device );
  67. }
  68. }
  69. void SX126xReadCommand2( lora_device_t* lora_device,RadioCommands_t command, uint8_t *buffer, uint16_t size ){
  70. uint16_t i = 0;
  71. SX126xCheckDeviceReady2( lora_device);
  72. uint8_t cmd[2] = {(uint8_t)command,0x00};
  73. lora_spi_transfer(lora_device, cmd, 2,buffer,size);
  74. SX126xWaitOnBusy2(lora_device );
  75. }
  76. void SX126xWriteRegister2s2(lora_device_t* lora_device, uint16_t address, uint8_t *buffer, uint16_t size ){
  77. uint16_t i = 0;
  78. SX126xCheckDeviceReady2( lora_device );
  79. uint8_t cmd[3+size];
  80. cmd[0] = RADIO_WRITE_REGISTER;
  81. cmd[1] = (address & 0xFF00 ) >> 8;
  82. cmd[2] = address & 0x00FF;
  83. memcpy(cmd+3,buffer,size);
  84. lora_spi_transfer(lora_device, cmd, 3+size,NULL,0);
  85. SX126xWaitOnBusy2( lora_device);
  86. }
  87. void SX126xWriteRegister2( lora_device_t* lora_device,uint16_t address, uint8_t value ){
  88. SX126xWriteRegister2s2( lora_device,address, &value, 1 );
  89. }
  90. void SX126xReadRegister2s2( lora_device_t* lora_device,uint16_t address, uint8_t *buffer, uint16_t size ){
  91. uint16_t i = 0;
  92. SX126xCheckDeviceReady2(lora_device );
  93. uint8_t cmd[4] = {RADIO_READ_REGISTER,( address & 0xFF00 ) >> 8,address & 0x00FF,0x00};
  94. lora_spi_transfer(lora_device, cmd, 4,buffer,size);
  95. SX126xWaitOnBusy2( lora_device);
  96. }
  97. uint8_t SX126xReadRegister2( lora_device_t* lora_device,uint16_t address ){
  98. uint8_t data;
  99. SX126xReadRegister2s2( lora_device,address, &data, 1 );
  100. return data;
  101. }
  102. void SX126xWriteBuffer2( lora_device_t* lora_device,uint8_t offset, uint8_t *buffer, uint8_t size ){
  103. uint16_t i = 0;
  104. SX126xCheckDeviceReady2( lora_device);
  105. uint8_t cmd[2+size];
  106. cmd[0] = RADIO_WRITE_BUFFER;
  107. cmd[1] = offset;
  108. memcpy(cmd+2,buffer,size);
  109. luat_spi_send(lora_device->lora_spi_id, (const char *)cmd, 2+size);
  110. lora_spi_transfer(lora_device, cmd, 2+size,NULL,0);
  111. SX126xWaitOnBusy2(lora_device );
  112. }
  113. void SX126xReadBuffer2(lora_device_t* lora_device, uint8_t offset, uint8_t *buffer, uint8_t size ){
  114. uint16_t i = 0;
  115. SX126xCheckDeviceReady2(lora_device);
  116. uint8_t cmd[3] = {RADIO_READ_BUFFER,offset,0x00};
  117. lora_spi_transfer(lora_device, cmd, 3,buffer,size);
  118. SX126xWaitOnBusy2(lora_device );
  119. }
  120. void SX126xSetRfTxPower2(lora_device_t* lora_device, int8_t power ){
  121. SX126xSetTx2Params2( lora_device,power, RADIO_RAMP_40_US );
  122. }
  123. uint8_t SX126xGetPaSelect2(lora_device_t* lora_device, uint32_t channel ){
  124. return SX1262;
  125. }
  126. void SX126xAntSwOn2( lora_device_t* lora_device ){
  127. }
  128. void SX126xAntSwOff2( lora_device_t* lora_device ){
  129. }
  130. bool SX126xCheckRfFrequency2( lora_device_t* lora_device,uint32_t frequency ){
  131. // Implement check. Currently all frequencies are supported
  132. return true;
  133. }