sx1276-Hal.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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_OType=GPIO_OType_PP;
  45. GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
  46. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
  47. GPIO_Init(RESET_IOPORT, &GPIO_InitStruct);
  48. //DIO0_PIN.mode(INPUT);
  49. RCC_AHB1PeriphClockCmd ( RCC_AHB1Periph_GPIOG, ENABLE);
  50. GPIO_InitStruct.GPIO_Pin = DIO0_PIN;
  51. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
  52. GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_NOPULL;
  53. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  54. GPIO_Init(DIO0_IOPORT, &GPIO_InitStruct);
  55. spi_config();
  56. }
  57. void SX1276SetReset( uint8_t state )
  58. {
  59. GPIO_WriteBit( RESET_IOPORT, RESET_PIN,(BitAction) state );
  60. }
  61. void SX1276Write( uint8_t addr, uint8_t data )
  62. {
  63. SX1276WriteBuffer( addr, &data, 1 );
  64. }
  65. void SX1276Read( uint8_t addr, uint8_t *data )
  66. {
  67. SX1276ReadBuffer( addr, data, 1 );
  68. }
  69. void SX1276WriteBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
  70. {
  71. uint8_t i;
  72. //NSS = 0;
  73. SpiNSSEnable(0);
  74. SPI2_ReadWriteByte( addr | 0x80 );
  75. for( i = 0; i < size; i++ )
  76. {
  77. SPI2_ReadWriteByte( buffer[i] );
  78. }
  79. //NSS = 1;
  80. SpiNSSEnable(1);
  81. }
  82. void SX1276ReadBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
  83. {
  84. uint8_t i;
  85. //NSS = 0;
  86. SpiNSSEnable(0);
  87. SPI2_ReadWriteByte( addr & 0x7F );
  88. for( i = 0; i < size; i++ )
  89. {
  90. buffer[i] = SPI2_ReadWriteByte( 0 );
  91. }
  92. //NSS = 1;
  93. SpiNSSEnable( 1 );
  94. }
  95. void SX1276WriteFifo( uint8_t *buffer, uint8_t size )
  96. {
  97. SX1276WriteBuffer( 0, buffer, size );
  98. }
  99. void SX1276ReadFifo( uint8_t *buffer, uint8_t size )
  100. {
  101. SX1276ReadBuffer( 0, buffer, size );
  102. }
  103. inline uint8_t SX1276ReadDio0( void )
  104. {
  105. return GPIO_ReadInputDataBit( DIO0_IOPORT, DIO0_PIN );
  106. }
  107. inline uint8_t SX1276ReadDio1( void )
  108. {
  109. return 1;
  110. }
  111. inline uint8_t SX1276ReadDio2( void )
  112. {
  113. return 1;
  114. }
  115. inline uint8_t SX1276ReadDio3( void )
  116. {
  117. return 1;
  118. }
  119. inline uint8_t SX1276ReadDio4( void )
  120. {
  121. return 1;
  122. }
  123. inline uint8_t SX1276ReadDio5( void )
  124. {
  125. return 1;
  126. }
  127. inline void SX1276WriteRxTx( uint8_t txEnable )
  128. {
  129. // if( txEnable != 0 )
  130. // {
  131. // IoePinOn( FEM_CTX_PIN );
  132. // IoePinOff( FEM_CPS_PIN );
  133. // }
  134. // else
  135. // {
  136. // IoePinOff( FEM_CTX_PIN );
  137. // IoePinOn( FEM_CPS_PIN );
  138. // }
  139. }
  140. #endif // USE_SX1276_RADIO