修复中断临界区保护的bug
This commit is contained in:
6
.vscode/settings.json
vendored
6
.vscode/settings.json
vendored
@@ -190,8 +190,10 @@
|
||||
"uart5.h": "c",
|
||||
"wk2xx_hw.h": "c",
|
||||
"cstdlib": "c",
|
||||
"locale": "c"
|
||||
"locale": "c",
|
||||
"uart4.h": "c",
|
||||
"u_queue.h": "c"
|
||||
},
|
||||
"cortex-debug.showRTOS": false,
|
||||
"cortex-debug.variableUseNaturalFormat": true,
|
||||
"cortex-debug.variableUseNaturalFormat": false,
|
||||
}
|
||||
@@ -69,6 +69,8 @@ add_custom_target(
|
||||
COMMAND
|
||||
cp mkrtos.elf ${CMAKE_SOURCE_DIR}/build/output/mkrtos.elf
|
||||
COMMAND
|
||||
${CMAKE_OBJDUMP} -s -S mkrtos.elf > ${CMAKE_SOURCE_DIR}/build/output/mkrtos.S
|
||||
COMMAND
|
||||
${CMAKE_SIZE} mkrtos.elf
|
||||
)
|
||||
add_dependencies(mkrtos.elf link_file_dump)
|
||||
|
||||
@@ -71,10 +71,6 @@ void HardFault_Handler(void)
|
||||
status = cpulock_lock();
|
||||
task_kill(thread_get_current_task());
|
||||
cpulock_set(status);
|
||||
/* Go to infinite loop when Hard Fault exception occurs */
|
||||
// while (1)
|
||||
// {
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,15 +96,8 @@ void MemManage_Handler(void)
|
||||
if (SCB->CFSR & 128)
|
||||
{
|
||||
fault_addr = (addr_t)(SCB->MMFAR);
|
||||
// printk("%x\t", SCB->MMFAR);
|
||||
|
||||
if (mm_page_alloc_fault(&cur_task->mm_space.mm_pages, fault_addr) == NULL)
|
||||
{
|
||||
printk("semgement fault.\n");
|
||||
/*TODO:杀死进程*/
|
||||
// while (1)
|
||||
// {
|
||||
// }
|
||||
goto end;
|
||||
}
|
||||
return;
|
||||
@@ -120,11 +109,6 @@ void MemManage_Handler(void)
|
||||
|
||||
if (mm_page_alloc_fault(&cur_task->mm_space.mm_pages, fault_addr) == NULL)
|
||||
{
|
||||
printk("semgement fault.\n");
|
||||
/*TODO:杀死进程*/
|
||||
// while (1)
|
||||
// {
|
||||
// }
|
||||
goto end;
|
||||
}
|
||||
return;
|
||||
@@ -136,8 +120,6 @@ void MemManage_Handler(void)
|
||||
|
||||
if (mm_page_alloc_fault(&cur_task->mm_space.mm_pages, fault_addr) == NULL)
|
||||
{
|
||||
printk("semgement fault.\n");
|
||||
/*TODO:杀死进程*/
|
||||
goto end;
|
||||
}
|
||||
return;
|
||||
@@ -147,12 +129,8 @@ void MemManage_Handler(void)
|
||||
printk("浮点惰性压栈错误\n");
|
||||
}
|
||||
|
||||
printk("semgement fault.\n");
|
||||
/*TODO:杀死进程*/
|
||||
// while (1)
|
||||
// {
|
||||
// }
|
||||
end:
|
||||
printk("semgement fault.\n");
|
||||
status = cpulock_lock();
|
||||
task_kill(thread_get_current_task());
|
||||
cpulock_set(status);
|
||||
@@ -165,9 +143,11 @@ end:
|
||||
*/
|
||||
void BusFault_Handler(void)
|
||||
{
|
||||
thread_t *cur_th = thread_get_current();
|
||||
|
||||
umword_t status;
|
||||
printk("%s\n", __FUNCTION__);
|
||||
|
||||
cur_th = cur_th;
|
||||
/* Go to infinite loop when Bus Fault exception occurs */
|
||||
// while (1)
|
||||
// {
|
||||
@@ -209,10 +189,6 @@ void UsageFault_Handler(void)
|
||||
{
|
||||
printk("Division by zero error\n");
|
||||
}
|
||||
/* Go to infinite loop when Usage Fault exception occurs */
|
||||
// while (1)
|
||||
// {
|
||||
// }
|
||||
umword_t status;
|
||||
status = cpulock_lock();
|
||||
task_kill(thread_get_current_task());
|
||||
|
||||
@@ -32,6 +32,7 @@ syscall_ret_user:
|
||||
pop {r0,r1,r2,lr}
|
||||
|
||||
msr CONTROL, r1
|
||||
CPSIE I
|
||||
bx lr
|
||||
|
||||
/**
|
||||
@@ -41,6 +42,7 @@ syscall_ret_user:
|
||||
.global SVC_Handler
|
||||
.type SVC_Handler, %function
|
||||
SVC_Handler:
|
||||
CPSID I
|
||||
TST.W LR, #4
|
||||
ITE EQ
|
||||
MRSEQ R1, MSP
|
||||
@@ -104,7 +106,9 @@ SYS_CALL:
|
||||
isb
|
||||
orr lr, lr, #0x18
|
||||
bic lr, lr, #0x04
|
||||
CPSIE I
|
||||
bx lr
|
||||
|
||||
TO_END:
|
||||
CPSIE I
|
||||
bx lr
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
/*
|
||||
* @Author: zhangzheng 1358745329@qq.com
|
||||
* @Date: 2023-08-18 15:03:16
|
||||
* @LastEditors: zhangzheng 1358745329@qq.com
|
||||
* @LastEditTime: 2023-08-18 16:31:06
|
||||
* @FilePath: /mkrtos-real/mkrtos_knl/arch/inc/arch.h
|
||||
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
||||
/**
|
||||
* @file arch.h
|
||||
* @author zhangzheng (1358745329@qq.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-10-04
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ umword_t sys_tick_cnt_get(void)
|
||||
|
||||
void SysTick_Handler(void)
|
||||
{
|
||||
|
||||
// 进行上下文切换
|
||||
thread_sched();
|
||||
timeout_times_tick();
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
#include "mm_man.h"
|
||||
#include "ipc.h"
|
||||
static mem_t global_mem; //!< 全局内存管理块
|
||||
static uint8_t mem_block[58 * 1024]; //!< 内核内存分配堆 TODO:自动识别大小,或者从bootstrap中读取
|
||||
static uint8_t mem_block[55 * 1024]; //!< 内核内存分配堆 TODO:自动识别大小,或者从bootstrap中读取
|
||||
static kobject_t *kobj_ls[FACTORY_FUNC_MAX]; //!< 全局静态内核对象
|
||||
|
||||
void global_reg_kobj(kobject_t *kobj, int inx)
|
||||
|
||||
@@ -26,7 +26,8 @@ static void print_raw(const char *str)
|
||||
{
|
||||
for (int i = 0; str[i]; i++)
|
||||
{
|
||||
xputc(str[i]);
|
||||
// xputc(str[i]);
|
||||
uart_putc(uart_get_global(), str[i]);
|
||||
}
|
||||
}
|
||||
/**
|
||||
@@ -36,7 +37,8 @@ static void print_raw(const char *str)
|
||||
*/
|
||||
void putc(int c)
|
||||
{
|
||||
xputc(c);
|
||||
// xputc(c);
|
||||
uart_putc(uart_get_global(), c);
|
||||
}
|
||||
int getc(void)
|
||||
{
|
||||
|
||||
@@ -33,12 +33,16 @@ void scheduler_init(void)
|
||||
{
|
||||
for (int i = 0; i < PRIO_MAX; i++)
|
||||
{
|
||||
slist_init(&scheduler.prio_list[i]);
|
||||
slist_init(&(scheduler.prio_list[i]));
|
||||
}
|
||||
}
|
||||
INIT_HIGH_HAD(scheduler_init);
|
||||
void scheduler_add(sched_t *node)
|
||||
{
|
||||
thread_t *node_th = container_of(node, thread_t, sche);
|
||||
|
||||
assert(node_th->magic == THREAD_MAIGC);
|
||||
|
||||
scheduler_t *sched = scheduler_get_current();
|
||||
assert(node->prio >= 0);
|
||||
assert(node->prio < PRIO_MAX);
|
||||
@@ -50,17 +54,25 @@ void scheduler_add(sched_t *node)
|
||||
}
|
||||
|
||||
MK_SET_BIT(sched->bitmap[node->prio / PRIO_MAX], node->prio);
|
||||
slist_add(&sched->prio_list[node->prio], &node->node);
|
||||
|
||||
slist_add(&(sched->prio_list[node->prio]), &node->node);
|
||||
}
|
||||
void scheduler_del(sched_t *node)
|
||||
{
|
||||
scheduler_t *sched = scheduler_get_current();
|
||||
thread_t *cur_th = thread_get_current();
|
||||
|
||||
assert(node->prio >= 0);
|
||||
assert(node->prio < PRIO_MAX);
|
||||
slist_del(&node->node);
|
||||
// slist_init(&node->node);
|
||||
// if (node == &cur_th->sche)
|
||||
// { // 删除的是当前的,重新调度
|
||||
// sched->cur_sche = NULL;
|
||||
// }
|
||||
if (slist_is_empty(&sched->prio_list[node->prio]))
|
||||
{
|
||||
MK_CLR_BIT(sched->bitmap[node->prio / PRIO_MAX], node->prio);
|
||||
MK_CLR_BIT(sched->bitmap[node->prio / PRIO_MAX], node->prio % PRIO_MAX);
|
||||
for (mword_t i = (PRIO_MAX / WORD_BITS - 1); i >= 0; i--)
|
||||
{
|
||||
int ret = clz(sched->bitmap[i]);
|
||||
@@ -88,12 +100,13 @@ sched_t *scheduler_next(void)
|
||||
else
|
||||
{
|
||||
next = sche->cur_sche->node.next;
|
||||
if (next == &sche->prio_list[sche->max_prio])
|
||||
if (next == &(sche->prio_list[sche->max_prio]))
|
||||
{
|
||||
next = slist_first(&sche->prio_list[sche->max_prio]);
|
||||
}
|
||||
}
|
||||
next_sch = container_of(next, sched_t, node);
|
||||
assert(next_sch->prio < PRIO_MAX);
|
||||
sche->cur_sche = next_sch;
|
||||
return next_sch;
|
||||
}
|
||||
@@ -105,6 +118,8 @@ sp_info_t *schde_to(void *usp, void *ksp, umword_t sp_type)
|
||||
sched_t *next = sche->cur_sche;
|
||||
thread_t *next_th = container_of(next, thread_t, sche);
|
||||
|
||||
assert(next_th->magic == THREAD_MAIGC);
|
||||
|
||||
if (sched_reset)
|
||||
{
|
||||
thread_t *cur_th = thread_get_current();
|
||||
|
||||
@@ -221,7 +221,7 @@ static void task_release_stage2(kobject_t *kobj)
|
||||
{
|
||||
task_t *tk = container_of(kobj, task_t, kobj);
|
||||
kobj_del_list_t kobj_list;
|
||||
task_t *cur_tk = thread_get_current_task();
|
||||
// task_t *cur_tk = thread_get_current_task();
|
||||
|
||||
obj_space_release(&tk->obj_space, tk->lim);
|
||||
mm_limit_free_align(tk->lim, tk->mm_space.mm_block, tk->mm_space.mm_block_size);
|
||||
|
||||
@@ -175,15 +175,19 @@ void thread_dead(thread_t *th)
|
||||
*/
|
||||
void thread_sched(void)
|
||||
{
|
||||
umword_t status = cpulock_lock();
|
||||
sched_t *next_sche = scheduler_next();
|
||||
thread_t *th = thread_get_current();
|
||||
|
||||
assert(th->magic == THREAD_MAIGC);
|
||||
if (next_sche == &th->sche)
|
||||
{
|
||||
cpulock_set(status);
|
||||
|
||||
return;
|
||||
}
|
||||
to_sche();
|
||||
cpulock_set(status);
|
||||
}
|
||||
/**
|
||||
* @brief 线程进入就绪态
|
||||
@@ -293,11 +297,13 @@ static void thread_syscall(kobject_t *kobj, syscall_prot_t sys_p, msg_tag_t in_t
|
||||
break;
|
||||
case RUN_THREAD:
|
||||
{
|
||||
umword_t status = cpulock_lock();
|
||||
if (!slist_in_list(&tag_th->sche.node))
|
||||
{
|
||||
tag_th->sche.prio = (f->r[1] >= PRIO_MAX ? PRIO_MAX - 1 : f->r[1]);
|
||||
thread_ready(tag_th, TRUE);
|
||||
}
|
||||
cpulock_set(status);
|
||||
tag = msg_tag_init4(0, 0, 0, 0);
|
||||
}
|
||||
break;
|
||||
@@ -355,8 +361,12 @@ INIT_KOBJ(thread_factory_register);
|
||||
*/
|
||||
task_t *thread_get_current_task(void)
|
||||
{
|
||||
thread_t *cur = thread_get_current();
|
||||
kobject_t *kobj = cur->task;
|
||||
|
||||
assert(kobj);
|
||||
return container_of(
|
||||
thread_get_current()->task, task_t, kobj);
|
||||
kobj, task_t, kobj);
|
||||
}
|
||||
task_t *thread_get_task(thread_t *th)
|
||||
{
|
||||
|
||||
@@ -14,3 +14,4 @@ add_subdirectory(stm32f1_bsp)
|
||||
add_subdirectory(libc_backend)
|
||||
add_subdirectory(mlibc)
|
||||
add_subdirectory(cpio)
|
||||
add_subdirectory(util)
|
||||
|
||||
@@ -10,9 +10,10 @@ typedef struct sys_info
|
||||
|
||||
msg_tag_t sys_read_info(obj_handler_t obj, sys_info_t *info);
|
||||
|
||||
#define sys_read_tick() ({ \
|
||||
sys_info_t info; \
|
||||
\
|
||||
sys_read_info(SYS_PROT, &info); \
|
||||
info.sys_tick; \
|
||||
})
|
||||
static inline umword_t sys_read_tick(void)
|
||||
{
|
||||
sys_info_t info;
|
||||
|
||||
sys_read_info(SYS_PROT, &info);
|
||||
return info.sys_tick;
|
||||
}
|
||||
|
||||
@@ -46,10 +46,124 @@ RPC_GENERATION_OP2(ns_t, NS_QUERY_OP, query,
|
||||
return ret;
|
||||
}
|
||||
|
||||
RPC_GENERATION_DISPATCH2(ns_t, NS_QUERY_OP, query,
|
||||
rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_IN, RPC_TYPE_DATA, path,
|
||||
rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_INOUT, RPC_TYPE_BUF, cli_hd)
|
||||
|
||||
// RPC_GENERATION_DISPATCH2(ns_t, NS_QUERY_OP, query,
|
||||
// rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_IN, RPC_TYPE_DATA, path,
|
||||
// rpc_obj_handler_t_t, rpc_obj_handler_t_t, RPC_DIR_INOUT, RPC_TYPE_BUF, cli_hd)
|
||||
msg_tag_t ns_t_query_dispatch(ns_t *obj, msg_tag_t tag, ipc_msg_t *ipc_msg)
|
||||
{
|
||||
rpc_array_uint32_t_uint8_t_32_t var0;
|
||||
rpc_obj_handler_t_t var1;
|
||||
size_t op_val;
|
||||
uint8_t *value = (uint8_t *)(ipc_msg->msg_buf);
|
||||
int off = 0;
|
||||
rpc_var_rpc_array_uint32_t_uint8_t_32_t_init(&var0);
|
||||
rpc_var_rpc_obj_handler_t_t_init(&var1);
|
||||
op_val = *((typeof(((uint16_t)1)) *)value);
|
||||
if (op_val != ((uint16_t)1))
|
||||
{
|
||||
return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (-100)});
|
||||
}
|
||||
off += sizeof(typeof(((uint16_t)1)));
|
||||
off = rpc_align(off, __alignof(typeof(((uint16_t)1))));
|
||||
do
|
||||
{
|
||||
if (1 == 1)
|
||||
{
|
||||
if (1 == 1 || 1 == 4)
|
||||
{
|
||||
int ret = rpc_svr_buf_to_msg_rpc_array_uint32_t_uint8_t_32_t(&var0, (uint8_t *)(value), off, tag.msg_buf_len * (sizeof(void *)));
|
||||
if (ret < 0)
|
||||
{
|
||||
return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret)});
|
||||
}
|
||||
off = ret;
|
||||
}
|
||||
}
|
||||
} while (0);
|
||||
do
|
||||
{
|
||||
if (2 == 1)
|
||||
{
|
||||
if (4 == 1 || 4 == 4)
|
||||
{
|
||||
int ret = rpc_svr_buf_to_msg_rpc_obj_handler_t_t(&var1, (uint8_t *)(value), off, tag.msg_buf_len * (sizeof(void *)));
|
||||
if (ret < 0)
|
||||
{
|
||||
return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret)});
|
||||
}
|
||||
off = ret;
|
||||
}
|
||||
}
|
||||
} while (0);
|
||||
short ret_val = ns_t_query_op(obj, &var0, &var1);
|
||||
if (ret_val < 0)
|
||||
{
|
||||
return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret_val)});
|
||||
}
|
||||
off = 0;
|
||||
int off_map = 0;
|
||||
do
|
||||
{
|
||||
if (1 == 1)
|
||||
{
|
||||
if (1 == 2 || 1 == 4)
|
||||
{
|
||||
int ret = rpc_svr_msg_to_buf_rpc_array_uint32_t_uint8_t_32_t(&var0, (uint8_t *)(value), off);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret)});
|
||||
}
|
||||
off = ret;
|
||||
}
|
||||
}
|
||||
} while (0);
|
||||
do
|
||||
{
|
||||
if (2 == 1)
|
||||
{
|
||||
if (4 == 2 || 4 == 4)
|
||||
{
|
||||
int ret = rpc_svr_msg_to_buf_rpc_obj_handler_t_t(&var1, (uint8_t *)(value), off);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret)});
|
||||
}
|
||||
off = ret;
|
||||
}
|
||||
}
|
||||
} while (0);
|
||||
do
|
||||
{
|
||||
if (1 == 2)
|
||||
{
|
||||
if (1 == 2 || 1 == 4)
|
||||
{
|
||||
int ret = rpc_svr_msg_to_buf_rpc_array_uint32_t_uint8_t_32_t(&var0, (uint8_t *)(ipc_msg->map_buf), off_map);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret)});
|
||||
}
|
||||
off_map = ret;
|
||||
}
|
||||
}
|
||||
} while (0);
|
||||
do
|
||||
{
|
||||
if (2 == 2)
|
||||
{
|
||||
if (4 == 2 || 4 == 4)
|
||||
{
|
||||
int ret = rpc_svr_msg_to_buf_rpc_obj_handler_t_t(&var1, (uint8_t *)(ipc_msg->map_buf), off_map);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret)});
|
||||
}
|
||||
off_map = ret;
|
||||
}
|
||||
}
|
||||
} while (0);
|
||||
return ((msg_tag_t){.flags = (0), .msg_buf_len = ((((off) / ((sizeof(void *)))) + (((off) % ((sizeof(void *)))) ? 1 : 0))), .map_buf_len = ((((off_map) / ((sizeof(void *)))) + (((off_map) % ((sizeof(void *)))) ? 1 : 0))), .prot = (ret_val)});
|
||||
}
|
||||
RPC_DISPATCH2(ns_t, typeof(NS_REGISTER_OP), NS_REGISTER_OP, register, NS_QUERY_OP, query)
|
||||
|
||||
void ns_init(ns_t *ns)
|
||||
|
||||
@@ -20,6 +20,7 @@ target_include_directories(
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/sys_util/inc
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/sys/inc
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/cpio
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/util/inc
|
||||
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/arch/arm/
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/arch/generic
|
||||
|
||||
@@ -286,8 +286,97 @@ RPC_TYPE_DEF_ALL(int) //!< 定义所有的
|
||||
* @brief 定义一个32字节长度的数组
|
||||
*
|
||||
*/
|
||||
RPC_ARRAY_DEF(uint32_t, uint8_t, 32)
|
||||
|
||||
// RPC_ARRAY_DEF(uint32_t, uint8_t, 32)
|
||||
typedef struct rpc_array_uint32_t_uint8_t_32
|
||||
{
|
||||
uint32_t len;
|
||||
uint8_t data[32];
|
||||
} rpc_array_uint32_t_uint8_t_32_t;
|
||||
static inline int rpc_svr_msg_to_buf_rpc_array_uint32_t_uint8_t_32_t(rpc_array_uint32_t_uint8_t_32_t *d, uint8_t *buf, int len)
|
||||
{
|
||||
if (rpc_align(len, __alignof(d->len)) > 96)
|
||||
{
|
||||
return -1024;
|
||||
}
|
||||
len = rpc_align(len, __alignof(d->len));
|
||||
*((typeof(d->len) *)(&buf[len])) = d->len;
|
||||
if (rpc_align(len, __alignof(d->data[0]) + d->len * sizeof(d->data[0])) > 96)
|
||||
{
|
||||
return -1024;
|
||||
}
|
||||
len += sizeof(d->len);
|
||||
len = rpc_align(len, __alignof(d->data[0]));
|
||||
for (int i = 0; i < d->len * sizeof(d->data[0]); i++)
|
||||
{
|
||||
buf[i + len] = ((uint8_t *)(d->data))[i];
|
||||
}
|
||||
len += d->len * sizeof(d->data[0]);
|
||||
return len;
|
||||
}
|
||||
static inline int rpc_svr_buf_to_msg_rpc_array_uint32_t_uint8_t_32_t(rpc_array_uint32_t_uint8_t_32_t *d, uint8_t *buf, int len, int max)
|
||||
{
|
||||
if (rpc_align(len, __alignof(d->len)) > max)
|
||||
{
|
||||
return -1024;
|
||||
}
|
||||
len = rpc_align(len, __alignof(d->len));
|
||||
d->len = *((typeof(d->len) *)(&buf[len]));
|
||||
if (rpc_align(len, __alignof(d->data[0]) + d->len * sizeof(d->data[0])) > max)
|
||||
{
|
||||
return -1024;
|
||||
}
|
||||
len += sizeof(d->len);
|
||||
len = rpc_align(len, __alignof(d->data[0]));
|
||||
for (int i = 0; i < d->len * sizeof(d->data[0]); i++)
|
||||
{
|
||||
((uint8_t *)(d->data))[i] = buf[i + len];
|
||||
}
|
||||
len += d->len * sizeof(d->data[0]);
|
||||
return len;
|
||||
}
|
||||
static inline int rpc_cli_msg_to_buf_rpc_array_uint32_t_uint8_t_32_t(rpc_array_uint32_t_uint8_t_32_t *d, uint8_t *buf, int len)
|
||||
{
|
||||
if (rpc_align(len, __alignof(d->len)) > 96)
|
||||
{
|
||||
return -1024;
|
||||
}
|
||||
len = rpc_align(len, __alignof(d->len));
|
||||
*((typeof(d->len) *)(&buf[len])) = d->len;
|
||||
if (rpc_align(len, __alignof(d->data[0]) + d->len * sizeof(d->data[0])) > 96)
|
||||
{
|
||||
return -1024;
|
||||
}
|
||||
len += sizeof(d->len);
|
||||
len = rpc_align(len, __alignof(d->data[0]));
|
||||
for (int i = 0; i < d->len * sizeof(d->data[0]); i++)
|
||||
{
|
||||
buf[i + len] = ((uint8_t *)(d->data))[i];
|
||||
}
|
||||
len += d->len * sizeof(d->data[0]);
|
||||
return len;
|
||||
}
|
||||
static inline int rpc_cli_buf_to_msg_rpc_array_uint32_t_uint8_t_32_t(rpc_array_uint32_t_uint8_t_32_t *d, uint8_t *buf, int len, int max)
|
||||
{
|
||||
if (rpc_align(len, __alignof(d->len)) > max)
|
||||
{
|
||||
return -1024;
|
||||
}
|
||||
len = rpc_align(len, __alignof(d->len));
|
||||
d->len = *((typeof(d->len) *)(&buf[len]));
|
||||
if (rpc_align(len, __alignof(d->data[0]) + d->len * sizeof(d->data[0])) > max)
|
||||
{
|
||||
return -1024;
|
||||
}
|
||||
len += sizeof(d->len);
|
||||
len = rpc_align(len, __alignof(d->data[0]));
|
||||
for (int i = 0; i < d->len * sizeof(d->data[0]); i++)
|
||||
{
|
||||
((uint8_t *)(d->data))[i] = buf[i + len];
|
||||
}
|
||||
len += d->len * sizeof(d->data[0]);
|
||||
return len;
|
||||
}
|
||||
static inline void rpc_var_rpc_array_uint32_t_uint8_t_32_t_init(rpc_array_uint32_t_uint8_t_32_t *d) {}
|
||||
/**
|
||||
* @brief 引用类型的数组定义,数组的数据来自其它地方
|
||||
*
|
||||
@@ -326,7 +415,7 @@ RPC_ARRAY_DEF(uint32_t, uint8_t, 32)
|
||||
len += d->len * sizeof(d->data[0]); \
|
||||
return len; \
|
||||
} \
|
||||
RPC_CLI_BUF_TO_MSG_WITHOUT_IMPL(rpc_ref_array_##len_type##_##data_type##_##length##_t, int) \
|
||||
RPC_CLI_BUF_TO_MSG_WITHOUT_IMPL(rpc_ref_array_##len_type##_##data_type##_##length##_t, int) \
|
||||
{ \
|
||||
if (rpc_align(len, __alignof(d->len)) > \
|
||||
max) \
|
||||
|
||||
5
mkrtos_user/lib/sys_util/inc/u_str.h
Normal file
5
mkrtos_user/lib/sys_util/inc/u_str.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
#include "u_queue.h"
|
||||
|
||||
void print_hex(char *buffer, int len);
|
||||
void print_q_hex(queue_t *q);
|
||||
@@ -24,5 +24,5 @@ void u_sleep_init(void)
|
||||
|
||||
void u_sleep_ms(size_t ms)
|
||||
{
|
||||
ipc_call(hd, msg_tag_init4(0, 0, 0, 0), ipc_timeout_create2((1000 / SYS_SCHE_HZ) * ms, 0));
|
||||
ipc_call(hd, msg_tag_init4(0, 0, 0, 0), ipc_timeout_create2(ms / (1000 / SYS_SCHE_HZ), 0));
|
||||
}
|
||||
|
||||
34
mkrtos_user/lib/sys_util/src/u_str.c
Normal file
34
mkrtos_user/lib/sys_util/src/u_str.c
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
#include "u_types.h"
|
||||
#include "u_queue.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void print_hex(char *buffer, int len)
|
||||
{
|
||||
int i;
|
||||
for (i = 1; i <= len; i++)
|
||||
{
|
||||
printf("0x%02X ", buffer[i - 1]);
|
||||
// if (i % 16 == 0)
|
||||
// {
|
||||
// printf("\n");
|
||||
// }
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
void print_q_hex(queue_t *q)
|
||||
{
|
||||
int i;
|
||||
for (i = 1; !q_empty(q); i++)
|
||||
{
|
||||
uint8_t d;
|
||||
|
||||
q_dequeue(q, &d);
|
||||
printf("0x%02X ", d);
|
||||
// if (i % 16 == 0)
|
||||
// {
|
||||
// printf("\n");
|
||||
// }
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
42
mkrtos_user/lib/util/CMakeLists.txt
Normal file
42
mkrtos_user/lib/util/CMakeLists.txt
Normal file
@@ -0,0 +1,42 @@
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
|
||||
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} \
|
||||
-fPIC -n -pie -fpie -fpic -msingle-pic-base -mno-pic-data-is-text-relative \
|
||||
-Wl,--gc-sections -D__dietlibc__ -D__arm__ -D__WORDSIZE=32 -D__ARM_ARCH_7M__ \
|
||||
" )
|
||||
set(CMAKE_ASM_FLAGS ${CMAKE_C_FLAGS})
|
||||
|
||||
|
||||
file(GLOB_RECURSE deps *.c *.S)
|
||||
add_library(
|
||||
util
|
||||
STATIC
|
||||
${deps}
|
||||
)
|
||||
target_include_directories(
|
||||
util
|
||||
PUBLIC
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/util/inc
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/sys/inc
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/cpio
|
||||
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/arch/arm/
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/arch/generic
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/obj/src/internal
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/src/include
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/src/internal
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/obj/include
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/include
|
||||
)
|
||||
target_link_libraries(
|
||||
util
|
||||
PUBLIC
|
||||
sys
|
||||
muslc
|
||||
cpio
|
||||
)
|
||||
add_dependencies(util sys)
|
||||
add_dependencies(util muslc)
|
||||
|
||||
|
||||
18
mkrtos_user/lib/util/inc/u_queue.h
Normal file
18
mkrtos_user/lib/util/inc/u_queue.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "u_types.h"
|
||||
|
||||
typedef struct queue
|
||||
{
|
||||
uint8_t *m;
|
||||
int front;
|
||||
int rear;
|
||||
int size;
|
||||
} queue_t;
|
||||
|
||||
void q_init(queue_t *q, uint8_t *data, int size);
|
||||
int q_empty(queue_t *q);
|
||||
int q_enqueue(queue_t *q, uint8_t e);
|
||||
int q_dequeue(queue_t *q, uint8_t *e);
|
||||
int q_queue_len(queue_t *q);
|
||||
void q_queue_clear(queue_t *q);
|
||||
52
mkrtos_user/lib/util/src/u_queue.c
Normal file
52
mkrtos_user/lib/util/src/u_queue.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "u_types.h"
|
||||
#include "u_queue.h"
|
||||
#include <assert.h>
|
||||
|
||||
void q_init(queue_t *q, uint8_t *data, int size)
|
||||
{
|
||||
q->front = 0;
|
||||
q->rear = 0;
|
||||
q->m = data;
|
||||
q->size = size;
|
||||
}
|
||||
int q_empty(queue_t *q)
|
||||
{
|
||||
assert(q);
|
||||
if (q->front == q->rear)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
int q_enqueue(queue_t *q, uint8_t e)
|
||||
{
|
||||
if (((q->rear + 1) % q->size) == q->front)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
q->m[q->rear] = e;
|
||||
q->rear = (q->rear + 1) % q->size;
|
||||
return 0;
|
||||
}
|
||||
int q_dequeue(queue_t *q, uint8_t *e)
|
||||
{
|
||||
if (q->front == q->rear)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
*e = q->m[q->front];
|
||||
q->front = (q->front + 1) % q->size;
|
||||
return 0;
|
||||
}
|
||||
int q_queue_len(queue_t *q)
|
||||
{
|
||||
return (q->rear - q->front + q->size) % q->size;
|
||||
}
|
||||
void q_queue_clear(queue_t *q)
|
||||
{
|
||||
q->front = 0;
|
||||
q->rear = 0;
|
||||
}
|
||||
@@ -16,6 +16,7 @@ target_link_libraries(app.elf
|
||||
sys
|
||||
sys_util
|
||||
sys_svr
|
||||
util
|
||||
xtinymodbus
|
||||
stm32f1_bsp
|
||||
${GCC_LIB_PATH}/libgcc.a
|
||||
@@ -51,6 +52,7 @@ target_include_directories(
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/stm32f1_bsp/core_inc
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/server/app/hmi_lcd
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/server/app/util
|
||||
${CMAKE_SOURCE_DIR}/mkrtos_user/lib/util/inc
|
||||
)
|
||||
add_dependencies(app.elf
|
||||
muslc
|
||||
@@ -80,4 +82,5 @@ add_custom_target(
|
||||
add_dependencies(app_dump app.elf)
|
||||
add_dependencies(app_dump sys)
|
||||
add_dependencies(app_dump sys_util)
|
||||
add_dependencies(app_dump util)
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
#include "hmi_lcd_process.h"
|
||||
#include "music_control.h"
|
||||
#include "MDM_RTU_APP.h"
|
||||
#include "e180-zg120.h"
|
||||
#include "timer.h"
|
||||
#include <assert.h>
|
||||
|
||||
void music2_send_bytes(u8 *bytes, int len)
|
||||
@@ -67,4 +69,11 @@ void drv_init(void)
|
||||
bluetooth_init_cfg(1, music2_send_bytes);
|
||||
|
||||
MDM_RTU_APPInit();
|
||||
|
||||
// timer_init();
|
||||
|
||||
mod_reset();
|
||||
mod_set_cod();
|
||||
mod_reset();
|
||||
mod_cfg_net();
|
||||
}
|
||||
|
||||
302
mkrtos_user/server/app/drv/e180-zg120.c
Normal file
302
mkrtos_user/server/app/drv/e180-zg120.c
Normal file
@@ -0,0 +1,302 @@
|
||||
#include "u_types.h"
|
||||
#include "uart4.h"
|
||||
#include "u_sys.h"
|
||||
#include "delay.h"
|
||||
#include "u_str.h"
|
||||
#include "u_queue.h"
|
||||
|
||||
#define TYPE_CFG 0x00
|
||||
#define TYPE_ZDO_REQ 0x01
|
||||
#define TYPE_ZCL_SEND 0x02
|
||||
#define TYPE_NOTIFY 0x80
|
||||
#define TYPE_ZDO_RSP 0x81
|
||||
#define TYPE_ZCL_IND 0x82
|
||||
#define TYPE_SEND_CNF 0x8F
|
||||
|
||||
void zigbee_send_bytes(uint8_t *bytes, int len)
|
||||
{
|
||||
// print_hex(bytes, len);
|
||||
|
||||
uart4_send_bytes(bytes, len);
|
||||
}
|
||||
static uint8_t zigbee_cal_check_sum(uint8_t *buffer, int len)
|
||||
{
|
||||
uint8_t xor = 0;
|
||||
|
||||
for (int j = 0; j < len; j++)
|
||||
{
|
||||
xor ^= buffer[j];
|
||||
}
|
||||
return xor;
|
||||
}
|
||||
bool_t zigbee_check_sum(uint8_t *buffer, int len)
|
||||
{
|
||||
uint8_t xor = 0;
|
||||
|
||||
for (int j = 2; j < len - 1; j++)
|
||||
{
|
||||
xor ^= buffer[j];
|
||||
}
|
||||
return buffer[len - 1] == xor;
|
||||
}
|
||||
int zigbee_gen_pack(
|
||||
uint8_t *buffer, uint8_t cmd_type, uint8_t cmd_code,
|
||||
uint8_t *data,
|
||||
uint8_t data_len)
|
||||
{
|
||||
int i = 0;
|
||||
buffer[i++] = 0x55;
|
||||
buffer[i++] = 5 + data_len;
|
||||
buffer[i++] = cmd_type;
|
||||
buffer[i++] = cmd_code;
|
||||
if (data)
|
||||
{
|
||||
for (int j = i; j < data_len + i; j++)
|
||||
{
|
||||
buffer[j] = data[j - i];
|
||||
}
|
||||
i += data_len;
|
||||
}
|
||||
uint8_t xor = 0;
|
||||
|
||||
for (int j = 2; j < i - 2; j++)
|
||||
{
|
||||
xor ^= buffer[j];
|
||||
}
|
||||
buffer[i++] = xor;
|
||||
return i;
|
||||
}
|
||||
int zigbee_pack_get(queue_t *que, uint8_t *o_data)
|
||||
{
|
||||
int i = 0;
|
||||
int step = 0;
|
||||
int len = 0;
|
||||
while (1)
|
||||
{
|
||||
uint8_t data;
|
||||
|
||||
int ret = q_dequeue(que, &data);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch (step)
|
||||
{
|
||||
case 0:
|
||||
if (data == 0x55)
|
||||
{
|
||||
o_data[i] = data;
|
||||
i++;
|
||||
step++;
|
||||
}
|
||||
else
|
||||
{
|
||||
q_queue_clear(que);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
o_data[i] = data;
|
||||
len = data;
|
||||
i++;
|
||||
step++;
|
||||
break;
|
||||
case 2:
|
||||
o_data[i] = data;
|
||||
i++;
|
||||
if (i >= len + 2)
|
||||
{
|
||||
int pack_len = len + 2;
|
||||
|
||||
bool_t suc = zigbee_check_sum(o_data, pack_len);
|
||||
if (suc)
|
||||
{
|
||||
return len + 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
q_queue_clear(que);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void query_mod_status(void)
|
||||
{
|
||||
uint8_t data_cache[64];
|
||||
int len;
|
||||
|
||||
len = zigbee_gen_pack(data_cache, TYPE_CFG, 0x00, NULL, 0);
|
||||
zigbee_send_bytes(data_cache, len);
|
||||
|
||||
umword_t last_tick = sys_read_tick();
|
||||
queue_t *q = uart4_queue_get();
|
||||
int last_len = q_queue_len(q);
|
||||
while (sys_read_tick() - last_tick < 50)
|
||||
{
|
||||
if (last_len != q_queue_len(q))
|
||||
{ // 长度发生了变化,则更新时间
|
||||
last_tick = sys_read_tick();
|
||||
last_len = q_queue_len(q);
|
||||
}
|
||||
}
|
||||
|
||||
print_q_hex(q);
|
||||
}
|
||||
|
||||
void mod_start(void)
|
||||
{
|
||||
uint8_t data_cache[64];
|
||||
int len;
|
||||
|
||||
uint8_t data_0 = 0x1;
|
||||
len = zigbee_gen_pack(data_cache, TYPE_CFG, 0x01, &data_0, 1);
|
||||
|
||||
zigbee_send_bytes(data_cache, len);
|
||||
|
||||
umword_t last_tick = sys_read_tick();
|
||||
queue_t *q = uart4_queue_get();
|
||||
int last_len = q_queue_len(q);
|
||||
while (sys_read_tick() - last_tick < 50)
|
||||
{
|
||||
if (last_len != q_queue_len(q))
|
||||
{ // 长度发生了变化,则更新时间
|
||||
last_tick = sys_read_tick();
|
||||
last_len = q_queue_len(q);
|
||||
}
|
||||
}
|
||||
|
||||
print_q_hex(q);
|
||||
}
|
||||
void mod_reset(void)
|
||||
{
|
||||
uint8_t data_cache[64] = {0x55, 0x07, 0x00, 0x04, 0x00, 0xFF, 0xFF, 0x00, 0x04};
|
||||
int len;
|
||||
|
||||
zigbee_send_bytes(data_cache, 9);
|
||||
|
||||
umword_t last_tick = sys_read_tick();
|
||||
queue_t *q = uart4_queue_get();
|
||||
int last_len = q_queue_len(q);
|
||||
while ((sys_read_tick() - last_tick) < 100)
|
||||
{
|
||||
if (last_len != q_queue_len(q))
|
||||
{ // 长度发生了变化,则更新时间
|
||||
last_tick = sys_read_tick();
|
||||
last_len = q_queue_len(q);
|
||||
}
|
||||
}
|
||||
int ret;
|
||||
while ((ret = zigbee_pack_get(q, data_cache)) > 0)
|
||||
{
|
||||
print_hex(data_cache, ret);
|
||||
}
|
||||
}
|
||||
void mod_set_cod(void)
|
||||
{
|
||||
uint8_t data_cache[64] = {0x55, 0x04, 0x00, 0x05, 0x00, 0x05};
|
||||
zigbee_send_bytes(data_cache, 6);
|
||||
|
||||
umword_t last_tick = sys_read_tick();
|
||||
queue_t *q = uart4_queue_get();
|
||||
int last_len = q_queue_len(q);
|
||||
while (sys_read_tick() - last_tick < 100)
|
||||
{
|
||||
if (last_len != q_queue_len(q))
|
||||
{ // 长度发生了变化,则更新时间
|
||||
last_tick = sys_read_tick();
|
||||
last_len = q_queue_len(q);
|
||||
}
|
||||
}
|
||||
|
||||
int ret;
|
||||
while ((ret = zigbee_pack_get(q, data_cache)) > 0)
|
||||
{
|
||||
print_hex(data_cache, ret);
|
||||
}
|
||||
}
|
||||
void mod_cfg_net(void)
|
||||
{
|
||||
uint8_t data_cache[64] = {0x55, 0x03, 0x00, 0x02, 0x02};
|
||||
zigbee_send_bytes(data_cache, 5);
|
||||
|
||||
umword_t last_tick = sys_read_tick();
|
||||
queue_t *q = uart4_queue_get();
|
||||
int last_len = q_queue_len(q);
|
||||
while (sys_read_tick() - last_tick < 100)
|
||||
{
|
||||
if (last_len != q_queue_len(q))
|
||||
{ // 长度发生了变化,则更新时间
|
||||
last_tick = sys_read_tick();
|
||||
last_len = q_queue_len(q);
|
||||
}
|
||||
}
|
||||
|
||||
int ret;
|
||||
while ((ret = zigbee_pack_get(q, data_cache)) > 0)
|
||||
{
|
||||
print_hex(data_cache, ret);
|
||||
}
|
||||
}
|
||||
void mod_send_data(uint16_t short_addr)
|
||||
{
|
||||
uint8_t data_cache[] = {0x55, 0x19, 0x02, 0x0F, 0x00,
|
||||
short_addr & 0xff, (short_addr >> 8) & 0xff, // short addr
|
||||
0x01, 0x8C, 0x00, 0x08,
|
||||
0xFC, 0x00, 0x20, 0x00, 0x00, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x29};
|
||||
|
||||
uint8_t check_sum = zigbee_cal_check_sum(data_cache + 2, sizeof(data_cache) - 3);
|
||||
data_cache[sizeof(data_cache) - 1] = check_sum;
|
||||
zigbee_send_bytes(data_cache, sizeof(data_cache));
|
||||
|
||||
umword_t last_tick = sys_read_tick();
|
||||
queue_t *q = uart4_queue_get();
|
||||
int last_len = q_queue_len(q);
|
||||
while (sys_read_tick() - last_tick < 100)
|
||||
{
|
||||
if (last_len != q_queue_len(q))
|
||||
{ // 长度发生了变化,则更新时间
|
||||
last_tick = sys_read_tick();
|
||||
last_len = q_queue_len(q);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t data_cache_read[64];
|
||||
int ret;
|
||||
while ((ret = zigbee_pack_get(q, data_cache_read)) > 0)
|
||||
{
|
||||
print_hex(data_cache_read, ret);
|
||||
}
|
||||
}
|
||||
|
||||
void e180_loop(void)
|
||||
{
|
||||
{
|
||||
umword_t last_tick = sys_read_tick();
|
||||
queue_t *q = uart4_queue_get();
|
||||
int last_len = q_queue_len(q);
|
||||
while (sys_read_tick() - last_tick < 100)
|
||||
{
|
||||
if (last_len != q_queue_len(q))
|
||||
{ // 长度发生了变化,则更新时间
|
||||
last_tick = sys_read_tick();
|
||||
last_len = q_queue_len(q);
|
||||
}
|
||||
}
|
||||
if (q_queue_len(q) > 0)
|
||||
{
|
||||
uint8_t data_cache_read[64];
|
||||
int ret;
|
||||
while ((ret = zigbee_pack_get(q, data_cache_read)) > 0)
|
||||
{
|
||||
print_hex(data_cache_read, ret);
|
||||
}
|
||||
}
|
||||
uart4_recv_flags = 0;
|
||||
}
|
||||
}
|
||||
7
mkrtos_user/server/app/drv/e180-zg120.h
Normal file
7
mkrtos_user/server/app/drv/e180-zg120.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
void mod_reset(void);
|
||||
void mod_set_cod(void);
|
||||
void mod_cfg_net(void);
|
||||
void e180_loop(void);
|
||||
void mod_send_data(uint16_t short_addr);
|
||||
61
mkrtos_user/server/app/drv/timer.c
Normal file
61
mkrtos_user/server/app/drv/timer.c
Normal file
@@ -0,0 +1,61 @@
|
||||
#include "stm32_sys.h"
|
||||
#include "u_types.h"
|
||||
#include "u_prot.h"
|
||||
#include "u_factory.h"
|
||||
#include "u_hd_man.h"
|
||||
#include "u_irq_sender.h"
|
||||
#include "u_mm.h"
|
||||
#include "u_log.h"
|
||||
#include "u_local_thread.h"
|
||||
#include <assert.h>
|
||||
|
||||
#define IRQ_THREAD_PRIO 3
|
||||
#define STACK_SIZE 512
|
||||
static __attribute__((aligned(8))) uint8_t stack0[STACK_SIZE];
|
||||
static obj_handler_t obj;
|
||||
|
||||
static void *TIM2_IRQHandler(void *arg);
|
||||
|
||||
void timer_init(void)
|
||||
{
|
||||
obj = handler_alloc();
|
||||
assert(obj != HANDLER_INVALID);
|
||||
msg_tag_t tag = factory_create_irq_sender(FACTORY_PROT, vpage_create_raw3(0, 0, obj));
|
||||
assert(msg_tag_get_val(tag) >= 0);
|
||||
uirq_bind(obj, TIM2_IRQn, 0);
|
||||
|
||||
int ret = thread_create(IRQ_THREAD_PRIO, TIM2_IRQHandler, (umword_t)(stack0 + STACK_SIZE), NULL);
|
||||
assert(ret >= 0);
|
||||
|
||||
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
||||
|
||||
TIM_TimeBaseStructure.TIM_Period = 100 - 1; // 自动重装值(此时改为100ms)
|
||||
TIM_TimeBaseStructure.TIM_Prescaler = 720 - 1; // 时钟预分频
|
||||
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数
|
||||
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 时钟分频1
|
||||
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
|
||||
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
||||
|
||||
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
|
||||
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
||||
TIM_Cmd(TIM2, ENABLE);
|
||||
}
|
||||
|
||||
static void *TIM2_IRQHandler(void *arg)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
msg_tag_t tag = uirq_wait(obj, 0);
|
||||
if (msg_tag_get_val(tag) >= 0)
|
||||
{
|
||||
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) // 检查TIM3更新中断发生否
|
||||
{
|
||||
|
||||
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
||||
}
|
||||
uirq_ack(obj, TIM2_IRQn);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
3
mkrtos_user/server/app/drv/timer.h
Normal file
3
mkrtos_user/server/app/drv/timer.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
void timer_init(void);
|
||||
@@ -7,6 +7,8 @@
|
||||
#include "u_hd_man.h"
|
||||
#include "u_local_thread.h"
|
||||
#include "u_sleep.h"
|
||||
#include "u_str.h"
|
||||
#include "u_queue.h"
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
static obj_handler_t irq_obj;
|
||||
@@ -15,15 +17,26 @@ static obj_handler_t irq_obj;
|
||||
#define STACK_SIZE 512
|
||||
static __attribute__((aligned(8))) uint8_t stack0[STACK_SIZE];
|
||||
|
||||
static uint8_t uart4_queue_data[257];
|
||||
static queue_t uart4_queue;
|
||||
int uart4_recv_flags = 0;
|
||||
queue_t *uart4_queue_get(void)
|
||||
{
|
||||
return &uart4_queue;
|
||||
}
|
||||
|
||||
static void *UART4_IRQHandler(void *arg);
|
||||
|
||||
void init_uart4(u32 baudRate)
|
||||
{
|
||||
|
||||
q_init(&uart4_queue, uart4_queue_data, sizeof(uart4_queue_data));
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStructure = {0};
|
||||
USART_InitTypeDef USART_InitStructure = {0};
|
||||
NVIC_InitTypeDef NVIC_InitStructure = {0};
|
||||
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); // 使能GPIOC时钟
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); // 使能GPIOC时钟
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); // 使能串口3时钟
|
||||
|
||||
USART_DeInit(UART4);
|
||||
@@ -43,17 +56,19 @@ void init_uart4(u32 baudRate)
|
||||
USART_InitStructure.USART_Parity = USART_Parity_No; // 无奇偶校验位
|
||||
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件流控制
|
||||
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // 发送和接收模式
|
||||
USART_Init(UART4, &USART_InitStructure); // 初始化串口
|
||||
USART_Init(UART4, &USART_InitStructure); // 初始化串口
|
||||
|
||||
// NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
|
||||
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 抢占优先级为1
|
||||
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // 子优先级为1
|
||||
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
|
||||
// NVIC_Init(&NVIC_InitStructure); // 中断优先级初始化
|
||||
|
||||
USART_Cmd(UART4, ENABLE); // 使能串口
|
||||
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); // 开启中断
|
||||
USART_ClearITPendingBit(UART4, USART_IT_RXNE);
|
||||
// USART_ITConfig(UART4, USART_IT_IDLE, ENABLE); // 开启串口空闲中断
|
||||
// USART_ClearITPendingBit(UART4, USART_IT_IDLE);
|
||||
// USART_ClearFlag(UART4, USART_FLAG_IDLE);
|
||||
USART_Cmd(UART4, ENABLE); // 使能串口
|
||||
|
||||
irq_obj = handler_alloc();
|
||||
assert(irq_obj != HANDLER_INVALID);
|
||||
@@ -64,6 +79,7 @@ void init_uart4(u32 baudRate)
|
||||
int ret = thread_create(IRQ_THREAD_PRIO, UART4_IRQHandler, (umword_t)(stack0 + STACK_SIZE), NULL);
|
||||
assert(ret >= 0);
|
||||
}
|
||||
|
||||
static void *UART4_IRQHandler(void *arg)
|
||||
{
|
||||
while (1)
|
||||
@@ -71,13 +87,26 @@ static void *UART4_IRQHandler(void *arg)
|
||||
msg_tag_t tag = uirq_wait(irq_obj, 0);
|
||||
if (msg_tag_get_val(tag) >= 0)
|
||||
{
|
||||
if (USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
|
||||
if (USART_GetFlagStatus(UART4, USART_FLAG_RXNE) != RESET)
|
||||
{
|
||||
USART_ClearITPendingBit(UART4, USART_IT_RXNE); // 清除中断标志
|
||||
uint8_t data = USART_ReceiveData(UART4);
|
||||
// queue_push(data);
|
||||
|
||||
q_enqueue(&uart4_queue, data);
|
||||
// uart4_send_byte(data);
|
||||
}
|
||||
if (USART_GetFlagStatus(UART4, USART_FLAG_IDLE) != RESET)
|
||||
{
|
||||
// 空闲帧中断
|
||||
// 处理接受的数据
|
||||
uart4_recv_flags = 1;
|
||||
}
|
||||
if (USART_GetFlagStatus(UART4, USART_FLAG_ORE) != RESET)
|
||||
{
|
||||
USART_ReceiveData(UART4);
|
||||
// USART_ClearFlag(USART1, USART_FLAG_ORE); //清除溢出中断,其实没用,因为手册里讲了
|
||||
// 通过读入USART_SR 寄存器,然后读入 USART_DR寄存器来清除标志位
|
||||
}
|
||||
uirq_ack(irq_obj, UART4_IRQn);
|
||||
}
|
||||
// u_sleep_ms(1);
|
||||
@@ -89,7 +118,7 @@ void uart4_send_byte(u8 byte)
|
||||
while (USART_GetFlagStatus(UART4, USART_FLAG_TXE) == RESET)
|
||||
;
|
||||
USART_SendData(UART4, byte);
|
||||
while (USART_GetFlagStatus(UART4, USART_FLAG_TXE) == RESET)
|
||||
while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET)
|
||||
;
|
||||
}
|
||||
void uart4_send_bytes(u8 *bytes, int len)
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "u_types.h"
|
||||
#include "u_queue.h"
|
||||
#include "stm32_sys.h"
|
||||
void init_uart4(u32 baudRate);
|
||||
void uart4_send_string(char *string);
|
||||
void uart4_send_bytes(u8 *bytes, int len);
|
||||
void uart4_send_byte(u8 byte);
|
||||
queue_t *uart4_queue_get(void);
|
||||
extern int uart4_recv_flags;
|
||||
@@ -20,9 +20,12 @@
|
||||
#include "temp_cal.h"
|
||||
#include "usart3.h"
|
||||
#include "usart2.h"
|
||||
#include "uart4.h"
|
||||
#include "rs485.h"
|
||||
#include "wk2xx.h"
|
||||
#include "auto_close.h"
|
||||
#include "u_str.h"
|
||||
#include "e180-zg120.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
int main(int argc, char *args[])
|
||||
@@ -37,6 +40,8 @@ int main(int argc, char *args[])
|
||||
// assert(sys_info.devID == 12);
|
||||
ulog_write_str(u_get_global_env()->log_hd, "app start..\n");
|
||||
relay_test();
|
||||
mod_send_data(0x10de);
|
||||
|
||||
while (1)
|
||||
{
|
||||
user_spl0601_get();
|
||||
@@ -44,6 +49,15 @@ int main(int argc, char *args[])
|
||||
UpdateUI();
|
||||
usart2_loop();
|
||||
io_ctrl_loop();
|
||||
|
||||
e180_loop();
|
||||
|
||||
// if (uart4_cn > 0)
|
||||
// {
|
||||
// print_hex(uart4_data, uart4_cn);
|
||||
// uart4_cn = 0;
|
||||
// recv_flags = 0;
|
||||
// }
|
||||
// u_sleep_ms(10);
|
||||
// printf("temp:%f press:%f\n", (sys_info.board_temp), (sys_info.pressure));
|
||||
// for (int i = 0; i < 4; i++)
|
||||
|
||||
@@ -44,17 +44,17 @@ int main(int argc, char *args[])
|
||||
env.ns_hd = ipc_hd;
|
||||
namespace_init(ipc_hd);
|
||||
u_sleep_init();
|
||||
// ret = app_load("app", &env);
|
||||
// if (ret < 0)
|
||||
// {
|
||||
// printf("app load fail, 0x%x\n", ret);
|
||||
// // ulog_write_str(LOG_PROT, "app load fail.\n");
|
||||
// }
|
||||
ret = app_load("hello", &env);
|
||||
ret = app_load("app", &env);
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("app load fail, 0x%x\n", ret);
|
||||
// ulog_write_str(LOG_PROT, "app load fail.\n");
|
||||
}
|
||||
// ret = app_load("hello", &env);
|
||||
// if (ret < 0)
|
||||
// {
|
||||
// printf("app load fail, 0x%x\n", ret);
|
||||
// }
|
||||
ret = app_load("fatfs", &env);
|
||||
if (ret < 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user