Subversion Repositories NaviCtrl

Rev

Rev 23 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*#######################################################################################*/
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
/*#######################################################################################*/
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Copyright (c) 2008 Ingo Busker, Holger Buss
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
// + 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.
// + 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 oder Nutzung 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 permitted
// +     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 the sources to other systems or using the software on other systems (except 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 "main.h"

u8 I2C_RxBufferSize, I2C_TxBufferSize;
u8 *I2C_TxBuffer, *I2C_RxBuffer;
u8 Tx_Idx=0, Rx_Idx=0, I2C_Direction;
u8 I2C_Command;

struct str_I2C_Heading       I2C_Heading;
struct str_I2C_WriteNickRoll I2C_WriteNickRoll;
struct str_I2C_Mag           I2C_Mag;
struct str_I2C_EEPROM        I2C_ReadEEPROM, I2C_WriteEEPROM;
struct str_I2C_Version       I2C_Version;
struct str_I2C_WriteCal      I2C_WriteCal;
u8 CompassUpdateActiv = 0;
u8 CompassCalState = 0;
volatile u8 I2C_ReadRequest = 0;

//--------------------------------------------------------------
void I2C1_Init(void)
{
    I2C_InitTypeDef   I2C_Struct;
  GPIO_InitTypeDef  GPIO_Struct;
 
  SerialPutString("I2C init...");
  SCU_APBPeriphClockConfig(__I2C1,ENABLE);
  I2C_DeInit(I2C1);

  GPIO_Struct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
  GPIO_Struct.GPIO_Type = GPIO_Type_OpenCollector;
  GPIO_Struct.GPIO_IPConnected = GPIO_IPConnected_Enable;
  GPIO_Struct.GPIO_Alternate=GPIO_OutputAlt2;
  GPIO_Init(GPIO2, &GPIO_Struct);

  I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;
  I2C_Struct.I2C_Ack = I2C_Ack_Enable;
  I2C_Struct.I2C_CLKSpeed = 20000;
  I2C_Struct.I2C_OwnAddress = I2C_SLAVE_ADDRESS;
  I2C_Init(I2C1, &I2C_Struct);
 
  I2C_Cmd(I2C1, ENABLE);
  I2C_ITConfig(I2C1, ENABLE);
 
  VIC_Config(I2C1_ITLine,VIC_IRQ , 8);
  VIC_ITCmd(I2C1_ITLine, ENABLE);

  I2C_Heading.Heading = 0;
  SerialPutString("ok\n\r");
 
 
}

//--------------------------------------------------------------
void I2C1_IRQHandler(void)
{
       
  switch (I2C_GetLastEvent(I2C1))
  {
    case I2C_EVENT_MASTER_MODE_SELECT:             // EV5
      Tx_Idx = 0;
          Rx_Idx = 0;
          I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction);
//        DebugOut.Analog[16]++;
      break;

    case I2C_EVENT_MASTER_MODE_SELECTED:           // EV6
      // Clear EV6 by set again the PE bit
      I2C1->CR |=  0x20;
      if (I2C_Direction == I2C_MODE_TRANSMITTER)
      {  
             I2C_SendData(I2C1, I2C_Command);//EV8 just after EV6
          }
      break;

    case I2C_EVENT_MASTER_BYTE_TRANSMITTED:         // EV8
//      DebugOut.Analog[17]++;
          if ( Tx_Idx >= I2C_TxBufferSize )
      {
        I2C_GenerateSTOP (I2C1, ENABLE);
        Tx_Idx = 0;
                if (I2C_RxBufferSize > 0)                  // wird Antwort erwartet ?
                {
                  I2C_ReadRequest = 1;
                  TimerI2CReadDelay = SetDelay(10);
                }
      }
      else
      {
        I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]);
        Tx_Idx++;
      }
      break;
           
    case I2C_EVENT_MASTER_BYTE_RECEIVED:           // EV7
          DebugOut.Analog[16]++;
          //if (Rx_Idx == 1) DebugOut.Analog[19] = I2C_RxBuffer[0];
          if (Rx_Idx < I2C_RxBufferSize)
          { I2C_RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);
           
          }     else I2C_ReceiveData(I2C1);
         
          Rx_Idx++;
          if ( Rx_Idx == I2C_RxBufferSize-2 )
      {
        I2C_AcknowledgeConfig (I2C1, DISABLE);
      }
      if ( Rx_Idx == I2C_RxBufferSize -1 )
      {
        I2C_GenerateSTOP(I2C1, ENABLE);
                if (I2C_Command == I2C_CMD_READ_HEADING) CompassUpdateActiv = 0;
      }
      break;

    default:
      break;
  }    
}
//----------------------------------------------------------------
void SendI2C_Command(u8 command)
{
  I2C_Command = command;
  //I2C_GenerateSTOP(I2C1, ENABLE);

  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.Inhalt;
                   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:
               CompassUpdateActiv = 1;
           I2C_RxBuffer = (u8 *)&I2C_Heading;
                   I2C_RxBufferSize = sizeof(I2C_Heading);
                   I2C_TxBuffer =  (u8 *)&I2C_WriteNickRoll;
                   I2C_TxBufferSize = sizeof(I2C_WriteNickRoll);
          break;

  }
  if (I2C_RxBufferSize > 0)
  {
    I2C_RxBufferSize++;
        if (I2C_RxBufferSize < 3) I2C_RxBufferSize = 3;
  }

  I2C_AcknowledgeConfig (I2C1, ENABLE);
  I2C_Direction = I2C_MODE_TRANSMITTER;
 
  I2C_GenerateStart(I2C1, ENABLE);

}