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

Revision:
2:cb90c271c412
Parent:
1:2a7edc2be6df
Child:
3:9cf83f16c17d
diff -r 2a7edc2be6df -r cb90c271c412 SC18IS602.cpp
--- 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