[LW Mesh] SPI on LW Mesh

Go To Last Post
2 posts / 0 new
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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

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 !

Last Edited: Fri. Oct 16, 2015 - 02:11 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

BitCloud SPI code is concentrated in one file Components\HAL\avr\atmega1281\common\src\halSpi.c and a few depended header files. It is mostly equivalent to your (or rather datasheet) code.

You are correct about XCKn_DDR and XCKn - they should be replaced by corresponding DDR register and pin index.

It is better to use symbolic names. Here is how they are defined in BitCloud (in the same file):

#define UDORD0                  2
#define UCPHA0                  1
#define UCPOL0                  0

NOTE: I no longer actively read this forum. Please ask your question on www.eevblog.com/forum if you want my answer.