I have a quite weird problem, i implemented a UART receive ring buffer (part of a much larger project...) and use a word variable (next_in) as pointer to buffer ( ~3kb byte array).
The sub routine is called upon a UsartRX2Ready Interrupt event:
Sub readUSART
buffer(next_in) = HSerReceive2
next_in = ( next_in + 1 )
IF (NEXT_IN>buffer_size) Then /// 'buffer_size' is a constant of 2900 value
NEXT_IN=1
END IF
End Sub
Now, as next_in is a 16bit word variable, the IF (NEXT_IN>buffer_size) command needs quite a lot of instructions to execute, and this happens with every received byte from uart port!
So, i was thinking of a way to somehow "lighten" up things, by trying a somewhat different approach, using 'if' commands with byte and bit variables (that execute much, much faster), and i try this:
Sub readUSART
buffer(next_in) = HSerReceive2
next_in = ( next_in + 1 )
IF (next_in_H.3=true) then
IF (next_in_H=0xb) then
IF (low_next_IN=0x55) Then
/// 'low_next_in' defined as: Dim low_next_in as Byte alias next_in
NEXT_IN=1
END IF
end if
end if
buffersize value is 2900=&0b54 so 'next_in_h' should be &0B and 'low_next_in' should be &54, when pointer reaches the limit of ring buffer and by adding '1' it needs to reset to '1'.
The first 'IF(next_in_H.3=true)' executes in only 1 instruction (checks if pointer>2048) so in most cases, checking if pointer reaches ring buffer is done much, much faster than previous code (that needs dozens of instructions for each byte received)
Now, i'm using EXACTLY the same code in another place in my code and seems to work ok, but when i place the code inside the subroutine, it simply stops working (it's like as none of the 'IF' commands function properly...)!
So i was wondering, is there something i'm doing wrong, or this is a case of weird compiler bug?
Last edit: ikonsgr74 3 days ago
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
buffer(next_in) = HSerReceive2 is this doing what you expect? Examine the ASM. IF (next_in_H.3=true) then what is the ASM for this? Is this doing what you expect?
But, it sounds like you have a WORD variable defined somewhere that needs redifining in this sub. The Help explains that a WORD variable defined in one sub is only a BYTE variable in another sub so things like next_in_h may work as expected. Just Dim the variable in the second sub.
I can look at the ASM for you., if you post it.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have a quite weird problem, i implemented a UART receive ring buffer (part of a much larger project...) and use a word variable (next_in) as pointer to buffer ( ~3kb byte array).
The sub routine is called upon a UsartRX2Ready Interrupt event:
Sub readUSART
buffer(next_in) = HSerReceive2
next_in = ( next_in + 1 )
IF (NEXT_IN>buffer_size) Then /// 'buffer_size' is a constant of 2900 value
NEXT_IN=1
END IF
End Sub
Now, as next_in is a 16bit word variable, the IF (NEXT_IN>buffer_size) command needs quite a lot of instructions to execute, and this happens with every received byte from uart port!
So, i was thinking of a way to somehow "lighten" up things, by trying a somewhat different approach, using 'if' commands with byte and bit variables (that execute much, much faster), and i try this:
Sub readUSART
buffer(next_in) = HSerReceive2
next_in = ( next_in + 1 )
IF (next_in_H.3=true) then
IF (next_in_H=0xb) then
IF (low_next_IN=0x55) Then
/// 'low_next_in' defined as: Dim low_next_in as Byte alias next_in
NEXT_IN=1
END IF
end if
end if
buffersize value is 2900=&0b54 so 'next_in_h' should be &0B and 'low_next_in' should be &54, when pointer reaches the limit of ring buffer and by adding '1' it needs to reset to '1'.
The first 'IF(next_in_H.3=true)' executes in only 1 instruction (checks if pointer>2048) so in most cases, checking if pointer reaches ring buffer is done much, much faster than previous code (that needs dozens of instructions for each byte received)
Now, i'm using EXACTLY the same code in another place in my code and seems to work ok, but when i place the code inside the subroutine, it simply stops working (it's like as none of the 'IF' commands function properly...)!
So i was wondering, is there something i'm doing wrong, or this is a case of weird compiler bug?
Last edit: ikonsgr74 3 days ago
Not likely to be a compiler bug.
buffer(next_in) = HSerReceive2
is this doing what you expect? Examine the ASM.IF (next_in_H.3=true) then
what is the ASM for this? Is this doing what you expect?But, it sounds like you have a WORD variable defined somewhere that needs redifining in this sub. The Help explains that a WORD variable defined in one sub is only a BYTE variable in another sub so things like
next_in_h
may work as expected. Just Dim the variable in the second sub.I can look at the ASM for you., if you post it.