Statistics
| Branch: | Tag: | Revision:

amiro-os / modules / LightRing_1-0 / module.c @ 0c28913b

History | View | Annotate | Download (11.485 KB)

1 e545e620 Thomas Schöpping
/*
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 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 27286ba5 Thomas Schöpping
/**
20
 * @file
21
 * @brief   Structures and constant for the LightRing v1.0 module.
22
 *
23
 * @addtogroup lightring_module
24
 * @{
25
 */
26
27 c53ef0b1 Thomas Schöpping
#include <amiroos.h>
28
29
#include <string.h>
30 e545e620 Thomas Schöpping
31
/*===========================================================================*/
32
/**
33
 * @name Module specific functions
34
 * @{
35
 */
36
/*===========================================================================*/
37
38
/** @} */
39
40
/*===========================================================================*/
41
/**
42
 * @name ChibiOS/HAL configuration
43
 * @{
44
 */
45
/*===========================================================================*/
46
47
CANConfig moduleHalCanConfig = {
48
  /* mcr  */ CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
49
  /* btr  */ CAN_BTR_SJW(1) | CAN_BTR_TS2(2) | CAN_BTR_TS1(13) | CAN_BTR_BRP(1),
50
};
51
52
I2CConfig moduleHalI2cEepromConfig = {
53
  /* I²C mode   */ OPMODE_I2C,
54
  /* frequency  */ 400000, // TODO: replace with some macro (-> ChibiOS/HAL)
55
  /* duty cycle */ FAST_DUTY_CYCLE_2,
56
};
57
58
SerialConfig moduleHalProgIfConfig = {
59
  /* bit rate */ 115200,
60
  /* CR1      */ 0,
61
  /* CR1      */ 0,
62
  /* CR1      */ 0,
63
};
64
65
SPIConfig moduleHalSpiLightConfig = {
66 0128be0f Marc Rothmann
  /* circular buffer mode        */ false,
67 e545e620 Thomas Schöpping
  /* callback function pointer   */ NULL,
68 3106e8cc Thomas Schöpping
  /* chip select line port       */ PAL_PORT(LINE_LIGHT_XLAT),
69
  /* chip select line pad number */ PAL_PAD(LINE_LIGHT_XLAT),
70 e545e620 Thomas Schöpping
  /* CR1                         */ SPI_CR1_BR_0 | SPI_CR1_BR_1,
71
  /* CR2                         */ SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN,
72 22be62dc Thomas Schöpping
};
73
74 02c29a8f Thomas Schöpping
SPIConfig moduleHalSpiWlConfig = {
75 22be62dc Thomas Schöpping
  /* circular buffer mode        */ false,
76
  /* callback function pointer   */ NULL,
77 3106e8cc Thomas Schöpping
  /* chip select line port       */ PAL_PORT(LINE_WL_SS_N),
78
  /* chip select line pad number */ PAL_PAD(LINE_WL_SS_N),
79 02c29a8f Thomas Schöpping
  /* CR1                         */ SPI_CR1_BR_0,
80 22be62dc Thomas Schöpping
  /* CR2                         */ SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN,
81 e545e620 Thomas Schöpping
};
82
83 4c72a54c Thomas Schöpping
/** @} */
84
85 e545e620 Thomas Schöpping
/*===========================================================================*/
86
/**
87
 * @name GPIO definitions
88
 * @{
89
 */
90
/*===========================================================================*/
91
92 1e5f7648 Thomas Schöpping
/**
93
 * @brief   LIGHT_BANK output signal GPIO.
94
 */
95
static apalGpio_t _gpioLightBlank = {
96 3106e8cc Thomas Schöpping
  /* line */ LINE_LIGHT_BLANK,
97 e545e620 Thomas Schöpping
};
98 acc97cbf Thomas Schöpping
ROMCONST apalControlGpio_t moduleGpioLightBlank = {
99 1e5f7648 Thomas Schöpping
  /* GPIO */ &_gpioLightBlank,
100
  /* meta */ {
101
    /* direction      */ APAL_GPIO_DIRECTION_OUTPUT,
102
    /* active state   */ TLC5947_LLD_BLANK_ACTIVE_STATE,
103
    /* interrupt edge */ APAL_GPIO_EDGE_NONE,
104
  },
105
};
106 e545e620 Thomas Schöpping
107 1e5f7648 Thomas Schöpping
/**
108
 * @brief   LASER_EN output signal GPIO.
109
 */
