sx1276-Hal.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * THE FOLLOWING FIRMWARE IS PROVIDED: (1) "AS IS" WITH NO WARRANTY; AND
  3. * (2)TO ENABLE ACCESS TO CODING INFORMATION TO GUIDE AND FACILITATE CUSTOMER.
  4. * CONSEQUENTLY, SEMTECH SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
  5. * CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
  6. * OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
  7. * CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  8. *
  9. * Copyright (C) SEMTECH S.A.
  10. */
  11. /*!
  12. * \file sx1276-Hal.c
  13. * \brief SX1276 Hardware Abstraction Layer
  14. *
  15. * \version 2.0.B2
  16. * \date Nov 21 2012
  17. * \author Miguel Luis
  18. *
  19. * Last modified by Miguel Luis on Jun 19 2013
  20. */
  21. #include <stdint.h>
  22. #include <stdbool.h>
  23. #include "platform.h"
  24. #if defined( USE_SX1276_RADIO )
  25. #include "spi.h"
  26. #include "sx1276-Hal.h"
  27. /*!
  28. * SX1276 RESET I/O definitions
  29. */
  30. #define RESET_IOPORT GPIOB
  31. #define RESET_PIN GPIO_Pin_11
  32. /*!
  33. * SX1276 DIO pins I/O definitions
  34. */
  35. #define DIO0_IOPORT GPIOG
  36. #define DIO0_PIN GPIO_Pin_6
  37. void SX1276InitIo( void )
  38. {
  39. RCC_AHB1PeriphClockCmd ( RCC_AHB1Periph_GPIOB, ENABLE);
  40. // Configure RESET PIN as output
  41. GPIO_InitTypeDef GPIO_InitStruct;
  42. GPIO_InitStruct.GPIO_Pin = RESET_PIN;
  43. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  44. GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
  45. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
  46. GPIO_Init(RESET_IOPORT, &GPIO_InitStruct);
  47. //DIO0_PIN.mode(INPUT);
  48. RCC_AHB1PeriphClockCmd ( RCC_AHB1Periph_GPIOG, ENABLE);
  49. GPIO_InitStruct.GPIO_Pin = DIO0_PIN;
  50. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
  51. GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_NOPULL;
  52. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  53. GPIO_Init(DIO0_IOPORT, &GPIO_InitStruct);
  54. spi_config();
  55. }
  56. void SX1276SetReset( uint8_t state )
  57. {
  58. GPIO_WriteBit( RESET_IOPORT, RESET_PIN,(BitAction) state );
  59. }
  60. void SX1276Write( uint8_t addr, uint8_t data )
  61. {
  62. SX1276WriteBuffer( addr, &data, 1 );
  63. }
  64. void SX1276Read( uint8_t addr, uint8_t *data )
  65. {
  66. SX1276ReadBuffer( addr, data, 1 );
  67. }
  68. void SX1276WriteBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
  69. {
  70. uint8_t i;
  71. //NSS = 0;
  72. SpiNSSEnable(0);
  73. SPI2_ReadWriteByte( addr | 0x80 );
  74. for( i = 0; i < size; i++ )
  75. {
  76. SPI2_ReadWriteByte( buffer[i] );
  77. }
  78. //NSS = 1;
  79. SpiNSSEnable(1);
  80. }
  81. void SX1276ReadBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
  82. {
  83. uint8_t i;
  84. //NSS = 0;
  85. SpiNSSEnable(0);
  86. SPI2_ReadWriteByte( addr & 0x7F );
  87. for( i = 0; i < size; i++ )
  88. {
  89. buffer[i] = SPI2_ReadWriteByte( 0 );
  90. }
  91. //NSS = 1;
  92. SpiNSSEnable( 1 );
  93. }
  94. void SX1276WriteFifo( uint8_t *buffer, uint8_t size )
  95. {
  96. SX1276WriteBuffer( 0, buffer, size );
  97. }
  98. void SX1276ReadFifo( uint8_t *buffer, uint8_t size )
  99. {
  100. SX1276ReadBuffer( 0, buffer, size );
  101. }
  102. inline uint8_t SX1276ReadDio0( void )
  103. {
  104. return GPIO_ReadInputDataBit( DIO0_IOPORT, DIO0_PIN );
  105. }
  106. inline uint8_t SX1276ReadDio1( void )
  107. {
  108. return 1;
  109. }
  110. inline uint8_t SX1276ReadDio2( void )
  111. {
  112. return 1;
  113. }
  114. inline uint8_t SX1276ReadDio3( void )
  115. {
  116. return 1;
  117. }
  118. inline uint8_t SX1276ReadDio4( void )
  119. {
  120. return 1;
  121. }
  122. inline uint8_t SX1276ReadDio5( void )
  123. {
  124. return 1;
  125. }
  126. inline void SX1276WriteRxTx( uint8_t txEnable )
  127. {
  128. // if( txEnable != 0 )
  129. // {
  130. // IoePinOn( FEM_CTX_PIN );
  131. // IoePinOff( FEM_CPS_PIN );
  132. // }
  133. // else
  134. // {
  135. // IoePinOff( FEM_CTX_PIN );
  136. // IoePinOn( FEM_CPS_PIN );
  137. // }
  138. }
  139. #endif // USE_SX1276_RADIO