Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures 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  {
47  switch (config_mkk_v2.trans.status) {
48  case I2CTransFailed:
50  break;
51  case I2CTransSuccess:
52  case I2CTransDone:
55  break;
56  default:
57  return;
58  }
59  }
60 }
61 
62 #ifndef DOWNLINK_DEVICE
63 #define DOWNLINK_DEVICE DOWNLINK_AP_DEVICE
64 #endif
65 #include "mcu_periph/uart.h"
66 #include "messages.h"
68 
69 
71 {
72  static uint8_t send_nr = 0;
73 
74  DOWNLINK_SEND_MKK(DefaultChannel, DefaultDevice, &send_nr, &actuators_mkk_v2.data[send_nr].MaxPWM, &actuators_mkk_v2.data[send_nr].Current, &actuators_mkk_v2.data[send_nr].Temperature);
75 
76  send_nr++;
77  if (send_nr >= ACTUATORS_MKK_V2_NB)
78  send_nr = 0;
79 }
80 
81 
82 
84 // MKK Config
85 
87 
88 // Following 2 structs are known from: http://mikrokopter.de/mikrosvn/FlightCtrl/tags/V0.88n/twimaster.h
89 
90 /*
91 typedef struct
92 {
93  uint8_t revision; // must be BL_revision
94  uint8_t SetMask; // settings mask
95  uint8_t PwmScaling; // maximum value of control pwm, acts like a thrust limit
96  uint8_t CurrentLimit; // current limit in A
97  uint8_t TempLimit; // in °C
98  uint8_t CurrentScaling; // scaling factor for current measurement
99  uint8_t BitConfig; // see defines above
100  uint8_t crc; // checksum
101 } __attribute__((packed)) config_mkk_v2_eeprom_t;
102 
103 extern config_mkk_v2_eeprom_t config_mkk_v2_eeprom;
104 */
105 
107 {
108  uint8_t crc = 0xaa;
109  for(int i=offset; i<(offset+7); i++)
110  {
111  crc += config_mkk_v2.trans.buf[i];
112  }
113  return crc;
114 }
115 
116 
118 
119 
120 #define BL_READMODE_CONFIG 16
121 #define config_mkk_v2_EEPROM_REVISION 2
122 
123 
124 #define RETURN_IF_NOT_KILLMODE() \
125 { \
126  if (!actuators_delay_done) \
127  return; \
128 }
129 
131 {
132  // Activate decoder
134 
135  // Do not read config while running
137 
138  // New I2C Write/Read Transaction
142  config_mkk_v2.trans.buf[0] = 0;
145 
146  i2c_submit(&ACTUATORS_MKK_V2_I2C_DEV, &config_mkk_v2.trans);
147 }
148 
150 {
151  config_mkk_v2_eeprom.crc = config_mkk_v2.trans.buf[7]; // checksum
152  if (config_mkk_v2_crc(0) != config_mkk_v2_eeprom.crc)
153  {
155  }
156  else
157  {
158  config_mkk_v2_eeprom.revision = config_mkk_v2.trans.buf[0]; // must be BL_revision
159  config_mkk_v2_eeprom.SetMask = config_mkk_v2.trans.buf[1]; // settings mask
160  config_mkk_v2_eeprom.PwmScaling = config_mkk_v2.trans.buf[2]; // maximum value of control pwm, acts like a thrust limit
161  config_mkk_v2_eeprom.CurrentLimit = config_mkk_v2.trans.buf[3]; // current limit in A
162  config_mkk_v2_eeprom.TempLimit = config_mkk_v2.trans.buf[4]; // in °C
163  config_mkk_v2_eeprom.CurrentScaling = config_mkk_v2.trans.buf[5]; // scaling factor for current measurement
164  config_mkk_v2_eeprom.BitConfig = config_mkk_v2.trans.buf[6]; // see defines above
165  }
166 }
167 
169 {
170  // Do not upload while running
172 
173  // Do not upload bad data:
174  if (config_mkk_v2_eeprom.revision != config_mkk_v2_EEPROM_REVISION)
175  return;
176 
177  // New I2C Write Transaction
181  config_mkk_v2.trans.buf[0] = 0;
183 
184  config_mkk_v2.trans.buf[2] = config_mkk_v2_eeprom.revision;
185  config_mkk_v2.trans.buf[3] = config_mkk_v2_eeprom.SetMask;
186  config_mkk_v2.trans.buf[4] = config_mkk_v2_eeprom.PwmScaling;
187  config_mkk_v2.trans.buf[5] = config_mkk_v2_eeprom.CurrentLimit;
188  config_mkk_v2.trans.buf[6] = config_mkk_v2_eeprom.TempLimit;
189  config_mkk_v2.trans.buf[7] = config_mkk_v2_eeprom.CurrentScaling;
190  config_mkk_v2.trans.buf[8] = config_mkk_v2_eeprom.BitConfig;
192 
193  i2c_submit(&ACTUATORS_MKK_V2_I2C_DEV, &config_mkk_v2.trans);
194 
195 }
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define RETURN_IF_NOT_KILLMODE()
Actuators driver for Mikrokopter motor controllers.
bool_t i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Definition: i2c_arch.c:333
void config_mkk_v2_parse_eeprom(void)
void config_mkk_v2_read_eeprom(void)
#define BL_READMODE_CONFIG
struct actuators_mkk_v2_telemetry_struct data[ACTUATORS_MKK_V2_NB]
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 slave_addr
Definition: i2c.h:79
#define config_mkk_v2_EEPROM_REVISION
Read Status and Config from MKK (Mikrokopter) BLDC motor controllers.
struct i2c_transaction trans
Definition: config_mkk_v2.h:42
Hardware independent API for actuators (servos, motor controllers).
enum I2CTransactionStatus status
Definition: i2c.h:83
volatile uint8_t buf[I2C_BUF_LEN]
Definition: i2c.h:82
struct config_mkk_v2_struct config_mkk_v2
Definition: config_mkk_v2.c:29
uint8_t len_w
Definition: i2c.h:81
uint16_t len_r
Definition: i2c.h:80
unsigned char uint8_t
Definition: types.h:14
void config_mkk_v2_periodic_telemetry(void)
Definition: config_mkk_v2.c:70
Definition: i2c.h:45
enum I2CTransactionType type
Definition: i2c.h:78
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