Luigi Santivetti | 69972f9 | 2019-11-12 22:55:40 +0000 | [diff] [blame] | 1 | /*********************************************************************** |
| 2 | This sketch writes a `$I` build info string directly into Arduino EEPROM |
| 3 | |
| 4 | To use: |
| 5 | - Just alter the "build_info_line" string to whatever you'd like. Then |
| 6 | compile and upload this sketch to your Arduino. |
| 7 | |
| 8 | - If your Arduino is blinking slowly, your string has already been |
| 9 | written to your EEPROM and been verified by checksums! That's it! |
| 10 | |
| 11 | - If you Arduino LED is blinking fast, something went wrong and the |
| 12 | checksums don't match. You can optionally connect to the Arduino via |
| 13 | the serial monitor, and the sketch will show what its doing. |
| 14 | |
| 15 | NOTE: This sketch is provided as a tool template for OEMs who may need |
| 16 | to restrict users from altering their build info, so they can place |
| 17 | important product information here when enabling the restriction. |
| 18 | |
| 19 | NOTE: When uploading Grbl to the Arduino with this sketch on it, make |
| 20 | sure you see the slow blink before you start the upload process. This |
| 21 | ensures you aren't flashing Grbl when it's in mid-write of the EEPROM. |
| 22 | |
| 23 | Copyright (c) 2016 Sungeun K. Jeon for Gnea Research LLC |
| 24 | Released under the MIT-license. See license.txt for details. |
| 25 | ***********************************************************************/ |
| 26 | |
| 27 | #include <avr/pgmspace.h> |
| 28 | #include <EEPROM.h> |
| 29 | |
| 30 | #define SERIAL_BAUD_RATE 115200 |
| 31 | #define LINE_LENGTH 80U // Grbl line length |
| 32 | #define BYTE_LOCATION 942U // Grbl build info EEPROM address. |
| 33 | |
| 34 | |
| 35 | // ----- CHANGE THIS LINE ----- |
| 36 | |
| 37 | char build_info_line[LINE_LENGTH] = "Testing123."; |
| 38 | |
| 39 | // ----------------------------- |
| 40 | |
| 41 | |
| 42 | uint8_t status = false; |
| 43 | int ledPin = 13; // LED connected to digital pin 13 |
| 44 | |
| 45 | void setup() { |
| 46 | Serial.begin(SERIAL_BAUD_RATE); |
| 47 | delay(500); |
| 48 | |
| 49 | uint32_t address = BYTE_LOCATION; |
| 50 | uint32_t size = LINE_LENGTH; |
| 51 | char *write_pointer = (char*)build_info_line; |
| 52 | uint8_t write_checksum = 0; |
| 53 | for (; size>0; size--) { |
| 54 | write_checksum = (write_checksum << 1) || (write_checksum >> 7); |
| 55 | write_checksum += *write_pointer; |
| 56 | EEPROM.put(address++, *(write_pointer++)); |
| 57 | } |
| 58 | EEPROM.put(address,write_checksum); |
| 59 | |
| 60 | Serial.print(F("-> Writing line to EEPROM: '")); |
| 61 | Serial.print(build_info_line); |
| 62 | Serial.print(F("'\n\r-> Write checksum: ")); |
| 63 | Serial.println(write_checksum,DEC); |
| 64 | |
| 65 | size = LINE_LENGTH; |
| 66 | address = BYTE_LOCATION; |
| 67 | uint8_t data = 0; |
| 68 | char read_line[LINE_LENGTH]; |
| 69 | char *read_pointer = (char*)read_line; |
| 70 | uint8_t read_checksum = 0; |
| 71 | uint8_t stored_checksum = 0; |
| 72 | for(; size > 0; size--) { |
| 73 | data = EEPROM.read(address++); |
| 74 | read_checksum = (read_checksum << 1) || (read_checksum >> 7); |
| 75 | read_checksum += data; |
| 76 | *(read_pointer++) = data; |
| 77 | } |
| 78 | stored_checksum = EEPROM.read(address); |
| 79 | |
| 80 | Serial.print(F("<- Reading line from EEPROM: '")); |
| 81 | Serial.print(read_line); |
| 82 | Serial.print("'\n\r<- Read checksum: "); |
| 83 | Serial.println(read_checksum,DEC); |
| 84 | |
| 85 | if ((read_checksum == write_checksum) && (read_checksum == stored_checksum)) { |
| 86 | status = true; |
| 87 | Serial.print(F("SUCCESS! All checksums match!\r\n")); |
| 88 | } else { |
| 89 | if (write_checksum != stored_checksum) { |
| 90 | Serial.println(F("ERROR! Write and stored EEPROM checksums don't match!")); |
| 91 | } else { |
| 92 | Serial.println(F("ERROR! Read and stored checksums don't match!")); |
| 93 | } |
| 94 | } |
| 95 | pinMode(ledPin, OUTPUT); // sets the digital pin as output |
| 96 | } |
| 97 | |
| 98 | void loop() { |
| 99 | // Blink to let user know EEPROM write status. |
| 100 | // Slow blink is 'ok'. Fast blink is an 'error'. |
| 101 | digitalWrite(ledPin, HIGH); // sets the LED on |
| 102 | if (status) { delay(1500); } // Slow blink |
| 103 | else { delay(100); } // Rapid blink |
| 104 | digitalWrite(ledPin, LOW); // sets the LED off |
| 105 | if (status) { delay(1500); } |
| 106 | else { delay(100); } |
| 107 | } |
| 108 | |
| 109 | |