summaryrefslogtreecommitdiff
path: root/bsp/ls1cdev/drivers/drv_can.c
blob: 42745e438b4e0044e447efa36eed55ab64e152c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author            Notes
 * 2018-01-06     sundm75       first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <rthw.h>

#include <drv_can.h>

#include "ls1c.h"
#include "../libraries/ls1c_public.h"
#include "../libraries/ls1c_regs.h"
#include "../libraries/ls1c_clock.h"
#include "../libraries/ls1c_can.h"
#include "../libraries/ls1c_pin.h"


#ifdef RT_USING_CAN

CanRxMsg RxMessage;

struct ls1c_bxcan
{
    CAN_TypeDef *reg;
    void * irq;
};

static rt_err_t bxmodifyfilter(struct ls1c_bxcan *pbxcan, struct rt_can_filter_item *pitem, rt_uint32_t actived)
{
    rt_int32_t fcase;
    rt_err_t res;
    rt_int32_t hdr, fbase, foff;
    CAN_TypeDef* CANx;
    CANx = pbxcan->reg;
    
    /*pitem->mode     1-掩码模式; 0- 滤波器模式 SJA1000中使用以下方式*/
    /*SJA1000中AFM    1-单滤波器模式; 0- 双滤波器模式 */

    fcase = pitem->mode;/*1-单滤波器模式; 0- 双滤波器模式*/
    {
        if (!actived)
        {
            return RT_EOK;
        }
        else if (pitem->hdr == -1)
        {
            res = -1;
            if (res != RT_EOK)
            {
                return res;
            }
        }
        else if (pitem->hdr >= 0)
        {
            rt_enter_critical();
            res = RT_EOK;
            if (res != RT_EOK)
            {
                return res;
            }
            hdr = pitem->hdr;
            rt_exit_critical();
        }
    }

    CAN_FilterInitTypeDef   CAN_FilterInitStruct;
    unsigned char ide, rtr, id , idmask, mode; 
    ide =  (unsigned char) pitem->ide;
    rtr = (unsigned char)  pitem->rtr;
    id = pitem->id;
    idmask = pitem->mask;
    mode = (unsigned char) pitem->mode;
    CAN_FilterInitStruct.IDE = ide;
    CAN_FilterInitStruct.RTR = rtr;
    CAN_FilterInitStruct.ID = id;
    CAN_FilterInitStruct.IDMASK = idmask;
    CAN_FilterInitStruct.MODE = mode;
    CAN_FilterInit(CANx, &CAN_FilterInitStruct);
    
    return RT_EOK;
}

static rt_err_t setfilter(struct ls1c_bxcan *pbxcan, struct rt_can_filter_config *pconfig)
{
    struct rt_can_filter_item *pitem = pconfig->items;
    rt_uint32_t count = pconfig->count;
    rt_err_t res;
    while (count)
    {
        res = bxmodifyfilter(pbxcan, pitem, pconfig->actived);
        if (res != RT_EOK)
        {
            return res;
        }
        pitem++;
        count--;
    }
    return RT_EOK;
}

static void bxcan0_filter_init(struct rt_can_device *can)
{
        struct ls1c_bxcan *pbxcan;
        pbxcan = (struct ls1c_bxcan *) can->parent.user_data; 
    
}

static void bxcan1_filter_init(struct rt_can_device *can)
{
        struct ls1c_bxcan *pbxcan;
        pbxcan = (struct ls1c_bxcan *) can->parent.user_data; 
    
}

