1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
  ______                              _
 / _____)             _              | |
( (____  _____ ____ _| |_ _____  ____| |__
 \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 _____) ) ____| | | || |_| ____( (___| | | |
(______/|_____)_|_|_| \__)_____)\____)_| |_|
    (C)2013 Semtech
 
Description: SX126x driver specific target board functions implementation
 
License: Revised BSD License, see LICENSE.TXT file include in the project
 
Maintainer: Miguel Luis and Gregory Cristian
*/
//#include "stm32f0xx.h"
#include "delay.h"
//#include "gpio.h"
//#include "Spi.h"
//#include "spi.h"
#include "radio.h"
#include "sx126x.h"
#include "sx126x-board.h"
#include "lora_1268.h"
#include "PCA9555.h"
//#include "main.h"
//#include "stm32l0xx_hal_spi.h"
#include "mk_spi.h"
//extern SPI_HandleTypeDef hspi1;
 
 
//void HAL_Delay_nMS( uint32_t Delay )
//{
//    uint32_t tickstart = 0;
//    tickstart = HAL_GetTick( );
//    while( ( HAL_GetTick( ) - tickstart ) < Delay );
//}
//void SX126x_CS_Di( )
//{
//     if(!HAL_GPIO_ReadPin( SPIx_CS_GPIO, SPIx_CS))
//     {
//        HAL_GPIO_WritePin( SPIx_CS_GPIO, SPIx_CS,GPIO_PIN_SET);
//         printf("uwb_cs_error\r\n");
//     }
 
//}
void spi_transfer_callback(void *dev, uint32_t err_code)
{
 
}
static void spi_receive_callback(void *dev, uint32_t err_code)
{
 
}
/*!
 * @brief Sends txBuffer and receives rxBuffer
 *
 * @param [IN] txBuffer Byte to be sent
 * @param [OUT] rxBuffer Byte to be sent
 * @param [IN] size Byte to be sent
 */
uint8_t SpiInOut( uint8_t txBuffer)
{
      uint8_t rxData = 0;      
    spi_transfer(SPI_ID0, &txBuffer,&rxData, 1, spi_transfer_callback);                                  
    return( rxData ); 
}
 
 
 
void SX126xReset( void )
{
    HAL_Delay_nMS( 10 );
//    HAL_GPIO_WritePin( RADIO_nRESET_GPIO_Port, RADIO_nRESET_Pin,GPIO_PIN_RESET);
//    gpio_pin_clr(LORA_NRST);
      LORA_NRST_DOWN;
    delay_us( 2000 );
//    HAL_GPIO_WritePin( RADIO_nRESET_GPIO_Port, RADIO_nRESET_Pin,GPIO_PIN_SET);
//    gpio_pin_set(LORA_NRST);
      LORA_NRST_UP;
    HAL_Delay_nMS( 10 );
}
 
void SX126xWaitOnBusy( void )
{
//   while(HAL_GPIO_ReadPin(RADIO_BUSY_GPIO_Port,RADIO_BUSY_Pin)==GPIO_PIN_SET);
   while(gpio_pin_get_val(LORA_BUSY)==1);
}
 
 
void SX126xWakeup( void )
{
 
    gpio_pin_clr(LORA_CS);
    SpiInOut(RADIO_GET_STATUS);
    SpiInOut(0);
    
    gpio_pin_set(LORA_CS);
    // Wait for chip to be ready.
    SX126xWaitOnBusy( );
}
 
void SX126xWriteCommand( RadioCommands_t command, uint8_t *buffer, uint16_t size )
{
 
    SX126xCheckDeviceReady( );
    gpio_pin_clr(LORA_CS);
    SpiInOut(( uint8_t )command );
 
    for( uint16_t i = 0; i < size; i++ )
    {
        SpiInOut(buffer[i] );
    }
        gpio_pin_set(LORA_CS);
    
    if( command != RADIO_SET_SLEEP )
    {
        SX126xWaitOnBusy( );
    }
}
 
