iLLD_TC27xC  1.0
How to use the ASCLIN ASC Interface driver?
Collaboration diagram for How to use the ASCLIN ASC Interface driver?:

The ASC interface driver provides a default ASCLIN configuration for asynchronous serial communication in 8bit mode, and a set of data transfer routines.

Data transfers are buffered by the hardware based FIFOs, and in addition by software based FIFOs with a configurable size. Incoming and outgoing data is transfered in background from/to the ASCLIN peripheral by interrupt service handlers, which are part of this driver as well. This allows a nonblocking communication without stalling the thread(s) from where data is sent and received.

In the following sections it will be described, how to integrate the driver into the application framework.

Preparation

Include Files

Include following header file into your C code:

Variables

Declare the ASC handle and the FIFOs as global variables in your C code:

// used globally
static IfxAsclin_Asc asc;
#define ASC_TX_BUFFER_SIZE 64
static uint8 ascTxBuffer[ASC_TX_BUFFER_SIZE + sizeof(Fifo) + 8];
#define ASC_RX_BUFFER_SIZE 64
static uint8 ascRxBuffer[ASC_RX_BUFFER_SIZE + sizeof(Fifo) + 8];

As you can see above, the transfer buffers allocate not only memory for the data itself, but also for FIFO runtime variables. 8 bytes have to be added to ensure a proper circular buffer handling independent from the address to which the buffers have been located.

Interrupt Handler Installation

See also How to define Interrupts?

Define priorities for the Interrrupt handlers. This is normally done in the Ifx_IntPrioDef.h file:

// priorities are normally defined in Ifx_IntPrioDef.h
#define IFX_INTPRIO_ASCLIN0_TX 1
#define IFX_INTPRIO_ASCLIN0_RX 2
#define IFX_INTPRIO_ASCLIN0_ER 3

Add the interrupt service routines to your C code. They have to call the ASC interrupt handlers by passing the asc handle:

IFX_INTERRUPT(asclin0TxISR, 0, IFX_INTPRIO_ASCLIN0_TX)
{
}
IFX_INTERRUPT(asclin0RxISR, 0, IFX_INTPRIO_ASCLIN0_RX)
{
}
IFX_INTERRUPT(asclin0ErISR, 0, IFX_INTPRIO_ASCLIN0_ER)
{
}

Finally install the interrupt handlers in your initialisation function:

// install interrupt handlers
IfxCpu_Irq_installInterruptHandler(&asclin0TxISR, IFX_INTPRIO_ASCLIN0_TX);
IfxCpu_Irq_installInterruptHandler(&asclin0RxISR, IFX_INTPRIO_ASCLIN0_RX);
IfxCpu_Irq_installInterruptHandler(&asclin0ErISR, IFX_INTPRIO_ASCLIN0_ER);

Module Initialisation

The module initialisation can be done in the same function. Here an example:

// create module config
IfxAsclin_Asc_initModuleConfig(&ascConfig, &MODULE_ASCLIN0);
// set the desired baudrate
ascConfig.baudrate.prescaler = 1;
ascConfig.baudrate.baudrate = 1000000; // FDR values will be calculated in initModule
// ISR priorities and interrupt target
ascConfig.interrupt.txPriority = IFX_INTPRIO_ASCLIN0_TX;
ascConfig.interrupt.rxPriority = IFX_INTPRIO_ASCLIN0_RX;
ascConfig.interrupt.erPriority = IFX_INTPRIO_ASCLIN0_ER;
// FIFO configuration
ascConfig.txBuffer = &ascTxBuffer;
ascConfig.txBufferSize = ASC_TX_BUFFER_SIZE;
ascConfig.rxBuffer = &ascRxBuffer;
ascConfig.rxBufferSize = ASC_RX_BUFFER_SIZE;
// pin configuration
const IfxAsclin_Asc_Pins pins = {
NULL, IfxPort_InputMode_pullUp, // CTS pin not used
NULL, IfxPort_OutputMode_pushPull, // RTS pin not used
};
ascConfig.pins = &pins;
// initialize module
//IfxAsclin_Asc asc; // defined globally
IfxAsclin_Asc_initModule(&asc, &ascConfig);

The ASC is ready for use now!

Data Transfers

Simple Transfers

The ASC driver provides simple to use transfer functions, which are blocking.

This means: you can send as much data as you want without taking care for the fill state of the FIFO. If the FIFO is full, the blocking function will wait until the next byte has been transfered to ASCLIN before putting the new byte into the FIFO:

// send 3 bytes

A simple to use receive function is available as well. If no data is in the receive FIFO, it will wait until the next byte has been received:

// receive a byte

Streamed Transfers

Streamed transfers are handled faster by the ASC driver and therefore they are recommended whenever a large bulk of data should be sent. Here an example:

uint8 txData[9] = { 0x49, 0x6e, 0x66, 0x69, 0x6e, 0x65, 0x6f, 0x6e, 0x0a };
Ifx_SizeT count = 9;
IfxAsclin_Asc_write(&asc, txData, &count, TIME_INFINITE);

Data can be received the following way:

uint8 rxData[5];
// wait until 5 bytes have been received
Ifx_SizeT count = 5;
IfxAsclin_Asc_read(&asc, rxData, &count, TIME_INFINITE);

Or alternatively with:

uint8 rxData[5];
// how many bytes have been received?
// limit to our buffer size
count = count < 5 ? count : 5;
// transfer received data into buffer
IfxAsclin_Asc_read(&asc, rxData, &count, TIME_INFINITE);