提交 2ee400f6 编写于 作者: R Rbb666 提交者: guo

Add RTC device

上级 ac92106a
...@@ -38,6 +38,9 @@ if GetDepend(['BSP_USING_SPI']): ...@@ -38,6 +38,9 @@ if GetDepend(['BSP_USING_SPI']):
if GetDepend(['BSP_USING_ADC']): if GetDepend(['BSP_USING_ADC']):
src += ['drv_adc.c'] src += ['drv_adc.c']
if GetDepend('BSP_USING_RTC'):
src += ['drv_rtc.c']
if GetDepend(['RT_USING_WDT']): if GetDepend(['RT_USING_WDT']):
src += ['drv_wdt.c'] src += ['drv_wdt.c']
......
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-07-25 Rbb666 first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <sys/time.h>
#include "drv_common.h"
#ifdef BSP_USING_RTC
//#define DRV_DEBUG
#define LOG_TAG "drv.rtc"
#include <drv_log.h>
cyhal_rtc_t rtc_obj;
static rt_rtc_dev_t ifx32_rtc_dev;
static int get_day_of_week(int day, int month, int year)
{
int ret;
int k = 0;
int j = 0;
if (month < CY_RTC_MARCH)
{
month += CY_RTC_MONTHS_PER_YEAR;
year--;
}
k = (year % 100);
j = (year / 100);
ret = (day + (13 * (month + 1) / 5) + k + (k / 4) + (j / 4) + (5 * j)) % 7;
ret = ((ret + 6) % 7);
return ret;
}
static rt_err_t set_rtc_time_stamp(time_t time_stamp)
{
struct tm tm = {0};
struct tm new_time = {0};
gmtime_r(&time_stamp, &tm);
if (tm.tm_year < 100)
{
return -RT_ERROR;
}
new_time.tm_sec = tm.tm_sec ;
new_time.tm_min = tm.tm_min ;
new_time.tm_hour = tm.tm_hour;
new_time.tm_mday = tm.tm_mday;
new_time.tm_mon = tm.tm_mon;
new_time.tm_year = tm.tm_year;
new_time.tm_wday = get_day_of_week(tm.tm_mday, tm.tm_mon, tm.tm_year);
if (cyhal_rtc_write(&rtc_obj, &new_time) != RT_EOK)
{
return -RT_ERROR;
}
LOG_D("set rtc time.");
return RT_EOK;
}
static rt_err_t ifx_rtc_get_timeval(struct timeval *tv)
{
struct tm tm_new = {0};
struct tm date_time = {0};
cyhal_rtc_read(&rtc_obj, &date_time);
tm_new.tm_sec = date_time.tm_sec;
tm_new.tm_min = date_time.tm_min;
tm_new.tm_hour = date_time.tm_hour;
tm_new.tm_mday = date_time.tm_mday;
tm_new.tm_mon = date_time.tm_mon;
tm_new.tm_year = date_time.tm_year;
tv->tv_sec = timegm(&tm_new);
return RT_EOK;
}
static rt_err_t _rtc_init(void)
{
if (cyhal_rtc_init(&rtc_obj) != RT_EOK)
{
LOG_E("rtc init failed.");
return -RT_ERROR;
}
return RT_EOK;
}
static rt_err_t _rtc_get_secs(time_t *sec)
{
struct timeval tv;
ifx_rtc_get_timeval(&tv);
*(time_t *) sec = tv.tv_sec;
LOG_D("RTC: get rtc_time %d", *sec);
return RT_EOK;
}
static rt_err_t _rtc_set_secs(time_t *sec)
{
rt_err_t result = RT_EOK;
if (set_rtc_time_stamp(*sec))
{
result = -RT_ERROR;
}
LOG_D("RTC: set rtc_time %d", *sec);
return result;
}
static const struct rt_rtc_ops _rtc_ops =
{
_rtc_init,
_rtc_get_secs,
_rtc_set_secs,
RT_NULL,
RT_NULL,
ifx_rtc_get_timeval,
RT_NULL,
};
/**
* @brief RTC initialization function.
*
* @return RT_EOK indicates successful initialization, other value indicates failed;
*/
static int rt_hw_rtc_init(void)
{
rt_err_t result = RT_EOK;
ifx32_rtc_dev.ops = &_rtc_ops;
if (rt_hw_rtc_register(&ifx32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL) != RT_EOK)
{
LOG_E("rtc init failed");
result = RT_ERROR;
}
else
{
LOG_D("rtc init success");
}
return result;
}
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
#endif
...@@ -67,7 +67,6 @@ static void uart_isr(struct rt_serial_device *serial) ...@@ -67,7 +67,6 @@ static void uart_isr(struct rt_serial_device *serial)
struct ifx_uart *uart = (struct ifx_uart *) serial->parent.user_data; struct ifx_uart *uart = (struct ifx_uart *) serial->parent.user_data;
RT_ASSERT(uart != RT_NULL); RT_ASSERT(uart != RT_NULL);
#ifdef BSP_USING_UART5
if ((uart->config->usart_x->INTR_RX_MASKED & SCB_INTR_RX_MASKED_NOT_EMPTY_Msk) != 0) if ((uart->config->usart_x->INTR_RX_MASKED & SCB_INTR_RX_MASKED_NOT_EMPTY_Msk) != 0)
{ {
/* Clear UART "RX fifo not empty interrupt" */ /* Clear UART "RX fifo not empty interrupt" */
...@@ -75,7 +74,6 @@ static void uart_isr(struct rt_serial_device *serial) ...@@ -75,7 +74,6 @@ static void uart_isr(struct rt_serial_device *serial)
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND); rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
} }
#endif
} }
#ifdef BSP_USING_UART0 #ifdef BSP_USING_UART0
...@@ -162,16 +160,28 @@ void uart5_isr_callback(void) ...@@ -162,16 +160,28 @@ void uart5_isr_callback(void)
*/ */
static rt_err_t ifx_configure(struct rt_serial_device *serial, struct serial_configure *cfg) static rt_err_t ifx_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{ {
struct ifx_uart *uart = (struct ifx_uart *) serial->parent.user_data;
RT_ASSERT(serial != RT_NULL); RT_ASSERT(serial != RT_NULL);
struct ifx_uart *uart = (struct ifx_uart *) serial->parent.user_data;
RT_ASSERT(uart != RT_NULL); RT_ASSERT(uart != RT_NULL);
cy_en_scb_uart_status_t result; cy_en_scb_uart_status_t result;
const cyhal_uart_cfg_t uart_config =
{
.data_bits = 8,
.stop_bits = 1,
.parity = CYHAL_UART_PARITY_NONE,
.rx_buffer = NULL,
.rx_buffer_size = 0
};
/* Initialize retarget-io to use the debug UART port */ /* Initialize retarget-io to use the debug UART port */
result = cy_retarget_io_init(uart->config->tx_pin, uart->config->rx_pin, result = cyhal_uart_init(uart->config->uart_obj, uart->config->tx_pin, uart->config->rx_pin, NC, NC, NULL, &uart_config);
CY_RETARGET_IO_BAUDRATE);
if (result == CY_RSLT_SUCCESS)
{
result = cyhal_uart_set_baud(uart->config->uart_obj, cfg->baud_rate, NULL);
}
RT_ASSERT(result != RT_ERROR); RT_ASSERT(result != RT_ERROR);
...@@ -229,10 +239,13 @@ static int ifx_uarths_getc(struct rt_serial_device *serial) ...@@ -229,10 +239,13 @@ static int ifx_uarths_getc(struct rt_serial_device *serial)
{ {
int ch; int ch;
rt_uint8_t read_data; rt_uint8_t read_data;
RT_ASSERT(serial != RT_NULL); RT_ASSERT(serial != RT_NULL);
struct ifx_uart *uart = (struct ifx_uart *) serial->parent.user_data;
RT_ASSERT(uart != RT_NULL);
ch = -1; ch = -1;
if (RT_EOK == cyhal_uart_getc(&cy_retarget_io_uart_obj, (uint8_t *)&read_data, 1)) if (RT_EOK == cyhal_uart_getc(uart->config->uart_obj, (uint8_t *)&read_data, 1))
{ {
ch = read_data & 0xff; ch = read_data & 0xff;
} }
...@@ -258,15 +271,17 @@ void rt_hw_uart_init(void) ...@@ -258,15 +271,17 @@ void rt_hw_uart_init(void)
int index; int index;
rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct ifx_uart); rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct ifx_uart);
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; struct serial_configure serial_config = RT_SERIAL_CONFIG_DEFAULT;
rt_err_t result = 0; rt_err_t result = 0;
for (index = 0; index < obj_num; index++) for (index = 0; index < obj_num; index++)
{ {
uart_obj[index].config = &uart_config[index]; uart_obj[index].config = &uart_config[index];
uart_obj[index].serial.ops = &_uart_ops; uart_obj[index].serial.ops = &_uart_ops;
uart_obj[index].serial.config = config; uart_obj[index].serial.config = serial_config;
uart_obj[index].config->uart_obj = rt_malloc(sizeof(cyhal_uart_t));
RT_ASSERT(uart_obj[index].config->uart_obj != RT_NULL);
/* register uart device */ /* register uart device */
result = rt_hw_serial_register(&uart_obj[index].serial, result = rt_hw_serial_register(&uart_obj[index].serial,
uart_obj[index].config->name, uart_obj[index].config->name,
......
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
struct ifx_uart_config struct ifx_uart_config
{ {
cyhal_uart_t *uart_obj;
const char *name; const char *name;
rt_uint32_t tx_pin; rt_uint32_t tx_pin;
rt_uint32_t rx_pin; rt_uint32_t rx_pin;
......
...@@ -95,6 +95,10 @@ if GetDepend(['RT_USING_SPI']): ...@@ -95,6 +95,10 @@ if GetDepend(['RT_USING_SPI']):
if GetDepend(['RT_USING_I2C']): if GetDepend(['RT_USING_I2C']):
src += ['mtb-hal-cat1/source/cyhal_i2c.c'] src += ['mtb-hal-cat1/source/cyhal_i2c.c']
if GetDepend('BSP_USING_RTC'):
src += ['mtb-pdl-cat1/drivers/source/cy_rtc.c']
src += ['mtb-hal-cat1/source/cyhal_rtc.c']
if GetDepend(['RT_USING_WDT']): if GetDepend(['RT_USING_WDT']):
src += ['mtb-pdl-cat1/drivers/source/cy_wdt.c'] src += ['mtb-pdl-cat1/drivers/source/cy_wdt.c']
src += ['mtb-hal-cat1/source/cyhal_wdt.c'] src += ['mtb-hal-cat1/source/cyhal_wdt.c']
......
...@@ -9,9 +9,7 @@ ...@@ -9,9 +9,7 @@
* *
******************************************************************************** ********************************************************************************
* \copyright * \copyright
* Copyright 2018-2021 Cypress Semiconductor Corporation (an Infineon company) or * Copyright 2018-2021 Cypress Semiconductor Corporation
* an affiliate of Cypress Semiconductor Corporation
*
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
...@@ -72,6 +70,12 @@ ...@@ -72,6 +70,12 @@
extern "C" { extern "C" {
#endif #endif
/** \cond INTERNAL */
/** Generate a result code specific to the HAL driver */
#define CYHAL_RSLT_CREATE(type, driver, code) \
(CY_RSLT_CREATE(type, CY_RSLT_MODULE_ABSTRACTION_HAL, ((uint16_t)driver | (uint16_t)code)))
/** \endcond */
/** \addtogroup group_hal_results_rtc RTC HAL Results /** \addtogroup group_hal_results_rtc RTC HAL Results
* RTC specific return codes * RTC specific return codes
* \ingroup group_hal_results * \ingroup group_hal_results
...@@ -80,10 +84,10 @@ extern "C" { ...@@ -80,10 +84,10 @@ extern "C" {
/** RTC not initialized */ /** RTC not initialized */
#define CY_RSLT_RTC_NOT_INITIALIZED \ #define CY_RSLT_RTC_NOT_INITIALIZED \
(CY_RSLT_CREATE_EX(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_HAL, CYHAL_RSLT_MODULE_RTC, 0)) (CYHAL_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_RTC, 0))
/** Bad argument */ /** Bad argument */
#define CY_RSLT_RTC_BAD_ARGUMENT \ #define CY_RSLT_RTC_BAD_ARGUMENT \
(CY_RSLT_CREATE_EX(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_HAL, CYHAL_RSLT_MODULE_RTC, 1)) (CYHAL_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_RTC, 1))
/** /**
* \} * \}
...@@ -145,23 +149,6 @@ typedef struct ...@@ -145,23 +149,6 @@ typedef struct
typedef void (*cyhal_rtc_event_callback_t)(void *callback_arg, cyhal_rtc_event_t event); typedef void (*cyhal_rtc_event_callback_t)(void *callback_arg, cyhal_rtc_event_t event);
/** Initialize the RTC peripheral /** Initialize the RTC peripheral
*
* Power up the RTC in preparation for access. This function must be called
* before any other RTC functions are called. This does not change the state
* of the RTC. It just enables access to it.
* @note Before calling this, make sure all necessary System Clocks are setup
* correctly. Generally this means making sure the RTC has access to a crystal
* oscillator for optimal accuracy and operation in low power.
* @note Previously set time configurations are retained. This will only reset
* the time if no prior configuration can be determined.
*
* @param[out] obj Pointer to an RTC object. The caller must allocate the memory
* for this object but the init function will initialize its contents.
* @return The status of the init request
*/
cy_rslt_t cyhal_rtc_init(cyhal_rtc_t *obj);
/** Initialize the RTC peripheral using a configurator generated configuration struct
* *
* Power up the RTC in preparation for access. This function must be called * Power up the RTC in preparation for access. This function must be called
* before any other RTC functions are called. This does not change the state * before any other RTC functions are called. This does not change the state
...@@ -174,10 +161,9 @@ cy_rslt_t cyhal_rtc_init(cyhal_rtc_t *obj); ...@@ -174,10 +161,9 @@ cy_rslt_t cyhal_rtc_init(cyhal_rtc_t *obj);
* *
* @param[out] obj Pointer to an RTC object. The caller must allocate the memory * @param[out] obj Pointer to an RTC object. The caller must allocate the memory
* for this object but the init function will initialize its contents. * for this object but the init function will initialize its contents.
* @param[in] cfg Configuration structure generated by a configurator.
* @return The status of the init request * @return The status of the init request
*/ */
cy_rslt_t cyhal_rtc_init_cfg(cyhal_rtc_t *obj, const cyhal_rtc_configurator_t *cfg); cy_rslt_t cyhal_rtc_init(cyhal_rtc_t *obj);
/** Deinitialize RTC /** Deinitialize RTC
* *
...@@ -213,25 +199,7 @@ cy_rslt_t cyhal_rtc_read(cyhal_rtc_t *obj, struct tm *time); ...@@ -213,25 +199,7 @@ cy_rslt_t cyhal_rtc_read(cyhal_rtc_t *obj, struct tm *time);
*/ */
cy_rslt_t cyhal_rtc_write(cyhal_rtc_t *obj, const struct tm *time); cy_rslt_t cyhal_rtc_write(cyhal_rtc_t *obj, const struct tm *time);
/** Write the specified time and date values to the RTC peripheral
* @param[in] obj RTC object
* @param[in] sec Second to set (0-59)
* @param[in] min Minute to set (0-59)
* @param[in] hour Hour to set (0-23)
* @param[in] day Day of month to set (1-31)
* @param[in] month Month to set (1-12)
* @param[in] year 4-digit year to set
* @return The status of the write request
*/
cy_rslt_t cyhal_rtc_write_direct(cyhal_rtc_t *obj, uint32_t sec, uint32_t min, uint32_t hour,
uint32_t day, uint32_t month, uint32_t year);
/** Set the start and end time for Day Light Savings /** Set the start and end time for Day Light Savings
*
* Calling this function will allow alarms to account for daylight saving time.
* This means that the RTC will be adjusted when a daylight saving time
* transition occurs, meaning times passed to \ref cyhal_rtc_set_alarm()
* will be interpreted as being in DST/not in DST as appropriate.
* *
* @param[in] obj RTC object * @param[in] obj RTC object
* @param[in] start When Day Light Savings time should start * @param[in] start When Day Light Savings time should start
......
...@@ -2,16 +2,14 @@ ...@@ -2,16 +2,14 @@
* \file cyhal_rtc.c * \file cyhal_rtc.c
* *
* \brief * \brief
* Provides a high level interface for interacting with the Infineon Real-Time Clock. * Provides a high level interface for interacting with the Cypress Real-Time Clock.
* This interface abstracts out the chip specific details. If any chip specific * This interface abstracts out the chip specific details. If any chip specific
* functionality is necessary, or performance is critical the low level functions * functionality is necessary, or performance is critical the low level functions
* can be used directly. * can be used directly.
* *
******************************************************************************** ********************************************************************************
* \copyright * \copyright
* Copyright 2018-2021 Cypress Semiconductor Corporation (an Infineon company) or * Copyright 2018-2021 Cypress Semiconductor Corporation
* an affiliate of Cypress Semiconductor Corporation
*
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
...@@ -31,15 +29,13 @@ ...@@ -31,15 +29,13 @@
#include "cy_utils.h" #include "cy_utils.h"
#include "cyhal_rtc.h" #include "cyhal_rtc.h"
#include "cyhal_system.h" #include "cyhal_system.h"
#include "cyhal_utils_psoc.h"
#include "cyhal_irq_psoc.h"
/** /**
* \addtogroup group_hal_impl_rtc RTC (Real Time Clock) * \addtogroup group_hal_impl_rtc RTC (Real Time Clock)
* \ingroup group_hal_impl * \ingroup group_hal_impl
* \{ * \{
* *
* Internally the CAT1 (PSoCâ„¢ 6) RTC only stores the year as a two digit BCD value * Internally the CAT1 (PSoC 6) RTC only stores the year as a two digit BCD value
* (0-99); no century information is stored. On RTC initialization the HAL must, * (0-99); no century information is stored. On RTC initialization the HAL must,
* as a result, assume a default century. If cyhal_rtc_write has been called * as a result, assume a default century. If cyhal_rtc_write has been called
* with a different century than the default, its value must be stored and that * with a different century than the default, its value must be stored and that
...@@ -60,7 +56,7 @@ ...@@ -60,7 +56,7 @@
* \} group_hal_impl_wdt * \} group_hal_impl_wdt
*/ */
#if (CYHAL_DRIVER_AVAILABLE_RTC) #if (defined(CY_IP_MXS40SSRSS) || defined(CY_IP_MXS40SRSS)) && SRSS_BACKUP_PRESENT
#if defined(__cplusplus) #if defined(__cplusplus)
extern "C" { extern "C" {
...@@ -69,66 +65,22 @@ extern "C" { ...@@ -69,66 +65,22 @@ extern "C" {
#define _CYHAL_RTC_STATE_UNINITIALIZED 0 #define _CYHAL_RTC_STATE_UNINITIALIZED 0
#define _CYHAL_RTC_STATE_ENABLED 1 #define _CYHAL_RTC_STATE_ENABLED 1
#define _CYHAL_RTC_STATE_TIME_SET 2 #define _CYHAL_RTC_STATE_TIME_SET 2
#if (defined(COMPONENT_CAT1C) && (CORE == CM0P))
// To account for the lower __NVIC_PRIO_BITS value
#define _CYHAL_RTC_DEFAULT_PRIORITY 3
#else
#define _CYHAL_RTC_DEFAULT_PRIORITY 5 #define _CYHAL_RTC_DEFAULT_PRIORITY 5
#endif // (defined(COMPONENT_CAT1C) && (CORE == CM0P))
#define _CYHAL_RTC_INIT_CENTURY 2000 #define _CYHAL_RTC_INIT_CENTURY 2000
#define _CYHAL_RTC_TM_YEAR_BASE 1900 #define _CYHAL_RTC_TM_YEAR_BASE 1900
#if defined(COMPONENT_CAT1A) || defined (COMPONENT_CAT1C)
#define _CYHAL_RTC_BREG (BACKUP->BREG[SRSS_BACKUP_NUM_BREG-1]) #define _CYHAL_RTC_BREG (BACKUP->BREG[SRSS_BACKUP_NUM_BREG-1])
#elif defined(COMPONENT_CAT1B)
#if defined(SRSS_BACKUP_NUM_BREG3) && (SRSS_BACKUP_NUM_BREG3 > 0)
#define _CYHAL_RTC_BREG (BACKUP->BREG_SET3[SRSS_BACKUP_NUM_BREG3-1])
#elif defined(SRSS_BACKUP_NUM_BREG2) && (SRSS_BACKUP_NUM_BREG2 > 0)
#define _CYHAL_RTC_BREG (BACKUP->BREG_SET2[SRSS_BACKUP_NUM_BREG2-1])
#elif defined(SRSS_BACKUP_NUM_BREG1) && (SRSS_BACKUP_NUM_BREG1 > 0)
#define _CYHAL_RTC_BREG (BACKUP->BREG_SET1[SRSS_BACKUP_NUM_BREG1-1])
#elif defined(SRSS_BACKUP_NUM_BREG0) && (SRSS_BACKUP_NUM_BREG0 > 0)
#define _CYHAL_RTC_BREG (BACKUP->BREG_SET0[SRSS_BACKUP_NUM_BREG0-1])
#endif
#endif /* defined(COMPONENT_CAT1B) */
#define _CYHAL_RTC_BREG_CENTURY_Pos 0UL #define _CYHAL_RTC_BREG_CENTURY_Pos 0UL
#define _CYHAL_RTC_BREG_CENTURY_Msk 0x0000FFFFUL #define _CYHAL_RTC_BREG_CENTURY_Msk 0x0000FFFFUL
#define _CYHAL_RTC_BREG_STATE_Pos 16UL #define _CYHAL_RTC_BREG_STATE_Pos 16UL
#define _CYHAL_RTC_BREG_STATE_Msk 0xFFFF0000UL #define _CYHAL_RTC_BREG_STATE_Msk 0xFFFF0000UL
static const uint32_t _CYHAL_RTC_MAX_RETRY = 10;
static const uint32_t _CYHAL_RTC_RETRY_DELAY_MS = 1;
// Note: Use PDL directly rather than HAL. RTOS-aware delay is not needed and actually breaks functionality.
#define _CYHAL_RTC_WAIT_ONE_MS() Cy_SysLib_Delay(_CYHAL_RTC_RETRY_DELAY_MS);
static void _cyhal_rtc_from_pdl_time(cy_stc_rtc_config_t *pdlTime, const int year, struct tm *time) {
CY_ASSERT(NULL != pdlTime);
CY_ASSERT(NULL != time);
// The number of days that precede each month of the year, not including Feb 29
static const uint16_t CUMULATIVE_DAYS[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
time->tm_sec = (int)pdlTime->sec;
time->tm_min = (int)pdlTime->min;
time->tm_hour = (int)pdlTime->hour;
time->tm_mday = (int)pdlTime->date;
time->tm_mon = (int)(pdlTime->month - 1u);
time->tm_year = (int)(year - _CYHAL_RTC_TM_YEAR_BASE);
time->tm_wday = (int)(pdlTime->dayOfWeek - 1u);
time->tm_yday = (int)CUMULATIVE_DAYS[time->tm_mon] + (int)pdlTime->date - 1 +
(((int)(pdlTime->month) >= 3 && (int)(Cy_RTC_IsLeapYear((uint32_t)year) ? 1u : 0u)));
time->tm_isdst = -1;
}
/** Wrapper around the PDL Cy_RTC_DeepSleepCallback to adapt the function signature */ /** Wrapper around the PDL Cy_RTC_DeepSleepCallback to adapt the function signature */
static cy_en_syspm_status_t _cyhal_rtc_syspm_callback(cy_stc_syspm_callback_params_t *params, cy_en_syspm_callback_mode_t mode) static cy_en_syspm_status_t _cyhal_rtc_syspm_callback(cy_stc_syspm_callback_params_t *params, cy_en_syspm_callback_mode_t mode)
{ {
return Cy_RTC_DeepSleepCallback(params, mode); return Cy_RTC_DeepSleepCallback(params, mode);
} }
static bool _cyhal_rtc_dst_skip_next_alarm = false;
static cy_stc_rtc_dst_t *_cyhal_rtc_dst; static cy_stc_rtc_dst_t *_cyhal_rtc_dst;
static cy_stc_syspm_callback_params_t _cyhal_rtc_pm_cb_params = {NULL, NULL}; static cy_stc_syspm_callback_params_t _cyhal_rtc_pm_cb_params = {NULL, NULL};
static cy_stc_syspm_callback_t _cyhal_rtc_pm_cb = { static cy_stc_syspm_callback_t _cyhal_rtc_pm_cb = {
...@@ -185,11 +137,7 @@ static void _cyhal_rtc_isr_handler(void) ...@@ -185,11 +137,7 @@ static void _cyhal_rtc_isr_handler(void)
/* Override weak function from PDL */ /* Override weak function from PDL */
void Cy_RTC_Alarm1Interrupt(void) void Cy_RTC_Alarm1Interrupt(void)
{ {
if (_cyhal_rtc_dst_skip_next_alarm) if (NULL != _cyhal_rtc_user_handler)
{
_cyhal_rtc_dst_skip_next_alarm = false;
}
else if (NULL != _cyhal_rtc_user_handler)
{ {
(*_cyhal_rtc_user_handler)(_cyhal_rtc_handler_arg, CYHAL_RTC_ALARM); (*_cyhal_rtc_user_handler)(_cyhal_rtc_handler_arg, CYHAL_RTC_ALARM);
} }
...@@ -200,15 +148,23 @@ void Cy_RTC_CenturyInterrupt(void) ...@@ -200,15 +148,23 @@ void Cy_RTC_CenturyInterrupt(void)
_cyhal_rtc_set_century(_cyhal_rtc_get_century() + 100); _cyhal_rtc_set_century(_cyhal_rtc_get_century() + 100);
} }
static cy_rslt_t _cyhal_rtc_init_common(const cy_stc_rtc_config_t* default_time) cy_rslt_t cyhal_rtc_init(cyhal_rtc_t *obj)
{ {
CY_UNUSED_PARAMETER(obj);
CY_ASSERT(NULL != obj);
cy_rslt_t rslt = CY_RSLT_SUCCESS; cy_rslt_t rslt = CY_RSLT_SUCCESS;
if (_cyhal_rtc_get_state() == _CYHAL_RTC_STATE_UNINITIALIZED) if (_cyhal_rtc_get_state() == _CYHAL_RTC_STATE_UNINITIALIZED)
{ {
if (Cy_RTC_IsExternalResetOccurred()) if (Cy_RTC_IsExternalResetOccurred())
{ {
// Reset to default time // Reset to default time
Cy_RTC_SetDateAndTime(default_time); static const cy_stc_rtc_config_t defaultTime = {
.dayOfWeek = CY_RTC_SATURDAY,
.date = 1,
.month = 1,
.year = 0,
};
Cy_RTC_SetDateAndTime(&defaultTime);
_cyhal_rtc_set_century(_CYHAL_RTC_INIT_CENTURY); _cyhal_rtc_set_century(_CYHAL_RTC_INIT_CENTURY);
} }
...@@ -223,65 +179,29 @@ static cy_rslt_t _cyhal_rtc_init_common(const cy_stc_rtc_config_t* default_time) ...@@ -223,65 +179,29 @@ static cy_rslt_t _cyhal_rtc_init_common(const cy_stc_rtc_config_t* default_time)
} }
else if(_cyhal_rtc_get_state() == _CYHAL_RTC_STATE_ENABLED || _cyhal_rtc_get_state() == _CYHAL_RTC_STATE_TIME_SET) else if(_cyhal_rtc_get_state() == _CYHAL_RTC_STATE_ENABLED || _cyhal_rtc_get_state() == _CYHAL_RTC_STATE_TIME_SET)
{ {
if (Cy_RTC_GetInterruptStatus() & CY_RTC_INTR_CENTURY) if(Cy_RTC_GetInterruptStatus() & CY_RTC_INTR_CENTURY)
Cy_RTC_CenturyInterrupt(); Cy_RTC_CenturyInterrupt();
} }
Cy_RTC_ClearInterrupt(CY_RTC_INTR_CENTURY); Cy_RTC_ClearInterrupt(CY_RTC_INTR_CENTURY);
Cy_RTC_SetInterruptMask(CY_RTC_INTR_CENTURY); Cy_RTC_SetInterruptMask(CY_RTC_INTR_CENTURY);
_cyhal_irq_register(srss_interrupt_backup_IRQn, _CYHAL_RTC_DEFAULT_PRIORITY, _cyhal_rtc_isr_handler); static const cy_stc_sysint_t irqCfg = {.intrSrc = srss_interrupt_backup_IRQn, .intrPriority = _CYHAL_RTC_DEFAULT_PRIORITY};
Cy_SysInt_Init(&irqCfg, &_cyhal_rtc_isr_handler);
if (rslt == CY_RSLT_SUCCESS) if (rslt == CY_RSLT_SUCCESS)
{ {
_cyhal_rtc_dst = NULL; _cyhal_rtc_dst = NULL;
_cyhal_irq_enable(srss_interrupt_backup_IRQn); NVIC_EnableIRQ(srss_interrupt_backup_IRQn);
} }
return rslt; return rslt;
} }
cy_rslt_t cyhal_rtc_init(cyhal_rtc_t *obj)
{
CY_UNUSED_PARAMETER(obj);
CY_ASSERT(NULL != obj);
static const cy_stc_rtc_config_t default_time =
{
.dayOfWeek = CY_RTC_SATURDAY,
.date = 1,
.month = 1,
.year = 0,
};
return _cyhal_rtc_init_common(&default_time);
}
cy_rslt_t cyhal_rtc_init_cfg(cyhal_rtc_t *obj, const cyhal_rtc_configurator_t *cfg)
{
CY_UNUSED_PARAMETER(obj);
CY_ASSERT(NULL != obj);
cy_rslt_t rslt = _cyhal_rtc_init_common(cfg->config);
if (NULL != cfg->dst_config)
{
_cyhal_rtc_set_state(_CYHAL_RTC_STATE_TIME_SET);
cy_stc_rtc_config_t dateTime;
Cy_RTC_GetDateAndTime(&dateTime);
rslt = Cy_RTC_EnableDstTime(cfg->dst_config, &dateTime);
if (rslt == CY_RSLT_SUCCESS)
{
obj->dst = *(cfg->dst_config);
_cyhal_rtc_dst = &(obj->dst);
}
}
return rslt;
}
void cyhal_rtc_free(cyhal_rtc_t *obj) void cyhal_rtc_free(cyhal_rtc_t *obj)
{ {
CY_UNUSED_PARAMETER(obj); CY_UNUSED_PARAMETER(obj);
CY_ASSERT(NULL != obj); CY_ASSERT(NULL != obj);
_cyhal_irq_free(srss_interrupt_backup_IRQn); NVIC_DisableIRQ(srss_interrupt_backup_IRQn);
Cy_RTC_SetInterruptMask(CY_RTC_INTR_CENTURY); Cy_RTC_SetInterruptMask(CY_RTC_INTR_CENTURY);
_cyhal_rtc_dst = NULL; _cyhal_rtc_dst = NULL;
...@@ -298,66 +218,68 @@ cy_rslt_t cyhal_rtc_read(cyhal_rtc_t *obj, struct tm *time) ...@@ -298,66 +218,68 @@ cy_rslt_t cyhal_rtc_read(cyhal_rtc_t *obj, struct tm *time)
{ {
CY_UNUSED_PARAMETER(obj); CY_UNUSED_PARAMETER(obj);
CY_ASSERT(NULL != obj); CY_ASSERT(NULL != obj);
// The number of days that precede each month of the year, not including Feb 29
static const uint16_t CUMULATIVE_DAYS[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
cy_stc_rtc_config_t dateTime = { .hrFormat = CY_RTC_24_HOURS }; cy_stc_rtc_config_t dateTime;
uint32_t savedIntrStatus = cyhal_system_critical_section_enter(); uint32_t savedIntrStatus = cyhal_system_critical_section_enter();
Cy_RTC_GetDateAndTime(&dateTime); Cy_RTC_GetDateAndTime(&dateTime);
const int year = (int)(dateTime.year + _cyhal_rtc_get_century()); int year = (int)(dateTime.year + _cyhal_rtc_get_century());
cyhal_system_critical_section_exit(savedIntrStatus); cyhal_system_critical_section_exit(savedIntrStatus);
_cyhal_rtc_from_pdl_time(&dateTime, year, time); time->tm_sec = (int)dateTime.sec;
time->tm_min = (int)dateTime.min;
time->tm_hour = (int)dateTime.hour;
time->tm_mday = (int)dateTime.date;
time->tm_mon = (int)(dateTime.month - 1u);
time->tm_year = (int)(year - _CYHAL_RTC_TM_YEAR_BASE);
time->tm_wday = (int)(dateTime.dayOfWeek - 1u);
time->tm_yday = (int)CUMULATIVE_DAYS[time->tm_mon] + (int)dateTime.date - 1 +
(((int)(dateTime.month) >= 3 && (int)(Cy_RTC_IsLeapYear((uint32_t)year) ? 1u : 0u)));
time->tm_isdst = -1;
return CY_RSLT_SUCCESS; return CY_RSLT_SUCCESS;
} }
cy_rslt_t cyhal_rtc_write(cyhal_rtc_t *obj, const struct tm *time) cy_rslt_t cyhal_rtc_write(cyhal_rtc_t *obj, const struct tm *time)
{
CY_ASSERT(NULL != obj);
return cyhal_rtc_write_direct(obj, time->tm_sec, time->tm_min, time->tm_hour, time->tm_mday,
time->tm_mon + 1, _CYHAL_RTC_TM_YEAR_BASE + time->tm_year);
}
cy_rslt_t cyhal_rtc_write_direct(cyhal_rtc_t *obj, uint32_t sec, uint32_t min, uint32_t hour,
uint32_t day, uint32_t month, uint32_t year)
{ {
CY_UNUSED_PARAMETER(obj); CY_UNUSED_PARAMETER(obj);
uint32_t year2digit = year % 100; CY_ASSERT(NULL != obj);
uint32_t year2digit = time->tm_year % 100;
cy_stc_rtc_config_t newtime = {
.sec = (uint32_t)time->tm_sec,
.min = (uint32_t)time->tm_min,
.hour = (uint32_t)time->tm_hour,
.hrFormat = CY_RTC_24_HOURS,
.dayOfWeek = (uint32_t)time->tm_wday + 1,
.date = (uint32_t)time->tm_mday,
.month = (uint32_t)(time->tm_mon + 1),
.year = year2digit
};
cy_rslt_t rslt; cy_rslt_t rslt;
uint32_t retry = 0; uint32_t retry = 0;
if (!CY_RTC_IS_SEC_VALID(sec) || !CY_RTC_IS_MIN_VALID(min) || !CY_RTC_IS_HOUR_VALID(hour) || !CY_RTC_IS_MONTH_VALID(month) || !CY_RTC_IS_YEAR_SHORT_VALID(year2digit)) static const uint32_t MAX_RETRY = 10, RETRY_DELAY_MS = 1;
{ do {
return CY_RSLT_RTC_BAD_ARGUMENT;
}
do
{
if (retry != 0) if (retry != 0)
_CYHAL_RTC_WAIT_ONE_MS(); Cy_SysLib_Delay(RETRY_DELAY_MS);
uint32_t savedIntrStatus = cyhal_system_critical_section_enter(); uint32_t savedIntrStatus = cyhal_system_critical_section_enter();
rslt = Cy_RTC_SetDateAndTimeDirect(sec, min, hour, day, month, year2digit); rslt = (cy_rslt_t)Cy_RTC_SetDateAndTime(&newtime);
if (rslt == CY_RSLT_SUCCESS) if (rslt == CY_RSLT_SUCCESS)
_cyhal_rtc_set_century((uint16_t)(year) - (uint16_t)(year2digit)); _cyhal_rtc_set_century((uint16_t)(time->tm_year) - (uint16_t)(year2digit) + (uint16_t)(_CYHAL_RTC_TM_YEAR_BASE));
cyhal_system_critical_section_exit(savedIntrStatus); cyhal_system_critical_section_exit(savedIntrStatus);
++retry; ++retry;
} while (rslt == CY_RTC_INVALID_STATE && retry < _CYHAL_RTC_MAX_RETRY); } while (rslt == CY_RTC_INVALID_STATE && retry < MAX_RETRY);
retry = 0; while (CY_RTC_BUSY == Cy_RTC_GetSyncStatus()) { }
while (CY_RTC_BUSY == Cy_RTC_GetSyncStatus() && retry < _CYHAL_RTC_MAX_RETRY)
{
_CYHAL_RTC_WAIT_ONE_MS();
++retry;
}
if (rslt == CY_RSLT_SUCCESS) if (rslt == CY_RSLT_SUCCESS)
{
_cyhal_rtc_set_state(_CYHAL_RTC_STATE_TIME_SET); _cyhal_rtc_set_state(_CYHAL_RTC_STATE_TIME_SET);
}
return rslt; return rslt;
} }
cy_rslt_t cyhal_rtc_set_dst(cyhal_rtc_t *obj, const cyhal_rtc_dst_t *start, const cyhal_rtc_dst_t *stop) cy_rslt_t cyhal_rtc_set_dst(cyhal_rtc_t *obj, const cyhal_rtc_dst_t *start, const cyhal_rtc_dst_t *stop)
{ {
CY_UNUSED_PARAMETER(obj);
CY_ASSERT(NULL != obj); CY_ASSERT(NULL != obj);
CY_ASSERT(NULL != start); CY_ASSERT(NULL != start);
CY_ASSERT(NULL != stop); CY_ASSERT(NULL != stop);
...@@ -370,18 +292,16 @@ cy_rslt_t cyhal_rtc_set_dst(cyhal_rtc_t *obj, const cyhal_rtc_dst_t *start, cons ...@@ -370,18 +292,16 @@ cy_rslt_t cyhal_rtc_set_dst(cyhal_rtc_t *obj, const cyhal_rtc_dst_t *start, cons
cy_rslt_t rslt = Cy_RTC_EnableDstTime(&(obj->dst), &dateTime); cy_rslt_t rslt = Cy_RTC_EnableDstTime(&(obj->dst), &dateTime);
if (rslt == CY_RSLT_SUCCESS) if (rslt == CY_RSLT_SUCCESS)
_cyhal_rtc_dst = &(obj->dst); _cyhal_rtc_dst = &(obj->dst);
return rslt; return rslt;
} }
bool cyhal_rtc_is_dst(cyhal_rtc_t *obj) bool cyhal_rtc_is_dst(cyhal_rtc_t *obj)
{ {
CY_UNUSED_PARAMETER(obj);
CY_ASSERT(NULL != obj); CY_ASSERT(NULL != obj);
cy_stc_rtc_config_t dateTime; cy_stc_rtc_config_t dateTime;
Cy_RTC_GetDateAndTime(&dateTime); Cy_RTC_GetDateAndTime(&dateTime);
return Cy_RTC_GetDstStatus(_cyhal_rtc_dst, &dateTime); return Cy_RTC_GetDstStatus(&(obj->dst), &dateTime);
} }
cy_rslt_t cyhal_rtc_set_alarm(cyhal_rtc_t *obj, const struct tm *time, cyhal_alarm_active_t active) cy_rslt_t cyhal_rtc_set_alarm(cyhal_rtc_t *obj, const struct tm *time, cyhal_alarm_active_t active)
...@@ -389,9 +309,7 @@ cy_rslt_t cyhal_rtc_set_alarm(cyhal_rtc_t *obj, const struct tm *time, cyhal_ala ...@@ -389,9 +309,7 @@ cy_rslt_t cyhal_rtc_set_alarm(cyhal_rtc_t *obj, const struct tm *time, cyhal_ala
// Note: the hardware does not support year matching // Note: the hardware does not support year matching
CY_UNUSED_PARAMETER(obj); CY_UNUSED_PARAMETER(obj);
CY_ASSERT(NULL != obj); CY_ASSERT(NULL != obj);
_cyhal_rtc_dst_skip_next_alarm = false; cy_stc_rtc_alarm_t alarm = {
cy_stc_rtc_alarm_t alarm =
{
.sec = (uint32_t)time->tm_sec, .sec = (uint32_t)time->tm_sec,
.secEn = active.en_sec ? CY_RTC_ALARM_ENABLE : CY_RTC_ALARM_DISABLE, .secEn = active.en_sec ? CY_RTC_ALARM_ENABLE : CY_RTC_ALARM_DISABLE,
.min = (uint32_t)time->tm_min, .min = (uint32_t)time->tm_min,
...@@ -406,29 +324,7 @@ cy_rslt_t cyhal_rtc_set_alarm(cyhal_rtc_t *obj, const struct tm *time, cyhal_ala ...@@ -406,29 +324,7 @@ cy_rslt_t cyhal_rtc_set_alarm(cyhal_rtc_t *obj, const struct tm *time, cyhal_ala
.monthEn = active.en_month ? CY_RTC_ALARM_ENABLE : CY_RTC_ALARM_DISABLE, .monthEn = active.en_month ? CY_RTC_ALARM_ENABLE : CY_RTC_ALARM_DISABLE,
.almEn = CY_RTC_ALARM_ENABLE .almEn = CY_RTC_ALARM_ENABLE
}; };
return (cy_rslt_t)Cy_RTC_SetAlarmDateAndTime(&alarm, CY_RTC_ALARM_1);
cy_rslt_t rslt;
uint32_t retry = 0;
do
{
if (retry != 0)
_CYHAL_RTC_WAIT_ONE_MS();
rslt = (cy_rslt_t)Cy_RTC_SetAlarmDateAndTime(&alarm, CY_RTC_ALARM_1);
++retry;
} while (rslt == CY_RTC_INVALID_STATE && retry < _CYHAL_RTC_MAX_RETRY);
return rslt;
}
static uint32_t _cyhal_rtc_update_field(uint32_t remaining, uint32_t* curr, uint32_t *next, uint32_t max)
{
*curr += remaining % max;
if (*curr >= max)
{
*curr %= max;
(*next)++;
}
return remaining / max;
} }
cy_rslt_t cyhal_rtc_set_alarm_by_seconds(cyhal_rtc_t *obj, const uint32_t seconds) cy_rslt_t cyhal_rtc_set_alarm_by_seconds(cyhal_rtc_t *obj, const uint32_t seconds)
...@@ -441,113 +337,24 @@ cy_rslt_t cyhal_rtc_set_alarm_by_seconds(cyhal_rtc_t *obj, const uint32_t second ...@@ -441,113 +337,24 @@ cy_rslt_t cyhal_rtc_set_alarm_by_seconds(cyhal_rtc_t *obj, const uint32_t second
if(seconds > SECONDS_IN_YEAR) if(seconds > SECONDS_IN_YEAR)
return CY_RSLT_RTC_BAD_ARGUMENT; return CY_RSLT_RTC_BAD_ARGUMENT;
cy_stc_rtc_config_t now; struct tm now;
uint32_t savedIntrStatus = cyhal_system_critical_section_enter(); uint32_t savedIntrStatus = cyhal_system_critical_section_enter();
Cy_RTC_GetDateAndTime(&now); cyhal_rtc_read(obj, &now);
const int year = (int)(now.year + _cyhal_rtc_get_century());
cyhal_system_critical_section_exit(savedIntrStatus); cyhal_system_critical_section_exit(savedIntrStatus);
bool nowDst = _cyhal_rtc_dst && Cy_RTC_GetDstStatus(_cyhal_rtc_dst, &now); time_t future_time_t = mktime(&now) + seconds;
struct tm* future = localtime(&future_time_t);
uint32_t remaining = seconds;
remaining = _cyhal_rtc_update_field(remaining, &now.sec, &now.min, 60);
remaining = _cyhal_rtc_update_field(remaining, &now.min, &now.hour, 60);
remaining = _cyhal_rtc_update_field(remaining, &now.hour, &now.date, 24);
uint32_t days;
now.date += remaining;
while (now.date > (days = Cy_RTC_DaysInMonth(now.month, year)))
{
now.date -= days;
now.month++;
if (now.month > 12)
{
now.year++;
now.month = 1;
}
}
bool setSkipNextAlarm = false;
// Handle crossing of daylight savings time boundaries static const cyhal_alarm_active_t active = {
if (_cyhal_rtc_dst)
{
bool futureDst = Cy_RTC_GetDstStatus(_cyhal_rtc_dst, &now);
if (nowDst && !futureDst)
{
// If the alarm time is within the hour following the end of DST,
// ignore the first alarm since the adjusted time will be before
// the DST boundary causing two alarms to occur: one before the
// boundary and one after the boundary.
if (now.hour == _cyhal_rtc_dst->stopDst.hour)
{
setSkipNextAlarm = true;
}
if (now.hour == 0)
{
now.hour = 23;
now.date--;
if (now.date < 1)
{
now.month--;
if (now.month < 1)
{
now.month = 12;
now.year--;
}
now.date = Cy_RTC_DaysInMonth(now.month, year);
}
}
else
{
now.hour--;
}
}
else if (!nowDst && futureDst)
{
now.hour++;
if (now.hour >= 24)
{
now.hour = 0;
now.date++;
if (now.date > days)
{
now.date = 1;
now.month++;
if (now.month > 12)
{
now.month = 1;
// Increment year, but alarm doesn't care
}
}
}
}
}
struct tm future;
_cyhal_rtc_from_pdl_time(&now, year, &future);
static const cyhal_alarm_active_t active =
{
.en_sec = CY_RTC_ALARM_ENABLE, .en_sec = CY_RTC_ALARM_ENABLE,
.en_min = CY_RTC_ALARM_ENABLE, .en_min = CY_RTC_ALARM_ENABLE,
.en_hour = CY_RTC_ALARM_ENABLE, .en_hour = CY_RTC_ALARM_ENABLE,
.en_day = CY_RTC_ALARM_DISABLE, // We do not actually compute the day as we don't care. .en_day = CY_RTC_ALARM_ENABLE,
.en_date = CY_RTC_ALARM_ENABLE, // The absolute time (eg: date) is what is important. .en_date = CY_RTC_ALARM_ENABLE,
.en_month = CY_RTC_ALARM_ENABLE .en_month = CY_RTC_ALARM_ENABLE
}; };
savedIntrStatus = cyhal_system_critical_section_enter(); return cyhal_rtc_set_alarm(obj, future, active);
cy_rslt_t result = cyhal_rtc_set_alarm(obj, &future, active);
_cyhal_rtc_dst_skip_next_alarm = setSkipNextAlarm;
cyhal_system_critical_section_exit(savedIntrStatus);
return result;
} }
void cyhal_rtc_register_callback(cyhal_rtc_t *obj, cyhal_rtc_event_callback_t callback, void *callback_arg) void cyhal_rtc_register_callback(cyhal_rtc_t *obj, cyhal_rtc_event_callback_t callback, void *callback_arg)
...@@ -567,13 +374,12 @@ void cyhal_rtc_enable_event(cyhal_rtc_t *obj, cyhal_rtc_event_t event, uint8_t i ...@@ -567,13 +374,12 @@ void cyhal_rtc_enable_event(cyhal_rtc_t *obj, cyhal_rtc_event_t event, uint8_t i
CY_ASSERT(NULL != obj); CY_ASSERT(NULL != obj);
CY_ASSERT(CYHAL_RTC_ALARM == event); CY_ASSERT(CYHAL_RTC_ALARM == event);
Cy_RTC_ClearInterrupt(CY_RTC_INTR_ALARM1 | CY_RTC_INTR_ALARM2); Cy_RTC_ClearInterrupt(CY_RTC_INTR_ALARM1 | CY_RTC_INTR_ALARM2);
uint32_t alarm2_status = (Cy_RTC_GetInterruptMask() & CY_RTC_INTR_ALARM2); Cy_RTC_SetInterruptMask((enable ? CY_RTC_INTR_ALARM1 : 0) | CY_RTC_INTR_CENTURY);
Cy_RTC_SetInterruptMask((enable ? CY_RTC_INTR_ALARM1 : 0) | CY_RTC_INTR_CENTURY | alarm2_status); NVIC_SetPriority(srss_interrupt_backup_IRQn, intr_priority);
_cyhal_irq_set_priority(srss_interrupt_backup_IRQn, intr_priority);
} }
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif
#endif /* CYHAL_DRIVER_AVAILABLE_RTC */ #endif /* (defined(CY_IP_MXS40SSRSS) || defined(CY_IP_MXS40SRSS)) && SRSS_BACKUP_PRESENT */
# Cypress CY8CKIT-062-BLE PSoC 6 BLE Pioneer Kit 说明 # Cypress Psoc6-CY8CKIT-062S2-43012 说明
## 简介 ## 简介
本文档为Cypress为PSoC6 BLE Pioneer Kit开发板提供的 BSP (板级支持包) 说明。 本文档为 `RT-Thread``PSoC6 CY8CKIT-062S2-43012`开发板提供的 BSP (板级支持包) 说明。
主要内容如下: 主要内容如下:
...@@ -10,89 +10,91 @@ ...@@ -10,89 +10,91 @@
- BSP 快速上手 - BSP 快速上手
- 进阶使用方法 - 进阶使用方法
通过阅读快速上手章节开发者可以快速地上手该 BSP,将 RT-Thread 运行在开发板上。在进阶使用指南章节,将会介绍更多高级功能,帮助开发者利用 RT-Thread 驱动更多板载资源。 通过阅读快速上手章节开发者可以快速地上手该 BSP,将 RT-Thread 运行在开发板上。在进阶使用指南章节,将会介绍更多高级功能,帮助开发者利用 `RT-Thread` 驱动更多板载资源。
## 开发板介绍 ## 开发板介绍
CY8CKIT-062-BLE PSoC6 BLE Pioneer Kit 是赛普拉斯推出的一款32位双核CPU子系统( ARM Cortex-M4 和 ARM Cortex-M0)的开发板,具有单周期乘法的150-MHz Arm Cortex-M4F CPU (浮点和 `PSoC6 CY8CKIT-062S2-43012` 是赛普拉斯推出的一款32位双核CPU子系统( ARM Cortex-M4 和 ARM Cortex-M0)的开发板,具有单周期乘法的150-MHz Arm Cortex-M4F CPU (浮点和存储器保护单元),100-MHz Cortex M0+ CPU,带单周期乘法和MPU,可以充分发挥 PSoC6 双核芯片性能。
存储器保护单元),100-MHz Cortex M0+ CPU,带单周期乘法和MPU,可以充分发挥 PSoC6 双核芯片性能。
开发板外观详细信息:https://www.cypress.com/file/390496/download 开发板外观详细信息:[CY8CPROTO-062-4343W - Infineon Technologies](https://www.infineon.com/cms/en/product/evaluation-boards/cy8cproto-062-4343w/)
该开发板核心 **板载资源** 如下:
该开发板常用 **板载资源** 如下: - MCU:CY8C624ABZI-S2D44,Cortex-M4主频 150MHz,Cortex-M0主频 100MHz,2MB Flash 和 1MB SRAM
MCU手册更多详细信息请参考文档 [PSoC 6 MCU: CY8C62x8, CY8C62xA Datasheet (infineon.com)](https://www.infineon.com/dgdl/Infineon-PSOC_6_MCU_CY8C62X8_CY8C62XA-DataSheet-v17_00-EN.pdf?fileId=8ac78c8c7d0d8da4017d0ee7d03a70b1)
- MCU:CY8C6347BZI-BLD53,Cortex-M4主频 150MHz,Cortex-M0主频 100MHz,1 MB 应用闪存,32 KB EEPROM 区域和32 KB 安全闪存 ,288 KB 集成SRAM - 板载资源:microSD card , 64-Mb Quad-SPI NOR flash, CYW43012 Wi-Fi + Bluetooth Combo Chip
MCU手册更多详细信息请参考文档 https://www.cypress.com/file/457541/download - 开发环境:ModusToolbox 2.0/MDK V5
PSoC® Creator™ 下载链接 [ModusToolbox™ Software - Infineon Technologies](https://www.infineon.com/cms/en/design-support/tools/sdk/modustoolbox-software/)
- 开发环境:ModusToolbox 2.0
PSoC® Creator™ 下载链接 https://www.cypress.com/products/modustoolbox-software-environment
- 开发板:CY8CKIT-062-BLE PSoC 6 BLE Pioneer Kit - 开发板:CY8CKIT-062-BLE PSoC 6 BLE Pioneer Kit
开发板更多详细信息请参考文档 https://www.cypress.com/file/390496/download 开发板更多详细信息请参考文档 https://www.cypress.com/file/390496/download
## 外设支持 ## 外设支持
本 BSP 目前对外设的支持情况详细信息请参考文档 https://www.cypress.com/file/390496/download 本 BSP 目前对外设的支持情况如下:
| **片上外设** | **支持情况** | **备注** |
| :----------: | :----------: | :-----------: |
| USB 转串口 | 支持 | — |
| GPIO | 支持 | — |
| UART | 支持 | UART0-5 |
| I2C | 支持 | 软件+硬件 I2C |
| RTC | 支持 | — |
| WDT | 支持 | — |
| PWM | 支持 | — |
| SPI | 支持 | — |
| HardTimer | 暂不支持 | — |
| DAC | 暂不支持 | — |
| Flash | 暂不支持 | — |
| SDIO | 暂不支持 | — |
| USB Device | 暂不支持 | — |
| USB Host | 暂不支持 | — |
## 使用说明 ## 使用说明
使用说明分为如下两个章节:
- 快速上手 - 快速上手
本章节是为刚接触 RT-Thread 的新手准备的使用说明,遵循简单的步骤即可将 RT-Thread 操作系统运行在该开发板上,看到实验效果 。 本章节是为刚接触 RT-Thread 的新手准备的使用说明,遵循简单的步骤即可将 RT-Thread 操作系统运行在该开发板上,看到实验效果 。
- 进阶使用
本章节是为需要在 RT-Thread 操作系统上使用赛普拉斯开发板资源的开发者准备的。
### 快速上手 ### 快速上手
本 BSP 以 ModusToolbox 2.0开发环境(GCC),介绍如何将系统运行起来。 本 BSP 是以 MDK V5 开发环境(编译器:ARMClang ),接下来介绍如何将系统运行起来。
#### 硬件连接 #### 硬件连接
使用Type-C数据线连接开发板到 PC. 使用数据线连接开发板到 PC。
#### 编译下载 #### 编译下载
1, 安装ModusToolbox 2.0时请使用默认路径 1、配置工程:
2, 打开ModusToolbox 2.0时workspace选择工程所在目录下(例如workspace: C:\Git\rt-thread\bsp\cypress) 首先打开 MDK ,若没有安装 `Cypress-PSoC6` 的芯片支持包会提示在线安装,根据提示安装即可。若受网络问题,可以进入 [keil](https://www.keil.com/dd2/pack) 官网下载安装包,离线安装。
3, 在Project Explorer的空白处右键,点击import,General->Existing Projects into Workspace ->next,点击Browse选择 ![mdk_package](./figures/mdk_package.png)
此BSP所在目录加载工程->Finish
4, 下载lib:在左下角Quick Panel的Tools栏,点击library Manager-> BSPs下面勾选CY8CKIT-062-BLE (若已勾选可以不用再选) 2、 编译此工程:在安装好芯片支持包后,在 `MDK`工程中进行编译。
-> Libraries里PSoC6 Base Libraries下面全部勾选core-lib,psoc6cm0p,psoc6hal,psoc6make,psoc6pdl -> 点击apply 进行下载
5, 编译此工程 3、下载此工程:
6, 下载此工程
工程默认配置使用板载 `DAP-LINK` 使用 `SWD` 方式下载程序,使用数据线连接开发板,编译之后直接点击下载按钮即可。
工程默认配置使用 SWD方式下载程序,Type-C数据线连接开发板,编译之后直接点击下载按钮即可。
#### 运行结果 #### 运行结果
下载程序成功之后,系统会自动运行。打开终端工具串口小助手,复位设备后,可以看到 RT-Thread 的输出信息: 下载程序成功之后,系统会自动运行。打开终端工具串口助手,选择波特率为 115200。复位设备后,LED 将会以 500HZ 的频率闪烁,而且在终端上可以看到 `RT-Thread` 的输出信息:
注:推荐使用串口调试助手如:sscom 注:推荐使用串口调试助手如:`MobaXterm`
``` ```
\ | / \ | /
- RT - Thread Operating System - RT - Thread Operating System
/ | \ 4.0.3 build Jan 6 2020 / | \ 4.1.1 build Jul 25 2022 18:03:35
2006 - 2019 Copyright by rt-thread team 2006 - 2022 Copyright by RT-Thread team
hello rt-thread msh >
msh >hello rt-thread
hello rt-thread
``` ```
## 联系人信息 ## 联系人信息
维护人: 维护人:
- [amyqian379](https://github.com/amyqian379) - [Rbb666](https://github.com/Rbb666)
\ No newline at end of file \ No newline at end of file
...@@ -196,6 +196,23 @@ menu "On-chip Peripheral Drivers" ...@@ -196,6 +196,23 @@ menu "On-chip Peripheral Drivers"
endif endif
endif endif
menuconfig BSP_USING_RTC
bool "Enable RTC"
select RT_USING_RTC
default n
if BSP_USING_RTC
choice
prompt "Select clock source"
default BSP_RTC_USING_LSE
config BSP_RTC_USING_LSE
bool "RTC USING LSE"
config BSP_RTC_USING_LSI
bool "RTC USING LSI"
endchoice
endif
config BSP_USING_WDT config BSP_USING_WDT
bool "Enable Watchdog Timer" bool "Enable Watchdog Timer"
select RT_USING_WDT select RT_USING_WDT
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册