Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 469 → Rev 470

/trunk/usb_pwr.c
File deleted
/trunk/usbinc/usb_desc.h
File deleted
/trunk/usbinc/usb_prop.h
File deleted
/trunk/usbinc/usb_conf.h
File deleted
/trunk/usbinc/usb_pwr.h
File deleted
/trunk/i2c.c
File deleted
/trunk/i2c.h
File deleted
/trunk/usb.c
File deleted
/trunk/usb.h
File deleted
/trunk/usb_endp.c
File deleted
/trunk/USB Software Driver/MK_VirtualComPort.inf
File deleted
/trunk/usb_desc.c
File deleted
/trunk/usb_istr.c
File deleted
/trunk/usb_prop.c
File deleted
/trunk/usblibrary/src/usb_init.c
File deleted
/trunk/usblibrary/src/usb_core.c
File deleted
/trunk/usblibrary/src/usb_int.c
File deleted
/trunk/usblibrary/src/usb_mem.c
File deleted
/trunk/usblibrary/src/usb_regs.c
File deleted
/trunk/usblibrary/inc/usb_mem.h
File deleted
/trunk/usblibrary/inc/usb_def.h
File deleted
/trunk/usblibrary/inc/usb_regs.h
File deleted
/trunk/usblibrary/inc/usb_type.h
File deleted
/trunk/usblibrary/inc/usb_init.h
File deleted
/trunk/usblibrary/inc/usb_lib.h
File deleted
/trunk/usblibrary/inc/usb_core.h
File deleted
/trunk/usblibrary/inc/usb_int.h
File deleted
/trunk/GPS.c
74,7 → 74,7
#include "timer1.h"
#include "spi_slave.h"
#include "waypoints.h"
#include "i2c.h"
#include "i2c1.h"
 
 
#define M_PI_180 (M_PI / 180.0f)
/trunk/Navi-Ctrl.Uv2
8,23 → 8,18
Group (Startup Code)
Group (Str9-Lib)
Group (Documentation)
Group (USB-Lib)
 
