Merge remote-tracking branch 'origin/master' into jerryn_tick

This commit is contained in:
Jerry Needell 2018-04-19 20:54:47 -04:00
commit 3399d541c3
44 changed files with 685 additions and 144 deletions

View File

@ -16,7 +16,6 @@ env:
- TRAVIS_BOARD=itsybitsy_m4_express
- TRAVIS_BOARD=metro_m0_express
- TRAVIS_BOARD=metro_m4_express
- TRAVIS_BOARD=metro_m4_express_revb
- TRAVIS_BOARD=pirkey_m0
- TRAVIS_BOARD=trinket_m0
- TRAVIS_BOARD=gemma_m0

View File

@ -39,6 +39,7 @@ INC += -I. \
-Iasf4/$(CHIP_FAMILY)/hpl/gclk \
-Iasf4/$(CHIP_FAMILY)/hpl/pm \
-Iasf4/$(CHIP_FAMILY)/hpl/port \
-Iasf4/$(CHIP_FAMILY)/hpl/rtc \
-Iasf4/$(CHIP_FAMILY)/hpl/tc \
-Iasf4/$(CHIP_FAMILY)/include \
-Iasf4/$(CHIP_FAMILY)/CMSIS/Include \
@ -173,6 +174,7 @@ SRC_ASF := \
gcc/system_$(CHIP_FAMILY).c \
hal/src/hal_adc_sync.c \
hal/src/hal_atomic.c \
hal/src/hal_calendar.c \
hal/src/hal_dac_sync.c \
hal/src/hal_delay.c \
hal/src/hal_flash.c \
@ -278,6 +280,8 @@ SRC_COMMON_HAL = \
microcontroller/Processor.c \
neopixel_write/__init__.c \
os/__init__.c \
rtc/__init__.c \
rtc/RTC.c \
storage/__init__.c \
supervisor/__init__.c \
supervisor/Runtime.c \
@ -285,6 +289,8 @@ SRC_COMMON_HAL = \
analogio/__init__.c \
analogio/AnalogIn.c \
analogio/AnalogOut.c \
nvm/__init__.c \
nvm/ByteArray.c \
pulseio/__init__.c \
pulseio/PulseIn.c \
pulseio/PulseOut.c \
@ -293,9 +299,7 @@ SRC_COMMON_HAL = \
usb_hid/Device.c \
audioio/__init__.c \
audioio/AudioOut.c \
# nvm/__init__.c \
audiobusio/PDMIn.c \
nvm/ByteArray.c \
# audiobusio/PDMIn.c \
touchio/__init__.c \
touchio/TouchIn.c \

@ -1 +1 @@
Subproject commit aaa0f428111fbea7d56ab548053b11c9f12068f1
Subproject commit 02e264253c76b85c567ecdfce69ad0de0970da63

View File

@ -190,7 +190,7 @@
// <i> Indicates whether generic clock 2 configuration is enabled or not
// <id> enable_gclk_gen_2
#ifndef CONF_GCLK_GENERATOR_2_CONFIG
#define CONF_GCLK_GENERATOR_2_CONFIG 0
#define CONF_GCLK_GENERATOR_2_CONFIG 1
#endif
// <h> Generic Clock Generator Control
@ -205,7 +205,7 @@
// <i> Indicates whether Divide Selection is enabled or not
// <id> gclk_gen_2_div_sel
#ifndef CONF_GCLK_GEN_2_DIVSEL
#define CONF_GCLK_GEN_2_DIVSEL 0
#define CONF_GCLK_GEN_2_DIVSEL 1
#endif
// <q> Output Enable
@ -233,7 +233,7 @@
// <i> Indicates whether Generic Clock Generator Enable is enabled or not
// <id> gclk_arch_gen_2_enable
#ifndef CONF_GCLK_GEN_2_GENEN
#define CONF_GCLK_GEN_2_GENEN 0
#define CONF_GCLK_GEN_2_GENEN 1
#endif
// <y> Generic clock generator 2 source
@ -268,7 +268,7 @@
// <i>
// <id> gclk_gen_2_div
#ifndef CONF_GCLK_GEN_2_DIV
#define CONF_GCLK_GEN_2_DIV 1
#define CONF_GCLK_GEN_2_DIV 4
#endif
// </h>

View File

@ -10,6 +10,14 @@
#define CONF_RTC_ENABLE 1
#endif
// <q> Force reset RTC on initialization
// <i> Force RTC to reset on initialization, else init is not going on if RTC is already enabled.
// <i> Note that the previous power down data in RTC is lost if it's enabled.
// <id> rtc_arch_init_reset
#ifndef CONF_RTC_INIT_RESET
#define CONF_RTC_INIT_RESET 0
#endif
// <o> Prescaler configuration
// <0x0=>Peripheral clock divided by 1
// <0x1=>Peripheral clock divided by 2
@ -25,7 +33,7 @@
// <i> These bits define the RTC clock relative to the peripheral clock
// <id> rtc_arch_prescaler
#ifndef CONF_RTC_PRESCALER
#define CONF_RTC_PRESCALER 0x0
#define CONF_RTC_PRESCALER 0xa
#endif

View File

