Skip to content

Commit

Permalink
Merge pull request #460 from kaluma-project/rtc
Browse files Browse the repository at this point in the history
Rtc
  • Loading branch information
niklauslee authored Dec 15, 2021
2 parents e85b315 + dc930dc commit db5cd54
Show file tree
Hide file tree
Showing 15 changed files with 286 additions and 13 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
Kaluma is a tiny and efficient JavaScript runtime for microcontrollers. The main features are:

- Small footprint (Run minimally on 300KB ROM, 64KB RAM)
- ECMAScript 5.1/6+ (subset) standard compliant (Powered by [JerryScript](http://jerryscript.net/))
- Non-blocking I/O
- [APIs](https://docs.kaluma.io/) like Node.js and Arduino
- Support REPL mode
- ECMAScript 5/6/6+ (subset) standard compliant (Powered by [JerryScript](http://jerryscript.net/))
- Easy to port
- Non-blocking I/O
- [API](https://docs.kaluma.io/) similar with Node.js and Arduino
- REPL mode
- File systems (LittleFS)
- Graphics API
- Networking (Socket, HTTP)

# Supported targets

- **RP2040**
- [Raspberry Pi Pico](https://www.raspberrypi.org/products/raspberry-pi-pico/)
- Support boards based on RP2040
- Supports PIO (Programmable I/O)
- Dormant mode for low-power
- Support PIO (Programmable I/O)
- Support RTC
- Low-power (dormant mode)

# Resources

Expand Down
41 changes: 41 additions & 0 deletions include/port/rtc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* Copyright (c) 2017 Kaluma
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#ifndef __KM_RTC_H
#define __KM_RTC_H

#include <stdbool.h>
#include <stdint.h>

void km_rtc_init();
void km_rtc_cleanup();

/**
* Set RTC to the number of milliseconds since the Unix Epoch
*/
void km_rtc_set_time(uint64_t time);

/**
* Returns the number of milliseconds since the Unix Epoch
*/
uint64_t km_rtc_get_time();

#endif /* __KM_RTC_H */
11 changes: 5 additions & 6 deletions src/jerry_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "jerryscript-ext/handler.h"
#include "jerryscript-port.h"
#include "jerryscript.h"
#include "rtc.h"
#include "tty.h"

/**
* Aborts the program.
*/
Expand Down Expand Up @@ -55,13 +58,9 @@ double jerry_port_get_local_time_zone_adjustment(double unix_ms, bool is_utc) {
}

/**
* Dummy function to get the current time.
*
* @return 0
* function to get the current time.
*/
double jerry_port_get_current_time(void) {
return 0;
} /* jerry_port_get_current_time */
double jerry_port_get_current_time(void) { return (double)km_rtc_get_time(); }

/**
* Opens file with the given path and reads its source.
Expand Down
2 changes: 2 additions & 0 deletions src/modules/rtc/module.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
list(APPEND SOURCES ${SRC_DIR}/modules/rtc/module_rtc.c)
include_directories(${SRC_DIR}/modules/rtc)
5 changes: 5 additions & 0 deletions src/modules/rtc/module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": true,
"js": false,
"native": true
}
61 changes: 61 additions & 0 deletions src/modules/rtc/module_rtc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* Copyright (c) 2017 Kaluma
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <stdlib.h>

#include "jerryscript.h"
#include "jerryxx.h"
#include "rtc.h"
#include "rtc_magic_strings.h"

/**
* rtc.setTime(t)
* args:
* time: {number}
*/
JERRYXX_FUN(rtc_set_time_fn) {
// check and get args
JERRYXX_CHECK_ARG_NUMBER(0, "time")
double time = JERRYXX_GET_ARG_NUMBER(0);
km_rtc_set_time((uint64_t)time);
return jerry_create_undefined();
}

/**
* rtc.getTime(t)
* returns:
* {number}
*/
JERRYXX_FUN(rtc_get_time_fn) {
uint64_t time = km_rtc_get_time();
return jerry_create_number((double)time);
}

/**
* Initialize 'rtc' module
*/
jerry_value_t module_rtc_init() {
/* rtc module exports */
jerry_value_t exports = jerry_create_object();
jerryxx_set_property_function(exports, MSTR_RTC_SET_TIME, rtc_set_time_fn);
jerryxx_set_property_function(exports, MSTR_RTC_GET_TIME, rtc_get_time_fn);
return exports;
}
24 changes: 24 additions & 0 deletions src/modules/rtc/module_rtc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Copyright (c) 2017 Kaluma
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include "jerryscript.h"

jerry_value_t module_rtc_init();
29 changes: 29 additions & 0 deletions src/modules/rtc/rtc_magic_strings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Copyright (c) 2017 Kaluma
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#ifndef __RTC_MAGIC_STRINGS_H
#define __RTC_MAGIC_STRINGS_H

#define MSTR_RTC_RTC "rtc"
#define MSTR_RTC_GET_TIME "getTime"
#define MSTR_RTC_SET_TIME "setTime"

#endif /* __RTC_MAGIC_STRINGS_H */
30 changes: 30 additions & 0 deletions targets/linux/src/rtc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* Copyright (c) 2017 Kaluma
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include "rtc.h"

void km_rtc_init() {}

void km_rtc_cleanup() {}

void km_rtc_set_time(uint64_t time) {}

uint64_t km_rtc_get_time() { return (uint64_t)0; }
3 changes: 3 additions & 0 deletions targets/linux/src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "gpio.h"
#include "i2c.h"
#include "pwm.h"
#include "rtc.h"
#include "spi.h"
#include "tty.h"
#include "uart.h"
Expand Down Expand Up @@ -79,6 +80,7 @@ void km_system_init() {
km_i2c_init();
km_spi_init();
km_uart_init();
km_rtc_init();
}

void km_system_cleanup() {
Expand All @@ -88,6 +90,7 @@ void km_system_cleanup() {
km_spi_cleanup();
km_uart_cleanup();
km_gpio_cleanup();
km_rtc_cleanup();
}

uint8_t km_running_script_check() { return false; }
2 changes: 2 additions & 0 deletions targets/linux/target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ if(NOT MODULES)
net
http
url
rtc
path
fs
vfs_lfs
Expand Down Expand Up @@ -60,6 +61,7 @@ set(SOURCES
${TARGET_SRC_DIR}/uart.c
${TARGET_SRC_DIR}/i2c.c
${TARGET_SRC_DIR}/spi.c
${TARGET_SRC_DIR}/rtc.c
${TARGET_SRC_DIR}/main.c
${BOARD_DIR}/board.c)

Expand Down
67 changes: 67 additions & 0 deletions targets/rp2/src/rtc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* Copyright (c) 2017 Kaluma
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include "rtc.h"

#include <time.h>

#include "hardware/rtc.h"
#include "pico/stdlib.h"
#include "pico/util/datetime.h"

void km_rtc_init() {
rtc_init();
// set as unix epoch time
datetime_t t = {
.year = 1970, .month = 1, .day = 1, .dotw = 4, .min = 0, .sec = 0};
rtc_set_datetime(&t);
}

void km_rtc_cleanup() {}

void km_rtc_set_time(uint64_t time) {
struct tm* ptm;
time_t t = (time_t)time;
ptm = gmtime(&t);
datetime_t datetime;
datetime.sec = ptm->tm_sec;
datetime.min = ptm->tm_min;
datetime.hour = ptm->tm_hour;
datetime.day = ptm->tm_mday;
datetime.dotw = ptm->tm_wday;
datetime.month = ptm->tm_mon + 1;
datetime.year = ptm->tm_year + 1900;
rtc_set_datetime(&datetime);
}

uint64_t km_rtc_get_time() {
datetime_t datetime;
rtc_get_datetime(&datetime);
struct tm ts;
ts.tm_sec = datetime.sec;
ts.tm_min = datetime.min;
ts.tm_hour = datetime.hour;
ts.tm_mday = datetime.day;
ts.tm_mon = datetime.month - 1;
ts.tm_year = datetime.year - 1900;
time_t tsec = mktime(&ts);
return (uint64_t)(tsec * 1000);
}
4 changes: 4 additions & 0 deletions targets/rp2/src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "io.h"
#include "pico/stdlib.h"
#include "pwm.h"
#include "rtc.h"
#include "spi.h"
#include "tty.h"
#include "tusb.h"
Expand Down Expand Up @@ -69,12 +70,14 @@ void km_micro_delay(uint32_t usec) { sleep_us(usec); }
* Kaluma Hardware System Initializations
*/
void km_system_init() {
stdio_init_all();
km_gpio_init();
km_adc_init();
km_pwm_init();
km_i2c_init();
km_spi_init();
km_uart_init();
km_rtc_init();
}

void km_system_cleanup() {
Expand All @@ -84,6 +87,7 @@ void km_system_cleanup() {
km_spi_cleanup();
km_uart_cleanup();
km_gpio_cleanup();
km_rtc_cleanup();
}

uint8_t km_running_script_check() {
Expand Down
Loading

0 comments on commit db5cd54

Please sign in to comment.