File 1,1,<.\main.c><main.c>
File 1,1,<.\ramfunc.c><ramfunc.c>
File 1,1,<.\menu.c><menu.c>
File 1,1,<.\spi_slave.c><spi_slave.c>
File 1,1,<.\i2c.c><i2c.c>
File 1,1,<.\usb.c><usb.c>
File 1,1,<.\fat16.c><fat16.c>
File 1,1,<.\i2c0.c><i2c0.c>
File 1,1,<.\i2c1.c><i2c1.c>
File 1,1,<.\sdc.c><sdc.c>
File 1,1,<.\settings.c><settings.c>
File 1,1,<.\ftphelper.c><ftphelper.c>
File 1,1,<.\ssc.c><ssc.c>
File 1,1,<.\usb_desc.c><usb_desc.c>
File 1,1,<.\usb_endp.c><usb_endp.c>
File 1,1,<.\usb_istr.c><usb_istr.c>
File 1,1,<.\usb_prop.c><usb_prop.c>
File 1,1,<.\usb_pwr.c><usb_pwr.c>
File 1,1,<.\led.c><led.c>
File 1,1,<.\crc16.c><crc16.c>
File 1,1,<.\ubx.c><ubx.c>
49,14 → 44,13
File 1,1,<.\eeprom.c><eeprom.c>
File 1,1,<.\ncmag.c><ncmag.c>
File 1,1,<.\mymath.c><mymath.c>
File 1,1,<.\ftphelper.c><ftphelper.c>
File 2,5,<.\ramfunc.h><ramfunc.h>
File 2,5,<.\main.h><main.h>
File 2,5,<.\menu.h><menu.h>
File 2,5,<.\settings.h><settings.h>
File 2,5,<.\usb.h><usb.h>
File 2,5,<.\i2c0.h><i2c0.h>
File 2,5,<.\i2c1.h><i2c1.h>
File 2,5,<.\spi_slave.h><spi_slave.h>
File 2,5,<.\i2c.h><i2c.h>
File 2,5,<.\sdc.h><sdc.h>
File 2,5,<.\ssc.h><ssc.h>
File 2,5,<.\fat16.h><fat16.h>
90,10 → 84,6
File 2,5,<.\eeprom.h><eeprom.h>
File 2,5,<.\ncmag.h><ncmag.h>
File 2,5,<.\mymath.h><mymath.h>
File 2,5,<.\usbinc\usb_pwr.h><usb_pwr.h>
File 2,5,<.\usbinc\usb_conf.h><usb_conf.h>
File 2,5,<.\usbinc\usb_desc.h><usb_desc.h>
File 2,5,<.\usbinc\usb_prop.h><usb_prop.h>
File 2,5,<.\ftphelper.h><ftphelper.h>
File 3,2,<.\startup912.s><startup912.s>
File 4,1,<.\libstr91x\src\91x_scu.c><91x_scu.c>
107,11 → 97,6
File 4,1,<.\libstr91x\src\91x_wiu.c><91x_wiu.c>
File 4,1,<.\libstr91x\src\91x_adc.c><91x_adc.c>
File 4,1,<.\libstr91x\src\91x_it.c><91x_it.c>
File 6,1,<.\usblibrary\src\usb_regs.c><usb_regs.c>
File 6,1,<.\usblibrary\src\usb_core.c><usb_core.c>
File 6,1,<.\usblibrary\src\usb_int.c><usb_int.c>
File 6,1,<.\usblibrary\src\usb_init.c><usb_init.c>
File 6,1,<.\usblibrary\src\usb_mem.c><usb_mem.c>
 
 
Options 1,0,0 // Target 'Navi-Ctrl'
/trunk/eeprom.c
56,7 → 56,7
#include <string.h>
#include <stdio.h>
#include "91x_lib.h"
#include "i2c.h"
#include "i2c1.h"
#include "uart1.h"
#include "timer1.h"
#include "eeprom.h"
101,7 → 101,7
retval = EEPROM_ERROR_OUT_OF_ADDRESS_RANGE;
return(retval);
}
if((DataLen+2) > I2C_BUFFER_LEN)
if((DataLen+2) > I2C1_BUFFER_LEN)
{
retval = EEPROM_I2C_BUFFER_OVERRUN;
return(retval);
108,23 → 108,23
}
do
{
if(!I2C_LockBuffer(I2C_IDLE_TIMEOUT)) return EEPROM_ERROR_I2C_IDLE_TIMEOUT;
if(!I2C1_LockBuffer(I2C_IDLE_TIMEOUT)) return EEPROM_ERROR_I2C_IDLE_TIMEOUT;
// buffer is now locked
TxBytes = 0;
// transmitt address
I2C_Buffer[TxBytes++] = (u8)(0x00FF & (Address>>8));
I2C_Buffer[TxBytes++] = (u8)(0x00FF & Address);
I2C1_Buffer[TxBytes++] = (u8)(0x00FF & (Address>>8));
I2C1_Buffer[TxBytes++] = (u8)(0x00FF & Address);
if(Direction == EEPROM_WRITE)
{ // copy data to i2c transfer buffer
for(i = 0; i<DataLen;i++)
{
I2C_Buffer[TxBytes++] = pData[i];
I2C1_Buffer[TxBytes++] = pData[i];
}
// prepare pointer to rx data
EEPROM_pData = 0;
EEPROM_DataLen = 0;
// start transmission
if(!I2C_Transmission(EEPROM_I2C_ADDRESS, TxBytes, 0, 0))
if(!I2C1_Transmission(EEPROM_I2C_ADDRESS, TxBytes, 0, 0))
{
return(retval);
}
135,15 → 135,15
EEPROM_pData = pData;
EEPROM_DataLen = DataLen;
// start transmission
if(!I2C_Transmission(EEPROM_I2C_ADDRESS, TxBytes, &EEPROM_RxDataHandler, DataLen))
if(!I2C1_Transmission(EEPROM_I2C_ADDRESS, TxBytes, &EEPROM_RxDataHandler, DataLen))
{
return(retval);
}
}
//wait for end of this transfer
if(I2C_WaitForEndOfTransmission(I2C_TRANSFER_TIMEOUT))
if(I2C1_WaitForEndOfTransmission(I2C_TRANSFER_TIMEOUT))
{
if(I2C_Error == I2C_ERROR_NONE) return(EEPROM_SUCCESS);
if(I2C1_Error == I2C_ERROR_NONE) return(EEPROM_SUCCESS);
else retval = EEPROM_DATA_TRANSFER_INCOMPLETE;
}
else// i2c transfer timed out
190,7 → 190,7
 
while(DataLen > 0)
{
if(DataLen > I2C_BUFFER_LEN) RxLen = I2C_BUFFER_LEN;
if(DataLen > I2C1_BUFFER_LEN) RxLen = I2C1_BUFFER_LEN;
else RxLen = DataLen;
retval = EEPROM_Transfer(EEPROM_READ, Address+AdrOffset, &(pData[AdrOffset]), RxLen);
/trunk/i2c0.c
56,7 → 56,7
#include <string.h>
#include "91x_lib.h"
#include "i2c0.h"
#include "i2c.h"
#include "i2c1.h"
#include "uart1.h"
#include "timer1.h"
#include "config.h"
71,7 → 71,7
// number of bytes to receive
volatile u8 I2C0_RxBufferSize;
// the transfer buffer
volatile u8 I2C0_Buffer[I2C0_Buffer_LEN];
volatile u8 I2C0_Buffer[I2C0_BUFFER_LEN];
// the transfer direction
volatile u8 I2C0_Direction;
// the slave address
419,7 → 419,7
u8 retval = 0;
if(I2C0_State == I2C_STATE_BUFFBUSY)
{
if((RxBytes > I2C0_Buffer_LEN) || (TxBytes > I2C0_Buffer_LEN))
if((RxBytes > I2C0_BUFFER_LEN) || (TxBytes > I2C0_BUFFER_LEN))
{
I2C0_State = I2C_STATE_IDLE;
return(retval);
427,8 → 427,8
I2C0_RxBufferSize = RxBytes;
I2C0_TxBufferSize = TxBytes;
// set direction to master transmitter
if( (I2C0_TxBufferSize > 0) && (I2C0_TxBufferSize < I2C0_Buffer_LEN) ) I2C0_Direction = I2C_MODE_TRANSMITTER;
else if (( I2C0_RxBufferSize > 0 ) && (I2C0_RxBufferSize < I2C0_Buffer_LEN) ) I2C0_Direction = I2C_MODE_RECEIVER;
if( (I2C0_TxBufferSize > 0) && (I2C0_TxBufferSize < I2C0_BUFFER_LEN) ) I2C0_Direction = I2C_MODE_TRANSMITTER;
else if (( I2C0_RxBufferSize > 0 ) && (I2C0_RxBufferSize < I2C0_BUFFER_LEN) ) I2C0_Direction = I2C_MODE_RECEIVER;
else // nothing to send or receive
{
I2C0_State = I2C_STATE_IDLE;
/trunk/i2c0.h
1,10 → 1,9
#ifndef __I2C0_H
#define __I2C0_H
 
#include "i2c.h"
#include "i2c1.h"
 
extern volatile u32 I2C0_Timeout;
 
 
// current I2C state
extern volatile u8 I2C0_State;
// the last I2C error
13,7 → 12,7
#define I2C0_TIMEOUT 500 // 500 ms
 
// define the size of the rx/tx buffer
#define I2C0_Buffer_LEN 100
#define I2C0_BUFFER_LEN 100
// transfer buffer should be filled after a successful
// I2C_LockBuffer(...) and before a start of transmission
// initiated by I2C_Transmission(...).
23,18 → 22,15
 
void I2C0_Deinit(void);
 
// the pointer to the rxbuffer handler function
// called by the IRQ routine after all bytes are recieved from slave
 
// Initiate i2c transmission
// A transmission sends first TxBytes from I2C_Buffer0 to slave
// and then RxBytes are read from slave to I2C_Buffer0
// A transmission sends first TxBytes from I2C0_Buffer to slave
// and then RxBytes are read from slave to I2C0_Buffer
// replacing the byte that have been sent.
// Then the RxHandler function is called to handle the result.
// This function returns imediatly after a start condition.
// returns 1 if a transmission has been started, otherwise 0
u8 I2C0_Transmission(u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes);
// try to allocate the I2C_Buffer0 within the timeout limit
// try to allocate the I2C0_Buffer within the timeout limit
// returns 1 on success
u8 I2C0_LockBuffer(u32 timeout);
// wait until transmission progess is finished or timeout
/trunk/i2c1.c
0,0 → 1,448
/*#######################################################################################*/
/* !!! THIS IS NOT FREE SOFTWARE !!! */
/*#######################################################################################*/
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + www.MikroKopter.com
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Software Nutzungsbedingungen (english version: see below)
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand
// + des Mitverschuldens offen.
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
// + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Software LICENSING TERMS
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
// + The Software may only be used with the Licensor's products.
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
// + agreement shall be the property of the Licensor.
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
// + features that can be used to identify the program may not be altered or defaced by the customer.
// + The customer shall be responsible for taking reasonable precautions
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
// + #### END OF LICENSING TERMS ####
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <string.h>
#include "91x_lib.h"
#include "i2c1.h"
#include "uart1.h"
#include "timer1.h"
#include "config.h"
#include "led.h"
 
 
volatile u8 I2C1_State = I2C_STATE_OFF; // only one byte, because of sync by nesting irqs
volatile u8 I2C1_Error = I2C_ERROR_NOACK; // only one byte!
 
// number of bytes to send
volatile u8 I2C1_TxBufferSize;
// number of bytes to receive
volatile u8 I2C1_RxBufferSize;
// the transfer buffer
volatile u8 I2C1_Buffer[I2C1_BUFFER_LEN];
// the transfer direction
volatile u8 I2C1_Direction;
// the slave address
volatile u8 I2C1_SlaveAddress = 0x00;
// function pointer to process the received bytes
I2C_pRxHandler_t I2C1_pRxHandler = NULL;
// goblal timeout
volatile u32 I2C1_Timeout = 0;
 
//--------------------------------------------------------------
void I2C1_Init(void)
{
I2C_InitTypeDef I2C_Struct;
GPIO_InitTypeDef GPIO_InitStructure;
I2C1_State = I2C_STATE_OFF;
 
UART1_PutString("\r\n I2C1 init...");
// enable Port 2 peripherie
SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
// disable a reset state
SCU_APBPeriphReset(__GPIO2, DISABLE);
 
// free a busy bus
 
// At switch on I2C devices can get in a state where they
// are still waiting for a command due to all the bus lines bouncing
// around at startup have started clocking data into the device(s).
// Enable the ports as open collector port outputs
// and clock out at least 9 SCL pulses, then generate a stop
// condition and then leave the clock line high.
 
// configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT to normal port operation
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
GPIO_Init(GPIO2, &GPIO_InitStructure);
 
u8 i;
u32 delay;
// set SCL high and then SDA to low (start condition)
GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET);
delay = SetDelay(1);
while (!CheckDelay(delay));
GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_RESET);
// toggle SCL at least 10 times from high to low to high
for(i = 0; i < 10; i++)
{
delay = SetDelay(1);
while (!CheckDelay(delay));
 
GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET);
delay = SetDelay(1);
while (!CheckDelay(delay));
GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET);
}
delay = SetDelay(1);
while (!CheckDelay(delay));
// create stop condition setting SDA HIGH when SCL is HIGH
GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_SET);
 
 
// reconfigure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT
GPIO_Init(GPIO2, &GPIO_InitStructure);
 
// enable I2C peripherie
SCU_APBPeriphClockConfig(__I2C1,ENABLE);
// reset I2C peripherie
SCU_APBPeriphReset(__I2C1,ENABLE);
SCU_APBPeriphReset(__I2C1,DISABLE);
 
I2C_DeInit(I2C1);
I2C_StructInit(&I2C_Struct);
I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;
I2C_Struct.I2C_Ack = I2C_Ack_Enable;
I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK;
I2C_Struct.I2C_OwnAddress = 0x00;
I2C_Init(I2C1, &I2C_Struct);
 
// empty rx and tx buffer counters
I2C1_TxBufferSize = 0;
I2C1_RxBufferSize = 0;
 
I2C_Cmd(I2C1, ENABLE);
I2C_ITConfig(I2C1, ENABLE);
 
VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1);
 
