Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
giannhssdra
PostPosted: Apr 23, 2012 - 02:35 PM
Wannabe


Joined: Apr 03, 2010
Posts: 82


Hello all

Im trying to set up the spi in order to connect it with an sd card through fatfs library.
If you can just check my code and point out any mistakes.I have tried everything with both low level driver(spi.h) and the master interface(spi_master.h).
Below is the code i changed inside diskio.c from fatfs.

Initialization
Code:
static
void power_on (void)
{
   static const gpio_map_t SPI_GPIO_MAP =
   {
      {AVR32_SPI1_NPCS_0_0_PIN , AVR32_SPI1_NPCS_0_0_FUNCTION},
      {AVR32_SPI1_SCK_0_0_PIN  , AVR32_SPI1_SCK_0_0_FUNCTION },
      {AVR32_SPI1_MISO_0_0_PIN , AVR32_SPI1_MISO_0_0_FUNCTION},
      {AVR32_SPI1_MOSI_0_0_PIN , AVR32_SPI1_MOSI_0_0_FUNCTION}             
   };
   
   static spi_options_t SPI_OPTIONS =
   {
      .reg = 0,
      .baudrate = 1000000,
      .bits = 8,
      .spck_delay = 0,
      .trans_delay = 0,
      .stay_act = 0,
      .spi_mode = SPI_MODE_1,
      .modfdis = 1
   };
   //sysclk_enable_pba_module(SYSCLK_SPI1);
   gpio_enable_module(SPI_GPIO_MAP , sizeof(SPI_GPIO_MAP)/sizeof(SPI_GPIO_MAP[0]));
   spi_initMaster(&AVR32_SPI1 , &SPI_OPTIONS);
   
   spi_setupChipReg(&AVR32_SPI1,&SPI_OPTIONS,sysclk_get_pba_hz());     
   spi_selectionMode(&AVR32_SPI1 ,0 ,0 ,0);
   spi_enable((&AVR32_SPI1));
}


As chip select for the sd i want to use the SS pin of the spi1.SS is the PB09 pin which is the AVR32_SPI1_NPCS_0_PIN

Select deselect device
Code:
#define SELECT()   spi_selectChip((&AVR32_SPI1), 0)/*MMC CS = L */
#define   DESELECT()   spi_unselectChip((&AVR32_SPI1), 0)/*MMC CS = H */


Read write operations
Code:
#define xmit_spi(dat)    spi_write(&AVR32_SPI1 , (uint16_t)dat )

static
BYTE rcvr_spi (void)
{
   uint16_t  data;
   spi_read(&AVR32_SPI1 , &data);
   return (BYTE)data;
}

/* Alternative macro to receive data fast */
#define rcvr_spi_m(dst)   *(dst) = rcvr_spi()//spi_read(&AVR32_SPI1 , (uint16_t*)dst)


I have already worked with spi with another device that but its write only(MISO is not used).But now that im trying to use it with the sd card i cant read.
Now the strange thing is that it cant read from the spi.Every time it goes in the spi_read() function it never gets the data and exits with time_out error.
Any idea why this is happening?

Thanks
 
 View user's profile Send private message  
Reply with quote Back to top
mikech
PostPosted: Apr 24, 2012 - 12:13 AM
Hangaround


Joined: Aug 19, 2003
Posts: 396
Location: Australia

SPI_MODE_1 ?

To initialise the SD card into SPI mode, the clock should be <400kHz, after the device is initialised you can increase that to a few MHz.

Try a baudrate of 400kHz and spi mode 0.
 
 View user's profile Send private message  
Reply with quote Back to top
giannhssdra
PostPosted: Apr 24, 2012 - 01:43 AM
Wannabe


Joined: Apr 03, 2010
Posts: 82


Thanks for the answer.
I tried your way but still i could't get it working.
The code above is correct with the power on function and the read right operations?

I still have the same problem with the spi_read() .
With spi_srite this line
Code:
spi->tdr = data << AVR32_SPI_TDR_TD_OFFSET;

is actually executed in debugging.

But with spi_read() it never get to this instuction
Code:
*data = spi->rdr >> AVR32_SPI_RDR_RD_OFFSET;

it always run out of time and returns a TIMEOUT_ERROR.
 
 View user's profile Send private message  
Reply with quote Back to top
mikech
PostPosted: Apr 24, 2012 - 02:24 AM
Hangaround


Joined: Aug 19, 2003
Posts: 396
Location: Australia

try .stay_act = 1
that should stop the chip-select line from automatically deselecting.
 
 View user's profile Send private message  
Reply with quote Back to top
jkuusama
PostPosted: Apr 24, 2012 - 07:39 AM
Hangaround


Joined: Sep 21, 2004
Posts: 210


I remembered writing about this long time ago. Smile Read http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=110134.
 
 View user's profile Send private message  
Reply with quote Back to top
giannhssdra
PostPosted: Apr 24, 2012 - 05:26 PM
Wannabe


Joined: Apr 03, 2010
Posts: 82


Hello

Still nothing.
I copied your code from the other topic but still nothing.
Quote:
SPI_MODE_1 ?

To initialise the SD card into SPI mode, the clock should be <400kHz, after the device is initialised you can increase that to a few MHz.

Are you sure about SPI_MODE_0?In the datasheet says tha mode1 is pol = 0 pha = 0.And mode0 is pol = 0 pha = 1.
But either way i could not get it working.
I changed the baudrate from 1Mhz to 300Khz

Another thing tha looks strange is that when i use the spi_selectChip() the specific pin doesnt go low its still high.I made the call and then stopped there(while(1)) but the pin was high.I want to use the AVR32_SPI1_NPCS_0_0_PIN which is defined with the value 41(PB09)

