Prof Greg Egan
/
UAVXArm-GKE
UAVX Multicopter Flight Controller.
i2c.c@0:62a1c91a859a, 2011-02-18 (annotated)
- Committer:
- gke
- Date:
- Fri Feb 18 22:28:05 2011 +0000
- Revision:
- 0:62a1c91a859a
- Child:
- 1:1e3318a30ddd
First release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gke | 0:62a1c91a859a | 1 | // =============================================================================================== |
gke | 0:62a1c91a859a | 2 | // = UAVXArm Quadrocopter Controller = |
gke | 0:62a1c91a859a | 3 | // = Copyright (c) 2008 by Prof. Greg Egan = |
gke | 0:62a1c91a859a | 4 | // = Original V3.15 Copyright (c) 2007 Ing. Wolfgang Mahringer = |
gke | 0:62a1c91a859a | 5 | // = http://code.google.com/p/uavp-mods/ http://uavp.ch = |
gke | 0:62a1c91a859a | 6 | // =============================================================================================== |
gke | 0:62a1c91a859a | 7 | |
gke | 0:62a1c91a859a | 8 | // This is part of UAVXArm. |
gke | 0:62a1c91a859a | 9 | |
gke | 0:62a1c91a859a | 10 | // UAVXArm is free software: you can redistribute it and/or modify it under the terms of the GNU |
gke | 0:62a1c91a859a | 11 | // General Public License as published by the Free Software Foundation, either version 3 of the |
gke | 0:62a1c91a859a | 12 | // License, or (at your option) any later version. |
gke | 0:62a1c91a859a | 13 | |
gke | 0:62a1c91a859a | 14 | // UAVXArm is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without |
gke | 0:62a1c91a859a | 15 | // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
gke | 0:62a1c91a859a | 16 | // See the GNU General Public License for more details. |
gke | 0:62a1c91a859a | 17 | |
gke | 0:62a1c91a859a | 18 | // You should have received a copy of the GNU General Public License along with this program. |
gke | 0:62a1c91a859a | 19 | // If not, see http://www.gnu.org/licenses/ |
gke | 0:62a1c91a859a | 20 | |
gke | 0:62a1c91a859a | 21 | #include "UAVXArm.h" |
gke | 0:62a1c91a859a | 22 | |
gke | 0:62a1c91a859a | 23 | uint32 MinI2CRate = I2C_MAX_RATE_HZ; |
gke | 0:62a1c91a859a | 24 | |
gke | 0:62a1c91a859a | 25 | void TrackMinI2CRate(uint32 r) { |
gke | 0:62a1c91a859a | 26 | if ( r < MinI2CRate ) |
gke | 0:62a1c91a859a | 27 | MinI2CRate = r; |
gke | 0:62a1c91a859a | 28 | } // TrackMinI2CRate |
gke | 0:62a1c91a859a | 29 | |
gke | 0:62a1c91a859a | 30 | void ShowI2CDeviceName(uint8 d) |
gke | 0:62a1c91a859a | 31 | { |
gke | 0:62a1c91a859a | 32 | TxChar(' '); |
gke | 0:62a1c91a859a | 33 | switch ( d ) { |
gke | 0:62a1c91a859a | 34 | case ADXL345_ID: TxString("ADXL345 Acc"); break; |
gke | 0:62a1c91a859a | 35 | case ITG3200_ID: TxString("ITG3200 Gyro"); break; |
gke | 0:62a1c91a859a | 36 | case HMC5843_ID: TxString("HMC5843 Magnetometer"); break; |
gke | 0:62a1c91a859a | 37 | case HMC6352_ID: TxString("HMC6352 Compass"); break; |
gke | 0:62a1c91a859a | 38 | case ADS7823_ID: TxString("ADS7823 ADC"); break; |
gke | 0:62a1c91a859a | 39 | case MCP4725_ID: TxString("MCP4725 DAC"); break; |
gke | 0:62a1c91a859a | 40 | case BOSCH_ID: TxString("Bosch Baro"); break; |
gke | 0:62a1c91a859a | 41 | case TMP100_ID: TxString("TMP100 Temp"); break; |
gke | 0:62a1c91a859a | 42 | case PCA9551_ID: TxString("PCA9551 LED");break; |
gke | 0:62a1c91a859a | 43 | case LISL_ID: TxString("LIS3L Acc"); break; |
gke | 0:62a1c91a859a | 44 | default: break; |
gke | 0:62a1c91a859a | 45 | } // switch |
gke | 0:62a1c91a859a | 46 | TxChar(' '); |
gke | 0:62a1c91a859a | 47 | |
gke | 0:62a1c91a859a | 48 | } // ShowI2CDeviceName |
gke | 0:62a1c91a859a | 49 | |
gke | 0:62a1c91a859a | 50 | uint8 ScanI2CBus(void) { |
gke | 0:62a1c91a859a | 51 | uint8 s; |
gke | 0:62a1c91a859a | 52 | uint8 d; |
gke | 0:62a1c91a859a | 53 | |
gke | 0:62a1c91a859a | 54 | d = 0; |
gke | 0:62a1c91a859a | 55 | |
gke | 0:62a1c91a859a | 56 | TxString("Buss 0\r\n"); |
gke | 0:62a1c91a859a | 57 | for ( s = 0x10 ; s <= 0xf6 ; s += 2 ) { |
gke | 0:62a1c91a859a | 58 | I2C0.start(); |
gke | 0:62a1c91a859a | 59 | if ( I2C0.write(s) == I2C_ACK ) { |
gke | 0:62a1c91a859a | 60 | d++; |
gke | 0:62a1c91a859a | 61 | TxString("\t0x"); |
gke | 0:62a1c91a859a | 62 | TxValH(s); |
gke | 0:62a1c91a859a | 63 | ShowI2CDeviceName( s ); |
gke | 0:62a1c91a859a | 64 | TxNextLine(); |
gke | 0:62a1c91a859a | 65 | } |
gke | 0:62a1c91a859a | 66 | I2C0.stop(); |
gke | 0:62a1c91a859a | 67 | |
gke | 0:62a1c91a859a | 68 | Delay1mS(2); |
gke | 0:62a1c91a859a | 69 | } |
gke | 0:62a1c91a859a | 70 | |
gke | 0:62a1c91a859a | 71 | /* |
gke | 0:62a1c91a859a | 72 | TxString("Buss 1\r\n"); |
gke | 0:62a1c91a859a | 73 | for ( s = 0x10 ; s <= 0xf6 ; s += 2 ) { |
gke | 0:62a1c91a859a | 74 | I2C1.start(); |
gke | 0:62a1c91a859a | 75 | if ( I2C1.write(s) == I2C_ACK ) { |
gke | 0:62a1c91a859a | 76 | d++; |
gke | 0:62a1c91a859a | 77 | TxString("\t0x"); |
gke | 0:62a1c91a859a | 78 | TxValH(s); |
gke | 0:62a1c91a859a | 79 | TxNextLine(); |
gke | 0:62a1c91a859a | 80 | } |
gke | 0:62a1c91a859a | 81 | I2C1.stop(); |
gke | 0:62a1c91a859a | 82 | |
gke | 0:62a1c91a859a | 83 | Delay1mS(2); |
gke | 0:62a1c91a859a | 84 | } |
gke | 0:62a1c91a859a | 85 | */ |
gke | 0:62a1c91a859a | 86 | |
gke | 0:62a1c91a859a | 87 | PCA9551Test(); |
gke | 0:62a1c91a859a | 88 | |
gke | 0:62a1c91a859a | 89 | return(d); |
gke | 0:62a1c91a859a | 90 | } // ScanI2CBus |
gke | 0:62a1c91a859a | 91 | |
gke | 0:62a1c91a859a | 92 | void ProgramSlaveAddress(uint8 addr) { |
gke | 0:62a1c91a859a | 93 | static uint8 s; |
gke | 0:62a1c91a859a | 94 | |
gke | 0:62a1c91a859a | 95 | for (s = 0x10 ; s < 0xf0 ; s += 2 ) { |
gke | 0:62a1c91a859a | 96 | I2CESC.start(); |
gke | 0:62a1c91a859a | 97 | if ( I2CESC.read(s) == I2C_ACK ) |
gke | 0:62a1c91a859a | 98 | if ( s == addr ) { // ESC is already programmed OK |
gke | 0:62a1c91a859a | 99 | I2CESC.stop(); |
gke | 0:62a1c91a859a | 100 | TxString("\tESC at SLA 0x"); |
gke | 0:62a1c91a859a | 101 | TxValH(addr); |
gke | 0:62a1c91a859a | 102 | TxString(" is already programmed OK\r\n"); |
gke | 0:62a1c91a859a | 103 | return; |
gke | 0:62a1c91a859a | 104 | } else { |
gke | 0:62a1c91a859a | 105 | if ( I2CESC.read(0x87) == I2C_ACK ) // select register 0x07 |
gke | 0:62a1c91a859a | 106 | if ( I2CESC.write( addr ) == I2C_ACK ) { // new slave address |
gke | 0:62a1c91a859a | 107 | I2CESC.stop(); |
gke | 0:62a1c91a859a | 108 | TxString("\tESC at SLA 0x"); |
gke | 0:62a1c91a859a | 109 | TxValH(s); |
gke | 0:62a1c91a859a | 110 | TxString(" reprogrammed to SLA 0x"); |
gke | 0:62a1c91a859a | 111 | TxValH(addr); |
gke | 0:62a1c91a859a | 112 | TxNextLine(); |
gke | 0:62a1c91a859a | 113 | return; |
gke | 0:62a1c91a859a | 114 | } |
gke | 0:62a1c91a859a | 115 | } |
gke | 0:62a1c91a859a | 116 | I2CESC.stop(); |
gke | 0:62a1c91a859a | 117 | } |
gke | 0:62a1c91a859a | 118 | TxString("\tESC at SLA 0x"); |
gke | 0:62a1c91a859a | 119 | TxValH(addr); |
gke | 0:62a1c91a859a | 120 | TxString(" no response - check cabling and pullup resistors!\r\n"); |
gke | 0:62a1c91a859a | 121 | } // ProgramSlaveAddress |
gke | 0:62a1c91a859a | 122 | |
gke | 0:62a1c91a859a | 123 | boolean CheckESCBus(void) { |
gke | 0:62a1c91a859a | 124 | return ( true ); |
gke | 0:62a1c91a859a | 125 | } // CheckESCBus |
gke | 0:62a1c91a859a | 126 | |
gke | 0:62a1c91a859a | 127 | void ConfigureESCs(void) { |
gke | 0:62a1c91a859a | 128 | int8 m; |
gke | 0:62a1c91a859a | 129 | |
gke | 0:62a1c91a859a | 130 | if ( (int8)P[ESCType] == ESCYGEI2C ) { |
gke | 0:62a1c91a859a | 131 | TxString("\r\nProgram YGE ESCs\r\n"); |
gke | 0:62a1c91a859a | 132 | for ( m = 0 ; m < NoOfI2CESCOutputs ; m++ ) { |
gke | 0:62a1c91a859a | 133 | TxString("Connect ONLY "); |
gke | 0:62a1c91a859a | 134 | switch ( m ) { |
gke | 0:62a1c91a859a | 135 | #ifdef HEXACOPTER |
gke | 0:62a1c91a859a | 136 | not yet! |
gke | 0:62a1c91a859a | 137 | #else |
gke | 0:62a1c91a859a | 138 | case 0 : |
gke | 0:62a1c91a859a | 139 | TxString("Front"); |
gke | 0:62a1c91a859a | 140 | break; |
gke | 0:62a1c91a859a | 141 | case 1 : |
gke | 0:62a1c91a859a | 142 | TxString("Back"); |
gke | 0:62a1c91a859a | 143 | break; |
gke | 0:62a1c91a859a | 144 | case 2 : |
gke | 0:62a1c91a859a | 145 | TxString("Right"); |
gke | 0:62a1c91a859a | 146 | break; |
gke | 0:62a1c91a859a | 147 | case 3 : |
gke | 0:62a1c91a859a | 148 | TxString("Left"); |
gke | 0:62a1c91a859a | 149 | break; |
gke | 0:62a1c91a859a | 150 | #endif // HEXACOPTER |
gke | 0:62a1c91a859a | 151 | } |
gke | 0:62a1c91a859a | 152 | TxString(" ESC, then press any key \r\n"); |
gke | 0:62a1c91a859a | 153 | while ( PollRxChar() != 'x' ); // UAVPSet uses 'x' for any key button |
gke | 0:62a1c91a859a | 154 | // TxString("\r\n"); |
gke | 0:62a1c91a859a | 155 | ProgramSlaveAddress( 0x62 + ( m*2 )); |
gke | 0:62a1c91a859a | 156 | } |
gke | 0:62a1c91a859a | 157 | TxString("\r\nConnect ALL ESCs and power-cycle the Quadrocopter\r\n"); |
gke | 0:62a1c91a859a | 158 | } else |
gke | 0:62a1c91a859a | 159 | TxString("\r\nYGEI2C not selected as ESC?\r\n"); |
gke | 0:62a1c91a859a | 160 | } // ConfigureESCs |
gke | 0:62a1c91a859a | 161 | |
gke | 0:62a1c91a859a | 162 | |
gke | 0:62a1c91a859a | 163 |