Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The SPI configuration failed to configure the CS pin #128

Merged
merged 10 commits into from
Mar 3, 2022
44 changes: 38 additions & 6 deletions src/ConfigurableFirmata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ FirmataClass::FirmataClass()
firmwareVersionMajor = 0;
firmwareVersionName = "";
blinkVersionDisabled = false;
#ifdef LARGE_MEM_DEVICE
readCachePos = 0;
writeCachePos = 0;
#endif
systemReset();
}

Expand All @@ -95,6 +99,7 @@ void FirmataClass::begin(void)
void FirmataClass::begin(long speed)
{
Serial.begin(speed);
Serial.setTimeout(0);
FirmataStream = &Serial;
outputIsConsole = true;
blinkVersion();
Expand Down Expand Up @@ -255,11 +260,25 @@ void FirmataClass::processSysexMessage(void)
*/
void FirmataClass::processInput(void)
{
int inputData = FirmataStream->read(); // this is 'int' to handle -1 when no data
if (inputData != -1)
{
parse(inputData);
}
#ifdef LARGE_MEM_DEVICE
if (writeCachePos == readCachePos)
{
writeCachePos = readCachePos = 0;
writeCachePos = FirmataStream->readBytes(readCache, MAX_DATA_BYTES);
}
while (writeCachePos > readCachePos)
{
int inputData = readCache[readCachePos];
readCachePos++;
parse(inputData);
}
#else
int inputData = FirmataStream->read();
if (inputData != -1)
{
parse(inputData);
}
#endif
}

void FirmataClass::resetParser()
Expand All @@ -268,6 +287,9 @@ void FirmataClass::resetParser()
sysexBytesRead = 0;
waitForData = 0;
executeMultiByteCommand = 0;
#ifdef LARGE_MEM_DEVICE
writeCachePos = readCachePos = 0;
#endif
}

/**
Expand All @@ -280,6 +302,8 @@ void FirmataClass::parse(byte inputData)

// TODO make sure it handles -1 properly

// Firmata.sendStringf(F("Received byte 0x%x"), (int)inputData);

if (inputData == SYSTEM_RESET)
{
// A system reset shall always be done, regardless of the state of the parser.
Expand Down Expand Up @@ -503,8 +527,11 @@ void FirmataClass::sendStringf(const FlashString* flashString, ...)
#else
const int maxSize = 255;
#endif
// 32 bit boards. Note that sizeOfArgs may not be correct here (since all arguments are 32-bit padded)
int len = strlen_P((const char*)flashString);
if (len >= maxSize)
{
return;
}
va_list va;
va_start (va, flashString);
char bytesInput[maxSize];
Expand Down Expand Up @@ -593,6 +620,11 @@ void FirmataClass::write(byte c)
FirmataStream->write(c);
}

size_t FirmataClass::write(byte* buf, size_t length)
{
return FirmataStream->write(buf, length);
}


/**
* Attach a generic sysex callback function to a command (options are: ANALOG_MESSAGE,
Expand Down
12 changes: 10 additions & 2 deletions src/ConfigurableFirmata.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
* Query using the REPORT_FIRMWARE message.
*/
#define FIRMATA_FIRMWARE_MAJOR_VERSION 2 // for non-compatible changes
#define FIRMATA_FIRMWARE_MINOR_VERSION 11 // for backwards compatible changes
#define FIRMATA_FIRMWARE_MINOR_VERSION 12 // for backwards compatible changes
#define FIRMATA_FIRMWARE_BUGFIX_VERSION 0 // for bugfix releases

#ifdef ESP32
#ifdef LARGE_MEM_DEVICE
#define MAX_DATA_BYTES 255 // The ESP32 has enough RAM so we can reduce the number of packets, but the value must not exceed 2^8 - 1, because many methods use byte-indexing only
#else
#define MAX_DATA_BYTES 64 // max number of data bytes in incoming messages
Expand Down Expand Up @@ -168,6 +168,9 @@ class FirmataClass
void sendString(byte command, const char *string);
void sendSysex(byte command, byte bytec, byte *bytev);
void write(byte c);

size_t write(byte* buf, size_t length);

void sendPackedUInt14(uint16_t value);
void sendPackedUInt32(uint32_t value);
void sendPackedUInt64(uint64_t value);
Expand Down Expand Up @@ -237,6 +240,11 @@ class FirmataClass
void processSysexMessage(void);
void systemReset(void);
void strobeBlinkPin(byte pin, int count, int onInterval, int offInterval);
#ifdef LARGE_MEM_DEVICE
byte readCache[MAX_DATA_BYTES];
int readCachePos;
int writeCachePos;
#endif
};

extern FirmataClass Firmata;
Expand Down
4 changes: 1 addition & 3 deletions src/Encoder7Bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ class Encoder7BitClass
void startBinaryWrite();
void endBinaryWrite();
void writeBinary(byte data);
void readBinary(int outBytes, byte *inData, byte *outData);
static void readBinary(int outBytes, byte *inData, byte *outData);

private:
byte previous;
int shift;
};

extern Encoder7BitClass Encoder7Bit;

#endif
40 changes: 21 additions & 19 deletions src/FirmataScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ boolean FirmataScheduler::handleSysex(byte command, byte argc, byte* argv)
{
if (argc > 2) {
int len = num7BitOutbytes(argc - 2);
Encoder7Bit.readBinary(len, argv + 2, argv + 2); //decode inplace
Encoder7BitClass::readBinary(len, argv + 2, argv + 2); //decode inplace
addToTask(argv[1], len, argv + 2); //addToTask copies data...
}
break;
Expand All @@ -73,15 +73,15 @@ boolean FirmataScheduler::handleSysex(byte command, byte argc, byte* argv)
{
if (argc == 6) {
argv++;
Encoder7Bit.readBinary(4, argv, argv); //decode inplace
Encoder7BitClass::readBinary(4, argv, argv); //decode inplace
delayTask(*(long*)((byte*)argv));
}
break;
}
case SCHEDULE_FIRMATA_TASK:
{
if (argc == 7) { //one byte taskid, 5 bytes to encode 4 bytes of long
Encoder7Bit.readBinary(4, argv + 2, argv + 2); //decode inplace
Encoder7BitClass::readBinary(4, argv + 2, argv + 2); //decode inplace
schedule(argv[1], *(long*)((byte*)argv + 2)); //argv[1] | argv[2]<<8 | argv[3]<<16 | argv[4]<<24
}
break;
Expand Down Expand Up @@ -208,24 +208,26 @@ void FirmataScheduler::queryTask(byte id)
reportTask(id, task, false);
}

void FirmataScheduler::reportTask(byte id, firmata_task *task, boolean error)
void FirmataScheduler::reportTask(byte id, firmata_task* task, boolean error)
{
Firmata.write(START_SYSEX);
Firmata.write(SCHEDULER_DATA);
if (error) {
Firmata.write(ERROR_TASK_REPLY);
} else {
Firmata.write(QUERY_TASK_REPLY);
}
Firmata.write(id);
if (task) {
Encoder7Bit.startBinaryWrite();
for (unsigned int i = 3; i < firmata_task_len(task); i++) {
Encoder7Bit.writeBinary(((byte *)task)[i]); //don't write first 3 bytes (firmata_task*, byte); makes use of AVR byteorder (LSB first)
Encoder7BitClass encoder;
Firmata.write(START_SYSEX);
Firmata.write(SCHEDULER_DATA);
if (error) {
Firmata.write(ERROR_TASK_REPLY);
}
Encoder7Bit.endBinaryWrite();
}
Firmata.write(END_SYSEX);
else {
Firmata.write(QUERY_TASK_REPLY);
}
Firmata.write(id);
if (task) {
encoder.startBinaryWrite();
for (unsigned int i = 3; i < firmata_task_len(task); i++) {
encoder.writeBinary(((byte*)task)[i]); //don't write first 3 bytes (firmata_task*, byte); makes use of AVR byteorder (LSB first)
}
encoder.endBinaryWrite();
}
Firmata.write(END_SYSEX);
};

void FirmataScheduler::report(bool elapsed)
Expand Down
19 changes: 10 additions & 9 deletions src/OneWireFirmata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void OneWireFirmata::oneWireConfig(byte pin, boolean power)
boolean OneWireFirmata::handleSysex(byte command, byte argc, byte* argv)
{
if (command == ONEWIRE_DATA) {
Encoder7BitClass encoder;
if (argc > 1) {
byte subcommand = argv[0];
byte pin = argv[1];
Expand All @@ -70,14 +71,14 @@ boolean OneWireFirmata::handleSysex(byte command, byte argc, byte* argv)
boolean isAlarmSearch = (subcommand == ONEWIRE_SEARCH_ALARMS_REQUEST);
Firmata.write(isAlarmSearch ? (byte)ONEWIRE_SEARCH_ALARMS_REPLY : (byte)ONEWIRE_SEARCH_REPLY);
Firmata.write(pin);
Encoder7Bit.startBinaryWrite();
encoder.startBinaryWrite();
byte addrArray[8];
while (isAlarmSearch ? device->search(addrArray, false) : device->search(addrArray)) {
for (int i = 0; i < 8; i++) {
Encoder7Bit.writeBinary(addrArray[i]);
encoder.writeBinary(addrArray[i]);
}
}
Encoder7Bit.endBinaryWrite();
encoder.endBinaryWrite();
Firmata.write(END_SYSEX);
break;
}
Expand All @@ -104,7 +105,7 @@ boolean OneWireFirmata::handleSysex(byte command, byte argc, byte* argv)
int numReadBytes = 0;
int correlationId;
argv += 2;
Encoder7Bit.readBinary(numBytes, argv, argv); //decode inplace
Encoder7BitClass::readBinary(numBytes, argv, argv); //decode inplace

if (subcommand & ONEWIRE_SELECT_REQUEST_BIT) {
if (numBytes < 8) break;
Expand Down Expand Up @@ -140,13 +141,13 @@ boolean OneWireFirmata::handleSysex(byte command, byte argc, byte* argv)
Firmata.write(ONEWIRE_DATA);
Firmata.write(ONEWIRE_READ_REPLY);
Firmata.write(pin);
Encoder7Bit.startBinaryWrite();
Encoder7Bit.writeBinary(correlationId & 0xFF);
Encoder7Bit.writeBinary((correlationId >> 8) & 0xFF);
encoder.startBinaryWrite();
encoder.writeBinary(correlationId & 0xFF);
encoder.writeBinary((correlationId >> 8) & 0xFF);
for (int i = 0; i < numReadBytes; i++) {
Encoder7Bit.writeBinary(device->read());
encoder.writeBinary(device->read());
}
Encoder7Bit.endBinaryWrite();
encoder.endBinaryWrite();
Firmata.write(END_SYSEX);
}
}
Expand Down
Loading