Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/pal/freertos/tickcounter.c

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #include <stdlib.h>
5 #include "azure_c_shared_utility/gballoc.h"
6 #include <stdint.h>
7 #include <time.h>
8
9 /* Codes_SRS_TICKCOUNTER_FREERTOS_30_001: [ The tickcounter_freertos adapter shall use the following data types as defined in tickcounter.h. */
10 /* Codes_SRS_TICKCOUNTER_FREERTOS_30_002: [ The tickcounter_freertos adapter shall implement the API calls defined in tickcounter.h */
11 #include "azure_c_shared_utility/tickcounter.h"
12 #include "azure_c_shared_utility/optimize_size.h"
13 #include "azure_c_shared_utility/xlogging.h"
14
15 #include "freertos/FreeRTOS.h"
16 #include "freertos/task.h"
17
18 typedef struct TICK_COUNTER_INSTANCE_TAG
19 {
20 uint32_t original_tick_count;
21 } TICK_COUNTER_INSTANCE;
22
23 TICK_COUNTER_HANDLE tickcounter_create(void)
24 {
25 /* Codes_SRS_TICKCOUNTER_FREERTOS_30_003: [ tickcounter_create shall allocate and initialize an internally-defined TICK_COUNTER_INSTANCE structure and return its pointer on success.] */
26 TICK_COUNTER_INSTANCE* result = (TICK_COUNTER_INSTANCE*)malloc(sizeof(TICK_COUNTER_INSTANCE));
27 if (result == NULL)
28 {
29 /* Codes_SRS_TICKCOUNTER_FREERTOS_30_004: [ If allocation of the internally-defined TICK_COUNTER_INSTANCE structure fails, tickcounter_create shall return NULL.] */
30 LogError("Failed creating tick counter");
31 }
32 else
33 {
34 // The FreeRTOS call xTaskGetTickCount has no failure path
35 // Store the initial tick count in order to meet these two requirements:
36 /* Codes_SRS_TICKCOUNTER_FREERTOS_30_009: [ tickcounter_get_current_ms shall set *current_ms to the number of milliseconds elapsed since the tickcounter_create call for the specified tick_counter and return 0 to indicate success in situations where the FreeRTOS call xTaskGetTickCount has not experienced overflow between the calls to tickcounter_create and tickcounter_get_current_ms. (In FreeRTOS this call has no failure case.) ] */
37 /* Codes_SRS_TICKCOUNTER_FREERTOS_30_010: [ tickcounter_get_current_ms shall set *current_ms to the number of milliseconds elapsed since the tickcounter_create call for the specified tick_counter and return 0 to indicate success in situations where the FreeRTOS call xTaskGetTickCount has experienced overflow between the calls to tickcounter_create and tickcounter_get_current_ms. (In FreeRTOS this call has no failure case.) ] */
38 result->original_tick_count = xTaskGetTickCount();
39 }
40 return result;
41 }
42
43 void tickcounter_destroy(TICK_COUNTER_HANDLE tick_counter)
44 {
45 /* Codes_SRS_TICKCOUNTER_FREERTOS_30_006: [ If the tick_counter parameter is NULL, tickcounter_destroy shall do nothing. ] */
46 if (tick_counter != NULL)
47 {
48 /* Codes_SRS_TICKCOUNTER_FREERTOS_30_005: [ tickcounter_destroy shall delete the internally-defined TICK_COUNTER_INSTANCE structure specified by the tick_counter parameter. (This call has no failure case.) ] */
49 free(tick_counter);
50 }
51 }
52
53 int tickcounter_get_current_ms(TICK_COUNTER_HANDLE tick_counter, tickcounter_ms_t * current_ms)
54 {
55 int result;
56
57 if (tick_counter == NULL || current_ms == NULL)
58 {
59 /* Codes_SRS_TICKCOUNTER_FREERTOS_30_007: [ If the tick_counter parameter is NULL, tickcounter_get_current_ms shall return a non-zero value to indicate error. ] */
60 /* Codes_SRS_TICKCOUNTER_FREERTOS_30_008: [ If the current_ms parameter is NULL, tickcounter_get_current_ms shall return a non-zero value to indicate error. ] */
61 LogError("Invalid Arguments.");
62 result = MU_FAILURE;
63 }
64 else
65 {
66 /* Codes_SRS_TICKCOUNTER_FREERTOS_30_009: [ tickcounter_get_current_ms shall set *current_ms to the number of milliseconds elapsed since the tickcounter_create call for the specified tick_counter and return 0 to indicate success (In FreeRTOS this call has no failure case.) ] */
67 /* Codes_SRS_TICKCOUNTER_FREERTOS_30_010: [ If the FreeRTOS call xTaskGetTickCount experiences a single overflow between the calls to tickcounter_create and tickcounter_get_current_ms, the tickcounter_get_current_ms call shall still return the correct interval. ] */
68 *current_ms = (tickcounter_ms_t)(
69 // Subtraction of two uint32_t's followed by a cast to uint32_t
70 // ensures that the result remains valid until the real difference exceeds 32 bits.
71 ((uint32_t)(xTaskGetTickCount() - tick_counter->original_tick_count))
72 // Now that overflow behavior is ensured it is safe to scale. CONFIG_FREERTOS_HZ is typically
73 // equal to 1000 or less, so overflow won't happen until the 49.7 day limit
74 // of this call's effective uint32_t return value.
75 * 1000.0 / CONFIG_FREERTOS_HZ
76 );
77 result = 0;
78 }
79
80 return result;
81 }