View: 11124|Reply: 5

how to use a 2 slave spi device on OPi PC

[Copy link]

1

threads

3

posts

23

credits

Novice

Rank: 1

credits
23
Published in 2017-3-10 20:27:57 | Show all floors |Read mode
I have an ADC and DAC board built for Raspberry Pi which has 2 slaves on the SPI bus, requiring ce0 and ce1. This works fine on the Raspberry pi 3, which installs devices /dev/spidevo.0 and also /dev/spidev/0.1,  to access each slave.   On the OPi PC I can access slave 0, but not slave 1. In /dev on the Opi there is only a single spidev device: /dev/spidev0.0 and no spidev0.1 device. I have modified the script.bin (I am using Armbian legacy OS) by editing the .fex to add a spi_cs1 = portC07<3><default><default><default>   line. But this prevents any spidev device from loading, even spidev0.0 is now missing.
I have tried many variations to add a spi_cs1  connection in the fex file, but noothing works. the OPi pinout description includes SPI-CE1 (header pin 26) but how can i make this active?

How can I make the spi system on the OPi PC work with 2 slave devices on the bus as the rasberry pi does easily?

10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
Published in 2017-3-12 06:55:25 | Show all floors
There is information on Russian below.

http://orangepi.pp.ua/index.php? topic=768.0

But I don't know where PA15/PA16 is.


This thread contains more resources

You need to Log in to download or view,No account?    Register

x

1

threads

3

posts

23

credits

Novice

Rank: 1

credits
23
 Author| Published in 2017-3-12 22:49:44 | Show all floors
Thanks nopnop2002. I upgraded the kernel to 3.4.113-sun8i. But I still only have /dev/spidev0.0. Note that the image above shows spidev1.0 - this is not what i am asking about. It is a second spi bus, but again with only a single slave, slave 0.   I need a single spi bus which has 2 slaves 0.0 and 0.1. I cannot read the russian site for further information. Perhaps there is a differen w1-sunxi module around somewhere but the new module with the 113 upgrade did not give me a second SPI bus. Changing the script.bin (using .fex) to add PA21 as ce1 still completely removes all SPI devices.

10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
Published in 2017-3-13 21:08:47 | Show all floors
Edited by nopnop2002 at 2017-3-13 21:20
kgb replied at 2017-3-12 22:49
Thanks nopnop2002. I upgraded the kernel to 3.4.113-sun8i. But I still only have /dev/spidev0.0. Not ...

OK i understood.

When you'd like to control more than 2 of SLAVE device.
It's necessary to decide Chip Select Signal using external device.
Please refer this topic.

https://www.raspberrypi.org/forums/viewtopic.php?p=227101

10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
Published in 2017-3-14 20:59:43 | Show all floors
Edited by nopnop2002 at 2017-4-12 22:21
kgb replied at 2017-3-12 22:49
Thanks nopnop2002. I upgraded the kernel to 3.4.113-sun8i. But I still only have /dev/spidev0.0. Not ...

I tried this circuit.(I want OPI Fritzing parts)
Left :MCP3204
Right :MCP3002