I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
I2C_GenerateSTOP(I2C1, ENABLE);
I2C1_State = I2C_STATE_IDLE;
 
// start some dummy transmissions cycles
// to get the irq routine to work
for(i=0;i<10;i++)
{
I2C1_State = I2C_STATE_BUFFBUSY;
I2C1_Transmission(0,1,0,1);
if(I2C1_WaitForEndOfTransmission(10)) break;
UART1_Putchar('.');
}
UART1_PutString("ok");
}
 
 
//--------------------------------------------------------------
void I2C1_Deinit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
UART1_PutString("\r\n I2C1 deinit...");
I2C_GenerateStart(I2C1, DISABLE);
I2C_GenerateSTOP(I2C1, ENABLE);
VIC_ITCmd(I2C1_ITLine, DISABLE);
I2C1_State = I2C_STATE_OFF;
I2C_ITConfig(I2C1, DISABLE);
I2C_Cmd(I2C1, DISABLE);
I2C_DeInit(I2C1);
SCU_APBPeriphClockConfig(__I2C1, DISABLE);
 
// set ports to input
SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull;
GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
GPIO_Init(GPIO2, &GPIO_InitStructure);
 
// empty rx and tx buffer
I2C1_TxBufferSize = 0;
I2C1_RxBufferSize = 0;
 
