Statistics
| Branch: | Tag: | Revision:

amiro-os / modules / NUCLEO-L476RG / module.h @ 33de2690

History | View | Annotate | Download (9.935 KB)

1 27d0378b Simon Welzel
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3 96621a83 Thomas Schöpping
Copyright (C) 2016..2020  Thomas Schöpping et al.
4 27d0378b Simon Welzel

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
/**
20
 * @file
21 126ace3c Thomas Schöpping
 * @brief   Structures and constant for the NUCLEO-L476RG module.
22 27d0378b Simon Welzel
 *
23 126ace3c Thomas Schöpping
 * @addtogroup NUCLEO-L476RG_module
24 27d0378b Simon Welzel
 * @{
25
 */
26
27 6ff06bbf Thomas Schöpping
#ifndef AMIROOS_MODULE_H
28
#define AMIROOS_MODULE_H
29 27d0378b Simon Welzel
30 8543d0d9 Thomas Schöpping
#include <amiroos.h>
31
32 33de2690 Thomas Schöpping
#if (BOARD_MPU6050_CONNECTED == true)
33
#include <math.h>
34
#endif /* BOARD_MPU6050_CONNECTED == true */
35
36 27d0378b Simon Welzel
/*===========================================================================*/
37
/**
38
 * @name Module specific functions
39
 * @{
40
 */
41
/*===========================================================================*/
42
43
/** @} */
44
45
/*===========================================================================*/
46
/**
47
 * @name ChibiOS/HAL configuration
48
 * @{
49
 */
50
/*===========================================================================*/
51
52
/**
53 8543d0d9 Thomas Schöpping
 * @brief   Serial driver of the programmer interface.
54 27d0378b Simon Welzel
 */
55 8543d0d9 Thomas Schöpping
#define MODULE_HAL_PROGIF                       SD2
56 27d0378b Simon Welzel
57
/**
58 8543d0d9 Thomas Schöpping
 * @brief   Configuration for the programmer serial interface driver.
59 27d0378b Simon Welzel
 */
60 8543d0d9 Thomas Schöpping
extern SerialConfig moduleHalProgIfConfig;
61 27d0378b Simon Welzel
62
/**
63
 * @brief   Real-Time Clock driver.
64
 */
65
#define MODULE_HAL_RTC                          RTCD1
66
67 4c72a54c Thomas Schöpping
#if (BOARD_MPU6050_CONNECTED == true) || defined(__DOXYGEN__)
68 126ace3c Thomas Schöpping
69
/**
70
 * @brief   I2C driver to access multiplexer, proximity sensors 5 to 8, power monitors for VSYS4.2, VIO 5.0 and VDD, EEPROM, touch sensor, and fuel gauge (front battery).
71
 */
72
#define MODULE_HAL_I2C3                         I2CD3
73
74
/**
75
 * @brief   Configuration for the I2C driver #3.
76
 */
77
extern I2CConfig moduleHalI2c3Config;
78
79 4c72a54c Thomas Schöpping
#endif /* (BOARD_MPU6050_CONNECTED == true) */
80 126ace3c Thomas Schöpping
81 27d0378b Simon Welzel
/** @} */
82
83
/*===========================================================================*/
84
/**
85
 * @name GPIO definitions
86
 * @{
87
 */
88
/*===========================================================================*/
89
90
/**
91
 * @brief   LED output signal GPIO.
92
 */
93
extern ROMCONST apalControlGpio_t moduleGpioLed;
94
95
/**
96 8543d0d9 Thomas Schöpping
 * @brief   User button input signal.
97 27d0378b Simon Welzel
 */
98 8543d0d9 Thomas Schöpping
extern ROMCONST apalControlGpio_t moduleGpioUserButton;
99 27d0378b Simon Welzel
100 8543d0d9 Thomas Schöpping
/** @} */
101 27d0378b Simon Welzel
102 8543d0d9 Thomas Schöpping
/*===========================================================================*/
103 27d0378b Simon Welzel
/**
104 8543d0d9 Thomas Schöpping
 * @name AMiRo-OS core configurations
105
 * @{
106 27d0378b Simon Welzel
 */
