Delphi I2c

Dependencies:   mbed

Fork of Delphi2I2c by Sho Yama

Revision:
0:daee39a441b0
Child:
1:dcd9840bdd66
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Feb 29 10:58:00 2016 +0000
@@ -0,0 +1,170 @@
+#include    "mbed.h"
+
+#define PCBAUD            115200
+#define I2CSPEED        10000         /* 10 KBPS       */
+
+#define     HI      ( true  )
+#define     LOW     ( false )
+
+//////////////////// ポート定義  //////////////////////
+Serial      PC(USBTX,   USBRX);                 /* PC UART         */
+I2C         I2CS(p9, p10);                     /* I2C (SDA ,SCL)        */
+uint8_t     I2cDataBuffer[256];                 /* I2C DATA BUFFER       */
+uint8_t     I2cAddr;                            /* I2C Addr              */
+uint32_t    I2cSpeed;                           /* I2C Addr              */
+uint8_t     Code;                               /* Check Buffer          */
+
+
+/* //////////////////////////////////////////////////////////////////////// */
+/* //  I2Cシリアル出力書込み処理 /////////////////////////////////////// */
+/* //////////////////////////////////////////////////////////////////////// */
+int I2cWrite(uint8_t I2cAddr, uint8_t len )
+{
+   I2CS.frequency(I2cSpeed);
+   return I2CS.write(I2cAddr,(char *)I2cDataBuffer, len );           /* I2C シリアル書込み */
+}
+/* //////////////////////////////////////////////////////////////////////// */
+/* //  I2Cシリアル読み込み処理 ///////////////////////////////////////// */
+/* //////////////////////////////////////////////////////////////////////// */
+int I2cRead(uint8_t I2cAddr, uint8_t len )
+{
+   I2CS.frequency(I2cSpeed);
+   return I2CS.read(I2cAddr,(char *)I2cDataBuffer , len );       /* I2C シリアル読込み */
+}
+/* //////////////////////////////////////////////////////////////////////// */
+/* // PCへの送信バッファ設定処理 //////////////////////////////////////// */
+/* //////////////////////////////////////////////////////////////////////// */
+void PcReturn(uint8_t RetCode)
+{
+    PC.putc(RetCode);
+}
+/* //////////////////////////////////////////////////////////////////////// */
+/* // コマンド処理 //////////////////////////////////////////////////////// */
+/* //////////////////////////////////////////////////////////////////////// */
+void Command(void)
+{
+    uint8_t     i;
+    uint8_t     Count;
+    if( !PC.readable() )  return;          /* PCからの要求がなければ抜け */
+    Code = PC.getc();     
+
+    switch(Code)
+    {
+            /***********   I2C 送信 ************/
+            case  'O':  
+                      wait_us(200); //100ms 以内
+                      if( !PC.readable() )
+                      {
+                          PcReturn('T');         // Error;
+                          return;   
+                      } 
+                      Count = PC.getc();     
+
+  //                  PC.printf("size: %2d \n",Count);    
+                      wait_us(200); //100ms 以内
+                      if( !PC.readable() )
+                      {
+                          PcReturn('T');         // Error;
+                          return;
+                      } 
+                      I2cAddr = PC.getc();     
+ 
+                      if(Count != 0) Count--;
+                      for( i = 0 ; i < Count ; i++)
+                      {
+                          wait_us(300); //100ms 以内
+                          if( !PC.readable() )
+                          {
+                              PcReturn('T');         // Error;
+                              return;
+                          }
+                          I2cDataBuffer[i] = PC.getc();
+                      }
+                      {
+                          if(!I2cWrite( I2cAddr, Count ))
+                          {
+                             PcReturn('S');     // Success
+                          }
+                          else
+                          {
+                             PcReturn('E');     // Error
+                          }
+                      }
+                      break;
+            /***********   I2CI BUS ************/
+            case 'I': 
+                      wait_us(200); //100ms 以内
+                      if( !PC.readable() )
+                      {
+                          PcReturn('T');         // Error;
+                          return;          /* PCからの要求がなければ抜け */
+                      } 
+                      Count = PC.getc();     
+
+ //                   PC.printf("size: %2d \n",Count);    
+                      
+                      wait_us(200); //100ms 以内
+                      if( !PC.readable() )
+                      {
+                          PcReturn('T');         // Error;
+                          return;          /* PCからの要求がなければ抜け */
+                      } 
+                      I2cAddr = PC.getc();     
+ 
+                      if( Count == 0)
+                      {
+                         PcReturn('T');         // Error;
+                         return;
+                      }
+                      
+                      if(!I2cRead(I2cAddr, Count ))
+                      { // success //
+                         PcReturn('I');         // Success;
+                         PcReturn(Count);
+                         for( uint8_t i = 0 ; i < Count ; i ++ )
+                         {
+                           PcReturn(I2cDataBuffer[i]);
+                         }
+                      }
+                      else
+                      { // ng //
+                         PcReturn('E');         // Error;
+                      }
+                      break;
+            /***********   SPI1O BUS ************/
+            case 'S':  
+                     wait_us(200); //100ms 以内
+                      if( !PC.readable() )
+                      {
+                          PcReturn('T');         // Error;
+                          return;
+                      } 
+                      Code = PC.getc();     
+                     wait_us(200); //100ms 以内
+                      if( !PC.readable() )
+                      {
+                          PcReturn('T');         // Error;
+                          return;   
+                       } 
+                      I2cSpeed = ((uint16_t)Code << 8) | PC.getc();     
+
+                      PcReturn('S');         // Success;
+                      break;
+    }
+   
+}
+/* //////////////////////////////////////////////////////////////////////// */
+/* // メイン処理 ////////////////////////////////////////////////////////// */
+/* //////////////////////////////////////////////////////////////////////// */
+int main(void)  
+{
+     //// PC 通信設定  //////////// 
+     PC.baud(PCBAUD);                             /* PC間フォーマット設定 ボ-レート */
+     I2cSpeed = I2CSPEED;
+     PC.printf("Progam Start!\n");    
+     while( true )
+     {
+        Command();              //PC   =>PCB Command
+    } 
+}
+