Paparazzi UAS  v5.12_stable-4-g9b43e9b
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
vms_ecu_demo.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Michal Podhradsky
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 
30 
31 // Messages
32 #include "pprzlink/messages.h"
34 
35 #include "mcu_periph/gpio.h"
36 #include "ch.h" // for DAC
37 #include "hal.h" // for DAC
38 
39 #include "generated/airframe.h"
40 #include BOARD_CONFIG
41 
42 /*
43  * ADC
44  */
45 // define bit resolution
46 #ifndef ADC_BIT_RES
47 #define ADC_BIT_RES 12
48 #endif
49 
50 // default reference (3.3 Volts)
51 #define ADC_VREF 3300.0 //mV
52 
53 // reading multiplier
54 #define ADC_VREF_MULT ADC_VREF/(1<<ADC_BIT_RES)
55 
56 // voltage divider value
57 #define ADC_VGAIN 1.5 // 1k1 and 2k2 (5V->3.3V)
58 
59 #define DAC_BUFFER_SIZE 1//360
60 
61 float ain_1;
62 float ain_2;
63 float ain_3;
64 float ain_4;
65 
70 
71 
72 
73 /*
74  * DAC
75  */
78 
79 static const DACConfig dac1cfg1 = {
80  .init = 2047U,
81  .datamode = DAC_DHRM_12BIT_RIGHT
82 };
83 
84 static const DACConfig dac1cfg2 = {
85  .init = 0U,
86  .datamode = DAC_DHRM_12BIT_RIGHT
87 };
88 
89 
90 dacsample_t dac_ref1;
91 dacsample_t dac_ref2;
92 
93 
94 /*
95  * DIGITAL IO
96  */
98 bool pwr_ready;
99 bool pwr_stdby;
100 bool rtds;
101 
104 
105 
106 
107 /*
108  * CAN
109  */
110 struct can_instance {
111  CANDriver *canp;
113 };
114 
115 static const struct can_instance can1 = {&CAND1, 11};
116 static const struct can_instance can2 = {&CAND2, 12};
117 
118 
119 /*
120  * Internal loopback mode, 500KBaud, automatic wakeup, automatic recover
121  * from abort mode.
122  * See section 22.7.7 on the STM32 reference manual.
123  */
124 static const CANConfig cancfg_lb = {
125  CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
126  CAN_BTR_LBKM | CAN_BTR_SJW(0) | CAN_BTR_TS2(1) |
127  CAN_BTR_TS1(8) | CAN_BTR_BRP(6)
128 };
129 
130 /*
131  * Normal mode, see if we can ping each other
132  */
133 static const CANConfig cancfg = {
134  CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
135  CAN_BTR_SJW(0) | CAN_BTR_TS2(1) |
136  CAN_BTR_TS1(8) | CAN_BTR_BRP(6)
137 };
138 
139 /*
140  * Receiver thread.
141  */
142 static THD_WORKING_AREA(can_rx1_wa, 256);
143 static THD_WORKING_AREA(can_rx2_wa, 256);
144 static THD_FUNCTION(can_rx, p) {
145  struct can_instance *cip = p;
146  event_listener_t el;
147  CANRxFrame rxmsg;
148 
149  (void)p;
150  chRegSetThreadName("receiver");
151  chEvtRegister(&cip->canp->rxfull_event, &el, 0);
152  while(!chThdShouldTerminateX()) {
153  if (chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(100)) == 0)
154  continue;
155  while (canReceive(cip->canp, CAN_ANY_MAILBOX,
156  &rxmsg, TIME_IMMEDIATE) == MSG_OK) {
157  // Process message.
158  palTogglePad(GPIOD, cip->led);
159  }
160  }
161  //chEvtUnregister(&CAND1.rxfull_event, &el);
162  chEvtUnregister(&cip->canp->rxfull_event, &el);
163 }
164 
165 
166 /*
167  * Transmitter thread.
168  */
169 static THD_WORKING_AREA(can_tx_wa, 256);
170 static THD_FUNCTION(can_tx, p) {
171  CANTxFrame txmsg;
172 
173  (void)p;
174  chRegSetThreadName("transmitter");
175  txmsg.IDE = CAN_IDE_EXT;
176  txmsg.EID = 0x01234567;
177  txmsg.RTR = CAN_RTR_DATA;
178  txmsg.DLC = 8;
179  txmsg.data32[0] = 0x55AA55AA;
180  txmsg.data32[1] = 0x00FF00FF;
181 
182  while (!chThdShouldTerminateX()) {
183  //canTransmit(&CAND1, CAN_ANY_MAILBOX, &txmsg, MS2ST(100));
184  canTransmit(&CAND2, CAN_ANY_MAILBOX, &txmsg, MS2ST(100));
185  chThdSleepMilliseconds(500);
186  }
187 }
188 
189 
191 {
192  // Digital
193  ams_status = false;
194  pwr_ready = false;
195  pwr_stdby = false;
196  rtds = false;
197  stg_in = 0;
198  stb_in = 0;
199 
200  // Analog
201  ain_1 = 0.0;
202  ain_2 = 0.0;
203  ain_3 = 0.0;
204  ain_4 = 0.0;
205 
210 
211  dac_1 = 0;
212  dac_2 = 0;
213 
214  /*
215  * Activates the CAN drivers 1 and 2.
216  */
217  canStart(&CAND1, &cancfg);
218  canStart(&CAND2, &cancfg);
219 
220 
221 
222  /*
223  * Starting the transmitter and receiver threads.
224  */
225  chThdCreateStatic(can_rx1_wa, sizeof(can_rx1_wa), NORMALPRIO + 7,
226  can_rx, (void *)&can1);
227  chThdCreateStatic(can_rx2_wa, sizeof(can_rx2_wa), NORMALPRIO + 7,
228  can_rx, (void *)&can2);
229  chThdCreateStatic(can_tx_wa, sizeof(can_tx_wa), NORMALPRIO + 7,
230  can_tx, NULL);
231 
232  //DAC
233  /*
234  * Starting DAC1 driver, setting up the output pin as analog as suggested
235  * by the Reference Manual.
236  */
237  palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
238  palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
239  dacStart(&DACD1, &dac1cfg1);
240  dacStart(&DACD2, &dac1cfg2);
241  dac_ref1 = 0;
242  dac_ref2 = 0;
243 
244  dacPutChannelX(&DACD1,0,dac_ref1);
245  dacPutChannelX(&DACD2,1,dac_ref2);
246 }
247 
249 {
250  // * PD10 - Digital output. (DOUT_LS1)
251  if (ams_status) {
252  gpio_set(GPIOD, 10);
253  }
254  else {
255  gpio_clear(GPIOD, 10);
256  }
257 
258  // *PD11 - Digital output. (DOUT_LS2).
259  if (pwr_ready) {
260  gpio_set(GPIOD, 11);
261  }
262  else {
263  gpio_clear(GPIOD, 11);
264  }
265 
266  // *PD12 - Digital output. (DOUT_LS3).
267  if (pwr_stdby) {
268  gpio_set(GPIOD, 12);
269  }
270  else {
271  gpio_clear(GPIOD, 12);
272  }
273 
274  // *PD13 - Digital output. (DOUT_LS4).
275  static uint8_t cnt = 0;
276  if (rtds) {
277  // 120Hz, 0-5 Ain
278  // 5V -> fast flash
279  // 0V -> slow flash
280  if (cnt > (10-(uint8_t)(ain_1 + ain_2))) {
281 
282  gpio_set(GPIOD, 13);
283  cnt = 0;
284  }
285  else {
286  gpio_clear(GPIOD, 13);
287  cnt++;
288  }
289 
290 
291  }
292  else {
293  gpio_clear(GPIOD, 13);
294  }
295 
296  static bool flag = false;
297  // read inputs
298  if (flag) {
299  stg_in = palReadPad(GPIOE, 7); // PE7 - Digital input. (DIN1:DIN_STG1).
300  flag = false;
301  }
302  else {
303  stb_in = palReadPad(GPIOE, 9); // PE9 - Digital input. (DIN3:DIN_STB1).
304  flag = true;
305  }
306 
307  // Analog
312 }
313 
314 
316  DOWNLINK_SEND_ECU(DefaultChannel, DefaultDevice,
317  &stg_in,
318  &stb_in,
319  &ain_1,
320  &ain_2,
321  &ain_3,
322  &ain_4);
323 }
324 
325 
327  dac_1 = val;
328  dac_ref1 = dac_1;
329  dacPutChannelX(&DACD1,0,dac_ref1);
330 }
331 
333  dac_2 = val;
334  dac_ref2 = dac_2;
335  dacPutChannelX(&DACD2,1,dac_ref2);
336 }
uint8_t av_nb_sample
Definition: adc.h:57
#define ADC_4
Definition: board.h:756
unsigned short uint16_t
Definition: types.h:16
#define GPIOA
Definition: gpio_arch.h:34
uint16_t dac_1
Definition: vms_ecu_demo.c:76
static THD_FUNCTION(can_rx, p)
Definition: vms_ecu_demo.c:144
static void gpio_clear(ioportid_t port, uint16_t pin)
Clear a gpio output to low level.
Definition: gpio_arch.h:108
static const struct can_instance can1
Definition: vms_ecu_demo.c:115
#define ADC_VREF_MULT
Definition: vms_ecu_demo.c:54
Some architecture independent helper functions for GPIOs.
struct adc_buf adc_buf_2
Definition: vms_ecu_demo.c:67
void adc_buf_channel(uint8_t adc_channel, struct adc_buf *s, uint8_t av_nb_sample)
Link between ChibiOS ADC drivers and Paparazzi adc_buffers.
Definition: adc_arch.c:276
uint32_t sum
Definition: adc.h:54
#define ADC_1
Definition: booz_1.0.h:72
void vms_ecu_demo_downlink(void)
Definition: vms_ecu_demo.c:315
static const struct can_instance can2
Definition: vms_ecu_demo.c:116
bool pwr_stdby
Definition: vms_ecu_demo.c:99
bool ams_status
Definition: vms_ecu_demo.c:97
dacsample_t dac_ref1
Definition: vms_ecu_demo.c:90
static const DACConfig dac1cfg2
Definition: vms_ecu_demo.c:84
Generic interface for all ADC hardware drivers, independent from microcontroller architecture.
Definition: adc.h:53
#define GPIOD
Definition: gpio_arch.h:35
uint16_t dac_2
Definition: vms_ecu_demo.c:77
static const DACConfig dac1cfg1
Definition: vms_ecu_demo.c:79
Viking Motorsports Engine Control Unit demo module see https://wiki.paparazziuav.org/wiki/VMS_ECU for...
float ain_1
Definition: vms_ecu_demo.c:61
CANDriver * canp
Definition: vms_ecu_demo.c:111
static const CANConfig cancfg
Definition: vms_ecu_demo.c:133
uint16_t val[TCOUPLE_NB]
static THD_WORKING_AREA(can_rx1_wa, 256)
unsigned long uint32_t
Definition: types.h:18
struct adc_buf adc_buf_3
Definition: vms_ecu_demo.c:68
float ain_3
Definition: vms_ecu_demo.c:63
void vms_ecu_demo_UpdateDac2(uint16_t val)
Definition: vms_ecu_demo.c:332
float ain_2
Definition: vms_ecu_demo.c:62
bool rtds
Definition: vms_ecu_demo.c:100
struct adc_buf adc_buf_1
Definition: vms_ecu_demo.c:66
uint32_t led
Definition: vms_ecu_demo.c:112
unsigned char uint8_t
Definition: types.h:14
#define ADC_2
Definition: booz_1.0.h:81
#define ADC_VGAIN
Definition: vms_ecu_demo.c:57
dacsample_t dac_ref2
Definition: vms_ecu_demo.c:91
static const CANConfig cancfg_lb
Definition: vms_ecu_demo.c:124
uint8_t stg_in
Definition: vms_ecu_demo.c:102
void vms_ecu_demo_periodic(void)
Definition: vms_ecu_demo.c:248
static float p[2][2]
#define DEFAULT_AV_NB_SAMPLE
Definition: adc.h:41
float ain_4
Definition: vms_ecu_demo.c:64
#define ADC_3
Definition: booz_1.0.h:90
#define GPIOE
Definition: gpio_arch.h:36
static void gpio_set(ioportid_t port, uint16_t pin)
Set a gpio output to high level.
Definition: gpio_arch.h:98
void vms_ecu_demo_init(void)
Definition: vms_ecu_demo.c:190
uint8_t stb_in
Definition: vms_ecu_demo.c:103
struct adc_buf adc_buf_4
Definition: vms_ecu_demo.c:69
void vms_ecu_demo_UpdateDac1(uint16_t val)
Reset sweep number.
Definition: vms_ecu_demo.c:326
bool pwr_ready
Definition: vms_ecu_demo.c:98