Reading UART registers causes my program to hang - why?

Status
Not open for further replies.

bmd1103

Member
I'm just starting with Teensys, so apologies if this is a rudimentary question.

I'm trying to port an app from Arduino to Teensy 3.2 so I can use the extra resources. My app involves sending a 6-byte serial 2400 8N2 message to a remote module, then receiving a response which is in a PWM-type format. As the remote module start replying very soon after receiving the last byte of the transmission, I need to turn off the transmitter and set up (hopefully) one of the FTM units using Input Capture mode to find the time of the returned data pulses.

As a first step, I've managed to get the data bytes coming out of the Teensy. I now want to access the TCIE bit in UART1_C2 to determine when the transmission is complete. However, I find that if I read a UART register, my program hangs.

My code is:
Code:
int timeOut;
int trigLo = 2240;
int trigHi = 560;
byte message[] = { 0xFF, 0xFE, 0xFE, 0xFE, 0xFE, 0x00 };
long intTime;
boolean int0Flag, timedOutFlag;
long temp;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(2400, SERIAL_8N2);
  Serial.println(" ----------------");
//  temp = UART1_C2;
  Serial.println(" ----------------");
  for (int i = 4; i < 13; i++)
  {
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);
  }
  Serial.print(FTM0_SC, HEX);Serial.print(" ");Serial.print(FTM0_MOD, HEX);Serial.print(" ");Serial.print(FTM0_C7SC, HEX);Serial.print(" ");Serial.print(FTM0_MODE, HEX);
  Serial.print(" "); Serial.print(SIM_SCGC6, HEX);Serial.print("  ");Serial.print(FTM0_FMS, HEX);Serial.print("  ");Serial.println(PORTD_PCR7, HEX);Serial.println(" ----------------");
  ftmSetup();
  FTM0_C7V = 0x2000;
  Serial.print(FTM0_SC, HEX);Serial.print(" ");Serial.print(FTM0_MOD, HEX);Serial.print(" ");Serial.print(FTM0_C7SC, HEX);Serial.print(" ");Serial.print(FTM0_MODE, HEX);
  Serial.print(" "); Serial.print(SIM_SCGC6, HEX);Serial.print("  ");Serial.print(FTM0_FMS, HEX);Serial.print("  ");Serial.println(PORTD_PCR7, HEX);
}

and the response with the line "temp = UART1_C2; "commented out is:

Code:
 ----------------
 ----------------
89 8FFF 28 4 6B000001  0  144
 ----------------
8 3AC0 28 5 6B000001  0  400
TOF set  8  A8
28
TOF set  8  A8
28

With the comment removed so I actually read UART1_C2, all I get is:

Code:
 ----------------

I'm successfully accessing the registers in the FTM.

I'm probably missing something pretty basic, but have looked at the source files in \Arduino\hardware\teensy\avr\cores\teensy3 and don't think there is anything wrong with what I am trying.

I'm using a Teensy 3.2
VID: 16C0
PID: 0483
SN: 506318

I'm accessing the Teensy from Windows 10 version 1803 with the 28/09 update. I'm running Teensyduino version 1.8.5.
 
Serial1 is UART0.
That access to UART1 is for Serial2 which is hardware that has not been initialized so it is faulting.
 
Pulled your code to confirm - it has no loop() and no definition of ftmSetup()

In this case I could see the error without running - but it is best to post complete samples that compile and run - even if they are cut down just to exhibit the issue.

ALSO - can confirm when I hacked the code changing this line from Serial1 allows it to run with the line that was commented out:: Serial2.begin(2400, SERIAL_8N2);


... - this post is the last I uploaded - and posts before and after and examples give an idea of how to use it.

Edit - I ran this because I've enabled FAULT trapping with some debug out put in a library. I included this library and indeed the code presented results in the expected Hard Fault being reported just with the include of : #include "debug_t3.h"
And an edit to make sure Serial is available before it can get to the fault:
Code:
void setup()
{
  Serial.begin(115200);
  while (!Serial && millis() < 5000 );
  Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);

  // ...
 
Every peripheral has a clock gate control. If the clock is turned off, any access will fault, even just trying to read the registers. Normally all this is handled by the HardwareSerial classses. But if you access the registers directly, you need to deal with these sorts of details.
 
Thanks for the quick responses, guys - the immediate problem was solved by referencing UART0 for Serial1, and I'll look into the clock gate controls.
 
Thanks for the quick responses, guys - the immediate problem was solved by referencing UART0 for Serial1, and I'll look into the clock gate controls.

That is done in Serial1.begin() for UART0 as Serial2.begin() does for UART1.
 
Loaded SIM_SCGC4 with 0x1C00 and got access. Now to get the rest of it going ...

I did go through serial1.c but hadn't clicked that it was referring to UART0. Knew it had to be something pretty basic. Thanks again.
 
Status
Not open for further replies.
Back
Top