@ -274,7 +274,7 @@
// <i> Default: 122 us
// <id> xosc32k_arch_startup
#ifndef CONF_XOSC32K_STARTUP
#define CONF_XOSC32K_STARTUP CONF_XOSC32K_STARTUP_TIME_122MCS
#define CONF_XOSC32K_STARTUP CONF_XOSC32K_STARTUP_TIME_2000092MCS
#endif
// </h>

View File

@ -321,7 +321,7 @@
// <i> Select the clock source for RTC.
#ifndef CONF_GCLK_RTC_SRC
#define CONF_GCLK_RTC_SRC GCLK_CLKCTRL_GEN_GCLK0_Val
#define CONF_GCLK_RTC_SRC GCLK_CLKCTRL_GEN_GCLK2_Val
#endif
/**

View File

@ -172,7 +172,7 @@
// <i> Indicates whether generic clock 2 configuration is enabled or not
// <id> enable_gclk_gen_2
#ifndef CONF_GCLK_GENERATOR_2_CONFIG
#define CONF_GCLK_GENERATOR_2_CONFIG 0
#define CONF_GCLK_GENERATOR_2_CONFIG 1
#endif
// <h> Generic Clock Generator Control
@ -188,7 +188,7 @@
// <i> This defines the clock source for generic clock generator 2
// <id> gclk_gen_2_oscillator
#ifndef CONF_GCLK_GEN_2_SOURCE
#define CONF_GCLK_GEN_2_SOURCE GCLK_GENCTRL_SRC_XOSC1
#define CONF_GCLK_GEN_2_SOURCE GCLK_GENCTRL_SRC_OSCULP32K
#endif
// <q> Run in Standby
@ -230,7 +230,7 @@
// <i> Indicates whether Generic Clock Generator Enable is enabled or not
// <id> gclk_arch_gen_2_enable
#ifndef CONF_GCLK_GEN_2_GENEN
#define CONF_GCLK_GEN_2_GENEN 0
#define CONF_GCLK_GEN_2_GENEN 1
#endif
// </h>
@ -238,7 +238,7 @@
//<o> Generic clock generator 2 division <0x0000-0xFFFF>
// <id> gclk_gen_2_div
#ifndef CONF_GCLK_GEN_2_DIV
#define CONF_GCLK_GEN_2_DIV 1
#define CONF_GCLK_GEN_2_DIV 4
#endif
// </h>
// </e>

View File

@ -10,6 +10,14 @@
#define CONF_RTC_ENABLE 1
#endif
// <q> Force reset RTC on initialization
// <i> Force RTC to reset on initialization.
// <i> Note that the previous power down data in RTC is lost if it's enabled.
// <id> rtc_arch_init_reset
#ifndef CONF_RTC_INIT_RESET
#define CONF_RTC_INIT_RESET 0
#endif
// <o> Prescaler configuration
// <0x0=>OFF(Peripheral clock divided by 1)
// <0x1=>Peripheral clock divided by 1
@ -26,7 +34,7 @@
// <i> These bits define the RTC clock relative to the peripheral clock
// <id> rtc_arch_prescaler
#ifndef CONF_RTC_PRESCALER
#define CONF_RTC_PRESCALER 0x0
#define CONF_RTC_PRESCALER 0xb
#endif

View File

@ -42,8 +42,7 @@
// If you change this, then make sure to update the linker scripts as well to
// make sure you don't overwrite code.
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)

View File

@ -35,8 +35,7 @@
// If you change this, then make sure to update the linker scripts as well to
// make sure you don't overwrite code.
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)

View File

@ -37,8 +37,7 @@
// If you change this, then make sure to update the linker scripts as well to
// make sure you don't overwrite code.
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)

View File

@ -19,8 +19,7 @@
// If you change this, then make sure to update the linker scripts as well to
// make sure you don't overwrite code
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define CIRCUITPY_INTERNAL_NVM_SIZE 8192
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE)

View File

@ -35,8 +35,8 @@
// If you change this, then make sure to update the linker scripts as well to
// make sure you don't overwrite code.
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
#include "external_flash/devices.h"

View File

@ -19,8 +19,7 @@
// If you change this, then make sure to update the linker scripts as well to
// make sure you don't overwrite code
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define CIRCUITPY_INTERNAL_NVM_SIZE 8192
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE)

View File

@ -36,8 +36,7 @@
// If you change this, then make sure to update the linker scripts as well to
// make sure you don't overwrite code.
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)

View File

@ -22,8 +22,7 @@
// If you change this, then make sure to update the linker scripts as well to
// make sure you don't overwrite code
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define CIRCUITPY_INTERNAL_NVM_SIZE 8192
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE)

View File

@ -49,8 +49,7 @@
// If you change this, then make sure to update the linker scripts as well to
// make sure you don't overwrite code
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define CIRCUITPY_INTERNAL_NVM_SIZE 8192
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE)

View File

@ -5,8 +5,8 @@
/* Specify the memory areas */
MEMORY
{
/* Leave 16KiB for the bootloader. */
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 256K - 16K
/* Leave 16KiB for the bootloader. 8K for user data*/
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 256K - 16K - 8K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
}
@ -76,7 +76,7 @@ SECTIONS
.stack :
{
. = ALIGN(4);
. = . + 2K; /* Reserve a minimum of 2K for the stack. */
. = . + 10K; /* Reserve a minimum of 10K for the stack. nvm will temporarily store 8k on the stack when writing. */
. = ALIGN(4);
} >RAM