static void bxcan_init(CAN_TypeDef *pcan, rt_uint32_t baud, rt_uint32_t mode)
{
    CAN_InitTypeDef        CAN_InitStructure;

    Ls1c_CanBPS_t bps ;

         switch(baud)
            {
                case CAN1MBaud:
                    bps = LS1C_CAN1MBaud;
          break;
                case CAN800kBaud:
                    bps = LS1C_CAN800kBaud;
          break;
                case CAN500kBaud:
                    bps = LS1C_CAN500kBaud;
          break;
                case CAN250kBaud:
                    bps = LS1C_CAN250kBaud;
          break;
                case CAN125kBaud:
                    bps = LS1C_CAN125kBaud;
          break;
                case CAN50kBaud:
                    bps = LS1C_CAN40kBaud;
          break;
                default:
                    bps = LS1C_CAN250kBaud;
          break;
            }

    switch (mode)
    {
    case RT_CAN_MODE_NORMAL:
        CAN_InitStructure.CAN_Mode = 0x00;
        break;
    case RT_CAN_MODE_LISEN:
        CAN_InitStructure.CAN_Mode = CAN_Mode_LOM;
        break;
    case RT_CAN_MODE_LOOPBACK:
        CAN_InitStructure.CAN_Mode = CAN_Mode_STM;
        
        break;
    case RT_CAN_MODE_LOOPBACKANLISEN:
        CAN_InitStructure.CAN_Mode = CAN_Mode_STM|CAN_Mode_LOM;
        break;
    }
    CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;

    switch (bps)
    {
        case LS1C_CAN1MBaud: 
            CAN_InitStructure.CAN_Prescaler = 9;
            CAN_InitStructure.CAN_BS1 = CAN_BS1_4tq;
            CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
        break;
        case LS1C_CAN800kBaud: 
            CAN_InitStructure.CAN_Prescaler = 8;
            CAN_InitStructure.CAN_BS1 = CAN_BS1_7tq;
            CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
        break;
        case LS1C_CAN500kBaud: 
            CAN_InitStructure.CAN_Prescaler = 9;
            CAN_InitStructure.CAN_BS1 = CAN_BS1_11tq;
            CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
        break;
        case LS1C_CAN250kBaud: 
            CAN_InitStructure.CAN_Prescaler = 36;
            CAN_InitStructure.CAN_BS1 = CAN_BS1_4tq;
            CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
        break;
        case LS1C_CAN125kBaud: 
            CAN_InitStructure.CAN_Prescaler = 36;
            CAN_InitStructure.CAN_BS1 = CAN_BS1_11tq;
            CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
        break;
        case LS1C_CAN100kBaud: 
            CAN_InitStructure.CAN_Prescaler = 63;
            CAN_InitStructure.CAN_BS1 = CAN_BS1_7tq;
            CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
        break;
        case LS1C_CAN50kBaud: 
            CAN_InitStructure.CAN_Prescaler = 63;
            CAN_InitStructure.CAN_BS1 = CAN_BS1_16tq;
            CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;
        break;
        default: //250K
            CAN_InitStructure.CAN_Prescaler = 36;
            CAN_InitStructure.CAN_BS1 = CAN_BS1_4tq;
            CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
        break;
    }

    CAN_Init(pcan, &CAN_InitStructure);
}

#ifdef USING_BXCAN0
static void bxcan0_hw_init(void)
{
    pin_set_purpose(54, PIN_PURPOSE_OTHER);
    pin_set_purpose(55, PIN_PURPOSE_OTHER);
    pin_set_remap(54, PIN_REMAP_THIRD);
    pin_set_remap(55, PIN_REMAP_THIRD);
}
#endif

#ifdef USING_BXCAN1
static void bxcan1_hw_init(void)
{
    pin_set_purpose(56, PIN_PURPOSE_GPIO);
    pin_set_purpose(57, PIN_PURPOSE_GPIO);
    pin_set_remap(56, PIN_REMAP_DEFAULT);
    pin_set_remap(57, PIN_REMAP_DEFAULT);
}
#endif

static rt_err_t configure(struct rt_can_device *can, struct can_configure *cfg)
{
    CAN_TypeDef *pbxcan;

    pbxcan = ((struct ls1c_bxcan *) can->parent.user_data)->reg;
    if (pbxcan == CAN0)
    {
#ifdef USING_BXCAN0
        bxcan0_hw_init();  
        bxcan_init(pbxcan, cfg->baud_rate, cfg->mode);
#endif
    }
    else  if (pbxcan == CAN1)
    {
#ifdef USING_BXCAN1
        bxcan1_hw_init();  
        bxcan_init(pbxcan, cfg->baud_rate, cfg->mode);
#endif
    }
    return RT_EOK;
}

