Knud Dalgaard / 310-TMC3-TestHW

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers di.cpp Source File

di.cpp

00001 #include "mbed.h"
00002 #include "uart.h"
00003 #include "interpret.h"
00004 #include "di.h"
00005 #include "MCP23017.h"
00006 
00007 
00008 
00009 //---------------------------------------
00010 // Hardware recources
00011 //---------------------------------------
00012 MCP23017 *DI_PIN1_8_I2C;    // Pin: 1..8 and Low, High level reference
00013 MCP23017 *DI_PIN9_24_I2C;   // Pin: 9..24
00014 
00015 
00016 //---------------------------------------
00017 // Prototypes
00018 //---------------------------------------
00019 
00020 
00021 
00022 
00023 //---------------------------------------
00024 // Internal variables
00025 //---------------------------------------
00026 static int DI_PinMapping[ DI_NUMBER_PINS ];  // array, keeps MCP23017 pin number for easy access
00027 
00028 
00029 
00030 //---------------------------------------
00031 // External variables
00032 //---------------------------------------
00033 
00034 
00035 
00036 //---------------------------------------
00037 // Global Functions
00038 //---------------------------------------
00039 void DI_init( void )
00040 {
00041     DI_PinMapping[0] = DI_PIN1;
00042     DI_PinMapping[1] = DI_PIN2;
00043     DI_PinMapping[2] = DI_PIN3;
00044     DI_PinMapping[3] = DI_PIN4;
00045     DI_PinMapping[4] = DI_PIN5;
00046     DI_PinMapping[5] = DI_PIN6;
00047     DI_PinMapping[6] = DI_PIN7;
00048     DI_PinMapping[7] = DI_PIN8;
00049     DI_PinMapping[8] = DI_PIN9;
00050     DI_PinMapping[9] = DI_PIN10;
00051     DI_PinMapping[10] = DI_PIN11;
00052     DI_PinMapping[11] = DI_PIN12;
00053     DI_PinMapping[12] = DI_PIN13;
00054     DI_PinMapping[13] = DI_PIN14;
00055     DI_PinMapping[14] = DI_PIN15;
00056     DI_PinMapping[15] = DI_PIN16;
00057     DI_PinMapping[16] = DI_PIN17;
00058     DI_PinMapping[17] = DI_PIN18;
00059     DI_PinMapping[18] = DI_PIN19;
00060     DI_PinMapping[19] = DI_PIN20;
00061     DI_PinMapping[20] = DI_PIN21;
00062     DI_PinMapping[21] = DI_PIN22;
00063     DI_PinMapping[22] = DI_PIN23;
00064     DI_PinMapping[23] = DI_PIN24;
00065 
00066     DI_PIN1_8_I2C = new MCP23017( p9, p10, DI_PIN1_8_I2C_ADD);
00067     DI_PIN1_8_I2C->config(0, 0, 0x00FF);            // set to outputs, no pull-ups, not-inverted
00068 
00069   
00070     DI_PIN9_24_I2C = new MCP23017( p9, p10, DI_PIN9_24_I2C_ADD);
00071     DI_PIN9_24_I2C->config(0, 0, 0x00FF);           // set to outputs, no pull-ups, not-inverted
00072 
00073     
00074     DI_PIN1_8_I2C->write_bit(0, DI_LEVEL_LOW);
00075     DI_PIN1_8_I2C->write_bit(0, DI_LEVEL_HIGH); 
00076     DI_PIN1_8_I2C->write_mask(0x00FF, 0x00FF);      // set outputs high
00077     DI_PIN9_24_I2C->write_mask(0xFFFF, 0xFFFF);     // set outputs high  
00078 }
00079 
00080 
00081 
00082 // process commands
00083 void DI_deviceID_process( void )
00084 {
00085     unsigned int temp32U;
00086     unsigned short portTemp = 0;
00087     int i;
00088     int j;
00089     
00090     // Get data before processing and save to temp32U;
00091     // maximal 3 bytes
00092     temp32U = 0;
00093     for( i = 0; i < 3; i++ )
00094     {
00095         temp32U <<= 8;
00096         temp32U |= ( 0x000000FF & (unsigned int)(uartBuffer[INT_BUF_1DATA + i]) );
00097     }
00098     
00099     
00100     switch( uartBuffer[ INT_BUF_COMMAND ] )
00101     {
00102         //--------------------
00103         case DI_SET_LEVEL_LOW:
00104         //--------------------
00105         {
00106             // Write to DI HW
00107             if( uartBuffer[INT_BUF_1DATA] )
00108             {
00109                 DI_PIN1_8_I2C->write_bit(1, DI_LEVEL_LOW);
00110             }
00111             else
00112             {
00113                 DI_PIN1_8_I2C->write_bit(0, DI_LEVEL_LOW);
00114             }
00115             // Generate acknowledge
00116             INT_generateACKFrame(INT_ID_DI, INT_COM_ACK);
00117 
00118             break;
00119         }
00120         
00121         //--------------------
00122         case DI_GET_LEVEL_LOW:
00123         //--------------------
00124         {
00125             temp32U = DI_PIN1_8_I2C->read_bit(DI_LEVEL_LOW);
00126             
00127             // Generate acknowledge
00128             uartBuffer[INT_BUF_DEVICEID] = INT_ID_DI;
00129             uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK;
00130             uartBuffer[INT_BUF_NUM] = 1;
00131             uartBuffer[INT_BUF_1DATA] = (char)(0x000000FF & temp32U);
00132             UART_handler.bytesToWrite = 4;
00133         
00134             break;
00135         }  
00136         
00137         //--------------------
00138         case DI_SET_LEVEL_HIGH:
00139         //--------------------
00140         {            
00141             // Write to DI HW
00142             if( uartBuffer[INT_BUF_1DATA] )
00143             {
00144                 DI_PIN1_8_I2C->write_bit(1, DI_LEVEL_HIGH);
00145             }
00146             else
00147             {
00148                 DI_PIN1_8_I2C->write_bit(0, DI_LEVEL_HIGH);
00149             }
00150             // Generate acknowledge
00151             INT_generateACKFrame(INT_ID_DI, INT_COM_ACK);
00152 
00153             break;
00154         }
00155         
00156         //--------------------
00157         case DI_GET_LEVEL_HIGH:
00158         //--------------------
00159         {
00160             temp32U = DI_PIN1_8_I2C->read_bit(DI_LEVEL_HIGH);
00161             
00162             // Generate acknowledge
00163             uartBuffer[INT_BUF_DEVICEID] = INT_ID_DI;
00164             uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK;
00165             uartBuffer[INT_BUF_NUM] = 1;
00166             uartBuffer[INT_BUF_1DATA] = (char)(0x000000FF & temp32U);
00167             UART_handler.bytesToWrite = 4;
00168         
00169             break;
00170         }  
00171         
00172         
00173         //--------------------
00174         case DI_SET_SINGLE:
00175         //--------------------
00176         {
00177             // Limit check of pin number
00178             if( (uartBuffer[INT_BUF_1DATA] == 0) || (uartBuffer[INT_BUF_1DATA] > DI_NUMBER_PINS) )
00179             {
00180                 INT_generateACKFrame(INT_ID_DI, INT_COM_VAL_NOTVALID);        
00181                 break;
00182             }
00183             
00184             if( uartBuffer[INT_BUF_1DATA] > 8 ) // --> write to: DI_PIN9_24_I2C
00185             {
00186                 DI_PIN9_24_I2C->write_bit(0, DI_PinMapping[uartBuffer[INT_BUF_1DATA] - 1]);
00187                 // Generate acknowledge
00188                 INT_generateACKFrame(INT_ID_DI, INT_COM_ACK);
00189                 break;
00190             }
00191             
00192             // otherwise:--> write to: DI_PIN1_8_I2C
00193             DI_PIN1_8_I2C->write_bit(0, DI_PinMapping[uartBuffer[INT_BUF_1DATA] - 1]);
00194             // Generate acknowledge
00195             INT_generateACKFrame(INT_ID_DI, INT_COM_ACK);
00196             break; 
00197         }
00198         
00199         
00200         //--------------------
00201         case DI_CLEAR_SINGLE:
00202         //--------------------
00203         {
00204             // Limit check of pin number
00205             if( (uartBuffer[INT_BUF_1DATA] == 0) || (uartBuffer[INT_BUF_1DATA] > DI_NUMBER_PINS) )
00206             {
00207                 INT_generateACKFrame(INT_ID_DI, INT_COM_VAL_NOTVALID);        
00208                 break;
00209             }
00210             
00211             if( uartBuffer[INT_BUF_1DATA] > 8 ) // --> write to: DI_PIN9_24_I2C
00212             {
00213                 DI_PIN9_24_I2C->write_bit(1, DI_PinMapping[uartBuffer[INT_BUF_1DATA] - 1]);
00214                 // Generate acknowledge
00215                 INT_generateACKFrame(INT_ID_DI, INT_COM_ACK);
00216                 break;
00217             }
00218             
00219             // otherwise:--> write to: DI_PIN1_8_I2C
00220             DI_PIN1_8_I2C->write_bit(1, DI_PinMapping[uartBuffer[INT_BUF_1DATA] - 1]);
00221             // Generate acknowledge
00222             INT_generateACKFrame(INT_ID_DI, INT_COM_ACK);
00223             break; 
00224         }
00225         
00226         
00227         //--------------------
00228         case DI_GET_SINGLE:
00229         //--------------------
00230         {
00231             // Limit check of pin number
00232             if( (uartBuffer[INT_BUF_1DATA] == 0) || (uartBuffer[INT_BUF_1DATA] > DI_NUMBER_PINS) )
00233             {
00234                 INT_generateACKFrame(INT_ID_DI, INT_COM_VAL_NOTVALID);        
00235                 break;
00236             }
00237             
00238             if( uartBuffer[INT_BUF_1DATA] > 8 ) // --> read from: DI_PIN9_24_I2C
00239             {
00240                 temp32U = DI_PIN9_24_I2C->read_bit( DI_PinMapping[uartBuffer[INT_BUF_1DATA] - 1] );
00241             }
00242             else // otherwise:--> read from: DI_PIN1_8_I2C
00243             {
00244                 temp32U = DI_PIN1_8_I2C->read_bit( DI_PinMapping[uartBuffer[INT_BUF_1DATA] - 1] );
00245             }
00246             
00247             // Invert because DO function is inverted      
00248             temp32U = (~temp32U & 0x00000001);
00249                             
00250             // Generate acknowledge
00251             uartBuffer[INT_BUF_DEVICEID] = INT_ID_DI;
00252             uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK;
00253             uartBuffer[INT_BUF_NUM] = 1;
00254             uartBuffer[INT_BUF_1DATA] = (char)(0x000000FF & temp32U);
00255             UART_handler.bytesToWrite = 4;
00256             
00257             break;    
00258         }
00259           
00260           
00261         //--------------------
00262         case DI_SET_PORT:
00263         //--------------------
00264         { 
00265             // process Pin: 1..8 --> write to: DI_PIN1_8_I2C
00266             portTemp = 0;
00267             for( i = 0; i < 8; i++ )
00268             {
00269                if( temp32U & (1 << i) ) // Bit is set --> set coresponding pin
00270                {
00271                     portTemp |= ( 1 << DI_PinMapping[i]);
00272                }
00273             }
00274             DI_PIN1_8_I2C->write_mask((~portTemp & 0x00FF), 0x00FF);   // set outputs
00275             
00276 
00277             // process Pin: 9..24 --> write to: DI_PIN9_24_I2C
00278             portTemp = 0;
00279             for( i = 8; i < 24; i++ )
00280             {
00281                if( temp32U & (1 << i) ) // Bit is set --> set coresponding pin
00282                {
00283                     portTemp |= ( 1 << DI_PinMapping[i]);
00284                }
00285             }
00286             DI_PIN9_24_I2C->write_mask(~portTemp, 0xFFFF);   // set outputs
00287             
00288             // Generate acknowledge
00289             INT_generateACKFrame(INT_ID_DI, INT_COM_ACK);
00290             
00291             break;
00292         }
00293         
00294         
00295         //--------------------
00296         case DI_GET_PORT:
00297         //--------------------
00298         { 
00299             temp32U = 0;
00300             
00301             // process Pin: 1..8 --> read to: DI_PIN1_8_I2C
00302             portTemp = ~( DI_PIN1_8_I2C->read_mask(0x00FF) );
00303             
00304             for( i = 0; i < 8; i++ ) {
00305                 if(  portTemp & (1 << i) ) {
00306                     for( j = 0; j < 16; j++ ) {
00307                         if( DI_PinMapping[j] == i ) {
00308                             temp32U |= (1 << j);
00309                         }
00310                     }//for j
00311 
00312                 }// if
00313             }// for i
00314            
00315             
00316             // process Pin: 9..24 --> read to: DI_PIN9_24_I2C
00317             portTemp = ~( DI_PIN9_24_I2C->read_mask(0xFFFF) );
00318             
00319             for( i = 0; i < 16; i++ ) {
00320                 if(  portTemp & (1 << i) ) {
00321                     for( j = 8; j < 24; j++ ) {
00322                         if( DI_PinMapping[j] == i ) {
00323                             temp32U |= (1 << j);
00324                         }
00325                     }//for j
00326 
00327                 }// if
00328             }// for i
00329 
00330             // Generate acknowledge
00331             uartBuffer[INT_BUF_DEVICEID] = INT_ID_DI;
00332             uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK;
00333             uartBuffer[INT_BUF_NUM] = 3;
00334             uartBuffer[INT_BUF_1DATA]       = (char)(0x000000FF & (temp32U >> 16));
00335             uartBuffer[INT_BUF_1DATA + 1]   = (char)(0x000000FF & (temp32U >> 8) );
00336             uartBuffer[INT_BUF_1DATA + 2]   = (char)(0x000000FF & (temp32U) );
00337             UART_handler.bytesToWrite = 6;
00338             break;
00339         
00340         }
00341 
00342         
00343         //-----------------
00344         default:
00345         //-----------------
00346         {
00347             // Command not supported
00348             INT_generateACKFrame(INT_ID_DI, INT_COM_COM_NOTSUPP);
00349         }     
00350     }//switch
00351     
00352 }
00353 
00354 //---------------------------------------
00355 // Internal Functions
00356 //---------------------------------------
00357