View File

@ -5,8 +5,8 @@
/* Specify the memory areas */
MEMORY
{
/* Leave 16KiB for the bootloader. */
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 512K - 16K
/* Leave 16KiB for the bootloader. 8K for user data*/
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 512K - 16K - 8K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K
}
@ -76,7 +76,7 @@ SECTIONS
.stack :
{
. = ALIGN(4);
. = . + 2K; /* Reserve a minimum of 2K for the stack. */
. = . + 10K; /* Reserve a minimum of 10K for the stack. nvm will temporarily store 8k on the stack when writing. */
. = ALIGN(4);
} >RAM

View File

@ -5,8 +5,8 @@
/* Specify the memory areas */
MEMORY
{
/* Leave 16KiB for the bootloader and 256KiB for the internal file system. */
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 512K - 16K - 256K
/* Leave 16KiB for the bootloader, 256KiB for the internal file system, and 8KiB for user binary data. */
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 512K - 16K - 256K - 8K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K
}
@ -76,7 +76,7 @@ SECTIONS
.stack :
{
. = ALIGN(4);
. = . + 2K; /* Reserve a minimum of 2K for the stack. */
. = . + 10K; /* Reserve a minimum of 10K for the stack. nvm will temporarily store 8k on the stack when writing. */
. = ALIGN(4);
} >RAM

View File

@ -6,7 +6,7 @@
MEMORY
{
/* Leave 16KiB for the bootloader. */
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 1M - 16K
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 1M - 16K - 8K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 256K
}
@ -76,7 +76,7 @@ SECTIONS
.stack :
{
. = ALIGN(4);
. = . + 2K; /* Reserve a minimum of 2K for the stack. */
. = . + 10K; /* Reserve a minimum of 10K for the stack. nvm will temporarily store 8k on the stack when writing. */
. = ALIGN(4);
} >RAM

View File

@ -5,8 +5,8 @@
/* Specify the memory areas */
MEMORY
{
/* Leave 16KiB for the bootloader and 512k for the filesystem. */
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 1M - 16K - 512K
/* Leave 16KiB for the bootloader, 512k for the filesystem and 8k for user config data. */
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 1M - 16K - 512K - 8K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 256K
}
@ -76,7 +76,7 @@ SECTIONS
.stack :
{
. = ALIGN(4);
. = . + 2K; /* Reserve a minimum of 2K for the stack. */
. = . + 10K; /* Reserve a minimum of 10K for the stack. nvm will temporarily store 8k on the stack when writing. */
. = ALIGN(4);
} >RAM

View File

@ -5,7 +5,7 @@
/* Specify the memory areas */
MEMORY
{
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 1M
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 1M - 8K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 256K
}
@ -74,7 +74,7 @@ SECTIONS
.stack :
{
. = ALIGN(4);
. = . + 2K; /* Reserve a minimum of 2K for the stack. */
. = . + 10K; /* Reserve a minimum of 10K for the stack. nvm will temporarily store 8k on the stack when writing. */
. = ALIGN(4);
} >RAM

View File

@ -5,8 +5,8 @@
/* Specify the memory areas */
MEMORY
{
/* 1024 KiB minus 512KiB for the internal file system. */
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 1M - 512K
/* 1024 KiB minus 512KiB for the internal file system and 8KiB for the user nvm. */
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 1M - 512K - 8K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 256K
}
@ -75,7 +75,7 @@ SECTIONS
.stack :
{
. = ALIGN(4);
. = . + 2K; /* Reserve a minimum of 2K for the stack. */
. = . + 10K; /* Reserve a minimum of 10K for the stack. nvm will temporarily store 8k on the stack when writing. */
. = ALIGN(4);
} >RAM

View File

@ -44,8 +44,8 @@
// If you change this, then make sure to update the linker scripts as well to
// make sure you don't overwrite code.
//#define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
#include "external_flash/devices.h"

View File

@ -37,8 +37,8 @@
// If you change this, then make sure to update the linker scripts as well to
// make sure you don't overwrite code.
//#define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
#include "external_flash/devices.h"

View File

