DS1302Drv.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * author: xxJian
  3. * date: 2018-9-3
  4. * ported from https://github.com/msparks/arduino-ds1302
  5. *
  6. */
  7. #include "DS1302Drv.h"
  8. uint8_t time_buf_reg[8] = {0x20,0x10,0x09,0x14,0x23,0x59,0x50,0x02};//3?ê?ê±??
  9. uint32_t b_date=12, b_month=6, b_year=2021, b_hour=23, b_minute=59, b_second=50, b_day=2;
  10. // Returns the decoded decimal value from a binary-coded decimal (BCD) byte.
  11. // Assumes 'bcd' is coded with 4-bits per digit, with the tens place digit in
  12. // the upper 4 MSBs.
  13. uint8_t bcdToDec(const uint8_t bcd)
  14. {
  15. return (10 * ((bcd & 0xF0) >> 4) + (bcd & 0x0F));
  16. }
  17. // Returns the binary-coded decimal of 'dec'. Inverse of bcdToDec.
  18. uint8_t decToBcd(const uint8_t dec)
  19. {
  20. uint8_t tens, ones;
  21. tens = dec / 10;
  22. ones = dec % 10;
  23. return (tens << 4) | ones;
  24. }
  25. // Returns the hour in 24-hour format from the hour register value.
  26. uint8_t hourFromRegisterValue(const uint8_t value) {
  27. uint8_t adj;
  28. if (value & 128) // 12-hour mode
  29. adj = 12 * ((value & 32) >> 5);
  30. else // 24-hour mode
  31. adj = 10 * ((value & (32 + 16)) >> 4);
  32. return (value & 15) + adj;
  33. }
  34. void DS1302_writeOut(const uint8_t value, uint8_t readAfter)
  35. {
  36. GPIO_InitTypeDef GPIO_InitStruct;
  37. uint32_t i,j;
  38. //pinMode(io_pin_, OUTPUT);
  39. GPIO_InitStruct.Pin = DS1302_IO_Pin;
  40. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  41. GPIO_InitStruct.Pull = GPIO_NOPULL;
  42. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  43. HAL_GPIO_Init(DS1302_IO_GPIO_Port, &GPIO_InitStruct);
  44. for (i = 0; i < 8; i++)
  45. {
  46. //digitalWrite(io_pin_, (value >> i) & 1);
  47. if( (value >> i) & 1 ) {
  48. HAL_GPIO_WritePin(DS1302_IO_GPIO_Port, DS1302_IO_Pin, GPIO_PIN_SET);
  49. } else {
  50. HAL_GPIO_WritePin(DS1302_IO_GPIO_Port, DS1302_IO_Pin, GPIO_PIN_RESET);
  51. }
  52. //HAL_Delay(1);
  53. HAL_GPIO_WritePin(DS1302_SCLK_GPIO_Port, DS1302_SCLK_Pin, GPIO_PIN_RESET); //digitalWrite(sclk_pin_, HIGH);
  54. //HAL_Delay(2);
  55. j=2000;
  56. while(j>1)
  57. {
  58. j--;
  59. __nop();
  60. }
  61. if (readAfter && i == 7) {
  62. // We're about to read data -- ensure the pin is back in input mode
  63. // before the clock is lowered.
  64. //pinMode(io_pin_, INPUT);
  65. HAL_GPIO_WritePin(DS1302_SCLK_GPIO_Port, DS1302_SCLK_Pin, GPIO_PIN_SET);
  66. //HAL_Delay(1);
  67. j=2000;
  68. while(j>1)
  69. {
  70. j--;
  71. __nop();
  72. }
  73. HAL_GPIO_WritePin(DS1302_IO_GPIO_Port, DS1302_IO_Pin, GPIO_PIN_RESET);
  74. GPIO_InitStruct.Pin = DS1302_IO_Pin;
  75. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  76. GPIO_InitStruct.Pull = GPIO_NOPULL;
  77. HAL_GPIO_Init(DS1302_IO_GPIO_Port, &GPIO_InitStruct);
  78. //HAL_Delay(1);
  79. } else {
  80. HAL_GPIO_WritePin(DS1302_SCLK_GPIO_Port, DS1302_SCLK_Pin, GPIO_PIN_SET); //digitalWrite(sclk_pin_, LOW);
  81. //HAL_Delay(2); //delayMicroseconds(1);
  82. j=2000;
  83. while(j>1)
  84. {
  85. j--;
  86. __nop();
  87. }
  88. }
  89. }
  90. }
  91. uint8_t DS1302_readIn(void)
  92. {
  93. uint8_t input_value;
  94. uint8_t bit;
  95. uint32_t i,j;
  96. input_value = 0;
  97. bit = 0;
  98. GPIO_InitTypeDef GPIO_InitStruct;
  99. HAL_GPIO_WritePin(DS1302_IO_GPIO_Port, DS1302_IO_Pin, GPIO_PIN_RESET);
  100. //pinMode(io_pin_, INPUT);
  101. GPIO_InitStruct.Pin = DS1302_IO_Pin;
  102. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  103. GPIO_InitStruct.Pull = GPIO_NOPULL;
  104. HAL_GPIO_Init(DS1302_IO_GPIO_Port, &GPIO_InitStruct);
  105. // Bits from the DS1302 are output on the falling edge of the clock
  106. // cycle. This is called after readIn (which will leave the clock low) or
  107. // writeOut(..., true) (which will leave it high).
  108. for (i = 0; i < 8; i++) {
  109. HAL_GPIO_WritePin(DS1302_SCLK_GPIO_Port, DS1302_SCLK_Pin, GPIO_PIN_SET); //digitalWrite(sclk_pin_, HIGH);
  110. //HAL_Delay(2); //delayMicroseconds(1);
  111. while(j>1)
  112. {
  113. j--;
  114. __nop();
  115. }
  116. HAL_GPIO_WritePin(DS1302_SCLK_GPIO_Port, DS1302_SCLK_Pin, GPIO_PIN_RESET); //digitalWrite(sclk_pin_, LOW);
  117. //HAL_Delay(1); //delayMicroseconds(1);
  118. j=2000;
  119. while(j>1)
  120. {
  121. j--;
  122. __nop();
  123. }
  124. bit = HAL_GPIO_ReadPin(DS1302_IO_GPIO_Port, DS1302_IO_Pin); //bit = digitalRead(io_pin_);
  125. //HAL_Delay(1);
  126. j=2000;
  127. while(j>1)
  128. {
  129. j--;
  130. __nop();
  131. }
  132. input_value |= (bit << i); // Bits are read LSB first.
  133. }
  134. return input_value;
  135. }
  136. uint8_t DS1302_readRegister(const uint8_t reg)
  137. {
  138. //const SPISession s(ce_pin_, io_pin_, sclk_pin_);
  139. uint8_t cmd_byte;
  140. uint8_t result;
  141. uint32_t j;
  142. HAL_GPIO_WritePin(DS1302_CE_GPIO_Port, DS1302_CE_Pin, GPIO_PIN_SET);
  143. cmd_byte = (0x81 | (reg << 1));
  144. DS1302_writeOut(cmd_byte, DEF_true);
  145. result = DS1302_readIn();
  146. //HAL_Delay(1);
  147. j=2000;
  148. while(j>1)
  149. {
  150. j--;
  151. __nop();
  152. }
  153. HAL_GPIO_WritePin(DS1302_CE_GPIO_Port, DS1302_CE_Pin, GPIO_PIN_RESET);
  154. return result;
  155. }
  156. void DS1302_writeRegister(const uint8_t reg, const uint8_t value)
  157. {
  158. //const SPISession s(ce_pin_, io_pin_, sclk_pin_);
  159. uint8_t cmd_byte;
  160. HAL_GPIO_WritePin(DS1302_CE_GPIO_Port, DS1302_CE_Pin, GPIO_PIN_SET);
  161. cmd_byte = (0x80 | (reg << 1));
  162. DS1302_writeOut(cmd_byte, DEF_false);
  163. DS1302_writeOut(value, DEF_false);
  164. HAL_GPIO_WritePin(DS1302_SCLK_GPIO_Port, DS1302_SCLK_Pin, GPIO_PIN_RESET);
  165. HAL_GPIO_WritePin(DS1302_CE_GPIO_Port, DS1302_CE_Pin, GPIO_PIN_RESET);
  166. }
  167. void DS1302_writeProtect(const uint8_t enable)
  168. {
  169. HAL_GPIO_WritePin(DS1302_CE_GPIO_Port, DS1302_CE_Pin, GPIO_PIN_SET);
  170. DS1302_writeRegister(kWriteProtectReg, (enable << 7));
  171. HAL_GPIO_WritePin(DS1302_CE_GPIO_Port, DS1302_CE_Pin, GPIO_PIN_RESET);
  172. }
  173. void DS1302_halt(const uint8_t enable)
  174. {
  175. uint8_t sec;
  176. HAL_GPIO_WritePin(DS1302_CE_GPIO_Port, DS1302_CE_Pin, GPIO_PIN_SET);
  177. sec = DS1302_readRegister(kSecondReg);
  178. sec &= ~(1 << 7);
  179. sec |= (enable << 7);
  180. DS1302_writeRegister(kSecondReg, sec);
  181. HAL_GPIO_WritePin(DS1302_CE_GPIO_Port, DS1302_CE_Pin, GPIO_PIN_RESET);
  182. }
  183. void DS1302_writeRam(const uint8_t address, const uint8_t value)
  184. {
  185. if (address >= kRamSize) {
  186. return;
  187. }
  188. DS1302_writeRegister(kRamAddress0 + address, value);
  189. }
  190. uint8_t DS1302_readRam(const uint8_t address)
  191. {
  192. if (address >= kRamSize) {
  193. return 0;
  194. }
  195. return DS1302_readRegister(kRamAddress0 + address);
  196. }
  197. void DS1302_writeRamBulk(const uint8_t* const data, int len)
  198. {
  199. if (len <= 0) {
  200. return;
  201. }
  202. if (len > kRamSize) {
  203. len = kRamSize;
  204. }
  205. //const SPISession s(ce_pin_, io_pin_, sclk_pin_);
  206. DS1302_writeOut(kRamBurstWrite, DEF_false);
  207. for (int i = 0; i < len; ++i) {
  208. DS1302_writeOut(data[i], DEF_false);
  209. }
  210. }
  211. void DS1302_readRamBulk(uint8_t* const data, int len)
  212. {
  213. if (len <= 0) {
  214. return;
  215. }
  216. if (len > kRamSize) {
  217. len = kRamSize;
  218. }
  219. //const SPISession s(ce_pin_, io_pin_, sclk_pin_);
  220. DS1302_writeOut(kRamBurstRead, DEF_true);
  221. for (int i = 0; i < len; ++i) {
  222. data[i] = DS1302_readIn();
  223. }
  224. }
  225. void DS1302_timeRead()
  226. {
  227. //const SPISession s(ce_pin_, io_pin_, sclk_pin_);
  228. HAL_GPIO_WritePin(DS1302_CE_GPIO_Port, DS1302_CE_Pin, GPIO_PIN_SET);
  229. //Time t(2099, 1, 1, 0, 0, 0, Time::kSunday);
  230. //DS1302_writeOut(kClockBurstRead, DEF_true);
  231. time_buf_reg[7] = DS1302_readRegister(kSecondReg)& 0x7F; //sec
  232. time_buf_reg[6] = DS1302_readRegister(kMinuteReg); //min
  233. time_buf_reg[5] = DS1302_readRegister(kHourReg); //hr
  234. time_buf_reg[4] = DS1302_readRegister(kDateReg); //date
  235. time_buf_reg[3] = DS1302_readRegister(kMonthReg); //mon
  236. time_buf_reg[2] = DS1302_readRegister(kDayReg); //day
  237. time_buf_reg[1] = DS1302_readRegister(kYearReg); //yr
  238. /*
  239. time_buf_reg[7] = DS1302_readIn() & 0x7F; //sec
  240. time_buf_reg[6] = DS1302_readIn(); //min
  241. time_buf_reg[5] = DS1302_readIn(); //hr
  242. time_buf_reg[4] = DS1302_readIn(); //date
  243. time_buf_reg[3] = DS1302_readIn(); //mon
  244. time_buf_reg[2] = DS1302_readIn(); //day
  245. time_buf_reg[1] = DS1302_readIn(); //yr
  246. */
  247. b_year = 2000 + bcdToDec(time_buf_reg[1]);
  248. b_day = bcdToDec(time_buf_reg[2]);
  249. b_month = bcdToDec(time_buf_reg[3]);
  250. b_date = bcdToDec(time_buf_reg[4]);
  251. b_hour = hourFromRegisterValue(time_buf_reg[5]);
  252. b_minute = bcdToDec(time_buf_reg[6]);
  253. b_second = bcdToDec(time_buf_reg[7]);
  254. HAL_GPIO_WritePin(DS1302_SCLK_GPIO_Port, DS1302_SCLK_Pin, GPIO_PIN_RESET);
  255. HAL_GPIO_WritePin(DS1302_CE_GPIO_Port, DS1302_CE_Pin, GPIO_PIN_RESET);
  256. }
  257. void DS1302_timeWrite(void)
  258. {
  259. // We want to maintain the Clock Halt flag if it is set.
  260. uint8_t ch_value;
  261. HAL_GPIO_WritePin(DS1302_CE_GPIO_Port, DS1302_CE_Pin, GPIO_PIN_SET);
  262. ch_value = DS1302_readRegister(kSecondReg) & 0x80;
  263. //const SPISession s(ce_pin_, io_pin_, sclk_pin_);
  264. /*
  265. 2019年5月11日更新:有一段时间DS1302模块没有安装纽扣电池,重新使用本次代码发现有点问题。
  266. 经调试发现,DS1302_timeWrite() 函数内,有一行代码是专门判断Clock Halt(时间停止)标志位是否设置的。
  267. 估计是DS1302初安装纽扣电池上电时,这个时钟是停止计算的。
  268. 因此,上纽扣电池的第一次执行,需要使用这行:
  269. DS1302_writeRegister(kSecondReg, decToBcd(b_second));
  270. 随后,才可以使用这行:
  271. DS1302_writeRegister(kSecondReg,ch_value | decToBcd(b_second));
  272. */
  273. //DS1302_writeRegister(kSecondReg, decToBcd(b_second));
  274. DS1302_writeRegister(kSecondReg,ch_value | decToBcd(b_second));
  275. DS1302_writeRegister(kMinuteReg,decToBcd(b_minute));
  276. DS1302_writeRegister(kHourReg,decToBcd(b_hour));
  277. DS1302_writeRegister(kDateReg,decToBcd(b_date));
  278. DS1302_writeRegister(kMonthReg,decToBcd(b_month));
  279. DS1302_writeRegister(kDayReg ,decToBcd(b_day));
  280. DS1302_writeRegister(kYearReg,decToBcd(b_year - 2000));
  281. /*
  282. DS1302_writeOut(kClockBurstWrite, DEF_false);
  283. DS1302_writeOut(ch_value | decToBcd(b_second), DEF_false);
  284. DS1302_writeOut(decToBcd(b_minute), DEF_false);
  285. DS1302_writeOut(decToBcd(b_hour), DEF_false);
  286. DS1302_writeOut(decToBcd(b_date), DEF_false);
  287. DS1302_writeOut(decToBcd(b_month), DEF_false);
  288. DS1302_writeOut(decToBcd(b_day), DEF_false);
  289. DS1302_writeOut(decToBcd(b_year - 2000), DEF_false);
  290. // All clock registers *and* the WP register have to be written for the time
  291. // to be set.
  292. DS1302_writeOut(0, DEF_false); // Write protection register.
  293. DS1302_writeOut(0, DEF_false); // Trickle Charge register.
  294. */
  295. HAL_GPIO_WritePin(DS1302_SCLK_GPIO_Port, DS1302_SCLK_Pin, GPIO_PIN_RESET);
  296. HAL_GPIO_WritePin(DS1302_CE_GPIO_Port, DS1302_CE_Pin, GPIO_PIN_RESET);
  297. }