Go Down

Topic: Arduino Due libraries (official and 3rd party) (Read 265336 times) previous topic - next topic

antodom

Hi all,

I have developed a software serial library for the Arduino DUE which provides up to nine software serial objects associated with the Timer/Counter module channels available in the Atmel ATSAM3X8E. You can use those serial objects in the same manner that the hardware serial objects Serial, Serial1, Serial2 and Serial3.

I have called the library soft_uart, and it is openly available in the following link:

 http://bizet.dis.ulpgc.es/antodom/soft_uart

I have checked the library intensively with the hardware I have available, but any feedback or suggestion for improvement will be welcomed.

I hope it helps.

----
antodom
------------
antodom

antodom

Hi all,

I have made a small improvement to WInterrupts.c which allows a faster interrupt dispatching associated to generic I/O pins. That is, the callbacks we set with function attachInterrupt().

Basically I have modified the code of the interrupt handler functions PIOA_Handler(), PIOB_Handler(), PIOC_Handler() and PIOD_handler() in the original WInterrupts.c. The improvement consists mainly in taking advantage of ATSAM3XE assembler instruction CLZ. With this instruction it is possible to calculate the number of leading zeros in a 32 bit value in just one machine instruction. CLZ is available in C/C++ using CMSIS function __CLZ().

Using this small improvement I have been able to get to a bit rate of 115200 with my software serial library for the DUE, soft_uart (using the example provided with soft_uart and two stop bits). Take into account that soft_uart uses the interrupt for pin level changes on the RX pin associated to the software serial object when used. Without the improvement soft_uart could only get to 57600 (same example also with two stop bits).
                                                                                     
Herein I include attached the file WInterrupts.c with the PIO handlers modified (the previous versions of the handlers are also on the file, but commented). To illustrate the change here you have the code for handler PIOA_Handler() (the previous one appears commented), the other ones are alike:

Code: [Select]

//void PIOA_Handler(void) {
//      uint32_t isr = PIOA->PIO_ISR;                                                
//      uint32_t i;
//      for (i=0; i<32; i++, isr>>=1) {                                              
//              if ((isr & 0x1) == 0)                                                
//                      continue;
//              if (callbacksPioA[i])                                                
//                      callbacksPioA[i]();                                          
//      }      
//}                    

void PIOA_Handler(void) {                                                            
 register uint32_t isr = PIOA->PIO_ISR;                                              
 register uint8_t leading_zeros;                                                    
 while((leading_zeros=__CLZ(isr))<32)
 {
   register uint8_t pin=32-leading_zeros-1;                                          
   if(callbacksPioA[pin]) callbacksPioA[pin]();                                      
   isr=isr&(~(1<<pin));
 }
}


If you want to check this version of WInterrupts.c just overwrite the original with this new one.

Sincerely, I consider this change can benefit any code using interrupts associated to any Arduino DUE I/O pin with function attachInterrupt(). Once tested enough by more people, maybe it might be convenient to apply the change to the official Arduino DUE standard library. Anybody knows to whom I have to address to propose the change?.
------------
antodom

bikas

@antodom

""Anybody knows to whom I have to address to propose the change?""

i am not sure but probably Nick Gammon is the one you are looking for.

krtushink

Hi, I'm having a lot of problems with the libraries of DHT22 humidity and temperature sensor and Arduino DUE.

It seems to have an ARM microcontroller, that driver must have a configuación different pins, and not just go well.

There is someone who can send me a code that I work at least to put the value in a variable moisture and other temperature value?

From there and I would deal myself.

Thank you so much for everything.

Cheers,
Xavi

ArthurD

#109
Sep 10, 2015, 11:28 am Last Edit: Nov 22, 2015, 11:44 am by ArthurD
is there meanwhile a reasonable USB keyb lib available feat. standard C keyboard functions (stdin) like e.g.

kbhit()
getch()
getche()
getchar()
gets()
scanf()

?

currently the implementation of the USB.task is really weird, keyPressed and keyReleased don't return anything and it's totally osbscured how and when they do either or anything meaningful,
currently it's not even possible to indicate if a key was hit at all or not (like provided by kbhit() ),
usb.Task has to be called repeatedly but then blocks program execution intermediately and repeatedly for many seconds (which is unconscionable in the main loop() but shockingly also hinders different additional Scheduler multitasking threads ).

