lx_nor_flash_sector_read.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 | |||
| 13 | /**************************************************************************/ | ||
| 14 | /**************************************************************************/ | ||
| 15 | /** */ | ||
| 16 | /** LevelX Component */ | ||
| 17 | /** */ | ||
| 18 | /** NOR Flash */ | ||
| 19 | /** */ | ||
| 20 | /**************************************************************************/ | ||
| 21 | /**************************************************************************/ | ||
| 22 | |||
| 23 | #define LX_SOURCE_CODE | ||
| 24 | |||
| 25 | |||
| 26 | /* Disable ThreadX error checking. */ | ||
| 27 | |||
| 28 | #ifndef LX_DISABLE_ERROR_CHECKING | ||
| 29 | #define LX_DISABLE_ERROR_CHECKING | ||
| 30 | #endif | ||
| 31 | |||
| 32 | |||
| 33 | /* Include necessary system files. */ | ||
| 34 | |||
| 35 | #include "lx_api.h" | ||
| 36 | |||
| 37 | |||
| 38 | /**************************************************************************/ | ||
| 39 | /* */ | ||
| 40 | /* FUNCTION RELEASE */ | ||
| 41 | /* */ | ||
| 42 | /* _lx_nor_flash_sector_read PORTABLE C */ | ||
| 43 | /* 6.3.0 */ | ||
| 44 | /* AUTHOR */ | ||
| 45 | /* */ | ||
| 46 | /* William E. Lamie, Microsoft Corporation */ | ||
| 47 | /* */ | ||
| 48 | /* DESCRIPTION */ | ||
| 49 | /* */ | ||
| 50 | /* This function reads a logical sector from NOR flash. */ | ||
| 51 | /* */ | ||
| 52 | /* INPUT */ | ||
| 53 | /* */ | ||
| 54 | /* nor_flash NOR flash instance */ | ||
| 55 | /* logical_sector Logical sector number */ | ||
| 56 | /* buffer Pointer to buffer to read into*/ | ||
| 57 | /* (the size is 512 bytes) */ | ||
| 58 | /* */ | ||
| 59 | /* OUTPUT */ | ||
| 60 | /* */ | ||
| 61 | /* return status */ | ||
| 62 | /* */ | ||
| 63 | /* CALLS */ | ||
| 64 | /* */ | ||
| 65 | /* _lx_nor_flash_driver_write Driver flash sector write */ | ||
| 66 | /* _lx_nor_flash_driver_read Driver flash sector read */ | ||
| 67 | /* _lx_nor_flash_logical_sector_find Find logical sector */ | ||
| 68 | /* _lx_nor_flash_physical_sector_allocate */ | ||
| 69 | /* Allocate new logical sector */ | ||
| 70 | /* _lx_nor_flash_system_error Internal system error handler */ | ||
| 71 | /* tx_mutex_get Get thread protection */ | ||
| 72 | /* tx_mutex_put Release thread protection */ | ||
| 73 | /* */ | ||
| 74 | /* CALLED BY */ | ||
| 75 | /* */ | ||
| 76 | /* Application Code */ | ||
| 77 | /* */ | ||
| 78 | /**************************************************************************/ | ||
| 79 | 28120 | UINT _lx_nor_flash_sector_read(LX_NOR_FLASH *nor_flash, ULONG logical_sector, VOID *buffer) | |
| 80 | { | ||
| 81 | |||
| 82 | UINT status; | ||
| 83 | ULONG *mapping_address; | ||
| 84 | ULONG mapping_entry; | ||
| 85 | ULONG *sector_address; | ||
| 86 | |||
| 87 | |||
| 88 | #ifdef LX_THREAD_SAFE_ENABLE | ||
| 89 | |||
| 90 | /* Obtain the thread safe mutex. */ | ||
| 91 | 2812 | tx_mutex_get(&nor_flash -> lx_nor_flash_mutex, TX_WAIT_FOREVER); | |
| 92 | #endif | ||
| 93 | |||
| 94 | /* Increment the number of read requests. */ | ||
| 95 | 28120 | nor_flash -> lx_nor_flash_read_requests++; | |
| 96 | |||
| 97 | /* See if we can find the sector in the current mapping. */ | ||
| 98 | 28120 | _lx_nor_flash_logical_sector_find(nor_flash, logical_sector, LX_FALSE, &mapping_address, §or_address); | |
| 99 | |||
| 100 | /* Determine if the logical sector was found. */ | ||
| 101 |
2/4✓ Branch 3 → 4 taken 25308 times.
✗ Branch 3 → 9 not taken.
✓ Branch 4 → 5 taken 2812 times.
✗ Branch 4 → 10 not taken.
|
28120 | if (mapping_address) |
| 102 | { | ||
| 103 | |||
| 104 | /* Yes, we were able to find the logical sector. */ | ||
| 105 | |||
| 106 | /* Read the sector data from the physical sector. */ | ||
| 107 | 28120 | status = _lx_nor_flash_driver_read(nor_flash, sector_address, buffer, LX_NOR_SECTOR_SIZE); | |
| 108 | |||
| 109 | /* Check for an error from flash driver. Drivers should never return an error.. */ | ||
| 110 |
2/4✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 25308 times.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 9 taken 2812 times.
|
28120 | if (status) |
| 111 | { | ||
| 112 | |||
| 113 | /* Call system error handler. */ | ||
| 114 | ✗ | _lx_nor_flash_system_error(nor_flash, status); | |
| 115 | |||
| 116 | /* Adjust return status. */ | ||
| 117 | ✗ | status = LX_ERROR; | |
| 118 | } | ||
| 119 | else | ||
| 120 | { | ||
| 121 | |||
| 122 | /* Set the status to success. */ | ||
| 123 | 28120 | status = LX_SUCCESS; | |
| 124 | } | ||
| 125 | } | ||
| 126 | else | ||
| 127 | { | ||
| 128 | |||
| 129 | /* Allocate a new physical sector for this write. */ | ||
| 130 | ✗ | _lx_nor_flash_physical_sector_allocate(nor_flash, logical_sector, &mapping_address, §or_address); | |
| 131 | |||
| 132 | /* Determine if the new sector allocation was successful. */ | ||
| 133 | ✗ | if (mapping_address) | |
| 134 | { | ||
| 135 | |||
| 136 | /* Update the number of free physical sectors. */ | ||
| 137 | ✗ | nor_flash -> lx_nor_flash_free_physical_sectors--; | |
| 138 | |||
| 139 | /* Read the sector data from the physical sector. */ | ||
| 140 | ✗ | status = _lx_nor_flash_driver_read(nor_flash, sector_address, buffer, LX_NOR_SECTOR_SIZE); | |
| 141 | |||
| 142 | /* Check for an error from flash driver. Drivers should never return an error.. */ | ||
| 143 | ✗ | if (status) | |
| 144 | { | ||
| 145 | |||
| 146 | /* Call system error handler. */ | ||
| 147 | ✗ | _lx_nor_flash_system_error(nor_flash, status); | |
| 148 | } | ||
| 149 | |||
| 150 | /* Now build the new mapping entry. */ | ||
| 151 | ✗ | mapping_entry = ((ULONG) LX_NOR_PHYSICAL_SECTOR_VALID) | ((ULONG) LX_NOR_PHYSICAL_SECTOR_SUPERCEDED) | logical_sector; | |
| 152 | |||
| 153 | /* Write out the new mapping entry. */ | ||
| 154 | ✗ | status = _lx_nor_flash_driver_write(nor_flash, mapping_address, &mapping_entry, 1); | |
| 155 | |||
| 156 | /* Check for an error from flash driver. Drivers should never return an error.. */ | ||
| 157 | ✗ | if (status) | |
| 158 | { | ||
| 159 | |||
| 160 | /* Call system error handler. */ | ||
| 161 | ✗ | _lx_nor_flash_system_error(nor_flash, status); | |
| 162 | |||
| 163 | #ifdef LX_THREAD_SAFE_ENABLE | ||
| 164 | |||
| 165 | /* Release the thread safe mutex. */ | ||
| 166 | ✗ | tx_mutex_put(&nor_flash -> lx_nor_flash_mutex); | |
| 167 | #endif | ||
| 168 | |||
| 169 | /* Return status. */ | ||
| 170 | ✗ | return(LX_ERROR); | |
| 171 | } | ||
| 172 | #ifndef LX_NOR_DISABLE_EXTENDED_CACHE | ||
| 173 | #ifdef LX_NOR_ENABLE_MAPPING_BITMAP | ||
| 174 | |||
| 175 | /* Determine if the logical sector is within the mapping bitmap. */ | ||
| 176 | ✗ | if (logical_sector < nor_flash -> lx_nor_flash_extended_cache_mapping_bitmap_max_logical_sector) | |
| 177 | { | ||
| 178 | |||
| 179 | /* Set the bit in the mapping bitmap. */ | ||
| 180 | ✗ | nor_flash -> lx_nor_flash_extended_cache_mapping_bitmap[logical_sector >> 5] |= (ULONG)(1 << (logical_sector & 31)); | |
| 181 | } | ||
| 182 | #endif | ||
| 183 | #endif | ||
| 184 | |||
| 185 | /* Increment the number of mapped physical sectors. */ | ||
| 186 | ✗ | nor_flash -> lx_nor_flash_mapped_physical_sectors++; | |
| 187 | |||
| 188 | /* Set the status to success. */ | ||
| 189 | ✗ | status = LX_SUCCESS; | |
| 190 | } | ||
| 191 | else | ||
| 192 | { | ||
| 193 | |||
| 194 | /* Could not find the logical sector. */ | ||
| 195 | ✗ | status = LX_SECTOR_NOT_FOUND; | |
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | #ifdef LX_THREAD_SAFE_ENABLE | ||
| 200 | |||
| 201 | /* Release the thread safe mutex. */ | ||
| 202 | 2812 | tx_mutex_put(&nor_flash -> lx_nor_flash_mutex); | |
| 203 | #endif | ||
| 204 | |||
| 205 | /* Return status. */ | ||
| 206 | 28120 | return(status); | |
| 207 | } | ||
| 208 | |||
| 209 |