Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of X-NUCLEO-IHM05A1 by
Diff: Components/l6208/l6208_class.h
- Revision:
- 1:5cc2691ccfff
- Parent:
- 0:52a66fac0f64
- Child:
- 2:2af31245e67e
--- a/Components/l6208/l6208_class.h Fri Apr 08 15:30:36 2016 +0000 +++ b/Components/l6208/l6208_class.h Wed Apr 27 16:30:55 2016 +0000 @@ -0,0 +1,1194 @@ +/** + ****************************************************************************** + * @file l6208_class.h + * @author IPC Rennes + * @version V1.0.0 + * @date March 18th, 2016 + * @brief This file contains the class of a L6208 Motor Control component. + * @note (C) COPYRIGHT 2016 STMicroelectronics + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + + +/* Define to prevent recursive inclusion -------------------------------------*/ + +#ifndef __L6208_CLASS_H +#define __L6208_CLASS_H + + +/* Includes ------------------------------------------------------------------*/ + +/* ACTION 1 ------------------------------------------------------------------* + * Include here platform specific header files. * + *----------------------------------------------------------------------------*/ +#include "mbed.h" + +/* ACTION 2 ------------------------------------------------------------------* + * Include here component specific header files. * + *----------------------------------------------------------------------------*/ +#include "l6208.h" +/* ACTION 3 ------------------------------------------------------------------* + * Include here interface specific header files. * + * * + * Example: * + * #include "../Interfaces/Humidity_class.h" * + * #include "../Interfaces/Temperature_class.h" * + *----------------------------------------------------------------------------*/ +#include "../Interfaces/StepperMotor_class.h" + + +/* Classes -------------------------------------------------------------------*/ + +/** + * @brief Class representing a L6208 component. + */ +class L6208 : public StepperMotor +{ +public: + /*** Constructor and Destructor Methods ***/ + + /** + * @brief Constructor. + * @param flag_and_enable_pin pin name of the EN pin of the component. + * @param reset_pin pin name of the RESET pin of the component. + * @param direction_pin pin name of the CW_CCW pin of the component. + * @param half_full_pin pin name of the HALF_FULL pin of the component. + * @param control_pin pin name of the CONTROL pin of the component. + * @param clock_pin pin name of the CLOCK pin of the component. + * @param vrefA_pwm_pin pin name of the PWM connected to the VREFA pin of the component. + * @param vrefB_pwm_pin pin name of the PWM connected to the VREFB pin of the component. + */ + L6208(PinName flag_and_enable_pin, PinName reset_pin, PinName direction_pin, PinName half_full_pin, PinName control_pin, PinName clock_pin, PinName vrefA_pwm_pin, PinName vrefB_pwm_pin) : StepperMotor(), + flag_and_enable(flag_and_enable_pin), + reset(reset_pin), + direction(direction_pin), + half_full(half_full_pin), + control(control_pin), + clock(clock_pin), + vrefA_pwm(vrefA_pwm_pin), + vrefB_pwm(vrefB_pwm_pin) + { + /* Checking stackability. */ + if (!(numberOfDevices < MAX_NUMBER_OF_DEVICES)) + error("Instantiation of the L6208 component failed: it can be stacked up to %d times.\r\n", MAX_NUMBER_OF_DEVICES); + + /* ACTION 4 ----------------------------------------------------------* + * Initialize here the component's member variables, one variable per * + * line. * + * * + * Example: * + * measure = 0; * + * instance_id = number_of_instances++; * + *--------------------------------------------------------------------*/ + errorHandlerCallback = 0; + deviceInstance = numberOfDevices++; + /* default tick frequency */ + tickFreq = TIMER_TICK_FREQUENCY; + /* waveform microstepping PWM period sample array, 90 deg shifted */ + pMicroTable2 = &(microTable1[16]); + } + + /** + * @brief Destructor. + */ + virtual ~L6208(void) {} + + + /*** Public Component Related Methods ***/ + + /* ACTION 5 --------------------------------------------------------------* + * Implement here the component's public methods, as wrappers of the C * + * component's functions. * + * They should be: * + * + Methods with the same name of the C component's virtual table's * + * functions (1); * + * + Methods with the same name of the C component's extended virtual * + * table's functions, if any (2). * + * * + * Example: * + * virtual int GetValue(float *pData) //(1) * + * { * + * return COMPONENT_GetValue(float *pfData); * + * } * + * * + * virtual int EnableFeature(void) //(2) * + * { * + * return COMPONENT_EnableFeature(); * + * } * + *------------------------------------------------------------------------*/ + + /** + * @brief Public functions inherited from the Component Class + */ + + /** + * @brief Initialize the component. + * @param init Pointer to device specific initalization structure. + * @retval "0" in case of success, an error code otherwise. + */ + virtual int Init(void *init = NULL) + { + return (int) L6208_Init((void *) init); + } + + /** + * @brief Getting the ID of the component. + * @param id Pointer to an allocated variable to store the ID into. + * @retval "0" in case of success, an error code otherwise. + */ + virtual int ReadID(uint8_t *id = NULL) + { + return (int) L6208_ReadID((uint8_t *) id); + } + + /** + * @brief Public functions inherited from the StepperMotor Class + */ + + /** + * @brief Getting the value of the motor state . + * @param None. + * @retval The motor state accoring to motorState_t in motor.h + */ + virtual unsigned int GetStatus(void) + { + return (unsigned int) L6208_GetMotionState(); + } + + /** + * @brief Getting the position. + * @param None. + * @retval The position. + */ + virtual signed int GetPosition(void) + { + return (signed int)L6208_GetPosition(); + } + + /** + * @brief Getting the marked position. + * @param None. + * @retval The marked position. + */ + virtual signed int GetMark(void) + { + return (signed int)L6208_GetMark(); + } + + /** + * @brief Getting the current speed in pps. + * @param None. + * @retval The current speed in pps. + */ + virtual unsigned int GetSpeed(void) + { + return (unsigned int)L6208_GetCurrentSpeed(); + } + + /** + * @brief Getting the maximum speed in pps. + * @param None. + * @retval The maximum speed in pps. + */ + virtual unsigned int GetMaxSpeed(void) + { + return (unsigned int)L6208_GetMaxSpeed(); + } + + /** + * @brief Getting the minimum speed in pps. + * @param None. + * @retval The minimum speed in pps. + */ + virtual unsigned int GetMinSpeed(void) + { + return (unsigned int)L6208_GetMinSpeed(); + } + + /** + * @brief Getting the acceleration in pps^2. + * @param None. + * @retval The acceleration in pps^2. + */ + virtual unsigned int GetAcceleration(void) + { + return (unsigned int)L6208_GetAcceleration(); + } + + /** + * @brief Getting the deceleration in pps^2. + * @param None. + * @retval The deceleration in pps^2. + */ + virtual unsigned int GetDeceleration(void) + { + return (unsigned int)L6208_GetDeceleration(); + } + + /** + * @brief Getting the direction of rotation. + * @param None. + * @retval The direction of rotation. + */ + virtual direction_t GetDirection(void) + { + if (L6208_GetDirection()!=BACKWARD) + { + return FWD; + } + else + { + return BWD; + } + } + + /** + * @brief Setting the current position to be the home position. + * @param None. + * @retval None. + */ + virtual void SetHome(void) + { + L6208_SetHome(); + } + + /** + * @brief Setting the current position to be the marked position. + * @param None. + * @retval None. + */ + virtual void SetMark(void) + { + L6208_SetMark(); + } + + /** + * @brief Setting the maximum speed in pps. + * @param speed The maximum speed in pps. + * @retval "true" in case of success, "false" otherwise. + */ + virtual bool SetMaxSpeed(unsigned int speed) + { + if (speed <= 0xFFFF) + { + return L6208_SetMaxSpeed((uint16_t) speed); + } + else + { + return false; + } + } + + /** + * @brief Setting the minimum speed in pps. + * @param speed The minimum speed in pps. + * @retval "true" in case of success, "false" otherwise. + */ + virtual bool SetMinSpeed(unsigned int speed) + { + if (speed <= 0xFFFF) + { + return L6208_SetMinSpeed((uint16_t) speed); + } + else + { + return false; + } + } + + /** + * @brief Setting the acceleration in pps^2. + * @param acceleration The acceleration in pps/s^2. + * @retval "true" in case of success, "false" otherwise. + */ + virtual bool SetAcceleration(unsigned int acceleration) + { + if (acceleration <= 0xFFFF) + { + return L6208_SetAcceleration((uint16_t) acceleration); + } + else + { + return false; + } + } + + /** + * @brief Setting the deceleration in pps^2. + * @param deceleration The deceleration in pps^2. + * @retval "true" in case of success, "false" otherwise. + */ + virtual bool SetDeceleration(unsigned int deceleration) + { + if (deceleration <= 0xFFFF) + { + return L6208_SetDeceleration((uint16_t) deceleration); + } + else + { + return false; + } + } + + /** + * @brief Setting the Step Mode. + * @param step_mode The Step Mode. + * @retval "true" in case of success, "false" otherwise. + * @note step_mode can be one of the following: + * + STEP_MODE_FULL + * + STEP_MODE_WAVE + * + STEP_MODE_HALF + * + STEP_MODE_1_4 + * + STEP_MODE_1_8 + * + STEP_MODE_1_16 + */ + virtual bool SetStepMode(step_mode_t step_mode) + { + return L6208_SetStepMode((motorStepMode_t) step_mode); + } + + /** + * @brief Going to a specified position. + * @param position The desired position. + * @retval None. + */ + virtual void GoTo(signed int position) + { + L6208_GoTo((int32_t)position); + } + + /** + * @brief Going to the home position. + * @param None. + * @retval None. + */ + virtual void GoHome(void) + { + L6208_GoHome(); + } + + /** + * @brief Going to the marked position. + * @param None. + * @retval None. + */ + virtual void GoMark(void) + { + L6208_GoMark(); + } + + /** + * @brief Running the motor towards a specified direction. + * @param direction The direction of rotation. + * @retval None. + */ + virtual void Run(direction_t direction) + { + L6208_Run((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD)); + } + + /** + * @brief Moving the motor towards a specified direction for a certain number of steps. + * @param direction The direction of rotation. + * @param steps The desired number of steps. + * @retval None. + */ + virtual void Move(direction_t direction, unsigned int steps) + { + L6208_Move((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD), (uint32_t)steps); + } + + /** + * @brief Stopping the motor through an immediate deceleration up to zero speed. + * @param None. + * @retval None. + */ + virtual void SoftStop(void) + { + L6208_SoftStop(); + } + + /** + * @brief Stopping the motor through an immediate infinite deceleration. + * @param None. + * @retval None. + */ + virtual void HardStop(void) + { + L6208_HardStop(); + } + + /** + * @brief Disabling the power bridge after performing a deceleration to zero. + * @param None. + * @retval None. + */ + virtual void SoftHiZ(void) + { + motorStopMode_t stopMode = L6208_GetStopMode(); + if (stopMode==HIZ_MODE) + { + L6208_SoftStop(); + } + else + { + L6208_SetStopMode(HIZ_MODE); + L6208_SoftStop(); + L6208_SetStopMode(stopMode); + } + } + + /** + * @brief Disabling the power bridge immediately. + * @param None. + * @retval None. + */ + virtual void HardHiZ(void) + { + L6208_HardHiZ(); + } + + /** + * @brief Waiting while the motor is active. + * @param None. + * @retval None. + */ + virtual void WaitWhileActive(void) + { + L6208_WaitWhileActive(); + } + + /** + * @brief Public functions NOT inherited + */ + + /** + * @brief Attaching an error handler. + * @param fptr An error handler. + * @retval None. + */ + virtual void AttachErrorHandler(void (*fptr)(uint16_t error)) + { + L6208_AttachErrorHandler((void (*)(uint16_t error)) fptr); + } + + /** + * @brief Checks if the device is disabled or/and has an alarm flag set + * by reading the EN pin position. + * @param None. + * @retval One if the EN pin is low (the device is disabled or/and + * has an alarm flag set), otherwise zero. + */ + virtual unsigned int CheckStatusHw(void) + { + if (!flag_and_enable.read()) return 0x01; + else return 0x00; + } + + /** + * @brief Disabling the device. + * @param None. + * @retval None. + */ + virtual void Disable(void) + { + L6208_Disable(); + } + + /** + * @brief Enabling the device. + * @param None. + * @retval None. + */ + virtual void Enable(void) + { + L6208_Enable(); + } + + /** + * @brief Getting the motor decay mode. + * @param None. + * @retval The motor decay mode. + */ + virtual motorDecayMode_t GetDecayMode() + { + return L6208_GetDecayMode(); + } + + /** + * @brief Set the frequency of the VREFA and VREFB PWM + * @param frequency in Hz + * @retval None. + */ + virtual uint32_t GetFreqVrefPwm(void) + { + return L6208_VrefPwmGetFreq(); + } + + /** + * @brief Getting the version of the firmware. + * @param None. + * @retval The version of the firmware. + */ + virtual unsigned int GetFwVersion(void) + { + return (unsigned int) L6208_GetFwVersion(); + } + + /** + * @brief Getting the motor step mode. + * @param None. + * @retval The motor step mode. + */ + virtual step_mode_t GetStepMode(void) + { + return (step_mode_t) L6208_GetStepMode(); + } + + /** + * @brief Getting the motor stop mode. + * @param None. + * @retval The motor stop mode. + */ + virtual motorStopMode_t GetStopMode(void) + { + return L6208_GetStopMode(); + } + + /** + * @brief Going to a specified position with a specificied direction. + * @param direction The desired direction. + * @param position The desired position. + * @retval None. + */ + virtual void GoTo(direction_t direction, signed int position) + { + L6208_GoToDir((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD),(int32_t)position); + } + + /** + * @brief Release the L6208 reset (Reset pin set to high level). + * @param None. + * @retval None. + */ + virtual void ReleaseReset(void) + { + L6208_ReleaseReset(); + } + + /** + * @brief Reset the device with current step mode, resets current speed, + * positions and microstep variables. + * @param None. + * @retval None. + */ + virtual void Reset(void) + { + L6208_Reset(); + } + + /** + * @brief Reset the L6208 (Reset pin set to low level). + * @param None. + * @retval None. + */ + virtual void ResetDevice(void) + { + L6208_ResetDevice(); + } + + /** + * @brief Set the motor decay mode. + * @param decayMode The desired decay mode (SLOW_DECAY or FAST_DECAY). + * @retval None. + */ + virtual void SetDecayMode(motorDecayMode_t decayMode) + { + L6208_SetDecayMode(decayMode); + } + + /** + * @brief Set the motor direction. + * @param direction The desired direction. + * @retval None. + */ + virtual void SetDirection(direction_t direction) + { + L6208_SetDirection((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD)); + } + + /** + * @brief Set the frequency of the VREFA and VREFB PWM + * @param frequency in Hz + * @retval None. + */ + virtual void SetFreqVrefPwm(uint32_t frequency) + { + L6208_VrefPwmSetFreq(frequency); + } + + /** + * @brief Set the motor stop mode. + * @param stopMode The desired stop mode (HOLD_MODE or HIZ_MODE). + * @retval None. + */ + virtual void SetStopMode(motorStopMode_t stopMode) + { + L6208_SetStopMode(stopMode); + } + + /** + * @brief Public static functions + */ + + static uint8_t GetNbDevices(void) + { + return numberOfDevices; + } + + /*** Public Interrupt Related Methods ***/ + + /* ACTION 6 --------------------------------------------------------------* + * Implement here interrupt related methods, if any. * + * Note that interrupt handling is platform dependent, e.g.: * + * + mbed: * + * InterruptIn feature_irq(pin); //Interrupt object. * + * feature_irq.rise(callback); //Attach a callback. * + * feature_irq.mode(PullNone); //Set interrupt mode. * + * feature_irq.enable_irq(); //Enable interrupt. * + * feature_irq.disable_irq(); //Disable interrupt. * + * + Arduino: * + * attachInterrupt(pin, callback, RISING); //Attach a callback. * + * detachInterrupt(pin); //Detach a callback. * + * * + * Example (mbed): * + * void AttachFeatureIRQ(void (*fptr) (void)) * + * { * + * feature_irq.rise(fptr); * + * } * + * * + * void EnableFeatureIRQ(void) * + * { * + * feature_irq.enable_irq(); * + * } * + * * + * void DisableFeatureIRQ(void) * + * { * + * feature_irq.disable_irq(); * + * } * + *------------------------------------------------------------------------*/ + /** + * @brief Attaching an interrupt handler to the FLAG interrupt. + * @param fptr An interrupt handler. + * @retval None. + */ + + void AttachFlagIRQ(void (*fptr)(void)) + { + flag_and_enable.mode(PullDown); + flag_and_enable.fall(fptr); + } + + /** + * @brief Enabling the FLAG interrupt handling. + * @param None. + * @retval None. + */ + void EnableFlagIRQ(void) + { + flag_and_enable.enable_irq(); + } + +protected: + + /*** Protected Component Related Methods ***/ + + /* ACTION 7 --------------------------------------------------------------* + * Declare here the component's specific methods. * + * They should be: * + * + Methods with the same name of the C component's virtual table's * + * functions (1); * + * + Methods with the same name of the C component's extended virtual * + * table's functions, if any (2); * + * + Helper methods, if any, like functions declared in the component's * + * source files but not pointed by the component's virtual table (3). * + * * + * Example: * + * Status_t COMPONENT_GetValue(float *f); //(1) * + * Status_t COMPONENT_EnableFeature(void); //(2) * + * Status_t COMPONENT_ComputeAverage(void); //(3) * + *------------------------------------------------------------------------*/ + Status_t L6208_Init(void *init); + Status_t L6208_ReadID(uint8_t *id); + void L6208_AttachErrorHandler(void (*callback)(uint16_t error)); + void L6208_Disable(void); + void L6208_ErrorHandler(uint16_t error); + void L6208_Enable(void); + uint16_t L6208_GetAcceleration(void); + uint16_t L6208_GetCurrentSpeed(void); + uint16_t L6208_GetDeceleration(void); + motorDecayMode_t L6208_GetDecayMode(void); + motorDir_t L6208_GetDirection(void); + uint32_t L6208_GetFwVersion(void); + int32_t L6208_GetMark(void); + uint16_t L6208_GetMaxSpeed(void); + uint16_t L6208_GetMinSpeed(void); + motorState_t L6208_GetMotionState(void); + int32_t L6208_GetPosition(void); + motorStepMode_t L6208_GetStepMode(void); + motorStopMode_t L6208_GetStopMode(void); + void L6208_GoHome(void); + void L6208_GoMark(void); + void L6208_GoTo(int32_t targetPosition); + void L6208_GoToDir(motorDir_t direction, int32_t targetPosition); + void L6208_HardHiZ(void); + void L6208_HardStop(void); + void L6208_Move(motorDir_t direction, uint32_t stepCount); + void L6208_ReleaseReset(void); + void L6208_Reset(void); + void L6208_ResetDevice(void); + void L6208_Run(motorDir_t direction); + bool L6208_SetAcceleration(uint16_t newAcc); + void L6208_SetDecayMode(motorDecayMode_t decayMode); + bool L6208_SetDeceleration(uint16_t newDec); + void L6208_SetDirection(motorDir_t direction); + void L6208_SetHome(void); + void L6208_SetMark(void); + bool L6208_SetMaxSpeed(uint16_t volatile newSpeed); + bool L6208_SetMinSpeed(uint16_t volatile newSpeed); + bool L6208_SetStepMode(motorStepMode_t stepMode); + void L6208_SetStopMode(motorStopMode_t stopMode); + bool L6208_SoftStop(void); + void L6208_TickHandler(void); + uint32_t L6208_VrefPwmGetFreq(void); + void L6208_VrefPwmSetFreq(uint32_t newFreq); + void L6208_WaitWhileActive(void); + + /*** Component's I/O Methods ***/ + + /* ACTION 8 --------------------------------------------------------------* + * Implement here other I/O methods beyond those already implemented * + * above, which are declared extern within the component's header file. * + *------------------------------------------------------------------------*/ + /** + * @brief Reset the clock pin. + * @param None. + * @retval None. + */ + void L6208_Board_CLOCK_PIN_Reset(void) + { + clock = 0; + } + /** + * @brief Set the clock pin. + * @param None. + * @retval None. + */ + void L6208_Board_CLOCK_PIN_Set(void) + { + clock = 1; + } + + /** + * @brief Set the control pin. + * @param None. + * @retval None. + */ + void L6208_Board_CONTROL_PIN_Set(void) + { + control = 1; + } + + /** + * @brief Reset the control pin. + * @param None. + * @retval None. + */ + void L6208_Board_CONTROL_PIN_Reset(void) + { + control = 0; + } + + /** + * @brief Making the CPU wait. + * @param None. + * @retval None. + */ + void L6208_Board_Delay(uint32_t delay) + { + wait_ms(delay); + } + + /** + * @brief Reset the dir pin. + * @param None. + * @retval None. + */ + void L6208_Board_DIR_PIN_Reset(void) + { + direction = 0; + } + + /** + * @brief Set the dir pin. + * @param None. + * @retval None. + */ + void L6208_Board_DIR_PIN_Set(void) + { + direction = 1; + } + + /** + * @brief Disable the power bridges (leave the output bridges HiZ). + * @param None. + * @retval None. + */ + void L6208_Board_Disable(void) + { + flag_and_enable.disable_irq(); + flag_and_enable.mode(PullDown); + } + + /** + * @brief Disabling interrupts. + * @param None. + * @retval None. + */ + void L6208_Board_DisableIrq(void) + { + __disable_irq(); + } + + /** + * @brief Enable the power bridges (leave the output bridges HiZ). + * @param None. + * @retval None. + */ + void L6208_Board_Enable(void) + { + flag_and_enable.mode(PullUp); + flag_and_enable.enable_irq(); + } + + /** + * @brief Enabling interrupts. + * @param None. + * @retval None. + */ + void L6208_Board_EnableIrq(void) + { + __enable_irq(); + } + + /** + * @brief Reset the half full pin. + * @param None. + * @retval None. + */ + void L6208_Board_HALF_FULL_PIN_Reset(void) + { + half_full = 0; + } + + /** + * @brief Set the half full pin. + * @param None. + * @retval None. + */ + void L6208_Board_HALF_FULL_PIN_Set(void) + { + half_full = 1; + } + + /** + * @brief Initialising the the VREFA or VREFB PWM. + * @param None. + * @retval None. + */ + void L6208_Board_VrefPwmInit(uint8_t bridgeId, uint32_t pwmFreq) {} + + /** + * @brief Exit the device from reset mode. + * @param None. + * @retval None. + */ + void L6208_Board_ReleaseReset(void) + { + reset = 1; + } + + /** + * @brief Put the device in reset mode. + * @param None. + * @retval None. + */ + void L6208_Board_Reset(void) + { + reset = 0; + } + + /** + * @brief Get the tick timer frequency in Hz. + * @param None. + * @retval The tick timer frequency in Hz. + */ + uint32_t L6208_Board_TickGetFreq(void) + { + return TIMER_TICK_FREQUENCY; + } + + /** + * @brief Initialising the tick. + * @param None. + * @retval None. + */ + void L6208_Board_TickInit(void) {} + + /** + * @brief Starting the tick timer, setting its frequency + * and attaching a tick handler function to it. + * @param frequency the frequency of the tick. + * @retval None. + */ + void L6208_Board_TickStart(uint16_t frequency) + { + /* Computing the period of the tick. */ + double period = 1.0f / frequency; + + /* Attaching a tick handler function which updates */ + /* the state machine every elapsed period time. */ + ticker.attach(this, &L6208::L6208_TickHandler, period); + } + + /** + * @brief Stopping the tick. + * @param None. + * @retval None. + */ + void L6208_Board_TickStop(void) + { + ticker.detach(); + } + + /** + * @brief Set the pulse width of the VREFA PWM. + * @param[in] pulseWidthInUs pulse width of the PWM in microsecond. + * @retval None. + */ + void L6208_Board_VrefPwmSetPulseWidthA(uint8_t pulseWidthInUs) + { + vrefA_pwm.pulsewidth_us(pulseWidthInUs); + } + + /** + * @brief Set the pulse width of the VREFB PWM. + * @param[in] pulseWidthInUs pulse width of the PWM in microsecond. + * @retval None. + */ + void L6208_Board_VrefPwmSetPulseWidthB(uint8_t pulseWidthInUs) + { + vrefB_pwm.pulsewidth_us(pulseWidthInUs); + } + + /** + * @brief Start the timer for the VREFA or VREFB PWM. + * @param[in] bridgeId + * 0 for BRIDGE_A + * 1 for BRIDGE_B + * @param[in] pwmPeriod period of the PWM used to generate the reference + * voltage for the bridge. + * @retval "true" in case of success, "false" otherwise. + * @note the unit is 1/256th of a microsecond. The VREFA PWM must be started + * before the VREFB PWM. + */ + bool L6208_Board_VrefPwmStart(uint8_t bridgeId, uint16_t pwmPeriod) + { + pwmPeriod>>=8; + /* Setting the period and the duty-cycle of PWM. */ + if (bridgeId == 0) + { + vrefA_pwm.period_us(pwmPeriod); + } + else if (bridgeId == 1) + { + vrefB_pwm.period_us(pwmPeriod); + } + else return false; + return true; + } + + /** + * @brief Stop the timer for the VREFA or VREFB PWM. + * @param[in] bridgeId + * 0 for BRIDGE_A + * 1 for BRIDGE_B + * @retval "true" in case of success, "false" otherwise. + */ + bool L6208_Board_VrefPwmStop(uint8_t bridgeId) + { + if (bridgeId == 0) + { + vrefA_pwm.period_us(0); + } + else if (bridgeId == 1) + { + vrefB_pwm.period_us(0); + } + else return false; + return true; + } + +private: + + /*** Private Component Related Methods ***/ + + void L6208_ClearSysFlag(uint32_t mask); + uint32_t L6208_ComputeNbAccOrDecSteps(uint16_t accOrDecRate); + uint16_t L6208_ConvertAcceDecelRateValue(uint16_t newAccOrDecRate); + void L6208_DoAccel(void); + void L6208_DoDecel(void); + void L6208_DoRun(void); + uint8_t L6208_GetMicrostepSample2Scale(void); + void L6208_Indexmodeinit(void); + bool L6208_IsSysFlag(uint32_t mask); + void L6208_ResetSteps(void); + uint32_t L6208_ScaleWaveformSample(uint8_t sampleIndex); + void L6208_ScaleWaveformTable(void); + void L6208_SetDeviceParamsToGivenValues(l6208_Init_t* pInitDevicePrm); + void L6208_SetDeviceParamsToPredefinedValues(void); + void L6208_SetMicrostepSample2Scale(uint8_t value); + void L6208_SetMicrostepSample2Update(uint8_t value); + void L6208_SetMotionState(motorState_t newMotionState); + bool L6208_SetSpeed(uint16_t newSpeed, uint32_t volatile *pSpeed); + void L6208_SetSysFlag(uint32_t mask); + bool L6208_StartMovement(void); + void L6208_UpdateScanWaveformTable(void); + void L6208_UstepWaveformHandling(void); + bool L6208_VectorCalc(uint8_t newTorque); + bool L6208_VrefPwmComputePulseWidth(uint8_t bridgeId, uint16_t value, bool valueIsPwmDutyCycle); + void L6208_VrefPwmUpdatePulseWidth(void); + +protected: + + /*** Component's Instance Variables ***/ + + /* ACTION 9 --------------------------------------------------------------* + * Declare here interrupt related variables, if needed. * + * Note that interrupt handling is platform dependent, see * + * "Interrupt Related Methods" above. * + * * + * Example: * + * + mbed: * + * InterruptIn feature_irq; * + *------------------------------------------------------------------------*/ + /* Flag Interrupt and chip enable. */ + InterruptIn flag_and_enable; + + /* ACTION 10 -------------------------------------------------------------* + * Declare here other pin related variables, if needed. * + * * + * Example: * + * + mbed: * + * DigitalOut standby_reset; * + *------------------------------------------------------------------------*/ + /* RESET pin. */ + DigitalOut reset; + /* CW_CCW pin. */ + DigitalOut direction; + /* HALF_FULL pin */ + DigitalOut half_full; + /* CONTROL pin */ + DigitalOut control; + /* CLOCK pin */ + DigitalOut clock; + + /* Pulse Width Modulation pin for VREFA pin */ + PwmOut vrefA_pwm; + /* Pulse Width Modulation pin for VREFA pin */ + PwmOut vrefB_pwm; + + /* Timer for the tick */ + Ticker ticker; + + /* ACTION 11 -------------------------------------------------------------* + * Declare here communication related variables, if needed. * + * * + * Example: * + * + mbed: * + * DigitalOut ssel; * + * DevSPI &dev_spi; * + *------------------------------------------------------------------------*/ + /* Configuration. */ + + /* IO Device. */ + + /* ACTION 12 -------------------------------------------------------------* + * Declare here identity related variables, if needed. * + * Note that there should be only a unique identifier for each component, * + * which should be the "who_am_i" parameter. * + *------------------------------------------------------------------------*/ + /* Identity */ + uint8_t who_am_i; + + /* ACTION 13 -------------------------------------------------------------* + * Declare here the component's static and non-static data, one variable * + * per line. * + * * + * Example: * + * float measure; * + * int instance_id; * + * static int number_of_instances; * + *------------------------------------------------------------------------*/ + /* Data. */ + void (*errorHandlerCallback)(uint16_t error); + deviceParams_t devicePrm; + uint8_t deviceInstance; + uint32_t tickFreq; + /// microstepping PWM period and torque scaled waveform samples array + uint16_t updatedMicroTable[L6208_USTEPS_PER_QUARTER_PERIOD+1]; + /// waveform scanning microstepping PWM period sample arrays for VREFA wave + uint16_t microTable1[L6208_USTEPS_PER_QUARTER_PERIOD*3+1]; + /// waveform scanning microstepping PWM period sample array for VREFB wave + uint16_t *pMicroTable2; + + /* Static data. */ + static uint8_t numberOfDevices; + static const uint16_t RefMicroTable[L6208_USTEPS_PER_QUARTER_PERIOD*3]; + +public: + + /* Static data. */ + +}; + +#endif // __L6208_CLASS_H + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/