fm17622.lua 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. --[[
  2. @module fm17622
  3. @summary fm17622的LPCD和读写驱动
  4. @author 杨壮壮
  5. @data 2023/11/20
  6. @usage
  7. fm17622=require "fm17622"
  8. i2c.setup(1, i2c.FAST)
  9. fm17622.setup(1, 0x28,20,32,function()
  10. local Key_A = string.char(0x01,0x02,0x03,0x04,0x05,0x06)
  11. local wdata = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
  12. fm17622.write_datablock(4,wdata,0,Key_A)
  13. local strat,rdata=fm17622.read_datablock(4,0,Key_A)
  14. if strat then
  15. log.info(json.encode(rdata))
  16. end
  17. end,true)
  18. ]]
  19. local fm17622 = {}
  20. local sys = require "sys"
  21. local fm17622_iic_id, fm17622_iic_addr, fm17622_NPD,debug
  22. local endcall
  23. local nfc_Data = {}
  24. local nfc_work=false--防冲突
  25. -- LPCD配置信息
  26. local LPCD_THRSH_H = 0x00
  27. local LPCD_THRSH_L = 0x10
  28. local LPCD_CWP = 10 -- LPCD PMOS输出驱动 0~63
  29. local LPCD_CWN = 10 -- LPCD NMOS输出驱动 0~15
  30. local LPCD_SLEEPTIME = 10 --LPCD 唤醒间隔时间,每一档为32ms,休眠时间:(16+1)*32=544ms
  31. local TX1_TX2_CW_DISABLE = 0
  32. local TX1_CW_ENABLE = 1
  33. local TX2_CW_ENABLE = 2
  34. local TX1_TX2_CW_ENABLE = 3
  35. local JREG_PAGE0 = 0x00 -- Page register in page 0
  36. local JREG_COMMAND = 0x01 -- Contains Command bits, PowerDown bit and bit to switch receiver off.
  37. local JREG_COMMIEN = 0x02 -- Contains Communication interrupt enable bits andbit for Interrupt inversion.
  38. local JREG_DIVIEN = 0x03 -- Contains RfOn, RfOff, CRC and Mode Interrupt enable and bit to switch Interrupt pin to PushPull mode.
  39. local JREG_COMMIRQ = 0x04 -- Contains Communication interrupt request bits.
  40. local JREG_DIVIRQ = 0x05 -- Contains RfOn, RfOff, CRC and Mode Interrupt request.
  41. local JREG_ERROR = 0x06 -- Contains Protocol, Parity, CRC, Collision, Buffer overflow, Temperature and RF error flags.
  42. local JREG_STATUS1 = 0x07 -- Contains status information about Lo- and HiAlert, RF-field on, Timer, Interrupt request and CRC status.
  43. local JREG_STATUS2 = 0x08 -- Contains information about internal states (Modemstate),Mifare states and possibility to switch Temperature sensor off.
  44. local JREG_FIFODATA = 0x09 -- Gives access to FIFO. Writing to register increments theFIFO level (register =0x0A), reading decrements it.
  45. local JREG_FIFOLEVEL = 0x0A -- Contains the actual level of the FIFO.
  46. local JREG_WATERLEVEL = 0x0B -- Contains the Waterlevel value for the FIFO
  47. local JREG_CONTROL = 0x0C -- Contains information about last received bits, Initiator mode bit, bit to copy NFCID to FIFO and to Start and stopthe Timer unit.
  48. local JREG_BITFRAMING = 0x0D -- Contains information of last bits to send, to align received bits in FIFO and activate sending in Transceive]]
  49. local JREG_COLL = 0x0E -- Contains all necessary bits for Collission handling
  50. local JREG_RFU0F = 0x0F -- Currently not used.
  51. local JREG_PAGE1 = 0x10 -- Page register in page 1
  52. local JREG_MODE = 0x11 -- Contains bits for auto wait on Rf, to detect SYNC byte in NFC mode and MSB first for CRC calculation
  53. local JREG_TXMODE = 0x12 -- Contains Transmit Framing, Speed, CRC enable, bit for inverse mode and TXMix bit.
  54. local JREG_RXMODE = 0x13 -- Contains Transmit Framing, Speed, CRC enable, bit for multiple receive and to filter errors.
  55. local JREG_TXCONTROL = 0x14 -- Contains bits to activate and configure Tx1 and Tx2 and bit to activate 100% modulation.
  56. local JREG_TXAUTO = 0x15 -- Contains bits to automatically switch on/off the Rf and to do the collission avoidance and the initial rf-on.
  57. local JREG_TXSEL = 0x16 -- Contains SigoutSel, DriverSel and LoadModSel bits.
  58. local JREG_RXSEL = 0x17 -- Contains UartSel and RxWait bits.
  59. local JREG_RXTRESHOLD = 0x18 -- Contains MinLevel and CollLevel for detection.
  60. local JREG_DEMOD = 0x19 -- Contains bits for time constants, hysteresis and IQ demodulator settings.
  61. local JREG_FELICANFC = 0x1A -- Contains bits for minimum FeliCa length received and for FeliCa syncronisation length.
  62. local JREG_FELICANFC2 = 0x1B -- Contains bits for maximum FeliCa length received.
  63. local JREG_MIFARE = 0x1C -- Contains Miller settings, TxWait settings and MIFARE halted mode bit.
  64. local JREG_MANUALRCV = 0x1D -- Currently not used.
  65. local JREG_RFU1E = 0x1E -- Currently not used.
  66. local JREG_SERIALSPEED = 0x1F -- Contains speed settings for serila interface.
  67. local JREG_PAGE2 = 0x20 -- Page register in page 2
  68. local JREG_CRCRESULT1 = 0x21 -- Contains MSByte of CRC Result.
  69. local JREG_CRCRESULT2 = 0x22 -- Contains LSByte of CRC Result.
  70. local JREG_GSNLOADMOD = 0x23 -- Contains the conductance and the modulation settings for the N-MOS transistor only for load modulation (See difference to JREG_GSN!).
  71. local JREG_MODWIDTH = 0x24 -- Contains modulation width setting.
  72. local JREG_TXBITPHASE = 0x25 -- Contains TxBitphase settings and receive clock change.
  73. local JREG_RFCFG = 0x26 -- Contains sensitivity of Rf Level detector, the receiver gain factor and the RfLevelAmp.
  74. local JREG_GSN = 0x27 -- Contains the conductance and the modulation settings for the N-MOS transistor during active modulation (no load modulation setting!).
  75. local JREG_CWGSP = 0x28 -- Contains the conductance for the P-Mos transistor.
  76. local JREG_MODGSP = 0x29 -- Contains the modulation index for the PMos transistor.
  77. local JREG_TMODE = 0x2A -- Contains all settings for the timer and the highest 4 bits of the prescaler.
  78. local JREG_TPRESCALER = 0x2B -- Contais the lowest byte of the prescaler.
  79. local JREG_TRELOADHI = 0x2C -- Contains the high byte of the reload value.
  80. local JREG_TRELOADLO = 0x2D -- Contains the low byte of the reload value.
  81. local JREG_TCOUNTERVALHI = 0x2E -- Contains the high byte of the counter value.
  82. local JREG_TCOUNTERVALLO = 0x2F -- Contains the low byte of the counter value.
  83. local JREG_PAGE3 = 0x30 -- Page register in page 3
  84. local JREG_TESTSEL1 = 0x31 -- Test register
  85. local JREG_TESTSEL2 = 0x32 -- Test register
  86. local JREG_TESTPINEN = 0x33 -- Test register
  87. local JREG_TESTPINVALUE = 0x34 -- Test register
  88. local JREG_TESTBUS = 0x35 -- Test register
  89. local JREG_AUTOTEST = 0x36 -- Test register
  90. local JREG_VERSION = 0x37 -- Contains the product number and the version .
  91. local JREG_ANALOGTEST = 0x38 -- Test register
  92. local JREG_TESTSUP1 = 0x39 -- Test register
  93. local JREG_TESTSUP2 = 0x3A -- Test register
  94. local JREG_TESTADC = 0x3B -- Test register
  95. local JREG_ANALOGUETEST1 = 0x3C -- Test register
  96. local JREG_ANALOGUETEST0 = 0x3D -- Test register
  97. local JREG_ANALOGUETPD_A = 0x3E -- Test register
  98. local JREG_ANALOGUETPD_B = 0x3F -- Test register
  99. local CMD_MASK = 0xF0
  100. local CMD_IDLE = 0x00
  101. local CMD_CONFIGURE = 0x01
  102. local CMD_GEN_RAND_ID = 0x02
  103. local CMD_CALC_CRC = 0x03
  104. local CMD_TRANSMIT = 0x04
  105. local CMD_NOCMDCHANGE = 0x07
  106. local CMD_RECEIVE = 0x08
  107. local CMD_TRANSCEIVE = 0x0C
  108. local CMD_AUTOCOLL = 0x0D
  109. local CMD_AUTHENT = 0x0E
  110. local CMD_SOFT_RESET = 0x0F
  111. -- ============================================================================
  112. local JREG_EXT_REG_ENTRANCE = 0x0F -- ext register entrance
  113. -- ============================================================================
  114. local JBIT_EXT_REG_WR_ADDR = 0x40 -- wrire address cycle
  115. local JBIT_EXT_REG_RD_ADDR = 0x80 -- read address cycle
  116. local JBIT_EXT_REG_WR_DATA = 0xC0 -- write data cycle
  117. local JBIT_EXT_REG_RD_DATA = 0x00 -- read data cycle
  118. -- ============================================================================
  119. local JREG_LPCDTEST = 0x21
  120. -- ============================================================================
  121. -- ============================================================================
  122. local JREG_LPCDGMSEL = 0x24
  123. -- ============================================================================
  124. -- LPCD_ENB_AGC_AVDD = 0x00
  125. -- LPCD_ENB_AGC_DVDD = 0x10
  126. -- LPCD_RXCHANGE_JUDGE_MODE_0 = 0x00
  127. -- LPCD_RXCHANGE_JUDGE_MODE_1 = 0x04
  128. -- LPCD_GM_SEL_0 = 0x00
  129. -- LPCD_GM_SEL_1 = 0x01
  130. -- LPCD_GM_SEL_2 = 0x02
  131. -- LPCD_GM_SEL_3 = 0x03
  132. -- ============================================================================
  133. local JREG_LPCDSARADC1 = 0x25
  134. -- ============================================================================
  135. -- SARADC_SOC_CFG_4 = 0x00
  136. -- SARADC_SOC_CFG_8 = 0x01
  137. -- SARADC_SOC_CFG_16 = 0x02
  138. -- SARADC_SOC_CFG_24 = 0x03
  139. -- ============================================================================
  140. local JREG_LPCDDELTA_HI = 0x26
  141. -- ============================================================================
  142. -- ============================================================================
  143. local JREG_LPCDDELTA_LO = 0x27
  144. -- ============================================================================
  145. -- ============================================================================
  146. local JREG_LPCDICURR_HI = 0x28
  147. -- ============================================================================
  148. -- ============================================================================
  149. local JREG_LPCDICURR_LO = 0x29
  150. -- ============================================================================
  151. -- ============================================================================
  152. local JREG_LPCDQCURR_HI = 0x2A
  153. -- ============================================================================
  154. -- ============================================================================
  155. local JREG_LPCDQCURR_LO = 0x2B
  156. -- ============================================================================
  157. -- ============================================================================
  158. local JREG_LPCDILAST_HI = 0x2C
  159. -- ============================================================================
  160. -- ============================================================================
  161. local JREG_LPCDILAST_LO = 0x2D
  162. -- ============================================================================
  163. -- ============================================================================
  164. local JREG_LPCDQLAST_HI = 0x2E
  165. -- ============================================================================
  166. -- ============================================================================
  167. local JREG_LPCDQLAST_LO = 0x2F
  168. -- ============================================================================
  169. -- ============================================================================
  170. local JREG_LPCDAUX = 0x30
  171. -- ============================================================================
  172. -- IBN2U = 0x00
  173. -- TEST_BG = 0x01
  174. -- TEST_SAMPLE_I = 0x02
  175. -- TEST_AMP_OUT_IN = 0x03
  176. -- TEST_AMP_OUT_IP = 0x04
  177. -- TEST_AMP_OUT_QN = 0x05
  178. -- TEST_AMP_OUT_QP = 0x06
  179. -- OSC_64K = 0x07
  180. -- VBN2 = 0x08
  181. -- VBN1 = 0x09
  182. -- TEST_BG_VREF = 0x0A
  183. -- AVSS = 0x0B
  184. -- TEST_SAMPLE_Q = 0x0C
  185. -- DVDD = 0x0D
  186. -- TEST_CRYSTAL_VDD = 0x0E
  187. -- AVDD = 0x0F
  188. -- FLOAT_IN = 0x10
  189. -- ============================================================================
  190. local JREG_LPCDMISSWUP = 0x31
  191. -- ============================================================================
  192. -- ============================================================================
  193. local JREG_LPCDFLAGINV = 0x32
  194. -- ============================================================================
  195. -- LPCD_FLAG_INV_DISABLE = 0x00
  196. -- LPCD_FLAG_INV_ENABLE = 0x20
  197. -- ============================================================================
  198. local JREG_LPCDSLEEPTIMER = 0x33
  199. -- ============================================================================
  200. -- ============================================================================
  201. local JREG_LPCDTHRESH_H = 0x34
  202. -- ============================================================================
  203. -- ============================================================================
  204. local JREG_LPCDTHRESH_L = 0x35
  205. -- ============================================================================
  206. -- ============================================================================
  207. local JREG_LPCDREQATIMER = 0x37
  208. -- ============================================================================
  209. -- LPCD_IRQMISSWUP_ENABLE = 0x20
  210. -- LPCD_IRQMISSWUP_DISABLE = 0x00
  211. -- LPCD_REQA_TIME_80us = 0x00
  212. -- LPCD_REQA_TIME_100us = 0x01
  213. -- LPCD_REQA_TIME_120us = 0x02
  214. -- LPCD_REQA_TIME_150us = 0x03
  215. -- LPCD_REQA_TIME_200us = 0x04
  216. -- LPCD_REQA_TIME_250us = 0x05
  217. -- LPCD_REQA_TIME_300us = 0x06
  218. -- LPCD_REQA_TIME_400us = 0x07
  219. -- LPCD_REQA_TIME_500us = 0x08
  220. -- LPCD_REQA_TIME_600us = 0x09
  221. -- LPCD_REQA_TIME_800us = 0x0A
  222. -- LPCD_REQA_TIME_1ms = 0x0B
  223. -- LPCD_REQA_TIME_1ms2 = 0x0C
  224. -- LPCD_REQA_TIME_1ms6 = 0x0D
  225. -- LPCD_REQA_TIME_2ms = 0x0E
  226. -- LPCD_REQA_TIME_2ms5 = 0x0F
  227. -- LPCD_REQA_TIME_3ms = 0x10
  228. -- LPCD_REQA_TIME_3ms5 = 0x11
  229. -- LPCD_REQA_TIME_4ms = 0x12
  230. -- LPCD_REQA_TIME_5ms = 0x13
  231. -- LPCD_REQA_TIME_7ms = 0x14
  232. -- ============================================================================
  233. local JREG_LPCDREQAANA = 0x38
  234. -- ============================================================================
  235. -- LPCD_RXGAIN_23DB = 0x00
  236. -- LPCD_RXGAIN_33DB = 0x10 -- default
  237. -- LPCD_RXGAIN_38DB = 0x20
  238. -- LPCD_RXGAIN_43DB = 0x30
  239. -- LPCD_MINLEVEL_3 = 0x00
  240. -- LPCD_MINLEVEL_6 = 0x04
  241. -- LPCD_MINLEVEL_9 = 0x08
  242. -- LPCD_MINLEVEL_C = 0x0C
  243. -- LPCD_MODWIDTH_32 = 0x00
  244. -- LPCD_MODWIDTH_38 = 0x02
  245. -- ============================================================================
  246. local JREG_LPCDDETECTMODE = 0x39
  247. -- ============================================================================
  248. -- LPCD_TXSCALE_0 = 0x00 -- 1/8 of the TX power configured by CWGSP/CWGSNON
  249. -- LPCD_TXSCALE_1 = 0x08 -- 1/4 of the TX power configured by CWGSP/CWGSNON
  250. -- LPCD_TXSCALE_2 = 0x10 -- 1/2 of the TX power configured by CWGSP/CWGSNON
  251. -- LPCD_TXSCALE_3 = 0x18 -- 3/4 of the TX power configured by CWGSP/CWGSNON
  252. -- LPCD_TXSCALE_4 = 0x20 -- equal to of the TX power configured by CWGSP/CWGSNON
  253. -- LPCD_TXSCALE_5 = 0x28 -- twice of the TX power configured by CWGSP/CWGSNON
  254. -- LPCD_TXSCALE_6 = 0x30 -- 3 times of the TX power configured by CWGSP/CWGSNON
  255. -- LPCD_TXSCALE_7 = 0x31 -- 4 times of the TX power configured by CWGSP/CWGSNON
  256. -- LPCD_RX_CHANGE_MODE = 0x00
  257. -- LPCD_REQA_MODE = 0x01
  258. -- LPCD_COMBINE_MODE = 0x02
  259. -- ============================================================================
  260. local JREG_LPCDCTRLMODE = 0x3A
  261. -- ============================================================================
  262. -- RF_DET_ENABLE = 0x40
  263. -- RF_DET_DISABLE = 0x00
  264. -- RF_DET_SEN_135mV = 0x00
  265. -- RF_DET_SEN_180mV = 0x08
  266. -- RF_DET_SEN_240mV = 0x10
  267. -- RF_DET_SEN_300mV = 0x18
  268. -- LPCD_DISABLE = 0x00
  269. -- LPCD_ENABLE = 0x02
  270. -- ============================================================================
  271. local JREG_LPCDIRQ = 0x3B
  272. -- ============================================================================
  273. -- ============================================================================
  274. local JREG_LPCDRFTIMER = 0x3C
  275. -- ============================================================================
  276. -- LPCD_IRQINV_ENABLE = 0x20
  277. -- LPCD_IRQINV_DISABLE = 0x00
  278. -- LPCD_IRQ_PUSHPULL = 0x10
  279. -- LPCD_IRQ_OD = 0x00
  280. -- LPCD_RFTIME_5us = 0x00
  281. -- LPCD_RFTIME_10us = 0x01
  282. -- LPCD_RFTIME_15us = 0x02
  283. -- LPCD_RFTIME_20us = 0x03
  284. -- LPCD_RFTIME_25us = 0x04 -- default
  285. -- LPCD_RFTIME_30us = 0x05
  286. -- LPCD_RFTIME_35us = 0x06
  287. -- LPCD_RFTIME_40us = 0x07
  288. -- LPCD_RFTIME_50us = 0x08
  289. -- LPCD_RFTIME_60us = 0x09
  290. -- LPCD_RFTIME_70us = 0x0A
  291. -- LPCD_RFTIME_80us = 0x0B
  292. -- LPCD_RFTIME_100us = 0x0C
  293. -- LPCD_RFTIME_120us = 0x0D
  294. -- LPCD_RFTIME_150us = 0x0E
  295. -- LPCD_RFTIME_200us = 0x0F
  296. -- ============================================================================
  297. local JREG_LPCDTXCTRL1 = 0x3D
  298. -- ============================================================================
  299. -- LPCD_TX2_ENABLE = 0x20
  300. -- LPCD_TX2_DISABLE = 0x00
  301. -- LPCD_TX1_ENABLE = 0x10
  302. -- LPCD_TX1_DISABLE = 0x00
  303. -- LPCD_TX2ON_INV_ENABLE = 0x08
  304. -- LPCD_TX2ON_INV_DISABLE = 0x00
  305. -- LPCD_TX1ON_INV_ENABLE = 0x04
  306. -- LPCD_TX1ON_INV_DISABLE = 0x00
  307. -- LPCD_TX2OFF_INV_ENABLE = 0x02
  308. -- LPCD_TX2OFF_INV_DISABLE = 0x00
  309. -- LPCD_TX1OFF_INV_ENABLE = 0x01
  310. -- LPCD_TX1OFF_INV_DISABLE = 0x00
  311. -- ============================================================================
  312. local JREG_LPCDTXCTRL2 = 0x3E
  313. -- ============================================================================
  314. -- ============================================================================
  315. local JREG_LPCDTXCTRL3 = 0x3F
  316. -- ============================================================================
  317. -- LPCD_FLAG_TOUT_ENABLE = 0x20 -- TOUT Pad, set while LPCD DETECT, clear while LPCD SLEEP/SETUP(LPCD_TXEN_FLAG_INV = 0);
  318. -- LPCD_FLAG_TOUT_DISABLE = 0x00 -- disable TOUT Pad as the LPCD DETECT flag.
  319. -- LPCD_FLAG_D3_ENABLE = 0x10 -- D3 Pad, set while LPCD DETECT, clear while LPCD SLEEP/SETUP(LPCD_TXEN_FLAG_INV = 0);
  320. -- LPCD_FLAG_D3_DISABLE = 0x00 -- disable D3 Pad as the LPCD DETECT flag.
  321. local fm17622_SUCCESS = 0x00
  322. local fm17622_COMM_ERR = 0xf4
  323. --[[
  324. 写fm17622寄存器
  325. SetReg(address, value)
  326. @number address 地址
  327. @number value 值
  328. @usage
  329. write_rawrc(fm17622_bit_framing,0x07)
  330. ]]
  331. local function SetReg(address, value)
  332. -- log.info("寄存器写:0x",string.format("%X",address),"值为0x",string.format("%X",value))
  333. i2c.writeReg(fm17622_iic_id, fm17622_iic_addr, address, string.char(value))
  334. end
  335. --[[
  336. 读fm17622寄存器
  337. GetReg(address,len)
  338. @number address 地址
  339. @number len 读取长度
  340. @return number 寄存器值
  341. @usage
  342. local n = read_rawrc(fm17622_com_irq,1)
  343. ]]
  344. local function GetReg(address, len)
  345. if len == nil then
  346. len = 1
  347. end
  348. local val = i2c.readReg(fm17622_iic_id, fm17622_iic_addr, address, len)
  349. -- log.info("iic",val,type(val),#val)
  350. if #val == 0 then
  351. return nil;
  352. end
  353. -- log.info("寄存器读:0x",string.format("%X",address),"值为0x",string.format("%X",string.byte(val)))
  354. return string.byte(val)
  355. end
  356. local function SetReg_Ext(address, value)
  357. -- log.info("扩展寄存器写:0x",string.format("%X",address),"值为0x",string.format("%X",string.byte(value)))
  358. SetReg(JREG_EXT_REG_ENTRANCE, (JBIT_EXT_REG_WR_ADDR + address) % 256)
  359. SetReg(JREG_EXT_REG_ENTRANCE, (JBIT_EXT_REG_WR_DATA + value) % 256)
  360. return;
  361. end
  362. -- //***********************************************
  363. -- //函数名称:GetReg_Ext(unsigned char ExtRegAddr)
  364. -- //函数功能:读取扩展寄存器值
  365. -- //入口参数:ExtRegAddr:扩展寄存器地址
  366. -- //出口参数:unsigned char TRUE:读取成功 FALSE:失败
  367. -- //***********************************************
  368. local function GetReg_Ext(address)
  369. -- log.info("扩展寄存器读:0x",string.format("%X",address))
  370. SetReg(JREG_EXT_REG_ENTRANCE, (JBIT_EXT_REG_RD_ADDR + address))
  371. return GetReg(JREG_EXT_REG_ENTRANCE)
  372. end
  373. BIT0 = 0x01
  374. BIT1 = 0x02
  375. BIT2 = 0x04
  376. BIT3 = 0x08
  377. BIT4 = 0x10
  378. BIT5 = 0x20
  379. BIT6 = 0x40
  380. BIT7 = 0x80
  381. --[[
  382. 对rc522寄存器置位
  383. @api ModifyReg(address, mask)
  384. @number address 地址
  385. @number mask 置位值
  386. @number set 选择置位还是清位
  387. @usage
  388. ModifyReg (rc522_fifo_level, 0x80)
  389. ]]
  390. local function ModifyReg(addr, mask, set)
  391. local regdata = GetReg(addr)
  392. if set == 1 then
  393. regdata = regdata | mask
  394. else
  395. regdata = regdata & (~mask);
  396. end
  397. SetReg(addr, regdata);
  398. end
  399. --[[
  400. 对fm17622寄存器置位
  401. set_bit_mask(address, mask)
  402. @number address 地址
  403. @number mask 置位值
  404. @usage
  405. ]]
  406. local function set_bit_mask(address, mask)
  407. ModifyReg(address, mask, 1)
  408. end
  409. --[[
  410. 对fm17622寄存器清位
  411. clear_bit_mask(address, mask)
  412. @number address 地址
  413. @number mask 清位值
  414. @usage
  415. clear_bit_mask(rc522_com_irq, 0x80 )
  416. ]]
  417. local function clear_bit_mask(address, mask)
  418. ModifyReg(address, mask, 0)
  419. end
  420. -- //*************************************
  421. -- //函数 名:fm17622_Initial_ReaderA
  422. -- 设置A卡模式
  423. -- //入口参数:
  424. -- //出口参数:
  425. -- //*************************************
  426. local function fm17622_Initial_ReaderA()
  427. SetReg(JREG_MODWIDTH, 0x26) -- MODWIDTH = 106kbps
  428. ModifyReg(JREG_TXAUTO, BIT6, 1) -- Force 100ASK = 1
  429. SetReg(JREG_GSN, (0x0f << 4)) -- Config GSN Config ModGSN
  430. SetReg(JREG_CWGSP, 63) -- Config GSP 0-63
  431. SetReg(JREG_CONTROL, BIT4) -- Initiator = 1
  432. SetReg(JREG_RFCFG, 5 << 4) -- Config RxGain
  433. SetReg(JREG_RXTRESHOLD, (8 << 4) | 4) -- Config MinLevel Config CollLevel
  434. end
  435. local function ReaderA_Request()
  436. SetReg(JREG_TXMODE, 0) -- Disable TxCRC
  437. SetReg(JREG_RXMODE, 0) -- Disable RxCRC
  438. SetReg(JREG_COMMAND, 0) -- command = Idel
  439. SetReg(JREG_FIFOLEVEL, 128) -- Clear FIFO
  440. SetReg(JREG_FIFODATA, 0x26)
  441. SetReg(JREG_COMMAND, 12) -- command = Transceive
  442. SetReg(JREG_BITFRAMING, 0x87) -- Start Send
  443. sys.wait(5)
  444. local reg_data = GetReg(JREG_FIFOLEVEL)
  445. if reg_data == 2 then
  446. --
  447. nfc_Data["ATQA"] = GetReg(JREG_FIFODATA) * 256 + GetReg(JREG_FIFODATA)
  448. return fm17622_SUCCESS
  449. end
  450. return fm17622_COMM_ERR
  451. end
  452. local function ReaderA_AntiColl()
  453. SetReg(JREG_TXMODE, 0) -- Disable TxCRC
  454. SetReg(JREG_RXMODE, 0) -- Disable RxCRC
  455. SetReg(JREG_COMMAND, 0) -- command = Idel
  456. SetReg(JREG_FIFOLEVEL, 128) -- Clear FIFO
  457. SetReg(JREG_FIFODATA, 0x93)
  458. SetReg(JREG_FIFODATA, 0x20)
  459. SetReg(JREG_COMMAND, 12) -- command = Transceive
  460. SetReg(JREG_BITFRAMING, 0x80) -- Start Send
  461. sys.wait(5)
  462. local reg_data = GetReg(JREG_FIFOLEVEL)
  463. if reg_data == 5 then
  464. local uid1 = GetReg(JREG_FIFODATA)
  465. nfc_Data["UID"] = {}
  466. nfc_Data["UID"][1] = uid1
  467. local uid2 = GetReg(JREG_FIFODATA)
  468. nfc_Data["UID"][2] = uid2
  469. local uid3 = GetReg(JREG_FIFODATA)
  470. nfc_Data["UID"][3] = uid3
  471. local uid4 = GetReg(JREG_FIFODATA)
  472. nfc_Data["UID"][4] = uid4
  473. nfc_Data["BCC"] = GetReg(JREG_FIFODATA)
  474. if (uid1 ~ uid2 ~ uid3 ~ uid4) == nfc_Data["BCC"] then
  475. return fm17622_SUCCESS
  476. end
  477. return fm17622_COMM_ERR
  478. end
  479. return fm17622_COMM_ERR
  480. end
  481. local function ReaderA_Select()
  482. SetReg(JREG_TXMODE, 0x80) -- Enable TxCRC
  483. SetReg(JREG_RXMODE, 0x80) -- Enable RxCRC
  484. SetReg(JREG_COMMAND, 0) -- command = Idel
  485. SetReg(JREG_FIFOLEVEL, 128) -- Clear FIFO
  486. SetReg(JREG_FIFODATA, 0x93)
  487. SetReg(JREG_FIFODATA, 0x70)
  488. SetReg(JREG_FIFODATA, nfc_Data["UID"][1])
  489. SetReg(JREG_FIFODATA, nfc_Data["UID"][2])
  490. SetReg(JREG_FIFODATA, nfc_Data["UID"][3])
  491. SetReg(JREG_FIFODATA, nfc_Data["UID"][4])
  492. SetReg(JREG_FIFODATA, nfc_Data["BCC"])
  493. SetReg(JREG_COMMAND, 12) -- command = Transceive
  494. SetReg(JREG_BITFRAMING, 0x80) -- Start Send
  495. sys.wait(5)
  496. local reg_data = GetReg(JREG_FIFOLEVEL)
  497. if reg_data == 1 then
  498. nfc_Data["SAK"] = GetReg(JREG_FIFODATA)
  499. return fm17622_SUCCESS
  500. end
  501. return fm17622_COMM_ERR
  502. end
  503. -- *************************************
  504. -- 函数 名:ReaderA_CardActivate
  505. -- 开始寻卡,找卡选卡
  506. -- 入口参数:
  507. -- 出口参数:fm17622_SUCCESS, fm17622_COMM_ERR
  508. -- *************************************
  509. local function ReaderA_CardActivate()
  510. local result = ReaderA_Request()
  511. if result ~= fm17622_SUCCESS then
  512. return fm17622_COMM_ERR
  513. end
  514. result = ReaderA_AntiColl()
  515. if result ~= fm17622_SUCCESS then
  516. return fm17622_COMM_ERR
  517. end
  518. result = ReaderA_Select()
  519. if result ~= fm17622_SUCCESS then
  520. return fm17622_COMM_ERR
  521. end
  522. return result
  523. end
  524. local function SetCW(cw_mode)
  525. local reg
  526. SetReg(JREG_TXCONTROL, 0x80);
  527. if cw_mode == TX1_TX2_CW_DISABLE then
  528. ModifyReg(JREG_TXCONTROL, 1 | 2, 0);
  529. reg = GetReg(JREG_TXCONTROL)
  530. end
  531. if cw_mode == TX1_CW_ENABLE then
  532. ModifyReg(JREG_TXCONTROL, 1, 1);
  533. ModifyReg(JREG_TXCONTROL, 2, 0);
  534. end
  535. if cw_mode == TX2_CW_ENABLE then
  536. ModifyReg(JREG_TXCONTROL, 1, 0);
  537. ModifyReg(JREG_TXCONTROL, 2, 1);
  538. end
  539. if cw_mode == TX1_TX2_CW_ENABLE then
  540. ModifyReg(JREG_TXCONTROL, 1 | 2, 1);
  541. end
  542. sys.wait(5)
  543. end
  544. --[[
  545. fm17622 发送命令通讯
  546. @number command 发送(0x0e)还是放松并接受(0x0c)
  547. @number data 发送是数据
  548. ]]
  549. local function command(cmd, data)
  550. local out_data = {}
  551. local irqEn
  552. local waitFor
  553. -- SetReg(JREG_TXMODE, 0) -- Disable TxCRC
  554. -- SetReg(JREG_RXMODE, 0) -- Disable RxCRC
  555. SetReg(JREG_COMMAND, 0) -- command = Idel
  556. SetReg(JREG_FIFOLEVEL, 128) -- Clear FIFO
  557. for i = 1, #data do
  558. SetReg(JREG_FIFODATA, data[i])
  559. end
  560. SetReg(JREG_COMMAND, cmd) -- command = Transceive
  561. SetReg(JREG_BITFRAMING, 0x80) -- Start Send
  562. sys.wait(25)
  563. clear_bit_mask(JREG_BITFRAMING, 0x80)
  564. local reg_data = GetReg(JREG_FIFOLEVEL)
  565. if reg_data == 0 then
  566. return nil
  567. end
  568. for i = 1, reg_data do
  569. out_data[i] = GetReg(JREG_FIFODATA)
  570. end
  571. return out_data
  572. end
  573. --[[
  574. fm17622 LPCD初始化
  575. ]]
  576. function fm17622.LPCD_setup()
  577. SetReg(JREG_COMMAND, CMD_SOFT_RESET)
  578. sys.wait(5)
  579. if GetReg(JREG_COMMAND) ~= 0x20 then
  580. log.debug("没有复位成功", GetReg(JREG_COMMAND))
  581. return false;
  582. end
  583. SetReg_Ext(JREG_LPCDGMSEL, 0x10 | 0x04 | 0x00)
  584. SetReg_Ext(JREG_LPCDSARADC1, 0x38 | 0x02);
  585. SetReg_Ext(JREG_LPCDCTRLMODE, 0 | 0 | 2)
  586. SetReg_Ext(JREG_LPCDDETECTMODE, 0x20 | 0)
  587. SetReg_Ext(JREG_LPCDSLEEPTIMER, LPCD_SLEEPTIME)
  588. SetReg_Ext(JREG_LPCDRFTIMER, 0x20 | 0x10 | 0x01)
  589. SetReg_Ext(JREG_LPCDTHRESH_L, LPCD_THRSH_L)
  590. SetReg_Ext(JREG_LPCDTXCTRL2, LPCD_CWP)
  591. SetReg_Ext(JREG_LPCDTXCTRL3, LPCD_CWN)
  592. log.debug("复位成功")
  593. return true
  594. end
  595. -- //***********************************************
  596. -- //函数名称:Lpcd_Get_ADC_Value()
  597. -- //函数功能:Lpcd_Get_ADC_Value读取LPCD的ADC数据
  598. -- 用于调试的lpcd参数
  599. -- //入口参数:
  600. -- //出口参数:
  601. -- //***********************************************
  602. local function Lpcd_Get_ADC_Value()
  603. local reg = GetReg_Ext(JREG_LPCDICURR_HI);
  604. local reg1 = GetReg_Ext(JREG_LPCDICURR_LO);
  605. local Current1 = (reg << 6) + reg1;
  606. Current2 = reg % 4 -- ((reg<<6)>>8);
  607. log.debug("-> LPCD I Current is:", Current2, Current1, "\r\n");
  608. reg = GetReg_Ext(JREG_LPCDQCURR_HI);
  609. reg1 = GetReg_Ext(JREG_LPCDQCURR_LO);
  610. Current1 = (reg << 6) + reg1;
  611. Current2 = reg % 4 -- ((reg<<6)>>8);
  612. log.debug("-> LPCD Q Current is:", Current2, Current1, "\r\n");
  613. reg = GetReg_Ext(JREG_LPCDILAST_HI);
  614. reg1 = GetReg_Ext(JREG_LPCDILAST_LO);
  615. Current1 = (reg << 6) + reg1;
  616. Current2 = reg % 4 -- ((reg<<6)>>8);
  617. log.debug("-> LPCD I Last is:", Current2, Current1, "\r\n");
  618. reg = GetReg_Ext(JREG_LPCDQLAST_HI);
  619. reg1 = GetReg_Ext(JREG_LPCDQLAST_LO);
  620. Current1 = (reg << 6) + reg1;
  621. Current2 = reg % 4 -- ((reg<<6)>>8);
  622. log.debug("-> LPCD Q Last is:", Current2, Current1, "\r\n");
  623. reg = GetReg_Ext(JREG_LPCDDELTA_HI);
  624. reg1 = GetReg_Ext(JREG_LPCDDELTA_LO);
  625. Current1 = (reg << 6) + reg1;
  626. Current2 = reg % 4 -- ((reg<<6)>>8);
  627. log.debug("-> LPCD Delta is:", Current2, Current1, "\r\n");
  628. end
  629. -- //***********************************************
  630. -- //函数名称:Lpcd_Card_Event()
  631. -- //函数功能:LPCD检测到卡
  632. -- //入口参数:
  633. -- //出口参数:
  634. -- //***********************************************
  635. local function Lpcd_Card_Event()
  636. fm17622_Initial_ReaderA()
  637. SetCW(TX1_TX2_CW_ENABLE)
  638. nfc_Data = {}--清空选卡
  639. result = ReaderA_CardActivate()
  640. if result == fm17622_SUCCESS then
  641. if endcall ~= nil then
  642. endcall()
  643. end
  644. end
  645. SetCW(TX1_TX2_CW_DISABLE)
  646. nfc_Data = {}--清空结束
  647. end
  648. -- //***********************************************
  649. -- //函数名称:Lpcd_IRQ_Event()
  650. -- //函数功能:LPCD中断处理
  651. -- //入口参数:
  652. -- //出口参数:
  653. -- //***********************************************
  654. function fm17622.Lpcd_IRQ_Event()
  655. log.debug("fm17622.Lpcd_IRQ_Event");
  656. gpio.set(fm17622_NPD, 1)
  657. sys.wait(5)
  658. local reg = GetReg_Ext(JREG_LPCDIRQ) -- 读取LPCD中断标志,LPCD中断需要判断LPCD IRQ RXCHANGE(0x04)是否置位
  659. SetReg_Ext(JREG_LPCDIRQ, reg) -- CLEAR LPCD IRQ
  660. if (reg & 0x08) ~= 0 then
  661. log.debug("fm17622.Lpcd_IRQ_Event", "-> LPCD IRQ RFDET SET!\r\n");
  662. end
  663. if (reg & 0x04) ~= 0 then
  664. log.debug("fm17622.Lpcd_IRQ_Event", "-> LPCD IRQ RXCHANGE SET!\r\n"); -- LPCD中断标志
  665. if debug~=nil then
  666. if debug==true then
  667. Lpcd_Get_ADC_Value()
  668. end
  669. end
  670. Lpcd_Card_Event()
  671. end
  672. if (reg & 0x02) ~= 0 then
  673. log.debug("fm17622.Lpcd_IRQ_Event", "-> LPCD IRQ ATQAREC SET!\r\n");
  674. end
  675. if (reg & 0x10) ~= 0 then
  676. log.debug("fm17622.Lpcd_IRQ_Event", "-> LPCD IRQ MISSWUP SET!\r\n");
  677. end
  678. gpio.set(fm17622_NPD, 0)
  679. sys.wait(5)
  680. gpio.set(fm17622_NPD, 1)
  681. sys.wait(5)
  682. for i = 1, 10, 1 do
  683. if fm17622.LPCD_setup() == true then
  684. break
  685. end
  686. gpio.set(fm17622_NPD, 0)
  687. sys.wait(5)
  688. gpio.set(fm17622_NPD, 1)
  689. sys.wait(5)
  690. end
  691. sys.wait(5)
  692. gpio.set(fm17622_NPD, 0)
  693. end
  694. --[[
  695. fm17622初始化
  696. @api fm17622.setup(iic_id, iic_addr, NPD,IRQ,ENDCall)
  697. @number iic_id iic端口号
  698. @number iic_addr iic地址
  699. @number NPD rst引脚
  700. @number IRQ INT引脚
  701. @number Debug 是否调试
  702. @number ENDCall 终端回调执行不用可未空
  703. @return boolean 初始化结果
  704. @usage
  705. fm17622=require "fm17622"
  706. i2c.setup(1, i2c.FAST)
  707. fm17622.setup(1, 0x28,20,32,function()
  708. local Key_A = string.char(0x01,0x02,0x03,0x04,0x05,0x06)
  709. local wdata = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
  710. fm17622.write_datablock(4,wdata,0,Key_A)
  711. local strat,rdata=fm17622.read_datablock(4,0,Key_A)
  712. if strat then
  713. log.info(json.encode(rdata))
  714. end
  715. end,true)
  716. ]]
  717. function fm17622.setup(iic_id, iic_addr, NPD,IRQ,ENDCall,Debug)
  718. fm17622_iic_id = iic_id
  719. fm17622_iic_addr = iic_addr
  720. fm17622_NPD = NPD
  721. endcall=ENDCall
  722. debug=Debug;
  723. gpio.setup(fm17622_NPD, 0, gpio.PULLUP)
  724. sys.wait(5)
  725. gpio.set(fm17622_NPD, 1)
  726. sys.wait(5)
  727. gpio.debounce(32, 200)
  728. gpio.setup(32, function(var)
  729. if nfc_work==false then
  730. nfc_work=true;
  731. sys.taskInit(function()
  732. fm17622.Lpcd_IRQ_Event()
  733. nfc_work=false
  734. end)
  735. end
  736. end, gpio.PULLDOWN, gpio.FALLING)
  737. if GetReg(JREG_VERSION) == 0xA2 then
  738. log.debug("IC Version = fm17622 or FM17610 \r\n")
  739. fm17622.LPCD_setup()
  740. sys.wait(5)
  741. gpio.set(fm17622_NPD, 0)
  742. return true
  743. end
  744. return false;
  745. end
  746. --[[
  747. 写入ic卡数据
  748. @api write_datablock(addr, data, mode, key)
  749. @number addr ic卡地址块0到64
  750. @number data 写入数据
  751. @number A/B密码选择 A为0,B为1
  752. @number key 密码字符串
  753. @return boolean true为成功,false失败
  754. @usage
  755. local Key_A = string.char(0x01,0x02,0x03,0x04,0x05,0x06)
  756. local wdata = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
  757. fm17622.write_datablock(4,wdata,0,Key_A)
  758. ]]
  759. function fm17622.write_datablock(addr, data, mode, key)
  760. if #data ~= 16 then
  761. log.error("data must be 16 bytes")
  762. return false
  763. end
  764. if nfc_Data["UID"] == nil then
  765. log.error("UID 不能为空")
  766. return false
  767. end
  768. if #key ~= 6 then
  769. log.error("key must be 6 bytes")
  770. return false
  771. end
  772. -- 验证密码
  773. local buff = {}
  774. buff[1] = mode + 0x60
  775. buff[2] = addr
  776. for i = 1, 6 do
  777. buff[i + 2] = key:byte(i)
  778. end
  779. for i = 1, 4 do
  780. buff[i + 8] = nfc_Data["UID"][i]
  781. end
  782. command(0x0E, buff)
  783. if (GetReg(JREG_STATUS2) & 0x08) == 0 then
  784. log.info("密码错误", (GetReg(JREG_STATUS2) & 0x08))
  785. return false
  786. end
  787. buff = {}
  788. buff[1] = 0xa0
  789. buff[2] = addr
  790. command(0x0C, buff)
  791. command(0x0C, data)
  792. return true
  793. end
  794. --[[
  795. 读取ic卡数据
  796. @api fm17622.read_datablock(addr, mode, key)
  797. @number addr ic卡地址块0到64
  798. @number A/B密码选择 A为0,B为1
  799. @number key 密码字符串
  800. @return boolean true为成功,false失败
  801. @return table 读取到的值
  802. @usage
  803. local Key_A = string.char(0x01,0x02,0x03,0x04,0x05,0x06)
  804. local strat,rdata=fm17622.read_datablock(4,0,Key_A)
  805. if strat then
  806. log.info(json.encode(rdata))
  807. end
  808. ]]
  809. function fm17622.read_datablock(addr, mode, key)
  810. if nfc_Data["UID"] == nil then
  811. log.error("UID 不能为空")
  812. return false
  813. end
  814. if #key ~= 6 then
  815. log.error("key must be 6 bytes")
  816. return false
  817. end
  818. -- 验证密码
  819. local buff = {}
  820. buff[1] = mode + 0x60
  821. buff[2] = addr
  822. for i = 1, 6 do
  823. buff[i + 2] = key:byte(i)
  824. end
  825. for i = 1, 4 do
  826. buff[i + 8] = nfc_Data["UID"][i]
  827. end
  828. command(0x0E, buff)
  829. if (GetReg(JREG_STATUS2) & 0x08) == 0 then
  830. log.info("密码错误", (GetReg(JREG_STATUS2) & 0x08))
  831. return false
  832. end
  833. -- 读取数据
  834. local buff = {}
  835. buff[1] = 0x30
  836. buff[2] = addr
  837. return true, command(0x0C, buff)
  838. end
  839. return fm17622