1515
1616#ifdef RT_USING_SFUD
1717
18- #ifdef RT_DEBUG_SFUD
19- #define DEBUG_TRACE rt_kprintf("[SFUD] "); rt_kprintf
20- #else
21- #define DEBUG_TRACE (...)
22- #endif /* RT_DEBUG_SFUD */
23-
2418#ifndef RT_SFUD_DEFAULT_SPI_CFG
2519
2620#ifndef RT_SFUD_SPI_MAX_HZ
3428 .data_width = 8, \
3529 .max_hz = RT_SFUD_SPI_MAX_HZ, \
3630}
37- #endif
31+ #endif /* RT_SFUD_DEFAULT_SPI_CFG */
3832
3933#ifdef SFUD_USING_QSPI
4034#define RT_SFUD_DEFAULT_QSPI_CFG \
4438 .ddr_mode = 0, \
4539 .qspi_dl_width = 4, \
4640}
47- #endif
48-
49- static char log_buf [RT_CONSOLEBUF_SIZE ];
50-
51- void sfud_log_debug (const char * file , const long line , const char * format , ...);
41+ #endif /* SFUD_USING_QSPI */
5242
5343static rt_err_t rt_sfud_control (rt_device_t dev , int cmd , void * args ) {
5444 RT_ASSERT (dev );
@@ -259,44 +249,6 @@ static void retry_delay_100us(void) {
259249 rt_thread_delay ((RT_TICK_PER_SECOND * 1 + 9999 ) / 10000 );
260250}
261251
262- /**
263- * This function is print debug info.
264- *
265- * @param file the file which has call this function
266- * @param line the line number which has call this function
267- * @param format output format
268- * @param ... args
269- */
270- void sfud_log_debug (const char * file , const long line , const char * format , ...) {
271- va_list args ;
272-
273- /* args point to the first variable parameter */
274- va_start (args , format );
275- rt_kprintf ("[SFUD] (%s:%ld) " , file , line );
276- /* must use vprintf to print */
277- rt_vsnprintf (log_buf , sizeof (log_buf ), format , args );
278- rt_kprintf ("%s\n" , log_buf );
279- va_end (args );
280- }
281-
282- /**
283- * This function is print routine info.
284- *
285- * @param format output format
286- * @param ... args
287- */
288- void sfud_log_info (const char * format , ...) {
289- va_list args ;
290-
291- /* args point to the first variable parameter */
292- va_start (args , format );
293- rt_kprintf ("[SFUD] " );
294- /* must use vprintf to print */
295- rt_vsnprintf (log_buf , sizeof (log_buf ), format , args );
296- rt_kprintf ("%s\n" , log_buf );
297- va_end (args );
298- }
299-
300252sfud_err sfud_spi_port_init (sfud_flash * flash ) {
301253 sfud_err result = SFUD_SUCCESS ;
302254
@@ -311,7 +263,7 @@ sfud_err sfud_spi_port_init(sfud_flash *flash) {
311263 flash -> spi .unlock = spi_unlock ;
312264 flash -> spi .user_data = flash ;
313265 if (RT_TICK_PER_SECOND < 1000 ) {
314- rt_kprintf ("[SFUD] Warning: The OS tick(%d) is less than 1000. So the flash write will take more time.\n " , RT_TICK_PER_SECOND );
266+ LOG_W ("[SFUD] Warning: The OS tick(%d) is less than 1000. So the flash write will take more time." , RT_TICK_PER_SECOND );
315267 }
316268 /* 100 microsecond delay */
317269 flash -> retry .delay = retry_delay_100us ;
@@ -334,23 +286,23 @@ const static struct rt_device_ops flash_device_ops =
334286#endif
335287
336288/**
337- * Probe SPI flash by SFUD(Serial Flash Universal Driver) driver library and though SPI device.
289+ * Probe SPI flash by SFUD (Serial Flash Universal Driver) driver library and though SPI device by specified configuration .
338290 *
339291 * @param spi_flash_dev_name the name which will create SPI flash device
340292 * @param spi_dev_name using SPI device name
293+ * @param spi_cfg SPI device configuration
294+ * @param qspi_cfg QSPI device configuration
341295 *
342296 * @return probed SPI flash device, probe failed will return RT_NULL
343297 */
344- rt_spi_flash_device_t rt_sfud_flash_probe (const char * spi_flash_dev_name , const char * spi_dev_name ) {
298+ rt_spi_flash_device_t rt_sfud_flash_probe_ex (const char * spi_flash_dev_name , const char * spi_dev_name ,
299+ struct rt_spi_configuration * spi_cfg , struct rt_qspi_configuration * qspi_cfg )
300+ {
345301 rt_spi_flash_device_t rtt_dev = RT_NULL ;
346302 sfud_flash * sfud_dev = RT_NULL ;
347303 char * spi_flash_dev_name_bak = RT_NULL , * spi_dev_name_bak = RT_NULL ;
348- /* using default flash SPI configuration for initialize SPI Flash
349- * @note you also can change the SPI to other configuration after initialized finish */
350- struct rt_spi_configuration cfg = RT_SFUD_DEFAULT_SPI_CFG ;
351304 extern sfud_err sfud_device_init (sfud_flash * flash );
352305#ifdef SFUD_USING_QSPI
353- struct rt_qspi_configuration qspi_cfg = RT_SFUD_DEFAULT_QSPI_CFG ;
354306 struct rt_qspi_device * qspi_dev = RT_NULL ;
355307#endif
356308
@@ -380,7 +332,7 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const
380332 /* RT-Thread SPI device initialize */
381333 rtt_dev -> rt_spi_device = (struct rt_spi_device * ) rt_device_find (spi_dev_name );
382334 if (rtt_dev -> rt_spi_device == RT_NULL || rtt_dev -> rt_spi_device -> parent .type != RT_Device_Class_SPIDevice ) {
383- rt_kprintf ("ERROR: SPI device %s not found!\n " , spi_dev_name );
335+ LOG_E ("ERROR: SPI device %s not found!" , spi_dev_name );
384336 goto error ;
385337 }
386338 sfud_dev -> spi .name = spi_dev_name_bak ;
@@ -389,12 +341,12 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const
389341 /* set the qspi line number and configure the QSPI bus */
390342 if (rtt_dev -> rt_spi_device -> bus -> mode & RT_SPI_BUS_MODE_QSPI ) {
391343 qspi_dev = (struct rt_qspi_device * )rtt_dev -> rt_spi_device ;
392- qspi_cfg . qspi_dl_width = qspi_dev -> config .qspi_dl_width ;
393- rt_qspi_configure (qspi_dev , & qspi_cfg );
344+ qspi_cfg -> qspi_dl_width = qspi_dev -> config .qspi_dl_width ;
345+ rt_qspi_configure (qspi_dev , qspi_cfg );
394346 }
395347 else
396348#endif
397- rt_spi_configure (rtt_dev -> rt_spi_device , & cfg );
349+ rt_spi_configure (rtt_dev -> rt_spi_device , spi_cfg );
398350 }
399351 /* SFUD flash device initialize */
400352 {
@@ -406,7 +358,7 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const
406358 sfud_dev -> user_data = rtt_dev ;
407359 /* initialize SFUD device */
408360 if (sfud_device_init (sfud_dev ) != SFUD_SUCCESS ) {
409- rt_kprintf ("ERROR: SPI flash probe failed by SPI device %s.\n " , spi_dev_name );
361+ LOG_E ("ERROR: SPI flash probe failed by SPI device %s." , spi_dev_name );
410362 goto error ;
411363 }
412364 /* when initialize success, then copy SFUD flash device's geometry to RT-Thread SPI flash device */
@@ -416,8 +368,8 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const
416368#ifdef SFUD_USING_QSPI
417369 /* reconfigure the QSPI bus for medium size */
418370 if (rtt_dev -> rt_spi_device -> bus -> mode & RT_SPI_BUS_MODE_QSPI ) {
419- qspi_cfg . medium_size = sfud_dev -> chip .capacity ;
420- rt_qspi_configure (qspi_dev , & qspi_cfg );
371+ qspi_cfg -> medium_size = sfud_dev -> chip .capacity ;
372+ rt_qspi_configure (qspi_dev , qspi_cfg );
421373 if (qspi_dev -> enter_qspi_mode != RT_NULL )
422374 qspi_dev -> enter_qspi_mode (qspi_dev );
423375
@@ -442,10 +394,10 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const
442394
443395 rt_device_register (& (rtt_dev -> flash_device ), spi_flash_dev_name , RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE );
444396
445- DEBUG_TRACE ("Probe SPI flash %s by SPI device %s success.\n " ,spi_flash_dev_name , spi_dev_name );
397+ LOG_I ("Probe SPI flash %s by SPI device %s success." ,spi_flash_dev_name , spi_dev_name );
446398 return rtt_dev ;
447399 } else {
448- rt_kprintf ("ERROR: Low memory.\n " );
400+ LOG_E ("ERROR: Low memory." );
449401 goto error ;
450402 }
451403
@@ -463,6 +415,26 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const
463415 return RT_NULL ;
464416}
465417
418+ /**
419+ * Probe SPI flash by SFUD(Serial Flash Universal Driver) driver library and though SPI device.
420+ *
421+ * @param spi_flash_dev_name the name which will create SPI flash device
422+ * @param spi_dev_name using SPI device name
423+ *
424+ * @return probed SPI flash device, probe failed will return RT_NULL
425+ */
426+ rt_spi_flash_device_t rt_sfud_flash_probe (const char * spi_flash_dev_name , const char * spi_dev_name )
427+ {
428+ struct rt_spi_configuration cfg = RT_SFUD_DEFAULT_SPI_CFG ;
429+ #ifndef SFUD_USING_QSPI
430+ return rt_sfud_flash_probe_ex (spi_flash_dev_name , spi_dev_name , & cfg , RT_NULL );
431+ #else
432+ struct rt_qspi_configuration qspi_cfg = RT_SFUD_DEFAULT_QSPI_CFG ;
433+
434+ return rt_sfud_flash_probe_ex (spi_flash_dev_name , spi_dev_name , & cfg , & qspi_cfg );
435+ #endif
436+ }
437+
466438/**
467439 * Delete SPI flash device
468440 *
@@ -496,7 +468,7 @@ sfud_flash_t rt_sfud_flash_find(const char *spi_dev_name)
496468
497469 rt_spi_device = (struct rt_spi_device * ) rt_device_find (spi_dev_name );
498470 if (rt_spi_device == RT_NULL || rt_spi_device -> parent .type != RT_Device_Class_SPIDevice ) {
499- rt_kprintf ("ERROR: SPI device %s not found!\n " , spi_dev_name );
471+ LOG_E ("ERROR: SPI device %s not found!" , spi_dev_name );
500472 goto __error ;
501473 }
502474
@@ -505,7 +477,7 @@ sfud_flash_t rt_sfud_flash_find(const char *spi_dev_name)
505477 sfud_dev = (sfud_flash_t ) (rtt_dev -> user_data );
506478 return sfud_dev ;
507479 } else {
508- rt_kprintf ("ERROR: SFUD flash device not found!\n " );
480+ LOG_E ("ERROR: SFUD flash device not found!" );
509481 goto __error ;
510482 }
511483
@@ -520,15 +492,15 @@ sfud_flash_t rt_sfud_flash_find_by_dev_name(const char *flash_dev_name)
520492
521493 rtt_dev = (rt_spi_flash_device_t ) rt_device_find (flash_dev_name );
522494 if (rtt_dev == RT_NULL || rtt_dev -> flash_device .type != RT_Device_Class_Block ) {
523- rt_kprintf ("ERROR: Flash device %s not found!\n " , flash_dev_name );
495+ LOG_E ("ERROR: Flash device %s not found!" , flash_dev_name );
524496 goto __error ;
525497 }
526498
527499 if (rtt_dev -> user_data ) {
528500 sfud_dev = (sfud_flash_t ) (rtt_dev -> user_data );
529501 return sfud_dev ;
530502 } else {
531- rt_kprintf ("ERROR: SFUD flash device not found!\n " );
503+ LOG_E ("ERROR: SFUD flash device not found!" );
532504 goto __error ;
533505 }
534506
0 commit comments