Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
actuators_asctec_v2_new.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 The Paparazzi Team
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 
26 #include "subsystems/actuators.h"
28 
29 #include "mcu_periph/i2c.h"
30 #include "mcu_periph/sys_time.h"
31 
32 #define ACTUATORS_ASCTEC_V2_SLAVE_ADDR 0x00
33 
34 PRINT_CONFIG_VAR(ACTUATORS_ASCTEC_V2_I2C_DEV)
35 
37 
38 static uint16_t crc_update(uint16_t crc, uint8_t data);
39 
41 {
50 }
51 
52 
54 {
55 #if defined ACTUATORS_START_DELAY && ! defined SITL
56  if (!actuators_delay_done) {
57  if (SysTimeTimer(actuators_delay_time) < USEC_OF_SEC(ACTUATORS_START_DELAY)) {
58 #ifdef USE_I2C_ACTUATORS_REBOOT_HACK
59  //Lisa-L with Asctech v2 motors only start after reflashing when a bus error was sensed on stm32-i2c.
60  //multiple re-init solves the problem.
61  i2c1_init();
62 #endif
63  return;
64  } else { actuators_delay_done = true; }
65  }
66 #endif
67 
69  case I2CTransFailed:
72  break;
73  case I2CTransSuccess:
74  case I2CTransDone:
76  break;
77  default:
79  return;
80  }
81 
82  uint16_t crc=0xff;
83 
84 #ifdef KILL_MOTORS
90 
91  for (uint8_t i=0;i<5;i++)
93 
96 #else
97  switch (actuators_asctec_v2.cmd) {
98  case TEST:
103 
104  i2c_transmit(&ACTUATORS_ASCTEC_V2_I2C_DEV, &actuators_asctec_v2.i2c_trans, ACTUATORS_ASCTEC_V2_SLAVE_ADDR, 4);
106  break;
107  case REVERSE:
112 
113  i2c_transmit(&ACTUATORS_ASCTEC_V2_I2C_DEV, &actuators_asctec_v2.i2c_trans, ACTUATORS_ASCTEC_V2_SLAVE_ADDR, 4);
115  break;
116  case SET_ADDR: {
117  static uint8_t set_addr = 0;
118  static uint8_t serial_num[2];
119  switch(set_addr) {
120 
121  // Request address
122  case 0:
123  case 2:
124  i2c_receive(&ACTUATORS_ASCTEC_V2_I2C_DEV, &actuators_asctec_v2.i2c_trans, 0x06 + actuators_asctec_v2.cur_addr*2, 3);
125  set_addr++;
126  break;
127  case 1:
128  if(actuators_asctec_v2.i2c_trans.buf[0] == 2) {
129  serial_num[0] = actuators_asctec_v2.i2c_trans.buf[1];
130  set_addr++;
131  break;
132  }
133  set_addr--;
134  break;
135  case 3:
136  if(actuators_asctec_v2.i2c_trans.buf[0] == 3) {
137  serial_num[1] = actuators_asctec_v2.i2c_trans.buf[1];
138  set_addr++;
139  break;
140  }
141 
142  set_addr--;
143  break;
144  case 4:
145  //update ID of the motor based on the address
146  // NOTE: the missing break; at the end of this case is intentional
148  actuators_asctec_v2.i2c_trans.buf[1] = serial_num[0];
149  actuators_asctec_v2.i2c_trans.buf[2] = serial_num[1];
151 
152  i2c_transmit(&ACTUATORS_ASCTEC_V2_I2C_DEV, &actuators_asctec_v2.i2c_trans, ACTUATORS_ASCTEC_V2_SLAVE_ADDR, 4);
153  set_addr++;
154  /* Falls through. */
155  default:
156  set_addr = 0;
159  break;
160  }
161 
162  break;
163  }
164  case NONE:
170 
171  for (uint8_t i=0;i<5;i++)
173 
175  i2c_transmit(&ACTUATORS_ASCTEC_V2_I2C_DEV, &actuators_asctec_v2.i2c_trans, ACTUATORS_ASCTEC_V2_SLAVE_ADDR, 6);
176  break;
177  default:
178  break;
179  }
180 #endif
181 }
182 
184 {
185  data ^= (crc & 0xff);
186  data ^= data << 4;
187 
188  return ((((uint16_t)data << 8) | ((crc>>8)&0xff)) ^ (uint8_t)(data >> 4) ^ ((uint16_t)data << 3));
189 }
actuators_asctec_v2
struct ActuatorsAsctecV2 actuators_asctec_v2
Definition: actuators_asctec_v2_new.c:36
crc_update
static uint16_t crc_update(uint16_t crc, uint8_t data)
Definition: actuators_asctec_v2_new.c:183
uint16_t
unsigned short uint16_t
Definition: types.h:16
i2c_transaction::buf
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
TEST
@ TEST
Definition: actuators_asctec.h:34
I2CTransTx
@ I2CTransTx
transmit only transaction
Definition: i2c.h:47
ActuatorsAsctecV2::cmds
int32_t cmds[4]
Definition: actuators_asctec_v2.h:49
I2CTransFailed
@ I2CTransFailed
transaction failed
Definition: i2c.h:58
NONE
@ NONE
Definition: gvf.h:55
SysTimeTimer
#define SysTimeTimer(_t)
Definition: sys_time.h:219
i2c_transaction::len_w
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
I2CTransSuccess
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
ActuatorsAsctecV2::nb_err
volatile uint32_t nb_err
Definition: actuators_asctec_v2.h:51
USEC_OF_SEC
#define USEC_OF_SEC(sec)
Definition: sys_time.h:210
i2c_transmit
bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction.
Definition: i2c.c:324
actuators_asctec_v2_set
void actuators_asctec_v2_set(void)
Definition: actuators_asctec_v2_new.c:53
ActuatorsAsctecV2::i2c_trans
struct i2c_transaction i2c_trans
Definition: actuators_asctec_v2.h:50
ActuatorsAsctecV2
Definition: actuators_asctec_v2.h:45
ActuatorsAsctecV2::new_addr
enum actuators_asctec_v2_addr new_addr
Definition: actuators_asctec_v2.h:48
sys_time.h
Architecture independent timing functions.
REVERSE
@ REVERSE
Definition: actuators_asctec.h:35
uint8_t
unsigned char uint8_t
Definition: types.h:14
i2c_transaction::status
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
i2c_transaction::slave_addr
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
ActuatorsAsctecV2::cur_addr
enum actuators_asctec_v2_addr cur_addr
Definition: actuators_asctec_v2.h:47
ACTUATORS_ASCTEC_V2_SLAVE_ADDR
#define ACTUATORS_ASCTEC_V2_SLAVE_ADDR
Definition: actuators_asctec_v2_new.c:32
SET_ADDR
@ SET_ADDR
Definition: actuators_asctec.h:36
I2CTransDone
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
actuators_asctec_v2_new.h
FRONT
@ FRONT
Definition: actuators_asctec.h:39
i2c_transaction::type
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
ActuatorsAsctecV2::cmd
enum actuators_asctec_v2_cmd cmd
Definition: actuators_asctec_v2.h:46
i2c.h
i2c_receive
bool i2c_receive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint16_t len)
Submit a read only transaction.
Definition: i2c.c:334
actuators_asctec_v2_init
void actuators_asctec_v2_init(void)
Definition: actuators_asctec_v2_new.c:40
actuators.h