Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UNO R4 WIFI] - periodicCallback() does not seem to work correctly as long as it includes Serial.println(). #138

Open
eMUQI opened this issue Sep 19, 2023 · 4 comments · May be fixed by #304
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project

Comments

@eMUQI
Copy link
Contributor

eMUQI commented Sep 19, 2023

Hello everyone, when I tried to run the example in https://docs.arduino.cc/tutorials/uno-r4-wifi/rtc#Periodic-Interrupt, I found some errors in it.

In order to make the code run correctly, I made modifications to the code. The modified code is as follows:

// Include the RTC library
#include "RTC.h"

const int led = LED_BUILTIN;

void setup() {
  pinMode(led, OUTPUT);

  Serial.begin(9600);

  // Initialize the RTC
  RTC.begin();

  // RTC.setTime() must be called for RTC.setPeriodicCallback to work, but it doesn't matter
  // what date and time it's set to
  RTCTime mytime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);
  RTC.setTime(mytime);

  if (!RTC.setPeriodicCallback(periodicCallback, Period::ONCE_EVERY_2_SEC)) {
    Serial.println("ERROR: periodic callback not set");
  }
}

void loop() {
}

// This is the callback function to be passed to RTC.setPeriodicCallback()
void periodicCallback()
{
  static bool ledState = false;
  if (ledState == true) {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  else {
    digitalWrite(LED_BUILTIN, LOW);
  }
  ledState = !ledState;

  Serial.println("PERIODIC INTERRUPT");
}

Unfortunately, when I uploaded the code to my Arduino R4 WiFi, its performance did not meet expectations.The LED did not blink at a two-second interval and it was unable to print the complete message on the serial monitor.
However, when I uploaded this code to Uno R4 Minima, everything worked fine.
(Additional information: The firmware version of Arduino R4 WiFi is 0.3.0, and the library version displayed in the board manager is 1.0.4.)

Another strange thing is that when I comment out the line of code Serial.println("PERIODIC INTERRUPT"); in the periodicCallback() function, the LED can blink once every two seconds as expected.

Does anyone know why this is? Or has anyone done the same test on the R4 wifi board? Thank you very much for any help.

156372dc3448e24e7c88397cd2bde6d6286fe72d_2_664x499

I also reported this issue on the Arduino community forum, see this link.

A kind-hearted person tested my code and obtained the same result. https://forum.arduino.cc/t/there-may-be-some-conflict-between-rtc-and-the-serial/1169836/5?u=wulu

@eMUQI eMUQI changed the title periodicCallback() does not seem to work correctly as long as it includes Serial.println("PERIODIC INTERRUPT");. periodicCallback() does not seem to work correctly as long as it includes Serial.println("PERIODIC INTERRUPT");. [R4 WIFI] Sep 19, 2023
@eMUQI eMUQI changed the title periodicCallback() does not seem to work correctly as long as it includes Serial.println("PERIODIC INTERRUPT");. [R4 WIFI] [UNO R4 WIFI] - periodicCallback() does not seem to work correctly as long as it includes Serial.println("PERIODIC INTERRUPT");. Sep 19, 2023
@eMUQI eMUQI changed the title [UNO R4 WIFI] - periodicCallback() does not seem to work correctly as long as it includes Serial.println("PERIODIC INTERRUPT");. [UNO R4 WIFI] - periodicCallback() does not seem to work correctly as long as it includes Serial.println(). Sep 19, 2023
@per1234 per1234 added the type: imperfection Perceived defect in any part of project label Sep 19, 2023
@Fashion-Corp
Copy link

Fashion-Corp commented Sep 19, 2023 via email

@eMUQI
Copy link
Contributor Author

eMUQI commented Sep 20, 2023

Other related issues and pull requests: #38 #90

@Fashion-Corp
Copy link

Fashion-Corp commented Sep 20, 2023 via email

@delta-G
Copy link
Contributor

delta-G commented May 6, 2024

Currently the UART class sends what you print directly to the HAL layer by pointer. The HAL layer sends the characters out one by one on the Serial line in the TXI interrupt. If you modify the data pointed to by the pointer that the HAL has while it is still being used to print, then it will corrupt the output.

I have submitted PR #304 which modifies the UART class to make use of the 512 byte ring buffer that is already allocated in the code but not used. This means that a copy of the data is made into the ring buffer and the HAL layer is getting a pointer to that. This will solve the particular issue you reference.

@per1234 per1234 linked a pull request May 9, 2024 that will close this issue
@per1234 per1234 added the topic: code Related to content of the project itself label May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants