Add RTC device

This commit is contained in:
Rbb666 2022-07-26 12:52:25 +08:00 committed by guo
parent ac92106a7b
commit 2ee400f6d1
11 changed files with 349 additions and 368 deletions

View File

@ -38,6 +38,9 @@ if GetDepend(['BSP_USING_SPI']):
if GetDepend(['BSP_USING_ADC']):
src += ['drv_adc.c']
if GetDepend('BSP_USING_RTC'):
src += ['drv_rtc.c']
if GetDepend(['RT_USING_WDT']):
src += ['drv_wdt.c']

View File

@ -0,0 +1,164 @@
/*
* 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

View File

@ -67,7 +67,6 @@ static void uart_isr(struct rt_serial_device *serial)
struct ifx_uart *uart = (struct ifx_uart *) serial->parent.user_data;
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)
{
/* Clear UART "RX fifo not empty interrupt" */
@ -75,7 +74,6 @@ static void uart_isr(struct rt_serial_device *serial)
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
}
#endif
}
#ifdef BSP_USING_UART0
@ -162,16 +160,28 @@ void uart5_isr_callback(void)
*/
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);
struct ifx_uart *uart = (struct ifx_uart *) serial->parent.user_data;
RT_ASSERT(uart != RT_NULL);
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 */
result = cy_retarget_io_init(uart->config->tx_pin, uart->config->rx_pin,
CY_RETARGET_IO_BAUDRATE);
result = cyhal_uart_init(uart->config->uart_obj, uart->config->tx_pin, uart->config->rx_pin, NC, NC, NULL, &uart_config);
if (result == CY_RSLT_SUCCESS)
{
result = cyhal_uart_set_baud(uart->config->uart_obj, cfg->baud_rate, NULL);
}
RT_ASSERT(result != RT_ERROR);
@ -229,10 +239,13 @@ static int ifx_uarths_getc(struct rt_serial_device *serial)
{
int ch;
rt_uint8_t read_data;
RT_ASSERT(serial != RT_NULL);
struct ifx_uart *uart = (struct ifx_uart *) serial->parent.user_data;
RT_ASSERT(uart != RT_NULL);
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;
}
@ -258,15 +271,17 @@ void rt_hw_uart_init(void)
int index;
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;
for (index = 0; index < obj_num; index++)
{
uart_obj[index].config = &uart_config[index];
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 */
result = rt_hw_serial_register(&uart_obj[index].serial,
uart_obj[index].config->name,

View File

@ -21,6 +21,8 @@
struct ifx_uart_config
{
cyhal_uart_t *uart_obj;
const char *name;
rt_uint32_t tx_pin;
rt_uint32_t rx_pin;

View File

@ -95,6 +95,10 @@ if GetDepend(['RT_USING_SPI']):
if GetDepend(['RT_USING_I2C']):
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']):
src += ['mtb-pdl-cat1/drivers/source/cy_wdt.c']
src += ['mtb-hal-cat1/source/cyhal_wdt.c']

View File

@ -9,9 +9,7 @@
*
********************************************************************************
* \copyright
* Copyright 2018-2021 Cypress Semiconductor Corporation (an Infineon company) or
* an affiliate of Cypress Semiconductor Corporation
*
* Copyright 2018-2021 Cypress Semiconductor Corporation
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -72,6 +70,12 @@
extern "C" {
#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
* RTC specific return codes
* \ingroup group_hal_results
@ -80,10 +84,10 @@ extern "C" {
/** 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 */
#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
typedef void (*cyhal_rtc_event_callback_t)(void *callback_arg, cyhal_rtc_event_t event);
/** 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
* 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);
*
* @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.
* @param[in] cfg Configuration structure generated by a configurator.
* @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
*
@ -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);
/** 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
*
* 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] start When Day Light Savings time should start

View File

@ -2,16 +2,14 @@
* \file cyhal_rtc.c
*
* \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
* functionality is necessary, or performance is critical the low level functions
* can be used directly.
*
********************************************************************************
* \copyright
* Copyright 2018-2021 Cypress Semiconductor Corporation (an Infineon company) or
* an affiliate of Cypress Semiconductor Corporation
*
* Copyright 2018-2021 Cypress Semiconductor Corporation
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -31,15 +29,13 @@
#include "cy_utils.h"
#include "cyhal_rtc.h"
#include "cyhal_system.h"
#include "cyhal_utils_psoc.h"
#include "cyhal_irq_psoc.h"
/**
* \addtogroup group_hal_impl_rtc RTC (Real Time Clock)
* \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,
* 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
@ -60,7 +56,7 @@
* \} group_hal_impl_wdt
*/
#if (CYHAL_DRIVER_AVAILABLE_RTC)
#if (defined(CY_IP_MXS40SSRSS) || defined(CY_IP_MXS40SRSS)) && SRSS_BACKUP_PRESENT
#if defined(__cplusplus)
extern "C" {
@ -69,66 +65,22 @@ extern "C" {
#define _CYHAL_RTC_STATE_UNINITIALIZED 0
#define _CYHAL_RTC_STATE_ENABLED 1
#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
#endif // (defined(COMPONENT_CAT1C) && (CORE == CM0P))
#define _CYHAL_RTC_INIT_CENTURY 2000
#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])
#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_Msk 0x0000FFFFUL
#define _CYHAL_RTC_BREG_STATE_Pos 16UL
#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 */
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);
}
static bool _cyhal_rtc_dst_skip_next_alarm = false;
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_t _cyhal_rtc_pm_cb = {
@ -185,11 +137,7 @@ static void _cyhal_rtc_isr_handler(void)
/* Override weak function from PDL */
void Cy_RTC_Alarm1Interrupt(void)
{
if (_cyhal_rtc_dst_skip_next_alarm)
{
_cyhal_rtc_dst_skip_next_alarm = false;
}
else if (NULL != _cyhal_rtc_user_handler)
if (NULL != _cyhal_rtc_user_handler)
{
(*_cyhal_rtc_user_handler)(_cyhal_rtc_handler_arg, CYHAL_RTC_ALARM);
}
@ -200,15 +148,23 @@ void Cy_RTC_CenturyInterrupt(void)
_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;
if (_cyhal_rtc_get_state() == _CYHAL_RTC_STATE_UNINITIALIZED)
{
if (Cy_RTC_IsExternalResetOccurred())
{
// 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);
}
@ -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)
{
if (Cy_RTC_GetInterruptStatus() & CY_RTC_INTR_CENTURY)
if(Cy_RTC_GetInterruptStatus() & CY_RTC_INTR_CENTURY)
Cy_RTC_CenturyInterrupt();
}
Cy_RTC_ClearInterrupt(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)
{
_cyhal_rtc_dst = NULL;
_cyhal_irq_enable(srss_interrupt_backup_IRQn);
NVIC_EnableIRQ(srss_interrupt_backup_IRQn);
}
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)
{
CY_UNUSED_PARAMETER(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);
_cyhal_rtc_dst = NULL;
@ -298,66 +218,68 @@ cy_rslt_t cyhal_rtc_read(cyhal_rtc_t *obj, struct tm *time)
{
CY_UNUSED_PARAMETER(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();
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_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;
}
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);
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;
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))
{
return CY_RSLT_RTC_BAD_ARGUMENT;
}
do
{
static const uint32_t MAX_RETRY = 10, RETRY_DELAY_MS = 1;
do {
if (retry != 0)
_CYHAL_RTC_WAIT_ONE_MS();
Cy_SysLib_Delay(RETRY_DELAY_MS);
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)
_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);
++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() && retry < _CYHAL_RTC_MAX_RETRY)
{
_CYHAL_RTC_WAIT_ONE_MS();
++retry;
}
while (CY_RTC_BUSY == Cy_RTC_GetSyncStatus()) { }
if (rslt == CY_RSLT_SUCCESS)
{
_cyhal_rtc_set_state(_CYHAL_RTC_STATE_TIME_SET);
}
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_UNUSED_PARAMETER(obj);
CY_ASSERT(NULL != obj);
CY_ASSERT(NULL != start);
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
cy_rslt_t rslt = Cy_RTC_EnableDstTime(&(obj->dst), &dateTime);
if (rslt == CY_RSLT_SUCCESS)
_cyhal_rtc_dst = &(obj->dst);
return rslt;
}
bool cyhal_rtc_is_dst(cyhal_rtc_t *obj)
{
CY_UNUSED_PARAMETER(obj);
CY_ASSERT(NULL != obj);
cy_stc_rtc_config_t 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)
@ -389,46 +309,22 @@ 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
CY_UNUSED_PARAMETER(obj);
CY_ASSERT(NULL != obj);
_cyhal_rtc_dst_skip_next_alarm = false;
cy_stc_rtc_alarm_t alarm =
{
.sec = (uint32_t)time->tm_sec,
.secEn = active.en_sec ? CY_RTC_ALARM_ENABLE : CY_RTC_ALARM_DISABLE,
.min = (uint32_t)time->tm_min,
.minEn = active.en_min ? CY_RTC_ALARM_ENABLE : CY_RTC_ALARM_DISABLE,
.hour = (uint32_t)time->tm_hour,
.hourEn = active.en_hour ? CY_RTC_ALARM_ENABLE : CY_RTC_ALARM_DISABLE,
.dayOfWeek = (uint32_t)(time->tm_wday + 1),
.dayOfWeekEn = active.en_day ? CY_RTC_ALARM_ENABLE : CY_RTC_ALARM_DISABLE,
.date = (uint32_t)time->tm_mday,
.dateEn = active.en_date ? CY_RTC_ALARM_ENABLE : CY_RTC_ALARM_DISABLE,
.month = (uint32_t)(time->tm_mon + 1),
.monthEn = active.en_month ? CY_RTC_ALARM_ENABLE : CY_RTC_ALARM_DISABLE,
.almEn = CY_RTC_ALARM_ENABLE
cy_stc_rtc_alarm_t alarm = {
.sec = (uint32_t)time->tm_sec,
.secEn = active.en_sec ? CY_RTC_ALARM_ENABLE : CY_RTC_ALARM_DISABLE,
.min = (uint32_t)time->tm_min,
.minEn = active.en_min ? CY_RTC_ALARM_ENABLE : CY_RTC_ALARM_DISABLE,
.hour = (uint32_t)time->tm_hour,
.hourEn = active.en_hour ? CY_RTC_ALARM_ENABLE : CY_RTC_ALARM_DISABLE,
.dayOfWeek = (uint32_t)(time->tm_wday + 1),
.dayOfWeekEn = active.en_day ? CY_RTC_ALARM_ENABLE : CY_RTC_ALARM_DISABLE,
.date = (uint32_t)time->tm_mday,
.dateEn = active.en_date ? CY_RTC_ALARM_ENABLE : CY_RTC_ALARM_DISABLE,
.month = (uint32_t)(time->tm_mon + 1),
.monthEn = active.en_month ? CY_RTC_ALARM_ENABLE : CY_RTC_ALARM_DISABLE,
.almEn = CY_RTC_ALARM_ENABLE
};
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;
return (cy_rslt_t)Cy_RTC_SetAlarmDateAndTime(&alarm, CY_RTC_ALARM_1);
}
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
if(seconds > SECONDS_IN_YEAR)
return CY_RSLT_RTC_BAD_ARGUMENT;
cy_stc_rtc_config_t now;
struct tm now;
uint32_t savedIntrStatus = cyhal_system_critical_section_enter();
Cy_RTC_GetDateAndTime(&now);
const int year = (int)(now.year + _cyhal_rtc_get_century());
cyhal_rtc_read(obj, &now);
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
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 =
{
static const cyhal_alarm_active_t active = {
.en_sec = CY_RTC_ALARM_ENABLE,
.en_min = 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_date = CY_RTC_ALARM_ENABLE, // The absolute time (eg: date) is what is important.
.en_day = CY_RTC_ALARM_ENABLE,
.en_date = CY_RTC_ALARM_ENABLE,
.en_month = CY_RTC_ALARM_ENABLE
};
savedIntrStatus = cyhal_system_critical_section_enter();
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;
return cyhal_rtc_set_alarm(obj, future, active);
}
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
CY_ASSERT(NULL != obj);
CY_ASSERT(CYHAL_RTC_ALARM == event);
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 | alarm2_status);
_cyhal_irq_set_priority(srss_interrupt_backup_IRQn, intr_priority);
Cy_RTC_SetInterruptMask((enable ? CY_RTC_INTR_ALARM1 : 0) | CY_RTC_INTR_CENTURY);
NVIC_SetPriority(srss_interrupt_backup_IRQn, intr_priority);
}
#if defined(__cplusplus)
}
#endif
#endif /* CYHAL_DRIVER_AVAILABLE_RTC */
#endif /* (defined(CY_IP_MXS40SSRSS) || defined(CY_IP_MXS40SRSS)) && SRSS_BACKUP_PRESENT */

