FeliCa Link(RC-S730) Library

FeliCa Link(RC-S730) Library

  • Not all API is tested.

RC-S730

Sample

Committer:
hiro99ma
Date:
Sun Mar 29 06:11:57 2015 +0000
Revision:
0:be4ff952ae7a
Child:
1:bb5616cb01fb
first publish

Who changed what in which revision?

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