|
|
View previous topic :: View next topic |
Author |
Message |
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
Understanding how the stack works |
Posted: Tue Jul 17, 2018 1:24 am |
|
|
Goodday,
I want to know what the line 02A72 does, it seems that it should overwrite the TOS with W5 (which are the value which should move to the SPI buffer) without increment or decrementing the stack pointer for this to work correct.
Code: | .............................. spi_write(FALSE,Tx.Message[MsgNo]);
02A5E 818D50 MOV 31AA,W0 : W0 = [31AA]
02A60 231AC4 MOV #31AC,W4 : W4 = 31AC
02A62 400004 ADD W0,W4,W0 : W0 = W0+W4
02A64 784290 MOV.B [W0],W5L : W5L = [W0]
02A66 A9C240 BCLR.B 240.6 : SPI1STAT.SPIROV = 0
02A68 AE0240 BTSS.B 240.0 : Skip if SPI1STAT.SPIRBF = 1
02A6A 370002 BRA 2A70 : GoTo 2A70
02A6C BFC248 MOV.B 248,W0L : W0L = SPI1BUF
02A6E 37FFFC BRA 2A68 : GoTo 2A68
02A70 F80248 PUSH 248 : PUSH SPI1BUF to TOS
02A72 9FFFE5 MOV.B W5L,[W15-#2] : [W15+-2] = W5L
02A74 F90248 POP 248 : POP TOS to SPI1BUF |
Regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Tue Jul 17, 2018 1:43 am |
|
|
Though it is talking to the stack, it is a standard 'indirect addressing' operation.
Moves the byte held in W5L, to the location addressed by W15-2. This is what the brackets mean. Now W15, just happens to be the stack pointer, but could be any of the registers (note 7 lines before, where the byte addressed by W0 is used the same way). Here W0 is loaded with 'MsgNo', then has the location of the start of Tx.Message (0x31AC) added to it, and then the byte is loaded from this address.
In the Tx.Message version, the offset has to be calculated (since it changes), while in the one using W15, the offset is a fixed value of '-2', so can be included in the instruction. The fixed offset, can be a 'Slit10'. A 10bit signed value (-512 to 511). |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Tue Jul 17, 2018 4:28 am |
|
|
Thank you Ttelmah.
Understand now what happens, but couldn't W5L been moved directly to 248 instead of using 3 instructions as when you use a hardcoded offset.
Regards Code: | .............................. spi_write(FALSE,Tx.Message[0]);
02A50 A9C240 BCLR.B 240.6 : SPI1STAT.SPIROV = 0
02A52 AE0240 BTSS.B 240.0 : Skip if SPI1STAT.SPIRBF = 1
02A54 370002 BRA 2A5A : GoTo 2A5A
02A56 BFC248 MOV.B 248,W0L : W0L = SPI1BUF
02A58 37FFFC BRA 2A52 : GoTo 2A52
02A5A 818D60 MOV 31AC,W0 : W0 = [31AC]
02A5C B7E248 MOV.B W0L,248 : SPI1BUFL = W0L
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Tue Jul 17, 2018 6:57 am |
|
|
From some PIC24/33 data sheets:
Quote: |
Note: Do not perform read-modify-write operations
(such as bit-oriented instructions) on
the SPIxBUF register in either Standard or
Enhanced Buffer mode.
|
MOV.B, is a RMW instruction.
It's a way of coding round an issue that the SPIxBUF register has, because of it's nature as a FIFO. It is not normal memory, you have to write the whole 16bits each time, even if you are using byte wide SPI. So they would either have to read the whole register into RAM, write the byte into this, and then write the whole value back, or use the stack for the same operation.... |
|
|
|
|
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
|