Index: rtos_arduino/trunk/arduino_lib/libraries/Servo/src/Servo.h
===================================================================
--- rtos_arduino/trunk/arduino_lib/libraries/Servo/src/Servo.h	(revision 136)
+++ rtos_arduino/trunk/arduino_lib/libraries/Servo/src/Servo.h	(revision 224)
@@ -66,6 +66,8 @@
 #elif defined(ARDUINO_ARCH_SAMD)
 	//#include "samd/ServoTimersSamd.h"
-	#define MIN_PULSE_WIDTH_SAMD 500
-	#define MAX_PULSE_WIDTH_SAMD 2100
+	#define MIN_PULSE_WIDTH_SAMD_TCC 405
+	#define MAX_PULSE_WIDTH_SAMD_TCC 2300
+	#define MIN_PULSE_WIDTH_SAMD_TC 1700
+	#define MAX_PULSE_WIDTH_SAMD_TC 9200
 #else	
 	#error "This library only supports boards with an AVR,SAM or SAMD processor."
Index: rtos_arduino/trunk/arduino_lib/libraries/Servo/src/samd/Servo.cpp
===================================================================
--- rtos_arduino/trunk/arduino_lib/libraries/Servo/src/samd/Servo.cpp	(revision 136)
+++ rtos_arduino/trunk/arduino_lib/libraries/Servo/src/samd/Servo.cpp	(revision 224)
@@ -16,4 +16,5 @@
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */
+//Edited by Arduino Srl development team.
 
 #if defined(ARDUINO_ARCH_SAMD)
@@ -22,7 +23,10 @@
 #include <Servo.h>
 
+
 static servo_t servos[MAX_SERVOS];                          // static array of servo structures
 
 uint8_t ServoCount = 0;                                     // the total number of attached servos
+uint8_t isTC = 0 ;
+Tc* TCx ;
 Tcc* TCCx;
 uint8_t Channelx = 0;
@@ -30,6 +34,8 @@
 // convenience macros
 
-#define SERVO_MIN() (MIN_PULSE_WIDTH_SAMD)  // minimum value in uS for this servo
-#define SERVO_MAX() (MAX_PULSE_WIDTH_SAMD)  // maximum value in uS for this servo
+#define SERVO_MIN_TCC() (MIN_PULSE_WIDTH_SAMD_TCC)  // minimum value in uS for this servo if TCC timer is used
+#define SERVO_MAX_TCC() (MAX_PULSE_WIDTH_SAMD_TCC)  // maximum value in uS for this servo if TCC timer is used
+#define SERVO_MIN_TC() (MIN_PULSE_WIDTH_SAMD_TC)  // minimum value in uS for this servo if TC timer is used
+#define SERVO_MAX_TC() (MAX_PULSE_WIDTH_SAMD_TC)  // maximum value in uS for this servo if TC timer is used
 
 /************ static functions common to all instances ***********************/
@@ -46,6 +52,6 @@
   if (ServoCount < MAX_SERVOS) {
     this->servoIndex = ServoCount++;                    // assign a servo index to this instance
-  } else {                                                  //su questo costruttore forse si deve tornare
-    this->servoIndex = INVALID_SERVO;  // too many servos
+  } else {                                                 
+    this->servoIndex = INVALID_SERVO;  					// too many servos
   }
 }
@@ -53,5 +59,10 @@
 uint8_t Servo::attach(int pin)
 {
-  return this->attach(pin, MIN_PULSE_WIDTH_SAMD, MAX_PULSE_WIDTH_SAMD);
+  if((servos[this->servoIndex].Pin.nbr==4) | (servos[this->servoIndex].Pin.nbr==5) | (servos[this->servoIndex].Pin.nbr==10) | (servos[this->servoIndex].Pin.nbr==12) ){
+	return this->attach(pin, SERVO_MIN_TC(), SERVO_MAX_TC());
+  }
+  else{
+	return this->attach(pin, SERVO_MIN_TCC(), SERVO_MAX_TCC());
+  }
 }
 
@@ -63,6 +74,15 @@
     pinMode(pin, OUTPUT);                                   // set servo pin to output
     servos[this->servoIndex].Pin.nbr = pin;
-    if(min > MIN_PULSE_WIDTH_SAMD) min = MIN_PULSE_WIDTH_SAMD;
-	if (max > MAX_PULSE_WIDTH_SAMD) max = MAX_PULSE_WIDTH_SAMD;
+	int servo_min, servo_max;
+	if(pin==4 | pin==5 | pin==10 | pin==12){
+		servo_min=SERVO_MIN_TC();
+		servo_max=SERVO_MAX_TC();
+	}
+	else{
+		servo_min=SERVO_MIN_TCC();
+		servo_max=SERVO_MAX_TCC();
+	}
+    if(min > servo_min) min = servo_min;
+	if (max > servo_max) max = servo_max;
 	this->min  = min;
     this->max  = max;
@@ -75,4 +95,5 @@
 			TCCx=TCC0;
 			Channelx=0;
+			isTC=0;
 		}
 		break;
