peripherial.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * peripherial.c
  3. *
  4. * Created on: Sep 10, 2024
  5. * Author: jakubski
  6. */
  7. #include <stdlib.h>
  8. #include "peripherial.h"
  9. void DbgLEDOn (uint8_t ledNumber) {
  10. HAL_GPIO_WritePin (GPIOD, ledNumber, GPIO_PIN_SET);
  11. }
  12. void DbgLEDOff (uint8_t ledNumber) {
  13. HAL_GPIO_WritePin (GPIOD, ledNumber, GPIO_PIN_RESET);
  14. }
  15. void DbgLEDToggle (uint8_t ledNumber) {
  16. HAL_GPIO_TogglePin (GPIOD, ledNumber);
  17. }
  18. void EnableCurrentSensors (void) {
  19. HAL_GPIO_WritePin (GPIOE, MCU_CS_PWR_EN, GPIO_PIN_SET);
  20. }
  21. void DisableCurrentSensors (void) {
  22. HAL_GPIO_WritePin (GPIOE, MCU_CS_PWR_EN, GPIO_PIN_RESET);
  23. }
  24. void SelectCurrentSensorGain (CurrentSensor sensor, CurrentSensorGain gain) {
  25. uint8_t gpioOffset = 0;
  26. switch (sensor) {
  27. case CurrentSensorL1: gpioOffset = CURRENT_SENSOR_L1_GPIO_OFFSET; break;
  28. case CurrentSensorL2: gpioOffset = CURRENT_SENSOR_L2_GPIO_OFFSET; break;
  29. case CurrentSensorL3: gpioOffset = CURRENT_SENSOR_L3_GPIO_OFFSET; break;
  30. default: break;
  31. }
  32. if (gpioOffset > 0) {
  33. uint16_t gain0Gpio = 1 << gpioOffset;
  34. uint16_t gain1Gpio = 1 << (gpioOffset + 1);
  35. uint16_t gpioState = ((uint16_t)gain) & 0x0001;
  36. HAL_GPIO_WritePin (GPIOE, gain0Gpio, gpioState);
  37. gpioState = (((uint16_t)gain) >> 1) & 0x0001;
  38. HAL_GPIO_WritePin (GPIOE, gain1Gpio, gpioState);
  39. }
  40. }
  41. uint8_t
  42. MotorControl (TIM_HandleTypeDef* htim, TIM_OC_InitTypeDef* motorTimerConfigOC, uint8_t channel1, uint8_t channel2, osTimerId_t motorTimerHandle, int32_t motorPWMPulse, int32_t motorTimerPeriod, uint8_t switchLimiterUpStat, uint8_t switchLimiterDownStat) {
  43. uint32_t motorStatus = 0;
  44. MotorDriverState setMotorState = HiZ;
  45. HAL_TIM_PWM_Stop (htim, channel1);
  46. HAL_TIM_PWM_Stop (htim, channel2);
  47. if (motorTimerPeriod > 0) {
  48. if (motorPWMPulse > 0) {
  49. // Forward
  50. if (switchLimiterUpStat == 0) {
  51. setMotorState = Forward;
  52. MotorAction (htim, motorTimerConfigOC, channel1, channel2, setMotorState, abs (motorPWMPulse) * 10);
  53. HAL_TIM_PWM_Start (htim, channel1);
  54. motorStatus = 1;
  55. } else {
  56. HAL_TIM_PWM_Stop (htim, channel1);
  57. }
  58. HAL_TIM_PWM_Stop (htim, channel2);
  59. } else if (motorPWMPulse < 0) {
  60. // Reverse
  61. if (switchLimiterDownStat == 0) {
  62. setMotorState = Reverse;
  63. MotorAction (htim, motorTimerConfigOC, channel1, channel2, setMotorState, abs (motorPWMPulse) * 10);
  64. HAL_TIM_PWM_Start (htim, channel2);
  65. motorStatus = 1;
  66. } else {
  67. HAL_TIM_PWM_Stop (htim, channel2);
  68. }
  69. HAL_TIM_PWM_Stop (htim, channel1);
  70. } else {
  71. // Brake
  72. setMotorState = Brake;
  73. MotorAction (htim, motorTimerConfigOC, channel1, channel2, setMotorState, abs (motorPWMPulse) * 10);
  74. HAL_TIM_PWM_Start (htim, channel1);
  75. HAL_TIM_PWM_Start (htim, channel2);
  76. motorStatus = 0;
  77. }
  78. osTimerStart (motorTimerHandle, motorTimerPeriod * 1000);
  79. } else if ((motorTimerPeriod == 0) && (motorPWMPulse == 0)) {
  80. MotorAction (htim, motorTimerConfigOC, channel1, channel2, HiZ, abs (motorPWMPulse) * 10);
  81. HAL_TIM_PWM_Stop (htim, channel1);
  82. HAL_TIM_PWM_Stop (htim, channel2);
  83. osTimerStop (motorTimerHandle);
  84. motorStatus = 0;
  85. } else if (motorTimerPeriod == -1) {
  86. if (motorPWMPulse > 0) {
  87. // Forward
  88. if (switchLimiterUpStat == 0) {
  89. setMotorState = Forward;
  90. MotorAction (htim, motorTimerConfigOC, channel1, channel2, setMotorState, abs (motorPWMPulse) * 10);
  91. HAL_TIM_PWM_Start (htim, channel1);
  92. motorStatus = 1;
  93. } else {
  94. HAL_TIM_PWM_Stop (htim, channel1);
  95. }
  96. HAL_TIM_PWM_Stop (htim, channel2);
  97. } else {
  98. // Reverse
  99. if (switchLimiterDownStat == 0) {
  100. setMotorState = Reverse;
  101. MotorAction (htim, motorTimerConfigOC, channel1, channel2, setMotorState, abs (motorPWMPulse) * 10);
  102. HAL_TIM_PWM_Start (htim, channel2);
  103. motorStatus = 1;
  104. } else {
  105. HAL_TIM_PWM_Stop (htim, channel2);
  106. }
  107. HAL_TIM_PWM_Stop (htim, channel1);
  108. }
  109. }
  110. return motorStatus;
  111. }
  112. void MotorAction (TIM_HandleTypeDef* tim, TIM_OC_InitTypeDef* timerConf, uint32_t channel1, uint32_t channel2, MotorDriverState setState, uint32_t pulse) {
  113. timerConf->Pulse = pulse;
  114. switch (setState) {
  115. case Forward:
  116. case Reverse:
  117. case HiZ:
  118. timerConf->OCPolarity = TIM_OCPOLARITY_HIGH;
  119. if (HAL_TIM_PWM_ConfigChannel (tim, timerConf, channel1) != HAL_OK) {
  120. Error_Handler ();
  121. }
  122. timerConf->OCPolarity = TIM_OCPOLARITY_HIGH;
  123. if (HAL_TIM_PWM_ConfigChannel (tim, timerConf, channel2) != HAL_OK) {
  124. Error_Handler ();
  125. }
  126. break;
  127. case Brake:
  128. timerConf->OCPolarity = TIM_OCPOLARITY_LOW;
  129. if (HAL_TIM_PWM_ConfigChannel (tim, timerConf, channel1) != HAL_OK) {
  130. Error_Handler ();
  131. }
  132. timerConf->OCPolarity = TIM_OCPOLARITY_LOW;
  133. if (HAL_TIM_PWM_ConfigChannel (tim, timerConf, channel2) != HAL_OK) {
  134. Error_Handler ();
  135. }
  136. break;
  137. default:
  138. timerConf->OCPolarity = TIM_OCPOLARITY_HIGH;
  139. if (HAL_TIM_PWM_ConfigChannel (tim, timerConf, channel1) != HAL_OK) {
  140. Error_Handler ();
  141. }
  142. timerConf->OCPolarity = TIM_OCPOLARITY_HIGH;
  143. if (HAL_TIM_PWM_ConfigChannel (tim, timerConf, channel2) != HAL_OK) {
  144. Error_Handler ();
  145. }
  146. break;
  147. }
  148. }