TLC5946PRUSSphy::TLC5946PRUSSphy(SPI *spi, GPIOpin *ctrl, char *pruBinFile) : TLC5946phy(spi, ctrl) { use_pruss = true; //active has been set to true by superclass constructor active = false; gsclk_pin_pin = ctrl->findPinIndex((char *) "gsclk"); printf("prussdrv_open()\n"); if (prussdrv_open(PRU_EVTOUT_0)) { iooo_debug(0, "Cannot setup PRU_EVTOUT_0.\n"); use_pruss = false; return; } iooo_debug(2, "PRUSS driver initialized"); if (use_pruss && prussdrv_exec_program(0, pruBinFile)) { iooo_debug(0, "Non-zero result from prussdrv_pruintc_init\n"); use_pruss = false; return; } iooo_debug(2, "PRU binary loaded"); prussdrv_exit(); //setup clocking lines as outputs //ctrl->enableOutput(blank_pin_pin,true); ctrl->enableOutput(gsclk_pin_pin, true); active = true; use_pruss = true; }
bool EEPROM24CX::waitForCompletion() { size_t addr = 0; char temp[1]; bool success = false; double start = clock(); double elapsed = 0; iooo_debug(3, "Checking if EEPROM is ready...\n"); do { int rc = handle->writeRead(&addr, addressLength, temp, 1, false, false, false); success = rc == 1; if (!success) { iooo_debug(3, "Waiting...\n"); // Upper bound on most EEPROMs is 5ms usleep(5000); } elapsed = ((double) clock() - start) / CLOCKS_PER_SEC; } while (!success && elapsed < READ_TIMEOUT_SECS); return success; }
HD44780gpioPhy::HD44780gpioPhy(GPIOpin *wires) : HD44780phy() { this->wires = wires; for (int i = 0; i < 8; i++) { d[i] = -1; e[i] = -1; } rs = -1; rw = -1; n = 0; writeReady = false; rs = wires->findPinIndex((char *) "RS"); rw = wires->findPinIndex((char *) "RW"); e[0] = wires->findPinIndex((char *) "E"); n = 1; char buf[10]; if (e[0] < 0) { for (n = 0; n < 8; n++) { sprintf(buf, "E[%i]", n); e[n] = wires->findPinIndex(buf); if (e[n] < 0) break; } } for (int i = 0; i < 8; i++) { sprintf(buf, "D[%i]", i); d[i] = wires->findPinIndex(buf); iooo_debug(2, "HD44780gpioPhy::HD44780gpioPhy(): Index for pin d[%i] is %i\n", i, d[i]); if (d[i] < 0) { iooo_debug(2, "HD44780gpioPhy::HD44780gpioPhy() Data line search interrupted at i=%i\n", i); break; } if (i == 7) bits = 8; else if (i >= 3) bits = 4; } iooo_debug(2, "HD44780gpioPhy::HD44780gpioPhy(): detected %i bit interface\n", bits); if (bits == 0) return; wires->enableOutput(true); writeReady = true; }
void HD44780gpioPhy::write(uint8_t n, uint8_t x) { if (bits < 4) return; // if output buffers are disabled, enable them if (!writeReady) { wires->enableOutput(d, bits); writeReady = true; } //set write mode setRW(0); if (bits == 4) { iooo_debug(3, "Writing 0x%02x in 4 bit mode\n", x); //write higher nibble setNibble((x >> 4) & 0x0f); setE(n, 1); usleep(100); setE(n, 0); usleep(200); //write lower nibble setNibble(x & 0x0f); setE(n, 1); usleep(100); setE(n, 0); }
int SPI::close() { // Check if chip select and mutex lock resources have been initialized if (resources == nullptr) { iooo_error("SPI::close(): failed - no device has been opened\n"); return -EDESTADDRREQ; } // Mutex lock on this bus and channel std::lock_guard<std::recursive_mutex> lock(resources->rwlock); if (!isReady()) { active_bus = active_channel = -1; resources = nullptr; return -ENODEV; } iooo_debug(3, "SPI::close()\n"); mode = 0; bpw = 0; speed = 0; active_bus = active_channel = -1; resources = nullptr; int tmpfd = fd; fd = -1; return ::close(tmpfd); }
void TLC5946PRUSSphy::setBlank(uint8_t blank) { if (!active /*|| (blank_pin_addr < 0)*/) return; if (use_pruss) { iooo_debug(0, "TLC5946phy::setBlank(): Blanking with clock generated by PRUSS not supported yet.\n"); } else { TLC5946phy::setBlank(blank); } }
long int EEPROM24CX::write(size_t pos, size_t size, const void* wbuf) { if (eepromSize != EEPROM_UNKNOWN && pos >= eepromSize) { iooo_error( "EEPROM24CX::write() error: Tried to write past end of memory.\n"); errno = EINVAL; return -1; } if (eepromSize != EEPROM_UNKNOWN && pos + size >= eepromSize) { iooo_error( "EEPROM24CX::write() warning: Trying to write past end of memory. Data truncated.\n"); size = eepromSize - pos; } // Do in page-sized chunks int i = 0; int remaining = size; int written = 0; do { if (waitForCompletion()) { size_t offset = i * pageSize; size_t addr = pos + offset; // Create a pointer to the data at the current page offset const void *data = &((const unsigned char *) wbuf)[offset]; size_t writeSize = pageSize > remaining ? remaining : pageSize; iooo_debug(4, "Writing page of length %d to 0x%x\n", writeSize, addr); int w; if (addressLength == EEPROM_8_ADDR) { uint8_t a = addr; w = handle->writeWrite(&a, EEPROM_8_ADDR, data, writeSize); } else if (addressLength == EEPROM_16_ADDR) { uint16_t a = htons(addr); w = handle->writeWrite(&a, EEPROM_16_ADDR, data, writeSize); } else { uint32_t a = htonl(addr); w = handle->writeWrite(&a, addressLength, data, writeSize); } if (w < 0) written = w; else written += w; remaining -= pageSize; } else { written = -1; } i++; } while (remaining > 0 && written >= 0); return written; }
TLC5946PRUSSphy::~TLC5946PRUSSphy() { iooo_debug(2, "TLC5946PRUSSphy::~TLC5946PRUSSphy()"); }
int SPI::open(int bus, int channel) { // Check for valid bus and channel if (bus < 0 || channel < 0) return -ENODEV; // Obtain locking and chip select resources SharedResources *resources_tmp = &share[bus][channel]; std::lock_guard<std::recursive_mutex> lock(resources_tmp->rwlock); // If a device is already open, close it before continuing further if (active_bus >= 0) close(); iooo_debug(3, "SPI::open(): bus=%d, channel=%d\n", bus, channel); char path[MAX_PATH_LEN]; if (snprintf(path, MAX_PATH_LEN, "%s%d.%d", SPI_DEVICE_PATH_BASE, KERNEL_BUS(bus), channel) >= MAX_PATH_LEN) return -EINVAL; if ((fd = ::open(path, O_RDWR, 0)) < 0) { iooo_error("open(%s) failed\n", path); return fd; } uint8_t tmp; uint32_t tmp32; int r; if ((r = ioctl(fd, SPI_IOC_RD_MODE, &tmp)) < 0) { iooo_error("ioctl(fd, SPI_IOC_RD_MODE, &tmp) failed\n"); return r; } mode = tmp; if ((r = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &tmp)) < 0) { iooo_error("ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &tmp) failed\n"); return r; } bpw = tmp; if ((r = ioctl(fd, SPI_IOC_RD_LSB_FIRST, &tmp)) < 0) { iooo_error("ioctl(fd, SPI_IOC_WR_LSB_FIRST, &tmp) failed\n"); return r; } this->lsb_first = lsb_first; if ((r = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &tmp32)) < 0) { iooo_error("ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &tmp) failed\n"); return r; } speed = tmp32; active_bus = bus; active_channel = channel; resources = resources_tmp; return 1; }
SPI::~SPI() { iooo_debug(4, "SPI::~SPI()\n"); close(); }