I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
 
UART1_PutString("ok");
}
 
//--------------------------------------------------------------
void I2C1_IRQHandler(void)
{
static u8 Rx_Idx = 0, Tx_Idx = 0;
u16 status;
 
//IENABLE; // do not enable IRQ nesting for I2C!!!!
// detemine I2C State
status = I2C_GetLastEvent(I2C1);
 
if(status & (I2C_FLAG_AF|I2C_FLAG_BERR)) // if an acknowledge failure or bus error occured
{ // Set and subsequently clear the STOP bit while BTF is set.
while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET)
{
I2C_GenerateSTOP (I2C1, ENABLE); // free the bus
I2C_GenerateSTOP (I2C1, DISABLE); // free the bus
}
I2C1_State = I2C_STATE_IDLE;
I2C1_Error = I2C_ERROR_NOACK;
VIC_ITCmd(I2C1_ITLine, DISABLE);
return;
}
else
{ // depending on current i2c state
switch(status)
{
// the start condition was initiated on the bus
case I2C_EVENT_MASTER_MODE_SELECT:
// update current bus state variable
// jump to rx state if there is nothing to send
switch(I2C1_Direction)
{
case I2C_MODE_TRANSMITTER:
I2C1_State = I2C_STATE_TX_PROGRESS;
break;
 
case I2C_MODE_RECEIVER:
if (I2C1_RxBufferSize == 0) // nothing to send?
{
I2C_GenerateSTOP (I2C1, ENABLE);
VIC_ITCmd(I2C1_ITLine, DISABLE);
I2C1_State = I2C_STATE_IDLE;
I2C1_Error = I2C_ERROR_NONE;
return;
}
else
{
I2C1_State = I2C_STATE_RX_PROGRESS;
}
break;
 
default: // invalid direction
I2C_GenerateSTOP (I2C1, ENABLE);
VIC_ITCmd(I2C1_ITLine, DISABLE);
I2C1_State = I2C_STATE_IDLE;
I2C1_Error = I2C_ERROR_UNKNOWN;
return;
}
// enable acknowledge
I2C_AcknowledgeConfig (I2C1, ENABLE);
// send address/direction byte on the bus
I2C_Send7bitAddress(I2C1, I2C1_SlaveAddress, I2C1_Direction);
break;
 
// the address byte was send
case I2C_EVENT_MASTER_MODE_SELECTED:
// Clear EV6 by set again the PE bit
I2C_Cmd(I2C1, ENABLE);
switch(I2C1_State)
{
case I2C_STATE_TX_PROGRESS:
// send 1st data byte
Tx_Idx = 0;
I2C_SendData(I2C1, I2C1_Buffer[Tx_Idx]);
Tx_Idx++;
// reset timeout
I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset
break;
 
case I2C_STATE_RX_PROGRESS:
Rx_Idx = 0;
// disable acknoledge if only one byte has to be read
if(I2C1_RxBufferSize == 1) I2C_AcknowledgeConfig (I2C1, DISABLE);
break;
 
default: // unknown I2C state
// should never happen
I2C_GenerateSTOP (I2C1, ENABLE);
VIC_ITCmd(I2C1_ITLine, DISABLE);
I2C1_State = I2C_STATE_IDLE;
I2C1_Error = I2C_ERROR_UNKNOWN;
return;
break;
}
break;
 
// the master has transmitted a byte and slave has been acknowledged
case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
 
// some bytes have to be transmitted
if(Tx_Idx < I2C1_TxBufferSize)
{
I2C_SendData(I2C1, I2C1_Buffer[Tx_Idx]);
Tx_Idx++;
}
else // last byte was send
{
// generate stop or repeated start condition
if (I2C1_RxBufferSize > 0) // is any answer byte expected?
{
I2C1_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition
I2C_GenerateStart(I2C1, ENABLE); // initiate repeated start condition on the bus
}
else
{ // stop communication
I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus
VIC_ITCmd(I2C1_ITLine, DISABLE);
I2C1_State = I2C_STATE_IDLE; // ready for new actions
I2C1_Error = I2C_ERROR_NONE;
}
}
break;
 
// the master has received a byte from the slave
case I2C_EVENT_MASTER_BYTE_RECEIVED:
// some bytes have to be received
if ( Rx_Idx+1 < I2C1_RxBufferSize)
{ // copy received byte from the data register to the rx-buffer
I2C1_Buffer[Rx_Idx] = I2C_ReceiveData(I2C1);
}
else // if the last byte was received
{
// generate a STOP condition on the bus before reading data register
I2C_GenerateSTOP(I2C1, ENABLE);
I2C1_Buffer[Rx_Idx] = I2C_ReceiveData(I2C1);
// call the rx handler function to process recieved data
if(I2C1_pRxHandler != NULL) (*I2C1_pRxHandler)((u8*)I2C1_Buffer, I2C1_RxBufferSize);
I2C1_Timeout = SetDelay(I2C1_TIMEOUT);
DebugOut.Analog[15]++;
VIC_ITCmd(I2C1_ITLine, DISABLE);
I2C1_State = I2C_STATE_IDLE;
I2C1_Error = I2C_ERROR_NONE;
return;
}
Rx_Idx++;
// if the 2nd last byte was received disable acknowledge for the last one
if ( (Rx_Idx + 1) == I2C1_RxBufferSize )
{
I2C_AcknowledgeConfig(I2C1, DISABLE);
}
break;
 
default:// unknown event
// should never happen
I2C_GenerateSTOP (I2C1, ENABLE);
VIC_ITCmd(I2C1_ITLine, DISABLE);
I2C1_State = I2C_STATE_IDLE;
I2C1_Error = I2C_ERROR_UNKNOWN;
break;
}
}
//IDISABLE; // do not enable IRQ nesting for I2C!!!!
VIC1->VAR = 0xFF; // write any value to VIC1 Vector address register
}
 
