Use BDCMotor_class.h instead of motor_class.h
Dependents: HelloWorld_IHM04A1
Fork of X_NUCLEO_IHM04A1 by
Components/l6206/l6206_class.h
- Committer:
- brdirais
- Date:
- 2016-05-03
- Revision:
- 0:2cb6ce8e07bd
- Child:
- 1:2597a6165252
File content as of revision 0:2cb6ce8e07bd:
/** ****************************************************************************** * @file l6206_class.h * @author IPC Rennes * @version V1.1.0 * @date March 02, 2016 * @brief L6206 driver (dual full bridge driver) * @note (C) COPYRIGHT 2015 STMicroelectronics ****************************************************************************** * @attention * * <h2><center>© COPYRIGHT(c) 2015 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. * ****************************************************************************** */ /* Generated with STM32CubeTOO -----------------------------------------------*/ /* Revision ------------------------------------------------------------------*/ /* Repository: http://svn.x-nucleodev.codex.cro.st.com/svnroot/X-NucleoDev Branch/Trunk/Tag: trunk Based on: X-CUBE-SPN4/trunk/Drivers/BSP/Components/l6206/l6206.h Revision: 0 */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __L6206_CLASS_H #define __L6206_CLASS_H /* Includes ------------------------------------------------------------------*/ /* ACTION 1 ------------------------------------------------------------------* * Include here platform specific header files. * *----------------------------------------------------------------------------*/ #include "mbed.h" /* ACTION 2 ------------------------------------------------------------------* * Include here component specific header files. * *----------------------------------------------------------------------------*/ #include "l6206.h" /* ACTION 3 ------------------------------------------------------------------* * Include here interface specific header files. * * * * Example: * * #include "../Interfaces/Humidity_class.h" * * #include "../Interfaces/Temperature_class.h" * *----------------------------------------------------------------------------*/ #include "../Interfaces/Motor_class.h" /* Private constants ---------------------------------------------------------*/ /** @defgroup L6206_Private_Constants L6206 Private Constants * @{ */ /// The Number of L6206 devices required for initialisation is not supported #define L6206_ERROR_0 (0x8000) /// Error: Access a motor index greater than the one of the current brigde configuration #define L6206_ERROR_1 (0x8001) /// Error: Access a motor index which is not bidirectionnal #define L6206_ERROR_2 (0x8002) /// Maximum frequency of the PWMs in Hz #define L6206_MAX_PWM_FREQ (100000) /// Minimum frequency of the PWMs in Hz #define L6206_MIN_PWM_FREQ (2) /// Bridge Input 1A #define INPUT_1A (0) /// Bridge Input 2A #define INPUT_2A (1) /// Bridge Input 1B #define INPUT_1B (2) /// Bridge Input 2B #define INPUT_2B (3) /// Bridge A #define BRIDGE_A (0) /// Bridge B #define BRIDGE_B (1) /* Classes -------------------------------------------------------------------*/ /** * @brief Class representing a L6206 component. */ class L6206 : public Motor { public: /*** Constructor and Destructor Methods ***/ /** * @brief Constructor. */ L6206(PinName EN_flag_A, PinName EN_flag_B, PinName pwm_1A, PinName pwm_2A, PinName pwm_1B, PinName pwm_2B) : Motor(), flag_A_irq(EN_flag_A), flag_B_irq(EN_flag_B), EN_flag_A(EN_flag_A), EN_flag_B(EN_flag_B), pwm_1A(pwm_1A), pwm_2A(pwm_2A), pwm_1B(pwm_1B), pwm_2B(pwm_2B) { /* ACTION 4 ----------------------------------------------------------* * Initialize here the component's member variables, one variable per * * line. * * * * Example: * * measure = 0; * * instance_id = number_of_instances++; * *--------------------------------------------------------------------*/ flagInterruptCallback = 0; errorHandlerCallback = 0; numberOfDevices = 0; deviceInstance = 0; } /** * @brief Destructor. */ virtual ~L6206(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 *f) //(1) * * { * * return COMPONENT_GetValue(float *f); * * } * * * * virtual int EnableFeature(void) //(2) * * { * * return COMPONENT_EnableFeature(); * * } * *------------------------------------------------------------------------*/ virtual int Init(void *init = NULL) { return (int) L6206_Init((void *) init); } virtual int ReadID(uint8_t *id = NULL) { return (int) L6206_ReadId((uint8_t *) id); } virtual void AttachErrorHandler(void (*callback)(uint16_t error)) { L6206_AttachErrorHandler((void (*)(uint16_t error)) callback); } virtual void AttachFlagInterrupt(void (*callback)(void)) { L6206_AttachFlagInterrupt((void (*)(void)) callback); } virtual uint16_t GetCurrentSpeed(uint8_t motorId) { return (uint16_t) L6206_GetCurrentSpeed((uint8_t) motorId); } virtual motorState_t GetDeviceState(uint8_t motorId) { return (motorState_t) L6206_GetDeviceState((uint8_t) motorId); } virtual uint8_t GetFwVersion(void) { return (uint8_t) L6206_GetFwVersion(); } virtual uint16_t GetMaxSpeed(uint8_t motorId) { return (uint16_t) L6206_GetMaxSpeed((uint8_t) motorId); } virtual void HardStop(uint8_t motorId) { L6206_HardStop((uint8_t) motorId); } virtual void Run(uint8_t stepCount, motorDir_t direction) { L6206_Run((uint8_t) stepCount, (motorDir_t) direction); } virtual bool SetMaxSpeed(uint8_t motorId, uint16_t newMaxSpeed) { return (bool) L6206_SetMaxSpeed((uint8_t) motorId, (uint16_t) newMaxSpeed); } virtual void CmdDisable(uint8_t bridgeId) { L6206_DisableBridge((uint8_t) bridgeId); } virtual void CmdEnable(uint8_t bridgeId) { L6206_EnableBridge((uint8_t) bridgeId); } virtual uint16_t CmdGetStatus(uint8_t bridgeId) { return (uint16_t) L6206_GetBridgeStatus((uint8_t) bridgeId); } virtual void CmdHardHiZ(uint8_t motorId) { L6206_HardHiz((uint8_t) motorId); } virtual void ErrorHandler(uint16_t error) { L6206_ErrorHandler((uint16_t) error); } virtual void SetDualFullBridgeConfig(uint8_t newConfig) { L6206_SetDualFullBridgeConfig((uint8_t) newConfig); } virtual uint32_t GetBridgeInputPwmFreq(uint8_t bridgeId) { return (uint32_t) L6206_GetBridgeInputPwmFreq((uint8_t) bridgeId); } virtual void SetBridgeInputPwmFreq(uint8_t bridgeId, uint32_t newFreq) { L6206_SetBridgeInputPwmFreq((uint8_t) bridgeId, (uint32_t) newFreq); } virtual bool SetNbDevices(uint8_t nbDevices) { return (bool) L6206_SetNbDevices((uint8_t) nbDevices); } /*** 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.fall(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.fall(fptr); * * } * * * * void EnableFeatureIRQ(void) * * { * * feature_irq.enable_irq(); * * } * * * * void DisableFeatureIRQ(void) * * { * * feature_irq.disable_irq(); * * } * *------------------------------------------------------------------------*/ /** * @brief Enabling the FLAG interrupt handling. * @param None. * @retval None. */ void EnableFlagIRQ(uint8_t bridgeId) { if (bridgeId == BRIDGE_A) { flag_A_irq.mode(PullUp); flag_A_irq.fall(this, &L6206::L6206_FlagInterruptHandler); } else { flag_B_irq.mode(PullUp); flag_B_irq.fall(this, &L6206::L6206_FlagInterruptHandler); } } /** * @brief Disabling the FLAG interrupt handling. * @param None. * @retval None. */ void DisableFlagIRQ(uint8_t bridgeId) { if (bridgeId == BRIDGE_A) flag_A_irq.fall(NULL); else flag_B_irq.fall(NULL); } /*** Public In/Out Related Methods ***/ void OutVal( uint8_t bridgeId, uint8_t val) { if( bridgeId == BRIDGE_A) { EN_flag_A.output(); EN_flag_A.mode(PullNone); EN_flag_A.write(val); } else { EN_flag_B.output(); EN_flag_B.mode(PullNone); EN_flag_B.write(val); } } 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 L6206_Init(void *init); Status_t L6206_ReadId(uint8_t *id); void L6206_TickHandler(uint8_t deviceId); //Handle the device state machine at each tick timer pulse end void L6206_AttachErrorHandler(void (*callback)(uint16_t error)); //Attach a user callback to the error handler void L6206_AttachFlagInterrupt(void (*callback)(void)); //Attach a user callback to the flag Interrupt void L6206_DisableBridge(uint8_t bridgeId); //Disable the specified bridge void L6206_EnableBridge(uint8_t bridgeId); //Enable the specified bridge uint16_t L6206_GetBridgeStatus(uint8_t deviceId); //Get bridge status uint16_t L6206_GetCurrentSpeed(uint8_t motorId); //Return the current speed in pps motorState_t L6206_GetDeviceState(uint8_t motorId); //Return the device state uint8_t L6206_GetFwVersion(void); //Return the FW version uint16_t L6206_GetMaxSpeed(uint8_t motorId); //Return the max speed in pps void L6206_HardHiz(uint8_t motorId); //Stop the motor and disable the power bridge void L6206_HardStop(uint8_t motorId); //Stop the motor without disabling the power bridge void L6206_Run(uint8_t motorId, motorDir_t direction); //Run the motor uint32_t L6206_GetBridgeInputPwmFreq(uint8_t bridgeId); // Get the PWM frequency of the bridge input void L6206_SetBridgeInputPwmFreq(uint8_t bridgeId, uint32_t newFreq); // Set the PWM frequency of the bridge input void L6206_SetDualFullBridgeConfig(uint8_t newConfig); // Set dual full bridge configuration bool L6206_SetMaxSpeed(uint8_t motorId,uint16_t newMaxSpeed); //Set the max speed in pps bool L6206_SetNbDevices(uint8_t nbDevices); //Set the number of driver devices void L6206_ErrorHandler(uint16_t error); void L6206_FlagInterruptHandler(void); uint8_t L6206_GetBridgeIdUsedByMotorId(uint8_t motorId); uint8_t L6206_GetBridgeInputUsedByMotorId(uint8_t motorId); uint8_t L6206_GetMotorIdUsingbridgeInput(uint8_t bridgeInput); uint8_t L6206_GetSecondBridgeInputUsedByMotorId(uint8_t motorId); bool L6206_IsBidirectionnalMotor(uint8_t motorId); void L6206_SetDeviceParamsToPredefinedValues(void); void L6206_SetDeviceParamsToGivenValues(L6206_Init_t* initDevicePrm); /*** 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. * *------------------------------------------------------------------------*/ void L6206_Board_Delay(uint32_t delay) { wait_ms(delay); } void L6206_Board_DisableBridge(uint8_t bridgeId) { DisableFlagIRQ(BRIDGE_A); DisableFlagIRQ(BRIDGE_B); __disable_irq(); OutVal( bridgeId, 0); __enable_irq(); } void L6206_Board_EnableBridge(uint8_t bridgeId, uint8_t addDelay) { OutVal( bridgeId, 1); if (addDelay != 0) { wait_ms(BSP_MOTOR_CONTROL_BOARD_BRIDGE_TURN_ON_DELAY); } EnableFlagIRQ( bridgeId); } uint32_t L6206_Board_GetFlagPinState(uint8_t bridgeId) { if (bridgeId == 0) { EN_flag_A.input(); return EN_flag_A.read(); } else { EN_flag_B.input(); return EN_flag_B.read(); } } void L6206_Board_GpioInit(void) { /* Init bridge Enable */ EN_flag_A.output(); EN_flag_A.write(0); EN_flag_A.input(); EN_flag_B.output(); EN_flag_B.write(0); EN_flag_B.input(); /* Init flag Irq */ DisableFlagIRQ(BRIDGE_A); DisableFlagIRQ(BRIDGE_B); } /** Board PWM management **/ void L6206_Board_PwmSetFreq(uint8_t bridgeInput, uint32_t newFreq, uint8_t duty) { /* Computing the period of PWM. */ float period = 1.0f / newFreq; float duty_cycle; int period_us = (int)(period * 1E6); if (duty > 100) duty = 100; duty_cycle = (float)duty / 100.0f; switch (bridgeInput) { case 0: default: /* Setting the period and the duty-cycle of PWM. */ pwm_1A.period_us(period_us); pwm_1A.write(duty_cycle); break; case 1: /* Setting the period and the duty-cycle of PWM. */ pwm_2A.period_us(period_us); pwm_2A.write(duty_cycle); break; case 2: /* Setting the period and the duty-cycle of PWM. */ pwm_1B.period_us(period_us); pwm_1B.write(duty_cycle); break; case 3: /* Setting the period and the duty-cycle of PWM. */ pwm_2B.period_us(period_us); pwm_2B.write(duty_cycle); break; } } void L6206_Board_PwmDeInit(uint8_t bridgeInput) { switch (bridgeInput) { case 0: default: //timer_pwm_1A.detach(); break; case 1: //timer_pwm_2A.detach(); break; case 2: //timer_pwm_1B.detach(); break; case 3: //timer_pwm_2B.detach(); break; } } void L6206_Board_PwmInit(uint8_t bridgeInput) { } void L6206_Board_PwmStop(uint8_t bridgeInput) { switch (bridgeInput) { case 0: default: pwm_1A.write(0.0); break; case 1: pwm_2A.write(0.0); break; case 2: pwm_1B.write(0.0); break; case 3: pwm_2B.write(0.0); break; } } /*** 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. */ InterruptIn flag_A_irq; InterruptIn flag_B_irq; /* ACTION 10 -------------------------------------------------------------* * Declare here other pin related variables, if needed. * * * * Example: * * + mbed: * * DigitalOut standby_reset; * *------------------------------------------------------------------------*/ /* Digital In/Out for Flag EN pin */ DigitalInOut EN_flag_A; DigitalInOut EN_flag_B; /* PWM Out pin */ PwmOut pwm_1A; PwmOut pwm_2A; PwmOut pwm_1B; PwmOut pwm_2B; /* ACTION 11 -------------------------------------------------------------* * Declare here communication related variables, if needed. * * * * Example: * * + mbed: * * DigitalOut address; * * DevI2C &dev_i2c; * *------------------------------------------------------------------------*/ /* 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; * *------------------------------------------------------------------------*/ void (*flagInterruptCallback)(void); void (*errorHandlerCallback)(uint16_t error); uint8_t numberOfDevices; uint8_t deviceInstance; deviceParams_t devicePrm; /** PWM timer variables */ bool pwm_1A_activated; bool pwm_2A_activated; bool pwm_1B_activated; bool pwm_2B_activated; }; #endif /* __L6206_CLASS_H */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/