DB101 and its Dataflash - can't get USART0 to work

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

Hello,
I have been stuck for days now.
I am using the latest AVR Studio and WINAVR. I am trying to talk to the dataflash on the DB101 module. It uses USART0 as SPIM.

Code:

void DF_SPI_init (void)
{
UBRR0 = 0;
PORTG |= (1<<PG5);
DDRG |= (1<<DDG5); // set SS as outputs

UCSR0C = (1<<UMSEL01) | (1<<UMSEL00) | (1<<UCSZ00) | (1<<UCPOL0); //Enable SPI in Master mode, mode 3
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
UBRR0 = BAUD;
PORTG &= ~(1<<PG5);
}
I used debug stepping into it to see if the registers were set to right numbers, but they were not.

UCSR0C and UCSR0B were unchanged though this call.

Any help will be greatly appreciated.

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

Hm, the datasheet itself rather confusing when using USART as real USART or as SPIM.
After looking the datasheet a while, looks like there are some mistake in your code.
It think

UCSR0C = (1<<UMSEL01) | (1<<UMSEL00) | (1<<UCSZ00) | (1<<UCPOL0); //Enable SPI in Master mode, mode 3 

is valid for USART mode only since in SPIM mode there are no UCSZ00 bit, it is replaced with UCPHA0.
But I think this is still valid for the compiler itself. Have you ever tried the datasheet's example? Below is the copy-pasted code from datasheet page 235

void USART_Init( unsigned int baud )
{
UBRRn = 0;
/* Setting the XCKn port pin as output, enables master mode. */
XCKn_DDR |= (1<<XCKn);
/* Set MSPI mode of operation and SPI data mode 0. */
UCSRnC = (1<<UMSELn1)|(1<<UMSELn0)|(0<<UCPHAn)|(0<<UCPOLn);
/* Enable receiver and transmitter. */
UCSRnB = (1<<RXENn)|(1<<TXENn);
/* Set baud rate. */
/* IMPORTANT: The Baud Rate must be set after the transmitter is enabled
*/
UBRRn = baud;
}

KISS - Keep It Simple Stupid!

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

Thanks a lot m23402027.
The header file cames with WinAVR does not have UCPHA0 defined - that is why I used UCSZ00.
I also checked the assembly output and could not find anything wrong:

   a6ce:	a4 ec       	ldi	r26, 0xC4	; 196
    a6d0:	b0 e0       	ldi	r27, 0x00	; 0
    a6d2:	11 96       	adiw	r26, 0x01	; 1
    a6d4:	1c 92       	st	X, r1
    a6d6:	1e 92       	st	-X, r1
    a6d8:	a5 9a       	sbi	0x14, 5	; 20
    a6da:	8d b1       	in	r24, 0x0d	; 13
    a6dc:	86 60       	ori	r24, 0x06	; 6
    a6de:	8d b9       	out	0x0d, r24	; 13
    a6e0:	9d 9a       	sbi	0x13, 5	; 19
    a6e2:	e1 ec       	ldi	r30, 0xC1	; 193
    a6e4:	f0 e0       	ldi	r31, 0x00	; 0
    a6e6:	80 81       	ld	r24, Z
    a6e8:	88 61       	ori	r24, 0x18	; 24
    a6ea:	80 83       	st	Z, r24
    a6ec:	e2 ec       	ldi	r30, 0xC2	; 194
    a6ee:	f0 e0       	ldi	r31, 0x00	; 0
    a6f0:	80 81       	ld	r24, Z
    a6f2:	83 6c       	ori	r24, 0xC3	; 195
    a6f4:	80 83       	st	Z, r24
    a6f6:	80 81       	ld	r24, Z
    a6f8:	80 93 0c 1b 	sts	0x1B0C, r24
    a6fc:	80 81       	ld	r24, Z
    a6fe:	80 93 0c 1b 	sts	0x1B0C, r24
    a702:	8f e2       	ldi	r24, 0x2F	; 47
    a704:	90 e0       	ldi	r25, 0x00	; 0
    a706:	11 96       	adiw	r26, 0x01	; 1
    a708:	9c 93       	st	X, r25
    a70a:	8e 93       	st	-X, r24
    a70c:	08 95       	ret

Note: I added two read to a variable for debug.

The strange thing is that nothing gets changed running through this code. The read out from UCSR0C is still the default 0x06.

I am running this on DB101 board with no modifications.
I also used a scope to look at the signal. The CS signal is good. The SI and SCK on the dataflash stays low through this code.

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

Hi all,
I have found the problem - USART0 power is not on.
I thought I was smart - guess not all the time :-)