Go Down

Topic: Setting up the second SPI port on the Arduino Uno (Read 2 times) previous topic - next topic

Andy Baker

As I understand it, there's a second hardware SPI port that's available for use on the ATMEGA328P, by configuring the regular USART to run in SPI master mode, which should in theory mean it's available on the Arduino Uno. I'll admit, I'm not much of a genius in configuring the registers, etc, but I figure there's gotta be somebody out there that's done this on an Arduino. Thoughts? I've got an LED string that I have running GREAT over the SPI port, but I need to access other devices and there's no provisions for chip select on the LEDs.

I'm getting my onfo on page 206 of http://www.atmel.com/Images/doc8271.pdf

Or do I just give up and set up AND gates and MAKE my LED strip have chip select?

Andy

Nick Gammon

There's a slightly helpful application note here:

www.atmel.com/Images/doc2577.pdf

I'll see if I can make it work, since I can't easily find someone who has done it.
http://www.gammon.com.au/electronics

Nick Gammon

#2
Apr 12, 2012, 06:33 am Last Edit: Apr 12, 2012, 07:48 am by Nick Gammon Reason: 1
That wasn't too hard. Example:

Code: [Select]
/*
Example of USART in SPI mode on the Atmega328.

Author:   Nick Gammon
Date:     12th April 2012
Version:   1.0

Licence: Released for public use.

Pins: D0 MISO (Rx)
     D1 MOSI (Tx)
     D4 SCK  (clock)
     D5 SS   (slave select)  <-- this can be changed

Registers of interest:

UDR0 - data register

UCSR0A - USART Control and Status Register A
    Receive Complete, Transmit Complete, USART Data Register Empty

UCSR0B - USART Control and Status Register B
    RX Complete Interrupt Enable, TX Complete Interrupt Enable, Data Register Empty Interrupt Enable ,
    Receiver Enable, Transmitter Enable
 
UCSR0C - USART Control and Status Register C
    Mode Select (async, sync, SPI), Data Order, Clock Phase, Clock Polarity

UBRR0L and UBRR0H - Baud Rate Registers - together are UBRR0 (16 bit)

*/

const byte MSPIM_SCK = 4;
const byte MSPIM_SS = 5;

// sends/receives one byte
byte MSPIMTransfer (byte c)
{
 // wait for transmitter ready
 while ((UCSR0A & _BV (UDRE0)) == 0)
   {}
   
 // send byte
 UDR0 = c;
 
 // wait for receiver ready
 while ((UCSR0A & _BV (RXC0)) == 0)
   {}
   
 // receive byte, return it
 return UDR0;
}  // end of MSPIMTransfer

// select slave, write a string, wait for transfer to complete, deselect slave
void spiWriteString (const char * str)
 {
 if (!str) return;  // Sanity Clause
 
 char c;

 // enable slave select
 digitalWrite (MSPIM_SS, LOW);
 
 // send the string
 while (c = *str++)
   MSPIMTransfer (c);
 
 // wait for all transmissions to finish
 while ((UCSR0A & _BV (TXC0)) == 0)
   {}

 // disable slave select
 digitalWrite (MSPIM_SS, HIGH);
 }  // end of spiWriteString

void setup()
 {
 pinMode (MSPIM_SS, OUTPUT);   // SS
 // must be zero before enabling the transmitter
 UBRR0 = 0;
 UCSR0A = _BV (TXC0);  // any old transmit now complete
 pinMode (MSPIM_SCK, OUTPUT);   // set XCK pin as output to enable master mode
 UCSR0C = _BV (UMSEL00) | _BV (UMSEL01);  // Master SPI mode
 UCSR0B = _BV (TXEN0) | _BV (RXEN0);  // transmit enable and receive enable
 // must be done last, see page 206
 UBRR0 = 3;  // 2 Mhz clock rate
 }  // end of setup

void loop()
 {
 spiWriteString ("hello, world!");
 }  // end of loop


