AT24C on NodeMCU Lua

    This page is to Setup and Config AT24C on NodeMCU Lua

Hardware and Software

Hardware 
    
AT24C on NodeMCU Lua

Software 
    ESplorer 


Sanki Note

  • SDA : D3 (GPIO 0 ) / SCL D4 (GPIO 2)

Examples

          This example is AT24C Library (Lua) -- AT24CXXX_Lib.lua
  1. -- I2C Must be Sda:D3 and cls:D4
    --
    -- AT24C01  -->  1Kb  =  1024 Byte(Char) [ Page   8 * 128 ]
    -- AT24C32  -->  32Kb =  4096 Byte(Char) [ Page  32 * 128 ] 
    -- AT24Cbitpage --> bitpageKb = 32768 Byte(Char) [ Page  64 * 512 ]
    -- AT24C512 --> 512Kb = 65536 Byte(Char) [ Page 128 * 512 ]
    --
    -- Address 0x50 -- 0x57 based on A1, A2, A3 On and Off
    -- 
    -- E.g. AT24C32
    -- Pos from 0 to 4096
    -- write len page 32 words only

    local id = 0
    local eesize = 4096 -- default 32K
    local page = 32 -- degault 32k
    local bitpage = page * 8
    local Nom_du_module = ...
    local M = {}
    _G[Nom_du_module] = M

    -- c = id : EEPROM ID e.g. 0  
    -- s = eesize : Byte e.g. 4096 (32K)
    -- d = sda (Pin 3)
    -- l = scl (Pin 4)
    -- a = device : 0x50 - 0x57
    function M.init(c, d, l, a, s)

        id = c
        eesize = (s * 1024) / 8
        sda = d
        scl = l
        device = a
        if eesize == 512 then page = 16 end -- 4K
        if eesize == 16384 then page = 64 end -- 128K
        if eesize == 32768 then page = 64 end -- 256 Kb
        if eesize == 65536 then page = 128 end -- 512Kb
        bitpage = 256
      
        if (i2c.setup(id, sda, scl, i2c.SLOW)) ~= 0 then 
            print("------ I2C EEPROM Connected")
            print("ID     : " .. id)
            print("EEPROM : " .. s .. "Kb / " .. eesize .. " Bytes (Words)")
            print("Bit / Page : " .. bitpage .. " Bits")
            print("Byte / Page : " .. page .. " Bytes")
            print("Total Page : " .. eesize / page  .. " Pages")
            print("Device : " .. string.format("0x" .. "%x", a))
            print("SDA    : pin " .. d)
            print("SCL    : pin " .. l)
            print("---------------------------")
        elseif (i2c.setup(id, sda, scl, i2c.SLOW)) == 0 then  print("Error in the configuration of the I2C Port!") return nil
        end
        
    end

    function M.getInfo() 
        return id, eesize, page, bitpage
    end

    function M.read_octet(haute, basse)

        i2c.start(id)
        i2c.address(id, device, i2c.TRANSMITTER)
        i2c.write(id, haute)
        i2c.write(id, basse)
        i2c.stop(id)
        i2c.start(id)
        i2c.address(id, device, i2c.RECEIVER)
        c=i2c.read(id, 1)
        i2c.stop(id)
        return c

    end

    function M.read_byte(charpos)

        i2c.start(id)
        i2c.address(id, device, i2c.TRANSMITTER)
        i2c.write(id, charpos / bitpage % bitpage )
        i2c.write(id, charpos % bitpage)
        i2c.stop(id)
        i2c.start(id)
        i2c.address(id, device, i2c.RECEIVER)
        c=i2c.read(id, 1)
        i2c.stop(id)
        return c

    end

    function M.write_byte(charpos, valeur)

        i2c.start(id)
        i2c.address(id, device, i2c.TRANSMITTER)
        i2c.write(id, charpos / bitpage % bitpage)
        i2c.write(id, charpos % bitpage)
        i2c.write(id, valeur)
        i2c.stop(id)
        tmr.delay(750)
        
    end

    function M.write_octet(haute, basse, valeur)

        i2c.start(id)
        i2c.address(id, device, i2c.TRANSMITTER)
        i2c.write(id, haute)
        i2c.write(id, basse)
        i2c.write(id, valeur)
        i2c.stop(id)
        tmr.delay(750)
        
    end

    function M.write_sequenceHEX(haute, basse, valeur)

        fin_bas = bit.band(string.len(valeur),0xff)
        fin_haut =bit.rshift(string.len(valeur),8)
        i2c.start(id)
        i2c.address(id, device, i2c.TRANSMITTER)
        i2c.write(id, haute)
        i2c.write(id, basse)
        i2c.write(id, valeur)
        i2c.stop(id)
        tmr.delay(750)
        
    end

    function M.read_sequenceHEX(haute, basse, longueur)

        i2c.start(id)
        i2c.address(id, device, i2c.TRANSMITTER)
        i2c.write(id, haute)
        i2c.write(id, basse)
        i2c.stop(id)
        i2c.start(id)
        i2c.address(id, device, i2c.RECEIVER)
        c = i2c.read(id, longueur)
        print(c)
        i2c.stop(id)
        return c
        
    end

    function M.write_sequence(charpos, valeur)
        if (string.len(valeur) > page) then
            print("[Warning : Write Size " .. string.len(valeur) .. " over Page Size " .. page .."]")
        end
        fin_bas = bit.band(string.len(valeur),0xff)
        fin_haut =bit.rshift(string.len(valeur),8)
        i2c.start(id)
        i2c.address(id, device, i2c.TRANSMITTER)
        i2c.write(id, charpos / bitpage % bitpage)
        i2c.write(id, charpos % bitpage)
        i2c.write(id, valeur)
        i2c.stop(id)
        tmr.delay(750)
        
    end

    function M.read_sequence(charpos, longueur)

        i2c.start(id)
        i2c.address(id, device, i2c.TRANSMITTER)
        i2c.write(id, charpos / bitpage % bitpage)
        --print("charpos : " .. charpos)
        --print("bitpage : " .. bitpage)
        i2c.write(id, charpos % bitpage)
        i2c.stop(id)
        i2c.start(id)
        i2c.address(id, device, i2c.RECEIVER)
        c = i2c.read(id, longueur)
        --print(c)
        i2c.stop(id)
        return c
        
    end

    -- fill up all the EEPROM with 0xFF
    function M.format(_eesize)
        --local page = 32
        local words = _eesize / 8
        --if _eesize >= 16384 then page = 64 end
        --if _eesize >= 65536 then page = 128 end
        local s = string.rep(string.char(0xFF), page)
        for a=0, _eesize-page, page do
            M.write_sequence(a, s)
            tmr.wdclr()
            --print("Formated Page " .. a .. " / " .. _eesize)
        end
        --_ee = 0
        s=nil
        print("Formated Size : " .. _eesize .. " bits / " .. words .. " Bytes");            
         
    end

    -- get address of first 0xFF data in EE at addr/8
    function M.find(sPos, ePos, str)
        local r = nil
        for a=sPos, ePos, 1 do
        --print("Pos : " .. a .. " --> " .. M.read_sequence(a, string.len(str)))
        if M.read_sequence(a, string.len(str)) == str then
            r = a
            break
        end
        tmr.wdclr()
        end
        return r
    end

    function M.dump(sPos, ePos)
        local r = ""
        for a=sPos, ePos, 4 do
        r = r .. M.read_sequence(a, 4) .. " "
        tmr.wdclr()
        end
        return r
    end


    function M.dumpList(sPos, ePos)
        local r = string.format("%3s","")
        if page == 64  then r = r .. "    0    4    8    12   16   20   24   28   32   36   40   44   48   52   56   60" end
        if page == 128 then r = r .. "    0    4    8    12   16   20   24   28   32   36   40   44   48   52   56   60   64   68   72   76   80   84   88   92   96   100  104  108  112  116  120  124" end
        if page == 32  then r = r .. "    0    4    8    12   16   20   24   28" end    
        print("\n" .. r)
        for i=sPos, ePos, page do
            --for a=0, page, 4 do
                print(string.format("%4s",i) .. " : " .. AT24CXXX_Lib.dump(i, i + (page -1)))
                tmr.wdclr()
           -- end
        end    
    end

    function M.checkPos(aPos, str) 
        begin = aPos % page
        strLen = string.len(str)
        if (begin + strLen > 32) then 
            return "String too long" 
        end
        return "OK"
    end

