/* Function name : stepper_move return : void parameters : long int steps :- variable to hold the value of the number of steps needed to be move, if +ve the motion is in a direction, if -ve the motion is in reverse unsigned long int time_bet_steps_stepper :- time between each step, this is the value that determines the speed of motion Method of operation : its a non blocking function, used to move the stepper motor when it has permission i.e.(stepper_permission == 1), it sets the values of the global variables direction and stepper_steps, and sends the parameters needed to the timer setup function */ void ext_stepper_3d::stepper_move (long int steps, unsigned long int time_bet_steps_stepper ) { if (permission == 1) //if you have permission, execute { permission = 0; //permission is used for one time only, this line expires it, to execute again a new permission must be granted status_var = MOVE; //setting status to currently moving if (steps > 0) { direction = clockwise; stepper_steps = steps; } else if (steps < 0) { direction = anticlockwise; stepper_steps = steps * (-1); } else if(steps == 0) { stepper_steps = 0; //for safety and anti user stupidity } timer5_setup ( timer5_value_lookup_table , time_bet_steps_stepper); //make the timer do its magic } }
static void handle_data(void){ #ifdef USE_STTERM unsigned char tmp_c; #endif uint32_t tmp=0; char crc=0; #ifdef USE_STTERM if(stlinky_rx_ready()){ scanf("%c", &tmp_c); INPUT[k] = tmp_c; gpio_toggle(GPIOD, GPIO15); #else if(usart_has_data()){ INPUT[k] = usart_read(); #endif k++; if(k==1 && INPUT[0]!=0x53){ // 'S' #ifdef DEBUG printf("ERROR AT START-BYTE %d\r\n",INPUT[0]); #endif k=0; } if(k==10 && INPUT[9]!=0x45){ // 'E' #ifdef DEBUG printf("ERROR AT END-BYTE %d\r\n",INPUT[8]); #endif k=0; } if(k==11){ #ifdef DEBUG printf("Package: "); for(tmp=0;tmp<10;tmp++) printf("%d ",INPUT[tmp]); printf("\n"); #endif for(tmp=0;tmp<8;tmp++) KEYS[tmp] = INPUT[tmp+1]; crc = getCRC(KEYS,8); if(crc==INPUT[10]){ #ifdef DEBUG printf("INPUT OK!\r\n"); #endif if(usb_ready == 3) usb_send_packet(KEYS, 8); }else{ #ifdef DEBUG printf("ERROR AT CRC-BYTE %d vs %d\r\n",INPUT[10],crc); #endif } k=0; } } } void tim3_isr(void){ if(timer_get_flag(TIM3, TIM_SR_UIF)){ handle_data(); timer_clear_flag(TIM3, TIM_SR_UIF); } } void tim4_isr(void){ if(timer_get_flag(TIM4, TIM_SR_UIF)){ if(usb_ready==3){ gpio_toggle(GPIOD,GPIO14); KEYS[0]=0; KEYS[1]=0; KEYS[3]=0; KEYS[4]=0; KEYS[5]=0; KEYS[6]=0; KEYS[7]=0; KEYS[2]=0x04; usb_send_packet(KEYS, 8); KEYS[2]=0x00; usb_send_packet(KEYS, 8); KEYS[2]=0x05; usb_send_packet(KEYS, 8); KEYS[2]=0x00; usb_send_packet(KEYS, 8); } timer_clear_flag(TIM4, TIM_SR_UIF); } } void tim5_isr(void){ if(timer_get_flag(TIM5, TIM_SR_UIF)){ gpio_toggle(GPIOD, GPIO15); timer_clear_flag(TIM5, TIM_SR_UIF); } } int main(void) { iwdg_reset(); iwdg_set_period_ms(5); iwdg_start(); rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]); rcc_periph_clock_enable(RCC_GPIOD); systick_setup(); gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12 | GPIO13 | GPIO14 | GPIO15); gpio_clear(GPIOD, GPIO12 | GPIO13 | GPIO14 | GPIO15); iwdg_set_period_ms(500); iwdg_reset(); msleep(400); iwdg_set_period_ms(5); iwdg_reset(); gpio_set(GPIOD, GPIO15); // msleep(100000); /* SLEEEEEEEEEEEEEEEEEEEEEEP */ #ifdef USE_STTERM stlinky_init(); #else usart_setup(); #endif timer2_setup(100); usb_setup(); //timer4_setup(1); timer3_setup(100); timer5_setup(2); // gpio_set(GPIOD, GPIO12 | GPIO13 | GPIO14 | GPIO15); while(1) { iwdg_reset(); } return 0; }