Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
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 #include "mcu_periph/uart.h"
63 #include "messages.h"
65 
66 
68 {
69  static uint8_t send_nr = 0;
70 
71  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);
72 
73  send_nr++;
74  if (send_nr >= ACTUATORS_MKK_V2_NB)
75  send_nr = 0;
76 }
77 
78 
79 
81 // MKK Config
82 
84 
85 // Following 2 structs are known from: http://mikrokopter.de/mikrosvn/FlightCtrl/tags/V0.88n/twimaster.h
86 
87 /*
88 typedef struct
89 {
90  uint8_t revision; // must be BL_revision
91  uint8_t SetMask; // settings mask
92  uint8_t PwmScaling; // maximum value of control pwm, acts like a thrust limit
93  uint8_t CurrentLimit; // current limit in A
94  uint8_t TempLimit; // in °C
95  uint8_t CurrentScaling; // scaling factor for current measurement
96  uint8_t BitConfig; // see defines above
97  uint8_t crc; // checksum
98 } __attribute__((packed)) config_mkk_v2_eeprom_t;
99 
100 extern config_mkk_v2_eeprom_t config_mkk_v2_eeprom;
101 */
102 
104 {
105  uint8_t crc = 0xaa;
106  for(int i=offset; i<(offset+7); i++)
107  {
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)
150  {
152  }
153  else
154  {
155  config_mkk_v2_eeprom.revision = config_mkk_v2.trans.buf[0]; // must be BL_revision
156  config_mkk_v2_eeprom.SetMask = config_mkk_v2.trans.buf[1]; // settings mask
157  config_mkk_v2_eeprom.PwmScaling = config_mkk_v2.trans.buf[2]; // maximum value of control pwm, acts like a thrust limit
158  config_mkk_v2_eeprom.CurrentLimit = config_mkk_v2.trans.buf[3]; // current limit in A
159  config_mkk_v2_eeprom.TempLimit = config_mkk_v2.trans.buf[4]; // in °C
160  config_mkk_v2_eeprom.CurrentScaling = config_mkk_v2.trans.buf[5]; // scaling factor for current measurement
161  config_mkk_v2_eeprom.BitConfig = config_mkk_v2.trans.buf[6]; // see defines above
162  }
163 }
164 
166 {
167  // Do not upload while running
169 
170  // Do not upload bad data:
171  if (config_mkk_v2_eeprom.revision != config_mkk_v2_EEPROM_REVISION)
172  return;
173 
174  // New I2C Write Transaction
178  config_mkk_v2.trans.buf[0] = 0;
180 
181  config_mkk_v2.trans.buf[2] = config_mkk_v2_eeprom.revision;
182  config_mkk_v2.trans.buf[3] = config_mkk_v2_eeprom.SetMask;
183  config_mkk_v2.trans.buf[4] = config_mkk_v2_eeprom.PwmScaling;
184  config_mkk_v2.trans.buf[5] = config_mkk_v2_eeprom.CurrentLimit;
185  config_mkk_v2.trans.buf[6] = config_mkk_v2_eeprom.TempLimit;
186  config_mkk_v2.trans.buf[7] = config_mkk_v2_eeprom.CurrentScaling;
187  config_mkk_v2.trans.buf[8] = config_mkk_v2_eeprom.BitConfig;
189 
190  i2c_submit(&ACTUATORS_MKK_V2_I2C_DEV, &config_mkk_v2.trans);
191 
192 }
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)
Submit a I2C transaction.
Definition: i2c_arch.c:335
void config_mkk_v2_parse_eeprom(void)
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 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
Slave address.
Definition: i2c.h:104
#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.
struct i2c_transaction trans
Definition: config_mkk_v2.h:42
transaction failed
Definition: i2c.h:58
Hardware independent API for actuators (servos, motor controllers).
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
struct config_mkk_v2_struct config_mkk_v2
Definition: config_mkk_v2.c:29
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
unsigned char uint8_t
Definition: types.h:14
void config_mkk_v2_periodic_telemetry(void)
Definition: config_mkk_v2.c:67
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