chore: migrate project into clean repository

This commit is contained in:
yuuux
2026-08-13 16:50:52 +08:00
commit d1d25a09e7
27405 changed files with 9422808 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
# LISA RTC Driver
#
# Copyright (c) 2025, LISTENAI
# SPDX-License-Identifier: Apache-2.0
if(CONFIG_LISA_RTC)
listenai_library_named(lisa_rtc)
listenai_library_sources(
lisa_rtc_arcs.c # 平台适配层实现
)
listenai_include_directories(.)
endif() # CONFIG_LISA_RTC

View File

@@ -0,0 +1,16 @@
# LISA RTC Driver Configuration
#
# Copyright (c) 2025, LISTENAI
# SPDX-License-Identifier: Apache-2.0
menuconfig LISA_RTC
bool "Enable LISA RTC Driver"
default n
depends on LISA_DEVICE
help
Enable LISA RTC (Real-Time Clock) device driver based on lisa_device framework.
Provides unified interface for RTC with alarm and periodic interrupt support.
if LISA_RTC
endif # LISA_RTC

View File

@@ -0,0 +1,190 @@
# RTC 驱动
基于 lisa_device 框架的 RTC 设备驱动,为 ARCS 平台提供统一的实时时钟管理接口。
## 功能特性
- **时间管理**:支持完整的年/月/日/星期/时/分/秒设置与读取
- **闹钟功能**:提供多字段匹配的闹钟配置能力
- **周期中断**:支持秒、分钟、小时级别的周期性事件
- **事件回调**:统一的事件通知机制
- **时钟校准**:驱动初始化时自动启用校准功能
- **线程安全**:内部使用互斥锁保证并发访问安全
## 配置选项
在 `Kconfig` 中启用驱动:
```kconfig
CONFIG_LISA_RTC=y
```
驱动依赖芯片内置的 CALENDAR 外设,无需额外硬件配置。
## API 接口
### 时间读写
```c
int lisa_rtc_set_time(lisa_device_t *dev, const lisa_rtc_time_t *time);
int lisa_rtc_get_time(lisa_device_t *dev, lisa_rtc_time_t *time);
```
### 闹钟管理
```c
int lisa_rtc_set_alarm(lisa_device_t *dev, uint8_t alarm_id, const lisa_rtc_alarm_t *alarm);
int lisa_rtc_get_alarm(lisa_device_t *dev, uint8_t alarm_id, lisa_rtc_alarm_t *alarm);
int lisa_rtc_enable_alarm(lisa_device_t *dev, uint8_t alarm_id, bool enable);
```
### 周期性中断与回调
```c
int lisa_rtc_set_periodic_int(lisa_device_t *dev, lisa_rtc_event_t event, bool enable);
int lisa_rtc_set_callback(lisa_device_t *dev, lisa_rtc_callback_t callback, void *user_data);
int lisa_rtc_get_capabilities(lisa_device_t *dev, lisa_rtc_capabilities_t *caps);
```
## 使用示例
### 设置与读取时间
```c
#include "lisa_rtc.h"
// 获取 RTC 设备
lisa_device_t *rtc = lisa_device_get("rtc0");
if (!lisa_device_ready(rtc)) {
return -1;
}
// 设置时间到 2025-01-01 12:30:00
lisa_rtc_time_t time = {
.year = 25, // 25 代表 2025 年
.month = 1,
.day = 1,
.weekday = 3, // 0=Sunday
.hour = 12,
.minute = 30,
.second = 0,
};
if (lisa_rtc_set_time(rtc, &time) != LISA_DEVICE_OK) {
return -1;
}
// 读取当前时间
lisa_rtc_time_t current;
if (lisa_rtc_get_time(rtc, &current) == LISA_DEVICE_OK) {
printf("Current time: 20%02u-%02u-%02u %02u:%02u:%02u\n",
current.year, current.month, current.day,
current.hour, current.minute, current.second);
}
```
### 配置闹钟
```c
// 闹钟事件回调
void rtc_event_handler(uint32_t event, void *user_data)
{
if (event & LISA_RTC_EVENT_ALARM) {
printf("Alarm triggered\n");
}
}
// 注册回调函数
lisa_rtc_set_callback(rtc, rtc_event_handler, NULL);
// 配置闹钟时间为 2025-01-15 13:00:00
lisa_rtc_alarm_t alarm = {
.year = 25,
.month = 1,
.day = 15,
.weekday = 0,
.hour = 13,
.minute = 0,
.second = 0
};
lisa_rtc_set_alarm(rtc, 0, &alarm);
// 启用闹钟
lisa_rtc_enable_alarm(rtc, 0, true);
```
### 周期性中断
```c
// 周期性事件回调
void rtc_tick_handler(uint32_t event, void *user_data)
{
if (event & LISA_RTC_EVENT_SECOND) {
printf("Second tick\n");
}
if (event & LISA_RTC_EVENT_MINUTE) {
printf("Minute tick\n");
}
}
// 注册回调并启用秒/分钟周期事件
lisa_rtc_set_callback(rtc, rtc_tick_handler, NULL);
lisa_rtc_set_periodic_int(rtc, LISA_RTC_EVENT_SECOND, true);
lisa_rtc_set_periodic_int(rtc, LISA_RTC_EVENT_MINUTE, true);
```
## 硬件配置
### 时间字段范围
| 字段 | 说明 | 范围 |
|------|------|------|
| `year` | 年份(偏移值) | 0-127 |
| `month` | 月份 | 1-12 |
| `day` | 日期 | 1-31 |
| `weekday` | 星期(0=Sunday) | 0-6 |
| `hour` | 小时 | 0-23 |
| `minute` | 分钟 | 0-59 |
| `second` | 秒 | 0-59 |
### 闹钟字段范围
| 字段 | 说明 | 范围 |
|------|------|------|
| `year` | 年份匹配 | 0-127 |
| `month` | 月份匹配 | 1-12 |
| `day` | 日期匹配 | 1-31 |
| `hour` | 小时匹配 | 0-23 |
| `minute` | 分钟匹配 | 0-59 |
| `second` | 秒匹配 | 0-59 |
### 事件类型
- `LISA_RTC_EVENT_ALARM`:闹钟事件
- `LISA_RTC_EVENT_SECOND`:秒中断事件
- `LISA_RTC_EVENT_MINUTE`:分钟中断事件
- `LISA_RTC_EVENT_HOUR`:小时中断事件
## 返回值说明
- `LISA_DEVICE_OK (0)`:成功
- `LISA_DEVICE_ERR_INVALID`:参数无效
- `LISA_DEVICE_ERR_RANGE`:闹钟 ID 超出范围
- `LISA_DEVICE_ERR_NOT_READY`:设备未就绪
- `LISA_DEVICE_ERR_NOT_SUPPORT`:不支持的操作
- `LISA_DEVICE_ERR_IO`:底层 IO 错误
## 注意事项
1. 驱动基于芯片 CALENDAR 外设实现,闹钟匹配所有字段,硬件不支持通配符。
2. 如需每日或每小时重复提醒,可在回调中重新设置闹钟,或使用周期性中断替代。
3. 当前实现仅支持 1 个闹钟(`alarm_id` 固定为 0)。
4. 回调函数在中断上下文中执行,应保持逻辑简短。
5. 设置时间和闹钟前需确保设备已正确初始化并上电。
6. 线程安全已在驱动内部实现,可直接在多线程环境中调用。
## 文件说明
- `lisa_rtc.h` —— 驱动头文件,包含 API 与类型定义
- `lisa_rtc_arcs.c` —— ARCS 平台适配实现
- `CMakeLists.txt` —— 构建配置
- `Kconfig` —— 配置选项