@ -74,10 +74,12 @@ bool gclk_enabled(uint8_t gclk) {
void disable_gclk(uint8_t gclk) {
#ifdef SAMD51
while ((GCLK->SYNCBUSY.vec.GENCTRL & (1 << gclk)) != 0) {}
GCLK->GENCTRL[gclk].bit.GENEN = false;
while ((GCLK->SYNCBUSY.vec.GENCTRL & (1 << gclk)) != 0) {}
#endif
#ifdef SAMD21
while (GCLK->STATUS.bit.SYNCBUSY == 1) {}
GCLK->GENCTRL.reg = GCLK_GENCTRL_ID(gclk);
while (GCLK->STATUS.bit.SYNCBUSY == 1) {}
#endif

View File

@ -85,13 +85,13 @@ const mcu_processor_obj_t common_hal_mcu_processor_obj = {
// NVM is only available on Express boards for now.
#if CIRCUITPY_INTERNAL_NVM_SIZE > 0
// The singleton nvm.ByteArray object.
// const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
// .base = {
// .type = &nvm_bytearray_type,
// },
// .len = NVMCTRL_ROW_SIZE,
// .start_address = (uint8_t*) (FLASH_SIZE - NVMCTRL_ROW_SIZE)
// };
const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
.base = {
.type = &nvm_bytearray_type,
},
.len = CIRCUITPY_INTERNAL_NVM_SIZE,
.start_address = (uint8_t*) (FLASH_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE)
};
#endif
// This maps MCU pin names to pin objects.

View File

@ -26,7 +26,7 @@
#include "common-hal/nvm/ByteArray.h"
#include "asf/sam0/drivers/nvm/nvm.h"
#include "hal_flash.h"
#include <stdint.h>
#include <string.h>
@ -36,48 +36,12 @@ uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) {
}
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self,
uint32_t start_index, uint8_t* values, uint32_t len) {
uint32_t total_written = 0;
for (uint32_t i = 0; i < self->len / NVMCTRL_ROW_SIZE; i++) {
uint32_t row_start = NVMCTRL_ROW_SIZE * i;
if (row_start + NVMCTRL_ROW_SIZE < start_index || start_index + len < row_start) {
continue;
}
uint8_t temp_row[NVMCTRL_ROW_SIZE];
memcpy(temp_row,
self->start_address + row_start,
NVMCTRL_ROW_SIZE);
enum status_code error_code;
do {
error_code = nvm_erase_row((uint32_t) self->start_address + row_start);
} while (error_code == STATUS_BUSY);
if (error_code != STATUS_OK) {
return false;
}
uint32_t data_start = 0;
if (start_index > row_start) {
data_start = start_index - row_start;
}
uint32_t data_len = len;
uint32_t data_remaining = data_len - total_written;
uint32_t row_remaining = NVMCTRL_ROW_SIZE - data_start;
if (data_remaining > row_remaining) {
data_len = row_remaining;
}
memcpy(temp_row + data_start,
values + total_written,
data_len);
for (int page = 0; page < NVMCTRL_ROW_SIZE / NVMCTRL_PAGE_SIZE; page++) {
do {
error_code = nvm_write_buffer((uint32_t) self->start_address + row_start + page * NVMCTRL_PAGE_SIZE,
temp_row + page * NVMCTRL_PAGE_SIZE,
NVMCTRL_PAGE_SIZE);
} while (error_code == STATUS_BUSY);
if (error_code != STATUS_OK) {
return false;
}
}
}
uint32_t start_index, uint8_t* values, uint32_t len) {
// We don't use features that use any advanced NVMCTRL features so we can fake the descriptor
// whenever we need it instead of storing it long term.
struct flash_descriptor desc;
desc.dev.hw = NVMCTRL;
flash_write(&desc, (uint32_t) self->start_address + start_index, values, len);
return true;
}

View File

@ -0,0 +1,98 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Noralf Trønnes
*
* 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 <peripheral_clk_config.h>
#include <hal_init.h>
#include <hpl_gclk_base.h>
#include <hpl_pm_base.h>
#include <hal_calendar.h>
#include "py/obj.h"
#include "py/runtime.h"
#include "lib/timeutils/timeutils.h"
#include "shared-bindings/rtc/__init__.h"
static struct calendar_descriptor calendar;
void rtc_init(void) {
#ifdef SAMD21
_gclk_enable_channel(RTC_GCLK_ID, CONF_GCLK_RTC_SRC);
#endif
#ifdef SAMD51
hri_mclk_set_APBAMASK_RTC_bit(MCLK);
#endif
calendar_init(&calendar, RTC);
calendar_set_baseyear(&calendar, 2000);
calendar_enable(&calendar);
}
void common_hal_rtc_get_time(timeutils_struct_time_t *tm) {
struct calendar_date_time datetime;
calendar_get_date_time(&calendar, &datetime);
tm->tm_year = datetime.date.year;
tm->tm_mon = datetime.date.month;
tm->tm_mday = datetime.date.day;
tm->tm_hour = datetime.time.hour;
tm->tm_min = datetime.time.min;
tm->tm_sec = datetime.time.sec;
}
void common_hal_rtc_set_time(timeutils_struct_time_t *tm) {
struct calendar_date date = {
.year = tm->tm_year,
.month = tm->tm_mon,
.day = tm->tm_mday,
};
calendar_set_date(&calendar, &date);
struct calendar_time time = {
.hour = tm->tm_hour,
.min = tm->tm_min,
.sec = tm->tm_sec,
};
calendar_set_time(&calendar, &time);
}
// A positive value speeds up the clock by removing clock cycles.
int common_hal_rtc_get_calibration(void) {
int calibration = hri_rtcmode0_read_FREQCORR_VALUE_bf(calendar.device.hw);
if (!hri_rtcmode0_get_FREQCORR_SIGN_bit(calendar.device.hw))
calibration = -calibration;
return calibration;
}
void common_hal_rtc_set_calibration(int calibration) {
if (calibration > 127 || calibration < -127)
mp_raise_ValueError("calibration value out of range +/-127");
hri_rtcmode0_write_FREQCORR_SIGN_bit(calendar.device.hw, calibration < 0 ? 0 : 1);
hri_rtcmode0_write_FREQCORR_VALUE_bf(calendar.device.hw, abs(calibration));
}

View File

@ -0,0 +1,32 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Noralf Trønnes
*
* 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 MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_RTC_RTC_H
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_RTC_RTC_H
extern void rtc_init(void);
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_RTC_RTC_H

View File

