]> rtime.felk.cvut.cz Git - socketcan-simulink.git/commitdiff
The first round of changes to port RPP code to Linux SocketCAN.
authorPavel Pisa <pisa@cmp.felk.cvut.cz>
Mon, 6 Oct 2014 15:12:39 +0000 (17:12 +0200)
committerPavel Pisa <pisa@cmp.felk.cvut.cz>
Mon, 6 Oct 2014 15:58:55 +0000 (17:58 +0200)
Signed-off-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
blocks/tlc_c/common.tlc
blocks/tlc_c/rpp_can_common.tlc
blocks/tlc_c/sfunction_canreceive.tlc
blocks/tlc_c/sfunction_cansetup.tlc
blocks/tlc_c/sfunction_cantransmit.tlc

index 4cac9d6b2abf7025e6daffcaac099373d281f490..571cb85e8f1a2100c45054d95f28e25212264174 100644 (file)
@@ -49,8 +49,7 @@
         %assign _DONE_COMMON_BLOCK_TYPE_SETUP_ = 1
          %if !SLibCodeGenForSim()
 
-            %<LibAddToCommonIncludes("<rpp/rpp.h>")>
-            %<LibAddToCommonIncludes("can_message.h")>
+            %% %<LibAddToCommonIncludes("<rpp/rpp.h>")>
 
         %endif
    %endif
index 9a464cf44309cdde6b28db66d29d15f28c833831..e38b474d3810c4731bc75e2b5c84607507b24963 100644 (file)
        %foreach i = 3  %% We have 3 CAN controllers
          %addtorecord Rpp.Can Ctrl {}
        %endforeach
+
+       %<LibAddToCommonIncludes("<stdio.h>")>
+       %<LibAddToCommonIncludes("<sys/socket.h>")>
+       %<LibAddToCommonIncludes("<net/if.h>")>
+       %<LibAddToCommonIncludes("<sys/ioctl.h>")>
+       %<LibAddToCommonIncludes("<linux/can.h>")>
+       %<LibAddToCommonIncludes("<linux/can/raw.h>")>
+       %<LibAddToCommonIncludes("<fcntl.h>")>
+       %<LibAddToCommonIncludes("<errno.h>")>
   %endif
 %endfunction
 
