Jan,
You can also use the following words to avoid c!@spi, which is missing from the
preassembled package for the Arduino:
\ send a byte, ignore received byte
: c!spi ( c -- )
SPDR c! ( c addr -- )
;
\ receive a byte, send a dummy one
: c@spi ( -- c)
0 SPDR c! 1 ms SPDR c@
;
SPDR is a constant holding the address of the SPI data register (&78)
Peter
Below the complete package, which we have been using successfully (turn on and
off SPI before reading or writing via SPI with +SPI and -SPI):
\ basic SPI operations for a single device
\ after Trute, but not using the assembly word c!@spi,
\ which is not available preloaded for Arduino Uno
\ and did not work when included in a newly assembled package
-spiwords
marker -spiwords
\ SPI
&78 constant SPDR \ SPI Data Register
&77 constant SPSR \ SPI Status Register
$80 constant SPSR_SPIF \ SPI Interrupt Flag
$40 constant SPSR_WCOL \ Write Collision Flag
$01 constant SPSR_SPI2X \ Double SPI Speed Bit
&76 constant SPCR \ SPI Control Register
$80 constant SPCR_SPIE \ SPI Interrupt Enable
$40 constant SPCR_SPE \ SPI Enable
$20 constant SPCR_DORD \ Data Order
$10 constant SPCR_MSTR \ Master/Slave Select
$08 constant SPCR_CPOL \ Clock polarity
$04 constant SPCR_CPHA \ Clock Phase
$03 constant SPCR_SPR \ SPI Clock Rate Selects
\ SPI hardware pins
PORTB 5 portpin: spi.clk
PORTB 3 portpin: spi.mosi
PORTB 4 portpin: spi.miso
PORTB 2 portpin: spi.ss \ default
SPSR 0 portpin: spi.2x
SPCR 6 portpin: spi.enable
SPCR 5 portpin: spi.dord
SPCR 4 portpin: spi.master
SPCR %00001100 bitmask: spi.mode
SPCR %00000011 bitmask: spi.speed
$0 constant spi.mode0 \ sample rising/--
$4 constant spi.mode1 \ --/sample falling
$8 constant spi.mode2 \ sample falling/--
$c constant spi.mode3 \ --/sample rising
0 constant spi.f/4
1 constant spi.f/16
2 constant spi.f/64
3 constant spi.f/128
\ check SPI device datasheet for mode settings
: spi.setmode ( spi-mode -- )
spi.mode pin!
;
\ speed relative to f_cpu, constants see above
: spi.setspeed ( spi.speed -- )
spi.speed pin!
;
: +spi
\ Slave select *must* be *always* at a controlled level when SPI is
activated.
\ Changing a pin into output mode change its level to low. that makes a SPI
think
\ a communication has started which is not the case when this word is called.
spi.ss high \ deselect slave
spi.ss pin_output \ possibly short low pulse
spi.ss high \
spi.f/64 spi.setspeed
spi.clk pin_output
spi.mosi pin_output
spi.miso pin_input
spi.miso pin_pullup_on
\ now its save to turn on the SPI module
spi.master high
spi.enable high
;
: -spi spi.ss high ;
\ send a byte, ignore received byte
: c!spi ( c -- )
SPDR c! ( c addr -- )
;
\ receive a byte, send a dummy one
: c@spi ( -- c)
0 SPDR c! 1 ms SPDR c@
;
> On 7 Jun 2019, at 20:16, Jan Kromhout via Amforth-devel
> <[email protected]> wrote:
>
> Hello Tristan,Erich
>
> This is far over my knowledge, but will give it a try.
>
> But when I try to load the spi.frt I get an error here
>
> |C| 97|\ send a byte, ignore recieved byte
> |S| 98|: c!spi ( c -- )
> |S| 99| c!@spi drop
> |E= ?? -13 6
>
> Is this also a assembler word?
>
> Kindly regards,
>
> Jan
>
>
>
>
>> Op 7 jun. 2019, om 19:50 heeft Erich Wälde <[email protected]> het
>> volgende geschreven:
>>
>> Hello Jan,
>>
>>
>> Jan Kromhout writes:
>>
>>> Hi Tristan,
>>>
>>> What to load in the right sequence to fellow the examples in
>>> http://amforth.sourceforge.net/TG/recipes/SPI.html ?
>>> If I have the right sequence of loading the screens I will start as you
>>> mentiod.
>>>
>>> Kind regards,
>>>
>>> Jan
>>>
>>>
>>>
>>> Op 7 jun. 2019, om 19:25 heeft Tristan Williams
>>> <[email protected]<mailto:[email protected]>> het volgende geschreven:
>>>
>>> Hi Jan,
>>>
>>> No don’t have. Why?
>>>
>>> Because words/spirw.asm provides c!@spi which makes using the
>>> hardware spi easier, and it is used in the recipes
>>
>> you see the filename? "words/spirw.asm"? Please note: .asm
>> suffix. This means that in your project directory, you need to
>> add one line to the file "dict_appl.inc". Then you need to
>> reassemble the project and load the resulting .hex files to your
>> controller. I strongly recommend learning this workflow, if you
>> didn't already.
>>
>> Cheers,
>> Erich
>>
>>
>>>
>>> http://amforth.sourceforge.net/TG/recipes/SPI.html
>>>
>>> Separately, if you haven't read it already
>>>
>>> https://en.wikipedia.org/wiki/Serial_Peripheral_Interface
>>>
>>> will help a lot, as will starting with a simple SPI device (e.g. io
>>> expander, digital potentiometer) first.
>>>
>>> Kind regards,
>>>
>>> Tristan
>>>
>>>
>>> Verstuurd vanaf mijn iPad
>>>
>>> Op 7 jun. 2019 om 17:25 heeft Tristan Williams
>>> <[email protected]<mailto:[email protected]>> het volgende geschreven:
>>>
>>> Hello Jan,
>>>
>>> A quick question first.
>>>
>>> You have built your AmForth hex files with words/spirw.asm ?
>>>
>>> Kind regards,
>>>
>>> Tristan
>>>
>>> On 07Jun19 17:06, Jan Kromhout via Amforth-devel wrote:
>>> Hello
>>>
>>> I have take a close look into SPI routines.
>>> I really not understand them.
>>>
>>> I need simple make a connection withe the arduino in amForth.
>>> The basics I understand how to make a pin high or low etc.
>>> But I don’t know how to start to initialize the SPI etc.
>>> Can someone help me with this or give a simple example?
>>> The interface is using the standard pins for the SPI.
>>>
>>> I mark the part of the code with <===========? where I have trouble to
>>> convert to amForth.
>>>
>>> Thanks for any help.
>>>
>>> Cheers,
>>>
>>> Jan
>>>
>>>
>>> #include "SPI.h"
>>>
>>> #define SCK_PIN 13
>>> #define MISO_PIN 12
>>> #define MOSI_PIN 11
>>> #define SS_PIN 10
>>>
>>> void umFPU_begin(void)
>>> {
>>> digitalWrite(SS_PIN, HIGH);
>>> pinMode(SS_PIN, OUTPUT);
>>> umFPU_reset();
>>> }
>>>
>>> //------------------- reset
>>> -------------------------------------------------
>>>
>>> void umFPU_reset()
>>> {
>>> digitalWrite(SS_PIN, LOW);
>>>
>>> // disable SPI.Master
>>> SPI.end(); <===========?
>>>
>>> // reset the FPU
>>> digitalWrite(MOSI_PIN, HIGH);
>>> for (byte i = 0; i < 80; i++)
>>> {
>>> digitalWrite(SCK_PIN, HIGH);
>>> digitalWrite(SCK_PIN, LOW);
>>> }
>>> digitalWrite(MOSI_PIN, LOW);
>>>
>>> delay(10);
>>>
>>> // enable SPI.Master
>>> SPI.setDataMode(SPI_MODE0);
>>> SPI.setBitOrder(MSBFIRST);
>>> SPI.setClockDivider(SPI_CLOCK_DIV4);
>>> SPI.begin(); <===========?
>>>
>>> digitalWrite(SS_PIN, HIGH);
>>> }
>>>
>>> byte umFPU_read(void)
>>> {
>>> byte bval;
>>> digitalWrite(SS_PIN, LOW);
>>> umFPU_readDelay();
>>> bval = SPI.transfer(0); <===========?
>>> digitalWrite(SS_PIN, HIGH);
>>> return bval;
>>> }
>>>
>>> void umFPU_write_1(byte b1)
>>> {
>>> digitalWrite(SS_PIN, LOW);
>>> SPI.transfer(b1); <===========?
>>> digitalWrite(SS_PIN, HIGH);
>>> }
>>>
>>> _______________________________________________
>>> Amforth-devel mailing list for http://amforth.sf.net/
>>> [email protected]<mailto:[email protected]>
>>> https://lists.sourceforge.net/lists/listinfo/amforth-devel
>>>
>>>
>>> _______________________________________________
>>> Amforth-devel mailing list for http://amforth.sf.net/
>>> [email protected]<mailto:[email protected]>
>>> https://lists.sourceforge.net/lists/listinfo/amforth-devel
>>>
>>>
>>>
>>> _______________________________________________
>>> Amforth-devel mailing list for http://amforth.sf.net/
>>> [email protected]<mailto:[email protected]>
>>> https://lists.sourceforge.net/lists/listinfo/amforth-devel
>>>
>>>
>>> _______________________________________________
>>> Amforth-devel mailing list for http://amforth.sf.net/
>>> [email protected]<mailto:[email protected]>
>>> https://lists.sourceforge.net/lists/listinfo/amforth-devel
>>>
>>>
>>> _______________________________________________
>>> Amforth-devel mailing list for http://amforth.sf.net/
>>> [email protected]
>>> https://lists.sourceforge.net/lists/listinfo/amforth-devel
>>
>>
>> --
>> May the Forth be with you ...
>>
>>
>> _______________________________________________
>> Amforth-devel mailing list for http://amforth.sf.net/
>> <http://amforth.sf.net/>
>> [email protected]
>> <mailto:[email protected]>
>> https://lists.sourceforge.net/lists/listinfo/amforth-devel
>> <https://lists.sourceforge.net/lists/listinfo/amforth-devel>
>
> _______________________________________________
> Amforth-devel mailing list for http://amforth.sf.net/
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/amforth-devel
_______________________________________________
Amforth-devel mailing list for http://amforth.sf.net/
[email protected]
https://lists.sourceforge.net/lists/listinfo/amforth-devel