CONFIG SPI

From MCS Wiki AVR
(Difference between revisions)
Jump to: navigation, search
(Created page with "= <span class="f_Header">Action</span> = Configures the SPI related statements. <span style="font-family: Arial;"> </span> <span style="font-family: Arial;"> </sp...")
 
 
(One intermediate revision by one user not shown)
Line 5: Line 5:
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>
  
<span style="font-family: Arial;">&nbsp;</span>
+
= <span style="font-size: 14pt; font-weight: bold;">Syntax for software SPI</span> =
  
= <span class="f_Header">Syntax for software SPI</span> =
+
<span class="f_Syntax">CONFIG SPI|SPISOFT&nbsp;</span>= SOFT, DIN = PIN, DOUT = PIN , SS = PIN|NONE, CLOCK = PIN , SPIIN=value , MODE=mode, speed=speed, setup=setup
 
+
<span class="f_Syntax">CONFIG SPI|SPISOFT&nbsp;</span>= SOFT, DIN = PIN, DOUT = PIN , SS = PIN|NONE, CLOCK = PIN , SPIIN=value , MODE=mode
+
  
 
<span class="f_Syntax">&nbsp;</span>
 
<span class="f_Syntax">&nbsp;</span>
Line 20: Line 18:
  
 
<span class="f_Syntax">&nbsp;</span>
 
<span class="f_Syntax">&nbsp;</span>
 +
 +
&nbsp;
 +
 +
When you just want to use one SPI slave chip using the HW SPI, use this&nbsp;: Config Spi = Hard , Interrupt = Off , Data_Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 128
 +
 +
When you want more details, read more about the details and options below.
  
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>
Line 25: Line 29:
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>
  
= <span class="f_Header">Remarks</span> =
+
= <span class="f_Header">Remarks software SPI</span> =
<div style="padding: 0px; margin: 0px 0px 0px 4px;">
+
<div style="font-size: 13.3333330154419px; padding: 0px; margin: 0px 0px 0px 4px;">
 
