From 7e580c745a99228d9a857d0ed7fdf22a28c3effe Mon Sep 17 00:00:00 2001 From: Alexandre Maurer Date: Mon, 4 Mar 2024 10:07:44 +0100 Subject: [PATCH] bump to 1.0.1 --- LICENSE | 2 +- README.md | 7 +-- library.json | 13 +++-- library.properties | 2 +- src/SC16IS7X0.cpp | 142 ++++++++++++++++----------------------------- 5 files changed, 63 insertions(+), 103 deletions(-) diff --git a/LICENSE b/LICENSE index 74114bf..6e8598a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright 2022 Alexandre Maurer alexmaurer@madis.ch +Copyright 2024 Alexandre Maurer alexmaurer@madis.ch Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index c86c83e..e86872c 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,14 @@ # Add an additional hardware UART to your Arduino project. -DEV BRANCH OF LIBRARY - -| Feature | Tested ESP8266 | Tested ESP32 | Tester | +| New feature | Tested ESP8266 | Tested ESP32 | Tester | | :---------------- | :------------- | :----------- | :----- | -| I2C communication | - | - | - | +| I2C communication | not tested | not tested | - | # Contributors - The dev branch was created and committed with the fork of asantiagod https://github.com/asantiagod/SC16IS7X0 +- The dev branch has been merged into master on the 2024-03-04 # Compatibility and dependencies diff --git a/library.json b/library.json index 181af92..87cd88e 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "SC16IS7X0", - "version": "1.0.0-dev", + "version": "1.0.1", "keywords": "UART GPIO SC16IS740 SC16IS750 SC16IS760", "description": "Library to read and write UART and GPIO for the SC16IS740_750_760", "authors": { @@ -21,6 +21,11 @@ }, "license": "MIT", "frameworks": "arduino", - "platforms": ["espressif8266", "espressif32"], - "headers": ["SC16IS7X0.h"] -} + "platforms": [ + "espressif8266", + "espressif32" + ], + "headers": [ + "SC16IS7X0.h" + ] +} \ No newline at end of file diff --git a/library.properties b/library.properties index d67ee5d..dcc80f1 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SC16IS7X0 -version=1.0.0-dev +version=1.0.1 author=Alexandre Maurer, alexmaurer@madis.ch maintainer=Alexandre Maurer, alexmaurer@madis.ch sentence=Library to read and write UART and GPIO for the SC16IS740_750_760 diff --git a/src/SC16IS7X0.cpp b/src/SC16IS7X0.cpp index a4ea7cd..e57a3a5 100644 --- a/src/SC16IS7X0.cpp +++ b/src/SC16IS7X0.cpp @@ -5,8 +5,8 @@ * @details Only SPI interface for the moment. Does currently not implement the usage of IRQ pin, IrDA, RS485 RTS control and 9-bit mode. * - * @version 1.0.0 - * @date 2022-11-04 + * @version 1.0.1 + * @date 2023-03-04 * * @copyright MIT License @@ -19,13 +19,9 @@ * * @param crystalClock Frequence in Hz of the XTAL1 */ -SC16IS7X0::SC16IS7X0(uint32_t xtalFreq) : _mcr(0x00), - _lcr(0x03), - _efr(0x00), - _ioDir(0x00), - _ioState(0x00), - busIo(nullptr) -{ +SC16IS7X0::SC16IS7X0(uint32_t xtalFreq) + : _mcr(0x00), _lcr(0x03), _efr(0x00), _ioDir(0x00), _ioState(0x00), + busIo(nullptr) { assert(xtalFreq > 0); _xtalFreq = xtalFreq; } @@ -38,24 +34,22 @@ SC16IS7X0::SC16IS7X0(uint32_t xtalFreq) : _mcr(0x00), @return true if initialization successful, otherwise false. */ /**************************************************************************/ -bool SC16IS7X0::begin_SPI(uint8_t cs_pin, SPIClass *theSPI) -{ - return setBusIo(SC16IS7X0_BusIo::buildSPI(cs_pin, 4000000, SPI_BITORDER_MSBFIRST, - SPI_MODE0, theSPI)); +bool SC16IS7X0::begin_SPI(uint8_t cs_pin, SPIClass *theSPI) { + return setBusIo(SC16IS7X0_BusIo::buildSPI( + cs_pin, 4000000, SPI_BITORDER_MSBFIRST, SPI_MODE0, theSPI)); } /** - * @brief Initialize using of hardware I2C + * @brief Initialize using of hardware I2C * @param addr Address assigned to the I2C device * @param theWire Pointer to I2C instance * @return true Initialization was successful * @return false Initialization failed - * - * @warning The addresses showed in the table 32 of the - * data-sheet must be right-shifted one bite + * + * @warning The addresses showed in the table 32 of the + * data-sheet must be right-shifted one bit */ -bool SC16IS7X0::begin_I2C(uint8_t addr, TwoWire *theWire) -{ +bool SC16IS7X0::begin_I2C(uint8_t addr, TwoWire *theWire) { return setBusIo(SC16IS7X0_BusIo::buildI2C(addr, theWire)); } @@ -65,8 +59,7 @@ bool SC16IS7X0::begin_I2C(uint8_t addr, TwoWire *theWire) * @param baudrate Desired baudrate * @param config Serial configuration */ -void SC16IS7X0::begin_UART(unsigned long baudrate, SerialConfig config) -{ +void SC16IS7X0::begin_UART(unsigned long baudrate, SerialConfig config) { // Enable enhanced function to be able to change the clock prescaler enableEnhancedFunctions(); // Enable embedded 64 bytes FIFO for RX and TX @@ -99,8 +92,7 @@ void SC16IS7X0::begin_UART(unsigned long baudrate, SerialConfig config) * * @param baudrate new baudrate value in Hz. max 5MHz */ -void SC16IS7X0::updateBaudRate(unsigned long baudrate) -{ +void SC16IS7X0::updateBaudRate(unsigned long baudrate) { assert(baudrate > 0 && baudrate <= 5000000); /** @@ -129,21 +121,17 @@ void SC16IS7X0::updateBaudRate(unsigned long baudrate) // Serial.println(remainder4); uint8_t lastTry = false; - if (divisor1 == 0 || divisor1 > 0xFFFF) - { + if (divisor1 == 0 || divisor1 > 0xFFFF) { // Bad divisor lastTry = true; - } - else if (remainder1 == 0) - { + } else if (remainder1 == 0) { // Perfect match writeDivisorAndPrescaler(divisor1, DIVIDE_BY_1); return; } // Second try with /4 prescaler - if (divisor4 == 0 || divisor4 > 0xFFFF) - { + if (divisor4 == 0 || divisor4 > 0xFFFF) { // Bad divisor // No other possibility @@ -167,8 +155,7 @@ void SC16IS7X0::updateBaudRate(unsigned long baudrate) * @brief Enable enhanced functions * */ -void SC16IS7X0::enableEnhancedFunctions(void) -{ +void SC16IS7X0::enableEnhancedFunctions(void) { uint8_t request[2]; // Set LCR Register to 0xBF to access Enhanced register set @@ -192,8 +179,7 @@ void SC16IS7X0::enableEnhancedFunctions(void) * @brief Enable TX and RX 64 bytes FIFO * */ -void SC16IS7X0::enableFIFO(void) -{ +void SC16IS7X0::enableFIFO(void) { uint8_t request[2]; request[0] = SC16IS7X0_FCR << 3; @@ -205,8 +191,7 @@ void SC16IS7X0::enableFIFO(void) * @brief Enable internal loopback mode * */ -void SC16IS7X0::enableLoopback(void) -{ +void SC16IS7X0::enableLoopback(void) { uint8_t request[2]; _mcr |= 0x01 << 4; @@ -221,8 +206,7 @@ void SC16IS7X0::enableLoopback(void) * @brief Disable internal loopback mode * */ -void SC16IS7X0::disableLoopback(void) -{ +void SC16IS7X0::disableLoopback(void) { uint8_t request[2]; _mcr &= ~(0x01 << 4); @@ -237,8 +221,7 @@ void SC16IS7X0::disableLoopback(void) * @brief Enable TCR+TLR register * */ -void SC16IS7X0::enableTCR_TLR(void) -{ +void SC16IS7X0::enableTCR_TLR(void) { uint8_t request[2]; _mcr |= 0x01 << 2; @@ -253,8 +236,7 @@ void SC16IS7X0::enableTCR_TLR(void) * @brief Disable TCR+TLR register * */ -void SC16IS7X0::disableTCR_TLR(void) -{ +void SC16IS7X0::disableTCR_TLR(void) { uint8_t request[2]; _mcr &= ~(0x01 << 2); @@ -272,17 +254,13 @@ void SC16IS7X0::disableTCR_TLR(void) * @param prescaler DIVIDE_BY_1 or DIVIDE_BY_4 */ void SC16IS7X0::writeDivisorAndPrescaler(uint32_t divisor, - Prescaler prescaler) -{ + Prescaler prescaler) { uint8_t request[2]; // Change clock divisor bit - if (prescaler == DIVIDE_BY_1) - { + if (prescaler == DIVIDE_BY_1) { _mcr &= ~(0b10000000); - } - else if (prescaler == DIVIDE_BY_4) - { + } else if (prescaler == DIVIDE_BY_4) { _mcr |= 0b10000000; } @@ -319,8 +297,7 @@ void SC16IS7X0::writeDivisorAndPrescaler(uint32_t divisor, * @return size_t Return 1 if the byte has been pushed into the FIFO otherwise 0 * if the FIFO was full. */ -size_t SC16IS7X0::write(uint8_t c) -{ +size_t SC16IS7X0::write(uint8_t c) { uint8_t free = txlvl(); if (free < 1) return 0; @@ -339,8 +316,7 @@ size_t SC16IS7X0::write(uint8_t c) * @return size_t Return the number of bytes pushed into the FIFO. Can be less * than size. */ -size_t SC16IS7X0::write(const uint8_t *buffer, size_t size) -{ +size_t SC16IS7X0::write(const uint8_t *buffer, size_t size) { size_t free = (size_t)txlvl(); if (free < size) size = free; @@ -358,16 +334,14 @@ size_t SC16IS7X0::write(const uint8_t *buffer, size_t size) * * @return uint8_t */ -uint8_t SC16IS7X0::txlvl(void) -{ +uint8_t SC16IS7X0::txlvl(void) { uint8_t request[1] = {(SC16IS7X0_TXLVL << 3) | SC16IS7X0_READ_FLAG}; uint8_t txlvl; busIo->write_then_read(request, 1, &txlvl, 1); return txlvl; } -bool SC16IS7X0::setBusIo(SC16IS7X0_BusIo *theBusIo) -{ +bool SC16IS7X0::setBusIo(SC16IS7X0_BusIo *theBusIo) { if (busIo) delete busIo; // delete old instance busIo = theBusIo; @@ -378,13 +352,11 @@ bool SC16IS7X0::setBusIo(SC16IS7X0_BusIo *theBusIo) return false; } -uint8_t SC16IS7X0::getWordLength(SerialConfig config) -{ +uint8_t SC16IS7X0::getWordLength(SerialConfig config) { uint8_t wordLength; // Word Length - switch (config & UART_NB_BIT_MASK) - { + switch (config & UART_NB_BIT_MASK) { case UART_NB_BIT_5: wordLength = 0x00; break; @@ -405,12 +377,10 @@ uint8_t SC16IS7X0::getWordLength(SerialConfig config) return wordLength; } -uint8_t SC16IS7X0::getParity(SerialConfig config) -{ +uint8_t SC16IS7X0::getParity(SerialConfig config) { uint8_t parity; - switch (config & UART_PARITY_MASK) - { + switch (config & UART_PARITY_MASK) { case UART_PARITY_EVEN: parity = 0x18; break; @@ -427,13 +397,11 @@ uint8_t SC16IS7X0::getParity(SerialConfig config) return parity; } -uint8_t SC16IS7X0::getStopBits(SerialConfig config) -{ +uint8_t SC16IS7X0::getStopBits(SerialConfig config) { uint8_t stopBits = 0x00; // Stop bits - switch (config & UART_NB_STOP_BIT_MASK) - { + switch (config & UART_NB_STOP_BIT_MASK) { #ifdef ESP8266 case UART_NB_STOP_BIT_0: // Not applicable @@ -463,8 +431,7 @@ uint8_t SC16IS7X0::getStopBits(SerialConfig config) * * @return int */ -int SC16IS7X0::available(void) -{ +int SC16IS7X0::available(void) { uint8_t request[1] = {(SC16IS7X0_RXLVL << 3) | SC16IS7X0_READ_FLAG}; uint8_t rxlvl; busIo->write_then_read(request, 1, &rxlvl, 1); @@ -476,8 +443,7 @@ int SC16IS7X0::available(void) * * @return int */ -int SC16IS7X0::read(void) -{ +int SC16IS7X0::read(void) { if (available() == 0) return -1; @@ -518,8 +484,7 @@ size_t SC16IS7X0::readBytes(uint8_t *buffer, size_t len) * @return true overrun has occured * @return false no overrun */ -bool SC16IS7X0::hasOverrun(void) -{ +bool SC16IS7X0::hasOverrun(void) { uint8_t request[1] = {(SC16IS7X0_LSR << 3) | SC16IS7X0_READ_FLAG}; uint8_t lsr; busIo->write_then_read(request, 1, &lsr, 1); @@ -534,8 +499,7 @@ bool SC16IS7X0::hasOverrun(void) * in the receiver FIFO * @return false No error in FIFO */ -bool SC16IS7X0::hasRxError(void) -{ +bool SC16IS7X0::hasRxError(void) { uint8_t request[1] = {(SC16IS7X0_LSR << 3) | SC16IS7X0_READ_FLAG}; uint8_t lsr; busIo->write_then_read(request, 1, &lsr, 1); @@ -548,8 +512,7 @@ bool SC16IS7X0::hasRxError(void) * Transmission will stop when a high signal is detected on the CTS pin * */ -void SC16IS7X0::enableHardwareCTS(void) -{ +void SC16IS7X0::enableHardwareCTS(void) { uint8_t request[2]; // Set LCR Register to 0xBF to access Enhanced register set @@ -573,8 +536,7 @@ void SC16IS7X0::enableHardwareCTS(void) * @brief Disable Hardware CTS Flow Control * */ -void SC16IS7X0::disableHardwareCTS(void) -{ +void SC16IS7X0::disableHardwareCTS(void) { uint8_t request[2]; // Set LCR Register to 0xBF to access Enhanced register set @@ -600,8 +562,7 @@ void SC16IS7X0::disableHardwareCTS(void) * RTS pin goes low when the 'resume transmission trigger level' is reached * */ -void SC16IS7X0::enableHardwareRTS(void) -{ +void SC16IS7X0::enableHardwareRTS(void) { uint8_t request[2]; // TCR[7:4] Trigger level to resume transmission (set to 2h = 2x4 -> 8 @@ -632,8 +593,7 @@ void SC16IS7X0::enableHardwareRTS(void) * @brief Disable Hardware RTS Flow Control * */ -void SC16IS7X0::disableHardwareRTS(void) -{ +void SC16IS7X0::disableHardwareRTS(void) { uint8_t request[2]; // Set LCR Register to 0xBF to access Enhanced register set @@ -659,13 +619,11 @@ void SC16IS7X0::disableHardwareRTS(void) * @param pin pin number from 0 to 7 * @param mode INPUT or OUTPUT */ -void SC16IS7X0::pinMode(uint8_t pin, uint8_t mode) -{ +void SC16IS7X0::pinMode(uint8_t pin, uint8_t mode) { assert(pin <= 7); assert(mode == INPUT || mode == OUTPUT); - switch (mode) - { + switch (mode) { case OUTPUT: _ioDir |= 0x01 << pin; break; @@ -689,8 +647,7 @@ void SC16IS7X0::pinMode(uint8_t pin, uint8_t mode) * @param pin Pin number from 0 to 7 * @param val 0=low, other value = high */ -void SC16IS7X0::digitalWrite(uint8_t pin, uint8_t val) -{ +void SC16IS7X0::digitalWrite(uint8_t pin, uint8_t val) { assert(pin <= 7); if (val != 0) @@ -709,8 +666,7 @@ void SC16IS7X0::digitalWrite(uint8_t pin, uint8_t val) * @param pin Pin number from 0 to 7 * @return int 0=input low, 1=input high */ -int SC16IS7X0::digitalRead(uint8_t pin) -{ +int SC16IS7X0::digitalRead(uint8_t pin) { assert(pin <= 7); const uint8_t request[2] = {(SC16IS7X0_IOSTATE << 3) | SC16IS7X0_READ_FLAG};