Screenshot in operation:



The stuff in setup configures the USART.

spiWriteString shows the general idea of using it ... you select the slave (if wanted), then repeatedly call MSPIMTransfer to transfer each byte. It waits for the last one to finish (so we can deselect SS in an orderly way).

MSPIMTransfer sends/receives one byte - blocking.
http://www.gammon.com.au/electronics

diesel

#3
Feb 06, 2013, 04:50 am Last Edit: Feb 06, 2013, 05:14 am by Nick Gammon Reason: 1
Nick,

Thanks for posting the code to create SPI port on uart pins. I am, however, having a problem with it. I tried setting up a simple test. I am using an Arduino Uno w/no devices attached. Since I was having a problem, I tried this simple test using std SPI library, works as you would expect. I am using FTDI breakout from Sparkfun to read. hello there, hello there....

Code: [Select]
#include <SPI.h>
#include <SoftwareSerial.h>
SoftwareSerial DebugPort = SoftwareSerial(2,3);

char mystring[100]="hello there";
char mystring2[100]="";
void setup() {
 // put your setup code here, to run once:
 DebugPort.begin(9600);
 SPI.begin();
 SPI.setBitOrder(MSBFIRST);
 SPI.setClockDivider(SPI_CLOCK_DIV2);
 SPI.setDataMode(SPI_MODE0);
}

void loop() {
 uint8_t x=0;
 digitalWrite(SS,LOW);
for(x;x<(strlen(mystring));x++){
 mystring2[ x ] = SPI.transfer( mystring [ x ] ) ;
}
digitalWrite(SS,HIGH);
mystring2[x+1]='\0';
DebugPort.println(mystring2);
}


*************
Then I tried a similar setup using the code you posted. On this I get an output of ......,......,...... (yes dots). Do you know what may be the problem?
************



Code: [Select]
#include <SoftwareSerial.h>
SoftwareSerial DebugPort = SoftwareSerial(2,3);
const byte MSPIM_SCK = 4;
const byte MSPIM_SS = 5;
char mystring[100]="hello there";
char mystring2[100]="";
// sends/receives one byte
char MSPIMTransfer (char c)
{
 // wait for transmitter ready
 while ((UCSR0A & _BV (UDRE0)) == 0)
   {}
   
 // send byte
 UDR0 = c;
 
 // wait for receiver ready
 while ((UCSR0A & _BV (RXC0)) == 0)
   {}
   
 // receive byte, return it
 return UDR0;
}  // end of MSPIMTransfer

// select slave, write a string, wait for transfer to complete, deselect slave
void spiWriteString (const char * str)
 {
 if (!str) return;  // Sanity Clause
 
 char c;

 // enable slave select
 digitalWrite (MSPIM_SS, LOW);
// const char * mystr = str;
 uint8_t count =0;
 // send the string
 while (c = *str++){
  mystring[count] =MSPIMTransfer (c);
 }

 // wait for all transmissions to finish
while ((UCSR0A & _BV (TXC0)) == 0)
   {}

 // disable slave select
 digitalWrite (MSPIM_SS, HIGH);
 }  // end of spiWriteString

void setup()
 {
 DebugPort.begin(9600);
 pinMode (MSPIM_SS, OUTPUT);   // SS
 // must be zero before enabling the transmitter
 UBRR0 = 0;
 UCSR0A = _BV (TXC0);  // any old transmit now complete
 pinMode (MSPIM_SCK, OUTPUT);   // set XCK pin as output to enable master mode
 UCSR0C = _BV (UMSEL00) | _BV (UMSEL01);  // Master SPI mode
 UCSR0B = _BV (TXEN0) | _BV (RXEN0);  // transmit enable and receive enable
 // must be done last, see page 206
 UBRR0 =3;// 3;  // 2 Mhz clock rate
 }  // end of setup

