int ip470Write(int port,int chan,int val) /* Each port is a group of eight channels. Port 0 == 0-7 Port 1 == 8-15 Port 2 == 16-23 Port 3 == 24-31 Port 4 == 32-39 Port 5 == 40-47 */ { if (!toggle && nbits < 2) /* so nbits = 0,1 works like normal */ return wpnt470(&cblk_470,port,chan,val); else { unsigned int current, towrite; unsigned int mask=0; unsigned int i; for (i=0;i<nbits;i++) mask |= 1<<i; current = rprt470(&cblk_470, port); if (toggle) { towrite = (~(current & (mask<<chan)) & (mask<<chan)) | (current & ~(mask<<chan)); printf("current: %d\n", current); printf("towrite: %d\n", towrite); printf("current: %d\n", current); wprt470(&cblk_470, port, towrite); return wprt470(&cblk_470, port, current); } else { towrite = (current & ~(mask<<chan)) | ((val&mask)<<chan); printf("current: %d\n", current); printf("towrite: %d\n", towrite); return wprt470(&cblk_470, port, towrite); } } }
int setMultipleLevels(int basePort, int baseChan, int nbits, int value) { unsigned int current; unsigned int toWrite; unsigned int i; unsigned int mask=0; for (i=0;i<nbits;i++) mask |= 1<<i; current=rprt470(&config470, basePort); toWrite = (current & ~(mask<<baseChan)) | ((value&mask)<<baseChan); if(printToScreen) { printf("Base Port %d -- Current %d\t New %d\n", basePort,current,toWrite); } return wprt470(&config470, basePort, toWrite); }
int main() { /* DECLARE LOCAL DATA AREAS: */ char cmd_buff[40]; /* command line input buffer */ int item; /* menu item selection variable */ long status; /* returned status of driver routines */ // int hstatus; /* interrupt handler returned status */ unsigned finished; /* flag to exit program */ long addr; /* long to hold board address */ int flag; /* general flag for exiting loops */ int i; /* loop index */ int point; /* I/O point number */ int port; /* I/O port number */ int val; /* value of port or point */ int hflag; /* interrupt handler installed flag */ struct cblk470 c_block; /* configuration block */ struct handler_data hdata; /* interrupt handler data structure */ /* ENTRY POINT OF ROUTINE: INITIALIZATION */ flag = 0; /* indicate board address not yet assigned */ finished = 0; /* indicate not finished with program */ hflag = 0; /* indicate interrupt handler not installed yet */ /* Initialize the Configuration Parameter Block to default values. */ c_block.param = 0; /* parameter mask */ c_block.e_mode = 0; /* e_mode flag */ c_block.mask_reg = 0; /* output mask register */ c_block.deb_control = 0; /* debounce control register */ c_block.deb_clock = 1; /* debounce control register */ c_block.enable = 0; /* interrupt enable register */ c_block.vector = VECTOR; /* interrupt vector */ c_block.bCarrier = FALSE; /* indicate no carrier initialized and set up yet */ c_block.bInitialized = FALSE;/* indicate not ready to talk to IP */ c_block.slotLetter = SLOT_A; c_block.nHandle = 0; /* make handle to a closed carrier board */ for(i = 0; i != 2; i++) { c_block.ev_control[i] = 0;/* event control registers */ c_block.deb_duration[i] = 0;/* debounce duration registers */ } for(i = 0; i < 6; i++) c_block.event_status[i] = 0; /* initialize event status flags */ hdata.h_pid = getpid(); /* save it in the interrupt handler data structure */ hdata.hd_ptr = (char *)&c_block; /* put in address of c_block structure also */ /* Initialize the Carrier library */ if(InitCarrierLib() != S_OK) { printf("\nUnable to initialize the carrier library. Exiting program.\n"); exit(0); } /* Open an instance of a carrier device */ if(CarrierOpen(0, &c_block.nHandle) != S_OK) { printf("\nUnable to Open instance of carrier.\n"); finished = 1; /* indicate finished with program */ } else flag = 1; /* Enter main loop */ while(!finished) { printf("\n\nIP470 Library Demonstration Version A\n\n"); printf(" 1. Exit this Program\n"); printf(" 2. Set IP Module Base Address\n"); printf(" 3. Set IP Slot Letter\n"); printf(" 4. Set Up Configuration Parameters\n"); printf(" 5. Configure Board Command\n"); printf(" 6. Read Status Command and ID\n"); printf(" 7. N/A\n"); printf(" 8. N/A\n"); printf(" 9. Read Input Point\n"); printf("10. Read Input Port\n"); printf("11. Write Output Point\n"); printf("12. Write Output Port\n"); printf("\nSelect: "); scanf("%d",&item); /* perform the menu item selected. */ switch(item) { case 1: /* exit program command */ printf("Exit program(y/n)?: "); scanf("%s",cmd_buff); if( cmd_buff[0] == 'y' || cmd_buff[0] == 'Y' ) finished++; break; case 2: /* set IP address command */ do { if(flag == 0) { printf("\n\nenter base address of carrier board in hex: "); scanf("%lx",&addr); /* Set Carrier Address for Open Carrier Device */ SetCarrierAddress(c_block.nHandle, addr); /* Set Carrier Address */ } GetCarrierAddress(c_block.nHandle, &addr); /* Read back carrier address */ printf("address: %lX\n",addr); printf("is this value correct(y/n)?: "); scanf("%s",cmd_buff); if( cmd_buff[0] == 'y' || cmd_buff[0] == 'Y' ) { SetCarrierAddress(c_block.nHandle, addr); /* Set Carrier Address */ if(CarrierInitialize(c_block.nHandle) == S_OK)/* Initialize Carrier */ { c_block.bCarrier = TRUE; SetInterruptLevel(c_block.nHandle, INTERRUPT_LEVEL);/* Set carrier interrupt level */ } flag = 1; } else flag = 0; }while( cmd_buff[0] != 'y' && cmd_buff[0] != 'Y' ); break; case 3: /* set IP Slot Letter */ if(flag == 0 || c_block.bCarrier == FALSE) printf("\n>>> ERROR: BOARD ADDRESS NOT SET <<<\n"); else { printf("\n\nEnter IP slot letter (A, B etc...): "); scanf("%s",cmd_buff); cmd_buff[0] = toupper(cmd_buff[0]); if(cmd_buff[0] < 'A' || cmd_buff[0] > GetLastSlotLetter()) { printf("\nInvalid Slot Letter!\n"); c_block.bCarrier = FALSE; } else { c_block.slotLetter = cmd_buff[0]; /* Get the IPACK's base address based on the Slot letter, and initialize the IPACK's data structure with the returned address */ if(GetIpackAddress(c_block.nHandle, c_block.slotLetter, &addr) != S_OK) { printf("\nUnable to Get Ipack Address.\n"); c_block.bInitialized = FALSE; } else { c_block.brd_ptr = (struct map470 *)addr; c_block.bInitialized = TRUE; } } } break; case 4: /* set up configuration parameters */ scfg470(&c_block); break; case 5: /* configure board command */ if(!c_block.bInitialized) printf("\n>>> ERROR: BOARD ADDRESS NOT SET <<<\n"); else { /* Check the "interrupt handler attached" flag. If interrupt handlers are not attached, then print an error message. */ if( (c_block.enable == 1 || c_block.enable == 3 ) && hflag == 0 ) printf(">>> ERROR: INTERRUPT HANDLER NOT ATTACHED <<<\n"); else /* select enhanced mode & configure */ cnfg470(&c_block); /* configure the board */ } break; case 6: /* read board status command */ if(!c_block.bInitialized) printf("\n>>> ERROR: BOARD ADDRESS NOT SET <<<\n"); else psts470(&c_block); /* read board status */ break; case 7: /* attach exception handler */ /* if(hflag) printf("\n>>> ERROR: HANDLERS ARE ATTACHED <<<\n"); else { hstatus = 0; hstatus |= SetInterruptHandler(c_block.nHandle, c_block.slotLetter, INT_REQUEST_0, c_block.vector, (void*)isr_470, (void*)&hdata); if(hstatus ) { printf(">>> ERROR WHEN ATTACHING HANDLER <<<\n"); hflag = 0; } else { hstatus = 0; hstatus = EnableInterrupts(c_block.nHandle); if(hstatus != S_OK) { printf(">>> ERROR WHEN ENABLING INTERRUPTS <<<\n"); hflag = 0; } else { hflag = 1; printf("\nHandlers are now attached\n"); } } } / * end of if/else * / */ break; case 8: /* detach exception handlers */ hflag = 0; DisableInterrupts(c_block.nHandle); /* printf("\n>>> NO WAY TO DETACH AT THIS TIME <<<n"); */ break; case 9: /* Read Digital Input Point */ if(!c_block.bInitialized) printf("\n>>> ERROR: BOARD ADDRESS NOT SET <<<\n"); else { printf("\nEnter Input port number (0 - 5): "); scanf("%d",&port); printf("\nEnter Input point number (0 - 7): "); scanf("%d",&point); status = rpnt470(&c_block,port,point); if(status == -1) printf("\n>>> ERROR: PARAMETER OUT OF RANGE <<<\n"); else printf("\nValue of port %d point %d: %lx\n",port,point,status); } break; case 10: /* Read Digital Input Port */ if(!c_block.bInitialized) printf("\n>>> ERROR: BOARD ADDRESS NOT SET <<<\n"); else { printf("\nEnter Input port number (0 - 5): "); scanf("%d",&port); status = rprt470(&c_block,port); if(status == -1) printf("\n>>> ERROR: PARAMETER OUT OF RANGE <<<\n"); else printf("\nValue of port %d: %lx\n",port,status); } break; case 11: /* Write Digital Point */ if(!c_block.bInitialized) printf("\n>>> ERROR: BOARD ADDRESS NOT SET <<<\n"); else { printf("\nEnter Output port number (0 - 5): "); scanf("%d",&port); printf("\nEnter I/O point number (0 - 7): "); scanf("%d",&point); printf("\nEnter point value (0 - 1): "); scanf("%x",&val); status = wpnt470(&c_block,port,point,val); if(status == -1) printf("\n>>> ERROR: PARAMETER OUT OF RANGE <<<\n"); } break; case 12: /* Write Digital Port */ if(!c_block.bInitialized) printf("\n>>> ERROR: BOARD ADDRESS NOT SET <<<\n"); else { printf("\nEnter Output port number (0 - 5): "); scanf("%d",&port); printf("\nEnter output value in hex (0 - FF): "); scanf("%x",&val); status = wprt470(&c_block,port,val); if(status == -1) printf("\n>>> ERROR: PARAMETER OUT OF RANGE <<<\n"); } break; } /* end of switch */ } /* end of while */ /* disable interrupts from IP module */ if(flag != 0) /* module address was set */ { c_block.param = RESET_INTEN; /* parameter mask */ c_block.enable = 0; c_block.enable |= RESET; cnfg470(&c_block); /* configure the board */ } DisableInterrupts(c_block.nHandle); if(c_block.bCarrier) CarrierClose(c_block.nHandle); printf("\nEXIT PROGRAM\n"); return 0; } /* end of main */