user.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * user.c
  3. *
  4. * Created on: Apr 21, 2025
  5. * Author: andre
  6. */
  7. #include "main.h"
  8. #include "user.h"
  9. #include "uart.h"
  10. #include "adc.h"
  11. #include "settings.h"
  12. extern TIM_HandleTypeDef htim1;
  13. extern TIM_HandleTypeDef htim3;
  14. extern SettingsStruct Settings;
  15. extern RC_LinkStatistics LinkStatistics;
  16. uint8_t RC_LinkUpAtLeastOnce = 0;
  17. void USER_Init(void) {
  18. unsigned int ChannelValue;
  19. unsigned int ch;
  20. SettingsInit();
  21. for(ch=0; ch<5; ch++) {
  22. ChannelValue = Settings.start[ch];
  23. if (ChannelValue > 0) {
  24. USER_SetPWM(ch, ChannelValue);
  25. } else {
  26. USER_SetPWM(ch, 1000);
  27. }
  28. }
  29. TIM3->CCER |= (uint32_t)(TIM_CCx_ENABLE << (TIM_CHANNEL_1 & 0x1FU)); // PWM1
  30. TIM3->CCER |= (uint32_t)(TIM_CCx_ENABLE << (TIM_CHANNEL_2 & 0x1FU)); // PWM2
  31. TIM1->CCER |= (uint32_t)(TIM_CCx_ENABLE << (TIM_CHANNEL_1 & 0x1FU)); // PWM3
  32. TIM1->CCER |= (uint32_t)(TIM_CCx_ENABLE << (TIM_CHANNEL_4 & 0x1FU)); // PWM4
  33. TIM1->CCER |= (uint32_t)(TIM_CCx_ENABLE << (TIM_CHANNEL_2 & 0x1FU)); // PWM5
  34. __HAL_TIM_MOE_ENABLE(&htim1);
  35. __HAL_TIM_ENABLE(&htim1);
  36. __HAL_TIM_MOE_ENABLE(&htim3);
  37. __HAL_TIM_ENABLE(&htim3);
  38. Uart_StartReceive();
  39. ADC_Init();
  40. }
  41. void USER_SetPWM(uint8_t chanel, uint16_t value) {
  42. switch ( chanel )
  43. {
  44. case 0:
  45. TIM3->CCR1 = value;
  46. break;
  47. case 1:
  48. TIM3->CCR2 = value;
  49. break;
  50. case 2:
  51. TIM1->CCR1 = value;
  52. break;
  53. case 3:
  54. TIM1->CCR4 = value;
  55. break;
  56. case 4:
  57. TIM1->CCR2 = value;
  58. break;
  59. default:
  60. break;
  61. }
  62. }
  63. void USER_Main_Loop(void) {
  64. unsigned int ChannelValue;
  65. unsigned int tmp;
  66. unsigned int ch;
  67. if ((LinkStatistics.up_link_quality > 0) & (Uart_GetCounter() < 10)) { // ELRS connection checking
  68. RC_LinkUpAtLeastOnce = 1;
  69. for(ch=0; ch<5; ch++) {
  70. ChannelValue = Uart_GetChannel(ch);
  71. if (ChannelValue > 0) {
  72. USER_SetPWM(ch, ChannelValue);
  73. }
  74. }
  75. if (HAL_GPIO_ReadPin(SET_DEFAULT_GPIO_Port, SET_DEFAULT_Pin) == GPIO_PIN_RESET) { // Set Default Values
  76. for(ch=0; ch<5; ch++) {
  77. Settings.start[ch] = Uart_GetChannel(ch);
  78. }
  79. SettingsSave();
  80. while (HAL_GPIO_ReadPin(SET_DEFAULT_GPIO_Port, SET_DEFAULT_Pin) == GPIO_PIN_RESET) {
  81. HAL_Delay(10);
  82. }
  83. HAL_Delay(100);
  84. }
  85. if (HAL_GPIO_ReadPin(SET_FAILSAFE_GPIO_Port, SET_FAILSAFE_Pin) == GPIO_PIN_RESET) { // Set Fail safe Values
  86. for(ch=0; ch<5; ch++) {
  87. Settings.fail[ch] = Uart_GetChannel(ch);
  88. }
  89. SettingsSave();
  90. while (HAL_GPIO_ReadPin(SET_FAILSAFE_GPIO_Port, SET_FAILSAFE_Pin) == GPIO_PIN_RESET) {
  91. HAL_Delay(10);
  92. }
  93. HAL_Delay(100);
  94. }
  95. } else {
  96. if (RC_LinkUpAtLeastOnce == 1) {
  97. // Check if the values ​​are set
  98. tmp = 0;
  99. for(ch=0; ch<5; ch++) {
  100. tmp = tmp + Settings.fail[ch];
  101. }
  102. if (tmp > 1000) {
  103. // Set Fail safe values
  104. for(ch=0; ch<5; ch++) {
  105. ChannelValue = Settings.fail[ch];
  106. if (ChannelValue > 0) {
  107. USER_SetPWM(ch, ChannelValue);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. ADC_Start_Convertion();
  114. Make_Frame();
  115. Uart_StartSendFrame();
  116. Uart_IncCounter();
  117. HAL_Delay(1);
  118. }