Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8347785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:28:27+00:00 2026-06-09T07:28:27+00:00

I have a self-modifying program which writes to the program flash area (it does

  • 0

I have a self-modifying program which writes to the program flash area (it does not break the program flow because I write to another flash sector than the one my program is running in – it runs in the protected bootloader section).

The complicated part, writing to the flash, works. I can check it in the debugger, the values I have sent are successfully written to the flash.

However, when I try to check the contents with the LPM instruction, it always reads zero.

I identified the following causes when LPM can fail to read:

  1. When the lock bits are set, forbidding reading the flash. Not the case here, as no lock bits are set.
  2. The reading of the flash is locked because of a previous write instruction. Not the case here, as I set RWWSRE and wait until the green light with while (SPMCSR & 0b01000000) {}
  3. I miscalculated the address (the segmentation of the Z pointer can be tricky). Not the case here, as I also tried it with the very first word (at address 0) and it still doesn’t work.

I use the following code for my test, reading the first two bytes of the flash (the writing instruction completed successfully, as Program memory at that position is not zero, checked with the debugger)

  lpm r0,Z+
  lpm r1,Z+
  movw r2, r0

Before that, I set the Z pointer to zero, and check it with the debugger that it is really zero (r30 and r31).

However, r2 and r3 will always be zero, regardless of what was in the flash.

Is there another situation where LPM fails to read?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:28:29+00:00Added an answer on June 9, 2026 at 7:28 am

    I found the solution while running it step by step and checking the contents of all the affected registers. It turned out the problem has nothing to do with LPM, but I keep the question for future reference.

    the code part

      lpm r0,Z+
      lpm r1,Z+
      movw r2, r0
    

    was in a C for loop, and the Z pointer was set before that loop. However, the loop comparison just happened to be compiled to use r30 as a temporary register, so it corrupted the Z pointer. Problem solved by placing the initialization of the Z pointer and the part with the LPM into the same #asm ... #endasm block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have you ever created or encountered a self modifying code in Java? If yes,
Have this self-made slider: http://jsfiddle.net/wyc3P/4/ What it does: takes min and max values in
I have an application which needs to be able to write to Any User/Current
In my viewDidLoad , I have self.todaySession = (id)[fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; which works
I'm modifying Python code that have this form: def foo(self): try: connect socket except
I have a program which modifies its own code while running. When I'm trying
Reading up on WCF we have self hosting option available , one limitation here
Is there a difference between filter and exclude in django? If I have self.get_query_set().filter(modelField=x)
I have a self-referential table in my database that looks sort of like above.
I have a self-written Java application - a little mail monitor. It works with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.