CVS: libraries/spi makefile,NONE,1.1 shiftregister.c,NONE,1.1 spi_shift.c,NONE,1.1 spi_transfer.c,NONE,1.1

Chris Liechti <[email protected]>
Newsgroups gmane.comp.hardware.texas-instruments.msp430.gcc.cvs
Message-ID <[email protected]>
Update of /cvsroot/mspgcc/libraries/spi
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20805/libraries/spi

Added Files:
	makefile shiftregister.c spi_shift.c spi_transfer.c 
Log Message:
initial import of spi library

features a software SPI master, a  function to access a shiftregister, and an io function for SPI transfers

--- NEW FILE: makefile ---
# makfile configuration
LIBRARYNAME     = spi
CSOURCES        = shiftregister.c spi_shift.c spi_transfer.c
#~ ASOURCES        = 
CPU             = msp1

ASFLAGS         = -mmcu=${CPU} -D_GNU_ASSEMBLER_ -I ../include
CFLAGS          = -mmcu=${CPU} -O2 -Wall -std=gnu99 -g -I ../include

#switch the compiler (for the internal make rules)
CC              = msp430-gcc
AS              = msp430-gcc
AR              = msp430-ar

OBJECTS         = ${CSOURCES:.c=.o} ${ASOURCES:.S=.o}

.PHONY: all FORCE clean

#all should be the first target. it's built when make is run without args
all: lib$(LIBRARYNAME).a lib$(LIBRARYNAME).lst dependencies.d

lib$(LIBRARYNAME).a: $(OBJECTS)
	$(AR) rcf $@ $^

lib$(LIBRARYNAME).lst: lib$(LIBRARYNAME).a
	msp430-objdump -dSt $^ >$@
	@echo "----- RAM/Flash Usage -----"
	msp430-size $^

clean:
	rm -f lib$(LIBRARYNAME).a lib$(LIBRARYNAME).lst ${OBJECTS} dependencies.d


#dummy target as dependecy if something has to be build everytime
FORCE:

#project dependencies
dependencies.d:
	$(CC) -MM ${CFLAGS} ${CSOURCES} > dependencies.d
ifdef ASOURCES
	$(CC) -MM ${ASFLAGS} ${ASOURCES} >> dependencies.d
endif

-include dependencies.d
--- NEW FILE: shiftregister.c ---
#include "spi.h"

// simple shift only function
// useful for simple shift registers
void spi_push(unsigned char width, unsigned long data) {
    SPI_CLK_SET();              //ensure propper start
    SPI_CS_CLEAR();             //begin transmit
    while(width--) {
        SPI_CLK_CLEAR();        //begin bit
        if (data & 1) {         //check bit and set data line accordingly
            SPI_DATA_SET();
        } else {
            SPI_DATA_CLEAR();
        }
        SPI_CLK_SET();          //end bit
        data >>= 1;
    }
    SPI_CS_SET();               //end transmit
    SPI_DATA_SET();             //return to idle position
}

--- NEW FILE: spi_shift.c ---
#include "spi.h"

// shift one byte out while reading one
unsigned char spi_shift(unsigned char data) {
    unsigned char result = 0;
    SPI_CLK_SET();              //ensure propper start
    SPI_CS_CLEAR();             //begin transmit
    for (int width=8; width; width--) {
        SPI_CLK_CLEAR();        //begin bit
        if (data & 0x80) {      //check bit and set data line accordingly
            SPI_DATA_SET();
        } else {
            SPI_DATA_CLEAR();
        }
        SPI_CLK_SET();          //end bit
        data <<= 1;
        result <<= 1;
        result |= SPI_DATA_READ() ? 1 : 0;
    }
    SPI_CS_SET();               //end transmit
    SPI_DATA_SET();             //return to idle position
    return result;
}

--- NEW FILE: spi_transfer.c ---
#include "spi.h"

// send and receive an array of bytes
// either one of the buffers may be NULL, in that case it is ignored:
// if output is NULL -> send 0xff
// if input is NULL -> forget received data
// it is valid, tough not very useful to set both pointers to NULL

void spi_transfer(void *output, const void *input, int length) {
    unsigned char in;
    unsigned char out = 0xff;
    
    while (length--) {
        if (output) out = *((unsigned char *)output)++;
        in = spi_shift(out);
        if (input) *((unsigned char *)input)++ = in;
    }
}



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.