Arduino: Add full Arduino code
This is the full working Arduino code, for writing and loading. This shouldn not be used by students.
This commit is contained in:
parent
f34d81ad87
commit
edb3d35464
2 changed files with 189 additions and 0 deletions
71
Arduino/SendCart/SendCart.ino
Normal file
71
Arduino/SendCart/SendCart.ino
Normal file
|
@ -0,0 +1,71 @@
|
|||
#include <Wire.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
static const int eeprom_addr = 0b1010000;
|
||||
uint8_t pageData[128] = {};
|
||||
uint16_t pageAddr = 0;
|
||||
|
||||
void write_addr(uint16_t addr) {
|
||||
Wire.write((uint8_t)(addr >> 8));
|
||||
Wire.write((uint8_t)addr);
|
||||
}
|
||||
|
||||
uint16_t sum_complement(const uint8_t data[128]) {
|
||||
uint16_t sum = 0;
|
||||
for (int i = 0; i<128; i += 2) {
|
||||
sum += (data[i+1] << 8) | data[i];
|
||||
}
|
||||
return -sum;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(115200);
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
Wire.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
char ret[4] = "";
|
||||
|
||||
// put your main code here, to run repeatedly:
|
||||
do {
|
||||
if (!Serial.readBytesUntil('\n', ret, 2))
|
||||
Serial.read();
|
||||
} while (strncmp(ret, "GO", 2) != 0);
|
||||
|
||||
// Reset the EEPROM internal address to 0. It autoincrements after.
|
||||
Wire.beginTransmission(eeprom_addr);
|
||||
write_addr(0);
|
||||
Wire.endTransmission(false);
|
||||
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
/*
|
||||
* We don't know the size of the cartridge, and it could be the whole EEPROM.
|
||||
* Read and send all the pages (64kiB), it should not impact the final PNG.
|
||||
*/
|
||||
for (int i = 0; i < 512; i++) {
|
||||
memset(pageData, 0, 128);
|
||||
// Arduino I2C buffer is 32 bytes, so we need multiple requests per page.
|
||||
for (int j = 0; j < 4; j++) {
|
||||
Wire.requestFrom(eeprom_addr, 32);
|
||||
for (int k = 0; k < 32; k++) {
|
||||
pageData[32*j + k] = Wire.read();
|
||||
}
|
||||
}
|
||||
// Try to send the full page until the recipient ACKs
|
||||
uint16_t page_checksum = sum_complement(pageData);
|
||||
do {
|
||||
memset(ret, 0, 4);
|
||||
size_t written = 0;
|
||||
while (written < 128) {
|
||||
written += Serial.write(pageData + written, min(128 - written, 64));
|
||||
}
|
||||
Serial.write((uint8_t*)&page_checksum, 2);
|
||||
while(!Serial.readBytesUntil('\n', ret, 3));
|
||||
Serial.read();
|
||||
} while(strncmp(ret, "ACK", 3) != 0);
|
||||
}
|
||||
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue