Littlefs LPCXpresso55S16

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Littlefs LPCXpresso55S16

3,660 Views
ahmed007
Contributor II

Hello community! I'm working on a project with the LPCXpresso55S16 microcontroller and I'm using the Littelfs project for managing data storage in the flash memory. I would like to know if anyone has experience with creating, writing, and reading data using Littelfs on this specific microcontroller. Could you share an example or provide any guidance on how to accomplish this? Any help would be greatly appreciated. Thank you!

Ahmed,

0 Kudos
Reply
27 Replies

2,983 Views
ErichStyger
Specialist I

@ahmed007, you are welcome, and thanks for your kind words :-).

0 Kudos
Reply

2,777 Views
ahmed007
Contributor II
Dear Erich,

I hope this message finds you well.

Currently, I'm working on a project that involves establishing communication between LPCXpresso55S16 (DTLS server) and Raspberry Pi (DTLS client) via DTLS using mbedtls.

During the DTLS handshake, I encountered an error in the ssl_write_server_key_exchange function, specifically when the mbedtls_ssl_read_record function returns a negative value. I suspect that the issue might be related to my configuration as the client code works fine with a server code on the Raspberry Pi.

Here is the init_server function that I am using:

void dtls_init_server(void)
{
int ret;
mbedtls_ssl_init( &ssl );
mbedtls_ssl_config_init( &conf );
mbedtls_ssl_cookie_init( &cookie_ctx );
#if defined(MBEDTLS_SSL_CACHE_C)
mbedtls_ssl_cache_init( &cache );
#endif
mbedtls_x509_crt_init( &srvcert );
mbedtls_x509_crt_init( &cacert );
mbedtls_pk_init( &pkey );
mbedtls_entropy_init( &entropy );
mbedtls_ctr_drbg_init( &ctr_drbg );

#if defined(MBEDTLS_DEBUG_C)
mbedtls_debug_set_threshold( DEBUG_LEVEL );
#endif


if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
(const unsigned char *) pers,
strlen( pers ) ) ) != 0 )
{
printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
}
int res;
char* buffer = NULL;
size_t bufferSize = 0;
// First, set our EE certifiacte
res = McuLFS_ReadFile(pdtls->server_ee_cert_chain_filename_ptr, &buffer, &bufferSize);
if(res == 0){
ret = mbedtls_x509_crt_parse( &srvcert,(const unsigned char *)buffer, bufferSize +1);
if( ret != 0 )
{
printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
buffer = NULL;
bufferSize = 0;
}
PRINTF("We successfully parsing %s \r\n", pdtls->server_ee_cert_chain_filename_ptr);
buffer = NULL;
bufferSize = 0;
}

// Check if we also have an intermediate certificate... -> is the case for non IDevID certificates
if(strcmp(pdtls->server_ee_intermediate_filename_ptr,"") != 0)
{
res = McuLFS_ReadFile(pdtls->server_ee_intermediate_filename_ptr, &buffer, &bufferSize);
if((res == 0)){
ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *)buffer,bufferSize +1 );
if( ret != 0 )
{
printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
buffer = NULL;
bufferSize = 0;
}
PRINTF("We successfully parsing %s \r\n", pdtls->server_ee_intermediate_filename_ptr);
buffer = NULL;
bufferSize = 0;
}
}

if(strcmp(pdtls->server_root_ca_cert_filename_ptr,"") != 0)
{
res = McuLFS_ReadFile(pdtls->server_root_ca_cert_filename_ptr, &buffer, &bufferSize);
if((res == 0)){
ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *)buffer, bufferSize +1);
if( ret != 0 )
{
printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
buffer = NULL;
bufferSize = 0;
}
PRINTF("We successfully parsing %s \r\n", pdtls->server_root_ca_cert_filename_ptr);
buffer = NULL;
bufferSize = 0;
}

}
res = McuLFS_ReadFile(pdtls->private_key_filename_ptr, &buffer, &bufferSize);
if((res == 0)){
ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *)buffer,bufferSize +1 , NULL, 0 );
if( ret == 0 )
{
PRINTF("We successfully parsing %s \r\n", pdtls->private_key_filename_ptr);
}

buffer = NULL;
bufferSize = 0;
}

