Paparazzi UAS  v4.0.4_stable-3-gf39211a
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test_board.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2009 Antoine Drouin <poinix@gmail.com>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
22 #include <inttypes.h>
23 #include <string.h>
24 
25 #define DATALINK_C
26 
27 #include "test_board.h"
28 
29 #include "std.h"
30 #include "mcu.h"
31 #include "mcu_periph/uart.h"
32 #include "mcu_periph/i2c.h"
33 #include "mcu_periph/sys_time.h"
35 #include "led.h"
36 
38 #include "generated/settings.h"
39 
40 #include "lisa/lisa_baro.h"
42 
43 static inline void main_init( void );
44 static inline void main_periodic_task( void );
45 static inline void main_event_task( void );
46 
47 static void test_none_start(void);
48 static void test_none_periodic(void);
49 static void test_none_event(void);
50 
51 static void test_baro_start(void);
52 static void test_baro_periodic(void);
53 static void test_baro_event(void);
54 
55 static void test_bldc_start(void);
56 static void test_bldc_periodic(void);
57 static void test_bldc_event(void);
58 
59 static void test_srvo_start(void);
60 static void test_srvo_periodic(void);
61 static void test_srvo_event(void);
62 
63 static void test_uart_start(void);
64 static void test_uart_periodic(void);
65 static void test_uart_event(void);
66 
67 static void all_led_green(void);
68 static void all_led_red(void);
69 static void all_led_off(void);
70 
71 struct TestFuns {
72  void (*_start)(void);
73  void (*_periodic)(void);
74  void (*_event)(void);
75 };
76 
77 struct TestFuns tests[] = {
79  {._start=test_baro_start, ._periodic=test_baro_periodic, ._event=test_baro_event},
80  {._start=test_bldc_start, ._periodic=test_bldc_periodic, ._event=test_bldc_event},
81  {._start=test_srvo_start, ._periodic=test_srvo_periodic, ._event=test_srvo_event},
82  {._start=test_uart_start, ._periodic=test_uart_periodic, ._event=test_uart_event}
83 };
84 
86 
87 
88 int main( void ) {
89  main_init();
90  while(1) {
94  }
95  return 0;
96 }
97 
98 static inline void main_init( void ) {
99 
100  mcu_init();
102  led_init();
103 
104  baro_init();
105  actuators_init();
106 
107  // cur_test = TestTypeNone;
109 
110 }
111 
112 static inline void main_periodic_task( void ) {
113 
114  LED_PERIODIC();
115  RunOnceEvery(256, {DOWNLINK_SEND_ALIVE(DefaultChannel, DefaultDevice, 16, MD5SUM);});
116 
117  tests[cur_test]._periodic();
118 
119 }
120 
121 static inline void main_event_task( void ) {
122 
123  DatalinkEvent();
124 
125  tests[cur_test]._event();
126 
127 }
128 
129 void start_test(void) {
130  all_led_off();
131  tests[cur_test]._start();
132 }
133 
134 
135 
136 
137 /*
138  *
139  * Test nothing
140  *
141  */
142 static void test_none_start(void) {}
143 static void test_none_periodic(void) {}
144 static void test_none_event(void) {}
145 
146 
147 /*
148  *
149  * Test baro
150  *
151  */
152 static inline void test_baro_on_baro_diff(void);
153 static inline void test_baro_on_baro_abs(void);
154 static void test_baro_start(void) {all_led_green();}
155 static void test_baro_periodic(void) {
156  RunOnceEvery(2, {baro_periodic();});
157  RunOnceEvery(100,{
158  uint16_t i2c2_ack_fail_cnt = i2c2.errors->ack_fail_cnt;
159  uint16_t i2c2_miss_start_stop_cnt = i2c2.errors->miss_start_stop_cnt;
160  uint16_t i2c2_arb_lost_cnt = i2c2.errors->arb_lost_cnt;
161  uint16_t i2c2_over_under_cnt = i2c2.errors->over_under_cnt;
162  uint16_t i2c2_pec_recep_cnt = i2c2.errors->pec_recep_cnt;
163  uint16_t i2c2_timeout_tlow_cnt = i2c2.errors->timeout_tlow_cnt;
164  uint16_t i2c2_smbus_alert_cnt = i2c2.errors->smbus_alert_cnt;
165  uint16_t i2c2_unexpected_event_cnt = i2c2.errors->unexpected_event_cnt;
166  uint32_t i2c2_last_unexpected_event = i2c2.errors->last_unexpected_event;
167  const uint8_t _bus2 = 2;
168  DOWNLINK_SEND_I2C_ERRORS(DefaultChannel, DefaultDevice,
169  &i2c2_ack_fail_cnt,
170  &i2c2_miss_start_stop_cnt,
171  &i2c2_arb_lost_cnt,
172  &i2c2_over_under_cnt,
173  &i2c2_pec_recep_cnt,
174  &i2c2_timeout_tlow_cnt,
175  &i2c2_smbus_alert_cnt,
176  &i2c2_unexpected_event_cnt,
177  &i2c2_last_unexpected_event,
178  &_bus2);
179  });
180 }
182 static inline void test_baro_on_baro_abs(void) {
183  RunOnceEvery(5,{DOWNLINK_SEND_BOOZ_BARO2_RAW(DefaultChannel, DefaultDevice, &baro.abs_raw, &baro.diff_raw);});
184 }
185 static inline void test_baro_on_baro_diff(void) {
186  RunOnceEvery(5,{DOWNLINK_SEND_BOOZ_BARO2_RAW(DefaultChannel, DefaultDevice, &baro.abs_raw, &baro.diff_raw);});
187 }
188 
189 
190 /*
191  *
192  * Test motor controller
193  *
194  */
195 
196 static void test_bldc_start(void) {}
197 static void test_bldc_periodic(void) {
198 
199  i2c1_buf[0] = 0x04;
200  i2c1_transmit(0x58, 1, NULL);
201 
202  RunOnceEvery(100,{
203  uint16_t i2c1_ack_fail_cnt = i2c1.errors->ack_fail_cnt;
204  uint16_t i2c1_miss_start_stop_cnt = i2c1.errors->miss_start_stop_cnt;
205  uint16_t i2c1_arb_lost_cnt = i2c1.errors->arb_lost_cnt;
206  uint16_t i2c1_over_under_cnt = i2c1.errors->over_under_cnt;
207  uint16_t i2c1_pec_recep_cnt = i2c1.errors->pec_recep_cnt;
208  uint16_t i2c1_timeout_tlow_cnt = i2c1.errors->timeout_tlow_cnt;
209  uint16_t i2c1_smbus_alert_cnt = i2c1.errors->smbus_alert_cnt;
210  uint16_t i2c1_unexpected_event_cnt = i2c1.errors->unexpected_event_cnt;
211  uint32_t i2c1_last_unexpected_event = i2c1.errors->last_unexpected_event;
212  const uint8_t _bus1 = 1;
213  DOWNLINK_SEND_I2C_ERRORS(DefaultChannel, DefaultDevice,
214  &i2c1_ack_fail_cnt,
215  &i2c1_miss_start_stop_cnt,
216  &i2c1_arb_lost_cnt,
217  &i2c1_over_under_cnt,
218  &i2c1_pec_recep_cnt,
219  &i2c1_timeout_tlow_cnt,
220  &i2c1_smbus_alert_cnt,
221  &i2c1_unexpected_event_cnt,
222  &i2c1_last_unexpected_event,
223  &_bus1);
224  });
225 }
226 
227 static void test_bldc_event(void) {}
228 
229 
230 /*
231  *
232  * Test servos
233  *
234  */
235 static void test_srvo_start(void) {}
236 static void test_srvo_periodic(void) {
237  static float foo = 0.;
238  foo += 0.0025;
239  int32_t bar = 1500 + 500. * sin(foo);
240  for (uint8_t i=0; i<6; i++)
241  actuators_pwm_values[i] = bar;
243 }
244 static void test_srvo_event(void) {}
245 
246 
247 
248 /*
249  *
250  * Test Uarts
251  *
252  */
254 static const uint8_t buf_src[] = { 42, 43, 44, 45, 46, 122, 126, 128 };
255 static uint8_t buf_dest[sizeof(buf_src)];
259 
260 static void test_uart_start(void) {
261  idx_rx = 0;
262  idx_tx = 0;
264 }
265 
266 static void test_uart_periodic(void) {
267 
268  if (idx_tx<sizeof(buf_src)) {
269  switch (direction) {
270  case OneToThree : Uart1Transmit(buf_src[idx_tx]); break;
271  case ThreeToOne : Uart3Transmit(buf_src[idx_tx]); break;
272  default: break;
273  }
274  idx_tx++;
275  }
276 
277 }
278 
279 static void test_uart_event(void) {
280 
281  if (Uart3ChAvailable()) {
282  buf_dest[idx_rx] = Uart3Getch();
283  if (idx_rx<sizeof(buf_src)) {
284  DOWNLINK_SEND_DEBUG(DefaultChannel, DefaultDevice, sizeof(buf_src), buf_dest);
285  idx_rx++;
286  if (idx_rx == sizeof(buf_src)) {
287  if ( memcmp(buf_dest, buf_src, sizeof(buf_src)) ) {
288  all_led_red(); // test failed
289  }
290  else { // start test in other direction
291  idx_rx = 0;
292  idx_tx = 0;
294  }
295  }
296  }
297  }
298 
299  if (Uart1ChAvailable()) {
300  buf_dest[idx_rx] = Uart1Getch();
301  if (idx_rx<sizeof(buf_src)) {
302  DOWNLINK_SEND_DEBUG(DefaultChannel, DefaultDevice, sizeof(buf_src), buf_dest);
303  idx_rx++;
304  if (idx_rx == sizeof(buf_src)) {
305  if ( memcmp(buf_dest, buf_src, sizeof(buf_src)) ) {
306  all_led_red();
307  }
308  else {
309  all_led_green();
310  }
311  }
312  }
313  }
314 }
315 
316 
317 
318 /*
319  *
320  * LED utilities
321  *
322  */
323 
324 static void all_led_green(void) {
325  LED_OFF(0); LED_OFF(2); LED_OFF(4); LED_OFF(6);
326  LED_ON(1); LED_ON(3); LED_ON(5); LED_ON(7);
327 }
328 
329 static void all_led_red(void) {
330  LED_OFF(1); LED_OFF(3); LED_OFF(5); LED_OFF(7);
331  LED_ON(0); LED_ON(2); LED_ON(4); LED_ON(6);
332 }
333 
334 static void all_led_off(void) {
335  LED_OFF(0); LED_OFF(2); LED_OFF(4); LED_OFF(6);
336  LED_OFF(1); LED_OFF(3); LED_OFF(5); LED_OFF(7);
337 }
338 
339 
340 /*
341  *
342  * Datalink
343  *
344  */
345 #define IdOfMsg(x) (x[1])
346 
347 void dl_parse_msg(void) {
348 
349  uint8_t msg_id = IdOfMsg(dl_buffer);
350  switch (msg_id) {
351 
352  case DL_PING:
353  {
354  DOWNLINK_SEND_PONG(DefaultChannel, DefaultDevice);
355  }
356  break;
357 
358  case DL_SETTING :
359  {
360  if (DL_SETTING_ac_id(dl_buffer) != AC_ID) break;
361  uint8_t i = DL_SETTING_index(dl_buffer);
362  float var = DL_SETTING_value(dl_buffer);
363  DlSetting(i, var);
364  DOWNLINK_SEND_DL_VALUE(DefaultChannel, DefaultDevice, &i, &var);
365  }
366  break;
367 
368  default:
369  break;
370  }
371 }
TestType
Definition: test_board.h:4
unsigned short uint16_t
Definition: types.h:16
int main(void)
Definition: test_board.c:88
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
int32_t actuators_pwm_values[ACTUATORS_PWM_NB]
static void test_none_start(void)
Definition: test_board.c:142
enum TestType cur_test
Definition: test_board.c:85
UartTestType
Definition: test_board.c:253
void(* _event)(void)
Definition: test_board.c:74
struct TestFuns tests[]
Definition: test_board.c:77
void baro_periodic(void)
Definition: baro_board.c:56
#define BaroEvent(_b_abs_handler, _b_diff_handler)
Definition: baro_board.h:22
static void test_uart_periodic(void)
Definition: test_board.c:266
#define LED_ON(i)
Definition: led_hw.h:28
#define IdOfMsg(x)
Definition: test_board.c:345
static void all_led_green(void)
Definition: test_board.c:324
static void main_init(void)
Definition: test_board.c:98
static enum UartTestType direction
Definition: test_board.c:258
static void test_none_event(void)
Definition: test_board.c:144
static void test_baro_periodic(void)
Definition: test_board.c:155
static void test_baro_start(void)
Definition: test_board.c:154
#define PERIODIC_FREQUENCY
Definition: imu_aspirin2.c:54
static void test_uart_start(void)
Definition: test_board.c:260
static void test_srvo_start(void)
Definition: test_board.c:235
static void test_baro_on_baro_diff(void)
Definition: test_board.c:185
struct Baro baro
Definition: baro_board.c:34
Architecture independent timing functions.
unsigned long uint32_t
Definition: types.h:18
void actuators_init(void)
static void led_init(void)
Definition: led.h:86
static uint8_t idx_tx
Definition: test_board.c:256
static void test_srvo_event(void)
Definition: test_board.c:244
uint16_t foo
Definition: main_demo5.c:54
void dl_parse_msg(void)
Definition: test_board.c:347
static void test_baro_on_baro_abs(void)
Definition: test_board.c:182
static void test_bldc_event(void)
Definition: test_board.c:227
int sys_time_register_timer(float duration, sys_time_cb cb)
Register a new system timer.
Definition: sys_time.c:35
void baro_init(void)
Definition: baro_board.c:38
static bool_t sys_time_check_and_ack_timer(tid_t id)
Definition: sys_time.h:90
signed long int32_t
Definition: types.h:19
void(* _start)(void)
Definition: test_board.c:72
void(* _periodic)(void)
Definition: test_board.c:73
static const uint8_t buf_src[]
Definition: test_board.c:254
static uint8_t idx_rx
Definition: test_board.c:257
#define LED_PERIODIC()
Definition: led_hw.h:8
arch independent mcu ( Micro Controller Unit ) utilities
unsigned char uint8_t
Definition: types.h:14
static void test_bldc_start(void)
Definition: test_board.c:196
void start_test(void)
Definition: test_board.c:129
static uint8_t buf_dest[sizeof(buf_src)]
Definition: test_board.c:255
static void test_baro_event(void)
Definition: test_board.c:181
#define LED_OFF(i)
Definition: led_hw.h:29
arch independent LED (Light Emitting Diodes) API
static void test_none_periodic(void)
Definition: test_board.c:143
static void all_led_red(void)
Definition: test_board.c:329
static void all_led_off(void)
Definition: test_board.c:334
static void test_srvo_periodic(void)
Definition: test_board.c:236
static void test_uart_event(void)
Definition: test_board.c:279
static void main_event_task(void)
Definition: test_board.c:121
void actuators_pwm_commit(void)
static void main_periodic_task(void)
Definition: test_board.c:112
void mcu_init(void)
Definition: mcu.c:57
static void test_bldc_periodic(void)
Definition: test_board.c:197