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.

205 lines
6.2 KiB

3 years ago
  1. #include "zport.h"
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include "main.h"
  6. #include "tim.h"
  7. #include "usart.h"
  8. #include "zboard.h"
  9. /***********************************************************************************************************************
  10. * =========================================================================================================== *
  11. ***********************************************************************************************************************/
  12. /**
  13. * @brief printf
  14. *
  15. * @param ch
  16. * @param stream
  17. * @return int
  18. */
  19. int fputc(int ch, FILE* stream) {
  20. uint8_t c = ch;
  21. HAL_UART_Transmit(&DEBUG_UART, &c, 1, 100);
  22. return ch;
  23. }
  24. // systicket 辅助方法
  25. uint32_t port_haspassedms(uint32_t ticket) {
  26. uint32_t nowticket = HAL_GetTick();
  27. if (nowticket >= ticket) {
  28. return nowticket - ticket;
  29. }
  30. return UINT32_MAX - ticket + nowticket;
  31. }
  32. void port_delay_us(uint32_t us) {
  33. uint16_t counter = 0;
  34. __HAL_TIM_SET_COUNTER(&US_TIMER, 0);
  35. HAL_TIM_Base_Start(&US_TIMER);
  36. while (counter < us) {
  37. counter = __HAL_TIM_GET_COUNTER(&US_TIMER);
  38. }
  39. HAL_TIM_Base_Stop(&US_TIMER);
  40. }
  41. static uint8_t g_port_exit_critical_count;
  42. void port_enter_critical() {
  43. if (g_port_exit_critical_count == 0) {
  44. __disable_irq();
  45. }
  46. g_port_exit_critical_count++;
  47. }
  48. void port_exit_critical() {
  49. g_port_exit_critical_count--;
  50. if (g_port_exit_critical_count == 0) {
  51. __enable_irq();
  52. }
  53. }
  54. /***********************************************************************************************************************
  55. * ======================================================================================================= *
  56. ***********************************************************************************************************************/
  57. void port_set_led_brightness1(uint16_t brightness) {
  58. /**
  59. * @brief
  60. */
  61. uint16_t count = (1 - brightness / 65535.0) * __HAL_TIM_GET_AUTORELOAD(&LED_CRL_PWMX_TIM);
  62. __HAL_TIM_SET_COMPARE(&LED_CRL_PWMX_TIM, LED_CRL_PWM1_CH, count);
  63. __HAL_TIM_SET_COMPARE(&LED_CRL_PWMX_TIM, LED_CRL_PWM2_CH, count);
  64. __HAL_TIM_SET_COMPARE(&LED_CRL_PWMX_TIM, LED_CRL_PWM3_CH, count);
  65. __HAL_TIM_SET_COMPARE(&LED_CRL_PWMX_TIM, LED_CRL_PWM4_CH, count);
  66. HAL_TIM_PWM_Start(&LED_CRL_PWMX_TIM, LED_CRL_PWM1_CH);
  67. HAL_TIM_PWM_Start(&LED_CRL_PWMX_TIM, LED_CRL_PWM2_CH);
  68. HAL_TIM_PWM_Start(&LED_CRL_PWMX_TIM, LED_CRL_PWM3_CH);
  69. HAL_TIM_PWM_Start(&LED_CRL_PWMX_TIM, LED_CRL_PWM4_CH);
  70. }
  71. void port_set_led_brightness2(uint16_t brightness) {
  72. /**
  73. * @brief
  74. * 10k
  75. */
  76. uint16_t count = (1 - brightness / 65535.0) * __HAL_TIM_GET_AUTORELOAD(&LED_CRL_APWM1_TIM);
  77. __HAL_TIM_SET_COMPARE(&LED_CRL_APWM1_TIM, LED_CRL_APWM1_CH, count);
  78. HAL_TIM_PWM_Start(&LED_CRL_APWM1_TIM, LED_CRL_APWM1_CH);
  79. }
  80. /**
  81. * @brief
  82. *
  83. * @param brightness
  84. * @return uint16_t
  85. */
  86. uint16_t get_set_led_brightness2_limit(uint16_t brightness) {
  87. const float min_ratio = 0.16;
  88. const float max_ratio = 1 - 0.3;
  89. float expect_duty_ratio = brightness / 65535.0;
  90. float final_radio = min_ratio + (max_ratio - min_ratio) * expect_duty_ratio;
  91. return final_radio * 65535;
  92. }
  93. void port_set_led_brightness(uint16_t brightness) {
  94. if (brightness == 0) {
  95. port_set_led_brightness1(0);
  96. port_set_led_brightness2(get_set_led_brightness2_limit(0));
  97. } else {
  98. port_set_led_brightness1(65535);
  99. port_set_led_brightness2(get_set_led_brightness2_limit(brightness));
  100. }
  101. }
  102. /***********************************************************************************************************************
  103. * =====================================================ExtraGPIO===================================================== *
  104. ***********************************************************************************************************************/
  105. static extra_gpio_t s_extra_gpios[2] = {0};
  106. void port_extra_gpio_init() {
  107. GPIO_InitTypeDef GPIO_InitStruct = {0};
  108. s_extra_gpios[0].GPIOx = ExtraGPIO0_GPIOx;
  109. s_extra_gpios[0].pin = ExtraGPIO0_pin;
  110. s_extra_gpios[1].GPIOx = ExtraGPIO1_GPIOx;
  111. s_extra_gpios[1].pin = ExtraGPIO1_pin;
  112. }
  113. void port_extra_gpio_set_mirror(int gpioNum, bool mirror) {
  114. extra_gpio_t* gpio = NULL;
  115. if (gpioNum >= ARRARY_SIZE(s_extra_gpios)) {
  116. return;
  117. }
  118. gpio = &s_extra_gpios[gpioNum];
  119. gpio->mirror = mirror;
  120. }
  121. void port_extra_gpio_set_mode(int gpioNum, extra_gpio_mode_e mode) {
  122. extra_gpio_t* gpio = NULL;
  123. GPIO_InitTypeDef GPIO_InitStruct = {0};
  124. if (gpioNum >= ARRARY_SIZE(s_extra_gpios)) {
  125. return;
  126. }
  127. gpio = &s_extra_gpios[gpioNum];
  128. gpio->mode = mode;
  129. if (mode == kegm_disable) {
  130. //模拟输入
  131. HAL_GPIO_DeInit(gpio->GPIOx, gpio->pin);
  132. GPIO_InitStruct.Pin = gpio->pin;
  133. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  134. HAL_GPIO_Init(gpio->GPIOx, &GPIO_InitStruct);
  135. } else if (mode == kegm_camera_trigger_signal) {
  136. //不拉输出
  137. HAL_GPIO_DeInit(gpio->GPIOx, gpio->pin);
  138. port_extra_gpio_set_level(gpioNum, false);
  139. GPIO_InitStruct.Pin = gpio->pin;
  140. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  141. GPIO_InitStruct.Pull = GPIO_NOPULL;
  142. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  143. HAL_GPIO_Init(gpio->GPIOx, &GPIO_InitStruct);
  144. }
  145. }
  146. extra_gpio_mode_e port_extra_gpio_get_mode(int gpioNum) {
  147. extra_gpio_t* gpio = NULL;
  148. if (gpioNum >= ARRARY_SIZE(s_extra_gpios)) {
  149. return kegm_disable;
  150. }
  151. gpio = &s_extra_gpios[gpioNum];
  152. return gpio->mode;
  153. }
  154. void port_extra_gpio_set_level(int gpioNum, bool level) {
  155. extra_gpio_t* gpio = NULL;
  156. bool nowState = false;
  157. if (gpioNum >= ARRARY_SIZE(s_extra_gpios)) {
  158. return;
  159. }
  160. gpio = &s_extra_gpios[gpioNum];
  161. if (gpio->mirror) level = !level;
  162. if (level) {
  163. HAL_GPIO_WritePin(gpio->GPIOx, gpio->pin, GPIO_PIN_SET);
  164. } else {
  165. HAL_GPIO_WritePin(gpio->GPIOx, gpio->pin, GPIO_PIN_RESET);
  166. }
  167. }
  168. bool port_extra_gpio_get_level(int gpioNum) {
  169. extra_gpio_t* gpio = NULL;
  170. bool nowState = false;
  171. if (gpioNum > ARRARY_SIZE(s_extra_gpios)) {
  172. return false;
  173. }
  174. gpio = &s_extra_gpios[gpioNum];
  175. GPIO_PinState state = HAL_GPIO_ReadPin(gpio->GPIOx, gpio->pin);
  176. if (state == GPIO_PIN_SET) {
  177. nowState = true;
  178. } else {
  179. nowState = false;
  180. }
  181. if (gpio->mirror) nowState = !nowState;
  182. return nowState;
  183. }