if( ( ret = mbedtls_ssl_config_defaults( &conf,
MBEDTLS_SSL_IS_SERVER,
MBEDTLS_SSL_TRANSPORT_DATAGRAM,
MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
}
mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
mbedtls_ssl_conf_read_timeout( &conf, READ_TIMEOUT_MS );

#if defined(MBEDTLS_SSL_CACHE_C)
mbedtls_ssl_conf_session_cache( &conf, &cache,
mbedtls_ssl_cache_get,
mbedtls_ssl_cache_set );
#endif

mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );

if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
{
printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
}

if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
{
printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
}

/* Reevaluate this, since cookie checking is disabled by Null Pointing*/
mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, NULL,
&cookie_ctx );

if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
{
printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
}

mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay, mbedtls_timing_get_delay );

mbedtls_ssl_session_reset( &ssl );

mbedtls_ssl_set_bio( &ssl, NULL,
mbedtls_custom_send, mbedtls_custom_receive, NULL );
}

From my debugging efforts, I am confident that the server certificate and private key parsing have occurred successfully, and the ssl struct contains the appropriate cipher suite, srvcert, and private key.

If possible, I was wondering if you have an example that utilizes LPCXpresso55Sxx as a DTLS server. Having access to a working example might help me identify any misconfigurations on my part.

Thank you for your continued support.

Best regards,

Ahmed Bouzid.
0 Kudos
Reply

2,771 Views
ErichStyger
Specialist I

Hi @ahmed007 ,

I'm sorry, but I don't have such a ready-made example for you.

Erich

0 Kudos
Reply

2,657 Views
ahmed007
Contributor II

Dear @ErichStyger ,

Thank you for being supportive.

Best regards,

Ahmed.

0 Kudos
Reply

3,004 Views
ErichStyger
Specialist I

Just in case: I wrote up an article explaining how to use LittleFS with the internal FLASH memory, using an example project (LPC55S16 in that case):

https://mcuoneclipse.com/2023/07/04/littlefs-file-system-with-mcu-internal-flash-memory/

0 Kudos
Reply

2,994 Views
ahmed007
Contributor II

Dear @ErichStyger ,

I wanted to express my sincere gratitude for your support. I read the article showcasing your impressive work, and I must say, your value is truly remarkable. Thank you for your exceptional contributions.

Best regards,

Ahmed Bouzid

3,355 Views
ErichStyger
Specialist I

Hi @ahmed007 ,

