lx_nand_flash_memory_initialize.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*************************************************************************** | ||
| 2 | * Copyright (c) 2024 Microsoft Corporation | ||
| 3 | * Copyright (c) 2026-present Eclipse ThreadX contributors | ||
| 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 | // Portions of this file were generated with AI assistance. | ||
| 13 | |||
| 14 | /**************************************************************************/ | ||
| 15 | /**************************************************************************/ | ||
| 16 | /** */ | ||
| 17 | /** LevelX Component */ | ||
| 18 | /** */ | ||
| 19 | /** NAND Flash */ | ||
| 20 | /** */ | ||
| 21 | /**************************************************************************/ | ||
| 22 | /**************************************************************************/ | ||
| 23 | |||
| 24 | #define LX_SOURCE_CODE | ||
| 25 | |||
| 26 | |||
| 27 | /* Disable ThreadX error checking. */ | ||
| 28 | |||
| 29 | #ifndef LX_DISABLE_ERROR_CHECKING | ||
| 30 | #define LX_DISABLE_ERROR_CHECKING | ||
| 31 | #endif | ||
| 32 | |||
| 33 | |||
| 34 | /* Include necessary system files. */ | ||
| 35 | |||
| 36 | #include "lx_api.h" | ||
| 37 | |||
| 38 | |||
| 39 | /**************************************************************************/ | ||
| 40 | /* */ | ||
| 41 | /* FUNCTION RELEASE */ | ||
| 42 | /* */ | ||
| 43 | /* _lx_nand_flash_memory_initialize PORTABLE C */ | ||
| 44 | /* 6.2.1 */ | ||
| 45 | /* AUTHOR */ | ||
| 46 | /* */ | ||
| 47 | /* Xiuwen Cai, Microsoft Corporation */ | ||
| 48 | /* */ | ||
| 49 | /* DESCRIPTION */ | ||
| 50 | /* */ | ||
| 51 | /* This function imitialize memory buffer for NAND flash instance. */ | ||
| 52 | /* */ | ||
| 53 | /* INPUT */ | ||
| 54 | /* */ | ||
| 55 | /* nand_flash NAND flash instance */ | ||
| 56 | /* memory_ptr Pointer to memory used by the */ | ||
| 57 | /* LevelX for this NAND. */ | ||
| 58 | /* memory_size Size of memory */ | ||
| 59 | /* */ | ||
| 60 | /* OUTPUT */ | ||
| 61 | /* */ | ||
| 62 | /* return status */ | ||
| 63 | /* */ | ||
| 64 | /* CALLS */ | ||
| 65 | /* */ | ||
| 66 | /* LX_MEMSET Initialize memory */ | ||
| 67 | /* */ | ||
| 68 | /* CALLED BY */ | ||
| 69 | /* */ | ||
| 70 | /* Internal LevelX */ | ||
| 71 | /* */ | ||
| 72 | /**************************************************************************/ | ||
| 73 | 90 | UINT _lx_nand_flash_memory_initialize(LX_NAND_FLASH *nand_flash, ULONG* memory_ptr, UINT memory_size) | |
| 74 | { | ||
| 75 | |||
| 76 | UINT memory_offset; | ||
| 77 | UINT buffer_size; | ||
| 78 | |||
| 79 | |||
| 80 | /* Clear the memory buffer. */ | ||
| 81 |
2/2✓ Branch 4 → 3 taken 518112 times.
✓ Branch 4 → 5 taken 63 times.
|
518202 | LX_MEMSET(memory_ptr, 0, memory_size); |
| 82 | |||
| 83 | /* Reset the memory offset. */ | ||
| 84 | 90 | memory_offset = 0; | |
| 85 | |||
| 86 | /* Set memory size for block mapping table. */ | ||
| 87 | 90 | buffer_size = nand_flash -> lx_nand_flash_total_blocks * sizeof(*nand_flash -> lx_nand_flash_block_mapping_table); | |
| 88 | |||
| 89 | /* Make sure the size is at least one page size. */ | ||
| 90 |
2/4✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 27 times.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 63 times.
|
90 | if (buffer_size < nand_flash -> lx_nand_flash_bytes_per_page) |
| 91 | { | ||
| 92 | ✗ | buffer_size = nand_flash -> lx_nand_flash_bytes_per_page; | |
| 93 | } | ||
| 94 | |||
| 95 | /* Assign memory for block mapping table. */ | ||
| 96 | 90 | nand_flash -> lx_nand_flash_block_mapping_table = (USHORT*)(((UCHAR*)memory_ptr) + memory_offset); | |
| 97 | |||
| 98 | /* Update block mapping table size. */ | ||
| 99 | 90 | nand_flash -> lx_nand_flash_block_mapping_table_size = buffer_size; | |
| 100 | |||
| 101 | /* Update memory offset. */ | ||
| 102 | 90 | memory_offset += buffer_size; | |
| 103 | |||
| 104 | /* Check if there is enough memory. */ | ||
| 105 |
2/4✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 27 times.
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 63 times.
|
90 | if (memory_offset > memory_size) |
| 106 | { | ||
| 107 | |||
| 108 | /* No enough memory, return error. */ | ||
| 109 | ✗ | return(LX_NO_MEMORY); | |
| 110 | } | ||
| 111 | |||
| 112 | /* Set memory size for erase count table. */ | ||
| 113 | 90 | buffer_size = nand_flash -> lx_nand_flash_total_blocks * sizeof(*nand_flash -> lx_nand_flash_erase_count_table); | |
| 114 | |||
| 115 | /* Make sure the size is at least one page size. */ | ||
| 116 |
2/4✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 27 times.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 63 times.
|
90 | if (buffer_size < nand_flash -> lx_nand_flash_bytes_per_page) |
| 117 | { | ||
| 118 | ✗ | buffer_size = nand_flash -> lx_nand_flash_bytes_per_page; | |
| 119 | } | ||
| 120 | |||
| 121 | /* Assign memory for erase count table. */ | ||
| 122 | 90 | nand_flash -> lx_nand_flash_erase_count_table = (UCHAR*)(((UCHAR*)memory_ptr) + memory_offset); | |
| 123 | |||
| 124 | /* Update memory offset. */ | ||
| 125 | 90 | memory_offset += buffer_size; | |
| 126 | |||
| 127 | /* Update erase count table size. */ | ||
| 128 | 90 | nand_flash -> lx_nand_flash_erase_count_table_size = buffer_size; | |
| 129 | |||
| 130 | /* Check if there is enough memory. */ | ||
| 131 |
2/4✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 27 times.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 63 times.
|
90 | if (memory_offset > memory_size) |
| 132 | { | ||
| 133 | |||
| 134 | /* No enough memory, return error. */ | ||
| 135 | ✗ | return(LX_NO_MEMORY); | |
| 136 | } | ||
| 137 | |||
| 138 | /* Assign memory for block list. */ | ||
| 139 | 90 | nand_flash -> lx_nand_flash_block_list = (USHORT*)(((UCHAR*)memory_ptr) + memory_offset); | |
| 140 | |||
| 141 | /* Update memory offset. */ | ||
| 142 | 90 | memory_offset += nand_flash -> lx_nand_flash_total_blocks * sizeof(*nand_flash -> lx_nand_flash_block_list); | |
| 143 | |||
| 144 | /* Check if there is enough memory. */ | ||
| 145 |
2/4✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 27 times.
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 63 times.
|
90 | if (memory_offset > memory_size) |
| 146 | { | ||
| 147 | |||
| 148 | /* No enough memory, return error. */ | ||
| 149 | ✗ | return(LX_NO_MEMORY); | |
| 150 | } | ||
| 151 | |||
| 152 | /* Update block list size. */ | ||
| 153 | 90 | nand_flash -> lx_nand_flash_block_list_size = nand_flash -> lx_nand_flash_total_blocks; | |
| 154 | |||
| 155 | /* Initialize block list. */ | ||
| 156 | 90 | nand_flash -> lx_nand_flash_free_block_list_tail = 0; | |
| 157 | 90 | nand_flash -> lx_nand_flash_mapped_block_list_head = nand_flash -> lx_nand_flash_block_list_size - 1; | |
| 158 | |||
| 159 | /* Set memory size for block status table. */ | ||
| 160 | 90 | buffer_size = nand_flash -> lx_nand_flash_total_blocks * sizeof(*nand_flash -> lx_nand_flash_block_status_table); | |
| 161 | |||
| 162 | /* Make sure the size is at least one page size. */ | ||
| 163 |
2/4✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 27 times.
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 63 times.
|
90 | if (buffer_size < nand_flash -> lx_nand_flash_bytes_per_page) |
| 164 | { | ||
| 165 | ✗ | buffer_size = nand_flash -> lx_nand_flash_bytes_per_page; | |
| 166 | } | ||
| 167 | |||
| 168 | /* Assign memory for block status table. */ | ||
| 169 | 90 | nand_flash -> lx_nand_flash_block_status_table = (USHORT*)(((UCHAR*)memory_ptr) + memory_offset); | |
| 170 | |||
| 171 | /* Update memory offset. */ | ||
| 172 | 90 | memory_offset += buffer_size; | |
| 173 | |||
| 174 | /* Update block status table size. */ | ||
| 175 | 90 | nand_flash -> lx_nand_flash_block_status_table_size = buffer_size; | |
| 176 | |||
| 177 | /* Check if there is enough memory. */ | ||
| 178 |
2/4✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 27 times.
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 63 times.
|
90 | if (memory_offset > memory_size) |
| 179 | { | ||
| 180 | |||
| 181 | /* No enough memory, return error. */ | ||
| 182 | ✗ | return(LX_NO_MEMORY); | |
| 183 | } | ||
| 184 | |||
| 185 | #ifdef LX_NAND_FLASH_ENABLE_LAZY_SECTOR_RELEASE | ||
| 186 | { | ||
| 187 | |||
| 188 | ULONG i; | ||
| 189 | ULONG compaction_table_entries; | ||
| 190 | |||
| 191 | /* Set memory size for block compaction table. Same element type as block_mapping_table. */ | ||
| 192 | buffer_size = nand_flash -> lx_nand_flash_total_blocks * sizeof(*nand_flash -> lx_nand_flash_block_compaction_table); | ||
| 193 | |||
| 194 | /* Make sure the size is at least one page size. */ | ||
| 195 | if (buffer_size < nand_flash -> lx_nand_flash_bytes_per_page) | ||
| 196 | { | ||
| 197 | buffer_size = nand_flash -> lx_nand_flash_bytes_per_page; | ||
| 198 | } | ||
| 199 | |||
| 200 | /* Assign memory for block compaction table. */ | ||
| 201 | nand_flash -> lx_nand_flash_block_compaction_table = (USHORT*)(((UCHAR*)memory_ptr) + memory_offset); | ||
| 202 | |||
| 203 | /* Update block compaction table size. */ | ||
| 204 | nand_flash -> lx_nand_flash_block_compaction_table_size = buffer_size; | ||
| 205 | |||
| 206 | /* Update memory offset. */ | ||
| 207 | memory_offset += buffer_size; | ||
| 208 | |||
| 209 | /* Check if there is enough memory. */ | ||
| 210 | if (memory_offset > memory_size) | ||
| 211 | { | ||
| 212 | |||
| 213 | /* No enough memory, return error. */ | ||
| 214 | return(LX_NO_MEMORY); | ||
| 215 | } | ||
| 216 | |||
| 217 | /* Initialize all compaction table entries to unmapped. (LX_MEMSET zero-fills the buffer | ||
| 218 | above, but LX_NAND_BLOCK_UNMAPPED is 0xFFFF, so explicit initialisation is required.) */ | ||
| 219 | compaction_table_entries = buffer_size / sizeof(*nand_flash -> lx_nand_flash_block_compaction_table); | ||
| 220 | for (i = 0; i < compaction_table_entries; i++) | ||
| 221 | { | ||
| 222 | nand_flash -> lx_nand_flash_block_compaction_table[i] = (USHORT)LX_NAND_BLOCK_UNMAPPED; | ||
| 223 | } | ||
| 224 | } | ||
| 225 | #endif | ||
| 226 | |||
| 227 | /* Assign memory for page buffer. */ | ||
| 228 | 90 | nand_flash -> lx_nand_flash_page_buffer = ((UCHAR*)memory_ptr) + memory_offset; | |
| 229 | |||
| 230 | /* Update page buffer size. */ | ||
| 231 | 90 | nand_flash -> lx_nand_flash_page_buffer_size = memory_size - memory_offset; | |
| 232 | |||
| 233 | /* Check if there is enough memory. */ | ||
| 234 |
2/4✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 27 times.
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 63 times.
|
90 | if (nand_flash -> lx_nand_flash_page_buffer_size < (nand_flash -> lx_nand_flash_bytes_per_page + nand_flash -> lx_nand_flash_spare_total_length) * 2) |
| 235 | { | ||
| 236 | |||
| 237 | /* No enough memory, return error. */ | ||
| 238 | ✗ | return(LX_NO_MEMORY); | |
| 239 | } | ||
| 240 | |||
| 241 | /* Return a successful completion. */ | ||
| 242 | 90 | return(LX_SUCCESS); | |
| 243 | } | ||
| 244 | |||
| 245 |