Hi all !
I would like to get SPI to work in LW_Mesh.
I'm using ATZB-A24-UFLB and used pins for that are:
38 -> USART0_RXD
39 -> USART0_TXD
40 -> USART0_EXTCLK
and my CS -> chip select.
I used SPI ni BitCloud without any problems and had a look how it's implemented but I'm kind of lost there.
So I'm trying to implement SPI this way:
In datasheet of ATmega1281 p. 235 & 237 I found these code examples:
void USART_Init( unsigned int baud ) { UBRRn = 0; /* Setting the XCKn port pin as output, enables master mode. */ XCKn_DDR |= (1<<XCKn); /* Set MSPI mode of operation and SPI data mode 0. */ UCSRnC = (1<<UMSELn1)|(1<<UMSELn0)|(0<<UCPHAn)|(0<<UCPOLn); /* Enable receiver and transmitter. */ UCSRnB = (1<<RXENn)|(1<<TXENn); /* Set baud rate. */ /* IMPORTANT: The Baud Rate must be set after the transmitter is enabled */ UBRRn = baud; }
unsigned char USART_Receive( void ) { /* Wait for empty transmit buffer */ while ( !( UCSRnA & (1<<UDREn)) ); /* Put data into buffer, sends the data */ UDRn = data; /* Wait for data to be received */ while ( !(UCSRnA & (1<<RXCn)) ); /* Get and return received data from buffer */ return UDRn; }
I have few questions here:
1. Here we are talking about USART0, since real SPI is used for AT86RF230, so everywhere in code above "n" is replaced with "0", right?
I don't quite understand this line:
/* Setting the XCKn port pin as output, enables master mode. */ XCKn_DDR |= (1<<XCKn);
AS6 doesn't recognize "XCKn_DDR" register, do I have to include something or ??
What I think what is happening here is:
USART0_EXTCLK(PE2) is used as clock and set as output.
So insted of :
/* Setting the XCKn port pin as output, enables master mode. */ XCKn_DDR |= (1<<XCKn);
I can use:
DDRE |= (1<<PE2);
2. AS6 doesn't recognize "UDORD0" in UCSR0C register.
I need SPI in MSPIM mode, SPI mode 1 and MSB first
It's better to do it like this:
UCSR0C = 0xC2;
insted of:
UCSR0C = (1<<UMSELn1)|(1<<UMSELn0)|(1<<UCPHAn)|(0<<UCPOLn)|(0<<UDORD0);
Will this code above work with required modifications or is there better way to do it?
Thanks !