summaryrefslogtreecommitdiffstatsabout
path: root/SylixOS/driver/can/am335x_can.c
blob: e8ff67d958da5b277ddf849da3a6638923d3ec48 (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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*********************************************************************************************************
**
**                                    中国软件开源组织
**
**                                   嵌入式实时操作系统
**
**                                       SylixOS(TM)
**
**                               Copyright  All Rights Reserved
**
**--------------文件信息--------------------------------------------------------------------------------
**
** 文   件   名: am335x_can.c
**
** 创   建   人: Jiao.JinXing (焦进星)
**
** 文件创建日期: 2015 年 03 月 10 日
**
** 描        述: AM335X 处理器 CAN 驱动(总线驱动部分)
*********************************************************************************************************/
#define  __SYLIXOS_STDIO
#define  __SYLIXOS_KERNEL
#include "config.h"
#include "SylixOS.h"

#include "driver/am335x_common.h"
#include "dcan_frame.h"
#include "am335x_can.h"
#include "dcan.h"
/*********************************************************************************************************
  定义
*********************************************************************************************************/
#define __DCAN_NR                           (2)

#define __DCAN_NO_INT_PENDING               (0x00000000u)
#define __DCAN_ERROR_OCCURED                (0x8000u)
#define __DCAN_BIT_RATE                     (125000u)
#define __DCAN_IN_CLK                       (24000000u)
/*********************************************************************************************************
  CAN 通道控制块类型定义
*********************************************************************************************************/
typedef struct {
    addr_t              CANCHAN_ulPhyAddrBase;                          /*  物理地址基地址              */
    size_t              CANCHAN_stPhyAddrSize;                          /*  物理地址空间大小            */
    ULONG               CANCHAN_ulVector;                               /*  中断向量                    */
    ULONG               CANCHAN_ulParityVector;
    UINT                CANCHAN_uiIntPriority;                          /*  中断优先级                  */

    addr_t              CANCHAN_ulVirtAddrBase;                         /*  虚拟地址基地址              */
    PAM335X_PINMUX      CANCHAN_pPinMux;

    CAN_CHAN            CANCHAN_canChan;

    UINT                CANCHAN_uiBaud;

    INT               (*CANCHAN_pcbSetBusState)();                      /*  总线状态回调                */
    INT               (*CANCHAN_pcbGetTx)();                            /*  中断回调函数                */
    INT               (*CANCHAN_pcbPutRcv)();

    PVOID               CANCHAN_pvGetTxArg;                             /*  回调函数句柄                */
    PVOID               CANCHAN_pvPutRcvArg;
    PVOID               CANCHAN_pvSetBusStateArg;

    UINT                CANCHAN_uiId;

} __AM335X_CAN_CHAN;
typedef __AM335X_CAN_CHAN        *__PAM335X_CAN_CHAN;                   /*  指针类型                    */
/*********************************************************************************************************
  CAN 通道控制块数组定义
*********************************************************************************************************/
static __AM335X_CAN_CHAN  _G_am335xCanChannels[__DCAN_NR] = {
        {
                SOC_DCAN_0_REGS,
                LW_CFG_VMM_PAGE_SIZE,
                SYS_INT_DCAN0_INT0,
                SYS_INT_DCAN0_PARITY,
                BSP_CFG_CAN0_INT_PRIO,
        },

        {
                SOC_DCAN_1_REGS,
                LW_CFG_VMM_PAGE_SIZE,
                SYS_INT_DCAN1_INT0,
                SYS_INT_DCAN1_PARITY,
                BSP_CFG_CAN1_INT_PRIO,
        },
};
/*********************************************************************************************************
  CAN 操作锁定义
*********************************************************************************************************/
static LW_SPINLOCK_DEFINE(_G_am335xCanSpl);

#define __AM335X_CAN_OP_LOCK()      LW_SPIN_LOCK_QUICK(&_G_am335xCanSpl, &intReg)

#define __AM335X_CAN_OP_UNLOCK()    LW_SPIN_UNLOCK_QUICK(&_G_am335xCanSpl, intReg)
/*********************************************************************************************************
** 函数名称: __am335xCanHwInit
** 功能描述: 初始化 CAN 硬件
** 输 入  : pCanChan                  CAN 通道控制块指针
** 输 出  : ERROR_CODE
** 全局变量:
** 调用模块:
*********************************************************************************************************/
static INT  __am335xCanHwInit (__PAM335X_CAN_CHAN  pCanChan)
{
    UINT            uiChannel = pCanChan - _G_am335xCanChannels;
    INT             i;
    INTREG          intReg;

    /*
     * Configuring the system clocks for CAN instance.
     */
    switch (uiChannel) {
    case 0:
        am335xEnableModuleClock(am335xModuleIdGet("DCAN0"));

        __AM335X_CAN_OP_LOCK();

        /*
         * initializes the DCAN message RAM
         */
        write32(read32(SOC_CONTROL_REGS + CONTROL_DCAN_RAMINIT) |
                CONTROL_DCAN_RAMINIT_DCAN0_RAMINIT_START,
                SOC_CONTROL_REGS + CONTROL_DCAN_RAMINIT);

        __AM335X_CAN_OP_UNLOCK();
        break;

    case 1:
        am335xEnableModuleClock(am335xModuleIdGet("DCAN1"));

        __AM335X_CAN_OP_LOCK();

        /*
         * initializes the DCAN message RAM
         */
        write32(read32(SOC_CONTROL_REGS + CONTROL_DCAN_RAMINIT) |
                CONTROL_DCAN_RAMINIT_DCAN1_RAMINIT_START,
                SOC_CONTROL_REGS + CONTROL_DCAN_RAMINIT);

        __AM335X_CAN_OP_UNLOCK();
        break;

    default:
        return  (PX_ERROR);
    }

    /*
     * Performing the Pin Multiplexing for CAN instance.
     */
    am335xPinMuxSetup(pCanChan->CANCHAN_pPinMux);

    /* Reset the DCAN module */
    DCANReset(pCanChan->CANCHAN_ulVirtAddrBase);

    /* Enter the Initialization mode of CAN controller */
    DCANInitModeSet(pCanChan->CANCHAN_ulVirtAddrBase);

    /* Enable the write access to the DCAN configuration registers */
    DCANConfigRegWriteAccessControl(pCanChan->CANCHAN_ulVirtAddrBase, DCAN_CONF_REG_WR_ACCESS_ENABLE);

    /* Configure the bit timing values for CAN communication */
    CANSetBitTiming(pCanChan->CANCHAN_ulVirtAddrBase, __DCAN_IN_CLK, pCanChan->CANCHAN_uiBaud);

    /* Disable the write access to the DCAN configuration registers */
    DCANConfigRegWriteAccessControl(pCanChan->CANCHAN_ulVirtAddrBase, DCAN_CONF_REG_WR_ACCESS_DISABLE);

    i = CAN_NUM_OF_MSG_OBJS;
    while (i--) {
        /* Invalidate all message objects in the message RAM */
        CANInValidateMsgObject(pCanChan->CANCHAN_ulVirtAddrBase, i, DCAN_IF2_REG);
    }

    /* Start the CAN transfer */
    DCANNormalModeSet(pCanChan->CANCHAN_ulVirtAddrBase);

    /* Enable the error interrupts */
    DCANIntEnable(pCanChan->CANCHAN_ulVirtAddrBase, DCAN_ERROR_INT);

    /* Enable the interrupt line 0 of DCAN module */
    DCANIntLineEnable(pCanChan->CANCHAN_ulVirtAddrBase, DCAN_INT_LINE0);

    return  (ERROR_NONE);
}
/*********************************************************************************************************
** 函数名称: __am335xCanCbInstall
** 功能描述: CAN 通道安装回调函数
** 输 入  : pcanchan                  CAN 通道
**           iCallbackType             回调类型
**           callbackRoute             回调函数
**           pvCallbackArg             回调参数
** 输 出  : ERROR_CODE
** 全局变量:
** 调用模块:
*********************************************************************************************************/
static INT  __am335xCanCbInstall (CAN_CHAN          *pcanchan,
                                  INT                iCallbackType,
                                  CAN_CALLBACK       callbackRoute,
                                  PVOID              pvCallbackArg)
{
    __PAM335X_CAN_CHAN      pCanChan = container_of(pcanchan,
                                                __AM335X_CAN_CHAN, CANCHAN_canChan);

    switch (iCallbackType) {

    case CAN_CALLBACK_GET_TX_DATA:                                      /*  发送回调函数                */
        pCanChan->CANCHAN_pcbGetTx   = callbackRoute;
        pCanChan->CANCHAN_pvGetTxArg = pvCallbackArg;
        return    (ERROR_NONE);

    case CAN_CALLBACK_PUT_RCV_DATA:                                     /*  接收回调函数                */
        pCanChan->CANCHAN_pcbPutRcv   = callbackRoute;
        pCanChan->CANCHAN_pvPutRcvArg = pvCallbackArg;
        return    (ERROR_NONE);                                         /*  总线状态函数                */

    case CAN_CALLBACK_PUT_BUS_STATE:
        pCanChan->CANCHAN_pcbSetBusState   = callbackRoute;
        pCanChan->CANCHAN_pvSetBusStateArg = pvCallbackArg;
        return    (ERROR_NONE);

    default:
        _ErrorHandle(ENOSYS);
        return    (PX_ERROR);
    }
}
/*********************************************************************************************************
** 函数名称: __am335xSioStartup
** 功能描述: CAN 通道发送(没有使用中断)
** 输 入  : pcanchan                  CAN 通道
** 输 出  : ERROR_CODE
** 全局变量:
** 调用模块:
*********************************************************************************************************/
static INT   __am335xCanStartup (CAN_CHAN    *pcanchan)
{
    __PAM335X_CAN_CHAN      pCanChan = container_of(pcanchan,
                                                __AM335X_CAN_CHAN, CANCHAN_canChan);
    CAN_FRAME               canFrame;
    can_frame               dcanFrame;
    UINT                    auiData[2];

    if (pCanChan->CANCHAN_pcbGetTx) {
        if (pCanChan->CANCHAN_pcbGetTx(pCanChan->CANCHAN_pvGetTxArg,    /* 从发送缓冲区中读取数据发送   */
                                       &canFrame) == ERROR_NONE) {

            /* Populate the can_frame structure with the CAN frame information */
            dcanFrame.id  = canFrame.CAN_uiId;
            dcanFrame.dlc = canFrame.CAN_ucLen;

            if (canFrame.CAN_bExtId) {
                dcanFrame.flag = (CAN_EXT_FRAME | CAN_DATA_FRAME | CAN_MSG_DIR_TX);
            } else {
                dcanFrame.flag = (CAN_DATA_FRAME | CAN_MSG_DIR_TX);
            }

            if (!canFrame.CAN_bRtr) {
                lib_memcpy(auiData, canFrame.CAN_ucData, canFrame.CAN_ucLen);
                dcanFrame.data = (UINT *)auiData;
            } else {
                dcanFrame.data = LW_NULL;
            }

            API_InterVectorDisable(pCanChan->CANCHAN_ulVector);         /*  关闭 CAN 中断               */
            API_InterVectorDisable(pCanChan->CANCHAN_ulParityVector);   /*  关闭 CAN Parity 中断        */

            /* Configure a transmit message object */
            CANMsgObjectConfig(pCanChan->CANCHAN_ulVirtAddrBase, &dcanFrame);

            API_InterVectorEnable(pCanChan->CANCHAN_ulVector);          /*  打开 CAN 中断               */
            API_InterVectorEnable(pCanChan->CANCHAN_ulParityVector);    /*  打开 CAN Parity 中断        */
        }
    }

    return  (ERROR_NONE);
}
/*********************************************************************************************************
** 函数名称: __am335xCanStartupRx
** 功能描述: CAN 通道接收(没有使用中断)
** 输 入  : pCanChan                  CAN 通道
** 输 出  : ERROR_CODE
** 全局变量:
** 调用模块:
*********************************************************************************************************/
static INT   __am335xCanStartupRx (CAN_CHAN    *pcanchan)
{
    __PAM335X_CAN_CHAN      pCanChan = container_of(pcanchan,
                                                    __AM335X_CAN_CHAN, CANCHAN_canChan);
    can_frame               dcanFrame;

    dcanFrame.flag = (CAN_MSG_DIR_RX | CAN_DATA_FRAME);
    dcanFrame.id   = pCanChan->CANCHAN_uiId;
    dcanFrame.dlc  = 0;
    dcanFrame.data = LW_NULL;

    API_InterVectorDisable(pCanChan->CANCHAN_ulVector);                 /*  关闭 CAN 中断               */
    API_InterVectorDisable(pCanChan->CANCHAN_ulParityVector);           /*  关闭 CAN Parity 中断        */

    /*
     * Configure a receive message object to accept CAN
     * frames with standard ID.
     */
    CANMsgObjectConfig(pCanChan->CANCHAN_ulVirtAddrBase, &dcanFrame);

    dcanFrame.flag = (CAN_EXT_FRAME | CAN_MSG_DIR_RX | CAN_DATA_FRAME);

    /*
     * Configure a receive message object to accept CAN
     * frames with extended ID.
     */
    CANMsgObjectConfig(pCanChan->CANCHAN_ulVirtAddrBase, &dcanFrame);

    API_InterVectorEnable(pCanChan->CANCHAN_ulVector);                  /*  打开 CAN 中断               */
    API_InterVectorEnable(pCanChan->CANCHAN_ulParityVector);            /*  打开 CAN Parity 中断        */

    return  (ERROR_NONE);
}
/*********************************************************************************************************
** 函数名称: __am335xCanIsr
** 功能描述: CAN 通道中断服务例程
** 输 入  : pCanChan                  CAN 通道
**           ulVector                  中断向量
** 输 出  : 中断返回值
** 全局变量:
** 调用模块:
*********************************************************************************************************/
static irqreturn_t  __am335xCanIsr (__PAM335X_CAN_CHAN  pCanChan, ULONG  ulVector)
{
    UINT                    uiErrorValue;
    UINT                    uiMsgNum;
    CAN_FRAME               canFrame;
    UINT                    uiId;
    UINT                    auiData[2];

    while (DCANIntRegStatusGet(pCanChan->CANCHAN_ulVirtAddrBase, DCAN_INT_LINE0_STAT)) {

        if (DCANIntRegStatusGet(pCanChan->CANCHAN_ulVirtAddrBase,
                                DCAN_INT_LINE0_STAT) == __DCAN_ERROR_OCCURED) {

            /* Check the status of DCAN Status and error register */
            uiErrorValue = DCANErrAndStatusRegInfoGet(pCanChan->CANCHAN_ulVirtAddrBase);

            if (uiErrorValue & DCAN_MOD_IN_BUS_OFF_STATE) {

                pCanChan->CANCHAN_pcbSetBusState(pCanChan->CANCHAN_pvSetBusStateArg,
                                                 CAN_DEV_BUS_OFF);

                /*
                 * This feature will automatically get the CAN bus to bus-on
                 * state once the error counters are below the error warning
                 * limit.
                 */
                DCANAutoBusOnControl(pCanChan->CANCHAN_ulVirtAddrBase, DCAN_AUTO_BUS_ON_ENABLE);
            }

            if (uiErrorValue & DCAN_ERR_WARN_STATE_RCHD) {

                pCanChan->CANCHAN_pcbSetBusState(pCanChan->CANCHAN_pvSetBusStateArg,
                                                 CAN_DEV_BUS_LIMIT);
            }

            if (uiErrorValue & DCAN_CORE_IN_ERR_PASSIVE) {

                pCanChan->CANCHAN_pcbSetBusState(pCanChan->CANCHAN_pvSetBusStateArg,
                                                 CAN_DEV_BUS_PASSIVE);
            }
        } else {
            pCanChan->CANCHAN_pcbSetBusState(pCanChan->CANCHAN_pvSetBusStateArg,
                                             CAN_DEV_BUS_ERROR_NONE);
        }

        if ((DCANIntRegStatusGet(pCanChan->CANCHAN_ulVirtAddrBase,
                                 DCAN_INT_LINE0_STAT) != __DCAN_NO_INT_PENDING) &&
           ((DCANIntRegStatusGet(pCanChan->CANCHAN_ulVirtAddrBase,
                                 DCAN_INT_LINE0_STAT) != __DCAN_ERROR_OCCURED))) {

            /* Get the number of the message object which caused the interrupt */
            uiMsgNum = DCANIntRegStatusGet(pCanChan->CANCHAN_ulVirtAddrBase, DCAN_INT_LINE0_STAT);

            /* Interrupt handling for transmit objects */
            if (uiMsgNum < (CAN_NUM_OF_MSG_OBJS / 2)) {

                /* Clear the Interrupt pending status */
                CANClrIntPndStat(pCanChan->CANCHAN_ulVirtAddrBase, uiMsgNum, DCAN_IF1_REG);

                /* Disable the transmit interrupt of the message object */
                CANTxIntDisable(pCanChan->CANCHAN_ulVirtAddrBase, uiMsgNum, DCAN_IF1_REG);

                /* Invalidate the transmit message object */
                CANInValidateMsgObject(pCanChan->CANCHAN_ulVirtAddrBase, uiMsgNum, DCAN_IF1_REG);

                __am335xCanStartup(&pCanChan->CANCHAN_canChan);
            }

            if ((uiMsgNum >= (CAN_NUM_OF_MSG_OBJS / 2)) && (uiMsgNum <= CAN_NUM_OF_MSG_OBJS)) {

                /* Read a received message from message RAM to interface register */
                CANReadMsgObjData(pCanChan->CANCHAN_ulVirtAddrBase, uiMsgNum, (UINT *)auiData,
                                  DCAN_IF2_REG);

                canFrame.CAN_ucLen = (DCANIFMsgCtlStatusGet(pCanChan->CANCHAN_ulVirtAddrBase,
                                                            DCAN_IF2_REG) & DCAN_DAT_LEN_CODE_READ);

                uiId = DCANIFArbStatusGet(pCanChan->CANCHAN_ulVirtAddrBase, DCAN_IF2_REG);

                /* Clear the Interrupt pending status */
                CANClrIntPndStat(pCanChan->CANCHAN_ulVirtAddrBase, uiMsgNum, DCAN_IF2_REG);

                if ((uiId & DCAN_EXT_ID_READ) == DCAN_29_BIT_ID) {
                    canFrame.CAN_bExtId = LW_TRUE;
                    canFrame.CAN_uiId   = uiId & DCAN_MSG_ID_READ;
                } else {
                    canFrame.CAN_bExtId = LW_FALSE;
                    canFrame.CAN_uiId   = (uiId & DCAN_MSG_ID_READ) >> 18;
                }

                canFrame.CAN_bRtr       = LW_FALSE;
                canFrame.CAN_uiChannel  = 0;

                lib_memcpy(canFrame.CAN_ucData, auiData, canFrame.CAN_ucLen);

                if (pCanChan->CANCHAN_pcbPutRcv) {
                    if (pCanChan->CANCHAN_pcbPutRcv(pCanChan->CANCHAN_pvPutRcvArg,
                                                    &canFrame) != ERROR_NONE) {
                        pCanChan->CANCHAN_pcbSetBusState(pCanChan->CANCHAN_pvSetBusStateArg,
                                                         CAN_DEV_BUS_RXBUFF_OVERRUN);
                    }
                }

                /* Disable the receive interrupt of the message object */
                CANRxIntDisable(pCanChan->CANCHAN_ulVirtAddrBase, uiMsgNum, DCAN_IF2_REG);

                /* Invalidate the receive message object */
                CANInValidateMsgObject(pCanChan->CANCHAN_ulVirtAddrBase, uiMsgNum, DCAN_IF2_REG);

                __am335xCanStartupRx(&pCanChan->CANCHAN_canChan);
            }
        }
    }

    return  (LW_IRQ_HANDLED);
}
/*********************************************************************************************************
** 函数名称: __am335xCanParityIsr
** 功能描述: CAN 通道 Parity 中断服务例程
** 输 入  : pCanChan                  CAN 通道
**           ulVector                  中断向量
** 输 出  : 中断返回值
** 全局变量:
** 调用模块:
*********************************************************************************************************/
static irqreturn_t  __am335xCanParityIsr (__PAM335X_CAN_CHAN  pCanChan, ULONG  ulVector)
{
    UINT            uiErrorValue;
    UINT            uiWordNum;
    UINT            uiMsgNum;

    (VOID)uiWordNum;
    (VOID)uiMsgNum;

    if (DCANIntRegStatusGet(pCanChan->CANCHAN_ulVirtAddrBase,
                            DCAN_INT_LINE0_STAT) == __DCAN_ERROR_OCCURED) {
        /* Check the status of DCAN Status and error register */
        uiErrorValue = DCANErrAndStatusRegInfoGet(pCanChan->CANCHAN_ulVirtAddrBase);

        if (uiErrorValue & DCAN_PARITY_ERR_DETECTED) {
            /* Read the word number where parity error got detected */
            uiWordNum = DCANParityErrCdRegStatusGet(pCanChan->CANCHAN_ulVirtAddrBase,
                                                    DCAN_PARITY_ERR_WRD_NUM);

            /* Read the message number where parity error got detected */
            uiMsgNum = DCANParityErrCdRegStatusGet(pCanChan->CANCHAN_ulVirtAddrBase,
                                                   DCAN_PARITY_ERR_MSG_NUM);

            printk(KERN_ERR "__am335xCanParityIsr(): message number=%d, word number=%d\n",
                   uiMsgNum, uiWordNum);
        }
    }

    return  (LW_IRQ_HANDLED);
}
/*********************************************************************************************************
** 函数名称: __am335xCanIoctl
** 功能描述: CAN 通道控制
** 输 入  : pcanchan                  CAN 通道
**           iCmd                      命令
**           lArg                      参数
** 输 出  : ERROR_CODE
** 全局变量:
** 调用模块:
*********************************************************************************************************/
static INT  __am335xCanIoctl (CAN_CHAN    *pcanchan,
                              INT          iCmd,
                              LONG         lArg)
{
    __PAM335X_CAN_CHAN      pCanChan = container_of(pcanchan,
                                                __AM335X_CAN_CHAN, CANCHAN_canChan);
    CHAR                    cName[64];
    UINT                    uiChannel;

    uiChannel = pCanChan - _G_am335xCanChannels;

    switch (iCmd) {

    case CAN_DEV_OPEN:                                                  /*  打开 CAN 设备               */
        API_InterVectorDisable(pCanChan->CANCHAN_ulVector);             /*  关闭 CAN 中断               */
        API_InterVectorDisable(pCanChan->CANCHAN_ulParityVector);       /*  关闭 CAN Parity 中断        */

        pCanChan->CANCHAN_ulVirtAddrBase = (addr_t)API_VmmIoRemap(      /*  映射特殊功能寄存器区        */
                                                       (PVOID)pCanChan->CANCHAN_ulPhyAddrBase,
                                                       pCanChan->CANCHAN_stPhyAddrSize);
        if (!pCanChan->CANCHAN_ulVirtAddrBase) {
            _ErrorHandle(ENOMEM);
            return  (PX_ERROR);                                         /*  映射失败                    */
        }

        pCanChan->CANCHAN_uiBaud = __DCAN_BIT_RATE;                     /*  初始化波特率                */

        __am335xCanHwInit(pCanChan);                                    /*  初始化 CAN                  */

        /*
         * Setting the priority for the system interrupt in AINTC.
         */
        IntPrioritySet(pCanChan->CANCHAN_ulVector,
                       pCanChan->CANCHAN_uiIntPriority,
                       AINTC_HOSTINT_ROUTE_IRQ);

        snprintf(cName, sizeof(cName), "dcan%d_isr", uiChannel);

        API_InterVectorConnect(pCanChan->CANCHAN_ulVector,
                               (PINT_SVR_ROUTINE)__am335xCanIsr,
                               (PVOID)pCanChan,
                               cName);                                  /*  安装操作系统中断向量表      */

        IntPrioritySet(pCanChan->CANCHAN_ulParityVector,
                       pCanChan->CANCHAN_uiIntPriority,
                       AINTC_HOSTINT_ROUTE_IRQ);

        snprintf(cName, sizeof(cName), "dcan%d_par_isr", uiChannel);

        API_InterVectorConnect(pCanChan->CANCHAN_ulParityVector,
                               (PINT_SVR_ROUTINE)__am335xCanParityIsr,
                               (PVOID)pCanChan,
                               cName);                                  /*  安装操作系统中断向量表      */


        __am335xCanStartup(pcanchan);
        __am335xCanStartupRx(pcanchan);
        break;

    case CAN_DEV_CLOSE:                                                 /*  关闭 CAN 设备               */
        API_InterVectorDisable(pCanChan->CANCHAN_ulVector);             /*  关闭 CAN 中断               */
        API_InterVectorDisable(pCanChan->CANCHAN_ulParityVector);       /*  关闭 CAN Parity 中断        */

        API_InterVectorDisconnect(pCanChan->CANCHAN_ulParityVector,
                                 (PINT_SVR_ROUTINE)__am335xCanParityIsr,
                                 (PVOID)pCanChan);

        API_InterVectorDisconnect(pCanChan->CANCHAN_ulVector,
                                 (PINT_SVR_ROUTINE)__am335xCanIsr,
                                 (PVOID)pCanChan);

        API_VmmIoUnmap((PVOID)pCanChan->CANCHAN_ulVirtAddrBase);
        pCanChan->CANCHAN_ulVirtAddrBase = 0;
        break;

    case CAN_DEV_SET_BAUD:                                              /*  设置波特率                  */
        switch (lArg) {
        case 1000000:
        case 900000:
        case 800000:
        case 700000:
        case 600000:
        case 666000:
        case 500000:
        case 400000:
        case 250000:
        case 200000:
        case 125000:
        case 100000:
        case 80000:
        case 50000:
        case 40000:
        case 30000:
        case 20000:
        case 10000:
        case 5000:
            pCanChan->CANCHAN_uiBaud = (UINT)lArg;
            /*
             * Configure the bit timing values for CAN communication
             */
            /* Enter the Initialization mode of CAN controller */
            DCANInitModeSet(pCanChan->CANCHAN_ulVirtAddrBase);

            /* Enable the write access to the DCAN configuration registers */
            DCANConfigRegWriteAccessControl(pCanChan->CANCHAN_ulVirtAddrBase,
                                            DCAN_CONF_REG_WR_ACCESS_ENABLE);
            /* Wait for Init bit to set */
            while(!(HWREG(pCanChan->CANCHAN_ulVirtAddrBase + DCAN_CTL) & DCAN_CTL_INIT));

            _PrintFormat("__am335xCanIoctl: CAN_DEV_SET_BAUD %d\n\r", pCanChan->CANCHAN_uiBaud);
            CANSetBitTiming(pCanChan->CANCHAN_ulVirtAddrBase, __DCAN_IN_CLK, pCanChan->CANCHAN_uiBaud);
            /* Disable the write access to the DCAN configuration registers */
            DCANConfigRegWriteAccessControl(pCanChan->CANCHAN_ulVirtAddrBase,
                                            DCAN_CONF_REG_WR_ACCESS_DISABLE);
            /* Start the CAN transfer */
            DCANNormalModeSet(pCanChan->CANCHAN_ulVirtAddrBase);
            break;

        default:
            _ErrorHandle(EINVAL);
            return  (PX_ERROR);
        }
        break;

    case CAN_DEV_REST_CONTROLLER:
        API_InterVectorDisable(pCanChan->CANCHAN_ulVector);             /*  关闭 CAN 中断               */
        API_InterVectorDisable(pCanChan->CANCHAN_ulParityVector);       /*  关闭 CAN Parity 中断        */

        __am335xCanHwInit(pCanChan);                                    /*  初始化 CAN                  */
        break;

    case CAN_DEV_STARTUP:                                               /*  启动 CAN 卡                 */
        API_InterVectorDisable(pCanChan->CANCHAN_ulVector);             /*  关闭 CAN 中断               */
        API_InterVectorDisable(pCanChan->CANCHAN_ulParityVector);       /*  关闭 CAN Parity 中断        */

        __am335xCanStartup(pcanchan);
        __am335xCanStartupRx(pcanchan);
        break;

    default:
        _ErrorHandle(ENOSYS);
        return  (PX_ERROR);
    }

    return (ERROR_NONE);
}
/*********************************************************************************************************
  CAN 驱动程序
*********************************************************************************************************/
static CAN_DRV_FUNCS    _G_am335xCanDrvFuncs = {
             (INT (*)(CAN_CHAN *,INT, PVOID))__am335xCanIoctl,
             __am335xCanStartup,
             __am335xCanCbInstall
};
/*********************************************************************************************************
** 函数名称: canChanCreate
** 功能描述: 创建一个 CAN 通道
** 输 入  : uiChannel                 CAN 通道号
**           pPinMux                   管脚复用
**           uiId                      ID
** 输 出  : CAN 通道
** 全局变量:
** 调用模块:
*********************************************************************************************************/
CAN_CHAN  *canChanCreate (UINT  uiChannel, PAM335X_PINMUX  pPinMux, UINT  uiId)
{
    __PAM335X_CAN_CHAN        pCanChan;

    if (uiChannel >= __DCAN_NR) {
        return  (LW_NULL);                                              /*  通道号错误                  */
    }

    pCanChan = &_G_am335xCanChannels[uiChannel];
    pCanChan->CANCHAN_canChan.pDrvFuncs = &_G_am335xCanDrvFuncs;        /*  CAN FUNC                    */

    pCanChan->CANCHAN_pPinMux = pPinMux;

    return  (&pCanChan->CANCHAN_canChan);
}
/*********************************************************************************************************
  END
*********************************************************************************************************/