// ----------------------------------------------------------------------------------------
// wait for end of transmission
u8 I2C1_WaitForEndOfTransmission(u32 timeout)
{
u32 time = SetDelay(timeout);
while(I2C1_State != I2C_STATE_IDLE)
{
if(CheckDelay(time)) return(0);
}
return(1);
}
 
// ----------------------------------------------------------------------------------------
// try to get access to the transfer buffer within a timeout limit
// returs 1 on success and 0 on error/timeout
u8 I2C1_LockBuffer(u32 timeout)
{
if(I2C1_WaitForEndOfTransmission(timeout))
{
I2C1_State = I2C_STATE_BUFFBUSY;
I2C1_Error = I2C_ERROR_UNKNOWN;
return(1);
}
else return(0);
}
// ----------------------------------------------------------------------------------------
// initate an i2c transmission
u8 I2C1_Transmission(u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes)
{
u8 retval = 0;
if(I2C1_State == I2C_STATE_BUFFBUSY)
{
if((RxBytes > I2C1_BUFFER_LEN) || (TxBytes > I2C1_BUFFER_LEN))
{
I2C1_State = I2C_STATE_IDLE;
return(retval);
}
I2C1_RxBufferSize = RxBytes;
I2C1_TxBufferSize = TxBytes;
// set direction to master transmitter
if( (I2C1_TxBufferSize > 0) && (I2C1_TxBufferSize < I2C1_BUFFER_LEN) ) I2C1_Direction = I2C_MODE_TRANSMITTER;
else if (( I2C1_RxBufferSize > 0 ) && (I2C1_RxBufferSize < I2C1_BUFFER_LEN) ) I2C1_Direction = I2C_MODE_RECEIVER;
else // nothing to send or receive
{
I2C1_State = I2C_STATE_IDLE;
I2C1_Error = I2C_ERROR_NONE;
I2C1_TxBufferSize = 0;
I2C1_RxBufferSize = 0;
return(retval);
}
// update slave address and rx data handler function pointer
I2C1_SlaveAddress = SlaveAddr;
I2C1_pRxHandler = pRxHandler;
// test on busy flag and clear it
I2C_ClearFlag(I2C0, I2C_FLAG_BUSY);
// enable I2C IRQ
VIC_ITCmd(I2C1_ITLine, ENABLE);
// initiate start condition on the bus
I2C_GenerateStart(I2C1, ENABLE);
retval = 1;
}
return(retval);
}
/trunk/i2c1.h
0,0 → 1,57
#ifndef __I2C_H
#define __I2C_H
 
