bool init() { if(io_Init() == false) return false; // set ADC Base Address sb_Write(0xBC, sb_Read(0xBC) & (~(1L<<28))); // active adc sb1_Write16(0xDE, sb1_Read16(0xDE) | 0x02); // not Available for 8051A Access ADC sb1_Write(0xE0, 0x00500000UL | ADC_BASEADDR); // baseaddr = 0xfe00, disable irq return true; }
RBAPI(bool) spi_Init2(unsigned baseaddr, int clkmode) { int i; if(spi_InUse() == true) { err_SetMsg(ERROR_SPI_INUSE, "SPI was already opened"); return false; } if ((SPI_ioSection = io_Init()) == -1) return false; //NOTE: base address should be selected carefully to avoid conflicts with other devices! if (baseaddr != 0xffff) spi_SetBaseAddress(baseaddr); else { baseaddr = spi_SetDefaultBaseAddress(); if ((baseaddr == 0x0000) || (baseaddr == 0xffff)) spi_SetBaseAddress(0xfc00); } spidx_DisableCS(); spi_ClearErrors(); // enable FIFO and set clock divisor //if (spi_SetControlREG(0x10 + SPI_CLKMODE[clkmode]) == false) if (spi_SetControlREG(0x10 + get_spidivisor(clkmode)) == false) { spi_Close(); err_SetMsg(ERROR_SPI_INITFAIL, "fail to write SPI Control Register"); return false; } // clear the input buffer if it is not empty for (i=0; i<20; i++) { if (spi_InputReady() == false) break; if (spidx_Read() == 0xffff) { spi_Close(); err_SetMsg(ERROR_SPI_INITFAIL, "fail to clear SPI input buffer"); return false; } } // switch GPIO3[3:0] to external SPI interface OLD_SPIGPIO3FLAG = read_sb_reg(SB_MULTIFUNC_REG); write_sb_reg(SB_MULTIFUNC_REG, OLD_SPIGPIO3FLAG | 1L); OLD_SPIGPIO3FLAG = OLD_SPIGPIO3FLAG & 1L; return true; }
RBAPI(bool) i2c_InitSW2(unsigned devs, int i2c0mode, unsigned long i2c0clkdelay, int i2c1mode, unsigned long i2c1clkdelay) { int i; if (I2C_ioSection != -1) { err_SetMsg(ERROR_I2C_INUSE, "I2C lib was already opened"); return false; } if ((I2C_ioSection = io_Init()) == -1) return false; #ifdef ROBOIO switch (roboio_GetRBVer()) { case RB_100b1: case RB_100b2: case RB_100b3: case RB_100: case RB_100RD: case RB_110: case RB_050: devs = devs & I2C_USEMODULE0; break; default: devs = 0; break; } #endif I2C_swMode[0] = i2c0mode; I2CSW_delay[0] = i2c0clkdelay; I2C_swMode[1] = i2c1mode; I2CSW_delay[1] = i2c1clkdelay; I2C_action[0] = I2C_action[1] = I2CACT_DISABLE; for (i=0; i<2; i++) { if ((i == 0) && ((devs & I2C_USEMODULE0) == 0)) continue; if ((i == 1) && ((devs & I2C_USEMODULE1) == 0)) continue; I2C_action[i] = I2CACT_IDLE; // switch GPIO/I2C pins into GPIO pins OLD_I2CGPIO3FLAG[i] = read_sb_reg(SB_IPFCTRL3_REG) & OLD_I2CGPIO3MASK[i]; // backup GPIO/I2C switch flag write_sb_reg(SB_IPFCTRL3_REG, read_sb_reg(SB_IPFCTRL3_REG) & (~OLD_I2CGPIO3MASK[i])); OLD_I2CGPIO3DIR[i] = io_inpb(GPIO3_DIR) & (0x03 << (i*2+4)); // backup GPIO3 DIR OLD_I2CGPIO3VAL[i] = io_inpb(GPIO3_DATA) & (0x03 << (i*2+4)); // backup GPIO3 VAL // set GPIO pins as INPUT state (equiv. to OUT 1 of I2C lines) io_outpb(GPIO3_DIR, io_inpb(GPIO3_DIR) & ~(0x03 << (i*2+4))); } return true; }
void WIFI_SPIClass::begin() { void *pciDev = NULL; if(io_Init() == false) return; // Get SPI device base address pciDev = pci_Alloc(0x00, 0x10, 0x01); // PCI SPI configuration space if(pciDev == NULL) {printf("WiFi-SPI device don't exist\n"); return;} WIFI_SPI_IOaddr = (unsigned)(pci_In16(pciDev, 0x10) & 0xFFFFFFF0L); // get SPI base address #if defined WIFI_DEBUG_MODE printf("WiFi-SPI base address = %04X\n", WIFI_SPI_IOaddr); #endif pci_Free(pciDev); WIFI_WriteCTRR(WIFI_FULLDUPEX + WIFI_SPI_MODE0 + WIFI_RESET); io_outpb(WIFI_SPI_IOaddr + 7, WIFI_FULLDUPEX); // full-dupex io_outpb(WIFI_SPI_IOaddr + 7, io_inpb(WIFI_SPI_IOaddr + 7) & 0xF1 | WIFI_SPI_MODE0); // set mode io_outpb(WIFI_SPI_IOaddr + 0x0b, 0x08); // delay clk between two transfers //SOURCE clock/(2 * SPI_CLOCK_DIV) setClockDivider(WIFI_SPI_CLOCK_DIV800); // 125k Hz WIFI_useFIFO(); detachInterrupt(); io_outpb(WIFI_SPI_IOaddr + 4, 0x01); // set CS = high // Set SS to high so a connected chip will be "deselected" by default digitalWrite(SS, HIGH); // When the SS pin is set as OUTPUT, it can be used as // a general purpose output port (it doesn't influence // SPI operations). //pinMode(SS, OUTPUT); // Warning: if the SS pin ever becomes a LOW INPUT then SPI // automatically switches to Slave, so the data direction of // the SS pin MUST be kept as OUTPUT. //SPCR |= _BV(MSTR); //SPCR |= _BV(SPE); // Set direction register for SCK and MOSI pin. // MISO pin automatically overrides to INPUT. // By doing this AFTER enabling SPI, we avoid accidentally // clocking in a single bit since the lines go directly // from "input" to SPI control. // http://code.google.com/p/arduino/issues/detail?id=888 //pinMode(SCK, OUTPUT); //pinMode(MOSI, OUTPUT); }
uint8_t *EthernetClass::localMAC() { int i; void *mac_io; if (io_Init() == false) ; else if ((mac_io = io_Alloc(IO_USE_MMIO, 0xFFFFFFB0UL, 0x06UL)) == NULL) io_Close(); else { for (i = 0; i < 6; i++) MAC_address[i] = io_In8(mac_io, i); io_Free(mac_io); io_Close(); } return &MAC_address[0]; }
static bool set_gpio_config_addr(unsigned short addr) { if (io_Init() == false) { err_print((char*)"%s: Init IO lib error!!\n", __FUNCTION__); return false; } if ((gpio_config_addr = sb_Read16(0x62) & 0xfffe) == 0x0000) sb_Write16(0x62, sb_Read16(0x62) | (gpio_config_addr = GPIO_CONFIG_ADDR)); sb_Write16(0x62, sb_Read16(0x62) | 0x0001); // io_outpdw(GPIO_CONFIG_ADDR + 0x00, io_inpdw(GPIO_CONFIG_ADDR + 0x00) | 0x00000004L); if (io_Close() == false) err_print((char*)"%s: Close IO lib error!!\n", __FUNCTION__); return true; }
bool cm_init(void) { if (io_Init() == false) return false; sb_Write(0xc0, sb_Read(0xc0) & 0x7fffffffL | ((unsigned long)1UL << 31)); io_Close(); //com_SetUSBPins(2, 0, 2, 1); if ((Serial = com_Init(COM4)) == NULL) return false; com_SetTimeOut(Serial, 0); com_SetBPS(Serial, COM_UARTBAUD_1000000BPS); com_SetFormat(Serial, BYTESIZE8 + STOPBIT1 + NOPARITY); com_SetFlowControl(Serial, NO_CONTROL); com_EnableFIFO(Serial, FIFO_032); return true; }
/** * \brief Steuerung initialisieren. */ void cntrl_open( void ) { cntrl_cnt = 0; cntrl_TaskFlag_cnt = 0; cntrl_Heartbeat = 0x00; cntrl_Stoerungslampe = 0x00; Debug = 0; KBUSOPEN(); KBUSUPDATE(); sleep(1); MUTEX_lock { io_Init(); err_Init( &cntrl_err ); cntrl_err.i.common_errcnt += param_Init(); zeit_Init( &cntrl_zeit_absenkung, &cntrl_zeit_event ); if( io_Normal != io_ReadT( &io_ALL_Tau_MW ) ) cntrl_err.i.tempsens_errcnt --; task_Init( &cntrl_tau, io_ALL_Tau_MW.messwert ); sol_Init( &cntrl_sol ); fb_Init( &cntrl_fb ); hk_Init( &cntrl_hk ); ww_Init( &cntrl_ww, io_get_WW_WZ_MW() ); kes_Init( &cntrl_kes ); /*----- Module aktivieren ----*/ cntrl_mdl_aktiv.sol_aktiv = SET; cntrl_mdl_aktiv.fb_aktiv = SET; cntrl_mdl_aktiv.hk_aktiv = SET; cntrl_mdl_aktiv.ww_aktiv = SET; cntrl_mdl_aktiv.kes_aktiv = SET; cntrl_mdl_aktiv.err_aktiv = RESET; /* Default erst einmal AUS! */ cntrl_mdl_aktiv.inp_sol_aktiv = SET; cntrl_mdl_aktiv.inp_fb_aktiv = SET; cntrl_mdl_aktiv.inp_hk_aktiv = SET; cntrl_mdl_aktiv.inp_ww_aktiv = SET; cntrl_mdl_aktiv.inp_kes_aktiv = SET; cntrl_mdl_aktiv.inp_err_aktiv = SET; } MUTEX_unlock(); KBUSUPDATE(); sleep(1); }
bool init() { int i; if(io_Init() == false) return false; //set corssbar Base Address sb_Write16(0x64,0x0A01); //printf("SB C0~C3 REG: 0x%08X\n", sb_Read(0xc0)); sb_Write(0xc0, sb_Read(0xc0) | 0x8000C000L); //printf("SB C0~C3 REG: 0x%08X\n", sb_Read(0xc0)); //set GPIO Base Address sb_Write16(0x62,0xf101); // GPIO enable io_outpdw(0xf100,0x00ff); //set GPIO P0~9 dircetion & data Address //io_outpdw(0xf100,0x03ff); for(i=0;i<8;i++) { io_outpdw(0xf100 + (i+1)*4,((0xf202 + i*4)<<16) + 0xf200 + i*4); //io_outpb((sb_Read16(0x64)&0xfffe)+i,0x00); } //setADC Base Address sb_Write(0xbc, sb_Read(0xbc) & (~(1L<<28))); // active adc sb1_Write16(0xde, sb1_Read16(0xde) | 0x02); // not Available for 8051A Access ADC sb1_Write(0xe0, 0x0010fe00L); // baseaddr = 0xfe00, disable irq //CDC USBDEV = CreateUSBDevice(); if(USBDEV == NULL) { printf("init error\n"); return false; } usb_SetUSBPins(USBDEV, 7, 0, 7, 1); if(usb_Init(USBDEV) == false) { printf("init2 error\n"); return false; } //io_Close(); return true; }
RBAPI(bool) spi_InitSW(int mode, unsigned long clkdelay) { if(spi_InUse() == true) { err_SetMsg(ERROR_SPI_INUSE, "SPI was already opened"); return false; } if ((SPI_ioSection = io_Init()) == -1) return false; SPISW_mode = mode; SPISW_delay = clkdelay; // set the initial state of SPI GPIO pins OLD_SPIGPIODIR = io_inpb(GPIO3_DIR); OLD_SPIGPIODATA = io_inpb(GPIO3_DATA); io_outpb(GPIO3_DIR, (OLD_SPIGPIODIR & 0xf0) | 0x07); if ((SPISW_mode & SPIMODE_CPOL1) != 0) io_outpb(GPIO3_DATA, (OLD_SPIGPIODATA & 0xf0) + 0x03); // SPI_CLK = 1, SPI_CS = 1 else io_outpb(GPIO3_DATA, (OLD_SPIGPIODATA & 0xf0) + 0x01); // SPI_CLK = 0, SPI_CS = 1 OLD_SPIGPIODIR = OLD_SPIGPIODIR & 0x0f; OLD_SPIGPIODATA = OLD_SPIGPIODATA & 0x0f; // switch to GPIO interface OLD_SPIGPIO3FLAG = read_sb_reg(SB_MULTIFUNC_REG); write_sb_reg(SB_MULTIFUNC_REG, OLD_SPIGPIO3FLAG & 0xfffffffeL); OLD_SPIGPIO3FLAG = OLD_SPIGPIO3FLAG & 1L; SPISW_active = true; return true; }
DMPAPI(void *) CreateUART(int com) { SerialPort *port; if (io_Init() == false) { err_print((char*)"%s: Init IO lib error!!\n", __FUNCTION__); return NULL; } if ((port = (SerialPort *)ker_Malloc(sizeof(SerialPort))) == NULL) { io_Close(); return NULL; } port->com = com; port->addr = vx86_uart_GetBaseAddr(com); port->nIRQ = vx86_uart_GetIRQ(com); if (port->addr == 0x00 || port->nIRQ == 0) { err_print((char*)"%s: COM%d is null. [Base address: 0x%04x, IRQ: %d]\n", __FUNCTION__, com+1, port->addr, port->nIRQ); ker_Mfree((void*)port); io_Close(); return NULL; } port->nBuad = 115200L; port->nData = BYTESIZE8; port->nStop = STOPBIT1; port->nParity = NOPARITY; port->control = NO_CONTROL; port->InUse = 0; port->INT_InUse = 0; port->rts = 0; port->cts = CTS_OFF; port->xonxoff_rcvd = XOFF_RCVD; port->xonxoff_xmit = XOFF_XMIT; port->xon = 0; port->xoff = 0; port->old_lsb = port->lsb = 0; port->old_msb = port->msb = 0; port->old_ier = port->ier = 0; port->old_lcr = port->lcr = 0; port->old_mcr = port->mcr = 0; port->old_TimeOut = port->TimeOut = UART_NO_TIMEOUT; port->fcr = 0; port->RFIFO_Size = 0; port->WFIFO_Size = 0; if ((port->rcvd = CreateQueue(RX_QUEUE_SIZE)) == NULL) goto CREATE_RX_QUEUE_FAIL; if ((port->xmit = CreateQueue(TX_QUEUE_SIZE)) == NULL) goto CREATE_TX_QUEUE_FAIL; port->msr_handler = NULL; port->lsr_handler = NULL; port->TXDB = port->addr + 0; port->RXDB = port->addr + 0; port->DLLSB = port->addr + 0; port->DLMSB = port->addr + 1; port->IER = port->addr + 1; port->IIR = port->addr + 2; port->FCR = port->addr + 2; port->LCR = port->addr + 3; port->MCR = port->addr + 4; port->LSR = port->addr + 5; port->MSR = port->addr + 6; port->SCR = port->addr + 7; return (void *)port; CREATE_TX_QUEUE_FAIL: DestoryQueue(port->rcvd); CREATE_RX_QUEUE_FAIL: ker_Mfree((void*)port); io_Close(); return NULL; }
RBAPI(bool) i2c_Init2(unsigned baseaddr, unsigned devs, int i2c0irq, int i2c1irq) { int i; if (I2C_ioSection != -1) { err_SetMsg(ERROR_I2C_INUSE, "I2C lib was already opened"); return false; } if ((I2C_ioSection = io_Init()) == -1) return false; //NOTE: base address should be selected carefully to avoid conflicts with other devices! if (baseaddr != 0xffff) i2c_SetBaseAddress(baseaddr); else { if (i2c_SetDefaultBaseAddress() == 0x0000) i2c_SetBaseAddress(0xfb00); } #ifdef ROBOIO switch (roboio_GetRBVer()) { case RB_100b1: case RB_100b2: case RB_100b3: case RB_100: case RB_100RD: case RB_110: case RB_050: devs = devs & I2C_USEMODULE0; i2c1irq = I2CIRQ_DISABLE; break; default: devs = 0; i2c0irq = i2c1irq = I2CIRQ_DISABLE; break; } #endif i2c_SetIRQ(i2c0irq, i2c1irq); I2C_swMode[0] = I2C_swMode[1] = I2CSW_DISABLE; I2C_action[0] = I2C_action[1] = I2CACT_DISABLE; for (i=0; i<2; i++) { if ((i == 0) && ((devs & I2C_USEMODULE0) == 0)) continue; if ((i == 1) && ((devs & I2C_USEMODULE1) == 0)) continue; I2C_action[i] = I2CACT_IDLE; //switch GPIO/I2C pins into GPIO pins OLD_I2CGPIO3FLAG[i] = read_sb_reg(SB_IPFCTRL3_REG) & OLD_I2CGPIO3MASK[i]; //backup GPIO/I2C switch flag write_sb_reg(SB_IPFCTRL3_REG, read_sb_reg(SB_IPFCTRL3_REG) & (~OLD_I2CGPIO3MASK[i])); //send START & STOP signal to reset I2C devices OLD_I2CGPIO3DIR[i] = io_inpb(GPIO3_DIR) & (0x03 << (i*2+4)); //backup GPIO3 DIR OLD_I2CGPIO3VAL[i] = io_inpb(GPIO3_DATA) & (0x03 << (i*2+4)); //backup GPIO3 VAL //set_pins(i, 1, 1); delay_ms(1); //SCL = 1, SDA = 1; START //set_pins(i, 1, 0); delay_ms(1); //SCL = 1, SDA = 0 //set_pins(i, 0, 0); delay_ms(1); //SCL = 0, SDA = 0 //Note: if we send the above START, some I2C sensors, such as ADI ADXL345, may fail to respond //set_pins(i, 0, 0); delay_ms(1); //SCL = 0, SDA = 0; STOP //Note: if we perform the line above, some I2C sensors, such as MEMSIC MXC6202, may fail to respond //set_pins(i, 1, 0); delay_ms(1); //SCL = 1, SDA = 0 //set_pins(i, 1, 1); delay_ms(1); //SCL = 1, SDA = 1 //Note: the above lines work for all sensors we tested, but we disable them to avoid unexpected sensor behaviors if (i2c_Reset(i) == false) // assume the status of GPIO/I2C pins are GPIO "IN" or "OUT 1" { i2c_Close(); err_SetMsg(ERROR_I2C_INITFAIL, "can't reset the I2C modules"); return false; } i2c_DisableINT(i, I2CINT_ALL); i2c_ClearSTAT(i, I2CSTAT_ALL); //Remarks: for DX ver.2, we must disable the noise filter to ensure that 3.3Mbps works in high-speed mode i2c_DisableNoiseFilter(i); //i2c_EnableNoiseFilter(i); i2c_DisableStandardHSM(i); //SCL open-drain in high-speed mode //i2c_EnableStandardHSM(i); i2c_SetSpeed(i, I2CMODE_AUTO, 100000L); //default 100Kbps i2cslave_SetAddr(i, 0x7f); //set slave address 0x7F by default (change this if it collide with external I2C devices) i2cslave_EnableACK(i); //switch GPIO pins into I2C SCL,SDA pins //Remarks: Vortex86DX's H/W I2C has an issue here; if you call i2c_Reset() in case GPIO/SCL pin = GPIO out 0, // then, whenever you switch GPIO/SCL pin to SCL pin, the SCL pin always first send the 10 reset dummy clocks write_sb_reg(SB_IPFCTRL3_REG, read_sb_reg(SB_IPFCTRL3_REG) | OLD_I2CGPIO3MASK[i]); } return true; }
RBAPI(bool) com_Init(int com, int duplex) { if (com_InUse(com) == true) { err_SetMsg(ERROR_COM_INUSE, "COM%d was already opened", com); return false; } #ifdef ROBOIO duplex = (duplex == COM_ADUPLEX)? COM_FDUPLEX : duplex; switch (roboio_GetRBVer()) { case RB_100b1: switch (com) { case COM_PORT1: COM_duplex[com] = (duplex != COM_HDUPLEX_TXDEN)? duplex : COM_HDUPLEX; break; case COM_PORT2: COM_duplex[com] = COM_HDUPLEX_TXDEN; break; case COM_PORT3: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; case COM_PORT4: COM_duplex[com] = COM_HDUPLEX_RTS; break; } break; case RB_100b2: switch (com) { case COM_PORT1: COM_duplex[com] = COM_FDUPLEX; break; case COM_PORT2: COM_duplex[com] = COM_HDUPLEX_TXDEN; break; case COM_PORT3: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; case COM_PORT4: COM_duplex[com] = COM_HDUPLEX_RTS; break; } break; case RB_100b3: switch (com) { case COM_PORT1: COM_duplex[com] = (duplex != COM_HDUPLEX_TXDEN)? duplex : COM_HDUPLEX; break; case COM_PORT2: COM_duplex[com] = COM_HDUPLEX_TXDEN; break; case COM_PORT3: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; case COM_PORT4: COM_duplex[com] = COM_HDUPLEX; break; } break; case RB_100: case RB_100RD: switch (com) { case COM_PORT1: COM_duplex[com] = COM_FDUPLEX; break; case COM_PORT2: COM_duplex[com] = COM_HDUPLEX_TXDEN; break; case COM_PORT3: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; case COM_PORT4: COM_duplex[com] = COM_HDUPLEX; break; } break; case RB_110: case RB_050: switch (com) { case COM_PORT1: COM_duplex[com] = COM_FDUPLEX; break; case COM_PORT2: COM_duplex[com] = COM_HDUPLEX_TXDEN; break; case COM_PORT3: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; case COM_PORT4: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; } break; default: err_SetMsg(ERROR_RBVER_UNKNOWN, "unrecognized RoBoard"); return false; } #else COM_duplex[com] = duplex; #endif if((COM_ioSection[com] = io_Init()) == -1) return false; if(uart_isenabled(com) == false) { err_SetMsg(ERROR_COM_INVALID, "COM%d isn't enabled in BIOS", com); goto COMINIT_FAIL; } COM_baseaddr[com] = uart_getbaseaddr(com); COM_oldTMode[com] = com_IsTurboMode(com); COM_oldFMode[com] = com_IsFIFO32Mode(com); #if defined(RB_MSVC_WIN32) || defined(RB_MSVC_WINCE) { #ifdef RB_MSVC_WINCE int idx = com; #else int i, idx; // find the device name of the COM port for (idx=0, i=0; i<com; i++) if (uart_isenabled(i) == true) idx++; #endif COM_info[com].fp = CreateFile( COM_portname[idx], // device name of COM port GENERIC_READ | GENERIC_WRITE, // access mode 0, // share mode 0, // security attributes OPEN_EXISTING, // opens a device only if it exists 0, // non-overlapped NULL); // NULL when opening an existing file if (COM_info[com].fp == INVALID_HANDLE_VALUE) { err_SetMsg(ERROR_COM_FAIL, "cannot open COM%d device driver", com); goto COMINIT_FAIL; } // backup the old DCB if (GetCommState(COM_info[com].fp, &(COM_info[com].oldstate)) == FALSE) { err_SetMsg(ERROR_COM_FAIL, "fail to get DCB settings"); goto COMINIT_FAIL2; } memcpy(&(COM_info[com].newstate), &(COM_info[com].oldstate), sizeof(DCB)); // set new DCB COM_info[com].newstate.fBinary = TRUE; // binary mode COM_info[com].newstate.fOutxCtsFlow = FALSE; // no CTS output control COM_info[com].newstate.fOutxDsrFlow = FALSE; // no DSR output control COM_info[com].newstate.fDtrControl = DTR_CONTROL_DISABLE; // no DRT control COM_info[com].newstate.fDsrSensitivity = FALSE; // no sensitive to DSR COM_info[com].newstate.fOutX = FALSE; // no S/W output flow control COM_info[com].newstate.fInX = FALSE; // no S/W input flow control COM_info[com].newstate.fErrorChar = FALSE; // no replace parity-error byte COM_info[com].newstate.fNull = FALSE; // no discard NULL byte COM_info[com].newstate.fRtsControl = RTS_CONTROL_DISABLE; // no S/W input flow control COM_info[com].newstate.fAbortOnError = FALSE; // no terminate on errors if (SetCommState(COM_info[com].fp, &(COM_info[com].newstate)) == FALSE) { err_SetMsg(ERROR_COM_FAIL, "fail to set DCB settings"); goto COMINIT_FAIL2; } // get old timeout parameters if (GetCommTimeouts(COM_info[com].fp, &(COM_info[com].oldtimeouts)) == FALSE) { err_SetMsg(ERROR_COM_FAIL, "fail to get TIMEOUTS settings"); goto COMINIT_FAIL3; } // set timeout parameters (no waiting on read/write) COM_info[com].newtimeouts.ReadIntervalTimeout = MAXDWORD; COM_info[com].newtimeouts.ReadTotalTimeoutConstant = 0; COM_info[com].newtimeouts.ReadTotalTimeoutMultiplier = 0; COM_info[com].newtimeouts.WriteTotalTimeoutConstant = 0; COM_info[com].newtimeouts.WriteTotalTimeoutMultiplier = 0; if (SetCommTimeouts(COM_info[com].fp, &(COM_info[com].newtimeouts)) == FALSE) { err_SetMsg(ERROR_COM_FAIL, "fail to set TIMEOUT parameters"); goto COMINIT_FAIL3; } ClearCommBreak(COM_info[com].fp); ClearCommError(COM_info[com].fp, NULL, NULL); // clear all communication errors SetupComm(COM_info[com].fp, 8192, 8192); // set read/write FIFO to 8KB PurgeComm(COM_info[com].fp, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR); // clear all communication buffers } #elif defined(RB_LINUX) if ((COM_info[com].fp = open(COM_portname[com], O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) { err_SetMsg(ERROR_COM_FAIL, "cannot open COM%d device driver", com); goto COMINIT_FAIL; } // backup the old termios settings if (tcgetattr(COM_info[com].fp, &(COM_info[com].oldstate)) < 0) { err_SetMsg(ERROR_COM_FAIL, "fail to get termios settings"); goto COMINIT_FAIL2; } memcpy(&(COM_info[com].newstate), &(COM_info[com].oldstate), sizeof(termios)); // set new termios settings COM_info[com].newstate.c_cflag |= CLOCAL | CREAD; COM_info[com].newstate.c_cflag &= ~CRTSCTS; // disable H/W flow control COM_info[com].newstate.c_lflag &= ~(ICANON | // raw mode ISIG | // disable SIGxxxx signals IEXTEN | // disable extended functions ECHO | ECHOE); // disable all auto-echo functions COM_info[com].newstate.c_iflag &= ~(IXON | IXOFF | IXANY); // disable S/W flow control COM_info[com].newstate.c_oflag &= ~OPOST; // raw output COM_info[com].newstate.c_cc[VTIME] = 0; // no waiting to read COM_info[com].newstate.c_cc[VMIN] = 0; if(tcsetattr(COM_info[com].fp, TCSANOW, &(COM_info[com].newstate)) < 0) { err_SetMsg(ERROR_COM_FAIL, "fail to set termios settings"); goto COMINIT_FAIL2; } // clear input/output buffers tcflush(COM_info[com].fp, TCIOFLUSH); #else // TODO ... err_SetMsg(ERROR_COM_INVALID, "unsupported platform"); goto COMINIT_FAIL; #endif if (COM_duplex[com] == COM_HDUPLEX_RTS) clear_rts(com); // set COM direction as input com_SetFormat(com, COM_BYTESIZE8, COM_STOPBIT1, COM_NOPARITY); // default data format: 8 bits, 1 stop bit, no parity com_SetBaud(com, COMBAUD_115200BPS); // default baudrate: 115200 bps com_EnableFIFO32(com); // set Vortex86DX's UART FIFO as 32 bytes return true; #if defined(RB_MSVC_WIN32) || defined(RB_MSVC_WINCE) COMINIT_FAIL3: SetCommState(COM_info[com].fp, &(COM_info[com].oldstate)); COMINIT_FAIL2: CloseHandle(COM_info[com].fp); #elif defined(RB_LINUX) COMINIT_FAIL2: close(COM_info[com].fp); #endif COMINIT_FAIL: io_Close(COM_ioSection[com]); COM_ioSection[com] = -1; return false; }
RBAPI(bool) i2c_Initialize2(unsigned devs, int i2c0irq, int i2c1irq) { int i; if (I2C_ioSection != -1) { err_SetMsg(ERROR_I2C_INUSE, "I2C lib was already opened"); return false; } if ((I2C_ioSection = io_Init()) == -1) return false; if (i2c_SetDefaultBaseAddress() == 0x0000) i2c_SetBaseAddress(0xfb00); i2c_SetIRQ(i2c0irq, i2c1irq); I2C_action[0] = I2C_action[1] = I2CACT_DISABLE; for (i=0; i<2; i++) { if ((i == 0) && ((devs & I2C_USEMODULE0) == 0)) continue; if ((i == 1) && ((devs & I2C_USEMODULE1) == 0)) continue; I2C_action[i] = I2CACT_IDLE; //switch GPIO/I2C pins into GPIO pins OLD_I2CGPIO3FLAG[i] = read_sb_reg(SB_IPFCTRL3_REG) & OLD_I2CGPIO3MASK[i]; //backup GPIO/I2C switch flag write_sb_reg(SB_IPFCTRL3_REG, read_sb_reg(SB_IPFCTRL3_REG) & (~OLD_I2CGPIO3MASK[i])); //send START & STOP signal to reset I2C devices OLD_I2CGPIO3DIR[i] = io_inpb(0x9b) & (0x03 << (i*2+4)); //backup GPIO3 DIR OLD_I2CGPIO3VAL[i] = io_inpb(0x7b) & (0x03 << (i*2+4)); //backup GPIO3 VAL //set_pins(i, 1, 1); delay_ms(1); //SCL = 1, SDA = 1; START //set_pins(i, 1, 0); delay_ms(1); //SCL = 1, SDA = 0 //set_pins(i, 0, 0); delay_ms(1); //SCL = 0, SDA = 0 //Note: if we send the above START, some I2C sensors, such as ADI ADXL345, may fail to respond //set_pins(i, 0, 0); delay_ms(1); //SCL = 0, SDA = 0; STOP //Note: if we perform the line above, some I2C sensors, such as MEMSIC MXC6202, may fail to respond set_pins(i, 1, 0); delay_ms(1); //SCL = 1, SDA = 0 set_pins(i, 1, 1); delay_ms(1); //SCL = 1, SDA = 1 if (i2c_Reset(i) == false) { i2c_Close(); err_SetMsg(ERROR_I2C_INITFAIL, "can't reset the I2C modules"); return false; } i2c_DisableINT(i, I2CINT_ALL); i2c_ClearSTAT(i, I2CSTAT_ALL); //Remarks: for DX ver.2, we must disable the noise filter to ensure that 3.3Mbps works in high-speed mode i2c_DisableNoiseFilter(i); //i2c_EnableNoiseFilter(i); i2c_DisableStandardHSM(i); //SCL open-drain in high-speed mode //i2c_EnableStandardHSM(i); i2c_SetSpeed(i, I2CMODE_AUTO, 100000L); //default 100Kbps i2cslave_SetAddr(i, 0x7f); //set slave address 0x7F by default (change this if it collide with external I2C devices) i2cslave_EnableACK(i); //switch GPIO pins into I2C SCL,SDA pins //Remarks: Vortex86DX's H/W I2C has an issue here; if you call i2c_Reset() in case GPIO/SCL pin = GPIO out 0, // then, whenever you switch GPIO/SCL pin to SCL pin, the SCL pin always first send the 10 reset dummy clocks write_sb_reg(SB_IPFCTRL3_REG, read_sb_reg(SB_IPFCTRL3_REG) | OLD_I2CGPIO3MASK[i]); } return true; }
bool init() { int i, crossbarBase, gpioBase; if(io_Init() == false) return false; timer_NowTime(); // initialize timer CLOCKS_PER_MICROSEC = vx86_CpuCLK(); VORTEX86EX_CLOCKS_PER_MS = CLOCKS_PER_MICROSEC*1000UL; // Set IRQ4 as level-trigger io_outpb(0x4D0, io_inpb(0x4D0) | 0x10); //set corssbar Base Address crossbarBase = sb_Read16(SB_CROSSBASE) & 0xfffe; if(crossbarBase == 0 || crossbarBase == 0xfffe) sb_Write16(SB_CROSSBASE, CROSSBARBASE | 0x01); // Force set HIGH speed ISA on SB sb_Write(SB_FCREG, sb_Read(SB_FCREG) | 0x8000C000L); //set SB GPIO Base Address gpioBase = sb_Read16(SB_GPIOBASE) & 0xfffe; if(gpioBase == 0 || gpioBase == 0xfffe) { sb_Write16(SB_GPIOBASE, GPIOCTRLBASE | 0x01); gpioBase = GPIOCTRLBASE; } // Enable GPIO 0 ~ 7 io_outpdw(gpioBase, 0x00ff); // set GPIO Port 0~7 dircetion & data Address for(i=0;i<8;i++) io_outpdw(gpioBase + (i+1)*4,((GPIODIRBASE + i*4)<<16) + GPIODATABASE + i*4); // set ADC Base Address sb_Write(0xbc, sb_Read(0xbc) & (~(1L<<28))); // active adc sb1_Write16(0xde, sb1_Read16(0xde) | 0x02); // not Available for 8051A Access ADC sb1_Write(0xe0, 0x0050fe00L); // baseaddr = 0xfe00, disable irq // set MCM Base Address set_MMIO(); mc_setbaseaddr(); for(i=0; i<4; i++) mc_SetMode(i, MCMODE_PWM_SIFB); if(Global_irq_Init == false) { // set MCM IRQ if(irq_Init() == false) { printf("MCM IRQ init fail\n"); return false; } if(irq_Setting(GetMCIRQ(), IRQ_LEVEL_TRIGGER + IRQ_DISABLE_INTR) == false) { printf("MCM IRQ Setting fail\n"); return false; } Set_MCIRQ(GetMCIRQ()); Global_irq_Init = true; } //CDC USBDEV = CreateUSBDevice(); if(USBDEV == NULL) { printf("init error\n"); return false; } usb_SetUSBPins(USBDEV, 7, 0, 7, 1); usb_SetTimeOut(USBDEV, 0L, 500L); // USB RX timerout is 0ms and TX timeout is 500ms if(usb_Init(USBDEV) == false) { printf("init2 error\n"); return false; } //io_Close(); return true; }
DMPAPI(void *) CreateUSBDevice(void) { USB_Device *usb = NULL; if ((usb = (USB_Device *)ker_Malloc(sizeof(USB_Device))) == NULL) return NULL; if (io_Init() == false) { err_print((char*)"%s: Init IO lib error!!\n", __FUNCTION__); ker_Mfree((void *)usb); return NULL; } usb->addr = vx86_GetUSBDevAddr(); usb->nIRQ = vx86_GetUSBDevIRQ(); if (usb->addr == 0x0000 || usb->nIRQ == 0) { io_Close(); ker_Mfree((void *)usb); return NULL; } usb->DevAddr = 0x00; usb->ReadySetAddr = false; usb->state = USB_DEV_NOT_ATTACHED; usb->stall = false; usb->InUse = 0; usb->IsSet = 0; usb->setup_in_handled = false; usb->setup_out_handled = false; usb->bulk_in_transmitting = false; usb->TimeOut = USB_NO_TIMEOUT; usb->InDataPtr = NULL; usb->OutDataPtr = NULL; usb->InDataSize = 0; usb->OutDataSize = 0; if ((usb->rcvd = CreateQueue(RX_QUEUE_SIZE)) == NULL) goto CREATE_RX_QUEUE_FAIL; if ((usb->xmit = CreateQueue(TX_QUEUE_SIZE)) == NULL) goto CREATE_TX_QUEUE_FAIL; usb->Setup.bmRequestType = 0; usb->Setup.bRequest = 0; usb->Setup.wValue.Value = 0; usb->Setup.wIndex.Value = 0; usb->Setup.wLength = 0; usb->ling_coding.dwDTERate = 0; usb->ling_coding.bCharFormat = 0; usb->ling_coding.bParityType = 0; usb->ling_coding.bDataBits = 0; usb->control_line_state = 0; usb->serial_state = 0; usb->DAR = usb->addr + 0x00; usb->CFR = usb->addr + 0x02; usb->FNR = usb->addr + 0x06; usb->IER = usb->addr + 0x08; usb->ISR = usb->addr + 0x0C; usb->TMR = usb->addr + 0x68; memset((Endpoint *)usb->EP, 0, sizeof(usb->EP)); usb->EP[0].CtrlTR = usb->addr + 0x10; usb->EP[1].OutTR = usb->addr + 0x12; usb->EP[1].InTR = usb->addr + 0x14; usb->EP[2].OutTR = usb->addr + 0x16; usb->EP[2].InTR = usb->addr + 0x18; usb->EP[3].OutTR = usb->addr + 0x1A; usb->EP[3].InTR = usb->addr + 0x1C; usb->EP[0].SetupDLR = usb->addr + 0x20; usb->EP[0].OutDLR = usb->addr + 0x24; usb->EP[0].InDLR = usb->addr + 0x28; usb->EP[1].OutDLR = usb->addr + 0x2C; usb->EP[1].InDLR = usb->addr + 0x30; usb->EP[2].OutDLR = usb->addr + 0x34; usb->EP[2].InDLR = usb->addr + 0x38; usb->EP[3].OutDLR = usb->addr + 0x3C; usb->EP[3].InDLR = usb->addr + 0x40; usb->EP[0].SetupDSR = usb->addr + 0x44; usb->EP[0].OutDSR = usb->addr + 0x48; usb->EP[0].InDSR = usb->addr + 0x4C; usb->EP[1].OutDSR = usb->addr + 0x50; usb->EP[1].InDSR = usb->addr + 0x54; usb->EP[2].OutDSR = usb->addr + 0x58; usb->EP[2].InDSR = usb->addr + 0x5C; usb->EP[3].OutDSR = usb->addr + 0x60; usb->EP[3].InDSR = usb->addr + 0x64; #ifdef DMP_86DUINO_MODE set_gpio_config_addr(GPIO_CONFIG_ADDR); // for 86duino set_tx_led(7, 2); set_rx_led(7, 3); TX_LED_OFF(); RX_LED_OFF(); #endif return (void *)usb; CREATE_TX_QUEUE_FAIL: DestoryQueue(usb->rcvd); CREATE_RX_QUEUE_FAIL: io_Close(); ker_Mfree((void *)usb); return NULL; }