Introduction
This module explains how to include and use an IR Generation driver for CC11xx, CC13xx, and CC26xx devices. This example exercises the IR driver to transmit basic mark/space pattern over a standard 40 kHz modulated carrier.
Prerequisites
This module was based on the TI Drivers design as explained in the TI Drivers Project Zero training.
You will need:
- CC2640R2 Launchpad (Any CC11XX/CC13XX/CC26XX device can also be used)
- Logic Analyzer (optional; to see the IR output)
- CCS Cloud or CCS 7.1+
Task 0 - Importing the IR Generation Project
Find the IR Generation project under 'Projects' > 'TI Drivers IR Generation' and import the project into either the CCS or CCS Cloud.
Connect a CC2640R2 Launchpad, build, load, and run the project on the target. You should observe a blinking green LED if the project is running.
After we verified that target is running, we should take a brief look at the code.
Orienting ourselves in the code
The IR generation example comes preconfigured with two TI-RTOS tasks already
constructed in main() via calls to Task_construct() API. The first task is
set up to use the workTaskFunc function, which in turn uses the PIN Driver to
toggle an led. The second task is set up to use the pinTaskFunc function. The
pinTaskFunc function is used to handle button detection, including
Pin Debouncing.
A Semaphore is used to synchronize the pinTaskFunc task so it only runs upon a
key press. A Semaphore is also used later in this exercise to synchronize when
the IR driver has completed an IR transmission.
In the main() function, after BIOS_start() is called, the main function will
not progress further, but instead give control to the TI-RTOS scheduler which
will start the constructed tasks, for example workTaskFunc. Task functions are
generally structured to enter an infinite loop and never return, letting the
TI-RTOS kernel to switch to higher priority tasks or interrupts as needed.
Training solution
The solution to these exercises found in this training are contained within the
example's solution subdirectory. You can simply copy and pasted the contents of
the solution into their associated files and enable the build of the IR
generation driver.
Task 1 - Integrate IR generation driver
We will include the IR generation for a basic IR transmission, in the 'pinTaskFunc'. This task is given higher priority than the 'workTaskFunc' as IR generation is somewhat timing critical.
Board files
All TI Drivers depend on a Driver_config structure to be present at
link-time so the driver can access needed hardware attributes and data objects
for proper operation. Each entry in the Driver_config table corresponds to a
particular instance that can opened. In this case, the IRGENCC26XX driver
depends on an IRGENCC26XX_config structure with only one instance. The
hardware attributes of this single dictate what uDMA channels are to be used and
what pins need to be configured.
With the standard TI Drivers (those that need a #include IRGENCC26XX_config structure is included in portable
IRGEN_Board.c and IRGEN_Board.h files.
Shown below are the portable IRGEN_Board.c and IRGEN_Board.h files:
/*
* ============================= IRGEN begin ===================================
*/
#include "IRGENCC26XX.h"
IRGENCC26XX_Object irgenCC26XXObject = {0};
const IRGENCC26XX_HWAttrs irgenCC26XXHWAttrs = {
.irLedPin = CC2640R2_LAUNCHXL_LED_IR,
#ifdef IRGENCC26XX_DEBUG
.irOutputPin = CC2640R2_LAUNCHXL_IR_OUTPUT_DEBUG,
.irDataChPin = CC2640R2_LAUNCHXL_IR_DATA_CH_DEBUG,
.irShadowChPin = CC2640R2_LAUNCHXL_IR_SHADOW_CH_DEBUG,
#endif //IRGENCC26XX_DEBUG
.dmaChannelBitMask = (( 1 << UDMA_CHAN_TIMER0_A) | \
( 1 << UDMA_CHAN_TIMER0_B) | \
( 1 << UDMA_CHAN_TIMER1_A) | \
( 1 << UDMA_CHAN_SW_EVT0 ) | \
( 1 << UDMA_CHAN_SW_EVT1 ) | \
( 1 << UDMA_CHAN_SW_EVT2 ) | \
( 1 << UDMA_CHAN_SW_EVT3 )),
.dmaSoftChannelBitMask = (( 1 << UDMA_CHAN_SW_EVT0 ) | \
( 1 << UDMA_CHAN_SW_EVT1 ) | \
( 1 << UDMA_CHAN_SW_EVT2 ) | \
( 1 << UDMA_CHAN_SW_EVT3 )),
};
/* IRGEN configuration structure */
const IRGENCC26XX_Config IRGENCC26XX_config = {
&irgenCC26XXObject,
&irgenCC26XXHWAttrs
};
IRGENCC26XX_config in IRGEN_Board.c
/* IR Generation */
#define CC2640R2_LAUNCHXL_LED_IR IOID_1
#define CC2640R2_LAUNCHXL_IR_OUTPUT_DEBUG IOID_25 /* DP0 */
#define CC2640R2_LAUNCHXL_IR_DATA_CH_DEBUG IOID_24 /* DP1 */
#define CC2640R2_LAUNCHXL_IR_SHADOW_CH_DEBUG IOID_23 /* DP2 */
Pin defines in IRGEN_Board.h
In IRGEN_Board.h you can see that 4 IOs are defined. We will come back to the
_DEBUG pins later. When adding IRGEN_Board.[ch], make sure that these pins
are not used by any other TI Driver (again - think of the existing
Driver_config tables in the board.c file).
Tip!
The board .c file contains a BoardGpioInitTable pin array, that initializes
the listed pins to a known default state upon a board reset. If you find that
you need set the IR pin into some default state prior to opening the driver,
this table is where you set this behavior. This applies to all TI Drivers as
well.
Integrating the IR Generation driver
The next steps involve adding the following into irgeneration.c.
Step 1: Add the driver's header file
We need to add the driver's header so that we have access to the IR generation functions.
/* Add IR Generation module */
#include "IRGENCC26XX.h"
Including the IRGEN Driver's header file
Step 2: Storing the driver's handle
Similar to all TI Drivers, the IRGEN driver operate with handles. Now, we need
the application to store the handle returned by IRGENCC26XX_open().
/* IR generation handle */
static IRGENCC26XX_Handle irgenHandle;
Add the IRGEN driver's handle for the application
Step 3: Add the driver's callback function
The IRGEN driver performs callbacks when the IRGEN driver sends out IR
transmissions. Here, we define the callback function as defined in IRGENCC26XX.h
/* IR generation callback */
static void IRGENCC26XX_callbackFxn(IRGENCC26XX_Handle handle, bool done);
Defining a IRGENCC26XX driver callback function
For now, we'll have the application implement a basic callback function stub.
Implementing a proper IRGENCC26XX_callbackFxn will be done later in this lab.
IRGENCC26XX_callbackFxn driver callback
The IR generation driver calls our callback function for two conditions. It's important to note that this driver will invoke the callback function on one of these 2 instances:
- After the end of active part of the IR transmission, but just before the repeat period. It is safe to schedule next IR transmission now.
- After the end of repeat period. This is only called if there are no pending IR transmissions.
static void IRGENCC26XX_callbackFxn(IRGENCC26XX_Handle handle, bool done)
{
(void) handle;
(void) done;
}
A basic IRGENCC26XX driver's callback function
Step 4: Initializing the driver
The IRGENCC26XX driver is initialized by calling it's IRGENCC26XX_init()
function. Add the following:
/* Initialize IR engine */
IRGENCC26XX_init((IRGENCC26XX_Handle)&(IRGENCC26XX_config));
IRGEN Driver data
Next we will define a sample signal. This is the input to the IRGEN driver. Normally this would be read from a database, or calculated from a formula. There are two arrays. One array defines the Mark period and the other defines the Space period.
An IR signal is often described in terms of marks and spaces. Mark denotes the active part of a pulse, whereas Space denotes the inactive part of a pulse. A pulse is a pair of Mark and Space, and active means that the IR diode is actively driven. The Mark period is often modulated with a carrier, but it is not always the case.
A sample IR transmission

IR transmission with markBufferExample[0] highlighted

IR transmission with markBufferExample[0] and spaceBufferExample[0] highlighted

To conserve memory, each array contains 16 bit unsigned values. To still provide adequate resolution each value represents 4us ticks. A macro is used to convert from 1us to 4us by shifting by 2.
Step 5: Supply example IR data
Add the example data to be used by the IRGEN driver. You can correlate the lengths of the mark and space patterns with the IR transmission images above.
/* Static example IR signal */
uint16_t markBufferExample[] =
{
IRGEN_FROM_1US_TO_4US_TICKS(1200), IRGEN_FROM_1US_TO_4US_TICKS(600), IRGEN_FROM_1US_TO_4US_TICKS(360),
IRGEN_FROM_1US_TO_4US_TICKS(360), IRGEN_FROM_1US_TO_4US_TICKS(360), IRGEN_FROM_1US_TO_4US_TICKS(600),
IRGEN_FROM_1US_TO_4US_TICKS(600), IRGEN_FROM_1US_TO_4US_TICKS(360), IRGEN_FROM_1US_TO_4US_TICKS(600),
IRGEN_FROM_1US_TO_4US_TICKS(360)
};
uint16_t spaceBufferExample[] =
{
IRGEN_FROM_1US_TO_4US_TICKS(840), IRGEN_FROM_1US_TO_4US_TICKS(840), IRGEN_FROM_1US_TO_4US_TICKS(960),
IRGEN_FROM_1US_TO_4US_TICKS(960), IRGEN_FROM_1US_TO_4US_TICKS(840), IRGEN_FROM_1US_TO_4US_TICKS(840),
IRGEN_FROM_1US_TO_4US_TICKS(960), IRGEN_FROM_1US_TO_4US_TICKS(840), IRGEN_FROM_1US_TO_4US_TICKS(960),
IRGEN_FROM_1US_TO_4US_TICKS(84000)
};
IR signal example
Runtime IRGEN parameters
The IRGENCC26XX driver has runtime parameters IRGENCC26XX_Params that are
passed into the driver when opened and IRGENCC26XX_SubParams for when the
driver prepares for an IR transmission.
Let us define the default IRGENCC26XX_Params parameters to open the IR
generation driver with. It contains:
callbackFxn- The callback function we defined earlier.carrierPeriod24MHz- Carrier period in 24MHz tickscarrierDuty- Carrier duty cycle (Defines the active period of the carrier pulses in percent)timeoutOffset- Timeout offsetmarkBuffer- Array for Mark valuesspaceBuffer- Array for Space valuesbufferSize- Number of Mark + Space pairs
The IR transmission is generated based on a GPT (General Purpose Timer). The timer counter counts down in IR generation mode. The timer's period and duty cycle is based on Load and Match values.
The edge of the output transmission is set high when the count is equal to the load value. When it reaches the match value the transmission is set low.
Thus, it is important that the next load value is updated after the counter has counted down below the current load value.
A DMA is used to update the timer registers (Load and Match). If the IR transmission consists of Mark+Space values that are always greater than the previous Space, then the IR driver can use the match event to trigger the DMA. However, that is not a condition most IR signals comply with. Thus, a shadow timer is used to generate DMA triggers.
Now we can explain what the timeoutOffset is for. It defines when, relative to
the next Mark + Space pair, the shadow timer should trigger a DMA transfer.
There are two aspects to consider for this parameter
- It must be long enough to allow the DMA transfer to complete. With normal bus load 4us this is enough. However, if there are other higher priority DMAs or other operations that require bus access, then it may not be enough.
- It must be short enough to allow the smallest Space duration. 4us allows a single carrier Space duration, when carrier frequency is 250kHz. This is in most cases more than short enough.
Step 6: Add the default IRGENCC26XX_Params
/***********************************************************************************
* Configuration parameters
*/
IRGENCC26XX_Params irgenParams = {
IRGENCC26XX_callbackFxn,
CARRIER_PERIOD_24MHZ,
CARRIER_DUTY,
TIMEOUT_OFFSET,
NULL, /* Mark buffer pointer needs to be set before calling IRGENCC26XX_open */
NULL, /* Space buffer pointer needs to be set before calling IRGENCC26XX_open */
0, /* Size of buffers needs to be set before calling IRGENCC26XX_open */
};
IRGENCC26XX_Params for IRGENCC26XX_open()
The last three parameters: markBuffer, spaceBuffer and bufferSize are
populated when the IR signal is prepared.
Compile and run!
Let us test our changes and make sure that the driver was integrated into our project.
What happens when you press the button now?
Task 2 - Generate IR signal
Now that we have added the IR generation driver, it is time to use it! We will generate IR signals when a user presses button. We already do something when a user presses the button. So, we know where to start the signal.
Step 7: Modify pinTaskFunc as follows:
Void pinTaskFunc(UArg arg0, UArg arg1)
{
irgenParams.markBuffer = markBufferExample;
irgenParams.spaceBuffer = spaceBufferExample;
irgenParams.bufferSize = sizeof(markBufferExample)/sizeof(uint16_t);
while (1) {
Semaphore_pend(Semaphore_handle(&keyInterruptSem), BIOS_WAIT_FOREVER);
/* Debounce 25ms */
Task_sleep(25 * (1000 / Clock_tickPeriod));
/* Then safely read IO */
if (PIN_getInputValue(Board_GPIO_BUTTON0) == 0)
{
/* Button is pressed, so generate IR signal */
/* Open IRGEN handle; we'll assume that irgenHandle != NULL */
irgenHandle = IRGENCC26XX_open(&irgenParams);
PIN_setOutputValue(pinHandle, Board_GPIO_LED0, 1);
IRGENCC26XX_startIrGen(irgenHandle);
Semaphore_pend(Semaphore_handle(&irgenSem), BIOS_WAIT_FOREVER);
PIN_setOutputValue(pinHandle, Board_GPIO_LED0, 0);
/* Close IRGEN handle*/
IRGENCC26XX_close(irgenHandle);
}
}
}
Let us take a closer look on what exactly this task is supposed to do.
First we have updated the parameters to start the IR transmission. We've added pointers to the example buffers and updated the size.
irgenParams.markBuffer = markBufferExample;
irgenParams.spaceBuffer = spaceBufferExample;
irgenParams.bufferSize = sizeof(markBufferExample)/sizeof(uint16_t);
Next, the task performs the following in its while (1) loop:
- Open the driver
- Start the transmission of the IR signal
- Wait for the IR transmission to complete (this suspends the task's execution)
- Close the driver
/* Open IRGEN handle*/
irgenHandle = IRGENCC26XX_open(&irgenParams);
IRGENCC26XX_startIrGen(irgenHandle);
Semaphore_pend(Semaphore_handle(&irgenSem), BIOS_WAIT_FOREVER);
/* Close IRGEN handle*/
IRGENCC26XX_close(irgenHandle);
To suspend, or block, the pinTaskFunc task's execution, we utilize an RTOS
primitive known as a Semaphore. The idea is to let the IRGEN Driver wake up
pinTaskFunc and resume its execution when it finished sending the current IR
transmission.
To add the irgenSem object into our application, we'd simply construct one
similar to the existing keyInterruptSem which is used to unblock pinTaskFunc
when a key was pressed.
Step 8: Add the irgenSem Semaphore
Adding a Semaphore is easily done by first allocating memory for the Semaphore object via the following:
/* IR generation complete Semaphore */
Semaphore_Struct irgenSem;
Allocating Semaphore object data structure for irgenSem
After having allocated memory for the Semaphore, it must be constructed before
it can be used. We can do this exactly the same way keyInterruptSem is
constructed in main().
/*
* Construct irgenSem Semaphore to synchronize between
* IRGENCC26XX_callbackFxn and pinTaskFunc
*/
Semaphore_Params_init(¶ms.sem);
params.sem.mode = Semaphore_Mode_BINARY;
Semaphore_construct(&irgenSem, 0, ¶ms.sem);
Constructing irgenSem Semaphore object
Waiting for signal to complete.
At this stage in the lab, we have added and the task is pending on the irgemSem
Semaphore. The pinTaskFunc assumes that the semaphore will be posted when the
IR signal has completed. Will this ever happen?
The counterpart to Semaphore_pend() is a Semaphore_post(). So far, the
pinTaskFunc only pends Semaphores but we don't have anything calling
Semaphore_post(). This is where we need to have the IRGEN driver signal to the
pinTaskFunc that it has completed IR transmissions. Since we already have a
callback mechanism in the IRGEN driver, the next step is to have the callback
function post our irgenSem Semaphore.
What does the repeat period mean here?
Step 9: Add Semaphore_post() calls to the IRGEN callback
Let us now revisit the IRGENCC26XX_callbackFxn callback function implementation
and add the needed Semaphore_post() calls.
static void IRGENCC26XX_callbackFxn(IRGENCC26XX_Handle handle, bool done)
{
/*
* IR engine generates two interrupts
* 1. After the end of active part of the IR transmission, but just before
* the repeat period. It is safe to schedule next IR transmission now.
* 2. After the end of repeat period. This is only called if there are no
* pending IR transmissions.
*/
if (done)
{
/*
* IR engine has completed generation of the signal, and there are no
* pending signals.
*/
Semaphore_post(Semaphore_handle(&irgenSem));
}
else
{
/*
* Here we can safely update the buffers for next signal.
* Utilizing the repeat period!
*/
}
}
IR generation callback function with Semaphore_post()
Compile and Run. What happens when you press the button now?
Capturing the generated IR transmission
Now that we have updated the source as required, hit the build button and run the program! Press the button labeled BTN-1 to trigger IR generation.

To see the IR signal connect your favorite logic analyzer, or oscilloscope, to DIO1 so you can observe an output similar to the one shown here:

Task 3 - Keep transmitting IR signal
If the button is kept pressed, we want to keep transmitting IR signals. The IR generation driver anticipated this. There are two additional functions to assist with this:
Void IRGENCC26XX_prepareNextSignal(IRGENCC26XX_SubParams *irParams)Void IRGENCC26XX_kickNextSignal()
As we learned in the previous task, the IR generation driver calls our callback function two times per signal. Now we are interested in the first callback. This tells us that we can prepare a new transmission. If a new transmission is prepared before the current one has finished its repeat period, then the IR generation driver will start a new signal instead of calling the callback function at the end of the repeat period.
If we leave the IR generation driver open after it has finished its current signal, we can use the additional APIs to kick off a new signal. To save power, the driver should be closed as soon as no more signals need to be generated.
First, we need to define the signal we will transmit for the repeats. In our
case we'll just reuse the same signal. Add the following IRGENCC26XX_SubParams
into pinTaskFunc:
Void pinTaskFunc(UArg arg0, UArg arg1)
{
IRGENCC26XX_SubParams irParams;
irgenParams.markBuffer = markBufferExample;
irgenParams.spaceBuffer = spaceBufferExample;
irgenParams.bufferSize = sizeof(markBufferExample)/sizeof(uint16_t);
irParams.carrierPeriod24MHz = irgenParams.carrierPeriod24MHz;
irParams.markBuffer = irgenParams.markBuffer;
irParams.spaceBuffer = irgenParams.spaceBuffer;
irParams.bufferSize = irgenParams.bufferSize;
while (1) {
After we tell the IR generation driver start an IR transmission, we want to have
the pinTaskFunc wait for the active part to end and then check if the button is
still pressed.
Update the following snippet of pinTaskFunc with the following:
/* Then safely read IO */
if (PIN_getInputValue(Board_GPIO_BUTTON0) == 0)
{
/* Button is pressed, so generate IR signal */
/* Open IRGEN handle*/
irgenHandle = IRGENCC26XX_open(&irgenParams);
PIN_setOutputValue(pinHandle, Board_GPIO_LED0, 1);
IRGENCC26XX_startIrGen(irgenHandle);
/* First we wait for active part of signal to end */
Semaphore_pend(Semaphore_handle(&irgenSem), BIOS_WAIT_FOREVER);
/* Keep transmitting while button is pressed */
while (PIN_getInputValue(Board_GPIO_BUTTON0) == 0)
{
...
}
While BTN_1 is pressed, we should prepare our next transmission, and wait for next active part to end, before we check the button again.
/* Keep transmitting while button is pressed */
while (PIN_getInputValue(Board_GPIO_BUTTON0) == 0)
{
/* Then we can prepare the same signal again */
IRGENCC26XX_prepareNextSignal(&irParams);
/* Before we wait for active part to end again */
Semaphore_pend(Semaphore_handle(&irgenSem), BIOS_WAIT_FOREVER);
}
Finally, when the button is no longer detected, we will wait for the repeat period to end as well.
/* Keep transmitting while button is pressed */
while (PIN_getInputValue(Board_GPIO_BUTTON0) == 0)
{
/* Then we can prepare the same signal again */
IRGENCC26XX_prepareNextSignal(&irParams);
/* Before we wait for active part to end again */
Semaphore_pend(Semaphore_handle(&irgenSem), BIOS_WAIT_FOREVER);
}
/* Now wait for repeat period to end */
Semaphore_pend(Semaphore_handle(&irgenSem), BIOS_WAIT_FOREVER);
PIN_setOutputValue(pinHandle, Board_GPIO_LED0, 0);
/* Close IRGEN handle*/
IRGENCC26XX_close(irgenHandle);
Putting all the pieces together, your pinTaskFunc should now look like this:
Void pinTaskFunc(UArg arg0, UArg arg1)
{
IRGENCC26XX_SubParams irParams;
irgenParams.markBuffer = markBufferExample;
irgenParams.spaceBuffer = spaceBufferExample;
irgenParams.bufferSize = sizeof(markBufferExample)/sizeof(uint16_t);
irParams.carrierPeriod24MHz = irgenParams.carrierPeriod24MHz;
irParams.markBuffer = irgenParams.markBuffer;
irParams.spaceBuffer = irgenParams.spaceBuffer;
irParams.bufferSize = irgenParams.bufferSize;
while (1) {
Semaphore_pend(Semaphore_handle(&keyInterruptSem), BIOS_WAIT_FOREVER);
/* Debounce 25ms */
Task_sleep(25 * (1000 / Clock_tickPeriod));
/* Then safely read IO */
if (PIN_getInputValue(Board_GPIO_BUTTON0) == 0)
{
/* Button is pressed, so generate IR signal */
/* Open IRGEN handle*/
irgenHandle = IRGENCC26XX_open(&irgenParams);
PIN_setOutputValue(pinHandle, Board_GPIO_LED0, 1);
IRGENCC26XX_startIrGen(irgenHandle);
/* First we wait for active part of signal to end */
Semaphore_pend(Semaphore_handle(&irgenSem), BIOS_WAIT_FOREVER);
/* Keep transmitting while button is pressed */
while (PIN_getInputValue(Board_GPIO_BUTTON0) == 0)
{
/* Then we can prepare the same signal again */
IRGENCC26XX_prepareNextSignal(&irParams);
/* Before we wait for active part to end again */
Semaphore_pend(Semaphore_handle(&irgenSem), BIOS_WAIT_FOREVER);
}
/* Now wait for repeat period to end */
Semaphore_pend(Semaphore_handle(&irgenSem), BIOS_WAIT_FOREVER);
PIN_setOutputValue(pinHandle, Board_GPIO_LED0, 0);
/* Close IRGEN handle*/
IRGENCC26XX_close(irgenHandle);
}
}
}
Will this work?
Why?
We must make sure to post the semaphore when the IR generation driver invokes it callback function when the active part of the transmission has completed.
In IRGENCC26XX_callbackFxn, add a call to `Semaphore_post() after finishing the
active part of the transmission as shown below:
static void IRGENCC26XX_callbackFxn(IRGENCC26XX_Handle handle, bool done)
{
/*
* IR engine generates two interrupts
* 1. After end of active part of signal, just before the repeat period. It
* is safe to schedule next signal now.
* 2. After end of repeat period. This is only called if there are no
* pending signals.
*/
if (done)
{
/*
* IR engine has completed generation of the signal, and there are no
* pending signals.
*/
Semaphore_post(Semaphore_handle(&irgenSem));
}
else
{
/*
* Here we can safely update the buffers for next signal.
* Utilizing the repeat period!
*/
Semaphore_post(Semaphore_handle(&irgenSem));
}
}
IR generation driver call backs
The IR generation driver does not call back, at the end of repeat period, in case there's a pending signal.
This is why we can go directly from preparing the next transmission to wait for the end of next active part. If the IR generation driver had generated a callback for the end of repeat, even when there was a pending signal, we would have had to pend twice.
Try short button presses
Press the button quickly and see what happens.
Why is a signal generated after the button is released?
We poll the button only right after the active part of an IR signal has completed.
Task 4 - IR generation debugging signals
As discussed in the first task, we have defined 4 IOs for the IR generation driver. This even though the IR generation driver only produces one output. The other three IOs are used to output debugging signals.

To enable these signals we have to define IRGENCC26XX_DEBUG in the project's
predefined symbols. Edit project properties and remove the x in the predefined
xIRGENCC26XX_DEBUG symbol.
Epilogue
This example has given a hands on in how to integrate and use the IR generation driver. It is now up to you to generate your favorite IR signals!
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.