1 /*************************************************************************** 2 * Copyright (c) 2024 Microsoft Corporation 3 * 4 * This program and the accompanying materials are made available under the 5 * terms of the MIT License which is available at 6 * https://opensource.org/licenses/MIT. 7 * 8 * SPDX-License-Identifier: MIT 9 **************************************************************************/ 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_partial_defragment PORTABLE C */ 42 /* 6.1.7 */ 43 /* AUTHOR */ 44 /* */ 45 /* William E. Lamie, Microsoft Corporation */ 46 /* */ 47 /* DESCRIPTION */ 48 /* */ 49 /* This function defragments the NOR flash up to the specified */ 50 /* number of blocks. . */ 51 /* */ 52 /* INPUT */ 53 /* */ 54 /* nor_flash NOR flash instance */ 55 /* max_blocks Maximum number of blocks to */ 56 /* defragment */ 57 /* */ 58 /* OUTPUT */ 59 /* */ 60 /* return status */ 61 /* */ 62 /* CALLS */ 63 /* */ 64 /* _lx_nor_flash_block_reclaim Reclaim a NOR flash block */ 65 /* tx_mutex_get Get thread protection */ 66 /* tx_mutex_put Release thread protection */ 67 /* */ 68 /* CALLED BY */ 69 /* */ 70 /* Application Code */ 71 /* Internal LevelX */ 72 /* */ 73 /* RELEASE HISTORY */ 74 /* */ 75 /* DATE NAME DESCRIPTION */ 76 /* */ 77 /* 05-19-2020 William E. Lamie Initial Version 6.0 */ 78 /* 09-30-2020 William E. Lamie Modified comment(s), */ 79 /* resulting in version 6.1 */ 80 /* 06-02-2021 Bhupendra Naphade Modified comment(s), */ 81 /* resulting in version 6.1.7 */ 82 /* */ 83 /**************************************************************************/ _lx_nor_flash_partial_defragment(LX_NOR_FLASH * nor_flash,UINT max_blocks)84UINT _lx_nor_flash_partial_defragment(LX_NOR_FLASH *nor_flash, UINT max_blocks) 85 { 86 87 ULONG i; 88 89 90 #ifdef LX_THREAD_SAFE_ENABLE 91 92 /* Obtain the thread safe mutex. */ 93 tx_mutex_get(&nor_flash -> lx_nor_flash_mutex, TX_WAIT_FOREVER); 94 #endif 95 96 /* Determine if the maximum number of blocks exceeds the total blocks in this flash instance. */ 97 if (max_blocks >= nor_flash -> lx_nor_flash_total_blocks) 98 { 99 100 /* Adjust the maximum to the total number of blocks. */ 101 max_blocks = nor_flash -> lx_nor_flash_total_blocks; 102 } 103 104 /* Loop for max number of blocks, while there are obsolete count. */ 105 for (i = 0; i < max_blocks; i++) 106 { 107 108 /* Determine if there is any more defragment work. */ 109 if (nor_flash -> lx_nor_flash_obsolete_physical_sectors == 0) 110 break; 111 112 /* Call the block reclaim function to defragment. */ 113 _lx_nor_flash_block_reclaim(nor_flash); 114 } 115 116 #ifdef LX_THREAD_SAFE_ENABLE 117 118 /* Release the thread safe mutex. */ 119 tx_mutex_put(&nor_flash -> lx_nor_flash_mutex); 120 #endif 121 122 /* Return successful completion. */ 123 return(LX_SUCCESS); 124 } 125 126 127