2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
5 * @addtogroup PIOS_I2C_ESC Code for controlling I2C based ESCs
6 * @brief Deals with the hardware interface to the magnetometers
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
11 * @brief HMC5843 Magnetic Sensor Functions from AHRS
12 * @see The GNU Public License (GPL) Version 3
14 ******************************************************************************
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #ifdef PIOS_INCLUDE_I2C_ESC
36 /* Known i2c ESC addresses */
37 #define MK_I2C_ADDR 0x29
38 #define ASTEC4_I2C_ADDR 0x02
40 bool PIOS_SetMKSpeed(uint8_t motornum
, uint8_t speed
);
42 uint8_t base_address
= MK_I2C_ADDR
;
43 uint8_t valid_motors
= 0;
44 bool PIOS_I2C_ESC_Config()
46 base_address
= MK_I2C_ADDR
;
48 for (uint8_t i
= 0; i
< 4; i
++) {
49 if (PIOS_SetMKSpeed(i
, 0)) {
50 valid_motors
|= (1 << i
);
57 bool PIOS_I2C_ESC_SetSpeed(uint8_t speed
[4])
59 /*bool success = true;
60 for(uint8_t i = 0; i < 4; i++) {
61 //if(valid_motors & (1 << i))
62 success &= PIOS_SetMKSpeed(i, speed[i]);
66 const struct pios_i2c_txn txn_list
[] = {
69 .addr
= MK_I2C_ADDR
+ 0,
70 .rw
= PIOS_I2C_TXN_WRITE
,
71 .len
= sizeof(speed
[0]),
76 .addr
= MK_I2C_ADDR
+ 1,
77 .rw
= PIOS_I2C_TXN_WRITE
,
78 .len
= sizeof(speed
[1]),
83 .addr
= MK_I2C_ADDR
+ 2,
84 .rw
= PIOS_I2C_TXN_WRITE
,
85 .len
= sizeof(speed
[2]),
90 .addr
= MK_I2C_ADDR
+ 3,
91 .rw
= PIOS_I2C_TXN_WRITE
,
92 .len
= sizeof(speed
[3]),
97 return PIOS_I2C_Transfer(PIOS_I2C_ESC_ADAPTER
, txn_list
, NELEMENTS(txn_list
));
100 bool PIOS_SetMKSpeed(uint8_t motornum
, uint8_t speed
)
102 static uint8_t speeds
[4] = { 0, 0, 0, 0 };
108 if (speeds
[motornum
] == speed
) {
112 const struct pios_i2c_txn txn_list
[] = {
115 .addr
= MK_I2C_ADDR
+ motornum
,
116 .rw
= PIOS_I2C_TXN_WRITE
,
117 .len
= sizeof(speed
),
122 return PIOS_I2C_Transfer(PIOS_I2C_ESC_ADAPTER
, txn_list
, NELEMENTS(txn_list
));
125 bool PIOS_SetAstec4Address(uint8_t new_address
)
127 if ((new_address
< 0) || (new_address
> 4)) {
131 uint8_t data
[4] = { 250, 0, new_address
, 230 + new_address
};
133 const struct pios_i2c_txn txn_list
[] = {
136 .addr
= ASTEC4_I2C_ADDR
,
137 .rw
= PIOS_I2C_TXN_WRITE
,
143 return PIOS_I2C_Transfer(PIOS_I2C_ESC_ADAPTER
, txn_list
, NELEMENTS(txn_list
));
146 bool PIOS_SetAstec4Speed(uint8_t motornum
, uint8_t speed
)
148 static uint8_t speeds
[5] = { 0, 0, 0, 0 };
150 if ((motornum
< 0) || (motornum
>= 4)) {
154 speeds
[motornum
] = speed
;
160 /* Write in chunks of four */
161 speeds
[4] = 0xAA + speeds
[0] + speeds
[1] + speeds
[2] + speeds
[3];
163 const struct pios_i2c_txn txn_list
[] = {
166 .addr
= ASTEC4_I2C_ADDR
,
167 .rw
= PIOS_I2C_TXN_WRITE
,
168 .len
= sizeof(speeds
),
173 return PIOS_I2C_Transfer(PIOS_I2C_ESC_ADAPTER
, txn_list
, NELEMENTS(txn_list
));
176 #endif /* PIOS_INCLUDE_I2C_ESC */