|
| 1 | +#include "step_lowlevel.h" |
| 2 | + |
| 3 | +enum step_state_e { |
| 4 | + SPEED_UP = 0, |
| 5 | + SPEED_CONSTANT, |
| 6 | + SPEED_DOWN, |
| 7 | +}; |
| 8 | + |
| 9 | +enum step_mode_e { |
| 10 | + POSITION_MODE = 0, |
| 11 | + SPEED_MODE, |
| 12 | +}; |
| 13 | + |
| 14 | +struct step_param_t { |
| 15 | + enum step_state_e state; |
| 16 | + enum step_mode_e mode; |
| 17 | + volatile long remain_counts; |
| 18 | + volatile long total_conuts; |
| 19 | + volatile long constant_conunts; |
| 20 | + volatile long timer_load_val; |
| 21 | + volatile long timer_min_val; |
| 22 | + volatile long n; |
| 23 | + bool is_move_done; |
| 24 | + |
| 25 | + void(*positon_complete_callback)(void); |
| 26 | + void(*speed_complete_callback)(void); |
| 27 | +}step_param = {0}; |
| 28 | + |
| 29 | + |
| 30 | +bool step_init_ll(void){ |
| 31 | + DIR_OUTPUT; |
| 32 | + STEP_OUTPUT; |
| 33 | + |
| 34 | + DIR_LOW; |
| 35 | + STEP_LOW; |
| 36 | + |
| 37 | + memset( &step_param, 0x00, sizeof(struct step_param_t) ); |
| 38 | + |
| 39 | + noInterrupts(); |
| 40 | + TCCR4A = 0; // <! clear register value |
| 41 | + TCCR4B = 0; // <! clear register value |
| 42 | + TCNT4 = 0; |
| 43 | + OCR4A = 1600; |
| 44 | + TCCR4B |= (1 << WGM42); |
| 45 | + TCCR4B |= ((1 << CS41) | (1 << CS40)); |
| 46 | + |
| 47 | + interrupts(); |
| 48 | + |
| 49 | + return true; |
| 50 | +} |
| 51 | + |
| 52 | +bool set_steps(long steps, long timer_min_val, void(*complete_callback)(void), bool wait){ |
| 53 | + if( step_param.n > 1 ){ // <! already work |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + memset( &step_param, 0x00, sizeof(struct step_param_t) ); |
| 58 | + |
| 59 | + step_param.mode = POSITION_MODE; |
| 60 | + step_param.total_conuts = step_param.remain_counts = abs(steps); |
| 61 | + step_param.timer_min_val = timer_min_val; |
| 62 | + |
| 63 | + step_param.timer_load_val = 1600; |
| 64 | + |
| 65 | + step_param.is_move_done = false; |
| 66 | + step_param.positon_complete_callback = complete_callback; |
| 67 | + |
| 68 | + if(steps > 0){ // <! changed the direction |
| 69 | + DIR_LOW; |
| 70 | + }else{ |
| 71 | + DIR_HIGH; |
| 72 | + } |
| 73 | + |
| 74 | + step_param.n = 0; |
| 75 | + OCR4A = step_param.timer_load_val; |
| 76 | + TIMSK4 |= (1 << OCIE4A); // <! enbale timer4 interrupt |
| 77 | + |
| 78 | + if( wait ){ |
| 79 | + while( !step_param.is_move_done ){ |
| 80 | + delay_us(100); |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + return true; |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +bool set_speed(int8_t direction, long timer_min_val, void(*complete_callback)(void), bool wait){ |
| 89 | + if( timer_min_val == -1 ){ |
| 90 | + if( step_param.state == SPEED_DOWN ){ // <! already work |
| 91 | + return false; |
| 92 | + } |
| 93 | + step_param.state = SPEED_DOWN; |
| 94 | + if( wait ){ |
| 95 | + while( step_param.n > 1 ){ delay_us(100); } |
| 96 | + } |
| 97 | + }else{ |
| 98 | + if( step_param.n > 1 ){ // <! already work |
| 99 | + return false; |
| 100 | + } |
| 101 | + memset( &step_param, 0x00, sizeof(struct step_param_t) ); |
| 102 | + step_param.mode = SPEED_MODE; |
| 103 | + step_param.speed_complete_callback = complete_callback; |
| 104 | + if(direction > 0){ // <! changed the direction |
| 105 | + DIR_HIGH; |
| 106 | + }else{ |
| 107 | + DIR_LOW; |
| 108 | + } |
| 109 | + |
| 110 | + step_param.state = SPEED_UP; |
| 111 | + step_param.timer_min_val = timer_min_val; |
| 112 | + step_param.timer_load_val = 1600; |
| 113 | + step_param.n = 0; |
| 114 | + OCR4A = step_param.timer_load_val; |
| 115 | + TIMSK4 |= (1 << OCIE4A); // <! enbale timer4 interrupt |
| 116 | + if( wait ){ |
| 117 | + while( step_param.state != SPEED_CONSTANT ){ delay_us(100); } |
| 118 | + } |
| 119 | + } |
| 120 | + return true; |
| 121 | +} |
| 122 | + |
| 123 | +void position_interrrupt_handle(void){ |
| 124 | + if( step_param.remain_counts-- ){ |
| 125 | + STEP_HIGH; |
| 126 | + STEP_LOW; |
| 127 | + }else{ |
| 128 | + if( step_param.positon_complete_callback != NULL ){ |
| 129 | + step_param.positon_complete_callback(); |
| 130 | + } |
| 131 | + TIMSK4 &= ~(1 << OCIE4A); // <! disable timer4 interrupt |
| 132 | + step_param.is_move_done = true; |
| 133 | + } |
| 134 | + |
| 135 | + switch( step_param.state ){ |
| 136 | + case SPEED_UP: |
| 137 | + if( step_param.remain_counts>=step_param.total_conuts/2 ){ |
| 138 | + step_param.n++; |
| 139 | + step_param.timer_load_val = step_param.timer_load_val - (2 * step_param.timer_load_val) / (4 * step_param.n + 1 ); // <! Tarlor series |
| 140 | + |
| 141 | + if( step_param.timer_load_val < step_param.timer_min_val ){ |
| 142 | + step_param.constant_conunts = 2 * step_param.remain_counts - step_param.total_conuts; |
| 143 | + step_param.timer_load_val = step_param.timer_min_val; |
| 144 | + step_param.state = SPEED_CONSTANT; |
| 145 | + } |
| 146 | + }else{ |
| 147 | + step_param.state = SPEED_DOWN; // <! remain_counts reach half total counts, it mean starting to speed down |
| 148 | + } |
| 149 | + break; |
| 150 | + case SPEED_CONSTANT: |
| 151 | + if( step_param.constant_conunts-- <= 0 ){ |
| 152 | + step_param.state = SPEED_DOWN; |
| 153 | + } |
| 154 | + break; |
| 155 | + case SPEED_DOWN: |
| 156 | + if( step_param.remain_counts ){ |
| 157 | + step_param.n--; |
| 158 | + step_param.timer_load_val = (step_param.timer_load_val * (4 * step_param.n + 1)) / (4 * step_param.n + 1 - 2); // <! Tarlor series |
| 159 | + } |
| 160 | + break; |
| 161 | + } |
| 162 | + OCR4A = step_param.timer_load_val; |
| 163 | +} |
| 164 | + |
| 165 | +void speed_interrupt_handle(void){ |
| 166 | + STEP_HIGH; |
| 167 | + STEP_LOW; |
| 168 | + |
| 169 | + switch( step_param.state ){ |
| 170 | + case SPEED_UP: |
| 171 | + if( step_param.timer_load_val > step_param.timer_min_val ){ |
| 172 | + step_param.n++; |
| 173 | + step_param.timer_load_val = step_param.timer_load_val - (2 * step_param.timer_load_val) / (4 * step_param.n + 1 ); // <! Tarlor series |
| 174 | + }else{ |
| 175 | + if( step_param.speed_complete_callback != NULL ){ |
| 176 | + step_param.speed_complete_callback(); |
| 177 | + step_param.speed_complete_callback = NULL; |
| 178 | + } |
| 179 | + step_param.state = SPEED_CONSTANT; |
| 180 | + } |
| 181 | + break; |
| 182 | + case SPEED_CONSTANT: |
| 183 | + |
| 184 | + break; |
| 185 | + case SPEED_DOWN: |
| 186 | + if( step_param.n-- ){ |
| 187 | + step_param.timer_load_val = (step_param.timer_load_val * (4 * step_param.n + 1)) / (4 * step_param.n + 1 - 2); // <! Tarlor series |
| 188 | + }else{ |
| 189 | + TIMSK4 &= ~(1 << OCIE4A); // <! disable timer4 interrupt |
| 190 | + if( step_param.speed_complete_callback != NULL ){ |
| 191 | + step_param.speed_complete_callback(); |
| 192 | + step_param.speed_complete_callback = NULL; |
| 193 | + } |
| 194 | + } |
| 195 | + break; |
| 196 | + } |
| 197 | + |
| 198 | + OCR4A = step_param.timer_load_val; |
| 199 | +} |
| 200 | + |
| 201 | +bool step_terminate(void){ |
| 202 | + memset( &step_param, 0x00, sizeof(struct step_param_t) ); |
| 203 | + noInterrupts(); |
| 204 | + TCCR4A = 0; // <! clear register value |
| 205 | + TCCR4B = 0; // <! clear register value |
| 206 | + TCNT4 = 0; |
| 207 | + OCR4A = 1600; |
| 208 | + TCCR4B |= (1 << WGM42); |
| 209 | + TCCR4B |= ((1 << CS41) | (1 << CS40)); |
| 210 | + |
| 211 | + interrupts(); |
| 212 | + TIMSK4 &= ~(1 << OCIE4A); // <! disable timer4 interrupt |
| 213 | + return true; |
| 214 | +} |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | +ISR(TIMER4_COMPA_vect){ |
| 219 | + switch( step_param.mode ){ |
| 220 | + case POSITION_MODE: position_interrrupt_handle(); break; |
| 221 | + case SPEED_MODE: speed_interrupt_handle(); break; |
| 222 | + } |
| 223 | +} |
0 commit comments