1 of 41

THE

PERIPHERALS

USB | UART | I2C | SPI | PWM | Timer | Watchdog | The Realtime Clock (RTC) | ADC | SSI

A

2 of 41

THE PROGRAMMABLE INPUT/OUTPUT

USB

Compare the registers used in USB

Ap

UART

How to transfer data using UART?

An

I2C

How to perform transformation using I2C?

An

SPI

Analyze the operation of SPI

An

PWM

Experiment with PWM in RP2040

An

Timer

Demonstrate timer and related operations using RP2040

Ap

Watchdog

Justify the sequence of operations in functioning a watchdog timer

Ap

The Realtime Clock (RTC)

Outline RTC

An

ADC SSI

Summarize ADC and SSI features in RP2040

Ap

3 of 41

USB

Compare the registers used in USB

ILO

4 of 41

USB

Compare the registers used in USB

ILO

Device Mode: This means the microcontroller can act like a USB device when you connect it to your computer. It can do this at "Full Speed, which is a data transfer rate of 12 megabits per second (Mbps).

Host Mode: In this mode, the RP2040 can communicate with other USB devices, both those that work at Full Speed (12Mbps) and those that work at Low Speed (1.5Mbps). It can also talk to multiple devices through a USB hub.

For the RP2040 to do all of this, there's a part of it called the USB controller. The USB controller also has a place to store data called DPSRAM (4kB)

5 of 41

USB

Compare the registers used in USB

ILO

6 of 41

USB

Compare the registers used in USB

ILO

Physical layer connection

Detects various line states defined by the USB specification (e.g., Bus Reset, Connected, Suspend, Resume, etc.)

Decodes received data captured by the line state detection module

Operates similarly to the receive engine but handles data transmission

Provides 4KB (4096 bytes) of memory used to store control registers and data buffers

Manages operations when the RP2040 acts as a USB device.

Operates similarly to the device controller but focuses on managing transactions initiated by the host.

7 of 41

I2C

How to perform transformation

using I2C?

ILO

https://www.geeksforgeeks.org/i2c-communication-protocol/

8 of 41

I2C

How to perform transformation

using I2C?

ILO

I2C is a commonly used 2-wire interface that can be used to connect devices for low speed data transfer using clock SCL and data SDA wires.

I2C Protocols

START and STOP Conditions

I2C Terminology

  • Transmitter
  • Receiver
  • Master
  • Slave
  • Multi-master
  • Arbitration
  • Synchronization

9 of 41

I2C

How to perform transformation

using I2C?

ILO

Addressing Slave Protocol

7-bit Address Format

10-bit Address Format

Pg.445 in datasheet

10 of 41

I2C

How to perform transformation

using I2C?

ILO

Transmitting and Receiving Protocol

Master-Transmitter and Slave-Receiver

Master-Receiver and Slave-Transmitter

11 of 41

SPI

Analyze the operation

of SPI

ILO

https://www.techtarget.com/whatis/definition/serial-peripheral-interface-SPI

12 of 41

PWM

Experiment with PWM in RP2040

ILO

Pulse Width Modulation (PWM) is a technique used to control the amount of power delivered to an electrical device by rapidly turning it on and off. This is done by varying the width of the on-time while keeping the total time period constant. In simple terms, PWM is like flicking a light switch on and off very quickly to control the brightness of a light bulb.

13 of 41

PWM

In RP2040

Experiment with PWM in RP2040

ILO

Pulse width modulation (PWM) is a scheme where a digital signal provides a smoothly varying average voltage. This is achieved with positive pulses of some controlled width, at regular intervals. The fraction of time spent high is known as the duty cycle.

14 of 41

PWM

In RP2040

Experiment with PWM in RP2040

ILO

The RP2040 PWM block has 8 identical slices. Each slice can drive two PWM output signals, or measure the frequency

or duty cycle of an input signal. This gives a total of up to 16 controllable PWM outputs. All 30 GPIO pins can be driven

by the PWM block.

15 of 41

16 of 41

Features of PWM in RP2040

• 16-bit counter

• 8.4 fractional clock divider

• Two independent output channels, duty cycle from 0% to 100% inclusive

• Dual slope and trailing edge modulation

• Edge-sensitive input mode for frequency measurement

• Level-sensitive input mode for duty cycle measurement

• Configurable counter wrap value

• Interrupt request and DMA request on counter wrap