@ -166,6 +166,7 @@ extern const struct _mp_obj_module_t board_module;
extern const struct _mp_obj_module_t math_module;
extern const struct _mp_obj_module_t os_module;
extern const struct _mp_obj_module_t random_module;
extern const struct _mp_obj_module_t rtc_module;
extern const struct _mp_obj_module_t storage_module;
extern const struct _mp_obj_module_t struct_module;
extern const struct _mp_obj_module_t time_module;
@ -234,6 +235,7 @@ extern const struct _mp_obj_module_t usb_hid_module;
{ MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&os_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_pulseio), (mp_obj_t)&pulseio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_random), (mp_obj_t)&random_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_rtc), (mp_obj_t)&rtc_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_storage), (mp_obj_t)&storage_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_struct), (mp_obj_t)&struct_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_supervisor), (mp_obj_t)&supervisor_module }, \
@ -266,6 +268,7 @@ extern const struct _mp_obj_module_t usb_hid_module;
const char *readline_hist[8]; \
vstr_t *repl_line; \
mp_obj_t playing_audio[AUDIO_DMA_CHANNEL_COUNT]; \
mp_obj_t rtc_time_source; \
FLASH_ROOT_POINTERS \
void run_background_tasks(void);

View File

@ -52,7 +52,9 @@
#include "common-hal/pulseio/PulseIn.h"
#include "common-hal/pulseio/PulseOut.h"
#include "common-hal/pulseio/PWMOut.h"
#include "common-hal/rtc/RTC.h"
#include "common-hal/usb_hid/Device.h"
#include "shared-bindings/rtc/__init__.h"
#include "clocks.h"
#include "events.h"
#include "shared_dma.h"
@ -124,6 +126,7 @@ safe_mode_t port_init(void) {
// Configure millisecond timer initialization.
tick_init();
rtc_init();
// Init the nvm controller.
// struct nvm_config config_nvm;
@ -147,35 +150,6 @@ safe_mode_t port_init(void) {
return USER_SAFE_MODE;
}
// #if CIRCUITPY_INTERNAL_NVM_SIZE > 0
// // Upgrade the nvm flash to include one sector for eeprom emulation.
// struct nvm_fusebits fuses;
// if (nvm_get_fuses(&fuses) == STATUS_OK &&
// fuses.eeprom_size == NVM_EEPROM_EMULATOR_SIZE_0) {
// #ifdef INTERNAL_FLASH_FS
// // Shift the internal file system up one row.
// for (uint8_t row = 0; row < TOTAL_INTERNAL_FLASH_SIZE / NVMCTRL_ROW_SIZE; row++) {
// uint32_t new_row_address = INTERNAL_FLASH_MEM_SEG1_START_ADDR + row * NVMCTRL_ROW_SIZE;
// nvm_erase_row(new_row_address);
// nvm_write_buffer(new_row_address,
// (uint8_t*) (new_row_address + CIRCUITPY_INTERNAL_EEPROM_SIZE),
// NVMCTRL_ROW_SIZE);
// }
// #endif
// uint32_t nvm_size = CIRCUITPY_INTERNAL_NVM_SIZE;
// uint8_t enum_value = 6;
// while (nvm_size > 256 && enum_value != 255) {
// nvm_size /= 2;
// enum_value -= 1;
// }
// if (enum_value != 255 && nvm_size == 256) {
// // Mark the last section as eeprom now.
// fuses.eeprom_size = (enum nvm_eeprom_emulator_size) enum_value;
// nvm_set_fuses(&fuses);
// }
// }
// #endif
return NO_SAFE_MODE;
}
@ -209,6 +183,7 @@ void reset_port(void) {
pulsein_reset();
pulseout_reset();
pwmout_reset();
rtc_reset();
reset_gclks();

View File

@ -8,6 +8,12 @@ BOOTLOADER_PKG = boards/feather52/bootloader/feather52_bootloader_$(SOFTDEV_VERS
NRF_DEFINES += -DNRF52832_XXAA
ifeq ($(OS),Windows_NT)
NRFUTIL = ../../lib/nrfutil/binaries/win32/nrfutil.exe
else
NRFUTIL = nrfutil
endif
ifeq ($(SD), )
INC += -Idrivers/bluetooth/s132_$(MCU_VARIANT)_$(SOFTDEV_VERSION)/s132_$(MCU_VARIANT)_$(SOFTDEV_VERSION)_API/include
INC += -Idrivers/bluetooth/s132_$(MCU_VARIANT)_$(SOFTDEV_VERSION)/s132_$(MCU_VARIANT)_$(SOFTDEV_VERSION)_API/include/$(MCU_VARIANT)
@ -25,12 +31,12 @@ __check_defined = \
.PHONY: dfu-gen dfu-flash boot-flash
dfu-gen:
nrfutil dfu genpkg --dev-type 0x0052 --application $(BUILD)/$(OUTPUT_FILENAME).hex $(BUILD)/dfu-package.zip
$(NRFUTIL) dfu genpkg --dev-type 0x0052 --application $(BUILD)/$(OUTPUT_FILENAME).hex $(BUILD)/dfu-package.zip
dfu-flash:
@:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyUSB0)
nrfutil dfu serial --package $(BUILD)/dfu-package.zip -p $(SERIAL) -b 115200
$(NRFUTIL) dfu serial --package $(BUILD)/dfu-package.zip -p $(SERIAL) -b 115200
boot-flash:
@:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyUSB0)
nrfutil dfu serial --package $(BOOTLOADER_PKG) -p $(SERIAL) -b 115200
$(NRFUTIL) dfu serial --package $(BOOTLOADER_PKG) -p $(SERIAL) -b 115200

