Skip to content

Commit b21e26b

Browse files
committed
swap ev3-pru0 and ev3-pru1 project
it turns out that the existing soft-UART driver uses PRU0, so we want the tacho counter to be PRU1
1 parent 699fb87 commit b21e26b

File tree

7 files changed

+312
-312
lines changed

7 files changed

+312
-312
lines changed

ev3-pru0/.cproject

Lines changed: 39 additions & 39 deletions
Large diffs are not rendered by default.

ev3-pru0/AM18xx_PRU.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
MEMORY
1515
{
1616
PAGE 0:
17-
/* 2kB PRU0/1 Instruction RAM */
17+
/* 4kB PRU0/1 Instruction RAM */
1818
PRU_IMEM : org = 0x00000000 len = 0x00001000
1919

2020
PAGE 1:

ev3-pru0/main.c

Lines changed: 21 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -15,174 +15,54 @@
1515

1616
#include "resource_table.h"
1717

18-
/* from Linux */
19-
20-
#define EV3_PRU_TACHO_RING_BUF_SIZE 1024 /* must be power of 2! */
21-
22-
enum ev3_pru_tacho_msg_type {
23-
/* Host >< PRU: request memory map address of ring buffer data */
24-
EV3_PRU_TACHO_MSG_DATA_ADDR,
25-
};
26-
27-
struct ev3_pru_tacho_msg {
28-
uint32_t type;
29-
uint32_t value;
30-
};
31-
32-
enum ev3_pru_tacho {
33-
EV3_PRU_TACHO_A,
34-
EV3_PRU_TACHO_B,
35-
EV3_PRU_TACHO_C,
36-
EV3_PRU_TACHO_D,
37-
NUM_EV3_PRU_TACHO
38-
};
39-
40-
struct ev3_pru_tacho_remote_data {
41-
uint32_t position[NUM_EV3_PRU_TACHO][EV3_PRU_TACHO_RING_BUF_SIZE];
42-
uint32_t timestamp[NUM_EV3_PRU_TACHO][EV3_PRU_TACHO_RING_BUF_SIZE];
43-
uint32_t head[NUM_EV3_PRU_TACHO];
44-
};
45-
46-
/* end Linux */
47-
48-
enum direction {
49-
REVERSE = -1,
50-
UNKNOWN = 0,
51-
FORWARD = 1
52-
};
53-
54-
#define REMOTE_DATA_ADDR 0x80002000
55-
#define REMOTE_DATA (*(struct ev3_pru_tacho_remote_data *)(REMOTE_DATA_ADDR))
5618

5719
// system events to/from ARM
5820

5921
// PRU0
6022
#define HOST_INT (uint32_t)(1 << 30) // host interrupt 0
61-
#define EVENT_FROM_ARM 32
62-
#define EVENT_TO_ARM 33
23+
#define EVENT_FROM_ARM 60
24+
#define EVENT_TO_ARM 61
6325

6426
// PRU1
6527
//#define HOST_INT (uint32_t)(1 << 31) // host interrupt 1
66-
//#define EVENT_FROM_ARM 34
67-
//#define EVENT_TO_ARM 35
28+
//#define EVENT_FROM_ARM 62
29+
//#define EVENT_TO_ARM 63
6830

6931
volatile uint32_t register __R31;
7032

71-
// This is 512B (RPMSG_BUF_SIZE), so it won't fit in PRU RAM, so put it in shared RAM
72-
#define payload ((void *)(REMOTE_DATA_ADDR + sizeof(struct ev3_pru_tacho_remote_data)))
73-
74-
#define INTA GPIO.IN_DATA45_bit.GP5P11 // GPIO 5[11]
75-
#define INTB GPIO.IN_DATA45_bit.GP5P8 // GPIO 5[8]
76-
#define INTC GPIO.IN_DATA45_bit.GP5P13 // GPIO 5[13]
77-
#define INTD GPIO.IN_DATA67_bit.GP6P9 // GPIO 6[9]
78-
79-
#define DIRA GPIO.IN_DATA01_bit.GP0P4 // GPIO 0[4]
80-
#define DIRB GPIO.IN_DATA23_bit.GP2P9 // GPIO 2[9]
81-
#define DIRC GPIO.IN_DATA23_bit.GP3P14 // GPIO 3[14]
82-
#define DIRD GPIO.IN_DATA23_bit.GP2P8 // GPIO 2[8]
83-
84-
#define TACHO_STATE(x) ((INT##x << 1) | DIR##x)
85-
86-
static uint8_t tacho_state[NUM_EV3_PRU_TACHO];
87-
static uint32_t tacho_prev_timestamp[NUM_EV3_PRU_TACHO];
88-
static uint32_t tacho_counts[NUM_EV3_PRU_TACHO];
89-
90-
static void update_tacho_state(enum ev3_pru_tacho idx, uint8_t new_state)
91-
{
92-
uint8_t current_state = tacho_state[idx] & 0x3;
93-
enum direction new_dir = UNKNOWN;
94-
uint32_t now, elapsed;
95-
96-
switch (current_state) {
97-
case 0x0:
98-
if (new_state == 0x1) {
99-
new_dir = REVERSE;
100-
} else if (new_state == 0x2) {
101-
new_dir = FORWARD;
102-
}
103-
break;
104-
105-
case 0x1:
106-
if (new_state == 0x3) {
107-
new_dir = REVERSE;
108-
} else if (new_state == 0x0) {
109-
new_dir = FORWARD;
110-
}
111-
break;
112-
113-
case 0x3:
114-
if (new_state == 0x2) {
115-
new_dir = REVERSE;
116-
} else if (new_state == 0x1) {
117-
new_dir = FORWARD;
118-
}
119-
break;
120-
121-
case 0x2:
122-
if (new_state == 0x0) {
123-
new_dir = REVERSE;
124-
} else if (new_state == 0x3) {
125-
new_dir = FORWARD;
126-
}
127-
break;
128-
}
129-
130-
tacho_state[idx] = new_state;
131-
tacho_counts[idx] += new_dir;
33+
// only have 512B for stack/data, so this won't fit
34+
//uint8_t payload[RPMSG_BUF_SIZE];
35+
// using 128K shared RAM for now
36+
#define payload ((uint8_t *)(0x8001F200))
13237

133-
now = TIMER64P0.TIM34;
134-
elapsed = now - tacho_prev_timestamp[idx];
135-
136-
// if there was a change in count or if count hasn't changed for 50ms
137-
if (new_dir || elapsed > (50 * 1000000 * 3 / 125)) {
138-
uint32_t new_head = (REMOTE_DATA.head[idx] + 1) & (EV3_PRU_TACHO_RING_BUF_SIZE - 1);
139-
140-
tacho_prev_timestamp[idx] = now;
141-
142-
REMOTE_DATA.position[idx][new_head] = tacho_counts[idx];
143-
REMOTE_DATA.timestamp[idx][new_head] = now;
144-
REMOTE_DATA.head[idx] = new_head;
145-
}
146-
}
38+
#define LED (GPIO.OUT_DATA67_bit.GP6P7)
14739

14840
int main(void) {
14941
volatile uint8_t *status;
15042
struct pru_rpmsg_transport transport;
15143
uint16_t src, dst, len;
15244

15345
// Clear the status of the PRU-system event that the ARM will use to 'kick' us
154-
PRU_INTC.STATIDXCLR = EVENT_FROM_ARM;
46+
PRU_INTC.STATIDXCLR_bit.INDEX = EVENT_FROM_ARM;
15547

15648
// Wait until Linux gives us the OK that the driver is loaded
15749
status = &resource_table.rpmsg_vdev.status;
15850
while (!(*status & VIRTIO_CONFIG_S_DRIVER_OK));
15951

160-
pru_virtqueue_init(&transport.virtqueue0, &resource_table.vring0, EVENT_TO_ARM, EVENT_FROM_ARM);
161-
pru_virtqueue_init(&transport.virtqueue1, &resource_table.vring1, EVENT_TO_ARM, EVENT_FROM_ARM);
52+
pru_rpmsg_init(&transport, &resource_table.vring0, &resource_table.vring1, EVENT_TO_ARM, EVENT_FROM_ARM);
16253

163-
while (pru_rpmsg_channel(RPMSG_NS_CREATE, &transport, "ev3-tacho-rpmsg", 0) != PRU_RPMSG_SUCCESS);
54+
while (pru_rpmsg_channel(RPMSG_NS_CREATE, &transport, "rpmsg-client-sample", 0) != PRU_RPMSG_SUCCESS);
16455

16556
while (true) {
166-
// wait for the ARM to kick us
167-
while (!(__R31 & HOST_INT)) {
168-
update_tacho_state(EV3_PRU_TACHO_A, TACHO_STATE(A));
169-
update_tacho_state(EV3_PRU_TACHO_B, TACHO_STATE(B));
170-
update_tacho_state(EV3_PRU_TACHO_C, TACHO_STATE(C));
171-
update_tacho_state(EV3_PRU_TACHO_D, TACHO_STATE(D));
172-
}
173-
174-
// clear the interrupt
175-
PRU_INTC.STATIDXCLR = EVENT_FROM_ARM;
176-
177-
// Receive all available messages, multiple messages can be sent per kick
178-
while (pru_rpmsg_receive(&transport, &src, &dst, payload, &len) == PRU_RPMSG_SUCCESS) {
179-
struct ev3_pru_tacho_msg *msg = payload;
180-
181-
switch (msg->type) {
182-
case EV3_PRU_TACHO_MSG_DATA_ADDR:
183-
msg->value = REMOTE_DATA_ADDR;
184-
pru_rpmsg_send(&transport, dst, src, msg, sizeof(*msg));
185-
break;
57+
// Check bit 30 of register R31 to see if the ARM has kicked us
58+
if (__R31 & HOST_INT) {
59+
// clear the interrupt
60+
PRU_INTC.STATIDXCLR_bit.INDEX = EVENT_FROM_ARM;
61+
62+
// Receive all available messages, multiple messages can be sent per kick
63+
while (pru_rpmsg_receive(&transport, &src, &dst, payload, &len) == PRU_RPMSG_SUCCESS) {
64+
// Echo the message back
65+
pru_rpmsg_send(&transport, dst, src, payload, len);
18666
}
18767
}
18868
}

ev3-pru0/resource_table.h

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
/*
2-
* Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
2+
* Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
33
* Copyright (C) 2018 David Lechner <david@lechnology.com>
44
*
5-
* Redistribution and use in source and binary forms, with or without
6-
* modification, are permitted provided that the following conditions
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
77
* are met:
8-
*
9-
* * Redistributions of source code must retain the above copyright
8+
*
9+
* * Redistributions of source code must retain the above copyright
1010
* notice, this list of conditions and the following disclaimer.
11-
*
11+
*
1212
* * Redistributions in binary form must reproduce the above copyright
13-
* notice, this list of conditions and the following disclaimer in the
14-
* documentation and/or other materials provided with the
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the
1515
* distribution.
16-
*
16+
*
1717
* * Neither the name of Texas Instruments Incorporated nor the names of
1818
* its contributors may be used to endorse or promote products derived
1919
* from this software without specific prior written permission.
20-
*
21-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22-
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2323
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24-
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25-
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26-
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2727
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2828
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29-
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30-
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3131
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3232
*/
3333

@@ -42,7 +42,7 @@
4242
/*
4343
* The feature bitmap for virtio rpmsg
4444
*/
45-
#define VIRTIO_RPMSG_F_NS 0 //name service notifications
45+
#define VIRTIO_RPMSG_F_NS 0 //name service notifications
4646

4747
/* This firmware supports name service notifications as one of its features */
4848
#define RPMSG_FEATURES (1 << VIRTIO_RPMSG_F_NS)
@@ -62,8 +62,7 @@ struct custom_resource_table {
6262

6363
#pragma DATA_SECTION(resource_table, ".resource_table")
6464
#pragma RETAIN(resource_table)
65-
struct custom_resource_table resource_table =
66-
{
65+
struct custom_resource_table resource_table = {
6766
.base = {
6867
.ver = 1,
6968
.num = NUM_RESOURCES,
@@ -77,23 +76,23 @@ struct custom_resource_table resource_table =
7776
.id = VIRTIO_ID_RPMSG,
7877
.notifyid = 0,
7978
.dfeatures = RPMSG_FEATURES,
80-
.gfeatures = 0, //will be populated by host
79+
.gfeatures = 0, //will be populated by host
8180
.config_len = 0,
8281
.status = 0,
8382
.num_of_vrings = 2,
8483
.reserved = { 0, 0 },
8584
},
8685
.vring0 = {
87-
.da = 0, //will be populated by host
88-
.align = 16,
89-
.num = 16, // must be power of 2
86+
.da = 0, //will be populated by host
87+
.align = 16,
88+
.num = 16, // must be power of 2
9089
.notifyid = 0, //will be populated by host
9190
.reserved = 0,
9291
},
9392
.vring1 = {
94-
.da = 0, //will be populated by host
95-
.align = 16,
96-
.num = 16, // must be power of 2
93+
.da = 0, //will be populated by host
94+
.align = 16,
95+
.num = 16, // must be power of 2
9796
.notifyid = 0, //will be populated by host
9897
.reserved = 0,
9998
},

0 commit comments

Comments
 (0)