My Arduino Due arrived yesterday, and i was curious to make a "kernel" and boot it using the capabilities of the Cortex-M3 of the Due... However, i tried so much but my code still not works with the application code stored in my EEPROM, i have difficulties trying to use the vector table and jumping into application stored in ram...
Here's arduino code:
#include
#define EEPROM_ADDR 0x50 // 24LC512 default I2C address
#define APP_START_ADDRESS 0x20000100
#define APP_MAX_SIZE 84 // Adjust to your app size (20KB here)
void eeprom_read(uint16_t addr, uint8_t* buffer, uint16_t len) {
while (len > 0) {
Wire.beginTransmission(EEPROM_ADDR);
Wire.write((uint8_t)(addr >> 8)); // High byte of address
Wire.write((uint8_t)(addr & 0xFF)); // Low byte
Wire.endTransmission();
uint8_t chunk = len > 32 ? 32 : len;
Wire.requestFrom(EEPROM_ADDR, chunk);
uint8_t i = 0;
while (Wire.available() && i < chunk) {
uint8_t b = Wire.read();
buffer[i++] = b;
// Print byte in hex, padded with 0
if (b < 0x10) Serial.print('0');
Serial.print(b, HEX);
Serial.print(' ');
}
addr += i;
buffer += i;
len -= i;
Serial.println(); // newline after each chunk
}
}
void load_app_from_eeprom() {
uint8_t* app_memory = (uint8_t*)APP_START_ADDRESS;
uint16_t addr = 0;
uint16_t remaining = APP_MAX_SIZE;
while (remaining > 0) {
uint8_t chunk = remaining > 32 ? 32 : remaining;
eeprom_read(addr, app_memory + addr, chunk);
addr += chunk;
remaining -= chunk;
}
}
void jump_to_app() {
uint32_t* vector_table = (uint32_t*)APP_START_ADDRESS;
uint32_t app_sp = vector_table[0];
uint32_t app_reset_handler = vector_table[1];
Serial.println("Jumping to app...");
Serial.print("MSP: 0x"); Serial.println(app_sp, HEX);
Serial.print("Reset Vector: 0x"); Serial.println(app_reset_handler, HEX);
Serial.end();
delay(333);
#define SCB_VTOR (*(volatile uint32_t*)0xE000ED08)
SCB_VTOR = APP_START_ADDRESS;
__set_MSP(app_sp);
((void (*)(void))app_reset_handler)();
// Should never reach here
while (1);
}
void setup() {
Serial.begin(115200);
Wire.begin();
Serial.println("Loading app from EEPROM...");
load_app_from_eeprom();
jump_to_app();
}
void loop() {
// Never reached
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... ation-code