Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 89 additions & 25 deletions academy/gpio/gpio_toggle/firmware/main.asm
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
; SPDX-License-Identifier: BSD-3-Clause
; Copyright (C) 2024-2026 Texas Instruments Incorporated - http://www.ti.com/

;***************************************************************************************
;*****************************************************************************
; File: main.asm
;
; Brief: Toggle PRU GPO output signal
;
; Steps to build :
; What this example teaches:
; - How a PRU core drives a pin: write a value to register R30.
; Each bit of R30 is wired to one PRU GPO (General Purpose Output)
; pin of that core: R30 bit 4 -> GPO4, R30 bit 0 -> GPO0, and so on.
; - How to create a delay by counting down in a loop. Most PRU
; instructions take 1 cycle, so the delay is easy to calculate.
; See the PEAK cycles comment above main_loop.
;
; - Using ccs:
; - Import pru project to ccs workspace
; - main.asm file gets copied to ccs workspace
; - Modify main.asm file
; - Build the pru project, after which .out (Executable output file) and .h (Firmware header) files gets generated
; - Either .out (Executable output file) can be loaded to PRU using ccs or R5F can write to PRU IRAM using PRUICSS driver
; - Using makefile:
; - Use command gmake -all to build PRU project
; Who runs this code:
Comment thread
y-motwani marked this conversation as resolved.
Resolved
; A host core (for example an MCU+ core, or Linux on an
; application core) copies this firmware into the PRU instruction
; memory and starts the PRU. The PRU does not start by itself. The
; same main.asm is built for PRU0 and for PRU1. Each core toggles
; its own GPO4.
;
;***************************************************************************************
; The pin that GPO4 reaches on the board depends on the device. See the
; readme for the pin location on each board, and for debug steps.
;*****************************************************************************

; CCS/makefile specific settings
.retain ; Required for building .out with assembly file
Expand All @@ -26,10 +32,12 @@
.global main
.sect ".text"

; definitions
.asg R2, TEMP_REG
.asg R3.w0, OFFSET_ADDR

; definitions
; Number of loop iterations per delay. Each iteration costs 2 cycles
; (sub + qbne), so one delay lasts 2 x DELAY_COUNT cycles. Increase this
; value to slow the toggle down (easier to see on a slow logic analyzer),
; decrease it to speed the toggle up. See the PEAK cycles comment above
; main_loop for the resulting timing.
.asg 100000 ,DELAY_COUNT ; Delay counter value


Expand All @@ -43,36 +51,92 @@ init:

;----------------------------------------------------------------------------
; Clear the register space
; Before begining with the application, make sure all the registers are set
; to 0. PRU has 32 - 4 byte registers: R0 to R31, with R30 and R31 being special
; registers for output and input respectively.
; Before beginning with the application, make sure all the registers are
; set to 0. PRU has 32 - 4 byte registers: R0 to R31, with R30 and R31
; being special registers for output and input respectively.
;
; R0-R29 are general purpose and are cleared by the zero instruction
; below. R31 reads the input pins, so it cannot be cleared. R30 drives
; the output pins and is cleared separately, right after.
;----------------------------------------------------------------------------

; Give the starting address and number of bytes to clear.
zero &r0, 120
; &r0 = start at R0, 120 bytes = 30 registers x 4 bytes = R0 to R29.
zero &r0, 120

; Starting with every output low. Without this the
; level of the first half period is unknown.
ldi r30, 0

; GPIO configuration note:
; After reset the PRU GPIO config register (GPCFG0 for PRU0, GPCFG1 for
; PRU1) defaults to "direct" GPO mode. In direct mode, the value written to R30
; appears on the pins right away, so no configuration is needed here.