View File

@ -8,6 +8,12 @@ BOOTLOADER_FILENAME = boards/feather52840/bootloader/feather52840_bootloader_6.0
NRF_DEFINES += -DNRF52840_XXAA
ifeq ($(OS),Windows_NT)
NRFUTIL = ../../lib/nrfutil/binaries/win32/nrfutil.exe
else
NRFUTIL = nrfutil
endif
CFLAGS += -DADAFRUIT_FEATHER52840
ifeq ($(SD), )
@ -25,11 +31,11 @@ __check_defined = \
.PHONY: dfu-gen dfu-flash boot-flash
dfu-gen:
nrfutil dfu genpkg --sd-req 0xFFFE --dev-type 0x0052 --application $(BUILD)/$(OUTPUT_FILENAME).hex $(BUILD)/dfu-package.zip
$(NRFUTIL) dfu genpkg --sd-req 0xFFFE --dev-type 0x0052 --application $(BUILD)/$(OUTPUT_FILENAME).hex $(BUILD)/dfu-package.zip
dfu-flash:
@:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyUSB0)
nrfutil --verbose dfu serial --package $(BUILD)/dfu-package.zip -p $(SERIAL) -b 115200
$(NRFUTIL) --verbose dfu serial --package $(BUILD)/dfu-package.zip -p $(SERIAL) -b 115200
boot-flash:
nrfjprog --program $(BOOTLOADER_FILENAME).hex -f nrf52 --chiperase --reset

View File

@ -36,12 +36,13 @@
//| ================================================================================
//|
//| Non-volatile memory is available as a byte array that persists over reloads
//| and power cycles.
//| and power cycles. Each assignment causes an erase and write cycle so its recommended to assign
//| all values to change at once.
//|
//| Usage::
//|
//| import microcontroller
//| microcontroller.nvm[0] = 0xcc
//| microcontroller.nvm[0:3] = b"\xcc\x10\x00"
//|
//| .. class:: ByteArray()

135
shared-bindings/rtc/RTC.c Normal file
View File

@ -0,0 +1,135 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Noralf Trønnes
* Copyright (c) 2013, 2014 Damien P. George
*
* 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 <string.h>
#include "py/obj.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "lib/timeutils/timeutils.h"
#include "shared-bindings/rtc/__init__.h"
#include "shared-bindings/rtc/RTC.h"
#include "shared-bindings/time/__init__.h"
void MP_WEAK common_hal_rtc_get_time(timeutils_struct_time_t *tm) {
mp_raise_NotImplementedError("RTC is not supported on this board");
}
void MP_WEAK common_hal_rtc_set_time(timeutils_struct_time_t *tm) {
mp_raise_NotImplementedError("RTC is not supported on this board");
}
int MP_WEAK common_hal_rtc_get_calibration(void) {
return 0;
}
void MP_WEAK common_hal_rtc_set_calibration(int calibration) {
mp_raise_NotImplementedError("RTC calibration is not supported on this board");
}
const rtc_rtc_obj_t rtc_rtc_obj = {{&rtc_rtc_type}};
//| .. currentmodule:: rtc
//|
//| :class:`RTC` --- Real Time Clock
//| --------------------------------
//|
//| .. class:: RTC()
//|
//| This class represents the onboard Real Time Clock. It is a singleton and will always return the same instance.
//|
STATIC mp_obj_t rtc_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
// No arguments
mp_arg_check_num(n_args, n_kw, 0, 0, false);
// return constant object
return (mp_obj_t)&rtc_rtc_obj;
}
//| .. attribute:: datetime
//|
//| The date and time of the RTC.
//|
STATIC mp_obj_t rtc_rtc_obj_get_datetime(mp_obj_t self_in) {
timeutils_struct_time_t tm;
common_hal_rtc_get_time(&tm);
return struct_time_from_tm(&tm);
}
MP_DEFINE_CONST_FUN_OBJ_1(rtc_rtc_get_datetime_obj, rtc_rtc_obj_get_datetime);
STATIC mp_obj_t rtc_rtc_obj_set_datetime(mp_obj_t self_in, mp_obj_t datetime) {
timeutils_struct_time_t tm;
struct_time_to_tm(datetime, &tm);
common_hal_rtc_set_time(&tm);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(rtc_rtc_set_datetime_obj, rtc_rtc_obj_set_datetime);
const mp_obj_property_t rtc_rtc_datetime_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&rtc_rtc_get_datetime_obj,
(mp_obj_t)&rtc_rtc_set_datetime_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| .. attribute:: calibration
//|
//| The RTC calibration value.
//| A positive value speeds up the clock and a negative value slows it down.
//| Range and value is hardware specific, but one step is often approx. 1 ppm.
//|
STATIC mp_obj_t rtc_rtc_obj_get_calibration(mp_obj_t self_in) {
int calibration = common_hal_rtc_get_calibration();
return mp_obj_new_int(calibration);
}
MP_DEFINE_CONST_FUN_OBJ_1(rtc_rtc_get_calibration_obj, rtc_rtc_obj_get_calibration);
STATIC mp_obj_t rtc_rtc_obj_set_calibration(mp_obj_t self_in, mp_obj_t calibration) {
common_hal_rtc_set_calibration(mp_obj_get_int(calibration));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(rtc_rtc_set_calibration_obj, rtc_rtc_obj_set_calibration);
const mp_obj_property_t rtc_rtc_calibration_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&rtc_rtc_get_calibration_obj,
(mp_obj_t)&rtc_rtc_set_calibration_obj,
(mp_obj_t)&mp_const_none_obj},
};
STATIC const mp_rom_map_elem_t rtc_rtc_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_datetime), MP_ROM_PTR(&rtc_rtc_datetime_obj) },
{ MP_ROM_QSTR(MP_QSTR_calibration), MP_ROM_PTR(&rtc_rtc_calibration_obj) },
};
STATIC MP_DEFINE_CONST_DICT(rtc_rtc_locals_dict, rtc_rtc_locals_dict_table);
const mp_obj_type_t rtc_rtc_type = {
{ &mp_type_type },
.name = MP_QSTR_RTC,
.make_new = rtc_rtc_make_new,
.locals_dict = (mp_obj_dict_t*)&rtc_rtc_locals_dict,
};

