Find in Module Config File

From DSignT Support Database
Revision as of 15:49, 15 December 2017 by Root (talk | contribs) (Bugfix)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Find an Item in the Module Configuration File

#define CFGFILE_STARTADDR 0x10000  /* board specific, change may be required */
#define CFGFILE_ENDADDR   0x20000  /* board specific, change may be required */

/***********************************************************************
  @brief    find a string in the config file
    
  @param    str   - string to find
  @param    start - start address of Config File in Flash
  @param    end   - end address of config File in Flash
  @return   Flash Address, 0 if string not found

***********************************************************************/
uint32_t DM2_cfgFindString (char *str, uint32_t start, uint32_t end)
{
    /*******************************************************************
      locals
    *******************************************************************/
    uint32_t addr = start;
    char c;
    char *p_str;
    uint32_t tempaddr;

    /*******************************************************************
      read Flash from start to end address until string is found 
      or end of file 
    *******************************************************************/
    while (1)
    {
        // if address = end of Config File address range, return (0)
        if (addr == end) return (0);

        // read Flash
        c = DM2_flashRead((void *)addr);

        // 0xFF: end of file reached, return (0): section not found
        if (c == 0xFF) return (0);

        // if comment, skip rest of line
        if (c == ';') 
        {
            do
            {
                if (++addr == end) return (0);
                c = DM2_flashRead((void *)addr);
                if (c == 0xFF) return (0);
            } while (c != 0x0D);
        }

        // if first character matches, check remaining characters
        // if all characters match return address
        if (c == *str)
        {
            p_str = str;
            tempaddr = addr;
            do 
            {
                p_str++;
                tempaddr++;
                if (*p_str == 0)  return (addr);
                if (tempaddr == end) return (0);
                c = DM2_flashRead((void *)tempaddr);
            } while (c == *p_str);
        }
        addr++;
    }
}

Defines CFGFILE_STARTADDR and CFGFILE_ENDADDR limit the search range. The function returns 0 if the string was not found. The following example searches section [UART1] in the entire module configuration file:

sectionstart = DM2_cfgFindString ("[UART1]", CFGFILE_STARTADDR, CFGFILE_ENDADDR);

To search an entry within a section, first define the range by searching the desired section and the next section delimiter, then search the item inside this range

sectionstart = DM2_cfgFindString ("[UART1]", CFGFILE_STARTADDR, CFGFILE_ENDADDR);
if (sectionstart > 0)
{
    sectionend = DM2_cfgFindString ("[", sectionstart+1, CFGFILE_ENDADDR);
    itemaddr = DM2_cfgFindString ("baud rate", sectionstart, sectionend);
}


Contact Post.png Support Tool.png