Subversion Repositories FlightCtrl

Compare Revisions

Ignore whitespace Rev 1179 → Rev 1180

/branches/V0.72p Code Redesign killagreg/twimaster.c
1,5 → 1,53
/*############################################################################
############################################################################*/
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Copyright (c) 04.2007 Holger Buss
// + Nur für den privaten Gebrauch
// + www.MikroKopter.com
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
// + bzgl. der Nutzungsbedingungen aufzunehmen.
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
// + Verkauf von Luftbildaufnahmen, usw.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
// + eindeutig als Ursprung verlinkt werden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
// + Benutzung auf eigene Gefahr
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
// + mit unserer Zustimmung zulässig
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
// + this list of conditions and the following disclaimer.
// + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
// + from this software without specific prior written permission.
// + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
// + for non-commercial use (directly or indirectly)
// + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
// + with our written permission
// + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
// + clearly linked as origin
// + * porting to systems other than hardware from www.mikrokopter.de is not allowed
// + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// + POSSIBILITY OF SUCH DAMAGE.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
#include <avr/io.h>
#include <avr/interrupt.h>
10,10 → 58,16
#include "analog.h"
 
volatile uint8_t twi_state = 0;
volatile uint8_t motor_write = 0;
volatile uint8_t motor_read = 0;
uint8_t motor_write = 0;
uint8_t motor_read = 0;
volatile uint8_t dac_channel = 0;
volatile uint8_t motor_rx[8];
 
#ifdef USE_QUADRO
uint8_t motor_rx[8];
#else
uint8_t motor_rx[16];
#endif
 
volatile uint16_t I2CTimeout = 100;
 
 
147,11 → 201,13
I2C_WriteByte(0);
}
 
 
/****************************************/
/* I2C ISR */
/****************************************/
 
#ifdef USE_QUADRO
ISR (TWI_vect)
 
{
 
switch (twi_state++) // First i2c_start from SendMotorData()
164,16 → 220,16
switch(motor_write)
{
case 0:
I2C_WriteByte(Motor_Front);
I2C_WriteByte(Motor1);
break;
case 1:
I2C_WriteByte(Motor_Rear);
I2C_WriteByte(Motor2);
break;
case 2:
I2C_WriteByte(Motor_Right);
I2C_WriteByte(Motor3);
break;
case 3:
I2C_WriteByte(Motor_Left);
I2C_WriteByte(Motor4);
break;
}
break;
226,13 → 282,13
switch(dac_channel)
{
case 0:
I2C_WriteByte(AnalogOffsetNick); // 1st byte for Channel A
I2C_WriteByte(DacOffsetGyroNick); // 1st byte for Channel A
break;
case 1:
I2C_WriteByte(AnalogOffsetRoll); // 1st byte for Channel B
I2C_WriteByte(DacOffsetGyroRoll); // 1st byte for Channel B
break;
case 2:
I2C_WriteByte(AnalogOffsetYaw ); // 1st byte for Channel C
I2C_WriteByte(DacOffsetGyroYaw ); // 1st byte for Channel C
break;
}
break;
266,3 → 322,133
motor_read = 0;
}
}
#else // USE_OCTO, USE_OCTO2, USE_OCTO3
ISR (TWI_vect)
{
 
switch (twi_state++) // First i2c_start from SendMotorData()
{
// Master Transmit
case 0: // Send SLA-W
I2C_WriteByte(0x52 + (motor_write * 2) );
break;
case 1: // Send Data to Slave
switch(motor_write)
{
case 0:
I2C_WriteByte(Motor1);
break;
case 1:
I2C_WriteByte(Motor2);
break;
case 2:
I2C_WriteByte(Motor3);
break;
case 3:
I2C_WriteByte(Motor4);
break;
case 5:
I2C_WriteByte(Motor5);
break;
case 6:
I2C_WriteByte(Motor6);
break;
case 7:
I2C_WriteByte(Motor7);
break;
case 8:
I2C_WriteByte(Motor8);
break;
}
break;
case 2: // repeat case 0+1 for all motors
I2C_Stop();
if (motor_write < 7)
{
motor_write++; // jump to next motor
twi_state = 0; // and repeat from state 0
}
else
{ // data to last motor send
motor_write = 0; // reset motor write counter
}
I2C_Start(); // Repeated start -> switch slave or switch Master Transmit -> Master Receive
break;
 
// Master Receive
case 3: // Send SLA-R
I2C_WriteByte(0x53 + (motor_read * 2) );
break;
case 4:
//Transmit 1st byte
I2C_ReceiveByte();
break;
case 5: //Read 1st byte and transmit 2nd Byte
motor_rx[motor_read] = TWDR;
I2C_ReceiveLastByte();
break;
case 6:
//Read 2nd byte
motor_rx[motor_read + 8] = TWDR;
motor_read++;
if (motor_read > 7) motor_read = 0;
I2C_Stop();
twi_state = 0;
I2CTimeout = 10;
break;
 
// Gyro-Offsets
case 7:
I2C_WriteByte(0x98); // Address the DAC
break;
 
case 8:
I2C_WriteByte(0x10 + (dac_channel * 2)); // Select DAC Channel (0x10 = A, 0x12 = B, 0x14 = C)
break;
 
case 9:
switch(dac_channel)
{
case 0:
I2C_WriteByte(DacOffsetGyroNick); // 1st byte for Channel A
break;
case 1:
I2C_WriteByte(DacOffsetGyroRoll); // 1st byte for Channel B
break;
case 2:
I2C_WriteByte(DacOffsetGyroYaw ); // 1st byte for Channel C
break;
}
break;
 
case 10:
I2C_WriteByte(0x80); // 2nd byte for all channels is 0x80
break;
 
case 11:
I2C_Stop();
I2CTimeout = 10;
// repeat case 7...10 until all DAC Channels are updated
if(dac_channel < 2)
{
dac_channel ++; // jump to next channel
twi_state = 7; // and repeat from state 7
I2C_Start(); // start transmission for next channel
}
else
{ // data to last motor send
dac_channel = 0; // reset dac channel counter
twi_state = 0; // reset twi_state
}
break;
 
default:
I2C_Stop();
twi_state = 0;
I2CTimeout = 10;
motor_write = 0;
motor_read = 0;
}
}
#endif // USE_OCTO, USE_OCTO2