]> rtime.felk.cvut.cz Git - jenkicar/rpp-simulink.git/commitdiff
Algorithm for Resolving combination of aut. and man. meilboxes numbering improved
authorMichal Horn <hornmich@fel.cvut.cz>
Thu, 28 Aug 2014 15:00:03 +0000 (17:00 +0200)
committerMichal Horn <hornmich@fel.cvut.cz>
Thu, 28 Aug 2014 15:00:03 +0000 (17:00 +0200)
Code of the algorithm was moved to a separated file, included by both CAN RX and TX blocks.
Functions were made more generic to be apliable for both CAN blocks without further changes.

rpp/blocks/tlc_c/can_common.tlc [new file with mode: 0644]
rpp/blocks/tlc_c/sfunction_canreceive.tlc
rpp/blocks/tlc_c/sfunction_cantransmit.tlc

diff --git a/rpp/blocks/tlc_c/can_common.tlc b/rpp/blocks/tlc_c/can_common.tlc
new file mode 100644 (file)
index 0000000..2382939
--- /dev/null
@@ -0,0 +1,124 @@
+%% Copyright (C) 2014 Czech Technical University in Prague
+%%
+%% Authors:
+%%     Michal Horn  <hornmich@fel.cvut.cz>
+%%
+%% This document contains proprietary information belonging to Czech
+%% Technical University in Prague. Passing on and copying of this
+%% document, and communication of its contents is not permitted
+%% without prior written authorization.
+%%
+%% File : can_common.tlc
+%% Abstract:
+%%     TLC file with common functions and variables for sfunction_canreceive, sfunction_cansetup and sfunction_cantransmit
+%%
+%% References:
+
+
+%% Function: FindFreeMailboxNumber =============================================
+%function FindFreeMailboxNumber(end, module) 
+       %foreach mn = %<end>
+               %assign index = %<mn>+1
+               %if EXISTS("::can%<module>_mailbox_assigned%<index>_flag") == 0
+                       %return %<mn>
+                       %break
+               %endif
+       %endforeach
+       %return -1
+%endfunction
+
+%% Function: MailboxNumberDuplicite ============================================
+%function MailboxNumberDuplicite(module, mailbox_number, mailbox_auto)
+       %if %<mailbox_auto> == 0U       
+               %if EXISTS("::can%<module>_mailbox_number%<mailbox_number>") == 0
+                       %assign ::can%<module>_mailbox_number%<mailbox_number> = 1
+               %else
+                       %return 1
+               %endif
+       %endif
+       %return 0
+       
+%endfunction
+
+%% Function: AssertDirectionSpecificator =======================================
+%function AssertDirectionSpecificator(direction_sp) void
+       %if !ISEQUAL(direction_sp, "t") && !ISEQUAL(direction_sp, "r")
+               %<LibBlockReportFatalError(block, "Illegal direction specificator: " + direction_sp)>
+       %endif
+%endfunction
+
+%% Function: CreateNewBuffer ===================================================
+%function CreateNewBuffer(module, mailbox_num, message_type, message_id, mask, direction_sp) void
+       %<AssertDirectionSpecificator(direction_sp)>
+       %if ISEQUAL(direction_sp, "t")
+               %createrecord ::tx_buffer_%<::can_tx_cnt> {type message_type; controller module; msg_obj mailbox_num; name "%<LibGetBlockName(block)>" }
+       %else
+               %createrecord ::rx_buffer_%<::can_rx_cnt> {type message_type; controller module; msg_obj mailbox_num; name "%<LibGetBlockName(block)>"; id message_id; msk mask }
+       %endif
+%endfunction
+
+%% Function: PlaceAutomaticMailboxNumber =======================================
+%function PlaceAutomaticMailboxNumber(max_mailbox_number, module, message_type, message_id, mask, direction_sp) void
+       %<AssertDirectionSpecificator(direction_sp)>
+
+       %% Find first free mailbox number
+       %assign mailbox_number = FindFreeMailboxNumber(max_mailbox_number, module)
+       %if %<mailbox_number> < 0 
+               %<LibBlockReportError(block, "Too many blocks in one module!")>
+       %endif
+       %assign mailbox_number = mailbox_number+1
+       
+       %% Mark the mailbox number as used and assign a record about buffer to it
+       %assign ::can%<module>_mailbox_assigned%<mailbox_number>_flag = 1
+       %createrecord ::can%<module>_mailbox_assigned%<mailbox_number> { direction "%<direction_sp>"; buffer ::can_%<direction_sp>x_cnt }
+       
+       %<CreateNewBuffer(module, mailbox_number, message_type, message_id, mask, direction_sp)>
+
+%endfunction
+
+%% Function: PlaceManualMailboxNumber ==========================================
+%function PlaceManualMailboxNumber(max_mailbox_number, mailbox_num, module, message_type, message_id, mask, direction_sp) void
+       %<AssertDirectionSpecificator(direction_sp)>
+
+       %% Check whether the mailbox number is already used
+       %if EXISTS(::can%<module>_mailbox_assigned%<mailbox_num>_flag) == 0
+               %assign ::can%<module>_mailbox_assigned%<mailbox_num>_flag = 1
+               %createrecord ::can%<module>_mailbox_assigned%<mailbox_num> { direction "%<direction_sp>"; buffer ::can_%<direction_sp>x_cnt }
+       %else
+               %% The mailbox number is already used by the automaticaly placed buffer.
+               %% The case that it is used by another manualy placed buffer is handeled by the duplication check.
+               %% Find first free mailbox number
+               %assign mailbox_number = FindFreeMailboxNumber(max_mailbox_number, module)
+               %if mailbox_number < 0 
+                       %<LibBlockReportError(block, "Too many blocks in one module!")>
+               %endif
+               %assign mailbox_number = %<mailbox_number>+1
+               
+               %% Mark the mailbox number as used              
+               %assign ::can%<module>_mailbox_assigned%<mailbox_number>_flag = 1
+
+               %% Move record with buffer information to the new slot with free mailbox number id
+               %copyrecord   ::can%<module>_mailbox_assigned%<mailbox_number> ::can%<module>_mailbox_assigned%<mailbox_num>
+       
+               %% Create new record with buffer information in the required slot
+               %createrecord ::can%<module>_mailbox_assigned%<mailbox_num> {direction "%<direction_sp>"; buffer ::can_%<direction_sp>x_cnt}
+
+               %% Fix mailbox number in the old buffer record
+               %copyrecord tmp_ma  ::can%<module>_mailbox_assigned%<mailbox_number>                                    
+               %copyrecord tmp_buf ::%<tmp_ma.direction>x_buffer_%<tmp_ma.buffer>
+               %if ISEQUAL(tmp_ma.direction, "t")
+                       %createrecord tmp_result {type tmp_buf.type; controller tmp_buf.controller; msg_obj mailbox_number; name "%<tmp_buf.name>" }
+                       %copyrecord ::tx_buffer_%<tmp_ma.buffer> tmp_result
+               %elseif ISEQUAL(tmp_ma.direction, "r")
+                       %createrecord rx_buffer_%<tmp_ma.buffer> { type tmp_buf.type; controller tmp_buf.controller; msg_obj mailbox_number; name "%<tmp_buf.name>"; id tmp_buf.id; msk tmp_buf.msk }
+               %else
+                       %<LibBlockReportFatalError(block, "Unknown block direction, this is probably a bug!")>
+               %endif
+       %endif  
+
+       %<CreateNewBuffer(module, mailbox_num, message_type, message_id, mask, direction_sp)>
+
+%endfunction
+
+%% [EOF]
+
index a95a486ac5567a417f9b92d5c2d30b22ea1de5c9..cc351458abf42a67f852dde05bcb926b2f264dec 100644 (file)
@@ -20,6 +20,7 @@
 %implements sfunction_canreceive "C"
 
 %include "common.tlc"
