Skip to content

Commit 47293e9

Browse files
committed
Implement SX126x RTC functions in LoRaMac stack for SX1262 device
1 parent f1675b9 commit 47293e9

File tree

2 files changed

+71
-11
lines changed

2 files changed

+71
-11
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ idf_component_register(SRCS ${COMPONENT_SRCS}
6262
include_directories(
6363
$ENV{IDF_PATH}/components/driver/gpio/include
6464
$ENV{IDF_PATH}/components/driver/spi/include
65+
$ENV{IDF_PATH}/components/esp_timer/include
6566
)
6667

6768
add_definitions(-DREGION_EU868=1)

src/board/generic_esp32/generic-esp32-board.c

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "freertos/FreeRTOS.h"
55
#include "esp_err.h"
66
#include "esp_log.h"
7+
#include "esp_timer.h"
78

89
#include "driver/gpio.h"
910
#include "driver/spi_common.h"
@@ -13,9 +14,11 @@
1314

1415
// from LoRaMac
1516
#include "board.h"
17+
#include "timer.h"
1618

1719
// ANSI
1820
#include <string.h>
21+
#include <sys/time.h>
1922

2023
#define ESP_INTR_FLAG_DEFAULT 0
2124

@@ -25,6 +28,10 @@
2528

2629
#define SX1262_MAX_SPI_CLOCK_SPEED_MHZ 16
2730

31+
#define MS_PER_S 1000
32+
#define US_PER_MS 1000
33+
#define US_PER_S (MS_PER_S * US_PER_MS)
34+
2835
typedef struct spi_s {
2936
spi_device_handle_t handle;
3037
gpio_num_t miso;
@@ -41,16 +48,28 @@ typedef struct spi_s {
4148
static spi_c lora_spi;
4249

4350
static const char *TAG = "ESP32Board";
44-
4551
static portMUX_TYPE my_spinlock = portMUX_INITIALIZER_UNLOCKED;
4652

53+
static esp_timer_handle_t irq_timer_handle;
54+
static uint32_t timer_context_ticks;
55+
static uint32_t current_timeout_ticks;
56+
57+
static void IrqTimerExpiryCallback(void* arg) {
58+
TimerIrqHandler();
59+
}
60+
4761
void BoardInitMcu( void )
4862
{
4963
}
5064

5165
void BoardInitPeriph(void)
5266
{
53-
esp_err_t ret;
67+
// Create timer
68+
const esp_timer_create_args_t timer_args = {
69+
.callback = &IrqTimerExpiryCallback,
70+
.name = "SX1262 IRQ Timer"
71+
};
72+
ESP_ERROR_CHECK(esp_timer_create(&timer_args, &irq_timer_handle));
5473

5574
lora_spi.miso = CONFIG_LORAWAN_SPI_MISO_GPIO;
5675
lora_spi.mosi = CONFIG_LORAWAN_SPI_MOSI_GPIO;
@@ -250,8 +269,12 @@ void RtcBkupRead( uint32_t *data0, uint32_t *data1 )
250269
*/
251270
uint32_t RtcGetCalendarTime( uint16_t *milliseconds )
252271
{
253-
*milliseconds = 29;
254-
return 42 /* seconds */;
272+
int64_t time_us = esp_timer_get_time();
273+
uint32_t time_s = (uint32_t)(time_us / US_PER_S);
274+
275+
*milliseconds = (uint16_t)(time_us * US_PER_MS);
276+
277+
return time_s;
255278
}
256279

257280
/*!
@@ -271,7 +294,16 @@ uint32_t RtcGetMinimumTimeout( void )
271294
*/
272295
uint32_t RtcGetTimerElapsedTime( void )
273296
{
274-
return 0;
297+
uint64_t expiry_time;
298+
int64_t current_time;
299+
uint32_t time_remaining_ticks, elapsed_time;
300+
301+
ESP_ERROR_CHECK(esp_timer_get_expiry_time(irq_timer_handle, &expiry_time));
302+
current_time = esp_timer_get_time();
303+
time_remaining_ticks = (uint32_t)(pdMS_TO_TICKS((expiry_time - current_time) / US_PER_MS));
304+
elapsed_time = current_timeout_ticks - time_remaining_ticks;
305+
306+
return elapsed_time;
275307
}
276308

277309
/*!
@@ -281,7 +313,9 @@ uint32_t RtcGetTimerElapsedTime( void )
281313
*/
282314
uint32_t RtcGetTimerValue( void )
283315
{
284-
return 0;
316+
uint32_t timer_value_ticks = (uint32_t)(esp_timer_get_time() / US_PER_MS);
317+
318+
return timer_value_ticks;
285319
}
286320

287321
/*!
@@ -292,7 +326,7 @@ uint32_t RtcGetTimerValue( void )
292326
*/
293327
uint32_t RtcMs2Tick( TimerTime_t milliseconds )
294328
{
295-
return 0;
329+
return pdMS_TO_TICKS(milliseconds);
296330
}
297331

298332

@@ -304,7 +338,7 @@ uint32_t RtcMs2Tick( TimerTime_t milliseconds )
304338
*/
305339
TimerTime_t RtcTick2Ms( uint32_t tick )
306340
{
307-
return 0;
341+
return pdTICKS_TO_MS(tick);
308342
}
309343

310344

@@ -315,7 +349,21 @@ TimerTime_t RtcTick2Ms( uint32_t tick )
315349
*/
316350
uint32_t RtcSetTimerContext( void )
317351
{
318-
return 0;
352+
int64_t timer_val_ms = esp_timer_get_time() / US_PER_MS;
353+
354+
timer_context_ticks = (uint32_t)(pdMS_TO_TICKS(timer_val_ms));
355+
356+
return timer_context_ticks;
357+
}
358+
359+
360+
/*!
361+
* \brief Gets the RTC timer reference
362+
*
363+
* \retval value Timer reference value in ticks
364+
*/
365+
uint32_t RtcGetTimerContext( void ) {
366+
return timer_context_ticks;
319367
}
320368

321369

@@ -328,15 +376,26 @@ uint32_t RtcSetTimerContext( void )
328376
*/
329377
void RtcSetAlarm( uint32_t timeout )
330378
{
331-
379+
uint32_t timeout_us = pdTICKS_TO_MS(timeout) * US_PER_MS;
380+
381+
current_timeout_ticks = timeout;
382+
383+
if (esp_timer_is_active(irq_timer_handle)) {
384+
ESP_ERROR_CHECK(esp_timer_restart(irq_timer_handle, timeout_us));
385+
}
386+
else {
387+
ESP_ERROR_CHECK(esp_timer_start_once(irq_timer_handle, timeout_us));
388+
}
332389
}
333390

334391
/*!
335392
* \brief Stops the Alarm
336393
*/
337394
void RtcStopAlarm( void )
338395
{
339-
396+
if (esp_timer_is_active(irq_timer_handle)) {
397+
ESP_ERROR_CHECK(esp_timer_stop(irq_timer_handle));
398+
}
340399
}
341400

342401

0 commit comments

Comments
 (0)