long openPortByName(char** ppszError, char* pszDeviceName, long baudRate, long options) { int hPort = 0; int openFlags = 0; ppszError = malloc(sizeof(char *)); /* do not become the controlling tty */ openFlags = O_RDWR | O_NOCTTY; hPort = open(pszDeviceName, openFlags); if (hPort < 0) { *ppszError = getLastErrorMsg("open failed"); return -1; } /* Set exclusive use flag to block other open calls with EBUSY. */ //ioctl(hPort, TIOCEXCL, 0); configurePort(ppszError, hPort, baudRate, options); if (*ppszError != NULL) { close(hPort); return -1; } return (int)hPort; }
/** * QThread::run() * * Open the serial port and set it up, then starts polling. * */ void KettlerConnection::run() { // Open and configure serial port m_serial = new QSerialPort(); m_serial->setPortName(m_serialPortName); m_timer = new QTimer(); QTimer *startupTimer = new QTimer(); if (!m_serial->open(QSerialPort::ReadWrite)) { qDebug() << "Error opening serial"; this->exit(-1); } else { configurePort(m_serial); // Discard any existing data QByteArray data = m_serial->readAll(); // Set up polling connect(m_timer, SIGNAL(timeout()), this, SLOT(requestAll()),Qt::DirectConnection); // Set up initial model detection connect(startupTimer, SIGNAL(timeout()), this, SLOT(initializePcConnection()),Qt::DirectConnection); } m_timer->setInterval(1000); m_timer->start(); startupTimer->setSingleShot(true); startupTimer->setInterval(0); startupTimer->start(); exec(); }
/** * Open a serial port by logical device name. * * @param pszDeviceName logical name of the port (for example, COM1) * @param baudRate baud rate to set the port at * @param options options for the serial port * bit 0: 0 - 1 stop bit, 1 - 2 stop bits * bit 2-1: 00 - no parity, 01 - odd parity, 10 - even parity * bit 4: 0 - no auto RTS, 1 - set auto RTS * bit 5: 0 - no auto CTS, 1 - set auto CTS * bit 7-6: 01 - 7 bits per symbol, 11 - 8 bits per symbol * @param pHandle returns the connection handle; it is * set only when this function returns PCSL_NET_SUCCESS * @param pContext filled by ptr to data for reinvocations * * @return PCSL_NET_SUCCESS for successful read operation;\n * PCSL_NET_WOULDBLOCK if the operation would block,\n * PCSL_NET_INTERRUPTED for an Interrupted IO Exception,\n * PCSL_NET_IOERROR for all other errors */ int openPortByNameStart(char* pszDeviceName, int baudRate, int options, int *pHandle, void **pContext) { int hPort, openFlags; /* Linux implementation never returns PCSL_NET_WOULDBLOCK */ (void)pContext; /* do not become the controlling tty */ openFlags = O_RDWR | O_NOCTTY; hPort = open(logical_port_name2device_port_name(pszDeviceName), openFlags); if (hPort < 0) { *pHandle = (int)INVALID_HANDLE; return PCSL_NET_IOERROR; } /* Set exclusive use flag to block other open calls with EBUSY. */ ioctl(hPort, TIOCEXCL, 0); if (configurePort(hPort, baudRate, options) != PCSL_NET_SUCCESS) { close(hPort); *pHandle = (int)INVALID_HANDLE; return PCSL_NET_IOERROR; } *pHandle = hPort; return PCSL_NET_SUCCESS; }
int PosixSerial::initializePort() { if(fd_ == -1) { if(openPort() == CROI_ERROR) { return CROI_ERROR; } if(configurePort() == CROI_ERROR) { return CROI_ERROR; } } initialized_ = true; return CROI_SUCCESS; }
/** * Configure a serial port optional parameters. * * @param port device port returned from open * @param baud baud rate to set the port at * @param flags options for the serial port: * bit 0: 0 - 1 stop bit, 1 - 2 stop bits * bit 2-1: 00 - no parity, 01 - odd parity, 10 - even parity * bit 4: 0 - no auto RTS, 1 - set auto RTS * bit 5: 0 - no auto CTS, 1 - set auto CTS * bit 7-6: 01 - 7 bits per symbol, 11 - 8 bits per symbol */ KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_midp_io_j2me_comm_Protocol_native_1configurePort() { unsigned int flags = (int)KNI_GetParameterAsInt(3); int baud = (int)KNI_GetParameterAsInt(2); int port = (int)KNI_GetParameterAsInt(1); int status = configurePort(port, baud, flags); if (status != PCSL_NET_SUCCESS) { midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "Error configure port %d \n", port); REPORT_INFO1(LC_PROTOCOL, "%s\n", gKNIBuffer); KNI_ThrowNew(midpIOException, gKNIBuffer); } KNI_ReturnVoid(); }
/** * This functions takes a serial port and tries if it can find a Monark bike connected * to it. */ bool MonarkConnection::discover(QString portName) { bool found = false; QSerialPort sp; sp.setPortName(portName); if (sp.open(QSerialPort::ReadWrite)) { configurePort(&sp); // Discard any existing data QByteArray data = sp.readAll(); // Read id from bike sp.write("id\r"); sp.waitForBytesWritten(-1); QByteArray id; do { bool readyToRead = sp.waitForReadyRead(1000); if (readyToRead) { id.append(sp.readAll()); } else { id.append('\r'); } } while ((id.indexOf('\r') == -1)); id.replace("\r", "\0"); // Should check for all bike ids known to use this protocol if (QString(id).toLower().contains("lt") || QString(id).toLower().contains("lc") || QString(id).toLower().contains("novo")) { found = true; } } sp.close(); return found; }
int main (int argc, const char** argv) { int dev_fd = -1, res = -1; uint8 buffer[256] = {0}; uint8 pages_used[PIC_NUM_PAGES] = {0}; uint8* bin_buff = NULL; puts("+++++++++++++++++++++++++++++++++++++++++++"); puts(" Pirate-Loader for BP with Bootloader v4+ "); puts(" Loader version: " PIRATE_LOADER_VERSION " OS: " OS_NAME(OS)); puts("+++++++++++++++++++++++++++++++++++++++++++\n"); if( (res = parseCommandLine(argc, argv)) < 0 ) { return -1; } else if( res == 0 ) { return 0; } if( !g_hello_only ) { if( !g_hexfile_path ) { fprintf(stderr, "Please specify hexfile path --hex=/path/to/hexfile.hex\n"); return -1; } bin_buff = (uint8*)malloc(0xFFFFFF * sizeof(uint8)); //256kB if( !bin_buff ) { fprintf(stderr, "Could not allocate 256kB buffer\n"); goto Error; } //fill the buffer with 0xFF memset(bin_buff, 0xFFFFFFFF, (0xFFFFFF * sizeof(uint8))); printf("Parsing HEX file [%s]\n", g_hexfile_path); res = readHEX(g_hexfile_path, bin_buff, (0xFFFFFF * sizeof(uint8)), pages_used); if( res <= 0 || res > flashsize ) { fprintf(stderr, "Could not load HEX file, result=%d\n", res); goto Error; } printf("Found %d words (%d bytes)\n", res, res * 3); //printf("Fixing bootloader/userprogram jumps\n"); //fixJumps(bin_buff, pages_used); } if( g_simulate ) { sendFirmware(dev_fd, bin_buff, pages_used); goto Finished; } if( !g_device_path ) { fprintf(stderr, "Please specify serial device path --dev=/dev/...\n"); return -1; } printf("Opening serial device %s...", g_device_path); dev_fd = openPort(g_device_path, 0); if( dev_fd < 0 ) { puts("ERROR"); fprintf(stderr, "Could not open %s\n", g_device_path); goto Error; } puts("OK"); printf("Configuring serial port settings..."); if( configurePort(dev_fd, B115200) < 0 ) { puts("ERROR"); fprintf(stderr, "Could not configure device, errno=%d\n", errno); goto Error; } puts("OK"); printf("Sending Hello to the Bootloader..."); //send HELLO res = write(dev_fd, BOOTLOADER_HELLO_STR, 1); res = readWithTimeout(dev_fd, buffer, 4, 3); if( res != 4 || buffer[3] != BOOTLOADER_OK ) { puts("ERROR"); fprintf(stderr, "No reply from the bootloader, or invalid reply received: %d\n", res); fprintf(stderr, "Please make sure that PGND and PGC are connected, replug the devide and try again\n"); goto Error; } puts("OK\n"); //extra LF for spacing printf("Bootloader version: %d,%02d\n", buffer[1], buffer[2]); printf("Device ID [%02x]:",buffer[0]); switch(buffer[0]) { // case 0xd4: // printf("PIC24FJ64GA002\n"); // break; case 9: printf("PIC24FJ128GB206\n"); // __PIC24FJ128GB206__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 17: printf("PIC24FJ128GB210\n"); // __PIC24FJ128GB210__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 18: printf("PIC24FJ256GB206\n"); // __PIC24FJ256GB206__ family = IS_24FJ; flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 19: printf("PIC24FJ256GB210\n"); // __PIC24FJ256GB210__ family = IS_24FJ; flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 191: printf("PIC24FJ256DA206\n"); // __PIC24FJ256DA206__ family = IS_24FJ; flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 192: printf("PIC24FJ256DA210\n"); // __PIC24FJ256DA210__ family = IS_24FJ; flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 217: printf("PIC24FJ64GB106\n"); // __PIC24FJ64GB106__ family = IS_24FJ; flashsize = 0xAC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 218: printf("PIC24FJ64GB108\n"); // __PIC24FJ64GB108__ family = IS_24FJ; flashsize = 0xAC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 219: printf("PIC24FJ64GB110\n"); // __PIC24FJ64GB110__ family = IS_24FJ; flashsize = 0xAC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 229: printf("PIC24FJ128GB106\n"); // __PIC24FJ128GB106__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 230: printf("PIC24FJ128GB108\n"); // __PIC24FJ128GB108__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 231: printf("PIC24FJ128GB110\n"); // __PIC24FJ128GB110__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 235: printf("PIC24FJ192GB106\n"); // __PIC24FJ192GB106__ family = IS_24FJ; flashsize = 0x20C00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 236: printf("PIC24FJ192GB108\n"); // __PIC24FJ192GB108__ family = IS_24FJ; flashsize = 0x20C00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 237: printf("PIC24FJ192GB110\n"); // __PIC24FJ192GB110__ family = IS_24FJ; flashsize = 0x20C00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 241: printf("PIC24FJ256GB106\n"); // __PIC24FJ256GB106__ #define IS_24FJ 1 flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 242: printf("PIC24FJ256GB108\n"); // __PIC24FJ256GB108__ family = IS_24FJ; flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 243: printf("PIC24FJ256GB110\n"); // __PIC24FJ256GB110__ family = IS_24FJ; flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 244: printf("PIC24FJ32GB002\n"); // __PIC24FJ32GB002__ family = IS_24FJ; flashsize = 0x5800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 245: printf("PIC24FJ32GB004\n"); // __PIC24FJ32GB004__ family = IS_24FJ; flashsize = 0x5800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 246: printf("PIC24FJ64GB002\n"); // __PIC24FJ64GB002__ family = IS_24FJ; flashsize = 0xAC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 247: printf("PIC24FJ64GB004\n"); // __PIC24FJ64GB004__ family = IS_24FJ; flashsize = 0xAC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 250: printf("PIC24FJ128DA106\n");// __PIC24FJ128DA106__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 251: printf("PIC24FJ128DA110\n"); // __PIC24FJ128DA110__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 252: printf("PIC24FJ128DA206\n"); // __PIC24FJ128DA206__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 253: printf("PIC24FJ128DA210\n"); // __PIC24FJ128DA210__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 254: printf("PIC24FJ256DA106\n"); // __PIC24FJ256DA106__ family = IS_24FJ; flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 255: printf("PIC24FJ256DA110\n"); // __PIC24FJ256DA110__ family = IS_24FJ; flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 206: printf("PIC24FJ16GA002\n"); flashsize = 0x2C00; break; case 207: printf("PIC24FJ16GA004\n"); flashsize = 0x2C00; break; case 208: printf("PIC24FJ32GA002\n"); flashsize = 0x5800; break; case 209: printf("PIC24FJ32GA004\n"); flashsize = 0x5800; break; case 210: printf("PIC24FJ48GA002\n"); flashsize = 0x8400; break; case 211: printf("PIC24FJ48GA004\n"); flashsize = 0x8400; break; case 212: printf("PIC24FJ64GA002\n"); flashsize = 0xAC00; break; case 213: printf("PIC24FJ64GA004\n"); flashsize = 0xAC00; break; case 214: printf("PIC24FJ64GA006\n"); flashsize = 0xAC00; break; printf("PIC24FJ64GA008\n"); flashsize = 0xAC00; case 215: break; case 216: printf("PIC24FJ64GA010\n"); flashsize = 0xAC00; break; case 220: printf("PIC24FJ96GA006\n"); flashsize = 0x10000; break; case 221: printf("PIC24FJ96GA008\n"); flashsize = 0x10000; break; case 222: printf("PIC24FJ96GA010\n"); flashsize = 0x10000; break; case 223: printf("PIC24FJ128GA006\n"); flashsize = 0x15800; break; case 224: printf("PIC24FJ128GA008\n"); flashsize = 0x15800; break; case 225: printf("PIC24FJ128GA010\n"); flashsize = 0x15800; break; case 226: printf("PIC24FJ128GA106\n"); flashsize = 0x15800; break; case 227: printf("PIC24FJ128GA108\n"); flashsize = 0x15800; break; case 228: printf("PIC24FJ128GA110\n"); flashsize = 0x15800; break; case 232: printf("PIC24FJ192GA106\n"); flashsize = 0x20C00; break; case 233: printf("PIC24FJ192GA108\n"); flashsize = 0x20C00; break; case 234: printf("PIC24FJ192GA110\n"); flashsize = 0x20C00; break; case 238: printf("PIC24FJ256GA106\n"); flashsize = 0x2AC00; break; case 239: printf("PIC24FJ256GA108\n"); flashsize = 0x2AC00; break; case 240: printf("PIC24FJ256GA110\n"); flashsize = 0x2AC00; break; default: printf("UNKNOWN"); fprintf(stderr, "Unsupported device (%02x:UNKNOWN)\n", buffer[0]); goto Error; break; } if( !g_hello_only ) { res = sendFirmware(dev_fd, bin_buff, pages_used); if( res > 0 ) { puts("\nFirmware updated successfully :)!"); //printf("Use screen %s 115200 to verify\n", g_device_path); } else { puts("\nError updating firmware :("); goto Error; } } Finished: if( bin_buff ) { free( bin_buff ); } if( dev_fd >= 0 ) { close(dev_fd); } return 0; Error: if( bin_buff ) { free( bin_buff ); } if( dev_fd >= 0 ) { close(dev_fd); } return -1; }
void spiPort::takeOwnership(spiDevice *bossyDevice) { _portOwner = bossyDevice; configurePort(); }
QSerialPort::QSerialPort(QString filename, QObject *parent) : QIODevice(parent), m_name(filename), #ifdef WIN32 m_handle(INVALID_HANDLE_VALUE) #else m_handle(-1) #endif { } QSerialPort::~QSerialPort() { close(); } bool QSerialPort::open(OpenMode) { #ifdef WIN32 COMMTIMEOUTS cto; char realPortname[10]; memset(realPortname, 0, 10); strncpy(realPortname, "\\\\.\\", 4); strncpy(realPortname+4, m_name.toLocal8Bit(), 5); m_handle = CreateFileA(realPortname, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0); if(m_handle == INVALID_HANDLE_VALUE) return false; cto.ReadIntervalTimeout = 0; cto.ReadTotalTimeoutMultiplier = 0; cto.ReadTotalTimeoutConstant = 1; cto.WriteTotalTimeoutMultiplier = 0; cto.WriteTotalTimeoutConstant = 0; if(!SetCommTimeouts(m_handle, &cto)) { CloseHandle(m_handle); m_handle = INVALID_HANDLE_VALUE; return false; } if(!SetupComm(m_handle, 2048, 2048)) { CloseHandle(m_handle); m_handle = INVALID_HANDLE_VALUE; return false; } if(!SetCommMask(m_handle, 0)) { CloseHandle(m_handle); m_handle = INVALID_HANDLE_VALUE; return false; } configurePort(); return QIODevice::open(ReadWrite); #else m_handle = ::open(m_name.toLocal8Bit(), O_RDWR | O_NDELAY | O_NOCTTY); if(m_handle > 0) { ::fcntl(m_handle, F_SETFL, ~O_NDELAY & ::fcntl(m_handle, F_GETFL, 0)); configurePort(); return QIODevice::open(ReadWrite); } #endif return false; }
int main(int argc, char** argv) { int dev_fd = -1, res = -1; uint32_t i; printf("Logic Sniffer ROM loader v0.2 (September 10, 2010)\n"); if( (res = parseCommandLine(argc, argv)) < 0 ) { return -1; } else if( res == 0 ) { return 0; } if( !param_port ) { fprintf(stderr, "Please specify serial port \n"); return -1; } printf("Opening serial port '%s' @ %ld ... ", param_port, param_speed); #ifndef NO_PUMP dev_fd = openPort(param_port, 0); if (dev_fd < 0) { printf("Error opening :(\n"); return -1; } if( configurePort(dev_fd, param_speed) < 0 ) { printf("Error configuring :(\n"); return -1; } #endif printf("OK\n"); #ifndef NO_PUMP if ((res = PUMP_GetID(dev_fd))!=0) { return -1; } #endif #ifndef NO_PUMP if((res = PUMP_GetFlashID(dev_fd))!=0) { return -1; } #else PUMP_FlashID = 0; #endif if (cmd_boot) { #ifndef NO_PUMP PUMP_EnterBootloader(dev_fd); #endif return 0; } // now the PUMP_FlashID contains offset into PUMP_Flash array bin_buf_size = PUMP_Flash[PUMP_FlashID].pages * PUMP_Flash[PUMP_FlashID].page_size * sizeof(uint8_t); bin_buf = (uint8_t*)malloc(bin_buf_size); if (bin_buf == NULL) { printf("Error allocating %ld bytes of memory\n", (long)bin_buf_size); return -1; } memset(bin_buf, 0, bin_buf_size); if (cmd_status) { #ifndef NO_PUMP PUMP_GetStatus(dev_fd); #endif } if (cmd_erase) { #ifndef NO_PUMP PUMP_FlashErase(dev_fd); #endif } if (cmd_write) { uint32_t pages = 0; uint32_t read_size; if(param_write_hex) { printf("Reading HEX file '%s' ... ", param_write_hex); read_size = HEX_ReadFile(param_write_hex, bin_buf, bin_buf_size); } else if(param_write_bin) { printf("Reading BIN file '%s' ... ", param_write_bin); read_size = BIN_ReadFile(param_write_bin, bin_buf, bin_buf_size); } else { printf("no input file specified ! \n"); return -1; } if (read_size == 0) { printf("Error!\n"); return -1; } printf("OK! (binary size = %ld)\n", (long int)read_size); if (param_limit <= 0) { // read_size / page_size .. round up when necessary pages = (read_size+PUMP_Flash[PUMP_FlashID].page_size-1)/PUMP_Flash[PUMP_FlashID].page_size; } else { pages = param_limit; } if (pages > PUMP_Flash[PUMP_FlashID].pages) pages = PUMP_Flash[PUMP_FlashID].pages; printf("Will write %ld pages \n", (long int)pages); for (i = 0; i < pages; i ++) { #ifndef NO_PUMP PUMP_FlashWrite(dev_fd, i, bin_buf + (PUMP_Flash[PUMP_FlashID].page_size * i)); #else printf("Writing from mem=0x%p to page=%ld\n", bin_buf + (PUMP_Flash[PUMP_FlashID].page_size * i), (long int)i); #endif } } if (cmd_read) { uint32_t pages; uint32_t temp_size; if (param_read_hex == NULL && param_read_bin == NULL) { printf("no output file specified ! \n"); return -1; } if (param_limit <= 0) { pages = PUMP_Flash[PUMP_FlashID].pages; } else { pages = param_limit; } temp_size = pages * PUMP_Flash[PUMP_FlashID].page_size; printf("Will read %ld pages \n", (long int)pages); for (i = 0; i < pages; i ++) { #ifndef NO_PUMP PUMP_FlashRead(dev_fd, i, bin_buf + (PUMP_Flash[PUMP_FlashID].page_size * i)); #else printf("Reading from page=%ld to mem=0x%p\n", (long int)i, bin_buf + (PUMP_Flash[PUMP_FlashID].page_size * i)); #endif } if (param_read_hex) { printf("Writing HEX file '%s' ... ", param_read_hex); HEX_WriteFile(param_read_hex, bin_buf, temp_size); printf("done!\n"); } if (param_read_bin) { printf("Writing BIN file '%s' ... ", param_read_bin); BIN_WriteFile(param_read_bin, bin_buf, temp_size); printf("done!\n"); } } if (cmd_run) { #ifndef NO_PUMP PUMP_EnterRunMode(dev_fd); #endif } free(bin_buf); return 0; }