Cannot get SPI on XmegaA3BU-Xplained to work

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

Hi Group,

 

I am having an issue getting the SPI on Port C to work on the XmegaA3BU-Xplained to work.

 

I started with an Atmel example program (ADC_EXAMPLE1_GFX1) so I could display values on the LCD. 

This worked just fine.

 

Then I added some simple code for the SPI which I cannot get to work.

 

I write to Port C; the SS signal toggles; the LEDs also blink; but the SPI does nothing.

The values read back from the SPI registers are 0. 

It's almost like I am not accessing the SPI Port.

 

Please tell me what I am doing wrong, I am sure I applied something incorrectly. 

 

The program is below.

Thanks for your help and suggestions.

 

Paul

 

 

/*******************************************************************************
 *
 * ADC Test v2
 *
 * Created:	6/30/2017 7:33:47 AM
 *  Author: Paul A. Teseny
 * Company: Scaletron Industries, LLC
 *
 * Processor: ATxmega256A3BU
 *     Board: Xmega-A3BU Xplainned
 *
 * This program reads 24 bit data from the LT2400 ADC using the
 * SPI interface then it writes it on the integrated LCD screen.
 *
 * This software is based on the ADC_Example1_GFX1 example from Atmel.
 *
 ******************************************************************************/

#include <asf.h>
#include <stdio.h>

/********!*********!*********!*********!*********!*********!*********!*********!
 *  Hardware defines
 ********!*********!*********!*********!*********!*********!*********!*********/

#define SPI_SS_bm           0x10	/* Bit mask for the SS pin.		*/
#define SPI_MOSI_bm         0x20	/* Bit mask for the MOSI pin.	*/
#define SPI_MISO_bm         0x40	/* Bit mask for the MISO pin.	*/
#define SPI_SCK_bm          0x80	/* Bit mask for the SCK pin.	*/

/*	SPI master status code defines	*/

#define SPI_OK              0		/* The transmission completed successfully. */
#define SPI_INTERRUPTED     1		/* The transmission was interrupted by another master. */
#define SPI_BUSY            2		/* The SPI module is busy with another transmission. */

/*	Size of the string output to the display	*/
#define OUTPUT_STR_SIZE		32
#define NUM_BYTES			4

/*******************************************************************************
*  Global Variables
*********!*********!*********!*********!*********!*********!*********!*********/

char		out_str[OUTPUT_STR_SIZE] ;
uint16_t	paul	=	0x2A ;
uint16_t	joe		=	0x3C ;
uint16_t	herman	=	0x4D ;

/*  Test data to send from master. */
uint8_t masterSendData[NUM_BYTES] = {0x10, 0x20, 0x30, 0x40} ;

/*  Data received from slave. */
uint8_t masterReceivedData[NUM_BYTES] ;

/********!*********!*********!*********!*********!*********!*********!*********!
 *	Program Start
 ********!*********!*********!*********!*********!*********!*********!*********/

int main(void)
{
	board_init() ;
	sysclk_init() ;
	sleepmgr_init() ;
	irq_initialize_vectors() ;
	cpu_irq_enable() ;
	gfx_mono_init() ;

	/*	Turn ON the LCD Back light	*/
	ioport_set_pin_high(LCD_BACKLIGHT_ENABLE_PIN) ;

	/*	Make Port C Bit 6 an Input	*/
	PORTC_DIRCLR	=	SPI_MISO_bm ;

	/*	Make Port C Bits 7, 5, 4 Outputs	*/
 	PORTC_DIRSET	=	SPI_SCK_bm | SPI_MOSI_bm | SPI_SS_bm ;

	/*	Make Port C Outputs	have pullups	*/
	PORTC_PIN4CTRL	=	PORT_OPC_PULLUP_gc ;	// !SS
	PORTC_PIN5CTRL	=	PORT_OPC_PULLUP_gc ;	// MOSI
	PORTC_PIN7CTRL	=	PORT_OPC_PULLUP_gc ;	// SCK

	/*	Setup SPI on Port C	*/
	SPIC_CTRL = SPI_ENABLE_bm | SPI_MASTER_bm | SPI_MODE_0_gc | SPI_PRESCALER_DIV128_gc ;

	/*	Setup stuff to display	*/
	paul	=	PORTC_DIRSET ;
	joe		=	SPIC_CTRL ;
	herman  =	SPIC_STATUS ;
	
	/*	Display Values	*/
	snprintf(out_str, OUTPUT_STR_SIZE, "Paul:   %6X  \n", paul) ;
	gfx_mono_draw_string(out_str, 0, 0, &sysfont) ;

	snprintf(out_str, OUTPUT_STR_SIZE, "Joe:    %6X  \n", joe) ;
	gfx_mono_draw_string(out_str, 0, 10, &sysfont) ;

	snprintf(out_str, OUTPUT_STR_SIZE, "Herman: %6X  \n", herman) ;
	gfx_mono_draw_string(out_str, 0, 20, &sysfont) ;

/********!*********!*********!*********!*********!*********!*********!*********!
 *  Program Loop
 *
 *	Write a byte to the SPI on Port C
 ********!*********!*********!*********!*********!*********!*********!*********/

	while (1)
	{
		/*	PHASE 1: Send individual bytes out SPI	*/
		for(uint8_t i = 0; i < NUM_BYTES; i++)
		{
			LED_Toggle (LED0_GPIO) ;

			/*	MASTER: Pull SS line low	*/
			PORTC_OUTCLR = PIN4_bm ;

			/*	Slow it down to see what is happening	*/
			delay_us (5) ;

			/*	MASTER: Transmit data from master	*/
			SPIC_DATA = masterSendData[i] ;
//			while(!(SPIC_STATUS & (1<<7))) ;

			/*	Slow it down to see what is happening	*/
			delay_us (5) ;

			/*	MASTER: Release SS to slave. */
			PORTC_OUTSET = PIN4_bm ;

			/*	Slow it down to see what is happening	*/
			delay_ms (10) ;

		}	/*	End of Phase 1	*/

			LED_Toggle (LED2_GPIO) ;

	}	/*	End of Program Loop	*/

}	/*	End of MAIN	*/

/********!*********!*********!*********!*********!*********!*********!*********!
 *        That's all folks!
 ********!*********!*********!*********!*********!*********!*********!*********/

 

 

 

 

This topic has a solution.

You never know where life is going to take you; sit back and enjoy the ride!

Last Edited: Fri. Jul 14, 2017 - 03:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is where a logic analyzer or Oscope comes in handy.

 

Jim

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The call to sysclk_init() is enabling Power Reduction (turning off the peripheral clock) for ALL peripherals.  You must TURN OFF power reduction for SPIC.

Greg Muth

Portland, OR, US

Xplained Boards mostly

Atmel Studio 7.0 on Windows 10 VM hosted by Ubuntu 17

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have an o'scope connected, there is just nothing to see!

 

Paul

You never know where life is going to take you; sit back and enjoy the ride!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Greg,

 

I'll give that a try.

 

Paul

You never know where life is going to take you; sit back and enjoy the ride!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PaulieT wrote:
there is just nothing to see!

Not even these?

LED_Toggle (LED0_GPIO) ;

			/*	MASTER: Pull SS line low	*/
			PORTC_OUTCLR = PIN4_bm ;

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okay; valid point! wink

 

Paul

You never know where life is going to take you; sit back and enjoy the ride!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Greg,

 

That did it! I see 8 clocks per byte now!

 

Paul

You never know where life is going to take you; sit back and enjoy the ride!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@Paul,

 

Been there, done that, got the T-shirt.  (More times than I care to admit!)

 

 

Greg Muth

Portland, OR, US

Xplained Boards mostly

Atmel Studio 7.0 on Windows 10 VM hosted by Ubuntu 17