#ifndef _UTILS_H_ #define _UTILS_H_ #include "FreeRTOS.h" #include "portmacro.h" #include "task.h" /* Small functions in header file for efficient inlining. */ /* Rounding integer division for positive numbers * a/b + 0.5 = (a + b/2) / b */ static inline unsigned int round_div(unsigned int a, unsigned int b) { return (a + b/2) / b; } /* Delay with millisecond parameter. * Always delay atleast ms milliseconds. * FreeRTOS vTaskDelay delays atleast ticks-1 ticks, because the * delay might begin midway through a tick period. Therefore add +1. */ static inline void vTaskDelayMS(int ms) { vTaskDelay(ms / portTICK_RATE_MS + 1); } /* Get number of elapsed milliseconds compared to a previous tick value. * Use like this: * portTickType ticks = xTaskGetTickCount(); * while (elapsedMS(ticks) < 100) do_something_until_timeout(); */ static inline portTickType elapsedMS(portTickType ticks) { return (xTaskGetTickCount() - ticks) / portTICK_RATE_MS; } #endif