I'm using a PIC18F14K50 as a USB translator that talks to the PC and to a "Master Controller", a PIC18F26K42 (the K42 being the focus of this post). The USB part is working. The USB Packet consists of a Command Code and 8 Data Bytes (iCmd & iDat[8] for PIC-to-PC and oCmd & oDat[8] for PC to PIC). With a PicoScope Logic Analyzer I've verified UART transmissions; BAUD is correct, format is correct, etc. I've disabled Vectored and Priority Interrupts on the K42. I'm using "DEFINE INTHAND Sort" with an ASM ISR. I've defined an array to store incoming bytes from the PC (passed on by the K50) as they arrive with a counter (byte variable "Pct") to track array offset (PcIn[Pct]). The ISR receives a byte, transfers it from U2RXB to pTemp (MOVFFL U2RXB, pTemp), sets a flag (BSF PC_In ;which is Work.4), clears the U2RXIF, then RETFIE. From the Main loop I poll this PC_In flag:
If the Command code (which would be oCmd from the USB) is => $60 it is a Request code requesting a data packet. The K50 filters this and will only send 1 byte when it's a Request. Otherwise, incoming bytes are packed into the array "PcIn[Pct]", where Pct is the variable used for the offset. The RedI subroutine checks oCmd for Requests (where the Pct offset variable = 0) with IF/THEN and calls SendP_Data to send requested data packets. Otherwise, it moves the received byte from pTemp to PcIn[Pct].
When a full packet has arrived (Pct = 9) another flag is set; pPac = 1. From the Main loop, I'm polling for this flag:
The subroutine Red_Phone then disseminates the PcIn packet.
Because of the ASM ISR I've declared some variables as SYSTEM and some specifically in the Access Bank. One example:
-- ********
-- ** This brings me to question number 1: Using generic variables (b0 & b1) in FOR/NEXT LOOPs, I'm getting 5 bytes of data instead of 9. I tried specifying an address for these ISR variables:
The Compiler says I'm declaring the same thing twice and says it in RED. I decided to try this because I'm wondering if the Compiler is overlapping addresses because of my ASM code. This may explain a number of corruptions while the code executes.
---********
-- ** Question number 2: On start up, the PC sends multiple Requests for data to load parameters & variables from the controller. It then toggles between 2 Requests for constantly changing data. The K42 occasionally responds to Requests in a timely manner, but more often it has a serious delay (and eventually seems to stop responding altogether). I started at 4 MHz Fosc then increased it to 16 MHz to see if that would help. It made no difference. I tried to upload the complete code as .txt, but it was more than double the allowable upload size.
I've been debugging for a week now and am running out of options and time (I was supposed to deliver this bugger this past Monday!!). Thanks in advance for any insight you can offer.
Some notes about the code:
- I'm using PBP3.1 v.3.1.2.4; Micro Code Studio v.5.0.0.5; MPLABX v.5.15; Windows 10
- ASM ISR is "Sort" near the top
- Within Sort, the UART2 routine is "Get_PC"
- The K42 uses both MOVFF and MOVFFL. I used MOVFFL just to be sure
- This version doesn't compile because of "@Hin0 EQU 0x0058 ;Places Hin0 in Access Bank at specific address 0x0058. Comment them out & it compiles. However...
- I tried "Hin0 VAR BYTE BANKA SYSTEM" previously, which compiles, but places Hin0 anywhere in the Access Bank, which may be an issue
- Tach is a WORD sized variable that uses TMR1H as Tach.HIGHBYTE and TMR1L as Tach.LOWBYTE. The following lines allow manipulation is the ASM ISR:
- In Start2 I'm looping through waiting for a delay (rDel is the variable) to time out. The commented out code was used to debug UART2 U2TXB function. One note here is that I should have seen a single b1 value output, then a rather lengthy pause. What I actually got was rapid-fire output with no delays. (Again, was a variable being corrupted??)
- Originally I only polled for PC_In once per loop. To find out why I was missing Requests I placed the poll every other function, which didn't help:
Code:
IF PC_In = 1 THEN ;A USB Byte Has Arrived, PC_In Set in Get_PC of ISR GOSUB RedI ENDIF
Code:
RedI: IF Pct = 0 THEN ;First Byte of Packet, oCmd IF pTemp > $5F THEN ;Request Code GOSUB SendP_Data ENDIF ENDIF PcIn[Pct] = pTemp ;Move Byte to Array PC_In = 0 ;Clear Flag Pct = Pct + 1 ;INC Counter IF Pct = 9 THEN ;If Array Full... pPac = 1 ;Set Flag Pct = 0 ;Clear Counter ENDIF @ CLRWDT RETURN
Code:
IF pPac = 1 THEN ;Full USB Packet Has Arrived GOSUB Red_Phone. ;Call from the President ENDIF
Code:
Red_Phone: SELECT CASE PcIn[0] ;oCmd CASE $40 FOR b0 = 0 TO 7 b1 = b0 + 1 EHI[b0] = PcIn[b1] ;Load the Array NEXT b0 DO LOOP WHILE PIR6.3 = 0 ;Wait till U2TXB is Empty U2TXB = Ack ;Acknowledge Receive CASE $41 FOR b0 = 8 TO 15 b1 = b0 - 7 EHI[b0] = PcIn[b1] NEXT b0 DO LOOP WHILE PIR6.3 = 0 U2TXB = Ack . . . . . . . . . . . . . . . . . . . . . . CASE $4A CcDc = PcIn[1] CcWot = PcIn[2] Secs = PcIn[3] bConf = PcIn[4] DO LOOP WHILE PIR6.3 = 0 U2TXB = Ack CASE ELSE GOSUB SendP_Data END SELECT PC_In = 0 ;Clear the ISR-Set Flag Pct = 0 ;Clear the Array Offset pPac = 0 ;Set the "Packet Full" Flag @ CLRWDT RETURN
Code:
Work VAR BYTE BANKA SYSTEM ;Source of Working Bits Dat VAR Work.0 ;UART1 Data Has Arrived from Hhombre Time VAR Work.1 ;Seconds Have Been Decremented in ISR CPS VAR Work.2 ;RPM Numbers are In Cold VAR Work.3 ;Cold-Start Ramp-In, 0 = Cold, 1 = Warm PC_In VAR Work.4 ;UART2 Data Has Arrived from PC/Laptop Dry VAR Work.5 ;Rprb = 1 pPac VAR Work.6 ;Full USB Packet Has Arrived
-- ** This brings me to question number 1: Using generic variables (b0 & b1) in FOR/NEXT LOOPs, I'm getting 5 bytes of data instead of 9. I tried specifying an address for these ISR variables:
Code:
Work VAR BYTE SYSTEM ;Source of Working Bits @Work EQU 0x005F ;Places Work at End of Access Bank Dat VAR Work.0 ;UART1 Data Has Arrived from Hhombre .............
---********
-- ** Question number 2: On start up, the PC sends multiple Requests for data to load parameters & variables from the controller. It then toggles between 2 Requests for constantly changing data. The K42 occasionally responds to Requests in a timely manner, but more often it has a serious delay (and eventually seems to stop responding altogether). I started at 4 MHz Fosc then increased it to 16 MHz to see if that would help. It made no difference. I tried to upload the complete code as .txt, but it was more than double the allowable upload size.
I've been debugging for a week now and am running out of options and time (I was supposed to deliver this bugger this past Monday!!). Thanks in advance for any insight you can offer.
Some notes about the code:
- I'm using PBP3.1 v.3.1.2.4; Micro Code Studio v.5.0.0.5; MPLABX v.5.15; Windows 10
- ASM ISR is "Sort" near the top
- Within Sort, the UART2 routine is "Get_PC"
- The K42 uses both MOVFF and MOVFFL. I used MOVFFL just to be sure
- This version doesn't compile because of "@Hin0 EQU 0x0058 ;Places Hin0 in Access Bank at specific address 0x0058. Comment them out & it compiles. However...
- I tried "Hin0 VAR BYTE BANKA SYSTEM" previously, which compiles, but places Hin0 anywhere in the Access Bank, which may be an issue
- Tach is a WORD sized variable that uses TMR1H as Tach.HIGHBYTE and TMR1L as Tach.LOWBYTE. The following lines allow manipulation is the ASM ISR:
Code:
Tach VAR WORD BANKA SYSTEM ;(TachH << 8) | TachL @TachL EQU Tach ;TMR1L RPM Value @TachH EQU Tach + 1 ;TMR1H RPM Value
- Originally I only polled for PC_In once per loop. To find out why I was missing Requests I placed the poll every other function, which didn't help:
Code:
IF PC_In = 1 THEN ;A USB Byte Has Arrived, PC_In Set in Get_PC of ISR GOSUB RedI ENDIF
Comment