Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

FRDM with Arduino Ethernet Shield R3, Part 4: MinIni

DZone's Guide to

FRDM with Arduino Ethernet Shield R3, Part 4: MinIni

· IoT Zone
Free Resource

I 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!

Ethernet Shield with SD card

Ethernet Shield with SD card

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:

MinIni Component Methods

MinIni Component Methods

That way I have tool tips help for each of the methods:

MinIni Tooltip help

MinIni Tooltip help

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:

Component Help with miniIni User Manual

Component Help with miniIni User Manual

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.
MinIni Component Properites

MinIni Component Properites

FatFS String Functions

Because MinIni uses the FatFS string functions f_gets() and f_puts(), make sure you have them enabled:

Enabled String Functions in FatFS

U Enabled String Functions in FatFS

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:

  1. Adding command line interface to the component
  2. Offer other storage than FatFS: microcontroller Flash memory, external EEPROM would be great!

PS: Thanks again to Marc!

Topics:

Published at DZone with permission of Erich Styger, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}