Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: [AVR-Chat] Help with bit banged spi

2013-11-26 by David Kelly

On Nov 26, 2013, at 4:46 AM, Cagri Tanriover <dr_cagri_tanriover@yahoo.co.uk> wrote:

>          P4 = ( *(SPIOutBuf + j) & 0x80 ? P4 | 1 : 0 );

I suggest looking at the compiler output listing in assembly language.

Philippe is using an 8051 variant so I doubt he is using gcc, but in my observation with optimization enabled gcc does better with SPIOutBuf[j] than with *(SPIOutBuf + j). Other compilers appreciate having the pointers broken out manually, namely classic 68k compilers under Unix.

8051's have single-bit types and perhaps P4 has been defined as an output bit. If so AND-ing and OR-ing is useless. Hopefully the compiler optimized them out.

While we are at it this doesn't look right: P4 &= 0; That clears all bits in P4. How about P4 &= ~1; which is 0b1111 1110 and only clears the zeroth bit and is exactly as fast at run time? Of course that only matters if P4 is larger than 1 bit wide. But if P4 is a bit variable then use P4 = 1; and P4 = 0;

I think it might be best to unroll the bit shifting. This also has the possible advantage of not destroying the original buffer:

for( j=0 ; j<Num ; j++ ) {
        Tmp = SPIOutBuf[j];

        if( Tmp & (1<<7) )
            P4 |= 1;	
        else
            P4 &= ~1;
        SPICLK = 1;
        PICLK = 0;

        if( Tmp & (1<<6) )
            P4 |= 1;	
        else
            P4 &= ~1;
        SPICLK = 1;
        PICLK = 0;

        if( Tmp & (1<<5) )
            P4 |= 1;	
        else
            P4 &= ~1;
        SPICLK = 1;
        PICLK = 0;

        if( Tmp & (1<<4) )
            P4 |= 1;	
        else
            P4 &= ~1;
        SPICLK = 1;
        PICLK = 0;

        if( Tmp & (1<<3) )
            P4 |= 1;	
        else
            P4 &= ~1;
        SPICLK = 1;
        PICLK = 0;

        if( Tmp & (1<<2) )
            P4 |= 1;	
        else
            P4 &= ~1;
        SPICLK = 1;
        PICLK = 0;

        if( Tmp & (1<<1) )
            P4 |= 1;	
        else
            P4 &= ~1;
        SPICLK = 1;
        PICLK = 0;

        if( Tmp & (1<<0) )
            P4 |= 1;	
        else
            P4 &= ~1;
        SPICLK = 1;
        PICLK = 0;
}

--
David Kelly N4HHE, dkelly@HiWAAY.net
============================================================
Whom computers would destroy, they must first drive mad.

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.