107 8543d0d9 Thomas Schöpping
/*===========================================================================*/
108 27d0378b Simon Welzel
109
/**
110 8543d0d9 Thomas Schöpping
 * @brief   Event flag to be set on a USER_BUTTON interrupt.
111 27d0378b Simon Welzel
 */
112 cda14729 Thomas Schöpping
#define MODULE_OS_GPIOEVENTFLAG_USERBUTTON      AOS_GPIOEVENT_FLAG(PAL_PAD(LINE_BUTTON))
113 27d0378b Simon Welzel
114 cda14729 Thomas Schöpping
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
115 27d0378b Simon Welzel
/**
116 8543d0d9 Thomas Schöpping
 * @brief   Shell prompt text.
117 27d0378b Simon Welzel
 */
118 8543d0d9 Thomas Schöpping
extern ROMCONST char* moduleShellPrompt;
119 cda14729 Thomas Schöpping
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
120 27d0378b Simon Welzel
121
/**
122 8543d0d9 Thomas Schöpping
 * @brief   Interrupt initialization macro.
123 27d0378b Simon Welzel
 */
124 8543d0d9 Thomas Schöpping
#define MODULE_INIT_INTERRUPTS() {                                            \
125
  /* user button */                                                           \
126 cda14729 Thomas Schöpping
  palSetLineCallback(moduleGpioUserButton.gpio->line, aosSysGetStdGpioCallback(), &moduleGpioUserButton.gpio->line);  \
127 56dc4779 Thomas Schöpping
  palEnableLineEvent(moduleGpioUserButton.gpio->line, APAL2CH_EDGE(moduleGpioUserButton.meta.edge));                  \
128 8543d0d9 Thomas Schöpping
}
129 27d0378b Simon Welzel
130
/**
131 4c72a54c Thomas Schöpping
 * @brief   Test initialization hook.
132 27d0378b Simon Welzel
 */
133 8543d0d9 Thomas Schöpping
#define MODULE_INIT_TESTS() {                                                 \
134 cda14729 Thomas Schöpping
  /* add test commands to shell */                                            \
135 4c72a54c Thomas Schöpping
  aosShellAddCommand(&aos.shell, &moduleTestLedShellCmd);                     \
136
  aosShellAddCommand(&aos.shell, &moduleTestButtonShellCmd);                  \
137 126ace3c Thomas Schöpping
  MODULE_INIT_TEST_MPU6050();                                                 \
138 4c72a54c Thomas Schöpping
  aosShellAddCommand(&aos.shell, &moduleTestAllShellCmd);                     \
139 8543d0d9 Thomas Schöpping
}
140 4c72a54c Thomas Schöpping
#if (BOARD_MPU6050_CONNECTED == true)
141 126ace3c Thomas Schöpping
  #define MODULE_INIT_TEST_MPU6050() {                                        \
142 4c72a54c Thomas Schöpping
    aosShellAddCommand(&aos.shell, &moduleTestMpu6050ShellCmd);               \
143 126ace3c Thomas Schöpping
  }
144 4c72a54c Thomas Schöpping
#else /* (BOARD_MPU6050_CONNECTED == true) */
145 126ace3c Thomas Schöpping
  #define MODULE_INIT_TEST_MPU6050() {}
146 4c72a54c Thomas Schöpping
#endif /* (BOARD_MPU6050_CONNECTED == true) */
147 27d0378b Simon Welzel
148
/**
149 8543d0d9 Thomas Schöpping
 * @brief   Periphery communication interfaces initialization hook.
150 27d0378b Simon Welzel
 */
151 4c72a54c Thomas Schöpping
#define MODULE_INIT_PERIPHERY_IF() {                                          \
152 8543d0d9 Thomas Schöpping
  /* serial driver */                                                         \
153
  sdStart(&MODULE_HAL_PROGIF, &moduleHalProgIfConfig);                        \
154 126ace3c Thomas Schöpping
  /* MPU6050 demo */                                                          \
155 4c72a54c Thomas Schöpping
  MODULE_INIT_PERIPHERY_IF_MPU6050();                                         \
156 8543d0d9 Thomas Schöpping
}
157 4c72a54c Thomas Schöpping
#if (BOARD_MPU6050_CONNECTED == true)
158
  #define MODULE_INIT_PERIPHERY_IF_MPU6050() {                                \
