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.
206 lines
6.2 KiB
206 lines
6.2 KiB
#include "zport.h"
|
|
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
|
|
#include "main.h"
|
|
#include "tim.h"
|
|
#include "usart.h"
|
|
#include "zboard.h"
|
|
|
|
/***********************************************************************************************************************
|
|
* =====================================================基础组件====================================================== *
|
|
***********************************************************************************************************************/
|
|
/**
|
|
* @brief printf 串口重定向
|
|
*
|
|
* @param ch
|
|
* @param stream
|
|
* @return int
|
|
*/
|
|
int fputc(int ch, FILE* stream) {
|
|
uint8_t c = ch;
|
|
HAL_UART_Transmit(&DEBUG_UART, &c, 1, 100);
|
|
return ch;
|
|
}
|
|
|
|
// systicket 辅助方法
|
|
uint32_t port_haspassedms(uint32_t ticket) {
|
|
uint32_t nowticket = HAL_GetTick();
|
|
if (nowticket >= ticket) {
|
|
return nowticket - ticket;
|
|
}
|
|
return UINT32_MAX - ticket + nowticket;
|
|
}
|
|
void port_delay_us(uint32_t us) {
|
|
uint16_t counter = 0;
|
|
__HAL_TIM_SET_COUNTER(&US_TIMER, 0);
|
|
HAL_TIM_Base_Start(&US_TIMER);
|
|
while (counter < us) {
|
|
counter = __HAL_TIM_GET_COUNTER(&US_TIMER);
|
|
}
|
|
HAL_TIM_Base_Stop(&US_TIMER);
|
|
}
|
|
static uint8_t g_port_exit_critical_count;
|
|
void port_enter_critical() {
|
|
if (g_port_exit_critical_count == 0) {
|
|
__disable_irq();
|
|
}
|
|
g_port_exit_critical_count++;
|
|
}
|
|
void port_exit_critical() {
|
|
g_port_exit_critical_count--;
|
|
if (g_port_exit_critical_count == 0) {
|
|
__enable_irq();
|
|
}
|
|
}
|
|
/***********************************************************************************************************************
|
|
* ===================================================硬件接口相关==================================================== *
|
|
***********************************************************************************************************************/
|
|
|
|
void port_set_led_brightness1(uint16_t brightness) {
|
|
/**
|
|
* @brief 低电平有效
|
|
*/
|
|
uint16_t count = (1 - brightness / 65535.0) * __HAL_TIM_GET_AUTORELOAD(&LED_CRL_PWMX_TIM);
|
|
|
|
__HAL_TIM_SET_COMPARE(&LED_CRL_PWMX_TIM, LED_CRL_PWM1_CH, count);
|
|
__HAL_TIM_SET_COMPARE(&LED_CRL_PWMX_TIM, LED_CRL_PWM2_CH, count);
|
|
__HAL_TIM_SET_COMPARE(&LED_CRL_PWMX_TIM, LED_CRL_PWM3_CH, count);
|
|
__HAL_TIM_SET_COMPARE(&LED_CRL_PWMX_TIM, LED_CRL_PWM4_CH, count);
|
|
|
|
HAL_TIM_PWM_Start(&LED_CRL_PWMX_TIM, LED_CRL_PWM1_CH);
|
|
HAL_TIM_PWM_Start(&LED_CRL_PWMX_TIM, LED_CRL_PWM2_CH);
|
|
HAL_TIM_PWM_Start(&LED_CRL_PWMX_TIM, LED_CRL_PWM3_CH);
|
|
HAL_TIM_PWM_Start(&LED_CRL_PWMX_TIM, LED_CRL_PWM4_CH);
|
|
}
|
|
void port_set_led_brightness2(uint16_t brightness) {
|
|
/**
|
|
* @brief 高有效
|
|
* 10k
|
|
*/
|
|
uint16_t count = (1 - brightness / 65535.0) * __HAL_TIM_GET_AUTORELOAD(&LED_CRL_APWM1_TIM);
|
|
__HAL_TIM_SET_COMPARE(&LED_CRL_APWM1_TIM, LED_CRL_APWM1_CH, count);
|
|
HAL_TIM_PWM_Start(&LED_CRL_APWM1_TIM, LED_CRL_APWM1_CH);
|
|
}
|
|
/**
|
|
* @brief 模拟控制灯光亮度百分比限制
|
|
*
|
|
* @param brightness
|
|
* @return uint16_t
|
|
*/
|
|
uint16_t get_set_led_brightness2_limit(uint16_t brightness) {
|
|
const float min_ratio = 0.16;
|
|
const float max_ratio = 1 - 0.3;
|
|
float expect_duty_ratio = brightness / 65535.0;
|
|
float final_radio = min_ratio + (max_ratio - min_ratio) * expect_duty_ratio;
|
|
return final_radio * 65535;
|
|
}
|
|
|
|
void port_set_led_brightness(uint16_t brightness) {
|
|
if (brightness == 0) {
|
|
port_set_led_brightness1(0);
|
|
port_set_led_brightness2(get_set_led_brightness2_limit(0));
|
|
} else {
|
|
port_set_led_brightness1(65535);
|
|
port_set_led_brightness2(get_set_led_brightness2_limit(brightness));
|
|
}
|
|
}
|
|
|
|
/***********************************************************************************************************************
|
|
* =====================================================ExtraGPIO===================================================== *
|
|
***********************************************************************************************************************/
|
|
|
|
static extra_gpio_t s_extra_gpios[2] = {0};
|
|
void port_extra_gpio_init() {
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
s_extra_gpios[0].GPIOx = ExtraGPIO0_GPIOx;
|
|
s_extra_gpios[0].pin = ExtraGPIO0_pin;
|
|
|
|
s_extra_gpios[1].GPIOx = ExtraGPIO1_GPIOx;
|
|
s_extra_gpios[1].pin = ExtraGPIO1_pin;
|
|
}
|
|
void port_extra_gpio_set_mirror(int gpioNum, bool mirror) {
|
|
extra_gpio_t* gpio = NULL;
|
|
if (gpioNum >= ARRARY_SIZE(s_extra_gpios)) {
|
|
return;
|
|
}
|
|
gpio = &s_extra_gpios[gpioNum];
|
|
gpio->mirror = mirror;
|
|
}
|
|
|
|
void port_extra_gpio_set_mode(int gpioNum, extra_gpio_mode_e mode) {
|
|
extra_gpio_t* gpio = NULL;
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
if (gpioNum >= ARRARY_SIZE(s_extra_gpios)) {
|
|
return;
|
|
}
|
|
|
|
gpio = &s_extra_gpios[gpioNum];
|
|
gpio->mode = mode;
|
|
|
|
if (mode == kegm_disable) {
|
|
//模拟输入
|
|
HAL_GPIO_DeInit(gpio->GPIOx, gpio->pin);
|
|
|
|
GPIO_InitStruct.Pin = gpio->pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
|
HAL_GPIO_Init(gpio->GPIOx, &GPIO_InitStruct);
|
|
} else if (mode == kegm_camera_trigger_signal) {
|
|
//不拉输出
|
|
HAL_GPIO_DeInit(gpio->GPIOx, gpio->pin);
|
|
|
|
port_extra_gpio_set_level(gpioNum, false);
|
|
|
|
GPIO_InitStruct.Pin = gpio->pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
HAL_GPIO_Init(gpio->GPIOx, &GPIO_InitStruct);
|
|
}
|
|
}
|
|
extra_gpio_mode_e port_extra_gpio_get_mode(int gpioNum) {
|
|
extra_gpio_t* gpio = NULL;
|
|
if (gpioNum >= ARRARY_SIZE(s_extra_gpios)) {
|
|
return kegm_disable;
|
|
}
|
|
gpio = &s_extra_gpios[gpioNum];
|
|
return gpio->mode;
|
|
}
|
|
void port_extra_gpio_set_level(int gpioNum, bool level) {
|
|
extra_gpio_t* gpio = NULL;
|
|
bool nowState = false;
|
|
|
|
if (gpioNum >= ARRARY_SIZE(s_extra_gpios)) {
|
|
return;
|
|
}
|
|
|
|
gpio = &s_extra_gpios[gpioNum];
|
|
if (gpio->mirror) level = !level;
|
|
if (level) {
|
|
HAL_GPIO_WritePin(gpio->GPIOx, gpio->pin, GPIO_PIN_SET);
|
|
} else {
|
|
HAL_GPIO_WritePin(gpio->GPIOx, gpio->pin, GPIO_PIN_RESET);
|
|
}
|
|
}
|
|
bool port_extra_gpio_get_level(int gpioNum) {
|
|
extra_gpio_t* gpio = NULL;
|
|
bool nowState = false;
|
|
|
|
if (gpioNum > ARRARY_SIZE(s_extra_gpios)) {
|
|
return false;
|
|
}
|
|
|
|
gpio = &s_extra_gpios[gpioNum];
|
|
GPIO_PinState state = HAL_GPIO_ReadPin(gpio->GPIOx, gpio->pin);
|
|
if (state == GPIO_PIN_SET) {
|
|
nowState = true;
|
|
} else {
|
|
nowState = false;
|
|
}
|
|
if (gpio->mirror) nowState = !nowState;
|
|
return nowState;
|
|
}
|