|
|
View previous topic :: View next topic |
Author |
Message |
aefelgate Guest
|
spi_xfer byte order in multi-byte commands / responses? |
Posted: Mon Dec 17, 2007 7:05 pm |
|
|
Hello,
I am using the CCS compiler's built-in spi_xfer() function to transmit 16 bit and 24 bit commands to a device.
What is the BYTE order required? Can anybody answer this simple question?
e.g. I wish to transfer the following bytes, in the following order:
0x01 0x00 0x00
I create a 32-bit variable, and us it as follows:
#use spi(MASTER,
DI=PIN_A3,
DO=PIN_A5,
CLK=PIN_A2,
ENABLE=PIN_C0,
ENABLE_ACTIVE=0,
MODE=3,
BITS=32,
LSB_FIRST,
STREAM=SPI_STREAM)
int32 spi_data = 5;
int32 spi_xfer_status = 0;
// Send 0x05 0x00 0x00
spi_xfer_status = spi_xfer(SPI_STREAM, spi_data, 24);
Is this correct? Or, does it need to be 0x05000000?
Or 0x00050000
I am sending data LSB FIRST, but what is the byte order???
Also, I am assumin the response, spi_xfer_status in my code above, will be returned with the byte order mapped in the same way as the command?
The CCS compiler manual and examples only give a single byte example when using the spi_xfer() command; there needs to be a multi-byte example, which would then easily clear up this confusion. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Dec 18, 2007 12:58 am |
|
|
You can find answer from the .LST file, after compiling your test program:
Here, the LSB (5) is put into address 0x21:
Code: |
... int32 spi_data = 5;
004D: MOVLW 05
004E: MOVWF 21
004F: CLRF 22
0050: CLRF 23
0051: CLRF 24
|
Then the LSB is moved into RAM address 0x29, before the program
jumps to the SPI library code at ROM address 0004.
Code: |
... // Send 0x05 0x00 0x00
... spi_xfer_status = spi_xfer(SPI_STREAM, spi_data, 24);
0056: MOVF 24,W
0057: MOVWF 2C
0058: MOVF 23,W
0059: MOVWF 2B
005A: MOVF 22,W
005B: MOVWF 2A
005C: MOVF 21,W
005D: MOVWF 29 // LSB is now in 0x29
005E: MOVLW 18
005F: MOVWF 2D // Bit count (24) is in 0x2D
0060: GOTO 004
|
// Then the LSB byte (05) is shifted out first.
Code: |
... #use spi(MASTER,DI=PIN_A3,DO=PIN_A5,CLK=PIN_A2,
ENABLE=PIN_C0,ENABLE_ACTIVE=0,MODE=3,BITS=32,
LSB_FIRST,STREAM=SPI_STREAM)
0004: BSF 03.5
0005: BSF 05.3
0006: BCF 05.5
0007: BCF 05.2
0008: BCF 03.5
0009: BSF 05.2
000A: BCF 20.0
000B: MOVF 20,W
000C: BSF 03.5
000D: MOVWF 07
000E: BCF 03.5
000F: BCF 07.0
0010: MOVF 2D,W
0011: MOVWF 2E
0012: BCF 05.2
0013: BTFSS 29.0 // Test Bit of the LSB.
0014: BCF 05.5 // Set SDO to the same value.
0015: BTFSC 29.0
0016: BSF 05.5
0017: RRF 2C,F // Then rotate all four data bytes
0018: RRF 2B,F // right by 1 bit position.
0019: RRF 2A,F
001A: RRF 29,F
001B: BSF 05.2
001C: RRF 7A,F
001D: RRF 79,F
001E: RRF 78,F
001F: RRF 77,F
0020: BTFSS 05.3
0021: BCF 7A.7
0022: BTFSC 05.3
0023: BSF 7A.7
0024: DECFSZ 2E,F // Decrement bit count.
0025: GOTO 012 // Loop until all bits are sent.
// (Remainder of library code is not shown)
|
|
|
|
aefelgate Guest
|
Thanks |
Posted: Tue Dec 18, 2007 11:45 am |
|
|
Thanks for clearing this up. Now that I understand how the compiler orders the bits and bytes I know that is not the problem. |
|
|
|
|
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
|