Go Down

Topic: ARDUINO DUE Native Port Baudrate Can't Go Up. (Read 1 time) previous topic - next topic

morris861211

Hi there,
I use the programming port on DUE to upload my code, then I plug out USB from the programming port and connect it to the Native port.
Select Native USB port on both boards and COM and open Serial monitor.

But here is the question, no matter how high the baud rate I tried to the SerialUSB.begin() or select high baud rate from Serial monitor, I always get the same speed of baud rate.
I do not know is there anything I'm missing?

Here is my code.

Code: [Select]
#include <SPI.h>
#include <avr/pgmspace.h>

// Registers
#define REG_Product_ID                           0x00
#define REG_Revision_ID                          0x01
#define REG_Motion                               0x02
#define REG_Delta_X_L                            0x03
#define REG_Delta_X_H                            0x04
#define REG_Delta_Y_L                            0x05
#define REG_Delta_Y_H                            0x06
#define REG_SQUAL                                0x07
#define REG_Pixel_Sum                            0x08
#define REG_Maximum_Pixel                        0x09
#define REG_Minimum_Pixel                        0x0a
#define REG_Shutter_Lower                        0x0b
#define REG_Shutter_Upper                        0x0c
#define REG_Frame_Period_Lower                   0x0d
#define REG_Frame_Period_Upper                   0x0e
#define REG_Configuration_I                      0x0f
#define REG_Configuration_II                     0x10
#define REG_Frame_Capture                        0x12
#define REG_SROM_Enable                          0x13
#define REG_Run_Downshift                        0x14
#define REG_Rest1_Rate                           0x15
#define REG_Rest1_Downshift                      0x16
#define REG_Rest2_Rate                           0x17
#define REG_Rest2_Downshift                      0x18
#define REG_Rest3_Rate                           0x19
#define REG_Frame_Period_Max_Bound_Lower         0x1a
#define REG_Frame_Period_Max_Bound_Upper         0x1b
#define REG_Frame_Period_Min_Bound_Lower         0x1c
#define REG_Frame_Period_Min_Bound_Upper         0x1d
#define REG_Shutter_Max_Bound_Lower              0x1e
#define REG_Shutter_Max_Bound_Upper              0x1f
#define REG_LASER_CTRL0                          0x20
#define REG_Observation                          0x24
#define REG_Data_Out_Lower                       0x25
#define REG_Data_Out_Upper                       0x26
#define REG_SROM_ID                              0x2a
#define REG_Lift_Detection_Thr                   0x2e
#define REG_Configuration_V                      0x2f
#define REG_Configuration_IV                     0x39
#define REG_Power_Up_Reset                       0x3a
#define REG_Shutdown                             0x3b
#define REG_Inverse_Product_ID                   0x3f
#define REG_Motion_Burst                         0x50
#define REG_SROM_Load_Burst                      0x62
#define REG_Pixel_Burst                          0x64
//#define BUFSIZE 300

byte initComplete=0;
volatile byte xydat[4];
int16_t * x = (int16_t *) &xydat[0];
int16_t * y = (int16_t *) &xydat[2];

volatile byte movementflag=0;
const int ncs = 10;   

extern const unsigned short firmware_length;
extern const unsigned char firmware_data[];

void setup() {
 
  SerialUSB.begin(500000);
  while (!SerialUSB);
  pinMode(ncs, OUTPUT);
  SPI.begin();
  SPI.setDataMode(SPI_MODE3);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(42);
  delay(1);
 
  performStartup();
  delay(100);
}


void adns_com_begin(){
  digitalWrite(ncs, LOW);
}

void adns_com_end(){
  digitalWrite(ncs, HIGH);
}


byte adns_read_reg(byte reg_addr){
  adns_com_begin();
 
  SPI.transfer(reg_addr & 0x7f);
  delayMicroseconds(100); // tSRAD 
  byte data = SPI.transfer(0);
  delayMicroseconds(1); // tSCLK-NCS for read operation is 120ns
  adns_com_end();
  delayMicroseconds(19); //  tSRW/tSRR (=20us) minus tSCLK-NCS

  return data;
}

void adns_write_reg(byte reg_addr, byte data){
  adns_com_begin();
 

  SPI.transfer(reg_addr | 0x80 );
 
  SPI.transfer(data);
 
  delayMicroseconds(20); // tSCLK-NCS for write operation
  adns_com_end();
  delayMicroseconds(100); // tSWW/tSWR (=120us) minus tSCLK-NCS. Could be shortened, but is looks like a safe lower bound
}

