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