6. Function Block Scripts
Next you will learn to program the neoECU-12 Function Block Scripts. From the Scripting and Automation pull-down menu select Function Blocks.
Next click on the “+” symbol and select “Script”.
Click on the “Function Block 1” Description to rename it. For example, rename it to “MISC IO as inputs”. This is not mandatory but it is good coding practice to name your scripts that are meaningful for debugging and code re-use.
6.1. MISC IO as input(s)
Next double-click on the empty cell row 2 column Description to bring up a pull-down menu of all the scripting commands. Select “Set Value”.
Next click on the empty cell row 2 column Value. Note the red text “The SetValue expression could not compile” message. Please note all error messages needs to be corrected before the script can be compiled and programmed into CoreMini. Once we set the value correctly the error message will go away.
Next double-click where it shows “Click For Setup” The Expression Builder pop-up window will show. Click on “Physical IO” followed by “Misc IO” followed by the “+” symbol.
Next click on “MISC IO 1” followed by double-clicking on the “IS Output” property. This adds MISC IO 1 (Is Output) to the “Value to Set” box. Then put “0” for the Expression and click the OK button. “0” = input and “1” = output.
Note the Value column is now filled and the error message was replaced by “No Errors”.
Next add the appropriate comment by double-clicking in the comment box to the right.
Use the same format to setup the remaining MISC IO as inputs:
The next section is optional. Add variables (Application Signals) to your script. From the Scripting and Automation pull-down menu select Application Signals.
Next click on the “+” symbol to add “Application Signals”. These are like variables in C code. Clicking on the “+” symbol again to add additional application signals and clicking on the “-“ symbol will delete the application signal that is highlighted.
Next rename the variable to something that is more meaningful. Click on the description and backspace to rename. Since we are creating a variable for MISC IO 1 input let’s name it “var_misc_io_1_input” for example
Next set the Signal Type to “Digital”. When we read the port it will be “low” or “high”.
Do this for the remaining MISC IO inputs. Note the Format is defaulted to “True/False” but you can switch to “On/Off”, “Yes/No”, “Passed/Failed”, “Open/Closed”, etc…
Next, let’s read the ports and assign the status to their respective variables. Select “Set Value” as the command double-click in the Value column. Click on App Signals then double-click on “app_sig_misc_io_1”.
Next, click on “Physical IO” followed by “MISC IO 1” followed by “Value” followed by “Add to Expression”. Then click “OK”.
Repeat for all MISC IO. Below shows the MISC IO ports to be read into their respective variable.
End of optional section.
6.2. Send MISC IO port status in CAN message.
From the Spy Networks pull-down menu select Message Editor.
Click on the Transmit button then click on the “+” symbol to create a new HS CAN message.
Click on “Tx Message HS CAN 1” to rename it (i.e. misc_io_input_status) and set the Arb ID to a value not being used (i.e. 101).
Add seven 1-bit CAN signals to this message. Click on the down arrow next to “+8” and select “+1”. Then click the “+1” six more times.
Next rename the signals to something more meaningful (i.e. can_sig_misc_io_1_input).
Next modify the script to get the MISC IO port value and copy it into its respective CAN signal. In this example the port is read and copied into the CAN signals then the CAN message is sent every 50 ms.
NOTE – If you had done the optional section above and created application signals and read the MISC IO into the application signals then you would copy the application signals to their respective can signals. The image shown below is copying the MISC IO directly to their respective can signals. Either way works.
Program CoreMini with this script. Connect another Intrepid tool to the neoECU-12 and monitor the HS CAN bus. You should see the status of all seven input ports.
NOTE – If you do not connect a voltage to the pins they tend to float high (3.3V) except for IO7 which is a 5V tolerant IO pin. It floats low as you cen see from the image below. All of the other pins floated high except IO3 which was connected to GND.
6.3. MISC IO as output(s)
Setting the MISC IO as outputs is the same procedure as setting the port to an input but the expression is set to “1” instead of “0”
To write to the port set the MISC IO value property. In the example below the ports are alternated “high” and “low”.
6.4. MISC IO as PWM outputs
There are five PWM Outputs. Please note channel 3 PWM is mirrored on channel 6 PWM.
Setting the MISC IO as PWM outputs is like setting the MISC IO as outputs. After selecting the “Set Value” command double-click where it shows “Click For Setup”. The Expression Builder pop-up window will show. Click on “Physical IO” followed by “PWM Outputs” followed by the “+” symbol. Select PWM Output 1 and then double-click on “PWM Frequency”. Set the frequency in the Expression box. In this example, the frequency is set to 100 Hz.
Continue setting the PWM Output 2 thru 5 to 100Hz as shown below.
Repeat the same steps for setting the Duty Cycle for all five PWM channels.
In this example the duty cycle is 10%, 30%, 50%, 70%, and 90%, respectively for the five PWM channels.
Below is a screenshot of the five PWM channels. Each measured 10%, 30%, 50%, 70%, 90% duty cycles, respectively.
Note: The PWM output is 3.3V signal driven by a series 10K resistor. Please buffer accordingly to drive higher current loads.
6.5. MISC IO as Analog Inputs
There are four 0-5V Analog inputs. These are connected to 12-bit A/D Converters so the maximum value read from the A/D Converter is 4095 counts.
The next section is optional (just like it was for the MISC IO as inputs).
Add variables (Application Signals) to your script. From the Scripting and Automation pull-down menu select Application Signals. Add 4 variables and rename them appropriately. Note the Signal Type is set to “Analog” and the Max Value is set to “4095”.
Now read the analog inputs into the variables.
End of optional section.
6.6. Send Analog input values in CAN message
From the Spy Networks pull-down menu select Message Editor.
Click on the Transmit button then click on the “+” symbol to create a new HS CAN message.
Click on “Tx Message HS CAN 3” to rename it (i.e. ain_status) and set the Arb ID to a value not being used (i.e. 103).
Add four 16-bit CAN signals to this message. Click on the down arrow next to “+8” and select “+16”. Then click the “+1” three more times.
Next rename the signals to something more meaningful (i.e. can_sig_ain_1).
Or re-size the 16-bit signal values to 12-bit values by dragging the edge of the blue box inwards.
Next modify the script to get the analog in value and copy it into its respective CAN signal. In this example the port is read and directly copied into the CAN signals then the CAN message is sent every 10 ms.
Optionally, if you used a variable and read the analog input port into a variable then you would assign the variable to the appropriate CAN signal as shown below.
Program CoreMini with this script. Connect another Intrepid tool to the neoECU-12 and monitor the HS CAN bus. You should see the status of all four analog input ports as shown in the example below.
The circuit that was used in the example was four LEDs connected to each AIN port. A yellow LED was connected to AIN 1, a red LED connected to AIN 2, a green LED connected to AIN 3, and a blue LED connected to AIN 4. From the signal plot you can see the forward voltage drop on the yellow and red LED are about the same and the forward voltage drop on the green and blue are about the same. To calculate the forward voltage drop of the LED take the measured counts, divide by 4096 (for 12-bit A/D) then multiply by 5 (for 5V maximum voltage).
Test circuit used:
6.7. Controlling the Tri-Color LEDs
The neoECU-12 has default behavior for the first two tri-color LEDs. The 1st LED is for HS CAN 1 and the 2nd LED is for HS CAN 2.
Interpretation of RGB LED Colors
These are “RGB” LEDs because they contain separate red, green and blue elements. For networks, each indicates a different aspect of the device’s overall status:
Green: Device is transmitting messages on this channel.
Blue: Device is receiving messages on this channel.
Red: Device is detecting errors on this channel.
It is possible for more than one LED component to be lit, producing the following results:
Green+Blue (Cyan): Device is transmitting and receiving on this channel.
Green+Red (Yellow): Device is transmitting and detecting errors on this channel.
Blue+Red (Magenta): Device is receiving and detecting errors on this channel.
Green+Blue+Red (White): Device is transmitting, receiving and detecting errors on this channel.
LED3 blinks magenta when a function block script is running.
In bootloader mode, all five LEDs blink red in succession.
To override the default behavior, use the command “Set Value” and set “LED x (Auto) = 0” as shown below. To restore to default set “LED x (Auto) = 1”. To set the color of the LED set the red/green/blue property of the LED to 255 as shown below. 255 is the maximum intensity. A value of 50 would be a very low intensity. You can also mix the red/green/blue property to create additional colors.
The proper way to set the LED color is to assign values to the red/green/blue property. This example shows how to set LED 1 to green color.
This example shows how to set LED 1 to yellow color.
This example shows how to set LED 1 to purple color.
This example shows how to set LED 1 to cyan color.
This example shows how to set LED 1 to white color. (note even though the LED is white for the most part you can see the red filament of the LED turn on)
This example shows how to set LED 1 to orange color. Note the intensity changes from 255 to 100 on the green property.
Recommendation: Since LED 1 thru LED 3 are predefined start with using LED 4 and LED 5 for user control.
6.8. LIN example
Below is a simple LIN Master schedule table. Each message is transmitted with 20 ms delay between messages.
In this example, three LIN Transmit messages were created. “LIN Master msg1” is a message that contains data that will be sent to the LIN slaves. The signals were pre-defined as “01” thru “08” but can be modified in the script. The signals in “LIN Master msg1” were named “Data1” thru “Data8” as shown below. Note the Type is “Master”. For LIN messages that will contain data from the slaves the Type needs to be set to “Header Only”.
If we program this example into the neoECU-12 and then use another Intrepid device to monitor the LIN Bus these are the messages on the LIN Bus. Note there is an error for LIN message “LIN 02” and “LIN 03”. This is because the monitoring or slave device has not sent back any data to the LIN Master (neoECU-12).
Next have the monitoring or slave device add data to “LIN Master msg2” and “LIN Master msg3”. Note the data can be any length from 1 to 8 bytes. It is now up to the LIN Master to process the data bytes sent back from the LIN Slave.
To see the LIN columns in the message view select LIN from the Columns button at the bottom of the messages view.
To test the LIN Master is receiving the LIN Slave data a simple script was written to control LED 4 and LED 5 based on receipt of the LIN Slave data. If the “msg2_Data1” = 170 then LED 4 is blue otherwise it is red. If the “mg3_Data1” = 238 the LED 5 is blue otherwise it is red.
Also note for this example the “LIN Master msg2” and “LIN Master msg3” were copied from the Transmit table and added to the Receive table. Then “msg2_Data1” thru “msg2_Data4” signals and “msg3_Data1” thru “msg3_Data2” signals were added to their respective messages. The signals needed to be defined so they could be processed in the script.
Receive table – “LIN Master msg2” and signals
Receive table – “LIN Master msg3” and signal
6.9. CAN to CAN-FD Gateway example
In this example, HS CAN 1 will be configured for standard CAN 2.0B. HS CAN 2 will be configured for HS CAN 2. The baud rate will be set to 500 Kbps and 2000 Kbps, respectively.
Create 3 HS CAN 1 Receive messages and their respective signals as shown below.
Create 3 HS CAN 2 Transmit messages and their respective signals as shown below. Hint: go to the HS CAN 1 Receive table and copy the 3 messages. Then go to the HS CAN 2 Transmit table and paste the messages. Paste into the Description column. This will copy over the signals as well! Change the Type to “CAN FD Std 11 bit” for all 3 messages.
Next, create a script to copy the signals from the Receive messages into the Transmit messages.
The following screenshot shows the comments for each script line item …
When the script is complete program it into the neoECU-12.
When creating the script make sure the Tx Message is selected followed by HS CAN2 followed by the 1st data byte in message 1. Next, make sure the Rx Messages is selected followed by HS CAN followed by the 1st data byte in message 1 then click “Add to Expression” button. Repeat for all signals in all 3 messages.
Now use another Intrepid tool that is CAN/CAN-FD capable to generate CAN traffic on HS CAN 1. Transmit 3 CAN messages with ArbID 300, 301, and 302. The 1st message should contain 8 data bytes, the 2nd message should contain 4 data bytes, and the 3rd message should contain 2 data bytes.
In the Tx Panel set the rate to be 100 ms and periodic. This is a test only. The ECU will have it’s own rates for each message or you can duplicate the rates here.
Next transmit these messages and monitor both HS CAN 1 and HS CAN 2. Each time the message was received on HS CAN 1 it was forwarded to HS CAN 2. In this case, the same ArbID and all of the data bytes were forwarded as is and not changed or scaled. Note the FDF bit (CAN-FD) bit is set and the BRS bit (Bit Rate Switch) is set. If you were to put a scope on the HS CAN 2 line you would see the data bytes are transmitted at the higher bit rate. To see the CAN-FD columns in the message view select CAN FD from the Columns button at the bottom of the messages view.
Check out our new Gateway Builder in Vehicle Spy Enterprise version. Drag and Drop GUI makes it super easy to build a custom Gateway! It generates the scripts for you. 😊