0
\$\begingroup\$

I am reading the reference manual page 75 that shows the flash memory organization of STM32F4 MCUs. There is an OTP area of 528 Bytes in it as shown in the image:

enter image description here

Is it possible to write on this OTP area at run-time under user program control or is it writable at the chip-flash time only? I have tried to search this detail in the document but i cannot find it.

\$\endgroup\$
3
  • 6
    \$\begingroup\$ The 8th question in 24 hours asking us to read the documentation for you. Just do a search for "OTP" in the document you linked and all of your questions will be answered. \$\endgroup\$ – Elliot Alderson Feb 20 '19 at 19:02
  • \$\begingroup\$ I better not post any question for next 24 hours. As a matter of fact I appreciate your comments since I am learning how to ask question on a public forum like this. What I understand so far is that my question should be of significant use for other users also. \$\endgroup\$ – scico111 Feb 20 '19 at 19:19
  • \$\begingroup\$ We will also expect "other users" to try to find their own answers, so this question will not be of use to our average "other users". \$\endgroup\$ – Elliot Alderson Feb 20 '19 at 19:32
2
\$\begingroup\$

Is it possible to write on this OTP area at run-time under user program control

Yes. There are some details in section 3.8 (page 97) of the reference manual.

In brief, the OTP area behaves similarly to other parts of flash memory, except that it cannot be erased, and it contains a set of fine-grained lock bytes which can be used to prevent further writes.

\$\endgroup\$
2
  • \$\begingroup\$ Are there any CubeMX HAL library functions for reading writing this OTP area? \$\endgroup\$ – scico111 Feb 20 '19 at 19:12
  • 1
    \$\begingroup\$ @scico111 You should not ask another question in the comments. This should have been part of your original question, and you should have provided a link to the documentation of the CubeMX HAL library. \$\endgroup\$ – Elliot Alderson Feb 20 '19 at 19:32
1
\$\begingroup\$

This is indeed possible. I've been using the STM32 HAL for this (from inside Arduino with the STM32 Arduino Core, but should be usable without as well, I think).

Below is the code I used (slightly simplified, so no guarantees). It has some helper calculation to figure out the right addresses, and then write 4 bytes to the start of the OTP area, and then locks the first block (32 bytes in my case).

Note that locking prevents further writes (changing bits from 1 to 0), erasing (changing bits from 0 to 1) is never possible, not even when still unlocked.

// Values for STM32F401RE
static constexpr size_t OTP_SIZE = 512;
static constexpr size_t OTP_BLOCKS = 16;
static constexpr size_t OTP_BLOCK_SIZE = OTP_SIZE / OTP_BLOCKS;
static_assert(FLASH_OTP_END - FLASH_OTP_BASE + 1 == OTP_SIZE + OTP_BLOCKS, "Unexpected OTP flash size");

static constexpr size_t OTP_BLOCK_NUM = 0;
// Define some pointers for writing, but also to allow easy reading back values
const uint8_t *otp = reinterpret_cast<uint8_t*>(FLASH_OTP_BASE + OTP_BLOCK_NUM * OTP_BLOCK_SIZE);
const uint8_t *lock = reinterpret_cast<uint8_t*>(FLASH_OTP_BASE + OTP_SIZE + OTP_BLOCK_NUM);

static bool flash_otp() {
  if(HAL_FLASH_Unlock() != HAL_OK) {
      Serial.println("HAL_FLASH_Unlock failed");
      return false;
  }

  // Write 4 bytes to the start of OTP. Repeat these calls to write more.
  if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, reinterpret_cast<uint32_t>(otp), 0xdeadbeef) != HAL_OK) {
    // Prints HAL_FLASH_ERROR_* error codes
    Serial.print("OTP flash failed: 0x");
    Serial.println(HAL_FLASH_GetError(), HEX);
    HAL_FLASH_Lock();
    return false;
  }

  // Lock OTP page
  if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, reinterpret_cast<uint32_t>(lock), 0x00)) {
    // Prints HAL_FLASH_ERROR_* error codes
    Serial.print("OTP lock failed: 0x");
    Serial.println(HAL_FLASH_GetError(), HEX);
    HAL_FLASH_Lock();
    return false;
  }

  HAL_FLASH_Lock();
  return true;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.