Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
paulh002 committed Jan 6, 2022
1 parent 3f0e8ea commit d9dd8d2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 62 deletions.
18 changes: 12 additions & 6 deletions driver/radioberry.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ sudo cp radioberry.ko /lib/modules/$(uname -r)/kernel/drivers/sdr
#include <linux/firmware.h>
#include <linux/circ_buf.h>
#include <linux/wait.h>
#include <linux/spinlock.h>
#include <linux/mutex.h>
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/gpio/consumer.h>
Expand All @@ -55,12 +55,14 @@ sudo cp radioberry.ko /lib/modules/$(uname -r)/kernel/drivers/sdr
#include <linux/property.h>
#include <linux/interrupt.h>

static struct mutex spi_mutex;

#include "radioberry_rpi.h"
#include "radioberry_ioctl.h"
#include "radioberry_gateware.h"
#include "radioberry_firmware.h"

#define VERSION "0.9"
#define VERSION "93"

static DEFINE_MUTEX(radioberry_mutex);
static wait_queue_head_t rx_sample_queue;
Expand Down Expand Up @@ -126,7 +128,7 @@ ssize_t radioberry_read(struct file *flip, char *buf, size_t count, loff_t *pos)
return count;
}

static int radioberry_write(struct file *flip, const char *buf, size_t count, loff_t *pos) {
static ssize_t radioberry_write(struct file *flip, const char *buf, size_t count, loff_t *pos) {

unsigned char tx_stream[4];

Expand Down Expand Up @@ -192,8 +194,9 @@ static long radioberry_ioctl(struct file *fp, unsigned int cmd, unsigned long ar
//printk(KERN_INFO "Command kernel %2X - %2X - %2X - %2X - %2X - %2X \n", data[0], data[1], data[2], data[3], data[4], data[5]);
if ((data[1] & 0xFE) == 0x00) lnrx = ((data[5] & 0x38) >> 3) + 1;

// tell the gateware the command.
spiXfer(data, data, 6);
mutex_lock(&spi_mutex);
spiXfer(0, data, data, 6); //spi channel 0 // tell the gateware the command.
mutex_unlock(&spi_mutex);

_nrx = lnrx;

Expand All @@ -203,7 +206,7 @@ static long radioberry_ioctl(struct file *fp, unsigned int cmd, unsigned long ar
rb_info_ret.minor = data[5];

rb_info_ret.fpga = data[3] & 0x03;
rb_info_ret.version = 0.9;
rb_info_ret.version = 93;

if (copy_to_user((struct rb_info_arg_t *)arg, &rb_info_ret, sizeof(struct rb_info_arg_t))) return -EACCES;

Expand Down Expand Up @@ -314,6 +317,8 @@ static int __init radioberry_init(void) {
NULL);

printk(KERN_INFO "Radioberry: The interrupt request result is: %d\n", result);

mutex_init(&spi_mutex);

initialize_rpi();
loading_radioberry_gateware(radioberryCharDevice);
Expand All @@ -340,6 +345,7 @@ static void __exit radioberry_exit(void) {
unregister_chrdev(majorNumber, DEVICE_NAME);

mutex_destroy(&radioberry_mutex);
mutex_destroy(&spi_mutex);

deinitialize_rpi();

Expand Down
Binary file modified driver/radioberry.rbf
Binary file not shown.
86 changes: 31 additions & 55 deletions driver/radioberry_firmware.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef __RADIOBERRY_FIRMWARE_H__
#define __RADIOBERRY_FIRMWARE_H__

#include <linux/delay.h>

#define PI_ALT0 4

#define RPI_RX_CLK 6
Expand Down Expand Up @@ -40,14 +38,14 @@
static int initialize_firmware(void);
int rxStream(int nrx, unsigned char stream[]);
void read_iq_sample(int lnrx, int iqs, unsigned char iqdata[]);
int spiXfer(char *txBuf, char *rxBuf, unsigned cnt);
int spiXfer(int spi_channel, char *txBuf, char *rxBuf, unsigned cnt);
int write_iq_sample(unsigned char tx_iqdata[]);

static int initialize_firmware() {

//SPI mode pins
setPinMode(RPI_SPI_CE0, PI_ALT0);
//setPinMode(RPI_SPI_CE1, PI_ALT0);
setPinMode(RPI_SPI_CE1, PI_ALT0);

setPinMode(RPI_SPI_SCLK, PI_ALT0);
setPinMode(RPI_SPI_MISO, PI_ALT0);
Expand All @@ -57,27 +55,22 @@ static int initialize_firmware() {
initialize_gpio_for_output(rpi_io, RPI_RX_CLK);
*rpi_set_io_low = (1<<RPI_RX_CLK); // init pi-rx_clk

initialize_gpio_for_input(rpi_io, 21); // rx iq data
initialize_gpio_for_input(rpi_io, 23); // rx iq data
initialize_gpio_for_input(rpi_io, 20); // rx iq data
initialize_gpio_for_input(rpi_io, 19); // rx iq data
initialize_gpio_for_input(rpi_io, 18); // rx iq data
initialize_gpio_for_input(rpi_io, 16); // rx iq data
initialize_gpio_for_input(rpi_io, 13); // rx iq data
initialize_gpio_for_input(rpi_io, 12); // rx iq data
initialize_gpio_for_input(rpi_io, 5); // rx iq data
initialize_gpio_for_input(rpi_io, 25); // available samples.

//TX IO Init part
initialize_gpio_for_output(rpi_io, RPI_TX_CLK);
*rpi_set_io_low = (1<<RPI_TX_CLK); // init pi-tx_clk
initialize_gpio_for_output(rpi_io, 5); // tx iq data
initialize_gpio_for_output(rpi_io, 12); // tx iq data
initialize_gpio_for_output(rpi_io, 17); // tx iq data
initialize_gpio_for_output(rpi_io, 18); // tx iq data
initialize_gpio_for_input(rpi_io, 7); // check txbuffer.

printk(KERN_INFO "GPIO ready for rx and tx streaming...\n");

return 0;
}

int spiXfer(char *txBuf, char *rxBuf, unsigned cnt){
int spiXfer(int spi_channel, char *txBuf, char *rxBuf, unsigned cnt){

unsigned speed = 9000000;
uint32_t flags = 49155;
Expand All @@ -88,7 +81,7 @@ int spiXfer(char *txBuf, char *rxBuf, unsigned cnt){
uint32_t spiDefaults;
unsigned mode, channel, cspol, cspols;

flags = flags | PI_SPI_FLAGS_CHANNEL(0); // SPI0
flags = flags | PI_SPI_FLAGS_CHANNEL(spi_channel);

channel = PI_SPI_FLAGS_GET_CHANNEL(flags);
mode = PI_SPI_FLAGS_GET_MODE (flags);
Expand Down Expand Up @@ -150,54 +143,37 @@ int rxStream(int nrx, unsigned char stream[]){

void read_iq_sample(int lnrx, int iqs, unsigned char iqdata[]){
uint32_t value = 0;

int i = 0;
for (i = 0; i < 6 ; i++) {

*rpi_set_io_high = (1<<RPI_RX_CLK);
value = *rpi_read_io;

iqdata[i] = (((value >> 16) & 1) << 7);
iqdata[i] |= (((value >> 19) & 1) << 6);
iqdata[i] |= (((value >> 20) & 1) << 5);
iqdata[i] |= (((value >> 21) & 1) << 4);

*rpi_set_io_low = (1<<RPI_RX_CLK);
value = *rpi_read_io;

if (i % 2 == 0) {
*rpi_set_io_high = (1<<RPI_RX_CLK);
value = *rpi_read_io;
} else {
*rpi_set_io_low = (1<<RPI_RX_CLK);
value = *rpi_read_io;
}
iqdata[i] = (((value >> 23) & 1) << 7);
iqdata[i] |= (((value >> 20) & 1) << 6);
iqdata[i] |= (((value >> 19) & 1) << 5);
iqdata[i] |= (((value >> 18) & 1) << 4);
iqdata[i] |= (((value >> 16) & 1) << 3);
iqdata[i] |= (((value >> 19) & 1) << 2);
iqdata[i] |= (((value >> 20) & 1) << 1);
iqdata[i] |= (((value >> 21) & 1));
iqdata[i] |= (((value >> 13) & 1) << 2);
iqdata[i] |= (((value >> 12) & 1) << 1);
iqdata[i] |= (((value >> 5) & 1));
}
}

int write_iq_sample(unsigned char tx_iqdata[]) {

int write_iq_sample(unsigned char tx_iqdata[]){

uint32_t value = 0;
int i = 0;
for (i = 0; i < 4 ; i++) {
if (tx_iqdata[i] & 0x80) *rpi_set_io_high = (1<<17); else *rpi_set_io_low = (1<<17);
if (tx_iqdata[i] & 0x40) *rpi_set_io_high = (1<<5); else *rpi_set_io_low = (1<<5);
if (tx_iqdata[i] & 0x20) *rpi_set_io_high = (1<<18); else *rpi_set_io_low = (1<<18);
if (tx_iqdata[i] & 0x10) *rpi_set_io_high = (1<<12); else *rpi_set_io_low = (1<<12);
*rpi_set_io_high = (1<<RPI_TX_CLK);

if (tx_iqdata[i] & 0x08) *rpi_set_io_high = (1<<17); else *rpi_set_io_low = (1<<17);
if (tx_iqdata[i] & 0x04) *rpi_set_io_high = (1<<5); else *rpi_set_io_low = (1<<5);
if (tx_iqdata[i] & 0x02) *rpi_set_io_high = (1<<18); else *rpi_set_io_low = (1<<18);
if (tx_iqdata[i] & 0x01) *rpi_set_io_high = (1<<12); else *rpi_set_io_low = (1<<12);
*rpi_set_io_low = (1<<RPI_TX_CLK);
}
unsigned char data[4];

mutex_lock(&spi_mutex);
spiXfer(1, tx_iqdata, data, 4); //spi channel 1; write IQ sample (4bytes)
mutex_unlock(&spi_mutex);

// get the tx fifo state
// if tx fifo is full; than a 0 is returned otherwise 1.
// the sleep is in firmware... i think it should be long here... but delay in kernel mode
// is not working properly....
// to do... i like to solve it in the driver.
value = *rpi_read_io;
return ((value >> 7) & 1);
return 4;
}

#endif
Expand Down
2 changes: 1 addition & 1 deletion driver/radioberry_ioctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct rb_info_arg_t

int fpga;

float version;
int version;

int rb_command;
int command;
Expand Down

0 comments on commit d9dd8d2

Please sign in to comment.