diff --git a/boards/known/xmc4500-hexagon.lua b/boards/known/xmc4500-hexagon.lua index 1f21b56c..2d8382ae 100644 --- a/boards/known/xmc4500-hexagon.lua +++ b/boards/known/xmc4500-hexagon.lua @@ -20,6 +20,7 @@ return { sercon = { uart = 0, speed = 115200 }, xmc45_pot = true, xmc45_dts = true, + xmc45_rtc = true, wofs = false, romfs = true, mmcfs = { spi = 0, cs_port = 0, cs_pin = 0 }, diff --git a/src/platform/xmc4000/build_config.lua b/src/platform/xmc4000/build_config.lua index 5cb88deb..5f20c3c6 100644 --- a/src/platform/xmc4000/build_config.lua +++ b/src/platform/xmc4000/build_config.lua @@ -21,6 +21,7 @@ function add_platform_components( t, board, cpu ) if board == 'XMC4500-HEXAGON' then t.xmc45_pot = { macro = 'ENABLE_POTENTIOMETER' } t.xmc45_dts = { macro = 'ENABLE_DTS' } + t.xmc45_rtc = { macro = 'ENABLE_RTC' } end end @@ -36,6 +37,7 @@ function get_platform_modules( board, cpu ) if board == 'XMC4500-HEXAGON' then m.pot = { guards = { 'ENABLE_POTENTIOMETER' }, lib = '"pot"', open = false } m.dts = { guards = { 'ENABLE_DTS' }, lib = '"dts"', open = false } + m.rtc = { guards = { 'ENABLE_RTC' }, lib = '"rtc"', open = false } end return m end diff --git a/src/platform/xmc4000/conf.lua b/src/platform/xmc4000/conf.lua index e7bf6e1e..57973b28 100755 --- a/src/platform/xmc4000/conf.lua +++ b/src/platform/xmc4000/conf.lua @@ -1,7 +1,7 @@ -- Configuration file for the XMC4500 microcontroller -specific_files = "platform.c startup_XMC4500.S system_XMC4500.c xmclib/src/xmc4_eru.c xmclib/src/xmc4_flash.c xmclib/src/xmc4_gpio.c xmclib/src/xmc4_rtc.c xmclib/src/xmc4_scu.c xmclib/src/xmc_can.c xmclib/src/xmc_ccu4.c xmclib/src/xmc_ccu8.c xmclib/src/xmc_common.c xmclib/src/xmc_dac.c xmclib/src/xmc_dma.c xmclib/src/xmc_dsd.c xmclib/src/xmc_ebu.c xmclib/src/xmc_ecat.c xmclib/src/xmc_eru.c xmclib/src/xmc_eth_mac.c xmclib/src/xmc_fce.c xmclib/src/xmc_gpio.c xmclib/src/xmc_hrpwm.c xmclib/src/xmc_i2c.c xmclib/src/xmc_i2s.c xmclib/src/xmc_ledts.c xmclib/src/xmc_posif.c xmclib/src/xmc_rtc.c xmclib/src/xmc_sdmmc.c xmclib/src/xmc_spi.c xmclib/src/xmc_uart.c xmclib/src/xmc_usbd.c xmclib/src/xmc_usic.c xmclib/src/xmc_vadc.c xmclib/src/xmc_wdt.c xmclib/gen/CLOCK_XMC4/clock_xmc4.c xmclib/gen/CLOCK_XMC4/clock_xmc4_conf.c xmclib/gen/UART/uart.c xmclib/gen/UART/uart_conf.c xmclib/gen/CPU_CTRL_XMC4/cpu_ctrl_xmc4.c xmclib/gen/CPU_CTRL_XMC4/cpu_ctrl_xmc4_conf.c xmclib/gen/FATFS/fatfs.c xmclib/gen/FATFS/fatfs_conf.c xmclib/gen/FATFS/sltha.c xmclib/gen/RTC/rtc.c xmclib/gen/RTC/rtc_conf.c xmclib/gen/SDMMC_BLOCK/sdmmc_block_private_sd.c xmclib/gen/SDMMC_BLOCK/sdmmc_block.c xmclib/gen/SDMMC_BLOCK/sdmmc_block_conf.c xmclib/gen/SYSTIMER/systimer.c xmclib/gen/SYSTIMER/systimer_conf.c xmclib/gen/DAVE.c pot.c dts.c" +specific_files = "platform.c startup_XMC4500.S system_XMC4500.c xmclib/src/xmc4_eru.c xmclib/src/xmc4_flash.c xmclib/src/xmc4_gpio.c xmclib/src/xmc4_rtc.c xmclib/src/xmc4_scu.c xmclib/src/xmc_can.c xmclib/src/xmc_ccu4.c xmclib/src/xmc_ccu8.c xmclib/src/xmc_common.c xmclib/src/xmc_dac.c xmclib/src/xmc_dma.c xmclib/src/xmc_dsd.c xmclib/src/xmc_ebu.c xmclib/src/xmc_ecat.c xmclib/src/xmc_eru.c xmclib/src/xmc_eth_mac.c xmclib/src/xmc_fce.c xmclib/src/xmc_gpio.c xmclib/src/xmc_hrpwm.c xmclib/src/xmc_i2c.c xmclib/src/xmc_i2s.c xmclib/src/xmc_ledts.c xmclib/src/xmc_posif.c xmclib/src/xmc_rtc.c xmclib/src/xmc_sdmmc.c xmclib/src/xmc_spi.c xmclib/src/xmc_uart.c xmclib/src/xmc_usbd.c xmclib/src/xmc_usic.c xmclib/src/xmc_vadc.c xmclib/src/xmc_wdt.c xmclib/gen/CLOCK_XMC4/clock_xmc4.c xmclib/gen/CLOCK_XMC4/clock_xmc4_conf.c xmclib/gen/UART/uart.c xmclib/gen/UART/uart_conf.c xmclib/gen/CPU_CTRL_XMC4/cpu_ctrl_xmc4.c xmclib/gen/CPU_CTRL_XMC4/cpu_ctrl_xmc4_conf.c xmclib/gen/FATFS/fatfs.c xmclib/gen/FATFS/fatfs_conf.c xmclib/gen/FATFS/sltha.c xmclib/gen/RTC/rtc.c xmclib/gen/RTC/rtc_conf.c xmclib/gen/SDMMC_BLOCK/sdmmc_block_private_sd.c xmclib/gen/SDMMC_BLOCK/sdmmc_block.c xmclib/gen/SDMMC_BLOCK/sdmmc_block_conf.c xmclib/gen/SYSTIMER/systimer.c xmclib/gen/SYSTIMER/systimer_conf.c xmclib/gen/DAVE.c pot.c dts.c rtc.c" local ldscript = "" local cpu = comp.cpu:upper() diff --git a/src/platform/xmc4000/rtc.c b/src/platform/xmc4000/rtc.c new file mode 100644 index 00000000..0947aef9 --- /dev/null +++ b/src/platform/xmc4000/rtc.c @@ -0,0 +1,162 @@ + +// eLua module for XMC4500's hardware RTC module. Code taken from +// Mizar2's RTC implementation and adapted for the XMC4500. + +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" +#include "platform.h" +#include "lrotable.h" +//#include "lrodefs.h" +#include "platform_conf.h" +#include "DAVE.h" + +/* + * All defines here + */ + +// The number of fields in the above tables +#define NFIELDS 6 + +// The order offsets of the fields in the DS1337 register set +#define SEC 0 +#define MIN 1 +#define HOUR 2 +#define WDAY 3 +#define DAY 4 +#define MONTH 5 +#define YEAR 6 + +// The names of the Lua time table elements to return, in the order they are +// in the DS1337 register set. +static const char * const fieldnames[] = { + "sec", "min", "hour", "day", "month", "year" +}; + +static const u16 minval[] = { + 0, 0, 0, 1, 1, 1, 1900 +}; + +static const u16 maxval[] = { + 59, 59, 23, 7, 31, 12, 2099 +}; + +/* + * Functions start here + */ + +// Initialize the RTC module +static int rtc_init( lua_State *L ) { + RTC_Init(&RTC_0); + return 0; +} + +// Get elapsed number of seconds +static int rtc_elapsed( lua_State *L ) { + time_t t; + + RTC_Time(&t); + lua_pushinteger(L, (lua_Integer)t); + return 1; +} + +// Get elapsed number of seconds +static int rtc_status( lua_State *L ) { + lua_pushinteger(L, (lua_Integer)RTC_GetFlagStatus()); + return 1; +} + +// Read the time from the RTC. +static int rtc_get( lua_State *L ) { + XMC_RTC_TIME_t t; + + // Get the time. + RTC_GetTime( &t ); + + // Construct the table to return the result + lua_createtable( L, 0, 6 ); + + lua_pushstring( L, "sec" ); + lua_pushinteger( L, t.seconds ); + lua_rawset( L, -3 ); + + lua_pushstring( L, "min" ); + lua_pushinteger( L, t.minutes ); + lua_rawset( L, -3 ); + + lua_pushstring( L, "hour" ); + lua_pushinteger( L, t.hours ); + lua_rawset( L, -3 ); + + lua_pushstring( L, "day" ); + lua_pushinteger( L, t.days ); + lua_rawset( L, -3 ); + + lua_pushstring( L, "month" ); + lua_pushinteger( L, t.month ); + lua_rawset( L, -3 ); + + lua_pushstring( L, "year" ); + lua_pushinteger( L, t.year ); + lua_rawset( L, -3 ); + + return 1; +} + +// xmc4000.rtc.set() +// Parameter is a table containing fields with the usual Lua time +// field names. Missing elements are not set and remain the same as +// they were. +static int rtc_set( lua_State *L ) { + lua_Integer value; + int field; // Which field are we handling (0-5) + XMC_RTC_TIME_t t; + + // Set any values that they specified as table entries + for( field = 0; field < NFIELDS; field++ ) { + lua_getfield( L, 1, fieldnames[field] ); + switch( lua_type( L, -1 ) ) { + case LUA_TNIL: + // Do not set unspecified fields + break; + + case LUA_TNUMBER: + case LUA_TSTRING: + value = lua_tointeger( L, -1 ); + if (value < minval[field] || value > maxval[field]) + return luaL_error( L, "Time value out of range" ); + + // Special cases for some fields + switch( field ) { + case SEC: t.seconds = value; break; + case MIN: t.minutes = value; break; + case HOUR: t.hours = value; break; + case DAY: t.days = value; break; + case MONTH: t.month = value; break; + case YEAR: t.year = value; break; + }; + break; + default: + return luaL_error( L, "Time values must be numbers" ); + }; + + lua_pop( L, 1 ); + } + + RTC_SetTime( &t ); + + return 0; +} + +#define MIN_OPT_LEVEL 2 +#include "lrodefs.h" + +// xmc4000.rtc.*() module function map +const LUA_REG_TYPE rtc_map[] = { + { LSTRKEY( "init" ), LFUNCVAL( rtc_init ) }, + { LSTRKEY( "get" ), LFUNCVAL( rtc_get ) }, + { LSTRKEY( "set" ), LFUNCVAL( rtc_set ) }, + { LSTRKEY( "elapsed" ), LFUNCVAL( rtc_elapsed ) }, + { LSTRKEY( "status" ), LFUNCVAL( rtc_status ) }, + { LNILKEY, LNILVAL } +};