添加一些驱动模拟接口的测试

This commit is contained in:
zhangzheng
2023-11-21 00:21:13 +08:00
parent d8ad231b90
commit f58d4fe537
12 changed files with 193 additions and 52 deletions

View File

@@ -2,6 +2,8 @@
#include "u_types.h"
#include "u_prot.h"
msg_tag_t thread_yield(obj_handler_t obj);
msg_tag_t thread_msg_buf_set(obj_handler_t obj, void *msg);
msg_tag_t thread_msg_buf_get(obj_handler_t obj, umword_t *msg, umword_t *len);
msg_tag_t thread_exec_regs(obj_handler_t obj, umword_t pc, umword_t sp, umword_t ram, umword_t cp_stack);

View File

@@ -8,7 +8,25 @@ enum thread_op
BIND_TASK,
MSG_BUG_GET,
MSG_BUG_SET,
YIELD,
};
msg_tag_t thread_yield(obj_handler_t obj)
{
register volatile umword_t r0 asm("r0");
mk_syscall(syscall_prot_create4(YIELD, THREAD_PROT, obj, TRUE).raw,
0,
0,
0,
0,
0,
0);
asm __volatile__(""
:
:
: "r0", "r1", "r2");
return msg_tag_init(r0);
}
msg_tag_t thread_msg_buf_set(obj_handler_t obj, void *msg)
{
register volatile umword_t r0 asm("r0");
@@ -16,12 +34,12 @@ msg_tag_t thread_msg_buf_set(obj_handler_t obj, void *msg)
register volatile umword_t r2 asm("r2");
mk_syscall(syscall_prot_create(MSG_BUG_SET, THREAD_PROT, obj).raw,
0,
msg,
0,
0,
0,
0);
0,
msg,
0,
0,
0,
0);
asm __volatile__(""
:
:
@@ -35,12 +53,12 @@ msg_tag_t thread_msg_buf_get(obj_handler_t obj, umword_t *msg, umword_t *len)
register volatile umword_t r2 asm("r2");
mk_syscall(syscall_prot_create4(MSG_BUG_GET, THREAD_PROT, obj, TRUE).raw,
0,
0,
0,
0,
0,
0);
0,
0,
0,
0,
0,
0);
asm __volatile__(""
:
:
@@ -61,12 +79,12 @@ msg_tag_t thread_exec_regs(obj_handler_t obj, umword_t pc, umword_t sp, umword_t
register volatile umword_t r0 asm("r0");
mk_syscall(syscall_prot_create(SET_EXEC_REGS, THREAD_PROT, obj).raw,
0,
pc,
sp,
ram,
cp_stack,
0);
0,
pc,
sp,
ram,
cp_stack,
0);
asm __volatile__(""
:
:
@@ -80,12 +98,12 @@ msg_tag_t thread_run(obj_handler_t obj, uint8_t prio)
register volatile umword_t r0 asm("r0");
mk_syscall(syscall_prot_create(RUN_THREAD, THREAD_PROT, obj).raw,
0,
prio,
0,
0,
0,
0);
0,
prio,
0,
0,
0,
0);
msg_tag_t tag = msg_tag_init(r0);
return tag;
@@ -95,11 +113,11 @@ msg_tag_t thread_bind_task(obj_handler_t obj, obj_handler_t tk_obj)
register volatile umword_t r0 asm("r0");
mk_syscall(syscall_prot_create(BIND_TASK, THREAD_PROT, obj).raw,
0,
tk_obj,
0,
0,
0, 0);
0,
tk_obj,
0,
0,
0, 0);
asm __volatile__(""
:
:

View File

@@ -2,4 +2,5 @@
#include <u_types.h>
void u_thread_del(obj_handler_t th_hd);
int u_thread_create(obj_handler_t *th_hd, void *stack, umword_t stack_size, void *msg_buf, void (*thread_func)(void), int prio);
int u_thread_create(obj_handler_t *th_hd, void *stack, umword_t stack_size, void *msg_buf, void (*thread_func)(void));
void u_thread_run(obj_handler_t th_hd, int prio);

View File

@@ -11,7 +11,7 @@ void u_thread_del(obj_handler_t th_hd)
{
task_unmap(TASK_THIS, vpage_create_raw3(KOBJ_DELETE_RIGHT, 0, th_hd));
}
int u_thread_create(obj_handler_t *th_hd, void *stack, umword_t stack_size, void *msg_buf, void (*thread_func)(void), int prio)
int u_thread_create(obj_handler_t *th_hd, void *stack, umword_t stack_size, void *msg_buf, void (*thread_func)(void))
{
assert(th_hd);
msg_tag_t tag;
@@ -35,7 +35,7 @@ int u_thread_create(obj_handler_t *th_hd, void *stack, umword_t stack_size, void
handler_free_umap(th1_hd);
return msg_tag_get_prot(tag);
}
tag = thread_exec_regs(th1_hd, (umword_t)thread_func, (umword_t)stack + stack_size - sizeof(void *), RAM_BASE(), 0);
tag = thread_exec_regs(th1_hd, (umword_t)thread_func, (umword_t)stack + stack_size - sizeof(void *)*2, RAM_BASE(), 0);
if (msg_tag_get_prot(tag) < 0)
{
handler_free_umap(th1_hd);
@@ -47,6 +47,10 @@ int u_thread_create(obj_handler_t *th_hd, void *stack, umword_t stack_size, void
handler_free_umap(th1_hd);
return msg_tag_get_prot(tag);
}
thread_run(th1_hd, prio);
*th_hd = th1_hd;
return 0;
}
void u_thread_run(obj_handler_t th_hd, int prio)
{
thread_run(th_hd, prio);
}

View File

@@ -48,6 +48,7 @@ target_link_libraries(
sys_util
sys_svr
src
util
drivers
stm32f103_onenet_nbiot
stm32f1xx_hal

View File

@@ -914,7 +914,7 @@ struct rt_spinlock
*/
struct rt_thread
{
pthread_t th; /**< use pthread*/
// pthread_t th; /**< use pthread*/
rt_list_t tlist; /**< the thread list */
int stat;

View File

@@ -0,0 +1,3 @@
#pragma once
void rtthread_drv_test(void);

View File

@@ -5,6 +5,7 @@
#include <stdio.h>
#include <syscall_backend.h>
#include <rtthread_inter.h>
#include <test.h>
/* defined the LED0 pin: PC0 */
#define LED0_PIN GET_PIN(C, 0)
extern void rt_hw_board_init(void);
@@ -17,15 +18,18 @@ int main(void)
/* init board */
rt_hw_board_init();
dfs_init();
rtthread_drv_test();
/* set LED0 pin mode to output */
rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
while (1)
{
rt_pin_write(LED0_PIN, PIN_HIGH);
rt_thread_mdelay(500);
rt_thread_mdelay(200);
rt_pin_write(LED0_PIN, PIN_LOW);
rt_thread_mdelay(500);
rt_thread_mdelay(200);
printf("led test..\n");
}
}

View File

@@ -49,7 +49,12 @@ void *rt_realloc(void *ptr, rt_size_t newsize)
}
void rt_schedule(void)
{
u_sleep_ms(0);
thread_yield(-1);
}
rt_err_t rt_thread_yield(void)
{
thread_yield(-1);
return 0;
}
static umword_t intr_status = 0;
rt_base_t rt_hw_interrupt_disable(void)
@@ -120,6 +125,15 @@ rt_err_t rt_thread_suspend_with_flag(rt_thread_t thread, int suspend_flag)
thread->stat = 0;
return 0;
}
rt_err_t rt_thread_suspend(rt_thread_t thread)
{
//! 这里锁两次,第二次加锁将会导致挂起
printf("[drv]%s:%d\n", __func__, __LINE__);
thread->stat = RT_THREAD_SUSPEND;
pthread_mutex_lock(&thread->suspend_lock);
thread->stat = 0;
return 0;
}
rt_err_t rt_thread_resume(rt_thread_t thread)
{
pthread_mutex_unlock(&thread->suspend_lock);
@@ -207,7 +221,7 @@ rt_err_t rt_mq_delete(rt_mq_t mq)
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#define STACK_SIZE 1024
#define STACK_SIZE 2048
static obj_handler_t timer_hd = HANDLER_INVALID;
static char timer_thmsg_buf[MSG_BUG_LEN];
@@ -230,6 +244,7 @@ static struct rt_timer *timer_alloc(struct rt_timer *tim, void (*func_cb)(void *
timer_list[i]->func_cb = func_cb;
timer_list[i]->exp_times = exp_times;
timer_list[i]->flags = flags;
timer_list[i]->start_times = 0;
pthread_spin_unlock(&timer_lock);
return timer_list[i];
}
@@ -260,16 +275,9 @@ void rt_timer_check(void)
{
if (timer_list[i] &&
timer_list[i]->start_times != 0 &&
timer_list[i]->exp_times + timer_list[i]->start_times >= now_tick)
now_tick >= timer_list[i]->exp_times + timer_list[i]->start_times)
{
if ((timer_list[i]->flags & RT_TIMER_FLAG_ONE_SHOT) == 0)
{
pthread_spin_unlock(&timer_lock);
timer_list[i]->func_cb(timer_list[i]->data);
pthread_spin_lock(&timer_lock);
timer_list[i] = NULL;
}
else if (timer_list[i]->flags & RT_TIMER_FLAG_PERIODIC)
if (timer_list[i]->flags & RT_TIMER_FLAG_PERIODIC)
{
pthread_spin_unlock(&timer_lock);
timer_list[i]->func_cb(timer_list[i]->data);
@@ -279,6 +287,13 @@ void rt_timer_check(void)
timer_list[i]->start_times = sys_read_tick();
}
}
else if ((timer_list[i]->flags & RT_TIMER_FLAG_ONE_SHOT) == 0)
{
pthread_spin_unlock(&timer_lock);
timer_list[i]->func_cb(timer_list[i]->data);
pthread_spin_lock(&timer_lock);
timer_list[i] = NULL;
}
else
{
/*Error.*/
@@ -287,6 +302,7 @@ void rt_timer_check(void)
}
pthread_spin_unlock(&timer_lock);
}
void rt_timer_init(rt_timer_t timer,
const char *name,
void (*timeout)(void *parameter),
@@ -297,7 +313,7 @@ void rt_timer_init(rt_timer_t timer,
assert(timeout);
if (timer_hd == HANDLER_INVALID)
{
int ret = u_thread_create(&timer_hd, timer_stack, STACK_SIZE, timer_thmsg_buf, timer_func, 2);
int ret = u_thread_create(&timer_hd, timer_stack, STACK_SIZE, timer_thmsg_buf, timer_func);
if (ret < 0)
{
@@ -312,7 +328,23 @@ void rt_timer_init(rt_timer_t timer,
errno = -ENOMEM;
return;
}
printf("%s name %s tick %d flag 0x%x.\n", name, time, flag);
u_thread_run(timer_hd, 2);
printf("%s name %s tick %d flag 0x%x.\n", __func__, name, time, flag);
}
rt_timer_t rt_timer_create(const char *name,
void (*timeout)(void *parameter),
void *parameter,
rt_tick_t time,
rt_uint8_t flag)
{
rt_timer_t t = rt_malloc(sizeof(*t));
if (t == NULL)
{
return NULL;
}
rt_timer_init(t, name, timeout, parameter, time, flag);
return t;
}
rt_err_t rt_timer_start(rt_timer_t timer)
{

View File

@@ -0,0 +1,69 @@
#include <rtthread.h>
#include <assert.h>
#include <rtthread.h>
/* 定时器的控制块 */
static rt_timer_t timer1;
static rt_timer_t timer2;
static int cnt = 0;
/* 定时器 1 超时函数 */
static void timeout1(void *parameter)
{
rt_kprintf("periodic timer is timeout %d\n", cnt);
/* 运行第 10 次,停止周期定时器 */
if (cnt++ >= 9)
{
rt_timer_stop(timer1);
rt_kprintf("periodic timer was stopped! \n");
}
}
/* 定时器 2 超时函数 */
static void timeout2(void *parameter)
{
rt_kprintf("one shot timer is timeout\n");
}
int timer_sample(void)
{
/* 创建定时器 1 周期定时器 */
timer1 = rt_timer_create("timer1", timeout1,
RT_NULL, 10,
RT_TIMER_FLAG_PERIODIC);
/* 启动定时器 1 */
if (timer1 != RT_NULL)
rt_timer_start(timer1);
/* 创建定时器 2 单次定时器 */
timer2 = rt_timer_create("timer2", timeout2,
RT_NULL, 30,
RT_TIMER_FLAG_ONE_SHOT);
/* 启动定时器 2 */
if (timer2 != RT_NULL)
rt_timer_start(timer2);
return 0;
}
void rtthread_drv_test(void)
{
rt_schedule();
rt_base_t a = rt_hw_interrupt_disable();
rt_base_t b = rt_hw_interrupt_disable();
rt_hw_interrupt_enable(b);
rt_hw_interrupt_enable(a);
assert(rt_thread_self != 0);
#if 0
rt_thread_resume(rt_thread_self());
rt_thread_suspend(rt_thread_self());
#endif
timer_sample();
}