Knud Dalgaard / 310-TMC3-TestHW

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers do.cpp Source File

do.cpp

00001 #include "mbed.h"
00002 #include "uart.h"
00003 #include "interpret.h"
00004 #include "do.h"
00005 #include "MCP23017.h"
00006 
00007 
00008 
00009 //---------------------------------------
00010 // Hardware recources
00011 //---------------------------------------
00012 MCP23017 *DO_I2C;    //
00013 
00014 
00015 //---------------------------------------
00016 // Prototypes
00017 //---------------------------------------
00018 
00019 
00020 
00021 
00022 //---------------------------------------
00023 // Internal variables
00024 //---------------------------------------
00025 static int DO_PinMapping[ DO_NUMBER_PINS ];  // array, keeps MCP23017 pin number for easy access
00026 
00027 
00028 //---------------------------------------
00029 // External variables
00030 //---------------------------------------
00031 
00032 
00033 
00034 //---------------------------------------
00035 // Global Functions
00036 //---------------------------------------
00037 void DO_init( void )
00038 {
00039     DO_PinMapping[0] = DO_PIN1;
00040     DO_PinMapping[1] = DO_PIN2;
00041     DO_PinMapping[2] = DO_PIN3;
00042     DO_PinMapping[3] = DO_PIN4;
00043     DO_PinMapping[4] = DO_PIN5;
00044     DO_PinMapping[5] = DO_PIN6;
00045     DO_PinMapping[6] = DO_PIN7;
00046     DO_PinMapping[7] = DO_PIN8;
00047     DO_PinMapping[8] = DO_PIN9;
00048     DO_PinMapping[9] = DO_PIN10;
00049     DO_PinMapping[10] = DO_PIN11;
00050     DO_PinMapping[11] = DO_PIN12;
00051     DO_PinMapping[12] = DO_PIN13;
00052     DO_PinMapping[13] = DO_PIN14;
00053     DO_PinMapping[14] = DO_PIN15;
00054     DO_PinMapping[15] = DO_PIN16;
00055 
00056     DO_I2C = new MCP23017( p9, p10, DO_I2C_ADD);
00057     DO_I2C->config(0xFFFF,0,0);           // set to inputs, no pull-ups, not-inverted
00058 }
00059 
00060 
00061 
00062 // process commands
00063 void DO_deviceID_process( void )
00064 {
00065     unsigned int temp32U = 0;
00066     unsigned short portTemp = 0;
00067     int i;
00068     int j;
00069 
00070     temp32U = 0x000000FF & ( (unsigned int)uartBuffer[INT_BUF_1DATA] );
00071 
00072     switch( uartBuffer[ INT_BUF_COMMAND ] ) {
00073             //--------------------
00074         case DO_GET_SINGLE:
00075             //--------------------
00076         {
00077             if( (temp32U == 0) || (temp32U > DO_NUMBER_PINS) ) {
00078                 // Generate acknowledge
00079                 INT_generateACKFrame(INT_ID_DO, INT_COM_VAL_NOTVALID);
00080 
00081                 break;
00082             }
00083 
00084             temp32U = DO_I2C->read_bit( DO_PinMapping[temp32U - 1] );
00085 
00086             // Generate acknowledge
00087             uartBuffer[INT_BUF_DEVICEID] = INT_ID_DO;
00088             uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK;
00089             uartBuffer[INT_BUF_NUM] = 1;
00090             uartBuffer[INT_BUF_1DATA] = (char)(0x000000FF & temp32U);
00091             UART_handler.bytesToWrite = 4;
00092             break;
00093 
00094         }
00095 
00096 
00097         //--------------------
00098         case DO_GET_PORT:
00099             //--------------------
00100         {
00101             temp32U = 0;
00102 
00103             portTemp = DO_I2C->read_mask(0xFFFF);
00104 
00105             for( i = 0; i < 16; i++ ) {
00106                 if(  portTemp & (1 << i) ) {
00107                     for( j = 0; j < 16; j++ ) {
00108                         if( DO_PinMapping[j] == i ) {
00109                             temp32U |= (1 << j);
00110                         }
00111                     }//for j
00112 
00113                 }// if
00114             }// for i
00115 
00116             /*
00117                         for( i = 0; i < 16; i++ )
00118                         {
00119                            if(  portTemp & (1 << i) )
00120                            {
00121                                 for( j = 1; j < 17; j++ )
00122                                 {
00123                                     if( DO_PinMapping[j] == i )
00124                                     {
00125                                         temp32U |= (1 << (j-1));
00126                                     }
00127                                 }//for j
00128 
00129                            }// if
00130                         }// for i
00131             */
00132 
00133             // Generate acknowledge
00134             uartBuffer[INT_BUF_DEVICEID] = INT_ID_DO;
00135             uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK;
00136             uartBuffer[INT_BUF_NUM] = 2;
00137             uartBuffer[INT_BUF_1DATA]       = (char)(0x000000FF & (temp32U >> 8) );
00138             uartBuffer[INT_BUF_1DATA + 1]   = (char)(0x000000FF & (temp32U) );
00139             UART_handler.bytesToWrite = 5;
00140             break;
00141 
00142         }
00143 
00144 
00145         //-----------------
00146         default:
00147             //-----------------
00148         {
00149             // Command not supported
00150             INT_generateACKFrame(INT_ID_DO, INT_COM_COM_NOTSUPP);
00151         }
00152     }//switch
00153 
00154 }
00155 
00156 //---------------------------------------
00157 // Internal Functions
00158 //---------------------------------------
00159