159 126ace3c Thomas Schöpping
    /* maximum I2C frequency is 1MHz for this MCU */                          \
160
    uint32_t i2c3_freq = 1000000;                                             \
161
    /* find minimum amon all devices connected to this bus */                 \
162
    i2c3_freq = (MPU6050_LLD_I2C_MAXFREQUENCY < i2c3_freq) ? MPU6050_LLD_I2C_MAXFREQUENCY : i2c3_freq;  \
163
    /* calculate PRESC (prescaler):                                           \
164
     *   target is 1/(I2CXCLK * (PRESC + 1)) = 125ns                          \
165
     */                                                                       \
166
    moduleHalI2c3Config.timingr = ((uint8_t)((0.000000125f * STM32_I2C3CLK) - 1)) << I2C_TIMINGR_PRESC_Pos; \
167
    /* SCL shall be low half of the time. */                                  \
168
    moduleHalI2c3Config.timingr |= ((uint8_t)((1.f / i2c3_freq / 2 / 0.000000125f) - 1)) << I2C_TIMINGR_SCLL_Pos; \
169
    /* SCL shall be high half the time of low or slightly longer. */          \
170
    moduleHalI2c3Config.timingr |= (uint8_t)(ceilf(((moduleHalI2c3Config.timingr & I2C_TIMINGR_SCLL_Msk) >> I2C_TIMINGR_SCLL_Pos) / 2.f)) << I2C_TIMINGR_SCLH_Pos;  \
171
    /* SDA shall be delayed 1/10 of SCL low, or shorter */                    \
172
    moduleHalI2c3Config.timingr |= (uint8_t)(((moduleHalI2c3Config.timingr & I2C_TIMINGR_SCLL_Msk) >> I2C_TIMINGR_SCLL_Pos) * 0.1f) << I2C_TIMINGR_SDADEL_Pos;  \
173
    /* SCL shall be delyed twice as long as SDA, but longer than 0. */        \
174
    moduleHalI2c3Config.timingr |= ((((moduleHalI2c3Config.timingr & I2C_TIMINGR_SDADEL_Msk) >> I2C_TIMINGR_SDADEL_Pos) * 2) + 1) << I2C_TIMINGR_SCLDEL_Pos;  \
175
    /* now we can start the I2C driver */                                     \
176 97017dbb Thomas Schöpping
    chSysLock();                                                              \
177 4c72a54c Thomas Schöpping
    palSetLineMode(LINE_ARD_A4, PAL_MODE_ALTERNATE(4));                       \
178
    palSetLineMode(LINE_ARD_A5, PAL_MODE_ALTERNATE(4));                       \
179 97017dbb Thomas Schöpping
    chSysUnlock();                                                            \
180 126ace3c Thomas Schöpping
    i2cStart(&MODULE_HAL_I2C3, &moduleHalI2c3Config);                         \
181
  }
182 4c72a54c Thomas Schöpping
#else /* (BOARD_MPU6050_CONNECTED == true) */
183
  #define MODULE_INIT_PERIPHERY_IF_MPU6050() {}
184
#endif /* (BOARD_MPU6050_CONNECTED == true) */
185 27d0378b Simon Welzel
186
/**
187 8543d0d9 Thomas Schöpping
 * @brief   Periphery communication interface deinitialization hook.
188 27d0378b Simon Welzel
 */
189 4c72a54c Thomas Schöpping
#define MODULE_SHUTDOWN_PERIPHERY_IF() {                                      \
190 cda14729 Thomas Schöpping
  /* don't stop the serial driver so messages can still be printed */         \
191 8543d0d9 Thomas Schöpping
}
192 27d0378b Simon Welzel
193
/**
194 8543d0d9 Thomas Schöpping
 * @brief   HOOK to toggle the LEDs when the user button is pressed.
195 27d0378b Simon Welzel
 */
196 cda14729 Thomas Schöpping
#define MODULE_MAIN_LOOP_GPIOEVENT(eventflags) {                              \
197
  if (eventflags & MODULE_OS_GPIOEVENTFLAG_USERBUTTON) {                      \
198 4c72a54c Thomas Schöpping
    button_lld_state_t buttonstate;                                           \
199
    button_lld_get(&moduleLldUserButton, &buttonstate);                       \
200
    led_lld_set(&moduleLldLed, (buttonstate == BUTTON_LLD_STATE_PRESSED) ? LED_LLD_STATE_ON : LED_LLD_STATE_OFF); \
201 8543d0d9 Thomas Schöpping
  }                                                                           \
