lx_nor_flash_defragment.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_defragment PORTABLE C */ | ||
| 43 | /* 6.1.7 */ | ||
| 44 | /* AUTHOR */ | ||
| 45 | /* */ | ||
| 46 | /* William E. Lamie, Microsoft Corporation */ | ||
| 47 | /* */ | ||
| 48 | /* DESCRIPTION */ | ||
| 49 | /* */ | ||
| 50 | /* This function defragments the NOR flash. */ | ||
| 51 | /* */ | ||
| 52 | /* INPUT */ | ||
| 53 | /* */ | ||
| 54 | /* nor_flash NOR flash instance */ | ||
| 55 | /* */ | ||
| 56 | /* OUTPUT */ | ||
| 57 | /* */ | ||
| 58 | /* return status */ | ||
| 59 | /* */ | ||
| 60 | /* CALLS */ | ||
| 61 | /* */ | ||
| 62 | /* _lx_nor_flash_block_reclaim Reclaim a NOR flash block */ | ||
| 63 | /* tx_mutex_get Get thread protection */ | ||
| 64 | /* tx_mutex_put Release thread protection */ | ||
| 65 | /* */ | ||
| 66 | /* CALLED BY */ | ||
| 67 | /* */ | ||
| 68 | /* Application Code */ | ||
| 69 | /* Internal LevelX */ | ||
| 70 | /* */ | ||
| 71 | /**************************************************************************/ | ||
| 72 | 100 | UINT _lx_nor_flash_defragment(LX_NOR_FLASH *nor_flash) | |
| 73 | { | ||
| 74 | |||
| 75 | ULONG i; | ||
| 76 | |||
| 77 | |||
| 78 | #ifdef LX_THREAD_SAFE_ENABLE | ||
| 79 | |||
| 80 | /* Obtain the thread safe mutex. */ | ||
| 81 | 10 | tx_mutex_get(&nor_flash -> lx_nor_flash_mutex, TX_WAIT_FOREVER); | |
| 82 | #endif | ||
| 83 | |||
| 84 | /* Loop for max number of blocks, while there are obsolete count. */ | ||
| 85 |
2/4✓ Branch 7 → 3 taken 270 times.
✗ Branch 7 → 8 not taken.
✓ Branch 8 → 4 taken 30 times.
✗ Branch 8 → 9 not taken.
|
300 | for (i = 0; i < nor_flash -> lx_nor_flash_total_blocks; i++) |
| 86 | { | ||
| 87 | |||
| 88 | /* Determine if there is any more defragment work. */ | ||
| 89 |
4/4✓ Branch 3 → 4 taken 90 times.
✓ Branch 3 → 5 taken 180 times.
✓ Branch 4 → 5 taken 10 times.
✓ Branch 4 → 6 taken 20 times.
|
300 | if (nor_flash -> lx_nor_flash_obsolete_physical_sectors == 0) |
| 90 | 100 | break; | |
| 91 | |||
| 92 | /* Call the block reclaim function to defragment. */ | ||
| 93 | 200 | _lx_nor_flash_block_reclaim(nor_flash); | |
| 94 | } | ||
| 95 | |||
| 96 | #ifdef LX_THREAD_SAFE_ENABLE | ||
| 97 | |||
| 98 | /* Release the thread safe mutex. */ | ||
| 99 | 10 | tx_mutex_put(&nor_flash -> lx_nor_flash_mutex); | |
| 100 | #endif | ||
| 101 | |||
| 102 | /* Return successful completion. */ | ||
| 103 | 100 | return(LX_SUCCESS); | |
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 |