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_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.                            */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    nor_flash                             NOR flash instance            */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    return status                                                       */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _lx_nor_flash_block_reclaim           Reclaim a NOR flash block     */
62 /*    tx_mutex_get                          Get thread protection         */
63 /*    tx_mutex_put                          Release thread protection     */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Application Code                                                    */
68 /*    Internal LevelX                                                     */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
75 /*  09-30-2020     William E. Lamie         Modified comment(s),          */
76 /*                                            resulting in version 6.1    */
77 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
78 /*                                            resulting in version 6.1.7  */
79 /*                                                                        */
80 /**************************************************************************/
_lx_nor_flash_defragment(LX_NOR_FLASH * nor_flash)81 UINT  _lx_nor_flash_defragment(LX_NOR_FLASH *nor_flash)
82 {
83 
84 ULONG    i;
85 
86 
87 #ifdef LX_THREAD_SAFE_ENABLE
88 
89     /* Obtain the thread safe mutex.  */
90     tx_mutex_get(&nor_flash -> lx_nor_flash_mutex, TX_WAIT_FOREVER);
91 #endif
92 
93     /* Loop for max number of blocks, while there are obsolete count.  */
94     for (i = 0; i < nor_flash -> lx_nor_flash_total_blocks; i++)
95     {
96 
97         /* Determine if there is any more defragment work.  */
98         if (nor_flash -> lx_nor_flash_obsolete_physical_sectors == 0)
99             break;
100 
101         /* Call the block reclaim function to defragment.  */
102         _lx_nor_flash_block_reclaim(nor_flash);
103     }
104 
105 #ifdef LX_THREAD_SAFE_ENABLE
106 
107     /* Release the thread safe mutex.  */
108     tx_mutex_put(&nor_flash -> lx_nor_flash_mutex);
109 #endif
110 
111     /* Return successful completion.  */
112     return(LX_SUCCESS);
113 }
114 
115 
116