userspace_utils.cpp 3.39 KB
Newer Older
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1
/*******************************************************************************
Martin Jeřábek's avatar
Martin Jeřábek committed
2
 *
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
3
 * CTU CAN FD IP Core
4
 * Copyright (C) 2015-2018
Martin Jeřábek's avatar
Martin Jeřábek committed
5
 *
6 7
 * Authors:
 *     Ondrej Ille <ondrej.ille@gmail.com>
8
 *     Martin Jerabek <martin.jerabek01@gmail.com>
9
 *     Jaroslav Beran <jara.beran@gmail.com>
Martin Jeřábek's avatar
Martin Jeřábek committed
10 11
 *
 * Project advisors:
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
12 13
 * 	Jiri Novak <jnovak@fel.cvut.cz>
 * 	Pavel Pisa <pisa@cmp.felk.cvut.cz>
Martin Jeřábek's avatar
Martin Jeřábek committed
14
 *
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
15 16 17
 * Department of Measurement         (http://meas.fel.cvut.cz/)
 * Faculty of Electrical Engineering (http://www.fel.cvut.cz)
 * Czech Technical University        (http://www.cvut.cz/)
Martin Jeřábek's avatar
Martin Jeřábek committed
18
 *
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
19 20 21 22
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
Martin Jeřábek's avatar
Martin Jeřábek committed
23
 *
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
24 25 26 27
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
Martin Jeřábek's avatar
Martin Jeřábek committed
28
 *
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
29 30 31
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Martin Jeřábek's avatar
Martin Jeřábek committed
32
 *
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
33 34
*******************************************************************************/

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#include "userspace_utils.h"

#include <iostream>

static const char * const memdev = "/dev/mem";
static int mem_fd = -1;
#define CANFD_ADDR_RANGE 4096

static void mem_open()
{
    if (mem_fd >= 0)
        return;

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

static 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;
}

77
unsigned ctu_can_fd_read8(struct ctucan_hw_priv *priv, enum ctu_can_fd_can_registers reg) {
78
    return priv->read_reg(priv, (enum ctu_can_fd_can_registers)(reg & ~3)) >> (8 * (reg & 3));
79
}
80
unsigned ctu_can_fd_read16(struct ctucan_hw_priv *priv, enum ctu_can_fd_can_registers reg) {
81
    return priv->read_reg(priv, (enum ctu_can_fd_can_registers)(reg & ~1)) >> (8 * (reg & 1));
82 83
}
/*
84
void ctu_can_fd_write8(struct ctucan_hw_priv *priv, enum ctu_can_fd_can_registers reg, uint8_t val) {
85 86
    iowrite8(val, (uint8_t*)priv->mem_base + reg);
}
87
void ctu_can_fd_write16(struct ctucan_hw_priv *priv, enum ctu_can_fd_can_registers reg, uint16_t val) {
88 89 90
    iowrite16(val, (uint8_t*)priv->mem_base + reg);
}*/

91
struct ctucan_hw_priv* ctucanfd_init(uint32_t addr)
92 93 94 95
{
    mem_open();
    volatile void * const base = mem_map(addr, CANFD_ADDR_RANGE);

96
    struct ctucan_hw_priv *priv = new ctucan_hw_priv;
97 98 99
    memset(priv, 0, sizeof(*priv));

    priv->mem_base = base;
100 101
    priv->read_reg = ctucan_hw_read32;
    priv->write_reg = ctucan_hw_write32;
102 103 104 105

     // will leak memory, but who cares, this is just a prototype testing tool
    return priv;
}