• Phase can be precisely advanced or retarded while running (increments of one count

17 of 41

• The 16 PWM channels (8 2-channel slices) appear on GPIO0 to GPIO15, in the order PWM0 A, PWM0 B, PWM1 A…

• This repeats for GPIO16 to GPIO29. GPIO16 is PWM0 A, GPIO17 is PWM0 B, so on up to PWM6 B on GPIO29

• The same PWM output can be selected on two GPIO pins; the same signal will appear on each GPIO.

• If a PWM B pin is used as an input, and is selected on multiple GPIO pins, then the PWM slice will see the logical OR of those two GPIO inputs

18 of 41

Pulse Width Modulation

The counting period is controlled by the TOP (is the wrapping value) register, with a maximum possible period of 65536 cycles, as the counter and TOP are 16 bits in size. The input values are configured via the CC register.

19 of 41

Pulse Width Modulation

20 of 41

21 of 41

22 of 41

Configuring PWM Period

When free-running, the period of a PWM slice’s output (measured in system clock cycles) is controlled by three parameters:

• The TOP register

• Whether phase-correct mode is enabled (CSR_PH_CORRECT)

• The DIV register

Unless told otherwise,

the DIV_FRAC is 15

23 of 41

• The TOP register

• Whether phase-correct mode is enabled (CSR_PH_CORRECT)

• The DIV register

24 of 41

• The TOP register

• Whether phase-correct mode is enabled (CSR_PH_CORRECT)

• The DIV register

25 of 41

• The TOP register

• Whether phase-correct mode is enabled (CSR_PH_CORRECT)

• The DIV register

26 of 41

• The TOP register – is the wrapping value

• Whether phase-correct mode is enabled (CSR_PH_CORRECT)

• The DIV register

  1. TOP register – 1100 1100 0010 0100

  • Phase correct mode – enabled

III. DIV register - xxxx xxxx xxxx xxxx xxxx 0000 0000 0010 - 2

1100 1100 0010 0100 – decimal –

1 + 0 + 0 + 8 + 0 + 0 + 64 + 128 + 256 + 0 + 0 + 0 + 4096 = 4653

ƒ PWM = 52KHz

27 of 41

Timer

Demonstrate timer and related operations using RP2040

ILO

In the RP2040 microcontroller, a timer is a built-in feature that helps keep track of time or measure time intervals. It works like a digital stopwatch, and it's handy for various tasks.

Think of it as a small, electronic clock inside the microcontroller.

  1. Timekeeper: A timer in RP2040 is like a digital timekeeper. It counts seconds, milliseconds, or even smaller units of time, depending on how it's configured.
  2. Precise Timing: It's really good at keeping time accurately. You can use it to measure how long things take to happen, like how fast a motor spins or how long a button is pressed.
  3. Control: You can tell the timer to start counting when you want and stop when you want. This gives you control over when and how you measure time.
  4. Useful for Many Things: Timers are used in all sorts of projects. They're great for tasks like flashing LEDs at specific intervals, controlling motors, or even making simple games.

28 of 41

Timer - features

Demonstrate timer and related operations using RP2040

ILO

• A single 64-bit counter, incrementing once per microsecond

• This counter can be read from a pair of latching registers, for race-free reads over a 32-bit bus.

• Four alarms: match on the lower 32 bits of counter, IRQ on match.

29 of 41

In the RP2040 microcontroller, the timer is a 64-bit counter, but the data bus is only 32 bits wide.

To work with time values, there are pairs of registers:

- TIMEHW and TIMELW are used for writing time. (32 bit)

- TIMEHR and TIMELR are used for reading time. (32 bit)

  • When reading time, accessing the TIMELW register latches the value in the TIMEHR register, ensuring accurate time reading.
  • Alternatively, you can use TIMERAWH and TIMERAWL to read the raw time without latching.

30 of 41

  1. Enable the interrupt at the timer by setting the appropriate alarm bit in INTE (e.g., (1 << 0) for ALARM0).
  2. Enable the corresponding timer interrupt at the processor.
  3. Write the desired alarm time in microseconds to ALARM0, setting the ARMED bit as a side effect.

Once the alarm fires, the ARMED bit will be cleared. To clear the latched interrupt, write a 1 to the appropriate bit in INTR. These timers are designed for short sleeps; for longer alarms, refer to another section.

The timer has four alarms, each triggering a separate interrupt.

These alarms match on the lower 32 bits of the 64-bit counter, allowing them to be fired up to approximately 4295 seconds (or around 72 minutes) into the future.

To enable an alarm:

31 of 41

32 of 41

Memorize this registers address and names

33 of 41

34 of 41

I. In the address :0x40054038, Value : 0x00000003

II. In the address : 0x40054004, Value : 0x00215E80

III. In the address: 0x40054018, Value is constantly checked

35 of 41

I. In the address :0x40054038, Value : 0x00000003

II. In the address : 0x40054004, Value : 0x00215E80

III. In the address: 0x40054018, Value is constantly checked

0x0: 0 * 16^0 = 0 * 1 = 0

0x8: 8 * 16^1 = 8 * 16 = 128

0xE: 14 * 16^2 = 14 * 256 = 3584

0x5: 5 * 16^3 = 5 * 4096 = 20480

0x1: 1 * 16^4 = 1 * 65536 = 65536

0x2: 2 * 16^5 = 2 * 1048576 = 2097152

0x00215E80

2172868 Microseconds

2172868 Microseconds / 1000 = 2172.868 seconds

2172.868/60 = 36.21 Minutes

36 of 41

Watchdog

Demonstrate timer and related operations using RP2040

ILO

In simple terms, the watchdog in the RP2040 microcontroller is like a timer that counts down, and if it ever reaches zero, it can restart parts of the chip. Its primary purpose is to prevent software issues, like getting stuck in a loop, from causing the chip to stop working properly.

Here's what you need to know:

1. Countdown Timer: The watchdog is a timer that continuously counts down.

2. Preventing Stuck Software: To prevent issues where software gets stuck, a programmer must regularly "pet" the watchdog by writing a value to it. This action resets the countdown timer.

37 of 41

Watchdog

Demonstrate timer and related operations using RP2040

ILO

3. Watchdog Reset: The watchdog timer is also connected to the power-on state machine and reset controller. When it's reset, it can affect other parts of the chip, depending on how it's configured.

4. Tick Generation: The watchdog uses a reference clock (clk_tick) that's ideally derived from a crystal oscillator for accuracy. This reference clock is divided internally to create a tick (approximately 1 microsecond) used as the watchdog's time unit.

5. Watchdog Counter: The watchdog's countdown value is loaded from the LOAD register. You can check the current value in the CTRL.TIME register.

6. Scratch Registers: The watchdog includes eight 32-bit scratch registers that store information between resets. These registers can be used to carry data from one chip reset to another.

38 of 41

Watchdog

ILO

7. Bootrom Magic Number: The bootrom checks the watchdog's scratch registers for a "magic number" during boot. If it finds this magic number, it can initiate a soft reset and execute user-specified code.

In essence, the watchdog is a safety feature that ensures the microcontroller keeps running reliably by monitoring and preventing software-related issues. It's a fail-safe mechanism to maintain chip functionality.

39 of 41

Watchdog

Demonstrate timer and related operations using RP2040

ILO

40 of 41

RTC

ILO

The Real-time Clock (RTC) provides time in human-readable format and can be used to generate interrupts at specific times.

41 of 41

ADC

ILO

RP2040 has an internal analogue-digital converter (ADC)

SAR ADC

12 bit

1. Number of Channels: The RP2040 ADC module has 3 channels, labeled ADC0, ADC1, and ADC2. This means you can simultaneously measure up to three different analog signals.

2. Resolution: The ADC provides 12-bit resolution, which means it can represent analog voltages as digital values ranging from 0 to 4095.

3. Voltage Reference: The ADC can be configured to use an internal voltage reference (Vref) or an external reference voltage for conversions. The reference voltage is used as a basis for converting the analog signal to a digital value.

4. Conversion Modes: The RP2040 ADC supports both single-ended and differential conversion modes. In single-ended mode, each channel is measured with respect to the reference voltage. In differential mode, the voltage difference between two channels is measured.

5. Sampling Rate: The ADC can operate at different sampling rates, allowing you to adjust the speed of your analog-to-digital conversions based on your application's requirements.

6. Software Control: The ADC module can be configured and controlled through software using the RP2040 SDK or other programming environments. You can initiate conversions, read the results, and set various configuration options as needed.

7. Interrupts: The ADC can generate interrupts upon completing conversions, making it useful for applications that require real-time data acquisition or monitoring.