149 lines
3.3 KiB
C
149 lines
3.3 KiB
C
#include "board.h"
|
|
#include "main.h"
|
|
#include "rtc.h"
|
|
|
|
board_t board;
|
|
|
|
void led_open(led_e led)
|
|
{
|
|
switch (led)
|
|
{
|
|
case LED_USER:
|
|
GPIO_SET(LED_USER_GPIO_Port, LED_USER_Pin);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void led_close(led_e led)
|
|
{
|
|
switch (led)
|
|
{
|
|
case LED_USER:
|
|
GPIO_RESET(LED_USER_GPIO_Port, LED_USER_Pin);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void led_toggle(led_e led)
|
|
{
|
|
switch (led)
|
|
{
|
|
case LED_USER:
|
|
GPIO_TOGGLE(LED_USER_GPIO_Port, LED_USER_Pin);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief RTC写入后备区域SRAM
|
|
* @param bkrx : 后备区寄存器编号,范围:0~31
|
|
* @param data : 要写入的数据,32位长度
|
|
* @retval 无
|
|
*/
|
|
void rtc_write_bkr(uint32_t bkrx, uint32_t data)
|
|
{
|
|
HAL_PWR_EnableBkUpAccess(); /* 取消备份区写保护 */
|
|
HAL_RTCEx_BKUPWrite(&hrtc, bkrx, data);
|
|
}
|
|
|
|
/**
|
|
* @brief RTC读取后备区域SRAM
|
|
* @param bkrx : 后备区寄存器编号,范围:0~31
|
|
* @retval 读取到的值
|
|
*/
|
|
static uint32_t rtc_read_bkr(uint32_t bkrx)
|
|
{
|
|
uint32_t temp = 0;
|
|
temp = RTC_BASE + 0x50 + bkrx * 4;
|
|
return (*(uint32_t *)temp); /* 返回读取到的值 */
|
|
}
|
|
|
|
BOOL rtc_set_time(rtc_time_t time, uint8_t ampm)
|
|
{
|
|
RTC_TimeTypeDef rtc_time;
|
|
rtc_time.Hours = time.hour;
|
|
rtc_time.Minutes = time.minute;
|
|
rtc_time.Seconds = time.second;
|
|
rtc_time.TimeFormat = ampm;
|
|
rtc_time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
|
|
rtc_time.StoreOperation = RTC_STOREOPERATION_RESET;
|
|
return HAL_RTC_SetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN) == HAL_OK;
|
|
}
|
|
|
|
BOOL rtc_set_date(rtc_date_t date)
|
|
{
|
|
RTC_DateTypeDef rtc_date;
|
|
rtc_date.Month = date.month;
|
|
rtc_date.Date = date.day;
|
|
rtc_date.Year = date.year;
|
|
rtc_date.WeekDay = date.weekday;
|
|
|
|
return HAL_RTC_SetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN) == HAL_OK;
|
|
}
|
|
|
|
void rtc_update(void)
|
|
{
|
|
RTC_DateTypeDef rtc_date;
|
|
RTC_TimeTypeDef rtc_time;
|
|
|
|
HAL_RTC_GetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN);
|
|
HAL_RTC_GetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN);
|
|
|
|
board.rtc_time.Hours = rtc_time.Hours;
|
|
board.rtc_time.Minutes = rtc_time.Minutes;
|
|
board.rtc_time.Seconds = rtc_time.Seconds;
|
|
|
|
board.rtc_date.WeekDay = rtc_date.WeekDay;
|
|
board.rtc_date.Month = rtc_date.Month;
|
|
board.rtc_date.Date = rtc_date.Date;
|
|
board.rtc_date.Year = rtc_date.Year;
|
|
}
|
|
|
|
BOOL rtc_init(void)
|
|
{
|
|
uint16_t bkpflag = 0;
|
|
|
|
/* 检查是不是第一次配置时钟 */
|
|
bkpflag = rtc_read_bkr(RTC_BKP_DR0); /* 读取BKP0的值 */
|
|
|
|
if ((bkpflag != 0x5050) && (bkpflag != 0x5051)) /* 之前未初始化过, 重新配置 */
|
|
{
|
|
rtc_date_t date;
|
|
rtc_time_t time;
|
|
date.year = 25;
|
|
date.month = 1;
|
|
date.day = 1;
|
|
date.weekday = get_weekday(date.year, date.month, date.day);
|
|
time.hour = 0;
|
|
time.minute = 0;
|
|
time.second = 0;
|
|
|
|
rtc_set_time(time, RTC_HOURFORMAT12_AM); /* 设置时间, 根据实际时间修改 */
|
|
rtc_set_date(date); /* 设置日期 */
|
|
|
|
rtc_write_bkr(RTC_BKP_DR0, 0x5051); /* 写入标记位 */
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
/**
|
|
* @brief 初始化显示板
|
|
*
|
|
*/
|
|
void board_init(void)
|
|
{
|
|
rtc_init();
|
|
sd_init();
|
|
if (sd_test() == FALSE)
|
|
{
|
|
DBG_ASSERT(FALSE __DBG_LINE);
|
|
}
|
|
}
|