FRDM with Arduino Ethernet Shield R3, Part 4: MinIni
Join the DZone community and get the full member experience.
Join For Freei admit: my ethernet shield project got stuck because of too many urgent other priorities. i was not happy with the way the project was using configuration data from flash memory: i have now multiple ethernet shields in use, and configuring the ip address for each shield is a pain. i have not got dhcp working (yet), so why not using the sd card on the shield for configuration data? and right on time i received a tip from marc about minini : perfect, exactly what i need!
minini
minini (see http://www.compuphase.com/minini.htm ) is a programmers library to read and write ini files for embedded systems. ini files are text files with ‘sections’ and ‘keys’, like this one:
[w5100] gateway = 192.168.1.1 netmask = 255.255.255.0 mac = 90-a2-da-0d-42-dd ip = 192.168.0.90
the minini library is small and efficient, basically is only one source and one header file, and is used with ‘glue’ files for different memory systems. one of it is fatfs which i already use a lot. so minini is just perfect for what i need.
minini component
while it the minini sources can be used ‘as is’, i wanted to bring it to the next level, so i have created a minini processor expert component for it:
that way i have tool tips help for each of the methods:
it does not stop here: right-click on the component and use ‘ help on component ‘, and you have access to the user manual (pdf) too:
below is a screenshot of the properties of the component:
- portable strnicmp() : if enabled, the library has a library for strnicmp().
- use real : if enabled, the library enables reading and writing floating point values.
- read only : if enabled, it only reads from the configuration file, and all write methods are disabled.
- no debug : if enabled, assert() in the library is not used. if you disable it, make sure assert() is provided either in your application code or in the standard library.
- fatfs buffer size : this size (in bytes) defines the maximum file name size and the size of a configuration line item. the library does not use any global variables, so make sure that your stack has enough space available.
fatfs string functions
because minini uses the fatfs string functions f_gets() and f_puts(), make sure you have them enabled:
usage
the following code shows how i read configuration data from the sd card to configure the wiznet 5100 chip:
#if pl_use_ini { uint8_t buf[32]; int val; const unsigned char *p; uint8_t res; cls1_sendstr((unsigned char*)"loading values from " ini_file_name "\r\n", cls1_getstdio()->stdout); /* gateway */ val = mini1_ini_gets(ini_section_name, "gateway", "192.168.1.1", (char*)buf, sizeof(buf), ini_file_name); cls1_sendstr((unsigned char*)"gateway: ", cls1_getstdio()->stdout); cls1_sendstr(buf, cls1_getstdio()->stdout); cls1_sendstr((unsigned char*)"\r\n", cls1_getstdio()->stdout); if (val!=0) { p = &buf[0]; res = util1_scanseparatednumbers(&p, &w5100_config.gateway[0], sizeof(w5100_config.gateway), '.', util1_sep_num_type_uint8); if (res!=err_ok) { cls1_sendstr((unsigned char*)"gateway failed!\r\n", cls1_getstdio()->stdout); } } /* netmask */ val = mini1_ini_gets(ini_section_name, "netmask", "255.255.255.0", (char*)buf, sizeof(buf), ini_file_name); cls1_sendstr((unsigned char*)"netmask: ", cls1_getstdio()->stdout); cls1_sendstr(buf, cls1_getstdio()->stdout); cls1_sendstr((unsigned char*)"\r\n", cls1_getstdio()->stdout); if (val!=0) { p = &buf[0]; res = util1_scanseparatednumbers(&p, &w5100_config.netmask[0], sizeof(w5100_config.netmask), '.', util1_sep_num_type_uint8); if (res!=err_ok) { cls1_sendstr((unsigned char*)"netmask failed!\r\n", cls1_getstdio()->stdout); } } /* ip */ val = mini1_ini_gets(ini_section_name, "ip", "192.168.0.1", (char*)buf, sizeof(buf), ini_file_name); cls1_sendstr((unsigned char*)"ip: ", cls1_getstdio()->stdout); cls1_sendstr(buf, cls1_getstdio()->stdout); cls1_sendstr((unsigned char*)"\r\n", cls1_getstdio()->stdout); if (val!=0) { p = &buf[0]; res = util1_scanseparatednumbers(&p, &w5100_config.ipaddr[0], sizeof(w5100_config.ipaddr), '.', util1_sep_num_type_uint8); if (res!=err_ok) { cls1_sendstr((unsigned char*)"ip failed!\r\n", cls1_getstdio()->stdout); } } /* ip */ val = mini1_ini_gets(ini_section_name, "mac", "ff-ff-ff-ff-ff-ff", (char*)buf, sizeof(buf), ini_file_name); cls1_sendstr((unsigned char*)"mac: ", cls1_getstdio()->stdout); cls1_sendstr(buf, cls1_getstdio()->stdout); cls1_sendstr((unsigned char*)"\r\n", cls1_getstdio()->stdout); if (val!=0) { p = &buf[0]; res = util1_scanseparatednumbers(&p, &w5100_config.hwaddr[0], sizeof(w5100_config.hwaddr), '-', util1_sep_num_type_uint8_hex_no_prefix); if (res!=err_ok) { cls1_sendstr((unsigned char*)"mac failed!\r\n", cls1_getstdio()->stdout); } } } #endif
with this, the output to the shell in my application is as below:
reset w5100. configure network. loading values from config.ini gateway: 192.168.1.1 netmask: 255.255.255.0 ip: 192.168.0.90 mac: 90-a2-da-0d-42-dd configure rx/tx memory. done! running web server...
the full source code can be found in the github project link at the end of this article.
summary
with minini i have a convenient way to configure my projects based on fatfs and minini . having both parts as processor expert components makes things even simpler and faster to use. my ethernet project for the frdm-kl25z has been updated on github to use minini . the component sources are on github too and will be released with the next *.peupd update.
and of course i have some more ideas:
- adding command line interface to the component
- offer other storage than fatfs: microcontroller flash memory, external eeprom would be great!
ps: thanks again to marc!
Published at DZone with permission of Erich Styger, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Integrate Cucumber in Playwright With Java
-
Alpha Testing Tutorial: A Comprehensive Guide With Best Practices
-
Mastering Go-Templates in Ansible With Jinja2
-
Step Into Serverless Computing
Comments