2 * Copyright (c) 2015-2016, Texas Instruments Incorporated
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 * ======== ClockP_freertos.c ========
37 #include <ti/drivers/dpl/ClockP.h>
45 static TickType_t ticksToWait = portMAX_DELAY;
47 void ClockP_callbackFxn(uintptr_t arg);
49 typedef struct ClockP_FreeRTOSObj {
56 * ======== ClockP_callbackFxn ========
58 void ClockP_callbackFxn(uintptr_t arg)
60 TimerHandle_t handle = (TimerHandle_t)arg;
61 ClockP_FreeRTOSObj *obj;
63 obj = (ClockP_FreeRTOSObj *)pvTimerGetTimerID(handle);
68 * ======== ClockP_create ========
70 ClockP_Handle ClockP_create(ClockP_Fxn clockFxn, ClockP_Params *params)
72 ClockP_Params defaultParams;
73 ClockP_FreeRTOSObj *pObj;
74 TimerHandle_t handle = NULL;
77 params = &defaultParams;
78 ClockP_Params_init(&defaultParams);
81 if ((pObj = pvPortMalloc(sizeof(ClockP_FreeRTOSObj))) == NULL) {
85 handle = xTimerCreate(params->name, 1, 0, (void *)pObj,
86 (TimerCallbackFunction_t)ClockP_callbackFxn);
95 pObj->arg = params->arg;
97 return ((ClockP_Handle)pObj);
101 * ======== ClockP_delete ========
103 ClockP_Status ClockP_delete(ClockP_Handle handle)
105 ClockP_FreeRTOSObj *pObj = (ClockP_FreeRTOSObj *)handle;
108 status = xTimerDelete((TimerHandle_t)pObj->timer, ticksToWait);
110 if (status != pdPASS) {
111 return (ClockP_FAILURE);
120 * ======== ClockP_getCpuFreq ========
122 void ClockP_getCpuFreq(ClockP_FreqHz *freq)
124 unsigned long configCpuFreq;
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:
132 * #define configCPU_CLOCK_HZ ( SystemFrequency ) // extern uint32_t
134 * #define configCPU_CLOCK_HZ ( ( unsigned long ) 8000000 )
137 configCpuFreq = (unsigned long)configCPU_CLOCK_HZ;
138 freq->lo = (uint32_t)configCpuFreq;
140 // freq->hi = (uint32_t)(configCpuFreq >> 32);
144 * ======== ClockP_getSystemTickPeriod ========
146 uint32_t ClockP_getSystemTickPeriod()
148 uint32_t tickPeriodUs;
151 * Tick period in microseconds. configTICK_RATE_HZ is defined in the
152 * application's FreeRTOSConfig.h, which is include by FreeRTOS.h
154 tickPeriodUs = 1000000 / configTICK_RATE_HZ;
156 return (tickPeriodUs);
160 * ======== ClockP_getSystemTicks ========
161 * TODO determine if we ever call this from an ISR
163 uint32_t ClockP_getSystemTicks()
165 return ((uint32_t)xTaskGetTickCount());
169 * ======== ClockP_Params_init ========
171 void ClockP_Params_init(ClockP_Params *params)
174 params->arg = (uintptr_t)0;
178 * ======== ClockP_start ========
180 ClockP_Status ClockP_start(ClockP_Handle handle, uint32_t timeout)
182 ClockP_FreeRTOSObj *pObj = (ClockP_FreeRTOSObj *)handle;
185 status = xTimerChangePeriod(pObj->timer, (TickType_t)timeout, ticksToWait);
187 if (status != pdPASS) {
188 return (ClockP_FAILURE);
190 status = xTimerStart(pObj->timer, ticksToWait);
192 if (status != pdPASS) {
193 return (ClockP_FAILURE);
200 * ======== ClockP_startFromISR ========
202 ClockP_Status ClockP_startFromISR(ClockP_Handle handle, uint32_t timeout)
204 ClockP_FreeRTOSObj *pObj = (ClockP_FreeRTOSObj *)handle;
205 BaseType_t xHigherPriorityTaskWoken;
208 status = xTimerChangePeriodFromISR(pObj->timer, (TickType_t)timeout,
209 &xHigherPriorityTaskWoken);
210 if (status != pdPASS) {
211 return (ClockP_FAILURE);
213 status = xTimerStartFromISR(pObj->timer, &xHigherPriorityTaskWoken);
215 if (status != pdPASS) {
216 return (ClockP_FAILURE);
223 * ======== ClockP_stop ========
225 ClockP_Status ClockP_stop(ClockP_Handle handle)
227 ClockP_FreeRTOSObj *pObj = (ClockP_FreeRTOSObj *)handle;
230 status = xTimerStop(pObj->timer, ticksToWait);
232 if (status != pdPASS) {
233 return (ClockP_FAILURE);
239 * ======== ClockP_stopFromISR ========
241 ClockP_Status ClockP_stopFromISR(ClockP_Handle handle)
243 ClockP_FreeRTOSObj *pObj = (ClockP_FreeRTOSObj *)handle;
244 BaseType_t xHigherPriorityTaskWoken;
247 status = xTimerStopFromISR(pObj->timer, &xHigherPriorityTaskWoken);
249 if (status != pdPASS) {
250 return (ClockP_FAILURE);
256 * ======== ClockP_sleep ========
258 ClockP_Status ClockP_sleep(uint32_t sec)
260 uint32_t msecs = sec * 1000;
263 /* Take the ceiling */
264 xDelay = (msecs + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS;
272 * ======== ClockP_usleep ========
274 ClockP_Status ClockP_usleep(uint32_t usec)
276 uint32_t msecs = (usec + 999) / 1000;
279 /* Take the ceiling */
280 xDelay = (msecs + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS;