Prusa MINI Firmware overview
servo_private.h
Go to the documentation of this file.
1 /**
2  * Marlin 3D Printer Firmware
3  * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4  *
5  * Based on Sprinter and grbl.
6  * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 #pragma once
23 
24 /**
25  * servo_private.h - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2
26  * Copyright (c) 2009 Michael Margolis. All right reserved.
27  *
28  * This library is free software; you can redistribute it and/or
29  * modify it under the terms of the GNU Lesser General Public
30  * License as published by the Free Software Foundation; either
31  * version 2.1 of the License, or (at your option) any later version.
32  *
33  * This library is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36  * Lesser General Public License for more details.
37  *
38  * You should have received a copy of the GNU Lesser General Public
39  * License along with this library; if not, write to the Free Software
40  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
41  */
42 
43 #include <stdint.h>
44 
45 // Architecture specific include
46 #ifdef __AVR__
47  #include "../HAL_AVR/ServoTimers.h"
48 #elif defined(ARDUINO_ARCH_SAM)
49  #include "../HAL_DUE/ServoTimers.h"
50 #elif defined(__SAMD51__)
51  #include "../HAL_SAMD51/ServoTimers.h"
52 #else
53  #error "This library only supports boards with an AVR, SAM3X or SAMD51 processor."
54 #endif
55 
56 // Macros
57 
58 #define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo
59 #define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo
60 #define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached
61 #define REFRESH_INTERVAL 20000 // minimum time to refresh servos in microseconds
62 
63 #define SERVOS_PER_TIMER 12 // the maximum number of servos controlled by one timer
64 #define MAX_SERVOS (_Nbr_16timers * SERVOS_PER_TIMER)
65 
66 #define INVALID_SERVO 255 // flag indicating an invalid servo index
67 
68 // Convert microseconds to ticks and back (PRESCALER depends on architecture)
69 #define usToTicks(_us) (clockCyclesPerMicrosecond() * (_us) / (SERVO_TIMER_PRESCALER))
70 #define ticksToUs(_ticks) (unsigned(_ticks) * (SERVO_TIMER_PRESCALER) / clockCyclesPerMicrosecond())
71 
72 // convenience macros
73 #define SERVO_INDEX_TO_TIMER(_servo_nbr) ((timer16_Sequence_t)(_servo_nbr / (SERVOS_PER_TIMER))) // returns the timer controlling this servo
74 #define SERVO_INDEX_TO_CHANNEL(_servo_nbr) (_servo_nbr % (SERVOS_PER_TIMER)) // returns the index of the servo on this timer
75 #define SERVO_INDEX(_timer,_channel) ((_timer*(SERVOS_PER_TIMER)) + _channel) // macro to access servo index by timer and channel
76 #define SERVO(_timer,_channel) (servo_info[SERVO_INDEX(_timer,_channel)]) // macro to access servo class by timer and channel
77 
78 // Types
79 
80 typedef struct {
81  uint8_t nbr : 7 ; // a pin number from 0 to 127
82  uint8_t isActive : 1 ; // true if this channel is enabled, pin not pulsed if false
83 } ServoPin_t;
84 
85 typedef struct {
86  ServoPin_t Pin;
87  unsigned int ticks;
88 } ServoInfo_t;
89 
90 // Global variables
91 
92 extern uint8_t ServoCount;
94 
95 // Public functions
96 
97 extern void initISR(timer16_Sequence_t timer);
98 extern void finISR(timer16_Sequence_t timer);
ServoInfo_t
Definition: servo_private.h:72
ServoCount
uint8_t ServoCount
ServoPin_t
Definition: servo_private.h:67
timer16_Sequence_t
timer16_Sequence_t
Definition: ServoTimers.h:77
servo_info
ServoInfo_t servo_info[MAX_SERVOS]
uint8_t
const uint8_t[]
Definition: 404_html.c:3
MAX_SERVOS
#define MAX_SERVOS
Definition: servo_private.h:64
ServoInfo_t::ticks
unsigned int ticks
Definition: servo_private.h:87
initISR
void initISR(timer16_Sequence_t timer)
finISR
void finISR(timer16_Sequence_t timer)