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!
Library
Features
- 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
Dependencies
- CMSIS
- STM32F4xx
- STM32F4xx RCC
- STM32F4xx FLASH
- TM
- defines.h
- defines.h
Functions and enumerations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
/** * OTP Result enumeration * * Parameters: * - TM_OTP_Result_Ok: * Everything OK * - TM_OTP_Result_Error: * An error occured * This is returned when you try to write data to location which does not exists, * or if you try to write data to locked block */ typedef enum { TM_OTP_Result_Ok = 0, TM_OTP_Result_Error } TM_OTP_Result_t; /** * Write one-time data to specific block and specific byte in this block * * Note: * - You can only ONCE write data at desired byte in specific block, * if you will try to do it more times, you can have broken data at this location. * * Parameters: * - uint8_t block: * OTP block number, 0 to 15 is allowed * - uint8_t byte: * OTP byte inside one block, 0 to 31 is allowed * - uint8_t data: * Data to be written to OTP memory * * Member of TM_OTP_Result_t enumeration is returned */ extern TM_OTP_Result_t TM_OTP_Write(uint8_t block, uint8_t byte, uint8_t data); /** * Read data from specific block and specific byte in this block * * You can read data unlimited times from locations * * Parameters: * - uint8_t block: * OTP block number, 0 to 15 is allowed * - uint8_t byte: * OTP byte inside one block, 0 to 31 is allowed * * Returns value at specific block and byte location, or 0 if location is invalid */ extern uint8_t TM_OTP_Read(uint8_t block, uint8_t byte); /** * Lock entire block to prevent future programming inside * * Note: * - When you lock your block, then you are not able to program it anymore. * Even, if it is totally empty. You can't unlock it back! * * Parameters: * - uint8_t block: * OTP block number, 0 to 15 is allowed * * Member of TM_OTP_Result_t enumeration is returned */ extern TM_OTP_Result_t TM_OTP_BlockLock(uint8_t block); /** * Check if block is locked or not * * Parameters: * - uint8_t block: * OTP block number, 0 to 15 is allowed * * Returns 1 if locked or 0 if not */ #define TM_OTP_BlockLocked(block) ((*(__IO uint8_t *) (OTP_LOCK_ADDR + block)) == 0x00 ? 1 : 0) |
Example
- 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
/** * Keil project example for OTP memory * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @packs STM32F4xx Keil packs version 2.2.0 or greater required * @stdperiph STM32F4xx Standard peripheral drivers version 1.4.0 or greater required */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_disco.h" #include "tm_stm32f4_otp.h" #include <stdio.h> int main(void) { uint8_t d; /* Initialize system */ SystemInit(); /* Initialize leds on board */ TM_DISCO_LedInit(); /* Write data to block 15, byte number 6, write 0x96 to OTP memory */ if (TM_OTP_Write(15, 6, 0x96) == TM_OTP_Result_Ok) { /* Turn on green LED */ TM_DISCO_LedOn(LED_GREEN); } /* Lock block 15 forever */ /* Be careful when you do this!! */ //TM_OTP_BlockLock(15); /* Read data from block 15, byte number 6 from OTP memory */ d = TM_OTP_Read(15, 6); /* Check if they are the same */ if (d == 0x96) { /* Written data is the same as data we read back */ TM_DISCO_LedOn(LED_RED); } /* If block 15 is locked */ if (TM_OTP_BlockLocked(15)) { /* Do your stuff here if it is locked */ } while (1) { } } |
Project is available on Github, download library below.
Recent comments