It work fine by this code.
Please try.
  1. /*
  2. * Read MPC3002/MCP3204 with wiringPiSPIDataRW
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <wiringPi.h>
  7. #include <wiringPiSPI.h>

  8. #define DEBUG 1
  9. #define SPI0 0 // /dev/spidev0.0
  10. #define CS0  8 // Slave1 ChipSelect
  11. #define CS1  9 // Slave2 ChipSekect

  12. int main(int argc, char **argv){
  13.   int retCode;
  14.   int i;
  15.   int a2dChannel = 0;  // analog channel
  16.   int a2dVal = 0;
  17.   float a2dVol = 0;
  18.   float Vref = 3.3;
  19.   unsigned char data[3];

  20.   if (argc == 2) {
  21.     printf("argv[1]=%s\n",argv[1]);
  22.     a2dChannel=atoi(argv[1]);
  23.   }

  24.   if (wiringPiSetup() == -1) {
  25.     printf("wiringPiSetup Error\n");
  26.   }
  27.   pinMode(CS0,OUTPUT);
  28.   pinMode(CS1,OUTPUT);
  29.   digitalWrite(CS0,HIGH);
  30.   digitalWrite(CS1,HIGH);

  31. // SPI channel 0 = /dev/spidev0.0
  32.   if (wiringPiSPISetup(SPI0, 1000000) < 0)
  33.   {
  34.     printf("SPISetup failed:\n");
  35.   }

  36.   for(i=0;i<10;i++) {
  37.     digitalWrite(CS0,LOW); // Activate MCP3002
  38.     data[0] = 0b01100000 |( ((a2dChannel & 0x03) << 4));  //  first byte transmitted -> start bit
  39.     data[1] = 0; // third byte transmitted....don't care
  40. if(DEBUG)printf("[MCP3002]data[write]=%02x-%02x\n",data[0],data[1]);
  41.     retCode=wiringPiSPIDataRW (SPI0,data,sizeof(data));
  42. if(DEBUG)printf("[MCP3002]wiringPiSPIDataRW=%d\n",retCode);
  43. if(DEBUG)printf("[MCP3002]data[read]=%02x-%02x\n",data[0],data[1]);
  44.     a2dVal = (data[0]<< 8) & 0b1100000000; //first 2 bit
  45.     a2dVal |=  (data[1] & 0xff);
  46.     a2dVol = (float)a2dVal/1023 * Vref;
  47.     printf("[MCP3002]a2dVal=%d\n",a2dVal);
  48.     printf("[MCP3002]a2dVol=%f[V]\n",a2dVol);
  49.     digitalWrite(CS0,HIGH);
  50.     sleep(1);

  51.     digitalWrite(CS1,LOW); // Activate MCP3204
  52.     data[0] = 0b00000110 |( ((a2dChannel & 0x04) >> 2));  //  first byte transmitted -> start bit -> (SGL/DIF = 1, D2=0)
  53.     data[1] = 0b00000000 |( ((a2dChannel & 0x03) << 6)); // second byte transmitted -> (D1=D0=0)
  54.     data[2] = 0; // third byte transmitted....don't care
  55. if(DEBUG)printf("[MCP3204]data[write]=%02x-%02x-%02x\n",data[0],data[1],data[2]);
  56.     retCode=wiringPiSPIDataRW (SPI0,data,sizeof(data));
  57. if(DEBUG)printf("[MCP3004]wiringPiSPIDataRW=%d\n",retCode);
  58. if(DEBUG)printf("[MCP3204]data[read]=%02x-%02x-%02x\n",data[0],data[1],data[2]);
  59.     a2dVal = (data[1]<< 8) & 0b111100000000; //first 4 bit
  60.     a2dVal |=  (data[2] & 0xff);
  61.     a2dVol = (float)a2dVal/4095 * Vref;
  62.     printf("[MCP3204]a2dVal=%d\n",a2dVal);
  63.     printf("[MCP3204]a2dVol=%f[V]\n",a2dVol);
  64.     digitalWrite(CS1,HIGH);
  65.     sleep(1);
  66.   }
  67. }
Copy code


This thread contains more resources

You need to Log in to download or view,No account?    Register

x

1

threads

3

posts

23

credits

Novice

Rank: 1

credits
23
 Author| Published in 2017-3-18 22:44:23 | Show all floors
Thanks. I followed your idea and used 2 spare GPIO pins as device selects, which i drive from the code. I have then been able to use device access through /dev/spidev0.0 for both devices, rather than using wiringPiSPIDataRW (...)   This avoids the need for me to use the device setup/access data strings as I can rely on the library provided by the manufacturer of the ADC-DAC card i am using.
You need to log in before you can reply login | Register

Points Rule

Quick reply Top Return list