Imported RTC access from a DiUS internal project.

Added Lua access module named "rtcmem" for read/write of RTC user module.
This commit is contained in:
Johny Mattsson 2015-06-25 12:26:54 +10:00
parent 67d785ffec
commit 1a613effeb
4 changed files with 129 additions and 1 deletions

View File

@ -0,0 +1,58 @@
#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
static inline uint32_t rtc_mem_read(uint32_t addr)
{
return ((uint32_t*)RTC_USER_MEM_BASE)[addr];
}
static inline void 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

View File

@ -35,6 +35,7 @@
#define LUA_USE_MODULES_CRYPTO
#define LUA_USE_MODULES_RC
#define LUA_USE_MODULES_DHT
#define LUA_USE_MODULES_RTCMEM
#endif /* LUA_USE_MODULES */

View File

@ -181,6 +181,14 @@
#define ROM_MODULES_DHT
#endif
#if defined(LUA_USE_MODULES_RTCMEM)
#define MODULES_RTCMEM "rtcmem"
#define ROM_MODULES_RTCMEM \
_ROM(MODULES_RTCMEM, luaopen_rtcmem, rtcmem_map)
#else
#define ROM_MODULES_RTCMEM
#endif
#define LUA_MODULES_ROM \
ROM_MODULES_GPIO \
ROM_MODULES_PWM \
@ -203,6 +211,7 @@
ROM_MODULES_CJSON \
ROM_MODULES_CRYPTO \
ROM_MODULES_RC \
ROM_MODULES_DHT
ROM_MODULES_DHT \
ROM_MODULES_RTCMEM \
#endif

60
app/modules/rtcmem.c Normal file
View File

@ -0,0 +1,60 @@
// Module for RTC user memory access
#include "lauxlib.h"
#include "rtc/rtcaccess.h"
static int rtcmem_read32 (lua_State *L)
{
int idx = luaL_checknumber (L, 1);
int n = 1;
if (lua_isnumber (L, 2))
n = lua_tonumber (L, 2);
if (!lua_checkstack (L, n))
return 0;
int ret = 0;
while (n > 0 && idx >= 0 && idx < RTC_USER_MEM_NUM_DWORDS)
{
lua_pushinteger (L, rtc_mem_read (idx++));
--n;
++ret;
}
return ret;
}
static int rtcmem_write32 (lua_State *L)
{
int idx = luaL_checknumber (L, 1);
int n = lua_gettop (L) - 1;
luaL_argcheck (
L, idx + n <= RTC_USER_MEM_NUM_DWORDS, 1, "RTC mem would overrun");
int src = 2;
while (n-- > 0)
{
rtc_mem_write (idx++, lua_tonumber (L, src++));
}
return 0;
}
// Module function map
#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
const LUA_REG_TYPE rtcmem_map[] =
{
{ LSTRKEY("read32"), LFUNCVAL(rtcmem_read32) },
{ LSTRKEY("write32"), LFUNCVAL(rtcmem_write32) },
{ LNILKEY, LNILVAL }
};
LUALIB_API int luaopen_rtcmem (lua_State *L)
{
#if LUA_OPTIMIZE_MEMORY > 0
return 0;
#else
luaL_register (L, AUXLIB_RTCMEM, rtcmem_map);
return 1;
#endif
}