problem s uartem procesoru PIC32MX795F512L - poskozeny prvni byte

nevrklap na volny.cz nevrklap na volny.cz
Středa Červenec 8 20:40:10 CEST 2015


Zdravim,
mame tu dva male projekty zalozene na procesoru PIC32MX795F512L.. (Microchip si vybral kolega programator, prosim nekomentovat :-)
V obou pripadech se projevuje stejny problem - prvni byte (po zapnuti zarizeni) prijaty pres UART (odeslany typicky z PC) se neprijme korektne... vyvola se korektne preruseni, ale precte se nesmysl. Dalsi znaky uz chodi v poradku (vsechno typicky jednobytove prikazy..).
Je to ponekud otravne, nesetkal se s tim prosim nekdo?  Tento odkaz jsem kolegovi daval http://www.microchip.com/forums/m531951.aspx  ale zatim to nepomohlo...
Nize je vyseknuty kod, ktery zmineny UART pouziva, pokud by mel nekdo chut se na to mrknout...
Diky moc
P.





#include <plib.h>
#include <stdlib.h>

#define UART_HS_9600_ID              (3)     // Identifikator
#define UART_HS_9600_BRG             (1041)  // BRG value
#define UART_HS_9600_BRGH            (1)     // BRGH (high speed)

#define UART_HS_115200_ID            (9)     // Identifikator
#define UART_HS_115200_BRG           (86)    // BRG value
#define UART_HS_115200_BRGH          (1)     // BRGH (high speed)
    
#define UART_B_COMPILER              UART2  


/**
 * Init UART module
 */
void UartInit(void){
    PORTSetPinsDigitalIn(IOPORT_UART_RX, BIT_UART_RX); // INPUT
    PORTReadBits(IOPORT_UART_RX, BIT_UART_RX);
    PORTSetPinsDigitalOut(IOPORT_UART_TX, BIT_UART_TX); // OUTPUT
    PORTSetBits(IOPORT_UART_TX, BIT_UART_TX);


    U2MODE = 0x8000; // Enable UART for 8-bit data, No parity, 1 stop
    U2MODESET = 0x80; // Enable UART wake-up from sleep

    // Nastavim Baud Rate pro High Speed rezim:
    UART_Sens_B_SetBrg(psUART_Sens_B->psCfg->ucBaudRateID, SYS_CLK_HS);


    // Parity, stopbit:
    U2MODECLR = 0b110; // b2-1 = 8-bit data, no parity
    U2MODECLR = 1; // b0 = One stop bit 

    // Nastavim preruseni (PIC32MX795F512L.pdf str 123, Sec. 21 UART.pdf, str 23):
    IEC1SET = 0b1000000000; // set the UxRXIE bit in the corresponding Interrupt Enable Control register (IEC).
    IPC8SET = 0b1100; // Priority 3, b4:2 - Specify the priority (0 - 7, 0 = ISR disable) and subpriority for the interrupt using the UxIP<2:0> and UxIS<1:0>
    IPC8SET = 0b10; // Subpriority 2, b1:0 - subpriority

    U2STA = 0; // Clear register
    U2STACLR = 0b11000000; // b7:6 - Interrupt flag bit is asserted while receive buffer is not empty
    U2STASET = 0x1400; // Enable Transmit and Receive
}


/**
 * Change UART speed on-the-fly
 */
void UART_Sens_B_SetBrg(unsigned char ucBaudRateID) {
    unsigned short usBackUp;


    // Disable Rx and Tx
    usBackUp = U2STA & 0x1400; // Backup actuall state.
    U2STACLR = 0x1400; 	// Disable

    // Clear b3 = BRGH
    U2MODECLR = 0b1000;

    // Nastavim Baud Rate
    switch (psUART_Sens_B->psCfg->ucBaudRateID) {
        case UART_HS_9600_ID:
            U2BRG = UART_HS_9600_BRG; // Set Baud rate
            U2MODESET = UART_HS_9600_BRGH << 3; // b3 = BRGH
            break;
        case UART_HS_115200_ID:
            U2BRG = UART_HS_115200_BRG; // Set Baud rate
            U2MODESET = UART_HS_115200_BRGH << 3; // b3 = BRGH
            break;
    }

    // Reenable UART module:
    U2STASET = usBackUp;	// Backuped value
    return;
}



/**
 * UART 2 interrupt handler, the priority set in init function
 */
void __ISR(_UART_2_VECTOR, ipl3) IntUart2Handler(void) {
    volatile char cDummy;

    //Is this an RX interrupt?
    if (INTGetFlag(INT_SOURCE_UART_RX(UART_B_COMPILER))) {
        cDummy = UARTGetDataByte(UART_B_COMPILER);	// Get data
        INTClearFlag(INT_SOURCE_UART_RX(UART_B_COMPILER)); // Clear the RX interrupt Flag
        QueueInsertLast(psQueueB, cDummy);	// Save data to my buffer
    }

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


Další informace o konferenci Hw-list