1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
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 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
76 /*  09-30-2020     William E. Lamie         Modified comment(s),          */
77 /*                                            resulting in version 6.1    */
78 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
79 /*                                            resulting in version 6.1.7  */
80 /*                                                                        */
81 /**************************************************************************/
_lx_nor_flash_defragment(LX_NOR_FLASH * nor_flash)82 UINT  _lx_nor_flash_defragment(LX_NOR_FLASH *nor_flash)
83 {
84 
85 ULONG    i;
86 
87 
88 #ifdef LX_THREAD_SAFE_ENABLE
89 
90     /* Obtain the thread safe mutex.  */
91     tx_mutex_get(&nor_flash -> lx_nor_flash_mutex, TX_WAIT_FOREVER);
92 #endif
93 
94     /* Loop for max number of blocks, while there are obsolete count.  */
95     for (i = 0; i < nor_flash -> lx_nor_flash_total_blocks; i++)
96     {
97 
98         /* Determine if there is any more defragment work.  */
99         if (nor_flash -> lx_nor_flash_obsolete_physical_sectors == 0)
100             break;
101 
102         /* Call the block reclaim function to defragment.  */
103         _lx_nor_flash_block_reclaim(nor_flash);
104     }
105 
106 #ifdef LX_THREAD_SAFE_ENABLE
107 
108     /* Release the thread safe mutex.  */
109     tx_mutex_put(&nor_flash -> lx_nor_flash_mutex);
110 #endif
111 
112     /* Return successful completion.  */
113     return(LX_SUCCESS);
114 }
115 
116 
117