View File

@ -1,8 +1,8 @@
# 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 @@
- 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 (浮点和
存储器保护单元)100-MHz Cortex M0+ CPU带单周期乘法和MPU可以充分发挥 PSoC6 双核芯片性能。
`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 双核芯片性能。
开发板外观详细信息:https://www.cypress.com/file/390496/download
开发板外观详细信息:[CY8CPROTO-062-4343W - Infineon Technologies](https://www.infineon.com/cms/en/product/evaluation-boards/cy8cproto-062-4343w/)
该开发板核心 **板载资源** 如下:
该开发板常用 **板载资源** 如下:
- MCUCY8C6347BZI-BLD53Cortex-M4主频 150MHzCortex-M0主频 100MHz1 MB 应用闪存32 KB EEPROM 区域和32 KB 安全闪存 288 KB 集成SRAM
MCU手册更多详细信息请参考文档 https://www.cypress.com/file/457541/download
- 开发环境ModusToolbox 2.0
PSoC® Creator™ 下载链接 https://www.cypress.com/products/modustoolbox-software-environment
- MCUCY8C624ABZI-S2D44Cortex-M4主频 150MHzCortex-M0主频 100MHz2MB 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)
- 板载资源microSD card , 64-Mb Quad-SPI NOR flash CYW43012 Wi-Fi + Bluetooth Combo Chip
- 开发环境ModusToolbox 2.0/MDK V5
PSoC® Creator™ 下载链接 [ModusToolbox™ Software - Infineon Technologies](https://www.infineon.com/cms/en/design-support/tools/sdk/modustoolbox-software/)
- 开发板CY8CKIT-062-BLE PSoC 6 BLE Pioneer Kit
开发板更多详细信息请参考文档 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 操作系统上使用赛普拉斯开发板资源的开发者准备的。
### 快速上手
本 BSP 以 ModusToolbox 2.0开发环境GCC介绍如何将系统运行起来。
本 BSP 是以 MDK V5 开发环境编译器ARMClang ),接下来介绍如何将系统运行起来。
#### 硬件连接
使用Type-C数据线连接开发板到 PC.
使用数据线连接开发板到 PC。
#### 编译下载
1, 安装ModusToolbox 2.0时请使用默认路径
2, 打开ModusToolbox 2.0时workspace选择工程所在目录下例如workspace: C:\Git\rt-thread\bsp\cypress
1、配置工程
3, 在Project Explorer的空白处右键点击importGeneral->Existing Projects into Workspace ->next点击Browse选择
此BSP所在目录加载工程->Finish
首先打开 MDK ,若没有安装 `Cypress-PSoC6` 的芯片支持包会提示在线安装,根据提示安装即可。若受网络问题,可以进入 [keil](https://www.keil.com/dd2/pack) 官网下载安装包,离线安装。
4, 下载lib在左下角Quick Panel的Tools栏点击library Manager-> BSPs下面勾选CY8CKIT-062-BLE (若已勾选可以不用再选)
-> Libraries里PSoC6 Base Libraries下面全部勾选core-lib,psoc6cm0p,psoc6hal,psoc6make,psoc6pdl -> 点击apply 进行下载
![mdk_package](./figures/mdk_package.png)
5, 编译此工程
2、 编译此工程:在安装好芯片支持包后,在 `MDK`工程中进行编译。
6, 下载此工程
3、下载此工程
工程默认配置使用 SWD方式下载程序Type-C数据线连接开发板,编译之后直接点击下载按钮即可。
工程默认配置使用板载 `DAP-LINK` 使用 `SWD` 方式下载程序,使用数据线连接开发板,编译之后直接点击下载按钮即可。
#### 运行结果
下载程序成功之后,系统会自动运行。打开终端工具串口小助手,复位设备后,可以看到 RT-Thread 的输出信息:
下载程序成功之后,系统会自动运行。打开终端工具串口助手,选择波特率为 115200。复位设备后LED 将会以 500HZ 的频率闪烁,而且在终端上可以看到 `RT-Thread` 的输出信息:
注:推荐使用串口调试助手如:sscom
注:推荐使用串口调试助手如:`MobaXterm`
```
\ | /
- RT - Thread Operating System
/ | \ 4.0.3 build Jan 6 2020
2006 - 2019 Copyright by rt-thread team
hello rt-thread
msh >hello rt-thread
hello rt-thread
```
/ | \ 4.1.1 build Jul 25 2022 18:03:35
2006 - 2022 Copyright by RT-Thread team
msh >
```
## 联系人信息
维护人:
- [amyqian379](https://github.com/amyqian379)
- [Rbb666](https://github.com/Rbb666)

View File

@ -196,6 +196,23 @@ menu "On-chip Peripheral Drivers"
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
bool "Enable Watchdog Timer"
select RT_USING_WDT

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB