ctu_can_fd_userspace.cpp 7.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
extern "C" {
#include "ctu_can_fd_linux_defs.h"
#include "ctu_can_fd_hw.h"
}

#undef abs
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <inttypes.h>
#include <err.h>

//#include "clara.hpp"
#include <iostream>

#define CANFD_ADDR_BASE    0x43C30000
#define CANFD_ADDR_RANGE   0x10000

static const char *memdev = "/dev/mem";
static int mem_fd = -1;

static void mem_open()
{
    mem_fd = open(memdev, O_RDWR|O_SYNC);
    if (mem_fd < 0) {
        err(1, "open memory device");
    }
}

void *mem_map(unsigned long mem_start, unsigned long mem_length)
{
    unsigned long pagesize, mem_window_size;
    void *mm, *mem;

    //pagesize = getpagesize();
    pagesize = sysconf(_SC_PAGESIZE);

    mem_window_size = ((mem_start & (pagesize-1)) + mem_length + pagesize-1) & ~(pagesize-1);

    mm = mmap(NULL, mem_window_size, PROT_WRITE|PROT_READ,
              MAP_SHARED, mem_fd, mem_start & ~(pagesize-1));
    mem = (char*)mm + (mem_start & (pagesize-1));

    if (mm == MAP_FAILED) {
        err(1, "mmap");
        return NULL;
    }

    fprintf(stderr, "mmap 0x%lx -> %p\n",mem_start,mem);
    return mem;
}

Martin Jeřábek's avatar
Martin Jeřábek committed
56 57 58 59 60 61
unsigned ctu_can_fd_read8(struct ctucanfd_priv *priv, enum ctu_can_fd_regs reg) {
    return priv->read_reg(priv, (enum ctu_can_fd_regs)(reg & ~3)) >> (8 * (reg & 3));
}
unsigned ctu_can_fd_read16(struct ctucanfd_priv *priv, enum ctu_can_fd_regs reg) {
    return priv->read_reg(priv, (enum ctu_can_fd_regs)(reg & ~1)) >> (8 * (reg & 1));
}
62
/*
63 64 65 66 67
void ctu_can_fd_write8(struct ctucanfd_priv *priv, enum ctu_can_fd_regs reg, uint8_t val) {
    iowrite8(val, (uint8_t*)priv->mem_base + reg);
}
void ctu_can_fd_write16(struct ctucanfd_priv *priv, enum ctu_can_fd_regs reg, uint16_t val) {
    iowrite16(val, (uint8_t*)priv->mem_base + reg);
68
}*/
Martin Jeřábek's avatar
Martin Jeřábek committed
69 70


