TI Tiva ARM Programming For Embedded Systems: Programming ARM Cortex-M4 TM4C123G With C (Mazidi Amp; ~UPD~
TI Tiva ARM Programming For Embedded Systems: Programming ARM Cortex-M4 TM4C123G with C (Mazidi & Naimi)
Introduction
If you are interested in developing embedded systems using a powerful and versatile microcontroller, you might want to consider using TI Tiva ARM. TI Tiva ARM is a family of microcontrollers based on the ARM Cortex-M4 core, which offers high performance, low power consumption, and rich features. In this article, we will introduce you to the basics of TI Tiva ARM programming, using the TM4C123G LaunchPad as an example. We will also show you how to use C language to program the TM4C123G microcontroller, and how to explore some advanced topics such as timers, communication protocols, and real-time operating systems (RTOS).
TI Tiva ARM Programming For Embedded Systems: Programming ARM Cortex-M4 TM4C123G with C (Mazidi amp;
What is TI Tiva ARM?
TI Tiva ARM is a brand name for a series of microcontrollers developed by Texas Instruments (TI), based on the 32-bit ARM Cortex-M4 core. The ARM Cortex-M4 core is a high-performance processor that supports both 32-bit and 16-bit instructions, as well as floating-point arithmetic and digital signal processing (DSP) operations. The TI Tiva ARM microcontrollers also include various peripherals such as GPIOs, timers, ADCs, DACs, UARTs, SPIs, I2Cs, CANs, USBs, and more. These peripherals allow the microcontroller to interface with various sensors, actuators, displays, and other devices.
What is ARM Cortex-M4 TM4C123G?
The TM4C123G is one of the most popular microcontrollers in the TI Tiva ARM family. It has a clock speed of up to 80 MHz, 256 KB of flash memory, 32 KB of SRAM, and 43 GPIO pins. It also has two 12-bit ADCs with 16 channels each, four 16-bit timers with PWM capability, six UARTs, four SPIs, four I2Cs, two CANs, one USB device controller, and more. The TM4C123G is ideal for applications that require high performance, low power consumption, and connectivity.
What are the benefits of using TI Tiva ARM for embedded systems?
There are many benefits of using TI Tiva ARM for embedded systems development. Some of them are:
TI Tiva ARM microcontrollers are easy to program using C language, which is widely used and supported by many compilers and IDEs.
TI Tiva ARM microcontrollers have a large and active community of developers and users who share their knowledge and experience through forums, blogs, tutorials, and books.
TI Tiva ARM microcontrollers are compatible with many sensors, actuators, displays, and other devices that are commonly used in embedded systems projects.
TI Tiva ARM microcontrollers are affordable and accessible, as they are available in various packages and prices, and can be purchased online or from local distributors.
TI Tiva ARM microcontrollers are scalable and flexible, as they can be used for a wide range of applications, from simple to complex, and can be customized and optimized according to the needs and preferences of the developer.
Getting started with TI Tiva ARM programming
Now that you have learned some basic information about TI Tiva ARM, you might be wondering how to get started with programming it. In this section, we will guide you through the steps of setting up the hardware and software requirements for TI Tiva ARM programming.
Hardware requirements
The hardware requirements for TI Tiva ARM programming are minimal. All you need are:
A TM4C123G LaunchPad board, which is a development kit that includes the TM4C123G microcontroller and some other components such as LEDs, buttons, switches, and connectors.
A USB cable, which is used to connect the TM4C123G LaunchPad board to your computer and to provide power supply and data transfer.
A breadboard, some jumper wires, and some optional components such as resistors, LEDs, potentiometers, etc., which are used to build circuits and test the functionality of the TM4C123G microcontroller.
Software requirements
The software requirements for TI Tiva ARM programming are also minimal. All you need are:
A C compiler, which is a program that converts your C code into machine code that can be executed by the TM4C123G microcontroller. There are many C compilers available for TI Tiva ARM programming, such as Keil uVision, Code Composer Studio (CCS), IAR Embedded Workbench, etc. In this article, we will use Keil uVision as an example.
A driver, which is a program that allows your computer to communicate with the TM4C123G LaunchPad board via the USB cable. The driver is usually included in the installation package of the C compiler or can be downloaded separately from the TI website.
A debugger, which is a program that allows you to monitor and control the execution of your program on the TM4C123G microcontroller. The debugger is usually integrated with the C compiler or can be accessed separately using tools such as J-Link or OpenOCD.
Setting up the development environment
The development environment is the combination of hardware and software that you use to write, compile, debug, and run your TI Tiva ARM programs. To set up the development environment for TI Tiva ARM programming, you need to follow these steps:
Download and install Keil uVision from https://www.keil.com/download/product/. You can choose the free version or the paid version depending on your needs and preferences. The free version has some limitations such as code size restriction and feature restriction.
Download and install the driver for the TM4C123G LaunchPad board from https://www.ti.com/tool/download/TIVAWARE-C-SERIES. This will allow your computer to recognize the board when you connect it via the USB cable.
Download and install the TivaWare library from https://www.ti.com/tool/download/TIVAWARE-C-SERIES. This is a collection of software components that provide functions and examples for using the peripherals and features of the TM4C123G microcontroller.
Connect the TM4C123G LaunchPad board to your computer using the USB cable. Make sure that the board is powered on and that the computer detects it correctly.
Launch Keil uVision and create a new project. Choose the TM4C123GH6PM device as the target device and select a suitable template for your project. You can also configure some settings such as memory allocation, optimization level, output format, etc.
Basic concepts of TI Tiva ARM programming
After setting up the development environment, you are ready to write your first TI Tiva ARM program. In this section, we will introduce you to some basic concepts of TI Tiva ARM programming, such as the structure of a TI Tiva ARM program, the syntax of C language for TI Tiva ARM, and the use of registers, peripherals, and interrupts.
The structure of a TI Tiva ARM program
A TI Tiva ARM program consists of three main parts: the header files, the global variables and constants, and the main function. The header files are used to include the libraries and definitions that are needed for your program. The global variables and constants are used to declare and initialize the variables and constants that are accessible throughout your program. The main function is the entry point of your program, where you write the logic and instructions that you want your microcontroller to execute.
Here is an example of a simple TI Tiva ARM program that blinks an LED on the TM4C123G LaunchPad board:
// Include the header files #include "TM4C123GH6PM.h" // This file defines the TM4C123G device #include "tm4c123gh6pm_startup_ccs.c" // This file contains the startup code #include "inc/hw_memmap.h" // This file contains the memory map #include "driverlib/sysctl.h" // This file contains the system control functions #include "driverlib/gpio.h" // This file contains the GPIO functions // Define some constants #define LED_RED GPIO_PIN_1 // This constant represents the red LED pin #define LED_BLUE GPIO_PIN_2 // This constant represents the blue LED pin #define LED_GREEN GPIO_PIN_3 // This constant represents the green LED pin #define DELAY 3000000 // This constant represents the delay time in cycles // Declare some global variables uint8_t led = LED_RED; // This variable holds the current LED color // Define the main function int main(void) SYSCTL_XTAL_16MHZ); // Enable the GPIO port F peripheral SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); // Configure the GPIO pins for output GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, LED_RED
The syntax of C language for TI Tiva ARM
The syntax of C language for TI Tiva ARM is similar to the standard C language syntax, with some minor differences and extensions. The syntax of C language consists of keywords, operators, identifiers, literals, comments, expressions, statements, and blocks. Here are some examples of each element:
Keywords are reserved words that have special meanings and functions in C language. For example: int, void, if, while, return, etc.
Operators are symbols that perform arithmetic, logical, bitwise, or assignment operations on operands. For example: +, -, *, /, %, &&, , !, &, , ^, , , >>, =, +=, -=, etc.
Identifiers are names that you give to variables, constants, functions, or types. For example: led, LED_RED, main, uint8_t, etc.
Literals are values that you write directly in your code. For example: 0, 1, 'A', "Hello", etc.
Comments are text that you write in your code to explain or document your program. Comments are ignored by the compiler and do not affect the execution of your program. Comments can be either single-line or multi-line. For example: // This is a single-line comment, /* This is a multi-line comment */.
Expressions are combinations of operands and operators that produce a value or a result. For example: a + b, c * d / e, f && g h, etc.
Statements are instructions that tell the microcontroller what to do. Statements can be simple or compound. Simple statements are usually terminated by a semicolon. Compound statements are usually enclosed by curly braces. For example: x = y + z;, if (x > 0) x--; , etc.
Blocks are groups of statements that are treated as a single unit. Blocks can be nested inside other blocks. Blocks are usually enclosed by curly braces. For example: a = b + c; d = e - f; , etc.
The use of registers, peripherals, and interrupts
Registers, peripherals, and interrupts are some of the most important and useful features of TI Tiva ARM programming. Registers are special memory locations that store data or control information for the microcontroller. Peripherals are devices that provide additional functionality and features for the microcontroller, such as GPIOs, timers, ADCs, DACs, UARTs, SPIs, I2Cs, CANs, USBs, etc. Interrupts are signals that notify the microcontroller of an event or condition that requires immediate attention, such as a button press, a timer overflow, a data transfer completion, etc.
To use registers, peripherals, and interrupts in TI Tiva ARM programming, you need to know how to access them, how to configure them, and how to handle them. You can access registers and peripherals using pointers or macros that represent their addresses or names. You can configure registers and peripherals using bitwise operations or functions that set or clear their bits or fields. You can handle interrupts using interrupt service routines (ISRs) or functions that execute when an interrupt occurs.
Here is an example of how to use registers, peripherals, and interrupts in TI Tiva ARM programming:
// Include the header files #include "TM4C123GH6PM.h" // This file defines the TM4C123G device #include "tm4c123gh6pm_startup_ccs.c" // This file contains the startup code #include "inc/hw_memmap.h" // This file contains the memory map #include "driverlib/sysctl.h" // This file contains the system control functions #include "driverlib/gpio.h" // This file contains the GPIO functions #include "driverlib/interrupt.h" // This file contains the interrupt functions // Define some constants #define LED_RED GPIO_PIN_1 // This constant represents the red LED pin #define LED_BLUE GPIO_PIN_2 // This constant represents the blue LED pin #define LED_GREEN GPIO_PIN_3 // This constant represents the green LED pin #define SW1 GPIO_PIN_4 // This constant represents the switch 1 pin #define SW2 GPIO_PIN_0 // This constant represents the switch 2 pin // Declare some global variables uint8_t led = LED_RED; // This variable holds the current LED color // Define the main function int main(void) SW2, GPIO_FALLING_EDGE); // Register the GPIO port F interrupt handler GPIOIntRegister(GPIO_PORTF_BASE, PortFIntHandler); // Enable the global interrupt IntMasterEnable(); // Turn off all LEDs GPIOPinWrite(GPIO_PORTF_BASE, LED_RED // Define the GPIO port F interrupt handler void PortFIntHandler(void) SW2); // Check which switch was pressed if (sw == (SW1
Advanced topics of TI Tiva ARM programming
Once you have mastered the basic concepts of TI Tiva ARM programming, you can move on to some advanced topics that will allow you to explore more features and capabilities of the TM4C123G microcontroller. In this section, we will introduce you to some advanced topics of TI Tiva ARM programming, such as how to use timers, PWM, ADC, and DAC, how to communicate with UART, SPI, I2C, and CAN, and how to implement RTOS and multitasking.
How to use timers, PWM, ADC, and DAC
Timers are peripherals that can generate periodic or one-shot signals based on a predefined count value and a clock source. Timers can be used for various purposes such as measuring time intervals, generating delays, creating waveforms, triggering events, etc. The TM4C123G microcontroller has six general-purpose timers (GPTM), each with two sub-timers (A and B) that can operate independently or in conjunction. Each sub-timer can operate in one of four modes: one-shot mode, periodic mode, capture mode, or PWM mode.
PWM stands for pulse-width modulation, which is a technique that can vary the duty cycle of a digital signal to control the amount of power delivered to a load. PWM can be used for various purposes such as dimming LEDs, driving motors, generating audio signals, etc. The TM4C123G microcontroller can generate PWM signals using its timers or its dedicated PWM module. The PWM module has four PWM generators (0-3), each with two PWM outputs (A and B) that can operate independently or in sync. Each PWM output can have its own frequency and duty cycle.
processing signals, etc. The TM4C123G microcontroller has two 12-bit ADC modules (0 and 1), each with 12 input channels (0-11) that can be configured as single-ended or differential. Each ADC module can operate in one of four modes: single sample mode, sequencer mode, comparator mode, or temperature sensor mode.
DAC stands for digital-to-analog converter, which is a peripheral that can convert a digital value into an analog signal. DAC can be used for various purposes such as generating signals, controlling devices, testing circuits, etc. The TM4C123G microcontroller has one 12-bit DAC module that has one output channel. The DAC module can operate in one of two modes: buffered mode or unbuffered mode.
Here is an example of how to use timers, PWM, ADC, and DAC in TI Tiva ARM programming:
// Include the header files #include "TM4C123GH6PM.h" // This file defines the TM4C123G device #include "tm4c123gh6pm_startup_ccs.c" // This file contains the startup code #include "inc/hw_memmap.h" // This file contains the memory map #include "driverlib/sysctl.h" // This file contains the system control functions #include "driverlib/gpio.h" // This file contains the GPIO functions #include "driverlib/timer.h" // This file contains the timer functions #include "driverlib/pwm.h" // This file contains the PWM functions #include "driverlib/adc.h" // This file contains the ADC functions #include "driverlib/dac.h" // This file contains the DAC functions // Define some constants #define LED_RED GPIO_PIN_1 // This constant represents the red LED pin #define LED_BLUE GPIO_PIN_2 // This constant represents the blue LED pin #define LED_GREEN GPIO_PIN_3 // This constant represents the green LED pin #define SW1 GPIO_PIN_4 // This constant represents the switch 1 pin #define SW2 GPIO_PIN_0 // This constant represents the switch 2 pin #define POT GPIO_PIN_0 // This constant represents the potentiometer pin #define SPEAKER GPIO_PIN_0 // This constant represents the speaker pin // Declare some global variables uint32_t adcValue = 0; // This variable holds the ADC value uint32_t dacValue = 0; // This variable holds the DAC value // Define the main function int main(void) SW2); // Enable the timer 0 peripheral SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); // Configure the timer 0 sub-timer A for periodic mode with a period of 1 second TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet() - 1); // Enable the timer 0 sub-timer A interrupt TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT); // Register the timer 0 sub-timer A interrupt handler TimerIntRegister(TIMER0_BASE, TIMER_A, Timer0IntHandler); // Enable the PWM module 1 peripheral // Define the timer 0 sub-timer A interrupt handler void Timer0IntHandler(void) // Clear the timer 0 sub-timer A interrupt flag TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT); // Toggle the green LED GPIOPinWrite(GPIO_PORTF_BASE, LED_GREEN, GPIOPinRead(GPIO_PORTF_BASE, LED_GREEN));
How to communicate with UART, SPI, I2C, and CAN
UART stands for universal asynchronous receiver/transmitter, which is a peripheral that can transmit and receive serial data using two wires (TX and RX). UART can be used for various purposes such as communicating with other devices, sending and receiving data, debugging, etc. The TM4C123G microcontroller has six UART modules (0-5), each with one TX and one RX pin. Each UART module can operate in one of four modes: normal mode, loopback mode, IrDA mode, or LIN mode.
SPI stands for serial peripheral interface, which is a peripheral that can transmit and receive serial data using four wires (SCK, MOSI, MISO, and SS). SPI can be used for various purposes such as communicating with other devices, sending and receiving data, controlling devices, etc. The TM4C123G microcontroller has four SPI modules (SSI0-3), each with one SCK, one MOSI, one MISO, and one SS pin. Each SPI module can operate in one of two modes: master mode or slave mode.
I2C stands for inter-integrated circuit, which is a peripheral that can transmit and receive serial data using two wires (SCL and SDA). I2C can be used for various purposes such as communicating with other devices, sending and receiving data, controlling devices, etc. The TM4C123G microcontroller has four I2C modules (I2C0-3)