I am writing a hardware abstraction layer for a PIC32MZ2048EFH064. Right now I am focusing on UART. I am fairly inexperienced with low level programming but I have done a few simple projects with other PIC32 microcontrollers. I am experiencing a strange issue that I am hoping someone can explain. Here is the breakdown.
I have code, where I manually set the registers for UART1 that works. Right now the main code just spams a message to make debugging easier.
SYSKEY = 0x00000000; // unlock sequence
SYSKEY = 0xAA996655;
SYSKEY = 0x556699AA;
\\ Configure PPS pins
CFGCONbits.IOLOCK = 0U
U1RXR = 0b0000; // 0b0000 = RPD2
RPD3R = 0b0001; // 0b0001 = U1TX
U1CTSR = 0b0100; // 0b0100 = RPD4
RPD5R = 0b0001; // 0b0001 = U1RTS
CFGCONbits.IOLOCK = 1U;
SYSKEY = 0x33333333; // lock
U1MODEbits.BRGH = 0; // Low baud rate
U1BRG = 650; // 100 MHz PBCLK2, 9600 baud
U1STA = 0; // Disable TX and RX Pins, clear all flags
U1MODEbits.PDSEL = 0; // 8-bit, no parity
U1MODEbits.STSEL = 0; // 1 stop bit
U1STAbits.URXEN = 1; // Enable RX pin
U1STAbits.UTXEN = 1; // Enable TX pin
U1MODEbits.UEN = 0b10; // (0b00) RX, TX, are enabled. no RTS, and CTS flow control 0b10 = TX,RX,CTS, and RTS
U1MODEbits.ON = 1; // Turn UART on
//Write data from UART1
void WriteUART(const char *string){
while(*string != '\0'){
while(U1STAbits.UTXBF){
asm("nop"); // wait until the tx buffer isn't full
}
U1TXREG = *string;
++string;
}
}
The above configuration and write function work just fine. Below you will find the configuration code that I am attempting to implement. I am using this configuration with the above write function. The code that sets the registers is at the bottom of this code sample. This code lives in a uart.c file.
typedef enum {
UART1 = 1,
UART2 = 2,
UART3 = 3,
UART4 = 4,
UART5 = 5,
UART6 = 6
} uart_id_t;
typedef struct {
uint32_t pbclk_hz;
uint32_t baud;
} uart_time_t;
typedef struct {
pps_pin_t rx;
pps_pin_t tx;
pps_pin_t rts;
pps_pin_t cts;
} uart_pps_t;
typedef struct {
uart_id_t id;
uint32_t baud;
uart_pps_t pins;
} UART;
void uart_construct(UART* obj,uart_id_t id, uint32_t baud_rate, pps_pin_t RX_pin,
pps_pin_t TX_pin, pps_pin_t RTS_pin, pps_pin_t CTS_pin){
obj->id = id;
obj->baud = baud_rate;
obj->pins.rx = RX_pin;
obj->pins.tx = TX_pin;
obj->pins.rts = RTS_pin;
obj->pins.cts = CTS_pin;
}
static volatile uint32_t* get_brg_reg(uart_id_t id){
switch(id){
case 1: return &U1BRG;
case 2: return &U2BRG;
case 3: return &U3BRG;
case 4: return &U4BRG;
case 5: return &U5BRG;
case 6: return &U6BRG;
default: return 0;
}
}
static volatile uint8_t get_TX_code(uart_id_t id){
switch(id){
case 1: return pps_out_func[PPS_U1TX];
case 2: return pps_out_func[PPS_U2TX];
case 3: return pps_out_func[PPS_U3TX];
case 4: return pps_out_func[PPS_U4TX];
case 5: return pps_out_func[PPS_U5TX];
case 6: return pps_out_func[PPS_U6TX];
default: return 0;
}
}
static volatile uint8_t get_RTS_code(uart_id_t id){
switch(id){
case 1: return pps_out_func[PPS_U1RTS];
case 2: return pps_out_func[PPS_U2RTS];
case 3: return pps_out_func[PPS_U3RTS];
case 4: return pps_out_func[PPS_U4RTS];
case 5: return pps_out_func[PPS_U5RTS];
case 6: return pps_out_func[PPS_U6RTS];
default: return 0;
}
}
static volatile uint32_t* get_RX_reg(uart_id_t id){
switch(id){
case 1: return pps_in_reg[PPS_U1RX];
case 2: return pps_in_reg[PPS_U2RX];
case 3: return pps_in_reg[PPS_U3RX];
case 4: return pps_in_reg[PPS_U4RX];
case 5: return pps_in_reg[PPS_U5RX];
case 6: return pps_in_reg[PPS_U6RX];
default: return 0;
}
}
static volatile uint32_t* get_CTS_reg(uart_id_t id){
switch(id){
case 1: return pps_in_reg[PPS_U1CTS];
case 2: return pps_in_reg[PPS_U2CTS];
case 3: return pps_in_reg[PPS_U3CTS];
case 4: return pps_in_reg[PPS_U4CTS];
case 5: return pps_in_reg[PPS_U5CTS];
case 6: return pps_in_reg[PPS_U6CTS];
default: return 0;
}
}
static volatile uint32_t* status_reg(uart_id_t id){
switch(id){
case 1: return &U1STA;
case 2: return &U2STA;
case 3: return &U3STA;
case 4: return &U4STA;
case 5: return &U5STA;
case 6: return &U6STA;
default: return 0;
}
}
static volatile uint32_t* mode_reg(uart_id_t id){
switch(id){
case 1: return &U1MODE;
case 2: return &U2MODE;
case 3: return &U3MODE;
case 4: return &U4MODE;
case 5: return &U5MODE;
case 6: return &U6MODE;
default: return 0;
}
}
static volatile uint32_t* get_receive_reg(uart_id_t id){
switch(id){
case 1: return &U1RXREG;
case 2: return &U2RXREG;
case 3: return &U3RXREG;
case 4: return &U4RXREG;
case 5: return &U5RXREG;
case 6: return &U6RXREG;
default: return 0;
}
}
static volatile uint32_t* get_transmit_reg(uart_id_t id){
switch(id){
case 1: return &U1TXREG;
case 2: return &U2TXREG;
case 3: return &U3TXREG;
case 4: return &U4TXREG;
case 5: return &U5TXREG;
case 6: return &U6TXREG;
default: return 0;
}
}
void set_TX_reg(UART* uart){
volatile uint32_t* TX_reg = pps_out_reg[uart->pins.tx];
if (TX_reg){
*TX_reg = get_TX_code(uart->id);
}
}
void set_RTS_reg(UART* uart){
volatile uint32_t* RTS_reg = pps_out_reg[uart->pins.rts];
if (RTS_reg){
*RTS_reg = get_RTS_code(uart->id);
}
}
void set_RX_reg(UART* uart){
volatile uint32_t* RX_reg = get_RX_reg(uart->id);
if (RX_reg){
*RX_reg = pps_in_pins[uart->pins.rx];
}
}
void set_CTS_reg(UART* uart){
volatile uint32_t* CTS_reg = get_CTS_reg(uart->id);
if (CTS_reg){
*CTS_reg = pps_in_pins[uart->pins.rx];
}
}
void set_baud(UART* uart){
volatile uint32_t* reg = get_brg_reg(uart->id);
if(reg){
*reg = PBCLK_FREQ/(16*uart->baud)-1;
}
}
void config_uart_pins(UART* uart){
set_RX_reg(uart);
set_TX_reg(uart);
if (uart->pins.cts != PPS_PIN_NONE && uart->pins.cts != PPS_PIN_NONE){
set_RTS_reg(uart);
set_CTS_reg(uart);
}
}
void init_uart(UART* uart){
// Follow these steps to set up a UART transmission:
// 1. Initialize the UxBRG register for the appropriate baud rate (refer to 21.3 ?UART Baud
// Rate Generator?).
// 2. Set the number of data and Stop bits, and parity selection by writing to the PDSEL<1:0>
// bits (UxMODE<2:1>) and STSEL bit (UxMODE<0>).
// 3. If transmit interrupts are desired, set the UxTXIE control bit in the corresponding Interrupt
// Enable Control register (IEC). Specify the interrupt priority and subpriority for the transmit
// interrupt using the UxIP<2:0> and UxIS<1:0> control bits in the corresponding Interrupt
// Priority Control register (IPC). Also, select the Transmit Interrupt mode by writing to the
// UTXISEL bits (UxSTA<15:14>).
// 4. Enable the transmission by setting the UTXEN bit (UxSTA<10>), which also sets the
// UxTXIF bit. The UxTXIF bit should be cleared in the software routine that services the
// UART transmit interrupt. The operation of the UxTXIF bit is controlled by the UTXISEL
// control bits.
// 5. Enable the UART module by setting the ON bit (UxMODE<15>).
// 6. Load data to the UxTXREG register (starts transmission).
volatile uint32_t* sta_reg_add = status_reg(uart->id);
volatile uint32_t* mode_reg_add = mode_reg(uart->id);
*mode_reg_add = 0; // disable and remove all settings
*sta_reg_add = 0;
CFGCONbits.IOLOCK = 0U; // unlock peripheral pins for configuration
config_uart_pins(uart); // configure the pins
CFGCONbits.IOLOCK = 1U; // lock peripheral pins
set_baud(uart);
*mode_reg_add |= (0b0<<4); // low baud rate (UxMODEbits.BRGH)(bit <4>)
*mode_reg_add |= (0b00<<1); // 8-bit, no parity (UxMODEbits.PDSEL)(bits <2,1>)
*mode_reg_add |= (0b0); // 1 stop bit (UxMODEbits.STSEL) (UxMODE bit <0>)
*sta_reg_add |= (0b1<<12); // Enable RX pin (UxSTAbits.URXEN) (UxSTA bit <12>)
*sta_reg_add |= (0b1<<10); // Enable TX pin (UXSTAbits.UTXEN) (UxSTA bit <10>)
if (uart->pins.rts != PPS_PIN_NONE && uart->pins.cts != PPS_PIN_NONE){ // Enable (UxMODEbits.UEN)(bits <9,8>)
*mode_reg_add |= (0b10<<8); // TX,RX,CTS,RTS enabled with flow control
} else {
*mode_reg_add |= (0b00<<8); // TX,RX only, no flow control
}
*mode_reg_add |= (0b1<<15); // Turn UART on (UxMODEbits.ON) (bit <15>)
}
I have another .h and .c to get the values and registers of the PPS pins. I won't put it here as this is already quite long. Then in main I set up uart 1 with
UART uart1;
sys_unlock();
uart_construct(&uart1,UART1,9600,PPS_RPD2,PPS_RPD3,PPS_RPD5,PPS_RPD4);
init_uart(&uart1);
sys_lock();
sys_unlock and sys_long just implement the SYSKEY sequences you can see in the working code. From there the code goes into an infinite loop that spams a simple message using the write function from the working code. What I have been doing is writing if statements that check the registers to confirm that they are what I think they are. If so, then the microcontroller turns on an LED (active low logic). For example, this can go before the infinite loop
if (U1BRG == 650) {
LATFbits.LATF5 = 0; // Turn on LED if HAL successfully wrote the registers
}
The registers all seem to be set correctly however nothing is transmitting. That isn't to strange by itself. But, if I add this check before the loop then everything works just fine and the message begins to transmit.
if (U1MODEbits.ON) {
LATFbits.LATF5 = 0;
}
So I guess the question is, what is it about checking U1MODEbits.ON that makes the thing work, or what is it about my code that makes it not work if I don't check the ON bit? I suspect that the issue is the way that I configure the registers using pointers but I am having a hard time finding a better way to do it. Since I have never done HAL before I also wonder if there are other methods I should consider.
Source: Electronics Stack Exchange Archive (CC BY-SA 4.0)