josheeg

I noticed my post on the Eigen library was for the pc and not the arduino one I wrote for the Due.
I used the library tutorial and got the newest stable release of Eigen to run.
I wold add my ziped library file if I could to make it easier to run Eigen libraries on the DUE in win/linux/mac.
A header and ccp file was needed I named the folder in home/arduino/libraries EIGEN.
The header file was Eigen.h and program file Eigen.cpp

Code: [Select]
/*
  Eigen.h - Library for adding Eigen library code.
  Created by Joshua Wojnas, September 15, 2015.
*/

#ifndef Eigen_h
#define Eigen_h

#include "Dense"
#include "LU"
#include "Arduino.h" //statement that gives you access to the standard types and constants of the Arduino

using Eigen::MatrixXd;
using namespace std;
using namespace Eigen;


#endif



Code: [Select]
/*
  Eigen.cpp - Library for adding Eigen library code.
  Created by Joshua Wojnas, September 15, 2015.
*/

#include "Dense"
#include "LU"
#include "Arduino.h"//give the rest of the code access to the standard Arduino functions
#include "Eigen.h"

using Eigen::MatrixXd;
using namespace std;
using namespace Eigen;



Then their was the program I used to test it by randomvibe

Code: [Select]
//Eigan library test by josheeg example by random vibe and eigen library team.
#include <Eigen.h>

// PRINT MATRIX (float type)
// By: randomvibe
//-----------------------------


void print_mtxf(const Eigen::MatrixXf& K);


void setup()
{
  Serial.begin(9600);

  // DECLARE MATRICES
  //--------------------
  MatrixXf Pp(6, 6);  // Produces 6x6 float matrix class
  MatrixXf H(6, 6);   // Note: without "using namespace Eigen", declaration would be: Eigen::MatrixXf H(6,6);
  MatrixXf R(6, 6);
  MatrixXf X(6, 6);
  MatrixXf K(6, 6);
  MatrixXf Z(6, 6);

  // INPUT MATRICES (so-called comma-initialize syntax)
  //---------------------------------------------------------
  Pp << 0.3252,  0.3192,  1.0933, -0.0068, -1.0891, -1.4916,
  -0.7549,  0.3129,  1.1093,  1.5326,  0.0326, -0.7423,
  1.3703, -0.8649, -0.8637, -0.7697,  0.5525, -1.0616,
  -1.7115, -0.0301,  0.0774,  0.3714,  1.1006,  2.3505,
  -0.1022, -0.1649, -1.2141, -0.2256,  1.5442, -0.6156,
  -0.2414,  0.6277, -1.1135,  1.1174,  0.0859,  0.7481 ;

  H << 0.8147, 0.2785, 0.9572, 0.7922, 0.6787, 0.7060,
  0.9058, 0.5469, 0.4854, 0.9595, 0.7577, 0.0318,
  0.1270, 0.9575, 0.8003, 0.6557, 0.7431, 0.2769,
  0.9134, 0.9649, 0.1419, 0.0357, 0.3922, 0.0462,
  0.6324, 0.1576, 0.4218, 0.8491, 0.6555, 0.0971,
  0.0975, 0.9706, 0.9157, 0.9340, 0.1712, 0.8235;

  R << 0.3252,  0.3192,  1.0933, -0.0068, -1.0891, -1.4916,
  -0.7549,  0.3129,  1.1093,  1.5326,  0.0326, -0.7423,
  1.3703, -0.8649, -0.8637, -0.7697,  0.5525, -1.0616,
  -1.7115, -0.0301,  0.0774,  0.3714,  1.1006,  2.3505,
  -0.1022, -0.1649, -1.2141, -0.2256,  1.5442, -0.6156,
  -0.2414,  0.6277, -1.1135,  1.1174,  0.0859,  0.7481;


  // Kalman Gain Example; Matlab form:  K = Pp * H' * inv(H * Pp * H' + R)
  //-----------------------------------
  X  = H * Pp * H.transpose() + R;
  K  = Pp * H.transpose() * X.inverse();


  // Print Result
  //----------------------------
  print_mtxf(K);      // Print Matrix Result (passed by reference)


}

void loop()
{
 }


