⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 can_hw.lst

📁 can bus 源代码
💻 LST
📖 第 1 页 / 共 5 页
字号:
  246          
  247          CAN_ERROR CAN_hw_wr (U32 ctrl, CAN_msg *msg)  {
  248   1        U32 ctrl0 = ctrl-1;                 /* Controller index 0 .. x-1           */
  249   1        U32 CANData;
  250   1        regCAN *ptrcan = (regCAN *) CAN_BASE[ctrl0];
  251   1      
  252   1        CANData       = (((U32) msg->len) << 16)          & 0x000F0000 | 
  253   1                        (msg->format == EXTENDED_FORMAT ) * 0x80000000 |
  254   1                        (msg->type   == REMOTE_FRAME)     * 0x40000000;
  255   1      
  256   1        if (ptrcan->CANSR & 0x00000004)  {  /* Transmit buffer 1 free              */
ARM COMPILER V2.32a,  CAN_hw                                                               15/03/07  08:58:24  PAGE 5   

  257   2          ptrcan->CANTF1  = CANData;        /* Write frame informations            */
  258   2          ptrcan->CANTID1 = msg->id;        /* Write CAN message identifier        */
  259   2          ptrcan->CANTDA1 = *(U32 *) &msg->data[0]; /* Write first 4 data bytes    */
  260   2          ptrcan->CANTDB1 = *(U32 *) &msg->data[4]; /* Write second 4 data bytes   */
  261   2          ptrcan->CANCMR  = 0x30;           /* Start self-transmission             */
  262   2        }
  263   1        else
  264   1          return CAN_TX_BUSY_ERROR;
  265   1      
  266   1        return CAN_OK;
  267   1      }
  268          
  269          
  270          /*--------------------------- CAN_hw_rd -------------------------------------
  271           *
  272           *  Read CAN_msg from the hardware registers of the requested controller
  273           *
  274           *  Parameter:  ctrl:       Index of the hardware CAN controller (1 .. x)
  275           *              msg:        Pointer where CAN message will be read
  276           *
  277           *  Return:     CAN_ERROR:  Error code
  278           *---------------------------------------------------------------------------*/
  279          
  280          static void CAN_hw_rd (U32 ctrl, CAN_msg *msg)  {
  281   1        U32 ctrl0 = ctrl-1;                 /* Controller index 0 .. x-1           */
  282   1        U32 CANData;
  283   1        U32 *CANAddr;
  284   1        regCAN *ptrcan = (regCAN *) CAN_BASE[ctrl0];
  285   1      
  286   1        /* Read frame informations                                                 */
  287   1        CANData = ptrcan->CANRFS;
  288   1        msg->format   = (CANData & 0x80000000) == 0x80000000;
  289   1        msg->type     = (CANData & 0x40000000) == 0x40000000;
  290   1        msg->len      = ((U8)(CANData >> 16)) & 0x0F;
  291   1      
  292   1        /* Read CAN message identifier                                             */
  293   1        msg->id = ptrcan->CANRID;
  294   1      
  295   1        /* Read the data if received message was DATA FRAME                        */
  296   1        if (msg->type == DATA_FRAME)  {     
  297   2      
  298   2          /* Read first 4 data bytes                                               */
  299   2          CANAddr = (U32 *) &msg->data[0];
  300   2          *CANAddr++ = ptrcan->CANRDA;
  301   2      
  302   2          /* Read second 4 data bytes                                              */
  303   2          *CANAddr   = ptrcan->CANRDB;
  304   2        }
  305   1      }
  306          
  307          
  308          /*--------------------------- CAN_hw_set ------------------------------------
  309           *  Set a message that will automatically be sent as an answer to the REMOTE
  310           *  FRAME message, as this functionality is not enabled by hardware this 
  311           *  function is not implemented
  312           *
  313           *  Parameter:  ctrl:       Ignorred
  314           *              msg:        Pointer to CAN message to be set
  315           *
  316           *  Return:     CAN_ERROR:  Error code
  317           *---------------------------------------------------------------------------*/
  318          
  319          CAN_ERROR CAN_hw_set (U32 ctrl, CAN_msg *msg)  {
  320   1        ctrl = ctrl;                        /* To avoid compiler warning           */
  321   1        msg  = msg;                         /* "unreferenced parameter"            */
  322   1      
ARM COMPILER V2.32a,  CAN_hw                                                               15/03/07  08:58:24  PAGE 6   

  323   1        return CAN_NOT_IMPLEMENTED_ERROR;
  324   1      }
  325          
  326          
  327          /*--------------------------- CAN_hw_rx_object ------------------------------
  328           *
  329           *  Enable reception of messages, on specified controller with specified 
  330           *  identifier, by setting acceptance filter
  331           *
  332           *  Parameter:  ctrl:       Index of the hardware CAN controller (1 .. x)
  333           *              ch:         Ignorred for LPC2xxx
  334           *              id:         CAN message identifier
  335           *              CAN_FORMAT: Format of CAN identifier (standard or extended)
  336           *
  337           *  Return:     CAN_ERROR:  Error code
  338           *---------------------------------------------------------------------------*/
  339          
  340          CAN_ERROR CAN_hw_rx_object (U32 ctrl, U32 ch, U32 id, CAN_FORMAT format)  {
  341   1        static S16 CAN_std_cnt = 0;
  342   1        static S16 CAN_ext_cnt = 0;
  343   1        regCAN_AF    *ptrcan_af    = CAN_AF_BASE;
  344   1        regCAN_AFRAM *ptrcan_afram = CAN_AFRAM_BASE;
  345   1        U32 buf0, buf1;
  346   1        S16 cnt1, cnt2, bound1;
  347   1      
  348   1        ch     = ch;                        /* To avoid compiler warning           */
  349   1                                            /* "unreferenced parameter"            */
  350   1      
  351   1        /* Acceptance Filter Memory full                                           */
  352   1        if ((((CAN_std_cnt + 1) >> 1) + CAN_ext_cnt) >= 512)
  353   1          return CAN_OBJECTS_FULL_ERROR;
  354   1      
  355   1        /* Setup Acceptance Filter Configuration                                   
  356   1           Acceptance Filter Mode Register = Off                                   */
  357   1        ptrcan_af->AFMR = 0x00000001;
  358   1      
  359   1        /* Initialize masks for first call of function                             */
  360   1        if (CAN_std_cnt == 0 && CAN_ext_cnt == 0)  {
  361   2          ptrcan_afram->mask[0] = 0xF7FF0000; /* For std identifier pairing        */
  362   2          ptrcan_afram->mask[1] = 0xFFFFFFFF; /* For ext identifier sorting        */
  363   2          CAN_std_cnt = 1;
  364   2          CAN_ext_cnt = 1;
  365   2        }
  366   1      
  367   1        if (format == STANDARD_FORMAT)  {   /* Add mask for standard identifiers   */
  368   2          id |= ctrl << 13;                 /* Add controller number               */
  369   2          id &= 0x0000F7FF;                 /* Mask out 16-bits of ID              */
  370   2      
  371   2          cnt1 = 0;
  372   2          bound1 = (CAN_std_cnt + 1) >> 1;  /* "x >> 1" = "x / 2"                  */
  373   2          while (cnt1 < bound1)  {          /* Loop through standard existing masks  */
  374   3            if ((ptrcan_afram->mask[cnt1] >> 16) > id)  {
  375   4              cnt2 = cnt1 * 2;
  376   4              break;
  377   4            }
  378   3            if ((ptrcan_afram->mask[cnt1] & 0x0000F7FF) > id)  {
  379   4              cnt2 = cnt1 * 2 + 1;
  380   4              break;
  381   4            }
  382   3            cnt1++;                         /* cnt1 = U32 where to insert new mask */
  383   3          }                                 /* cnt2 = U16 where to insert new mask */
  384   2      
  385   2          buf0 = ptrcan_afram->mask[cnt1];  /* Remember current entry              */
  386   2      
  387   2          if ((cnt2 & 0x0001) == 0)  {      /* Insert new mask to even address     */
  388   3            buf1 = id << 16 | buf0 >> 16;
ARM COMPILER V2.32a,  CAN_hw                                                               15/03/07  08:58:24  PAGE 7   

  389   3          }  else  {                        /* Insert new mask to odd address      */
  390   3            buf1 = buf0 & 0xF7FF0000 | id;
  391   3          }
  392   2          ptrcan_afram->mask[cnt1] = buf1;  /* Insert mask                         */
  393   2      
  394   2          CAN_std_cnt++;
  395   2      
  396   2          /* Even number of standard masks, no extended masks moving needed        */
  397   2          if ((CAN_std_cnt & 0x0001) == 0)  {
  398   3            bound1 = CAN_std_cnt - 2;
  399   3            /* Move all remaining standard mask entries one place up               */
  400   3            while (cnt2 < bound1)  {
  401   4              cnt1++;
  402   4              cnt2++;
  403   4              cnt2++;
  404   4              buf1 = ptrcan_afram->mask[cnt1];
  405   4              ptrcan_afram->mask[cnt1] = buf1 >> 16 | buf0 << 16;
  406   4              buf0 = buf1;
  407   4            }
  408   3          }  else  {                        /* Odd number of standard masks        */
  409   3            bound1 = CAN_std_cnt;
  410   3            /* Move all remaining standard mask entries one place up               */
  411   3            while (cnt2 < bound1)  {
  412   4              cnt1++;
  413   4              cnt2++;
  414   4              cnt2++;
  415   4              buf1 = ptrcan_afram->mask[cnt1];
  416   4              ptrcan_afram->mask[cnt1] = 0x0000F7FF | buf0 << 16;
  417   4              buf0 = buf1;
  418   4            }
  419   3      
  420   3            bound1 = ((CAN_std_cnt + 1) >> 1) + CAN_ext_cnt;
  421   3            /* Move all remaining extended mask entries one place up               */
  422   3            while (cnt1 < bound1)  {
  423   4              cnt1++;
  424   4              buf1 = ptrcan_afram->mask[cnt1];
  425   4              ptrcan_afram->mask[cnt1] = buf0;
  426   4              buf0 = buf1;
  427   4            }        
  428   3          }
  429   2        }  else  {                          /* Add mask for extended identifiers   */
  430   2          id |= (ctrl) << 29;               /* Add controller number               */
  431   2      
  432   2          cnt1 = ((CAN_std_cnt + 1) >> 1);
  433   2          cnt2 = 0;
  434   2          while (cnt2 < CAN_ext_cnt)  {     /* Loop through extended existing masks  */
  435   3            if (ptrcan_afram->mask[cnt1] > id)
  436   3              break;
  437   3            cnt1++;                         /* cnt1 = U32 where to insert new mask */
  438   3            cnt2++;
  439   3          }
  440   2      
  441   2          buf0 = ptrcan_afram->mask[cnt1];  /* Remember current entry              */
  442   2          ptrcan_afram->mask[cnt1] = id;    /* Insert mask                         */
  443   2      
  444   2          CAN_ext_cnt++;
  445   2      
  446   2          bound1 = CAN_ext_cnt - 1;
  447   2          /* Move all remaining extended mask entries one place up                 */
  448   2          while (cnt2 < bound1)  {
  449   3            cnt1++;
  450   3            cnt2++;
  451   3            buf1 = ptrcan_afram->mask[cnt1];
  452   3            ptrcan_afram->mask[cnt1] = buf0;
  453   3            buf0 = buf1;
  454   3          }        
ARM COMPILER V2.32a,  CAN_hw                                                               15/03/07  08:58:24  PAGE 8   

  455   2        }
  456   1        
  457   1        buf0 = ((CAN_std_cnt == 0) + (CAN_std_cnt + 1) / 2) * 4;
  458   1        buf1 = buf0 + CAN_ext_cnt * 4;
  459   1      
  460   1        /* Setup acceptance filter pointers                                        */
  461   1        ptrcan_af->SFF_sa     = 0;
  462   1        ptrcan_af->SFF_GRP_sa = buf0;
  463   1        ptrcan_af->EFF_sa     = buf0;
  464   1        ptrcan_af->EFF_GRP_sa = buf1;
  465   1        ptrcan_af->ENDofTable = buf1;
  466   1      
  467   1        ptrcan_af->AFMR = 0x00000000;       /* Use acceptance filter               */
  468   1      
  469   1        return CAN_OK;
  470   1      }
  471          
  472          /*--------------------------- CAN_hw_tx_object ------------------------------
  473           *
  474           *  This function has no usage on LPC2xxx, and so it does nothing
  475           *
  476           *  Parameter:  ctrl:       Index of the hardware CAN controller (1 .. x)
  477           *              ch:         Ignorred for LPC2xxx
  478           *              id:         CAN message identifier
  479           *              CAN_FORMAT: Format of CAN identifier (standard or extended)
  480           *
  481           *  Return:     CAN_ERROR:  Error code
  482           *---------------------------------------------------------------------------*/
  483          
  484          CAN_ERROR CAN_hw_tx_object (U32 ctrl, U32 ch, CAN_FORMAT format)  {
  485   1        ctrl   = ctrl;                      /* To avoid compiler warning           */
  486   1        ch     = ch;                        /* "unreferenced parameter"            */
  487   1        format = format;
  488   1      
  489   1        return CAN_OK;
  490   1      }
  491          
  492          
  493          #if USE_CAN_CTRL1 == 1
  494          
  495          /*--------------------------- CAN_TX1_ISR ----------------------------------
  496           *
  497           *  CAN transmit interrupt function for controller 1
  498           *  If there are messages in mailbox for transmit it writes it to hardware

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -