Johny Mattsson 2187424928 Imported RTC+sleep timekeeping from an internal DiUS project.
Added Lua module rtctime to interface with it.

This keeps as accurate time as is possible on the ESP8266, including across
deep sleeps (provided rtctime.dsleep() is used rather than node.dsleep()).
Intended to be used together with NTP for high accuracy time keeping. The
API is via rtctime.{get,set}timeofday(), working from Unix epoch.

Note that 160MHz CPU clock is not currently supported by the rtctime code,
as it is only aware of the 52MHz boot clock and the regular 80Mhz default
clock.

See rtctime.h for detailed info on how this all works.
2015-07-10 15:12:31 +10:00

60 lines
1.2 KiB
C

#ifndef RTC_ACCESS_H
#define RTC_ACCESS_H
#include <c_types.h>
#define RTC_MMIO_BASE 0x60000700
#define RTC_USER_MEM_BASE 0x60001200
#define RTC_USER_MEM_NUM_DWORDS 128
#define RTC_TARGET_ADDR 0x04
#define RTC_COUNTER_ADDR 0x1c
#define EARLY_ENTRY_ATTR __attribute__((section(".text")))
static inline uint32_t EARLY_ENTRY_ATTR rtc_mem_read(uint32_t addr)
{
return ((uint32_t*)RTC_USER_MEM_BASE)[addr];
}
static inline void EARLY_ENTRY_ATTR rtc_mem_write(uint32_t addr, uint32_t val)
{
((uint32_t*)RTC_USER_MEM_BASE)[addr]=val;
}
static inline uint64_t rtc_make64(uint32_t high, uint32_t low)
{
return (((uint64_t)high)<<32)|low;
}
static inline uint64_t rtc_mem_read64(uint32_t addr)
{
return rtc_make64(rtc_mem_read(addr+1),rtc_mem_read(addr));
}
static inline void rtc_mem_write64(uint32_t addr, uint64_t val)
{
rtc_mem_write(addr+1,val>>32);
rtc_mem_write(addr,val&0xffffffff);
}
static inline void rtc_memw(void)
{
asm volatile ("memw");
}
static inline void rtc_reg_write(uint32_t addr, uint32_t val)
{
rtc_memw();
addr+=RTC_MMIO_BASE;
*((volatile uint32_t*)addr)=val;
rtc_memw();
}
static inline uint32_t rtc_reg_read(uint32_t addr)
{
addr+=RTC_MMIO_BASE;
rtc_memw();
return *((volatile uint32_t*)addr);
}
#endif