userspace_utils.cpp 3.34 KB
Newer Older
1
/*******************************************************************************
Martin Jeřábek's avatar
Martin Jeřábek committed
2
 *
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>
Martin Jeřábek's avatar
Martin Jeřábek committed
9 10
 *
 * Project advisors:
11 12
 * 	Jiri Novak <jnovak@fel.cvut.cz>
 * 	Pavel Pisa <pisa@cmp.felk.cvut.cz>
Martin Jeřábek's avatar
Martin Jeřábek committed
13
 *
14 15 16
 * 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
17
 *
18 19 20 21
 * 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
22
 *
23 24 25 26
 * 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
27
 *
28 29 30
 * 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
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#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;
}

76 77
unsigned ctu_can_fd_read8(struct ctucanfd_priv *priv, enum ctu_can_fd_can_registers reg) {
    return priv->read_reg(priv, (enum ctu_can_fd_can_registers)(reg & ~3)) >> (8 * (reg & 3));
78
}
79 80
unsigned ctu_can_fd_read16(struct ctucanfd_priv *priv, enum ctu_can_fd_can_registers reg) {
    return priv->read_reg(priv, (enum ctu_can_fd_can_registers)(reg & ~1)) >> (8 * (reg & 1));
81 82
}
/*
83
void ctu_can_fd_write8(struct ctucanfd_priv *priv, enum ctu_can_fd_can_registers reg, uint8_t val) {
84 85
    iowrite8(val, (uint8_t*)priv->mem_base + reg);
}
86
void ctu_can_fd_write16(struct ctucanfd_priv *priv, enum ctu_can_fd_can_registers reg, uint16_t val) {
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    iowrite16(val, (uint8_t*)priv->mem_base + reg);
}*/

struct ctucanfd_priv* ctucanfd_init(uint32_t addr)
{
    mem_open();
    volatile void * const base = mem_map(addr, CANFD_ADDR_RANGE);

    struct ctucanfd_priv *priv = new ctucanfd_priv;
    memset(priv, 0, sizeof(*priv));

    priv->mem_base = base;
    priv->read_reg = ctu_can_fd_read32;
    priv->write_reg = ctu_can_fd_write32;

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