static rt_err_t control(struct rt_can_device *can, int cmd, void *arg)
{
    struct ls1c_bxcan *pbxcan;
    rt_uint32_t argval;

    pbxcan = (struct ls1c_bxcan *) can->parent.user_data;
    switch (cmd)
    {
    case RT_CAN_CMD_SET_FILTER:
          return setfilter(pbxcan, (struct rt_can_filter_config *) arg);
        break;
    case RT_CAN_CMD_SET_MODE:
        argval = (rt_uint32_t) arg;
        if (argval != RT_CAN_MODE_NORMAL ||
                argval != RT_CAN_MODE_LISEN ||
                argval != RT_CAN_MODE_LOOPBACK ||
                argval != RT_CAN_MODE_LOOPBACKANLISEN)
        {
            return RT_ERROR;
        }
        if (argval != can->config.mode)
        {
            can->config.mode = argval;
            return CAN_SetMode(pbxcan->reg, argval);
        }
        break;
    case RT_CAN_CMD_SET_BAUD:
        argval = (rt_uint32_t) arg;
        if (argval != CAN1MBaud &&
                argval != CAN800kBaud &&
                argval != CAN500kBaud &&
                argval != CAN250kBaud &&
                argval != CAN125kBaud &&
                argval != CAN100kBaud &&
                argval != CAN50kBaud  )
        {
            return RT_ERROR;
        }
        if (argval != can->config.baud_rate)
        {
            can->config.baud_rate = argval;
         Ls1c_CanBPS_t bps;
         switch(argval)
            {
                case CAN1MBaud:
                    bps = LS1C_CAN1MBaud;
          break;
                case CAN800kBaud:
                    bps = LS1C_CAN800kBaud;
          break;
                case CAN500kBaud:
                    bps = LS1C_CAN500kBaud;
          break;
                case CAN250kBaud:
                    bps = LS1C_CAN250kBaud;
          break;
                case CAN125kBaud:
                    bps = LS1C_CAN125kBaud;
          break;
                case CAN50kBaud:
                    bps = LS1C_CAN40kBaud;
          break;
                default:
                    bps = LS1C_CAN250kBaud;
          break;
            }
            return CAN_SetBps( pbxcan->reg,  bps);
        }
        break;
    case RT_CAN_CMD_GET_STATUS:
    { 
        rt_uint32_t errtype;

        errtype = pbxcan->reg->RXERR;
        can->status.rcverrcnt = errtype ;
        errtype = pbxcan->reg->TXERR;
        can->status.snderrcnt = errtype ;
        errtype = pbxcan->reg->ECC;
        can->status.errcode = errtype ;
        if (arg != &can->status)
        {
            rt_memcpy(arg, &can->status, sizeof(can->status));
        }
    }
    break;
    }

    return RT_EOK;
}

static int sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t boxno)
{
    CAN_TypeDef *pbxcan;
    CanTxMsg TxMessage;
    struct rt_can_msg *pmsg = (struct rt_can_msg *) buf;
    int i;

    pbxcan = ((struct ls1c_bxcan *) can->parent.user_data)->reg;

    TxMessage.StdId = pmsg->id;
    TxMessage.ExtId = pmsg->id;
    TxMessage.RTR = pmsg->rtr;
    TxMessage.IDE = pmsg->ide;
    TxMessage.DLC = pmsg->len;
    for( i=0; i<TxMessage.DLC ;i++)
    {
      TxMessage.Data[i] = pmsg->data[i];
    }
    CAN_Transmit(pbxcan, &TxMessage);

    return RT_EOK;
}

static int recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t boxno)
{
    CAN_TypeDef *pbxcan;

   struct rt_can_msg *pmsg = (struct rt_can_msg *) buf;
   int i;

    pbxcan = ((struct ls1c_bxcan *) can->parent.user_data)->reg;

    pmsg->ide = (rt_uint32_t) RxMessage.IDE; 
    if(RxMessage.IDE == 1)
        pmsg->id = RxMessage.ExtId;
    else
        pmsg->id = RxMessage.StdId;
    pmsg->len = RxMessage.DLC;
    pmsg->rtr = RxMessage.RTR;
    pmsg->hdr = 0;
    for(i= 0;i< RxMessage.DLC; i++)
    {
      pmsg->data[i] = RxMessage.Data[i];
    }
     return RT_EOK;
}

static const struct rt_can_ops canops =
{
    configure,
    control,
    sendmsg,
    recvmsg,
};