void adns_upload_firmware(){

  adns_write_reg(REG_Configuration_IV, 0x02); // bit 1 = 1 for 3k mode, other bits are reserved

  adns_write_reg(REG_SROM_Enable, 0xd);

  delay(10); // assume that the frame rate is as low as 100fps... even if it should never be that low
 

  adns_write_reg(REG_SROM_Enable, 0x8);
 

  adns_com_begin();
  SPI.transfer(REG_SROM_Load_Burst | 0x80); // write burst destination adress
  delayMicroseconds(15);
 
 
  unsigned char c;
  for(int i = 0; i < firmware_length; i++){
    c = (unsigned char)pgm_read_byte(firmware_data + i);
    SPI.transfer(c);
    delayMicroseconds(15);
  }
  adns_com_end();
  }


void performStartup(void){
  adns_com_begin(); // ensure that the Serial port is reset
  adns_com_end(); // ensure that the Serial port is reset
  adns_com_begin();
 
  adns_write_reg(REG_Power_Up_Reset, 0x5a); // force reset
  delay(50);
 
  adns_read_reg(REG_Motion);
  adns_read_reg(REG_Delta_X_L);
  adns_read_reg(REG_Delta_X_H);
  adns_read_reg(REG_Delta_Y_L);
  adns_read_reg(REG_Delta_Y_H); 
  // upload the firmware
  adns_upload_firmware();
  delay(10);

 
  byte laser_ctrl0 = adns_read_reg(REG_LASER_CTRL0);
  adns_write_reg(REG_LASER_CTRL0, laser_ctrl0 & 0xf0 );
  delay(1);
}

void frameCapture(){

  digitalWrite(ncs, LOW);
  delayMicroseconds(1);
  SPI.transfer(REG_Frame_Capture | 0x80);
  SPI.transfer(0x93);
 
  SPI.transfer(REG_Frame_Capture | 0x80);
  SPI.transfer(0xc5);


  digitalWrite(ncs, HIGH);

  delayMicroseconds(200); // assuming a very slow frame rate ~200Hz
  digitalWrite(ncs, LOW);

  delayMicroseconds(100); //TsRAD

  byte readys = 0;
  while(readys == 0){
    SPI.transfer(REG_Motion & 0x7f);
    delayMicroseconds(100); // tSRAD
    readys = SPI.transfer(0);
    readys = readys & 1;
    delayMicroseconds(20);
  }


  SPI.transfer(REG_Pixel_Burst & 0x7f);
  delayMicroseconds(100); // tSRAD

   
 
  int i;
  for(i = 0; i < 900; i++){

 
   
   
    byte output = 0;
    output = SPI.transfer(0);
   
   
    SerialUSB.print(0);
    SerialUSB.print(" ");
    SerialUSB.println(output);
    SerialUSB.print(" ");
   
    delayMicroseconds(15);
   
 
  }
 

  digitalWrite(ncs, HIGH);
  delayMicroseconds(4);
  delayMicroseconds(100);

}



  int numCaptured = 0;

  void loop() {
     
        frameCapture();
     
  }

pert

This is the normal behavior of the USB CDC serial ports like SerialUSB on the Due. It doesn't matter what baud rate you set with SerialUSB.begin(). You can set any baud rate you like in Serial Monitor. The baudrate set by Serial.begin() only matters with the non-CDC serial ports.

morris861211

So, is the baud rate automatically set by the device if I use the native port?

If so, what can I do to increase the baud rate? 

pjrc

The native port is virtual serial.  The data is always sent at the USB speed.  There isn't any baud rate setting that changes the actual communication speed.  However, USB uses a complex protocol which includes flow control.  So the speed depends on how rapidly both Due and your PC are ready to communicate.  The design and efficiency of drivers on both ends matters greatly.

Recently I've been putting quite a lot of work into measuring and optimizing native USB communication speed.  Here is a quick benchmark program I wrote.

https://github.com/PaulStoffregen/USB-Serial-Print-Speed-Test/blob/master/usb_serial_print_speed.ino

It does 4 prints, to measure how rapidly a board can send a typical line of information with 2 numbers and 2 strings.  I'm running here on a Due, seeing about 15840 lines/sec, using a 64 bit Linux system to run the Arduino IDE receiving the data.

While this won't make it go faster, it can at least give you a sanity check to measure against, to see if your Due and PC are communicating slower, about the same, or faster.

westfw

How fast is it going for you?
Presumably, this is the part that is going slower than you'd want?

Quote
Code: [Select]
  for(i = 0; i < 900; i++){
    byte output = 0;
    output = SPI.transfer(0);
   
    SerialUSB.print(0);
    SerialUSB.print(" ");
    SerialUSB.println(output);
    SerialUSB.print(" ");
   
    delayMicroseconds(15);
  }

15 microseconds of delay for each SPI byte will "obviously" limit you to about 500kbps input, and of course you're printing at least 5 bytes ("0 x\n ") on SerialUSB for each SPI byte...


Go Up