Skip to content

Commit

Permalink
Move check_writable function into optiboot.h/cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
MCUdude committed Apr 2, 2021
1 parent e12eda5 commit 5b1dac8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
28 changes: 2 additions & 26 deletions avr/libraries/Optiboot_flasher/src/Flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,14 @@ void Flash::set_far_address(uint32_t address)
/**
* @brief Checks if the microcontroller contains a bootloader that has flash
* writing capabilities. It does so by checking if a spesific number is placed
* at the very end of the flash memory.
* at the very end of the flash memory
*
* @return true if compatible bootloader is present
* @return false if incompatible or no bootloader is present
*/
bool Flash::check_writable()
{
uint8_t content = 0;
// 256kiB flash
#if FLASHEND == 0x3FFFF
content = pgm_read_byte_far(0x3FFFF);
// 128kiB flash
#elif FLASHEND == 0x1FFFF
content = pgm_read_byte_far(0x1FFFF);
//64kiB flash
#elif FLASHEND == 0xFFFF
content = pgm_read_byte(0xFFFF);
// 32kiB flash
#elif FLASHEND == 0x7FFF
content = pgm_read_byte(0x7FFF);
// 16kiB flash
#elif FLASHEND == 0x3FFF
content = pgm_read_byte(0x3FFF);
// 8kiB flash
#elif FLASHEND == 0x1FFF
content = pgm_read_byte(0x1FFF);
#endif

if(content >= 8)
return true;
else
return false;
return optiboot_check_writable();
}

/**
Expand Down
38 changes: 38 additions & 0 deletions avr/libraries/Optiboot_flasher/src/optiboot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,44 @@ void do_spm_cli(optiboot_addr_t address, uint8_t command, uint16_t data)
}


/**
* @brief Checks if the microcontroller contains a bootloader that has flash
* writing capabilities. It does so by checking if a spesific number is placed
* at the very end of the flash memory
*
* @return true if compatible bootloader is present
* @return false if incompatible or no bootloader is present
*/
bool optiboot_check_writable()
{
uint8_t content = 0;
// 256kiB flash
#if FLASHEND == 0x3FFFF
content = pgm_read_byte_far(0x3FFFF);
// 128kiB flash
#elif FLASHEND == 0x1FFFF
content = pgm_read_byte_far(0x1FFFF);
//64kiB flash
#elif FLASHEND == 0xFFFF
content = pgm_read_byte(0xFFFF);
// 32kiB flash
#elif FLASHEND == 0x7FFF
content = pgm_read_byte(0x7FFF);
// 16kiB flash
#elif FLASHEND == 0x3FFF
content = pgm_read_byte(0x3FFF);
// 8kiB flash
#elif FLASHEND == 0x1FFF
content = pgm_read_byte(0x1FFF);
#endif

if(content == 8)
return true;
else
return false;
}


/**
* @brief Erase flash page
*
Expand Down
1 change: 1 addition & 0 deletions avr/libraries/Optiboot_flasher/src/optiboot.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ typedef void (*do_spm_t)(uint16_t address, uint8_t command, uint16_t data);


void do_spm_cli(optiboot_addr_t address, uint8_t command, uint16_t data);
bool optiboot_check_writable();
void optiboot_page_erase(optiboot_addr_t address);
void optiboot_page_fill(optiboot_addr_t address, uint16_t data);
void optiboot_page_write(optiboot_addr_t address);
Expand Down

0 comments on commit 5b1dac8

Please sign in to comment.