/******************************************************************************* * Function Name: pwm_Start ******************************************************************************** * * Summary: * The start function initializes the pwm with the default values, the * enables the counter to begin counting. It does not enable interrupts, * the EnableInt command should be called if interrupt generation is required. * * Parameters: * None * * Return: * None * * Global variables: * pwm_initVar: Is modified when this function is called for the * first time. Is used to ensure that initialization happens only once. * *******************************************************************************/ void pwm_Start(void) { /* If not Initialized then initialize all required hardware and software */ if(pwm_initVar == 0u) { pwm_Init(); pwm_initVar = 1u; } pwm_Enable(); }
//****************************************************************************** // Configure Stepper Motor // // Configure the stepp er motor we will be using: // - Configure control pins: DIR, // - Configure SPI uses to talk to EasySpin chip // - Setup interrupt handlers to catch IRQ requests from EasySpin //****************************************************************************** int configure_stepper_motor (void) { int rc; TIM_HandleTypeDef *tim_handle; // CHEAP HACK - FIX TIM_MasterConfigTypeDef sMasterConfig; // TEMP HACK // --- begin --- The following is equivalent to Easyspin_Begin() code //--------------------------------------------------------------------- // Configure easySPIN - DIR pin for device 1 - D7 //--------------------------------------------------------------------- pin_Config (L6474_DIR_1_PIN, GPIO_OUTPUT, 0); pin_High (L6474_DIR_1_PIN); // set default direction = Forward // 1 = forward, 0 = Backward //--------------------------------------------------------------------- // Configure easySPIN - STBY/RESET pin - D8 //--------------------------------------------------------------------- pin_Config (L6474_RESET_PIN, GPIO_OUTPUT, 0); pin_Low (L6474_RESET_PIN); // turn on RESET to L6474 //-------------------------------------------------------------------------- // Config easySPIN - Interrupt IRQ Flag pin and associated EXTI in NVIC - D2 // // Note that this configures everything, but leaves the interrupt disabled. // This is done in order to complete any other needed config operations, // before turning on interrupts. // A subsequent call to pin_Enable_IRQ() will enable the NVIC interrupt. //-------------------------------------------------------------------------- pin_Config_IRQ_Pin (L6474_IRQ_PIN, GPIO_RUPT_MODE_FALLING, PIN_USE_PULLUP, L6474_EXTI_IRQ_NUM, 5); // BUG in above code not setting IRQn right ? /* Set Priority of External Line Interrupt used for the Flag interrupt*/ HAL_NVIC_SetPriority (L6474_EXTI_IRQ_NUM, 5, 0); // EXTI15_10_IRQn /* Enable the External Line Interrupt used for the Flag interrupt */ HAL_NVIC_EnableIRQ (L6474_EXTI_IRQ_NUM); //----------------------------------------------------------------------- // SPI Init // Config easySPIN SPI interface and CS pin. CS pin is D10 // Uses SPI 1 (PA5 / PA6 / PA7) on most Nucleos. //----------------------------------------------------------------------- // easyspin uses: SPI_BAUDRATEPRESCALER_32 - FIX THIS pin_Config (L6474_CS_PIN, GPIO_OUTPUT, 0); // configure Chip Select pin_High (L6474_CS_PIN); // and DE-Assert CS rc = spi_Init (L6474_SPI_MODULE, SPI_MASTER, L6474_SPI_MODE, L6474_SPI_BAUD, 0); //----------------------------------------------------------------- // PWM Init // Configure the PWM module to be used to drive the Stepper motor // // Uses PC7 (Arduino D9) = TIM3_CH2 // PC7 has Timer3 Ch2 for _all_ chips _except_ F3-02 and L0-53 //----------------------------------------------------------------- // SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file. // easy_spin uses Init.ClockDivision = TIM_CLOCKDIVISION_DIV1 !!! ??? FIX THIS !!! ??? WVD /// uwPeriod = (SystemCoreClock / 20000 ) - 1; // set period = 20 K Hz - WVD LOGIC uwPeriod = 0; // at startup, period is set to 0 if (rc == HAL_OK) rc = pwm_Init (L6474_PWM_1_MODULE, uwPeriod, 0); // actual rate on scope = 40 KHz ! timer_Set_Prescalar(L6474_PWM_1_MODULE, (TIMER_PRESCALER - 1), 0); // ensure pwm_Init turned on clocks //pHTim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; //------------------------------------- // Configure PWM channel to be used //------------------------------------- if (rc == HAL_OK) rc = pwm_Config_Channel (L6474_PWM_1_MODULE, L6474_PWM_1_CHANNEL, 0, TIMER_PIN_POLARITY_HIGH); // 0 % duty cycle // sConfigOC.OCMode = TIM_OCMODE_PWM1; // sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; // sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; // ??? !!! WVD ??? !!! RESOLVE THIS tim_handle = (TIM_HandleTypeDef*) board_timerpwm_get_handle (L6474_PWM_1_MODULE); // !!! CHEAP HACK - WVD FIX THIS !!! ??? sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; HAL_TIMEx_MasterConfigSynchronization (tim_handle, &sMasterConfig); //--------------------------------------------------------- // Setup the callback handler for the PWM/Timer Interrupt //--------------------------------------------------------- tim_handle = (TIM_HandleTypeDef*) board_timerpwm_get_handle (L6474_PWM_1_MODULE); // !!! CHEAP HACK - WVD FIX THIS !!! ??? timer_Set_Callback (L6474_PWM_1_MODULE, timer_period_completed_callback, tim_handle); // !!! CHEAP HACK - WVD FIX THIS !!! ??? //------------------------------------------------------------------- // Enable the PWM module and its associated channels, and // have it issue a PWM/Timer Interrupt at end of each period rollover. //-------------------------------------------------------------------- if (rc == HAL_OK) rc = pwm_Enable (L6474_PWM_1_MODULE, TIMER_PERIOD_INTERRUPT_ENABLED); // rc = HAL_TIM_PWM_Start_IT (&hTimPwm1, easySPIN_CHAN_TIMER_PWM1); --> see easyspin.c EasySpin_Pwm1SetFreq() //--------------------------------------------------- // Enable the NVIC for EXTI interrupts from the L6474 //--------------------------------------------------- pin_Enable_IRQ (L6474_IRQ_PIN, L6474_EXTI_IRQ_NUM, 1); //------------------------------------------------------------- // Take L6474 out of reset, so it can start up //------------------------------------------------------------- pin_High (L6474_RESET_PIN); //--------------------------------------------------------------- // Attach the function MyFlagInterruptHandler (defined below) to // EasySpin's IRQ flag interrupt //--------------------------------------------------------------- EasySpin_AttachFlagInterrupt (MyFlagInterruptHandler); //--------------------------------------------------------------- // Attach the function Error_Handler (defined below) to // the error Handler for EasySpin //--------------------------------------------------------------- EasySpin_AttachErrorHandler (Error_Handler); //------------------------------------------------------------- // Take L6474 out of reset, so it can start up //------------------------------------------------------------- pin_High (L6474_RESET_PIN); /* Let a delay after reset */ HAL_Delay(1); //-------------------------------------------------------------------- // Set all registers and context variables to the predefined values // from the easyspin_target_config.h file. // The easySPin registers are set with the predefined values from // file ~\motion_cube\Libraries\Easyspin\Inc\easyspin_target_config.h //-------------------------------------------------------------------- //while (debug_test) // WVD loop for SPI debug testing EasySpin_SetDeviceParamsToPredefinedValues(); /* Disable easySPIN powerstage */ for (uint32_t i = 0; i < 1; i++) { EasySpin_CmdDisable (i); /* Get Status, which will clear flags after start up */ EasySpin_CmdGetStatus (i); } // --- end --- The above is equivalent to Easyspin_Begin() code return (ret_code); }