49
shared-bindings/rtc/RTC.h Normal file
View File

@ -0,0 +1,49 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Noralf Trønnes
*
* 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 MICROPY_INCLUDED_SHARED_BINDINGS_RTC_RTC_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_RTC_RTC_H
#include <stdint.h>
#include <stdbool.h>
#include "lib/timeutils/timeutils.h"
extern void common_hal_rtc_get_time(timeutils_struct_time_t *tm);
extern void common_hal_rtc_set_time(timeutils_struct_time_t *tm);
extern int common_hal_rtc_get_calibration(void);
extern void common_hal_rtc_set_calibration(int calibration);
extern const mp_obj_type_t rtc_rtc_type;
typedef struct _rtc_rtc_obj_t {
mp_obj_base_t base;
} rtc_rtc_obj_t;
extern const rtc_rtc_obj_t rtc_rtc_obj;
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_RTC_RTC_H

View File

@ -0,0 +1,100 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Noralf Trønnes
* Copyright (c) 2013, 2014 Damien P. George
*
* 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 "py/obj.h"
#include "py/runtime.h"
#include "shared-bindings/rtc/__init__.h"
#include "shared-bindings/rtc/RTC.h"
#include "shared-bindings/time/__init__.h"
//| :mod:`rtc` --- Real Time Clock
//| ========================================================
//|
//| .. module:: rtc
//| :synopsis: Real Time Clock
//| :platform: SAMD21
//|
//| The `rtc` module provides support for a Real Time Clock.
//| It also backs the `time.time()` and `time.localtime()` functions using the onboard RTC if present.
//|
//| Libraries
//|
//| .. toctree::
//| :maxdepth: 3
//|
//| RTC
//|
void rtc_reset(void) {
MP_STATE_VM(rtc_time_source) = (mp_obj_t)&rtc_rtc_obj;
}
mp_obj_t rtc_get_time_source_time(void) {
mp_obj_t datetime = mp_load_attr(MP_STATE_VM(rtc_time_source), MP_QSTR_datetime);
timeutils_struct_time_t tm;
struct_time_to_tm(datetime, &tm);
// This sets tm_wday and tm_yday
return struct_time_from_tm(&tm);
}
//| .. function:: set_time_source(rtc)
//|
//| Sets the rtc time source used by time.localtime().
//| The default is `rtc.RTC()`.
//|
//| Example usage::
//|
//| import rtc
//| import time
//|
//| class RTC(object):
//| @property
//| def datetime(self):
//| return time.struct_time((2018, 3, 17, 21, 1, 47, 0, 0, 0))
//|
//| r = RTC()
//| rtc.set_time_source(r)
//|
STATIC mp_obj_t rtc_set_time_source(mp_obj_t time_source) {
MP_STATE_VM(rtc_time_source) = time_source;
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(rtc_set_time_source_obj, rtc_set_time_source);
STATIC const mp_rom_map_elem_t rtc_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_rtc) },
{ MP_ROM_QSTR(MP_QSTR_set_time_source), MP_ROM_PTR(&rtc_set_time_source_obj) },
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&rtc_rtc_type) },
};
STATIC MP_DEFINE_CONST_DICT(rtc_module_globals, rtc_module_globals_table);
const mp_obj_module_t rtc_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&rtc_module_globals,
};

View File

@ -0,0 +1,33 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Noralf Trønnes
*
* 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 MICROPY_INCLUDED_SHARED_BINDINGS_RTC___INIT___H
#define MICROPY_INCLUDED_SHARED_BINDINGS_RTC___INIT___H
extern void rtc_reset(void);
extern mp_obj_t rtc_get_time_source_time(void);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_RTC___INIT___H

View File

