Library 49- One-Time programmable (OTP) bytes on STM32F4

STm32F4xx devices have OTP (One-Time-Programmable) bytes. They can be used for permanent store of configuration data for your device. Bytes are structured in 16 data blocks where each block has 32 data bytes of available memory.

There is also 17th block with 16 bytes of data. His “work” is to store information about states of data block, because you can lock each block forever even if it is totally empty.

Important notes:

  • When you write data to specific OTP location which is empty, and if you try to write data there again, you can expect broken data after write,
  • If you lock your block, you are unable to unblock it back and you are unable to write data to block even if it is completelly empty,
  • You don’t need to write entire block at a time!



  • Operate with OTP memory
  • Write data to specific block and byte
  • Read data from specific block and byte
  • Lock block permanently
  • Check if block is locked


    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx FLASH
  • TM
    • defines.h

Functions and enumerations


  • If GREEN LED is on, then byte is written OK to OTP memory
  • If RED LED is on, then read byte is the same as byte we write before

Project is available on Github, download library below.

TM STM32F4 OTP Library


Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • Pingback: All STM32F4 libraries - STM32F4 Discovery()

  • Thái Tấn Lợi

    Hello !
    Why i just read and write correct data only 1 times. When i try to change data to read and write it’s wrong. I checked block this block is unblock. And how to unblock block is blocked. I used STM32F407 Discovery in this project.
    Thanks in advance for your answer

    • Please read actual post.

      • Thái Tấn Lợi

        That mean i only write “One time ” even if i upload new code.

        • One time is one time only. It hs nothing to do with you data. When you upload new data, you are doing another write. Only 1 (ONE) write is allowed.

          • Thái Tấn Lợi

            Thanks you very much, now i understand.

    • Kevin Goh Seng Tak

      This is because the OTP area resides in the FLASH the memory must also behave like a FLASH. The bytes are initially all ONEs, and each write operation flip the bit to 0..and there is no erase operation possible for this area. So the usage of OTP is you are allowed to flip the unflipped bit as much time as you can as long as the corresponding LOCK byte is not flipped…