SPI-Brigde (capseld), for polling interrupt, it is neccessary to adapt the constructor and the function getInt()

Files at this revision

API Documentation at this revision

Comitter:
x1dmoesc
Date:
Mon Feb 18 16:03:44 2019 +0000
Parent:
1:2a7edc2be6df
Child:
3:9cf83f16c17d
Commit message:
change class construct

Changed in this revision

SC18IS602.cpp Show annotated file Show diff for this revision Revisions of this file
SC18IS602.h Show annotated file Show diff for this revision Revisions of this file
--- a/SC18IS602.cpp	Fri Feb 01 15:46:53 2019 +0000
+++ b/SC18IS602.cpp	Mon Feb 18 16:03:44 2019 +0000
@@ -15,9 +15,9 @@
         cCmd[i] = 0;   
         
     if(getInt())  clearInt();                                                   //
-    
+    wait(0.01);
     
-    getInt_ptr = NULL;
+    //getInt_ptr = NULL;
 
 }
 
@@ -36,9 +36,9 @@
     bAck = NACK;                                                                //
     for(int i = 0; i < BUFFER_SIZE; i++)                                        // clear buffer
         cCmd[i] = 0;   
-        
+    
     if(getInt())  clearInt();                                                   //
-
+    wait(0.01);
 }
 
 
@@ -58,6 +58,7 @@
         cCmd[i] = 0;   
         
     if(getInt())  clearInt();                                                   //
+    wait(0.01);
 
 }
 
@@ -80,7 +81,7 @@
 //******************************************************************************//
 bool SC18IS602::enableGPIO(uint8_t uiConf){                                     //
                                                                                 //
-    cCmd[0] = ADR_GPIO_EN;                                                      //
+    cCmd[0] = ADR_GPIO.EN;                                                      //
     cCmd[1] = uiConf & 0x0F;                                                    // clear reserved bits 0b0000'1111
     return sendViaI2C(cCmd, 2, "Enable GPIO");                                                //
 }
@@ -93,7 +94,7 @@
 //******************************************************************************//
 bool SC18IS602::configGPIO(uint8_t uiConf){                                     //
                                                                                 //
-    cCmd[0] = ADR_GPIO_CONF;                                                    //
+    cCmd[0] = ADR_GPIO.CONF;                                                    //
     cCmd[1] = uiConf;                                                           // clear reserved bits 0b0000'1111
     return sendViaI2C(cCmd, 2);                                                //
 }