#ifdef USING_BXCAN0
struct rt_can_device bxcan0;
void ls1c_can0_irqhandler(int irq, void *param)  
{  
    CAN_TypeDef* CANx;
    unsigned char status;
    CANx =  CAN0;
    /*读寄存器清除中断*/
   status = CANx->IR;
   
    /*接收中断*/
    if (( status & CAN_IR_RI) == CAN_IR_RI) 
    {
        /*清除RI 中断*/
       CAN_Receive(CANx, &RxMessage);
       CANx->CMR |= CAN_CMR_RRB; 
       CANx->CMR |= CAN_CMR_CDO; 
       rt_hw_can_isr(&bxcan0, RT_CAN_EVENT_RX_IND);
       rt_kprintf("\r\nCan0 int RX happened!\r\n");
    }
    /*发送中断*/
    else if (( status  & CAN_IR_TI) == CAN_IR_TI) 
    {
       rt_hw_can_isr(&bxcan0, RT_CAN_EVENT_TX_DONE | 0 << 8);
       rt_kprintf("\r\nCan0 int TX happened!\r\n");
    }
    /*数据溢出中断*/
    else if (( status  & CAN_IR_DOI) == CAN_IR_DOI) 
    {
       rt_hw_can_isr(&bxcan0, RT_CAN_EVENT_RXOF_IND);
       rt_kprintf("\r\nCan0 int RX OF happened!\r\n");
    }
}  
static struct ls1c_bxcan bxcan0data =
{
    .reg = CAN0,
    .irq = ls1c_can0_irqhandler,
};
#endif /*USING_BXCAN0*/

#ifdef USING_BXCAN1
struct rt_can_device bxcan1;
void ls1c_can1_irqhandler(int irq, void *param)  
{  
    CAN_TypeDef* CANx;
    unsigned char status;
    CANx =  CAN1;
    /*读寄存器清除中断*/
   status = CANx->IR;
   
    /*接收中断*/
    if (( status & CAN_IR_RI) == CAN_IR_RI) 
    {
        /*清除RI 中断*/
       CAN_Receive(CANx, &RxMessage);
       CANx->CMR |= CAN_CMR_RRB; 
       CANx->CMR |= CAN_CMR_CDO; 
       rt_hw_can_isr(&bxcan1, RT_CAN_EVENT_RX_IND);
       rt_kprintf("\r\nCan1 int RX happened!\r\n");
    }
    /*发送中断*/
    else if (( status  & CAN_IR_TI) == CAN_IR_TI) 
    {
       rt_hw_can_isr(&bxcan1, RT_CAN_EVENT_TX_DONE | 0 << 8);
       rt_kprintf("\r\nCan1 int TX happened!\r\n");
    }
    /*数据溢出中断*/
    else if (( status  & CAN_IR_DOI) == CAN_IR_DOI) 
    {
       rt_hw_can_isr(&bxcan1, RT_CAN_EVENT_RXOF_IND);
       rt_kprintf("\r\nCan1 int RX OF happened!\r\n");
    }
}  
static struct ls1c_bxcan bxcan1data =
{
    .reg = CAN1,
    .irq = ls1c_can1_irqhandler,
};

#endif /*USING_BXCAN1*/

int ls1c_bxcan_init(void)
{

#ifdef USING_BXCAN0
    bxcan0.config.baud_rate = CAN250kBaud;
    bxcan0.config.msgboxsz = 1;
    bxcan0.config.sndboxnumber = 1;
    bxcan0.config.mode = RT_CAN_MODE_NORMAL;
    bxcan0.config.privmode = 0;
    bxcan0.config.ticks = 50;
#ifdef RT_CAN_USING_HDR
    bxcan0.config.maxhdr = 2;
#endif
    rt_hw_can_register(&bxcan0, "bxcan0", &canops, &bxcan0data);
    rt_kprintf("\r\ncan0 register! \r\n");
    
    rt_hw_interrupt_install(LS1C_CAN0_IRQ,( rt_isr_handler_t)bxcan0data.irq , RT_NULL, "can0");  
    rt_hw_interrupt_umask(LS1C_CAN0_IRQ);  
#endif
#ifdef USING_BXCAN1
    bxcan1.config.baud_rate = CAN250kBaud;
    bxcan1.config.msgboxsz = 1;
    bxcan1.config.sndboxnumber = 1;
    bxcan1.config.mode = RT_CAN_MODE_NORMAL;
    bxcan1.config.privmode = 0;
    bxcan1.config.ticks = 50;
#ifdef RT_CAN_USING_HDR
    bxcan1.config.maxhdr = 2;
#endif
    rt_hw_can_register(&bxcan1, "bxcan1", &canops, &bxcan1data);
    rt_kprintf("\r\ncan1 register! \r\n");
    
    rt_hw_interrupt_install(LS1C_CAN1_IRQ,( rt_isr_handler_t)bxcan1data.irq , RT_NULL, "can1");  
    rt_hw_interrupt_umask(LS1C_CAN1_IRQ); 
#endif
    return RT_EOK;
}

INIT_BOARD_EXPORT(ls1c_bxcan_init);

#endif /*RT_USING_CAN*/