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.

_images/function-block.png

Next click on the “+” symbol and select “Script”.

_images/function-block1.png

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.

_images/function-block2.png

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”.

_images/function-block3.png

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.

_images/misc-iox.png

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.

_images/physical-io.png

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.

_images/physical-io1.png

Note the Value column is now filled and the error message was replaced by “No Errors”.

_images/misc-iox1.png

Next add the appropriate comment by double-clicking in the comment box to the right.

_images/misc-iox2.png

Use the same format to setup the remaining MISC IO as inputs:

_images/misc-iox3.png

The next section is optional. Add variables (Application Signals) to your script. From the Scripting and Automation pull-down menu select Application Signals.

_images/scripting.png

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.

_images/application-signal.png

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

_images/application-signal1.png

Next set the Signal Type to “Digital”. When we read the port it will be “low” or “high”.

_images/application-signal2.png

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…

_images/application-signal3.png

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”.

_images/application-signal4.png

Next, click on “Physical IO” followed by “MISC IO 1” followed by “Value” followed by “Add to Expression”. Then click “OK”.

_images/application-signal5.png

Repeat for all MISC IO. Below shows the MISC IO ports to be read into their respective variable.

_images/application-signal6.png

End of optional section.

6.2. Send MISC IO port status in CAN message.

From the Spy Networks pull-down menu select Message Editor.

_images/mannual.png

Click on the Transmit button then click on the “+” symbol to create a new HS CAN message.

_images/transmit.png

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).

_images/transmit1.png

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.

_images/sign.png

Next rename the signals to something more meaningful (i.e. can_sig_misc_io_1_input).

_images/sign1.png

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.

_images/note.png

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.

_images/gnd.png

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”

_images/misc-io.png

To write to the port set the MISC IO value property. In the example below the ports are alternated “high” and “low”.

_images/misc-io1.png

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.

_images/pwm.png

Continue setting the PWM Output 2 thru 5 to 100Hz as shown below.

_images/pwm1.png

Repeat the same steps for setting the Duty Cycle for all five PWM channels.

_images/pwm2.png

In this example the duty cycle is 10%, 30%, 50%, 70%, and 90%, respectively for the five PWM channels.

_images/pwm3.png

Below is a screenshot of the five PWM channels. Each measured 10%, 30%, 50%, 70%, 90% duty cycles, respectively.

_images/pwm-channel.png

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”.

_images/ai.png

Now read the analog inputs into the variables.

_images/ai1.png

End of optional section.

6.6. Send Analog input values in CAN message

From the Spy Networks pull-down menu select Message Editor.

_images/ai-can.png

Click on the Transmit button then click on the “+” symbol to create a new HS CAN message.

_images/ai-can1.png

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).

_images/ai-can2.png

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.

_images/ai-sign.png

Next rename the signals to something more meaningful (i.e. can_sig_ain_1).

_images/ai-sign1.png

Or re-size the 16-bit signal values to 12-bit values by dragging the edge of the blue box inwards.

_images/ai-sign2.png

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.

_images/can-sig.png

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.

_images/can-sig1.png

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.

_images/prog-core-mini.png

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).

_images/led-calc.png

Test circuit used:

_images/test-cir.png _images/test-cir1.png

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.

_images/led-control.png

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.

_images/led-control1.png

This example shows how to set LED 1 to yellow color.

_images/led-control2.png

This example shows how to set LED 1 to purple color.

_images/led-control3.png

This example shows how to set LED 1 to cyan color.

_images/led-control4.png

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)

_images/led-control5.png

This example shows how to set LED 1 to orange color. Note the intensity changes from 255 to 100 on the green property.

_images/led-control6.png

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.

_images/lin-example.png

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”.

_images/lin-example1.png _images/lin-example2.png

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).

_images/lin-example3.png

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.

_images/lin-example4.png

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.

_images/lin-example5.png

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

_images/lin-mes.png _images/lin-mes1.png

Receive table – “LIN Master msg3” and signal

_images/lin-mes2.png _images/lin-mes3.png

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.

_images/can-fd.png _images/can-fd1.png _images/can-fd2.png _images/can-fd3.png

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.

_images/can-fd-std.png _images/can-fd-std1.png _images/can-fd-std2.png _images/can-fd-std3.png

Next, create a script to copy the signals from the Receive messages into the Transmit messages.

_images/script-mes.png

The following screenshot shows the comments for each script line item …

_images/script-line.png

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.

_images/expression.png _images/expression1.png

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.

_images/expression2.png

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.

_images/expression3.png

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.

_images/expression4.png

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. 😊