You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
8.3 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "driver.hpp"
  2. using namespace iflytop;
  3. #define TIMER2_FREQ 1000
  4. #define TIMER1_FREQ 1000
  5. #define ENABL_TRACE false
  6. #define TAG "DRIVER"
  7. /*******************************************************************************
  8. * *
  9. *******************************************************************************/
  10. PWMSpeedCtrlModule::config_t fan0cfg = {
  11. .name = "fan0",
  12. .pwm_cfg =
  13. {
  14. .name = "fan0pwmtimer",
  15. .htim = &htim2,
  16. .freq = TIMER2_FREQ,
  17. .polarity = false,
  18. },
  19. .nfan = 2,
  20. .fan0Channel = {3, 0, 0, 0},
  21. .fanFBGpioCfg =
  22. {
  23. {.pin = PC10},
  24. {.pin = PC11},
  25. },
  26. .fanPowerGpioCfg =
  27. {
  28. {
  29. .pin = PC4,
  30. .mode = ZGPIO::kMode_nopull,
  31. .mirror = false,
  32. },
  33. },
  34. .enablefbcheck = false,
  35. .enableTrace = ENABL_TRACE,
  36. };
  37. PWMSpeedCtrlModule::config_t fan1cfg = {
  38. .name = "fan1",
  39. .pwm_cfg =
  40. {
  41. .name = "fan1pwmtimer",
  42. .htim = &htim2,
  43. .freq = TIMER2_FREQ,
  44. .polarity = false,
  45. },
  46. .nfan = 2,
  47. .fan0Channel = {4, 0, 0, 0},
  48. .fanFBGpioCfg =
  49. {
  50. {.pin = PC12},
  51. {.pin = PC13},
  52. },
  53. .fanPowerGpioCfg =
  54. {
  55. {
  56. .pin = PC5,
  57. .mode = ZGPIO::kMode_nopull,
  58. .mirror = false,
  59. },
  60. },
  61. .enablefbcheck = false,
  62. .enableTrace = ENABL_TRACE,
  63. };
  64. /*******************************************************************************
  65. * FanCtrlModule *
  66. *******************************************************************************/
  67. void FanCtrlModule::initialize(fan_index_t index) {
  68. m_fanindex = index;
  69. if (m_fanindex == kfan0) {
  70. fan0.initialize(&fan0cfg);
  71. } else if (m_fanindex == kfan1) {
  72. fan1.initialize(&fan1cfg);
  73. } else if (m_fanindex == kfan0and1) {
  74. fan0.initialize(&fan0cfg);
  75. fan1.initialize(&fan1cfg);
  76. }
  77. }
  78. void FanCtrlModule::setFanSpeed(int32_t duty) {
  79. if (m_fanindex == kfan0) {
  80. fan0.startModule(duty);
  81. } else if (m_fanindex == kfan1) {
  82. fan1.startModule(duty);
  83. } else if (m_fanindex == kfan0and1) {
  84. fan0.startModule(duty);
  85. fan1.startModule(duty);
  86. }
  87. }
  88. void FanCtrlModule::stop() {
  89. if (m_fanindex == kfan0) {
  90. fan0.stopModule();
  91. } else if (m_fanindex == kfan1) {
  92. fan1.stopModule();
  93. } else if (m_fanindex == kfan0and1) {
  94. fan0.stopModule();
  95. fan1.stopModule();
  96. }
  97. }
  98. bool FanCtrlModule::isWorking() {
  99. if (m_fanindex == kfan0) {
  100. return fan0.isWorking();
  101. } else if (m_fanindex == kfan1) {
  102. return fan1.isWorking();
  103. } else if (m_fanindex == kfan0and1) {
  104. return fan0.isWorking() || fan1.isWorking();
  105. }
  106. return false;
  107. }
  108. void FanCtrlModule::clearError() {
  109. if (m_fanindex == kfan0) {
  110. fan0.clearError();
  111. } else if (m_fanindex == kfan1) {
  112. fan1.clearError();
  113. } else if (m_fanindex == kfan0and1) {
  114. fan0.clearError();
  115. fan1.clearError();
  116. }
  117. };
  118. bool FanCtrlModule::isError() {
  119. if (m_fanindex == kfan0) {
  120. return fan0.isError();
  121. } else if (m_fanindex == kfan1) {
  122. return fan1.isError();
  123. } else if (m_fanindex == kfan0and1) {
  124. return fan0.isError() || fan1.isError();
  125. }
  126. return false;
  127. }
  128. /*******************************************************************************
  129. * ˮ *
  130. *******************************************************************************/
  131. PWMSpeedCtrlModule::config_t pumpcfg = {
  132. .name = "pump",
  133. .pwm_cfg =
  134. {
  135. .name = "pumptime",
  136. .htim = &htim2,
  137. .freq = TIMER2_FREQ,
  138. .polarity = false,
  139. },
  140. .nfan = 1,
  141. .fan0Channel = {2, 0, 0, 0},
  142. .fanFBGpioCfg =
  143. {
  144. {.pin = PC3},
  145. },
  146. .fanPowerGpioCfg =
  147. {
  148. {
  149. .pin = PC2,
  150. .mode = ZGPIO::kMode_nopull,
  151. .mirror = false,
  152. },
  153. },
  154. .enablefbcheck = false,
  155. .enableTrace = ENABL_TRACE,
  156. };
  157. /*******************************************************************************
  158. * PumpCtrlModule *
  159. *******************************************************************************/
  160. void PumpCtrlModule::initialize() { pump.initialize(&pumpcfg); }
  161. void PumpCtrlModule::setPumpSpeed(int32_t duty) { pump.startModule(duty); }
  162. void PumpCtrlModule::stop() { pump.stopModule(); }
  163. bool PumpCtrlModule::isError() { return pump.isError(); }
  164. bool PumpCtrlModule::isWorking() { return pump.isWorking(); }
  165. void PumpCtrlModule::clearError() { return pump.clearError(); }
  166. /*******************************************************************************
  167. * *
  168. *******************************************************************************/
  169. #define PELTIER_POLARITY false
  170. DRV8710::config_t peltier_config0 = {
  171. .pwm_cfg =
  172. {
  173. .name = "peltier1_pwm",
  174. .htim = &htim1,
  175. .freq = TIMER1_FREQ,
  176. .polarity = false,
  177. },
  178. .in1_chnannel_index = 3, // PE13
  179. .in2 = PE14,
  180. .nsleep = PB13,
  181. .nfault = PB14,
  182. .sensePin = PB15,
  183. .shaft = PELTIER_POLARITY,
  184. .enableTrace = ENABL_TRACE,
  185. };
  186. DRV8710::config_t peltier_config1 = {
  187. .pwm_cfg =
  188. {
  189. .name = "peltier0_pwm",
  190. .htim = &htim1,
  191. .freq = TIMER1_FREQ,
  192. .polarity = false,
  193. },
  194. .in1_chnannel_index = 1, // PE9
  195. .in2 = PE11,
  196. .nsleep = PB2,
  197. .nfault = PB3,
  198. .sensePin = PB4,
  199. .shaft = !PELTIER_POLARITY,
  200. .enableTrace = ENABL_TRACE,
  201. };
  202. void PeltierCtrlModule::initialize(peltier_index_t index) {
  203. m_index = index;
  204. if (m_index == kpeltier0) {
  205. peltier0.initialize(&peltier_config0);
  206. } else if (m_index == kpeltier1) {
  207. peltier1.initialize(&peltier_config1);
  208. } else if (m_index == kpeltier0and1) {
  209. peltier0.initialize(&peltier_config0);
  210. peltier1.initialize(&peltier_config1);
  211. }
  212. }
  213. void PeltierCtrlModule::setSpeed(int32_t duty) {
  214. m_workflag = true;
  215. if (m_index == kpeltier0) {
  216. peltier0.enable(true);
  217. peltier0.move(duty);
  218. } else if (m_index == kpeltier1) {
  219. peltier1.enable(true);
  220. peltier1.move(duty);
  221. } else if (m_index == kpeltier0and1) {
  222. peltier0.enable(true);
  223. peltier0.move(duty);
  224. peltier1.enable(true);
  225. peltier1.move(duty);
  226. }
  227. }
  228. void PeltierCtrlModule::stop() {
  229. m_workflag = false;
  230. if (m_index == kpeltier0) {
  231. peltier0.enable(false);
  232. peltier0.move(0);
  233. } else if (m_index == kpeltier1) {
  234. peltier1.enable(false);
  235. peltier1.move(0);
  236. } else if (m_index == kpeltier0and1) {
  237. peltier0.enable(false);
  238. peltier0.move(0);
  239. peltier1.enable(false);
  240. peltier1.move(0);
  241. }
  242. }
  243. bool PeltierCtrlModule::isError() {
  244. if (m_index == kpeltier0) {
  245. return peltier0.isFault();
  246. } else if (m_index == kpeltier1) {
  247. return peltier1.isFault();
  248. } else if (m_index == kpeltier0and1) {
  249. return peltier0.isFault() || peltier1.isFault();
  250. }
  251. return false;
  252. }
  253. bool PeltierCtrlModule::dumpErrorInfo() { //
  254. if (m_index == kpeltier0) {
  255. ZLOGI(TAG, "motor0 fault:%d,sense:%d", peltier0.isFault(), peltier0.getSensePinState());
  256. } else if (m_index == kpeltier1) {
  257. ZLOGI(TAG, "motor1 fault:%d,sense:%d", peltier1.isFault(), peltier1.getSensePinState());
  258. } else if (m_index == kpeltier0and1) {
  259. ZLOGI(TAG, "motor0 fault:%d,sense:%d", peltier0.isFault(), peltier0.getSensePinState());
  260. ZLOGI(TAG, "motor1 fault:%d,sense:%d", peltier1.isFault(), peltier1.getSensePinState());
  261. }
  262. return true;
  263. };
  264. bool PeltierCtrlModule::isWorking() { return m_workflag; }
  265. void PeltierCtrlModule::clearError() {
  266. if (m_index == kpeltier0) {
  267. peltier0.clearFault();
  268. } else if (m_index == kpeltier1) {
  269. peltier1.clearFault();
  270. } else if (m_index == kpeltier0and1) {
  271. peltier0.clearFault();
  272. peltier1.clearFault();
  273. }
  274. }