Skip to content

[rtc] use gmtime_r to replace gmtime #6012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions bsp/acm32/acm32f0x0-nucleo/drivers/drv_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ static rt_err_t set_rtc_time_stamp(time_t time_stamp)
{
RTC_TimeTypeDef RTC_TimeStruct = {0};
RTC_DateTypeDef RTC_DateStruct = {0};
struct tm *p_tm;
struct tm now;

p_tm = gmtime(&time_stamp);
if (p_tm->tm_year < 100)
gmtime_r(&time_stamp, &now);
if (now.tm_year < 100)
{
return -RT_ERROR;
}

RTC_TimeStruct.u8_Seconds = dec2hex(p_tm->tm_sec);
RTC_TimeStruct.u8_Minutes = dec2hex(p_tm->tm_min);
RTC_TimeStruct.u8_Hours = dec2hex(p_tm->tm_hour);
RTC_DateStruct.u8_Date = dec2hex(p_tm->tm_mday);
RTC_DateStruct.u8_Month = dec2hex(p_tm->tm_mon + 1);
RTC_DateStruct.u8_Year = dec2hex(p_tm->tm_year - 100);
RTC_DateStruct.u8_WeekDay = dec2hex(p_tm->tm_wday) + 1;
RTC_TimeStruct.u8_Seconds = dec2hex(now.tm_sec);
RTC_TimeStruct.u8_Minutes = dec2hex(now.tm_min);
RTC_TimeStruct.u8_Hours = dec2hex(now.tm_hour);
RTC_DateStruct.u8_Date = dec2hex(now.tm_mday);
RTC_DateStruct.u8_Month = dec2hex(now.tm_mon + 1);
RTC_DateStruct.u8_Year = dec2hex(now.tm_year - 100);
RTC_DateStruct.u8_WeekDay = dec2hex(now.tm_wday) + 1;

HAL_RTC_SetTime(&RTC_TimeStruct);
HAL_RTC_SetDate(&RTC_DateStruct);
Expand Down
18 changes: 9 additions & 9 deletions bsp/apollo2/board/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
{
time_t *time;
struct tm time_temp;
struct tm* time_new;
struct tm time_new;
am_hal_rtc_time_t hal_time;

RT_ASSERT(dev != RT_NULL);
Expand Down Expand Up @@ -71,16 +71,16 @@ static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)

case RT_DEVICE_CTRL_RTC_SET_TIME:
time = (time_t *)args;
time_new = gmtime(time);
gmtime_r(time, &time_new);

hal_time.ui32Hour = time_new->tm_hour;
hal_time.ui32Minute = time_new->tm_min;
hal_time.ui32Second = time_new->tm_sec;
hal_time.ui32Hour = time_new.tm_hour;
hal_time.ui32Minute = time_new.tm_min;
hal_time.ui32Second = time_new.tm_sec;
hal_time.ui32Hundredths = 00;
hal_time.ui32Weekday = time_new->tm_wday;
hal_time.ui32DayOfMonth = time_new->tm_mday;
hal_time.ui32Month = time_new->tm_mon + 1;
hal_time.ui32Year = time_new->tm_year + 1900 - 2000;
hal_time.ui32Weekday = time_new.tm_wday;
hal_time.ui32DayOfMonth = time_new.tm_mday;
hal_time.ui32Month = time_new.tm_mon + 1;
hal_time.ui32Year = time_new.tm_year + 1900 - 2000;
hal_time.ui32Century = 0;

am_hal_rtc_time_set(&hal_time);
Expand Down
10 changes: 5 additions & 5 deletions bsp/at32/libraries/rt_drivers/drv_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ static rt_err_t set_rtc_time_stamp(time_t time_stamp)
{
#if defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437) || \
defined (SOC_SERIES_AT32F415)
struct tm *p_tm;
struct tm now;

p_tm = gmtime(&time_stamp);
if (p_tm->tm_year < 100)
gmtime_r(&time_stamp, &now);
if (now.tm_year < 100)
{
return -RT_ERROR;
}

/* set time */
if(ertc_time_set(p_tm->tm_hour, p_tm->tm_min, p_tm->tm_sec, ERTC_AM) != SUCCESS)
if(ertc_time_set(now.tm_hour, now.tm_min, now.tm_sec, ERTC_AM) != SUCCESS)
{
return -RT_ERROR;
}

/* set date */
if(ertc_date_set(p_tm->tm_year - 100, p_tm->tm_mon + 1, p_tm->tm_mday, p_tm->tm_wday + 1) != SUCCESS)
if(ertc_date_set(now.tm_year - 100, now.tm_mon + 1, now.tm_mday, now.tm_wday + 1) != SUCCESS)
{
return -RT_ERROR;
}
Expand Down
12 changes: 1 addition & 11 deletions bsp/essemi/es32f0654/drivers/drv_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ static void __rtc_init(rtc_init_t *init)
static rt_err_t es32f0_rtc_control(rt_device_t dev, int cmd, void *args)
{
rt_err_t result = RT_EOK;

struct tm time_temp;
struct tm *pNow;
rtc_date_t date;
rtc_time_t time;

Expand All @@ -76,15 +74,7 @@ static rt_err_t es32f0_rtc_control(rt_device_t dev, int cmd, void *args)
break;

case RT_DEVICE_CTRL_RTC_SET_TIME:

rt_enter_critical();
/* converts calendar time time into local time. */
pNow = gmtime((const time_t *)args);
/* copy the statically located variable */
memcpy(&time_temp, pNow, sizeof(struct tm));
/* unlock scheduler. */
rt_exit_critical();

gmtime_r((const time_t *)args, &time_temp);
time.hour = time_temp.tm_hour;
time.minute = time_temp.tm_min;
time.second = time_temp.tm_sec;
Expand Down
12 changes: 1 addition & 11 deletions bsp/essemi/es32f369x/drivers/drv_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ static void __rtc_init(rtc_init_t *init)
static rt_err_t es32f0_rtc_control(rt_device_t dev, int cmd, void *args)
{
rt_err_t result = RT_EOK;

struct tm time_temp;
struct tm *pNow;
rtc_date_t date;
rtc_time_t time;

Expand All @@ -75,15 +73,7 @@ static rt_err_t es32f0_rtc_control(rt_device_t dev, int cmd, void *args)
break;

case RT_DEVICE_CTRL_RTC_SET_TIME:

rt_enter_critical();
/* converts calendar time time into local time. */
pNow = gmtime((const time_t *)args);
/* copy the statically located variable */
memcpy(&time_temp, pNow, sizeof(struct tm));
/* unlock scheduler. */
rt_exit_critical();

gmtime_r((const time_t *)args, &time_temp);
time.hour = time_temp.tm_hour;
time.minute = time_temp.tm_min;
time.second = time_temp.tm_sec;
Expand Down
16 changes: 8 additions & 8 deletions bsp/imxrt/libraries/drivers/drv_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ static time_t get_timestamp(void)

static int set_timestamp(time_t timestamp)
{
struct tm *p_tm;
struct tm now;
snvs_hp_rtc_datetime_t rtcDate = {0};

p_tm = gmtime(&timestamp);
gmtime_r(&timestamp, &now);

rtcDate.second = p_tm->tm_sec ;
rtcDate.minute = p_tm->tm_min ;
rtcDate.hour = p_tm->tm_hour;
rtcDate.second = now.tm_sec ;
rtcDate.minute = now.tm_min ;
rtcDate.hour = now.tm_hour;

rtcDate.day = p_tm->tm_mday;
rtcDate.month = p_tm->tm_mon + 1;
rtcDate.year = p_tm->tm_year + 1900;
rtcDate.day = now.tm_mday;
rtcDate.month = now.tm_mon + 1;
rtcDate.year = now.tm_year + 1900;

if (SNVS_HP_RTC_SetDatetime(SNVS, &rtcDate) != kStatus_Success)
{
Expand Down
28 changes: 14 additions & 14 deletions bsp/loongson/ls1bdev/drivers/drv_uart.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand Down Expand Up @@ -181,19 +181,19 @@ void rt_hw_uart_init(void)
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;

#ifdef RT_USING_UART5
uart = &uart5;
serial5.ops = &ls1b_uart_ops;
serial5.config = config;
rt_hw_interrupt_install(uart->IRQ, uart_irq_handler, &serial5, "UART5");
/* register UART5 device */
rt_hw_serial_register(&serial5,
"uart5",
//RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX,
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
uart);
uart = &uart5;

serial5.ops = &ls1b_uart_ops;
serial5.config = config;

rt_hw_interrupt_install(uart->IRQ, uart_irq_handler, &serial5, "UART5");

/* register UART5 device */
rt_hw_serial_register(&serial5,
"uart5",
//RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX,
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
uart);
#endif /* RT_USING_UART5 */


Expand Down
34 changes: 17 additions & 17 deletions bsp/loongson/ls1cdev/drivers/display_controller.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2011-08-09 lgnq first version for LS1B DC
* 2015-07-06 chinesebear modified for loongson 1c
* 2018-01-06 sundm75 modified for smartloong
* 2018-01-06 sundm75 modified for smartloong
*/
#include <rtthread.h>

Expand Down Expand Up @@ -78,13 +78,13 @@ int caclulate_freq(rt_uint32_t XIN, rt_uint32_t PCLK)
pix_div = (pix_div>>24)&0xff;
rt_kprintf("old pll_clk=%d, pix_div=%d\n", pll_clk, pix_div);

divider_int = pll_clk/(1000000) *PCLK/1000;
divider_int = pll_clk/(1000000) *PCLK/1000;
if(divider_int%1000>=500)
divider_int = divider_int/1000+1;
else
divider_int = divider_int/1000;
rt_kprintf("divider_int = %d\n", divider_int);

/* check whether divisor is too small. */
if (divider_int < 1) {
rt_kprintf("Warning: clock source is too slow.Try smaller resolution\n");
Expand All @@ -102,9 +102,9 @@ int caclulate_freq(rt_uint32_t XIN, rt_uint32_t PCLK)
regval &= ~0x80000030; //PIX_DIV_VALID PIX_SEL 置0
regval &= ~(0x3f<<24); //PIX_DIV 清零
regval |= divider_int << 24;
PLL_DIV_PARAM = regval;
PLL_DIV_PARAM = regval;
regval |= 0x80000030; //PIX_DIV_VALID PIX_SEL 置1
PLL_DIV_PARAM = regval;
PLL_DIV_PARAM = regval;
}
rt_kprintf("new PLL_FREQ=0x%x, PLL_DIV_PARAM=0x%x\n", PLL_FREQ, PLL_DIV_PARAM);
rt_thread_delay(10);
Expand All @@ -115,11 +115,11 @@ static rt_err_t rt_dc_init(rt_device_t dev)
{
int i, out, mode=-1;
int val;

rt_kprintf("PWM initied\n");
/* Set the back light PWM. */
pwminit();

for (i=0; i<sizeof(vga_mode)/sizeof(struct vga_struct); i++)
{
if (vga_mode[i].hr == FB_XSIZE && vga_mode[i].vr == FB_YSIZE)
Expand All @@ -140,7 +140,7 @@ static rt_err_t rt_dc_init(rt_device_t dev)
DC_FB_CONFIG = 0x0;
DC_FB_CONFIG = 0x3; // // framebuffer configuration RGB565
DC_DITHER_CONFIG = 0x0; //颜色抖动配置寄存器
DC_DITHER_TABLE_LOW = 0x0; //颜色抖动查找表低位寄存器
DC_DITHER_TABLE_LOW = 0x0; //颜色抖动查找表低位寄存器
DC_DITHER_TABLE_HIGH = 0x0; //颜色抖动查找表高位寄存器
DC_PANEL_CONFIG = 0x80001311; //液晶面板配置寄存器
DC_PANEL_TIMING = 0x0;
Expand All @@ -165,10 +165,10 @@ static rt_err_t rt_dc_init(rt_device_t dev)
#elif defined(CONFIG_VIDEO_12BPP)
DC_FB_CONFIG = 0x00100101;
DC_FB_BUFFER_STRIDE = (FB_XSIZE*2+255)&(~255);
#else
#else
DC_FB_CONFIG = 0x00100104;
DC_FB_BUFFER_STRIDE = (FB_XSIZE*4+255)&(~255);
#endif
#endif
return RT_EOK;
}

Expand Down Expand Up @@ -200,7 +200,7 @@ static rt_err_t rt_dc_control(rt_device_t dev, int cmd, void *args)
break;
case RTGRAPHIC_CTRL_POWEROFF:
break;
case RTGRAPHIC_CTRL_GET_INFO:
case RTGRAPHIC_CTRL_GET_INFO:
rt_memcpy(args, &_dc_info, sizeof(_dc_info));
break;
case RTGRAPHIC_CTRL_SET_MODE:
Expand All @@ -213,7 +213,7 @@ static rt_err_t rt_dc_control(rt_device_t dev, int cmd, void *args)
void rt_hw_dc_init(void)
{
rt_device_t dc = rt_malloc(sizeof(struct rt_device));
if (dc == RT_NULL)
if (dc == RT_NULL)
{
rt_kprintf("dc == RT_NULL\n");
return; /* no memory yet */
Expand All @@ -232,10 +232,10 @@ void rt_hw_dc_init(void)
dc->close = RT_NULL;
dc->control = rt_dc_control;
dc->user_data = (void*)&_dc_info;

/* register Display Controller device to RT-Thread */
rt_device_register(dc, "dc", RT_DEVICE_FLAG_RDWR);

rt_device_init(dc);
}

Expand All @@ -250,7 +250,7 @@ int rtgui_lcd_init(void)

pin_set_purpose(76, PIN_PURPOSE_OTHER);
pin_set_remap(76, PIN_REMAP_DEFAULT);

/* init Display Controller */
rt_hw_dc_init();

Expand All @@ -259,7 +259,7 @@ int rtgui_lcd_init(void)

/* set Display Controller device as rtgui graphic driver */
rtgui_graphic_set_device(dc);

return 0;
}

Expand Down
Loading