GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lx_nand_flash_block_data_move.c Lines: 0 38 0.0 %
Date: 2026-03-06 18:45:40 Branches: 0 20 0.0 %

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
/**   NAND 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_nand_flash_block_data_move                      PORTABLE C      */
43
/*                                                           6.2.1       */
44
/*  AUTHOR                                                                */
45
/*                                                                        */
46
/*    Xiuwen Cai, Microsoft Corporation                                   */
47
/*                                                                        */
48
/*  DESCRIPTION                                                           */
49
/*                                                                        */
50
/*    This function finds a block with less erase count and copies pages  */
51
/*    into new block.                                                     */
52
/*                                                                        */
53
/*  INPUT                                                                 */
54
/*                                                                        */
55
/*    nand_flash                            NAND flash instance           */
56
/*    new_block                             New block number              */
57
/*                                                                        */
58
/*  OUTPUT                                                                */
59
/*                                                                        */
60
/*    return status                                                       */
61
/*                                                                        */
62
/*  CALLS                                                                 */
63
/*                                                                        */
64
/*    _lx_nand_flash_mapped_block_list_get  Get mapped block index        */
65
/*    _lx_nand_flash_block_find             Find the mapped block         */
66
/*    _lx_nand_flash_data_page_copy         Copy data pages               */
67
/*    _lx_nand_flash_free_block_list_add    Add free block to list        */
68
/*    _lx_nand_flash_block_status_set       Set block status              */
69
/*    _lx_nand_flash_block_mapping_set      Set block mapping             */
70
/*    _lx_nand_flash_mapped_block_list_add  Add mapped block to list      */
71
/*    _lx_nand_flash_driver_block_erase     Erase block                   */
72
/*    _lx_nand_flash_erase_count_set        Set erase count               */
73
/*    _lx_nand_flash_free_block_list_add    Add free block to list        */
74
/*    _lx_nand_flash_system_error           Internal system error handler */
75
/*    tx_mutex_get                          Get thread protection         */
76
/*    tx_mutex_put                          Release thread protection     */
77
/*                                                                        */
78
/*  CALLED BY                                                             */
79
/*                                                                        */
80
/*    Application Code                                                    */
81
/*                                                                        */
82
/**************************************************************************/
83
UINT  _lx_nand_flash_block_data_move(LX_NAND_FLASH *nand_flash, ULONG new_block)
84
{
85
86
UINT        status;
87
ULONG       block;
88
USHORT      block_status;
89
USHORT      new_block_status;
90
ULONG       block_mapping_index;
91
92
93
    /* Get the mapped address to move the data.  */
94
    status = _lx_nand_flash_mapped_block_list_get(nand_flash, &block_mapping_index);
95
96
    if (status)
97
    {
98
99
        /* Return an error.  */
100
        return(LX_ERROR);
101
    }
102
103
    /* Find the block number to be moved.  */
104
    status = _lx_nand_flash_block_find(nand_flash, block_mapping_index * nand_flash -> lx_nand_flash_pages_per_block, &block, &block_status);
105
106
    /* Check the status.  */
107
    if (status)
108
    {
109
110
        /* Return an error.  */
111
        return(LX_ERROR);
112
    }
113
114
    /* Set new block status to allocated for now.  */
115
    new_block_status = LX_NAND_BLOCK_STATUS_ALLOCATED;
116
117
    /* Copy data from old block to new block.  */
118
    status = _lx_nand_flash_data_page_copy(nand_flash, block_mapping_index * nand_flash -> lx_nand_flash_pages_per_block, block, block_status, new_block, &new_block_status, nand_flash -> lx_nand_flash_pages_per_block);
119
120
    /* Check the status.  */
121
    if (status)
122
    {
123
124
        /* Call system error handler.  */
125
        _lx_nand_flash_system_error(nand_flash, status, new_block, 0);
126
127
        /* Return an error.  */
128
        return(LX_ERROR);
129
    }
130
131
    /* Check if no page was written.  */
132
    if ((new_block_status & LX_NAND_BLOCK_STATUS_PAGE_NUMBER_MASK) == 0)
133
    {
134
135
        /* Mark the new block as free.  */
136
        new_block_status = LX_NAND_BLOCK_STATUS_FREE;
137
138
        /* Add the new block to free list.  */
139
        _lx_nand_flash_free_block_list_add(nand_flash, new_block);
140
141
    }
142
143
    /* Set the block status for the new block.  */
144
    status = _lx_nand_flash_block_status_set(nand_flash, new_block, new_block_status);
145
146
    /* Check for an error from flash driver.   */
147
    if (status)
148
    {
149
150
        /* Call system error handler.  */
151
        _lx_nand_flash_system_error(nand_flash, status, new_block, 0);
152
153
        /* Return an error.  */
154
        return(LX_ERROR);
155
    }
156
157
    /* Check if the block is actually mapped.  */
158
    if (new_block_status == LX_NAND_BLOCK_STATUS_FREE)
159
    {
160
161
        /* Unmap the block.  */
162
        new_block = LX_NAND_BLOCK_UNMAPPED;
163
    }
164
165
    /* Update block mapping.  */
166
    _lx_nand_flash_block_mapping_set(nand_flash, block_mapping_index * nand_flash -> lx_nand_flash_pages_per_block, new_block);
167
168
    /* Check if the block is actually mapped.  */
169
    if (new_block != LX_NAND_BLOCK_UNMAPPED)
170
    {
171
172
        /* Add the block to mapped list.  */
173
        _lx_nand_flash_mapped_block_list_add(nand_flash, block_mapping_index);
174
    }
175
176
    /* Erase the old block.  */
177
    status = _lx_nand_flash_driver_block_erase(nand_flash, block, nand_flash -> lx_nand_flash_base_erase_count + nand_flash -> lx_nand_flash_erase_count_table[block] + 1);
178
179
    /* Check for an error from flash driver.   */
180
    if (status)
181
    {
182
183
        /* Call system error handler.  */
184
        _lx_nand_flash_system_error(nand_flash, status, block, 0);
185
186
        /* Return an error.  */
187
        return(LX_ERROR);
188
    }
189
190
    /* Update the erase count for the erased block.  */
191
    status = _lx_nand_flash_erase_count_set(nand_flash, block, (UCHAR)(nand_flash -> lx_nand_flash_erase_count_table[block] + 1));
192
193
    /* Check for an error from flash driver.   */
194
    if (status)
195
    {
196
197
        /* Call system error handler.  */
198
        _lx_nand_flash_system_error(nand_flash, status, block, 0);
199
200
        /* Return an error.  */
201
        return(LX_ERROR);
202
    }
203
204
    /* Set the block status to free.  */
205
    status = _lx_nand_flash_block_status_set(nand_flash, block, LX_NAND_BLOCK_STATUS_FREE);
206
207
    /* Check for an error from flash driver.   */
208
    if (status)
209
    {
210
211
        /* Call system error handler.  */
212
        _lx_nand_flash_system_error(nand_flash, status, block, 0);
213
214
215
        /* Return an error.  */
216
        return(LX_ERROR);
217
    }
218
219
    /* Add the block to free block list.  */
220
    status = _lx_nand_flash_free_block_list_add(nand_flash, block);
221
222
    /* Return status.  */
223
    return(status);
224
}
225