// I2C states
#define I2C_STATE_UNDEF 0
#define I2C_STATE_IDLE 1
#define I2C_STATE_BUFFBUSY 2
#define I2C_STATE_TX_PENDING 3
#define I2C_STATE_TX_PROGRESS 4
#define I2C_STATE_RX_PENDING 5
#define I2C_STATE_RX_PROGRESS 6
#define I2C_STATE_OFF 7
// I2C Errors
#define I2C_ERROR_NONE 0
#define I2C_ERROR_UNKNOWN 1
#define I2C_ERROR_NOACK 2
 
// the pointer to the rxbuffer handler function
// called by the IRQ routine after all bytes are recieved from slave
typedef void (*I2C_pRxHandler_t) (u8* pRxBuffer, u8 RxBufferSize);
 
extern volatile u32 I2C1_Timeout;
// current I2C state
extern volatile u8 I2C1_State;
// the last I2C error
extern volatile u8 I2C1_Error;
 
#define I2C1_TIMEOUT 500 // 500 ms
 
// define the size of the rx/tx buffer
#define I2C1_BUFFER_LEN 100
// transfer buffer should be filled after a successful
// I2C_LockBuffer(...) and before a start of transmission
// initiated by I2C_Transmission(...).
extern volatile u8 I2C1_Buffer[];
 
