|
1 | 1 | /* |
2 | | - * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/ |
| 2 | + * Copyright (C) 2018 David Lechner <david@lechnology.com> |
3 | 3 | * |
4 | 4 | * |
5 | 5 | * Redistribution and use in source and binary forms, with or without |
|
31 | 31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | 32 | */ |
33 | 33 |
|
| 34 | +#include <stdbool.h> |
34 | 35 | #include <stdint.h> |
35 | | -#include "resource_table_empty.h" |
| 36 | +#include <stdio.h> |
| 37 | + |
| 38 | +#include <rsc_types.h> |
| 39 | + |
| 40 | +#include <pru_cfg.h> |
| 41 | +#include <pru_ctrl.h> |
| 42 | +#include <pru_intc.h> |
| 43 | +#include <pru_rpmsg.h> |
| 44 | + |
| 45 | +#include <sys_gpio.h> |
| 46 | +#include <sys_timer_1ms.h> |
| 47 | + |
| 48 | +#include "resource_table.h" |
| 49 | + |
| 50 | +/* from Linux */ |
| 51 | + |
| 52 | +#define EV3_PRU_TACHO_RING_BUF_SIZE 256 /* must be power of 2! */ |
| 53 | + |
| 54 | +enum ev3_pru_tacho_msg_type { |
| 55 | + /* Host >< PRU: request memory map address of ring buffer data */ |
| 56 | + EV3_PRU_TACHO_MSG_DATA_ADDR, |
| 57 | +}; |
| 58 | + |
| 59 | +struct ev3_pru_tacho_msg { |
| 60 | + uint32_t type; |
| 61 | + uint32_t value; |
| 62 | +}; |
| 63 | + |
| 64 | +enum ev3_pru_tacho { |
| 65 | + EV3_PRU_TACHO_A, |
| 66 | + EV3_PRU_TACHO_B, |
| 67 | + EV3_PRU_TACHO_C, |
| 68 | + EV3_PRU_TACHO_D, |
| 69 | + NUM_EV3_PRU_TACHO |
| 70 | +}; |
| 71 | + |
| 72 | +struct ev3_pru_tacho_remote_data { |
| 73 | + uint32_t position[NUM_EV3_PRU_TACHO][EV3_PRU_TACHO_RING_BUF_SIZE]; |
| 74 | + uint32_t timestamp[NUM_EV3_PRU_TACHO][EV3_PRU_TACHO_RING_BUF_SIZE]; |
| 75 | + uint32_t head[NUM_EV3_PRU_TACHO]; |
| 76 | +}; |
| 77 | + |
| 78 | +/* end Linux */ |
| 79 | + |
| 80 | +enum direction { |
| 81 | + REVERSE = -1, |
| 82 | + UNKNOWN = 0, |
| 83 | + FORWARD = 1 |
| 84 | +}; |
| 85 | + |
| 86 | +/* Host-0 Interrupt sets bit 30 in register R31 */ |
| 87 | +#define HOST_INT ((uint32_t) 1 << 30) |
| 88 | + |
| 89 | +/* The PRU-ICSS system events used for RPMsg are defined in the Linux device tree |
| 90 | + * PRU0 uses system event 16 (To ARM) and 17 (From ARM) |
| 91 | + * PRU1 uses system event 18 (To ARM) and 19 (From ARM) |
| 92 | + */ |
| 93 | +#define TO_ARM_HOST 16 |
| 94 | +#define FROM_ARM_HOST 17 |
| 95 | + |
| 96 | +#define PRU_GLOBAL_BASE_ADDR 0x4a300000 |
| 97 | +#define PRU_SRAM __far __attribute__((cregister("PRU_SHAREDMEM", far))) |
| 98 | + |
| 99 | +/* |
| 100 | + * Used to make sure the Linux drivers are ready for RPMsg communication |
| 101 | + * Found at linux-x.y.z/include/uapi/linux/virtio_config.h |
| 102 | + */ |
| 103 | +#define VIRTIO_CONFIG_S_DRIVER_OK 4 |
| 104 | + |
| 105 | +volatile register uint32_t __R31; |
| 106 | + |
| 107 | +#if defined(PLAT_EVB) |
| 108 | + |
| 109 | +#define INTA GPIO2.GPIO_DATAIN_bit.DATAIN4 |
| 110 | +#define INTB GPIO0.GPIO_DATAIN_bit.DATAIN26 |
| 111 | +#define INTC GPIO0.GPIO_DATAIN_bit.DATAIN27 |
| 112 | +#define INTD GPIO2.GPIO_DATAIN_bit.DATAIN11 |
| 113 | + |
| 114 | +#define DIRA GPIO2.GPIO_DATAIN_bit.DATAIN3 |
| 115 | +#define DIRB GPIO1.GPIO_DATAIN_bit.DATAIN12 |
| 116 | +#define DIRC GPIO1.GPIO_DATAIN_bit.DATAIN31 |
| 117 | +#define DIRD GPIO2.GPIO_DATAIN_bit.DATAIN9 |
| 118 | + |
| 119 | +#elif defined(PLAT_QUEST) |
| 120 | + |
| 121 | +#define INTA GPIO2.GPIO_DATAIN_bit.DATAIN4 |
| 122 | +#define INTB GPIO0.GPIO_DATAIN_bit.DATAIN26 |
| 123 | +#define INTC GPIO0.GPIO_DATAIN_bit.DATAIN27 |
| 124 | +#define INTD GPIO2.GPIO_DATAIN_bit.DATAIN12 |
| 125 | + |
| 126 | +#define DIRA GPIO2.GPIO_DATAIN_bit.DATAIN3 |
| 127 | +#define DIRB GPIO1.GPIO_DATAIN_bit.DATAIN12 |
| 128 | +#define DIRC GPIO1.GPIO_DATAIN_bit.DATAIN31 |
| 129 | +#define DIRD GPIO2.GPIO_DATAIN_bit.DATAIN13 |
| 130 | + |
| 131 | +#else |
| 132 | +#error Must define PLAT_xyz |
| 133 | +#endif |
| 134 | + |
| 135 | +#define TACHO_STATE(x) ((INT##x << 1) | DIR##x) |
| 136 | + |
| 137 | +static uint8_t tacho_state[NUM_EV3_PRU_TACHO]; |
| 138 | +static uint32_t tacho_prev_timestamp[NUM_EV3_PRU_TACHO]; |
| 139 | +static uint32_t tacho_counts[NUM_EV3_PRU_TACHO]; |
| 140 | + |
| 141 | +static uint8_t payload[RPMSG_BUF_SIZE]; |
| 142 | +static PRU_SRAM struct ev3_pru_tacho_remote_data remote_data; |
| 143 | + |
| 144 | +static void update_tacho_state(enum ev3_pru_tacho idx, uint8_t new_state) |
| 145 | +{ |
| 146 | + uint8_t current_state = tacho_state[idx] & 0x3; |
| 147 | + enum direction new_dir = UNKNOWN; |
| 148 | + uint32_t now, elapsed; |
| 149 | + |
| 150 | + switch (current_state) { |
| 151 | + case 0x0: |
| 152 | + if (new_state == 0x1) { |
| 153 | + new_dir = FORWARD; |
| 154 | + } else if (new_state == 0x2) { |
| 155 | + new_dir = REVERSE; |
| 156 | + } |
| 157 | + break; |
| 158 | + |
| 159 | + case 0x1: |
| 160 | + if (new_state == 0x3) { |
| 161 | + new_dir = FORWARD; |
| 162 | + } else if (new_state == 0x0) { |
| 163 | + new_dir = REVERSE; |
| 164 | + } |
| 165 | + break; |
| 166 | + |
| 167 | + case 0x3: |
| 168 | + if (new_state == 0x2) { |
| 169 | + new_dir = FORWARD; |
| 170 | + } else if (new_state == 0x1) { |
| 171 | + new_dir = REVERSE; |
| 172 | + } |
| 173 | + break; |
| 174 | + |
| 175 | + case 0x2: |
| 176 | + if (new_state == 0x0) { |
| 177 | + new_dir = FORWARD; |
| 178 | + } else if (new_state == 0x3) { |
| 179 | + new_dir = REVERSE; |
| 180 | + } |
| 181 | + break; |
| 182 | + } |
| 183 | + |
| 184 | + tacho_state[idx] = new_state; |
| 185 | + tacho_counts[idx] += new_dir; |
| 186 | + |
| 187 | + now = DMTIMER1_1MS.TCRR; |
| 188 | + elapsed = now - tacho_prev_timestamp[idx]; |
| 189 | + |
| 190 | + // if there was a change in count or if count hasn't changed for 50ms |
| 191 | + if (new_dir || elapsed > (50 * 1000000 * 3 / 125)) { |
| 192 | + uint32_t new_head = (remote_data.head[idx] + 1) & (EV3_PRU_TACHO_RING_BUF_SIZE - 1); |
| 193 | + |
| 194 | + tacho_prev_timestamp[idx] = now; |
| 195 | + |
| 196 | + remote_data.position[idx][new_head] = tacho_counts[idx]; |
| 197 | + remote_data.timestamp[idx][new_head] = now; |
| 198 | + remote_data.head[idx] = new_head; |
| 199 | + } |
| 200 | +} |
36 | 201 |
|
37 | 202 | int main(void) { |
38 | | - __halt(); |
| 203 | + volatile uint8_t *status; |
| 204 | + struct pru_rpmsg_transport transport; |
| 205 | + uint16_t src, dst, len; |
| 206 | + |
| 207 | + // Clear the status of the PRU-system event that the ARM will use to 'kick' us |
| 208 | + CT_INTC.SICR_bit.STS_CLR_IDX = FROM_ARM_HOST; |
| 209 | + |
| 210 | + // Wait until Linux gives us the OK that the driver is loaded |
| 211 | + status = &resource_table.rpmsg_vdev.status; |
| 212 | + while (!(*status & VIRTIO_CONFIG_S_DRIVER_OK)); |
| 213 | + |
| 214 | + pru_virtqueue_init(&transport.virtqueue0, &resource_table.rpmsg_vring0, TO_ARM_HOST, FROM_ARM_HOST); |
| 215 | + pru_virtqueue_init(&transport.virtqueue1, &resource_table.rpmsg_vring1, TO_ARM_HOST, FROM_ARM_HOST); |
| 216 | + |
| 217 | + while (pru_rpmsg_channel(RPMSG_NS_CREATE, &transport, "ev3-tacho-rpmsg", "", 0) != PRU_RPMSG_SUCCESS); |
| 218 | + |
| 219 | + while (true) { |
| 220 | + // wait for the ARM to kick us |
| 221 | + while (!(__R31 & HOST_INT)) { |
| 222 | + update_tacho_state(EV3_PRU_TACHO_A, TACHO_STATE(A)); |
| 223 | + update_tacho_state(EV3_PRU_TACHO_B, TACHO_STATE(B)); |
| 224 | + update_tacho_state(EV3_PRU_TACHO_C, TACHO_STATE(C)); |
| 225 | + update_tacho_state(EV3_PRU_TACHO_D, TACHO_STATE(D)); |
| 226 | + } |
| 227 | + |
| 228 | + // clear the interrupt |
| 229 | + CT_INTC.SICR_bit.STS_CLR_IDX = FROM_ARM_HOST; |
| 230 | + |
| 231 | + // Receive all available messages, multiple messages can be sent per kick |
| 232 | + while (pru_rpmsg_receive(&transport, &src, &dst, payload, &len) == PRU_RPMSG_SUCCESS) { |
| 233 | + struct ev3_pru_tacho_msg *msg = (void *)payload; |
| 234 | + |
| 235 | + switch (msg->type) { |
| 236 | + case EV3_PRU_TACHO_MSG_DATA_ADDR: |
| 237 | + msg->value = PRU_GLOBAL_BASE_ADDR + (uint32_t)&remote_data; |
| 238 | + pru_rpmsg_send(&transport, dst, src, msg, sizeof(*msg)); |
| 239 | + break; |
| 240 | + } |
| 241 | + } |
| 242 | + } |
39 | 243 | } |
0 commit comments