|
|
View previous topic :: View next topic |
Author |
Message |
Mortenc
Joined: 22 Feb 2007 Posts: 55
|
Re-setup of SPI cause problems |
Posted: Tue Mar 10, 2009 6:53 am |
|
|
Hello clever forum,
I have found a strange thing about the SPI setup.
For safety reasons I always re-setup my TRIS and special functions on every program turnaround time. This include the SPI function.
However this gives problem for me because the re-setup of the SPI causes deactivating SPI and re-activating SPI.
Processor PIC18F4520
Code: | setup_spi(spi_slave |spi_l_to_h | SPI_SS_DISABLED); |
gives the asm list file:
Code: | 04E0: BCF FC6.5
04E2: MOVLW 25
04E4: MOVWF FC6
04E6: MOVLW 00
04E8: MOVWF FC7 |
The first ASM line is "BCF FC6.5" this disables the SPI, resulting the I/O to be a normal output in few uSec.
The result of this is that the DO (data out) is disturbed.
Why is the compiler made like this?
I think it's always good practice to make such re-setup in case of corrupted registers no matter using BOD on the processor. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Mar 10, 2009 7:38 am |
|
|
Quote: | Why is the compiler made like this? | Either if this behaviour serves a purpose or if it don't - it most likely won't be changed now for simple compatibility reasons. Take it as is and configure the SFRs according to your ideas.
Quote: | I think it's always good practice to make such re-setup in case of corrupted registers. | I don't see a particular reason to expect corrupted registers in this case, otherwise you have to expect anything possibly wrong. |
|
|
Ttelmah Guest
|
|
Posted: Tue Mar 10, 2009 9:15 am |
|
|
What the line does, during the disabled period, is completely up to you. If your SPI idles high, then simply set the line high, _before_ calling setup_spi the first time. When the SPI is disabled, the value latched in the output register, is what the line goes to.
As for why the compiler does this, it has to. There are errata, on quite a lot of PICs, covering not being able to configure the MSSP while it is enabled, and even on the ones without this, changing mode with the peripheral enabled, _will_ result in spurious signals on the output lines.
The 'fully safe' way to work, is to resistively bias the clock line to it's idle state, and program the line as an input (the default on boot up). This way there will be no extra clocks, either on initial power up, or when reprogramming the peripheral.
It is 'arguable', whether this actually adds safety. In some cases changing registers inreases the probability of errors. The 'better' way, would be to add external hardware monitoring the functionality of the whole system, and if this shows a fault, trigger a complete restart (external hardware watchdog). For some high reliability applications, this is required, but reprogramming registers uneccessarily, is deprecated in such systems...
YPYMATYC
Best Wishes |
|
|
Mortenc
Joined: 22 Feb 2007 Posts: 55
|
|
Posted: Tue Mar 10, 2009 3:59 pm |
|
|
Thanks Ttelmah, (you are clever)
I will take your good advice into account.
However in this project I have solved the problem by moving the re-setup to another place in the program, so I only make the re-setup after the whole package is sended. (I send 5 bytes with 1 byte each program cycle). |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|