Examples

          This example is AT24C Library (Lua) -- AT24CXXX_Test.lua
  1. -- I2C Must be Sda:D3 and cls:D4
    --
    -- AT24C01  -->  1Kb  =  1024 Byte(Char) [ Page   8 * 128 ]
    -- AT24C32  -->  32Kb =  4096 Byte(Char) [ Page  32 * 128 ] 
    -- AT24C256 --> 256Kb = 32768 Byte(Char) [ Page  64 * 512 ]
    -- AT24C512 --> 512Kb = 65536 Byte(Char) [ Page 128 * 512 ]
    --
    -- Address 0x50 -- 0x57 based on A1, A2, A3 On and Off
    -- 
    -- E.g. AT24C32
    -- Pos from 0 to 4096
    -- write len page 32 words only

    require("AT24CXXX_Lib")

    -- c = id : EEPROM ID e.g. 0  
    -- s = eesize : 32k = (32 * 1024) / 8 =  4096 (32K)
    -- d = sda (Pin 3)
    -- l = scl (Pin 4)
    -- a = device : 0x50 - 0x57 

    AT24CXXX_Lib.init(0, 3, 4, 0x50, 256)

    print("---------------- Format -------------")

    --AT24CXXX_Lib.format(4096)

    print("\n---------------- Char Pos -------------")
    aaPos = 896
    aByte = string.format("Position : " .. "%s", aaPos)
    rrPos = aaPos
    AT24CXXX_Lib.write_byte(aaPos, aByte)
    print("\nWrite : " .. aByte .. " to " .. aaPos)
    print("\nRead  : " .. aaPos .. " --> [" .. AT24CXXX_Lib.read_byte(rrPos) .. "]")



    print("\n---------------- a Words -------------")
    aWords = "感冒其實不必看醫生";
    aWordsLen = string.len(aWords);
    wPos = 0

    AT24CXXX_Lib.write_sequence(wPos, aWords)
    print("\nWrite : Pos [" .. wPos .. "] < -- [" .. aWords .. "] (" .. aWordsLen .. ")")
    print("\nRead : Pos [" .. wPos .. "] --> [" .. AT24CXXX_Lib.read_sequence(wPos, aWordsLen ) .. "]")

    print("\nCheck : [" .. AT24CXXX_Lib.checkPos(wPos, aWords) .. "]") 


    print("\n---------------- DUMP -------------")

    id, eesize, page, bitpage = AT24CXXX_Lib.getInfo
    for i=0, 1024, 32 do
        print(string.format("%4s",i) .. " : " .. AT24CXXX_Lib.dump(i, i + 31))
    end

    AT24CXXX_Lib.dumpList(0, 1024)

    print("\n---------------- Find -------------")

    sPos = 0
    ePos = 100
    findStr = "看醫"
    retPos = AT24CXXX_Lib.find(sPos, ePos, findStr)
    if retPos ~= nil then
        print("\nFound : [" .. findStr .. "] on Byte Pos [" .. retPos .. "] in Range [" .. sPos .. ":" .. ePos .. "]")
    else
        print("\nNot Found [" .. findStr .. "] in Byte Range [" .. sPos .. ":" .. ePos .. "]")
    end    
    print("\n")


    AT24CXXX_Lib = nil
    package.loaded["AT24CXXX_Lib"]=nil