@ -30,6 +30,8 @@
#include "py/obj.h"
#include "py/objnamedtuple.h"
#include "lib/timeutils/timeutils.h"
#include "shared-bindings/rtc/__init__.h"
#include "shared-bindings/time/__init__.h"
//| :mod:`time` --- time and timing related functions
@ -133,6 +135,122 @@ const mp_obj_namedtuple_type_t struct_time_type_obj = {
MP_QSTR_tm_isdst
},
};
mp_obj_t struct_time_from_tm(timeutils_struct_time_t *tm) {
timeutils_struct_time_t tmp;
mp_uint_t secs = timeutils_seconds_since_2000(tm->tm_year, tm->tm_mon, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
timeutils_seconds_since_2000_to_struct_time(secs, &tmp);
tm->tm_wday = tmp.tm_wday;
tm->tm_yday = tmp.tm_yday;
mp_obj_t elems[9] = {
mp_obj_new_int(tm->tm_year),
mp_obj_new_int(tm->tm_mon),
mp_obj_new_int(tm->tm_mday),
mp_obj_new_int(tm->tm_hour),
mp_obj_new_int(tm->tm_min),
mp_obj_new_int(tm->tm_sec),
mp_obj_new_int(tm->tm_wday),
mp_obj_new_int(tm->tm_yday),
mp_obj_new_int(-1), // tm_isdst is not supported
};
return namedtuple_make_new((const mp_obj_type_t*)&struct_time_type_obj, 9, 0, elems);
};
void struct_time_to_tm(mp_obj_t t, timeutils_struct_time_t *tm) {
mp_obj_t *elems;
size_t len;
if (!MP_OBJ_IS_TYPE(t, &mp_type_tuple) && !MP_OBJ_IS_TYPE(t, MP_OBJ_FROM_PTR(&struct_time_type_obj))) {
mp_raise_TypeError("Tuple or struct_time argument required");
}
mp_obj_tuple_get(t, &len, &elems);
if (len != 9) {
mp_raise_TypeError("function takes exactly 9 arguments");
}
tm->tm_year = mp_obj_get_int(elems[0]);
tm->tm_mon = mp_obj_get_int(elems[1]);
tm->tm_mday = mp_obj_get_int(elems[2]);
tm->tm_hour = mp_obj_get_int(elems[3]);
tm->tm_min = mp_obj_get_int(elems[4]);
tm->tm_sec = mp_obj_get_int(elems[5]);
tm->tm_wday = mp_obj_get_int(elems[6]);
tm->tm_yday = mp_obj_get_int(elems[7]);
// elems[8] tm_isdst is not supported
}
mp_obj_t MP_WEAK rtc_get_time_source_time(void) {
mp_raise_RuntimeError("RTC is not supported on this board");
}
//| .. method:: time()
//|
//| Return the current time in seconds since since Jan 1, 2000.
//|
//| :return: the current time
//| :rtype: int
//|
STATIC mp_obj_t time_time(void) {
timeutils_struct_time_t tm;
struct_time_to_tm(rtc_get_time_source_time(), &tm);
mp_uint_t secs = timeutils_seconds_since_2000(tm.tm_year, tm.tm_mon, tm.tm_mday, // mp_uint_t date
tm.tm_hour, tm.tm_min, tm.tm_sec);
return mp_obj_new_int_from_uint(secs);
}
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
//| .. method:: localtime([secs])
//|
//| Convert a time expressed in seconds since Jan 1, 2000 to a struct_time in
//| local time. If secs is not provided or None, the current time as returned
//| by time() is used.
//|
//| :return: the current time
//| :rtype: time.struct_time
//|
STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
if (n_args == 0 || args[0] == mp_const_none) {
return rtc_get_time_source_time();
}
timeutils_struct_time_t tm;
timeutils_seconds_since_2000_to_struct_time(mp_obj_get_int(args[0]), &tm);
return struct_time_from_tm(&tm);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime);
//| .. method:: mktime(t)
//|
//| This is the inverse function of localtime(). Its argument is the
//| struct_time or full 9-tuple (since the dst flag is needed; use -1 as the
//| dst flag if it is unknown) which expresses the time in local time, not UTC.
//| The earliest date for which it can generate a time is Jan 1, 2000.
//|
//| :return: seconds
//| :rtype: int
//|
STATIC mp_obj_t time_mktime(mp_obj_t t) {
mp_obj_t *elem;
size_t len;
if (!MP_OBJ_IS_TYPE(t, &mp_type_tuple) && !MP_OBJ_IS_TYPE(t, MP_OBJ_FROM_PTR(&struct_time_type_obj))) {
mp_raise_TypeError("Tuple or struct_time argument required");
}
mp_obj_tuple_get(t, &len, &elem);
if (len != 9) {
mp_raise_TypeError("function takes exactly 9 arguments");
}
return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]), mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]),
mp_obj_get_int(elem[3]), mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5])));
}
MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
#endif
STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
@ -142,7 +260,10 @@ STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&time_sleep_obj) },
#if MICROPY_PY_COLLECTIONS
{ MP_ROM_QSTR(MP_QSTR_struct_time), MP_ROM_PTR(&struct_time_type_obj) },
{ MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) },
{ MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) },
#endif
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
};
STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);

View File

@ -30,6 +30,11 @@
#include <stdint.h>
#include <stdbool.h>
#include "lib/timeutils/timeutils.h"
extern mp_obj_t struct_time_from_tm(timeutils_struct_time_t *tm);
extern void struct_time_to_tm(mp_obj_t t, timeutils_struct_time_t *tm);
extern uint64_t common_hal_time_monotonic(void);
extern void common_hal_time_delay_ms(uint32_t);