@@ -242,9 +242,15 @@ bool power_adc_disable()
242
242
243
243
// Apollo3 is capapble of 14-bit ADC but Arduino defaults to 10-bit
244
244
// This modifies the global var that controls what is returned from analogRead()
245
- void analogReadResolution (uint8_t bits)
245
+ ap3_err_t analogReadResolution (uint8_t bits)
246
246
{
247
+ if (bits > 16 )
248
+ {
249
+ _analogBits = 16 ; // max out the resolution when this happens
250
+ return AP3_ERR;
251
+ }
247
252
_analogBits = bits;
253
+ return AP3_OK;
248
254
}
249
255
250
256
ap3_err_t ap3_adc_setup ()
@@ -652,15 +658,28 @@ ap3_err_t servoWriteResolution(uint8_t res)
652
658
return AP3_OK;
653
659
}
654
660
661
+ uint8_t getServoResolution ()
662
+ {
663
+ return (_servoWriteBits);
664
+ }
665
+
655
666
ap3_err_t servoWrite (uint8_t pin, uint32_t val)
667
+ {
668
+ return (servoWrite (pin, val, 544 , 2400 )); // Call servoWrite with Arduino default min/max microseconds. See: https://www.arduino.cc/en/Reference/ServoAttach
669
+ }
670
+
671
+ ap3_err_t servoWrite (uint8_t pin, uint32_t val, uint16_t minMicros, uint16_t maxMicros)
656
672
{
657
673
// Determine the high time based on input value and the current resolution setting
658
674
uint32_t fsv = (0x01 << _servoWriteBits); // full scale value for the current resolution setting
659
675
val = val % fsv; // prevent excess
660
676
uint32_t clk = AM_HAL_CTIMER_HFRC_3MHZ; // Using 3 MHz to get fine-grained control up to 20 ms wide
661
677
uint32_t fw = 60000 ; // 20 ms wide frame
662
- uint32_t max = 6000 ; // max width of RC pwm pulse is 2 ms or 6000 counts
663
- uint32_t min = 3000 ; // min width of RC pwm pulse is 1 ms or 3000 counts
678
+
679
+ // Convert microSeconds to PWM counts.
680
+ uint32_t min = minMicros * 3 ;
681
+ uint32_t max = maxMicros * 3 ;
682
+
664
683
uint32_t th = (uint32_t )(((max - min) * val) / fsv) + min;
665
684
666
685
return ap3_pwm_output (pin, th, fw, clk);
0 commit comments