接口的实现之can:simple_can.c
- #include <stdio.h>
- #include "sample.h"
- #include "sample_can.h"
- #include "Device_info.h"
- #include "os_depend.h"
- extern ConfigCan_Info_t CAN_Info;
- static int sample_can(void *param)
- {
- sample_can_t *can;
- if(param ==NULL){
- printf("%d: param NULL!\n",__LINE__ + 1);
- return -__LINE__;
- }
- list_for_each_entry(can,(struct list_head *)param,node) {
- if(can->recv_msg!= NULL) {
- //printf("+++++++++ name:%s\n",can->name);
- //pos->init(pos->tsjw,pos->tbs2,pos->tbs1,pos->brp,pos->mode);
- }
- }
-
- return 0;
- }
- static int on_can_recv(void *param)
- {
- if(param ==NULL){
- printf("%d: param NULL!\n",__LINE__ + 1);
- return -__LINE__;
- }
-
- return 0;
- }
- sample_methods_t sample_can_methords = {
- .sample = sample_can,
- .on_info_recv = on_can_recv
- };
- static struct list_head dev_can_head;
- int add_can_sample_dev(struct list_head *node)
- {
- ENTER_CRITICAL();
- list_add(node,&dev_can_head);
- EXIT_CRITICAL();
- return 0;
- }
- sample_can_t g_sampler_can1 = {
- .name = "can1",
- .tsjw = CAN_SJW_1tq,
- .tbs2 = CAN_BS2_3tq,
- .tbs1 = CAN_BS1_10tq,
- .brp = 12,
- .mode = CAN_Mode_Normal,
- .init = CAN1_Mode_Init,
- .recv_msg = CAN1_Receive_Msg_Ex
- };
- sample_can_t g_sampler_can2 = {
- .name = "can2",
- .tsjw = CAN_SJW_1tq,
- .tbs2 = CAN_BS2_3tq,
- .tbs1 = CAN_BS1_10tq,
- .brp = 12, //CAN1初始化成功 波特率为 250K
- .mode = CAN_Mode_Normal,
- .init = CAN2_Mode_Init,
- .recv_msg = CAN2_Receive_Msg_Ex
- };
- static int sample_can_start(unsigned char channel)
- {
- switch(channel) {
- case 0x00: //默认不开启can
- printf("\r\nCAN不初始化\r\n");
- break;
- case 0x01: //开启can1
- add_can_sample_dev(&g_sampler_can1.node);
- break;
- case 0x02: //开启can2
- add_can_sample_dev(&g_sampler_can2.node);
- break;
- case 0x03: //开启can3
- add_can_sample_dev(&g_sampler_can1.node);
- add_can_sample_dev(&g_sampler_can2.node);
- break;
- default:
- break;
- }
- return 0;
- }
- static sample_interface_t dev_can = {
- .name = "can",
- .pdev = &dev_can_head,
- .methods = &sample_can_methords
- };
- int sample_can_init(unsigned char channel)
- {
- INIT_LIST_HEAD(&dev_can_head);
-
-
- sample_can_t *pos;
- sample_can_start(channel);
- list_for_each_entry(pos,&dev_can_head,node) {
- if(pos->init != NULL) {
- printf("+++++++ can init:init:0x%x pos->tsjw:%d\n",pos->init,pos->tsjw);
- pos->init(pos->tsjw,pos->tbs2,pos->tbs1,pos->brp,pos->mode);
- }
- }
- int ret = register_sampler(&dev_can);
-
- return ret;
- }
|