jianghuai/HARDWARE/CAN/porting.h
2024-05-07 09:36:09 +08:00

171 lines
4.0 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/****************************************************************
* porting.h *
* 2022-11-15 *
****************************************************************/
#ifndef _PROTOCOL_PORTING_H_
#define _PROTOCOL_PORTING_H_
/* 类型定义 */
#include <stdlib.h>
#include "stdint.h"
#include <stdbool.h>
#include "sys.h"
#define NELEMENTS(x) (sizeof(x)/sizeof((x)[0]))
#define _SUCCESS 0
#define _FAILURE 1
#define _OVERTIME 2
#define _ABANDON 3
typedef struct {
unsigned short deepth; /* 总共有多少元素 */
unsigned short occupy; /* 已占用元素个数 */
unsigned short stsize; /* 每个元素的大小 */
void *array; /* 存放的缓存 */
void *limit; /* 缓存末尾 */
void *wp; /* 写指针 */
void *rp; /* 读指针 */
} st_fifo_t;
/**
* @brief: 初始化一个FIFO
*
*
*/
void sfifo_init(st_fifo_t *stfifo, void *array, unsigned short stsize, unsigned short deepth);
void sfifo_reset(st_fifo_t *stfifo);
bool sfifo_write(st_fifo_t *stfifo, const void *stunit);
bool sfifo_empty(st_fifo_t *stfifo);
bool sfifo_full(st_fifo_t *stfifo);
unsigned int sfifo_read(st_fifo_t *stfifo,void *stunit);
unsigned int sfifo_occupy_get(st_fifo_t *stfifo);
#define QUEUEMEM unsigned char
#define QUEUENODE struct node
typedef struct node{
QUEUENODE *next;
} NODE;
typedef struct {
QUEUENODE *head;
QUEUENODE *tail;
unsigned short item;
} QUEUE;
bool CreateQueue(QUEUE *que);
unsigned short QueueItem(QUEUE *que);
QUEUEMEM *QueueHead(QUEUE *que);
QUEUEMEM *QueueTail(QUEUE *que);
QUEUEMEM *QueueNext(QUEUEMEM *element);
QUEUEMEM *DelQueueElement(QUEUE *que, QUEUEMEM *element);
QUEUEMEM *DelQueueHead(QUEUE *que);
QUEUEMEM *DelQueueTail(QUEUE *que);
bool AppendQueue(QUEUE *que, QUEUEMEM *element);
bool InsertBeforeQueue(QUEUE *que, QUEUEMEM *element,QUEUEMEM *curelement);
/* 内存相关的接口移植 */
/*
* void mem_cpy (void* dst, const void* src, int cnt); CAN_COPY
* void mem_set (void* dst, int val, int cnt);
* int mem_cmp (const void* dst, const void* src, int cnt);
* void *mem_malloc(mem_size_t size);
* void mem_free(void *mem);
*/
#define CAN_MALLOC(size) malloc(size)
#define CAN_COPY(p_des, p_src, size) memcpy((p_des), (p_src), (size))
#define CAN_CMPY(p_des, p_src, size) memcmp((p_des), (p_src), (size))
#define CAN_FREE(p) free(p)
/*
* 信号的回调
* para:
* signal: 信号标志
* para1: 参数,与发送者定义
* para2: 参数,与发送者定义
*/
typedef void (*signal_callback)(unsigned int signal, int para1, int para2);
/*
* \brief: 发送一个消息给进程PID
* \para:
* signal: my_signal_e 类型的信号值
* para1,para2: 参数my_kill 与对应消息回调之间传递参数用
*/
int can_kill(unsigned int signal, unsigned int para1, unsigned int para2);
/*
* \brief: 申明捕获进程PID 的消息signal
* \para:
* signal: 要捕获的消息
* cb: 捕获函数
* \return:
* 0-成功 <0 失败
* \remark:
* 1) my_signal_queue_create 中已申明的消息ID不能再my_signal
* 2) 同一个消息可以多次my_signal但只有最后一次有效
* 但是不要这么用
*/
int can_signal(unsigned int signal, signal_callback cb);
/*
* 定义CAN信号类型
*/
typedef enum {
E_CAN_SIGNAL_NULL = 0,
E_CAN_MSG_SIG,
E_CAN_MSG_CB_SIG, /* 用来处理信号的发送回调 */
E_CAN_15765_NET_SIG,
E_CAN_15765_SS_SIG,
} can_signal_e;
/*************************************定时器********************************************************/
typedef unsigned int TIMER;
typedef void (*can_tmr_func)(TIMER tmr_id); //定时执行任务的指针
TIMER can_timer_create(can_tmr_func cb);
void can_timer_remove(TIMER tmr);
void can_timer_start(TIMER tmr,unsigned int ms);
void can_timer_stop(TIMER tmr);
bool can_timer_switch(TIMER tmr);
#endif