+%include "can_common.tlc"
 
 %% Function: BlockTypeSetup ====================================================
 %function BlockTypeSetup(block, system) void
@@ -35,6 +36,7 @@
 
 %endfunction
 
+%% Function: BlockInstanceSetup ================================================
 %function BlockInstanceSetup(block, system) void                
 
        %assign module_id_val = LibBlockParameterValue(module_id, 0)                 
        %assign message_mask_val = LibBlockParameterValue(message_mask, 0)
        %assign mailbox_auto_val = LibBlockParameterValue(mailbox_auto, 0)      
 
-       %% Declare a new mailbox counter for this CAN module if needed.
-       %if %<mailbox_auto_val>!=0U
-               %if EXISTS("::can%<module_id_val>_mailbox_cnt") == 0
-                       %assign ::can%<module_id_val>_mailbox_cnt = 1
-               %endif
-       %endif
-
-       %% If manual mailbox numbering selected, check for duplications
-       %if %<mailbox_auto_val> == 0U   
-               %if EXISTS("::can%<module_id_val>_mailbox_number%<mailbox_id_val>") == 0
-                       %assign ::can%<module_id_val>_mailbox_number%<mailbox_id_val> = 1
-               %else
-                       %<LibBlockReportError(block, "Duplicit mailbox number found!")>
-               %endif
-       %endif
-
-       %% Create record for the block
-       %if %<mailbox_auto_val>==1
-               %assign tmp_cnt = ::can%<module_id_val>_mailbox_cnt
-               %assign mailbox = %<tmp_cnt>
-               %assign ::can%<module_id_val>_mailbox_cnt = %<tmp_cnt> + 1
-       %else
-               %assign mailbox = %<mailbox_id_val>
-       %endif
-
-    %if %<message_filter_val>==1
-            %assign mask = 8388607 %%0x7FFFFF
+    %if message_filter_val == 1
+            %assign mask = 8388607 %% 0x7FFFFF
     %else
