lx_nor_flash_format.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*************************************************************************** | ||
| 2 | * Copyright (c) 2025 Microsoft Corporation | ||
| 3 | * Copyright (c) 2025 STmicroelectronics | ||
| 4 | * | ||
| 5 | * This program and the accompanying materials are made available under the | ||
| 6 | * terms of the MIT License which is available at | ||
| 7 | * https://opensource.org/licenses/MIT. | ||
| 8 | * | ||
| 9 | * SPDX-License-Identifier: MIT | ||
| 10 | **************************************************************************/ | ||
| 11 | |||
| 12 | /**************************************************************************/ | ||
| 13 | /**************************************************************************/ | ||
| 14 | /** */ | ||
| 15 | /** LevelX Component */ | ||
| 16 | /** */ | ||
| 17 | /** NOR Flash */ | ||
| 18 | /** */ | ||
| 19 | /**************************************************************************/ | ||
| 20 | /**************************************************************************/ | ||
| 21 | |||
| 22 | #define LX_SOURCE_CODE | ||
| 23 | |||
| 24 | |||
| 25 | /* Disable ThreadX error checking. */ | ||
| 26 | |||
| 27 | #ifndef LX_DISABLE_ERROR_CHECKING | ||
| 28 | #define LX_DISABLE_ERROR_CHECKING | ||
| 29 | #endif | ||
| 30 | |||
| 31 | |||
| 32 | /* Include necessary system files. */ | ||
| 33 | |||
| 34 | #include "lx_api.h" | ||
| 35 | |||
| 36 | |||
| 37 | /**************************************************************************/ | ||
| 38 | /* */ | ||
| 39 | /* FUNCTION RELEASE */ | ||
| 40 | /* */ | ||
| 41 | /* _lx_nor_flash_format PORTABLE C */ | ||
| 42 | /* */ | ||
| 43 | /* DESCRIPTION */ | ||
| 44 | /* */ | ||
| 45 | /* This function opens a NOR flash instance then erases all the non */ | ||
| 46 | /* erased blocks, then writes the erase count */ | ||
| 47 | /* */ | ||
| 48 | /* INPUT */ | ||
| 49 | /* */ | ||
| 50 | /* nor_flash NOR flash instance */ | ||
| 51 | /* name Name of NOR flash instance */ | ||
| 52 | /* nor_driver_initialize Driver initialize */ | ||
| 53 | /* nor_driver_info_ptr Drivier user data pointer */ | ||
| 54 | /* */ | ||
| 55 | /* OUTPUT */ | ||
| 56 | /* */ | ||
| 57 | /* return status */ | ||
| 58 | /* */ | ||
| 59 | /* CALLS */ | ||
| 60 | /* */ | ||
| 61 | /* (nor_driver_initialize) Driver initialize */ | ||
| 62 | /* _lx_nor_flash_driver_read Driver read */ | ||
| 63 | /* _lx_nor_flash_driver_write Driver write */ | ||
| 64 | /* (lx_nor_flash_driver_block_erased_verify) */ | ||
| 65 | /* NOR flash verify block erased */ | ||
| 66 | /* _lx_nor_flash_driver_block_erase Driver block erase */ | ||
| 67 | /* */ | ||
| 68 | /* CALLED BY */ | ||
| 69 | /* */ | ||
| 70 | /* Application Code */ | ||
| 71 | /* */ | ||
| 72 | /**************************************************************************/ | ||
| 73 | |||
| 74 | 70 | UINT _lx_nor_flash_format(LX_NOR_FLASH *nor_flash, CHAR *name, UINT (*nor_driver_initialize)(LX_NOR_FLASH *), VOID *nor_driver_info_ptr) | |
| 75 | { | ||
| 76 | |||
| 77 | ULONG l; | ||
| 78 | UINT status; | ||
| 79 | |||
| 80 | ULONG block_word; | ||
| 81 | ULONG *block_word_ptr; | ||
| 82 | |||
| 83 | LX_PARAMETER_NOT_USED(name); | ||
| 84 | |||
| 85 | /* Clear the NOR flash control block. User extension is not cleared. */ | ||
| 86 |
2/2✓ Branch 4 → 3 taken 25088 times.
✓ Branch 4 → 5 taken 49 times.
|
25158 | LX_MEMSET(nor_flash, 0, (ULONG)((UCHAR*)&(nor_flash -> lx_nor_flash_open_previous) - (UCHAR*)nor_flash) + sizeof(nor_flash -> lx_nor_flash_open_previous)); |
| 87 | |||
| 88 | 70 | nor_flash -> lx_nor_flash_driver_info_ptr = nor_driver_info_ptr; | |
| 89 | |||
| 90 | /* Call the flash driver's initialization function. */ | ||
| 91 | 70 | (nor_driver_initialize)(nor_flash); | |
| 92 | |||
| 93 | #ifndef LX_DIRECT_READ | ||
| 94 | |||
| 95 | /* Determine if the driver supplied a RAM buffer for reading the NOR sector if direct read is not | ||
| 96 | supported. */ | ||
| 97 |
2/4✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 14 times.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 42 times.
|
56 | if (nor_flash -> lx_nor_flash_sector_buffer == LX_NULL) |
| 98 | { | ||
| 99 | /* Return an error. */ | ||
| 100 | ✗ | return(LX_NO_MEMORY); | |
| 101 | } | ||
| 102 | #endif | ||
| 103 | |||
| 104 |
8/8✓ Branch 19 → 4 taken 56 times.
✓ Branch 19 → 20 taken 7 times.
✓ Branch 21 → 6 taken 112 times.
✓ Branch 21 → 22 taken 14 times.
✓ Branch 22 → 7 taken 56 times.
✓ Branch 22 → 23 taken 7 times.
✓ Branch 24 → 9 taken 336 times.
✓ Branch 24 → 25 taken 42 times.
|
630 | for (l = 0; l < nor_flash -> lx_nor_flash_total_blocks; l++) |
| 105 | { | ||
| 106 | #ifdef LX_NOR_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE | ||
| 107 | /* Check that the block is already erased */ | ||
| 108 | 56 | status = (nor_flash -> lx_nor_flash_driver_block_erased_verify)(nor_flash, l); | |
| 109 | #else | ||
| 110 | 504 | status = (nor_flash -> lx_nor_flash_driver_block_erased_verify)(l); | |
| 111 | |||
| 112 | #endif | ||
| 113 | |||
| 114 | /* the block is not erased, thus make sure to erase it */ | ||
| 115 |
4/8✓ Branch 5 → 6 taken 56 times.
✗ Branch 5 → 14 not taken.
✓ Branch 7 → 8 taken 112 times.
✗ Branch 7 → 16 not taken.
✓ Branch 8 → 9 taken 56 times.
✗ Branch 8 → 17 not taken.
✓ Branch 10 → 11 taken 336 times.
✗ Branch 10 → 19 not taken.
|
560 | if (status) |
| 116 | { | ||
| 117 | /* the max erase count is not needed just pass the value 0 */ | ||
| 118 | 560 | status = _lx_nor_flash_driver_block_erase(nor_flash, l, 0); | |
| 119 | /* Check for an error from flash driver */ | ||
| 120 |
4/8✗ Branch 7 → 8 not taken.
✓ Branch 7 → 10 taken 56 times.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 112 times.
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 13 taken 56 times.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 15 taken 336 times.
|
560 | if (status) |
| 121 | { | ||
| 122 | /* Call system error handler. */ | ||
| 123 | ✗ | _lx_nor_flash_system_error(nor_flash, status); | |
| 124 | |||
| 125 | /* Return an error. */ | ||
| 126 | ✗ | return(LX_ERROR); | |
| 127 | } | ||
| 128 | |||
| 129 | /* check that the sector was correctly erased */ | ||
| 130 | #ifdef LX_NOR_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE | ||
| 131 | 56 | status = (nor_flash -> lx_nor_flash_driver_block_erased_verify)(nor_flash, l); | |
| 132 | #else | ||
| 133 | 504 | status = (nor_flash -> lx_nor_flash_driver_block_erased_verify)(l); | |
| 134 | #endif | ||
| 135 |
4/8✗ Branch 11 → 12 not taken.
✓ Branch 11 → 14 taken 56 times.
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 16 taken 112 times.
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 17 taken 56 times.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 19 taken 336 times.
|
560 | if (status) |
| 136 | { | ||
| 137 | |||
| 138 | /* Call system error handler. */ | ||
| 139 | ✗ | _lx_nor_flash_system_error(nor_flash, status); | |
| 140 | |||
| 141 | /* Return an error. */ | ||
| 142 | ✗ | return(LX_ERROR); | |
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | /* Setup the block word pointer to the first word of the first block */ | ||
| 147 | 560 | block_word_ptr = nor_flash -> lx_nor_flash_base_address + l * nor_flash -> lx_nor_flash_words_per_block; | |
| 148 | |||
| 149 | /* Setup the initial erase_count to 1 and mark the block as erased. | ||
| 150 | * When the lx_nor_flash_open() gets called only the erase count will be kept. | ||
| 151 | */ | ||
| 152 | 560 | block_word = ((ULONG)1 | LX_BLOCK_ERASED); | |
| 153 | |||
| 154 | 560 | status = _lx_nor_flash_driver_write(nor_flash, block_word_ptr, &block_word, 1); | |
| 155 |
4/8✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 56 times.
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 20 taken 112 times.
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 21 taken 56 times.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 23 taken 336 times.
|
560 | if (status) |
| 156 | { | ||
| 157 | /* Call system error handler. */ | ||
| 158 | ✗ | _lx_nor_flash_system_error(nor_flash, status); | |
| 159 | |||
| 160 | /* Return an error. */ | ||
| 161 | ✗ | return(LX_ERROR); | |
| 162 | } | ||
| 163 | |||
| 164 | } | ||
| 165 | |||
| 166 | 70 | return(LX_SUCCESS); | |
| 167 | } | ||
| 168 |