Menu

USART NOT WORKING ON PIC18F47Q10 with latest compiler

ikonsgr74
2024-12-23
2024-12-24
  • ikonsgr74

    ikonsgr74 - 2024-12-23

    I just compiled a gcb code for 18F47Q10 and the HW USART i'm using ,stopped functioning. This is the compiler output:
    14:57:32 G+Stool started with parameter 'hex' -> processing C:\GCstudio\G+Stools\makeHEX.bat
    Source-File = F:\EBAY\other\Pic_projects\CPC_SERIAL_PORT\Serial port ii\18f47q10_7d_WPUD_ACTIVATE_serial1.gcb
    Target-File = F:\EBAY\other\Pic_projects\CPC_SERIAL_PORT\Serial port ii\18f47q10_7d_WPUD_ACTIVATE_serial1.hex
    Compiler Version: 2024.12.15 (Windows 64 bit) : Build 1447) Program Memory: 64740/65536 words (98,79%) RAM: 3159/3359 bytes (94,05%) OSC: HFINTOSC_1MHZ, 64Mhz (Internal oscillator) Chip: 18F47Q10
    Duration: 11.4 Seconds.

    And these are the declarations of HW USART in the code:

     `    #define USART2_BAUD_RATE 1065600
    #define USART2_DELAY 0 ms
    #define USART_DELAY 0 ms
    #define USART2_BLOCKING
    #define SerInPort PORTB.1
    #define SerOutPort PORTB.2
    

    On Interrupt UsartRX2Ready Call readUSART

    Sub readUSART
       buffer(next_in) = HSerReceive2
       next_in = ( next_in + 1 )
       IF (NEXT_IN>BUFFER_SIZE) Then
       NEXT_IN=1
       END IF
     End Sub`
    

    With previous compiler everything worked ok, so definetely something is wrong with the new compiler regarding the HW USART.

     
    • Anobium

      Anobium - 2024-12-23

      Can you post your GCBASIC source ? The segments here mean I cannot test compile.

       
  • Anobium

    Anobium - 2024-12-23

    A contributor may have caused a double read in the receive routine.

    Please try attached.

     
  • ikonsgr74

    ikonsgr74 - 2024-12-23

    No, i'm afraid i doesn't work. Attached file is the gcb source code.

     
  • Anobium

    Anobium - 2024-12-23

    OK. Can you create a program that does Serial Buffer loopback. Use the same ports as in your program, pps etc. One that should work. Like the example in the HELP for the Serial Buffer.

    If that does not work for you, post here, I will need to test to figure out what is happening.

     
  • ikonsgr74

    ikonsgr74 - 2024-12-23

    I'm using pic uart to communicate with a ch376 usb host module, testing communication (send a sequence of 4 bytes) doesn't respond at all, it's like as if nothing is sent and/or nothing is received back.
    Now, i also made further tests, using a usb2serial cable and a terminal windows program, it seems that hardware usart receives bytes without problem, but it can't send any byte!
    So probably the problem must be somewhere in the hsersend routine?

     
  • Anobium

    Anobium - 2024-12-23

    Please write the test routine And, I will examine here.

     
  • ikonsgr74

    ikonsgr74 - 2024-12-23

    the "test routine" is actually a basic program in Amstrad CPC that communicate with PIC chip through I/O requests at specific ports (these are configured using various computer signals and the CLC's of PIC). uart is connected with a serial cable to a pc where a small terminal program is running and i'm able to exchange messages.
    Anything i sent from pc to Amstrad is received ok, so the ring buffer and hserreceive routine must work ok, but when i try to send a byte from Amstrad to pc, nothing is received...
    Perhaps, you want to make a small gcb program that will send bytes to serial port in a loop then?

     
    • Anobium

      Anobium - 2024-12-23

      Yes, take the Serial Buffer demo from the Help, change chip, set pps as your settings.

      Does that work? open a terminal does it return the characters? if not, send me this test ( newly created) program.

       

      Last edit: Anobium 2024-12-23
  • ikonsgr74

    ikonsgr74 - 2024-12-23

    OK, I made this smal testing code:

    #chip 18F47q10, 64
    #option explicit
    
        #startup InitPPS, 85
        #define PPSToolPart 18f47q10
        #define USART2_BAUD_RATE 1115200
        #define STRINGSIZE 15
        #define BUFFER_SIZE 2900
        #define USART2_DELAY 0 ms
        #define USART_DELAY 0 ms
        #define USART2_BLOCKING
        #define SerInPort PORTB.1
        #define SerOutPort PORTB.2
    
        Sub InitPPS
         UNLOCKPPS
    
        RX2PPS = 0x0009    'RB1 > RX2
        RB2PPS = 0x000B    'TX2 > RB2
    
            LOCKPPS
        End Sub
    
    
      'USART settings
      ;#define USART_BAUD_RATE 115200  'Initializes USART port with 9600 baud
      '#define USART_BLOCKING   ' Both of these blocking statements will
      ;#define USART_TX_BLOCKING ' wait for tx register to be empty
                    ' use only one of the two constants
      ;#define USART_DELAY OFF
    
      'Main loop
      Do
        'Send PORTB value through USART
        HSerSend 65,2
        Wait 200 ms      'probably not necessary with blocking statement
      Loop
    

    This should send to uart port 'A' character 5 times/second. I powered board with PIC, connect Tx,Rx, Gnd , but unfortunately i don't get anything on PC terminal window...

     
  • Anobium

    Anobium - 2024-12-23

    Excellent. Thank you. I will use yours.


    How urgent is this ? it is Christmas soon.


    I just wrote a full test program.


     
  • ikonsgr74

    ikonsgr74 - 2024-12-23

    Unfortunately i get nothing on PC terminal with first-start-sample.gcb too....
    Well, it's not too urgent ,but i will need to compile an updated code soon.

     
  • Anobium

    Anobium - 2024-12-23

    :-)

    I will resolve ASAP! I promise.

     
    • ikonsgr74

      ikonsgr74 - 2024-12-23

      Thanks evan, i really appreciate your help and concern! :-)

       
  • ikonsgr74

    ikonsgr74 - 2024-12-23

    where can i find previous versions of cow basic? Maybe i could pinpoint exactly when this bug occurs...

     
  • Anobium

    Anobium - 2024-12-23

    It will take me more time to roll back the USART.H.

    THANK YOU!

     
  • Anobium

    Anobium - 2024-12-24

    I have found the root cause. Microchip have renamed some of the USART2 register bits. So, when the DAT files were regen'ed the new names are used.

    I have resolved here. Just testing.

     
    • Anobium

      Anobium - 2024-12-24

      This is resolves the issue.

      I have added a check to ensure the baud rate calculation is completed - would now issue an error the baud rate calculation is zero.


      Test program used


      This is how I diagnosed at resolved.

      1. Used the basic test program to send data, see above. This key to this test in minimal code and the use of HSerSend 65,2. HSerSend essentially sets the output register to 65. This failed.
      2. Examine at ASM. Examined the HSerSend routine in the generated ASM. Looks good - this waits for USART data flag to be set ( TX2IF ) then sets TX2REG = SerData.

      No errant registers, or register bits. Uses TX2, so assume that is this good.

      ;Overloaded signature: BYTE:byte:, Source: usart.h (2028)
      HSERSEND343
      ;USART2_BLOCKING and NOT USART2_TX_BLOCKING
      ;Wait While TX2IF = Off
      SysWaitLoop1
          banksel PIR3
          btfss   PIR3,TX2IF,BANKED
          bra SysWaitLoop1
      ;TX2REG = SerData
          movff   SERDATA,TX2REG
          banksel 0
          return
      
      1. Examine initialisation routines.

      Check that INITUSART is being called. It is, so assume that is this good.

      ;Call initialisation routines
          rcall   INITSYS
          rcall   INITPPS
          rcall   INITUSART
      
      1. Examine INITUSART routine.

      Ah ah... this is not correct. Why is the SPBRG2, SPBRGH2 regiser being set to Zero when they control the baudrate!!!

      ;PIC USART 2 Init
      ;Set baud rate
      ;SPBRG2 = SPBRGL_TEMP2
          banksel SPBRG2
          clrf    SPBRG2,BANKED
      ;SPBRGH2 = SPBRGH_TEMP2
          clrf    SPBRGH2,BANKED
      
      1. Examine the CDF for the test program. Search for SPBRGL_TEMP2, open the CDF and do a search.

      Not good...

      Shows that the final result is zero.

      FINAL/CONSTANT :SPBRGL_TEMP2 0

      The CDF shows SCRIPT/AddConstant: Line 705 SPBRGL_TEMP2 0 C:\GCstudio\gcbasic\INCLUDE\LOWLEVEL\usart.h so something in USART is set the contsant SPBRGL_TEMP2 to zero. That is not correct.

      Note: Line numbers may vary

      1. Open USART.H. Examine line 705.

      SPBRG_TEMP2 is the basis to set SPBRGL_TEMP2, therefore SPBRG_TEMP2 is zero before the calc.

      There is two tests in the part of the USART If Bit(BAUDCON2_BRG16) and If Bit(U2BRGS).

      Open PICINFO, search for the bits shown above ... niether exist. Arghhh.

      BAUDCON2_BRG16 must have worked in the past... and, there is a register bit called BAUD2CON_BRG16... The DAT file controls all the register.bits... so, something has changed as this WAS working in the past.

      1. Using SVN ( the SourceForge respository is an SVN respository ). Compare the 18f47q10.dat file.

      RESULT - BAUDCON2_BRG16 is missing in the current release compared to the current release.

      The root cause is the DAT file has changed.


      RULES ON DAT FILES

      The DAT file generation process to generate the DAT is fully automated. There is NO manual intervention.
      The DAT file generation process uses XML provided from MPLAB-X. . There is NO editing. Microchip make corrections to address, bit registers etc etc. these will include corrections from the greater Microchip community.
      The XLM is the master source. The outcome of the DAT file generation process is master.

      There are 1416 dat files and visual inspection is IMPOSSIBLE.

      So, the DAT is the DAT and we NEVER edit.

      We need to change the libraries if the DAT file changes.


      RESOLUTION

      a. Change the script section in USART.H to cater for the DAT change.

      From
      If Bit(BAUDCON2_BRG16) Then

      to

      If Bit(BAUDCON2_BRG16) or Bit(BAUD2CON_BRG16) Then

      b. Add an inspection that the constant SPBRG_TEMP2 is zero. Is Zero issue a warning.

      c. Replicate this change to all USART 1,3,4 & 5 sections.

      d. Examine the ASM

      ASM is correct and the registers are no longer set to zero.

      ;PIC USART 2 Init
      ;Set baud rate
      ;SPBRG2 = SPBRGL_TEMP2
          movlw   129
          banksel SPBRG2
          movwf   SPBRG2,BANKED
      ;SPBRGH2 = SPBRGH_TEMP2
          movlw   6
          movwf   SPBRGH2,BANKED
      

      e. Test on real silicon

      Works.

      Resolved.

       

      Last edit: Anobium 2024-12-24
      • Anobium

        Anobium - 2024-12-24

        An idea. I could issue a DIFF file for all the DAT files for changes to the DAT files. See attached the DIFF for this specific Q10.

        But, to be honest there are so many changes it is hard to figure out what is what and you would need the DIFF tools ( part of SVN ).

        This shows that BAUDCON2_BRG16 was removed at the last DAT file regen.
        Anything is RED is deleted, GREEN is added. So, you can see the additions to the DAT file. These improvements would be missed if we did not autogenerate the DAT file.

        No easy answer here. Users needs to test projects as we continually improve GCBASIC.

        Anyone got any constructive thoughts ? It is very easy to be critical of the process but I will happily discuss and implement any and all improvements.

         

        Last edit: Anobium 2024-12-24
  • Anobium

    Anobium - 2024-12-24

    Latest USART.h

    Download by clicking the link above.

     
  • ikonsgr74

    ikonsgr74 - 2024-12-24

    With the new USART.h everything works ok! So it was Microchip's fault after all... :-)
    Thanks again Evan for the quick resolve, Merry Christmas and a productive-creative new year!

     
  • Anobium

    Anobium - 2024-12-24

    MERRY CHRISTMAS!!!

    Great news.

    We cannot blame anyone. We just learn all the time. :-)


    I have added the new USART.H to the release code. Thank you helping, again!

     
    ❤️
    1

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.