]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_SimpleLink_CC3220SF_CCS/ti/drivers/dpl/ClockP_freertos.c
Add SimpleLink CC3220SF demo.
[freertos] / FreeRTOS / Demo / CORTEX_M4_SimpleLink_CC3220SF_CCS / ti / drivers / dpl / ClockP_freertos.c
1 /*
2  * Copyright (c) 2015-2016, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 /*
34  *  ======== ClockP_freertos.c ========
35  */
36
37 #include <ti/drivers/dpl/ClockP.h>
38 #include <FreeRTOS.h>
39 #include <timers.h>
40
41 #include <stdint.h>
42 #include <stdbool.h>
43 #include <stdlib.h>
44
45 static TickType_t ticksToWait = portMAX_DELAY;
46
47 void ClockP_callbackFxn(uintptr_t arg);
48
49 typedef struct ClockP_FreeRTOSObj {
50     TimerHandle_t    timer;
51     ClockP_Fxn    fxn;
52     uintptr_t        arg;
53 } ClockP_FreeRTOSObj;
54
55 /*
56  *  ======== ClockP_callbackFxn ========
57  */
58 void ClockP_callbackFxn(uintptr_t arg)
59 {
60     TimerHandle_t    handle = (TimerHandle_t)arg;
61     ClockP_FreeRTOSObj *obj;
62
63     obj = (ClockP_FreeRTOSObj *)pvTimerGetTimerID(handle);
64     (obj->fxn)(obj->arg);
65 }
66
67 /*
68  *  ======== ClockP_create ========
69  */
70 ClockP_Handle ClockP_create(ClockP_Fxn clockFxn, ClockP_Params *params)
71 {
72     ClockP_Params defaultParams;
73     ClockP_FreeRTOSObj *pObj;
74     TimerHandle_t    handle = NULL;
75
76     if (params == NULL) {
77         params = &defaultParams;
78         ClockP_Params_init(&defaultParams);
79     }
80
81     if ((pObj = pvPortMalloc(sizeof(ClockP_FreeRTOSObj))) == NULL) {
82         return (NULL);
83     }
84
85     handle = xTimerCreate(params->name, 1, 0, (void *)pObj,
86                           (TimerCallbackFunction_t)ClockP_callbackFxn);
87
88     if (handle == NULL) {
89         vPortFree(pObj);
90         return (NULL);
91     }
92
93     pObj->timer = handle;
94     pObj->fxn = clockFxn;
95     pObj->arg = params->arg;
96
97     return ((ClockP_Handle)pObj);
98 }
99
100 /*
101  *  ======== ClockP_delete ========
102  */
103 ClockP_Status ClockP_delete(ClockP_Handle handle)
104 {
105     ClockP_FreeRTOSObj *pObj = (ClockP_FreeRTOSObj *)handle;
106     BaseType_t status;
107
108     status = xTimerDelete((TimerHandle_t)pObj->timer, ticksToWait);
109
110     if (status != pdPASS) {
111         return (ClockP_FAILURE);
112     }
113
114     vPortFree(pObj);
115
116     return (ClockP_OK);
117 }
118
119 /*
120  *  ======== ClockP_getCpuFreq ========
121  */
122 void ClockP_getCpuFreq(ClockP_FreqHz *freq)
123 {
124     unsigned long configCpuFreq;
125
126     /*
127      *  configCPU_CLOCK_HZ is #define'd in the target's header file,
128      *  eg, in FreeRTOS/Demo/ARM7_AT91FR40008_GCC/FreeRTOSConfig.h.
129      *  Sometimes configCPU_CLOCK_HZ is #define'd to a specific value,
130      *  or to an extern uint32_t variable, eg:
131      *
132      *  #define configCPU_CLOCK_HZ     ( SystemFrequency )  // extern uint32_t
133      *
134      *  #define configCPU_CLOCK_HZ     ( ( unsigned long ) 8000000 )
135      */
136
137     configCpuFreq = (unsigned long)configCPU_CLOCK_HZ;
138     freq->lo = (uint32_t)configCpuFreq;
139     freq->hi = 0;
140 //    freq->hi = (uint32_t)(configCpuFreq >> 32);
141 }
142
143 /*
144  *  ======== ClockP_getSystemTickPeriod ========
145  */
146 uint32_t ClockP_getSystemTickPeriod()
147 {
148     uint32_t tickPeriodUs;
149
150     /*
151      *  Tick period in microseconds. configTICK_RATE_HZ is defined in the
152      *  application's FreeRTOSConfig.h, which is include by FreeRTOS.h
153      */
154     tickPeriodUs = 1000000 / configTICK_RATE_HZ;
155
156     return (tickPeriodUs);
157 }
158
159 /*
160  *  ======== ClockP_getSystemTicks ========
161  *  TODO determine if we ever call this from an ISR
162  */
163 uint32_t ClockP_getSystemTicks()
164 {
165     return ((uint32_t)xTaskGetTickCount());
166 }
167
168 /*
169  *  ======== ClockP_Params_init ========
170  */
171 void ClockP_Params_init(ClockP_Params *params)
172 {
173     params->name = NULL;
174     params->arg = (uintptr_t)0;
175 }
176
177 /*
178  *  ======== ClockP_start ========
179  */
180 ClockP_Status ClockP_start(ClockP_Handle handle, uint32_t timeout)
181 {
182     ClockP_FreeRTOSObj *pObj = (ClockP_FreeRTOSObj *)handle;
183     BaseType_t status;
184
185     status = xTimerChangePeriod(pObj->timer, (TickType_t)timeout, ticksToWait);
186
187     if (status != pdPASS) {
188         return (ClockP_FAILURE);
189     }
190     status = xTimerStart(pObj->timer, ticksToWait);
191
192     if (status != pdPASS) {
193         return (ClockP_FAILURE);
194     }
195
196     return (ClockP_OK);
197 }
198
199 /*
200  *  ======== ClockP_startFromISR ========
201  */
202 ClockP_Status ClockP_startFromISR(ClockP_Handle handle, uint32_t timeout)
203 {
204     ClockP_FreeRTOSObj *pObj = (ClockP_FreeRTOSObj *)handle;
205     BaseType_t xHigherPriorityTaskWoken;
206     BaseType_t status;
207
208     status = xTimerChangePeriodFromISR(pObj->timer, (TickType_t)timeout,
209                                        &xHigherPriorityTaskWoken);
210     if (status != pdPASS) {
211         return (ClockP_FAILURE);
212     }
213     status = xTimerStartFromISR(pObj->timer, &xHigherPriorityTaskWoken);
214
215     if (status != pdPASS) {
216         return (ClockP_FAILURE);
217     }
218
219     return (ClockP_OK);
220 }
221
222 /*
223  *  ======== ClockP_stop ========
224  */
225 ClockP_Status ClockP_stop(ClockP_Handle handle)
226 {
227     ClockP_FreeRTOSObj *pObj = (ClockP_FreeRTOSObj *)handle;
228     BaseType_t status;
229
230     status = xTimerStop(pObj->timer, ticksToWait);
231
232     if (status != pdPASS) {
233         return (ClockP_FAILURE);
234     }
235     return (ClockP_OK);
236 }
237
238 /*
239  *  ======== ClockP_stopFromISR ========
240  */
241 ClockP_Status ClockP_stopFromISR(ClockP_Handle handle)
242 {
243     ClockP_FreeRTOSObj *pObj = (ClockP_FreeRTOSObj *)handle;
244     BaseType_t xHigherPriorityTaskWoken;
245     BaseType_t status;
246
247     status = xTimerStopFromISR(pObj->timer, &xHigherPriorityTaskWoken);
248
249     if (status != pdPASS) {
250         return (ClockP_FAILURE);
251     }
252     return (ClockP_OK);
253 }
254
255 /*
256  *  ======== ClockP_sleep ========
257  */
258 ClockP_Status ClockP_sleep(uint32_t sec)
259 {
260     uint32_t msecs = sec * 1000;
261     TickType_t xDelay;
262
263     /* Take the ceiling */
264     xDelay = (msecs + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS;
265
266     vTaskDelay(xDelay);
267
268     return (ClockP_OK);
269 }
270
271 /*
272  *  ======== ClockP_usleep ========
273  */
274 ClockP_Status ClockP_usleep(uint32_t usec)
275 {
276     uint32_t msecs = (usec + 999) / 1000;
277     TickType_t xDelay;
278
279     /* Take the ceiling */
280     xDelay = (msecs + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS;
281
282     vTaskDelay(xDelay);
283
284     return (ClockP_OK);
285 }