[UC3-A3] SPI Problems, suspect clock or gpio problem.

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

Hi All,

I've just started out into the 32 bit world, with a UC3-A3 XPLAINED Board, and the ASF.

I'm trying (in vain!) to get the SPI interface up and running, and I *think* I'm stuck at a clock set-up issue.

I've been following the layout for the ASF with regard to getting the SPI up and running, with input from the past topics in this forum.

The code compiles, but where I could have sworn I was once stepping through the main loop repeatedly, now appears to hang on "while(!spi_is_tx_empty(SPI1));",

Having started out with a logic analyzer on the header for SPI1, and now an Oscillocope, I can not observe /any/ activity on the SPI-related pins; the SCK line remains constantly high.

If I comment out the line of code "SPI1_init()", the SCK line remains low. This makes me think my pin mapping in "SPI1_setup()" is correct, (and I've traced it through the defines, which also make me think it's correct...), and leads me on to clocks.

My main suspiscion is a badly configured clock source, though the PBA div is set to 2 (ie,2^1), and this is in turn referenced by the function "spi_setupChipReg()"... I've attached the relevant code listings below, please let me know if I'm missing anything that would aid in diagnosis!

Old Code - Linked for Reference. Newer code linked in post below!
http://pastebin.com/6gGnpVsg <- main_listing
http://pastebin.com/kLWmA1rN <- clock_conf.h

I'm apologise if I've missed something obvious, but this is a world apart from the simple ATTINYs I'm used to! I'd very much appreciate some pointers as to where I might have overlooked something.

Thanks very much,

Tom

Edit: Updated Main Listing to show revised code, after a few "d'oh" moments. Still no pin activity though!

Last Edited: Wed. May 1, 2013 - 10:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

TLDR; I thought I had a clock problem, I now am leaning towards the GPIO mapping, but I've checked too many times now. I'd apprciate if anyone could offer some insight on this, please.

Ok, I now realize I may have had a few flaws in my previous code, but I have now re-written the following initializaiton for the XPLAINED's SPI1 Bus, on the J1 header.

However, it still does not work, and I can not think of any more areas to look at.

I've listed the pertinant bits below, so as to save people having to look elsewhere. Full code is also linked lower down.

#define SPI1 (&AVR32_SPI1)
spi_options_t SPI1_options = 
	{
		.reg			= 1,
		.baudrate		= 10000,
		.bits			= 8,
		.spck_delay		= 0,
		.trans_delay	= 0, 
		.stay_act		= 1,
		.spi_mode		= 0,
		.modfdis		= 1		
	};

void SPI1_map_pins()
	{
		
		//SPI1 Pins on XPLAINED BOARD 
		// J1.5 = SPI1.CS0 = PB09 = GPIO.41[A]
		// J1.6 = SPI1.MOSI = PB10 = GPIO.42[A]
		// J1.7 = SPI1.MISO = PB08 = GPIO.40[A]
		// J1.8 = SPI1.SCK = PB07 = GPIO.39 [A]
			static const gpio_map_t SPI1_gpio_map = {
			{39,0},
			{40,0},
			{41,0},	
			{42,0}
			};
		gpio_enable_module_pin(SPI1_gpio_map,sizeof(SPI1_gpio_map)/sizeof(SPI1_gpio_map[0]));	
	}

void SPI1_init()
	{
		SPI1_map_pins();
		
		spi_initMaster(SPI1, &SPI1_options);
		
		spi_selectionMode(SPI1,0,0,0);
		
		spi_enable(SPI1);
		
		
				
	}

My program flow is as such that I:

1) run sys_clkinit();
CONFIG_SYSCLK_SOURCE is defined as SYSCLK_SRC_OSC0.
All CPU/PBA/PBB DIVs are defines as 1.
CONFIG_SYSCLK_INIT_PBAMASK is defined as (1 << SYSCLK_SPI1).

2) board_init() - Nothing interesting, LED and Button GPIO mapping, functional (LEDs work, Button Works.)

3)pcl_switch_to_osc(PCL_OSC0,FOSC0,OSC0_STARTUP); to move off the slow osciallator and onto the 12MHz xtal...

4) sysclk_enable_peripheral_clock(SPI1) - this is what I thought I was missing previously, it hasn't helped.

5) Init the SPI1 - as per SPIn_init() in the attached code, options struct detailed there also.

6) Loop - spi_write(SPI,0x80), with wait on is_tx_ready, and is_tx_empty. Loops repeatedly every 500ms, with the LED toggle.

Observed Behaviour; The program seems to run, that is, the LED toggles, and the loop repeats itself. Using breakpoints, I can sequentially, repeatedly step through each line. However, there is no observed electrical activity on any of the corresponding pins.

It's also worth noting that if I use while(!spi_is_tx_empty(SPI1)){;}, the code does actually hang there if a breakpoint is set, and will never continue.

main listing -> http://pastebin.com/2Z8wYcQ8
clock_conf.h -> http://pastebin.com/Wrf42wZY

I am really quite stuck here. The attached code runs. The LED toggles every cycle, there are no points where the code hangs, as previously. Can anyone please shed some light on what I'm doing wrong?

Thanks very much in advance,

Tom

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

Didn't spend a whole lot of time on it, just glancing at the code, this looks wrong:
gpio_enable_module_pin(SPI1_gpio_map,sizeof(SPI1_gpio_map)/sizeof(SPI1_gpio_map[0]));

should probably be:
gpio_enable_module not gpio_enable_module_pin

Did it not give you warnings about the 1st argument being the wrong type?

HTH

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

Hi Klausb,

(Sorry I hadn't noticed this before, I've been living back in 8-bit land for a while, and only just noticed the reply whilst investigating LUFA elsewhere on the site!)

I didn't notice any warnings at compile time, but your'e right, that looks like the obvious sort of mistake that I was looking for!

I'll give this a try tonight and let you know how I get on.

Thanks!

Tom

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

I am struggeling at the same point you struggled at. Please please post your solution here! :(