Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SakuraIO by
SakuraIO_I2C.cpp
00001 /* SAKURA Internet IoT Communication Module Library for mbed 00002 * 00003 * The MIT License (MIT) 00004 * 00005 * Copyright (c) SAKURA Internet Inc. 00006 * 00007 * Permission is hereby granted, free of charge, to any person obtaining a copy 00008 * of this software and associated documentation files (the "Software"), 00009 * to deal in the Software without restriction, including without limitation 00010 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00011 * and/or sell copies of the Software, and to permit persons to whom the Software 00012 * is furnished to do so, subject to the following conditions: 00013 * 00014 * The above copyright notice and this permission notice shall be included 00015 * in all copies or substantial portions of the Software. 00016 * 00017 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00018 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00020 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00021 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00022 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00023 */ 00024 00025 #include "mbed.h" 00026 #include "SakuraIO.h" 00027 #include "debug.h" 00028 00029 #define SAKURAIO_SLAVE_ADDR 0x4F 00030 00031 #define MODE_IDLE 0x00 00032 #define MODE_WRITE 0x01 00033 #define MODE_READ 0x02 00034 00035 void SakuraIO_I2C::begin(){ 00036 mode = MODE_IDLE; 00037 } 00038 00039 void SakuraIO_I2C::end(){ 00040 switch(mode){ 00041 case MODE_WRITE: 00042 i2c.stop(); 00043 break; 00044 case MODE_READ: 00045 while (_length > 0) { 00046 _length --; 00047 i2c.read(_length > 0 ? 1 : 0); 00048 } 00049 i2c.stop(); 00050 break; 00051 } 00052 00053 mode = MODE_IDLE; 00054 } 00055 00056 void SakuraIO_I2C::sendByte(uint8_t data){ 00057 if(mode != MODE_WRITE){ 00058 dbg("beginTr\r\n"); 00059 i2c.start(); 00060 i2c.write(SAKURAIO_SLAVE_ADDR<<1); 00061 mode = MODE_WRITE; 00062 } 00063 dbg("Write=%02x\r\n", data); 00064 i2c.write(data); 00065 } 00066 00067 00068 uint8_t SakuraIO_I2C::startReceive(uint8_t length){ 00069 if(mode != MODE_IDLE){ 00070 dbg("endTransmission\r\n"); 00071 i2c.stop(); 00072 } 00073 dbg("requestForm=%d\r\n", length); 00074 _length = length; 00075 i2c.start(); 00076 i2c.write(SAKURAIO_SLAVE_ADDR<<1 | 1); 00077 mode = MODE_READ; 00078 return 0; 00079 } 00080 00081 uint8_t SakuraIO_I2C::receiveByte(){ 00082 return receiveByte(false); 00083 } 00084 00085 uint8_t SakuraIO_I2C::receiveByte(bool stop){ 00086 uint8_t ret = 0; 00087 if (_length > 0) { 00088 _length --; 00089 ret = i2c.read(_length > 0 ? 1 : 0); 00090 } 00091 if(stop){ 00092 end(); 00093 } 00094 dbg("Read=%d\r\n", ret); 00095 return ret; 00096 } 00097 00098 SakuraIO_I2C::SakuraIO_I2C (I2C &_i2c) : i2c(_i2c) { 00099 mode = MODE_IDLE; 00100 } 00101 00102 SakuraIO_I2C::SakuraIO_I2C (PinName sda, PinName scl) : i2c(sda, scl) { 00103 mode = MODE_IDLE; 00104 }
Generated on Tue Jul 19 2022 22:02:05 by
1.7.2
