mirror of
https://github.com/thelsing/knx.git
synced 2025-09-23 17:51:00 +02:00
* Add defines for LED, button and serial port To make supporting different board layouts easier, this introduces some new defines: For all platforms: - `KNX_LED` - the programming LED, defaults to `LED_BUILTIN` - `KNX_BUTTON` - the programming button, defaults to `0` For Arduino platforms: - `KNX_DEBUG_SERIAL` - the serial port that is used for debugging, defaults to `Serial` - `KNX_SERIAL` - the serial port used for communication with the TPUART, default depends on platform * GD32 flash workaround Addressing #181 At least some GD32 devices seem to require unlocking the flash twice. As the unlock function exits with `HAL_OK` if the flash is already unlocked, STM32 devices can run the same code without issues. * Add H8I8O and H8C09 configurations to knx-demo Addressing #181 These configurations both serve as examples for the use of these boards and other custom boards that don't use the standard pinout. * Add STM32 unlock target This copies the brute force unlock approach by @pavkriz into a custom target. With this commit, it is possible to automatically unlock e.g. a H8I8O board without leaving the PlatformIO IDE. See #181
300 lines
5.0 KiB
C++
300 lines
5.0 KiB
C++
#include "arduino_platform.h"
|
|
#include "knx/bits.h"
|
|
|
|
#include <Arduino.h>
|
|
#ifndef KNX_NO_SPI
|
|
#include <SPI.h>
|
|
#endif
|
|
|
|
#ifndef KNX_NO_PRINT
|
|
Stream* ArduinoPlatform::SerialDebug = &KNX_DEBUG_SERIAL;
|
|
#endif
|
|
|
|
ArduinoPlatform::ArduinoPlatform() : _knxSerial(nullptr)
|
|
{
|
|
}
|
|
|
|
ArduinoPlatform::ArduinoPlatform(HardwareSerial* knxSerial) : _knxSerial(knxSerial)
|
|
{
|
|
}
|
|
|
|
void ArduinoPlatform::fatalError()
|
|
{
|
|
while (true)
|
|
{
|
|
#ifdef KNX_LED
|
|
static const long LED_BLINK_PERIOD = 200;
|
|
|
|
if ((millis() % LED_BLINK_PERIOD) > (LED_BLINK_PERIOD / 2))
|
|
digitalWrite(KNX_LED, HIGH);
|
|
else
|
|
digitalWrite(KNX_LED, LOW);
|
|
#endif
|
|
}
|
|
}
|
|
|
|
void ArduinoPlatform::knxUart( HardwareSerial* serial )
|
|
{
|
|
if (_knxSerial)
|
|
closeUart();
|
|
_knxSerial = serial;
|
|
setupUart();
|
|
}
|
|
|
|
HardwareSerial* ArduinoPlatform::knxUart()
|
|
{
|
|
return _knxSerial;
|
|
}
|
|
|
|
void ArduinoPlatform::setupUart()
|
|
{
|
|
_knxSerial->begin(19200, SERIAL_8E1);
|
|
while (!_knxSerial)
|
|
;
|
|
}
|
|
|
|
|
|
void ArduinoPlatform::closeUart()
|
|
{
|
|
_knxSerial->end();
|
|
}
|
|
|
|
|
|
int ArduinoPlatform::uartAvailable()
|
|
{
|
|
return _knxSerial->available();
|
|
}
|
|
|
|
|
|
size_t ArduinoPlatform::writeUart(const uint8_t data)
|
|
{
|
|
//printHex("<p", &data, 1);
|
|
return _knxSerial->write(data);
|
|
}
|
|
|
|
|
|
size_t ArduinoPlatform::writeUart(const uint8_t *buffer, size_t size)
|
|
{
|
|
//printHex("<p", buffer, size);
|
|
return _knxSerial->write(buffer, size);
|
|
}
|
|
|
|
|
|
int ArduinoPlatform::readUart()
|
|
{
|
|
int val = _knxSerial->read();
|
|
//if(val > 0)
|
|
// printHex("p>", (uint8_t*)&val, 1);
|
|
return val;
|
|
}
|
|
|
|
|
|
size_t ArduinoPlatform::readBytesUart(uint8_t *buffer, size_t length)
|
|
{
|
|
size_t toRead = length;
|
|
uint8_t* pos = buffer;
|
|
while (toRead > 0)
|
|
{
|
|
size_t val = _knxSerial->readBytes(pos, toRead);
|
|
pos += val;
|
|
toRead -= val;
|
|
}
|
|
//printHex("p>", buffer, length);
|
|
return length;
|
|
}
|
|
|
|
#ifndef KNX_NO_SPI
|
|
void ArduinoPlatform::setupSpi()
|
|
{
|
|
SPI.begin();
|
|
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
|
|
}
|
|
|
|
void ArduinoPlatform::closeSpi()
|
|
{
|
|
SPI.endTransaction();
|
|
SPI.end();
|
|
}
|
|
|
|
int ArduinoPlatform::readWriteSpi(uint8_t *data, size_t len)
|
|
{
|
|
SPI.transfer(data, len);
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
#ifndef KNX_NO_PRINT
|
|
void printUint64(uint64_t value, int base = DEC)
|
|
{
|
|
char buf[8 * sizeof(uint64_t) + 1];
|
|
char* str = &buf[sizeof(buf) - 1];
|
|
*str = '\0';
|
|
|
|
uint64_t n = value;
|
|
do {
|
|
char c = n % base;
|
|
n /= base;
|
|
|
|
*--str = c < 10 ? c + '0' : c + 'A' - 10;
|
|
} while (n > 0);
|
|
|
|
print(str);
|
|
}
|
|
|
|
void print(const char* s)
|
|
{
|
|
ArduinoPlatform::SerialDebug->print(s);
|
|
}
|
|
void print(char c)
|
|
{
|
|
ArduinoPlatform::SerialDebug->print(c);
|
|
}
|
|
|
|
void print(unsigned char num)
|
|
{
|
|
ArduinoPlatform::SerialDebug->print(num);
|
|
}
|
|
|
|
void print(unsigned char num, int base)
|
|
{
|
|
ArduinoPlatform::SerialDebug->print(num, base);
|
|
}
|
|
|
|
void print(int num)
|
|
{
|
|
ArduinoPlatform::SerialDebug->print(num);
|
|
}
|
|
|
|
void print(int num, int base)
|
|
{
|
|
ArduinoPlatform::SerialDebug->print(num, base);
|
|
}
|
|
|
|
void print(unsigned int num)
|
|
{
|
|
ArduinoPlatform::SerialDebug->print(num);
|
|
}
|
|
|
|
void print(unsigned int num, int base)
|
|
{
|
|
ArduinoPlatform::SerialDebug->print(num, base);
|
|
}
|
|
|
|
void print(long num)
|
|
{
|
|
ArduinoPlatform::SerialDebug->print(num);
|
|
}
|
|
|
|
void print(long num, int base)
|
|
{
|
|
ArduinoPlatform::SerialDebug->print(num, base);
|
|
}
|
|
|
|
void print(unsigned long num)
|
|
{
|
|
ArduinoPlatform::SerialDebug->print(num);
|
|
}
|
|
|
|
void print(unsigned long num, int base)
|
|
{
|
|
ArduinoPlatform::SerialDebug->print(num, base);
|
|
}
|
|
|
|
void print(unsigned long long num)
|
|
{
|
|
printUint64(num);
|
|
}
|
|
|
|
void print(unsigned long long num, int base)
|
|
{
|
|
printUint64(num, base);
|
|
}
|
|
|
|
void print(double num)
|
|
{
|
|
ArduinoPlatform::SerialDebug->print(num);
|
|
}
|
|
|
|
void println(const char* s)
|
|
{
|
|
ArduinoPlatform::SerialDebug->println(s);
|
|
}
|
|
|
|
void println(char c)
|
|
{
|
|
ArduinoPlatform::SerialDebug->println(c);
|
|
}
|
|
|
|
void println(unsigned char num)
|
|
{
|
|
ArduinoPlatform::SerialDebug->println(num);
|
|
}
|
|
|
|
void println(unsigned char num, int base)
|
|
{
|
|
ArduinoPlatform::SerialDebug->println(num, base);
|
|
}
|
|
|
|
void println(int num)
|
|
{
|
|
ArduinoPlatform::SerialDebug->println(num);
|
|
}
|
|
|
|
void println(int num, int base)
|
|
{
|
|
ArduinoPlatform::SerialDebug->println(num, base);
|
|
}
|
|
|
|
void println(unsigned int num)
|
|
{
|
|
ArduinoPlatform::SerialDebug->println(num);
|
|
}
|
|
|
|
void println(unsigned int num, int base)
|
|
{
|
|
ArduinoPlatform::SerialDebug->println(num, base);
|
|
}
|
|
|
|
void println(long num)
|
|
{
|
|
ArduinoPlatform::SerialDebug->println(num);
|
|
}
|
|
|
|
void println(long num, int base)
|
|
{
|
|
ArduinoPlatform::SerialDebug->println(num, base);
|
|
}
|
|
|
|
void println(unsigned long num)
|
|
{
|
|
ArduinoPlatform::SerialDebug->println(num);
|
|
}
|
|
|
|
void println(unsigned long num, int base)
|
|
{
|
|
ArduinoPlatform::SerialDebug->println(num, base);
|
|
}
|
|
|
|
void println(unsigned long long num)
|
|
{
|
|
printUint64(num);
|
|
println("");
|
|
}
|
|
|
|
void println(unsigned long long num, int base)
|
|
{
|
|
printUint64(num, base);
|
|
println("");
|
|
}
|
|
|
|
void println(double num)
|
|
{
|
|
ArduinoPlatform::SerialDebug->println(num);
|
|
}
|
|
|
|
void println(void)
|
|
{
|
|
ArduinoPlatform::SerialDebug->println();
|
|
}
|
|
#endif // KNX_NO_PRINT
|