// PRINT MATRIX (float type)
// By: randomvibe
//-----------------------------
void print_mtxf(const Eigen::MatrixXf& X) 
{
   int i, j, nrow, ncol;
   
   nrow = X.rows();
   ncol = X.cols();

   Serial.print("nrow: "); Serial.println(nrow);
   Serial.print("ncol: "); Serial.println(ncol);     
   Serial.println();
   
   for (i=0; i<nrow; i++)
   {
       for (j=0; j<ncol; j++)
       {
           Serial.print(X(i,j), 6);   // print 6 decimal places
           Serial.print(", ");
       }
       Serial.println();
   }
   Serial.println();
}

josheeg

I found the quick reply does not let me add the zip file of my arduino due library to connect the Eigen library.

Also here is some test code after the arduino due eigen library is installed.

Code: [Select]
//Eigan library test by josheeg example by random vibe and eigen library team.
#include <Eigen.h>

// PRINT MATRIX (float type)
// By: randomvibe
//-----------------------------


void print_mtxf(const Eigen::MatrixXf& K);


void setup()
{
  Serial.begin(9600);

  // DECLARE MATRICES
  //--------------------
  MatrixXf Pp(6, 6);  // Produces 6x6 float matrix class
  MatrixXf H(6, 6);   // Note: without "using namespace Eigen", declaration would be: Eigen::MatrixXf H(6,6);
  MatrixXf R(6, 6);
  MatrixXf X(6, 6);
  MatrixXf K(6, 6);
  MatrixXf Z(6, 6);

  // INPUT MATRICES (so-called comma-initialize syntax)
  //---------------------------------------------------------
  Pp << 0.3252,  0.3192,  1.0933, -0.0068, -1.0891, -1.4916,
  -0.7549,  0.3129,  1.1093,  1.5326,  0.0326, -0.7423,
  1.3703, -0.8649, -0.8637, -0.7697,  0.5525, -1.0616,
  -1.7115, -0.0301,  0.0774,  0.3714,  1.1006,  2.3505,
  -0.1022, -0.1649, -1.2141, -0.2256,  1.5442, -0.6156,
  -0.2414,  0.6277, -1.1135,  1.1174,  0.0859,  0.7481 ;

  H << 0.8147, 0.2785, 0.9572, 0.7922, 0.6787, 0.7060,
  0.9058, 0.5469, 0.4854, 0.9595, 0.7577, 0.0318,
  0.1270, 0.9575, 0.8003, 0.6557, 0.7431, 0.2769,
  0.9134, 0.9649, 0.1419, 0.0357, 0.3922, 0.0462,
  0.6324, 0.1576, 0.4218, 0.8491, 0.6555, 0.0971,
  0.0975, 0.9706, 0.9157, 0.9340, 0.1712, 0.8235;

  R << 0.3252,  0.3192,  1.0933, -0.0068, -1.0891, -1.4916,
  -0.7549,  0.3129,  1.1093,  1.5326,  0.0326, -0.7423,
  1.3703, -0.8649, -0.8637, -0.7697,  0.5525, -1.0616,
  -1.7115, -0.0301,  0.0774,  0.3714,  1.1006,  2.3505,
  -0.1022, -0.1649, -1.2141, -0.2256,  1.5442, -0.6156,
  -0.2414,  0.6277, -1.1135,  1.1174,  0.0859,  0.7481;


  // Kalman Gain Example; Matlab form:  K = Pp * H' * inv(H * Pp * H' + R)
  //-----------------------------------
  X  = H * Pp * H.transpose() + R;
  K  = Pp * H.transpose() * X.inverse();


  // Print Result
  //----------------------------
  print_mtxf(K);      // Print Matrix Result (passed by reference)


}

void loop()
{
 }


// PRINT MATRIX (float type)
// By: randomvibe
//-----------------------------
void print_mtxf(const Eigen::MatrixXf& X) 
{
   int i, j, nrow, ncol;
   
   nrow = X.rows();
   ncol = X.cols();

   Serial.print("nrow: "); Serial.println(nrow);
   Serial.print("ncol: "); Serial.println(ncol);     
   Serial.println();
   
   for (i=0; i<nrow; i++)
   {
       for (j=0; j<ncol; j++)
       {
           Serial.print(X(i,j), 6);   // print 6 decimal places
           Serial.print(", ");
       }
       Serial.println();
   }
   Serial.println();
}

ArthurD

