FeliCa Link(RC-S730) Library

FeliCa Link(RC-S730) Library

  • Not all API is tested.

RC-S730

Sample

Committer:
hiro99ma
Date:
Sun Mar 29 07:59:13 2015 +0000
Revision:
1:bb5616cb01fb
Parent:
0:be4ff952ae7a
fix comment

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hiro99ma 1:bb5616cb01fb 1 /** FeliCa Link(RC-S730) Library
hiro99ma 1:bb5616cb01fb 2 *
hiro99ma 1:bb5616cb01fb 3 * @file RCS730.cpp
hiro99ma 1:bb5616cb01fb 4 * @author hiro99ma
hiro99ma 1:bb5616cb01fb 5 * @version 1.00
hiro99ma 1:bb5616cb01fb 6 */
hiro99ma 1:bb5616cb01fb 7
hiro99ma 0:be4ff952ae7a 8 #include "RCS730.h"
hiro99ma 0:be4ff952ae7a 9
hiro99ma 0:be4ff952ae7a 10
hiro99ma 0:be4ff952ae7a 11 namespace {
hiro99ma 0:be4ff952ae7a 12 const int I2C_SLV_ADDR = 0x80; //Default Slave Address(8bit)
hiro99ma 0:be4ff952ae7a 13 const int RETRY_NUM = 5; //max I2C Retry count
hiro99ma 0:be4ff952ae7a 14
hiro99ma 0:be4ff952ae7a 15
hiro99ma 0:be4ff952ae7a 16 inline int set_tag_rf_send_enable(RCS730 *pRcs)
hiro99ma 0:be4ff952ae7a 17 {
hiro99ma 0:be4ff952ae7a 18 uint32_t val = 0x00000001;
hiro99ma 0:be4ff952ae7a 19 return pRcs->pageWrite(RCS730::REG_TAG_TX_CTRL, reinterpret_cast<const uint8_t*>(&val), sizeof(val));
hiro99ma 0:be4ff952ae7a 20 }
hiro99ma 0:be4ff952ae7a 21
hiro99ma 0:be4ff952ae7a 22 int read_rf_buf(RCS730 *pRcs, uint8_t *pData)
hiro99ma 0:be4ff952ae7a 23 {
hiro99ma 0:be4ff952ae7a 24 const int LEN_FIRST = 16;
hiro99ma 0:be4ff952ae7a 25 int len = 0;
hiro99ma 0:be4ff952ae7a 26 int ret;
hiro99ma 0:be4ff952ae7a 27
hiro99ma 0:be4ff952ae7a 28 //read from LEN
hiro99ma 0:be4ff952ae7a 29 ret = pRcs->sequentialRead(RCS730::BUF_RF_COMM, pData, LEN_FIRST);
hiro99ma 0:be4ff952ae7a 30 if (ret == 0) {
hiro99ma 0:be4ff952ae7a 31 len = pData[0];
hiro99ma 0:be4ff952ae7a 32 }
hiro99ma 0:be4ff952ae7a 33 if ((ret == 0) && (pData[0] > LEN_FIRST)) {
hiro99ma 0:be4ff952ae7a 34 ret = pRcs->sequentialRead(RCS730::BUF_RF_COMM + LEN_FIRST, pData + LEN_FIRST, pData[0] - LEN_FIRST);
hiro99ma 0:be4ff952ae7a 35 if (ret != 0) {
hiro99ma 0:be4ff952ae7a 36 len = 0;
hiro99ma 0:be4ff952ae7a 37 }
hiro99ma 0:be4ff952ae7a 38 }
hiro99ma 0:be4ff952ae7a 39
hiro99ma 0:be4ff952ae7a 40 return len;
hiro99ma 0:be4ff952ae7a 41 }
hiro99ma 0:be4ff952ae7a 42 }
hiro99ma 0:be4ff952ae7a 43
hiro99ma 0:be4ff952ae7a 44
hiro99ma 0:be4ff952ae7a 45 RCS730::RCS730(I2C &I2c)
hiro99ma 0:be4ff952ae7a 46 : _i2c(I2c), _slvAddr(I2C_SLV_ADDR)
hiro99ma 0:be4ff952ae7a 47 {
hiro99ma 0:be4ff952ae7a 48 _cbTable.pUserData = 0;
hiro99ma 0:be4ff952ae7a 49 _cbTable.pCbRxHTRDone = 0;
hiro99ma 0:be4ff952ae7a 50 _cbTable.pCbRxHTWDone = 0;
hiro99ma 0:be4ff952ae7a 51 }
hiro99ma 0:be4ff952ae7a 52
hiro99ma 1:bb5616cb01fb 53
hiro99ma 0:be4ff952ae7a 54 RCS730::~RCS730()
hiro99ma 0:be4ff952ae7a 55 {
hiro99ma 0:be4ff952ae7a 56 }
hiro99ma 0:be4ff952ae7a 57
hiro99ma 0:be4ff952ae7a 58
hiro99ma 0:be4ff952ae7a 59 void RCS730::setCallbackTable(const callbacktable_t *pInitTable)
hiro99ma 0:be4ff952ae7a 60 {
hiro99ma 0:be4ff952ae7a 61 _cbTable = *pInitTable;
hiro99ma 0:be4ff952ae7a 62 }
hiro99ma 0:be4ff952ae7a 63
hiro99ma 0:be4ff952ae7a 64
hiro99ma 0:be4ff952ae7a 65 int RCS730::byteWrite(uint16_t MemAddr, uint8_t Data)
hiro99ma 0:be4ff952ae7a 66 {
hiro99ma 0:be4ff952ae7a 67 int ret;
hiro99ma 0:be4ff952ae7a 68 int retry = RETRY_NUM;
hiro99ma 0:be4ff952ae7a 69 char buf[3];
hiro99ma 0:be4ff952ae7a 70
hiro99ma 0:be4ff952ae7a 71 buf[0] = (char)(MemAddr >> 8);
hiro99ma 0:be4ff952ae7a 72 buf[1] = (char)(MemAddr & 0xff);
hiro99ma 0:be4ff952ae7a 73 buf[2] = (char)Data;
hiro99ma 0:be4ff952ae7a 74
hiro99ma 0:be4ff952ae7a 75 do {
hiro99ma 0:be4ff952ae7a 76 ret = _i2c.write(_slvAddr, buf, (int)sizeof(buf));
hiro99ma 0:be4ff952ae7a 77 } while ((ret != 0) && (retry--));
hiro99ma 0:be4ff952ae7a 78
hiro99ma 0:be4ff952ae7a 79 return ret;
hiro99ma 0:be4ff952ae7a 80 }
hiro99ma 0:be4ff952ae7a 81
hiro99ma 0:be4ff952ae7a 82
hiro99ma 0:be4ff952ae7a 83 int RCS730::pageWrite(uint16_t MemAddr, const uint8_t *pData, int Length)
hiro99ma 0:be4ff952ae7a 84 {
hiro99ma 0:be4ff952ae7a 85 int ret;
hiro99ma 0:be4ff952ae7a 86 int retry = RETRY_NUM;
hiro99ma 0:be4ff952ae7a 87 char buf[2];
hiro99ma 0:be4ff952ae7a 88
hiro99ma 0:be4ff952ae7a 89 buf[0] = (char)(MemAddr >> 8);
hiro99ma 0:be4ff952ae7a 90 buf[1] = (char)(MemAddr & 0xff);
hiro99ma 0:be4ff952ae7a 91
hiro99ma 0:be4ff952ae7a 92 do {
hiro99ma 0:be4ff952ae7a 93 ret = _i2c.write(_slvAddr, buf, (int)sizeof(buf), true);
hiro99ma 0:be4ff952ae7a 94 } while ((ret != 0) && (retry--));
hiro99ma 0:be4ff952ae7a 95 if (ret == 0) {
hiro99ma 0:be4ff952ae7a 96 for (int i = 0; i < Length; i++) {
hiro99ma 0:be4ff952ae7a 97 ret = !_i2c.write(pData[i]); //I2C::write(b) return 1 if success.
hiro99ma 0:be4ff952ae7a 98 if (ret != 0) {
hiro99ma 0:be4ff952ae7a 99 break;
hiro99ma 0:be4ff952ae7a 100 }
hiro99ma 0:be4ff952ae7a 101 }
hiro99ma 0:be4ff952ae7a 102 }
hiro99ma 0:be4ff952ae7a 103 _i2c.stop();
hiro99ma 0:be4ff952ae7a 104
hiro99ma 0:be4ff952ae7a 105 return ret;
hiro99ma 0:be4ff952ae7a 106 }
hiro99ma 0:be4ff952ae7a 107
hiro99ma 0:be4ff952ae7a 108
hiro99ma 0:be4ff952ae7a 109 int RCS730::randomRead(uint16_t MemAddr, uint8_t *pData)
hiro99ma 0:be4ff952ae7a 110 {
hiro99ma 0:be4ff952ae7a 111 return sequentialRead(MemAddr, pData, 1);
hiro99ma 0:be4ff952ae7a 112 }
hiro99ma 0:be4ff952ae7a 113
hiro99ma 0:be4ff952ae7a 114
hiro99ma 0:be4ff952ae7a 115 int RCS730::sequentialRead(uint16_t MemAddr, uint8_t *pData, int Length)
hiro99ma 0:be4ff952ae7a 116 {
hiro99ma 0:be4ff952ae7a 117 int ret;
hiro99ma 0:be4ff952ae7a 118 int retry = RETRY_NUM;
hiro99ma 0:be4ff952ae7a 119 char buf[2];
hiro99ma 0:be4ff952ae7a 120
hiro99ma 0:be4ff952ae7a 121 buf[0] = (char)(MemAddr >> 8);
hiro99ma 0:be4ff952ae7a 122 buf[1] = (char)(MemAddr & 0xff);
hiro99ma 0:be4ff952ae7a 123
hiro99ma 0:be4ff952ae7a 124 do {
hiro99ma 0:be4ff952ae7a 125 ret = _i2c.write(_slvAddr, buf, (int)sizeof(buf), true);
hiro99ma 0:be4ff952ae7a 126 } while ((ret != 0) && (retry--));
hiro99ma 0:be4ff952ae7a 127 if (ret == 0) {
hiro99ma 0:be4ff952ae7a 128 ret = _i2c.read(_slvAddr | 1, reinterpret_cast<char*>(pData), Length);
hiro99ma 0:be4ff952ae7a 129 }
hiro99ma 0:be4ff952ae7a 130
hiro99ma 0:be4ff952ae7a 131 return ret;
hiro99ma 0:be4ff952ae7a 132 }
hiro99ma 0:be4ff952ae7a 133
hiro99ma 0:be4ff952ae7a 134
hiro99ma 0:be4ff952ae7a 135 int RCS730::currentAddrRead(uint8_t *pData)
hiro99ma 0:be4ff952ae7a 136 {
hiro99ma 0:be4ff952ae7a 137 int ret;
hiro99ma 0:be4ff952ae7a 138 int retry = RETRY_NUM;
hiro99ma 0:be4ff952ae7a 139
hiro99ma 0:be4ff952ae7a 140 do {
hiro99ma 0:be4ff952ae7a 141 ret = _i2c.read((int)(_slvAddr | 1), reinterpret_cast<char*>(pData), 1);
hiro99ma 0:be4ff952ae7a 142 } while ((ret != 0) && (retry--));
hiro99ma 0:be4ff952ae7a 143
hiro99ma 0:be4ff952ae7a 144 return ret;
hiro99ma 0:be4ff952ae7a 145 }
hiro99ma 0:be4ff952ae7a 146
hiro99ma 0:be4ff952ae7a 147
hiro99ma 0:be4ff952ae7a 148 inline int RCS730::readRegister(uint16_t Reg, uint32_t* pData)
hiro99ma 0:be4ff952ae7a 149 {
hiro99ma 0:be4ff952ae7a 150 return sequentialRead(Reg, reinterpret_cast<uint8_t*>(pData), sizeof(uint32_t));
hiro99ma 0:be4ff952ae7a 151 }
hiro99ma 0:be4ff952ae7a 152
hiro99ma 0:be4ff952ae7a 153
hiro99ma 0:be4ff952ae7a 154 inline int RCS730::writeRegisterForce(uint16_t Reg, uint32_t Data)
hiro99ma 0:be4ff952ae7a 155 {
hiro99ma 0:be4ff952ae7a 156 return pageWrite(Reg, reinterpret_cast<const uint8_t*>(&Data), sizeof(Data));
hiro99ma 0:be4ff952ae7a 157 }
hiro99ma 0:be4ff952ae7a 158
hiro99ma 0:be4ff952ae7a 159
hiro99ma 0:be4ff952ae7a 160 int RCS730::writeRegister(uint16_t Reg, uint32_t Data, uint32_t Mask/*=0xffffffff*/)
hiro99ma 0:be4ff952ae7a 161 {
hiro99ma 0:be4ff952ae7a 162 int ret;
hiro99ma 0:be4ff952ae7a 163 uint32_t cur; //current register value
hiro99ma 0:be4ff952ae7a 164
hiro99ma 0:be4ff952ae7a 165 ret = readRegister(Reg, &cur);
hiro99ma 0:be4ff952ae7a 166 if (ret == 0) {
hiro99ma 0:be4ff952ae7a 167 if ((cur & Mask) != Data) {
hiro99ma 0:be4ff952ae7a 168 // change value
hiro99ma 0:be4ff952ae7a 169 Data |= cur & ~Mask;
hiro99ma 0:be4ff952ae7a 170 ret = writeRegisterForce(Reg, Data);
hiro99ma 0:be4ff952ae7a 171 }
hiro99ma 0:be4ff952ae7a 172 }
hiro99ma 0:be4ff952ae7a 173
hiro99ma 0:be4ff952ae7a 174 return ret;
hiro99ma 0:be4ff952ae7a 175 }
hiro99ma 0:be4ff952ae7a 176
hiro99ma 0:be4ff952ae7a 177
hiro99ma 0:be4ff952ae7a 178 int RCS730::setRegOpMode(OpMode Mode)
hiro99ma 0:be4ff952ae7a 179 {
hiro99ma 0:be4ff952ae7a 180 return writeRegister(REG_OPMODE, static_cast<uint32_t>(Mode));
hiro99ma 0:be4ff952ae7a 181 }
hiro99ma 0:be4ff952ae7a 182
hiro99ma 0:be4ff952ae7a 183
hiro99ma 0:be4ff952ae7a 184 int RCS730::setRegSlaveAddr(int SAddr)
hiro99ma 0:be4ff952ae7a 185 {
hiro99ma 0:be4ff952ae7a 186 int ret;
hiro99ma 0:be4ff952ae7a 187
hiro99ma 0:be4ff952ae7a 188 ret = writeRegister(REG_I2C_SLAVE_ADDR, static_cast<uint32_t>(SAddr));
hiro99ma 0:be4ff952ae7a 189
hiro99ma 0:be4ff952ae7a 190 if (ret == 0) {
hiro99ma 0:be4ff952ae7a 191 _slvAddr = SAddr << 1;
hiro99ma 0:be4ff952ae7a 192 }
hiro99ma 0:be4ff952ae7a 193
hiro99ma 0:be4ff952ae7a 194 return ret;
hiro99ma 0:be4ff952ae7a 195 }
hiro99ma 0:be4ff952ae7a 196
hiro99ma 0:be4ff952ae7a 197
hiro99ma 0:be4ff952ae7a 198 int RCS730::setRegInterruptMask(uint32_t Mask, uint32_t Value)
hiro99ma 0:be4ff952ae7a 199 {
hiro99ma 0:be4ff952ae7a 200 return writeRegister(REG_INT_MASK, Value, Mask);
hiro99ma 0:be4ff952ae7a 201 }
hiro99ma 0:be4ff952ae7a 202
hiro99ma 0:be4ff952ae7a 203
hiro99ma 0:be4ff952ae7a 204 int RCS730::setRegPlugSysCode(PlugSysCode SysCode)
hiro99ma 0:be4ff952ae7a 205 {
hiro99ma 0:be4ff952ae7a 206 return writeRegister(REG_PLUG_CONF1, static_cast<uint32_t>(SysCode), 0x00000002);
hiro99ma 0:be4ff952ae7a 207 }
hiro99ma 0:be4ff952ae7a 208
hiro99ma 0:be4ff952ae7a 209
hiro99ma 0:be4ff952ae7a 210 int RCS730::goToInitializeStatus()
hiro99ma 0:be4ff952ae7a 211 {
hiro99ma 0:be4ff952ae7a 212 return writeRegisterForce(REG_INIT_CTRL, 0x0000004a);
hiro99ma 0:be4ff952ae7a 213 }
hiro99ma 0:be4ff952ae7a 214
hiro99ma 0:be4ff952ae7a 215
hiro99ma 0:be4ff952ae7a 216 int RCS730::initFTMode(OpMode Mode)
hiro99ma 0:be4ff952ae7a 217 {
hiro99ma 0:be4ff952ae7a 218 int ret;
hiro99ma 0:be4ff952ae7a 219
hiro99ma 0:be4ff952ae7a 220 if (OPMODE_PLUG < Mode) {
hiro99ma 0:be4ff952ae7a 221 return -1;
hiro99ma 0:be4ff952ae7a 222 }
hiro99ma 0:be4ff952ae7a 223
hiro99ma 0:be4ff952ae7a 224 ret = setRegOpMode(Mode);
hiro99ma 0:be4ff952ae7a 225 if (ret == 0) {
hiro99ma 0:be4ff952ae7a 226 ret = setRegInterruptMask(MSK_INT_TAG_RW_RX_DONE2, 0);
hiro99ma 0:be4ff952ae7a 227 }
hiro99ma 0:be4ff952ae7a 228
hiro99ma 0:be4ff952ae7a 229 return ret;
hiro99ma 0:be4ff952ae7a 230 }
hiro99ma 0:be4ff952ae7a 231
hiro99ma 0:be4ff952ae7a 232
hiro99ma 0:be4ff952ae7a 233 #if 0
hiro99ma 0:be4ff952ae7a 234 int RCS730::initNfcDepMode()
hiro99ma 0:be4ff952ae7a 235 {
hiro99ma 0:be4ff952ae7a 236 int ret;
hiro99ma 0:be4ff952ae7a 237
hiro99ma 0:be4ff952ae7a 238 ret = setRegOpMode(OPMODE_NFCDEP);
hiro99ma 0:be4ff952ae7a 239 if (ret == 0) {
hiro99ma 1:bb5616cb01fb 240 ret = setRegInterruptMask(MSK_INT_TAG_NFC_DEP_RX_DONE, 0);
hiro99ma 0:be4ff952ae7a 241 }
hiro99ma 0:be4ff952ae7a 242
hiro99ma 0:be4ff952ae7a 243 return ret;
hiro99ma 0:be4ff952ae7a 244 }
hiro99ma 0:be4ff952ae7a 245 #endif
hiro99ma 0:be4ff952ae7a 246
hiro99ma 0:be4ff952ae7a 247
hiro99ma 0:be4ff952ae7a 248 void RCS730::isrIrq()
hiro99ma 0:be4ff952ae7a 249 {
hiro99ma 0:be4ff952ae7a 250 int ret;
hiro99ma 0:be4ff952ae7a 251 bool b_send = false;
hiro99ma 0:be4ff952ae7a 252 uint32_t intstat;
hiro99ma 0:be4ff952ae7a 253 uint8_t rf_buf[256];
hiro99ma 0:be4ff952ae7a 254
hiro99ma 0:be4ff952ae7a 255 ret = readRegister(REG_INT_STATUS, &intstat);
hiro99ma 0:be4ff952ae7a 256 if (ret == 0) {
hiro99ma 0:be4ff952ae7a 257
hiro99ma 0:be4ff952ae7a 258 if (intstat & MSK_INT_TAG_RW_RX_DONE2) {
hiro99ma 0:be4ff952ae7a 259 //Read or Write w/o Enc Rx done for HT block
hiro99ma 0:be4ff952ae7a 260 int len = read_rf_buf(this, rf_buf);
hiro99ma 0:be4ff952ae7a 261 if (len > 0) {
hiro99ma 0:be4ff952ae7a 262 switch (rf_buf[1]) {
hiro99ma 0:be4ff952ae7a 263 case 0x06: //Read w/o Enc
hiro99ma 0:be4ff952ae7a 264 if (_cbTable.pCbRxHTRDone) {
hiro99ma 0:be4ff952ae7a 265 b_send = (*_cbTable.pCbRxHTRDone)(_cbTable.pUserData, rf_buf, len);
hiro99ma 0:be4ff952ae7a 266 }
hiro99ma 0:be4ff952ae7a 267 break;
hiro99ma 0:be4ff952ae7a 268 case 0x08: //Write w/o Enc;
hiro99ma 0:be4ff952ae7a 269 if (_cbTable.pCbRxHTWDone) {
hiro99ma 0:be4ff952ae7a 270 b_send = (*_cbTable.pCbRxHTWDone)(_cbTable.pUserData, rf_buf, len);
hiro99ma 0:be4ff952ae7a 271 }
hiro99ma 0:be4ff952ae7a 272 break;
hiro99ma 0:be4ff952ae7a 273 default:
hiro99ma 0:be4ff952ae7a 274 break;
hiro99ma 0:be4ff952ae7a 275 }
hiro99ma 0:be4ff952ae7a 276 }
hiro99ma 0:be4ff952ae7a 277 }
hiro99ma 0:be4ff952ae7a 278 #if 0
hiro99ma 0:be4ff952ae7a 279 if (_cbTable.pCbRxDepDone && (intstat & MSK_INT_TAG_NFC_DEP_RX_DONE)) {
hiro99ma 0:be4ff952ae7a 280 //DEP command Rx done
hiro99ma 0:be4ff952ae7a 281 int len = read_rf_buf(this, rf_buf);
hiro99ma 0:be4ff952ae7a 282 (*_cbTable.pCbRxDepDone)(_cbTable.pUserData, rf_buf, len);
hiro99ma 0:be4ff952ae7a 283 }
hiro99ma 0:be4ff952ae7a 284 if (_cbTable.pCbTxDone && (intstat & MSK_INT_TAG_TX_DONE)) {
hiro99ma 0:be4ff952ae7a 285 //Tx Done
hiro99ma 0:be4ff952ae7a 286 int len = read_rf_buf(this, rf_buf);
hiro99ma 0:be4ff952ae7a 287 (*_cbTable.pCbTxDone)(_cbTable.pUserData, rf_buf, len);
hiro99ma 0:be4ff952ae7a 288 }
hiro99ma 0:be4ff952ae7a 289
hiro99ma 0:be4ff952ae7a 290 uint32_t intother = intstat & ~(MSK_INT_TAG_TX_DONE | MSK_INT_TAG_NFC_DEP_RX_DONE | MSK_INT_TAG_RW_RX_DONE2);
hiro99ma 0:be4ff952ae7a 291 if (_cbTable.pCbOther && intother) {
hiro99ma 0:be4ff952ae7a 292 (*_cbTable.mCbOther)(_cbTable.pUserData, 0, intother);
hiro99ma 0:be4ff952ae7a 293 }
hiro99ma 0:be4ff952ae7a 294 #endif
hiro99ma 0:be4ff952ae7a 295
hiro99ma 0:be4ff952ae7a 296 //response
hiro99ma 0:be4ff952ae7a 297 if (b_send) {
hiro99ma 0:be4ff952ae7a 298 ret = pageWrite(BUF_RF_COMM, rf_buf, rf_buf[0]);
hiro99ma 0:be4ff952ae7a 299 if (ret == 0) {
hiro99ma 0:be4ff952ae7a 300 set_tag_rf_send_enable(this);
hiro99ma 0:be4ff952ae7a 301 }
hiro99ma 0:be4ff952ae7a 302 }
hiro99ma 0:be4ff952ae7a 303
hiro99ma 0:be4ff952ae7a 304 writeRegisterForce(REG_INT_CLEAR, intstat);
hiro99ma 0:be4ff952ae7a 305 }
hiro99ma 0:be4ff952ae7a 306 }