Add write to memory; fix write delay.
authorWalter Fetter Lages <w.fetter@ieee.org>
Fri, 1 Jun 2018 01:32:20 +0000 (22:32 -0300)
committerWalter Fetter Lages <w.fetter@ieee.org>
Fri, 1 Jun 2018 01:32:20 +0000 (22:32 -0300)
Makefile
lib/st25_i2c.cpp
st25_i2c_write/Makefile [new file with mode: 0644]
st25_i2c_write/st25_i2c_write.cpp [new file with mode: 0644]

index 7b06fb2..3189332 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -11,7 +11,8 @@ export CCLIBDIR=
 export LIBDIR=-L$(PACKAGEROOT)/lib
 
 PACKAGES=              lib \
-                       st25_i2c_read
+                       st25_i2c_read \
+                       st25_i2c_write
 
 all:
        for i in $(PACKAGES); do $(MAKE) -C $$i; done
index a9828ff..781f86a 100644 (file)
@@ -38,6 +38,8 @@
 
 #include <st25_i2c.hpp>
 
+#define TW_US  5000
+
 enum DYNAMIC_REGISTERS
 {
         GPO_CTRL_DYN=  0x2000,
@@ -69,7 +71,6 @@ St25I2c::St25I2c(const char i2cDev[],uint8_t i2cAdd): i2cAdd_(i2cAdd)
                                     => dynamic configuration => register 0x2000-0x2007
                                     => FTM mailbox => register 0x2008-0x2107 */
         if(ioctl(fd_,I2C_SLAVE,i2cAdd_ >> 1) < 0) throw I2cAddressError(errno);
-
 }
 
 St25I2c::~St25I2c(void)
@@ -128,6 +129,10 @@ size_t St25I2c::write(uint16_t addr,const void *buf,size_t count)
         if((c=::write(fd_,buffer,count+sizeof addr)) < 0) throw WriteError(errno);
         delete[] buffer;
 
+        /* Tw*pages */
+        int writeTime=TW_US*(count+3+addr%4)/4;
+        usleep(writeTime);
+
         if((addr < DYNAMIC_CONFIG) || (addr == FTM_MAILBOX)) setFtm(savedFtm);
         
         return c-sizeof addr;
diff --git a/st25_i2c_write/Makefile b/st25_i2c_write/Makefile
new file mode 100644 (file)
index 0000000..d02f778
--- /dev/null
@@ -0,0 +1,27 @@
+TARGET=st25_i2c_write
+SRCS=$(TARGET).cpp
+
+FLAGS=-O2 -Wall -g -MMD
+INCLUDE=-I../include
+LIBDIR=-L../lib
+LIBS=-lst25_i2c
+
+CC=$(CROSS_COMPILE)g++
+CFLAGS=$(FLAGS) $(INCLUDE)
+LDFLAGS=$(LIBDIR) $(LIBS) -MMD
+
+all: $(TARGET)
+
+$(TARGET): $(SRCS:.cpp=.o)
+       $(CC) -o $@ $^ $(LDFLAGS)
+
+%.o: %.cpp
+       $(CC) $(CFLAGS) -c -o $@ $<
+
+-include $(SRCS:.cpp=.d)
+
+clean:
+       rm -f *~ *.bak *.o *.d
+
+distclean: clean
+       rm -f $(TARGET)
diff --git a/st25_i2c_write/st25_i2c_write.cpp b/st25_i2c_write/st25_i2c_write.cpp
new file mode 100644 (file)
index 0000000..403e05a
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+  
+  Copyright (c) 2018 Walter Fetter Lages <w.fetter@ieee.org>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+    You can also obtain a copy of the GNU General Public License
+    at <http://www.gnu.org/licenses>.
+
+*/
+
+#include <stdlib.h>
+
+#include <iomanip>
+#include <iostream>
+
+#include <st25_i2c.hpp>
+
+int main(int argc,char *argv[])
+{
+        if(argc < 3 || argc > 256+2)
+        {
+            std::cout << argv[0]
+                << "Write to ST25 user memory, dynamic configuration registers or FTM mailbox."
+                << std::endl;
+            std::cout << "\tUsage: " << argv[0] << " address data0 [.. [data255]]" << std::endl;
+            return -1;
+        }
+    
+        uint16_t addr=strtol(argv[1],NULL,0);
+        size_t count=argc-2;
+   
+        char *data=new char[count];
+        for(size_t i=0;i < count;i++) data[i]=strtol(argv[i+2],NULL,0);
+       
+        try
+        {
+            St25I2c st25I2c;
+            st25I2c.write(addr,data,count);
+        }
+        catch(std::exception &e)
+        {
+            std::cerr << e.what() << std::endl;
+            const St25I2c::Error *err=dynamic_cast<St25I2c::Error *>(&e);
+            return (err==NULL)? -1:err->code();
+        }
+
+        delete[] data;
+
+        return 0;
+}