%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(block, system) void
%assign module_id_val = LibBlockParameterValue(module_id, 0)
%if %<mailbox_auto_val>==1
%% Place new buffer for automaticaly numbered mailbox.
- %% Retreive the mailbox number
- %assign mailbox = ::can%<module_id_val>_mailbox_cnt
- %assign ::can%<module_id_val>_mailbox_cnt = ::can%<module_id_val>_mailbox_cnt + 1
-
- %% Find first free mailbox number
- %assign mailbox_number = FindFreeMailboxNumber(31, module_id_val)
- %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_id_val>_mailbox_assigned%<mailbox_number>_flag = 1
- %createrecord ::can%<module_id_val>_mailbox_assigned%<mailbox_number> { direction "t"; buffer ::can_tx_cnt }
- %assign ::can%<module_id_val>_mailbox_cnt = mailbox_number
-
- %% Create a new buffer assigned to this mailbox
- %createrecord ::tx_buffer_%<::can_tx_cnt> {type message_type_val; controller module_id_val; msg_obj mailbox_number; name "%<LibGetBlockName(block)>" }
+ %<PlaceAutomaticTXMailboxNumber(31, module_id_val, message_type_val)>
%else
%% Place new buffer for manualy numbered mailbox, replace the automaticaly numbered one, if needed.
-
- %% Declare a new mailbox counter for this CAN module if needed.
- %if mailbox_auto_val == 0U && EXISTS("::can%<module_id_val>_mailbox_cnt") == 0
- %assign ::can%<module_id_val>_mailbox_cnt = 1
- %endif
-
- %% Check whether the mailbox number is already used
- %if EXISTS(::can%<module_id_val>_mailbox_assigned%<mailbox_id_val>_flag) == 0
- %assign ::can%<module_id_val>_mailbox_assigned%<mailbox_id_val>_flag = 1
- %createrecord ::can%<module_id_val>_mailbox_assigned%<mailbox_id_val> { 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(31, module_id_val)
- %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_id_val>_mailbox_assigned%<mailbox_number>_flag = 1
-
- %% Increment the mailbox counter to make next automatic itteration faster
- %% FIXME: mailbox counter might not be necessary
- %assign ::can%<module_id_val>_mailbox_cnt = mailbox_number
-
- %% 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_id_val>
-
- %% Create new record with buffer information in the required slot
- %createrecord ::can%<module_id_val>_mailbox_assigned%<mailbox_id_val> {direction "t"; buffer ::can_tx_cnt}
-
- %% Fix mailbox number in the old buffer record
- %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 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_val; controller module_id_val; msg_obj mailbox_id_val; name "%<LibGetBlockName(block)>" }
+ %<PlaceManualTXMailboxNumber(30, mailbox_id_val, module_id_val, message_type_val)>
%endif
%assign ::can_tx_cnt = %<::can_tx_cnt> + 1