-            %assign mask = %<message_mask_val>
+            %assign mask = message_mask_val
     %endif
 
+       %if MailboxNumberDuplicite(module_id_val, mailbox_id_val, mailbox_auto_val) == 1
+               %<LibBlockReportError(block, "Duplicit mailbox number found!")>
+       %endif
+       
        %if %<mailbox_auto_val>==1
-               %% Find first free mailbox number
-               %foreach mn = 32-1
-                       %if EXISTS("::can%<module_id_val>_mailbox_assigned%<mn+1>_flag") == 0
-                               %assign ::can%<module_id_val>_mailbox_assigned%<mn+1>_flag = 1
-                               %createrecord ::can%<module_id_val>_mailbox_assigned%<mn+1> { direction "r"; buffer %<::can_rx_cnt> }
-                               %assign mailbox_number = %<mn>+1
-                               %assign ::can%<module_id_val>_mailbox_cnt = mailbox_number
-                               %break
-                       %endif
-               %endforeach
-               %createrecord ::rx_buffer_%<::can_rx_cnt> {type %<message_type_val>; controller %<module_id_val>; msg_obj %<mailbox_number>; name "%<LibGetBlockName(block)>"; id %<message_id_val>; msk %<mask>        }
+               %% Place new buffer for automaticaly numbered mailbox.
+               %<PlaceAutomaticMailboxNumber(31, module_id_val, message_type_val, message_id_val, mask, "r")>
        %else
