Go Down

Topic: Due not running "some" serial sketches (Read 2246 times) previous topic - next topic

djkrugger

I just started a project with a DUE board, it includes serial communication, but it seems to be something wrong in the DUE serial library or i'm getting crazy. The sketch works on the CAN bus of the DUE using Collin80's library. When i try to run this example
Code: [Select]
// Arduino Due - CAN Sample 1
// Brief CAN example for Arduino Due
// Test the transmission from CAN0 Mailbox 0 to CAN1 Mailbox 0
// By Thibaut Viard/Wilfredo Molina/Collin Kidder 2013

// Required libraries
#include "variant.h"
#include <due_can.h>

#define TEST1_CAN_COMM_MB_IDX    0
#define TEST1_CAN_TRANSFER_ID    0x07
#define TEST1_CAN0_TX_PRIO       15
#define CAN_MSG_DUMMY_DATA       0x55AAAA55

// CAN frame max data length
#define MAX_CAN_FRAME_DATA_LEN   8

// Message variable to be send
uint32_t CAN_MSG_1 = 0;

void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
  Serial.println("Type CAN message to send");
  while (Serial.available() == 0);
}
void loop(){

  while (Serial.available() > 0) {
    CAN_MSG_1 = Serial.parseInt();
    if (Serial.read() == '\n') {
      Serial.print("Sent value= ");
      Serial.println(CAN_MSG_1);
    }
  }

  // Initialize CAN0 and CAN1, baudrate is 1Mb/s
  CAN.init(SystemCoreClock, CAN_BPS_1000K);
  CAN2.init(SystemCoreClock, CAN_BPS_1000K);

  // Initialize CAN1 mailbox 0 as receiver, frame ID is 0x07
  CAN2.mailbox_init(0);
  CAN2.mailbox_set_mode(0, CAN_MB_RX_MODE);
  CAN2.mailbox_set_accept_mask(0, 0x7FF, false);
  CAN2.mailbox_set_id(0, TEST1_CAN_TRANSFER_ID, false);

  // Initialize CAN0 mailbox 0 as transmitter, transmit priority is 15
  CAN.mailbox_init(0);
  CAN.mailbox_set_mode(0, CAN_MB_TX_MODE);
  CAN.mailbox_set_priority(0, TEST1_CAN0_TX_PRIO);
  CAN.mailbox_set_accept_mask(0, 0, false);
  // Prepare transmit ID, data and data length in CAN0 mailbox 0
  CAN.mailbox_set_id(0, TEST1_CAN_TRANSFER_ID, false);
  CAN.mailbox_set_datal(0, CAN_MSG_1);
  CAN.mailbox_set_datah(0, CAN_MSG_DUMMY_DATA);
  CAN.mailbox_set_datalen(0, MAX_CAN_FRAME_DATA_LEN);

  // Send out the information in the mailbox
  CAN.global_send_transfer_cmd(CAN_TCR_MB0);

  // Wait for CAN1 mailbox 0 to receive the data
  while (!(CAN2.mailbox_get_status(0) & CAN_MSR_MRDY)) {
  }

  RX_CAN_FRAME incoming;
  // Read the received data from CAN1 mailbox 0
  CAN2.mailbox_read(0, &incoming);
  Serial.print("CAN message received= ");
  Serial.print(incoming.data[0]);
  Serial.print(incoming.data[1]);
  Serial.print(incoming.data[2]);
  Serial.print(incoming.data[3]);
  Serial.print(incoming.data[4]);
  Serial.print(incoming.data[5]);
  Serial.print(incoming.data[6]);
  Serial.println(incoming.data[7]);
 
  // Disable CAN0 Controller
  CAN.disable();

  // Disable CAN1 Controller
  CAN2.disable();

  Serial.print("End of test");

  while (1) {
  }
}

I get nothing on the serial monitor, i stripped all the sketch looking for the failure and ended with this!
Code: [Select]
void setup() {

Serial.begin(9600);
Serial.print("whatever");


}

void loop() {
 
}
And still get nothing!, now if i put the Serial.print on the loop it works, repeatedly of course. I tried the AnalogInOutSerial example and it works great, tried the loopback test and it runs fine, flashed the 16u2 firmware and same thing, tried both USB ports, tried adding  "delay(500)" between Serial.begin and Serial.print and nada. I'm out of ideas, anyone saw something similar? or have an idea what else to try?.

nickgammon

Read: http://arduino.cc/en/Guide/ArduinoDue

In particular:

Quote

Native port

Opening and closing the ''Native' port at the baud rate of 1200bps triggers a "soft erase" procedure: the flash memory is erased and the board is restarted with the bootloader. If, for some reason, the MCU were to crash during this process, it is likely that the soft erase procedure wouldn't work as it's done in software by the MCU itself.

Opening and closing the Native port at a baudrate other than 1200bps will not reset the SAM3X. To use the serial monitor, and see what your sketch does from the beginning, you'll need to add few lines of code inside the setup(). This will ensure the SAM3X will wait for the SerialUSB port to open before executing the sketch:
Code: [Select]
while (!Serial) ;
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

djkrugger


Read: http://arduino.cc/en/Guide/ArduinoDue

In particular:

Quote

Native port

Opening and closing the ''Native' port at the baud rate of 1200bps triggers a "soft erase" procedure: the flash memory is erased and the board is restarted with the bootloader. If, for some reason, the MCU were to crash during this process, it is likely that the soft erase procedure wouldn't work as it's done in software by the MCU itself.

Opening and closing the Native port at a baudrate other than 1200bps will not reset the SAM3X. To use the serial monitor, and see what your sketch does from the beginning, you'll need to add few lines of code inside the setup(). This will ensure the SAM3X will wait for the SerialUSB port to open before executing the sketch:
Code: [Select]
while (!Serial) ;



Thanks Nick, i tried that and it still the same, i started to check the board and i found that i didn't have 3.3v on the reset button, i soldered a jumper from pin 47 of the SAM3X to the reset button and it works now resetting it manually, but i still have one problem the loop seems to halt if i include "if (Serial.read() == '\n') {" i tried this
Code: [Select]
void setup() {

  // start serial port at 9600 bps:
  Serial.begin(9600);
  Serial.println("Start");
  while (Serial.available() == 0);

}

void loop() {
  while (Serial.available() > 0) {
   int CAN_MSG_1 = Serial.parseInt();
    if (Serial.read() == '\n') {
      Serial.print("Sent value= ");
      Serial.println(CAN_MSG_1);
    }
  }
}


on a duemilanove and it does't work either. =(

guix

What if you try to put a little delay between Serial.begin and the Serial.print ?

djkrugger


What if you try to put a little delay between Serial.begin and the Serial.print ?
i tried that up to 1000ms, but anyway it is printing "start' now, it just halts the loop whenever i include "if (Serial.read() == '\n') {" please note that it is happening with a Duemilanove too, i will try an older IDE version as i'm using the 1.5 beta.

Go Up