Learning embedded programming with DEPIK's ARM embedded board
Embedded programming involve, writing programs in C language on PC and
executing them on the embedded board. Embedded programs should talk to the
hardware on the board. Hardware on embedded board consists of peripheral
chips and peripheral devices. Peripheral devices are connected to the
peripheral chips. Embedded programs talk to the peripheral chips by reading
and writing to the registers present in the peripheral chips.
We have put all our embedded teaching experience in use while developing this
ARM embedded board. As a result this board is very convenient to use. It
got lot of potential for learning from it. The best part of this board is
monitor program built into it. With the help of this monitor program, students
can explore all the peripheral chips. So the students gain very good insight
into peripheral chips. Following are the various activities, that students can
do with this board.
1. Exploring the peripheral chips by using monitor commands
- Exploring peripheral chips with monitor commands
- Loading and executing programs on the board by using monitor commands
- Writing and testing driver functions for the various peripheral chips
- Writing application programs by using the above driver functions
- Writing and testing RTOS applications
Along with the ARM board we are supplying user manual on using monitor
commands. We are also suppling a manual describing in detail how to explore
various peripheral chips present in the embedded board. Following are the
various small experiments that can be done to explore the function of various
General Purpose IO (GPIO) peripheral chip
On this embedded board, two GPIO pins are connected to two LEDs. And another
two GPIO pins are connected to two push buttons. By writing to direction
register, we can set the LED GPIO pins as output pins, and push button
GPIO pins as input pins. Next by writing to SET register, we can set the
LED GPIO pins to high. Then both LEDs should glow. Next by writing to
CLEAR register, we can clear the LED GPIO pins to low. Then LEDs should
Read the IOPIN register and find the values of GPIO pins connected to the
push buttons. Both these pins should be high. Now press one push button,
while keep pressing this button, read the IOPIN register. The GPIO pin
value of pressed push button should become zero. Similarly press second
push button and verify that its value also becoming zero.
Real Time Clock (RTC) peripheral chip
RTC chip has got the following registers:
- Seconds register
- Minutes register
- Hours register
- Date register
- Month register
- Year register
- Week register
We can read these registers and verify that they are holding the correct
time and date values. If not, set the correct values into those registers.
After setting the correct values, power-off the board. Wait for few minutes
and power-on again. By reading the date and time registers verify that
time was maintained properly even when board is powered-off. This is
possible because RTC chip has got separate power from button cell.
RTC chip also got consolidated date and time registers. This single time
register will have seconds, minutes and hours in a single register. Similarly
date, month and year are present in another consolidated register.
We can read and verify these consolidated registers.
Watch dog timer peripheral
Enable the watch dog register, by writing into its register. Once it is
enabled, we must clear the watch dog, by writing to another register, within
few milli seconds. If not, the watch dog will reset the CPU. We will observe
that, the moment we enabled the watch dog, the board will get reset.
Start the timer, by writing start bit as 1 in the timer control register.
Next keep reading the counter register. Each time you read, counter register
will be incremented by tens of millions. This is because the clock, that is
driving the counter is 30 MHz. So counter is incrementing by 30 millions per
second. So after how many seconds, this 32 bit counter register will get
overflow. You can find it both practically and theoritically.
Clear the start bit in the timer control register. Now read the counter again.
We will observe that counter is stopped. But it contains the count value, to
which it counted so far. Now write zero to this counter register. It will
Counting at 30MHZ speed may not be convenient for us. We can reduce the
clock rate to the counter. Write 30,000,000 (30 million) to timer prescale
register. So now 30MHz clock is divided by 30 million and applied to
counter. As a result the clock coming to counter will increment once in every
second. So now clock frequency is 1Hz. Now you keep reading the counter,
it is incrementing just once in every second. We are happy with it.
Now this counter is keep counting once in every second. This count to
reach maximum value (2 power 32), we have to just wait 136 years.
So we wish to put a limit to this counter. By writing 60 to match register
and setting match control register, we can control the counter. Now
counter will count only upto 60 and becomes zero. It also set one match
bit indicating that it reached match register value and got reset to zero.
We can read the match bit and found that counter completed its time cycle,
that is 60 seconds. This way we can use counter as timer to measure any
time required. If we need to measure at micro second level, we can get
micro second clock, by writing appropriate value to prescale register.
It is also possible to control the match pin, whenever counter reaches
match value. To the match pin, we can connect LED and make LED to on and off
whenever counter reaches match register value.
The same timer which we used above can be used to count the pulses on
external capture pin. As a timer it counts the internal clock
(after prescale). But as a counter it counts the pulses coming on an external
capture pin. By connecting capture pin to push button, we can count the
number of times push button is pressed. Press push button and read the
counter register, it should be incremented.
Capture mode Timer
We can also explore using timer in capture mode. Capture mode is useful
to measure the width of a input pulse, or to measure the time difference
between rising or falling edges of two inputs. When we use timer in capture
mode, the counter keeps counting with the clock. Whenever capture input
pin changes from low to high (rising edge) or hight to low (falling edge),
the value in the counter is copied to capture register. Capture mode time
supports four capture input pins. There are four capture registers, for
four capture input pins. Whenever any of capture input signal changes,
the counter value is loaded into the corresponding capture register.
In the embedded ARM board, two capture pins are connected to push buttons.
We can enable capture mode and press the push buttons. Next read the
capture registers to find the captured counter value.
Pulse Width Modulating (PWM) Timer
PWM timer controls the PWM output pin. In our board, PWM output pin is
connected to the LED. PWM pin output can be controlled with two match
registers. These are match register 0 and match register 1. PWM counter
keeps counting with the prescaled clock input. We can control the frequency
of this clock input, just by writing into prescale register. Remember that
before prescaling, the clock is at 30MHz. If we write 30,000,000 into
prescale register, the input clock to counter will be 1Hz. If we write
3,000,000 to prescalre, input clock is at 10Hz.
When counter starts at zero count, the PWM output pin will become high.
Whenever count in counter matches value in match register 1, the PWM output
pin become low. When counter matches match register 0, the counter will get
reset to zero, and PWM output becomes high again. This cycle continues
Note that the value in match register 0, decides the period of PWM cycle.
Match register 1, gives the width of the high pulse. So match regiser 1,
should have a less value than match register 0.
Let us write prescale register with 30,000,000. So counter increments once
in every second. Now write 10 to match register 0 and 2 to match register 1.
Next start the PWM timer. We will observe that LED is glowing for 2 seconds
and off for 8 seconds. We can keep changing the match register 1 value and
observe the effect of LED.
Just to see the effect of PWM clearly, we set PWM cycle as 10 seconds.
But real purpose of PWM is to control the intensity of LED. So set the
counter input clock period to 1 milli second. Let us keep match registers 0
and 1 values to 10 and 2 only. Now LED glows for 2 milli seconds and goes
off for 8 milli seconds. This way LED glows total 100 times in one second.
Because of this 100 times, our eye will not know that LED is really going
on and off. Instead we only see its intensity is changing. So it is a nice
way to see PWM working.
UART (serial communication) peripheral
ARM embedded development board has got two UARTS. Connect both the UART
connectors using a cross RS-232 cable. First read the line status register of
UART-2. The RXRDY bit in that register should be zero. Now write a byte into
the TX buffer register of UART-1. Next read the status register of UART-2.
Now in this status register RXRDY bit should be one. This indicates that
UART-2 has received the byte from UART-1 over RS-232 cable. Now read the
RX buffer register of UART-2. It should have the same byte, that is written
to UART-1 TX buffer. Next read the status register of UART-2. The RXRDY bit
in UART-2 should become zero. As we read the data received, RXRDY bit becomes
zero. Similarly we can try communication from UART-2 to the UART-1 also.
Analog to Digital Converter (ADC)
ADC analog input pin is connected to a small potentiometer. By rotating the
potentiometer, we can vary the voltage at anlog input pin from 0 volts to
3.3 volts. Keep the potentiometer approximately in center position.
Start the analog to digital conversion process, by writting to ADC control
register. Next read the ADC data register to find the digital value of
voltage at the anlog input pin. Change the potentiometer position this and
that way. And each time read the digital values of voltage.
Digital to Analog Converter (DAC)
To test the DAC, connect the anaglog output pin of DAC to analog input pin
of ADC pin. Now write the digital value into DAC register. DAC will convert
this digital value to equivalent analog output. This analog output voltage
will appear on the analog output pin of DAC. If you have multi-meter, you
can measure the voltage the analog output pin.
Next start the ADC conversion and read the digital value of voltage.
This digital value should be approximately equal to the digital value we
have written to the DAC register. Note that DAC has converted the digital
value to analog voltage. ADC is converting this analog voltage back to digital
value. So both digital values should be nearly equal.
2. Loading and executing programs on the board
The monitor program allows us to download programs from PC to the board's
RAM very easily. We can also directly enter opcodes of program in to the RAM.
Ofcourse it is very tedious and error prone to enter program opcodes. Instead
X-modem serial protocol is used to load program file into RAM.
After loading the program to RAM, we can run the program by giving a
3. Writing and testing driver functions
Once we are familiar with the peripheral chips, next thing we can do is
developing the driver functions for the peripheral chips. For example we
can develop following driver functions for the RTC peripheral chip.
rtcGetTime(struct Time *t)
rtcGetDate(struct Date *d)
rtcGetDateTime(struct Date *d, struct Time *t)
rtcSetDateTime(struct Date *d, struct Time *t)
Note that driver functions hide hardware details. So who ever using these
driver functions need not know any thing about the RTC chip. By simply
calling these functions, they can get and set the date and time.
4. Writing application programs by using the above driver functions
Once driver functions are developed for all the peripheral chips, applications
can be easily developed by using these driver functions.
5. Writing and testing RTOS applications
It is also possible to develop and run Real-Time OS applications on this