71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
int main(int argc, char *argv[])
{
    uint32_t addr_base;
    unsigned ifc = 0;
    bool do_transmit = false;
    //bool do_showhelp = false;

    int c;
    char *e;
    const char *progname = argv[0];
    while ((c = getopt(argc, argv, "i:th")) != -1) {
        switch (c) {
            case 'i':
                ifc = strtoul(optarg, &e, 0);
                if (*e != '\0')
                    err(1, "-i expects a number");
            break;
            case 't': do_transmit = true; break;
            case 'h':
                printf("Usage: %s [-i ifc] [-t]\n\n"
                       "  -t: Transmit\n",
                       progname
                );
                return 0;
        }
    }
/*
    using namespace clara;
    auto cli = //Opt(addr_base, "addr_base")
               //    ["-A"]["--addr_base"]("CAN FD registers base address")
               Opt(ifc, "ifc") ["-i"]("CAN FD interface number")
             | Opt(do_transmit)
                   ["-t"]("Do transmit")
             | Help(do_showhelp)
             ;
             std::cout << "IFC:" << ifc << std::endl;

    auto result = cli.parse(Args(argc, argv));

    if (!result) {
        std::cerr << "Error in command line: " << result.errorMessage() << std::endl;
        exit(1);
    }
    if (do_showhelp) {
        std::cout << cli;
        return 0;
    }
*/
    static const uint32_t addrs[] = {0x43C30000, 0x43C70000};
    if (ifc >= 2) {
        std::cerr << "Err: ifc number must be 0 or 1.\n";
        exit(1);
    }
    addr_base = addrs[ifc];

    mem_open();



    volatile void * const base = mem_map(addr_base, CANFD_ADDR_RANGE);
    struct ctucanfd_priv _p, *priv = &_p;
    int res;

    priv->mem_base = base;
    priv->read_reg = ctu_can_fd_read32;
Martin Jeřábek's avatar
Martin Jeřábek committed
136
    priv->write_reg = ctu_can_fd_write32;
137 138

    union ctu_can_fd_device_id_version reg;
139
    reg.u32 = priv->read_reg(priv, CTU_CAN_FD_DEVICE_ID);
140 141 142 143 144 145 146 147 148 149 150 151

    printf("DevID: 0x%08x, should be 0x%08x\n", reg.s.device_id, CTU_CAN_FD_ID);

    if (!ctu_can_fd_check_access(priv))
        errx(1, "error: ctu_can_fd_check_access");

    u32 version = ctu_can_fd_get_version(priv);
    printf("Core version: %u\n", version);

    //struct can_ctrlmode ctrlmode = {CAN_CTRLMODE_FD, CAN_CTRLMODE_FD};
    //ctu_can_fd_set_mode(priv, &ctrlmode);

152 153


154 155
    //printf("NOT RESETTING!\n");
    ctu_can_fd_reset(priv);
Martin Jeřábek's avatar
Martin Jeřábek committed
156 157 158 159

    {
        union ctu_can_fd_mode_command_status_settings mode;
        mode.u32 = priv->read_reg(priv, CTU_CAN_FD_MODE);
160

Martin Jeřábek's avatar
Martin Jeřábek committed
161 162 163 164
        if (mode.s.ena) {
            printf("Core is enabled but should be disabled!\n");
        }
    }
165 166 167 168 169

    struct net_device nd;
    nd.can.clock.freq = 100000000;

    struct can_bittiming nom_timing = {
170
        .bitrate = 1000000,
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    };
    res = can_get_bittiming(&nd, &nom_timing,
                      &ctu_can_fd_bit_timing_max,
                      NULL,
                      0);
    if (res)
        err(res, "can_get_bittiming");
    printf("sample_point .%03d, tq %d, prop %d, seg1 %d, seg2 %d, sjw %d, brp %d, bitrate %d\n",
           nom_timing.sample_point,
           nom_timing.tq,
           nom_timing.prop_seg,
           nom_timing.phase_seg1,
           nom_timing.phase_seg2,
           nom_timing.sjw,
           nom_timing.brp,
           nom_timing.bitrate
    );

189 190
    priv->write_reg(priv, CTU_CAN_FD_INT_MASK_CLR, 0xffff);
    priv->write_reg(priv, CTU_CAN_FD_INT_ENA_SET, 0xffff);
191 192
    ctu_can_fd_set_nom_bittiming(priv, &nom_timing);
    ctu_can_fd_rel_rx_buf(priv);
Martin Jeřábek's avatar
Martin Jeřábek committed
193 194
    //ctu_can_fd_set_ret_limit(priv, true, 1);
    ctu_can_fd_set_ret_limit(priv, false, 0);
195 196 197 198 199 200 201 202 203
    ctu_can_fd_abort_tx(priv);
    ctu_can_fd_txt_set_abort(priv, CTU_CAN_FD_TXT_BUFFER_1);
    ctu_can_fd_txt_set_empty(priv, CTU_CAN_FD_TXT_BUFFER_1);
    ctu_can_fd_enable(priv, true);
    usleep(10000);


    if (do_transmit) {
        struct canfd_frame txf;
204
        txf.can_id = 0x1FF;
205
        txf.flags = 0;
206 207
        //u8 d[] = {0xde, 0xad, 0xbe, 0xef};
        u8 d[] = {0xDE, 0xAD, 0xBE, 0xEF};
208 209 210
        memcpy(txf.data, d, sizeof(d));
        txf.len = sizeof(d);

Martin Jeřábek's avatar
Martin Jeřábek committed
211
        res = ctu_can_fd_insert_frame(priv, &txf, 0, CTU_CAN_FD_TXT_BUFFER_1, false);
212 213 214
        if (!res)
            printf("TX failed\n");
        ctu_can_fd_txt_set_rdy(priv, CTU_CAN_FD_TXT_BUFFER_1);
215
        return 0;
216 217 218 219 220 221 222 223 224 225 226
    }

    while (1) {
        u32 nrxf = ctu_can_fd_get_rx_frame_count(priv);//ctu_can_fd_get_rx_frame_ctr(priv);
        union ctu_can_fd_rx_mem_info reg;
        reg.u32 = ctu_can_fd_read32(priv, CTU_CAN_FD_RX_MEM_INFO);
        u32 rxsz = reg.s.rx_buff_size - reg.s.rx_mem_free;

        printf("%u RX frames, %u words", nrxf, rxsz);
        printf(", status 0x%02hhx", ctu_can_fd_read8(priv, CTU_CAN_FD_STATUS));
        printf(", settings 0x%02hhx", ctu_can_fd_read8(priv, CTU_CAN_FD_SETTINGS));
227 228 229 230
        printf(", INT_STAT 0x%04hhx", ctu_can_fd_read16(priv, CTU_CAN_FD_INT_STAT));
        printf(", CTU_CAN_FD_INT_ENA_SET 0x%04hx", priv->read_reg(priv, CTU_CAN_FD_INT_ENA_SET));
        printf(", CTU_CAN_FD_INT_MASK_SET 0x%04hx", priv->read_reg(priv, CTU_CAN_FD_INT_MASK_SET));
        printf(", CTU_CAN_FD_TX_STATUS 0x%04hx", priv->read_reg(priv, CTU_CAN_FD_TX_STATUS));
231 232 233 234
        //printf(", CTU_CAN_FD_ERR_CAPT 0x%02hhx", ctu_can_fd_read8(priv, CTU_CAN_FD_ERR_CAPT));


        printf("\n");
235 236 237 238 239 240
        /*
        while (rxsz--) {
            u32 data = priv->read_reg(priv, CTU_CAN_FD_RX_DATA);
            printf("  0x%08x\n", data);
        }
        */
241 242 243 244
        if (nrxf || rxsz) {
            struct canfd_frame cf;
            u64 ts;
            ctu_can_fd_read_rx_frame(priv, &cf, &ts);
245
            printf("%llu: #%x [%u]", ts, cf.can_id, cf.len);
246 247 248 249
            for (int i=0; i<cf.len; ++i)
                printf(" %02x", cf.data[i]);
            printf("\n");
        }
250

251 252 253 254 255
        usleep(1000000);
    }

    return 0;
}