{| width="604" cellspacing="0" cellpadding="1" border="1" style="border: 2px solid rgb(0, 0, 0); border-spacing: 0px; border-collapse: collapse;"
 
{| width="604" cellspacing="0" cellpadding="1" border="1" style="border: 2px solid rgb(0, 0, 0); border-spacing: 0px; border-collapse: collapse;"
 
|- style="vertical-align: top;"
 
|- style="vertical-align: top;"
Line 33: Line 37:
  
 
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |  
 
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |  
SOFT
+
<span style="font-weight: bold;">SOFT</span>
  
for software emulation of SPI, this allows you to choose the PINS to use. Only works in master mode.
+
for software emulation of SPI, this allows you to choose the pins to use. Only works in master mode.
  
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>
  
HARD for the internal SPI hardware, that will use fixed pins of the microprocessor.
+
<span style="font-weight: bold;">HARD</span>&nbsp;for the internal SPI hardware, that will use fixed pins of the microprocessor.
  
 
|- style="vertical-align: top;"
 
|- style="vertical-align: top;"
Line 64: Line 68:
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>
  
Use NONE when you do not want the SS signal to be generated. See remarks
+
Use NONE when you do not want the SS signal to be generated. See remarks. Or as an alternative you can use&nbsp;: NOSS=1.
  
 
|- style="vertical-align: top;"
 
|- style="vertical-align: top;"
Line 79: Line 83:
 
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |  
 
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |  
 
Selects if MSB or LSB is transferred first.
 
Selects if MSB or LSB is transferred first.
 +
 +
For soft SPI you need to use the MODE option as well.
 +
 +
Otherwise only MSB order is available.
  
 
|- style="vertical-align: top;"
 
|- style="vertical-align: top;"
Line 86: Line 94:
 
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |  
 
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |  
 
Selects if the SPI is run in master or slave mode.
 
Selects if the SPI is run in master or slave mode.
 
|- style="vertical-align: top;"
 
| valign="top" width="22%" style="width: 129px; border: 1px solid rgb(0, 0, 0);" |
 
POLARITY
 
 
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |
 
Select HIGH to make the CLOCK line high while the SPI is idle. LOW will make clock LOW while idle.
 
 
|- style="vertical-align: top;"
 
| valign="top" width="22%" style="width: 129px; border: 1px solid rgb(0, 0, 0);" |
 
PHASE
 
 
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |
 
Refer to a data sheet to learn about the different settings in combination with polarity.
 
 
|- style="vertical-align: top;"
 
| valign="top" width="22%" style="width: 129px; border: 1px solid rgb(0, 0, 0);" |
 
CLOCKRATE
 
 
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |
 
The clock rate selects the division of the of the oscillator frequency that serves as the SPI clock. So with 4 you will have a clock rate of 4.000000 / 4 = 1 MHz , when a 4 MHZ XTAL is used.
 
 
|- style="vertical-align: top;"
 
| valign="top" width="22%" style="width: 129px; border: 1px solid rgb(0, 0, 0);" |
 
NOSS
 
 
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |
 
1 or 0. Use 1 when you do not want the SS signal to be generated in master mode.
 
 
|- style="vertical-align: top;"
 
| valign="top" width="22%" style="width: 129px; border: 1px solid rgb(0, 0, 0);" |
 
INTERRUPT
 
 
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |
 
Specify ON or OFF. ON will enable the SPI interrupts to occur. While OFF disables SPI interrupts. ENABLE SPI and DISABLE SPI will accomplish the same.
 
  
 
|- style="vertical-align: top;"
 
|- style="vertical-align: top;"
Line 204: Line 177:
  
 
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |  
 
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |  
Is a numeric constant for an optional delay. This delay is in us. When you specify 1, it will result in 2 us delay&nbsp;: 1 use before and 1 us after the clock. By default there is no delay. Slower chip would require a delay.
+
Is a numeric constant for an optional delay. This delay is in us. When you specify 1, it will result in 2 us delay&nbsp;: 1 us before and 1 us after the clock. By default there is no delay. Only slow slave chips might require a delay.
  
SPEED is for Software SPI only and when MODE is used&nbsp;!
+
SPEED only applies when MODE is specified.
  
 
|- style="vertical-align: top;"
 
|- style="vertical-align: top;"
Line 221: Line 194:
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>
  
The default setting for hardware SPI when set from the Compiler, Options, SPI menu is MSB first, POLARITY = HIGH, MASTER = YES, PHASE = 0, CLOCKRATE = 4
+
&nbsp;
  
<span style="font-family: Arial;">&nbsp;</span>
+
Software SPI allows you to chose the processor pins for the SPI operation. Typically you need a MISO, MOSI, CLOCK and SS pin.
  
When you use CONFIG SPI = HARD alone without the other parameters, the SPI will only be enabled. It will work in slave mode then with CPOL =0 and CPH=0.
+
While this is an advantage, the disadvantage is that software SPI uses more processor resources.
 +
 
 +
&nbsp;
 +
 
 +
In software spi mode the&nbsp;[[SPIINIT]]&nbsp;statement will set the SPI pins to the proper logic level. For example to&nbsp;:
 +
 
 +
<span class="f_CodeExample">sbi&nbsp;PORTB,5&nbsp;;set&nbsp;latch&nbsp;bit&nbsp;hi&nbsp;(inactive)SS</span>
 +
 
 +
<span class="f_CodeExample">sbi&nbsp;DDRB,5&nbsp;&nbsp;;make&nbsp;it&nbsp;an&nbsp;output&nbsp;SS</span>
 +
 
 +
<span class="f_CodeExample">cbi&nbsp;PORTB,4&nbsp;;set&nbsp;clk&nbsp;line&nbsp;lo</span>
 +
 
 +
<span class="f_CodeExample">sbi&nbsp;DDRB,4&nbsp;&nbsp;;make&nbsp;it&nbsp;an&nbsp;output</span>
 +
 
 +
<span class="f_CodeExample">cbi&nbsp;PORTB,6&nbsp;;set&nbsp;data-out&nbsp;lo&nbsp;MOSI</span>
 +
 
 +
<span class="f_CodeExample">sbi&nbsp;DDRB,6&nbsp;&nbsp;;make&nbsp;it&nbsp;an&nbsp;output&nbsp;MOSI</span>
 +
 
 +
<span class="f_CodeExample">cbi&nbsp;DDRB,7&nbsp;&nbsp;;MISO&nbsp;input</span>
 +
 
 +
<span class="f_CodeExample">Ret</span>
 +
 
 +
<span class="f_CodeExample">&nbsp;</span>
 +
 
 +
This is just an example. The actual code differs from processor to processor. And also depends on the used port pins.
  
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>
  
In hardware spi mode the&nbsp;[[SPIINIT]]&nbsp;statement will set the SPI pins to&nbsp;:
+
In most cases, there is just one slave chip to control/address. In such a case you need only one slave select(SS) pin to control this chip. But SPI can also be used to control multiple SPI slaves.
  
SCK = Ouput
+
These slaves need to use the same mode. You can not dynamically change the SPI mode at run time.
  
MISO = Input
+
&nbsp;
  
MOSI = Output
+
BASCOM will automatically set the SS pin to logic level 0 when you use a SPI command. And when the SPI command has executed, it will set the SS pin back to a logic 1.
 +
 
 +
When the slave chip has in inverted SS pin (it requires a 1 to be active) you can not use this automatic SS signal generation.
 +
 
 +
When you want to address multiple slaves with the software SPI you need multiple pins to select the different slave chips. In this case you also can not use the automatic SS signal generation.
 +
 
 +
The solution is to specify&nbsp;<span style="font-weight: bold;">NONE</span>&nbsp;for SS. This will eliminate the automatic SS signal generation. But it also means that you as a user need to handle this. In practice this means&nbsp;:
 +
 
 +
- choose a port pin to serve as SS pin
 +
 
 +
- set it to output and to the right logic level (1 in most cases to disable the slave)
 +
 
 +
- before using a SPI statement, select the slave by making SS logic 0.
 +
 
 +
- after the SPI statement, set the SS logic level back to 1.
  
 
&nbsp;
 
&nbsp;
  
In software spi mode the&nbsp;[[SPIINIT]]&nbsp;statement will set the SPI pins for example to&nbsp;:
+
&nbsp;
  
sbi PORTB,5&nbsp;;set latch bit hi (inactive)SS
+
= <span class="f_Header">Example user controlled SS pin.</span> =
  
sbi DDRB,5&nbsp;;make it an output SS
+
&nbsp;
  
cbi PORTB,4&nbsp;;set clk line lo
+
<span style="font-size: 12pt; font-family: Consolas; color: rgb(0, 0, 128);">Config</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;Spi&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">=</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;Soft&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">,</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;Din&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">=</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(128, 0, 0);">Pinb</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">.</span><span style="font-size: 12pt; font-family: Consolas;">0&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">,</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;Dout&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">=</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(128, 0, 0);">Portb</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">.</span><span style="font-size: 12pt; font-family: Consolas;">1&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">,</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;Ss&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">=</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;NONE&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">,</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;Clock&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">=</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(128, 0, 0);">Portb</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">.</span><span style="font-size: 12pt; font-family: Consolas;">3</span><br/><span style="font-size: 12pt; font-family: Consolas;">MySS&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(0, 0, 128);">alias</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(128, 0, 0);">portb</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">.</span><span style="font-size: 12pt; font-family: Consolas;">2</span><br/><span style="font-size: 12pt; font-family: Consolas; color: rgb(0, 0, 128);">Config</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;MySS</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">=</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(0, 0, 128);">OUTPUT</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">:</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;MySS</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">=</span><span style="font-size: 12pt; font-family: Consolas;">1&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(0, 128, 0);">' deactivate</span><br/><span style="font-size: 12pt; font-family: Consolas; color: rgb(0, 0, 128);">Dim</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;var&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(0, 0, 128);">As</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(0, 0, 128);">Byte</span><br/><span style="font-size: 12pt; font-family: Consolas; color: rgb(0, 0, 128);">SPIINIT</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(0, 128, 0);">' Init SPI state and pins.</span><br/><span style="font-size: 12pt; font-family: Consolas;">MySS</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">=</span><span style="font-size: 12pt; font-family: Consolas;">0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(0, 128, 0);">' select SS</span><br/><span style="font-size: 12pt; font-family: Consolas; color: rgb(0, 0, 128);">SPIOUT</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;var&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">,</span><span style="font-size: 12pt; font-family: Consolas;">&nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(0, 128, 0);">' send 1 byte</span><br/><span style="font-size: 12pt; font-family: Consolas;">MySS</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(255, 0, 0);">=</span><span style="font-size: 12pt; font-family: Consolas;">1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-size: 12pt; font-family: Consolas; color: rgb(0, 128, 0);">' deselect SS</span>
  
sbi DDRB,4&nbsp;;make it an output
+
&nbsp;
  
cbi PORTB,6&nbsp;;set data-out lo MOSI
+
&nbsp;
  
sbi DDRB,6&nbsp;;make it an output MOSI
+
<span style="font-family: Arial;">&nbsp;</span>
  
cbi DDRB,7&nbsp;;MISO input
+
&nbsp;
  
Ret
+
<span style="font-family: Arial;">&nbsp;</span>
 +
 
 +
= <span class="f_Header">Remarks Hardware SPI</span> =
 +
<div style="font-size: 13.3333330154419px; padding: 0px; margin: 0px 0px 0px 4px;">
 +
{| width="604" cellspacing="0" cellpadding="1" border="1" style="border: 2px solid rgb(0, 0, 0); border-spacing: 0px; border-collapse: collapse;"
 +
|- style="vertical-align: top;"
 +
| valign="top" width="22%" style="width: 129px; border: 1px solid rgb(0, 0, 0);" |
 +
SPI
 +
 
 +
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |
 +
<span style="font-weight: bold;">SOFT</span>
 +
 
 +
for software emulation of SPI, this allows you to choose the pins to use. Only works in master mode.
  
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>
  
 +
<span style="font-weight: bold;">HARD</span>&nbsp;for the internal SPI hardware, that will use fixed pins of the microprocessor.
 +
 +
|- style="vertical-align: top;"
 +
| valign="top" width="22%" style="width: 129px; border: 1px solid rgb(0, 0, 0);" |
 +
DATA_ORDER
 +
 +
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |
 +
Selects if MSB or LSB is transferred first.
 +
 +
|- style="vertical-align: top;"
 +
| valign="top" width="22%" style="width: 129px; border: 1px solid rgb(0, 0, 0);" |
 +
MASTER
 +
 +
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |
 +
Selects if the SPI is run in master or slave mode.
 +
 +
|- style="vertical-align: top;"
 +
| valign="top" width="22%" style="width: 129px; border: 1px solid rgb(0, 0, 0);" |
 +
POLARITY
 +
 +
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |
 +
Select HIGH to make the CLOCK line high while the SPI is idle. LOW will make clock LOW while idle.
 +
 +
|- style="vertical-align: top;"
 +
| valign="top" width="22%" style="width: 129px; border: 1px solid rgb(0, 0, 0);" |
 +
PHASE
 +
 +
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |
 +
Refer to a data sheet to learn about the different settings in combination with polarity.
 +
 +
|- style="vertical-align: top;"
 +
| valign="top" width="22%" style="width: 129px; border: 1px solid rgb(0, 0, 0);" |
 +
CLOCKRATE
 +
 +
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |
 +
The clock rate selects the division of the of the oscillator frequency that serves as the SPI clock. So with 4 you will have a clock rate of 4.000000 / 4 = 1 MHz , when a 4 MHZ XTAL is used.
 +
 +
|- style="vertical-align: top;"
 +
| valign="top" width="22%" style="width: 129px; border: 1px solid rgb(0, 0, 0);" |
 +
NOSS
 +
 +
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |
 +
1 or 0. Use 1 when you do not want the SS signal to be automatically generated in master mode.
 +
 +
|- style="vertical-align: top;"
 +
| valign="top" width="22%" style="width: 129px; border: 1px solid rgb(0, 0, 0);" |
 +
INTERRUPT
 +
 +
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |
 +
Specify ON or OFF. ON will enable the SPI interrupts to occur. While OFF disables SPI interrupts. ENABLE SPI and DISABLE SPI will accomplish the same.
 +
 +
|- style="vertical-align: top;"
 +
| valign="top" width="22%" style="width: 129px; border: 1px solid rgb(0, 0, 0);" |
 +
SPIIN
 +
 +
| valign="top" width="100%" style="width: 466px; border: 1px solid rgb(0, 0, 0);" |
 +
When reading from the SPI slave, it should not matter what kind of data you send. But some chips require a value of 255 while others require a value of 0. By default, when the SPIIN option is not provided, a value of 0 will be sent to the SPI slave. With this SPIIN option you can override this value.
 +
 +
|}
 +
</div>
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>
  
When you want to address multiple slaves with the software SPI you need multiple pins to select/activate the slave chip. Specify NONE for SS in that case. This also means that before every SPI command you need to set the logic level to 0 to address the chip and after the SPI command you need to set it back to a logic high level.
+
&nbsp;
 +
 
 +
Hardware SPI is the best option when it is available. Hardware SPI can be used in master and slave mode. All BASCOM SPI statements are master mode routines.
 +
 
 +
The only disadvantage is that you must use the dedicated hardware pins, the SS pin included!
 +
 
 +
&nbsp;
 +
 
 +
When you use CONFIG SPI = HARD without any other parameter, the SPI will only be enabled. It will work in slave mode then with CPOL =0 and CPH=0.
  
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>
  
The hardware SPI also has this option. The NOSS parameter with a value of 1, will not set the SS line to logic 0 when the SPI operation begins. You need to set SS or any other pin of your choice to a logic 0 yourself. After the SPI command(s) are used you need to set it back to a logic 1 to deselect the slave chip.
+
In hardware spi mode the&nbsp;[[SPIINIT]]&nbsp;statement will set the SPI pins to&nbsp;:
 +
 
 +
<span class="f_CodeExample">SCK&nbsp;=&nbsp;Ouput</span>
 +
 
 +
<span class="f_CodeExample">MISO&nbsp;=&nbsp;Input</span>
 +
 
 +
<span class="f_CodeExample">MOSI&nbsp;=&nbsp;Output&nbsp;</span>
 +
 
 +
In Master mode, the SS pin will be set to output too.
  
 
&nbsp;
 
&nbsp;
  
[[File:Notice.jpg|left|Notice.jpg]]In order to use the SPI in master mode, you need to set the SS pin to output. In input mode, this pin can be used to set the SPI bus into slave mode. You only need to set the pin to output when you use the NOSS=1 option.
+
As explained for Software SPI, it is not always desirable to use the SS pin to control the SPI slave chip. Because you want to use a different pin, use multiple slave, or the slaves has an inverted SS signal.
  
 +
Since the hardware SPI always has an SS pin, there is an override for this with a different name than for soft spi&nbsp;:&nbsp;<span style="font-weight: bold;">NOSS</span>=0|1
 +
 +
So where SS=NONE is used for SOFT SPI to disable automatic SPI signal generation, the HARDWARE SPI use the option NOSS=1 to do the same. NOSS means NO SS signal generation.
 +
 +
When NOSS is not used or NOSS=0, the default will be used where the dedicated SS pin will create the slave select signals.
 +
 +
&nbsp;
 +
 +
One big difference with software SPI, is that in order to use the SPI in master mode, the SS pin must be set to output mode. Even if you do not use the dedicated SS pin to control a SPI slave chip&nbsp;!
 +
 +
When the SS pin is in input mode, a logic 0 at the input will turn the master mode into slave mode. A pull up resistor could do the same but our advise&nbsp;: use the SS pin as an output pin.
 +
 +
The SS pin is set to output mode when the MASTER mode is selected. So even if NOSS=1, the SS pin is set to output mode when MASTER=YES.
 +
 +
&nbsp;
 +
 +
&nbsp;
 +
 +
[[File:Notice.jpg|notice]]When using NOSS=1&nbsp;: In order to use the Hardware SPI in master mode, you need to set the SS pin to output. In input mode, this pin can be used to set the SPI bus into slave mode. You only need to set the pin to output when you use the<span style="font-weight: bold;">NOSS=1&nbsp;</span>option. With NOSS=0, the compiler will set the SS pin to output and makes SS pin logic 1.
 +
 +
When NOSS=1 is used, the SS pin is only made an output pin in MASTER mode. No logic level is set when NOSS=1.
 +
 +
&nbsp;
 +
 +
This table show how SS pin is set with the various options for HW mode.
 +
<div style="font-size: 13.3333330154419px; padding: 0px; margin: 0px;">
 +
{| cellspacing="0" cellpadding="0" border="0" style="border: none; border-spacing: 0px; border-collapse: collapse;"
 +
|- style="vertical-align: top;"
 +
| valign="top" width="155" style="width: 155px; border-style: solid; border-color: rgb(0, 0, 0);" |
 +
MODE
 +
 +
| valign="top" width="90" style="width: 90px; border-style: solid; border-color: rgb(0, 0, 0);" |
 +
NOSS
 +
 +
| valign="top" width="261" style="width: 261px; border-style: solid; border-color: rgb(0, 0, 0);" |
 +
SS PIN
 +
 +
|- style="vertical-align: top;"
 +
| valign="top" width="155" style="width: 155px; border-style: solid; border-color: rgb(0, 0, 0);" |
 +
MASTER
 +
 +
| valign="top" width="90" style="width: 90px; border-style: solid; border-color: rgb(0, 0, 0);" |
 +
0
 +
 +
| valign="top" width="261" style="width: 261px; border-style: solid; border-color: rgb(0, 0, 0);" |
 +
output, logic 1
 +
 +
|- style="vertical-align: top;"
 +
| valign="top" width="155" style="width: 155px; border-style: solid; border-color: rgb(0, 0, 0);" | <br/>
 +
| valign="top" width="90" style="width: 90px; border-style: solid; border-color: rgb(0, 0, 0);" |
 +
1
 +
 +
| valign="top" width="261" style="width: 261px; border-style: solid; border-color: rgb(0, 0, 0);" |
 +
output, logic level unchanged
 +
 +
|- style="vertical-align: top;"
 +
| valign="top" width="155" style="width: 155px; border-style: solid; border-color: rgb(0, 0, 0);" |
 +
SLAVE
 +
 +
| valign="top" width="90" style="width: 90px; border-style: solid; border-color: rgb(0, 0, 0);" |
 +
0
 +
 +
| valign="top" width="261" style="width: 261px; border-style: solid; border-color: rgb(0, 0, 0);" |
 +
input
 +
 +
|- style="vertical-align: top;"
 +
| valign="top" width="155" style="width: 155px; border-style: solid; border-color: rgb(0, 0, 0);" | <br/>
 +
| valign="top" width="90" style="width: 90px; border-style: solid; border-color: rgb(0, 0, 0);" |
 +
1
 +
 +
| valign="top" width="261" style="width: 261px; border-style: solid; border-color: rgb(0, 0, 0);" |
 +
input
 +
 +
|}
 +
</div>
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>
  
 
All SPI routines are SPI-master routines. In the samples directory you will also find a SPI hardware master and SPI hardware slave sample.
 
All SPI routines are SPI-master routines. In the samples directory you will also find a SPI hardware master and SPI hardware slave sample.
 +
 +
The SPI protocol is explained in the chapter&nbsp;:&nbsp;[[Using the SPI protocol|Using the SPI protocol]]
  
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>
 +
 +
[[File:Notice.jpg|notice]]<span style="font-family: Arial;">When using a processor for both the master and slave&nbsp;: Take in mind that the SPI master processor clock frequency must be 1/4 of the SPI slave processor frequency.</span>
  
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>
 +
 +
<span style="font-family: Arial;">&nbsp;</span>
 +
 +
= <span style="font-family: Arial;"></span><span style="font-size: 14pt; font-weight: bold;">Chips with 2 full SPI ports</span> =
 +
 +
<span style="font-family: Arial;">Some new processors like the ATMEGA328</span><span style="font-family: Arial; font-weight: bold;">PB</span><span style="font-family: Arial;">have 2 SPI ports. In order to use this second SPI port you have to add a '1' to the statement.</span>
 +
 +
<span style="font-family: Arial;">CONFIG SPI becomes CONFIG SPI1</span>
 +
 +
<span style="font-family: Arial;">SPIIN becomes SPIIN1</span>
 +
 +
<span style="font-family: Arial;">SPIOUT becomes SPIOUT1</span>
 +
 +
<span style="font-family: Arial;">SPIINIT becomes SPI1INIT</span>
 +
 +
<span style="font-family: Arial;">SPIMOVE becomes SPI1MOVE</span>
  
 
= <span class="f_Header">See also</span> =
 
= <span class="f_Header">See also</span> =

Latest revision as of 20:13, 17 January 2017

Contents

Action

Configures the SPI related statements.

 

Syntax for software SPI

CONFIG SPI|SPISOFT = SOFT, DIN = PIN, DOUT = PIN , SS = PIN|NONE, CLOCK = PIN , SPIIN=value , MODE=mode, speed=speed, setup=setup

 

 

Syntax for hardware SPI

CONFIG SPI|SPIHARD = HARD, INTERRUPT=ON|OFF, DATA_ORDER = LSB|MSB , MASTER = YES|NO , POLARITY = HIGH|LOW , PHASE = 0|1, CLOCKRATE = 4|16|64|128 , NOSS=1|0 , SPIIN=value

 

 

When you just want to use one SPI slave chip using the HW SPI, use this : Config Spi = Hard , Interrupt = Off , Data_Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 128

When you want more details, read more about the details and options below.

 

 

Remarks software SPI

SPI

SOFT

for software emulation of SPI, this allows you to choose the pins to use. Only works in master mode.

 

HARD for the internal SPI hardware, that will use fixed pins of the microprocessor.

DIN

Data input or MISO. Pin is the pin number to use such as PINB.0

DOUT

Data output or MOSI. Pin is the pin number to use such as PORTB.1

SS

Slave Select. Pin is the pin number to use such as PORTB.2

 

Use NONE when you do not want the SS signal to be generated. See remarks. Or as an alternative you can use : NOSS=1.

CLOCK

Clock. Pin is the pin number to use such as PORTB.3

DATA ORDER

Selects if MSB or LSB is transferred first.

For soft SPI you need to use the MODE option as well.

Otherwise only MSB order is available.

MASTER

Selects if the SPI is run in master or slave mode.

SPIIN

When reading from the SPI slave, it should not matter what kind of data you send. But some chips require a value of 255 while others require a value of 0. By default, when the SPIIN option is not provided, a value of 0 will be sent to the SPI slave. With this SPIIN option you can override this value.

MODE

A constant in the range from 0-3 which defines the SPI MODE.

Without MODE, the default mode 1 will be used.

Also, when using MODE, new SPI code will be used.

When using MODE, you can also specify SPEED and SETUP.

MODE is for Software SPI only !

 

Mode

Leading Edge

Trailing Edge

0

Rising, Sample

Falling, Setup

1

Rising, Setup

Falling, Sample

2

Falling, Sample

Rising, Setup

3

Falling, Setup

Rising, Sample

SPEED

Is a numeric constant for an optional delay. This delay is in us. When you specify 1, it will result in 2 us delay : 1 us before and 1 us after the clock. By default there is no delay. Only slow slave chips might require a delay.

SPEED only applies when MODE is specified.

SETUP

Setup is the delay in uS before sampling the MISO pin. A numeric constant must be used.

SETUP is for Software SPI only and when MODE is used !

 

 

Software SPI allows you to chose the processor pins for the SPI operation. Typically you need a MISO, MOSI, CLOCK and SS pin.

While this is an advantage, the disadvantage is that software SPI uses more processor resources.

 

In software spi mode the SPIINIT statement will set the SPI pins to the proper logic level. For example to :

sbi PORTB,5 ;set latch bit hi (inactive)SS

sbi DDRB,5  ;make it an output SS

cbi PORTB,4 ;set clk line lo

sbi DDRB,4  ;make it an output

cbi PORTB,6 ;set data-out lo MOSI

sbi DDRB,6  ;make it an output MOSI

cbi DDRB,7  ;MISO input

Ret

 

This is just an example. The actual code differs from processor to processor. And also depends on the used port pins.

 

In most cases, there is just one slave chip to control/address. In such a case you need only one slave select(SS) pin to control this chip. But SPI can also be used to control multiple SPI slaves.

These slaves need to use the same mode. You can not dynamically change the SPI mode at run time.

 

BASCOM will automatically set the SS pin to logic level 0 when you use a SPI command. And when the SPI command has executed, it will set the SS pin back to a logic 1.

When the slave chip has in inverted SS pin (it requires a 1 to be active) you can not use this automatic SS signal generation.

When you want to address multiple slaves with the software SPI you need multiple pins to select the different slave chips. In this case you also can not use the automatic SS signal generation.

The solution is to specify NONE for SS. This will eliminate the automatic SS signal generation. But it also means that you as a user need to handle this. In practice this means :

- choose a port pin to serve as SS pin

- set it to output and to the right logic level (1 in most cases to disable the slave)

- before using a SPI statement, select the slave by making SS logic 0.

- after the SPI statement, set the SS logic level back to 1.

 

 

Example user controlled SS pin.

 

Config Spi = Soft , Din = Pinb., Dout = Portb., Ss = NONE , Clock = Portb.3
MySS alias portb.2
Config MySS=OUTPUT : MySS=' deactivate
Dim var As Byte
SPIINIT                     ' Init SPI state and pins.
MySS=0                     ' select SS
SPIOUT var , 1             ' send 1 byte
MySS=1                     ' deselect SS

 

 

 

 

 

Remarks Hardware SPI

SPI

SOFT

for software emulation of SPI, this allows you to choose the pins to use. Only works in master mode.

 

HARD for the internal SPI hardware, that will use fixed pins of the microprocessor.

DATA_ORDER

Selects if MSB or LSB is transferred first.

MASTER

Selects if the SPI is run in master or slave mode.

POLARITY

Select HIGH to make the CLOCK line high while the SPI is idle. LOW will make clock LOW while idle.

PHASE

Refer to a data sheet to learn about the different settings in combination with polarity.

CLOCKRATE

The clock rate selects the division of the of the oscillator frequency that serves as the SPI clock. So with 4 you will have a clock rate of 4.000000 / 4 = 1 MHz , when a 4 MHZ XTAL is used.

NOSS

1 or 0. Use 1 when you do not want the SS signal to be automatically generated in master mode.

INTERRUPT

Specify ON or OFF. ON will enable the SPI interrupts to occur. While OFF disables SPI interrupts. ENABLE SPI and DISABLE SPI will accomplish the same.

SPIIN

When reading from the SPI slave, it should not matter what kind of data you send. But some chips require a value of 255 while others require a value of 0. By default, when the SPIIN option is not provided, a value of 0 will be sent to the SPI slave. With this SPIIN option you can override this value.

 

 

Hardware SPI is the best option when it is available. Hardware SPI can be used in master and slave mode. All BASCOM SPI statements are master mode routines.

The only disadvantage is that you must use the dedicated hardware pins, the SS pin included!

 

When you use CONFIG SPI = HARD without any other parameter, the SPI will only be enabled. It will work in slave mode then with CPOL =0 and CPH=0.

 

In hardware spi mode the SPIINIT statement will set the SPI pins to :

SCK = Ouput

MISO = Input

MOSI = Output 

In Master mode, the SS pin will be set to output too.

 

As explained for Software SPI, it is not always desirable to use the SS pin to control the SPI slave chip. Because you want to use a different pin, use multiple slave, or the slaves has an inverted SS signal.

Since the hardware SPI always has an SS pin, there is an override for this with a different name than for soft spi : NOSS=0|1

So where SS=NONE is used for SOFT SPI to disable automatic SPI signal generation, the HARDWARE SPI use the option NOSS=1 to do the same. NOSS means NO SS signal generation.

When NOSS is not used or NOSS=0, the default will be used where the dedicated SS pin will create the slave select signals.

 

One big difference with software SPI, is that in order to use the SPI in master mode, the SS pin must be set to output mode. Even if you do not use the dedicated SS pin to control a SPI slave chip !

When the SS pin is in input mode, a logic 0 at the input will turn the master mode into slave mode. A pull up resistor could do the same but our advise : use the SS pin as an output pin.

The SS pin is set to output mode when the MASTER mode is selected. So even if NOSS=1, the SS pin is set to output mode when MASTER=YES.

 

 

noticeWhen using NOSS=1 : In order to use the Hardware SPI in master mode, you need to set the SS pin to output. In input mode, this pin can be used to set the SPI bus into slave mode. You only need to set the pin to output when you use theNOSS=1 option. With NOSS=0, the compiler will set the SS pin to output and makes SS pin logic 1.

When NOSS=1 is used, the SS pin is only made an output pin in MASTER mode. No logic level is set when NOSS=1.

 

This table show how SS pin is set with the various options for HW mode.

MODE

NOSS

SS PIN

MASTER

0

output, logic 1


1

output, logic level unchanged

SLAVE

0

input


1

input

 

All SPI routines are SPI-master routines. In the samples directory you will also find a SPI hardware master and SPI hardware slave sample.

The SPI protocol is explained in the chapter : Using the SPI protocol

 

noticeWhen using a processor for both the master and slave : Take in mind that the SPI master processor clock frequency must be 1/4 of the SPI slave processor frequency.

 

 

Chips with 2 full SPI ports

Some new processors like the ATMEGA328PBhave 2 SPI ports. In order to use this second SPI port you have to add a '1' to the statement.

CONFIG SPI becomes CONFIG SPI1

SPIIN becomes SPIIN1

SPIOUT becomes SPIOUT1

SPIINIT becomes SPI1INIT

SPIMOVE becomes SPI1MOVE

See also

SPIIN , SPIOUT , SPIINIT , SPI , SPIMOVE

 

 

Example for Software SPI


Config Spi = Soft , Din = Pinb.0 , Dout = Portb.1 , Ss = Portb.2 , Clock = Portb.3
Dim var As Byte
SPIINIT 'Init SPI state and pins.
SPIOUT var , 1 'send 1 byte

Example for Hardware SPI


Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = High , Phase = 1 , Clockrate = 4 , Noss = 1
Spiinit

Languages   English Deutsch  
Personal tools
Namespaces
Variants
Actions
Navigation
In other languages
Language