110
static apalGpio_t _gpioLaserEn = {
111 3106e8cc Thomas Schöpping
  /* line */ LINE_LASER_EN,
112 e545e620 Thomas Schöpping
};
113 acc97cbf Thomas Schöpping
ROMCONST apalControlGpio_t moduleGpioLaserEn = {
114 1e5f7648 Thomas Schöpping
  /* GPIO */ &_gpioLaserEn,
115
  /* meta */ {
116
    /* direction      */ APAL_GPIO_DIRECTION_OUTPUT,
117 ddf34c3d Thomas Schöpping
    /* active state   */ TPS20xxB_LLD_ENABLE_ACTIVE_STATE,
118 1e5f7648 Thomas Schöpping
    /* interrupt edge */ APAL_GPIO_EDGE_NONE,
119
  },
120
};
121 e545e620 Thomas Schöpping
122 1e5f7648 Thomas Schöpping
/**
123
 * @brief   LASER_OC input signal GPIO.
124
 */
125
static apalGpio_t _gpioLaserOc = {
126 3106e8cc Thomas Schöpping
  /* line */ LINE_LASER_OC_N,
127 e545e620 Thomas Schöpping
};
128 acc97cbf Thomas Schöpping
ROMCONST apalControlGpio_t moduleGpioLaserOc = {
129 1e5f7648 Thomas Schöpping
  /* GPIO */ &_gpioLaserOc,
130
  /* meta */ {
131
    /* direction      */ APAL_GPIO_DIRECTION_INPUT,
132 ddf34c3d Thomas Schöpping
    /* active state   */ TPS20xxB_LLD_OVERCURRENT_ACTIVE_STATE,
133 1e5f7648 Thomas Schöpping
    /* interrupt edge */ APAL_GPIO_EDGE_BOTH,
134
  },
135
};
136 e545e620 Thomas Schöpping
137 1e5f7648 Thomas Schöpping
/**
138
 * @brief   SYS_UART_DN bidirectional signal GPIO.
139
 */
140
static apalGpio_t _gpioSysUartDn = {
141 3106e8cc Thomas Schöpping
  /* line */ LINE_SYS_UART_DN,
142 e545e620 Thomas Schöpping
};
143 acc97cbf Thomas Schöpping
ROMCONST apalControlGpio_t moduleGpioSysUartDn = {
144 1e5f7648 Thomas Schöpping
  /* GPIO */ &_gpioSysUartDn,
145
  /* meta */ {
146
    /* direction      */ APAL_GPIO_DIRECTION_BIDIRECTIONAL,
147
    /* active state   */ APAL_GPIO_ACTIVE_LOW,
148
    /* interrupt edge */ APAL_GPIO_EDGE_BOTH,
149
  },
150
};
151 e545e620 Thomas Schöpping
152 1e5f7648 Thomas Schöpping
/**
153
 * @brief   WL_GDO2 input signal GPIO.
154
 */
155
static apalGpio_t _gpioWlGdo2 = {
156 3106e8cc Thomas Schöpping
  /* line */ LINE_WL_GDO2,
157 e545e620 Thomas Schöpping
};
158 acc97cbf Thomas Schöpping
ROMCONST apalControlGpio_t moduleGpioWlGdo2 = {
159 1e5f7648 Thomas Schöpping
  /* GPIO */ &_gpioWlGdo2,
160
  /* meta */ {
161
    /* direction      */ APAL_GPIO_DIRECTION_INPUT,
162
    /* active state   */ APAL_GPIO_ACTIVE_HIGH,
163
    /* interrupt edge */ APAL_GPIO_EDGE_BOTH,
164
  },
165
};
166 e545e620 Thomas Schöpping
167 1e5f7648 Thomas Schöpping
/**
168
 * @brief   WL_GDO0 input signal GPIO.
169
 */
