I am trying to use two serial ports on the STM32 Nucleo-C092RC, but the 2nd serial port (i.e. USART1) is completely silent (USART2 is the default serial port used by STLink/VCP/USB on the C092RC). No output whatsoever.
I am using Arduino IDE version 2.1.1 and Windows 8 (later version of the IDE does not work on Windows 8).
My purpose is to output bytes to an IR module YS-IRTM and print the response code from the IR module on the USB, which uses USART2 via the STLink interface. I seemed to be getting no response on the IR module so I then substituted a TTL-USB converter for the IR module to see whether the data is being sent, and on the TTL-USB converter I am getting nothing. I am getting no stated errors during compiling.
The TTL-USB is connected to Arduino pins D0 and D1, which on the C092RC are Rx and Tx of USART1 (PB7 and PB6). Naturally I crossed the Rx and Tx lines.
The USART2/Serial2 port is connected to the STLink and the output works normally. I am not clear about how to instantiate Serial1; it looks like I have not succeeded. Do I need to declare the pin numbers for Serial1 or are they picked up from the hardware definition? Is there something else I need to declare to get Serial1 fully enabled?
First I created a text file build_opt.h in the sketch folder with the single line:
-DENABLE_HWSERIAL1
and closed the IDE. When I restarted the IDE the new build_opt.h tab appeared and seemed to be included in the compile. No output from the TTL-USB converter connected to Serial1.
I then added the line:
-DPIN_SERIAL1_RX=PB7 -DPIN_SERIAL1_TX=PB6
to build_opt.h but still no output on Serial1 (test print text to each serial port). Serial2 works perfectly.
I also tried define the Serial1 pins at the beginning of the main sketch but that also had no effect.
What am I doing wrong?
Here is the code I am using:
#define PIN_SERIAL1_RX PB7
#define PIN_SERIAL1_TX PB6
uint8_t my_serial_bytes[5]={0xA1, 0xF1, 0x00, 0xFF, 0x45};
int incomingByte = 0; // for incoming serial data
void setup() {
Serial1.begin(9600);
Serial2.begin(57600);
}
void loop() {
// code for transmitting
Serial1.write(my_serial_bytes,sizeof(my_serial_bytes));
delay(100);
if (Serial1.available() > 0) {
// read the response code from YS-IRTM
incomingByte = Serial1.read();
delay(10);
// say what you got
Serial2.print("Response code: ");
Serial2.println(incomingByte, HEX);
}
delay(2000); // delay 2sec
Serial1.println("test print serial1");
Serial2.println("test print serial2");
}