Need help with little assembly language code

Can anyone help me convert the below assembly language code to Arduino(Embedded C) Code. I'm trying to program ATTiny24 as an SPI slave to establish communication between it and arduino NANO(Master).

init:
       ldi r16,(1<<USIWM0)|(1<<USICS1)
       out USICR,r16
...
SlaveSPITransfer:
                    out USIDR,r16
                    ldi r16,(1<<USIOIF)
                    out USISR,r16
SlaveSPITransfer_loop:
                    in r16, USISR
                    sbrs r16, USIOIF                      //sbrs - skip if bit in register is set
                    rjmp SlaveSPITransfer_loop    //relative jump
                    in r16,USIDR
                    ret

I've only figured that the initialization part is written as: USICR = USICR | (1<<USIWM0)|(1<<USICS1);

Thanks.

void SPIBegin()
{
  // Set Wire Mode 1: Three-wire mode. Uses DO, DI, and USCK pins. 
  // Set clock select 4: External, positive edge
  USICR = (1 << USIWM0) | (1 << USICS1);
}


uint8_t SPITransfer(uint8_t data)
{
  USIDR = data; // Load the data register
  USISR = 1 << USIOIF; // Clear Counter Overflow Interrupt Flag (yes, by writing a 1 to it)

  // Wait for the Counter Overflow Interrupt Flag to be set 
  // which means the transfer is complete
  while (!(USISR && (1 << USIOIF))) {}

  return USIDR; // Return received data
}

johnwasser:

void SPIBegin()

{
 // Set Wire Mode 1: Three-wire mode. Uses DO, DI, and USCK pins.
 // Set clock select 4: External, positive edge
 USICR = (1 << USIWM0) | (1 << USICS1);
}

uint8_t SPITransfer(uint8_t data)
{
 USIDR = data; // Load the data register
 USISR = 1 << USIOIF; // Clear Counter Overflow Interrupt Flag (yes, by writing a 1 to it)

// Wait for the Counter Overflow Interrupt Flag to be set
 // which means the transfer is complete
 while (!(USISR && (1 << USIOIF))) {}

return USIDR; // Return received data
}

Thanks a lot bro...will implement this and let you know about the result.

kzkhan98:
Thanks a lot bro...will implement this and let you know about the result.

Doesn't your ATtiny core include an SPI library? It would be much easier to use an existing library than to re-invent the wheel.

johnwasser:
Doesn't your ATtiny core include an SPI library? It would be much easier to use an existing library than to re-invent the wheel.

No bro there's no such spi library for ATTiny :confused:...infact there's no dedicated spi port... you've to use usi to create spi hence the trouble of writing code

SPI support:
On the following chips, full SPI functionality is provided in hardware, and works identically to SPI on Atmega chips:

  • ATtiny828
  • ATtiny x7 (87/167) (it has both a USI and full SPI, but the SPI library will use the SPI hardware)
  • ATtiny x41 (441/841)
  • ATtiny x8 (48, 88)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.