Statistics
| Branch: | Tag: | Revision:

amiro-os / periphery-lld / periphAL.h @ 340f2bdf

History | View | Annotate | Download (20.536 KB)

1 e545e620 Thomas Schöpping
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3 84f0ce9e Thomas Schöpping
Copyright (C) 2016..2019  Thomas Schöpping et al.
4 e545e620 Thomas Schöpping

5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU General Public License for more details.
14

15
You should have received a copy of the GNU General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19 6ff06bbf Thomas Schöpping
#ifndef AMIROOS_PERIPHAL_H
20
#define AMIROOS_PERIPHAL_H
21 e545e620 Thomas Schöpping
22 3940ba8a Thomas Schöpping
#include <amiro-lld.h>
23
24 e545e620 Thomas Schöpping
/*============================================================================*/
25
/* VERSION                                                                    */
26
/*============================================================================*/
27
28
/**
29
 * @brief   The periphery abstraction layer interface major version.
30
 * @note    Changes of the major version imply incompatibilities.
31
 */
32
#define PERIPHAL_VERSION_MAJOR    1
33
34
/**
35
 * @brief   The periphery abstraction layer interface minor version.
36
 * @note    A higher minor version implies new functionalty, but all old interfaces are still available.
37
 */
38 3106e8cc Thomas Schöpping
#define PERIPHAL_VERSION_MINOR    1
39 e545e620 Thomas Schöpping
40
/*============================================================================*/
41
/* DEPENDENCIES                                                               */
42
/*============================================================================*/
43
44 1f94ac64 Thomas Schöpping
#include <aosconf.h>
45 e545e620 Thomas Schöpping
#include <hal.h>
46
47
/*============================================================================*/
48 1f94ac64 Thomas Schöpping
/* DEBUG                                                                      */
49
/*============================================================================*/
50
51
#if (AMIROOS_CFG_DBG == true) || defined(__DOXYGEN__)
52
53 7de0cc90 Thomas Schöpping
#if defined(__cplusplus)
54 1f94ac64 Thomas Schöpping
extern "C" {
55 7de0cc90 Thomas Schöpping
#endif /* defined(__cplusplus) */
56 1f94ac64 Thomas Schöpping
  void _apalDbgAssertMsg(const bool c, const char* fmt, ...);
57
  void apalDbgPrintf(const char* fmt, ...);
58 7de0cc90 Thomas Schöpping
#if defined(__cplusplus)
59 1f94ac64 Thomas Schöpping
}
60 7de0cc90 Thomas Schöpping
#endif /* defined(__cplusplus) */
61 1f94ac64 Thomas Schöpping
62
/**
63
 * @brief Assert function to check a given condition.
64
 *
65
 * @param[in] c     The condition to check.
66
 */
67
#define apalDbgAssert(c)                                                      \
68
  _apalDbgAssertMsg(c, "%s(%u): apalDbgAssert failed", __FILE__, __LINE__);
69
70 7de0cc90 Thomas Schöpping
#else /* (AMIROOS_CFG_DBG != true) */
71 1f94ac64 Thomas Schöpping
72
#define apalDbgAssert(constition)
73
#define apalDbgAssertMsg(condition, fmt, ...)
74
#define apalDbgPrintf(fmt, ...)
75
76 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_DBG == true) */
77 1f94ac64 Thomas Schöpping
78
/*============================================================================*/
79 e545e620 Thomas Schöpping
/* GENERAL                                                                    */
80
/*============================================================================*/
81
82
/**
83
 * @brief Delay execution by a specific number of microseconds.
84
 *
85
 * @param[in]   us    Time to sleep until execution continues in microseconds.
86
 */
87
static inline void usleep(apalTime_t us)
88
{
89
  // check if the specified time can be represented by the system
90 1f94ac64 Thomas Schöpping
  apalDbgAssert(us <= chTimeI2US(TIME_INFINITE));
91 e545e620 Thomas Schöpping
92 3940ba8a Thomas Schöpping
  const sysinterval_t interval = chTimeUS2I(us);
93 e545e620 Thomas Schöpping
  // TIME_IMMEDIATE makes no sense and would even cause system halt
94 3940ba8a Thomas Schöpping
  if (interval != TIME_IMMEDIATE) {
95
    chThdSleep(interval);
96 e545e620 Thomas Schöpping
  }
97
  return;
98
}
99
100
/*============================================================================*/
101
/* GPIO                                                                       */
102
/*============================================================================*/
103
104 7de0cc90 Thomas Schöpping
#if (HAL_USE_PAL == TRUE) || defined (__DOXYGEN__)
105 e545e620 Thomas Schöpping
106
/**
107
 * @brief GPIO driver type.
108
 */
109
struct apalGpio_t {
110 3106e8cc Thomas Schöpping
  ioline_t line;
111 e545e620 Thomas Schöpping
} PACKED_VAR;
112
113
/**
114
 * @brief Read the current value of a GPIO pin.
115
 *
116
 * @param[in]   gpio  GPIO to read.
117
 * @param[out]  val   Current value of the GPIO.
118
 *
119
 * @return The status indicates whether the function call was successful.
120
 */
121
static inline apalExitStatus_t apalGpioRead(apalGpio_t* gpio, apalGpioState_t* const val)
122
{
123 1f94ac64 Thomas Schöpping
  apalDbgAssert(gpio != NULL);
124
  apalDbgAssert(val != NULL);
125 e545e620 Thomas Schöpping
126 3106e8cc Thomas Schöpping
  *val = (palReadLine(gpio->line) == PAL_HIGH) ? APAL_GPIO_HIGH : APAL_GPIO_LOW;
127 e545e620 Thomas Schöpping
  return APAL_STATUS_OK;
128
}
129
130
/**
131
 * @brief Set the value of a GPIO pin.
132
 *
133
 * @param[in] gpio  GPIO to write.
134
 * @param[in] val   Value to set for the GPIO.
135
 *
136
 * @return The status indicates whether the function call was successful.
137
 */
