Merge pull request #6468 from blues/stm32_rtc
feat(swan_r5): adds a basic STM32 RTC implementation.
This commit is contained in:
commit
ab346a27fc
|
@ -70,3 +70,4 @@ CIRCUITPY_BLEIO = 0
|
|||
CIRCUITPY_BUSDEVICE = 0
|
||||
CIRCUITPY_KEYPAD = 1
|
||||
CIRCUITPY_RGBMATRIX = 0
|
||||
CIRCUITPY_RTC = 1
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 20212 Matthew McGowan for Blues Wireless Inc
|
||||
*
|
||||
* 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 "py/obj.h"
|
||||
#include "py/runtime.h"
|
||||
#include "shared/timeutils/timeutils.h"
|
||||
#include "shared-bindings/rtc/__init__.h"
|
||||
#include "common-hal/rtc/RTC.h"
|
||||
#include "shared-bindings/rtc/RTC.h"
|
||||
#include "supervisor/port.h"
|
||||
#include "supervisor/shared/translate/translate.h"
|
||||
#include "peripherals/rtc.h"
|
||||
|
||||
|
||||
void common_hal_rtc_set_time(timeutils_struct_time_t *tm) {
|
||||
stm32_peripherals_rtc_set_time(tm);
|
||||
}
|
||||
|
||||
void common_hal_rtc_get_time(timeutils_struct_time_t *tm) {
|
||||
stm32_peripherals_rtc_get_time(tm);
|
||||
}
|
||||
|
||||
int common_hal_rtc_get_calibration(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void common_hal_rtc_set_calibration(int calibration) {
|
||||
mp_raise_NotImplementedError_varg(translate("%q"), MP_QSTR_calibration);
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2022 Matthew McGowan for Blues Wireless Inc
|
||||
*
|
||||
* 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_STM_COMMON_HAL_RTC_RTC_H
|
||||
#define MICROPY_INCLUDED_STM_COMMON_HAL_RTC_RTC_H
|
||||
|
||||
extern void rtc_init(void);
|
||||
extern void rtc_reset(void);
|
||||
|
||||
#endif // MICROPY_INCLUDED_STM_COMMON_HAL_RTC_RTC_H
|
|
@ -24,7 +24,7 @@ ifeq ($(MCU_SERIES),F4)
|
|||
CIRCUITPY_I2CPERIPHERAL ?= 0
|
||||
CIRCUITPY_NVM ?= 0
|
||||
CIRCUITPY_ROTARYIO ?= 0
|
||||
CIRCUITPY_RTC ?= 0
|
||||
CIRCUITPY_RTC ?= 1
|
||||
USB_NUM_ENDPOINT_PAIRS = 4
|
||||
UF2_FAMILY_ID ?= 0x57755a57
|
||||
endif
|
||||
|
@ -42,7 +42,7 @@ ifeq ($(MCU_SERIES),H7)
|
|||
CIRCUITPY_PULSEIO ?= 0
|
||||
CIRCUITPY_PWMIO ?= 0
|
||||
CIRCUITPY_ROTARYIO ?= 0
|
||||
CIRCUITPY_RTC ?= 0
|
||||
CIRCUITPY_RTC ?= 1
|
||||
|
||||
USB_NUM_ENDPOINT_PAIRS = 9
|
||||
UF2_FAMILY_ID ?= 0x6db66082
|
||||
|
@ -59,7 +59,7 @@ ifeq ($(MCU_SERIES),F7)
|
|||
CIRCUITPY_NEOPIXEL_WRITE ?= 0
|
||||
CIRCUITPY_NVM ?= 0
|
||||
CIRCUITPY_ROTARYIO ?= 0
|
||||
CIRCUITPY_RTC ?= 0
|
||||
CIRCUITPY_RTC ?= 1
|
||||
|
||||
USB_NUM_ENDPOINT_PAIRS = 6
|
||||
UF2_FAMILY_ID ?= 0x53b80f00
|
||||
|
@ -76,7 +76,7 @@ ifeq ($(MCU_SERIES),L4)
|
|||
CIRCUITPY_NEOPIXEL_WRITE ?= 0
|
||||
CIRCUITPY_NVM ?= 0
|
||||
CIRCUITPY_ROTARYIO ?= 0
|
||||
CIRCUITPY_RTC ?= 0
|
||||
CIRCUITPY_RTC ?= 1
|
||||
# todo - this varies between devices in the series
|
||||
# This slide deck https://www.st.com/content/ccc/resource/training/technical/product_training/98/89/c8/6c/3e/e9/49/79/STM32L4_Peripheral_USB.pdf/files/STM32L4_Peripheral_USB.pdf/jcr:content/translations/en.STM32L4_Peripheral_USB.pdf
|
||||
# cites 16 endpoints, 8 endpoint pairs, while section 3.39 of the L4R5 datasheet states 6 endpoint pairs.
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2021 Lucian Copeland for Adafruit Industries
|
||||
* Copyright (c) 2022 Matthew McGowan for Blues Wireless Inc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -76,6 +77,46 @@ void stm32_peripherals_rtc_init(void) {
|
|||
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
|
||||
}
|
||||
|
||||
#if CIRCUITPY_RTC
|
||||
void stm32_peripherals_rtc_get_time(timeutils_struct_time_t *tm) {
|
||||
RTC_DateTypeDef date = {0};
|
||||
RTC_TimeTypeDef time = {0};
|
||||
|
||||
int code;
|
||||
if ((code = HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN)) == HAL_OK &&
|
||||
(code = HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN)) == HAL_OK) {
|
||||
tm->tm_hour = time.Hours;
|
||||
tm->tm_min = time.Minutes;
|
||||
tm->tm_sec = time.Seconds;
|
||||
tm->tm_wday = date.WeekDay - 1;
|
||||
tm->tm_mday = date.Date;
|
||||
tm->tm_mon = date.Month;
|
||||
tm->tm_year = date.Year + 2000;
|
||||
tm->tm_yday = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void stm32_peripherals_rtc_set_time(timeutils_struct_time_t *tm) {
|
||||
RTC_DateTypeDef date = {0};
|
||||
RTC_TimeTypeDef time = {0};
|
||||
|
||||
time.Hours = tm->tm_hour;
|
||||
time.Minutes = tm->tm_min;
|
||||
time.Seconds = tm->tm_sec;
|
||||
date.WeekDay = tm->tm_wday + 1;
|
||||
date.Date = tm->tm_mday;
|
||||
date.Month = tm->tm_mon;
|
||||
date.Year = tm->tm_year - 2000;
|
||||
time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
|
||||
time.StoreOperation = RTC_STOREOPERATION_RESET;
|
||||
|
||||
if (HAL_RTC_SetTime(&hrtc, &time, RTC_FORMAT_BIN) != HAL_OK ||
|
||||
HAL_RTC_SetDate(&hrtc, &date, RTC_FORMAT_BIN) != HAL_OK) {
|
||||
// todo - throw an exception
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// This function is called often for timing so we cache the seconds elapsed computation based on the
|
||||
// register value. The STM HAL always does shifts and conversion if we use it directly.
|
||||
uint64_t stm32_peripherals_rtc_raw_ticks(uint8_t *subticks) {
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2021 Lucian Copeland for Adafruit Industries
|
||||
* Copyright (c) 2022 Matthew McGowan for Blues Wireless Inc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -48,4 +49,10 @@ void stm32_peripherals_rtc_assign_alarm_callback(uint8_t alarm_idx, void (*callb
|
|||
void stm32_peripherals_rtc_set_alarm(uint8_t alarm_idx, uint32_t ticks);
|
||||
bool stm32_peripherals_rtc_alarm_triggered(uint8_t alarm_idx);
|
||||
|
||||
#if CIRCUITPY_RTC
|
||||
typedef struct _timeutils_struct_time_t timeutils_struct_time_t;
|
||||
void stm32_peripherals_rtc_get_time(timeutils_struct_time_t *tm);
|
||||
void stm32_peripherals_rtc_set_time(timeutils_struct_time_t *tm);
|
||||
#endif
|
||||
|
||||
#endif // __MICROPY_INCLUDED_STM32_PERIPHERALS_RTC_H__
|
||||
|
|
|
@ -61,6 +61,9 @@
|
|||
#if CIRCUITPY_ALARM
|
||||
#include "common-hal/alarm/__init__.h"
|
||||
#endif
|
||||
#if CIRCUITPY_RTC
|
||||
#include "shared-bindings/rtc/__init__.h"
|
||||
#endif
|
||||
|
||||
#include "peripherals/clocks.h"
|
||||
#include "peripherals/gpio.h"
|
||||
|
@ -241,6 +244,10 @@ void SysTick_Handler(void) {
|
|||
|
||||
void reset_port(void) {
|
||||
reset_all_pins();
|
||||
#if CIRCUITPY_RTC
|
||||
rtc_reset();
|
||||
#endif
|
||||
|
||||
#if CIRCUITPY_AUDIOPWMIO
|
||||
audiopwmout_reset();
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue