mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-08-15 04:29:28 +03:00
throw out interrupts
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stm32g4.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "flash.h"
|
||||
@@ -26,10 +27,21 @@ static uint16_t servo_tagpos[SERVO_AMOUNT] = {SG90_MIDPULSE, SG90_MIDPULSE, SG90
|
||||
static uint16_t servo_speed[SERVO_AMOUNT] = {0, 0, 0, 0};
|
||||
|
||||
void process_servo(){
|
||||
static bool notinited = true;
|
||||
#if SERVO_AMOUNT != 4
|
||||
#error "Add timers' variables"
|
||||
#endif
|
||||
static const uint32_t trgflags[SERVO_AMOUNT] = {TIM_SR_CC1IF, TIM_SR_CC2IF, TIM_SR_CC3IF, TIM_SR_CC4IF};
|
||||
if(notinited){ // init tagpos with starting values from settings at first run
|
||||
for(int i = 0; i < SERVO_AMOUNT; ++i) servo_tagpos[i] = the_conf.startpulse[i];
|
||||
notinited = false;
|
||||
}
|
||||
uint32_t sr = TIM3->SR;
|
||||
uint32_t clear_mask = 0;
|
||||
for(uint8_t i = 0; i < SERVO_AMOUNT; ++i){
|
||||
uint16_t curpos;
|
||||
if(!get_servo(i, &curpos) || !tim_triggered[i]) continue;
|
||||
tim_triggered[i] = false;
|
||||
if(!get_servo(i, &curpos) || !(sr & trgflags[i])) continue;
|
||||
clear_mask |= trgflags[i];
|
||||
if(servo_tagpos[i] == curpos || servo_speed[i] < 1) continue;
|
||||
int32_t newpos = curpos;
|
||||
if(servo_tagpos[i] > curpos){
|
||||
@@ -41,12 +53,13 @@ void process_servo(){
|
||||
}
|
||||
set_servo(i, (uint16_t) newpos);
|
||||
}
|
||||
if(clear_mask) TIM3->SR &= ~clear_mask;
|
||||
}
|
||||
|
||||
// set speed in steps per tick
|
||||
bool servo_set_speed(uint8_t N, uint16_t s){
|
||||
if(N >= SERVO_AMOUNT) return false;
|
||||
if(s < 1 || s > the_conf.maxspeed[N]) return false;
|
||||
if(s > the_conf.maxspeed[N]) return false; // let s==0: this will allow to stop mowing to tagpos
|
||||
servo_speed[N] = s;
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user