(perhaps cross posted, sorry)
I found this one, does this help?
https://github.com/vancegroup/EigenArduino

sharpx

#113
Nov 17, 2015, 12:39 am Last Edit: Nov 17, 2015, 12:50 am by sharpx
EDIT: Solved: Managed to find WInterrupts.c in C:\Users\<USERNAME>\AppData\Roaming\Arduino15\packages\arduino\hardware\sam\1.6.4\cores\arduino
not sure if this is common but that's where is located in my installation, I can't remember if I did something strange when installing or if I had to add DUE at a later time than when I initially installed it.

Hi all,

I have made a small improvement to WInterrupts.c which allows a faster interrupt dispatching associated to generic I/O pins. That is, the callbacks we set with function attachInterrupt().

Basically I have modified the code of the interrupt handler functions PIOA_Handler(), PIOB_Handler(), PIOC_Handler() and PIOD_handler() in the original WInterrupts.c. The improvement consists mainly in taking advantage of ATSAM3XE assembler instruction CLZ. With this instruction it is possible to calculate the number of leading zeros in a 32 bit value in just one machine instruction. CLZ is available in C/C++ using CMSIS function __CLZ().
[...]
@antodom & all

I'm drawing a blank here - I can't find the WInterrupts.c in my installation containing PIOA_Handler, PIOB_Handler, etc, just the one for AVR based boards. I searched the arduino installation folder and that's the only one I have. Any help locating it would be greatly appreciated, I have IDE v1.6.5
By the way, the __CLZ solution is brilliant, looks to be about 4 times faster than the for()  loop.
Thanks in advance

antodom

Hi there @sharpx,

WInterrupts.c for Atmel's Due uC ATSAM3xE should be included in "$HOME/.arduino15/packages/arduino/hardware/sam/1.6.4/cores/arduino" (I´m using Arduino IDE 1.6.5) in Ubuntu 14.04 (the one I usually use), and in Windows last time I saw was something like "C:\Users\<your-user-name>\AppData\Roaming\arduino15\packages\arduino\hardware\sam\1.6.4\cores\arduino".


If you can not find it there, maybe, have you changed your installation paths for the Arduino IDE?.

Best
------------
antodom

sharpx

@antodom
found it after I posted the message, crossed my mind to search the whole drive. There's probably a way to default installation to the same folder but since it works I'll leave it for now.
Thanks for the __CLZ trick, it's great - wish they added it to the dist since it's significantly faster especially if you're closer to the LSB, my 4+ times test was with bits 14 and 15.

Jagohu

Hi,

Does anyone know about a digitalIo library which works on the Due? This is the only one I'm missing at the moment, while I'm trying to migrate my gateway (NRF24L01, Ethernet shield) to Due from Mega...
Thank you!

antodom

@sharpx

I have already summited the WInterrupts.c improvement through a pull request for its inclusion in the Arduino standard library for the DUE. I hope it gets in soon.

Best.
------------
antodom

antodom

Hi all,

This post is just to announce that the software serial library for the DUE, soft_uart (already mentioned in a previous post) is now on GitHub, in this link:

 https://github.com/antodom/soft_uart

In addition, I would like to comment that the library has been improved with a new mode of operation in half-duplex, and a new bunch of examples illustrating its use. The library is completely compatible with the hardware serial objects Serial, Serial1, Serial2 and Serial3, and you can use the software serial objects provided by the library using the same serial API. Moreover, another improvement is that right now you can compile and upload the library and the examples on command line using CMake.

So far we have checked the library intensively with the hardware we have available, but any feedback or suggestion for improvement will be welcomed.
------------
antodom

ArthurD

is there an update to USB keyboard libs for the Due USB host port (HID, stdin), like on Raspberry Pi or on Windows PCs?

is there meanwhile a reasonable USB keyb lib available feat. standard C keyboard functions (stdin) like e.g.

kbhit()
getch()
getche()
getchar()
gets()
scanf()

?

currently the implementation of the USB.task is really weird, keyPressed and keyReleased don't return anything and it's totally osbscured how and when they do either or anything meaningful,
currently it's not even possible to indicate if a key was hit at all or not (like provided by kbhit() ),
usb.Task has to be called repeatedly but then blocks program execution intermediately and repeatedly for many seconds (which is unconscionable in the main loop() but shockingly also hinders different additional Scheduler multitasking threads ).

Go Up