How to use SPI1 on Raspberry Pi with support package?

3 visualizzazioni (ultimi 30 giorni)
Hello,
i want to use the SPI 1 device of my Raspberry Pi with the RPi-Supportpackage by using Matlab and Simulink.
I activated SPI1 on my RPi over the "/boot/config.txt" already, but when i connect my RPi to matlab with this function mypi = raspi(ipaddress,username,password) and checked the available SPI channels, two chipselects from the SPI0 are available only.
Does the RPi Supportpackage support other SPI-devices then the SPI0 and if it does, how can i make the other SPI devices (e.g. SPI1, SPI2) visible/usable.
  1 Commento
LUIS GARCIA
LUIS GARCIA il 23 Giu 2021
I have the same problem with Simulink. I need to use both SPI channels and SPI blocks only allow to select SPI0

Accedi per commentare.

Risposte (1)

Keno Pape
Keno Pape il 4 Ott 2021
Actually its not possible to use SPI1, SPI2 .. with the supportpackage
Since there are only 2 Chipselects on SPI0, you need to raise the CS-number, if you want to use SPI0 only
( https://www.raspberrypi.org/forums/viewtopic.php?f=107&t=241191).
I made some s-functions to communicate with the raspberry pi. In the end its pretty simple but propably there is a more simple and smother solution than mine.
I put some selfmade libraries onto my RPi, where I define the c-function for communication. Inside my s-function i just call those function for my needs.
As i had a hard time to find this solution i want to share it, so maybe it can help someone.
One of my s-functions looks like this.
/* Includes_BEGIN */
#include <math.h>
#ifndef MATLAB_MEX_FILE
#include </home/pi/Desktop/libraries/library_ad5761r_dac_1.c>
#endif
/* Includes_END */
/* Externs_BEGIN */
/* extern double func(double a); */
/* Externs_END */
void write_read_dac_1_Start_wrapper(real_T *xD)
{
/* Start_BEGIN */
/*
* Custom Start code goes here.
*/
/* Start_END */
}
void write_read_dac_1_Outputs_wrapper(const uint16_T *u0,
const real_T *xD)
{
/* Output_BEGIN */
if (xD[0] ==1) {
#ifndef MATLAB_MEX_FILE
int fd_dac_1 = open(DEVICE_dac_1, O_RDWR); %Connection star
% DEVICE_dac_1 was declared in my library_ad5761r_dac_1
write_and_update_dac_register_dac_1(fd_dac_1,u0[0]); %Function
close(fd_dac_1); %Connection close
#endif
}
/* Output_END */
}
void write_read_dac_1_Update_wrapper(const uint16_T *u0,
real_T *xD)
{
/* Update_BEGIN */
if (xD[0]!=1) {
#ifndef MATLAB_MEX_FILE
int fd_dac_init_1 = open(DEVICE_dac_1, O_RDWR);
software_full_reset_dac_1(fd_dac_init_1);
write_control_register_dac_1(fd_dac_init_1);
if(read_control_register_dac_1(fd_dac_init_1) == 0) {
printf("DAC1 erfoglreich konfiguriert \n");
}
close(fd_dac_init_1);
#endif
xD[0]=1;
}
/* Update_END */
}
void write_read_dac_1_Terminate_wrapper(real_T *xD)
{
/* Terminate_BEGIN */
#ifndef MATLAB_MEX_FILE
int fd_dac_terminate_1 = open(DEVICE_dac_1, O_RDWR);
software_full_reset_dac_1(fd_dac_terminate_1);
close(fd_dac_terminate_1);
#endif
/* Terminate_END */
}
And one of my library
/* Library ad5671r DAC1*/
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#define ARRAY_SIZE(array) sizeof(array) / sizeof(array[0])
// Parameter ad5761r == DAC1
static const char *DEVICE_dac_1 = "/dev/spidev0.1";
static uint8_t MODE_dac_1 = SPI_MODE_1;
static uint8_t BITS_dac_1 = 8;
static uint32_t CLOCK_dac_1 = 1000000;
static uint16_t DELAY_dac_1 = 5;
// funktionen ad5761r == DAC1
int32_t software_full_reset_dac_1(int fd) {
uint8_t tx_dac_1[] ={175,0,1} ; %send Bits-Buff
uint8_t rx_dac_1[3]; %recieve Bits-Buff
struct spi_ioc_transfer tr_dac_1 = {
.tx_buf = (unsigned long)tx_dac_1,
.rx_buf = (unsigned long)rx_dac_1,
.len = ARRAY_SIZE(tx_dac_1),
.delay_usecs = DELAY_dac_1,
.speed_hz = CLOCK_dac_1,
.bits_per_word = BITS_dac_1,
};
int32_t ret;
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr_dac_1);
if (ret == 1) {
printf("%s: Can't reset dac1\n\r", __func__);
//abort();
return -1;
}
return 0;
}
/*Write_Control_register == Ground lösen und Einstellungen setzen*/
int32_t write_control_register_dac_1(int fd) {
uint8_t tx_dac_1[] = {164, 2, 104};
uint8_t rx_dac_1[3];
struct spi_ioc_transfer tr_dac_1 = {
.tx_buf = (unsigned long)tx_dac_1,
.rx_buf = (unsigned long)rx_dac_1,
.len = ARRAY_SIZE(tx_dac_1),
.delay_usecs = DELAY_dac_1,
.speed_hz = CLOCK_dac_1,
.bits_per_word = BITS_dac_1,
};
int32_t ret;
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr_dac_1);
if ( ret == 1) {
printf("%s: Can't send spi message\n\r", __func__);
//abort();
return -1;
}
return 0;
}
/*Write/Update DAC-Register == Spannungsausgabe definieren*/
int32_t write_and_update_dac_register_dac_1(int fd, uint16_t dac_data) {
uint8_t tx_dac_1[3];
tx_dac_1[0] = 3;
tx_dac_1[1] = (dac_data & 0xFF00) >> 8;
tx_dac_1[2] = (dac_data & 0x00FF) >> 0;
uint8_t rx_dac_1[3];
struct spi_ioc_transfer tr_dac_1 = {
.tx_buf = (unsigned long)tx_dac_1,
.rx_buf = (unsigned long)rx_dac_1,
.len = ARRAY_SIZE(tx_dac_1),
.delay_usecs = DELAY_dac_1,
.speed_hz = CLOCK_dac_1,
.bits_per_word = BITS_dac_1,
};
int32_t ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr_dac_1);
if (ret == 1) {
perror("IO Error");
//abort();
return -1;
}
return 0;
}
//Read Control-Register
int32_t read_control_register_dac_1(int fd) {
uint8_t tx_dac_1[] = {12, 2, 104};
uint8_t rx_dac_1[3];
struct spi_ioc_transfer tr_dac_1 = {
.tx_buf = (unsigned long)tx_dac_1,
.rx_buf = (unsigned long)rx_dac_1,
.len = ARRAY_SIZE(tx_dac_1),
.delay_usecs = DELAY_dac_1,
.speed_hz = CLOCK_dac_1,
.bits_per_word = BITS_dac_1,
};
if (ioctl(fd, SPI_IOC_MESSAGE(1), &tr_dac_1) == 1) {
perror("IO Error");
abort();
}
uint8_t tx_dac_1_1[] ={0, 1, 1};
uint8_t rx_dac_1_1[3];
struct spi_ioc_transfer tr_dac_1_1 = {
.tx_buf = (unsigned long)tx_dac_1_1,
.rx_buf = (unsigned long)rx_dac_1_1,
.len = ARRAY_SIZE(tx_dac_1_1),
.delay_usecs = DELAY_dac_1,
.speed_hz = CLOCK_dac_1,
.bits_per_word = BITS_dac_1,
};
}

Categorie

Scopri di più su MATLAB Support Package for Raspberry Pi Hardware in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by