;----------------------------------------------------------------------------
; PEAK cycles per pass of main_loop: 4 x DELAY_COUNT + 7
Comment thread
y-motwani marked this conversation as resolved.
Resolved
;
; ldi32 r5 + ldi32 r6 ............ 2 + 2 = 4
; delay_loop1: sub + qbne ........ 2 x COUNT = 2 x DELAY_COUNT
; ldi r30.b0, 0x10 ............... 1
; delay_loop2: sub + qbne ........ 2 x COUNT = 2 x DELAY_COUNT
; ldi r30.b0, 0x00 ............... 1
; qba main_loop .................. 1
;
; DELAY_COUNT = 100000 gives 400007 cycles. At a 200 MHz PRU clock
; (5 ns per cycle) that is about 2.0 ms, so the pin is a ~500 Hz
; square wave with ~50% duty cycle (about 1 ms high, 1 ms low).
;
; Hand-counted from the instruction listing (dispru). The one-time
; "zero" and "ldi r30, 0" in init are not counted. The loop counts are
; constants, so this is also the worst case. Update this total if
; DELAY_COUNT or the loop body changes.
;----------------------------------------------------------------------------
main_loop:
; Generate a delay
; Load the two delay counters. ldi32 loads a 32-bit constant and is
; assembled as two ldi instructions, so each costs 2 cycles.
ldi32 r5, DELAY_COUNT
ldi32 r6, DELAY_COUNT

; Delay #1: count r5 down to 0. The pin is LOW during this delay
; (R30 was cleared in init, and at the end of the previous pass).
delay_loop1:
sub r5, r5, 1
qbne delay_loop1, r5, 0

; Toggle GPIO pin 4 by writing 1 into bit 4 of R30
; Three ways to change an output pin. This example uses the first.
; 1. Overwrite: ldi r30.b0, 0x10
; 0x10 = binary 0001 0000 = bit 4 set. ".b0" writes the lowest
; byte (bits 7:0) of R30. ALL 8 bits are overwritten: bit 4 is
; set and bits 7:5 and 3:0 are cleared. Bits 31:8 are not
; touched. That is fine here, because only GPO4 is used. Use
; "r30" instead of "r30.b0" to overwrite all 32 bits.
; 2. Set or clear one bit: set r30, r30, 4 (pin high)
; clr r30, r30, 4 (pin low)
; Only bit 4 changes. Every other bit of R30 keeps its value.
; 3. Toggle (XOR): xor r30, r30, 0x10
; Flips bit 4, whatever its current level is, and leaves every
; other bit alone. The immediate can only be 0 to 255 (bits
; 7:0). To flip a higher bit, put the mask in a register:
; xor r30, r30, r1. The C version (gpio_toggle_c) uses this
; method.
;
; Toggle GPIO pin 4 by writing 1 into bit 4 of R30.
; The pin goes HIGH as soon as this instruction executes.
Comment thread
y-motwani marked this conversation as resolved.
Resolved
ldi r30.b0, 0x10

; Delay #2: the pin stays HIGH for this long.
delay_loop2:
sub r6, r6, 1
qbne delay_loop2, r6, 0

; Toggle GPIO pin 4 by writing 0 into bit 4 of R30
; Toggle GPIO pin 4 by writing 0 into bit 4 of R30.
; This overwrites all of bits 7:0 with 0. The pin goes LOW again.
ldi r30.b0, 0x00

; Continue loop
; qba = "branch always". Jump back and repeat forever.
qba main_loop

; The GPIO toggled signal pin is device-specific
; see readme for details

; halt the program
halt ; end of program (never reached)
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ i2c1.I2C_child.$name = "drivers_i2c_v0_i2c_v0_template0";

