Using SPID on xplained 256A3BU

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

Hello everyone,

I'm currently trying to get SPI working on Poort D of my xplained board, but it isn't working.

I have first tried to let it work on Poort C (SPIC). There everything works fine, but when i want to poort it to SPID it isn't working.

 

I have changed poorts to work on the Poort D and have set alle the registers of Poort D and SPID. But i don't get any response.

 

This is the code i use to init the SPI and get data from it.

void SPI_init(void){
    cli();
    PORTD.DIRCLR = (1<<SPIDMI);
    PORTD.DIRSET = (1<<SPIDMO) | (1<<SPIDSC) | (1<<SPIDSS);
    SPID.CTRL = SPI_ENABLE_bm | SPI_MASTER_bm | SPI_MODE_1_gc | SPI_PRESCALER_DIV16_gc;
    PR.PRPD = (~SPID_clock_enable);
    sei();
}
#define SPIDMO   3
#define SPIDMI   2
#define SPIDSC   1
#define SPIDSS   0
#define SS_PIN		 IOPORT_CREATE_PIN(PORTD,0)

uint8_t spi(uint8_t val)
/*Sending data to RTC*/
{
	SPID.DATA = val;
	while(!(SPID.STATUS & (1<<7))){};
	return SPID.DATA;
}

void Select_slave(void){
	ioport_set_pin_level(SS_PIN,0);
}

void Unselect_slave(void){
	ioport_set_pin_level(SS_PIN,1);
}

Can anybody help me with the problem?

Thanks in advance.

Rgamer

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

That SPI port is connected to the dataflash. Make sure you have it deselected by setting it's SS high (PF4).

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

Thz fot he response,

 

with the Board_init() function these pins are set high at init.

#ifdef CONF_BOARD_C12832A1Z
	ioport_configure_pin(NHD_C12832A1Z_SPI_SCK, IOPORT_DIR_OUTPUT
			| IOPORT_INIT_HIGH);
	ioport_configure_pin(NHD_C12832A1Z_SPI_MOSI, IOPORT_DIR_OUTPUT
			| IOPORT_INIT_HIGH);
	ioport_configure_pin(NHD_C12832A1Z_CSN, IOPORT_DIR_OUTPUT
			| IOPORT_INIT_HIGH);
	ioport_configure_pin(NHD_C12832A1Z_REGISTER_SELECT, IOPORT_DIR_OUTPUT
			| IOPORT_INIT_HIGH);
	ioport_configure_pin(NHD_C12832A1Z_RESETN, IOPORT_DIR_OUTPUT
			| IOPORT_INIT_HIGH);
	ioport_configure_pin(NHD_C12832A1Z_BACKLIGHT, IOPORT_DIR_OUTPUT
			| IOPORT_INIT_LOW);
#endif

#ifdef CONF_BOARD_AT45DBX
	ioport_configure_pin(AT45DBX_MASTER_SCK, IOPORT_DIR_OUTPUT
			| IOPORT_INIT_HIGH);
	ioport_configure_pin(AT45DBX_MASTER_MOSI, IOPORT_DIR_OUTPUT
			| IOPORT_INIT_HIGH);
	ioport_configure_pin(AT45DBX_MASTER_MISO, IOPORT_DIR_INPUT);
	ioport_configure_pin(AT45DBX_CS, IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH);
#endif

But i have set these pins high when i'm initialising my spi, but i still got the problem.

[EDIT:]

I have cut the jumper on the poort to disconnect the display. So that isn't the problem to.

Last Edited: Tue. May 5, 2015 - 08:20 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I attached a logic analyser to the pins.

It seems that the clock is not running on the pin.

I have tried to set the level of the pins manually to see if i got the right pins. But they are the correct pins.

 

So the clock is not running in the pin and there is no data on the line. The SS pin works fine, because i set this one myself

 

[EDIT:]

Is there a way to change the pins the clock work or something to let the clock work?

 

Last Edited: Tue. May 5, 2015 - 09:17 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But wait a minute. The SPI pins is on PORTD4-7, not PORTD1-3. If you want to use the lower pins you have to use USARTD0 in master SPI mode.

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

oh oke thx,

It's a pity that the xplained board doens't have these pin connected to the headers.

So i have to use USARTD0 in spi on these pins.

How do i init the USARTD0 so i can use it as  SPI? Is there some example for it that i can see how it works?

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

I don't know how to do it with ASF. I do it like this.

/*
  xspi_usart.h
  XMega USART in master SPI mode.
  Tested on XMegaE5.
 */

#ifndef XSPI_USART_H
#define XSPI_USART_H

#include <avr/io.h>

#define XSPI_USART USARTD0
#define XSPI_PORT  PORTD
#define XSPI_USART0_REMAP 0

// Set mode to 0, 1, 2 or 3
#define XSPI_MODE 0
// Maximum SPI-clk
#define F_XSPI 4000000
// Set to 1 to enable RX
#define XSPI_RX_EN 0

#define spi_send(x) spi_transfer(x)

//////////////////////////////////////////////////////////////////

#if !XSPI_REMAP
#define XSPI_MOSI PIN3_bm
#define XSPI_MISO PIN2_bm
#define XSPI_SCK  PIN1_bm
#else
#define XSPI_MOSI PIN7_bm
#define XSPI_MISO PIN6_bm
#define XSPI_SCK  PIN5_bm
#endif


static inline void spi_init(void)
{
#if USART0_REMAP
    XSPI_PORT.REMAP = PORT_USART0_bm;
#endif
#if XSPI_MODE >= 2
    // Invert clock in mode 2 and 3
#if XSPI_USART0_REMAP
    XSPI_PORT.PIN5CTRL = PORT_INVEN_bm;
#else
    XSPI_PORT.PIN1CTRL = PORT_INVEN_bm;
#endif
#endif
#if XSPI_RX_EN
    XSPI_PORT.DIRCLEAR = XSPI_MISO;
#endif
    XSPI_PORT.DIRSET = XSPI_MOSI | XSPI_SCK;
    XSPI_USART.BAUDCTRLA = 0;
    XSPI_USART.BAUDCTRLB = 0;
    XSPI_USART.CTRLC = USART_CMODE_MSPI_gc | (XSPI_MODE & 1) << 1;
#if XSPI_RX_EN
    USARTC0.CTRLB = USART_TXEN_bm | USART_RXEN_bm;
#else
    USARTC0.CTRLB = USART_TXEN_bm;
#endif
#if F_CPU / 2 / F_XSPI >= 1
    XSPI_USART.BAUDCTRLA = F_CPU / 2 / F_XSPI - 1;
#endif
}


static inline uint8_t spi_transfer(uint8_t data)
{
    XSPI_USART.STATUS = USART_TXCIF_bm;
    XSPI_USART.DATA = data;
    while((XSPI_USART.STATUS & USART_TXCIF_bm) == 0)
        ;
    return XSPI_USART.DATA;
}

#endif