@@ -62,6 +62,78 @@ struct flash_stm32_sector_t {
6262 volatile uint32_t * sr ;
6363};
6464
65+ static __unused int write_optb (const struct device * dev , uint32_t mask ,
66+ uint32_t value )
67+ {
68+ FLASH_TypeDef * regs = FLASH_STM32_REGS (dev );
69+ int rc ;
70+
71+ if (regs -> OPTCR & FLASH_OPTCR_OPTLOCK ) {
72+ LOG_ERR ("Option bytes locked" );
73+ return - EIO ;
74+ }
75+
76+ if ((regs -> OPTCR & mask ) == value ) {
77+ /* Done already */
78+ return 0 ;
79+ }
80+
81+ rc = flash_stm32_wait_flash_idle (dev );
82+ if (rc < 0 ) {
83+ LOG_ERR ("Err flash no idle" );
84+ return rc ;
85+ }
86+
87+ regs -> OPTCR = (regs -> OPTCR & ~mask ) | value ;
88+ regs -> OPTCR |= FLASH_OPTCR_OPTSTART ;
89+
90+ /* Make sure previous write is completed. */
91+ barrier_dsync_fence_full ();
92+
93+ rc = flash_stm32_wait_flash_idle (dev );
94+ if (rc < 0 ) {
95+ LOG_ERR ("Err flash no idle" );
96+ return rc ;
97+ }
98+
99+ return 0 ;
100+ }
101+
102+ #if defined(CONFIG_FLASH_STM32_READOUT_PROTECTION )
103+ uint8_t flash_stm32_get_rdp_level (const struct device * dev )
104+ {
105+ FLASH_TypeDef * regs = FLASH_STM32_REGS (dev );
106+
107+ return (regs -> OPTSR_CUR & FLASH_OPTSR_RDP_Msk ) >> FLASH_OPTSR_RDP_Pos ;
108+ }
109+
110+ void flash_stm32_set_rdp_level (const struct device * dev , uint8_t level )
111+ {
112+ write_optb (dev , FLASH_OPTSR_RDP_Msk ,
113+ (uint32_t )level << FLASH_OPTSR_RDP_Pos );
114+ }
115+ #endif /* CONFIG_FLASH_STM32_READOUT_PROTECTION */
116+
117+ int flash_stm32_option_bytes_lock (const struct device * dev , bool enable )
118+ {
119+ FLASH_TypeDef * regs = FLASH_STM32_REGS (dev );
120+
121+ if (enable ) {
122+ regs -> OPTCR |= FLASH_OPTCR_OPTLOCK ;
123+ } else if (regs -> OPTCR & FLASH_OPTCR_OPTLOCK ) {
124+ regs -> OPTKEYR = FLASH_OPT_KEY1 ;
125+ regs -> OPTKEYR = FLASH_OPT_KEY2 ;
126+ }
127+
128+ if (enable ) {
129+ LOG_DBG ("Option bytes locked" );
130+ } else {
131+ LOG_DBG ("Option bytes unlocked" );
132+ }
133+
134+ return 0 ;
135+ }
136+
65137bool flash_stm32_valid_range (const struct device * dev , off_t offset , uint32_t len , bool write )
66138{
67139#if defined(DUAL_BANK )
@@ -649,6 +721,9 @@ static DEVICE_API(flash, flash_stm32h7_api) = {
649721#ifdef CONFIG_FLASH_PAGE_LAYOUT
650722 .page_layout = flash_stm32_page_layout ,
651723#endif
724+ #ifdef CONFIG_FLASH_EX_OP_ENABLED
725+ .ex_op = flash_stm32_ex_op ,
726+ #endif
652727};
653728
654729static int stm32h7_flash_init (const struct device * dev )
0 commit comments