View File

@@ -0,0 +1,315 @@
/*
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file lisa_rtc.h
* @brief LISA RTC 实时时钟设备驱动接口
*/
#pragma once
#include "lisa_device.h"
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/* ========================================================================
* RTC 类型定义
* ======================================================================== */
/**
* @brief RTC 时间结构体
*/
typedef struct {
uint16_t year; /* 年份 (0-127,起始年份2000年,即0表示2000年,127表示2127年) */
uint8_t month; /* 月份 (1-12) */
uint8_t day; /* 日期 (1-31) */
uint8_t weekday; /* 星期 (0-6, 0=Sunday) */
uint8_t hour; /* 小时 (0-23) */
uint8_t minute; /* 分钟 (0-59) */
uint8_t second; /* 秒 (0-59) */
} lisa_rtc_time_t;
/**
* @brief RTC 闹钟结构体
*/
typedef struct {
uint16_t year; /* 年份 (0-127,起始年份2000年,即0表示2000年,127表示2127年) */
uint8_t month; /* 月份 (1-12) */
uint8_t day; /* 日期 (1-31) */
uint8_t weekday; /* 星期 (0-6, 0=Sunday) */
uint8_t hour; /* 小时 (0-23) */
uint8_t minute; /* 分钟 (0-59) */
uint8_t second; /* 秒 (0-59) */
} lisa_rtc_alarm_t;
/**
* @brief RTC 事件类型
*/
typedef enum {
LISA_RTC_EVENT_ALARM = (1 << 0), /* 闹钟事件 */
LISA_RTC_EVENT_SECOND = (1 << 1), /* 秒中断事件 */
LISA_RTC_EVENT_MINUTE = (1 << 2), /* 分钟中断事件 */
LISA_RTC_EVENT_HOUR = (1 << 3), /* 小时中断事件 */
} lisa_rtc_event_t;
/**
* @brief RTC 能力结构体
*/
typedef struct {
bool has_alarm; /* 是否支持闹钟 */
uint8_t alarm_count; /* 支持的闹钟数量 */
uint16_t min_year; /* 最小年份 */
uint16_t max_year; /* 最大年份 */
} lisa_rtc_capabilities_t;
/**
* @brief RTC 事件回调函数类型
*
* @param event 事件类型位掩码
* @param user_data 用户数据指针
*/
typedef void (*lisa_rtc_callback_t)(uint32_t event, void *user_data);
/* ========================================================================
* RTC 设备 API 结构体
* ======================================================================== */
typedef struct {
int (*set_time)(lisa_device_t *dev, const lisa_rtc_time_t *time);
int (*get_time)(lisa_device_t *dev, lisa_rtc_time_t *time);
int (*set_alarm)(lisa_device_t *dev, uint8_t alarm_id, const lisa_rtc_alarm_t *alarm);
int (*get_alarm)(lisa_device_t *dev, uint8_t alarm_id, lisa_rtc_alarm_t *alarm);
int (*enable_alarm)(lisa_device_t *dev, uint8_t alarm_id, bool enable);
int (*set_periodic_int)(lisa_device_t *dev, lisa_rtc_event_t event, bool enable);
int (*get_capabilities)(lisa_device_t *dev, lisa_rtc_capabilities_t *caps);
int (*set_callback)(lisa_device_t *dev, lisa_rtc_callback_t callback, void *user_data);
} lisa_rtc_api_t;
/* ========================================================================
* RTC 对外接口函数
* ======================================================================== */
/**
* @brief 设置RTC时间
*
* @param dev RTC设备指针
* @param time 时间结构体
*
* @return 0 成功
* @return LISA_DEVICE_ERR_INVALID 参数无效
* @return LISA_DEVICE_ERR_NOT_SUPPORT 不支持该操作
* @return <0 其他错误
*/
static inline int lisa_rtc_set_time(lisa_device_t *dev, const lisa_rtc_time_t *time)
{
if (!dev || !dev->api || !time) {
return LISA_DEVICE_ERR_INVALID;
}
/* 参数合法性检查 */
if (time->year > 127U || time->month < 1U || time->month > 12U ||
time->day < 1U || time->day > 31U || time->weekday > 6U ||
time->hour > 23U || time->minute > 59U || time->second > 59U) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_rtc_api_t *api = (lisa_rtc_api_t *)dev->api;
return api->set_time ? api->set_time(dev, time) : LISA_DEVICE_ERR_NOT_SUPPORT;
}
/**
* @brief 获取RTC时间
*
* @param dev RTC设备指针
* @param time 输出参数,用于接收时间
*
* @return 0 成功
* @return LISA_DEVICE_ERR_INVALID 参数无效
* @return LISA_DEVICE_ERR_NOT_SUPPORT 不支持该操作
* @return <0 其他错误
*/
static inline int lisa_rtc_get_time(lisa_device_t *dev, lisa_rtc_time_t *time)
{
if (!dev || !dev->api || !time) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_rtc_api_t *api = (lisa_rtc_api_t *)dev->api;
return api->get_time ? api->get_time(dev, time) : LISA_DEVICE_ERR_NOT_SUPPORT;
}
/**
* @brief 设置闹钟
*
* @param dev RTC设备指针
* @param alarm_id 闹钟ID (0-N)
* @param alarm 闹钟配置
*
* @return 0 成功
* @return LISA_DEVICE_ERR_INVALID 参数无效
* @return LISA_DEVICE_ERR_NOT_SUPPORT 不支持该操作
* @return <0 其他错误
*/
static inline int lisa_rtc_set_alarm(lisa_device_t *dev, uint8_t alarm_id, const lisa_rtc_alarm_t *alarm)
{
if (!dev || !dev->api || !alarm) {
return LISA_DEVICE_ERR_INVALID;
}
/* 参数合法性检查 */
if (alarm->year > 127U || alarm->month < 1U || alarm->month > 12U ||
alarm->day < 1U || alarm->day > 31U ||
alarm->hour > 23U || alarm->minute > 59U || alarm->second > 59U) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_rtc_api_t *api = (lisa_rtc_api_t *)dev->api;
return api->set_alarm ? api->set_alarm(dev, alarm_id, alarm) : LISA_DEVICE_ERR_NOT_SUPPORT;
}
/**
* @brief 获取闹钟配置
*
* @param dev RTC设备指针
* @param alarm_id 闹钟ID (0-N)
* @param alarm 输出参数,用于接收闹钟配置
*
* @return 0 成功
* @return LISA_DEVICE_ERR_INVALID 参数无效
* @return LISA_DEVICE_ERR_NOT_SUPPORT 不支持该操作
* @return <0 其他错误
*/
static inline int lisa_rtc_get_alarm(lisa_device_t *dev, uint8_t alarm_id, lisa_rtc_alarm_t *alarm)
{
if (!dev || !dev->api || !alarm) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_rtc_api_t *api = (lisa_rtc_api_t *)dev->api;
return api->get_alarm ? api->get_alarm(dev, alarm_id, alarm) : LISA_DEVICE_ERR_NOT_SUPPORT;
}
/**
* @brief 启用或禁用闹钟
*
* @param dev RTC设备指针
* @param alarm_id 闹钟ID (0-N)
* @param enable true=启用, false=禁用
*
* @return 0 成功
* @return LISA_DEVICE_ERR_INVALID 参数无效
* @return LISA_DEVICE_ERR_NOT_SUPPORT 不支持该操作
* @return <0 其他错误
*/
static inline int lisa_rtc_enable_alarm(lisa_device_t *dev, uint8_t alarm_id, bool enable)
{
if (!dev || !dev->api) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_rtc_api_t *api = (lisa_rtc_api_t *)dev->api;
return api->enable_alarm ? api->enable_alarm(dev, alarm_id, enable) : LISA_DEVICE_ERR_NOT_SUPPORT;
}
/**
* @brief 设置周期性中断
*
* @param dev RTC设备指针
* @param event 中断类型 (SECOND/MINUTE/HOUR)
* @param enable true=启用, false=禁用
*
* @return 0 成功
* @return LISA_DEVICE_ERR_INVALID 参数无效
* @return LISA_DEVICE_ERR_NOT_SUPPORT 不支持该操作
* @return <0 其他错误
*
* @note 某些硬件只能同时启用一种周期性中断
*/
static inline int lisa_rtc_set_periodic_int(lisa_device_t *dev, lisa_rtc_event_t event, bool enable)
{
if (!dev || !dev->api) {
return LISA_DEVICE_ERR_INVALID;
}
/* 参数合法性检查:event必须是有效的周期性中断类型 */
if (event != LISA_RTC_EVENT_SECOND &&
event != LISA_RTC_EVENT_MINUTE &&
event != LISA_RTC_EVENT_HOUR) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_rtc_api_t *api = (lisa_rtc_api_t *)dev->api;
return api->set_periodic_int ? api->set_periodic_int(dev, event, enable) : LISA_DEVICE_ERR_NOT_SUPPORT;
}
/**
* @brief 获取RTC设备能力
*
* @param dev RTC设备指针
* @param caps 输出参数,用于接收设备能力信息
*
* @return 0 成功
* @return LISA_DEVICE_ERR_INVALID 参数无效
* @return LISA_DEVICE_ERR_NOT_SUPPORT 不支持该操作
* @return <0 其他错误
*/
static inline int lisa_rtc_get_capabilities(lisa_device_t *dev, lisa_rtc_capabilities_t *caps)
{
if (!dev || !dev->api || !caps) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_rtc_api_t *api = (lisa_rtc_api_t *)dev->api;
return api->get_capabilities ? api->get_capabilities(dev, caps) : LISA_DEVICE_ERR_NOT_SUPPORT;
}
/**
* @brief 设置RTC事件回调函数
*
* @param dev RTC设备指针
* @param callback 回调函数指针
* @param user_data 用户数据指针
*
* @return 0 成功
* @return LISA_DEVICE_ERR_INVALID 参数无效
* @return LISA_DEVICE_ERR_NOT_SUPPORT 不支持该操作
* @return <0 其他错误
*/
static inline int lisa_rtc_set_callback(lisa_device_t *dev, lisa_rtc_callback_t callback, void *user_data)
{
if (!dev || !dev->api) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_rtc_api_t *api = (lisa_rtc_api_t *)dev->api;
return api->set_callback ? api->set_callback(dev, callback, user_data) : LISA_DEVICE_ERR_NOT_SUPPORT;
}
/* ========================================================================
* 便捷宏定义
* ======================================================================== */
/**
* @brief 星期枚举
*/
#define LISA_RTC_WEEKDAY_SUNDAY 0
#define LISA_RTC_WEEKDAY_MONDAY 1
#define LISA_RTC_WEEKDAY_TUESDAY 2
#define LISA_RTC_WEEKDAY_WEDNESDAY 3
#define LISA_RTC_WEEKDAY_THURSDAY 4
#define LISA_RTC_WEEKDAY_FRIDAY 5
#define LISA_RTC_WEEKDAY_SATURDAY 6
/**
* @brief 闹钟忽略标志
*/
#define LISA_RTC_ALARM_IGNORE_YEAR 0
#define LISA_RTC_ALARM_IGNORE_MONTH 0
#define LISA_RTC_ALARM_IGNORE_DAY 0
#define LISA_RTC_ALARM_IGNORE_WEEKDAY 0xFF
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,490 @@
/*
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file lisa_rtc_arcs.c
* @brief LISA RTC ARCS 平台适配层
*
* 此文件实现 ARCS 芯片平台的 RTC 硬件适配
*
* @note ARCS 芯片使用 CALENDAR 外设实现 RTC 功能
*/
#include "lisa_rtc.h"
#include "Driver_CALENDAR.h"
#include <stddef.h>
#include <string.h>
#include <lisa_mutex.h>
#include <stdbool.h>
#define LOG_TAG "lisa_rtc_arcs"
#include <lisa_log.h>
#define DEVICE_LOCK(priv) \
do { \
if (priv->mutex) { \
lisa_mutex_lock(priv->mutex, LISA_OS_WAIT_FOREVER); \
} \
} while (0)
#define DEVICE_UNLOCK(priv) \
do { \
if (priv->mutex) { \
lisa_mutex_unlock(priv->mutex); \
} \
} while (0)
/* ===== RTC 设备私有数据 ===== */
typedef struct {
void *hal_handler; /* HAL CALENDAR 句柄 */
lisa_mutex_t *mutex; /* 互斥锁 */
lisa_rtc_callback_t callback; /* 用户回调函数 */
void *user_data; /* 用户数据 */
uint32_t enabled_events; /* 已启用的事件掩码 */
} lisa_rtc_priv_t;
/* ===== RTC 设备静态实例 ===== */
static lisa_rtc_priv_t rtc0_priv;
/* ===== 内部辅助函数 ===== */
/**
* @brief HAL层中断回调
*/
static void rtc_hal_callback(uint32_t event, void *workspace)
{
lisa_rtc_priv_t *priv = (lisa_rtc_priv_t *)workspace;
if (!priv || !priv->callback) {
return;
}
/* 将HAL事件转换为LISA事件 */
uint32_t lisa_event = 0;
if (event & CSK_CALENDAR_EVENT_ALARM_INT) {
lisa_event |= LISA_RTC_EVENT_ALARM;
}
if (event & CSK_CALENDAR_EVENT_SEC_INT) {
lisa_event |= LISA_RTC_EVENT_SECOND;
}
if (event & CSK_CALENDAR_EVENT_MIN_INT) {
lisa_event |= LISA_RTC_EVENT_MINUTE;
}
if (event & CSK_CALENDAR_EVENT_HOUR_INT) {
lisa_event |= LISA_RTC_EVENT_HOUR;
}
if (lisa_event) {
priv->callback(lisa_event, priv->user_data);
}
}
/* ===== 参数校验辅助函数 ===== */
#define RTC_YEAR_MIN 0U
#define RTC_YEAR_MAX 127U
static bool rtc_validate_common_time(uint16_t year,
uint8_t month,
uint8_t day,
uint8_t hour,
uint8_t minute,
uint8_t second)
{
if (year < RTC_YEAR_MIN || year > RTC_YEAR_MAX) {
return false;
}
if (month < 1U || month > 12U) {
return false;
}
if (day < 1U || day > 31U) {
return false;
}
if (hour > 23U || minute > 59U || second > 59U) {
return false;
}
return true;
}
static bool rtc_validate_time_struct(const lisa_rtc_time_t *time)
{
if (!time) {
return false;
}
if (!rtc_validate_common_time(time->year,
time->month,
time->day,
time->hour,
time->minute,
time->second)) {
return false;
}
if (time->weekday > 6U) {
return false;
}
return true;
}
static bool rtc_validate_alarm_struct(const lisa_rtc_alarm_t *alarm)
{
if (!alarm) {
return false;
}
/* CALENDAR 闹钟必须匹配所有字段,不支持忽略 */
if (!rtc_validate_common_time(alarm->year,
alarm->month,
alarm->day,
alarm->hour,
alarm->minute,
alarm->second)) {
return false;
}
return true;
}
/* ===== ARCS平台RTC实现函数 ===== */
/**
* @brief 设置RTC时间
*/
static int arcs_rtc_set_time(lisa_device_t *dev, const lisa_rtc_time_t *time)
{
if (!lisa_device_is_initialized(dev) || !time) {
return LISA_DEVICE_ERR_INVALID;
}
if (!rtc_validate_time_struct(time)) {
LISA_LOGE(LOG_TAG, "Invalid RTC time parameters");
return LISA_DEVICE_ERR_INVALID;
}
lisa_rtc_priv_t *priv = (lisa_rtc_priv_t *)dev->priv_data;
/* 将LISA时间格式转换为HAL时间格式 */
CSK_CALENDAR_TIME hal_time = {
.year = time->year,
.month = time->month,
.weekend = time->weekday,
.day = time->day,
.hour = time->hour,
.min = time->minute,
.sec = time->second
};
DEVICE_LOCK(priv);
if (CALENDAR_SetTime(priv->hal_handler, &hal_time) != 0) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "Failed to set RTC time");
return LISA_DEVICE_ERR_IO;
}
DEVICE_UNLOCK(priv);
return LISA_DEVICE_OK;
}
/**
* @brief 获取RTC时间
*/
static int arcs_rtc_get_time(lisa_device_t *dev, lisa_rtc_time_t *time)
{
if (!lisa_device_is_initialized(dev) || !time) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_rtc_priv_t *priv = (lisa_rtc_priv_t *)dev->priv_data;
CSK_CALENDAR_TIME hal_time;
DEVICE_LOCK(priv);
if (CALENDAR_GetTime(priv->hal_handler, &hal_time) != 0) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "Failed to get RTC time");
return LISA_DEVICE_ERR_IO;
}
DEVICE_UNLOCK(priv);
/* 将HAL时间格式转换为LISA时间格式 */
time->year = hal_time.year;
time->month = hal_time.month;
time->day = hal_time.day;
time->weekday = hal_time.weekend;
time->hour = hal_time.hour;
time->minute = hal_time.min;
time->second = hal_time.sec;
return LISA_DEVICE_OK;
}
/**
* @brief 设置闹钟
*/
static int arcs_rtc_set_alarm(lisa_device_t *dev, uint8_t alarm_id, const lisa_rtc_alarm_t *alarm)
{
if (!lisa_device_is_initialized(dev) || !alarm) {
return LISA_DEVICE_ERR_INVALID;
}
if (alarm_id != 0) {
return LISA_DEVICE_ERR_RANGE;
}
if (!rtc_validate_alarm_struct(alarm)) {
LISA_LOGE(LOG_TAG, "Invalid RTC alarm parameters");
return LISA_DEVICE_ERR_INVALID;
}
lisa_rtc_priv_t *priv = (lisa_rtc_priv_t *)dev->priv_data;
/* 将LISA闹钟格式转换为HAL闹钟格式 */
CSK_CALENDAR_ALARM hal_alarm = {
.year = alarm->year,
.month = alarm->month,
.day = alarm->day,
.hour = alarm->hour,
.min = alarm->minute,
.sec = alarm->second
};
DEVICE_LOCK(priv);
if (CALENDAR_SetAlarm(priv->hal_handler, &hal_alarm) != 0) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "Failed to set RTC alarm");
return LISA_DEVICE_ERR_IO;
}
DEVICE_UNLOCK(priv);
return LISA_DEVICE_OK;
}
/**
* @brief 获取闹钟配置
*/
static int arcs_rtc_get_alarm(lisa_device_t *dev, uint8_t alarm_id, lisa_rtc_alarm_t *alarm)
{
if (!lisa_device_is_initialized(dev) || !alarm) {
return LISA_DEVICE_ERR_INVALID;
}
if (alarm_id != 0) {
return LISA_DEVICE_ERR_RANGE;
}
lisa_rtc_priv_t *priv = (lisa_rtc_priv_t *)dev->priv_data;
CSK_CALENDAR_ALARM hal_alarm;
DEVICE_LOCK(priv);
if (CALENDAR_GetAlarm(priv->hal_handler, &hal_alarm) != 0) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "Failed to get RTC alarm");
return LISA_DEVICE_ERR_IO;
}
DEVICE_UNLOCK(priv);
/* 将HAL闹钟格式转换为LISA闹钟格式 */
alarm->year = hal_alarm.year;
alarm->month = hal_alarm.month;
alarm->day = hal_alarm.day;
alarm->weekday = 0xFF; /* CALENDAR 不支持星期匹配 */
alarm->hour = hal_alarm.hour;
alarm->minute = hal_alarm.min;
alarm->second = hal_alarm.sec;
return LISA_DEVICE_OK;
}
/**
* @brief 启用或禁用闹钟
*/
static int arcs_rtc_enable_alarm(lisa_device_t *dev, uint8_t alarm_id, bool enable)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_NOT_READY;
}
if (alarm_id != 0) {
return LISA_DEVICE_ERR_RANGE;
}
lisa_rtc_priv_t *priv = (lisa_rtc_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
if (CALENDAR_Control(priv->hal_handler, CSK_CALENDAR_CTRL_ALARM_EN, enable ? 1 : 0) != 0) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "Failed to %s alarm", enable ? "enable" : "disable");
return LISA_DEVICE_ERR_IO;
}
if (enable) {
priv->enabled_events |= LISA_RTC_EVENT_ALARM;
} else {
priv->enabled_events &= ~LISA_RTC_EVENT_ALARM;
}
DEVICE_UNLOCK(priv);
return LISA_DEVICE_OK;
}
/**
* @brief 设置周期性中断
*/
static int arcs_rtc_set_periodic_int(lisa_device_t *dev, lisa_rtc_event_t event, bool enable)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_NOT_READY;
}
lisa_rtc_priv_t *priv = (lisa_rtc_priv_t *)dev->priv_data;
uint32_t control = 0;
/* 映射LISA事件到HAL控制位 */
switch (event) {
case LISA_RTC_EVENT_SECOND:
control = CSK_CALENDAR_CTRL_SEC_INT;
break;
case LISA_RTC_EVENT_MINUTE:
control = CSK_CALENDAR_CTRL_MIN_INT;
break;
case LISA_RTC_EVENT_HOUR:
control = CSK_CALENDAR_CTRL_HOUR_INT;
break;
default:
return LISA_DEVICE_ERR_NOT_SUPPORT;
}
DEVICE_LOCK(priv);
if (CALENDAR_Control(priv->hal_handler, control, enable ? 1 : 0) != 0) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "Failed to set periodic interrupt");
return LISA_DEVICE_ERR_IO;
}
if (enable) {
priv->enabled_events |= event;
} else {
priv->enabled_events &= ~event;
}
DEVICE_UNLOCK(priv);
return LISA_DEVICE_OK;
}
/**
* @brief 获取RTC设备能力
*/
static int arcs_rtc_get_capabilities(lisa_device_t *dev, lisa_rtc_capabilities_t *caps)
{
if (!lisa_device_is_initialized(dev) || !caps) {
return LISA_DEVICE_ERR_INVALID;
}
/* ARCS芯片CALENDAR能力 */
caps->has_alarm = true;
caps->alarm_count = 1;
caps->min_year = 0;
caps->max_year = 127; /* CALENDAR 支持年份 0-127,起始年份2000年 */
return LISA_DEVICE_OK;
}
/**
* @brief 设置RTC事件回调
*/
static int arcs_rtc_set_callback(lisa_device_t *dev, lisa_rtc_callback_t callback, void *user_data)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_NOT_READY;
}
lisa_rtc_priv_t *priv = (lisa_rtc_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->callback = callback;
priv->user_data = user_data;
DEVICE_UNLOCK(priv);
return LISA_DEVICE_OK;
}
/* ===== ARCS RTC API 实例 ===== */
static const lisa_rtc_api_t arcs_rtc_api = {
.set_time = arcs_rtc_set_time,
.get_time = arcs_rtc_get_time,
.set_alarm = arcs_rtc_set_alarm,
.get_alarm = arcs_rtc_get_alarm,
.enable_alarm = arcs_rtc_enable_alarm,
.set_periodic_int = arcs_rtc_set_periodic_int,
.get_capabilities = arcs_rtc_get_capabilities,
.set_callback = arcs_rtc_set_callback,
};
/* ===== 设备初始化函数 ===== */
static int arcs_rtc0_init(void)
{
/* 清空私有数据 */
memset(&rtc0_priv, 0, sizeof(lisa_rtc_priv_t));
/* 获取 HAL CALENDAR 句柄 */
rtc0_priv.hal_handler = CALENDAR();
if (!rtc0_priv.hal_handler) {
LISA_LOGE(LOG_TAG, "Failed to get CALENDAR handler");
return LISA_DEVICE_ERR_INIT_FAIL;
}
/* 创建互斥锁 */
rtc0_priv.mutex = lisa_mutex_create();
if (!rtc0_priv.mutex) {
LISA_LOGE(LOG_TAG, "Failed to create mutex");
return LISA_DEVICE_ERR_INIT_FAIL;
}
/* 初始化 HAL CALENDAR */
if (CALENDAR_Initialize(rtc0_priv.hal_handler, rtc_hal_callback, &rtc0_priv) != 0) {
LISA_LOGE(LOG_TAG, "Failed to initialize CALENDAR");
return LISA_DEVICE_ERR_INIT_FAIL;
}
/* 使能 CALENDAR 电源 */
if (CALENDAR_PowerControl(rtc0_priv.hal_handler, CSK_POWER_FULL) != 0) {
LISA_LOGE(LOG_TAG, "Failed to power on CALENDAR");
return LISA_DEVICE_ERR_INIT_FAIL;
}
/* 启用校准功能(可选) */
CALENDAR_Control(rtc0_priv.hal_handler, CSK_CALENDAR_CTRL_CALIBRATION_EN, 1);
LISA_LOGI(LOG_TAG, "RTC0 initialized successfully");
return LISA_DEVICE_OK;
}
/* ===== 设备注册 ===== */
LISA_DEVICE_REGISTER(rtc0, /* 设备名称 */
&arcs_rtc_api, /* API指针 */
&rtc0_priv, /* 私有数据指针 */
NULL, /* 用户数据 */
arcs_rtc0_init, /* 初始化函数 */
LISA_DEVICE_PRIORITY_NORMAL); /* 优先级 */