7,14 → 7,14 |
// + FOR NON COMMERCIAL USE ONLY |
// + 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. |
// + 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, |
// + 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 |
25,21 → 25,21 |
// + 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 |
// + 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, |
// + 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 permitted |
// + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
// + for non-commercial use (directly or indirectly) |
// + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
// + 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 |
// + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
// + clearly linked as origin |
// + * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
// |
// + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
52,8 → 52,9 |
// + 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. |
// + POSSIBILITY OF SUCH DAMAGE. |
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
#include <stdlib.h> |
#include "91x_lib.h" |
#include "i2c.h" |
#include "uart1.h" |
65,13 → 66,13 |
volatile I2C_State_t I2C_State; |
|
// rxbuffer |
volatile u8 I2C_RxBufferSize; |
volatile u8 I2C_RxBufferSize; |
volatile u8 *I2C_RxBuffer; |
volatile u8 Rx_Idx = 0; |
// txbuffer |
volatile u8 I2C_TxBufferSize; |
volatile u8 *I2C_TxBuffer; |
volatile u8 Tx_Idx = 0; |
volatile u8 *I2C_TxBuffer; |
volatile u8 Tx_Idx = 0; |
|
volatile u8 I2C_Direction; |
volatile u8 I2C_Command; |
79,7 → 80,7 |
volatile I2C_Heading_t I2C_Heading; |
volatile I2C_WriteAttitude_t I2C_WriteAttitude; |
volatile I2C_Mag_t I2C_Mag; |
volatile I2C_EEPROM_t I2C_ReadEEPROM, I2C_WriteEEPROM; |
volatile I2C_EEPROM_t I2C_ReadEEPROM, I2C_WriteEEPROM; |
volatile I2C_Version_t I2C_Version; |
volatile I2C_WriteCal_t I2C_WriteCal; |
|
95,34 → 96,96 |
void I2C1_Init(void) |
{ |
I2C_InitTypeDef I2C_Struct; |
GPIO_InitTypeDef GPIO_InitStructure; |
|
GPIO_InitTypeDef GPIO_InitStructure; |
|
SerialPutString("\r\n I2C init..."); |
SCU_APBPeriphClockConfig(__I2C1,ENABLE); |
I2C_DeInit(I2C1); |
// configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT |
// 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_IPConnected = GPIO_IPConnected_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_IPConnected = GPIO_IPConnected_Enable; |
GPIO_InitStructure.GPIO_Alternate=GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT |
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 = 20000; |
I2C_Struct.I2C_OwnAddress = 0x00; //?? |
I2C_Struct.I2C_OwnAddress = 0x00; |
I2C_Init(I2C1, &I2C_Struct); |
|
|
I2C_TxBuffer = NULL; |
Tx_Idx = 0; |
I2C_TxBufferSize = 0; |
|
I2C_RxBuffer = NULL; |
Rx_Idx = 0; |
I2C_RxBufferSize = 0; |
|
I2C_Cmd(I2C1, ENABLE); |
I2C_ITConfig(I2C1, ENABLE); |
|
|
VIC_Config(I2C1_ITLine, VIC_IRQ , 0); |
VIC_ITCmd(I2C1_ITLine, ENABLE); |
|
I2C1_Timeout = SetDelay(1000); |
I2C_Heading.Heading = -1; |
I2C_GenerateSTOP(I2C1, ENABLE); |
I2C_State = I2C_IDLE; |
SerialPutString("ok"); |
} |
133,16 → 196,26 |
{ |
SerialPutString("\r\n I2C deinit..."); |
I2C_GenerateStart(I2C1, DISABLE); |
I2C_GenerateSTOP(I2C1, ENABLE); |
VIC_ITCmd(I2C1_ITLine, DISABLE); |
I2C_ITConfig(I2C1, DISABLE); |
I2C_GenerateSTOP(I2C1, ENABLE); |
VIC_ITCmd(I2C1_ITLine, DISABLE); |
I2C_ITConfig(I2C1, DISABLE); |
I2C_Cmd(I2C1, DISABLE); |
I2C_DeInit(I2C1); |
SCU_APBPeriphClockConfig(__I2C1,DISABLE); |
SCU_APBPeriphClockConfig(__I2C1, DISABLE); |
|
I2C_TxBuffer = NULL; |
Tx_Idx = 0; |
I2C_TxBufferSize = 0; |
|
I2C_RxBuffer = NULL; |
Rx_Idx = 0; |
I2C_RxBufferSize = 0; |
|
I2C_State = I2C_UNDEF; |
|
I2C1_Timeout = 0; |
I2C_Heading.Heading = 0; |
|
SerialPutString("ok"); |
} |
|
150,34 → 223,48 |
//-------------------------------------------------------------- |
void I2C1_IRQHandler(void) |
{ |
u8 data; |
|
switch (I2C_GetLastEvent(I2C1)) |
{ |
// the start condition was initiated on the bus |
case I2C_EVENT_MASTER_MODE_SELECT: // EV5 |
// reset TX/RX buffer indices |
Tx_Idx = 0; |
Rx_Idx = 0; |
LED_GRN_ON; |
// update current bus state variable |
switch(I2C_Direction) |
{ |
case I2C_MODE_TRANSMITTER: |
I2C_State = I2C_TX_PROGRESS; |
break; |
I2C_State = I2C_TX_PROGRESS; |
break; |
|
case I2C_MODE_RECEIVER: |
I2C_State = I2C_RX_PROGRESS; |
if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0)) |
{ |
I2C_GenerateSTOP (I2C1, ENABLE); |
I2C_State = I2C_IDLE; |
return; |
} |
else |
{ |
I2C_State = I2C_RX_PROGRESS; |
} |
break; |
|
default: |
I2C_State = I2C_UNDEF; |
I2C_GenerateSTOP (I2C1, ENABLE); |
I2C_State = I2C_IDLE; |
LED_GRN_OFF; |
return; |
|
break; |
} |
// send address/direction byte on the bus |
I2C_AcknowledgeConfig (I2C1, ENABLE); |
// send address/direction byte on the bus |
I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction); |
// reset timeout |
I2C1_Timeout = SetDelay(500); // after 500 ms of inactivity the I2C1 bus will be reset |
break; |
// tha address byte was send |
// the address byte was send and slave has been acknowledged |
case I2C_EVENT_MASTER_MODE_SELECTED: // EV6 |
// Clear EV6 by set again the PE bit |
I2C1->CR |= 0x20; |
186,14 → 273,16 |
case I2C_TX_PROGRESS: |
// send command 1st data byte (allways the command id) |
I2C_SendData(I2C1, I2C_Command); |
Tx_Idx = 0; |
break; |
|
case I2C_RX_PROGRESS: |
// before the last byte is received the master should disable |
// the acknowledge bit to signal the slave that |
// no more bytes |
// the ACK is disabled here only if 1 byte is expected |
if(I2C_RxBufferSize == 1) I2C_AcknowledgeConfig(I2C1, DISABLE); |
// if only one byte should be received |
if (I2C_RxBufferSize == 1) |
{ // send no acknowledge after reception of the first byte |
I2C_AcknowledgeConfig (I2C1, DISABLE); |
} |
Rx_Idx = 0; |
break; |
|
default: |
201,58 → 290,69 |
I2C_GenerateSTOP (I2C1, ENABLE); |
I2C_State = I2C_IDLE; |
break; |
} |
} |
break; |
|
// the master has transmitted a byte to the slave |
case I2C_EVENT_MASTER_BYTE_TRANSMITTED: // EV8 |
// if all bytes are transmitted |
if ( Tx_Idx >= I2C_TxBufferSize ) // all bytes transmitted |
// if all bytes are transmitted |
if ( (Tx_Idx >= I2C_TxBufferSize) || (I2C_TxBuffer == NULL) ) // all bytes transmitted |
{ |
// generate a STOP condition on the bus |
I2C_GenerateSTOP (I2C1, ENABLE); // generate stop condition to free the bus |
if (I2C_RxBufferSize > 0) // is any answer byte expected? |
if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected? |
{ |
I2C_State = I2C_RX_PENDING; // data can be read back |
TimerI2CReadDelay = SetDelay(10); // start master receiver sequence in 10 ms |
} |
else I2C_State = I2C_IDLE; // ready for new actions |
else |
{ |
I2C_State = I2C_IDLE; // ready for new actions |
LED_GRN_OFF; |
} |
} |
else // some bytes have to be transmitted |
{ |
I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]); // send next byte |
Tx_Idx++; |
if(I2C_TxBuffer != NULL) |
{ |
I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx++]); // send next byte |
} |
else |
{ // send a dummy byte (should never happen) |
I2C_SendData(I2C1, 0x00); |
} |
} |
break; |
|
// the master has received a byte from the slave |
|
// the master has received a byte from the slave |
case I2C_EVENT_MASTER_BYTE_RECEIVED: // EV7 |
data = I2C_ReceiveData(I2C1); |
DebugOut.Analog[16]++; |
// before the last byte |
if ( (Rx_Idx + 2) >= I2C_RxBufferSize ) |
{ // disable the ACK to signal the slave that the next received byte is the last one |
|
// as long as there is some space in the rx-buffer |
if ((I2C_RxBuffer != NULL) && (Rx_Idx < I2C_RxBufferSize)) |
{ // copy received byte from the data register to the rx-buffer |
I2C_RxBuffer[Rx_Idx] = data; |
} |
Rx_Idx++; |
// if the 2nd last byte was received disable acknowledge for the last one |
if ( (Rx_Idx+1) >= I2C_RxBufferSize ) |
{ |
I2C_AcknowledgeConfig (I2C1, DISABLE); |
} |
// if the last byte was received |
if ( (Rx_Idx + 1) >= I2C_RxBufferSize ) |
// if the last byte was received |
if ( Rx_Idx >= I2C_RxBufferSize ) |
{ |
// generate a STOP condition on the bus |
// generate a STOP condition on the bus |
I2C_GenerateSTOP(I2C1, ENABLE); |
I2C_State = I2C_IDLE; |
LED_GRN_TOGGLE; |
LED_GRN_OFF; |
} |
// as long as there is some space in the rx-buffer |
if (Rx_Idx < I2C_RxBufferSize) |
{ // copy received byte from the data register to the rx-buffer |
I2C_RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
} |
else I2C_ReceiveData(I2C1); // just read the data register |
Rx_Idx++; // set index to next byte |
break; |
|
default: |
break; |
} |
} |
} |
//---------------------------------------------------------------- |
void I2C1_SendCommand(u8 command) |
264,43 → 364,52 |
I2C_ITConfig(I2C1, DISABLE); |
// update current command id |
I2C_Command = command; |
// set pointers to data area with respect to the command id |
// set pointers to data area with respect to the command id |
switch (command) |
{ |
case I2C_CMD_VERSION: |
I2C_RxBuffer = (u8 *)&I2C_Version; |
I2C_RxBufferSize = sizeof(I2C_Version); |
I2C_TxBufferSize = 0; |
break; |
case I2C_CMD_WRITE_EEPROM: |
I2C_RxBufferSize = 0; |
I2C_TxBuffer = (u8 *)&I2C_WriteEEPROM; |
I2C_TxBufferSize = sizeof(I2C_WriteEEPROM); |
break; |
case I2C_CMD_WRITE_CAL: |
I2C_RxBufferSize = 0; |
I2C_WriteCal.CalByte = CompassCalState; |
I2C_TxBuffer = (u8 *)&I2C_WriteCal; |
I2C_TxBufferSize = sizeof(I2C_WriteCal); |
break; |
case I2C_CMD_READ_EEPROM: |
I2C_RxBuffer = (u8 *)&I2C_ReadEEPROM.Content; |
I2C_RxBufferSize = 2; |
I2C_TxBuffer = (u8 *)&I2C_ReadEEPROM; |
I2C_TxBufferSize = 1; |
break; |
case I2C_CMD_READ_MAG: |
I2C_RxBuffer = (u8 *)&I2C_Mag; |
I2C_RxBufferSize = sizeof(I2C_Mag); |
I2C_TxBufferSize = 0; |
break; |
case I2C_CMD_READ_HEADING: |
I2C_RxBuffer = (u8 *)&I2C_Heading; |
I2C_RxBufferSize = sizeof(I2C_Heading); |
I2C_TxBuffer = (u8 *)&I2C_WriteAttitude; |
I2C_TxBufferSize = sizeof(I2C_WriteAttitude); |
break; |
|
case I2C_CMD_VERSION: |
I2C_RxBuffer = (u8 *)&I2C_Version; |
I2C_RxBufferSize = sizeof(I2C_Version); |
I2C_TxBuffer = NULL; |
I2C_TxBufferSize = 0; |
break; |
case I2C_CMD_WRITE_EEPROM: |
I2C_RxBuffer = NULL; |
I2C_RxBufferSize = 0; |
I2C_TxBuffer = (u8 *)&I2C_WriteEEPROM; |
I2C_TxBufferSize = sizeof(I2C_WriteEEPROM); |
break; |
case I2C_CMD_WRITE_CAL: |
I2C_RxBuffer = NULL; |
I2C_RxBufferSize = 0; |
I2C_WriteCal.CalByte = CompassCalState; |
I2C_TxBuffer = (u8 *)&I2C_WriteCal; |
I2C_TxBufferSize = sizeof(I2C_WriteCal); |
break; |
case I2C_CMD_READ_EEPROM: |
I2C_RxBuffer = (u8 *)&I2C_ReadEEPROM.Content; |
I2C_RxBufferSize = 2; |
I2C_TxBuffer = (u8 *)&I2C_ReadEEPROM; |
I2C_TxBufferSize = 1; |
break; |
case I2C_CMD_READ_MAG: |
I2C_RxBuffer = (u8 *)&I2C_Mag; |
I2C_RxBufferSize = sizeof(I2C_Mag); |
I2C_TxBuffer = NULL; |
I2C_TxBufferSize = 0; |
break; |
case I2C_CMD_READ_HEADING: |
I2C_RxBuffer = (u8 *)&I2C_Heading; |
I2C_RxBufferSize = sizeof(I2C_Heading); |
I2C_TxBuffer = (u8 *)&I2C_WriteAttitude; |
I2C_TxBufferSize = sizeof(I2C_WriteAttitude); |
break; |
default: // unknown command id |
I2C_RxBuffer = NULL; |
I2C_RxBufferSize = 0; |
I2C_TxBuffer = NULL; |
I2C_TxBufferSize = 0; |
break; |
} |
// enable I2C IRQ again |
I2C_ITConfig(I2C1, ENABLE); |
307,8 → 416,8 |
// set direction to master transmitter |
I2C_Direction = I2C_MODE_TRANSMITTER; |
// enable acknowledge |
I2C_AcknowledgeConfig(I2C1, ENABLE); |
// initiale start condition on the bus |
//I2C_AcknowledgeConfig(I2C1, ENABLE); |
// initiale start condition on the bus |
I2C_GenerateStart(I2C1, ENABLE); |
// to be continued in the I2C1_IRQHandler() above |
} |
321,8 → 430,8 |
// set direction to master receiver |
I2C_Direction = I2C_MODE_RECEIVER; |
// enable acknowledge |
I2C_AcknowledgeConfig(I2C1, ENABLE); |
// initiale start condition on the bus |
//I2C_AcknowledgeConfig(I2C1, ENABLE); |
// initiale start condition on the bus |
I2C_GenerateStart(I2C1, ENABLE); |
// to be continued in the I2C1_IRQHandler() above |
} |