202
}
203 27d0378b Simon Welzel
204
/** @} */
205
206
/*===========================================================================*/
207
/**
208 8543d0d9 Thomas Schöpping
 * @name Startup Shutdown Synchronization Protocol (SSSP)
209 27d0378b Simon Welzel
 * @{
210
 */
211
/*===========================================================================*/
212
213 cda14729 Thomas Schöpping
#if (AMIROOS_CFG_SSSP_ENABLE == true) || defined(__DOXYGEN__)
214 c53ef0b1 Thomas Schöpping
  #error "SSSP is not supported on this module."
215 cda14729 Thomas Schöpping
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
216
217 8543d0d9 Thomas Schöpping
/** @} */
218 27d0378b Simon Welzel
219 8543d0d9 Thomas Schöpping
/*===========================================================================*/
220 27d0378b Simon Welzel
/**
221 8543d0d9 Thomas Schöpping
 * @name Low-level drivers
222
 * @{
223 27d0378b Simon Welzel
 */
224 8543d0d9 Thomas Schöpping
/*===========================================================================*/
225 4c72a54c Thomas Schöpping
#include <alld_LED.h>
226
#include <alld_button.h>
227 27d0378b Simon Welzel
228 4c72a54c Thomas Schöpping
/**
229
 * @brief   LED driver.
230
 */
231
extern LEDDriver moduleLldLed;
232
233
/**
234
 * @brief   Button driver.
235
 */
236
extern ButtonDriver moduleLldUserButton;
237
238
#if (BOARD_MPU6050_CONNECTED == true) || defined(__DOXYGEN__)
239 126ace3c Thomas Schöpping
240
#include <alld_MPU6050.h>
241
242
/**
243
 * @brief   Accelerometer (MPU6050) driver.
244
 */
245
extern MPU6050Driver moduleLldMpu6050;
246
247 4c72a54c Thomas Schöpping
#endif /* (BOARD_MPU6050_CONNECTED == true) */
248 126ace3c Thomas Schöpping
249 8543d0d9 Thomas Schöpping
/** @} */
250 1678f270 Simon Welzel
251 8543d0d9 Thomas Schöpping
/*===========================================================================*/
252 1678f270 Simon Welzel
/**
253 4c72a54c Thomas Schöpping
 * @name Tests
254 8543d0d9 Thomas Schöpping
 * @{
255 1678f270 Simon Welzel
 */
256 8543d0d9 Thomas Schöpping
/*===========================================================================*/
257
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
258 1678f270 Simon Welzel
259 4c72a54c Thomas Schöpping
/**
260
 * @brief   LED test command.
261
 */
262
extern aos_shellcommand_t moduleTestLedShellCmd;
263
264
/**
265
 * @brief   User button test command.
266
 */
267
extern aos_shellcommand_t moduleTestButtonShellCmd;
268 126ace3c Thomas Schöpping
269 4c72a54c Thomas Schöpping
#if (BOARD_MPU6050_CONNECTED == true) || defined(__DOXYGEN__)
270 126ace3c Thomas Schöpping
271
/**
272 4c72a54c Thomas Schöpping
 * @brief   MPU6050 (Accelerometer & Gyroscope) test command.
273 126ace3c Thomas Schöpping
 */
274 4c72a54c Thomas Schöpping
extern aos_shellcommand_t moduleTestMpu6050ShellCmd;
275 126ace3c Thomas Schöpping
276 4c72a54c Thomas Schöpping
#endif /* (BOARD_MPU6050_CONNECTED == true) */
277
278
/**
279
 * @brief   Entire module test command.
280
 */
281
extern aos_shellcommand_t moduleTestAllShellCmd;
282 126ace3c Thomas Schöpping
283 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
284 8543d0d9 Thomas Schöpping
285
/** @} */
286
287 6ff06bbf Thomas Schöpping
#endif /* AMIROOS_MODULE_H */
288 27d0378b Simon Welzel
289
/** @} */