void loop()
 {
   
     uint8_t x=0;
 digitalWrite(MSPIM_SS,LOW);

for(x;x<(strlen(mystring));x++){
 mystring2 [ x ] =MSPIMTransfer( mystring  [ x ] );
}
digitalWrite(MSPIM_SS,HIGH);
 mystring2[x+1]='\0';
 DebugPort.println(&mystring2[0]);  
}


Thanks,

-diesel

Moderator edit: [code] ... [/code] tags added. (Nick Gammon)

Nick Gammon

Code: [Select]

  DebugPort.println(&mystring2[0]);   


Why not:

Code: [Select]

  DebugPort.println(mystring2);   


As you had in the working example.
http://www.gammon.com.au/electronics

diesel

Nick,

I was messing around with code trying to get it to work, that is why they were different.

I modified as you suggested, but same output......,......,. I did switch back to my first listed code and noticed for a short period of time it would output ......., .......,  then it went back to hello there. Maybe it is a timing thing? I saw something in the datasheet that it talks about setting up interrupts, are those necessary?

Thanks,

-diesel

Nick Gammon

Quote
I am using FTDI breakout from Sparkfun to read.


Read what? The software serial output?

Software serial turns interrupts off for a while. Try using a faster debugging baud rate.

Also it isn't clear what is being echoed from where.
http://www.gammon.com.au/electronics

diesel

Nick,

The plan is to use the secondary spi port to use with SPI ram. First, I want to make sure it is working, so now I am just reading and writting from SoftwareSerial. I am attempting to write and read into the SPI port, which works using the hardware spi.

Thanks, I will give the higher baud rate a try.

-diesel


Nick Gammon

It certainly should work, unfortunately sometimes debugging itself obscures what is happening.
http://www.gammon.com.au/electronics

diesel

#9
Feb 07, 2013, 06:50 am Last Edit: Feb 07, 2013, 06:52 am by diesel Reason: 1
Nick,

I put a logic analyzer on both so that you could see. I attached two images, 1st with Uart SPI and 2nd with Arduino std SPI. On the Uart SPI I get 255...255,\r,\n from serial. Nothing but 255 for the array of characters. On the Arduino std SPI I get ->hello there \r\n(although not perfect). The best software serial baud seemed to be 38400, I ranged from 9600-115200( best defined by what I received from Arduino SPI) . I also tried connecting the Arduino up with only 9v power supply, disconnecting usb, I thought that might be giving me interference, no change in output, still 255 from Uart SPI. Any other suggestions? I have a arduino mega 1280, perhaps I could give that a try w/the 2nd hardware serial port. Would I have to change the registers?

Thanks,

-diesel

Nick Gammon

OK, for me to reproduce that I need both ends. So far you have posted code for one of the devices. That isn't something that communicates with SPI RAM, right?
http://www.gammon.com.au/electronics

diesel

Nick,

I hooked up the SPI ram, it works on the arudino 328p, which is great, thanks! Unfortunately, I can't get it working on the mega 1280. The registers and bits seem to be the same. I looked over the datasheets for the 328 and the 1280, they both look same w/regards to UART in SPI mode. Do you know what maybe the problem?

Thanks,

-diesel

Nick Gammon

Ah, no I haven't looked closely at the registers for the Mega with respect to this. The fact that the subject line of each post is still "Setting up the second SPI port on the Arduino Uno" is obscuring somewhat the fact that we are talking about a Mega.
http://www.gammon.com.au/electronics

diesel

Nick,

I can start a new post if you like? I think that I have found the problem. XCKO is not mapped to an accessible pin. Ref. http://arduino.cc/en/Hacking/PinMapping2560 (which I believe has the same mapping as the 1280). If you search on XCK, none are mapped to an accessible pin. Are there any options of accessing the function of those pins?

Thanks,

-diesel

Nick Gammon

Looks like you are right. They haven't connected any of them to an external pin.

I don't know offhand of any way of making one pin "mirror" another. You could always bit-bang the SPI.
http://www.gammon.com.au/electronics

Go Up