From: Walter Fetter Lages Date: Thu, 31 May 2018 18:47:42 +0000 (-0300) Subject: Initial commit. X-Git-Url: http://git.ece.ufrgs.br/?a=commitdiff_plain;h=539521b3c659e8537ccce5c091a30abee15a9fdf;p=st25_i2c.git Initial commit. --- 539521b3c659e8537ccce5c091a30abee15a9fdf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c02f83a --- /dev/null +++ b/.gitignore @@ -0,0 +1,307 @@ +# Text editor +# Backup files +*.*~ +*.bak + +# C++ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +# Java +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#TeX +## Core latex/pdflatex auxiliary files: +*.aux +*.lof +*.log +*.lot +*.fls +*.out +*.toc +*.fmt +*.fot +*.cb +*.cb2 +.*.lb + +## Intermediate documents: +*.dvi +*.xdv +*-converted-to.* + +## Generated if empty string is given at "Please type another file name for output:" +.pdf + +## Bibliography auxiliary files (bibtex/biblatex/biber): +*.bbl +*.bcf +*.blg +*-blx.aux +*-blx.bib +*.run.xml + +## Build tool auxiliary files: +*.fdb_latexmk +*.synctex +*.synctex(busy) +*.synctex.gz +*.synctex.gz(busy) +*.pdfsync + +## Build tool directories for auxiliary files +# latexrun +latex.out/ + +## Auxiliary and intermediate files from other packages: +# algorithms +*.alg +*.loa + +# achemso +acs-*.bib + +# amsthm +*.thm + +# beamer +*.nav +*.pre +*.snm +*.vrb + +# changes +*.soc + +# cprotect +*.cpt + +# elsarticle (documentclass of Elsevier journals) +*.spl + +# endnotes +*.ent + +# fixme +*.lox + +# feynmf/feynmp +*.mf +*.mp +*.t[1-9] +*.t[1-9][0-9] +*.tfm + +#(r)(e)ledmac/(r)(e)ledpar +*.end +*.?end +*.[1-9] +*.[1-9][0-9] +*.[1-9][0-9][0-9] +*.[1-9]R +*.[1-9][0-9]R +*.[1-9][0-9][0-9]R +*.eledsec[1-9] +*.eledsec[1-9]R +*.eledsec[1-9][0-9] +*.eledsec[1-9][0-9]R +*.eledsec[1-9][0-9][0-9] +*.eledsec[1-9][0-9][0-9]R + +# glossaries +*.acn +*.acr +*.glg +*.glo +*.gls +*.glsdefs + +# gnuplottex +*-gnuplottex-* + +# gregoriotex +*.gaux +*.gtex + +# htlatex +*.4ct +*.4tc +*.idv +*.lg +*.trc +*.xref + +# hyperref +*.brf + +# knitr +*-concordance.tex +# TODO Comment the next line if you want to keep your tikz graphics files +*.tikz +*-tikzDictionary + +# listings +*.lol + +# makeidx +*.idx +*.ilg +*.ind +*.ist + +# minitoc +*.maf +*.mlf +*.mlt +*.mtc[0-9]* +*.slf[0-9]* +*.slt[0-9]* +*.stc[0-9]* + +# minted +_minted* +*.pyg + +# morewrites +*.mw + +# nomencl +*.nlg +*.nlo +*.nls + +# pax +*.pax + +# pdfpcnotes +*.pdfpc + +# sagetex +*.sagetex.sage +*.sagetex.py +*.sagetex.scmd + +# scrwfile +*.wrt + +# sympy +*.sout +*.sympy +sympy-plots-for-*.tex/ + +# pdfcomment +*.upa +*.upb + +# pythontex +*.pytxcode +pythontex-files-*/ + +# thmtools +*.loe + +# TikZ & PGF +*.dpth +*.md5 +*.auxlock + +# todonotes +*.tdo + +# easy-todo +*.lod + +# xmpincl +*.xmpi + +# xindy +*.xdy + +# xypic precompiled matrices +*.xyc + +# endfloat +*.ttt +*.fff + +# Latexian +TSWLatexianTemp* + +## Editors: +# WinEdt +*.bak +*.sav + +# Texpad +.texpadtmp + +# Kile +*.backup + +# KBibTeX +*~[0-9]* + +# auto folder when using emacs and auctex +./auto/* +*.el + +# expex forward references with \gathertags +*-tags.tex + +# standalone packages +*.sta + +# generated if using elsarticle.cls +*.spl diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7b06fb2 --- /dev/null +++ b/Makefile @@ -0,0 +1,44 @@ +export PACKAGEROOT=$(PWD) + +export CC=$(CROSS_COMPILE)gcc +export CCAR=$(CROSS_COMPILE)ar +export CCBIN2HEX=$(CROSS_COMPILE)bin2hex +export CCFLAGS=-Wall -O2 -g -MMD +export CPU= +export CCINCDIR=-I$(PACKAGEROOT)/include +export CCLIBDIR= + +export LIBDIR=-L$(PACKAGEROOT)/lib + +PACKAGES= lib \ + st25_i2c_read + +all: + for i in $(PACKAGES); do $(MAKE) -C $$i; done + +check: + for i in $(PACKAGES); do $(MAKE) -C $$i check; done + +clean: + for i in $(PACKAGES); do $(MAKE) -C $$i clean; done + rm -rf *~ *.bak + +configure: + for i in $(PACKAGES); do $(MAKE) -C $$i configure; done + +distclean: + for i in $(PACKAGES); do $(MAKE) -C $$i distclean; done + rm -rf *~ *.bak + +install: + for i in $(PACKAGES); do $(MAKE) -C $$i install; done + +uninstall: + for i in $(PACKAGES); do $(MAKE) -C $$i uninstall; done + +unpack: + for i in $(PACKAGES); do $(MAKE) -C $$i unpack; done + +upload: + for i in $(AICPACKAGES); do $(MAKE) -C $$i upload; done + \ No newline at end of file diff --git a/include/st25_i2c.hpp b/include/st25_i2c.hpp new file mode 100644 index 0000000..880670e --- /dev/null +++ b/include/st25_i2c.hpp @@ -0,0 +1,103 @@ +/* + EFC-Project + + St25I2c Class + + Copyright (c) 2017 Walter Fetter Lages + + 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 . + +*/ + +#ifndef ST25_I2C_HPP +#define ST25_I2C_HPP + +#include +#include + +class St25I2c +{ + int fd_; + uint8_t i2cAdd_; + + public: + St25I2c(const char i2cDev[]="/dev/i2c-0",uint8_t i2cAdd=0xa6); + ~St25I2c(void); + /** Random address read */ + size_t read(uint16_t addr,void *buf,size_t count); + /** Current address read */ + size_t read(void *buf,size_t count); + /** Random address write */ + size_t write(uint16_t addr,const void *buf,size_t count); + /** Set fast transfer mode */ + bool setFtm(bool ftm); + /** Get fast transfer mode */ + bool getFtm(void); + + enum MEMORY_MAP + { + USER_MEMORY= 0x0000, + DYNAMIC_CONFIG= 0x2000, + FTM_MAILBOX= 0x2008 + }; + + class Error + { + int e_; + + public: + Error(const int e):e_(e) { }; + }; + class OpenError: public Error + { + public: + OpenError(const int e):Error(e) { }; + }; + class CloseError: public Error + { + public: + CloseError(const int e):Error(e) { }; + }; + class I2cAddressError: public Error + { + public: + I2cAddressError(const int e):Error(e) { }; + }; + class WriteError: public Error + { + public: + WriteError(const int e):Error(e) { }; + }; + class ReadError: public Error + { + public: + ReadError(const int e):Error(e) { }; + }; + class SizeError: public Error + { + public: + SizeError(void):Error(EMSGSIZE) { }; + }; + class FtmAddressError: public Error + { + public: + FtmAddressError(void):Error(EFAULT) { }; + }; +}; + +#endif diff --git a/lib/Makefile b/lib/Makefile new file mode 100644 index 0000000..8ef066e --- /dev/null +++ b/lib/Makefile @@ -0,0 +1,29 @@ +TARGET=libst25_i2c.a +SRCS=st25_i2c.cpp + +FLAGS=-O2 -Wall -g -MMD +INCLUDE=-I../include +LIBDIR=-L../lib +LIBS= + +CC=$(CROSS_COMPILE)g++ +CCAR=$(CROSS_COMPILE)ar +CFLAGS=$(FLAGS) $(INCLUDE) +LDFLAGS=$(LIBDIR) $(LIBS) +CCARFLAGS=-crvs + +all: $(TARGET) + +$(TARGET): $(SRCS:.cpp=.o) + $(CCAR) $(CCARFLAGS) $@ $^ + +%.o: %.cpp + $(CC) $(CFLAGS) -c -o $@ $< + +-include $(SRCS:.cpp=.d) + +clean: + rm -f *~ *.bak *.o *.d + +distclean: clean + rm -f $(TARGET) diff --git a/lib/st25_i2c.cpp b/lib/st25_i2c.cpp new file mode 100644 index 0000000..8caa5d8 --- /dev/null +++ b/lib/st25_i2c.cpp @@ -0,0 +1,149 @@ +/* + EFC-Project + + St25I2c Class implementation + + Copyright (c) 2017 Walter Fetter Lages + + 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 . + +*/ + + +#include +#include +#include + +#include +#include + +#include +#include + +#include + +enum DYNAMIC_REGISTERS +{ + GPO_CTRL_DYN= 0x2000, + EH_CTRL_DYN= 0x2002, + RF_MNGT_DYN= 0x2003, + I2C_SSO_DYN= 0x2004, + IT_STS_DYN= 0x2005, + MB_CTRL_DYN= 0x2006, + MB_LEN_DYN= 0x2007 +}; + +enum MB_CTRL_DYN_BITS +{ + MB_EN= 0x01, + HOST_PUT_MSG= 0x02, + RF_PUT_MSG= 0x04, + RFU= 0x08, + HOST_MISS_MSG= 0x10, + RF_MISS_MSG= 0x20, + HOST_CURRENT_MSG=0x40, + RF_CURRENT_MSG= 0x80 +}; + +St25I2c::St25I2c(const char i2cDev[],uint8_t i2cAdd): i2cAdd_(i2cAdd) +{ + if((fd_=open(i2cDev,O_RDWR)) < 0) throw OpenError(errno); + /* I2C Address 0xae (system memory) => 0x57 is used by Galileo EEPROM */ + /* I2C Address 0xa6 => 0x53 => user memory => register 0x0000-0x1ffc + => dynamic configuration => register 0x2000-0x2007 + => FTM mailbox => register 0x2008-0x2107 */ + if(ioctl(fd_,I2C_SLAVE,i2cAdd_ >> 1) < 0) throw I2cAddressError(errno); + +} + +St25I2c::~St25I2c(void) +{ + if(close(fd_) < 0) throw CloseError(errno); +} + +size_t St25I2c::read(uint16_t addr,void *buf,size_t count) +{ + bool savedFtm=false; + if(addr >= FTM_MAILBOX && addr < FTM_MAILBOX+256) savedFtm=setFtm(true); + + addr=htons(addr); + if(::write(fd_,&addr,sizeof addr) < 0) throw WriteError(errno); + + size_t c; + if((c=::read(fd_,buf,count)) < 0) throw ReadError(errno); + + if(addr >= FTM_MAILBOX && addr < FTM_MAILBOX+256) setFtm(savedFtm); + + return c; +} + +size_t St25I2c::read(void *buf,size_t count) +{ + + size_t c; + if((c=::read(fd_,buf,count)) < 0) throw ReadError(errno); + return c; +} + +size_t St25I2c::write(uint16_t addr,const void *buf,size_t count) +{ + if(count > 256) throw SizeError(); + + if(addr > FTM_MAILBOX && addr < FTM_MAILBOX+256) throw FtmAddressError(); + + /* + I2C Writing data in user or system memory (EEPROM), transit + via the 256-Bytes Fast Transfer Mode's buffer. Consequently + Fast Transfer Mode must be deactivated before starting any write + operation in user or system memory, otherwise command will be + NotACK, programming is not done and device goes in standby mode. + */ + bool savedFtm=false; + if(addr < DYNAMIC_CONFIG) savedFtm=setFtm(false); + else if(addr == FTM_MAILBOX) savedFtm=setFtm(true); + + char *buffer=new char[count+sizeof addr]; + *buffer=htons(addr); + for(size_t i=0;i < count;i++) buffer[i+sizeof addr]=((char *)buf)[i]; + size_t c; + if((c=::write(fd_,buffer,count+sizeof addr)) < 0) throw WriteError(errno); + delete[] buffer; + + if((addr < DYNAMIC_CONFIG) || (addr == FTM_MAILBOX)) setFtm(savedFtm); + + return c-sizeof addr; +} + +bool St25I2c::setFtm(bool ftm) +{ + uint8_t mbCtrlDyn; + uint8_t newMbCtrlDyn; + + this->read(MB_CTRL_DYN,&mbCtrlDyn,sizeof mbCtrlDyn); + newMbCtrlDyn=mbCtrlDyn | MB_EN; + this->write(MB_CTRL_DYN,&newMbCtrlDyn,sizeof newMbCtrlDyn); + return (mbCtrlDyn & MB_EN); +} + +bool St25I2c::getFtm(void) +{ + uint8_t mbCtrlDyn; + + this->read(MB_CTRL_DYN,&mbCtrlDyn,sizeof mbCtrlDyn); + return (mbCtrlDyn & MB_EN); +} diff --git a/st25_i2c_read/Makefile b/st25_i2c_read/Makefile new file mode 100644 index 0000000..491fad2 --- /dev/null +++ b/st25_i2c_read/Makefile @@ -0,0 +1,27 @@ +TARGET=st25_i2c_read +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_read/st25_i2c_read.cpp b/st25_i2c_read/st25_i2c_read.cpp new file mode 100644 index 0000000..047a78e --- /dev/null +++ b/st25_i2c_read/st25_i2c_read.cpp @@ -0,0 +1,54 @@ +/* + + Copyright (c) 2018 Walter Fetter Lages + + 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 . + +*/ + +#include + +#include +#include + +#include + +int main(int argc,char *argv[]) +{ + if(argc != 2) + { + std::cout << argv[0] + << "Read from ST25 user memory, dynamic configuration registers or FTM mailbox." + << std::endl; + std::cout << "\tUsage: " << argv[0] << " address" << std::endl; + return -1; + } + + St25I2c st25I2c; + + unsigned char data; + + uint16_t addr=strtol(argv[1],NULL,0); + + st25I2c.read(addr,&data,sizeof data); + + std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') + << static_cast(data) << std::endl; + + return 0; +}