© 2024 EasyEDA Some rights reserved ISO/IEC
1.Easy to use and quick to get started
2.The process supports design scales of 300 devices or 1000 pads
3.Supports simple circuit simulation
4.For students, teachers, creators
1.Brand new interactions and interfaces
2.Smooth support for design sizes of over 5,000 devices or 10,000 pads
3.More rigorous design constraints, more standardized processes
4.For enterprises, more professional users
STD LPC1788 Industrial project
Mode: Editors' pick
/*
* File : timer.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2006-03-12 Bernard first version
* 2006-04-29 Bernard implement thread timer
* 2006-06-04 Bernard implement rt_timer_control
* 2006-08-10 Bernard fix the periodic timer bug
* 2006-09-03 Bernard implement rt_timer_detach
* 2009-11-11 LiJin add soft timer
* 2010-05-12 Bernard fix the timer check bug.
* 2010-11-02 Charlie re-implement tick overflow issue
* 2012-12-15 Bernard fix the next timeout issue in soft timer
*/
#include
#include
/* hard timer list */
static rt_list_t rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL];
#ifdef RT_USING_TIMER_SOFT
#ifndef RT_TIMER_THREAD_STACK_SIZE
#define RT_TIMER_THREAD_STACK_SIZE 512
#endif
#ifndef RT_TIMER_THREAD_PRIO
#define RT_TIMER_THREAD_PRIO 0
#endif
/* soft timer list */
static rt_list_t rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL];
static struct rt_thread timer_thread;
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
#endif
#ifdef RT_USING_HOOK
extern void (*rt_object_take_hook)(struct rt_object *object);
extern void (*rt_object_put_hook)(struct rt_object *object);
static void (*rt_timer_timeout_hook)(struct rt_timer *timer);
/**
* @addtogroup Hook
*/
/*@{*/
/**
* This function will set a hook function, which will be invoked when timer
* is timeout.
*
* @param hook the hook function
*/
void rt_timer_timeout_sethook(void (*hook)(struct rt_timer *timer))
{
rt_timer_timeout_hook = hook;
}
/*@}*/
#endif
static void _rt_timer_init(rt_timer_t timer,
void (*timeout)(void *parameter),
void *parameter,
rt_tick_t time,
rt_uint8_t flag)
{
int i;
/* set flag */
timer->parent.flag = flag;
/* set deactivated */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
timer->timeout_func = timeout;
timer->parameter = parameter;
timer->timeout_tick = 0;
timer->init_tick = time;
/* initialize timer list */
for (i = 0; i row[i]));
}
}
/* the fist timer always in the last row */
static rt_tick_t rt_timer_list_next_timeout(rt_list_t timer_list[])
{
struct rt_timer *timer;
if (rt_list_isempty(&timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
return RT_TICK_MAX;
timer = rt_list_entry(timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
return timer->timeout_tick;
}
rt_inline void _rt_timer_remove(rt_timer_t timer)
{
int i;
for (i = 0; i row[i]);
}
}
static int rt_timer_count_height(struct rt_timer *timer)
{
int i, cnt = 0;
for (i = 0; i row[i]))
cnt++;
}
return cnt;
}
void rt_timer_dump(rt_list_t timer_heads[])
{
rt_list_t *list;
for (list = timer_heads[RT_TIMER_SKIP_LIST_LEVEL-1].next;
list != &timer_heads[RT_TIMER_SKIP_LIST_LEVEL-1];
list = list->next)
{
struct rt_timer *timer = rt_list_entry(list,
struct rt_timer,
row[RT_TIMER_SKIP_LIST_LEVEL-1]);
rt_kprintf("%d", rt_timer_count_height(timer));
}
rt_kprintf("\n");
}
/**
* @addtogroup Clock
*/
/*@{*/
/**
* This function will initialize a timer, normally this function is used to
* initialize a static timer object.
*
* @param timer the static timer object
* @param name the name of timer
* @param timeout the timeout function
* @param parameter the parameter of timeout function
* @param time the tick of timer
* @param flag the flag of timer
*/
void rt_timer_init(rt_timer_t timer,
const char *name,
void (*timeout)(void *parameter),
void *parameter,
rt_tick_t time,
rt_uint8_t flag)
{
/* timer check */
RT_ASSERT(timer != RT_NULL);
/* timer object initialization */
rt_object_init((rt_object_t)timer, RT_Object_Class_Timer, name);
_rt_timer_init(timer, timeout, parameter, time, flag);
}
RTM_EXPORT(rt_timer_init);
/**
* This function will detach a timer from timer management.
*
* @param timer the static timer object
*
* @return the operation status, RT_EOK on OK; RT_ERROR on error
*/
rt_err_t rt_timer_detach(rt_timer_t timer)
{
register rt_base_t level;
/* timer check */
RT_ASSERT(timer != RT_NULL);
/* disable interrupt */
level = rt_hw_interrupt_disable();
_rt_timer_remove(timer);
/* enable interrupt */
rt_hw_interrupt_enable(level);
rt_object_detach((rt_object_t)timer);
return -RT_EOK;
}
RTM_EXPORT(rt_timer_detach);
#ifdef RT_USING_HEAP
/**
* This function will create a timer
*
* @param name the name of timer
* @param timeout the timeout function
* @param parameter the parameter of timeout function
* @param time the tick of timer
* @param flag the flag of timer
*
* @return the created timer object
*/
rt_timer_t rt_timer_create(const char *name,
void (*timeout)(void *parameter),
void *parameter,
rt_tick_t time,
rt_uint8_t flag)
{
struct rt_timer *timer;
/* allocate a object */
timer = (struct rt_timer *)rt_object_allocate(RT_Object_Class_Timer, name);
if (timer == RT_NULL)
{
return RT_NULL;
}
_rt_timer_init(timer, timeout, parameter, time, flag);
return timer;
}
RTM_EXPORT(rt_timer_create);
/**
* This function will delete a timer and release timer memory
*
* @param timer the timer to be deleted
*
* @return the operation status, RT_EOK on OK; RT_ERROR on error
*/
rt_err_t rt_timer_delete(rt_timer_t timer)
{
register rt_base_t level;
/* timer check */
RT_ASSERT(timer != RT_NULL);
/* disable interrupt */
level = rt_hw_interrupt_disable();
_rt_timer_remove(timer);
/* enable interrupt */
rt_hw_interrupt_enable(level);
rt_object_delete((rt_object_t)timer);
return -RT_EOK;
}
RTM_EXPORT(rt_timer_delete);
#endif
/**
* This function will start the timer
*
* @param timer the timer to be started
*
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
*/
rt_err_t rt_timer_start(rt_timer_t timer)
{
int row_lvl;
rt_list_t *timer_list;
register rt_base_t level;
rt_list_t *row_head[RT_TIMER_SKIP_LIST_LEVEL];
unsigned int tst_nr;
static unsigned int random_nr;
/* timer check */
RT_ASSERT(timer != RT_NULL);
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
return -RT_ERROR;
RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
/*
* get timeout tick,
* the max timeout tick shall not great than RT_TICK_MAX/2
*/
RT_ASSERT(timer->init_tick timeout_tick = rt_tick_get() + timer->init_tick;
/* disable interrupt */
level = rt_hw_interrupt_disable();
#ifdef RT_USING_TIMER_SOFT
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
{
/* insert timer to soft timer list */
timer_list = rt_soft_timer_list;
}
else
#endif
{
/* insert timer to system timer list */
timer_list = rt_timer_list;
}
row_head[0] = &timer_list[0];
for (row_lvl = 0; row_lvl next)
{
struct rt_timer *t;
rt_list_t *p = row_head[row_lvl]->next;
/* fix up the entry pointer */
t = rt_list_entry(p, struct rt_timer, row[row_lvl]);
/* If we have two timers that timeout at the same time, it's
* preferred that the timer inserted early get called early.
* So insert the new timer to the end the the some-timeout timer
* list.
*/
if ((t->timeout_tick - timer->timeout_tick) == 0)
{
continue;
}
else if ((t->timeout_tick - timer->timeout_tick) timeout_tick very easily
* (actually, the timeout_tick is not random and easy to be attacked). */
random_nr++;
tst_nr = random_nr;
rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL-1],
&(timer->row[RT_TIMER_SKIP_LIST_LEVEL-1]));
for (row_lvl = 2; row_lvl row[RT_TIMER_SKIP_LIST_LEVEL - row_lvl]));
else
break;
/* Shift over the bits we have tested. Works well with 1 bit and 2
* bits. */
tst_nr >>= (RT_TIMER_SKIP_LIST_MASK+1)>>1;
}
timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
/* enable interrupt */
rt_hw_interrupt_enable(level);
#ifdef RT_USING_TIMER_SOFT
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
{
/* check whether timer thread is ready */
if (timer_thread.stat != RT_THREAD_READY)
{
/* resume timer thread to check soft timer */
rt_thread_resume(&timer_thread);
rt_schedule();
}
}
#endif
return -RT_EOK;
}
RTM_EXPORT(rt_timer_start);
/**
* This function will stop the timer
*
* @param timer the timer to be stopped
*
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
*/
rt_err_t rt_timer_stop(rt_timer_t timer)
{
register rt_base_t level;
/* timer check */
RT_ASSERT(timer != RT_NULL);
if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
return -RT_ERROR;
RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
/* disable interrupt */
level = rt_hw_interrupt_disable();
_rt_timer_remove(timer);
/* enable interrupt */
rt_hw_interrupt_enable(level);
/* change stat */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
return RT_EOK;
}
RTM_EXPORT(rt_timer_stop);
/**
* This function will get or set some options of the timer
*
* @param timer the timer to be get or set
* @param cmd the control command
* @param arg the argument
*
* @return RT_EOK
*/
rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void *arg)
{
/* timer check */
RT_ASSERT(timer != RT_NULL);
switch (cmd)
{
case RT_TIMER_CTRL_GET_TIME:
*(rt_tick_t *)arg = timer->init_tick;
break;
case RT_TIMER_CTRL_SET_TIME:
timer->init_tick = *(rt_tick_t *)arg;
break;
case RT_TIMER_CTRL_SET_ONESHOT:
timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
break;
case RT_TIMER_CTRL_SET_PERIODIC:
timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
break;
}
return RT_EOK;
}
RTM_EXPORT(rt_timer_control);
/**
* This function will check timer list, if a timeout event happens, the
* corresponding timeout function will be invoked.
*
* @note this function shall be invoked in operating system timer interrupt.
*/
void rt_timer_check(void)
{
struct rt_timer *t;
rt_tick_t current_tick;
register rt_base_t level;
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));
current_tick = rt_tick_get();
/* disable interrupt */
level = rt_hw_interrupt_disable();
while (!rt_list_isempty(&rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL-1]))
{
t = rt_list_entry(rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
/*
* It supposes that the new tick shall less than the half duration of
* tick max.
*/
if ((current_tick - t->timeout_tick) timeout_func(t->parameter);
/* re-get tick */
current_tick = rt_tick_get();
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
{
/* start it */
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
rt_timer_start(t);
}
else
{
/* stop timer */
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
}
}
else
break;
}
/* enable interrupt */
rt_hw_interrupt_enable(level);
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
}
/**
* This function will return the next timeout tick in the system.
*
* @return the next timeout tick in the system
*/
rt_tick_t rt_timer_next_timeout_tick(void)
{
return rt_timer_list_next_timeout(rt_timer_list);
}
#ifdef RT_USING_TIMER_SOFT
/**
* This function will check timer list, if a timeout event happens, the
* corresponding timeout function will be invoked.
*/
void rt_soft_timer_check(void)
{
rt_tick_t current_tick;
rt_list_t *n;
struct rt_timer *t;
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check enter\n"));
current_tick = rt_tick_get();
for (n = rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL-1].next;
n != &(rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL-1]);)
{
t = rt_list_entry(n, struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL-1]);
/*
* It supposes that the new tick shall less than the half duration of
* tick max.
*/
if ((current_tick - t->timeout_tick) next;
/* remove timer from timer list firstly */
_rt_timer_remove(t);
/* call timeout function */
t->timeout_func(t->parameter);
/* re-get tick */
current_tick = rt_tick_get();
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
{
/* start it */
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
rt_timer_start(t);
}
else
{
/* stop timer */
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
}
}
else break; /* not check anymore */
}
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
}
/* system timer thread entry */
static void rt_thread_timer_entry(void *parameter)
{
rt_tick_t next_timeout;
while (1)
{
/* get the next timeout tick */
next_timeout = rt_timer_list_next_timeout(rt_soft_timer_list);
if (next_timeout == RT_TICK_MAX)
{
/* no software timer exist, suspend self. */
rt_thread_suspend(rt_thread_self());
rt_schedule();
}
else
{
rt_tick_t current_tick;
/* get current tick */
current_tick = rt_tick_get();
if ((next_timeout - current_tick)
[1]: /editor/20160103/568913beb2ad0.jpg
[2]: /editor/20160103/568913cb43266.jpg
[3]: /editor/20160103/568913d32a723.jpgID | Designator | Footprint | Quantity |
---|---|---|---|
1 | U3 | TQSP48 | 1 |
2 | U1 | SOT459-1_M | 1 |
3 | U2 | 54_TSOP2_400AF_N | 1 |
4 | R20,R22 | 0805R | 2 |
5 | U13 | VBH48A_L | 1 |
6 | U15,U16 | MP04A_N | 2 |
7 | C1,C2,C5,C3,C6,C4,C7,C8,C9,C10,C11,C12,C13,C14,C15,C16,C17,C18,C19,C20,C21,C27,C29,C30,C31,C32,C33,C34,C35,C22,C36,C39,C38,C37,C40,C41,C46,C49,C50,C51,C24,C25,C23,C26,C54,C77,C65,C62,C96,C97,C98,C100,C103,C99,C102,C101,C104,C59,C58,C110,C60,C53,C61,C52,C66,C85,C28,C63,C67,C68,C79,C80,C81,C82,C83,C84 | 0603C | 76 |
8 | R9,R8,R53,R54,R44,R45,R25,R27,R28 | 0603R | 9 |
9 | R10 | 0603R | 1 |
10 | R11,R36,R37,R61,R55,R47,R46,R62,R33,R64,R34,R35,R69,R70,R68,R71 | 0603R | 16 |
11 | R12,R2,R3,R4,R5,R6 | 0603R | 6 |
12 | L6,L5,R7,L13 | 0603R | 4 |
13 | R1,R21,R31,R38,R40,R42,R48,R51,R59,R63,R66 | 0603R | 11 |
14 | R13,R14,R15,R16 | 0603R | 4 |
15 | X4 | 贴片有源晶体 | 1 |
16 | E10,E1,E2,E6,E7,E8,E14,E12,E13 | 3528 钽电容 | 9 |
17 | R19,R17,R50,R58,R26,R30,R32,R39,R41,R43,R49,R57,R60,R65,R67 | 0603R | 15 |
18 | E11 | 3528 钽电容 | 1 |
19 | X1 | XTAL-SMD2 | 1 |
20 | X2 | XATL-32K | 1 |
21 | C42,C43,C44,C45,C94,C95 | 0603C | 6 |
22 | D9 | LED-0805 | 1 |
23 | R52 | 0603R | 1 |
24 | LAN | HR911105A | 1 |
25 | E4,E5 | RB.10 | 2 |
26 | E3 | RB.10 | 1 |
27 | FIX1,FIX2,FIX3,FIX4 | HOLE157-8STAR | 4 |
28 | CN2,CN1 | JP_3.5 1*10 | 2 |
29 | D2,D3 | SMB | 2 |
30 | L1,L9 | 电感12*12 | 2 |
31 | L3,L4,L2,L7,L8,L11,L12,L10 | L0603 | 8 |
32 | U4 | SOT23_N | 1 |
33 | U8 | SO16NB | 1 |
34 | USBDEVICE | USB_B | 1 |
35 | R56 | 0603R | 1 |
36 | U12,U11 | SOT23-6_N | 2 |
37 | D10 | LED-0805 | 1 |
38 | USBHOST | USB-H | 1 |
39 | U5 | SOT23_N | 1 |
40 | JTAG | RJ11 | 1 |
41 | T1 | EMI24V | 1 |
42 | C56,C57,C55,C71,C48,C47,C70 | 0805C | 7 |
43 | D5 | SOT23_N | 1 |
44 | COM0 | DB9 公 90° | 1 |
45 | RP1,RP2 | RA4 | 2 |
46 | U21 | LQFP48 | 1 |
47 | X3 | XTAL-SMD2 | 1 |
48 | R24,R23 | 0603R | 2 |
49 | C105 | 0603C | 1 |
50 | C106,C107,C108,C109 | 0603C | 4 |
51 | SD | TF-card | 1 |
52 | U23 | SOT-23-5 | 1 |
53 | CN7 | EAR-5P-3.5 | 1 |
54 | U22 | DBQ016 | 1 |
55 | C75,C76,C73,C74 | 0603C | 4 |
56 | CN6 | FPC-1.0-4P | 1 |
57 | R18 | 0603R | 1 |
58 | Q1,Q2 | SOT23_N | 2 |
59 | E9,E15 | 3528 钽电容 | 2 |
60 | U24 | SO16NB | 1 |
61 | CN3 | CN-4-2.0 | 1 |
62 | C69,C72 | 0603C | 2 |
63 | U9 | SO8-400W | 1 |
64 | R29 | 0805R | 1 |
65 | U25 | BS0505 | 1 |
66 | U19,U6 | SO-16W | 2 |
67 | S1 | KEY_2 | 1 |
68 | U7,U17,U26,U27,U28,U29,U30,U32,U33,U34 | SOP-4 | 10 |
69 | RP3,RP4,RP5 | RA4 | 3 |
70 | U10,U31 | SO16NB | 2 |
71 | U35,U36 | SO16NB | 2 |
72 | K1,K2,K3,K4,K5,K6,K7,K8,K9,K10 | HF49FD | 10 |
73 | CN4 | JP_3.5 1*12 | 1 |
74 | CN5 | CN-4-2.0 | 1 |
75 | LCD1 | FPC-0.5-40P | 1 |
76 | BT1 | 电池座 | 1 |
77 | FU1 | 1812-PPTC | 1 |
78 | U14,U20 | TO-263-5 | 2 |
79 | D1 | SMB | 1 |
80 | U18 | SOT-23-5 | 1 |
81 | Q3,Q4 | SOT23_N | 2 |
82 | D12,D11 | D1206 | 2 |
Unfold
Loading...
Do you need to add this project to the album?