0,0 → 1,449 |
/*#######################################################################################*/ |
/* !!! 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 "i2c0.h" |
#include "i2c.h" |
#include "uart1.h" |
#include "timer1.h" |
#include "config.h" |
#include "led.h" |
|
|
volatile u8 I2C0_State = I2C_STATE_OFF; // only one byte, because of sync by nesting irqs |
volatile u8 I2C0_Error = I2C_ERROR_NOACK; // only one byte! |
|
// number of bytes to send |
volatile u8 I2C0_TxBufferSize; |
// number of bytes to receive |
volatile u8 I2C0_RxBufferSize; |
// the transfer buffer |
volatile u8 I2C0_Buffer[I2C0_Buffer_LEN]; |
// the transfer direction |
volatile u8 I2C0_Direction; |
// the slave address |
volatile u8 I2C0_SlaveAddress = 0x00; |
// function pointer to process the received bytes |
I2C_pRxHandler_t I2C0_pRxHandler = NULL; |
// goblal timeout |
volatile u32 I2C0_Timeout = 0; |
|
//-------------------------------------------------------------- |
void I2C0_Init(void) |
{ |
I2C_InitTypeDef I2C_Struct; |
GPIO_InitTypeDef GPIO_InitStructure; |
|
I2C0_State = I2C_STATE_OFF; |
|
UART1_PutString("\r\n I2C0 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->I2C0_CLKOUT and P2.3->I2C0_DOUT to normal port operation |
GPIO_StructInit(&GPIO_InitStructure); |
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0| GPIO_Pin_1; |
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_0, Bit_SET); |
delay = SetDelay(1); |
while (!CheckDelay(delay)); |
GPIO_WriteBit(GPIO2, GPIO_Pin_1, 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_0, Bit_RESET); |
delay = SetDelay(1); |
while (!CheckDelay(delay)); |
GPIO_WriteBit(GPIO2, GPIO_Pin_0, Bit_SET); |
} |
delay = SetDelay(1); |
while (!CheckDelay(delay)); |
// create stop condition setting SDA HIGH when SCL is HIGH |
GPIO_WriteBit(GPIO2, GPIO_Pin_1, Bit_SET); |
|
|
// reconfigure P2.2->I2C0_CLKOUT and P2.3->I2C0_DOUT |
GPIO_StructInit(&GPIO_InitStructure); |
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; |
GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable; |
GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C0_CLKOUT, I2C0_DOUT |
GPIO_Init(GPIO2, &GPIO_InitStructure); |
|
// enable I2C peripherie |
SCU_APBPeriphClockConfig(__I2C0,ENABLE); |
// reset I2C peripherie |
SCU_APBPeriphReset(__I2C0,ENABLE); |
SCU_APBPeriphReset(__I2C0,DISABLE); |
|
I2C_DeInit(I2C0); |
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(I2C0, &I2C_Struct); |
|
// empty rx and tx buffer counters |
I2C0_TxBufferSize = 0; |
I2C0_RxBufferSize = 0; |
|
I2C_Cmd(I2C0, ENABLE); |
I2C_ITConfig(I2C0, ENABLE); |
|
VIC_Config(I2C0_ITLine, VIC_IRQ , PRIORITY_I2C0); |
|
I2C0_Timeout = SetDelay(10*I2C0_Timeout); |
I2C_GenerateSTOP(I2C0, ENABLE); |
I2C0_State = I2C_STATE_IDLE; |
|
// start some dummy transmissions cycles |
// to get the irq routine to work |
for(i=0;i<10;i++) |
{ |
I2C0_State = I2C_STATE_BUFFBUSY; |
I2C0_Transmission(0,1,0,1); |
if(I2C0_WaitForEndOfTransmission(10)) break; |
UART1_Putchar('.'); |
} |
UART1_PutString("ok"); |
} |
|
|
//-------------------------------------------------------------- |
void I2C0_Deinit(void) |
{ |
GPIO_InitTypeDef GPIO_InitStructure; |
UART1_PutString("\r\n I2C deinit..."); |
I2C_GenerateStart(I2C0, DISABLE); |
I2C_GenerateSTOP(I2C0, ENABLE); |
VIC_ITCmd(I2C0_ITLine, DISABLE); |
I2C0_State = I2C_STATE_OFF; |
I2C_ITConfig(I2C0, DISABLE); |
I2C_Cmd(I2C0, DISABLE); |
I2C_DeInit(I2C0); |
SCU_APBPeriphClockConfig(__I2C0, 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_0 | GPIO_Pin_1; |
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 |
I2C0_TxBufferSize = 0; |
I2C0_RxBufferSize = 0; |
|
I2C0_Timeout = SetDelay(10*I2C0_Timeout); |
|
UART1_PutString("ok"); |
} |
|
//-------------------------------------------------------------- |
void I2C0_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(I2C0); |
|
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 (I2C0, I2C_FLAG_BTF) != RESET) |
{ |
I2C_GenerateSTOP (I2C0, ENABLE); // free the bus |
I2C_GenerateSTOP (I2C0, DISABLE); // free the bus |
} |
I2C0_State = I2C_STATE_IDLE; |
I2C0_Error = I2C_ERROR_NOACK; |
VIC_ITCmd(I2C0_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(I2C0_Direction) |
{ |
case I2C_MODE_TRANSMITTER: |
I2C0_State = I2C_STATE_TX_PROGRESS; |
break; |
|
case I2C_MODE_RECEIVER: |
if (I2C0_RxBufferSize == 0) // nothing to send? |
{ |
I2C_GenerateSTOP (I2C0, ENABLE); |
VIC_ITCmd(I2C0_ITLine, DISABLE); |
I2C0_State = I2C_STATE_IDLE; |
I2C0_Error = I2C_ERROR_NONE; |
return; |
} |
else |
{ |
I2C0_State = I2C_STATE_RX_PROGRESS; |
} |
break; |
|
default: // invalid direction |
I2C_GenerateSTOP (I2C0, ENABLE); |
VIC_ITCmd(I2C0_ITLine, DISABLE); |
I2C0_State = I2C_STATE_IDLE; |
I2C0_Error = I2C_ERROR_UNKNOWN; |
return; |
} |
// enable acknowledge |
I2C_AcknowledgeConfig (I2C0, ENABLE); |
// send address/direction byte on the bus |
I2C_Send7bitAddress(I2C0, I2C0_SlaveAddress, I2C0_Direction); |
break; |
|
// the address byte was send |
case I2C_EVENT_MASTER_MODE_SELECTED: |
// Clear EV6 by set again the PE bit |
I2C_Cmd(I2C0, ENABLE); |
switch(I2C0_State) |
{ |
case I2C_STATE_TX_PROGRESS: |
// send 1st data byte |
Tx_Idx = 0; |
I2C_SendData(I2C0, I2C0_Buffer[Tx_Idx]); |
Tx_Idx++; |
// reset timeout |
I2C0_Timeout = SetDelay(I2C0_Timeout); // after inactivity the I2C0 bus will be reset |
break; |
|
case I2C_STATE_RX_PROGRESS: |
Rx_Idx = 0; |
// disable acknoledge if only one byte has to be read |
if(I2C0_RxBufferSize == 1) I2C_AcknowledgeConfig (I2C0, DISABLE); |
break; |
|
default: // unknown I2C state |
// should never happen |
I2C_GenerateSTOP (I2C0, ENABLE); |
VIC_ITCmd(I2C0_ITLine, DISABLE); |
I2C0_State = I2C_STATE_IDLE; |
I2C0_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 < I2C0_TxBufferSize) |
{ |
I2C_SendData(I2C0, I2C0_Buffer[Tx_Idx]); |
Tx_Idx++; |
} |
else // last byte was send |
{ |
// generate stop or repeated start condition |
if (I2C0_RxBufferSize > 0) // is any answer byte expected? |
{ |
I2C0_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition |
I2C_GenerateStart(I2C0, ENABLE); // initiate repeated start condition on the bus |
} |
else |
{ // stop communication |
I2C_GenerateSTOP(I2C0, ENABLE); // generate stop condition to free the bus |
VIC_ITCmd(I2C0_ITLine, DISABLE); |
I2C0_State = I2C_STATE_IDLE; // ready for new actions |
I2C0_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 < I2C0_RxBufferSize) |
{ // copy received byte from the data register to the rx-buffer |
I2C0_Buffer[Rx_Idx] = I2C_ReceiveData(I2C0); |
} |
else // if the last byte was received |
{ |
// generate a STOP condition on the bus before reading data register |
I2C_GenerateSTOP(I2C0, ENABLE); |
I2C0_Buffer[Rx_Idx] = I2C_ReceiveData(I2C0); |
// call the rx handler function to process recieved data |
if(I2C0_pRxHandler != NULL) (*I2C0_pRxHandler)((u8*)I2C0_Buffer, I2C0_RxBufferSize); |
I2C0_Timeout = SetDelay(I2C0_Timeout); |
DebugOut.Analog[15]++; |
VIC_ITCmd(I2C0_ITLine, DISABLE); |
I2C0_State = I2C_STATE_IDLE; |
I2C0_Error = I2C_ERROR_NONE; |
return; |
} |
Rx_Idx++; |
// if the 2nd last byte was received disable acknowledge for the last one |
if ( (Rx_Idx + 1) == I2C0_RxBufferSize ) |
{ |
I2C_AcknowledgeConfig(I2C0, DISABLE); |
} |
break; |
|
default:// unknown event |
// should never happen |
I2C_GenerateSTOP (I2C0, ENABLE); |
VIC_ITCmd(I2C0_ITLine, DISABLE); |
I2C0_State = I2C_STATE_IDLE; |
I2C0_Error = I2C_ERROR_UNKNOWN; |
break; |
} |
} |
//IDISABLE; // do not enable IRQ nesting for I2C!!!! |
VIC1->VAR = 0xFF; // write any value to VIC0 Vector address register |
} |
|
// ---------------------------------------------------------------------------------------- |
// wait for end of transmission |
u8 I2C0_WaitForEndOfTransmission(u32 timeout) |
{ |
u32 time = SetDelay(timeout); |
while(I2C0_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 I2C0_LockBuffer(u32 timeout) |
{ |
if(I2C0_WaitForEndOfTransmission(timeout)) |
{ |
I2C0_State = I2C_STATE_BUFFBUSY; |
I2C0_Error = I2C_ERROR_UNKNOWN; |
return(1); |
} |
else return(0); |
} |
// ---------------------------------------------------------------------------------------- |
// initate an i2c transmission |
u8 I2C0_Transmission(u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes) |
{ |
u8 retval = 0; |
if(I2C0_State == I2C_STATE_BUFFBUSY) |
{ |
if((RxBytes > I2C0_Buffer_LEN) || (TxBytes > I2C0_Buffer_LEN)) |
{ |
I2C0_State = I2C_STATE_IDLE; |
return(retval); |
} |
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; |
else // nothing to send or receive |
{ |
I2C0_State = I2C_STATE_IDLE; |
I2C0_Error = I2C_ERROR_NONE; |
I2C0_TxBufferSize = 0; |
I2C0_RxBufferSize = 0; |
return(retval); |
} |
// update slave address and rx data handler funbction pointer |
I2C0_SlaveAddress = SlaveAddr; |
I2C0_pRxHandler = pRxHandler; |
// test on busy flag and clear it |
I2C_CheckEvent( I2C0, I2C_FLAG_BUSY ); |
// enable I2C IRQ |
VIC_ITCmd(I2C0_ITLine, ENABLE); |
// initiate start condition on the bus |
I2C_GenerateStart(I2C0, ENABLE); |
retval = 1; |
} |
return(retval); |
} |