1. creation of device driver dir - loadable with "install.sh" file.

2. creation of udev file loading for smi to load automatically with the correct permissions
3. firmware bugfix - negate 2.4GHZ I/Q ddr data
bug_fixes_integration_tx
David Michaeli 2023-06-04 19:57:15 +00:00
rodzic 79d5ee11af
commit 603d4664ca
21 zmienionych plików z 22537 dodań i 20551 usunięć

Wyświetl plik

@ -0,0 +1,55 @@
cmake_minimum_required(VERSION 3.15)
project(smi_modules VERSION 0.1.0 LANGUAGES C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
# Module info
add_definitions(-D__KERNEL__ -DMODULE)
# Find the kernel release
execute_process(
COMMAND uname -r
OUTPUT_VARIABLE KERNEL_RELEASE
OUTPUT_STRIP_TRAILING_WHITESPACE
)
# Find the headers
find_path(
KERNELHEADERS_DIR
include/linux/user.h
PATHS /usr/src/linux-headers-${KERNEL_RELEASE}
)
message(STATUS "Kernel release: ${KERNEL_RELEASE}")
message(STATUS "Kernel headers: ${KERNELHEADERS_DIR}")
function(compile_module obj)
set(TARGET_NAME ${obj})
add_custom_target(${TARGET_NAME} ALL cp -f ${CMAKE_CURRENT_SOURCE_DIR}/*.c ${CMAKE_CURRENT_SOURCE_DIR}/*.h ${CMAKE_CURRENT_BINARY_DIR}/
COMMAND echo "compiling module ${obj}.ko...")
list(LENGTH ARGN argn_len)
set(i 0)
set(depend_objlist "")
while(i LESS ${argn_len})
list(GET ARGN ${i} argn_value)
set(depend_objlist "${depend_objlist} ${argn_value}.o")
math(EXPR i "${i} + 1")
endwhile()
add_custom_command(TARGET ${TARGET_NAME}
POST_BUILD
COMMAND cp Makefile Makefile.bak
COMMAND echo "obj-m += ${obj}.o" > ${CMAKE_CURRENT_BINARY_DIR}/Makefile
COMMAND echo "MY_CFLAGS += -g -DDEBUG" >> ${CMAKE_CURRENT_BINARY_DIR}/Makefile
COMMAND echo "ccflags-y += -g -DDEBUG" >> ${CMAKE_CURRENT_BINARY_DIR}/Makefile
COMMAND echo "CC += -g -DDEBUG" >> ${CMAKE_CURRENT_BINARY_DIR}/Makefile
COMMAND echo "${obj}-objs:=${depend_objlist}" >> ${CMAKE_CURRENT_BINARY_DIR}/Makefile
COMMAND make -C ${KERNELHEADERS_DIR} M=${CMAKE_CURRENT_BINARY_DIR} modules EXTRA_CFLAGS="-g"
COMMAND cp Makefile Makefile.op
COMMAND cp Makefile.bak Makefile
)
endfunction()
#compile_module(bcm2835_smi)
compile_module(smi_stream_dev)
#compile_module(bcm2835_smi_dev)

2
driver/README.md 100644
Wyświetl plik

@ -0,0 +1,2 @@
# README
TODO...

Wyświetl plik

@ -0,0 +1,391 @@
/**
* Declarations and definitions for Broadcom's Secondary Memory Interface
*
* Written by Luke Wren <luke@raspberrypi.org>
* Copyright (c) 2015, Raspberry Pi (Trading) Ltd.
* Copyright (c) 2010-2012 Broadcom. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer,
* without modification.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names of the above-listed copyright holders may not be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* ALTERNATIVELY, this software may be distributed under the terms of the
* GNU General Public License ("GPL") version 2, as published by the Free
* Software Foundation.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef BCM2835_SMI_H
#define BCM2835_SMI_H
#include <linux/ioctl.h>
#ifndef __KERNEL__
#include <stdint.h>
#include <stdbool.h>
#endif
#define BCM2835_SMI_IOC_MAGIC 0x1
#define BCM2835_SMI_INVALID_HANDLE (~0)
/* IOCTLs 0x100...0x1ff are not device-specific - we can use them */
#define BCM2835_SMI_IOC_GET_SETTINGS _IO(BCM2835_SMI_IOC_MAGIC, 0)
#define BCM2835_SMI_IOC_WRITE_SETTINGS _IO(BCM2835_SMI_IOC_MAGIC, 1)
#define BCM2835_SMI_IOC_ADDRESS _IO(BCM2835_SMI_IOC_MAGIC, 2)
#define BCM2835_SMI_IOC_MAX 2
#define SMI_WIDTH_8BIT 0
#define SMI_WIDTH_16BIT 1
#define SMI_WIDTH_9BIT 2
#define SMI_WIDTH_18BIT 3
/* max number of bytes where DMA will not be used */
#define DMA_THRESHOLD_BYTES 128
#define DMA_BOUNCE_BUFFER_SIZE (1024 * 1024 / 2)
#define DMA_BOUNCE_BUFFER_COUNT 3
struct smi_settings {
int data_width;
/* Whether or not to pack multiple SMI transfers into a
single 32 bit FIFO word */
bool pack_data;
/* Timing for reads (writes the same but for WE)
*
* OE ----------+ +--------------------
* | |
* +----------+
* SD -<==============================>-----------
* SA -<=========================================>-
* <-setup-> <-strobe -> <-hold -> <- pace ->
*/
int read_setup_time;
int read_hold_time;
int read_pace_time;
int read_strobe_time;
int write_setup_time;
int write_hold_time;
int write_pace_time;
int write_strobe_time;
bool dma_enable; /* DREQs */
bool dma_passthrough_enable; /* External DREQs */
int dma_read_thresh;
int dma_write_thresh;
int dma_panic_read_thresh;
int dma_panic_write_thresh;
};
/****************************************************************************
*
* Declare exported SMI functions
*
***************************************************************************/
#ifdef __KERNEL__
#include <linux/dmaengine.h> /* for enum dma_transfer_direction */
#include <linux/of.h>
#include <linux/semaphore.h>
struct bcm2835_smi_instance;
struct bcm2835_smi_bounce_info {
struct semaphore callback_sem;
void *buffer[DMA_BOUNCE_BUFFER_COUNT];
dma_addr_t phys[DMA_BOUNCE_BUFFER_COUNT];
struct scatterlist sgl[DMA_BOUNCE_BUFFER_COUNT];
};
void bcm2835_smi_set_regs_from_settings(struct bcm2835_smi_instance *);
struct smi_settings *bcm2835_smi_get_settings_from_regs(
struct bcm2835_smi_instance *inst);
void bcm2835_smi_write_buf(
struct bcm2835_smi_instance *inst,
const void *buf,
size_t n_bytes);
void bcm2835_smi_read_buf(
struct bcm2835_smi_instance *inst,
void *buf,
size_t n_bytes);
void bcm2835_smi_set_address(struct bcm2835_smi_instance *inst,
unsigned int address);
ssize_t bcm2835_smi_user_dma(
struct bcm2835_smi_instance *inst,
enum dma_transfer_direction dma_dir,
char __user *user_ptr,
size_t count,
struct bcm2835_smi_bounce_info **bounce);
struct bcm2835_smi_instance *bcm2835_smi_get(struct device_node *node);
#endif /* __KERNEL__ */
/****************************************************************
*
* Implementation-only declarations
*
****************************************************************/
#ifdef BCM2835_SMI_IMPLEMENTATION
/* Clock manager registers for SMI clock: */
#define CM_SMI_BASE_ADDRESS ((BCM2708_PERI_BASE) + 0x1010b0)
/* Clock manager "password" to protect registers from spurious writes */
#define CM_PWD (0x5a << 24)
#define CM_SMI_CTL 0x00
#define CM_SMI_DIV 0x04
#define CM_SMI_CTL_FLIP (1 << 8)
#define CM_SMI_CTL_BUSY (1 << 7)
#define CM_SMI_CTL_KILL (1 << 5)
#define CM_SMI_CTL_ENAB (1 << 4)
#define CM_SMI_CTL_SRC_MASK (0xf)
#define CM_SMI_CTL_SRC_OFFS (0)
#define CM_SMI_DIV_DIVI_MASK (0xf << 12)
#define CM_SMI_DIV_DIVI_OFFS (12)
#define CM_SMI_DIV_DIVF_MASK (0xff << 4)
#define CM_SMI_DIV_DIVF_OFFS (4)
/* SMI register mapping:*/
#define SMI_BASE_ADDRESS ((BCM2708_PERI_BASE) + 0x600000)
#define SMICS 0x00 /* control + status register */
#define SMIL 0x04 /* length/count (n external txfers) */
#define SMIA 0x08 /* address register */
#define SMID 0x0c /* data register */
#define SMIDSR0 0x10 /* device 0 read settings */
#define SMIDSW0 0x14 /* device 0 write settings */
#define SMIDSR1 0x18 /* device 1 read settings */
#define SMIDSW1 0x1c /* device 1 write settings */
#define SMIDSR2 0x20 /* device 2 read settings */
#define SMIDSW2 0x24 /* device 2 write settings */
#define SMIDSR3 0x28 /* device 3 read settings */
#define SMIDSW3 0x2c /* device 3 write settings */
#define SMIDC 0x30 /* DMA control registers */
#define SMIDCS 0x34 /* direct control/status register */
#define SMIDA 0x38 /* direct address register */
#define SMIDD 0x3c /* direct data registers */
#define SMIFD 0x40 /* FIFO debug register */
/* Control and Status register bits:
* SMICS_RXF : RX fifo full: 1 when RX fifo is full
* SMICS_TXE : TX fifo empty: 1 when empty.
* SMICS_RXD : RX fifo contains data: 1 when there is data.
* SMICS_TXD : TX fifo can accept data: 1 when true.
* SMICS_RXR : RX fifo needs reading: 1 when fifo more than 3/4 full, or
* when "DONE" and fifo not emptied.
* SMICS_TXW : TX fifo needs writing: 1 when less than 1/4 full.
* SMICS_AFERR : AXI FIFO error: 1 when fifo read when empty or written
* when full. Write 1 to clear.
* SMICS_EDREQ : 1 when external DREQ received.
* SMICS_PXLDAT : Pixel data: write 1 to enable pixel transfer modes.
* SMICS_SETERR : 1 if there was an error writing to setup regs (e.g.
* tx was in progress). Write 1 to clear.
* SMICS_PVMODE : Set to 1 to enable pixel valve mode.
* SMICS_INTR : Set to 1 to enable interrupt on RX.
* SMICS_INTT : Set to 1 to enable interrupt on TX.
* SMICS_INTD : Set to 1 to enable interrupt on DONE condition.
* SMICS_TEEN : Tear effect mode enabled: Programmed transfers will wait
* for a TE trigger before writing.
* SMICS_PAD1 : Padding settings for external transfers. For writes: the
* number of bytes initially written to the TX fifo that
* SMICS_PAD0 : should be ignored. For reads: the number of bytes that will
* be read before the data, and should be dropped.
* SMICS_WRITE : Transfer direction: 1 = write to external device, 0 = read
* SMICS_CLEAR : Write 1 to clear the FIFOs.
* SMICS_START : Write 1 to start the programmed transfer.
* SMICS_ACTIVE : Reads as 1 when a programmed transfer is underway.
* SMICS_DONE : Reads as 1 when transfer finished. For RX, not set until
* FIFO emptied.
* SMICS_ENABLE : Set to 1 to enable the SMI peripheral, 0 to disable.
*/
#define SMICS_RXF (1 << 31)
#define SMICS_TXE (1 << 30)
#define SMICS_RXD (1 << 29)
#define SMICS_TXD (1 << 28)
#define SMICS_RXR (1 << 27)
#define SMICS_TXW (1 << 26)
#define SMICS_AFERR (1 << 25)
#define SMICS_EDREQ (1 << 15)
#define SMICS_PXLDAT (1 << 14)
#define SMICS_SETERR (1 << 13)
#define SMICS_PVMODE (1 << 12)
#define SMICS_INTR (1 << 11)
#define SMICS_INTT (1 << 10)
#define SMICS_INTD (1 << 9)
#define SMICS_TEEN (1 << 8)
#define SMICS_PAD1 (1 << 7)
#define SMICS_PAD0 (1 << 6)
#define SMICS_WRITE (1 << 5)
#define SMICS_CLEAR (1 << 4)
#define SMICS_START (1 << 3)
#define SMICS_ACTIVE (1 << 2)
#define SMICS_DONE (1 << 1)
#define SMICS_ENABLE (1 << 0)
/* Address register bits: */
#define SMIA_DEVICE_MASK ((1 << 9) | (1 << 8))
#define SMIA_DEVICE_OFFS (8)
#define SMIA_ADDR_MASK (0x3f) /* bits 5 -> 0 */
#define SMIA_ADDR_OFFS (0)
/* DMA control register bits:
* SMIDC_DMAEN : DMA enable: set 1: DMA requests will be issued.
* SMIDC_DMAP : DMA passthrough: when set to 0, top two data pins are used by
* SMI as usual. When set to 1, the top two pins are used for
* external DREQs: pin 16 read request, 17 write.
* SMIDC_PANIC* : Threshold at which DMA will panic during read/write.
* SMIDC_REQ* : Threshold at which DMA will generate a DREQ.
*/
#define SMIDC_DMAEN (1 << 28)
#define SMIDC_DMAP (1 << 24)
#define SMIDC_PANICR_MASK (0x3f << 18)
#define SMIDC_PANICR_OFFS (18)
#define SMIDC_PANICW_MASK (0x3f << 12)
#define SMIDC_PANICW_OFFS (12)
#define SMIDC_REQR_MASK (0x3f << 6)
#define SMIDC_REQR_OFFS (6)
#define SMIDC_REQW_MASK (0x3f)
#define SMIDC_REQW_OFFS (0)
/* Device settings register bits: same for all 4 (or 3?) device register sets.
* Device read settings:
* SMIDSR_RWIDTH : Read transfer width. 00 = 8bit, 01 = 16bit,
* 10 = 18bit, 11 = 9bit.
* SMIDSR_RSETUP : Read setup time: number of core cycles between chip
* select/address and read strobe. Min 1, max 64.
* SMIDSR_MODE68 : 1 for System 68 mode (i.e. enable + direction pins,
* rather than OE + WE pin)
* SMIDSR_FSETUP : If set to 1, setup time only applies to first
* transfer after address change.
* SMIDSR_RHOLD : Number of core cycles between read strobe going
* inactive and CS/address going inactive. Min 1, max 64
* SMIDSR_RPACEALL : When set to 1, this device's RPACE value will always
* be used for the next transaction, even if it is not
* to this device.
* SMIDSR_RPACE : Number of core cycles spent waiting between CS
* deassert and start of next transfer. Min 1, max 128
* SMIDSR_RDREQ : 1 = use external DMA request on SD16 to pace reads
* from device. Must also set DMAP in SMICS.
* SMIDSR_RSTROBE : Number of cycles to assert the read strobe.
* min 1, max 128.
*/
#define SMIDSR_RWIDTH_MASK ((1<<31)|(1<<30))
#define SMIDSR_RWIDTH_OFFS (30)
#define SMIDSR_RSETUP_MASK (0x3f << 24)
#define SMIDSR_RSETUP_OFFS (24)
#define SMIDSR_MODE68 (1 << 23)
#define SMIDSR_FSETUP (1 << 22)
#define SMIDSR_RHOLD_MASK (0x3f << 16)
#define SMIDSR_RHOLD_OFFS (16)
#define SMIDSR_RPACEALL (1 << 15)
#define SMIDSR_RPACE_MASK (0x7f << 8)
#define SMIDSR_RPACE_OFFS (8)
#define SMIDSR_RDREQ (1 << 7)
#define SMIDSR_RSTROBE_MASK (0x7f)
#define SMIDSR_RSTROBE_OFFS (0)
/* Device write settings:
* SMIDSW_WWIDTH : Write transfer width. 00 = 8bit, 01 = 16bit,
* 10= 18bit, 11 = 9bit.
* SMIDSW_WSETUP : Number of cycles between CS assert and write strobe.
* Min 1, max 64.
* SMIDSW_WFORMAT : Pixel format of input. 0 = 16bit RGB 565,
* 1 = 32bit RGBA 8888
* SMIDSW_WSWAP : 1 = swap pixel data bits. (Use with SMICS_PXLDAT)
* SMIDSW_WHOLD : Time between WE deassert and CS deassert. 1 to 64
* SMIDSW_WPACEALL : 1: this device's WPACE will be used for the next
* transfer, regardless of that transfer's device.
* SMIDSW_WPACE : Cycles between CS deassert and next CS assert.
* Min 1, max 128
* SMIDSW_WDREQ : Use external DREQ on pin 17 to pace writes. DMAP must
* be set in SMICS.
* SMIDSW_WSTROBE : Number of cycles to assert the write strobe.
* Min 1, max 128
*/
#define SMIDSW_WWIDTH_MASK ((1<<31)|(1<<30))
#define SMIDSW_WWIDTH_OFFS (30)
#define SMIDSW_WSETUP_MASK (0x3f << 24)
#define SMIDSW_WSETUP_OFFS (24)
#define SMIDSW_WFORMAT (1 << 23)
#define SMIDSW_WSWAP (1 << 22)
#define SMIDSW_WHOLD_MASK (0x3f << 16)
#define SMIDSW_WHOLD_OFFS (16)
#define SMIDSW_WPACEALL (1 << 15)
#define SMIDSW_WPACE_MASK (0x7f << 8)
#define SMIDSW_WPACE_OFFS (8)
#define SMIDSW_WDREQ (1 << 7)
#define SMIDSW_WSTROBE_MASK (0x7f)
#define SMIDSW_WSTROBE_OFFS (0)
/* Direct transfer control + status register
* SMIDCS_WRITE : Direction of transfer: 1 -> write, 0 -> read
* SMIDCS_DONE : 1 when a transfer has finished. Write 1 to clear.
* SMIDCS_START : Write 1 to start a transfer, if one is not already underway.
* SMIDCE_ENABLE: Write 1 to enable SMI in direct mode.
*/
#define SMIDCS_WRITE (1 << 3)
#define SMIDCS_DONE (1 << 2)
#define SMIDCS_START (1 << 1)
#define SMIDCS_ENABLE (1 << 0)
/* Direct transfer address register
* SMIDA_DEVICE : Indicates which of the device settings banks should be used.
* SMIDA_ADDR : The value to be asserted on the address pins.
*/
#define SMIDA_DEVICE_MASK ((1<<9)|(1<<8))
#define SMIDA_DEVICE_OFFS (8)
#define SMIDA_ADDR_MASK (0x3f)
#define SMIDA_ADDR_OFFS (0)
/* FIFO debug register
* SMIFD_FLVL : The high-tide mark of FIFO count during the most recent txfer
* SMIFD_FCNT : The current FIFO count.
*/
#define SMIFD_FLVL_MASK (0x3f << 8)
#define SMIFD_FLVL_OFFS (8)
#define SMIFD_FCNT_MASK (0x3f)
#define SMIFD_FCNT_OFFS (0)
#endif /* BCM2835_SMI_IMPLEMENTATION */
#endif /* BCM2835_SMI_H */

114
driver/install.sh 100755
Wyświetl plik

@ -0,0 +1,114 @@
#! /bin/bash
ROOT_DIR=`pwd`
RED='\033[0;31m'
GREEN='\033[1;32m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
ERROR="0"
BUILD_DIR="build"
[ $(id -u) = 0 ] && printf "${RED}Please do not run this script as root${NC}\n" && exit 100
## FUNCTIONS
install() {
printf "${GREEN}Installation started...${NC}\n"
printf "\n[ 1 ] ${GREEN}Updating kernel headers and needed software${NC}\n"
sudo apt-get update
sudo apt-get -y install raspberrypi-kernel-headers module-assistant pkg-config libncurses5-dev cmake git
printf "\n[ 2 ] ${GREEN}Compiling module${NC}\n"
if [ -d "$BUILD_DIR" ]; then
echo "Subdirectory '$BUILD_DIR' exists. Deleting its contents..."
rm -rf "$BUILD_DIR"/*
else
echo "Subdirectory '$BUILD_DIR' does not exist. Creating it..."
mkdir "$BUILD_DIR"
fi
# enter build dir and build the ko file
cd "$BUILD_DIR"
cmake ../
make
# find the location to install
output_dir=$(find "/lib/modules" -type f -name "bcm2835_smi_dev*" -exec dirname {} \;)
# Check if the output is empty
if [ -z "$output_dir" ]; then
printf "${RED}Error: module 'bcm2835_smi_dev' couldn't be found.${NC}\n"
# suspicious - why doen't it exist? check of the base module bcm2835_smi exists
exit 100
fi
printf "\n[ 3 ] ${GREEN}Installing into '${output_dir}'${NC}\n"
xz -z smi_stream_dev.ko -c > smi_stream_dev.ko.xz
sudo cp smi_stream_dev.ko.xz ${output_dir}/
printf "\n[ 4 ] ${GREEN}Updating 'depmod'${NC}\n"
sudo depmod -a
printf "\n[ 5 ] ${GREEN}Blacklisting original bcm2835_smi_dev module${NC}\n"
echo "# blacklist the broadcom default smi module to replace with smi_stream_dev" | sudo tee "/etc/modprobe.d/blacklist-bcm_smi.conf" > /dev/null
echo "blacklist bcm2835_smi_dev" | sudo tee -a "/etc/modprobe.d/blacklist-bcm_smi.conf" > /dev/null
printf "\n[ 6 ] ${GREEN}Adding systemd configuration${NC}\n"
echo "# load SMI stream driver on startup" | sudo tee "/etc/modules-load.d/smi_stream_mod.conf" > /dev/null
echo "smi_stream_dev" | sudo tee -a "/etc/modules-load.d/smi_stream_mod.conf" > /dev/null
printf "${GREEN}Installation completed.${NC}\n"
}
uninstall() {
printf "${GREEN}Uninstalling started...${NC}\n"
# find the location of the older installed module
output_dir=$(find "/lib/modules" -type f -name "smi_stream_dev*" -exec dirname {} \;)
if [ -z "$output_dir" ]; then
printf "${CYAN}Warning: module 'smi_stream_dev' is not installed in the system${NC}\n"
sudo depmod -a
exit 0
fi
printf "\n[ 1 ] ${GREEN}Uninstalling from '${output_dir}'${NC}\n"
sudo rm ${output_dir}/smi_stream_dev.ko.xz
printf "\n[ 2 ] ${GREEN}Updating 'depmod'${NC}\n"
sudo depmod -a
printf "\n[ 3 ] ${GREEN}Removing the blacklist on the legacy smi device${NC}\n"
if [ -f "/etc/modprobe.d/blacklist-bcm_smi.conf" ]; then
sudo rm "/etc/modprobe.d/blacklist-bcm_smi.conf"
fi
printf "\n[ 4 ] ${GREEN}Removing device driver loading on start${NC}\n"
if [ -f "/etc/modules-load.d/smi_stream_mod.conf" ]; then
sudo rm "/etc/modules-load.d/smi_stream_mod.conf"
fi
printf "${GREEN}Uninstallation completed.${NC}\n"
}
## FLOW
printf "${GREEN}CaribouLite Device Driver Install / Uninstall${NC}\n"
printf "${GREEN}=============================================${NC}\n\n"
if [ "$1" == "install" ]; then
install
exit 0
elif [ "$1" == "uninstall" ]; then
uninstall
exit 0
else
printf "${CYAN}Usage: $0 [install|uninstall]${NC}\n"
exit 1
fi
## Say that restart is needed!
print "${GREEN}Now the RPI needs to be restarted...${NC}\n"

Wyświetl plik

@ -0,0 +1,166 @@
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sched.h>
#include <errno.h>
#include <pthread.h>
#include "../caribou_smi.h"
#include "bcm2835_smi.h"
#define SMI_STREAM_IOC_GET_NATIVE_BUF_SIZE _IO(BCM2835_SMI_IOC_MAGIC, 3)
#define SMI_STREAM_IOC_SET_NON_BLOCK_READ _IO(BCM2835_SMI_IOC_MAGIC, 4)
#define SMI_STREAM_IOC_SET_NON_BLOCK_WRITE _IO(BCM2835_SMI_IOC_MAGIC, 5)
#define SMI_STREAM_IOC_SET_STREAM_STATUS _IO(BCM2835_SMI_IOC_MAGIC, 6)
static void setup_settings (struct smi_settings *settings)
{
settings->read_setup_time = 0;
settings->read_strobe_time = 5;
settings->read_hold_time = 0;
settings->read_pace_time = 0;
settings->write_setup_time = 0;
settings->write_hold_time = 0;
settings->write_pace_time = 0;
settings->write_strobe_time = 4;
settings->data_width = SMI_WIDTH_8BIT;
settings->dma_enable = 1;
settings->pack_data = 1;
settings->dma_passthrough_enable = 1;
}
pthread_t tid;
int fd = -1;
size_t native_batch_length_bytes = 0;
int thread_running = 0;
void* read_thread(void *arg)
{
fd_set set;
int rv;
int timeout_num_millisec = 500;
uint8_t *buffer = malloc(native_batch_length_bytes);
int size_of_buf = native_batch_length_bytes;
while (thread_running)
{
while (1)
{
struct timeval timeout = {0};
FD_ZERO(&set); // clear the set mask
FD_SET(fd, &set); // add our file descriptor to the set - and only it
int num_sec = timeout_num_millisec / 1000;
timeout.tv_sec = num_sec;
timeout.tv_usec = (timeout_num_millisec - num_sec*1000) * 1000;
rv = select(fd + 1, &set, NULL, NULL, &timeout);
if(rv == -1)
{
int error = errno;
switch(error)
{
case EINTR: // A signal was caught.
continue;
case EBADF: // An invalid file descriptor was given in one of the sets.
// (Perhaps a file descriptor that was already closed, or one on which an error has occurred.)
case EINVAL: // nfds is negative or the value contained within timeout is invalid.
case ENOMEM: // unable to allocate memory for internal tables.
default: goto exit;
};
}
else if (rv == 0)
{
printf("Read poll timeout\n");
break;
}
else if (FD_ISSET(fd, &set))
{
int num_read = read(fd, buffer, size_of_buf);
printf("Read %d bytes\n", num_read);
break;
}
}
}
exit:
free(buffer);
return NULL;
}
int main()
{
char smi_file[] = "/dev/smi";
struct smi_settings settings = {0};
fd = open(smi_file, O_RDWR);
if (fd < 0)
{
printf("can't open smi driver file '%s'\n", smi_file);
return -1;
}
// Get the current settings
int ret = ioctl(fd, BCM2835_SMI_IOC_GET_SETTINGS, &settings);
if (ret != 0)
{
printf("failed reading ioctl from smi fd (settings)\n");
close (fd);
return -1;
}
// apply the new settings
setup_settings(&settings);
ret = ioctl(fd, BCM2835_SMI_IOC_WRITE_SETTINGS, &settings);
if (ret != 0)
{
printf("failed writing ioctl to the smi fd (settings)\n");
close (fd);
return -1;
}
// set the address to idle
ret = ioctl(fd, BCM2835_SMI_IOC_ADDRESS, caribou_smi_address_idle);
if (ret != 0)
{
printf("failed setting smi address (idle / %d) to device\n", caribou_smi_address_idle);
close (fd);
return -1;
}
// get the native batch length in bytes
ret = ioctl(fd, SMI_STREAM_IOC_GET_NATIVE_BUF_SIZE, &native_batch_length_bytes);
if (ret != 0)
{
printf("failed reading native batch length, setting default\n");
native_batch_length_bytes = (1024)*(1024)/2;
}
printf("Native batch size: %u\n", native_batch_length_bytes);
// start streaming data
ret = ioctl(fd, SMI_STREAM_IOC_SET_STREAM_STATUS, 1);
// start the reader thread
thread_running = 1;
int err = pthread_create(&tid, NULL, &read_thread, NULL);
if (err != 0)
{
printf("\ncan't create thread :[%s]", strerror(err));
}
getchar();
thread_running = 0;
pthread_join(tid, NULL);
ret = ioctl(fd, SMI_STREAM_IOC_SET_STREAM_STATUS, 0);
close (fd);
return 0;
}

Plik diff jest za duży Load Diff

Wyświetl plik

@ -0,0 +1,103 @@
/**
* Declarations and definitions for Broadcom's Secondary Memory Interface
*
* Written by David Michaeli <cariboulabs.co@gmail.com>
* Copyright (c) 2021, CaribouLabs.co
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer,
* without modification.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names of the above-listed copyright holders may not be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* ALTERNATIVELY, this software may be distributed under the terms of the
* GNU General Public License ("GPL") version 2, as published by the Free
* Software Foundation.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SMI_STREAM_DEV_H_
#define _SMI_STREAM_DEV_H_
#include <linux/ioctl.h>
#ifndef __KERNEL__
#include <stdint.h>
#include <stdbool.h>
#else
#define BCM2835_SMI_IMPLEMENTATION
#include <linux/broadcom/bcm2835_smi.h>
#endif
#define DEVICE_NAME "smi-stream-dev"
#define DRIVER_NAME "smi-stream-dev"
#define DEVICE_MINOR 0
typedef enum
{
smi_stream_dir_smi_to_device = 0, // device data-bus is highZ (TX)
smi_stream_dir_device_to_smi = 1, // device data-bus is push-pull (RX)
} smi_stream_direction_en;
typedef enum
{
smi_stream_channel_0 = 0,
smi_stream_channel_1 = 1,
smi_stream_channel_max,
} smi_stream_channel_en;
typedef enum
{
smi_stream_idle = 0,
smi_stream_rx_channel_0 = 1,
smi_stream_rx_channel_1 = 2,
smi_stream_tx_channel = 3,
} smi_stream_state_en;
#ifdef __KERNEL__
struct bcm2835_smi_instance {
struct device *dev;
struct smi_settings settings;
__iomem void *smi_regs_ptr;
dma_addr_t smi_regs_busaddr;
struct dma_chan *dma_chan;
struct dma_slave_config dma_config;
struct bcm2835_smi_bounce_info bounce;
struct scatterlist buffer_sgl;
struct clk *clk;
/* Sometimes we are called into in an atomic context (e.g. by
JFFS2 + MTD) so we can't use a mutex */
spinlock_t transaction_lock;
};
#endif // __KERNEL__
// Expansion of ioctls
#define SMI_STREAM_IOC_GET_NATIVE_BUF_SIZE _IO(BCM2835_SMI_IOC_MAGIC,(BCM2835_SMI_IOC_MAX+1))
#define SMI_STREAM_IOC_SET_STREAM_STATUS _IO(BCM2835_SMI_IOC_MAGIC,(BCM2835_SMI_IOC_MAX+2))
#define SMI_STREAM_IOC_SET_STREAM_IN_CHANNEL _IO(BCM2835_SMI_IOC_MAGIC,(BCM2835_SMI_IOC_MAX+3))
#endif /* _SMI_STREAM_DEV_H_ */

Wyświetl plik

@ -5,7 +5,7 @@ pcf_file = ./io.pcf
top.bin:
yosys -p 'synth_ice40 -top top -json $(filename).json -blif $(filename).blif' -p 'ice40_opt' -p 'fsm_opt' $(filename).v
#nextpnr-ice40 --lp1k --package qn84 --json $(filename).json --pcf $(pcf_file) --asc $(filename).asc
nextpnr-ice40 --lp1k --package qn84 --json $(filename).json --pcf $(pcf_file) --asc $(filename).asc --freq 64 --parallel-refine --opt-timing
nextpnr-ice40 --lp1k --package qn84 --json $(filename).json --pcf $(pcf_file) --asc $(filename).asc --freq 80 --parallel-refine --opt-timing --placer-heap-timingweight 10
#nextpnr-ice40 --json blinky.json --pcf blinky.pcf --asc blinky.asc --gui
icepack $(filename).asc $(filename).bin

BIN
firmware/top.bin 100644

Plik binarny nie jest wyświetlany.

Wyświetl plik

@ -1,4 +1,4 @@
# Generated by Yosys 0.26+1 (git sha1 b1a011138, gcc 10.2.1-6 -fPIC -Os)
# Generated by Yosys 0.29+42 (git sha1 43b807fe6, gcc 10.2.1-6 -fPIC -Os)
.model top
.inputs i_glob_clock i_rst_b i_iq_rx_09_p i_iq_rx_24_n i_iq_rx_clk_p i_config[0] i_config[1] i_config[2] i_config[3] i_button io_pmod[0] io_pmod[1] io_pmod[2] io_pmod[3] io_pmod[4] io_pmod[5] io_pmod[6] io_pmod[7] i_smi_a2 i_smi_a3 i_smi_soe_se i_smi_swe_srw io_smi_data[0] io_smi_data[1] io_smi_data[2] io_smi_data[3] io_smi_data[4] io_smi_data[5] io_smi_data[6] io_smi_data[7] i_mosi i_sck i_ss

Wyświetl plik

@ -1,5 +1,5 @@
{
"creator": "Yosys 0.26+1 (git sha1 b1a011138, gcc 10.2.1-6 -fPIC -Os)",
"creator": "Yosys 0.29+42 (git sha1 43b807fe6, gcc 10.2.1-6 -fPIC -Os)",
"modules": {
"ICESTORM_LC": {
"attributes": {
@ -6754,7 +6754,7 @@
"DST_WIDTH": "00000000000000000000000000010000",
"EDGE_EN": "1",
"EDGE_POL": "1",
"FULL": "0",
"FULL": "1",
"SRC_DST_PEN": "0",
"SRC_DST_POL": "0",
"SRC_WIDTH": "00000000000000000000000000000001",
@ -7320,7 +7320,7 @@
"DST_WIDTH": "00000000000000000000000000010000",
"EDGE_EN": "1",
"EDGE_POL": "1",
"FULL": "0",
"FULL": "1",
"SRC_DST_PEN": "0",
"SRC_DST_POL": "0",
"SRC_WIDTH": "00000000000000000000000000000001",
@ -7956,7 +7956,7 @@
"DST_WIDTH": "00000000000000000000000000010000",
"EDGE_EN": "1",
"EDGE_POL": "1",
"FULL": "0",
"FULL": "1",
"SRC_DST_PEN": "0",
"SRC_DST_POL": "0",
"SRC_WIDTH": "00000000000000000000000000000001",
@ -8557,7 +8557,7 @@
"DST_WIDTH": "00000000000000000000000000010000",
"EDGE_EN": "1",
"EDGE_POL": "1",
"FULL": "0",
"FULL": "1",
"SRC_DST_PEN": "0",
"SRC_DST_POL": "0",
"SRC_WIDTH": "00000000000000000000000000000001",

Wyświetl plik

@ -97,11 +97,13 @@ make
sudo make install
sudo ldconfig
printf "${CYAN}3. SMI kernel module...${NC}\n"
cd $ROOT_DIR/software/libcariboulite/src/caribou_smi/kernel
mkdir -p build && cd build
cmake ../
make
printf "${CYAN}3. SMI kernel module & udev...${NC}\n"
cd $ROOT_DIR/driver
./install.sh
cd ..
cd udev
./install.sh
cd ..
printf "${CYAN}4. Main software...${NC}\n"
cd $ROOT_DIR
@ -145,9 +147,18 @@ else
ERROR="1"
fi
## UDEV rules
# Still the /dev/mem problem. Un-restricting the CONFIG_STRICT_DEVMEM kernel config option doesn't
# help. Neither adding "pi" to the kmem, dialout and mem groups. pigpiod may be the last resort.
printf "${GREEN}4. SPI1-3CS Configuration... "
DtparamSPI=`cat /boot/config.txt | grep "dtoverlay=spi1-3cs" | xargs | cut -d\= -f1`
if [ "$DtparamSPI" = "dtoverlay" ]; then
printf "${CYAN}OK :)${NC}\n"
else
printf "${RED}Warning${NC}\n"
printf "${RED}To communicate with CaribouLite Modem, FPGA, etc, SPI1 (AUX) with 3CS needs to be to be enabled${NC}\n"
printf "${RED}Please add the following to the '/boot/config.txt' file: 'dtoverlay=spi1-3cs'${NC}\n"
ERROR="1"
fi
if [ "$ERROR" = "1" ]; then
printf "\n[ 7 ] ${RED}Installation errors occured.${NC}\n\n\n"

Wyświetl plik

@ -88,13 +88,13 @@ static int caribou_smi_get_smi_settings(caribou_smi_st *dev, struct smi_settings
//=========================================================================
static int caribou_smi_setup_settings (caribou_smi_st* dev, struct smi_settings *settings, bool print)
{
settings->read_setup_time = 1;
settings->read_strobe_time = 4;
settings->read_setup_time = 0;
settings->read_strobe_time = 5;
settings->read_hold_time = 0;
settings->read_pace_time = 0;
settings->write_setup_time = 1;
settings->write_strobe_time = 4;
settings->write_setup_time = 0;
settings->write_strobe_time = 5;
settings->write_hold_time = 0;
settings->write_pace_time = 0;
@ -225,16 +225,18 @@ static int caribou_smi_find_buffer_offset(caribou_smi_st* dev, uint8_t *buffer,
if (dev->debug_mode == caribou_smi_none)
{
for (offs = 0; offs<(len-(CARIBOU_SMI_BYTES_PER_SAMPLE*3)); offs++)
for (offs = 0; offs<(len-(CARIBOU_SMI_BYTES_PER_SAMPLE*4)); offs++)
{
uint32_t s1 = *((uint32_t*)(&buffer[offs]));
uint32_t s2 = *((uint32_t*)(&buffer[offs+4]));
uint32_t s3 = *((uint32_t*)(&buffer[offs+8]));
uint32_t s4 = *((uint32_t*)(&buffer[offs+12]));
//printf("%d => %08X\n", offs, s);
if ((s1 & 0xC001C000) == 0x80004000 &&
(s2 & 0xC001C000) == 0x80004000 &&
(s3 & 0xC001C000) == 0x80004000)
(s3 & 0xC001C000) == 0x80004000 &&
(s4 & 0xC001C000) == 0x80004000)
{
found = true;
break;
@ -284,6 +286,7 @@ static int caribou_smi_rx_data_analyze(caribou_smi_st* dev,
// find the offset and adjust
offs = caribou_smi_find_buffer_offset(dev, data, data_length);
//printf("OFFSET = %d\n", offs);
if (offs < 0)
{
return -1;
@ -497,11 +500,11 @@ int caribou_smi_init(caribou_smi_st* dev,
// checking the loaded modules
// --------------------------------------------
if (caribou_smi_check_modules(true) < 0)
/*if (caribou_smi_check_modules(true) < 0)
{
ZF_LOGE("Problem reloading SMI kernel modules");
return -1;
}
}*/
// open the smi device file
// --------------------------------------------

Wyświetl plik

@ -371,7 +371,7 @@ static long smi_stream_ioctl(struct file *file, unsigned int cmd, unsigned long
case BCM2835_SMI_IOC_ADDRESS:
{
dev_info(inst->dev, "SMI address set: 0x%02x", (int)arg);
bcm2835_smi_set_address(inst->smi_inst, arg);
//bcm2835_smi_set_address(inst->smi_inst, arg);
break;
}
//-------------------------------

Wyświetl plik

@ -847,6 +847,7 @@ int cariboulite_radio_activate_channel(cariboulite_radio_state_st* radio,
cariboulite_radio_activate_channel(radio, radio->channel_direction, false);
return -1;
}
usleep(10000);
}
//===========================================================
@ -856,6 +857,25 @@ int cariboulite_radio_activate_channel(cariboulite_radio_state_st* radio,
// RX on both channels looks the same
if (radio->channel_direction == cariboulite_channel_dir_rx)
{
at86rf215_iq_interface_config_st modem_iq_config = {
.loopback_enable = 0,
.drv_strength = at86rf215_iq_drive_current_4ma,
.common_mode_voltage = at86rf215_iq_common_mode_v_ieee1596_1v2,
.tx_control_with_iq_if = false,
.radio09_mode = at86rf215_iq_if_mode,
.radio24_mode = at86rf215_iq_if_mode,
.clock_skew = at86rf215_iq_clock_data_skew_4_906ns,
};
at86rf215_setup_iq_if(&radio->sys->modem, &modem_iq_config);
at86rf215_radio_set_state( &radio->sys->modem,
GET_MODEM_CH(radio->type),
at86rf215_radio_state_cmd_rx);
radio->state = at86rf215_radio_state_cmd_rx;
ZF_LOGD("Setup Modem state cmd_rx");
usleep(10000);
// after modem is activated turn on the the smi stream
smi_stream_state_en smi_state = smi_stream_idle;
if (radio->smi_channel_id == caribou_smi_channel_900)
@ -872,12 +892,7 @@ int cariboulite_radio_activate_channel(cariboulite_radio_state_st* radio,
ZF_LOGD("Failed to configure modem with cmd_rx");
return -1;
}
at86rf215_radio_set_state( &radio->sys->modem,
GET_MODEM_CH(radio->type),
at86rf215_radio_state_cmd_rx);
radio->state = at86rf215_radio_state_cmd_rx;
ZF_LOGD("Setup Modem state cmd_rx");
//usleep(30000);
}
//===========================================================

Wyświetl plik

@ -1,5 +1,4 @@
# SMI Devices
KERNEL=="smi", SUBSYSTEM=="bcm2835-smi-dev", MODE="0666"
KERNEL=="smi", SUBSYSTEM=="smi-stream-dev", MODE="0666"
# Other RPI Devices

37
udev/install.sh 100755
Wyświetl plik

@ -0,0 +1,37 @@
#! /bin/bash
## FUNCTIONS
install() {
printf "Installing UDEV rules...\n"
sudo cp 40-cariboulite.rules /etc/udev/rules.d/
sudo udevadm control --reload-rules && udevadm trigger
printf "Installation finished\n"
}
uninstall() {
printf "Uninstalling UDEV rules...\n"
if [ -f "/etc/udev/rules.d/40-cariboulite.rules" ]; then
sudo rm "/etc/udev/rules.d/40-cariboulite.rules"
fi
sudo udevadm control --reload-rules && udevadm trigger
printf "Uninstallation finished\n"
}
## FLOW
printf "${GREEN}CaribouLite UDEV Rules (un)installation${NC}\n"
printf "${GREEN}=======================================${NC}\n\n"
if [ "$1" == "install" ]; then
install
exit 0
elif [ "$1" == "uninstall" ]; then
uninstall
exit 0
else
printf "${CYAN}Usage: $0 [install|uninstall]${NC}\n"
exit 1
fi

Wyświetl plik

@ -1,7 +0,0 @@
#! /bin/bash
# List all udev related files
#dpkg -L udev
cp 40-cariboulite.rules /etc/udev/rules.d/
udevadm control --reload-rules && udevadm trigger