ds3231.lua 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. --------------------------------------------------------------------------------
  2. -- DS3231 I2C module for NODEMCU
  3. -- NODEMCU TEAM
  4. -- LICENCE: http://opensource.org/licenses/MIT
  5. -- Tobie Booth <tbooth@hindbra.in>
  6. --------------------------------------------------------------------------------
  7. local moduleName = ...
  8. local M = {}
  9. _G[moduleName] = M
  10. -- Constants:
  11. M.EVERYSECOND = 6
  12. M.EVERYMINUTE = 7
  13. M.SECOND = 1
  14. M.MINUTE = 2
  15. M.HOUR = 3
  16. M.DAY = 4
  17. M.DATE = 5
  18. M.DISABLE = 0
  19. -- Default value for i2c communication
  20. local id = 0
  21. --device address
  22. local dev_addr = 0x68
  23. local function decToBcd(val)
  24. if val == nil then return 0 end
  25. return((((val/10) - ((val/10)%1)) *16) + (val%10))
  26. end
  27. local function bcdToDec(val)
  28. return((((val/16) - ((val/16)%1)) *10) + (val%16))
  29. end
  30. local function addAlarmBit(val,day)
  31. if day == 1 then return bit.bor(val,64) end
  32. return bit.bor(val,128)
  33. end
  34. -- initialize i2c
  35. --parameters:
  36. --d: sda
  37. --l: scl
  38. function M.init(d, l)
  39. if (d ~= nil) and (l ~= nil) and (d >= 0) and (d <= 11) and (l >= 0) and ( l <= 11) and (d ~= l) then
  40. sda = d
  41. scl = l
  42. else
  43. print("[ERROR] i2c config failed!") return nil
  44. end
  45. print("[LOG] DS3231 init done")
  46. i2c.setup(id, sda, scl, i2c.SLOW)
  47. end
  48. --get time from DS3231
  49. function M.getTime()
  50. i2c.start(id)
  51. i2c.address(id, dev_addr, i2c.TRANSMITTER)
  52. i2c.write(id, 0x00)
  53. i2c.stop(id)
  54. i2c.start(id)
  55. i2c.address(id, dev_addr, i2c.RECEIVER)
  56. local c=i2c.read(id, 7)
  57. i2c.stop(id)
  58. return bcdToDec(tonumber(string.byte(c, 1))),
  59. bcdToDec(tonumber(string.byte(c, 2))),
  60. bcdToDec(tonumber(string.byte(c, 3))),
  61. bcdToDec(tonumber(string.byte(c, 4))),
  62. bcdToDec(tonumber(string.byte(c, 5))),
  63. bcdToDec(tonumber(string.byte(c, 6))),
  64. bcdToDec(tonumber(string.byte(c, 7)))
  65. end
  66. --set time for DS3231
  67. -- enosc setted to 1 disables oscilation on battery, stopping time
  68. function M.setTime(second, minute, hour, day, date, month, year, disOsc)
  69. i2c.start(id)
  70. i2c.address(id, dev_addr, i2c.TRANSMITTER)
  71. i2c.write(id, 0x00)
  72. i2c.write(id, decToBcd(second))
  73. i2c.write(id, decToBcd(minute))
  74. i2c.write(id, decToBcd(hour))
  75. i2c.write(id, decToBcd(day))
  76. i2c.write(id, decToBcd(date))
  77. i2c.write(id, decToBcd(month))
  78. i2c.write(id, decToBcd(year))
  79. i2c.stop(id)
  80. i2c.start(id)
  81. i2c.address(id, dev_addr, i2c.TRANSMITTER)
  82. i2c.write(id, 0x0E)
  83. i2c.stop(id)
  84. i2c.start(id)
  85. i2c.address(id, dev_addr, i2c.RECEIVER)
  86. local c = string.byte(i2c.read(id, 1), 1)
  87. i2c.stop(id)
  88. if disOsc == 1 then c = bit.bor(c,128)
  89. else c = bit.band(c,127) end
  90. i2c.start(id)
  91. i2c.address(id, dev_addr, i2c.TRANSMITTER)
  92. i2c.write(id, 0x0E)
  93. i2c.write(id, c)
  94. i2c.stop(id)
  95. end
  96. -- Reset alarmId flag to let alarm to be triggered again
  97. function M.reloadAlarms ()
  98. if bit == nil or bit.band == nil or bit.bor == nil then
  99. print("[ERROR] Module bit is required to use alarm function")
  100. return nil
  101. end
  102. i2c.start(id)
  103. i2c.address(id, dev_addr, i2c.TRANSMITTER)
  104. i2c.write(id, 0x0F)
  105. i2c.stop(id)
  106. i2c.start(id)
  107. i2c.address(id, dev_addr, i2c.RECEIVER)
  108. local d = string.byte(i2c.read(id, 1), 1)
  109. i2c.stop(id)
  110. -- Both flag needs to be 0 to let alarms trigger
  111. d = bit.band(d,252)
  112. i2c.start(id)
  113. i2c.address(id, dev_addr, i2c.TRANSMITTER)
  114. i2c.write(id, 0x0F)
  115. i2c.write(id, d)
  116. i2c.stop(id)
  117. print('[LOG] Alarm '..almId..' reloaded')
  118. end
  119. -- Enable alarmId bit. Let it to be triggered
  120. function M.enableAlarm (almId)
  121. if bit == nil or bit.band == nil or bit.bor == nil then
  122. print("[ERROR] Module bit is required to use alarm function")
  123. return nil
  124. end
  125. if almId ~= 1 and almId ~= 2 then print('[ERROR] Wrong alarm id (1 or 2): '..almId) return end
  126. i2c.start(id)
  127. i2c.address(id, dev_addr, i2c.TRANSMITTER)
  128. i2c.write(id, 0x0E)
  129. i2c.stop(id)
  130. i2c.start(id)
  131. i2c.address(id, dev_addr, i2c.RECEIVER)
  132. local c = string.byte(i2c.read(id, 1), 1)
  133. i2c.stop(id)
  134. c = bit.bor(c,4)
  135. if almId == 1 then c = bit.bor(c,1)
  136. else c = bit.bor(c,2) end
  137. i2c.start(id)
  138. i2c.address(id, dev_addr, i2c.TRANSMITTER)
  139. i2c.write(id, 0x0E)
  140. i2c.write(id, c)
  141. i2c.stop(id)
  142. M.reloadAlarms()
  143. print('[LOG] Alarm '..almId..' enabled')
  144. end
  145. -- If almID equals 1 or 2 disable that alarm, otherwise disables both.
  146. function M.disableAlarm (almId)
  147. if bit == nil or bit.band == nil or bit.bor == nil then
  148. print("[ERROR] Module bit is required to use alarm function")
  149. return nil
  150. end
  151. i2c.start(id)
  152. i2c.address(id, dev_addr, i2c.TRANSMITTER)
  153. i2c.write(id, 0x0E)
  154. i2c.stop(id)
  155. i2c.start(id)
  156. i2c.address(id, dev_addr, i2c.RECEIVER)
  157. local c = string.byte(i2c.read(id, 1), 1)
  158. i2c.stop(id)
  159. if almId == 1 then c = bit.band(c, 254)
  160. elseif almId == 2 then c = bit.band(c, 253)
  161. else
  162. almId = '1 and 2'
  163. c = bit.band(c, 252)
  164. end
  165. i2c.start(id)
  166. i2c.address(id, dev_addr, i2c.TRANSMITTER)
  167. i2c.write(id, 0x0E)
  168. i2c.write(id, c)
  169. i2c.stop(id)
  170. print('[LOG] Alarm '..almId..' disabled')
  171. end
  172. -- almId can be 1 or 2;
  173. -- almType should be taken from constants
  174. function M.setAlarm (almId, almType, second, minute, hour, date)
  175. if bit == nil or bit.band == nil or bit.bor == nil then
  176. print("[ERROR] Module bit is required to use alarm function")
  177. return nil
  178. end
  179. if almId ~= 1 and almId ~= 2 then print('[ERROR] Wrong alarm id (1 or 2): '..almId) return end
  180. M.enableAlarm(almId)
  181. second = decToBcd(second)
  182. minute = decToBcd(minute)
  183. hour = decToBcd(hour)
  184. date = decToBcd(date)
  185. if almType == M.EVERYSECOND or almType == M.EVERYMINUTE then
  186. second = addAlarmBit(second)
  187. minute = addAlarmBit(minute)
  188. hour = addAlarmBit(hour)
  189. date = addAlarmBit(date)
  190. elseif almType == M.SECOND then
  191. minute = addAlarmBit(minute)
  192. hour = addAlarmBit(hour)
  193. date = addAlarmBit(date)
  194. elseif almType == M.MINUTE then
  195. hour = addAlarmBit(hour)
  196. date = addAlarmBit(date)
  197. elseif almType == M.HOUR then
  198. date = addAlarmBit(date)
  199. elseif almType == M.DAY then
  200. date = addAlarmBit(date,1)
  201. end
  202. local almStart = 0x07
  203. if almId == 2 then almStart = 0x0B end
  204. i2c.start(id)
  205. i2c.address(id, dev_addr, i2c.TRANSMITTER)
  206. i2c.write(id, almStart)
  207. if almId == 1 then i2c.write(id, second) end
  208. i2c.write(id, minute)
  209. i2c.write(id, hour)
  210. i2c.write(id, date)
  211. i2c.stop(id)
  212. print('[LOG] Alarm '..almId..' setted')
  213. end
  214. -- Get Control and Status bytes
  215. function M.getBytes ()
  216. i2c.start(id)
  217. i2c.address(id, dev_addr, i2c.TRANSMITTER)
  218. i2c.write(id, 0x0E)
  219. i2c.stop(id)
  220. i2c.start(id)
  221. i2c.address(id, dev_addr, i2c.RECEIVER)
  222. local c = i2c.read(id, 2)
  223. i2c.stop(id)
  224. return tonumber(string.byte(c, 1)), tonumber(string.byte(c, 2))
  225. end
  226. -- Resetting RTC Stop Flag
  227. function M.resetStopFlag ()
  228. if bit == nil or bit.band == nil or bit.bor == nil then
  229. print("[ERROR] Module bit is required to reset stop flag")
  230. return nil
  231. end
  232. i2c.start(id)
  233. i2c.address(id, dev_addr, i2c.TRANSMITTER)
  234. i2c.write(id, 0x0F)
  235. i2c.stop(id)
  236. i2c.start(id)
  237. i2c.address(id, dev_addr, i2c.RECEIVER)
  238. local s = string.byte(i2c.read(id, 1))
  239. i2c.stop(id)
  240. s = bit.band(s,127)
  241. i2c.start(id)
  242. i2c.address(id, dev_addr, i2c.TRANSMITTER)
  243. i2c.write(id, 0x0F)
  244. i2c.write(id, s)
  245. i2c.stop(id)
  246. print('[LOG] RTC stop flag resetted')
  247. end
  248. return M