138
static inline apalExitStatus_t apalGpioWrite(apalGpio_t* gpio, const apalGpioState_t val)
139
{
140 1f94ac64 Thomas Schöpping
  apalDbgAssert(gpio != NULL);
141 e545e620 Thomas Schöpping
142 3106e8cc Thomas Schöpping
  // palWriteLine() is not guaranteed to be atomic, thus the scheduler is locked.
143 e545e620 Thomas Schöpping
  syssts_t sysstatus = chSysGetStatusAndLockX();
144 3106e8cc Thomas Schöpping
  palWriteLine(gpio->line, (val == APAL_GPIO_HIGH) ? PAL_HIGH : PAL_LOW);
145 e545e620 Thomas Schöpping
  chSysRestoreStatusX(sysstatus);
146
  return APAL_STATUS_OK;
147
}
148
149
/**
150
 * @brief Toggle the output of a GPIO.
151
 *
152
 * @param[in] gpio  GPIO to toggle.
153
 *
154
 * @return The status indicates whether the function call was successful.
155
 */
156
static inline apalExitStatus_t apalGpioToggle(apalGpio_t* gpio)
157
{
158 1f94ac64 Thomas Schöpping
  apalDbgAssert(gpio != NULL);
159 e545e620 Thomas Schöpping
160 3106e8cc Thomas Schöpping
  // palWriteLine() is not guaranteed to be atomic, thus the scheduler is locked.
161 e545e620 Thomas Schöpping
  syssts_t sysstatus = chSysGetStatusAndLockX();
162 3106e8cc Thomas Schöpping
  palWriteLine(gpio->line, (palReadLine(gpio->line) == PAL_HIGH) ? PAL_LOW : PAL_HIGH);
163 e545e620 Thomas Schöpping
  chSysRestoreStatusX(sysstatus);
164
  return APAL_STATUS_OK;
165
}
166
167
/**
168
 * @brief Get the current on/off state of a control GPIO.
169
 *
170
 * @param[in]   gpio  Control GPIO to read.
171
 * @param[out]  val   Current activation status of the control GPIO.
172
 *
173
 * @return The status indicates whether the function call was successful.
174
 */
175
static inline apalExitStatus_t apalControlGpioGet(const apalControlGpio_t* const cgpio, apalControlGpioState_t* const val)
176
{
177 1f94ac64 Thomas Schöpping
  apalDbgAssert(cgpio != NULL);
178
  apalDbgAssert(cgpio->gpio != NULL);
179
  apalDbgAssert(val != NULL);
180 e545e620 Thomas Schöpping
181 3106e8cc Thomas Schöpping
  *val = ((palReadLine(cgpio->gpio->line) == PAL_HIGH) ^ (cgpio->meta.active == APAL_GPIO_ACTIVE_HIGH)) ? APAL_GPIO_OFF : APAL_GPIO_ON;
182 e545e620 Thomas Schöpping
  return APAL_STATUS_OK;
183
}
184
185
/**
186
 * @brief Turn a control GPIO 'on' or 'off' respectively.
187
 *
188
 * @param[in] gpio  Control GPIO to set.
189
 * @param[in] val   Activation value to set for the control GPIO.
190
 *
191
 * @return The status indicates whether the function call was successful.
192
 */
193
static inline apalExitStatus_t apalControlGpioSet(const apalControlGpio_t* const cgpio, const apalControlGpioState_t val)
194
{
195 1f94ac64 Thomas Schöpping
  apalDbgAssert(cgpio != NULL);
196
  apalDbgAssert(cgpio->gpio != NULL);
197
  apalDbgAssert(cgpio->meta.direction == APAL_GPIO_DIRECTION_OUTPUT || cgpio->meta.direction == APAL_GPIO_DIRECTION_BIDIRECTIONAL);
198 e545e620 Thomas Schöpping
199 3106e8cc Thomas Schöpping
  // palWriteLine() is not guaranteed to be atomic, thus the scheduler is locked.
200 e545e620 Thomas Schöpping
  syssts_t sysstatus = chSysGetStatusAndLockX();
201 3106e8cc Thomas Schöpping
  palWriteLine(cgpio->gpio->line, ((cgpio->meta.active == APAL_GPIO_ACTIVE_HIGH) ^ (val == APAL_GPIO_ON)) ? PAL_LOW : PAL_HIGH);
202 e545e620 Thomas Schöpping
  chSysRestoreStatusX(sysstatus);
203
  return APAL_STATUS_OK;
204
}
205
206
/**
207 1e5f7648 Thomas Schöpping
 * @brief   Converts an apalGpioEdge_t to an ChibiOS PAL edge.
208 e545e620 Thomas Schöpping
 */
209 1e5f7648 Thomas Schöpping
#define APAL2CH_EDGE(edge)                                            \
210
  ((edge == APAL_GPIO_EDGE_RISING) ? PAL_EVENT_MODE_RISING_EDGE :     \
211
    (edge == APAL_GPIO_EDGE_FALLING) ? PAL_EVENT_MODE_FALLING_EDGE :  \
212 542939ea Thomas Schöpping
     (edge == APAL_GPIO_EDGE_BOTH) ? PAL_EVENT_MODE_BOTH_EDGES :      \
213
      PAL_EVENT_MODE_DISABLED)
214