GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lx_nand_flash_memory_initialize.c Lines: 31 39 79.5 %
Date: 2026-03-06 18:45:40 Branches: 10 18 55.6 %

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_memory_initialize                    PORTABLE C      */
43
/*                                                           6.2.1       */
44
/*  AUTHOR                                                                */
45
/*                                                                        */
46
/*    Xiuwen Cai, Microsoft Corporation                                   */
47
/*                                                                        */
48
/*  DESCRIPTION                                                           */
49
/*                                                                        */
50
/*    This function imitialize memory buffer for NAND flash instance.     */
51
/*                                                                        */
52
/*  INPUT                                                                 */
53
/*                                                                        */
54
/*    nand_flash                            NAND flash instance           */
55
/*    memory_ptr                            Pointer to memory used by the */
56
/*                                            LevelX for this NAND.       */
57
/*    memory_size                           Size of memory                */
58
/*                                                                        */
59
/*  OUTPUT                                                                */
60
/*                                                                        */
61
/*    return status                                                       */
62
/*                                                                        */
63
/*  CALLS                                                                 */
64
/*                                                                        */
65
/*    LX_MEMSET                             Initialize memory             */
66
/*                                                                        */
67
/*  CALLED BY                                                             */
68
/*                                                                        */
69
/*    Internal LevelX                                                     */
70
/*                                                                        */
71
/**************************************************************************/
72
9
UINT  _lx_nand_flash_memory_initialize(LX_NAND_FLASH  *nand_flash, ULONG* memory_ptr, UINT memory_size)
73
{
74
75
UINT    memory_offset;
76
UINT    buffer_size;
77
78
79
    /* Clear the memory buffer.  */
80
74025
    LX_MEMSET(memory_ptr, 0, memory_size);
81
82
    /* Reset the memory offset.  */
83
9
    memory_offset = 0;
84
85
    /* Set memory size for block mapping table.  */
86
9
    buffer_size = nand_flash -> lx_nand_flash_total_blocks * sizeof(*nand_flash -> lx_nand_flash_block_mapping_table);
87
88
    /* Make sure the size is at least one page size.  */
89
9
    if (buffer_size < nand_flash -> lx_nand_flash_bytes_per_page)
90
    {
91
        buffer_size = nand_flash -> lx_nand_flash_bytes_per_page;
92
    }
93
94
    /* Assign memory for block mapping table.  */
95
9
    nand_flash -> lx_nand_flash_block_mapping_table = (USHORT*)(((UCHAR*)memory_ptr) + memory_offset);
96
97
    /* Update block mapping table size.  */
98
9
    nand_flash -> lx_nand_flash_block_mapping_table_size = buffer_size;
99
100
    /* Update memory offset.  */
101
9
    memory_offset += buffer_size;
102
103
    /* Check if there is enough memory.  */
104
9
    if (memory_offset > memory_size)
105
    {
106
107
        /* No enough memory, return error.  */
108
        return(LX_NO_MEMORY);
109
    }
110
111
    /* Set memory size for erase count table.  */
112
9
    buffer_size = nand_flash -> lx_nand_flash_total_blocks * sizeof(*nand_flash -> lx_nand_flash_erase_count_table);
113
114
    /* Make sure the size is at least one page size.  */
115
9
    if (buffer_size < nand_flash -> lx_nand_flash_bytes_per_page)
116
    {
117
        buffer_size = nand_flash -> lx_nand_flash_bytes_per_page;
118
    }
119
120
    /* Assign memory for erase count table.  */
121
9
    nand_flash -> lx_nand_flash_erase_count_table = (UCHAR*)(((UCHAR*)memory_ptr) + memory_offset);
122
123
    /* Update memory offset.  */
124
9
    memory_offset += buffer_size;
125
126
    /* Update erase count table size.  */
127
9
    nand_flash -> lx_nand_flash_erase_count_table_size = buffer_size;
128
129
    /* Check if there is enough memory.  */
130
9
    if (memory_offset > memory_size)
131
    {
132
133
        /* No enough memory, return error.  */
134
        return(LX_NO_MEMORY);
135
    }
136
137
    /* Assign memory for block list.  */
138
9
    nand_flash -> lx_nand_flash_block_list = (USHORT*)(((UCHAR*)memory_ptr) + memory_offset);
139
140
    /* Update memory offset.  */
141
9
    memory_offset += nand_flash -> lx_nand_flash_total_blocks * sizeof(*nand_flash -> lx_nand_flash_block_list);
142
143
    /* Check if there is enough memory.  */
144
9
    if (memory_offset > memory_size)
145
    {
146
147
        /* No enough memory, return error.  */
148
        return(LX_NO_MEMORY);
149
    }
150
151
    /* Update block list size.  */
152
9
    nand_flash -> lx_nand_flash_block_list_size = nand_flash -> lx_nand_flash_total_blocks;
153
154
    /* Initialize block list. */
155
9
    nand_flash -> lx_nand_flash_free_block_list_tail = 0;
156
9
    nand_flash -> lx_nand_flash_mapped_block_list_head = nand_flash -> lx_nand_flash_block_list_size - 1;
157
158
    /* Set memory size for block status table.  */
159
9
    buffer_size = nand_flash -> lx_nand_flash_total_blocks * sizeof(*nand_flash -> lx_nand_flash_block_status_table);
160
161
    /* Make sure the size is at least one page size.  */
162
9
    if (buffer_size < nand_flash -> lx_nand_flash_bytes_per_page)
163
    {
164
        buffer_size = nand_flash -> lx_nand_flash_bytes_per_page;
165
    }
166
167
    /* Assign memory for block status table.  */
168
9
    nand_flash -> lx_nand_flash_block_status_table = (USHORT*)(((UCHAR*)memory_ptr) + memory_offset);
169
170
    /* Update memory offset.  */
171
9
    memory_offset += buffer_size;
172
173
    /* Update block status table size.  */
174
9
    nand_flash -> lx_nand_flash_block_status_table_size = buffer_size;
175
176
    /* Check if there is enough memory.  */
177
9
    if (memory_offset > memory_size)
178
    {
179
180
        /* No enough memory, return error.  */
181
        return(LX_NO_MEMORY);
182
    }
183
184
    /* Assign memory for page buffer.  */
185
9
    nand_flash -> lx_nand_flash_page_buffer = ((UCHAR*)memory_ptr) + memory_offset;
186
187
    /* Update page buffer size.  */
188
9
    nand_flash -> lx_nand_flash_page_buffer_size = memory_size - memory_offset;
189
190
    /* Check if there is enough memory.  */
191
9
    if (nand_flash -> lx_nand_flash_page_buffer_size < (nand_flash -> lx_nand_flash_bytes_per_page + nand_flash -> lx_nand_flash_spare_total_length) * 2)
192
    {
193
194
        /* No enough memory, return error.  */
195
        return(LX_NO_MEMORY);
196
    }
197
198
    /* Return a successful completion.  */
199
9
    return(LX_SUCCESS);
200
}
201