1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
#define SII_OSAL_LINUX_TIMER_C
#include <linux/workqueue.h>
#include <linux/hrtimer.h>
#include <linux/slab.h>
#include "sii_hal.h"
#include "osal/include/osal.h"
#define MSEC_TO_NSEC(x) (x * 1000000UL)
#define MAX_TIMER_NAME_LEN 16
typedef struct _SiiOsTimerInfo_t {
struct list_head listEntry;
struct work_struct workItem;
uint8_t flags;
char timerName[MAX_TIMER_NAME_LEN];
struct hrtimer hrTimer;
timerCallbackHandler_t callbackHandler;
void *callbackParam;
uint32_t timeMsec;
bool bPeriodic;
} timerObject_t;
#define TIMER_OBJ_FLAG_WORK_IP 0x01
#define TIMER_OBJ_FLAG_DEL_REQ 0x02
static struct list_head timerList;
static struct workqueue_struct *timerWorkQueue;
static void WorkHandler(struct work_struct *work)
{
timerObject_t *pTimerObj = container_of(work, timerObject_t, workItem);
pTimerObj->flags |= TIMER_OBJ_FLAG_WORK_IP;
if (HalAcquireIsrLock() == HAL_RET_SUCCESS) {
if (pTimerObj->callbackHandler)
(pTimerObj->callbackHandler) (pTimerObj->callbackParam);
HalReleaseIsrLock();
}
pTimerObj->flags &= ~TIMER_OBJ_FLAG_WORK_IP;
if (pTimerObj->flags & TIMER_OBJ_FLAG_DEL_REQ) {
kfree(pTimerObj);
}
}
static enum hrtimer_restart TimerHandler(struct hrtimer *timer)
{
timerObject_t *pTimerObj = container_of(timer, timerObject_t, hrTimer);
ktime_t timerPeriod;
queue_work(timerWorkQueue, &pTimerObj->workItem);
if (pTimerObj->bPeriodic) {
timerPeriod = ktime_set(0, MSEC_TO_NSEC(pTimerObj->timeMsec));
hrtimer_forward(&pTimerObj->hrTimer,
pTimerObj->hrTimer.base->get_time(), timerPeriod);
return HRTIMER_RESTART;
}
return HRTIMER_NORESTART;
}
SiiOsStatus_t SiiOsInit(uint32_t maxChannels)
{
INIT_LIST_HEAD(&timerList);
timerWorkQueue = create_workqueue("Sii_timer_work");
if (timerWorkQueue == NULL) {
return SII_OS_STATUS_ERR_NOT_AVAIL;
}
return SII_OS_STATUS_SUCCESS;
}
SiiOsStatus_t SiiOsTerm(void)
{
timerObject_t *timerObj;
int status;
while (!list_empty(&timerList)) {
timerObj = list_first_entry(&timerList, timerObject_t, listEntry);
status = hrtimer_try_to_cancel(&timerObj->hrTimer);
if (status >= 0) {
list_del(&timerObj->listEntry);
kfree(timerObj);
}
}
flush_workqueue(timerWorkQueue);
destroy_workqueue(timerWorkQueue);
timerWorkQueue = NULL;
return SII_OS_STATUS_SUCCESS;
}
SiiOsStatus_t SiiOsTimerCreate(const char *pName, void (*pTimerFunction) (void *pArg),
void *pTimerArg, bool_t timerStartFlag,
uint32_t timeMsec, bool_t periodicFlag, SiiOsTimer_t *pTimerId)
{
timerObject_t *timerObj;
SiiOsStatus_t status = SII_OS_STATUS_SUCCESS;
if (pTimerFunction == NULL) {
return SII_OS_STATUS_ERR_INVALID_PARAM;
}
timerObj = kmalloc(sizeof(timerObject_t), GFP_KERNEL);
if (timerObj == NULL) {
return SII_OS_STATUS_ERR_NOT_AVAIL;
}
strncpy(timerObj->timerName, pName, MAX_TIMER_NAME_LEN - 1);
timerObj->timerName[MAX_TIMER_NAME_LEN - 1] = 0;
timerObj->callbackHandler = pTimerFunction;
timerObj->callbackParam = pTimerArg;
timerObj->timeMsec = timeMsec;
timerObj->bPeriodic = periodicFlag;
INIT_WORK(&timerObj->workItem, WorkHandler);
list_add(&timerObj->listEntry, &timerList);
hrtimer_init(&timerObj->hrTimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
timerObj->hrTimer.function = TimerHandler;
if (timerStartFlag) {
status = SiiOsTimerSchedule(timerObj, timeMsec);
}
*pTimerId = timerObj;
return status;
}
SiiOsStatus_t SiiOsTimerDelete(SiiOsTimer_t timerId)
{
timerObject_t *timerObj;
list_for_each_entry(timerObj, &timerList, listEntry) {
if (timerObj == timerId) {
break;
}
}
if (timerObj != timerId) {
SII_DEBUG_PRINT(SII_OSAL_DEBUG_TRACE,
"Invalid timerId %p passed to SiiOsTimerDelete\n", timerId);
return SII_OS_STATUS_ERR_INVALID_PARAM;
}
list_del(&timerObj->listEntry);
hrtimer_cancel(&timerObj->hrTimer);
if (timerObj->flags & TIMER_OBJ_FLAG_WORK_IP) {
timerObj->flags |= TIMER_OBJ_FLAG_DEL_REQ;
return SII_OS_STATUS_SUCCESS;
}
cancel_work_sync(&timerObj->workItem);
kfree(timerObj);
return SII_OS_STATUS_SUCCESS;
}
SiiOsStatus_t SiiOsTimerSchedule(SiiOsTimer_t timerId, uint32_t timeMsec)
{
timerObject_t *timerObj;
ktime_t timerPeriod;
list_for_each_entry(timerObj, &timerList, listEntry) {
if (timerObj == timerId) {
break;
}
}
if (timerObj != timerId) {
SII_DEBUG_PRINT(SII_OSAL_DEBUG_TRACE,
"Invalid timerId %p passed to SiiOsTimerSchedule\n", timerId);
return SII_OS_STATUS_ERR_INVALID_PARAM;
}
timerPeriod = ktime_set(0, MSEC_TO_NSEC(timeMsec));
hrtimer_start(&timerObj->hrTimer, timerPeriod, HRTIMER_MODE_REL);
return SII_OS_STATUS_SUCCESS;
}
uint32_t SiiOsGetTimeResolution(void)
{
return 1;
}
|