yes, we used LittleFS with the LPC55S16 in different projects (one I have described in https://mcuoneclipse.com/2021/12/30/lorawan-with-nxp-lpc55s16-and-arm-cortex-m33/). For space (data amount) reasons we mostly used an external flash device (Winbond in that case) with it, but we used as well MinINI and LittleFS usint the internal FLASH memory for others.

The Flash memory of the LPC55x is somewhat 'special', but we have covered that with the McuFlash module in these projects which works for other devices too. The McuFlash module implements the block device used by all the different file systems (MinINI, LittleFS, FatFS (https://mcuoneclipse.com/2021/05/15/using-fatfs-and-minini-with-the-nxp-lpc55s16-evk/)) we are using.

I recommend you have a look at the above article/projects (link to Github is in the article).

I hope this helps,

Erich

0 Kudos
Reply

3,256 Views
ahmed007
Contributor II

Hi @ErichStyger ,

I want to express my gratitude for your tremendous support. After carefully studying and attempting the example you provided, I regret to inform you that the code did not successfully mount the LittleFS on the LPCXpresso55S16. Unfortunately, I encountered the same error as in my previous code.

The issue arises when the lfs_mount function returns a result of -84. This particular result is derived from the following line of code:

lfs_stag_t tag = lfs_dir_fetchmatch(lfs, &dir, dir.tail, LFS_MKTAG(0x7ff, 0x3ff, 0), LFS_MKTAG(LFS_TYPE_SUPERBLOCK, 0, 8), NULL, lfs_dir_find_match, &(struct lfs_dir_find_match){ lfs, "littlefs", 8});

Upon debugging, I discovered that the generated tag inside the above function is considered invalid, leading to the function returning -84. This invalidity is caused by a corrupted directory pair.

To provide further context, I have shared my code at the following GitHub link: https://github.com/ahmedbouzid07/littlefs_lpcxpresso55s16/tree/master/lpcxpresso55s16_flashiap1 

I have removed multiple configuration files from your code, as I will solely be working with LPCXpresso55S16. Consequently, there is no need to utilize preprocessors, and I have also omitted the shell part.

If you have the time and willingness, I would greatly appreciate your assistance in finding a solution to this predicament.

Thank you very much for your valuable support.

0 Kudos
Reply

3,247 Views
ErichStyger
Specialist I

Hi @ahmed007 ,

Just to check: have you tried my example 'as is' without modifications, as that works fine on my side.

Did you format your storage area first, as outlined in my terminal screenshot? Do not remove or change anything, unless you know what you are doing. The Shell support is there for a reason.

Just in case, I have attached you the binary files I'm using with my board/system.

I hope this helps,

Erich

0 Kudos
Reply

3,237 Views
ahmed007
Contributor II

Hi @ErichStyger 

Another question, Could you give me how to open an interactive terminal with the board? Since I know how to open a serial terminal with the MCUXpresso IDE terminal feature. I tried to open a serial terminal with Putty and I choose the right COM but didn't succeed.

 

0 Kudos
Reply

3,228 Views
ErichStyger
Specialist I

The LPC55S16 LinkServer debug firmware comes with a VCOM port. The application is using that UART to VCOM.

0 Kudos
Reply

3,224 Views
ahmed007
Contributor II

Hi @ErichStyger,

 

Are you talking about this example: https://github.com/ErichStyger/mcuoneclipse/tree/master/Examples/MCUXpresso/LPC55S16-EVK/LPC55S16_Bl...

If so I think it uses FatFs and minINI and use LittleFS to program the SD card, not the on-chip flash.

Should I try first as it is?

 

Ahmed.

0 Kudos
Reply

3,218 Views
ErichStyger
Specialist I

Hi @ahmed007 ,

short answer: yes.

longer answer: that example includes both FatFS (with SD card) and littleFS (using on-chip Flash). And yes, it supports as well MinINI for both.

Erich

0 Kudos
Reply

3,182 Views
ahmed007
Contributor II

Hi @ErichStyger ,

Thank you for being so supportive. I kindly request your guidance in locating the section where the short names of commands are defined and assigned to their respective functions.

Thank you in advance for your assistance.

Best regards, Ahmed

0 Kudos
Reply

3,175 Views
ErichStyger
Specialist I

Hi @ahmed007 ,

I'm sorry, but I think I don't understand what you are talking about?

But just in case: you have all the source files and code, so you should be able to see how it works?

Erich

0 Kudos
Reply

3,171 Views
ahmed007
Contributor II

Hi @ErichStyger ,

For example, to list directory files or to write binaries to a file we used short commands like ls, or bincat, but these names are not function names, so I thought that we are assigned to this abbreviation like ls the function McuLFS_FileList. I make a small search if there is a file where you assign command names to function but I didn't find anything, that's what I am asking about.

 
Ahmed,
0 Kudos
Reply

3,098 Views
ErichStyger
Specialist I
0 Kudos
Reply

3,089 Views
ahmed007
Contributor II

Hi @ErichStyger 

Thank you very much for being so supportive, thank you for your efforts.

Ahmed,

0 Kudos
Reply

3,186 Views
ahmed007
Contributor II

Hello, Erich Styger!

I want to express my gratitude for your valuable support. I'm pleased to inform you that the example you provided is functioning smoothly on the board. The littlefs has been successfully mounted, allowing us to both write and read files.

I would like to inquire whether you suggest making modifications to your example to align with my specific requirements. In my project, I intend to automate the tasks and refrain from opening a UART terminal on the LPCXpresso. Therefore, it is essential for the file system to be mounted automatically.

Thank you once again for your assistance.

Best regards, Ahmed

 

ahmed007_0-1687357126459.png

 

0 Kudos
Reply

3,239 Views
ahmed007
Contributor II

Hi @ErichStyger 

Actually, I did but I got multiple errors and one of them is in the McuLib.h returns that I have these files missed. I tried to locate them but I didn't find them.

  /* Include shared modules, which are used for the whole project */
    #include "PE_Types.h"
    #include "PE_Error.h"
    #include "PE_Const.h"
    #include "IO_Map.h"
    #include "Cpu.h" /* include CPU related interfaces and defines */
0 Kudos
Reply