Yahoo Groups archive

AVR-Chat

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

Message

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

2013-12-09 by Philippe Habib

First I want to offer a late thank you for everyone who took the time to help me out.  Even though this wasn't an AVR problem, I knew that this group's great experience and willingness to help would be what I needed.

Because of shifting project priorities and a death in the family, I wasn't able to get back to the messages or to my project for a few days.

David caught my bug where I was clearing out the entire register instead of only the bit I was interested in.  He was also right about single bit types.  It happened that my particular part has 4 ports, and port 1-3 are bit selectable, while port 4 is not so I had to do the ANDing and ORing for that port.

In the end, the hardware I was controlling was a much larger contributor to the overall time consumed from command to completion that optimizing the SPI to pick up a microsecond or two didn't turn out to be worthwhile.

I got all of the firmware features done in plenty of time and the HW designer now has a whole week to tune his stuff prior to delivery so we can all live happily ever after.

Thanks again for the assistance, this is the the best group I'm on.

----- Original Message -----
From: "David Kelly" <dkelly@hiwaay.net>
To: AVR-Chat@yahoogroups.com
Sent: Tuesday, November 26, 2013 7:32:10 AM
Subject: Re: [AVR-Chat] Help with bit banged spi


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.



------------------------------------

Yahoo Groups Links

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.