ATTiny 4313 UART as SPI

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

Look out! It's me again

 

I'm trying to use the UART for SPI.

 

DO is Port D 1

DI is Port D 0

XCK is PORT D 2

 

But I don't think my uart init is right as I don't get any clock signal when I try to write 0xAA repetedly.

 

void SpiSetup()
{
	  DDRD |= (1<<1); // as output (DO)
	  DDRD |= (1<<2); // as output (USISCK)
	  DDRD &= ~(1<<0); // as input (DI)
	  UCSRB = (1<<RXEN)|(1<<TXEN);
	  UCSRC = (1<<UCSZ1)|(1<<UCSZ0);
	  UBRRL = 0; // go fast on 1MHz clock
	  UBRRH = 0;
}

uint8_t SpiTransfer( uint8_t B)
{
	while( (UCSRA & (1<<UDRE)) == 0) ;
	UDR = B ;
	while( (UCSRA & (1<<RXC)) == 0) ;
	return UDR ;
}

(I've always used the SPI peripheral before.)

 

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

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

USART in MSPI mode should use RXD/TXD/XCK signals on PD0, PD1, PA0.

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

You have set the UCSZ bits for 8-N-1 UART.   You should set the UMSEL bits for MSPI.  e.g.

void spi_config(unsigned char master, unsigned char mode, unsigned char speed)
{
    PORTB |= (1<<SS);     // pull-up i.e. disabled
    DDRB |= (1<<SS);
    UBRR0 = 0;
    XCK0_DDR |= (1<<XCK0);
    UCSR0C = (3<<UMSEL00)|mode;
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);
    UBRR0 = speed;
}

You probably use mode#0 and speed=0 i.e. F_CPU/2 which gives you 8MHz SPI on a 16MHz tiny4313

XCK is on PD2 for tiny2313A, 4313

You can choose any GPIO pin for CS.   (my naming SS is inappropriate since MSPI can never be a Slave)

 

USART_MSPI mode has been available on Mega328, 324 family chips ever since they were born.

USART_MSPI mode only appeared with Tiny2313A (and Tiny4313).   It was never on Tiny2313.

 

David.

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

David wins the kewpie doll! Now I see the signals.

 

Thanks, guys. I was getting myself all in a tizzy over that.

 

(The example in the PDF doesn't seem to match the .h file's names.)

 

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

Last Edited: Sat. Feb 22, 2020 - 04:30 PM