-               %% Check whether the mailbox number is already used
-               %if EXISTS(::can%<module_id_val>_mailbox_assigned%<mailbox>_flag) == 0
-                       %assign ::can%<module_id_val>_mailbox_assigned%<mailbox>_flag = 1
-                       %createrecord ::can%<module_id_val>_mailbox_assigned%<mailbox> { direction "r"; buffer %<::can_rx_cnt> }
-               %else
-                       %% Find first free mailbox number
-                       %foreach mn = 32-1
-                               %if EXISTS("::can%<module_id_val>_mailbox_assigned%<mn+1>_flag") == 0
-                                       %assign ::can%<module_id_val>_mailbox_assigned%<mn+1>_flag = 1
-                                       %assign mailbox_number = %<mn>+1
-                                       %assign ::can%<module_id_val>_mailbox_cnt = mailbox_number
-                                       %break
-                               %endif
-                       %endforeach
-
-                       %% Move record with buffer information to the new slot with free mailbox number id
-                       %copyrecord   ::can%<module_id_val>_mailbox_assigned%<mailbox_number> ::can%<module_id_val>_mailbox_assigned%<mailbox>
-               
-                       %% Create new record with buffer information in the required slot
-                       %createrecord ::can%<module_id_val>_mailbox_assigned%<mailbox> {direction "r"; buffer %<::can_rx_cnt>}
-
-                       %% Fix mailbox number in the old buffer record
-                       %body
-                               %copyrecord tmp_ma  ::can%<module_id_val>_mailbox_assigned%<mailbox_number>                                     
-                               %copyrecord tmp_buf ::%<tmp_ma.direction>x_buffer_%<tmp_ma.buffer>
-                               %if ISEQUAL(tmp_ma.direction, "t")
-                                       %createrecord tmp_result {type %<tmp_buf.type>; controller %<tmp_buf.controller>; msg_obj %<mailbox_number>; name "%<tmp_buf.name>" }
-                                       %copyrecord ::tx_buffer_%<tmp_ma.buffer> tmp_result
-                               %elseif ISEQUAL(tmp_ma.direction, "r")
-                                       %createrecord tmp_result {type %<tmp_buf.type>; controller %<tmp_buf.controller>; msg_obj %<mailbox_number>; name "%<tmp_buf.name>"; id %<tmp_buf.id>; msk %<tmp_buf.msk> }
-                                       %copyrecord ::rx_buffer_%<tmp_ma.buffer> tmp_result
-                               %else
-                                       %<LibBlockReportFatalError(block, "Unknown block direction, this is probably a bug!")>
-                               %endif
-                       %endbody
-               %endif  
-
-               %% Create new buffer record
-               %createrecord ::rx_buffer_%<::can_rx_cnt> {type %<message_type_val>; controller %<module_id_val>; msg_obj %<mailbox>; name "%<LibGetBlockName(block)>"; id %<message_id_val>; msk %<mask>       }
+               %% Place new buffer for manualy numbered mailbox, replace the automaticaly numbered one, if needed.
+               %<PlaceManualMailboxNumber(30, mailbox_id_val, module_id_val, message_type_val, message_id_val, mask, "r")>
        %endif
 
        %assign ::can_rx_cnt = %<::can_rx_cnt> + 1
index 2e3d7aedc3de1fb21300e3cc47ab7532b82cb181..fc320b2e551fc31ea4836e50948c945ddc49355f 100644 (file)
@@ -20,6 +20,7 @@
 %implements sfunction_cantransmit "C"
 
 %include "common.tlc"
+%include "can_common.tlc"
 
 %% Function: BlockTypeSetup ====================================================
 %function BlockTypeSetup(block, system) void
 
 %endfunction
 
