Create UART Loopback Block Using IO Device Builder App
This example shows how to use the IO Device Builder app to create a UART Loopback block to read and transmit serial data to an SSH utiity (PuTTY).
Prerequisites
Ensure that iLLD for TC3x family is downloaded as recommended by hardware setup screens for Infineon Support Package
PuTTy SSH utility. You can download and install this utility from www.putty.org
Note: This example does not require you to create any circuit connections.
Open the IO Device Builder App
To open the IO Device Builder app, perform these steps.
1. Start MATLAB® and then open a new Simulink® model.
2. In the Simulink model, navigate to Modeling > Model Settings to open Configuration Parameters dialog box.
3. In the Configuration Parameters dialog box, click Hardware Implementation and then select Infineon
AURIX TC3x
as the hardware board and select TC39x
as Device series.
4. On the Hardware tab of the Simulink toolstrip, in Prepare section, under Design, choose IO Device Builder.
Select Working Directory and Add Third-Party Source Files
Once the Source files location page loads, select the working directory and add third-party source files.
On the Source files location page:
Click Select and select the working directory. This app saves generated System Object™ along with the corresponding C and H files, and the model in the working directory.
Click Add folder and add the folders you downloaded. This app includes only the files present directly within the selected folder and exludes any files present within subfolders.
Click Next to continue.
Select Source Files
On the Source files page, select the required source files and then click Next to continue.
Specify Block Parameters
On the Block parameters page:
1. Specify the block name and add block description.
2. Add the following block parameters of non tunable type for the block.
Baudrate with
uint32
as Data type, intial value of 115200StopBits with
uint8
as Data type, initial value of 1ParityType with
uint8
as Data type, inital value of 0DataLength with
uint8
as Data type, inital value of 7DataByteOrder with
uint8
as Data type, inital value of 0
3. Click Next to continue.
Define Outputs for the Block
On the Outputs page:
Remove outputs for the block, if any already added.
Click Next to continue.
Define Inputs for the Block
On the Inputs page:
Remove inputs for the block, if any already added.
Click Next to continue.
Preview Block
On the Block image page, view the preview of the block with the inputs and outputs you added. Click Next to continue.
Generate System Object Files
On the Generate page, the file generation location is displayed. Click Generate to generate the system object files.
Note: Clear the Select to generate a CPP driver option, as C++ is not supported for Infineon AURIX TC3x boards.
Next Steps
On the Next Steps page, the files generated are shown and the next steps to be performed are displayed. The generate files are created in a directory. This directory also includes a Dependencies folder that contains selected third-party source files. Simply copy these files and the folder to your desired location and integrate them into your Simulink project.
Click Finish.
Perform these steps.
1. The generated .c
file opens automatically. Modify it to include the necessary headers and setup functions by referring to the driver file as required.
A sample modified .c file is shown below.
#include "uart_loopback.h" #include "Ifx_Types.h" #include "IfxAsclin_Asc.h" #include "IfxPort.h" #define ISR_PRIORITY_ASCLIN_TX 8 #define ISR_PRIORITY_ASCLIN_RX 4 #define ISR_PRIORITY_ASCLIN_ER 12 #define ASC_TX_BUFFER_SIZE 256 #define ASC_RX_BUFFER_SIZE 256 #define ASC_BAUDRATE 115200 IfxAsclin_Asc g_asclin; char g_rxData; uint8 g_uartTxBuffer[ASC_TX_BUFFER_SIZE + sizeof(Ifx_Fifo) + 8]; uint8 g_uartRxBuffer[ASC_RX_BUFFER_SIZE + sizeof(Ifx_Fifo) + 8]; IFX_INTERRUPT(asc0TxISR, 0, ISR_PRIORITY_ASCLIN_TX); void asc0TxISR(void) { IfxAsclin_Asc_isrTransmit(&g_asclin); } IFX_INTERRUPT(asc0RxISR, 0, ISR_PRIORITY_ASCLIN_RX); void asc0RxISR(void) { IfxAsclin_Asc_isrReceive(&g_asclin); } // Baudrate uint32 [1,1] Non tunable // StopBits uint8 [1,1] Non tunable // ParityType uint8 [1,1] Non tunable // DataLength uint8 [1,1] Non tunable // DataByteOrder uint8 [1,1] Non tunable void setupFunctionuart_loopback(uint32_T Baudrate,int size_vector__1,uint8_T StopBits,int size_vector__2,uint8_T ParityType,int size_vector__3,uint8_T DataLength,int size_vector__4,int8_T DataByteOrder,int size_vector__5){ IfxAsclin_Asc_Config ascConf; /* Initialize an instance of IfxAsclin_Asc_Config with default values */ IfxAsclin_Asc_initModuleConfig(&ascConf, &MODULE_ASCLIN0); /* Initialize the structure with default values */ /* Set the desired baud rate */ ascConf.baudrate.baudrate = Baudrate; ascConf.baudrate.oversampling = IfxAsclin_OversamplingFactor_16; /* Set the oversampling factor */ ascConf.frame.stopBit = StopBits; ascConf.frame.shiftDir = DataByteOrder; ascConf.frame.dataLength = DataLength; ascConf.frame.parityType = ParityType; /* Configure the sampling mode */ ascConf.bitTiming.medianFilter = IfxAsclin_SamplesPerBit_three; /* Set the number of samples per bit*/ ascConf.bitTiming.samplePointPosition = IfxAsclin_SamplePointPosition_8; /* Set the first sample position */ /* ISR priorities and interrupt target */ ascConf.interrupt.txPriority = ISR_PRIORITY_ASCLIN_TX; /* Set the interrupt priority for TX events */ ascConf.interrupt.rxPriority = ISR_PRIORITY_ASCLIN_RX; /* Set the interrupt priority for RX events */ // ascConf.interrupt.erPriority = ISR_PRIORITY_ASCLIN_ER; /* Set the interrupt priority for Error events */ ascConf.interrupt.typeOfService = IfxSrc_Tos_cpu0; /* Pin configuration */ const IfxAsclin_Asc_Pins pins = { .cts = NULL_PTR, /* CTS pin not used */ .ctsMode = IfxPort_InputMode_pullUp, .rx = &IfxAsclin0_RXA_P14_1_IN, /* Select the pin for RX connected to the USB port */ .rxMode = IfxPort_InputMode_pullUp, /* RX pin */ .rts = NULL_PTR, /* RTS pin not used */ .rtsMode = IfxPort_OutputMode_pushPull, .tx = &IfxAsclin0_TX_P14_0_OUT, /* Select the pin for TX connected to the USB port */ .txMode = IfxPort_OutputMode_pushPull, /* TX pin */ .pinDriver = IfxPort_PadDriver_cmosAutomotiveSpeed1 }; ascConf.pins = &pins; /* FIFO buffers configuration */ ascConf.txBuffer = g_uartTxBuffer; /* Set the transmission buffer */ ascConf.txBufferSize = ASC_TX_BUFFER_SIZE; /* Set the transmission buffer size */ ascConf.rxBuffer = g_uartRxBuffer; /* Set the receiving buffer */ ascConf.rxBufferSize = ASC_RX_BUFFER_SIZE; /* Set the receiving buffer size */ /* Init ASCLIN module */ IfxAsclin_Asc_initModule(&g_asclin, &ascConf); /* Initialize the module with the given configuration */ } void stepFunctionuart_loopback(){ Ifx_SizeT numBytes = 1; IfxAsclin_Asc_read(&g_asclin, &g_rxData, &numBytes, TIME_NULL); /* Receive data via RX */ if(numBytes) { IfxAsclin_Asc_write(&g_asclin, &g_rxData, &numBytes, TIME_INFINITE); /* Transmit data via TX */ } }
2. In the already opened Simulink model, add a MATLAB System block and assign the generated system object file without .m extension to the block.
A sample Simulink model is shown here.
Deploy the Model on Infineon hardware board
When you perform Build, Deploy & Start action for the model, the host computer communicates with the target, on which the generated executable runs. To perform Build, Deploy & Start:
1. In the Simulink model, press Ctrl+E. The Configuration Parameters dialog box appears.
2. Navigate to Code Generation > Optimization and ensure that Tunable
is selected for Default parameter behavior.
3. Click Apply and OK.
4. In the Simulink model, click Hardware and click Build, Deploy & Start.
The code will be generated and the same will be automatically deployed to the Infineon AURIX TC3x board.
5. Start the SSH utility. This example uses PuTTY.
6. Configure the PuTTY with the block parameters that you specified ealier. A sample configuration is shown here.
7. Type some text into the PuTTY terminal, and the text you type should echo back immediately, as shown on this screen.
Other Things to Try
Create complex device driver blocks such as LIN, analog subsystem and other blocks.