/*#######################################################################################*/
/* !!! 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 "i2c.h"
#include "uart1.h"
#include "timer1.h"
#include "config.h"
#include "led.h"
// the transfer buffer
u8 I2C0_Buffer
[I2C_BUFFER_LEN
];
u8 I2C1_Buffer
[I2C_BUFFER_LEN
];
volatile I2C_Bus_t I2C0_Bus
;
volatile I2C_Bus_t I2C1_Bus
;
// Retourns pointer to data structure of the selected bus
volatile I2C_Bus_t
* I2CBus
(I2C_TypeDef
* I2Cx
)
{
volatile I2C_Bus_t
*pBus
= NULL
;
if(I2Cx
== I2C0
) pBus
= &I2C0_Bus
;
if(I2Cx
== I2C1
) pBus
= &I2C1_Bus
;
return(pBus
);
}
//--------------------------------------------------------------
void I2CBus_Init
(I2C_TypeDef
* I2Cx
)
{
volatile I2C_Bus_t
*pBus
= NULL
;
I2C_InitTypeDef I2C_Struct
;
GPIO_InitTypeDef GPIO_InitStructure
;
u8 SCL_Pin
= 0;
u8 SDA_Pin
= 0;
u32 SCL_Clock
= 0;
u32 APBPeriph
= 0;
u8 VIC_Priority
= 0;
if (I2Cx
== I2C0
)
{
UART1_PutString
("\r\n I2C0 init...");
SCL_Pin
= GPIO_Pin_0
;
SDA_Pin
= GPIO_Pin_1
;
SCL_Clock
= I2C0_CLOCK
;
APBPeriph
= __I2C0
;
VIC_Priority
= PRIORITY_I2C0
;
pBus
= &I2C0_Bus
;
pBus
->pData
= I2C0_Buffer
;
pBus
->VIC_Source
= I2C0_ITLine
;
}
if (I2Cx
== I2C1
)
{
UART1_PutString
("\r\n I2C1 init...");
SCL_Pin
= GPIO_Pin_2
;
SDA_Pin
= GPIO_Pin_3
;
SCL_Clock
= I2C1_CLOCK
;
APBPeriph
= __I2C1
;
VIC_Priority
= PRIORITY_I2C1
;
pBus
= &I2C1_Bus
;
pBus
->pData
= I2C1_Buffer
;
pBus
->VIC_Source
= I2C1_ITLine
;
}
if(pBus
== NULL
) return;
pBus
->State
= I2C_STATE_UNDEF
;
pBus
->Error
= I2C_ERROR_UNKNOWN
;
pBus
->Timeout
= 0;
pBus
->TxBufferSize
= 0;
pBus
->RxBufferSize
= 0;
pBus
->Direction
= 0;
pBus
->SlaveAddr
= 0;
pBus
->pRxHandler
= NULL
;
// 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 I2C_CLKOUT and I2C_DOUT to normal port operation
GPIO_StructInit
(&GPIO_InitStructure
);
GPIO_InitStructure.
GPIO_Direction = GPIO_PinOutput
;
GPIO_InitStructure.
GPIO_Pin = SCL_Pin
| SDA_Pin
;
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
, SCL_Pin
, Bit_SET
);
delay
= SetDelay
(1);
while (!CheckDelay
(delay
));
GPIO_WriteBit
(GPIO2
, SDA_Pin
, 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
, SCL_Pin
, Bit_RESET
);
delay
= SetDelay
(1);
while (!CheckDelay
(delay
));
GPIO_WriteBit
(GPIO2
, SCL_Pin
, Bit_SET
);
}
delay
= SetDelay
(1);
while (!CheckDelay
(delay
));
// create stop condition setting SDA HIGH when SCL is HIGH
GPIO_WriteBit
(GPIO2
, SDA_Pin
, Bit_SET
);
// reconfigure I2C_CLKOUT and I2C_DOUT
GPIO_StructInit
(&GPIO_InitStructure
);
GPIO_InitStructure.
GPIO_Direction = GPIO_PinOutput
;
GPIO_InitStructure.
GPIO_Pin = SCL_Pin
| SDA_Pin
;
GPIO_InitStructure.
GPIO_Type = GPIO_Type_OpenCollector
;
GPIO_InitStructure.
GPIO_IPInputConnected = GPIO_IPInputConnected_Enable
;
GPIO_InitStructure.
GPIO_Alternate = GPIO_OutputAlt2
; //I2C_CLKOUT, I2C_DOUT
GPIO_Init
(GPIO2
, &GPIO_InitStructure
);
// enable I2C peripherie
SCU_APBPeriphClockConfig
(APBPeriph
, ENABLE
);
// reset I2C peripherie
SCU_APBPeriphReset
(APBPeriph
, ENABLE
);
SCU_APBPeriphReset
(APBPeriph
, DISABLE
);
I2C_DeInit
(I2Cx
);
I2C_StructInit
(&I2C_Struct
);
I2C_Struct.
I2C_GeneralCall = I2C_GeneralCall_Disable
;
I2C_Struct.
I2C_Ack = I2C_Ack_Enable
;
I2C_Struct.
I2C_CLKSpeed = SCL_Clock
;
I2C_Struct.
I2C_OwnAddress = 0x00;
I2C_Init
(I2Cx
, &I2C_Struct
);
I2C_Cmd
(I2Cx
, ENABLE
);
I2C_ITConfig
(I2Cx
, ENABLE
);
VIC_Config
(pBus
->VIC_Source
, VIC_IRQ
, VIC_Priority
);
pBus
->Timeout
= SetDelay
(2*I2C_TIMEOUT
);
I2C_GenerateSTOP
(I2Cx
, ENABLE
);
pBus
->State
= I2C_STATE_IDLE
;
// start some dummy transmissions cycles
// to get the irq routine to work
for(i
= 0; i
< 10; i
++)
{
pBus
->State
= I2C_STATE_BUFFBUSY
;
I2CBus_Transmission
(I2Cx
, 0, NULL
, 1, 0, 0); // transfer 1 byte in the isr
if(I2CBus_WaitForEndOfTransmission
(I2Cx
, 10)) break;
UART1_Putchar
('.');
}
UART1_PutString
("ok");
}
//--------------------------------------------------------------
void I2CBus_Deinit
(I2C_TypeDef
* I2Cx
)
{
volatile I2C_Bus_t
*pBus
= NULL
;
GPIO_InitTypeDef GPIO_InitStructure
;
u32 APBPeriph
= 0;
u16 VIC_Source
= 0;
u8 SCL_Pin
= 0;
u8 SDA_Pin
= 0;
if (I2Cx
== I2C0
)
{
UART1_PutString
("\r\n I2C0 deinit...");
SCL_Pin
= GPIO_Pin_0
;
SDA_Pin
= GPIO_Pin_1
;
APBPeriph
= __I2C0
;
VIC_Source
= I2C0_ITLine
;
pBus
= &I2C0_Bus
;
}
if (I2Cx
== I2C1
)
{
UART1_PutString
("\r\n I2C1 deinit...");
SCL_Pin
= GPIO_Pin_2
;
SDA_Pin
= GPIO_Pin_3
;
APBPeriph
= __I2C1
;
VIC_Source
= I2C1_ITLine
;
pBus
= &I2C1_Bus
;
}
if(pBus
== NULL
) return;
I2C_GenerateStart
(I2Cx
, DISABLE
);
I2C_GenerateSTOP
(I2Cx
, ENABLE
);
VIC_ITCmd
(VIC_Source
, DISABLE
);
pBus
->State
= I2C_STATE_UNDEF
;
I2C_ITConfig
(I2Cx
, DISABLE
);
I2C_Cmd
(I2Cx
, DISABLE
);
I2C_DeInit
(I2Cx
);
SCU_APBPeriphClockConfig
(APBPeriph
, DISABLE
);
// set ports to input
SCU_APBPeriphClockConfig
(__GPIO2
, ENABLE
);
GPIO_StructInit
(&GPIO_InitStructure
);
GPIO_InitStructure.
GPIO_Direction = GPIO_PinInput
;
GPIO_InitStructure.
GPIO_Pin = SCL_Pin
| SDA_Pin
;
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
pBus
->TxBufferSize
= 0;
pBus
->RxBufferSize
= 0;
pBus
->Timeout
= SetDelay
(2*I2C_TIMEOUT
);
UART1_PutString
("ok");
}
//--------------------------------------------------------------
void I2C0_IRQHandler
(void)
{
static u8 Rx_Idx
= 0, Tx_Idx
= 0;
u16 status
;
u16 timeout
= 500;
//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
if(--timeout
== 0)
{
DebugOut.
Analog[14]++; // count I2C error
break;
}
}
I2C0_Bus.
State = I2C_STATE_IDLE
;
I2C0_Bus.
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_Bus.
Direction)
{
case I2C_MODE_TRANSMITTER
:
I2C0_Bus.
State = I2C_STATE_TX_PROGRESS
;
break;
case I2C_MODE_RECEIVER
:
if (I2C0_Bus.
RxBufferSize == 0) // nothing to send?
{
I2C_GenerateSTOP
(I2C0
, ENABLE
);
VIC_ITCmd
(I2C0_ITLine
, DISABLE
);
I2C0_Bus.
State = I2C_STATE_IDLE
;
I2C1_Bus.
Error = I2C_ERROR_NONE
;
return;
}
else
{
I2C0_Bus.
State = I2C_STATE_RX_PROGRESS
;
}
break;
default: // invalid direction
I2C_GenerateSTOP
(I2C0
, ENABLE
);
VIC_ITCmd
(I2C0_ITLine
, DISABLE
);
I2C1_Bus.
State = I2C_STATE_IDLE
;
I2C1_Bus.
Error = I2C_ERROR_UNKNOWN
;
return;
}
// enable acknowledge
I2C_AcknowledgeConfig
(I2C0
, ENABLE
);
// send address/direction byte on the bus
I2C_Send7bitAddress
(I2C0
, I2C0_Bus.
SlaveAddr, I2C0_Bus.
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_Bus.
State)
{
case I2C_STATE_TX_PROGRESS
:
// send 1st data byte
Tx_Idx
= 0;
I2C_SendData
(I2C0
, I2C0_Bus.
pData[Tx_Idx
]);
Tx_Idx
++;
// reset timeout
I2C0_Bus.
Timeout = SetDelay
(I2C_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(I2C0_Bus.
RxBufferSize == 1) I2C_AcknowledgeConfig
(I2C0
, DISABLE
);
break;
default: // unknown I2C state
// should never happen
I2C_GenerateSTOP
(I2C0
, ENABLE
);
VIC_ITCmd
(I2C0_ITLine
, DISABLE
);
I2C0_Bus.
State = I2C_STATE_IDLE
;
I2C0_Bus.
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_Bus.
TxBufferSize)
{
I2C_SendData
(I2C0
, I2C0_Bus.
pData[Tx_Idx
]);
Tx_Idx
++;
}
else // last byte was send
{
// generate stop or repeated start condition
if (I2C0_Bus.
RxBufferSize > 0) // is any answer byte expected?
{
I2C0_Bus.
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_Bus.
State = I2C_STATE_IDLE
; // ready for new actions
I2C0_Bus.
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_Bus.
RxBufferSize)
{ // copy received byte from the data register to the rx-buffer
I2C0_Bus.
pData[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_Bus.
pData[Rx_Idx
] = I2C_ReceiveData
(I2C0
);
// call the rx handler function to process recieved data
if(I2C0_Bus.
pRxHandler != NULL
) (*(I2C0_Bus.
pRxHandler))(I2C0_Bus.
pData, I2C0_Bus.
RxBufferSize);
I2C0_Bus.
Timeout = SetDelay
(I2C_TIMEOUT
);
DebugOut.
Analog[15]++;
VIC_ITCmd
(I2C0_ITLine
, DISABLE
);
I2C0_Bus.
State = I2C_STATE_IDLE
;
I2C0_Bus.
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_Bus.
RxBufferSize )
{
I2C_AcknowledgeConfig
(I2C0
, DISABLE
);
}
break;
default:// unknown event
// should never happen
I2C_GenerateSTOP
(I2C0
, ENABLE
);
VIC_ITCmd
(I2C0_ITLine
, DISABLE
);
I2C0_Bus.
State = I2C_STATE_IDLE
;
I2C0_Bus.
Error = I2C_ERROR_UNKNOWN
;
break;
}
}
//IDISABLE; // do not enable IRQ nesting for I2C!!!!
VIC1
->VAR
= 0xFF; // write any value to VIC1 Vector address register
}
//--------------------------------------------------------------
void I2C1_IRQHandler
(void)
{
static u8 Rx_Idx
= 0, Tx_Idx
= 0;
u16 status
;
u16 timeout
= 500;
//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
if(--timeout
== 0)
{
DebugOut.
Analog[14]++; // count I2C error
break;
}
}
I2C1_Bus.
State = I2C_STATE_IDLE
;
I2C1_Bus.
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_Bus.
Direction)
{
case I2C_MODE_TRANSMITTER
:
I2C1_Bus.
State = I2C_STATE_TX_PROGRESS
;
break;
case I2C_MODE_RECEIVER
:
if (I2C1_Bus.
RxBufferSize == 0) // nothing to send?
{
I2C_GenerateSTOP
(I2C1
, ENABLE
);
VIC_ITCmd
(I2C1_ITLine
, DISABLE
);
I2C1_Bus.
State = I2C_STATE_IDLE
;
I2C1_Bus.
Error = I2C_ERROR_NONE
;
return;
}
else
{
I2C1_Bus.
State = I2C_STATE_RX_PROGRESS
;
}
break;
default: // invalid direction
I2C_GenerateSTOP
(I2C1
, ENABLE
);
VIC_ITCmd
(I2C1_ITLine
, DISABLE
);
I2C1_Bus.
State = I2C_STATE_IDLE
;
I2C1_Bus.
Error = I2C_ERROR_UNKNOWN
;
return;
}
// enable acknowledge
I2C_AcknowledgeConfig
(I2C1
, ENABLE
);
// send address/direction byte on the bus
I2C_Send7bitAddress
(I2C1
, I2C1_Bus.
SlaveAddr, I2C1_Bus.
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_Bus.
State)
{
case I2C_STATE_TX_PROGRESS
:
// send 1st data byte
Tx_Idx
= 0;
I2C_SendData
(I2C1
, I2C1_Bus.
pData[Tx_Idx
]);
Tx_Idx
++;
// reset timeout
I2C1_Bus.
Timeout = SetDelay
(I2C_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_Bus.
RxBufferSize == 1) I2C_AcknowledgeConfig
(I2C1
, DISABLE
);
break;
default: // unknown I2C state
// should never happen
I2C_GenerateSTOP
(I2C1
, ENABLE
);
VIC_ITCmd
(I2C1_ITLine
, DISABLE
);
I2C1_Bus.
State = I2C_STATE_IDLE
;
I2C1_Bus.
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_Bus.
TxBufferSize)
{
I2C_SendData
(I2C1
, I2C1_Bus.
pData[Tx_Idx
]);
Tx_Idx
++;
}
else // last byte was send
{
// generate stop or repeated start condition
if (I2C1_Bus.
RxBufferSize > 0) // is any answer byte expected?
{
I2C1_Bus.
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_Bus.
State = I2C_STATE_IDLE
; // ready for new actions
I2C1_Bus.
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_Bus.
RxBufferSize)
{ // copy received byte from the data register to the rx-buffer
I2C1_Bus.
pData[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_Bus.
pData[Rx_Idx
] = I2C_ReceiveData
(I2C1
);
// call the rx handler function to process recieved data
if(I2C1_Bus.
pRxHandler != NULL
) (*(I2C1_Bus.
pRxHandler))(I2C1_Bus.
pData, I2C1_Bus.
RxBufferSize);
I2C1_Bus.
Timeout = SetDelay
(I2C_TIMEOUT
);
DebugOut.
Analog[15]++;
VIC_ITCmd
(I2C1_ITLine
, DISABLE
);
I2C1_Bus.
State = I2C_STATE_IDLE
;
I2C1_Bus.
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_Bus.
RxBufferSize )
{
I2C_AcknowledgeConfig
(I2C1
, DISABLE
);
}
break;
default:// unknown event
// should never happen
DebugOut.
Analog[14]++;
I2C_GenerateSTOP
(I2C1
, ENABLE
);
VIC_ITCmd
(I2C1_ITLine
, DISABLE
);
I2C1_Bus.
State = I2C_STATE_IDLE
;
I2C1_Bus.
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
// returns 1 on success or 0 on timeout
u8 I2CBus_WaitForEndOfTransmission
(I2C_TypeDef
* I2Cx
, u32 timeout
)
{
volatile I2C_Bus_t
*pBus
= NULL
;
u32
time = SetDelay
(timeout
);
if(I2Cx
== I2C0
) pBus
= &I2C0_Bus
;
if(I2Cx
== I2C1
) pBus
= &I2C1_Bus
;
if(pBus
== NULL
) return(0);
while(pBus
->State
!= I2C_STATE_IDLE
)
{
if(CheckDelay
(time)) // Timeout
{
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 I2CBus_LockBuffer
(I2C_TypeDef
* I2Cx
, u32 timeout
)
{
volatile I2C_Bus_t
*pBus
= NULL
;
if(I2Cx
== I2C0
) pBus
= &I2C0_Bus
;
if(I2Cx
== I2C1
) pBus
= &I2C1_Bus
;
if(pBus
== NULL
) return(0);
if(I2CBus_WaitForEndOfTransmission
(I2Cx
, timeout
))
{
pBus
->State
= I2C_STATE_BUFFBUSY
;
pBus
->Error
= I2C_ERROR_UNKNOWN
;
return(1);
}
else return(0);
}
// ----------------------------------------------------------------------------------------
// initate an i2c transmission
// before that function is called, the application has to call I2CBus_LockBuffer and has to fill the Buffer with data to be send
u8 I2CBus_Transmission
(I2C_TypeDef
* I2Cx
, u8 SlaveAddr
, u8
* pTxData
, u8 TxBytes
, I2C_pRxHandler_t pRxHandler
, u8 RxBytes
)
{
u8 retval
= 0;
volatile I2C_Bus_t
*pBus
= NULL
;
if(I2Cx
== I2C0
) pBus
= &I2C0_Bus
;
if(I2Cx
== I2C1
) pBus
= &I2C1_Bus
;
if(pBus
== NULL
) return(0);
if(pBus
->State
== I2C_STATE_BUFFBUSY
) // check for locked buffer
{
if((RxBytes
> I2C_BUFFER_LEN
) || (TxBytes
> I2C_BUFFER_LEN
))
{
pBus
->State
= I2C_STATE_IDLE
;
return(retval
);
}
pBus
->RxBufferSize
= RxBytes
;
pBus
->TxBufferSize
= TxBytes
;
// set direction to master transmitter
if( (pBus
->TxBufferSize
> 0) && (pBus
->TxBufferSize
< I2C_BUFFER_LEN
) )
{
pBus
->Direction
= I2C_MODE_TRANSMITTER
;
// copy data to send from source to tansfer buffer
if(pTxData
) memcpy(pBus
->pData
, pTxData
, pBus
->TxBufferSize
);
}
else if (( pBus
->RxBufferSize
> 0 ) && (pBus
->RxBufferSize
< I2C_BUFFER_LEN
) )
{
pBus
->Direction
= I2C_MODE_RECEIVER
;
}
else // nothing to send or receive
{
pBus
->State
= I2C_STATE_IDLE
;
pBus
->Error
= I2C_ERROR_NONE
;
pBus
->TxBufferSize
= 0;
pBus
->RxBufferSize
= 0;
return(retval
);
}
// update slave address and rx data handler function pointer
pBus
->SlaveAddr
= SlaveAddr
;
pBus
->pRxHandler
= pRxHandler
;
// test on busy flag and clear it
I2C_ClearFlag
(I2Cx
, I2C_FLAG_BUSY
);
// enable I2C IRQ
VIC_ITCmd
(pBus
->VIC_Source
, ENABLE
);
// initiate start condition on the bus
I2C_GenerateStart
(I2Cx
, ENABLE
);
retval
= 1;
}
return(retval
);
}