170
static apalGpio_t _gpioWlGdo0= {
171 3106e8cc Thomas Schöpping
  /* line */ LINE_WL_GDO0,
172 e545e620 Thomas Schöpping
};
173 acc97cbf Thomas Schöpping
ROMCONST apalControlGpio_t moduleGpioWlGdo0 = {
174 1e5f7648 Thomas Schöpping
  /* GPIO */ &_gpioWlGdo0,
175
  /* meta */ {
176
    /* direction      */ APAL_GPIO_DIRECTION_INPUT,
177
    /* active state   */ APAL_GPIO_ACTIVE_HIGH,
178
    /* interrupt edge */ APAL_GPIO_EDGE_BOTH,
179
  },
180
};
181 e545e620 Thomas Schöpping
182 1e5f7648 Thomas Schöpping
/**
183 c930aa01 Thomas Schöpping
 * @brief   LIGHT_XLAT output signal GPIO.
184
 */
185
static apalGpio_t _gpioLightXlat = {
186
  /* line */ LINE_LIGHT_XLAT,
187
};
188
ROMCONST apalControlGpio_t moduleGpioLightXlat = {
189
  /* GPIO */ &_gpioLightXlat,
190
  /* meta */ {
191
    /* direction      */ APAL_GPIO_DIRECTION_OUTPUT,
192
    /* active state   */ (TLC5947_LLD_XLAT_UPDATE_EDGE == APAL_GPIO_EDGE_RISING) ? APAL_GPIO_ACTIVE_HIGH : APAL_GPIO_ACTIVE_LOW,
193
    /* interrupt edge */ APAL_GPIO_EDGE_NONE,
194
  },
195
};
196
197
/**
198 1e5f7648 Thomas Schöpping
 * @brief   SYS_PD bidirectional signal GPIO.
199
 */
200
static apalGpio_t _gpioSysPd = {
201 3106e8cc Thomas Schöpping
  /* line */ LINE_SYS_PD_N,
202 e545e620 Thomas Schöpping
};
203 acc97cbf Thomas Schöpping
ROMCONST apalControlGpio_t moduleGpioSysPd = {
204 1e5f7648 Thomas Schöpping
  /* GPIO */ &_gpioSysPd,
205
  /* meta */ {
206
    /* direction      */ APAL_GPIO_DIRECTION_BIDIRECTIONAL,
207
    /* active state   */ APAL_GPIO_ACTIVE_LOW,
208
    /* interrupt edge */ APAL_GPIO_EDGE_BOTH,
209
  },
210
};
211 e545e620 Thomas Schöpping
212 1e5f7648 Thomas Schöpping
/**
213
 * @brief   SYS_SYNC bidirectional signal GPIO.
214
 */
215
static apalGpio_t _gpioSysSync = {
216 3106e8cc Thomas Schöpping
  /* line */ LINE_SYS_INT_N,
217 e545e620 Thomas Schöpping
};
218 acc97cbf Thomas Schöpping
ROMCONST apalControlGpio_t moduleGpioSysSync = {
219 1e5f7648 Thomas Schöpping
  /* GPIO */ &_gpioSysSync,
220
  /* meta */ {
221
    /* direction      */ APAL_GPIO_DIRECTION_BIDIRECTIONAL,
222
    /* active state   */ APAL_GPIO_ACTIVE_LOW,
223
    /* interrupt edge */ APAL_GPIO_EDGE_BOTH,
224
  },
225
};
226 e545e620 Thomas Schöpping
227
/** @} */
228
229
/*===========================================================================*/
230
/**
231
 * @name AMiRo-OS core configurations
232
 * @{
233
 */
234
/*===========================================================================*/
235
236 cda14729 Thomas Schöpping
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
237 acc97cbf Thomas Schöpping
ROMCONST char* moduleShellPrompt = "LightRing";
238 cda14729 Thomas Schöpping
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
239 6b53f6bf Thomas Schöpping
240
/** @} */
241
242
/*===========================================================================*/
243
/**
244
 * @name Startup Shutdown Synchronization Protocol (SSSP)
245
 * @{
246
 */
