2016-12-26 10:46:23 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Glenn Ruben Bakke
|
|
|
|
*
|
|
|
|
* 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 <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/mphal.h"
|
|
|
|
#include "rtc.h"
|
|
|
|
#include "hal_rtc.h"
|
|
|
|
|
|
|
|
#if MICROPY_PY_MACHINE_RTC
|
|
|
|
|
|
|
|
typedef struct _machine_rtc_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
RTC_HandleTypeDef *rtc;
|
2017-03-05 18:56:18 -05:00
|
|
|
mp_obj_t callback;
|
2016-12-26 10:46:23 -05:00
|
|
|
} machine_rtc_obj_t;
|
|
|
|
|
2017-03-05 18:56:18 -05:00
|
|
|
RTC_HandleTypeDef RTCHandle0 = {.config.p_instance = NULL, .id = 0};
|
|
|
|
RTC_HandleTypeDef RTCHandle1 = {.config.p_instance = NULL, .id = 1};
|
2016-12-26 10:46:23 -05:00
|
|
|
|
|
|
|
STATIC const machine_rtc_obj_t machine_rtc_obj[] = {
|
|
|
|
{{&machine_rtc_type}, &RTCHandle0},
|
|
|
|
{{&machine_rtc_type}, &RTCHandle1},
|
|
|
|
};
|
|
|
|
|
2017-03-05 18:56:18 -05:00
|
|
|
STATIC void hal_interrupt_handle(NRF_RTC_Type * p_instance) {
|
|
|
|
if (p_instance == RTC0) {
|
|
|
|
const machine_rtc_obj_t *self = &machine_rtc_obj[0];
|
|
|
|
mp_call_function_0(self->callback);
|
|
|
|
} else if (p_instance == RTC1) {
|
|
|
|
const machine_rtc_obj_t *self = &machine_rtc_obj[1];
|
|
|
|
mp_call_function_0(self->callback);
|
|
|
|
}
|
|
|
|
#if NRF52
|
|
|
|
else if (p_instance == RTC2) {
|
|
|
|
const machine_rtc_obj_t *self = &machine_rtc_obj[2];
|
|
|
|
mp_call_function_0(self->callback);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-12-26 10:46:23 -05:00
|
|
|
void rtc_init0(void) {
|
2017-03-05 18:56:18 -05:00
|
|
|
|
|
|
|
hal_rtc_callback_set(hal_interrupt_handle);
|
|
|
|
|
2016-12-26 10:46:23 -05:00
|
|
|
// reset the RTC handles
|
|
|
|
memset(&RTCHandle0, 0, sizeof(RTC_HandleTypeDef));
|
2017-03-05 18:56:18 -05:00
|
|
|
RTCHandle0.config.p_instance = RTC0;
|
|
|
|
RTCHandle0.config.irq_num = RTC0_IRQ_NUM;
|
2017-03-05 19:13:19 -05:00
|
|
|
#if (BLUETOOTH_SD == 100)
|
|
|
|
RTCHandle0.config.irq_priority = 3;
|
|
|
|
#else
|
|
|
|
RTCHandle0.config.irq_priority = 6;
|
|
|
|
#endif
|
2016-12-26 10:46:23 -05:00
|
|
|
memset(&RTCHandle1, 0, sizeof(RTC_HandleTypeDef));
|
2017-03-05 18:56:18 -05:00
|
|
|
RTCHandle1.config.p_instance = RTC1;
|
|
|
|
RTCHandle1.config.irq_num = RTC1_IRQ_NUM;
|
2017-03-05 19:13:19 -05:00
|
|
|
#if (BLUETOOTH_SD == 100)
|
|
|
|
RTCHandle1.config.irq_priority = 3;
|
|
|
|
#else
|
|
|
|
RTCHandle1.config.irq_priority = 6;
|
|
|
|
#endif
|
2016-12-26 10:46:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int rtc_find(mp_obj_t id) {
|
|
|
|
// given an integer id
|
|
|
|
int rtc_id = mp_obj_get_int(id);
|
|
|
|
if (rtc_id >= 0 && rtc_id <= MP_ARRAY_SIZE(machine_rtc_obj)
|
|
|
|
&& machine_rtc_obj[rtc_id].rtc != NULL) {
|
|
|
|
return rtc_id;
|
|
|
|
}
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
|
|
|
|
"RTC(%d) does not exist", rtc_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void rtc_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
|
|
|
|
machine_rtc_obj_t *self = o;
|
|
|
|
mp_printf(print, "RTC(%u)", self->rtc->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* MicroPython bindings for machine API */
|
|
|
|
|
2017-03-05 19:13:19 -05:00
|
|
|
/*
|
|
|
|
from machine import RTC
|
|
|
|
def cb():
|
|
|
|
print("Callback")
|
|
|
|
r = RTC(0, 8, cb)
|
|
|
|
r.start(16)
|
|
|
|
*/
|
|
|
|
|
2016-12-26 10:46:23 -05:00
|
|
|
STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
2017-03-05 18:56:18 -05:00
|
|
|
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
|
{ MP_QSTR_frequency, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
|
{ MP_QSTR_callback, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
2016-12-26 10:46:23 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// parse args
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
2017-03-05 18:56:18 -05:00
|
|
|
if (args[0].u_obj == MP_OBJ_NEW_SMALL_INT(-1)) {
|
2016-12-26 10:46:23 -05:00
|
|
|
// index -1 does not exist
|
|
|
|
return mp_const_none;
|
|
|
|
// TODO: raise exception
|
|
|
|
}
|
|
|
|
|
|
|
|
// get static peripheral object
|
2017-03-05 18:56:18 -05:00
|
|
|
int rtc_id = rtc_find(args[0].u_obj);
|
|
|
|
|
|
|
|
// unconst machine object in order to set a callback.
|
|
|
|
machine_rtc_obj_t * self = (machine_rtc_obj_t *)&machine_rtc_obj[rtc_id];
|
|
|
|
|
|
|
|
mp_obj_t freq_obj = args[1].u_obj;
|
|
|
|
|
|
|
|
if (freq_obj != mp_const_none && MP_OBJ_IS_INT(freq_obj)) {
|
|
|
|
self->rtc->config.frequency = mp_obj_get_int(freq_obj);
|
|
|
|
} else {
|
|
|
|
// raise exception
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args[2].u_obj != mp_const_none) {
|
|
|
|
self->callback = args[2].u_obj;
|
|
|
|
}
|
2016-12-26 10:46:23 -05:00
|
|
|
|
2017-03-05 18:56:18 -05:00
|
|
|
hal_rtc_init(&self->rtc->config);
|
2016-12-26 10:46:23 -05:00
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
2017-03-05 18:56:18 -05:00
|
|
|
/// \method start(period)
|
|
|
|
/// Start the RTC timer. Timeout occurs after number of periods
|
|
|
|
/// in the configured frequency has been reached.
|
|
|
|
///
|
|
|
|
STATIC mp_obj_t machine_rtc_start(mp_obj_t self_in, mp_obj_t period_in) {
|
|
|
|
machine_rtc_obj_t * self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_int_t period = mp_obj_get_int(period_in);
|
|
|
|
|
|
|
|
hal_rtc_start(&self->rtc->config, period);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_rtc_start_obj, machine_rtc_start);
|
|
|
|
|
|
|
|
/// \method stop()
|
|
|
|
/// Stop the RTC timer.
|
|
|
|
///
|
|
|
|
STATIC mp_obj_t machine_rtc_stop(mp_obj_t self_in) {
|
|
|
|
machine_rtc_obj_t * self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
hal_rtc_stop(&self->rtc->config);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_rtc_stop_obj, machine_rtc_stop);
|
|
|
|
|
|
|
|
|
|
|
|
STATIC const mp_map_elem_t machine_rtc_locals_dict_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_start), (mp_obj_t)(&machine_rtc_start_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_stop), (mp_obj_t)(&machine_rtc_stop_obj) },
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table);
|
|
|
|
|
2016-12-26 10:46:23 -05:00
|
|
|
const mp_obj_type_t machine_rtc_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_RTC,
|
|
|
|
.print = rtc_print,
|
|
|
|
.make_new = machine_rtc_make_new,
|
|
|
|
.locals_dict = (mp_obj_t)&machine_rtc_locals_dict
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MICROPY_PY_MACHINE_RTC
|