I'm testing a routine to transmit a string of characters using standard 2400_8N2 serial format on UART0, then immediately convert the Tx pin to receive a PWM response. As part of this I want to use the TC interrupt to indicate that the stop bits for the last character have been sent.
Even though I am explicitly setting the TCIE and TIE bits in UART0_C2, the NVIC_ENABLE_IRQ(IRQ_UART0_STATUS) function is clearing these and setting the RIE, RLIE, and RE bits, changing the register from 0xC8 to 0x3C..
My understanding is that NVIC_ENABLE_IRQ should simply activate the associated vector without affecting any of the associated register bits - this is obviously incorrect. If someone can enlighten me as to what is happening, I'd be grateful.
Code:
#define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
byte message[2][6] = { { 0xFF, 0xFE, 0xFE, 0xFE, 0xFE, 0x00 },
{ 0xFF, 0xFE, 0xFE, 0xFE, 0xFE, 0x00 } };
int phase, modID, i;
volatile boolean uartFlag;
void setup()
{
Serial.begin(115200);
Serial.println("Teensy_interrupt_Test_V3_1115a");Serial.println(" ++++++++++++++++++++++++ ");
uart0Setup();
Serial.print(UART0_C1, HEX);Serial.print(" ");Serial.print(UART0_C2, HEX);Serial.print(" ");Serial.print(UART0_C3, HEX);Serial.print(" ");Serial.print(UART0_C4, HEX);Serial.print(" ");Serial.println(UART0_C5, HEX);
Serial.print(UART0_S1, HEX);Serial.print(" ");Serial.println(UART0_S2, HEX);Serial.println(SIM_SCGC5, HEX);Serial.println(" ***************************** ");
for (int i = 3; i < 13; i++)
{
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
modID = 0;
i = 0;
} // </setup()>
void loop()
{
message[0][5] = cmdCheckSum(message[0]) | modID;
message[1][5] = cmdCheckSum(message[1]) | modID;
for (int i = 0; i < 6; i++)
{
UART0_D = message[0][i];
}
modID++;
if (modID > 3) modID = 0;
delay(40);
} // </loop()>
void uart0Setup()
{
SIM_SCGC4 |= SIM_SCGC4_UART0; //Turn on clock to UART0
PORTB_PCR17 = 0x300; // Alt 3 for PTB17(pin 1) to be UART0 output
UART0_BDH = 0x07; // 2400 bd @ 72 MHz
UART0_BDL = 0x53;
UART0_C2 = 0x00; // Clear to allow FIFO setup
UART0_PFIFO = 0xA0; // Enable 8-bit FIFO
UART0_CFIFO = 0x80; // Flush FIFO
UART0_C1 = 0x10; // M = 1 for 9 bits (2nd stop bit = bit 9)
UART0_C2 |= (UART_C2_TIE | UART_C2_TCIE | UART_C2_TE); // Enable tx interrupts and tx function
Serial.print(" -- ");Serial.println(UART0_C2, HEX);
UART0_C3 = 0x40; // T8 set for 2nd stop bit
NVIC_SET_PRIORITY(IRQ_UART0_STATUS, IRQ_PRIORITY);
NVIC_ENABLE_IRQ(IRQ_UART0_STATUS);
Serial.print(" -- ");Serial.println(UART0_C2, HEX);
} // </uart0Setup()>
void uart0_ISR()
{
if ((UART0_C2 & UART_C2_TCIE) && (UART0_S1 & UART_S1_TC))
{
uartFlag = true;
}
} // </uart0_ISR()>
byte cmdCheckSum(byte data[])
{
// int dataLength = sizeof(data);
int CS = 0;
for (int i = 1; i < 5; i++)
{
CS = CS + data[i];
}
CS = CS + (CS >> 8);
CS = CS + (CS << 4);
CS = CS & 0xF0;
return CS;
}
Even though I am explicitly setting the TCIE and TIE bits in UART0_C2, the NVIC_ENABLE_IRQ(IRQ_UART0_STATUS) function is clearing these and setting the RIE, RLIE, and RE bits, changing the register from 0xC8 to 0x3C..
Code:
Teensy_interrupt_Test_V3_1115a
++++++++++++++++++++++++
-- C8
-- 3C
10 3C 40 0 0
C0 0
43F82
*****************************
My understanding is that NVIC_ENABLE_IRQ should simply activate the associated vector without affecting any of the associated register bits - this is obviously incorrect. If someone can enlighten me as to what is happening, I'd be grateful.