@@ -83,7 +104,27 @@
 			TCCx=TCC0;
 			Channelx=1;
-		}
-		break;
-		
+			isTC=0;
+		}
+		break;
+
+			case 4:
+		{
+			pinPeripheral(pin, g_APinDescription[pin].ulPinType);
+			TCx=TC3;
+			Channelx=0;
+			isTC=1;
+			 
+		}
+		break;
+
+		case 5:
+		{
+			pinPeripheral(pin, g_APinDescription[pin].ulPinType);
+			TCx=TC3;
+			Channelx=1;
+			isTC=1;
+			 
+		}
+		break;
 		case 6:
 		{
@@ -91,4 +132,5 @@
 			TCCx=TCC0;
 			Channelx=2;
+			isTC=0;
 		}
 		break;
@@ -99,4 +141,5 @@
 			TCCx=TCC0;
 			Channelx=3;
+			isTC=0;
 		}
 		break;
@@ -107,4 +150,5 @@
 			TCCx=TCC1;
 			Channelx=0;
+			isTC=0;
 		}
 		break;
@@ -115,4 +159,15 @@
 			TCCx=TCC1;
 			Channelx=1;
+			isTC=0;
+		}
+		break;
+		
+		case 10:
+		{
+			pinPeripheral(pin, g_APinDescription[pin].ulPinType);
+			TCx=TC3;
+			Channelx=0;
+			isTC=1;
+			 
 		}
 		break;
@@ -123,4 +178,15 @@
 			TCCx=TCC2;
 			Channelx=0;
+			isTC=0;
+		}
+		break;
+		
+		case 12:
+		{
+			pinPeripheral(pin, g_APinDescription[pin].ulPinType);
+			TCx=TC3;
+			Channelx=1;
+			isTC=1;
+			 
 		}
 		break;
@@ -131,4 +197,5 @@
 			TCCx=TCC2;
 			Channelx=1;
+			isTC=0;
 		}
 		break;
@@ -140,10 +207,31 @@
 	
 	if ((TCCx==TCC0) | (TCCx==TCC1)) GCLK->CLKCTRL.reg = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK3 | GCLK_CLKCTRL_ID( GCM_TCC0_TCC1 )) ;
-	else if(TCCx==TCC2) GCLK->CLKCTRL.reg = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK3 | GCLK_CLKCTRL_ID( GCM_TCC2_TC3 )) ;
+	else if((TCCx==TCC2) | (TCx==TC3 ))GCLK->CLKCTRL.reg = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK3 | GCLK_CLKCTRL_ID( GCM_TCC2_TC3 )) ;
 	else;
-		
-	if(servos[this->servoIndex].Pin.isActive == false)
-	{	
-		TCCx->CTRLA.reg &=~(TCC_CTRLA_ENABLE);        //disable TCC module
+
+	if(servos[this->servoIndex].Pin.isActive == false){
+	// Set PORT
+    if ( isTC )
+    {
+      // -- Configure TC
+      //DISABLE TCx
+      TCx->COUNT16.CTRLA.reg &=~(TC_CTRLA_ENABLE);
+      //Set Timer counter Mode to 16 bits
+	  TCx->COUNT16.CTRLA.reg |= TC_CTRLA_MODE_COUNT16;
+	  //Set Prescaler to divide by 2
+	  TCx->COUNT16.CTRLA.reg |= TC_CTRLA_PRESCALER_DIV2;
+      //Set TCx as normal PWM
+      TCx->COUNT16.CTRLA.reg |= TC_CTRLA_WAVEGEN_NPWM;
+	  //default value for servo position
+      TCx->COUNT16.CC[Channelx].reg = 1500;
+	  //ENABLE TCx
+      TCx->COUNT16.CTRLA.reg |= TC_CTRLA_ENABLE;
+	  servos[this->servoIndex].Pin.isActive = true;
+    }
+    else
+    {
+      // -- Configure TCC
+	  
+	 TCCx->CTRLA.reg &=~(TCC_CTRLA_ENABLE);        //disable TCC module
 		TCCx->CTRLA.reg |=TCC_CTRLA_PRESCALER_DIV8;   //setting prescaler to divide by 8
 		TCCx->WAVE.reg |= TCC_WAVE_WAVEGEN_NPWM;      //Set TCCx as normal PWM
@@ -151,8 +239,8 @@
 		TCCx->PER.reg=20000;                          // setting servo frequency (50 hz) 
 		TCCx->CTRLA.reg |= TCC_CTRLA_ENABLE ;	      //ENABLE TCCx
-		servos[this->servoIndex].Pin.isActive = true; 
-	}	
-    
-    //servos[this->servoIndex].Pin.isActive = true;  // this must be set after the check for isTimerActive
+		servos[this->servoIndex].Pin.isActive = true;  
+    }
+	
+	}
   }
   return this->servoIndex;
@@ -167,18 +255,39 @@
   else if((servos[this->servoIndex].Pin.nbr == 8) | (servos[this->servoIndex].Pin.nbr == 9)) TCC1->CTRLA.reg &=~(TCC_CTRLA_ENABLE);
   else if ((servos[this->servoIndex].Pin.nbr == 11) | (servos[this->servoIndex].Pin.nbr == 13)) TCC2->CTRLA.reg &=~(TCC_CTRLA_ENABLE);	
