广和通l610 CAT.1模组opencpu开发《二》cola_os移植
在L610模组下实现cola os的移植,cola os使用一个freertos的线程来驱动,这样可以避免资源的竞争。
文章目录
前言
为了代码的统一且方便后期驱动的开发,现在将cola os系统适配到l610模组上。简化后期开发难度
一、cola os是什么?
在嵌入式软件开发,包括单片机开发中,软件架构对于开发人员是一个必须认真考虑的问题。软件架构对于系统整体的稳定性和可靠性是非常重要的,一个合适的软件架构不仅结构清晰,并且便于开发。本教程中使用的框架为前后台和时间片相结合,同时参考rt_thread和linux中优秀的思想设计而成,名为cola os。
二、cola os实现
1.cola os
cola os:
static KLIST_HEAD(task_list);
static task_t *current;
static uint32_t task_lock = 0;
void cola_task_create(task_t *task, void (*tcb_cb)(struct task_ctrl_blk *, uint32_t))
{
OS_CPU_SR cpu_sr;
cpu_sr = fibo_enterCS();
task->tcb_cb = tcb_cb;
list_add_tail(&task->entry, &task_list);
fibo_exitCS(cpu_sr);
}
void cola_task_schedule(void)
{
struct list_head *iter;
uint32_t sig = 0;
OS_CPU_SR cpu_sr;
fibo_sem_wait(task_lock);
list_for_each(iter, &task_list)
{
current = list_entry(iter, task_t, entry);
sig = current->signal;
if (sig)
{
cpu_sr = fibo_enterCS();
current->signal = 0;
fibo_exitCS(cpu_sr);
current->tcb_cb(current, sig);
}
}
}
void cola_task_send_signal(task_t *task, uint32_t sig)
{
OS_CPU_SR cpu_sr;
if(!task_lock)
{
task_lock = fibo_sem_new(0);
}
cpu_sr = fibo_enterCS();
task->signal |= sig;
fibo_sem_signal(task_lock);
fibo_exitCS(cpu_sr);
}
void cola_delay_ms(uint32_t ms)
{
fibo_taskSleep(ms);
}
uint32_t cola_get_ticker(void)
{
uint32_t tk = fibo_getSysTick();
return (uint32_t)((float)(tk) * 0.061);
}
2.cola timer
cola timer:
void cola_timer_create(stimer_t *timer, void (*tcb_cb)(void *arg),void *arg)
{
OS_CPU_SR cpu_sr;
cpu_sr = fibo_enterCS();
timer->data = arg;
timer->tcb_cb = tcb_cb;
timer->handle = 0;
fibo_exitCS(cpu_sr);
}
void cola_timer_start(stimer_t *timer,uint32_t period )
{
int ret;
if(timer->handle)
{
ret = fibo_timer_free(timer->handle);
if(ret < 0)
{
OSI_LOGI(0,"[estimer]->stop timer failed");
}
}
timer->handle = fibo_timer_new(period,timer->tcb_cb,timer->data);
if(timer->handle == 0)
{
OSI_LOGI(0,"[estimer]->timer start failed");
}
}
void cola_timer_stop(stimer_t *timer)
{
int ret;
if(timer->handle == 0)
return;
ret = fibo_timer_free(timer->handle);
if(ret < 0)
{
OSI_LOGI(0,"[estimer]->stop timer failed");
}
timer->handle = 0;
}
int cola_timer_is_running(stimer_t *timer)
{
return timer->handle;
}
3.cola device
cola device:
/**
********************************************************************************
* @filename : cola_device.c
* @author : Bit (QQ 1506564994)
* @versions : V1.0.0
* @date : 2020.01.12
* @brief :
******************************************************************************/
#include <string.h>
#include <stdbool.h>
#include "cola_device.h"
//struct cola_device *device_list = NULL;
static KLIST_HEAD(device_list);
static bool cola_device_is_exists(cola_device_t *dev)
{
struct cola_device *d;
list_for_each_entry(d, &device_list,entry) {
if (!strcmp(d->name, dev->name))
return true;
}
return false;
}
static int device_list_inster(cola_device_t *dev)
{
list_add_tail(&dev->entry, &device_list);
return 1;
}
int cola_device_register(cola_device_t *dev)
{
if ((NULL == dev) || (cola_device_is_exists(dev))) {
return 0;
}
if ((NULL == dev->name) || (NULL == dev->dops)) {
return 0;
}
return device_list_inster(dev);
}
cola_device_t *cola_device_find(const char *name)
{
struct cola_device *d;
list_for_each_entry(d, &device_list,entry) {
if (!strcmp(d->name, name))
return d;
}
return NULL;
}
int cola_device_read(cola_device_t *dev, int pos, void *buffer, int size)
{
if (dev) {
if (dev->dops->read) {
return dev->dops->read(dev, pos, buffer, size);
}
}
return 0;
}
int cola_device_peek(cola_device_t *dev, int pos, void *buffer, int size)
{
if (dev) {
if (dev->dops->peek) {
return dev->dops->peek(dev, pos, buffer, size);
}
}
return 0;
}
int cola_device_write(cola_device_t *dev, int pos, const void *buffer, int size)
{
if (dev) {
if (dev->dops->write) {
return dev->dops->write(dev, pos, buffer, size);
}
}
return 0;
}
int cola_device_ctrl(cola_device_t *dev, int cmd, void *arg)
{
if (dev) {
if (dev->dops->control) {
return dev->dops->control(dev, cmd, arg);
}
}
return 0;
}
int cola_device_cfg(cola_device_t *dev, void *args, void *var)
{
if (dev) {
if (dev->dops->config) {
return dev->dops->config(dev, args, var);
}
}
return 0;
}
void cola_device_set_owner(cola_device_t *dev, const void *owner)
{
dev->owner = (void *)owner;
}
三、最后
cola os适配还是比较简单的,至此cola 的移植已经完成,后面进行外设驱动的开发