#include
#include
#include
#include
#include "interrupt.h"
#include
#include
#include
#include
#include
#include "FreeRTOS.h"
#include "task.h"
#include "tinatest.h"
extern int amp_init(void);
#define ENC_CLK GPIOA(24)
#define ENC_DT GPIOA(25)
#define ENC_SW GPIOA(29)
int encode_counter = 0;
int encode_current_clk;
int encode_lask_clk;
int current_dir = 0;
static hal_irqreturn_t gpio_irq_encode(void *data)
{
gpio_data_t clk_value = GPIO_DATA_LOW;
gpio_data_t dt_value = GPIO_DATA_LOW;
hal_gpio_get_data(ENC_DT, &dt_value);
hal_gpio_get_data(ENC_CLK, &clk_value);
encode_current_clk = clk_value;
if (encode_current_clk != encode_lask_clk && encode_current_clk == 1){
if (dt_value != encode_current_clk) {
encode_counter ++;
current_dir = 1;
} else {
encode_counter --;
current_dir = -1;
}
printf("Direction = %d, Counter = %d\n", current_dir, encode_counter);
}
encode_lask_clk = encode_current_clk;
return 0;
}
void cpu0_app_entry(void *param)
{
int ret = 0;
amp_init();
hal_gpio_set_pull(ENC_CLK, GPIO_PULL_DOWN_DISABLED);
hal_gpio_set_direction(ENC_CLK, GPIO_DIRECTION_INPUT);
hal_gpio_pinmux_set_function(ENC_CLK, GPIO_MUXSEL_IN);
gpio_data_t clk_data;
hal_gpio_get_data(ENC_CLK, &clk_data);
encode_lask_clk = clk_data;
hal_gpio_set_pull(ENC_DT, GPIO_PULL_DOWN_DISABLED);
hal_gpio_set_direction(ENC_DT, GPIO_DIRECTION_INPUT);
hal_gpio_pinmux_set_function(ENC_DT, GPIO_MUXSEL_IN);
uint32_t irq_clk, irq_dt;
ret = hal_gpio_to_irq(ENC_CLK, &irq_clk);
if (ret < 0){
printf("gpio to irq error, irq num:%d error num: %d\n", irq_clk, ret);
}
ret = hal_gpio_irq_request(irq_clk, gpio_irq_encode, IRQ_TYPE_EDGE_BOTH, NULL);
if (ret < 0){
printf("request irq error, irq num:%d error num: %d\n", irq_clk, ret);
}
ret = hal_gpio_irq_enable(irq_clk);
if (ret < 0){
printf("request irq error, error num: %d\n", ret);
}
ret = hal_gpio_to_irq(ENC_DT, &irq_dt);
if (ret < 0){
printf("gpio to irq error, irq num:%d error num: %d\n", irq_dt, ret);
}
ret = hal_gpio_irq_request(irq_dt, gpio_irq_encode, IRQ_TYPE_EDGE_BOTH, NULL);
if (ret < 0){
printf("request irq error, irq num:%d error num: %d\n", irq_dt, ret);
}
ret = hal_gpio_irq_enable(irq_dt);
if (ret < 0){
printf("request irq error, error num: %d\n", ret);
}
vTaskDelete(NULL);
}