247
/*===========================================================================*/
248
249 c53ef0b1 Thomas Schöpping
#if ((AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MSI == true)) || defined(__DOXYGEN__)
250
251
/* some local definitions */
252
// maximum number of bytes per CAN frame
253
#define CAN_BYTES_PER_FRAME                     8
254
// identifier (as dominant as possible)
255
#define MSI_BCBMSG_CANID                        0
256
257
aos_ssspbcbstatus_t moduleSsspBcbTransmit(const uint8_t* buffer, size_t length)
258
{
259
  aosDbgCheck(buffer != NULL);
260
  aosDbgCheck(length > 0 && length <= CAN_BYTES_PER_FRAME);
261
262
  // local variables
263
  CANTxFrame frame;
264
265
  // setup the common parts of the message frame
266
  frame.DLC = (uint8_t)length;
267
  frame.RTR = CAN_RTR_DATA;
268
  frame.IDE = CAN_IDE_STD;
269
  frame.SID = MSI_BCBMSG_CANID;
270
  memcpy(frame.data8, buffer, length);
271
272
  // sent the frame and return
273
  return (canTransmitTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &frame, TIME_IMMEDIATE) == MSG_OK) ? AOS_SSSP_BCB_SUCCESS : AOS_SSSP_BCB_ERROR;
274
}
275
276
aos_ssspbcbstatus_t moduleSsspBcbReceive(uint8_t* buffer, size_t length)
277
{
278
  aosDbgCheck(buffer != NULL);
279
  aosDbgCheck(length > 0 && length <= CAN_BYTES_PER_FRAME);
280
281
  // local variables
282
  CANRxFrame frame;
283
284
  // receive a frame and check for errors
285
  if (canReceiveTimeout(&MODULE_HAL_CAN, CAN_ANY_MAILBOX, &frame, TIME_IMMEDIATE) == MSG_OK) {
286
    // a correct frame was received
287
    if (frame.DLC == length &&
288
        frame.RTR == CAN_RTR_DATA &&
289
        frame.IDE == CAN_IDE_STD &&
290
        frame.SID == MSI_BCBMSG_CANID) {
291
      // success: fetch the data and return
292
      memcpy(buffer, frame.data8, length);
293
      return AOS_SSSP_BCB_SUCCESS;
294
    }
295
    // an unexpected frame was received
296
    else {
297
      return AOS_SSSP_BCB_INVALIDMSG;
298
    }
299
  } else {
300
    // failure: return with error
301
    return AOS_SSSP_BCB_ERROR;
302
  }
303
}
304
305
#undef MSI_BCBMSG_CANID
306
#undef CAN_BYTES_PER_FRAME
307
308
#endif /* (AMIROOS_CFG_SSSP_ENABLE == true) && (AMIROOS_CFG_SSSP_MSI == true) */
309
310 e545e620 Thomas Schöpping
/** @} */
311
312
/*===========================================================================*/
313
/**
314
 * @name Low-level drivers
315
 * @{
316
 */
317
/*===========================================================================*/
318
319 ddf34c3d Thomas Schöpping
AT24C01BDriver moduleLldEeprom = {
320 e545e620 Thomas Schöpping
  /* I2C driver   */ &MODULE_HAL_I2C_EEPROM,
321
  /* I2C address  */ 0x00u,
322
};
323
324
TLC5947Driver moduleLldLedPwm = {
325
  /* SPI driver         */ &MODULE_HAL_SPI_LIGHT,
326 1e5f7648 Thomas Schöpping
  /* BLANK signal GPIO  */ &moduleGpioLightBlank,
327 c930aa01 Thomas Schöpping
  /* XLAT signal GPIO   */ &moduleGpioLightXlat,
328 e545e620 Thomas Schöpping
};
329
330 ddf34c3d Thomas Schöpping
TPS20xxBDriver moduleLldPowerSwitchLaser = {
331 1e5f7648 Thomas Schöpping
  /* laser enable GPIO      */ &moduleGpioLaserEn,
332
  /* laser overcurrent GPIO */ &moduleGpioLaserOc,
333 e545e620 Thomas Schöpping
};
334
335
/** @} */
336
337
/*===========================================================================*/
338
/**
339 4c72a54c Thomas Schöpping
 * @name Tests
340 e545e620 Thomas Schöpping
 * @{
341
 */