pruicss1.$name = "CONFIG_PRU_ICSS0";
pruicss1.AdditionalICSSSettings[0].$name = "CONFIG_PRU_ICSS_IO0";
pruicss1.AdditionalICSSSettings[0].PruGPIO.create(1);
pruicss1.AdditionalICSSSettings[0].PruGPIO.create(2);
pruicss1.AdditionalICSSSettings[0].PruGPIO[0].$name = "CONFIG_PRU_ICSS_GPIO0";
pruicss1.AdditionalICSSSettings[0].PruGPIO[0].PRU_ICSSG0_PRU.$assign = "PRU_ICSSG0_PRU0";
pruicss1.AdditionalICSSSettings[0].PruGPIO[0].PRU_ICSSG0_PRU.GPO4.$used = true;
pruicss1.AdditionalICSSSettings[0].PruGPIO[1].$name = "CONFIG_PRU_ICSS_GPIO1";
pruicss1.AdditionalICSSSettings[0].PruGPIO[1].PRU_ICSSG0_PRU.$assign = "PRU_ICSSG0_PRU1";
pruicss1.AdditionalICSSSettings[0].PruGPIO[1].PRU_ICSSG0_PRU.GPO4.$used = true;

debug_log.enableUartLog = true;
debug_log.uartLog.$name = "CONFIG_UART_CONSOLE";
Expand Down Expand Up @@ -268,5 +271,6 @@ i2c1.I2C.$suggestSolution
i2c1.I2C.SCL.$suggestSolution = "I2C1_SCL";
i2c1.I2C.SDA.$suggestSolution = "I2C1_SDA";
pruicss1.AdditionalICSSSettings[0].PruGPIO[0].PRU_ICSSG0_PRU.GPO4.$suggestSolution = "PRG0_PRU0_GPO4";
pruicss1.AdditionalICSSSettings[0].PruGPIO[1].PRU_ICSSG0_PRU.GPO4.$suggestSolution = "PRG0_PRU1_GPO4";
debug_log.uartLog.UART.RXD.$suggestSolution = "UART0_RXD";
debug_log.uartLog.UART.TXD.$suggestSolution = "UART0_TXD";
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ i2c1.I2C_child.$name = "drivers_i2c_v0_i2c_v0_template0";

pruicss1.$name = "CONFIG_PRU_ICSS0";
pruicss1.AdditionalICSSSettings[0].$name = "CONFIG_PRU_ICSS_IO0";
pruicss1.AdditionalICSSSettings[0].PruGPIO.create(1);
pruicss1.AdditionalICSSSettings[0].PruGPIO.create(2);
pruicss1.AdditionalICSSSettings[0].PruGPIO[0].$name = "CONFIG_PRU_ICSS_GPIO0";
pruicss1.AdditionalICSSSettings[0].PruGPIO[0].PRU_ICSSG0_PRU.$assign = "PRU_ICSSG0_PRU0";
pruicss1.AdditionalICSSSettings[0].PruGPIO[0].PRU_ICSSG0_PRU.GPO4.$used = true;
pruicss1.AdditionalICSSSettings[0].PruGPIO[1].$name = "CONFIG_PRU_ICSS_GPIO1";
pruicss1.AdditionalICSSSettings[0].PruGPIO[1].PRU_ICSSG0_PRU.$assign = "PRU_ICSSG0_PRU1";
pruicss1.AdditionalICSSSettings[0].PruGPIO[1].PRU_ICSSG0_PRU.GPO4.$used = true;

debug_log.enableUartLog = true;
debug_log.uartLog.$name = "CONFIG_UART_CONSOLE";
Expand Down Expand Up @@ -266,7 +270,7 @@ section11.output_section[0].alignment = 0;
i2c1.I2C.$suggestSolution = "I2C1";
i2c1.I2C.SCL.$suggestSolution = "I2C1_SCL";
i2c1.I2C.SDA.$suggestSolution = "I2C1_SDA";
pruicss1.AdditionalICSSSettings[0].PruGPIO[0].PRU_ICSSG0_PRU.$suggestSolution = "PRU_ICSSG0_PRU0";
pruicss1.AdditionalICSSSettings[0].PruGPIO[0].PRU_ICSSG0_PRU.GPO4.$suggestSolution = "PRG0_PRU0_GPO4";
pruicss1.AdditionalICSSSettings[0].PruGPIO[1].PRU_ICSSG0_PRU.GPO4.$suggestSolution = "PRG0_PRU1_GPO4";
debug_log.uartLog.UART.RXD.$suggestSolution = "UART0_RXD";
debug_log.uartLog.UART.TXD.$suggestSolution = "UART0_TXD";
64 changes: 54 additions & 10 deletions academy/gpio/gpio_toggle/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,39 @@ The PRU firmware is written in assembly (`firmware/main.asm`). The same example
The implementation varies slightly depending on the target device:
- For AM261x: The example toggles PRU1 GPO4
- For AM263x and AM263Px: The example toggles PRU0 GPO4
- For AM64x and AM243x: The example toggles PRU0 GPO4
- For AM64x and AM243x: The example toggles PRU0 GPO4 and PRU1 GPO4

