Learn ARM Programming with Interactive Examples
Table of Contents
- Introduction
- Arm Microcontroller Architecture
- Peripherals of Arm Microcontroller
- GPIO
- I2C
- SPI
- Enabling Clock Mechanisms
- Random Number Generator in Arm Microcontroller
- Configuration Registers
- Data Sheet Overview
- Control Register
- Status Register
- Data Register
- Summary of Registers
- Programming the Random Number Generator
- Code Explanation
- Uploading and Running the Code
- Conclusion
Programming the Internal Peripherals of an Arm Microcontroller
Programming internal peripherals of an Arm microcontroller can be a complex task due to the chip's sophisticated design and architecture. Arm, as a company, does not manufacture microcontrollers itself. Instead, it licenses its designs to different vendors, such as Texas Instruments and Freescale. The popularity of Arm microcontrollers stems from their low power consumption and high-speed capabilities, making them a common choice for cell phones and handheld devices worldwide.
When you initially power up an Arm microcontroller, all the peripherals are disabled, and the clocking mechanisms are shut down. To use specific functionalities like GPIO, I2C, or SPI, you need to enable the corresponding clocking mechanisms and devices. In this article, we will focus on one particular peripheral, the random number generator (RNG), and explore how to program it within an Arm microcontroller.
Random Number Generator in Arm Microcontroller
The RNG within an Arm microcontroller is responsible for generating 32-bit random numbers. It utilizes an analog generator, known as the ring oscillator, to produce these random numbers. The generator operates at regular intervals, typically every 40 periods of the PLL 48 clock. To access and control the RNG, we need to manipulate three configuration registers: Control Register, Status Register, and Data Register.
Control Register
The Control Register (RNG_CR) is the first register we need to configure to enable the random number generator. It has an address offset of 0x00, and we specifically set bit number 2 to enable the RNG.
Status Register
The Status Register (RNG_SR) provides information about the RNG's current state. It also contains error flags, but we are primarily interested in bit number 0, which indicates if the generated data is ready to be read. When bit number 0 is set, it means the Data Register holds a valid random number.
Data Register
The Data Register (RNG_DR) stores the actual 32-bit random number. It has an address offset of 0x08. To retrieve a random number, we read this register only when bit number 0 in the Status Register is set.
Taking a summarized view of the three registers, we can see the following:
- Control Register (RNG_CR)
- Status Register (RNG_SR)
- Data Register (RNG_DR)
To program the RNG, we need to ensure the RNG is enabled, monitor the data ready flag, and retrieve the random numbers from the Data Register.
Programming the Random Number Generator
To exercise the random number generator on an Arm microcontroller, we can write code that interacts with it in real-time through the serial port. The code provided below demonstrates how to enable the RNG and retrieve random numbers:
// Assigning addresses to the registers and clock enables
#define RNG_CR (*(volatile uint32_t*)0x40081000)
#define RNG_SR (*(volatile uint32_t*)0x40081004)
#define RNG_DR (*(volatile uint32_t*)0x40081008)
...
// Enable the RNG and clock enables
#define RNG_ENABLE (RNG_CR |= 1 << 2)
#define CLOCK_ENABLE (/* Enable necessary clocks */)
...
void printRegistersBinary(){
// Print the three registers' values in binary
}
#define RNG (/* Retrieve the random number from RNG_DR */)
...
The code starts by assigning the corresponding addresses to the Control Register (RNG_CR), Status Register (RNG_SR), and Data Register (RNG_DR). Additionally, clock enables for the RNG and PLL need to be enabled.
Several macros are defined to simplify code execution. For example, RNG_ENABLE
sets bit number 2 in the Control Register to enable the random number generator. Invoking the RNG
macro retrieves a random number from the Data Register.
To test the program, upload the code to the Arm microcontroller and execute the following commands through the serial port:
init
- Enables the RNG and necessary clocks.
RNG
- Displays a single random number.
RNG
(multiple times) - Shows eight random numbers.
RNG many
- Prints a large number of random numbers.
By observing the registers' values, you can verify that the RNG is functioning correctly.
In conclusion, programming the internal peripherals of an Arm microcontroller, such as the random number generator, requires enabling clock mechanisms, configuring specific registers, and controlling data retrieval. By following the steps outlined in this article, you can successfully program the RNG and utilize its random number generation capabilities in your Arm microcontroller projects.