void I2C1_Init(void);
 
void I2C1_Deinit(void);
 
// Initiate i2c transmission
// A transmission sends first TxBytes from I2C1_Buffer to slave
// and then RxBytes are read from slave to I2C1_Buffer
// replacing the byte that have been sent.
// Then the RxHandler function is called to handle the result.
// This function returns imediatly after a start condition.
// returns 1 if a transmission has been started, otherwise 0
u8 I2C1_Transmission(u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes);
// try to allocate the I2C1_Buffer within the timeout limit
// returns 1 on success
u8 I2C1_LockBuffer(u32 timeout);
// wait until transmission progess is finished or timeout
// returns 1 if no timeout occurs
u8 I2C1_WaitForEndOfTransmission(u32 timeout);
 
#endif // I2C_H
 
/trunk/libstr91x/src/91x_it.c
221,11 → 221,6
*******************************************************************************/
void USBHP_IRQHandler(void)
{
IENABLE;
 
CTR_HP();
 
IDISABLE;
}
/*******************************************************************************
* Function Name : USBLP_IRQHandler
236,11 → 231,6
*******************************************************************************/
void USBLP_IRQHandler(void)
{
IENABLE;
 
USB_Istr();
 
IDISABLE;
}
/*******************************************************************************
* Function Name : SCU_IRQHandler
/trunk/main.c
62,7 → 62,8
#include "uart1.h"
#include "uart2.h"
#include "gps.h"
#include "i2c.h"
#include "i2c0.h"
#include "i2c1.h"
#include "compass.h"
#include "ncmag.h"
#include "timer1.h"
70,7 → 71,6
#include "analog.h"
#include "spi_slave.h"
#include "fat16.h"
#include "usb.h"
#include "sdc.h"
#include "logging.h"
#include "params.h"
495,9 → 495,6
// initialize the debug UART1
UART1_Init();
UART1_PutString("\r\n---------------------------------------------");
// initialize usb
// to do
//USB_ConfigInit(); // disable because of unexpected side effects with I2C0
// initialize timer 2 for servo outputs
//TIMER2_Init();
// initialize UART2 to FLIGHTCTRL
/trunk/mk3mag.c
57,7 → 57,7
#include <string.h>
#include "91x_lib.h"
#include "mk3mag.h"
#include "i2c.h"
#include "i2c1.h"
#include "i2c0.h"
#include "timer1.h"
#include "led.h"
174,7 → 174,7
void MK3MAG_SendCommand(u8 command)
{
// try to catch the I2C buffer
if(I2C_LockBuffer(0))
if(I2C1_LockBuffer(0))
{
u16 TxBytes = 0;
u16 RxBytes = 0;
181,7 → 181,7
I2C_pRxHandler_t pRxHandlerFunc = NULL;
// update current command id
I2C_Buffer[TxBytes++] = command;
I2C1_Buffer[TxBytes++] = command;
 
// set pointers to data area with respect to the command id
switch (command)
193,7 → 193,7
case MK3MAG_CMD_WRITE_CAL:
RxBytes = sizeof(MK3MAG_ReadCal)+1;
pRxHandlerFunc = &MK3MAG_UpdateCalibration;
memcpy((u8*)I2C_Buffer+1, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal));
memcpy((u8*)I2C1_Buffer+1, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal));
TxBytes += sizeof(MK3MAG_WriteCal);
break;
case MK3MAG_CMD_READ_MAGVECT:
206,10 → 206,10
break;
}
// update packet checksum
I2C_Buffer[TxBytes] = MK3MAG_CalcCRC((u8*)I2C_Buffer, TxBytes);
I2C1_Buffer[TxBytes] = MK3MAG_CalcCRC((u8*)I2C1_Buffer, TxBytes);
TxBytes++;
// initiate I2C transmission
I2C_Transmission(MK3MAG_SLAVE_ADDRESS, TxBytes, pRxHandlerFunc, RxBytes);
I2C1_Transmission(MK3MAG_SLAVE_ADDRESS, TxBytes, pRxHandlerFunc, RxBytes);
} // EOF I2C_State == I2C_IDLE
}
 
273,7 → 273,7
u8 msg[64];
u16 MinCaclibration = 500;
 
if( (I2C_State == I2C_STATE_OFF) || !MK3MAG_Present ) return;
if( (I2C1_State == I2C_STATE_OFF) || !MK3MAG_Present ) return;
if(CheckDelay(TimerUpdate))
{
/trunk/mkprotocol.c
211,7 → 211,6
// if 2nd byte is an 'R' start bootloader
if(pRxBuff->pData[2] == 'R' && !(FC.StatusFlags & FC_STATUS_MOTOR_RUN)) // not if the motors are running)
{
PowerOff();
VIC_DeInit();
Execute_Bootloader(); // Reset-Commando - Bootloader starten
}
/trunk/ncmag.c
58,7 → 58,7
#include <string.h>
#include "91x_lib.h"
#include "ncmag.h"
#include "i2c.h"
#include "i2c1.h"
#include "i2c0.h"
 
#include "timer1.h"
755,8 → 755,8
static u32 TimerUpdate = 0;
static s8 send_config = 0;
u32 delay = 20;
 
if( (I2C_State == I2C_STATE_OFF) || !NCMAG_Present )
// todo State Handling for both busses !!
if( (I2C1_State == I2C_STATE_OFF) || !NCMAG_Present )
{
Compass_Heading = -1;
DebugOut.Analog[14]++; // count I2C error
928,11 → 928,11
}
else
{
I2C_WaitForEndOfTransmissionFunc = &I2C_WaitForEndOfTransmission;
I2C_LockBufferFunc = &I2C_LockBuffer;
I2C_TransmissionFunc = &I2C_Transmission;
I2C_BufferPnt = I2C_Buffer;
I2C_ErrorPnt = &I2C_Error;
I2C_WaitForEndOfTransmissionFunc = &I2C1_WaitForEndOfTransmission;
I2C_LockBufferFunc = &I2C1_LockBuffer;
I2C_TransmissionFunc = &I2C1_Transmission;
I2C_BufferPnt = I2C1_Buffer;
I2C_ErrorPnt = &I2C1_Error;
}
}
 
/trunk/ncmag.h
2,7 → 2,7
#define __NCMAG_H
 
#include "compass.h"
#include "i2c.h"
#include "i2c1.h"
 
extern s16vec_t ACC_Vector;
extern volatile s16vec_t MagRawVector;
/trunk/uart1.c
54,16 → 54,16
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
 
#include "91x_lib.h"
#include "main.h"
#include "math.h"
#include "config.h"
#include "menu.h"
#include "GPS.h"
#include "i2c.h"
#include "i2c1.h"
#include "uart0.h"
#include "uart1.h"
#include "uart2.h"