Hi,
I am trying for days now but had no success yet, hopefully you could help:
I checked out an example on getting a HCSR04 example to work from here: https://github.com/luigif/hcsr04
After setting up the PRU environment on the BBB the examples worked fine, but now I want to move the sonar to different pins on my BBB (echo pin P9_27 and trigger pin P9_31). I adapted the dts file accordingly and also tried to modify the .p-file. My overlay seems to be initialized correctly:
# cat /sys/devices/bone_capemgr.9/slots
1: 55:PF---
2: 56:PF---
3: 57:PF---
4: ff:P-O-L Bone-LT-eMMC-2G,00A0,Texas Instrument,BB-BONE-EMMC-2G
5: ff:P-O-- Bone-Black-HDMI,00A0,Texas Instrument,BB-BONELT-HDMI
6: ff:P-O-- Bone-Black-HDMIN,00A0,Texas Instrument,BB-BONELT-HDMIN
8: ff:P-O-L Override Board Name,00A0,Override Manuf,hcsr04_2
root@beaglebone:/home/debian/Projects/hcsr04_2#
and the pinmux-functions output:
root@beaglebone:/home/debian/Projects/hcsr04_2# cat /sys/kernel/debug/pinctrl/44e10800.pinmux/pinmux-functions
function: pinmux_userled_pins, groups = [ pinmux_userled_pins ]
function: pinmux_rstctl_pins, groups = [ pinmux_rstctl_pins ]
function: pinmux_i2c0_pins, groups = [ pinmux_i2c0_pins ]
function: pinmux_i2c2_pins, groups = [ pinmux_i2c2_pins ]
function: pinmux_mmc1_pins, groups = [ pinmux_mmc1_pins ]
function: pinmux_emmc2_pins, groups = [ pinmux_emmc2_pins ]
function: pinmux_userled_pins, groups = [ pinmux_userled_pins ]
function: pinctrl_hcsr04_2_pins, groups = [ pinctrl_hcsr04_2_pins ]
root@beaglebone:/home/debian/Projects/hcsr04_2#
When I now start the application, I don't get any reception from the sonar. I suppose I got something wrong in my assembly code, but I cannot figure out what exactly. I hope someone could give me a hint.
My modified .dts file looks like:
/dts-v1/;
/plugin/;
// www.valvers.com/.../step04-gpio
/ {
compatible = "ti,beaglebone", "ti,beaglebone-black";
/* identification */
part-number = "hcsr04_2";
version = "00A0";
/*exclusive-use =
"P8_12", "P8_11",
"GPIO44", "GPIO45";*/
exclusive-use =
"P9.31", "P9.27",
"gpio3_14", "gpio3_19";
fragment@10 {
target = <&am33xx_pinmux>;
__overlay__ {
pinctrl_hcsr04_2: pinctrl_hcsr04_2_pins {
pinctrl-single,pins = <
/* old stuff */
/*0x030 0x07*/ /* P8_12 gpio1[12] GPIO44 out pulldown Mode: 7 */
/*0x034 0x27*/ /* P8_11 gpio1[13] GPIO45 in pulldown Mode: 7 */
/* end of old stuff*/
0x1a4 0x07 /* Trigger: P9_27 gpio3[19] GPIO117 out pulldown Mode: 7 */
0x190 0x27 /* Echo: P9_31 gpio3[14] GPIO110 in pulldown Mode: 7 */
>;
};
};
};
fragment@11 {
target = <&ocp>;
__overlay__ {
hcsr04_2 {
compatible = "bone-pinmux-helper","gpio","pruss";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_hcsr04_2>;
status = "okay";
};
};
};
fragment@12{
target = <&pruss>;
__overlay__ {
status = "okay";
};
};
};
And also my .p-File
// Define the entry point of the program
.origin 0
.entrypoint START
// Address of the io controller for GPIO1 and GPIO2
#define GPIO1 0x4804C000
#define GPIO2 0x481AC000
#define GPIO3 0x481AE000
// Offset address for the output enable register of the gpio controller
#define GPIO_OE 0x134
// Offset address for the data in/out register of the gpio controller
#define GPIO_DATAIN 0x138
#define GPIO_CLEARDATAOUT 0x190
#define GPIO_SETDATAOUT 0x194
// PRU interrupt for PRU0 and PRU1
#define PRU0_ARM_INTERRUPT 19
#define PRU1_ARM_INTERRUPT 20
// original code
// gpio1[12] P8_12 gpio44 0x030
//#define BIT_TRIGGER 0x0C
// gpio1[13] P8_11 gpio45 0x034
//#define BIT_ECHO 0x0D
// end of original code
// gpio3[14] P9_31 gpio110 0x6B
#define BIT_ECHO 0x0E // gpio3_14 -> gpio pin 110
// gpio3[21] P9_27 gpio115 0x03
#define BIT_TRIGGER 0x13 // gpio3_19 -> gpio pin 125
#define delay r0
#define roundtrip r4
START:
// Clear the STANDBY_INIT bit in the SYSCFG register
// otherwise the PRU will not be able to write outside the PRU memory space
// and to the Beaglebone pins
LBCO r0, C4, 4, 4
CLR r0, r0, 4
SBCO r0, C4, 4, 4
// Make constant 24 (c24) point to the beginning of PRU0 data ram
MOV r0, 0x00000000
MOV r1, 0x22020
SBBO r0, r1, 0, 4
// Enable trigger as output and echo as input (clear BIT_TRIGGER and set BIT_ECHO of output enable)
MOV r3, GPIO3 | GPIO_OE
LBBO r2, r3, 0, 4
CLR r2, BIT_TRIGGER
SET r2, BIT_ECHO
SBBO r2, r3, 0, 4
TRIGGER:
// Fire the sonar
// Set trigger pin to high
MOV r2, 1<<BIT_TRIGGER
MOV r3, GPIO3 | GPIO_SETDATAOUT
SBBO r2, r3, 0, 4
// Delay 10 microseconds (200 MHz / 2 instructions = 10 ns per loop, 10 us = 1000 loops)
MOV delay, 1000
TRIGGER_DELAY:
SUB delay, delay, 1
QBNE TRIGGER_DELAY, delay, 0
// Set trigger pin to low
MOV r2, 1<<BIT_TRIGGER
MOV r3, GPIO3 | GPIO_CLEARDATAOUT
SBBO r2, r3, 0, 4
// Wait for BIT_ECHO to go high, i.e. wait for the echo cycle to start
MOV r3, GPIO3 | GPIO_DATAIN
WAIT_ECHO:
// Read the GPIO until BIT_ECHO goes high
LBBO r2, r3, 0, 4
QBBC WAIT_ECHO, r2, BIT_ECHO
// roundtrip measures the echo duration in microseconds, resolution is 1us
MOV roundtrip, 0
SAMPLE_ECHO:
// Delay 1 microsecond (adjusted because it takes time to query the GPIO pin)
MOV delay, 76
SAMPLE_ECHO_DELAY:
SUB delay, delay, 1
QBNE SAMPLE_ECHO_DELAY, delay, 0
// Add 1us to the roundtrip counter
ADD roundtrip, roundtrip, 1
// Read GPIO until BIT_ECHO goes low
LBBO r2, r3, 0, 4
QBBS SAMPLE_ECHO, r2, BIT_ECHO
// Echo is complete
// Store the microsecond count in the PRU's data ram so C program can read it
SBCO roundtrip, c24, 0, 4
// Trigger the PRU0 interrupt (C program gets the event)
MOV r31.b0, PRU0_ARM_INTERRUPT+16
// Delay to allow sonar to stop resonating and sound burst to decay in environment
MOV delay, 3000000
RESET_DELAY:
SUB delay, delay, 1
QBNE RESET_DELAY, delay, 0
// Jump back to triggering the sonar
JMP TRIGGER
HALT
Additional information: I work on a Beaglebone Black in revision C1,
thank you in advance!