-%function FindFreeMailboxNumber(end, module) 
-       %foreach mn = %<end>
-               %assign index = %<mn>+1
-               %if EXISTS("::can%<module>_mailbox_assigned%<index>_flag") == 0
-                       %return %<mn>
-                       %break
-               %endif
-       %endforeach
-       %return -1
-%endfunction
-
-%function MailboxNumberDuplicite(module, mailbox_number, mailbox_auto)
-       %if %<mailbox_auto> == 0U       
-               %if EXISTS("::can%<module>_mailbox_number%<mailbox_number>") == 0
-                       %assign ::can%<module>_mailbox_number%<mailbox_number> = 1
-               %else
-                       %return 1
-               %endif
-       %endif
-       %return 0
-       
-%endfunction
-
-%function PlaceAutomaticTXMailboxNumber(max_mailbox_number, module, message_type) void
-       %% Find first free mailbox number
-       %assign mailbox_number = FindFreeMailboxNumber(max_mailbox_number, module)
-       %if %<mailbox_number> < 0 
-               %<LibBlockReportError(block, "Too many blocks in one module!")>
-       %endif
-       %assign mailbox_number = mailbox_number+1
-       
-       %% Mark the mailbox number as used and assign a record about buffer to it
-       %assign ::can%<module>_mailbox_assigned%<mailbox_number>_flag = 1
-       %createrecord ::can%<module>_mailbox_assigned%<mailbox_number> { direction "t"; buffer ::can_tx_cnt }
-       
-       %% Create a new buffer assigned to this mailbox
-       %createrecord ::tx_buffer_%<::can_tx_cnt> {type message_type; controller module; msg_obj mailbox_number; name "%<LibGetBlockName(block)>" }
-
-%endfunction
-
-%function PlaceManualTXMailboxNumber(max_mailbox_number, mailbox_num, module, message_type) void
-       %% Check whether the mailbox number is already used
-       %if EXISTS(::can%<module>_mailbox_assigned%<mailbox_num>_flag) == 0
-               %assign ::can%<module>_mailbox_assigned%<mailbox_num>_flag = 1
-               %createrecord ::can%<module>_mailbox_assigned%<mailbox_num> { direction "t"; buffer ::can_tx_cnt }
-       %else
-               %% The mailbox number is already used by the automaticaly placed buffer.
-               %% The case that it is used by another manualy placed buffer is handeled by the duplication check.
-               %% Find first free mailbox number
-               %assign mailbox_number = FindFreeMailboxNumber(max_mailbox_number, module)
-               %if mailbox_number < 0 
-                       %<LibBlockReportError(block, "Too many blocks in one module!")>
-               %endif
-               %assign mailbox_number = %<mailbox_number>+1
-               
-               %% Mark the mailbox number as used              
-               %assign ::can%<module>_mailbox_assigned%<mailbox_number>_flag = 1
-
-               %% Move record with buffer information to the new slot with free mailbox number id
-               %copyrecord   ::can%<module>_mailbox_assigned%<mailbox_number> ::can%<module>_mailbox_assigned%<mailbox_num>
-       
-               %% Create new record with buffer information in the required slot
-               %createrecord ::can%<module>_mailbox_assigned%<mailbox_num> {direction "t"; buffer ::can_tx_cnt}
-
-               %% Fix mailbox number in the old buffer record
-               %copyrecord tmp_ma  ::can%<module>_mailbox_assigned%<mailbox_number>                                    
-               %copyrecord tmp_buf ::%<tmp_ma.direction>x_buffer_%<tmp_ma.buffer>
-               %if ISEQUAL(tmp_ma.direction, "t")
-                       %createrecord tmp_result {type tmp_buf.type; controller tmp_buf.controller; msg_obj mailbox_number; name "%<tmp_buf.name>" }
-                       %copyrecord ::tx_buffer_%<tmp_ma.buffer> tmp_result
-               %elseif ISEQUAL(tmp_ma.direction, "r")
-                       %createrecord rx_buffer_%<tmp_ma.buffer> { type tmp_buf.type; controller tmp_buf.controller; msg_obj mailbox_number; name "%<tmp_buf.name>"; id tmp_buf.id; msk tmp_buf.msk }
-               %else
-                       %<LibBlockReportFatalError(block, "Unknown block direction, this is probably a bug!")>
-               %endif
-       %endif  
-
-       %% Create new buffer record
-       %createrecord ::tx_buffer_%<::can_tx_cnt> {type message_type; controller module; msg_obj mailbox_num; name "%<LibGetBlockName(block)>" }
-       
-%endfunction
-
+%% Function: BlockInstanceSetup ================================================
 %function BlockInstanceSetup(block, system) void              
        %assign module_id_val = LibBlockParameterValue(module_id, 0)                 
        %assign mailbox_id_val = LibBlockParameterValue(mailbox_id, 0)+1 %% +1 added because the RPP layer counts with mailbox indexing 1-32
        
        %if %<mailbox_auto_val>==1
                %% Place new buffer for automaticaly numbered mailbox.
-               %<PlaceAutomaticTXMailboxNumber(31, module_id_val, message_type_val)>
+               %<PlaceAutomaticMailboxNumber(31, module_id_val, message_type_val, 0, 0, "t")>
        %else
                %% Place new buffer for manualy numbered mailbox, replace the automaticaly numbered one, if needed.
-               %<PlaceManualTXMailboxNumber(30, mailbox_id_val, module_id_val, message_type_val)>
+               %<PlaceManualMailboxNumber(30, mailbox_id_val, module_id_val, message_type_val, 0, 0, "t")>
        %endif
 
        %assign ::can_tx_cnt = %<::can_tx_cnt> + 1
 %endfunction
 
 %% [EOF]
-