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.

287 lines
8.1 KiB

2 years ago
  1. #include "driver.hpp"
  2. using namespace iflytop;
  3. #define TIMER2_FREQ 1000
  4. #define TIMER1_FREQ 1000
  5. #define HEART_SHAFT 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 = true,
  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 = true,
  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. .calltrace = true,
  140. },
  141. .nfan = 1,
  142. .fan0Channel = {2, 0, 0, 0},
  143. .fanFBGpioCfg =
  144. {
  145. {.pin = PC3},
  146. },
  147. .fanPowerGpioCfg =
  148. {
  149. {
  150. .pin = PC2,
  151. .mode = ZGPIO::kMode_nopull,
  152. .mirror = false,
  153. .log_when_setstate = true,
  154. },
  155. },
  156. .enablefbcheck = false,
  157. .enableTrace = true,
  158. };
  159. /*******************************************************************************
  160. * PumpCtrlModule *
  161. *******************************************************************************/
  162. void PumpCtrlModule::initialize() { pump.initialize(&pumpcfg); }
  163. void PumpCtrlModule::setPumpSpeed(int32_t duty) { pump.startModule(duty); }
  164. void PumpCtrlModule::stop() { pump.stopModule(); }
  165. bool PumpCtrlModule::isError() { return pump.isError(); }
  166. bool PumpCtrlModule::isWorking() { return pump.isWorking(); }
  167. void PumpCtrlModule::clearError() { return pump.clearError(); }
  168. /*******************************************************************************
  169. * *
  170. *******************************************************************************/
  171. DRV8710::config_t peltier_config0 = {
  172. .pwm_cfg =
  173. {
  174. .name = "peltier0_pwm",
  175. .htim = &htim1,
  176. .freq = TIMER1_FREQ,
  177. .polarity = false,
  178. },
  179. .in1_chnannel_index = 1, // PE9
  180. .in2 = PE11,
  181. .nsleep = PB2,
  182. .nfault = PB3,
  183. .sensePin = PB4,
  184. .shaft = HEART_SHAFT,
  185. .enableTrace = true,
  186. };
  187. DRV8710::config_t peltier_config1 = {
  188. .pwm_cfg =
  189. {
  190. .name = "peltier1_pwm",
  191. .htim = &htim1,
  192. .freq = TIMER1_FREQ,
  193. .polarity = false,
  194. },
  195. .in1_chnannel_index = 3, // PE13
  196. .in2 = PE14,
  197. .nsleep = PB13,
  198. .nfault = PB14,
  199. .sensePin = PB15,
  200. .shaft = HEART_SHAFT,
  201. .enableTrace = true,
  202. };
  203. void PeltierCtrlModule::initialize(peltier_index_t index) {
  204. m_index = index;
  205. if (m_index == kpeltier0) {
  206. peltier0.initialize(&peltier_config0);
  207. } else if (m_index == kpeltier1) {
  208. peltier1.initialize(&peltier_config1);
  209. } else if (m_index == kpeltier0and1) {
  210. peltier0.initialize(&peltier_config0);
  211. peltier1.initialize(&peltier_config1);
  212. }
  213. }
  214. void PeltierCtrlModule::setSpeed(int32_t duty) {
  215. m_workflag = true;
  216. if (m_index == kpeltier0) {
  217. peltier0.enable(true);
  218. peltier0.move(duty);
  219. } else if (m_index == kpeltier1) {
  220. peltier1.enable(true);
  221. peltier1.move(duty);
  222. } else if (m_index == kpeltier0and1) {
  223. peltier0.enable(true);
  224. peltier0.move(duty);
  225. peltier1.enable(true);
  226. peltier1.move(duty);
  227. }
  228. }
  229. void PeltierCtrlModule::stop() {
  230. m_workflag = false;
  231. if (m_index == kpeltier0) {
  232. peltier0.enable(false);
  233. peltier0.move(0);
  234. } else if (m_index == kpeltier1) {
  235. peltier1.enable(false);
  236. peltier1.move(0);
  237. } else if (m_index == kpeltier0and1) {
  238. peltier0.enable(false);
  239. peltier0.move(0);
  240. peltier1.enable(false);
  241. peltier1.move(0);
  242. }
  243. }
  244. bool PeltierCtrlModule::isError() {
  245. if (m_index == kpeltier0) {
  246. return peltier0.isFault();
  247. } else if (m_index == kpeltier1) {
  248. return peltier1.isFault();
  249. } else if (m_index == kpeltier0and1) {
  250. return peltier0.isFault() || peltier1.isFault();
  251. }
  252. return false;
  253. }
  254. bool PeltierCtrlModule::dumpErrorInfo() { //
  255. if (m_index == kpeltier0) {
  256. ZLOGI(TAG, "motor0 fault:%d,sense:%d", !peltier0.isFault(), peltier0.getSensePinState());
  257. } else if (m_index == kpeltier1) {
  258. ZLOGI(TAG, "motor1 fault:%d,sense:%d", !peltier1.isFault(), peltier1.getSensePinState());
  259. } else if (m_index == kpeltier0and1) {
  260. ZLOGI(TAG, "motor0 fault:%d,sense:%d", !peltier0.isFault(), peltier0.getSensePinState());
  261. ZLOGI(TAG, "motor1 fault:%d,sense:%d", !peltier1.isFault(), peltier1.getSensePinState());
  262. }
  263. return true;
  264. };
  265. bool PeltierCtrlModule::isWorking() { return m_workflag; }
  266. void PeltierCtrlModule::clearError() { return; }