void SX126xReadCommand( RadioCommands_t command, uint8_t *buffer, uint16_t size )
{
    SX126xCheckDeviceReady( );
      gpio_pin_clr(LORA_CS);
//    SX126x_CS_Di( );
//    HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_RESET);
 
    SpiInOut(( uint8_t )command );
    SpiInOut(0x00 );
    for( uint16_t i = 0; i < size; i++ )
    {
        buffer[i] = SpiInOut(0 );
    }
        gpio_pin_set(LORA_CS);
//    HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_SET);
 
    SX126xWaitOnBusy( );
}
 
void SX126xWriteRegisters( uint16_t address, uint8_t *buffer, uint16_t size )
{
    SX126xCheckDeviceReady( );
//    SX126x_CS_Di( );
//    HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_RESET);
    gpio_pin_clr(LORA_CS);    
    SpiInOut(RADIO_WRITE_REGISTER );
    SpiInOut(( address & 0xFF00 ) >> 8 );
    SpiInOut( address & 0x00FF );
    
    for( uint16_t i = 0; i < size; i++ )
    {
        SpiInOut(buffer[i] );
    }
 
        gpio_pin_set(LORA_CS);
//    HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_SET);
 
    SX126xWaitOnBusy( );
}
 
void SX126xWriteRegister( uint16_t address, uint8_t value )
{
    SX126xWriteRegisters( address, &value, 1 );
}
 
void SX126xReadRegisters( uint16_t address, uint8_t *buffer, uint16_t size )
{
    SX126xCheckDeviceReady( );
//    SX126x_CS_Di( );
//    HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_RESET);
        gpio_pin_clr(LORA_CS);
    SpiInOut(RADIO_READ_REGISTER );
    SpiInOut(( address & 0xFF00 ) >> 8 );
    SpiInOut( address & 0x00FF );
    SpiInOut( 0 );
    for( uint16_t i = 0; i < size; i++ )
    {
        buffer[i] = SpiInOut(0 );
    }
        gpio_pin_set(LORA_CS);
//    HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_SET);
 
    SX126xWaitOnBusy( );
}
 
uint8_t SX126xReadRegister( uint16_t address )
{
    uint8_t data;
    SX126xReadRegisters( address, &data, 1 );
    return data;
}
 
void SX126xWriteBuffer( uint8_t offset, uint8_t *buffer, uint8_t size )
{
    SX126xCheckDeviceReady( );
//    SX126x_CS_Di( );
//    HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_RESET);
    gpio_pin_clr(LORA_CS);
    SpiInOut( RADIO_WRITE_BUFFER );
    SpiInOut( offset );
    for( uint16_t i = 0; i < size; i++ )
    {
        SpiInOut( buffer[i] );
    }
        gpio_pin_set(LORA_CS);
//    HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_SET);
 
    SX126xWaitOnBusy( );
}
 
void SX126xReadBuffer( uint8_t offset, uint8_t *buffer, uint8_t size )
{
    SX126xCheckDeviceReady( );
//    SX126x_CS_Di( );
//    HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_RESET);
    gpio_pin_clr(LORA_CS);
    SpiInOut(  RADIO_READ_BUFFER );
    SpiInOut(  offset );
    SpiInOut(  0 );
    for( uint16_t i = 0; i < size; i++ )
    {
        buffer[i] = SpiInOut( 0 );
    }
 
//    HAL_GPIO_WritePin( RADIO_NSS_GPIO_Port, RADIO_NSS_Pin,GPIO_PIN_SET);
    gpio_pin_set(LORA_CS);    
    SX126xWaitOnBusy( );
}
 
void SX126xSetRfTxPower( int8_t power )
{
    SX126xSetTxParams( power, RADIO_RAMP_40_US );
}
 
uint8_t SX126xGetPaSelect( uint32_t channel )
{
//    if( GpioRead( &DeviceSel ) == 1 )
//    {
//        return SX1261;
//    }
//    else
//    {
//        return SX1262;
//    }
  
  return SX1262;
}
 
void SX126xAntSwOn( void )
{
    //GpioInit( &AntPow, ANT_SWITCH_POWER, PIN_OUTPUT, PIN_PUSH_PULL, PIN_PULL_UP, 1 );
}
 
void SX126xAntSwOff( void )
{
   // GpioInit( &AntPow, ANT_SWITCH_POWER, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
}
 
bool SX126xCheckRfFrequency( uint32_t frequency )
{
    // Implement check. Currently all frequencies are supported
    return true;
}