CVS: examples/libraries/spi README.txt,NONE,1.1 main.c,NONE,1.1 makefile,NONE,1.1
Chris Liechti <[email protected]>
| Newsgroups | gmane.comp.hardware.texas-instruments.msp430.gcc.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/mspgcc/examples/libraries/spi
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9169/examples/libraries/spi
Added Files:
README.txt main.c makefile
Log Message:
adding the next example: libspi basic use
--- NEW FILE: README.txt ---
Software SPI master
===================
Overview
--------
It is a simple example project for the MSP430 series MCU, the GCC port
of the mspgcc project and libspi.
Features:
- how to set up libspi
- Makefile
- compile and link with libmspgcc and libspi
- convert to intel hex format
- generate a listing with mixed C / assembly
libspi
------
The library implements a SPI master in software. It can be used on any MSP430
pin. The function saccessing the pins have to be implemened by the user, as
shown in this example.
``void spi_push(unsigned char width, unsigned long data)``
Shift out only, up to 32 bits. This function is useful if the data has
to be shifted into a simple shift register.
``void spi_transfer(void *output, const void *input, int length)``
SPI read/write function. ``length`` bytes are shited. if output is
``NULL``, 0xff is shifted out, otherwise each btye after the other
from the array.
While each byte is shifted out an other is read. If ``input`` is not
``NULL`` are these bytes stroed in this array.
The caller must ensure that there is enough space in both arrays.
``unsigned char spi_shift(unsigned char data)``
This function simply shifts out one byte and returns the shifted in data.
Required hardware
-----------------
- A MSP430F1121 or larger device (any from the F1x series)
- A SPI slave or a shift register connected to::
P1.0|-->CLK
P1.1|-->DATA OUT
P1.2|<--DATA IN (not used in the exmaple)
P1.3|<--CS ("load data" if a shift register is connected)
Disclaimer
----------
This example is part of the mspgcc project http://mspgcc.sf.net.
See license.txt_ for details.
chris
.. _license.txt: ../../license.html
--- NEW FILE: main.c ---
/* Demo application for thesoftware SPI master in libspi
*
* All numbers form 0..255 are shifted out
*
* MSP430F1121
* -----------------
* /|\| |
* | | |
* --|RST P1.0|-->CLK
* | P1.1|-->DATA OUT
* | P1.2|<--DATA IN
* | P1.3|<--CS ("load data" if a shift register is connected)
* | |
*
* (c) 2005, chris <[email protected]>
*/
#include <io.h>
#include <signal.h>
#include <mspgcc/util.h> // delay()
#include <spi.h>
// ____ _ _ _ _ _ _ _ ____
// CLK | | | | | | | | | | | | | | | |
// |_| |_| |_| |_| |_| |_| |_| |_|
// _____ ___ _______ _____
// OUT | | | | | |
// |___| |___| |_______|
// 0 1 0 1 1 0 0 1
void SPI_CS_SET(void) { ///< set chip select
P1OUT &= ~BIT3;
}
void SPI_CS_CLEAR(void) { ///< clear chip select
P1OUT |= BIT3;
}
void SPI_CLK_SET(void) { ///< set clock line, may add delay before edge to slow down
delay(500);
P1OUT |= BIT0;
delay(500);
}
void SPI_CLK_CLEAR(void) { ///< clear clock line, may add delay before edge to slow down
delay(500);
P1OUT &= ~BIT0;
delay(500);
}
void SPI_DATA_SET(void) { ///< set data line (master out)
P1OUT |= BIT1;
}
void SPI_DATA_CLEAR(void) { ///< clear data line (master out)
P1OUT &= ~BIT1;
}
int SPI_DATA_READ(void) { ///< read data line (master in)
return P1IN & BIT2;
}
// program entry. set up the hardware and launch the eventhandler
int main(void) {
WDTCTL = WDTPW|WDTHOLD; // stop watchdog timer
P1OUT = BIT0|BIT1|BIT3; // outputs are idle high
P1DIR = BIT0|BIT1|BIT3; // set outputs
while (1) {
for (int i=0; i<256; i++) {
spi_shift(i);
delay(10000);
}
}
}
--- NEW FILE: makefile ---
# makfile configuration
NAME = spi_demo
CSOURCES = main.c
#~ ASOURCES =
CPU = msp430x1121
ASFLAGS = -mmcu=${CPU} -D_GNU_ASSEMBLER_ -I .
CFLAGS = -mmcu=${CPU} -O2 -Wall -g --std=gnu99 -I .
LDFLAGS = -lmspgcc -lspi
#switch the compiler (for the internal make rules)
CC = msp430-gcc
AS = msp430-gcc
OBJECTS = ${CSOURCES:.c=.o} ${ASOURCES:.S=.o}
.PHONY: all FORCE clean download download-jtag download-bsl
#all should be the first target. it's built when make is run without args
all: ${NAME}.elf ${NAME}.a43 ${NAME}.lst dependencies.d
#confgigure the next line if you want to use the serial download
download: download-jtag
#~ download: download-bsl
#additional rules for files
${NAME}.elf: ${OBJECTS}
${CC} -mmcu=${CPU} -o $@ ${OBJECTS} $(LDFLAGS)
${NAME}.a43: ${NAME}.elf
msp430-objcopy -O ihex $^ $@
${NAME}.lst: ${NAME}.elf
msp430-objdump -dSt $^ >$@
@echo "----- RAM/Flash Usage -----"
msp430-size $^
download-jtag: all
msp430-jtag -e ${NAME}.elf
download-bsl: all
msp430-bsl $(BSLOPT) -e ${NAME}.elf
clean:
rm -f ${NAME}.elf ${NAME}.a43 ${NAME}.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
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click