index 602b23a9320fab5ca83690612208548423f3d7ec..e8b27a829da85be7b930ef1d7a5c25eee2c9ab96 100644 (file)
        %assign message = LibBlockOutputSignal(1, "", "", 1)
 
        {
-         struct rpp_can_pdu pdu;
+         struct can_frame sc_frame;
          int ret;
+         int dlc;
 
-         ret = rpp_can_read(%<RppRxInfo.HwObj>, &pdu);
-         switch (ret) {
-           case -RPP_EINVAL:
-               rpp_sci_printf("Receiving CAN message failed (%s).\n", "%<RppRxInfo.Name>");
-               break;
-           case -RPP_ENODATA:
-               break;
-           case SUCCESS: {
-               %if %<data_type_par>==4
-                 // CAN_MESSAGE
-                 %<message>.Length = pdu.dlc;
-                 %<message>.ID = pdu.id;
-                 int i;
-                 for (i = 0; i < pdu.dlc; i++ ) {
-                       %<message>.Data[i] = pdu.data[i];
-                       %%rpp_sci_printf("%X ", pdu.data[i]);
-                 }
-               %elseif %<data_type_par>==3
-                 // uint32
-                 int msg =     (pdu.data[0]) |
-                 (pdu.data[1]<<8) |
-                 (pdu.data[2]<<16) |
-                 (pdu.data[3]<<24);
-                 %<message> = msg;
-                 %%rpp_sci_printf("32b: %X ", msg);
-               %elseif %<data_type_par>==2
-                 // uint16
-                 int msg =     (pdu.data[0]) |
-                 (pdu.data[1]<<8);
-                 %<message> = msg;
-                 %%rpp_sci_printf("16b: %X ", msg);
-               %else
-                 // uint8
-                 int msg = pdu.data[0];
-                 %<message> = msg;
-                 %%rpp_sci_printf("8b: %X ", msg);
-               %endif
-               %%rpp_sci_printf("\n");
-
-               %% Call a function to process the received message via function-call subsystem
-               %foreach callIdx = NumSFcnSysOutputCalls
-                 %if LibIsEqual(SFcnSystemOutputCall[callIdx].BlockToCall,"unconnected")
-                       %continue
-                 %endif
-                 %% call the downstream system
-                 %<LibBlockExecuteFcnCall(block, callIdx)>\
-               %endforeach
-               break;
+         ret = recv(%<RppRxInfo.HwObj>, &sc_frame, sizeof(sc_frame), 0);
+         if (ret == -1) {
+            if (errno!=EAGAIN) {
+              printf("Receiving CAN message failed (%s).\n", "%<RppRxInfo.Name>");
            }
+         } else if (ret < sizeof(sc_frame)) {
+            printf("Receiving CAN message (%s) returns truncated length %d.\n", "%<RppRxInfo.Name>", ret);
+         } else {
+           dlc = sc_frame.can_dlc;
+           if (dlc > 8)
+             dlc = 8;
+           %if %<data_type_par>==4
+               // CAN_MESSAGE
+               %<message>.Length = dlc;
+               %<message>.ID = sc_frame.can_id & CAN_EFF_MASK;
+               int i;
+               for (i = 0; i < dlc; i++ ) {
+                 %<message>.Data[i] = sc_frame.data[i];
+                 printf("%X ", sc_frame.data[i]);
+               }
+           %elseif %<data_type_par>==3
+               // uint32
+               unsigned int msg = (sc_frame.data[0]) |
+                 (sc_frame.data[1]<<8) |
+                 (sc_frame.data[2]<<16) |
+                 (sc_frame.data[3]<<24);
+               if (dlc < 4)
+                 msg &= 0xffffffff << (8 * dlc);
+               %<message> = msg;
+               printf("32b: %X ", msg);
+           %elseif %<data_type_par>==2
+               // uint16
+               unsigned int msg = (sc_frame.data[0]) |
+                   (sc_frame.data[1]<<8);
+               if (dlc < 2)
+                 msg &= 0xffffffff << (8 * dlc);
+               %<message> = msg;
+               printf("16b: %X ", msg);
+           %else
+               // uint8
+               unsigned int msg = sc_frame.data[0];
+               %<message> = msg;
+               printf("8b: %X ", msg);
+           %endif
+           printf("\n");
+
+           %% Call a function to process the received message via function-call subsystem
+           %foreach callIdx = NumSFcnSysOutputCalls
+               %if LibIsEqual(SFcnSystemOutputCall[callIdx].BlockToCall,"unconnected")
+                 %continue
+               %endif
+               %% call the downstream system
+               %<LibBlockExecuteFcnCall(block, callIdx)>\
+           %endforeach
          }
        }
   %endif
index 481944c0fbc371ae061a222f2cadc7f86739e60a..f2eec3498b0e35c97327e485af301b982452bdff 100644 (file)
@@ -51,7 +51,7 @@
 
     %% Ensure required header files are included
     %<RppCommonBlockTypeSetup(block, system)>
-       %<LibAddToCommonIncludes("<sys/ti_drv_dmm.h>")>
+       %% %<LibAddToCommonIncludes("<sys/ti_drv_dmm.h>")>
 
        %assign ::rpp_can_config_present = 1
 
 %function Start(block, system) Output
  %openfile buffer
 
+       enum ert_can_msg_types {
+               ERT_CAN_STANDARD,
+               ERT_CAN_EXTENDED,
+               ERT_CAN_MIXED,
+       };
+
+       struct ert_can_channel_config {
+               unsigned int baudrate;
+               const char *net_dev_name;
+       };
+
+       struct ert_can_tx_config {
+               int channel;
+               int id_type;
+               canid_t id;
+               int msg_obj;
+       };
+
+       struct ert_can_rx_config {
+               int channel;
+               int id_type;
+               canid_t id;
+               canid_t mask;
+               int msg_obj;
+       };
+
+       struct ert_can_config {
+               int num_tx_obj;
+               int num_rx_obj;
+               struct ert_can_tx_config *tx_config;
+               struct ert_can_rx_config *rx_config;
+               struct ert_can_channel_config *channel_config;
+       };
+
        #define CAN_TX_COUNT %<Rpp.Can.Tx.NumBlocks>
        #define CAN_RX_COUNT %<Rpp.Can.Rx.NumBlocks>
 
-       struct rpp_can_ctrl_config can_ctrl_cfg[3] = {
+       struct ert_can_channel_config can_channel_config[3] = {
                {
                        .baudrate = %<LibBlockParameterValue(baudrate_can1, 0)>
                },
                {
-                   .baudrate = %<LibBlockParameterValue(baudrate_can2, 0)>
+                       .baudrate = %<LibBlockParameterValue(baudrate_can2, 0)>
                },
                {
-                   .baudrate = %<LibBlockParameterValue(baudrate_can3, 0)>
+                       .baudrate = %<LibBlockParameterValue(baudrate_can3, 0)>
                }
        };
 
-       struct rpp_can_tx_config tx_config[CAN_TX_COUNT] = {
+       struct ert_can_tx_config tx_config[CAN_TX_COUNT] = {
          %foreach id = Rpp.Can.Tx.NumBlocks
                %with Rpp.Can.Tx.Block[id]
                // %<Name>
                {
                  %if %<Type> == 1
-                       .type = RPP_CAN_STANDARD,
+                       .id_type = ERT_CAN_STANDARD,
                  %elseif %<Type> == 2
-                       .type = RPP_CAN_EXTENDED,
+                       .id_type = ERT_CAN_EXTENDED,
                  %else
-                       .type = RPP_CAN_MIXED,
+                       .id_type = ERT_CAN_MIXED,
                  %endif
-                 .controller = %<Controller>,
-                 .msg_obj = %<MsgObj>
+                 .id = %<Id>,
+                 .channel = %<Controller>,
+                 .msg_obj = %<MsgObj>,
                },
                %endwith
          %endforeach
        };
 
-       struct rpp_can_rx_config rx_config[CAN_RX_COUNT] = {
+       struct ert_can_rx_config rx_config[CAN_RX_COUNT] = {
          %foreach id = Rpp.Can.Rx.NumBlocks
                %with Rpp.Can.Rx.Block[id]
                // %<Name>
                {
                  %if %<Type>==1
-                       .type = RPP_CAN_STANDARD,
+                       .id_type = ERT_CAN_STANDARD,
                  %elseif %<Type>==2
-                       .type = RPP_CAN_EXTENDED,
+                       .id_type = ERT_CAN_EXTENDED,
                  %else
-                       .type = RPP_CAN_MIXED,
+                       .id_type = ERT_CAN_MIXED,
                  %endif
-                 .controller = %<Controller>,
-                 .msg_obj = %<MsgObj>,
                  .id = %<Id>,
-                 .mask = %<SPRINTF("%#x", Mask)>
+                 .mask = %<SPRINTF("%#x", Mask)>,
+                 .channel = %<Controller>,
+                 .msg_obj = %<MsgObj>,
                },
                %endwith
          %endforeach
        };
 
-       const struct rpp_can_config can_config = {
+       const struct ert_can_config can_config = {
                .num_tx_obj = CAN_TX_COUNT,
                .num_rx_obj = CAN_RX_COUNT,
                .tx_config = tx_config,
                .rx_config = rx_config,
-               .ctrl = can_ctrl_cfg
+               .channel_config = can_channel_config
        };
 
+        int can_rx_handles[CAN_RX_COUNT];
+
+        int can_tx_handles[CAN_RX_COUNT];
+
        %closefile buffer
        %<LibSetSourceFileSection(LibGetModelDotCFile(), "Declarations", buffer)>
        int returnstatus;
 
-       dmmREG->PC4 = 1<<13; // set CAN_NSTB
-    dmmREG->PC5 = 1<<15; // clr CAN_EN
-       dmmREG->PC5 = 1<<13; // clr CAN_NSTB
-       dmmREG->PC4 = 1<<13; // set CAN_NSTB
-       dmmREG->PC4 = 1<<15; // set CAN_EN
-
        returnstatus = rpp_can_init(&can_config);
-       if (returnstatus == FAILURE) {
-           rpp_sci_printf("CAN driver initialization error.\n");
+       if (returnstatus < 0) {
+           printf("CAN driver initialization error.\n");
        }
        %%else {
-       %%      rpp_sci_printf("CAN communication initialized.\n");
+       %%      printf("CAN communication initialized.\n");
        %%}
  %endfunction
 
index 35641d76e842ab3eb387f18677639cdcbc708c50..cd6d1d919fe1622e8dd11862b30ed627456e84c6 100644 (file)
     %assign msginput_data_type = LibBlockInputSignalDataTypeId(0)
 
     {
-      struct rpp_can_pdu pdu;
+      struct can_frame sc_frame;
+      int ret;
 
       %if %<msginput_data_type>==3
        // Transmit uint8
-       pdu.dlc = 1;
-       pdu.data[0]= %<message>;
+       sc_frame.can_dlc = 1;
+       sc_frame.data[0]= %<message>;
 
       %elseif %<msginput_data_type>==5
        // Transmit uint16
-       pdu.dlc = 2;
-       pdu.data[0]= (%<message>&0xFF);
-       pdu.data[1]= (%<message>&0xFF00)>>8;
+       sc_frame.can_dlc = 2;
+       sc_frame.data[0]= (%<message>&0xFF);
+       sc_frame.data[1]= (%<message>&0xFF00)>>8;
       %elseif %<msginput_data_type>==7
        // Transmit uint32
-       pdu.dlc = 4;
-       pdu.data[0]= (%<message>&0xFF);
-       pdu.data[1]= (%<message>&0xFF00)>>8;
-       pdu.data[2]= (%<message>&0xFF0000)>>16;
-       pdu.data[3]= (%<message>&0xFF000000)>>24;
+       sc_frame.can_dlc = 4;
+       sc_frame.data[0]= (%<message>&0xFF);
+       sc_frame.data[1]= (%<message>&0xFF00)>>8;
+       sc_frame.data[2]= (%<message>&0xFF0000)>>16;
+       sc_frame.data[3]= (%<message>&0xFF000000)>>24;
       %else
        // Transmit CAN_MESSAGE
-       pdu.dlc = %<message>.Length;
+       sc_frame.can_dlc = %<message>.Length;
        %foreach msg_index = 8
-         pdu.data[%<msg_index>]= %<message>.Data[%<msg_index>];
+         sc_frame.data[%<msg_index>]= %<message>.Data[%<msg_index>];
        %endforeach
       %endif
-      pdu.id=%<RppTxInfo.Id>;
+      sc_frame.can_id=%<RppTxInfo.Id>;
 
 
-      %%rpp_sci_printf("CAN%d (hw_obj: %d): Sending message id 0x%X from mailbox %d. Data: ", %<module_id_par>, %<::rpp_can_tx_hw_obj_cnt>, pdu.id, %<mailbox_num_par>);
+      %%printf("CAN%d (hw_obj: %d): Sending message id 0x%X from mailbox %d. Data: ", %<module_id_par>, %<::rpp_can_tx_hw_obj_cnt>, pdu.id, %<mailbox_num_par>);
       %%for (i = 0; i < pdu.dlc; i++ ) {
-       %%      rpp_sci_printf("%X ", pdu.data[i]);
+       %%      printf("%X ", pdu.data[i]);
        %%}
-       %%rpp_sci_printf("\n");
-       if (rpp_can_write(%<RppTxInfo.HwObj>, &pdu) != SUCCESS) {
-         /% rpp_sci_printf("CAN%d (hw_obj: %d): Sending a message id 0x%X from mailbox %d failed.\n", %<module_id_par>, %<::rpp_can_tx_hw_obj_cnt>, pdu.id, %<mailbox_num_par>); %/
+       %%printf("\n");
+       ret = send(%<RppTxInfo.HwObj>, &sc_frame, sizeof(sc_frame), 0);
+       if (ret < sizeof(sc_frame)) {
+         /% printf("CAN%d (hw_obj: %d): Sending a message id 0x%X from mailbox %d failed.\n", %<module_id_par>, %<::rpp_can_tx_hw_obj_cnt>, pdu.id, %<mailbox_num_par>); %/
        }
       }
     %endif