342
/*===========================================================================*/
343
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
344
345 8be006e0 Thomas Schöpping
/*
346 4c72a54c Thomas Schöpping
 * AT24C01BN-SH-B (EEPROM)
347 8be006e0 Thomas Schöpping
 */
348 4c72a54c Thomas Schöpping
#include <module_test_AT24C01B.h>
349
static int _testAt24co1bShellCmdCb(BaseSequentialStream* stream, int argc, char* argv[])
350 e545e620 Thomas Schöpping
{
351 4c72a54c Thomas Schöpping
  return moduleTestAt24c01bShellCb(stream, argc, argv, NULL);
352 e545e620 Thomas Schöpping
}
353 4c72a54c Thomas Schöpping
AOS_SHELL_COMMAND(moduleTestAt24c01bShellCmd, "test:EEPROM", _testAt24co1bShellCmdCb);
354 e545e620 Thomas Schöpping
355 8be006e0 Thomas Schöpping
/*
356 4c72a54c Thomas Schöpping
 * TLC5947 (24 channel PWM LED driver)
357 8be006e0 Thomas Schöpping
 */
358 4c72a54c Thomas Schöpping
#include <module_test_TLC5947.h>
359
static int _testTlc5947ShellCmdCb(BaseSequentialStream* stream, int argc, char* argv[])
360 e545e620 Thomas Schöpping
{
361 4c72a54c Thomas Schöpping
  return moduleTestTlc5947ShellCb(stream, argc, argv, NULL);
362 e545e620 Thomas Schöpping
}
363 4c72a54c Thomas Schöpping
AOS_SHELL_COMMAND(moduleTestTlc5947ShellCmd, "test:Lights", _testTlc5947ShellCmdCb);
364
365
/*
366
 * TPS2051BDBV (Current-limited power switch)
367
 */
368
#include <module_test_TPS20xxB.h>
369
static int _testTps2051bdbvShellCmdCb(BaseSequentialStream* stream, int argc, char* argv[])
370
{
371
  return moduleTestTps20xxbShellCb(stream, argc, argv, NULL);
372
}
373
AOS_SHELL_COMMAND(moduleTestTps2051bdbvShellCmd, "test:PowerSwitch", _testTps2051bdbvShellCmdCb);
374 e545e620 Thomas Schöpping
375 8be006e0 Thomas Schöpping
/*
376 4c72a54c Thomas Schöpping
 * entire module
377 8be006e0 Thomas Schöpping
 */
378 4c72a54c Thomas Schöpping
static int _testAllShellCmdCb(BaseSequentialStream* stream, int argc, char* argv[])
379 e545e620 Thomas Schöpping
{
380
  (void)argc;
381
  (void)argv;
382 4c72a54c Thomas Schöpping
383
  int status = AOS_OK;
384
  char* targv[AMIROOS_CFG_SHELL_MAXARGS] = {NULL};
385
  aos_testresult_t result_test = {0, 0};
386
  aos_testresult_t result_total = {0, 0};
387
388
  /* AT24C01B (EEPROM) */
389
  status |= moduleTestAt24c01bShellCb(stream, 0, targv, &result_test);
390
  result_total = aosTestResultAdd(result_total, result_test);
391
392
  /* TLC5947 (24 channel LED PWM driver) */
393
  status |= moduleTestTlc5947ShellCb(stream, 0, targv, &result_test);
394
  result_total = aosTestResultAdd(result_total, result_test);
395
396
  /* TPS2051BDBV (Current-limited power switch) */
397
  status |= moduleTestTps20xxbShellCb(stream, 0, targv, &result_test);
398
  result_total = aosTestResultAdd(result_total, result_test);
399
400
  // print total result
401
  chprintf(stream, "\n");
402
  aosTestResultPrintSummary(stream, &result_total, "entire module");
403
404
  return status;
405 e545e620 Thomas Schöpping
}
406 4c72a54c Thomas Schöpping
AOS_SHELL_COMMAND(moduleTestAllShellCmd, "test:all", _testAllShellCmdCb);
407 e545e620 Thomas Schöpping
408 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
409 e545e620 Thomas Schöpping
410
/** @} */
411 3940ba8a Thomas Schöpping
/** @} */