@@ -105,8 +106,8 @@
 /******************************************************************************/
 bool SC18IS602::sendViaI2C(const char *cData, int iLength, string sDebug)
 {  
-    bAck = (bool) i2c->write(SC18IS602_W, cData, iLength);       
-    
+    bAck = not i2c->write(SC18IS602_W, cData, iLength);       
+    //printf("%d\n", bAck);
     if (bAck == ACK) {
         return ACK;
         
@@ -124,7 +125,7 @@
 /******************************************************************************/
 bool SC18IS602::readViaI2C(char *cData, int iLength, string sDebug){  
 
-    bAck = (bool) i2c->read(SC18IS602_R, cData, iLength);       
+    bAck = not i2c->read(SC18IS602_R, cData, iLength);       
     
     if (bAck == ACK) {
         return ACK;
@@ -143,12 +144,19 @@
 bool SC18IS602::getInt(){
     //printf("Wait for Int...\n");  
     //wait(0.1);
-    if(iINT == 0){
+    if(iINT == 0){ 
         return INTERRUPT;
-        //if(getInt_ptr != NULL) return (*getInt_ptr)();// musst be modified
+    }
+    
+    if(iINT == 1){
+        //printf("GPIO\n");
+        return pca->getGPIO1_B7(true);
     }
-    if(iINT == 1) return pca->getGPIO1_B7(true);
-    if(iINT == 2) return IntPin->read();
+    
+    if(iINT == 2){
+        //printf("PIN\n");
+        return IntPin->read();
+    }
     
     return not INTERRUPT;
 }
@@ -172,13 +180,13 @@
     
     uiPort &= 0x0F;                                                             // clear reserved bits
     
-    cCmd[0] = ADR_GPIO_READ;                                                    // Read from GPIO port
+    cCmd[0] = ADR_GPIO.READ;                                                    // Read from GPIO port
     sendViaI2C(cCmd, 1, "Read GPIO");
     bAck = (bool) readViaI2C(&cCmd[1], 1);
         
     if(bAck == NACK) return bAck;                                               // if NACK, return
     
-    cCmd[0] = ADR_GPIO_WRITE;
+    cCmd[0] = ADR_GPIO.WRITE;
     cCmd[1] ^= uiPort;                                                          // toogle given pins (uiTogllePin)
     return sendViaI2C(cCmd, 2, "GPIO tog");
 
@@ -188,10 +196,10 @@
 /******************************************************************************/
 // swith the GPIO Pin for debugging on
 /******************************************************************************/
-bool SC18IS602::GPIO_pin3_off()
+bool SC18IS602::gpio_pin3_off()
 {
-    cCmd[0] = ADR_GPIO_WRITE;
-    cCmd[1] = (1 << GPIO_CS3);
+    cCmd[0] = ADR_GPIO.WRITE;
+    cCmd[1] = (1 << GPIO.CS3);
     return sendViaI2C(cCmd, 2, "GPIO off");
 }
 
@@ -199,10 +207,10 @@
 /******************************************************************************/
 // swith the GPIO Pin for debugging off
 /******************************************************************************/
-bool SC18IS602::GPIO_pin3_on()
+bool SC18IS602::gpio_pin3_on()
 {
-    cCmd[0] = ADR_GPIO_WRITE;
-    cCmd[1] = (0 << GPIO_CS3);
+    cCmd[0] = ADR_GPIO.WRITE;
+    cCmd[1] = (0 << GPIO.CS3);
     return sendViaI2C(cCmd, 2, "GPIO on");
 }
 
@@ -218,7 +226,7 @@
     
     
     uiNumByte = 0;
-    cCmd[uiNumByte] = CMD_RW_CS0;                                               // send via SPI and CS0 (SS0)
+    cCmd[uiNumByte] = CMD_RW.CS0;                                               // send via SPI and CS0 (SS0)
     cCmd[++uiNumByte] = cAdrByte;
     for(int n = uiNum - 1 ; n >= 0; n--) {
         cCmd[++uiNumByte] = cDataBytes[n];
@@ -226,12 +234,10 @@
     
     
     uiNumByte++;                                                                // char cCmd counts from 0, so the number is +1    
-    bAck = sendViaI2C(cCmd, uiNumByte, "TX via SPI");                          // send via SPI  
-
-    
-    while(getInt() == not INTERRUPT);                                           // wait until sending is finished and an interrupt occurs
+    bAck = sendViaI2C(cCmd, uiNumByte, "TX via SPI");                           // send via SPI      
+    waitFor(INTERRUPT);                                                  // If there was an interrupt, wait until it is cleared   
     clearInt();  
-    while(getInt() == INTERRUPT);                                               // If there was an interrupt, wait until it is cleared
+    waitFor(not INTERRUPT);                                                      // If there was an interrupt, wait until it is cleared
     
     return bAck;
 }
@@ -242,41 +248,58 @@
 //  returns reading data. The first byte is the status byte
 //******************************************************************************//
 bool SC18IS602::readViaSPI(char cAdrByte, char *cDataBytes, uint8_t uiNum)
-{
+{   
     if((int)uiNum >= BUFFER_SIZE - 1)                                           // If number of sending Bytes greather than BUFFER_SIZE - 1 (Register-Adress-Byte)
         return NACK;                                                            // returns NACK (failure)    
-    
+
     
     // send first time Read comand
     uiNumByte = 0;
-    cCmd[uiNumByte] = CMD_RW_CS0;                                               // send via SPI and CS0 (SS0)
-    cCmd[++uiNumByte] = cAdrByte;
-
-     for(int n = uiNum - 1 ; n >= 0; n--) {
-        cCmd[++uiNumByte] = 0x00;
-    }
-    uiNumByte++;                                                                // char cCmd counts from 0, so the number is +1
-                                                                      
-    while(getInt() == INTERRUPT);                                               // If there was an interrupt, wait until it is cleared
-    bAck = sendViaI2C(cCmd, uiNumByte, "RX via SPI");
-    while(getInt() == not INTERRUPT);
-    clearInt();
-
-
-    // send secound time read comand (dummy) to receive data from first read command
-    uiNumByte = 0;
-    cCmd[uiNumByte++] = CMD_RW_CS0;
-     for(int n = uiNum - 1 ; n >= 0; n--) {
+    cCmd[uiNumByte++] = CMD_RW.CS0;                                               // send via SPI and CS0 (SS0)
+    cCmd[uiNumByte++] = cAdrByte;
+    
+    for(int n = uiNum - 1 ; n >= 0; n--) {
         cCmd[uiNumByte++] = 0x00;
     }
+
     
-    while(getInt() == INTERRUPT);                                               // If there was an interrupt, wait until it is cleared
-    bAck =  sendViaI2C(cCmd, uiNumByte, "RX via SPI");
-    while(getInt() == not INTERRUPT);                                           // wait until sending is finished and an interrupt occurs
+    if(getInt() == INTERRUPT){                                                  // If interrupt isn't cleard, then 
+        //printf("Interrupt...");
+        clearInt();                                                             // clear interrupt
+        waitFor(not INTERRUPT);                                                 // wait until interrupt is cleard
+    }
+    
+    
+
+    bAck = sendViaI2C(cCmd, uiNumByte, "Send Rx Cmd via SPI");
+    waitFor(INTERRUPT);
     clearInt();
-       
+    
+ 
+    // send secound time read comand (dummy) to receive data from first read command   
+    cCmd[0] = CMD_RW.CS0;                                               // send via SPI and CS0 (SS0)
+    //cCmd[++uiNumByte] = cAdrByte;
+    
+    //for(int n = uiNum - 1 ; n >= 0; n--) {
+    //    cCmd[++uiNumByte] = 0x00;
+    //}
+    //uiNumByte++; 
+        
     
-    bAck = (bool) readViaI2C(cDataBytes, uiNumByte);
+    waitFor(not INTERRUPT);                                                      // If there was an interrupt, wait until it is cleared                
+    bAck =  sendViaI2C(cCmd, uiNumByte, "Send dummy data via SPI");
+    waitFor(INTERRUPT);                                                      // wait until sending is finished and an interrupt occurs
+    clearInt();
+    uiNumByte--; 
+    
+    bAck = (bool) readViaI2C(cDataBytes, uiNumByte, "RX via SPI");
+    
+    /*printf("Rx: ");
+    for(int i = 0; i < uiNumByte; i++){
+        printf("0x%02x  ", cDataBytes[i]);
+    }
+    printf("\n");*/
+   
     
     int n = 0;
     for(int i = 0;  i < int(uiNumByte/2); i++){
@@ -292,6 +315,20 @@
 
 
 
+//******************************************************************************//
+//
+//******************************************************************************//
+void SC18IS602::waitFor(bool bInt){
+    iTimeOut = 1000;
+    
+    while((getInt() !=  bInt) and iTimeOut > 0) {
+        iTimeOut--;
+        wait(1e-3);
+    }
+    
+    if(iTimeOut == 0) printf("TimeOut: Interrupt\n");        
+}
+
 
 //******************************************************************************//
 // musst modified
--- a/SC18IS602.h	Fri Feb 01 15:46:53 2019 +0000
+++ b/SC18IS602.h	Mon Feb 18 16:03:44 2019 +0000
@@ -9,27 +9,32 @@
 class SC18IS602
 {
     public:    
-    
-        static const int BUFFER_SIZE       = 200;// Byte
-        static const bool ACK              = false;
-        static const bool NACK             = true;
-        static const bool INTERRUPT        = false;
         
+        static const int BUFFER_SIZE        = 200;// Byte
+        static const bool ACK               = true;
+        static const bool NACK              = false;
+        static const bool INTERRUPT         = false;
+        static const bool WAIT_SEC          = 10e-6;
         
         // Register Adress
-        static const int HARD_ADR          = 0x50;
-        static const int USER_ADR_MASK     = 0x07;
+        static const int HARD_ADR           = 0x50;
+        static const int USER_ADR_MASK      = 0x07;
         
-        static const int CMD_RW_CS0        = 0x01;
-        static const int CMD_RW_CS1        = 0x02;
-        static const int CMD_RW_CS2        = 0x04;
-        static const int CMD_RW_CS3        = 0x08;
+        struct{
+            static const int CS0            = 0x01;
+            static const int CS1            = 0x02;
+            static const int CS2            = 0x04;
+            static const int CS3            = 0x08;
+        }CMD_RW;
         
-        static const int ADR_SPI_CONF      = 0xF0;
-        static const int ADR_GPIO_WRITE    = 0xF4;
-        static const int ADR_GPIO_READ     = 0xF5;
-        static const int ADR_GPIO_EN       = 0xF6;
-        static const int ADR_GPIO_CONF     = 0xF7;
+        static const int ADR_SPI_CONF       = 0xF0;
+        
+        struct{
+            static const int WRITE          = 0xF4;
+            static const int READ           = 0xF5;
+            static const int EN             = 0xF6;
+            static const int CONF           = 0xF7;
+        }ADR_GPIO;
         
         
         //Bit position of register Configure SPI Interface
@@ -38,8 +43,11 @@
         static const int SPI_CLK_RATE       = 0;
         
         // Register Value SPI bit order
-        static const int SPI_BIT_ORDER_MSB  = 0;                                // MSB of the data word is transmitted first
-        static const int SPI_BIT_ORDER_LSB  = 1;                                // LSB of the data word is transmitted first
+        struct{
+            static const int MSB            = 0;                                // MSB of the data word is transmitted first
+            static const int LSB            = 1;                                // LSB of the data word is transmitted first
+        }SPI_BITORDER;
+        
         
         // Register Value SPI Mode (CPOL, CPHA)
         static const int SPI_MODE_00       = 0;
@@ -56,10 +64,12 @@
         
                 
         // Bist position of Register GPIO Enable
-        static const int GPIO_CS0          = 0;
-        static const int GPIO_CS1          = 1;
-        static const int GPIO_CS2          = 2;
-        static const int GPIO_CS3          = 3;
+        struct{
+            static const int CS0          = 0;
+            static const int CS1          = 1;
+            static const int CS2          = 2;
+            static const int CS3          = 3;
+        }GPIO;
         
         
         
@@ -89,8 +99,8 @@
         bool enableGPIO(uint8_t uiConf);
         bool configGPIO(uint8_t uiConf);
 
-        bool GPIO_pin3_on();
-        bool GPIO_pin3_off();
+        bool gpio_pin3_on();
+        bool gpio_pin3_off();
         bool gpio_toggle(uint8_t uiPort);
         
         bool sendViaSPI(char cAdrByte, char *cDataBytes, uint8_t uiNum);
@@ -99,7 +109,7 @@
     
         //void setIntFuncPtr( bool (*Int_ptr)(void));
     
-    private:                                                                    // private
+    protected:                                                                    // private
         I2C *i2c;                                                               // Serial Data & Clock
         PCA9555 *pca;
         DigitalIn *IntPin;                                                      // Interrupt pin
@@ -107,11 +117,13 @@
         
         bool sendViaI2C(const char *cData, int iLength, string sDebug = "");
         bool readViaI2C(char *cData, int iLength, string sDebug = "");
+        void waitFor(bool bInt);
+        
         
         bool getInt();
         bool clearInt();
         
-        bool (*getInt_ptr)(void);
+        //bool (*getInt_ptr)(void);
         
         
         // variable
@@ -125,6 +137,8 @@
         
         char cDebug[100];
         
+        int iTimeOut;
+        
 };