// https://www.mikrocontroller.net/topic/578561?reply_to=7895508#7895423 #define EXT_EE_ADDR 0x50 // I2C address of 1. external eeprom new 15.05.2025 #define EXT_EE_SIZE 32768 // Max address for 32k-eeprom 22.05.2025 #define EXT_EE_LIMIT 30 // Write buffer limit #define EXT_EE_PAGE 64 // Page size #define EXT_EE_MELODY 0 // Melody data start address in external eeprom #define EXT_EE_CALENDAR 5000 // Calendar data start address in external eeprom 02.06.2025 #define EXT_EE_TEST1 12000 // Test-data 1 start address in external eeprom ++++ 18.06.2025 10000 -> 12000 #define EXT_EE_TEST2 17000 // Test-data 2 start address in external eeprom ++++ 15000 -> 17000 #define EXT_EE_TEST3 22000 // Test-data 3 start address in external eeprom ++++ 20000 -> 22000 int receive_timer; // Receive timer !!!! byte serbuf; // Serial Buffer byte xon; // Number of xoff while receiving byte xoff; // Number of xoff while receiving bool rx_flag = false; // Flag for XOFF status char test_addr[3] = {'\0'}; // Buffer byte Ser_Arr[31]; // Array for page write byte cur_byte; // Array index of bytes received !!!! 19.06.2025 unsigned int receive_addr = EXT_EE_TEST1; // Receive address int rx_err; // Receive error counter bool rx_run; // Receive ongoing int cnt; // Offset for receiving data byte for_count; // DEBUG! count for loop repeat int ram_idx; // DEBUG! index for storing to ram int log_idx; // DEBUG! index for logging int data_idx; // DEBUG! index for data byte rx_test[256]; // DEBUG! buffer byte log_data[200]; // DEBUG! data sent to page write byte log_status[200]; // DEBUG! i2c-status byte log_bytes[200]; // DEBUG! log bytes byte log_val[200]; // DEBUG! log value int log_cnt[200]; // DEBUG! log cnt-value void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); // CR LF Serial.println(F("Bitte Testdaten 00-9F senden")); // Message } void loop() { // put your main code here, to run repeatedly: test_data_rec_(); // Receive serial data } void test_data_rec_() { // External eeprom data receive - menu17 - page write unsigned int addr = receive_addr; // Set receive address for external eeprom byte hi, lo, val; char nibble; // High nibble, low nibble and value byte bytes; // Bytes left in page, no. of bytes processed by for-loop byte x {0}; while ((Serial.available() >= 2) || // As long as data are available (receive_timer > 0)) { // Or receive timeout is running if (!rx_run) { receive_timer = 299; // Set receive timeout counter (only once) ! rx_run = 1; } if (receive_timer) { receive_timer--; // Decrease receive timeout counter } delay(10); for_count++; // DEBUG! How often is the for loop exectuted? x = Serial.available(); // Store total number of char received (max. 64) for (int i = 0; i < x / 2; i++) { // Process total number of bytes (=2 nibbles) received 2 4 6 8 byte y = Serial.available(); // Current buffer state = no of char! if (y > serbuf) { serbuf = y; // Buffer max size } nibble = text2hex(Serial.read()); // read and convert 1. char = high nibble hi = nibble; // High nibble nibble = text2hex(Serial.read()); // read and convert 2. char = low nibble lo = nibble; // Low nibble val = hi << 4 | lo; // Combine hi + low = value rx_test[ram_idx] = val; // DEBUG! store to RAM ram_idx++; // DEBUG! index for ram //flow_control(Serial.available()); // Flow-control (current buffer state) - not used! bytes = bytes_in_page(addr + cnt); // Determinate max. number allowed in this page if ((cur_byte < x / 2) && // Not all data processed | -> collect data (cur_byte < bytes)) { // Below buffer or page-write border | Ser_Arr[cur_byte] = val; // Add data to page-write buffer | //debug_collect(); // DEBUG! show collecting state | cur_byte++; // Next index | } if ((cur_byte == x / 2) || (cur_byte == bytes)) { // current count = received bytes | -> page write // Buffer or page-write border reached | cnt = cnt + cur_byte; // Increase address by bytes added | -> prepare next packet cur_byte = 0; // Clear index | } } } if (rx_run) { // DEBUG! rx executed before Serial.print(F("Bytes left ")); Serial.print(x); // DEBUG! x = bytes processed by for-loop Serial.print(F(" Max. Buffer ")); Serial.print(serbuf); // DEBUG! max. buffer state Serial.print(F(" cnt ")); Serial.print(cnt); // DEBUG! cnt if (cnt != 160) { Serial.print(F(" ERROR 160 Bytes expected")); } else { Serial.print(F(" OK ")); } Serial.println(); ext_ee_test1_read(); // Print eeprom content ext_ee_test3_read(); // Print RAM-content rx_run = 0; // Clear rx run flag ram_idx = 0; // Reset receive ram index cnt = 0; // Clear counter for next try } } char text2hex(char myChar) { // Convert text to hex char var {'\0'}; if (myChar >= '0' && myChar <= '9') { // 0-9 var = myChar - '0'; } // else if (myChar >= 'A' && myChar <= 'F') { // A-F var = myChar - 'A' + 10; } // else { rx_err++; // Blank + rex_error old: return 0; } return var; } byte bytes_in_page(unsigned int addr) { // Number of bytes left in page byte var {0}; if (addr < EXT_EE_SIZE) { // Address is valid int page = addr / EXT_EE_PAGE; // Current page int border = (page + 1) * EXT_EE_PAGE; // Next page address = border int bytes = border - addr; // Number of bytes total int rest = bytes; int count = 0; // Rest of bytes if (rest >= EXT_EE_LIMIT) { // Enough space for 30 bytes count = EXT_EE_LIMIT; } // else if (rest < EXT_EE_LIMIT) { // Less space than 30 bytes count = rest; } // var = count; // Number of bytes } else { Serial.print(F("Illegal Address")); } return var; } void ext_ee_test1_read() { // Read test data 1 - menu 18 key 5 - settings Serial.println(); // CR LF Serial.print(F("EEPROM Settings")); Serial.println(); // Test 1 print_data(); // Ext eeprom 1 = page write } void ext_ee_test3_read() { // Read test data 3 - menu 18 key 7 - melody Serial.println(); // CR LF Serial.print(F("RAM rx_test")); Serial.println(); // Test 3 DEBUG! from test-ram print_data(); // Ext eeprom 3 = page write } void print_data(void) { // Print data for (unsigned int x = 0; x < 256; x++) { // Count 512 values byte y = 0; y = rx_test[x]; // Data from ram if (y < 0x10) { Serial.print("0"); // Add a leading 0 } Serial.print(y, HEX); // Print char Serial.print(" "); // Add a blank y = x & 0x000F; // Mask low nibble if (y == 15) { Serial.print(" "); // Blank Serial.print(x + 1); // Counter Serial.println(); } } // CR LF after each 16 bytes Serial.println(); // CR LF }