Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (11.485 KB)

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

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
 * @brief   Structures and constant for the LightRing v1.0 module.
22
 *
23
 * @addtogroup lightring_module
24
 * @{
25
 */
26

    
27
#include <amiroos.h>
28

    
29
#include <string.h>
30

    
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
  /* circular buffer mode        */ false,
67
  /* callback function pointer   */ NULL,
68
  /* chip select line port       */ PAL_PORT(LINE_LIGHT_XLAT),
69
  /* chip select line pad number */ PAL_PAD(LINE_LIGHT_XLAT),
70
  /* CR1                         */ SPI_CR1_BR_0 | SPI_CR1_BR_1,
71
  /* CR2                         */ SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN,
72
};
73

    
74
SPIConfig moduleHalSpiWlConfig = {
75
  /* circular buffer mode        */ false,
76
  /* callback function pointer   */ NULL,
77
  /* chip select line port       */ PAL_PORT(LINE_WL_SS_N),
78
  /* chip select line pad number */ PAL_PAD(LINE_WL_SS_N),
79
  /* CR1                         */ SPI_CR1_BR_0,
80
  /* CR2                         */ SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN,
81
};
82

    
83
/** @} */
84

    
85
/*===========================================================================*/
86
/**
87
 * @name GPIO definitions
88
 * @{
89
 */
90
/*===========================================================================*/
91

    
92
/**
93
 * @brief   LIGHT_BANK output signal GPIO.
94
 */
95
static apalGpio_t _gpioLightBlank = {
96
  /* line */ LINE_LIGHT_BLANK,
97
};
98
ROMCONST apalControlGpio_t moduleGpioLightBlank = {
99
  /* 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

    
107
/**
108
 * @brief   LASER_EN output signal GPIO.
109
 */
110
static apalGpio_t _gpioLaserEn = {
111
  /* line */ LINE_LASER_EN,
112
};
113
ROMCONST apalControlGpio_t moduleGpioLaserEn = {
114
  /* GPIO */ &_gpioLaserEn,
115
  /* meta */ {
116
    /* direction      */ APAL_GPIO_DIRECTION_OUTPUT,
117
    /* active state   */ TPS20xxB_LLD_ENABLE_ACTIVE_STATE,
118
    /* interrupt edge */ APAL_GPIO_EDGE_NONE,
119
  },
120
};
121

    
122
/**
123
 * @brief   LASER_OC input signal GPIO.
124
 */
125
static apalGpio_t _gpioLaserOc = {
126
  /* line */ LINE_LASER_OC_N,
127
};
128
ROMCONST apalControlGpio_t moduleGpioLaserOc = {
129
  /* GPIO */ &_gpioLaserOc,
130
  /* meta */ {
131
    /* direction      */ APAL_GPIO_DIRECTION_INPUT,
132
    /* active state   */ TPS20xxB_LLD_OVERCURRENT_ACTIVE_STATE,
133
    /* interrupt edge */ APAL_GPIO_EDGE_BOTH,
134
  },
135
};
136

    
137
/**
138
 * @brief   SYS_UART_DN bidirectional signal GPIO.
139
 */
140
static apalGpio_t _gpioSysUartDn = {
141
  /* line */ LINE_SYS_UART_DN,
142
};
143
ROMCONST apalControlGpio_t moduleGpioSysUartDn = {
144
  /* 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

    
152
/**
153
 * @brief   WL_GDO2 input signal GPIO.
154
 */
155
static apalGpio_t _gpioWlGdo2 = {
156
  /* line */ LINE_WL_GDO2,
157
};
158
ROMCONST apalControlGpio_t moduleGpioWlGdo2 = {
159
  /* 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

    
167
/**
168
 * @brief   WL_GDO0 input signal GPIO.
169
 */
170
static apalGpio_t _gpioWlGdo0= {
171
  /* line */ LINE_WL_GDO0,
172
};
173
ROMCONST apalControlGpio_t moduleGpioWlGdo0 = {
174
  /* 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

    
182
/**
183
 * @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
 * @brief   SYS_PD bidirectional signal GPIO.
199
 */
200
static apalGpio_t _gpioSysPd = {
201
  /* line */ LINE_SYS_PD_N,
202
};
203
ROMCONST apalControlGpio_t moduleGpioSysPd = {
204
  /* 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

    
212
/**
213
 * @brief   SYS_SYNC bidirectional signal GPIO.
214
 */
215
static apalGpio_t _gpioSysSync = {
216
  /* line */ LINE_SYS_INT_N,
217
};
218
ROMCONST apalControlGpio_t moduleGpioSysSync = {
219
  /* 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

    
227
/** @} */
228

    
229
/*===========================================================================*/
230
/**
231
 * @name AMiRo-OS core configurations
232
 * @{
233
 */
234
/*===========================================================================*/
235

    
236
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
237
ROMCONST char* moduleShellPrompt = "LightRing";
238
#endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */
239

    
240
/** @} */
241

    
242
/*===========================================================================*/
243
/**
244
 * @name Startup Shutdown Synchronization Protocol (SSSP)
245
 * @{
246
 */
247
/*===========================================================================*/
248

    
249
#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
/** @} */
311

    
312
/*===========================================================================*/
313
/**
314
 * @name Low-level drivers
315
 * @{
316
 */
317
/*===========================================================================*/
318

    
319
AT24C01BDriver moduleLldEeprom = {
320
  /* I2C driver   */ &MODULE_HAL_I2C_EEPROM,
321
  /* I2C address  */ 0x00u,
322
};
323

    
324
TLC5947Driver moduleLldLedPwm = {
325
  /* SPI driver         */ &MODULE_HAL_SPI_LIGHT,
326
  /* BLANK signal GPIO  */ &moduleGpioLightBlank,
327
  /* XLAT signal GPIO   */ &moduleGpioLightXlat,
328
};
329

    
330
TPS20xxBDriver moduleLldPowerSwitchLaser = {
331
  /* laser enable GPIO      */ &moduleGpioLaserEn,
332
  /* laser overcurrent GPIO */ &moduleGpioLaserOc,
333
};
334

    
335
/** @} */
336

    
337
/*===========================================================================*/
338
/**
339
 * @name Tests
340
 * @{
341
 */
342
/*===========================================================================*/
343
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
344

    
345
/*
346
 * AT24C01BN-SH-B (EEPROM)
347
 */
348
#include <module_test_AT24C01B.h>
349
static int _testAt24co1bShellCmdCb(BaseSequentialStream* stream, int argc, char* argv[])
350
{
351
  return moduleTestAt24c01bShellCb(stream, argc, argv, NULL);
352
}
353
AOS_SHELL_COMMAND(moduleTestAt24c01bShellCmd, "test:EEPROM", _testAt24co1bShellCmdCb);
354

    
355
/*
356
 * TLC5947 (24 channel PWM LED driver)
357
 */
358
#include <module_test_TLC5947.h>
359
static int _testTlc5947ShellCmdCb(BaseSequentialStream* stream, int argc, char* argv[])
360
{
361
  return moduleTestTlc5947ShellCb(stream, argc, argv, NULL);
362
}
363
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

    
375
/*
376
 * entire module
377
 */
378
static int _testAllShellCmdCb(BaseSequentialStream* stream, int argc, char* argv[])
379
{
380
  (void)argc;
381
  (void)argv;
382

    
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
}
406
AOS_SHELL_COMMAND(moduleTestAllShellCmd, "test:all", _testAllShellCmdCb);
407

    
408
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
409

    
410
/** @} */
411
/** @} */