diff --git a/academy/gpio/gpio_toggle/firmware/main.asm b/academy/gpio/gpio_toggle/firmware/main.asm index 0602004c1..e45b9324f 100644 --- a/academy/gpio/gpio_toggle/firmware/main.asm +++ b/academy/gpio/gpio_toggle/firmware/main.asm @@ -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: +; 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 @@ -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 @@ -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 +; +; 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. 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) diff --git a/academy/gpio/gpio_toggle/mcuplus/am243x-evm/r5fss0-0_freertos/example.syscfg b/academy/gpio/gpio_toggle/mcuplus/am243x-evm/r5fss0-0_freertos/example.syscfg index 6a20f8222..a5ab28b18 100644 --- a/academy/gpio/gpio_toggle/mcuplus/am243x-evm/r5fss0-0_freertos/example.syscfg +++ b/academy/gpio/gpio_toggle/mcuplus/am243x-evm/r5fss0-0_freertos/example.syscfg @@ -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"; @@ -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"; diff --git a/academy/gpio/gpio_toggle/mcuplus/am64x-evm/r5fss0-0_freertos/example.syscfg b/academy/gpio/gpio_toggle/mcuplus/am64x-evm/r5fss0-0_freertos/example.syscfg index 851a1166a..f1e6a647d 100644 --- a/academy/gpio/gpio_toggle/mcuplus/am64x-evm/r5fss0-0_freertos/example.syscfg +++ b/academy/gpio/gpio_toggle/mcuplus/am64x-evm/r5fss0-0_freertos/example.syscfg @@ -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"; @@ -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"; diff --git a/academy/gpio/gpio_toggle/readme.md b/academy/gpio/gpio_toggle/readme.md index c3f5eede7..58649e36f 100644 --- a/academy/gpio/gpio_toggle/readme.md +++ b/academy/gpio/gpio_toggle/readme.md @@ -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. +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 @@ -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 + +## 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 + 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 + 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. +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`. diff --git a/academy/gpio/gpio_toggle_c/firmware/main.c b/academy/gpio/gpio_toggle_c/firmware/main.c index 0ac5b58d9..b9ca8c448 100644 --- a/academy/gpio/gpio_toggle_c/firmware/main.c +++ b/academy/gpio/gpio_toggle_c/firmware/main.c @@ -1,49 +1,114 @@ /* * SPDX-License-Identifier: BSD-3-Clause * Copyright (C) 2024-2026 Texas Instruments Incorporated - http://www.ti.com/ + * + * File: main.c + * + * Brief: Toggle PRU GPO output signal + * + * 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 reach the special registers R30 (outputs) and R31 (inputs) + * from C. + * - How to create a delay with the __delay_cycles() compiler intrinsic, + * which waits for a fixed number of PRU clock cycles. + * + * Who runs this code: + * 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.c 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. */ #include #include -/* Delay between toggles, in PRU clock cycles (not time). At the 200 MHz PRU - * clock (5 ns per cycle), 100,000,000 cycles is 0.5 s, so the GPO pin stays - * high for 0.5 s and low for 0.5 s (1 Hz square wave, ~50% duty cycle). */ +/* Delay between toggles, in PRU clock cycles (not time). See the PEAK + * cycles comment above main() for the resulting timing. */ #define TOGGLE_DELAY_CYCLES 100000000 +/* + * R30 and R31 are PRU hardware registers, not memory. "register" tells the + * compiler to use the real register, and "volatile" tells it that the value + * can change outside the program (R31) or has an effect outside the program + * (R30), so it must not optimize accesses away. + * __R30: write to drive the GPO pins + * __R31: read to see the GPI pins + */ volatile register uint32_t __R30; volatile register uint32_t __R31; /* - * main + * main + * + * Toggles a PRU GPO pin at a fixed interval. + * + * Parameters: none * - * Toggles a PRU GPO pin at a fixed interval. + * return: none (never returns) * - * Parameters: none + * Pseudocode: + * - Select GPO pin 4 as the output to toggle (output mask = bit 4) + * - Repeat forever: + * * Invert the selected output bit, flipping the pin level + * * Wait for the toggle delay before the next flip * - * return: none (never returns) + * PEAK cycles per loop iteration: TOGGLE_DELAY_CYCLES + a few cycles + * __delay_cycles(N) is built as a counted loop that takes exactly N + * cycles. For N = 100,000,000 the listing (dispru) shows 2 ldi + + * 49,999,999 passes x 2 cycles (sub, qbne) = 100,000,000. + * On top of that each iteration runs an lbbo (reads "gpio" back + * from the stack, because it is volatile), an xor and a jmp. The + * lbbo access time was not counted, so the overhead is "a few + * cycles", not an exact number, and negligible next to the delay. * - * Pseudocode: - * - Select GPO pin 4 as the output to toggle (output mask = bit 4) - * - Repeat forever: - * * Invert the selected output bit, flipping the pin level - * * Wait for the toggle delay before the next flip + * At a 200 MHz PRU clock (5 ns per cycle), 100,000,000 cycles is + * 0.5 s, so the GPO pin stays high for 0.5 s and low for 0.5 s + * (1 Hz square wave, ~50% duty cycle). + * + * The delay is a build-time constant, so this is also the worst + * case. */ int main(void) { - volatile uint32_t gpio; + /* Bit mask of the pins to toggle. Bit n set means GPOn is toggled. */ + volatile uint32_t gpio; + + /* The reset default for the PRU GPIO config registers (GPCFG0 for PRU0, + * GPCFG1 for PRU1) is GPI Mode 0, GPO Mode 0, which is direct mode. + * Writing R30 drives the PRU GPO signals immediately, so no PRU CFG + * register write is needed here. */ + + /* Toggle GPO4 pin. 0x10 is binary 0001 0000, so only bit 4 is set. */ + gpio = 0x10; - /* The reset default for the PRU GPIO config registers (GPCFG0 for PRU0, - * GPCFG1 for PRU1) is GPI Mode 0, GPO Mode 0, which is direct mode. - * Writing R30 drives the PRU GPO signals immediately, so no PRU CFG - * register write is needed here. */ + /* Starting with every output low. Without this the + * level of the first half period is unknown. */ + __R30 = 0; - /* Note: 0xFFFF_FFFF toggles all GPO pins */ - /* Toggle GPO4 pin */ - gpio = 0x10; + /* Three ways to change an output pin. This example uses the third. + * 1. Overwrite: __R30 = 0x10; + * Writes all 32 bits of R30: bit 4 is set and every other bit + * is cleared. Fine if only GPO4 is used. The assembly version + * (gpio_toggle) uses this method, on the lowest byte only. + * 2. Set or clear one bit: __R30 |= (1u << 4); (pin high) + * __R30 &= ~(1u << 4); (pin low) + * Only bit 4 changes. Every other bit keeps its value. + * 3. Toggle (XOR): __R30 ^= (1u << 4); + * Flips bit 4, whatever its current level is, and leaves every + * other bit alone. Because one XOR flips the pin on every pass, + * the loop needs no separate set and clear steps. */ - while (1) { - __R30 ^= gpio; // toggle GPO pin 4 - __delay_cycles(TOGGLE_DELAY_CYCLES); - } + /* INTENTIONALLY_UNBOUNDED_LOOP: runs forever, the PRU firmware has + * no exit path. */ + while (1) { + __R30 ^= gpio; + __delay_cycles(TOGGLE_DELAY_CYCLES); + } } diff --git a/academy/gpio/gpio_toggle_c/mcuplus/am243x-evm/r5fss0-0_freertos/example.syscfg b/academy/gpio/gpio_toggle_c/mcuplus/am243x-evm/r5fss0-0_freertos/example.syscfg index 6a20f8222..a5ab28b18 100644 --- a/academy/gpio/gpio_toggle_c/mcuplus/am243x-evm/r5fss0-0_freertos/example.syscfg +++ b/academy/gpio/gpio_toggle_c/mcuplus/am243x-evm/r5fss0-0_freertos/example.syscfg @@ -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"; @@ -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"; diff --git a/academy/gpio/gpio_toggle_c/mcuplus/am64x-evm/r5fss0-0_freertos/example.syscfg b/academy/gpio/gpio_toggle_c/mcuplus/am64x-evm/r5fss0-0_freertos/example.syscfg index 851a1166a..f1e6a647d 100644 --- a/academy/gpio/gpio_toggle_c/mcuplus/am64x-evm/r5fss0-0_freertos/example.syscfg +++ b/academy/gpio/gpio_toggle_c/mcuplus/am64x-evm/r5fss0-0_freertos/example.syscfg @@ -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"; @@ -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"; diff --git a/academy/gpio/gpio_toggle_c/readme.md b/academy/gpio/gpio_toggle_c/readme.md index 1f9355fd7..55e238325 100644 --- a/academy/gpio/gpio_toggle_c/readme.md +++ b/academy/gpio/gpio_toggle_c/readme.md @@ -11,21 +11,36 @@ This example demonstrates a PRU core driving PRU GPO signals directly by writing The PRU firmware is written in C (`firmware/main.c`). The same example written in assembly is in `gpio_toggle`. The R5F core (running FreeRTOS) loads the PRU firmware onto the PRU cores. On AM64x and AM243x, the R5F also configures the board's I/O expander over I2C so that the PRU signals reach the board pins. Without this step the pin stays silent. The implementation varies slightly depending on the target device: -- For AM64x and AM243x: The example toggles PRU0 GPO4 +- For AM64x and AM243x: The example toggles PRU0 GPO4 and PRU1 GPO4 -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. Default: Enabled for PRU0. - -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. +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 + +The pin stays high for 0.5 s and low for 0.5 s: a 1 Hz square wave with a duty +cycle of about 50%. The cycle count calculation, including the loop overhead, +is in the comment above `main()` in `firmware/main.c`. + +It assumes a 200 MHz PRU clock. If your clock is different, the frequency +changes in proportion. -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. +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 @@ -40,5 +55,29 @@ 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 - - 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 - + - 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 + +## 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 + 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 + PRU_ICSSG0_PRU0) and halt it. R30 should be `0x10` or `0x00`, and the + program counter should be inside `main()`. 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 and AM243x the + I/O expander must be set (done in `gpio_toggle_c.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. **Speed it up for a scope.** Set `TOGGLE_DELAY_CYCLES` in `main.c` to + `100000`, which gives a 1 kHz square wave at 200 MHz. +6. **Toggle all pins at once.** In `main.c` change `gpio = 0x10;` to + `gpio = 0xFFFFFFFF;`. Pins only show a signal if they are pinmuxed as + PRU GPOs in `example.syscfg`. diff --git a/academy/readme.md b/academy/readme.md index 3f1ec045f..3d0ff074c 100644 --- a/academy/readme.md +++ b/academy/readme.md @@ -88,8 +88,8 @@ For more information about the PRU features on each processor, refer to app note | -------------------- | ------ | ------ | ------- | ------ | ----- | ----- | | crc/crc | Yport | Y | Y | Y | Yport | Yport | | getting_started_labs | Y | Y | Y | Y | Y | Y | -| gpio/gpio_toggle | Y | Y | Y | Y | N-sw | Y | -| gpio/gpio_toggle_c | Y | Yport | Yport | Yport | N-sw | Y | +| gpio/gpio_toggle | Y | Y | Y | Y | Yport | Y | +| gpio/gpio_toggle_c | Y | Yport | Yport | Yport | Yport | Y | | intc/intc_mcu | Yport | Y | Y | Y | N-sw | Yport | | mac/mac | Yport | Y | Y | Y | Yport | Yport | | mac/mac_c | Yport | Yport | Yport | Yport | Y | Y |