+  else if ((servos[this->servoIndex].Pin.nbr == 4 ) | (servos[this->servoIndex].Pin.nbr == 5 ) | (servos[this->servoIndex].Pin.nbr == 10 ) | (servos[this->servoIndex].Pin.nbr == 12 ))TC3->COUNT16.CTRLA.reg &=~(TC_CTRLA_ENABLE);
 }
 
 void Servo::write(int value)
-{
-  // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
-  if (value < MIN_PULSE_WIDTH)
-  {
-    if (value < 0)
-      value = 0;
-    else if (value > 180)
-      value = 180;
-
-    value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX());
-  }
+{  
+  //select the right values for servo motor
+  int servo_min;
+  int servo_max;
+  if((servos[this->servoIndex].Pin.nbr==4) | (servos[this->servoIndex].Pin.nbr==5) | (servos[this->servoIndex].Pin.nbr==10) | (servos[this->servoIndex].Pin.nbr==12) ){
+	servo_min=SERVO_MIN_TC();
+	servo_max=SERVO_MAX_TC();
+	// treat values less than 1700 as angles in degrees (valid values in microseconds are handled as microseconds)
+	if (value < servo_min)
+	{
+		if (value < 0)
+			value = 0;
+		else if (value > 180)
+			value = 180;
+		value = map(value, 0, 180, servo_min, servo_max);
+	}
+  }
+  else{
+	servo_min=SERVO_MIN_TCC();
+	servo_max=SERVO_MAX_TCC();
+	// treat values less than 400 as angles in degrees (valid values in microseconds are handled as microseconds)
+	if (value < servo_min)
+	{
+		if (value < 0)
+			value = 0;
+		else if (value > 180)
+			value = 180;
+		value = map(value, 0, 180, servo_min, servo_max);
+	}
+  }
+    
   writeMicroseconds(value);
 }
@@ -190,9 +299,20 @@
   if( (channel < MAX_SERVOS) )   // ensure channel is valid
   {
-    if (value < SERVO_MIN())          // ensure pulse width is valid
-      value = SERVO_MIN();
-    else if (value > SERVO_MAX())
-      value = SERVO_MAX();
-    servos[this->servoIndex].ticks = value;   //da sistemare
+	//select the right values for servo motor
+	int servo_min;
+	int servo_max;
+	if((servos[this->servoIndex].Pin.nbr==4) | (servos[this->servoIndex].Pin.nbr==5) | (servos[this->servoIndex].Pin.nbr==10) | (servos[this->servoIndex].Pin.nbr==12) ){
+		servo_min=SERVO_MIN_TC();
+		servo_max=SERVO_MAX_TC();
+	}
+	else{
+		servo_min=SERVO_MIN_TCC();
+		servo_max=SERVO_MAX_TCC();
+	}
+    if (value < servo_min)          // ensure pulse width is valid
+      value = servo_min;
+    else if (value > servo_max)
+      value = servo_max;
+    servos[this->servoIndex].ticks = value; 
 	switch(servos[this->servoIndex].Pin.nbr)
 	{
@@ -205,4 +325,12 @@
 			break;
 			
+		case 4:
+			TC3->COUNT16.CC[0].reg = value;
+			break;	
+			
+		case 5:
+			TC3->COUNT16.CC[1].reg = value;
+			break;	
+			
 		case 6:
 			TCC0->CC[2].reg=value;
@@ -220,4 +348,8 @@
 			TCC1->CC[1].reg=value;
 			break;
+		
+		case 10:
+			TC3->COUNT16.CC[0].reg = value;
+			break;		
 			
 		case 11:
@@ -225,4 +357,8 @@
 			break;
 
+		case 12:
+			TC3->COUNT16.CC[1].reg = value;
+			break;	
+
 		case 13:
 			TCC2->CC[1].reg=value;
@@ -234,5 +370,5 @@
 	}
     
-    //servos[this->servoIndex].ticks = value;   //da sistemare
+    //servos[this->servoIndex].ticks = value;   //to be fixed
 	//servos[channel].ticks = value;
   }
@@ -241,5 +377,16 @@
 int Servo::read() // return the value as degrees
 {
-  return map(readMicroseconds(), SERVO_MIN(), SERVO_MAX(), 0, 180);
+  //select the right values for servo motor
+  int servo_min;
+  int servo_max;
+  if((servos[this->servoIndex].Pin.nbr==4) | (servos[this->servoIndex].Pin.nbr==5) | (servos[this->servoIndex].Pin.nbr==10) | (servos[this->servoIndex].Pin.nbr==12) ){
+	servo_min=SERVO_MIN_TC();
+	servo_max=SERVO_MAX_TC();
+	}
+  else{
+	servo_min=SERVO_MIN_TCC();
+	servo_max=SERVO_MAX_TCC();
+	}
+  return map(readMicroseconds(), servo_min, servo_max, 0, 180);
 }
 
