Skip to content

Commit c984158

Browse files
committed
adding STM32 driver files
1 parent 1312f87 commit c984158

16 files changed

+3312
-0
lines changed
Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
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+
#include "fx_stm32_levelx_nand_driver.h"
13+
14+
/* define the struct used to identify the levelx driver to instantiate */
15+
struct fx_lx_nand_driver_instance
16+
{
17+
LX_NAND_FLASH flash_instance;
18+
CHAR name[32];
19+
UINT id;
20+
UINT (*nand_driver_initialize)(LX_NAND_FLASH *);
21+
UINT initialized;
22+
};
23+
24+
static struct fx_lx_nand_driver_instance fx_lx_nand_drivers[MAX_LX_NAND_DRIVERS] =
25+
{
26+
#ifdef LX_NAND_SIMULATOR_DRIVER
27+
{ .name = LX_NAND_SIMULATOR_DRIVER_NAME, .id = LX_NAND_SIMULATOR_DRIVER_ID, .nand_driver_initialize = lx_stm32_nand_simulator_initialize},
28+
#endif
29+
30+
#ifdef LX_NAND_CUSTOM_DRIVER
31+
LX_NAND_CUSTOM_DRIVERS
32+
#endif
33+
};
34+
35+
static struct fx_lx_nand_driver_instance *current_driver = NULL;
36+
37+
ULONG fx_lx_nand_driver_buffer[(7 * TOTAL_BLOCKS + 4 + 2 * (BYTES_PER_PHYSICAL_PAGE + SPARE_BYTES_PER_PAGE)) / sizeof(ULONG)];
38+
39+
/* Exported constants --------------------------------------------------------*/
40+
static const ULONG num_drivers = sizeof(fx_lx_nand_drivers)/sizeof(fx_lx_nand_drivers[0]);
41+
42+
/* Exported functions ------------------------------------------------------- */
43+
44+
static UINT find_driver_id(UINT driver_id)
45+
{
46+
UINT i = 0;
47+
48+
for (i = 0; i < num_drivers; i++)
49+
{
50+
if (fx_lx_nand_drivers[i].id == driver_id)
51+
return i;
52+
}
53+
54+
return UNKNOWN_DRIVER_ID;
55+
}
56+
57+
VOID fx_stm32_levelx_nand_driver(FX_MEDIA *media_ptr)
58+
{
59+
ULONG i;
60+
UINT status;
61+
UCHAR *source_buffer;
62+
UCHAR *destination_buffer;
63+
ULONG logical_sector;
64+
65+
66+
/* Process the driver request specified in the media control block.*/
67+
#ifdef USE_LX_NAND_DEFAULT_DRIVER
68+
i = find_driver_id(NAND_DEFAULT_DRIVER);
69+
#else
70+
if (media_ptr->fx_media_driver_info == NULL)
71+
{
72+
i = UNKNOWN_DRIVER_ID;
73+
}
74+
else
75+
{
76+
i = find_driver_id((UINT)media_ptr->fx_media_driver_info);
77+
}
78+
79+
#endif
80+
81+
if (i == UNKNOWN_DRIVER_ID)
82+
{
83+
/* No Driver found return an error */
84+
media_ptr->fx_media_driver_status = FX_MEDIA_INVALID;
85+
return;
86+
}
87+
else
88+
{
89+
current_driver = &fx_lx_nand_drivers[i];
90+
}
91+
92+
switch(media_ptr->fx_media_driver_request)
93+
{
94+
95+
case FX_DRIVER_INIT:
96+
{
97+
if (current_driver->initialized == FX_FALSE)
98+
{
99+
#ifdef FX_NAND_FORMAT_FLASH_BEFORE_OPEN
100+
/* Format flash instance*/
101+
status = lx_nand_flash_format(&current_driver->flash_instance, current_driver->name, current_driver->nand_driver_initialize, fx_lx_nand_driver_buffer, sizeof(fx_lx_nand_driver_buffer));
102+
if (status != LX_SUCCESS)
103+
{
104+
media_ptr->fx_media_driver_status = FX_IO_ERROR;
105+
106+
return;
107+
}
108+
#endif
109+
/* Open flash instance*/
110+
status = lx_nand_flash_open(&current_driver->flash_instance, current_driver->name, current_driver->nand_driver_initialize, fx_lx_nand_driver_buffer, sizeof(fx_lx_nand_driver_buffer));
111+
112+
/* LevelX driver correctly initialized */
113+
if (status == LX_SUCCESS)
114+
{
115+
current_driver->initialized = FX_TRUE;
116+
media_ptr->fx_media_driver_status = FX_SUCCESS;
117+
118+
media_ptr->fx_media_driver_free_sector_update = FX_TRUE;
119+
}
120+
else
121+
{
122+
media_ptr->fx_media_driver_status = FX_IO_ERROR;
123+
}
124+
}
125+
else
126+
{
127+
media_ptr->fx_media_driver_status = FX_SUCCESS;
128+
}
129+
130+
break;
131+
}
132+
133+
case FX_DRIVER_UNINIT:
134+
{
135+
/* Successful driver */
136+
status = lx_nand_flash_close(&current_driver->flash_instance);
137+
138+
if (status == LX_SUCCESS)
139+
{
140+
media_ptr->fx_media_driver_status = FX_SUCCESS;
141+
}
142+
else
143+
{
144+
media_ptr->fx_media_driver_status = FX_IO_ERROR;
145+
}
146+
147+
break;
148+
}
149+
150+
case FX_DRIVER_READ:
151+
{
152+
/* Setup the destination buffer and logical sector. */
153+
logical_sector = media_ptr->fx_media_driver_logical_sector;
154+
destination_buffer =(UCHAR *)media_ptr->fx_media_driver_buffer;
155+
156+
/* Loop to read sectors from flash. */
157+
for (i = 0; i < media_ptr->fx_media_driver_sectors; i++)
158+
{
159+
160+
/* Read a sector from NAND flash. */
161+
status = lx_nand_flash_sector_read(&current_driver->flash_instance, logical_sector, destination_buffer);
162+
163+
/* Determine if the read was successful. */
164+
if (status != LX_SUCCESS)
165+
{
166+
167+
/* Return an I/O error to FileX. */
168+
media_ptr->fx_media_driver_status = FX_IO_ERROR;
169+
170+
return;
171+
}
172+
173+
/* Move to the next entries. */
174+
logical_sector++;
175+
destination_buffer = destination_buffer + media_ptr->fx_media_bytes_per_sector;
176+
}
177+
178+
/* Successful driver request. */
179+
media_ptr->fx_media_driver_status = FX_SUCCESS;
180+
181+
break;
182+
}
183+
184+
case FX_DRIVER_BOOT_READ:
185+
{
186+
187+
/* Read the boot record and return to the caller. */
188+
189+
/* Setup the destination buffer. */
190+
destination_buffer = (UCHAR *) media_ptr -> fx_media_driver_buffer;
191+
192+
/* Read boot sector from NAND flash. */
193+
status = lx_nand_flash_sector_read(&current_driver->flash_instance, 0, destination_buffer);
194+
195+
/* Determine if the boot read was successful. */
196+
if (status != LX_SUCCESS)
197+
{
198+
/* Return an I/O error to FileX. */
199+
media_ptr -> fx_media_driver_status = FX_IO_ERROR;
200+
201+
return;
202+
}
203+
204+
/* Successful driver request. */
205+
media_ptr -> fx_media_driver_status = FX_SUCCESS;
206+
break;
207+
}
208+
209+
case FX_DRIVER_WRITE:
210+
{
211+
/* Setup the source buffer and logical sector. */
212+
logical_sector = media_ptr->fx_media_driver_logical_sector;
213+
source_buffer = (UCHAR *) media_ptr->fx_media_driver_buffer;
214+
215+
/* Loop to write sectors to flash. */
216+
for (i = 0; i < media_ptr->fx_media_driver_sectors; i++)
217+
{
218+
/* Write a sector to NAND flash. */
219+
status = lx_nand_flash_sector_write(&current_driver->flash_instance, logical_sector, source_buffer);
220+
221+
/* Determine if the write was successful. */
222+
if (status != LX_SUCCESS)
223+
{
224+
/* Return an I/O error to FileX. */
225+
media_ptr->fx_media_driver_status = FX_IO_ERROR;
226+
return;
227+
}
228+
229+
/* Move to the next entries. */
230+
logical_sector++;
231+
source_buffer = source_buffer + media_ptr->fx_media_bytes_per_sector;
232+
}
233+
234+
/* Successful driver request. */
235+
media_ptr->fx_media_driver_status = FX_SUCCESS;
236+
break;
237+
}
238+
239+
case FX_DRIVER_BOOT_WRITE:
240+
{
241+
242+
/* Write the boot record and return to the caller. */
243+
244+
/* Setup the source buffer. */
245+
source_buffer = (UCHAR *) media_ptr -> fx_media_driver_buffer;
246+
247+
/* Write boot sector to NAND flash. */
248+
status = lx_nand_flash_sector_write(&current_driver->flash_instance, 0, source_buffer);
249+
250+
/* Determine if the boot write was successful. */
251+
if (status != LX_SUCCESS)
252+
{
253+
254+
/* Return an I/O error to FileX. */
255+
media_ptr -> fx_media_driver_status = FX_IO_ERROR;
256+
257+
return;
258+
}
259+
260+
/* Successful driver request. */
261+
media_ptr -> fx_media_driver_status = FX_SUCCESS;
262+
break ;
263+
}
264+
case FX_DRIVER_RELEASE_SECTORS:
265+
{
266+
/* Setup the logical sector. */
267+
logical_sector = media_ptr->fx_media_driver_logical_sector;
268+
269+
/* Release sectors. */
270+
for (i = 0; i < media_ptr->fx_media_driver_sectors; i++)
271+
{
272+
/* Release NAND flash sector. */
273+
status = lx_nand_flash_sector_release(&current_driver->flash_instance, logical_sector);
274+
275+
/* Determine if the sector release was successful. */
276+
if (status != LX_SUCCESS)
277+
{
278+
/* Return an I/O error to FileX. */
279+
media_ptr->fx_media_driver_status = FX_IO_ERROR;
280+
return;
281+
}
282+
283+
/* Move to the next entries. */
284+
logical_sector++;
285+
}
286+
287+
/* Successful driver request. */
288+
media_ptr->fx_media_driver_status = FX_SUCCESS;
289+
break;
290+
}
291+
292+
case FX_DRIVER_FLUSH:
293+
{
294+
/* Return driver success. */
295+
media_ptr->fx_media_driver_status = FX_SUCCESS;
296+
break;
297+
}
298+
299+
case FX_DRIVER_ABORT:
300+
{
301+
/* Return driver success. */
302+
media_ptr->fx_media_driver_status = FX_SUCCESS;
303+
break;
304+
}
305+
306+
default:
307+
{
308+
/* Invalid driver request. */
309+
media_ptr->fx_media_driver_status = FX_IO_ERROR;
310+
break;
311+
}
312+
}
313+
}

0 commit comments

Comments
 (0)