For AM261-LP, the toggled signal is routed to a readily accessible location on the development boards - Pin 5 of the Boosterpack header J1/J3 on the launchpad and for AM261-SOM EVM, to the Pin 93 of J3 header (in J3 Row B). For AM263x and AM263Px, the toggled signal is routed to Pin 17 of the Boosterpack header J2 on the respective LaunchPads. This makes it convenient for users to observe and measure the output using standard measurement equipment like oscilloscopes or logic analyzers.

For AM64x and AM243x, the toggled signal is routed to the J2 header, Pin A13 (in J2 Row A). To verify the GPO pin toggle, the user will require a J2 High Speed Expansion Connector to get the required pin outs and measure the output using standard measurement equipment like oscilloscopes or logic analyzers. If using the TMDS64DC01 EVM as the J2 Expansion header for the AM64x EVM, it corresponds to the Pin 9 in the J2 header.
For AM64x and AM243x, the toggled signals are routed to the J2 header of the EVM: PRU0 GPO4 on Pin A13 (J2 Row A) and PRU1 GPO4 on Pin B13 (J2 Row B). To verify the GPO pin toggle, the user will require a J2 High Speed Expansion Connector to get the required pin outs and measure the output using standard measurement equipment like oscilloscopes or logic analyzers. If using the TMDS64DC01 EVM as the J2 Expansion header for the AM64x/AM243x EVM, PRU0 GPO4 corresponds to Pin 9 in the J2 header and PRU1 GPO4 corresponds to Pin 13 in the J4 header.

Cores Tested for AM64x and AM243x: PRU0 and PRU1.
The default pinmux uses PRU0 GPO4. If the pinmux is modified to use PRU1 GPO4 on AM64x and AM243x, probe Pin 13 of the J4 header on the high speed expansion board instead of Pin 9 of the J2 header.
Cores Tested for AM64x and AM243x: PRU0 and PRU1.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these lines actually needed? Seem to just be repeating info from before

The default pinmux enables GPO4 on both PRU cores for AM64x and AM243x, and the R5F loads the same firmware on PRU0 and PRU1, so both toggle independently:
- PRU0 GPO4: Pin A13 of the EVM J2 header. On the TMDS64DC01 high speed expansion board, this is Pin 9 of J2.
- PRU1 GPO4: Pin B13 of the EVM J2 header. On the TMDS64DC01 high speed expansion board, this is Pin 13 of J4.

The oscillating signal generated can be used to validate proper PRU operation and PRU GPO pinmux configuration, making this example particularly useful for initial hardware bring-up and verification of PRU GPO functionality.

## Supported Combinations
## Expected output

Refer to open-pru/academy/readme.md > Supported processors per-project
for the list of processors that support building this project, and information
about porting this project to other processors.
With the default `DELAY_COUNT = 100000` and a 200 MHz PRU clock, the signal
is a 500 Hz square wave with a duty cycle of about 50%. The cycle count
calculation is in the comment above `main_loop` in `firmware/main.asm`.

This is a calculated value. It assumes a 200 MHz PRU clock. If your clock is
different, the frequency changes in proportion. If your scope shows a
different frequency, check the clock first.