And i still have the same problem with spi_read().
Should i use instead spi_get() and spi_put() or stick with the spi_read() spi_write();
It cant read the rdr register for some reason

Besides that the code is the same.

I even tried the same code with spi0 and got the same errors.
The spi_read() doesnt read.
Is there anything that i might forget?

Do i have to init the clock of the spi?
Code:
sysclk_enable_peripheral_clock(&AVR32_SPI1);
or
sysclk_enable_pba_module(SYSCLK_SPI1);
 
 View user's profile Send private message  
Reply with quote Back to top
mikech
PostPosted: Apr 24, 2012 - 11:54 PM
Hangaround


Joined: Aug 19, 2003
Posts: 396
Location: Australia

Quote:
Are you sure about SPI_MODE_0?In the datasheet says tha mode1 is pol = 0 pha = 0.And mode0 is pol = 0 pha = 1.
Which datasheet ?, if you look at the Atmel AVR32xxx datasheets mode 0 is cpol=0 and ncpha=1. Why they did that inversion I do not know.
SD card clocks data on the rising edge of the clock, so atmel ncpha must be 1.

I still don't know which processor you are using, but usually after reset the PBACLOCK is enabled and it usually drives the SPI modules, so explicitly enabling the clocks won't hurt.

If your chip select line does not go low then the SD card will never respond.
After looking through 6 datasheets, the only processor that has any SPI-1 chip select function on PB09 is the UC3A3/4xxx., is that the processor you are using ???????
 
 View user's profile Send private message  
Reply with quote Back to top
giannhssdra
PostPosted: Apr 25, 2012 - 12:46 AM
Wannabe


Joined: Apr 03, 2010
Posts: 82


The processor im using is uc3a3256.
The NPCS_0 is PB09.
What i cant understand from this table is the different functionalities of the pins.PB09 has 3 gpio functions and the 3rd is EBI - NCS[4],and im already using the ebi for the external ram.But even if i change to spi0 it doesnt work.

I dont know if i have to change anything for the clock of the spi
Here is clock_config.h
Code:
#define CONFIG_SYSCLK_INIT_CPUMASK   (0)
#define CONFIG_SYSCLK_INIT_PBAMASK   (0)
#define CONFIG_SYSCLK_INIT_PBBMASK   (0)
#define CONFIG_SYSCLK_INIT_HSBMASK   (0)

#define CONFIG_SYSCLK_SOURCE         (SYSCLK_SRC_OSC0)
#define CONFIG_SYSCLK_CPU_DIV        (0)
#define CONFIG_SYSCLK_PBA_DIV        (0)
#define CONFIG_SYSCLK_PBB_DIV        (0)
#define CONFIG_PLL0_SOURCE           (PLL_SRC_OSC0)
#define CONFIG_PLL0_MUL              (48000000UL / BOARD_OSC0_HZ)
#define CONFIG_PLL0_DIV              (1)
#define CONFIG_USBCLK_SOURCE         (USBCLK_SRC_OSC0)
#define CONFIG_USBCLK_DIV            (1)
#define CONFIG_PLL1_SOURCE           (PLL_SRC_OSC0)
#define CONFIG_PLL1_DIV              (2)
#define CONFIG_PLL1_MUL              (8)


and in init_startup() im changing to external osc
Code:
pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
 
 View user's profile Send private message  
Reply with quote Back to top
mikech
PostPosted: Apr 25, 2012 - 02:20 AM
Hangaround


Joined: Aug 19, 2003
Posts: 396
Location: Australia

I am very confused.
pm_switch_to_osc0(,,) does not use anything from clock_config.h

Are you using sysclk_init() to setup the system clocks ??, if yes then your
Quote:
#define CONFIG_SYSCLK_INIT_PBAMASK (0)
will only enable the PBA clock to the GPIO, INT and PM modules.
See Table 7-7 in the datasheet and sysclk_init() in sysclk.c
Make CONFIG_SYSCLK_INIT_PBAMASK = 0x0040 (as a minimum) or use sysclk_enable_peripheral_clock(&AVR32_SPI1) if you want the clock to go to the SPI-1 module.
 
 View user's profile Send private message  
Reply with quote Back to top
giannhssdra
PostPosted: Apr 25, 2012 - 05:42 PM
Wannabe


Joined: Apr 03, 2010
Posts: 82


Thanks for the help.
I still cant get it working.Im sending you 3 files that im using the spi driver.Im using TC0 for an interrupt at 10ms , UART2 for the stdio_usb driver and i have my own init_startup() to enable the sdram in order for the heap to be placed there.Everything works except the spi driver.

I was using SPI1 but changed to SPI0 since SPI1 shares functionallity with EBI.But even with SPI0 i got nothing.
SS - PA07 (NPCS03)
MOSI - PA10
MISO - PA11
SCK - PA08

Atmels tecnical support sent me this as a dummy write in order to read the spi
Code:
static
BYTE rcvr_spi (void)
{
    uint16_t data;
    if(spi_write(&AVR32_SPI0,0x55)==SPI_OK)
        spi_read(&AVR32_SPI0,&data);
    else
      return (uint8_t)SPI_ERROR;
 
    return (uint8_t)data;
}


You will find the code for the spi configuration in diskio.c and at the end of main as comments.Also check the cllock_config.h to see my clocks and main.c.

I still have the same problems with the spi_read() and the chipSelect.Even if i start a new project with just the spi driver the CS pin doesnt go low.I think that im missing something with the clocks because it seems that spi is disabled.

Thanks again
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits