diff --git a/checker_m4.uvoptx b/checker_m4.uvoptx index c8d4262..1238780 100644 --- a/checker_m4.uvoptx +++ b/checker_m4.uvoptx @@ -978,6 +978,50 @@ 0 0 + + 6 + 64 + 1 + 0 + 0 + 0 + .\source\interface\if_gpioin.c + if_gpioin.c + 0 + 0 + + + 6 + 65 + 1 + 0 + 0 + 0 + .\source\interface\if_vuart.c + if_vuart.c + 0 + 0 + + + + + dev + 1 + 0 + 0 + 0 + + 7 + 66 + 1 + 0 + 0 + 0 + .\source\dev\dev_flash.c + dev_flash.c + 0 + 0 + @@ -987,8 +1031,8 @@ 0 0 - 7 - 64 + 8 + 67 1 0 0 @@ -999,8 +1043,8 @@ 0 - 7 - 65 + 8 + 68 1 0 0 @@ -1011,8 +1055,8 @@ 0 - 7 - 66 + 8 + 69 1 0 0 @@ -1023,8 +1067,8 @@ 0 - 7 - 67 + 8 + 70 1 0 0 @@ -1035,8 +1079,8 @@ 0 - 7 - 68 + 8 + 71 1 0 0 @@ -1047,8 +1091,8 @@ 0 - 7 - 69 + 8 + 72 1 0 0 @@ -1059,8 +1103,8 @@ 0 - 7 - 70 + 8 + 73 1 0 0 @@ -1071,8 +1115,8 @@ 0 - 7 - 71 + 8 + 74 1 0 0 @@ -1083,8 +1127,8 @@ 0 - 7 - 72 + 8 + 75 1 0 0 @@ -1095,8 +1139,8 @@ 0 - 7 - 73 + 8 + 76 1 0 0 @@ -1107,8 +1151,8 @@ 0 - 7 - 74 + 8 + 77 1 0 0 @@ -1120,6 +1164,50 @@ + + task + 1 + 0 + 0 + 0 + + 9 + 78 + 1 + 0 + 0 + 0 + .\source\task\key.c + key.c + 0 + 0 + + + 9 + 79 + 1 + 0 + 0 + 0 + .\source\task\moter.c + moter.c + 0 + 0 + + + 9 + 80 + 1 + 0 + 0 + 0 + .\source\task\commend.c + commend.c + 0 + 0 + + + ::CMSIS 0 diff --git a/checker_m4.uvprojx b/checker_m4.uvprojx index 54b15a7..5b2e568 100644 --- a/checker_m4.uvprojx +++ b/checker_m4.uvprojx @@ -338,7 +338,7 @@ CORE_CM4,NO_ATOMIC_64_SUPPORT,USE_HAL_DRIVER,STM32MP157Dxx,METAL_INTERNAL,METAL_MAX_DEVICE_REGIONS=2,METAL_INTERNAL,VIRTIO_SLAVE_ONLY,__LOG_TRACE_IO_,RT_THREAD - .\source\core;.\source\main;.\source\rt_thread;.\source\rt_thread\include;.\source\stm32lib\STM32MP1xx_HAL_Driver\Inc;.\source\stm32lib\CMSIS\Include;.\source\OpenAMP\open-amp\lib\include;.\source\OpenAMP\libmetal\lib\include;.\source\OpenAMP\virtual_driver;.\source\soft + .\source\core;.\source\main;.\source\rt_thread;.\source\rt_thread\include;.\source\stm32lib\STM32MP1xx_HAL_Driver\Inc;.\source\stm32lib\CMSIS\Include;.\source\OpenAMP\open-amp\lib\include;.\source\OpenAMP\libmetal\lib\include;.\source\OpenAMP\virtual_driver;.\source\soft;.\source\task;.\source\dev @@ -611,57 +611,6 @@ stm32mp1xx_hal_tim.c 1 .\source\stm32lib\STM32MP1xx_HAL_Driver\Src\stm32mp1xx_hal_tim.c - - - 2 - 0 - 0 - 0 - 0 - 0 - 2 - 2 - 2 - 2 - 11 - - - 1 - - - - 2 - 0 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 0 - 2 - 2 - 2 - 2 - 2 - 0 - 0 - 2 - 2 - 2 - 2 - 2 - - - - - - - - - @@ -772,57 +721,26 @@ if_pwm.c 1 .\source\interface\if_pwm.c - - - 2 - 0 - 0 - 0 - 0 - 0 - 2 - 2 - 2 - 2 - 11 - - - 1 - - - - 2 - 0 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 0 - 2 - 2 - 2 - 2 - 2 - 0 - 0 - 2 - 2 - 2 - 2 - 2 - - - - - - - - - + + + if_gpioin.c + 1 + .\source\interface\if_gpioin.c + + + if_vuart.c + 1 + .\source\interface\if_vuart.c + + + + + dev + + + dev_flash.c + 1 + .\source\dev\dev_flash.c @@ -858,57 +776,6 @@ list.c 1 .\source\soft\list.c - - - 2 - 0 - 0 - 0 - 0 - 0 - 2 - 2 - 2 - 2 - 11 - - - 1 - - - - 2 - 0 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 0 - 2 - 2 - 2 - 2 - 2 - 0 - 0 - 2 - 2 - 2 - 2 - 2 - - - - - - - - - mymisc.c @@ -919,113 +786,11 @@ mystdlib.c 1 .\source\soft\mystdlib.c - - - 2 - 0 - 0 - 0 - 0 - 0 - 2 - 2 - 2 - 2 - 11 - - - 1 - - - - 2 - 0 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 0 - 2 - 2 - 2 - 2 - 2 - 0 - 0 - 2 - 2 - 2 - 2 - 2 - - - - - - - - - mystring.c 1 .\source\soft\mystring.c - - - 2 - 0 - 0 - 0 - 0 - 0 - 2 - 2 - 2 - 2 - 11 - - - 1 - - - - 2 - 0 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 0 - 2 - 2 - 2 - 2 - 2 - 0 - 0 - 2 - 2 - 2 - 2 - 2 - - - - - - - - - signal.c @@ -1036,57 +801,26 @@ sort.c 1 .\source\soft\sort.c - - - 2 - 0 - 0 - 0 - 0 - 0 - 2 - 2 - 2 - 2 - 11 - - - 1 - - - - 2 - 0 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 0 - 2 - 2 - 2 - 2 - 2 - 0 - 0 - 2 - 2 - 2 - 2 - 2 - - - - - - - - - + + + + + task + + + key.c + 1 + .\source\task\key.c + + + moter.c + 1 + .\source\task\moter.c + + + commend.c + 1 + .\source\task\commend.c diff --git a/source/dev/dev_flash.c b/source/dev/dev_flash.c new file mode 100644 index 0000000..245f96c --- /dev/null +++ b/source/dev/dev_flash.c @@ -0,0 +1,58 @@ + + +#include "rtthread.h" +#include "stm32mp1xx.h" +#include "board.h" +#include "dev_flash.h" +#include "debug.h" +#include "stdlib.h" +#include "string.h" + +#ifndef RT_THREAD + +#define rt_interrupt_enter() +#define rt_interrupt_leave() +#define rt_mutex_create(...) 0 +#define rt_mutex_delete(...) +#define rt_mutex_take(...) +#define rt_mutex_release(...) + +#endif + +typedef struct{ + sys_param_def param; +}self_def; + +static self_def g_self; + + + + + + +// 保存参数 +int flash_save_param(sys_param_def *par) +{ + uint8_t *dst=(uint8_t *)&g_self.param; + uint8_t *src=(uint8_t *)par; + int size=sizeof(sys_param_def); + memcpy(dst,src,size); + return 0; +} + + +const sys_param_def *sys_param(void) +{ + return &g_self.param; +} + + + +const scheme_def *check_scheme(void) +{ + return 0; +} + + + + diff --git a/source/dev/dev_flash.h b/source/dev/dev_flash.h new file mode 100644 index 0000000..b105cd5 --- /dev/null +++ b/source/dev/dev_flash.h @@ -0,0 +1,76 @@ +#ifndef dev_flash_h__ +#define dev_flash_h__ + + +#include "stdint.h" + + + + + + + + + + + + +// 系统参数 +__packed +typedef struct{ + char pack_time[20];// 打包时间 + char host_if[8];// 主机接口 + char device_type[12];// 设备类型 ,批检仪还是赋码仪 + uint8_t mac[8];// mac地址 + uint8_t local_ip[4];// 本机ip地址 + uint8_t host_ip[4];// 主机ip地址 + int host_port;// 主机端口 + int local_cmd_port;// 本机命令端口 + int host_log_port;// 主机log端口 + int local_id;// 本机识别号 + uint8_t host_log_ip[4];// 主机logip + int uartbsp;// 串口4的波特率 + int coder_ret_mode;// 注码指令返回格式 + int slave_addr_start;// 小板起始地址 + int moter_max_count;// 电机最大步数 +}sys_param_def; + + + +int flash_save_param(sys_param_def *par); +const sys_param_def *sys_param(void); + + +__packed +typedef struct{ + uint32_t max; + uint32_t min; + uint32_t err; +}scheme_range_def; + + + +__packed +typedef struct +{ + uint32_t item_num; + uint32_t err; + scheme_range_def range[16]; +}scheme_task_def; + +// 方案参数 +__packed +typedef struct{ + uint8_t slave_data[2048]; + uint32_t plan_id; + uint32_t timeout_m; + uint32_t task_num; + scheme_task_def task[0]; +}scheme_def; + + +const scheme_def *check_scheme(void); + + +#endif + diff --git a/source/interface/if_gpioin.c b/source/interface/if_gpioin.c new file mode 100644 index 0000000..437e2f1 --- /dev/null +++ b/source/interface/if_gpioin.c @@ -0,0 +1,125 @@ + +#include "board.h" +#include "lock_resource.h" + + + + +// PG3 KEY +// PE6,PH9,PE1,PH8 拨码 +// 输入脚 + +#define GPIO_Initer() {.Mode=GPIO_MODE_INPUT,\ + .Pull=GPIO_PULLUP \ +} + + +typedef struct{ + const char *name; + void (*gpio_clock_fun)(void); + GPIO_TypeDef *gpio_base; + uint16_t gpio_pin; +}gpioin_dtb; + +static void rcc_gpioe_enable(void) +{ + __HAL_RCC_GPIOE_CLK_ENABLE(); +} +static void rcc_gpioh_enable(void) +{ + __HAL_RCC_GPIOH_CLK_ENABLE(); +} +static void rcc_gpiog_enable(void) +{ + __HAL_RCC_GPIOG_CLK_ENABLE(); +} + + +static const gpioin_dtb g_dtb[]={ + { + .name="gpioin1", + .gpio_clock_fun=rcc_gpioe_enable, + .gpio_base=GPIOE, + .gpio_pin=GPIO_PIN_6, + }, + { + .name="gpioin2", + .gpio_clock_fun=rcc_gpioh_enable, + .gpio_base=GPIOH, + .gpio_pin=GPIO_PIN_9, + }, + { + .name="gpioin3", + .gpio_clock_fun=rcc_gpioe_enable, + .gpio_base=GPIOE, + .gpio_pin=GPIO_PIN_1, + }, + { + .name="gpioin4", + .gpio_clock_fun=rcc_gpioh_enable, + .gpio_base=GPIOH, + .gpio_pin=GPIO_PIN_8, + }, + { + .name="key", + .gpio_clock_fun=rcc_gpiog_enable, + .gpio_base=GPIOG, + .gpio_pin=GPIO_PIN_3, + }, +}; + + +typedef struct{ + const gpioin_dtb *dtb; +}self_data; + + +static self_data g_self[LENGTH(g_dtb)]; + + +def_find_fun(gpioin_dtb,g_dtb) + + + + +static int init(gpioin_def *g) +{ + param_check(g); + if(g->private_data) return 0; + GPIO_InitTypeDef init=GPIO_Initer(); + int index; + const gpioin_dtb *dtb=find(g->name,&index); + self_data *self=&g_self[index]; + self->dtb=dtb; + g->private_data=self; + + dtb->gpio_clock_fun(); + init.Pin = 1<gpio_pin; + HAL_GPIO_Init(dtb->gpio_base, &init); + return 0; +} + + +static int deinit(gpioin_def *g) +{ + return 0; +} + + +static int state(gpioin_def *g) +{ + param_check(g); + param_check(g->private_data); + self_data *self=g->private_data; + return HAL_GPIO_ReadPin(self->dtb->gpio_base,self->dtb->gpio_pin)==GPIO_PIN_RESET?0:1; +} + + + +gpioin_init_export(gpioin1,init,deinit,state,0) +gpioin_init_export(gpioin2,init,deinit,state,0) +gpioin_init_export(gpioin3,init,deinit,state,0) +gpioin_init_export(gpioin4,init,deinit,state,0) +gpioin_init_export(key,init,deinit,state,0) + + diff --git a/source/interface/if_pwm.c b/source/interface/if_pwm.c index fc16f6d..a25934f 100644 --- a/source/interface/if_pwm.c +++ b/source/interface/if_pwm.c @@ -2,10 +2,12 @@ #include "debug.h" #include "lock_resource.h" -// 使用定时器14 +// 使用定时器14,13 // PF7,8,9 是输出口 // 使用PF7来产生脉冲,PF8来改变方向 +// PI6 是输入口 +// 使用PI6来捕获到位中断 typedef struct{ int tick; @@ -20,6 +22,7 @@ typedef struct{ typedef struct{ TIM_HandleTypeDef htim; TIM_HandleTypeDef htim2; + EXTI_HandleTypeDef hexti; int count_all; int count_past; int fre; @@ -32,6 +35,7 @@ typedef struct{ static self_def g_self; +static void Exti14FallingCb(void); static int init(pwm_def *p) { @@ -67,13 +71,37 @@ static int init(pwm_def *p) GPIO_InitTypeDef init={0}; __HAL_RCC_GPIOF_CLK_ENABLE(); - init.Mode = GPIO_MODE_INPUT; + init.Mode = GPIO_MODE_OUTPUT_PP; init.Pull = GPIO_NOPULL; init.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9; PERIPH_LOCK(GPIOF); HAL_GPIO_Init(GPIOF, &init); PERIPH_UNLOCK(GPIOF); + + __HAL_RCC_GPIOI_CLK_ENABLE(); + init.Mode = GPIO_MODE_INPUT; + init.Pull = GPIO_NOPULL; + init.Pin = GPIO_PIN_6; + PERIPH_LOCK(GPIOI); + HAL_GPIO_Init(GPIOI, &init); + PERIPH_UNLOCK(GPIOI); + EXTI_ConfigTypeDef init2={0}; + init2.Line = EXTI_LINE_14; + init2.Trigger = EXTI_TRIGGER_FALLING; + init2.GPIOSel = EXTI_GPIOA; + init2.Mode = EXTI_MODE_C2_INTERRUPT; + PERIPH_LOCK(EXTI); + HAL_EXTI_SetConfigLine(&s->hexti, &init2); + PERIPH_UNLOCK(EXTI); + + /* Register callback to treat Exti interrupts in user Exti14FallingCb function */ + HAL_EXTI_RegisterCallback(&s->hexti, HAL_EXTI_FALLING_CB_ID, Exti14FallingCb); + + /* Enable and set line 14 Interrupt to the lowest priority */ + HAL_NVIC_SetPriority(EXTI6_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(EXTI6_IRQn); + return 0; } @@ -88,7 +116,7 @@ static int start(pwm_def *p,int step_count) param_check(p->private_data); self_def *self=p->private_data; if(step_count==0) return 0; - if((*self->dtb->bitmap_pin_zero==0)&&(step_count<0)) + if((HAL_GPIO_ReadPin(GPIOI,6)==GPIO_PIN_RESET)&&(step_count<0)) { // 到达零点后不能继续上升 if(self->end_irq) @@ -96,10 +124,10 @@ static int start(pwm_def *p,int step_count) return -1; } if(step_count>0) - *self->dtb->bitmap_pin_dir=0; + HAL_GPIO_WritePin(GPIOF,8,GPIO_PIN_RESET); else{ step_count=-step_count; - *self->dtb->bitmap_pin_dir=1; + HAL_GPIO_WritePin(GPIOF,8,GPIO_PIN_SET); } if(self->fre==0) { @@ -196,18 +224,14 @@ pwm_init_export(pwm,init,deinit,start,stop,set_fre,set_irq_fun,0) static inline void self_irq(self_def *self) { rt_interrupt_enter(); - volatile uint32_t *pin=self->dtb->bitmap_pin; - if(TIM_GetFlagStatus(self->dtb->tim,TIM_FLAG_Update)) + + HAL_GPIO_TogglePin(GPIOF,7); + irq_disable(); + self->count_past++; + irq_enable(); + if(self->count_all>0&&(self->count_all<=self->count_past)) { - TIM_ClearFlag(self->dtb->tim,TIM_FLAG_Update); - *pin=!(*pin); - irq_disable(); - self->count_past++; - irq_enable(); - if(self->count_all>0&&(self->count_all<=self->count_past)) - { - self_stop__(self); - } + self_stop__(self); } rt_interrupt_leave(); } @@ -249,13 +273,8 @@ static int calc_fre(self_def *self) static inline void self_irq2(self_def *self) { rt_interrupt_enter(); - volatile uint32_t *pin=self->dtb->bitmap_pin; - if(TIM_GetFlagStatus(self->dtb->tim2,TIM_FLAG_Update)) - { - TIM_ClearFlag(self->dtb->tim2,TIM_FLAG_Update); - if(self->fre==0) - self_set_fre(self,calc_fre(self)); - } + if(self->fre==0) + self_set_fre(self,calc_fre(self)); rt_interrupt_leave(); } @@ -263,24 +282,35 @@ static inline void self_irq2(self_def *self) static inline void self_stop_irq(self_def *self) { rt_interrupt_enter(); - if(EXTI_GetFlagStatus(1<dtb->gpio_zero_pin)){ - irq_disable(); - self->count_past=0; - self->count_all=0; - irq_enable(); - self_stop__(self); - EXTI_ClearFlag(1<dtb->gpio_zero_pin); - } + irq_disable(); + self->count_past=0; + self->count_all=0; + irq_enable(); + self_stop__(self); rt_interrupt_leave(); } +static void Exti14FallingCb(void) +{ + self_def *s=&g_self; + self_stop_irq(s); +} void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { + self_def *s=&g_self; //DBG_LOG("timer updata."); + if(htim==&s->htim) + { + self_irq(s); + } + else if(htim==&s->htim2) + { + self_irq2(s); + } } @@ -319,5 +349,10 @@ void TIM13_IRQHandler(void) HAL_TIM_IRQHandler(&s->htim2); } +void EXTI6_IRQHandler(void) +{ + self_def *s=&g_self; + HAL_EXTI_IRQHandler(&s->hexti); +} diff --git a/source/interface/if_vuart.c b/source/interface/if_vuart.c new file mode 100644 index 0000000..4123e7d --- /dev/null +++ b/source/interface/if_vuart.c @@ -0,0 +1,111 @@ +#include "board.h" +#include "lock_resource.h" +#include "openamp.h" +#include "string.h" + + + +// 与a7通信的虚拟串口 +// 系统启动的时候自动依次初始化,为了保证顺序 + + + + + +typedef struct{ + VIRT_UART_HandleTypeDef huart; + void (*irq_fun)(void *t,uint8_t d); + void *t; + void *mutex; +}self_def; + + +static self_def g_self[3]; + + +static int vuart_init(void) +{ + self_def *s=g_self; + + log_info("Virtual UART0 OpenAMP-rpmsg channel creation\r\n"); + if (VIRT_UART_Init(&s[0].huart) != VIRT_UART_OK) { + log_err("VIRT_UART_Init UART0 failed.\r\n"); + } + + log_info("Virtual UART1 OpenAMP-rpmsg channel creation\r\n"); + if (VIRT_UART_Init(&s[1].huart) != VIRT_UART_OK) { + log_err("VIRT_UART_Init UART1 failed.\r\n"); + } + + log_info("Virtual UART2 OpenAMP-rpmsg channel creation\r\n"); + if (VIRT_UART_Init(&s[2].huart) != VIRT_UART_OK) { + log_err("VIRT_UART_Init UART2 failed.\r\n"); + } +} + +app_init_export(vuart_init); + + + + +static int init(uart_def *u) +{ + self_def *s=g_self; + if(strcmp(u->name,"vuart0")==0) + { + u->private_data=&s[0].huart; + }else if(strcmp(u->name,"vuart1")==0) + { + u->private_data=&s[1].huart; + }else if(strcmp(u->name,"vuart2")==0) + { + u->private_data=&s[2].huart; + } + return 0; +} + + +static int deinit(uart_def *u) +{ + return 0; +} + + + +static int set_irq(uart_def *u,void (*irq)(void *t,uint8_t d),void *t) +{ + param_check(u); + param_check(u->private_data); + self_def *self=u->private_data; + irq_disable(); + self->irq_fun=irq; + + + self->t=t; + irq_enable(); + return 0; +} +static int read(uart_def *u,uint8_t *b,int len) +{ + param_check(u); + param_check(u->private_data); + return 0; +} +static int write(uart_def *u,const uint8_t *b,int len) +{ + param_check(u); + param_check(u->private_data); + self_def *self=u->private_data; + rt_mutex_take(self->mutex,RT_WAITING_FOREVER); + VIRT_UART_Transmit(&self->huart, (uint8_t *)b, len); + rt_mutex_release(self->mutex); + return len; +} + + + + + + + + diff --git a/source/rt_thread/board.h b/source/rt_thread/board.h index dc5d0b3..8d77bf1 100644 --- a/source/rt_thread/board.h +++ b/source/rt_thread/board.h @@ -5,7 +5,6 @@ #include "rtthread.h" #include #include "string.h" -#include "stm32mp1xx.h" struct dev_struct{ @@ -17,7 +16,7 @@ struct dev_struct{ typedef struct __uart_def{ const char *name; - int (*init)(struct __uart_def *u); + int (*init)(struct __uart_def *u,int bsp); int (*deinit)(struct __uart_def *u); int (*set_irq)(struct __uart_def *u,void (*irq)(void *t,uint8_t d),void *t); int (*set_end_irq)(struct __uart_def *u,uint8_t *rx_buff,int rx_buff_size, @@ -41,7 +40,13 @@ typedef struct __pwm_def{ }pwm_def; - +typedef struct __gpioin_def{ + const char *name; + int (*init)(struct __gpioin_def *u); + int (*deinit)(struct __gpioin_def *u); + int (*state)(struct __gpioin_def *u); + void *private_data; +}gpioin_def; @@ -89,7 +94,22 @@ typedef struct __pwm_def{ RT_USED static const struct dev_struct __dev_##name_ SECTION("devstruct")= \ { \ __dev_##name_##_name, \ - &_pwm_##name_, \ + &_pwm_##name_,\ + }; + +#define gpioin_init_export(name_,init_,deinit_,state_,priv_) \ + const static char __dev_##name_##_name[] SECTION(".rodata.devstr") = #name_; \ + RT_USED static gpioin_def _gpioin_##name_={\ + .name=__dev_##name_##_name,\ + .init=init_,\ + .deinit=deinit_,\ + .state=state_,\ + .private_data=priv_,\ + };\ + RT_USED static const struct dev_struct __dev_##name_ SECTION("devstruct")= \ + { \ + __dev_##name_##_name, \ + &_gpioin_##name_,\ }; @@ -132,7 +152,7 @@ void param_err_handle(const char *param,const char *file,const char *fun,int lin void cpy4byte(uint32_t *dst,uint32_t *src,int num_4byte); -// ���s==0,���ӡ +// 如果s==0,则打印 #define param_check(s) \ if((s)==0){\ param_err_handle(#s,__FILE__,__func__,__LINE__);} @@ -177,12 +197,12 @@ void __interrupt_enable(uint32_t level); #define MEM_ADDR(addr) *((volatile uint32_t *)(addr)) #define BIT_ADDR(addr, bitnum) (MEM_ADDR(BITBAND(addr, bitnum))) -// ��ȡ����Ĵ�����ַs=A~I +// 获取输出寄存器地址s=A~I #define GPIOx_ODR_ADDR(s) (GPIO##s##_BASE+20) -// ��ȡ����Ĵ�����ַs=A~I +// 获取输入寄存器地址s=A~I #define GPIOx_IDR_ADDR(s) (GPIO##s##_BASE+16) -// gpio�����s=A~I,n=0~15 +// gpio输出,s=A~I,n=0~15 #define PINOUT(s,n) BIT_ADDR(GPIOx_ODR_ADDR(s),n) #define PININ(s,n) BIT_ADDR(GPIOx_IDR_ADDR(s),n) diff --git a/source/task/commend.c b/source/task/commend.c new file mode 100644 index 0000000..d86715b --- /dev/null +++ b/source/task/commend.c @@ -0,0 +1,109 @@ +#include "commend.h" +#include "string.h" +#include "mystring.h" +#include "bytearray.h" +//#include "mystdlib.h" +#include "debug.h" +#include "board.h" + +#define CMD_RETURN_BUFF_SIZE 4096 + + + +typedef struct{ + char *data; +}self_def; + + + +static self_def g_self; +void *tappend(void *p,void *del); + + + +static commend_def *cmd_find(char *name) +{ + extern const int cmdstruct$$Base; + extern const int cmdstruct$$Limit; + commend_def *start=(commend_def *)&cmdstruct$$Base; + commend_def *end=(commend_def *)&cmdstruct$$Limit; + for(commend_def *t=start;tname,name)==0) + { + return t; + } + } + return 0; +} + + + + + +void cmd_recv_slot(void *obj,const char *coder,uint8_t cmd,array_def *data,char *err_str) +{ + self_def *s=&g_self; + commend_def *commd; + if((strcmp(err_str,"ok")!=0)||(cmd!=0)){ + return; + } + list_def *argv=str_split(str_simplified((const char *)arr_data(data)),' '); + DBG_LOG("list_str=%s",str_temp(list_string(argv))); + commd=cmd_find(list_get_str(argv,0)); + if(commd&&commd->fun) commd->fun(argv); + else cmd_print("unknown cmd of \"%s\"",list_get_str(argv,0)); +} + + +static int cmd_init(void) +{ + self_def *s=&g_self; + if(s->data==0){ + s->data=calloc(CMD_RETURN_BUFF_SIZE,sizeof(char)); + } + app_variable("cmd",s,0); + return 0; +} +app_init_export(cmd_init); + + +int cmd_print(const char *fmt,...) +{ + self_def *s=&g_self; + if(s->data==0) return 0; + va_list args; + size_t length=0; + va_start(args, fmt); + length += vsnprintf(&s->data[length], CMD_RETURN_BUFF_SIZE - length - 1, fmt, args); + if (length > CMD_RETURN_BUFF_SIZE - 1) + length = CMD_RETURN_BUFF_SIZE - 1; + va_end(args); + memcpy(&s->data[length],"\r\n",2); + length+=2; + s->data[length]=0; + array_def *arr=arr_creat(); + arr_appends(arr,s->data,length); + emit cmd_reply_signal(s,0,arr_temp(arr)); + return length; +} + + + + +static int cmd_help(list_def *argv) +{ + extern const int cmdstruct$$Base; + extern const int cmdstruct$$Limit; + commend_def *start=(commend_def *)&cmdstruct$$Base; + commend_def *end=(commend_def *)&cmdstruct$$Limit; + cmd_print("help ->"); + for(commend_def *t=start;tname,t->help); + } + return 0; +} + +commend_export(help,cmd_help,"print the help str of cmds.") + diff --git a/source/task/commend.h b/source/task/commend.h new file mode 100644 index 0000000..9d0223c --- /dev/null +++ b/source/task/commend.h @@ -0,0 +1,49 @@ +#ifndef commend_h__ +#define commend_h__ + + +#include "list.h" +#include "bytearray.h" +#include "signal.h" + + +// 执行命令函数不能调用延时函数 +typedef int (*cmd_fun_def)(list_def *argv /* str */); + + + +typedef struct{ + const char *name; + const char *help; + cmd_fun_def fun; +}commend_def; + + + + +#define commend_export(name_,fun_,help_) \ + const static char __cmd_##name_##_name[] SECTION(".rodata.cmdstr") = #name_; \ + const static char __cmd_##name_##_help[] SECTION(".rodata.cmdstr") = help_; \ + RT_USED static commend_def _cmd_##name_ SECTION("cmdstruct")= \ + {\ + .name=__cmd_##name_##_name,\ + .help=__cmd_##name_##_help,\ + .fun=fun_,\ + }; + + + + + +int cmd_print(const char *fmt,...); + + + +signal cmd_reply_signal(void *obj,uint8_t cmd,array_def *data); + +void cmd_recv_slot(void *obj,const char *codec_name,uint8_t cmd,array_def *data,char *err_str); + + + +#endif + diff --git a/source/task/key.c b/source/task/key.c new file mode 100644 index 0000000..ceffc10 --- /dev/null +++ b/source/task/key.c @@ -0,0 +1,95 @@ + +#include "board.h" +#include "rtthread.h" +#include "debug.h" +#include "string.h" +#include "dev_flash.h" +#include "commend.h" +#include "key.h" +#include "mystdlib.h" + + + +// 作为批检仪使用时读取按键 + + +typedef struct{ + int inited; + int run; + int input; + int input_lock; + void *tran; +}self_def; + + +static self_def g_self; + +static void request_check(void *p); + +static void key_thread(void *arg) +{ + self_def *s=arg; + gpioin_def *in1; + in1=dev_get("key"); + in1->init(in1); + s->input=0; + s->input_lock=1; + + while (s->run) + { + rt_thread_mdelay(20); + s->input=(in1->state(in1)); + if(s->input!=s->input_lock) + { + DBG_LOG("key=%08X",s->input); + s->input_lock=s->input; + if((s->input&1)==0) + { + // 发送按键按下消息 + cmd_print("key pressed"); + } + } + } + +} + +// 获取实时按键状态,1按下 +int key_pressed(void) +{ + self_def *s=&g_self; + return !s->input; +} + + +// cmd=0x37 +static void request_check(void *p) +{ + const sys_param_def *par=sys_param(); + array_def *d=arr_creat(); + emit key_send_signal(p,0x37,arr_temp(d)); + //DBG_LOG("tcp liver:%s",str_temp(arr_string(d))); +} + + + + + + + +static int init_thread(void) +{ + if(strcmp(sys_param()->device_type,"checker")==0) + { + self_def *s=&g_self; + s->inited=1; + s->run=1; + rt_thread_t rt_t=rt_thread_create("key_t",key_thread,s,1024,15,20); + rt_thread_startup(rt_t); + DBG_LOG("key thread created."); + } + return 0; +} +app_init_export(init_thread) + + + diff --git a/source/task/key.h b/source/task/key.h new file mode 100644 index 0000000..85e5685 --- /dev/null +++ b/source/task/key.h @@ -0,0 +1,28 @@ +#ifndef key_h__ +#define key_h__ + + +#include "rtthread.h" +#include "signal.h" +#include "bytearray.h" + + + + + +signal key_send_signal(void *obj,uint8_t cmd,array_def *data); + + + + +int key_pressed(void); + + + + + + + + + +#endif diff --git a/source/task/moter.c b/source/task/moter.c new file mode 100644 index 0000000..69b7098 --- /dev/null +++ b/source/task/moter.c @@ -0,0 +1,203 @@ +#include "moter.h" +#include "dev_flash.h" +#include "debug.h" +#include "commend.h" +#include "mymisc.h" +#include "board.h" + + +// ״̬ +typedef enum{ + STOP=0, + INITING=1, + DOWNING=2, + UPING=3, +}moter_stat; + + +typedef struct{ + int inited; + pwm_def *pwm; + rt_event_t event; + int run; + int want_count; + int count; + int max_count; + moter_stat stat; + void (*fun_in_end)(void *t); + void *t; +}self_def; + + +self_def g_self; + + + +#define MOTER_MAX_COUNT 22000 + +#define MOTER_END_EVENT 0x1 + + + + + + +static void end_irq(void *t) +{ + self_def *s=t; + if(s->inited) + rt_event_send(s->event,MOTER_END_EVENT); +} + + + +static void moter_run(void *t) +{ + self_def *s=t; + uint32_t ev; + while(s->run) + { + rt_event_recv(s->event,0xffffffff,RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR,RT_WAITING_FOREVER,&ev); + if(ev&MOTER_END_EVENT) + { + if(s->stat==INITING) + { + s->count=0; + }else if(s->stat==DOWNING) + { + s->count+=s->want_count; + }else if(s->stat==UPING) + { + // ʱ s->want_count ΪȻǼ + s->count+=s->want_count; + if(s->count<0) s->count=0; + } + s->stat=STOP; + DBG_LOG("moter stoped."); + emit moter_end_signal(s); + if(s->fun_in_end){ + s->fun_in_end(s->t); + s->fun_in_end=0; + s->t=0; + } + } + ev=0; + } +} + + + + +// ½ʱص +void moter_down(void (*fun)(void *t),void *t) +{ + self_def *s=&g_self; + if(fun){ + s->fun_in_end=fun; + s->t=t; + } + moter_start(0,s->max_count-s->count); +} + +// ʱص +void moter_up(void (*fun)(void *t),void *t) +{ + self_def *s=&g_self; + if(fun){ + s->fun_in_end=fun; + s->t=t; + } + moter_start(0,-s->count-100); +} + + +void moter_start(int fre,int step_count) +{ + self_def *s=&g_self; + if(s->inited==0) + { + DBG_WARN("moter object is not inited."); + return; + } + moter_start_slot(s,fre,step_count); +} + +void moter_start_slot(void *t,int fre,int step_count) +{ + self_def *s=t; + if(s->stat!=STOP) + { + DBG_WARN("moter is running!"); + return ; + } + if(s->count+step_count>s->max_count) + { + DBG_WARN("steps count exceeded limit!,max=%d",s->max_count); + end_irq(s); + return ; + } + else{ + DBG_LOG("s->count=%d,step_count=%d,s->max_count=%d",s->count,step_count,s->max_count); + } + if(step_count>0){ + s->stat=DOWNING; + }else if(step_count<0){ + s->stat=UPING; + }else{ + DBG_WARN("step_count can not == 0"); + return ; + } + s->want_count=step_count; + pwm_def *pwm=s->pwm; + pwm->set_fre(pwm,fre); + pwm->start(pwm,step_count); +} + + +void moter_init(void *t) +{ + self_def *s=t; + DBG_LOG("moter init to zero."); + pwm_def *pwm=s->pwm; + pwm->set_fre(pwm,2000); + pwm->start(pwm,-22000); +} + +int init_moter(void) +{ + self_def *s=&g_self; + const sys_param_def *par=sys_param(); + if(strcmp(par->device_type,"checker")==0){ + pwm_def *pwm=dev_get("pwm1"); + if(pwm==0){ + cmd_print("can not find device \"pwm1\""); + return -1; + } + pwm->init(pwm); + pwm->set_irq_fun(pwm,end_irq,s); + s->pwm=pwm; + s->stat=INITING; + if((par->moter_max_count>25000)||(par->moter_max_count<1000)) + { + s->max_count=MOTER_MAX_COUNT; + }else{ + s->max_count=par->moter_max_count; + } + s->count=s->max_count; + s->event=rt_event_create("moter_e",RT_IPC_FLAG_FIFO); + s->inited=1; + s->run=1; + rt_thread_t rt_t=rt_thread_create("moter_t",moter_run,s,2048,3,20); + rt_thread_startup(rt_t); + later_execute(moter_init,s,500); + app_variable("moter",s,0); + } + return 0; +} +app_init_export(init_moter) + + + + + + diff --git a/source/task/moter.h b/source/task/moter.h new file mode 100644 index 0000000..42c5613 --- /dev/null +++ b/source/task/moter.h @@ -0,0 +1,31 @@ +#ifndef moter_h__ +#define moter_h__ + + +#include "signal.h" +#include "bytearray.h" +#include "list.h" +#include "board.h" + + + + +signal moter_end_signal(void *m); + +void moter_start_slot(void *t,int fre,int step_count); + +void moter_start(int fre,int step_count); + +void moter_down(void (*fun)(void *t),void *t); + +void moter_up(void (*fun)(void *t),void *t); + + + + + + + + +#endif +