The PRU core clock depends on the device and on how the host software
configures it. Different devices support different PRU core frequencies.
The [PRU Subsystem Features Comparison](https://www.ti.com/lit/sprac90)
app note lists the maximum frequency per device. Check the device datasheet
and TRM, and the clock settings for the frequencies you can use.

For how many cycles each PRU instruction takes, and how to measure the
cycle count on hardware or in the simulator, see the
[PRU timing reference](https://github.com/TexasInstruments/pru-simulator/blob/main/skills/pru-assembly/references/timing.md)
in the pru-simulator repository.

## Validated HW & SW

Expand All @@ -47,5 +64,32 @@ This project was tested on hardware with these software versions:
1. Build and run the PRU firmware
2. Connect a logic analyzer or oscilloscope to header pin below to observe the toggling signal
- BP.17 (J2) for AM263x/AM263Px
- To probe, A13 (J2 Row A) on J2 Header, probe Pin 9 in J2 on TMDS64DC01 EVM (Expansion board) which is connected to the J2 header for AM64x and AM243x

- BP.5 (J1/J3) for AM261-LP
- Pin 93 (J3 row B) for AM261-SOM
- To probe PRU0 GPO4, A13 (J2 Row A) on J2 Header, probe Pin 9 in J2 on TMDS64DC01 EVM (Expansion board) which is connected to the J2 header for AM64x and AM243x
- To probe PRU1 GPO4, B13 (J2 Row B) on J2 Header, probe Pin 13 in J4 on TMDS64DC01 EVM (Expansion board) which is connected to the J2 header for AM64x and AM243x

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't re-list the EVM pin info if it's already handled above


## Debug steps

If the pin does not toggle, work through steps 1-4. Steps 5 and 6 are
firmware edits to try once the basic example works.

1. **Is the R5F application running?** Open the UART console. The host

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think some of these steps are are generic debug which should probably live somewhere other than within a specific example. Thoughts on in the PRU Getting Started Labs > Lab 5: How to debug PRU firmware? Or somewhere else? https://dev.ti.com/tirex/explore/node?isTheia=false&node=A__ASFrNyRlBnGGirCwEz1a1Q__AM64-ACADEMY__WI1KRXP__LATEST

application asserts on most initialization failures (`Board_driversOpen()`, I/O expander, `PRUICSS_initMemory()`, `PRUICSS_loadFirmware()`). If it halts with an assert, the PRU is never
started. `Drivers_open()` and `PRUICSS_open()` return values are not
checked in this example.
2. **Is the PRU running?** In CCS, connect to the PRU core (for example

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a good test for if the PRU core is running, since you could randomly attach when the R30 happens to be at 0x10 several times in a row. Better options include the PRU cycle counter, watching the PC counter increment, etc.

PRU_ICSSG0_PRU0) and halt it. R30 should be `0x10` or `0x00`, and the
program counter should be inside `main_loop`. Resume and halt a few
times. If R30 never changes, the firmware is not running.
3. **Is the pin routed to the header?** On AM64x, AM243x and AM263Px the
I/O expander must be set (done in `gpio_toggle.c`). Also check that GPO4
is enabled for the right PRU core in `example.syscfg`.
4. **Are you probing the right header pin?** PRU0 GPO4 and PRU1 GPO4 are
different pins. See the Overview for which header pin belongs to which core.
5. **Slow it down.** Set `DELAY_COUNT` in `main.asm` to `50000000`, which
gives a half period of about 0.5 s at 200 MHz. You can now see the
signal change on a multimeter or on an LED.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the multimeter suggestion

6. **Toggle all pins at once.** In `main.asm` change the R30 writes to
`ldi32 r30, 0xFFFFFFFF` and `ldi32 r30, 0x00000000`. Pins only show a
signal if they are pinmuxed as PRU GPOs in `example.syscfg`.
Loading
Loading