spi "freezes"

Discussions on the software and hardware for Atmel's STK standard.

Moderator: phalanx

Post Reply
thebignoob
Posts: 2
Joined: Mon Aug 18, 2014 6:14 am

spi "freezes"

Post by thebignoob » Mon Aug 18, 2014 6:19 am

Hello,
I have an issue with spi that freezes. I'm using atmega88 with nrf24l01 but when i try to test spi communication i get stuck.
I initialize spi based on atmel specifications:

Code: Select all

DDRB |= (1<<DDB5) | (1<<DDB3) | (1<<DDB2) |(1<<DDB1);
SPCR |= (1<<SPE)|(1<<MSTR);
And when I try to test out writing to spi (and print out to terminal ) i get stuck looping... ( i have usart initialized and used without any problems)

Code: Select all

char spi_rw(unsigned char x)
{
    SPDR = x;   
    //get stuck in this while loop
    while(!(SPSR & (1<<SPIF)));
    //printing to terminal after get's out of loop but never go out   
    return SPDR;
}
I'm trying to read STATUS register:

Code: Select all

uint8_t get_reg(uint8_t reg)
{   

    _delay_us(10);
    CLEARBIT(PORTB, 2);    //CSN low so nrf start listen for command
    _delay_us(10);
    spi_rw(R_REGISTER + reg);   
    _delay_us(10);
    reg = spi_rw(NOP);
    _delay_us(10);
    SETBIT(PORTB, 2);    //CSN IR_High nrf do nothing now
    return reg;   
}
I try calling get_reg(STATUS) and passing it to usart to print out resultand I get nowhere since code stuck in while loop.

P.S. i use this simple functions to set and clear bits

Code: Select all

#define BIT(x) (1<<(x))
#define SETBITS(x,y) ((x)|=(y)))
#define CLEARBITS(x,y) ((x) &=(~(y)))
#define SETBIT(x,y) SETBITS((x), (BIT((y))))
#define CLEARBIT(x,y) CLEARBITS((x), (BIT((Y))))
Thanks in advance, any help would be greatly appreciated. (sorry if i create thread on wrong place)

jremington
Support Volunteer
Posts: 2202
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: spi "freezes"

Post by jremington » Mon Aug 18, 2014 9:33 pm

This may not explain your particular problem, but the SPI will freeze if you forget to set SS (PORTB2) to high *before* initializing the SPI hardware, as SS low will make the controller switch to slave mode.

thebignoob
Posts: 2
Joined: Mon Aug 18, 2014 6:14 am

Re: spi "freezes"

Post by thebignoob » Tue Aug 19, 2014 12:04 am

Thanks,
that was a problem which i overlooked... I had badly organized project (and to tired and frustrated) with huge amount of code and added some other initialization that put B2 to low :( ... i fixed it before post was approved but thank you very much :)

Post Reply