LoRa Gateway Time Request

In my continuing series about programming WisBlock RAK 11310 devices using the native RP2040 SDK and the LoRaMac-node library, this article focuses on retrieving the current date and time from the LoRa gateway and setting the device real time clock.

In addition to sending sensor readings to a central server via LoRaWan, the devices that I am building need to be able to be read in the field and display the most recently acquired data along with the actual date and time of that occurrence. The real time clock cannot be set manually so that time must come from the gateway, which has the accurate time acquired via its GPS. The LoRa protocol includes MAC commands DeviceTimeReq/DeviceTimeAns. This is supported in ChirpStack for LoRaWan MAC version 1.03.

The Semtech LoRaMac-node reference project implements most of the required interface for sending and receiving the device time requests in the LmHandler.c function LmHandlerDeviceTimeReq. The response containing the time from the gateway is in LoRaMac.c in the ProcessMacCommands function where the SRV_MAC_DEVICE_TIME_ANS message is handled. So small additions are needed to provide an interface to trigger the device time request and some processing of the returned Unix time. The various pico-lorawan adaptations use an intermediate file lorawan.c to provide interfaces to the Semtech library. I have added an interface in lorawan.c called lorawa_get_time which simply calls LmHandlerGetTime.

When the Unix time is received in ProcessMacCommands I write the received time to the RAK12002 RTC module which contains a Micro Crystal RV-3028-C7 RTC chip and supercapacitor for battery backup (see my article WisBlock RTC). The RV-3028 RTC has separate registers for calendar time and Unix time but they are not coordinated (setting one does not set the other) so I write to both.

The structure of the LoRaMac-node library provides header files for hardware components that can be implemented in whatever hardware platform you are using. Of course the original did not support the RP2040 or RV-3028 but it is easy enough to add them. The rtc-board.h file does not specify reading or writing time to the RTC so I added interfaces to read and write both the calendar and Unix times. The rtc-board.c file is simply an interface to the RV2038.c file.

The RV-3028 charges the supercapacitor on the RAK12002 RTC module with a trickle charge but by default this is not enabled. It is important to enable it by setting bit 5 of the EEPROM_BACKUP register (address 0x37). I do this in the rtc-board.c RtcInit function.

Source code is in my Git repository here.


Posted

in

by

Tags:

Comments

Leave a Reply