1 /*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #ifndef _PICO_TYPES_H
8 #define _PICO_TYPES_H
9
10 #ifndef __ASSEMBLER__
11
12 #include "pico/assert.h"
13
14 #include <stdint.h>
15 #include <stdbool.h>
16 #include <stddef.h>
17
18 typedef unsigned int uint;
19
20 // PICO_CONFIG: PICO_OPAQUE_ABSOLUTE_TIME_T, Enable opaque type for absolute_time_t to help catch inadvertent confusing uint64_t delays with absolute times, default=0, advanced=true, group=pico_base
21 #ifndef PICO_OPAQUE_ABSOLUTE_TIME_T
22 #define PICO_OPAQUE_ABSOLUTE_TIME_T 0
23 #endif
24
25 /*! \typedef absolute_time_t
26 \brief An opaque 64 bit timestamp in microseconds
27
28 The type is used instead of a raw uint64_t to prevent accidentally passing relative times or times in the wrong
29 time units where an absolute time is required.
30
31 note: As of SDK 2.0.0 this type defaults to being a uin64_t (i.e. no protection); it is enabled
32 by setting PICO_OPAQUE_ABSOLUTE_TIME_T to 1
33
34 \see to_us_since_boot()
35 \see update_us_since_boot()
36 \ingroup timestamp
37 */
38 #if PICO_OPAQUE_ABSOLUTE_TIME_T
39 typedef struct {
40 uint64_t _private_us_since_boot;
41 } absolute_time_t;
42 #else
43 typedef uint64_t absolute_time_t;
44 #endif
45
46 /*! fn to_us_since_boot
47 * \brief convert an absolute_time_t into a number of microseconds since boot.
48 * \param t the absolute time to convert
49 * \return a number of microseconds since boot, equivalent to t
50 * \ingroup timestamp
51 */
to_us_since_boot(absolute_time_t t)52 static inline uint64_t to_us_since_boot(absolute_time_t t) {
53 #ifdef PICO_DEBUG_ABSOLUTE_TIME_T
54 return t._private_us_since_boot;
55 #else
56 return t;
57 #endif
58 }
59
60 /*! fn update_us_since_boot
61 * \brief update an absolute_time_t value to represent a given number of microseconds since boot
62 * \param t the absolute time value to update
63 * \param us_since_boot the number of microseconds since boot to represent. Note this should be representable
64 * as a signed 64 bit integer
65 * \ingroup timestamp
66 */
update_us_since_boot(absolute_time_t * t,uint64_t us_since_boot)67 static inline void update_us_since_boot(absolute_time_t *t, uint64_t us_since_boot) {
68 #ifdef PICO_DEBUG_ABSOLUTE_TIME_T
69 assert(us_since_boot <= INT64_MAX);
70 t->_private_us_since_boot = us_since_boot;
71 #else
72 *t = us_since_boot;
73 #endif
74 }
75
76 /*! fn from_us_since_boot
77 * \brief convert a number of microseconds since boot to an absolute_time_t
78 * \param us_since_boot number of microseconds since boot
79 * \return an absolute time equivalent to us_since_boot
80 * \ingroup timestamp
81 */
from_us_since_boot(uint64_t us_since_boot)82 static inline absolute_time_t from_us_since_boot(uint64_t us_since_boot) {
83 absolute_time_t t;
84 update_us_since_boot(&t, us_since_boot);
85 return t;
86 }
87
88 #ifdef NDEBUG
89 #define ABSOLUTE_TIME_INITIALIZED_VAR(name, value) name = value
90 #else
91 #define ABSOLUTE_TIME_INITIALIZED_VAR(name, value) name = {value}
92 #endif
93
94 // PICO_CONFIG: PICO_INCLUDE_RTC_DATETIME, Whether to include the datetime_t type used with the RP2040 RTC hardware, default=1 on RP2040, group=util_datetime
95 #ifndef PICO_INCLUDE_RTC_DATETIME
96 #define PICO_INCLUDE_RTC_DATETIME PICO_RP2040
97 #endif
98
99 #if PICO_INCLUDE_RTC_DATETIME
100 /** \struct datetime_t
101 * \ingroup util_datetime
102 * \brief Structure containing date and time information
103 *
104 * When setting an RTC alarm, set a field to -1 tells
105 * the RTC to not match on this field
106 */
107 typedef struct {
108 int16_t year; ///< 0..4095
109 int8_t month; ///< 1..12, 1 is January
110 int8_t day; ///< 1..28,29,30,31 depending on month
111 int8_t dotw; ///< 0..6, 0 is Sunday
112 int8_t hour; ///< 0..23
113 int8_t min; ///< 0..59
114 int8_t sec; ///< 0..59
115 } datetime_t;
116 #endif
117
118 #define bool_to_bit(x) ((uint)!!(x))
119
120 #endif
121 #endif
122