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
config_mkk_v2.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Christophe De Wagter
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 
23 #include "config_mkk_v2.h"
24 #include "generated/airframe.h"
25 #include "subsystems/actuators.h"
26 
28 
30 
32 {
35 
37 
38 }
39 
41 
43 {
44  // Read Config
45  if (config_mkk_v2.read_config > 0) {
46  switch (config_mkk_v2.trans.status) {
47  case I2CTransFailed:
49  break;
50  case I2CTransSuccess:
51  case I2CTransDone:
54  break;
55  default:
56  return;
57  }
58  }
59 }
60 
61 #include "mcu_periph/uart.h"
62 #include "pprzlink/messages.h"
64 
65 
67 {
68  static uint8_t send_nr = 0;
69 
70  DOWNLINK_SEND_MKK(DefaultChannel, DefaultDevice, &send_nr, &actuators_mkk_v2.data[send_nr].MaxPWM,
72 
73  send_nr++;
74  if (send_nr >= ACTUATORS_MKK_V2_NB) {
75  send_nr = 0;
76  }
77 }
78 
79 
80 
82 // MKK Config
83 
85 
86 // Following 2 structs are known from: http://mikrokopter.de/mikrosvn/FlightCtrl/tags/V0.88n/twimaster.h
87 
88 /*
89 typedef struct
90 {
91  uint8_t revision; // must be BL_revision
92  uint8_t SetMask; // settings mask
93  uint8_t PwmScaling; // maximum value of control pwm, acts like a thrust limit
94  uint8_t CurrentLimit; // current limit in A
95  uint8_t TempLimit; // in °C
96  uint8_t CurrentScaling; // scaling factor for current measurement
97  uint8_t BitConfig; // see defines above
98  uint8_t crc; // checksum
99 } __attribute__((packed)) config_mkk_v2_eeprom_t;
100 
101 extern config_mkk_v2_eeprom_t config_mkk_v2_eeprom;
102 */
103 
105 {
106  uint8_t crc = 0xaa;
107  for (int i = offset; i < (offset + 7); i++) {
108  crc += config_mkk_v2.trans.buf[i];
109  }
110  return crc;
111 }
112 
113 
115 
116 
117 #define BL_READMODE_CONFIG 16
118 #define config_mkk_v2_EEPROM_REVISION 2
119 
120 
121 #define RETURN_IF_NOT_KILLMODE() \
122  { \
123  if (!actuators_delay_done) \
124  return; \
125  }
126 
128 {
129  // Activate decoder
131 
132  // Do not read config while running
134 
135  // New I2C Write/Read Transaction
139  config_mkk_v2.trans.buf[0] = 0;
142 
143  i2c_submit(&ACTUATORS_MKK_V2_I2C_DEV, &config_mkk_v2.trans);
144 }
145 
147 {
148  config_mkk_v2_eeprom.crc = config_mkk_v2.trans.buf[7]; // checksum
149  if (config_mkk_v2_crc(0) != config_mkk_v2_eeprom.crc) {
151  } else {
152  config_mkk_v2_eeprom.revision = config_mkk_v2.trans.buf[0]; // must be BL_revision
153  config_mkk_v2_eeprom.SetMask = config_mkk_v2.trans.buf[1]; // settings mask
154  config_mkk_v2_eeprom.PwmScaling =
155  config_mkk_v2.trans.buf[2]; // maximum value of control pwm, acts like a thrust limit
156  config_mkk_v2_eeprom.CurrentLimit = config_mkk_v2.trans.buf[3]; // current limit in A
157  config_mkk_v2_eeprom.TempLimit = config_mkk_v2.trans.buf[4]; // in °C
158  config_mkk_v2_eeprom.CurrentScaling = config_mkk_v2.trans.buf[5]; // scaling factor for current measurement
159  config_mkk_v2_eeprom.BitConfig = config_mkk_v2.trans.buf[6]; // see defines above
160  }
161 }
162 
164 {
165  // Do not upload while running
167 
168  // Do not upload bad data:
169  if (config_mkk_v2_eeprom.revision != config_mkk_v2_EEPROM_REVISION) {
170  return;
171  }
172 
173  // New I2C Write Transaction
177  config_mkk_v2.trans.buf[0] = 0;
179 
180  config_mkk_v2.trans.buf[2] = config_mkk_v2_eeprom.revision;
181  config_mkk_v2.trans.buf[3] = config_mkk_v2_eeprom.SetMask;
182  config_mkk_v2.trans.buf[4] = config_mkk_v2_eeprom.PwmScaling;
183  config_mkk_v2.trans.buf[5] = config_mkk_v2_eeprom.CurrentLimit;
184  config_mkk_v2.trans.buf[6] = config_mkk_v2_eeprom.TempLimit;
185  config_mkk_v2.trans.buf[7] = config_mkk_v2_eeprom.CurrentScaling;
186  config_mkk_v2.trans.buf[8] = config_mkk_v2_eeprom.BitConfig;
188 
189  i2c_submit(&ACTUATORS_MKK_V2_I2C_DEV, &config_mkk_v2.trans);
190 
191 }
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define RETURN_IF_NOT_KILLMODE()
Actuators driver for Mikrokopter motor controllers.
void config_mkk_v2_parse_eeprom(void)
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
transaction successfully finished by I2C driver
Definition: i2c.h:57
void config_mkk_v2_read_eeprom(void)
transmit and receive transaction
Definition: i2c.h:49
#define BL_READMODE_CONFIG
struct i2c_transaction trans
Definition: config_mkk_v2.h:41
struct actuators_mkk_v2_struct actuators_mkk_v2
void config_mkk_v2_periodic_read_status(void)
Definition: config_mkk_v2.c:42
void config_mkk_v2_send_eeprom(void)
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
Hardware independent API for actuators (servos, motor controllers).
static const float offset[]
#define config_mkk_v2_EEPROM_REVISION
transaction set to done by user level
Definition: i2c.h:59
Read Status and Config from MKK (Mikrokopter) BLDC motor controllers.
bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
i2c_submit() function
Definition: i2c_arch.c:375
transaction failed
Definition: i2c.h:58
struct actuators_mkk_v2_telemetry_struct data[ACTUATORS_MKK_V2_NB]
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
struct config_mkk_v2_struct config_mkk_v2
Definition: config_mkk_v2.c:29
unsigned char uint8_t
Definition: types.h:14
void config_mkk_v2_periodic_telemetry(void)
Definition: config_mkk_v2.c:66
transmit only transaction
Definition: i2c.h:47
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
void config_mkk_v2_init(void)
Definition: config_mkk_v2.c:31
uint8_t config_mkk_v2_crc(uint8_t offset)
config_mkk_v2_eeprom_t config_mkk_v2_eeprom