Personal fork of the library for direct control instead of library control
Dependents: Thesis_Rotating_Platform
Fork of X_NUCLEO_IHM01A1 by
Diff: Components/l6474/l6474_class.h
- Revision:
- 7:f7e0c3621f77
- Parent:
- 6:a47569fc7534
- Child:
- 8:42e0b00b1e4d
diff -r a47569fc7534 -r f7e0c3621f77 Components/l6474/l6474_class.h --- a/Components/l6474/l6474_class.h Thu Nov 19 10:56:34 2015 +0000 +++ b/Components/l6474/l6474_class.h Fri Nov 20 18:00:03 2015 +0000 @@ -88,13 +88,14 @@ /** * @brief Constructor. + * @param flag_irq pin name of the FLAG pin of the component. * @param standby_reset pin name of the STBY\RST pin of the component. * @param direction pin name of the DIR pin of the component. * @param pwm pin name of the PWM pin of the component. * @param ssel pin name of the SSEL pin of the SPI device to be used for communication. * @param spi SPI device to be used for communication. */ - L6474(PinName standby_reset, PinName direction, PinName pwm, PinName ssel, DevSPI &spi) : StepperMotor(), standby_reset(standby_reset), direction(direction), pwm(pwm), ssel(ssel), dev_spi(spi) + L6474(PinName flag_irq, PinName standby_reset, PinName direction, PinName pwm, PinName ssel, DevSPI &spi) : StepperMotor(), flag_irq(flag_irq), standby_reset(standby_reset), direction(direction), pwm(pwm), ssel(ssel), dev_spi(spi) { /* ACTION 4 ----------------------------------------------------------* * Initialize here the component's member variables, one variable per * @@ -106,7 +107,6 @@ * T0_degC = 0; * * T1_degC = 0; * *--------------------------------------------------------------------*/ - flagInterruptCallback = 0; errorHandlerCallback = 0; deviceInstance = numberOfDevices; numberOfDevices++; @@ -142,24 +142,75 @@ * return COMPONENT_EnableFeature(); * * } * *------------------------------------------------------------------------*/ + /** + * @brief Initializing 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) { return (int) L6474_Init((MOTOR_InitTypeDef *) 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) { return (int) L6474_ReadID((uint8_t *) id); } + /** + * @brief Getting the value of the Status Register. + * @param None. + * @retval None. + * @note The Status Register's flags are cleared, contrary to ReadStatusRegister(). + */ virtual unsigned int GetStatus(void) { return (unsigned int) L6474_CmdGetStatus(); } + /** + * @brief Getting a parameter. + * @param parameter register adress. + * @retval a register value. + * @note The Status Register's flags are cleared, contrary to ReadStatusRegister(). + * parameter can be one of the following: + * + L6474_ABS_POS + * + L6474_EL_POS + * + L6474_MARK + * + L6474_RESERVED_REG01 + * + L6474_RESERVED_REG02 + * + L6474_RESERVED_REG03 + * + L6474_RESERVED_REG04 + * + L6474_RESERVED_REG05 + * + L6474_RESERVED_REG06 + * + L6474_TVAL + * + L6474_RESERVED_REG07 + * + L6474_RESERVED_REG08 + * + L6474_RESERVED_REG09 + * + L6474_RESERVED_REG10 + * + L6474_T_FAST + * + L6474_TON_MIN + * + L6474_TOFF_MIN + * + L6474_RESERVED_REG11 + * + L6474_ADC_OUT + * + L6474_OCD_TH + * + L6474_RESERVED_REG12 + * + L6474_STEP_MODE + * + L6474_ALARM_EN + * + L6474_CONFIG + * + L6474_STATUS + * + L6474_RESERVED_REG13 + * + L6474_RESERVED_REG14 + * + L6474_INEXISTENT_REG + */ virtual unsigned int GetParameter(unsigned int parameter) { - return (unsigned int) L6474_CmdGetParam((unsigned int) parameter); + return (unsigned int) L6474_CmdGetParam((L6474_Registers_t) parameter); } virtual signed int GetPosition(void) @@ -202,9 +253,44 @@ return (direction_t) (L6474_GetDirection() == FORWARD ? StepperMotor::FWD : StepperMotor::BWD); } + /** + * @brief Setting a parameter. + * @param parameter register adress. + * @param value value to be set. + * @retval None. + * @note parameter can be one of the following: + * + L6474_ABS_POS + * + L6474_EL_POS + * + L6474_MARK + * + L6474_RESERVED_REG01 + * + L6474_RESERVED_REG02 + * + L6474_RESERVED_REG03 + * + L6474_RESERVED_REG04 + * + L6474_RESERVED_REG05 + * + L6474_RESERVED_REG06 + * + L6474_TVAL + * + L6474_RESERVED_REG07 + * + L6474_RESERVED_REG08 + * + L6474_RESERVED_REG09 + * + L6474_RESERVED_REG10 + * + L6474_T_FAST + * + L6474_TON_MIN + * + L6474_TOFF_MIN + * + L6474_RESERVED_REG11 + * + L6474_ADC_OUT + * + L6474_OCD_TH + * + L6474_RESERVED_REG12 + * + L6474_STEP_MODE + * + L6474_ALARM_EN + * + L6474_CONFIG + * + L6474_STATUS + * + L6474_RESERVED_REG13 + * + L6474_RESERVED_REG14 + * + L6474_INEXISTENT_REG + */ virtual void SetParameter(unsigned int parameter, unsigned int value) { - L6474_CmdSetParam((unsigned int) parameter, (unsigned int) value); + L6474_CmdSetParam((L6474_Registers_t) parameter, (unsigned int) value); } virtual void SetHome(void) @@ -272,6 +358,21 @@ L6474_HardStop(); } + virtual void WaitWhileActive(void) + { + L6474_WaitWhileActive(); + } + + /** + * @brief Getting the device state. + * @param None. + * @retval The device state. + * @note The device state can be one of the following: + * + ACCELERATING + * + DECELERATING + * + STEADY + * + INACTIVE + */ virtual motorState_t GetDeviceState(void) { return (motorState_t) L6474_GetDeviceState(); @@ -287,16 +388,6 @@ L6474_SetDirection((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD)); } - virtual void StepClockHandler(void) - { - L6474_StepClockHandler(); - } - - virtual void WaitWhileActive(void) - { - L6474_WaitWhileActive(); - } - virtual void CmdDisable(void) { L6474_CmdDisable(); @@ -307,49 +398,44 @@ L6474_CmdEnable(); } - virtual void CmdNop(void) - { - L6474_CmdNop(); - } - + /** + * @brief Reading the Status Register. + * @param None. + * @retval None. + * @note The Status Register's flags are not cleared, contrary to GetStatus(). + */ virtual uint16_t ReadStatusRegister(void) { return (uint16_t) L6474_ReadStatusRegister(); } - virtual void ReleaseReset(void) - { - L6474_ReleaseReset(); - } - - virtual void Reset(void) + /** + * @brief Selecting the Step Mode. + * @param step_mode the Step Mode. + * @retval None. + * @note step_mode can be one of the following: + * + STEP_MODE_FULL + * + STEP_MODE_HALF + * + STEP_MODE_1_4 + * + STEP_MODE_1_8 + * + STEP_MODE_1_16 + * + STEP_MODE_1_32 + * + STEP_MODE_1_64 + * + STEP_MODE_1_128 + */ + virtual void SelectStepMode(motorStepMode_t step_mode) { - L6474_Reset(); - } - - virtual void SelectStepMode(motorStepMode_t stepMod) - { - L6474_SelectStepMode((motorStepMode_t) stepMod); + L6474_SelectStepMode((motorStepMode_t) step_mode); } - virtual void ErrorHandler(uint16_t error) - { - L6474_ErrorHandler((uint16_t) error); - } - - virtual void AttachErrorHandler(void (*callback)(uint16_t error)) + /** + * @brief Attaching an error handler. + * @param fptr an error handler. + * @retval None. + */ + virtual void AttachErrorHandler(void (*fptr)(uint16_t error)) { - L6474_AttachErrorHandler((void (*)(uint16_t error)) callback); - } - - virtual void AttachFlagInterrupt(void (*callback)(void)) - { - L6474_AttachFlagInterrupt((void (*)(void)) callback); - } - - virtual void FlagInterruptHandler(void) - { - L6474_FlagInterruptHandler(); + L6474_AttachErrorHandler((void (*)(uint16_t error)) fptr); } @@ -359,31 +445,60 @@ * Implement here interrupt related methods, if any. * * Note that interrupt handling is platform dependent, e.g.: * * + mbed: * - * InterruptIn feature_int(pin); //Interrupt object. * - * feature_int.rise(callback); //Attach a callback. * - * feature_int.mode(PullNone); //Set interrupt mode. * - * feature_int.enable_irq(); //Enable interrupt. * - * feature_int.disable_irq(); //Disable interrupt. * + * 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 Attach_Feature_IRQ(void (*fptr) (void)) * + * void AttachFeatureIRQ(void (*fptr) (void)) * * { * - * feature_int.rise(fptr); * + * feature_irq.rise(fptr); * + * } * + * * + * void EnableFeatureIRQ(void) * + * { * + * feature_irq.enable_irq(); * * } * * * - * void Enable_Feature_IRQ(void) * + * void DisableFeatureIRQ(void) * * { * - * feature_int.enable_irq(); * - * } * - * * - * void Disable_Feature_IRQ(void) * - * { * - * feature_int.disable_irq(); * + * feature_irq.disable_irq(); * * } * *------------------------------------------------------------------------*/ + /** + * @brief Attaching a FLAG interrupt handler. + * @param fptr an interrupt handler. + * @retval None. + */ + void AttachFlagIRQ(void (*fptr)(void)) + { + flag_irq.rise(fptr); + } + + /** + * @brief Enabling FLAG interrupt handling. + * @param None. + * @retval None. + */ + void EnableFlagIRQ(void) + { + flag_irq.enable_irq(); + } + + /** + * @brief Disabling FLAG interrupt handling. + * @param None. + * @retval None. + */ + void DisableFlagIRQ(void) + { + flag_irq.disable_irq(); + } protected: @@ -405,54 +520,52 @@ * DrvStatusTypeDef COMPONENT_EnableFeature(void); //(2) * * DrvStatusTypeDef COMPONENT_ComputeAverage(void); //(3) * *------------------------------------------------------------------------*/ - void L6474_AttachErrorHandler(void (*callback)(uint16_t error)); //Attach a user callback to the error handler - void L6474_AttachFlagInterrupt(void (*callback)(void)); //Attach a user callback to the flag Interrupt - DrvStatusTypeDef L6474_Init(MOTOR_InitTypeDef *init); //Start the L6474 library - DrvStatusTypeDef L6474_ReadID(uint8_t *id); //Read Id to get driver instance - uint16_t L6474_GetAcceleration(void); //Return the acceleration in pps^2 - uint16_t L6474_GetCurrentSpeed(void); //Return the current speed in pps - uint16_t L6474_GetDeceleration(void); //Return the deceleration in pps^2 - motorState_t L6474_GetDeviceState(void); //Return the device state - uint8_t L6474_GetFwVersion(void); //Return the FW version - int32_t L6474_GetMark(void); //Return the mark position - uint16_t L6474_GetMaxSpeed(void); //Return the max speed in pps - uint16_t L6474_GetMinSpeed(void); //Return the min speed in pps - int32_t L6474_GetPosition(void); //Return the ABS_POSITION (32b signed) - void L6474_GoHome(void); //Move to the home position - void L6474_GoMark(void); //Move to the Mark position - void L6474_GoTo(int32_t targetPosition); //Go to the specified position - void L6474_HardStop(void); //Stop the motor and disable the power bridge - void L6474_Move(motorDir_t direction, uint32_t stepCount); //Move the motor of the specified number of steps - void L6474_Run(motorDir_t direction); //Run the motor - bool L6474_SetAcceleration(uint16_t newAcc); //Set the acceleration in pps^2 - bool L6474_SetDeceleration(uint16_t newDec); //Set the deceleration in pps^2 - void L6474_SetHome(void); //Set current position to be the home position - void L6474_SetMark(void); //Set current position to be the Markposition - bool L6474_SetMaxSpeed(uint16_t newMaxSpeed); //Set the max speed in pps - bool L6474_SetMinSpeed(uint16_t newMinSpeed); //Set the min speed in pps - bool L6474_SoftStop(void); //Progressively stops the motor - void L6474_WaitWhileActive(void); //Wait for the device state becomes Inactive - void L6474_CmdDisable(void); //Send the L6474_DISABLE command - void L6474_CmdEnable(void); //Send the L6474_ENABLE command - uint32_t L6474_CmdGetParam(uint32_t param); //Send the L6474_GET_PARAM command - uint16_t L6474_CmdGetStatus(void); //Send the L6474_GET_STATUS command - void L6474_CmdNop(void); //Send the L6474_NOP command - void L6474_CmdSetParam(uint32_t param, uint32_t value); //Send the L6474_SET_PARAM command - uint16_t L6474_ReadStatusRegister(void); //Read the L6474_STATUS register without clearing the flags - void L6474_SelectStepMode(motorStepMode_t stepMod); //Step mode selection - motorDir_t L6474_GetDirection(void); //Get the direction of rotation - void L6474_SetDirection(motorDir_t direction); //Set the direction of rotation + void L6474_AttachErrorHandler(void (*callback)(uint16_t error)); + DrvStatusTypeDef L6474_Init(MOTOR_InitTypeDef *init); + DrvStatusTypeDef L6474_ReadID(uint8_t *id); + uint16_t L6474_GetAcceleration(void); + uint16_t L6474_GetCurrentSpeed(void); + uint16_t L6474_GetDeceleration(void); + motorState_t L6474_GetDeviceState(void); + uint8_t L6474_GetFwVersion(void); + int32_t L6474_GetMark(void); + uint16_t L6474_GetMaxSpeed(void); + uint16_t L6474_GetMinSpeed(void); + int32_t L6474_GetPosition(void); + void L6474_GoHome(void); + void L6474_GoMark(void); + void L6474_GoTo(int32_t targetPosition); + void L6474_HardStop(void); + void L6474_Move(motorDir_t direction, uint32_t stepCount); + void L6474_Run(motorDir_t direction); + bool L6474_SetAcceleration(uint16_t newAcc); + bool L6474_SetDeceleration(uint16_t newDec); + void L6474_SetHome(void); + void L6474_SetMark(void); + bool L6474_SetMaxSpeed(uint16_t newMaxSpeed); + bool L6474_SetMinSpeed(uint16_t newMinSpeed); + bool L6474_SoftStop(void); + void L6474_WaitWhileActive(void); + void L6474_CmdDisable(void); + void L6474_CmdEnable(void); + uint32_t L6474_CmdGetParam(L6474_Registers_t parameter); + uint16_t L6474_CmdGetStatus(void); + void L6474_CmdNop(void); + void L6474_CmdSetParam(L6474_Registers_t parameter, uint32_t value); + uint16_t L6474_ReadStatusRegister(void); + void L6474_SelectStepMode(motorStepMode_t stepMod); + motorDir_t L6474_GetDirection(void); + void L6474_SetDirection(motorDir_t direction); void L6474_ApplySpeed(uint16_t newSpeed); void L6474_ComputeSpeedProfile(uint32_t nbSteps); - int32_t L6474_ConvertPosition(uint32_t abs_position_reg); + int32_t L6474_ConvertPosition(uint32_t abs_position_reg); void L6474_ErrorHandler(uint16_t error); - void L6474_FlagInterruptHandler(void); void L6474_SendCommand(uint8_t param); void L6474_SetRegisterToPredefinedValues(void); - void L6474_WriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte); + void L6474_WriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte); void L6474_SetDeviceParamsToPredefinedValues(void); void L6474_StartMovement(void); - void L6474_StepClockHandler(void); + void L6474_StepClockHandler(void); uint8_t L6474_Tval_Current_to_Par(double Tval); uint8_t L6474_Tmin_Time_to_Par(double Tmin); @@ -503,99 +616,107 @@ * Implement here other I/O methods beyond those already implemented * * above, which are declared extern within the component's header file. * *------------------------------------------------------------------------*/ - /* - * System call to make the CPU wait. + /** + * @brief Making the CPU wait. + * @param None. + * @retval None. */ void L6474_Delay(uint32_t delay) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs.*/ wait_ms(delay); } - /* - * System call to enable interrupts. + /** + * @brief Enabling interrupts. + * @param None. + * @retval None. */ void L6474_EnableIrq(void) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs.*/ __enable_irq(); } - /* - * System call to disable interrupts. + /** + * @brief Disabling interrupts. + * @param None. + * @retval None. */ void L6474_DisableIrq(void) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs.*/ __disable_irq(); } - /* - * Sets the frequency of PWM. - * The frequency is directly the current speed of the device. + /** + * @brief Setting the frequency of PWM. + * The frequency controls directly the speed of the device. + * @param newFreq the frequency of PWM. + * @retval None. */ void L6474_PwmSetFreq(uint16_t newFreq) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs.*/ double period = 1.0f / newFreq; pwm.period(period); pwm.write(0.5f); ticker.attach(this, &L6474::L6474_StepClockHandler, period); } - /* - * Initialises the PWM uses by the specified device. - * + Device 0 uses PWM1 based on timer 1; - * + Device 1 uses PWM2 based on timer 2; - * + Device 2 uses PWM3 based on timer 3. + /** + * @brief Initialising the PWM. + * @param None. + * @retval None. */ - void L6474_PwmInit(void) - { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs.*/ - } + void L6474_PwmInit(void) {} - /* - * Stops the PWM. + /** + * @brief Stopping the PWM. + * @param None. + * @retval None. */ void L6474_PwmStop(void) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs.*/ pwm.write(0.0f); ticker.detach(); } - /* - * Puts the device in standby mode. + /** + * @brief Putting the device in standby mode. + * @param None. + * @retval None. */ void L6474_ReleaseReset(void) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ standby_reset = 1; } - /* - * Puts the device in reset mode. + /** + * @brief Putting the device in reset mode. + * @param None. + * @retval None. */ void L6474_Reset(void) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ standby_reset = 0; } - /* - * Set the direction of rotation. + /** + * @brief Setting the direction of rotation. + * @param gpioState direction of rotation: "1" for forward, "0" for backward. + * @retval None. */ void L6474_SetDirectionGpio(uint8_t gpioState) { direction = gpioState; } - /* - * Write and read bytes to/from the component through the SPI at the same time. + /** + * @brief Writing and reading bytes to/from the component through the SPI at the same time. + * @param[in] pByteToTransmit pointer to the buffer of data to send. + * @param[out] pReceivedByte pointer to the buffer to read data into. + * @retval "0" in case of success, "1" otherwise. */ uint8_t L6474_SpiWriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte) { - return (uint8_t) ReadWrite(pReceivedByte, pByteToTransmit, numberOfDevices); + return (uint8_t) (ReadWrite(pReceivedByte, pByteToTransmit, numberOfDevices) == COMPONENT_OK ? 0 : 1); } @@ -611,6 +732,9 @@ /* Type. */ uint8_t type; + /* Flag Interrupt. */ + InterruptIn flag_irq; + /* Standby/reset pin. */ DigitalOut standby_reset; @@ -650,15 +774,21 @@ * float T0_degC; * * float T1_degC; * *------------------------------------------------------------------------*/ - void (*flagInterruptCallback)(void); void (*errorHandlerCallback)(uint16_t error); deviceParams_t devicePrm; uint8_t deviceInstance; + + /* Static data. */ static uint8_t numberOfDevices; + static uint8_t spiTxBursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; + static uint8_t spiRxBursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; + + +public: + + /* Static data. */ static bool spiPreemtionByIsr; static bool isrFlag; - static uint8_t spiTxBursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; - static uint8_t spiRxBursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; }; #endif // __L6474_CLASS_H