2

What´s working so far:

  • Sending bytes from PC with USB-Converter to my PIC32MX with Click-Board RS485 on it
  • UART5 used to receive data and these data are sent back over UART4 to read it in a Terminal
  • the bytes are being echod correctly, BUT:

Problem:

  • Some additional garbage bytes like '<0>' or 0x01 are received by the PIC32 (did debug it already on my PIC32)
  • When I use printf for echoing data in the interrupt, less (but anyway some) addional bytes are received

Here is my Code:

void init() {
        SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

        /************************ UART5 for RS485 *********************************/
        UARTConfigure(UART_MODULE_ID, UART_ENABLE_PINS_TX_RX_ONLY);
        //UARTSetFifoMode(UART_MODULE_ID, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
        UARTSetFifoMode(UART_MODULE_ID, UART_INTERRUPT_ON_RX_NOT_EMPTY);
        UARTSetLineControl(UART_MODULE_ID, UART_DATA_SIZE_8_BITS | UART_PARITY_EVEN | UART_STOP_BITS_1);
        UARTSetDataRate(UART_MODULE_ID, GetPeripheralClock(), 19200);
        UARTEnable(UART_MODULE_ID, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));

        // Configure UART RX Interrupt
        INTEnable(INT_SOURCE_UART_RX(UART_MODULE_ID), INT_ENABLED);
        INTSetVectorPriority(INT_VECTOR_UART(UART_MODULE_ID), INT_PRIORITY_LEVEL_2);
        INTSetVectorSubPriority(INT_VECTOR_UART(UART_MODULE_ID), INT_SUB_PRIORITY_LEVEL_0);

        /************************* UART4 - for debugging **************************/
        UARTConfigure(UART4, UART_ENABLE_PINS_TX_RX_ONLY);
        UARTSetLineControl(UART4, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
        UARTSetDataRate(UART4, GetPeripheralClock(), 19200);
        UARTEnable(UART4, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_TX));

        // Enable multi-vector interrupts
        INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
        INTEnableInterrupts();}

void __ISR(_UART_5_VECTOR, ipl2) IntUart5Handler(void) {
        volatile UINT state;
        unsigned char received;

        // Is this an RX interrupt?
        if (INTGetFlag(INT_SOURCE_UART_RX(UART_MODULE_ID))) {

            INTClearFlag(INT_SOURCE_UART_RX(UART_MODULE_ID));

            state = UARTGetLineStatus(UART_MODULE_ID);

            if (UART_DATA_READY & state > 0
                    && (state & (UART_OVERRUN_ERROR |
                    UART_PARITY_ERROR |
                    UART_FRAMING_ERROR |
                    UART_OVERRUN_ERROR)) == 0) { 
                received = UARTGetDataByte(UART_MODULE_ID);

                printf("%c", received);

            } 
        }

        // We don't care about TX interrupt
        if (INTGetFlag(INT_SOURCE_UART_TX(UART_MODULE_ID))) {
            INTClearFlag(INT_SOURCE_UART_TX(UART_MODULE_ID));
        }
    }

void _mon_putc(char c) {
            while (!UARTTransmitterIsReady(UART4));
            UARTSendDataByte(UART4, c);
            while (!UARTTransmissionHasCompleted(UART4));
        }

I didnt put the PPSOutput like in plib-Example. Could that be an issue? I guess the mapping here isnt necessary because it takes its standard pins?

I hope you can help me, somehow. I am really frustated and have tried hard, but I just cant figure out, why I receive these additional bytes.


EDIT: SOLVED THE PROBLEM:

Found the solution here: http://www.edaboard.com/thread195556.html

Fact is, that u need to wait, till the data is available! Added the following line and its working perfectly!

        while(!UARTReceivedDataIsAvailable(UART_MODULE_ID));
        received = UARTGetDataByte(UART_MODULE_ID);
user3447279
  • 89
  • 11
  • 1
    Note: printf() is not safe in an interrupt handler or signal handler.(mostly because it can call malloc(), which is not async-safe) – joop Mar 21 '14 at 17:22
  • Ok, thank you for your hint! printf is calling _mon_putc in this case ... shouldnt be a problem? – user3447279 Mar 21 '14 at 17:31
  • Tried it by calling the renamed _mon_putc directly and its the same result. – user3447279 Mar 21 '14 at 17:52
  • It is not necessarily the source of the problem, just a bad habit that will bite back at you when you don't expect it. BTW: why dont use a larger (than one) (circular) buffer ? Start by allocating it globally, so you can use printfs() to monitor the state in the outer code. – joop Mar 21 '14 at 18:00
  • Yep, did so before and i even got more garbage bytes ... just want to get rid of them, :-( – user3447279 Mar 21 '14 at 18:10
  • I don't see anything obviously wrong. Are you sure the transmitter isn't sending them? – Jay Mar 21 '14 at 21:06
  • Thx for your idea: 1.) Its communicated over a protocol and there are extra symbols in it 2.) Its always on the 2nd Byte with different value ... maybe I am just wrong with the protocol difinition, but I doubt so, because I get extra chars when I dont send the seperat UART4... – user3447279 Mar 22 '14 at 21:10

0 Answers0