removed unnecessary message by printf (in 32bit register read function)

Fork of PCA9629A by InetrfaceProducts NXP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PCA9629A.h Source File

PCA9629A.h

00001 /** A sample code for PCA9629A
00002  *
00003  *  @author  Akifumi (Tedd) OKANO, NXP Semiconductors
00004  *  @version 1.1
00005  *  @date    12-Sep-2012
00006  *
00007  *  revision history (PCA9629A)
00008  *      version 0.1 (05-Jun-2013) : PCA9629"A" initial version
00009  *      version 0.2 (05-Jun-2013) : register name fix, register description added
00010  *      version 1.0 (23-Apr-2014) : unnecessary parameter for constructor has been removed
00011  *      version 1.1 (12-Sep-2014) : constructor variation added. ramp parameter API added
00012  *
00013  *  Released under the Apache 2 license License
00014  *
00015  *  An operation sample of PCA9629A stepper motor controller.
00016  *  The mbed accesses the PCA9629A registers through I2C.
00017  *
00018  *  About PCA9629A:
00019  *    http://www.nxp.com/products/interface_and_connectivity/i2c/i2c_bus_controller_and_bridge_ics/PCA9629APW.html
00020  */
00021 
00022 #ifndef     MBED_PCA9629A
00023 #define     MBED_PCA9629A
00024 
00025 #define     DEFAULT_PCA9629A_ADDR       0x42
00026 #define     I2C_SCL_FREQUENCY           (400 * 1000)    //  I2C SCL clock frequency (for when the cunstructor with pin names is used)
00027 #define     ALLCALL_ADDR                0xE0
00028 
00029 
00030 /** PCA9629A class
00031  *
00032  *  This is a driver code for the PCA9629A stepper motor controller.
00033  *  This class provides interface for PCA9629A operation and accessing its registers.
00034  *  Detail information is available on next URL.
00035  *    http://www.nxp.com/products/interface_and_connectivity/i2c/i2c_bus_controller_and_bridge_ics/PCA9629APW.html
00036  *
00037  *  Example:
00038  *  @code
00039  * #include "mbed.h"
00040  * #include "PCA9629A.h"
00041  *
00042  * PCA9629A motor( p28, p27, 0x42 );  //  SDA, SCL, I2C_address (option)
00043  *
00044  * int main() {
00045  *
00046  *     //  Speed setting 200pps for clockwise (CW) rotation
00047  *     motor.pps( PCA9629A::CW, 200 );
00048  *
00049  *     //  Set 24 steps for CW
00050  *     motor.steps( PCA9629A::CW, 24 );
00051  *
00052  *     //  Speed setting 100pps for counterclockwise (CCW) rotation
00053  *     motor.pps( PCA9629A::CCW, 100 );
00054  *
00055  *     //  Set 48 steps for CCW
00056  *     motor.steps( PCA9629A::CCW, 24 );
00057  *
00058  *     while ( 1 ) {
00059  *         motor.start( PCA9629A::CW ); //  Motor start for CW
00060  *         wait( 1.0 );
00061  *
00062  *         motor.start( PCA9629A::CCW ); //  Motor start for CCW
00063  *         wait( 1.0 );
00064  *     }
00065  * }
00066  *  @endcode
00067  */
00068 
00069 
00070 class PCA9629A
00071 {
00072 public:
00073     /** name of the PCA9629A registers */
00074     typedef enum {
00075         MODE,           /**< 0x00   Mode register                                           */
00076         WDTOI,          /**< 0x01   WatchDog Time-Out Interval register                     */
00077         WDCNTL,         /**< 0x02   WatchDog Control register                               */
00078         IO_CFG,         /**< 0x03   I/O Configuration register                              */
00079         INTMODE,        /**< 0x04   Interrupt Mode register                                 */
00080         MSK,            /**< 0x05   Mask interrupt register                                 */
00081         INTSTAT,        /**< 0x06   Interrupt Status register                               */
00082         IP,             /**< 0x07   Input Port register                                     */
00083         INT_MTR_ACT,    /**< 0x08   Interrupt motor action control register                 */
00084         EXTRASTEPS0,    /**< 0x09   Extra steps count for INTP0 control register            */
00085         EXTRASTEPS1,    /**< 0x0A   Extra steps count for INTP1 control register            */
00086         OP_CFG_PHS,     /**< 0x0B   Output Port Configuration and Phase control register    */
00087         OP_STAT_TO,     /**< 0x0C   Output state and time-out control register              */
00088         RUCNTL,         /**< 0x0D   Ramp-up control register                                */
00089         RDCNTL,         /**< 0x0E   Ramp-down control register                              */
00090         PMA,            /**< 0x0F   Perform multiple of actions control register            */
00091         LOOPDLY_CW,     /**< 0x10   Loop delay timer for CW to CCW control register         */
00092         LOOPDLY_CCW,    /**< 0x11   Loop delay timer for CCW to CW control register         */
00093         CWSCOUNTL,      /**< 0x12   Number of clockwise steps register (low byte)           */
00094         CWSCOUNTH,      /**< 0x13   Number of clockwise steps register (high byte)          */
00095         CCWSCOUNTL,     /**< 0x14   Number of counter-clockwise steps register (low byte)   */
00096         CCWSCOUNTH,     /**< 0x15   Number of counter-clockwise steps register (high byte)  */
00097         CWPWL,          /**< 0x16   Clockwise step pulse width register (low byte)          */
00098         CWPWH,          /**< 0x17   Clockwise step pulse width register (high byte)         */
00099         CCWPWL,         /**< 0x18   Counter-clockwise step pulse width register (low byte)  */
00100         CCWPWH,         /**< 0x19   Counter-clockwise step pulse width register (high byte) */
00101         MCNTL,          /**< 0x1A   Motor control register                                  */
00102         SUBADR1,        /**< 0x1B   I2C-bus subaddress 1                                    */
00103         SUBADR2,        /**< 0x1C   I2C-bus subaddress 2                                    */
00104         SUBADR3,        /**< 0x1D   I2C-bus subaddress 3                                    */
00105         ALLCALLADR,     /**< 0x1E   All Call I2C-bus address                                */
00106         STEPCOUNT0,     /**< 0x1F   Step counter registers: STEPCOUNT0                      */
00107         STEPCOUNT1,     /**< 0x20   Step counter registers: STEPCOUNT1                      */
00108         STEPCOUNT2,     /**< 0x21   Step counter registers: STEPCOUNT2                      */
00109         STEPCOUNT3      /**< 0x22   Step counter registers: STEPCOUNT3                      */
00110     } RegisterName;
00111 
00112 private:
00113     /* register names for 2 bytes accessing */
00114     typedef enum {
00115         CWPW_       = CWPWL      | 0x80,    /**< Step pulse width for CW rotation */
00116         CCWPW_      = CCWPWL     | 0x80,    /**< Step pulse width for CCW rotation */
00117         CWSCOUNT_   = CWSCOUNTL  | 0x80,    /**< Number of steps CW */
00118         CCWSCOUNT_  = CCWSCOUNTL | 0x80,    /**< Number of steps CCW */
00119 
00120     } _RegisterNameFor16bitAccess;
00121 
00122 public:
00123     /** register names for 2 bytes accessing */
00124     typedef enum {
00125         CW__STEP_WIDTH      = CWPW_,
00126         CCW_STEP_WIDTH      = CCWPW_,
00127         CW__STEP_COUNT      = CWSCOUNT_,
00128         CCW_STEP_COUNT      = CCWSCOUNT_,
00129     } RegisterNameFor16bitAccess;
00130 
00131     /** register names for 4 bytes accessing */
00132     typedef enum {
00133         STEPCOUNT           = STEPCOUNT0,
00134     } RegisterNameFor32bitAccess;
00135 
00136     /** keyword to select direction of rotation */
00137     typedef enum {
00138         CW      = 0,    /**< Clockwise direction */
00139         CCW             /**< ConterClockwise direction */
00140     } Direction;
00141 
00142     /** Create a PCA9629A instance connected to specified I2C pins with specified address
00143      *
00144      * @param I2C_sda I2C-bus SDA pin
00145      * @param I2C_scl I2C-bus SCL pin
00146      * @param I2C_address I2C-bus address (default: 0x42)
00147      */
00148     PCA9629A(
00149         PinName I2C_sda,
00150         PinName I2C_scl,
00151         char    I2C_address         = DEFAULT_PCA9629A_ADDR
00152     );
00153 
00154     /** Create a PCA9629A instance connected to specified I2C pins with specified address
00155      *
00156      * @param I2C object (instance)
00157      * @param I2C_address I2C-bus address (default: 0x42)
00158      */
00159     PCA9629A(
00160         I2C     &i2c_,
00161         char    I2C_address         = DEFAULT_PCA9629A_ADDR
00162     );
00163 
00164     /** Destractor
00165      *
00166      *
00167      *
00168      */
00169     ~PCA9629A();
00170 
00171     /** Software reset
00172      *
00173      *  Performs software reset through I2C bus
00174      */
00175     void software_reset( void );
00176 
00177     /** Initialize all registers
00178      *
00179      *  The initializing values are defined in the function
00180      */
00181     void init_registers( void );
00182 
00183     /** Initialize all registers
00184      *
00185      *  The initializing values are defined in the function
00186      */
00187     void set_all_registers( char *a, char size );
00188 
00189     /** Write 1 byte data into a register
00190      *
00191      *  Setting 8 bits data into a register
00192      *
00193      *  @param register_name the register name: data writing into
00194      *  @param value 8 bits writing data
00195      */
00196     void write( RegisterName register_name, char value );
00197 
00198     /** Write 2 bytes data into a register
00199      *
00200      *  Setting 16 bits data into registers
00201      *
00202      *  @param register_name the register name: data writing into (it can be "SROTN_", "CWPW_", "CCWPW_", "CWRCOUNT_", "CCWSCOUNT_", "CWRCOUNT_" or "CCWRCOUNT_" )
00203      *  @param value 16 bits writing data
00204      */
00205     void write( RegisterNameFor16bitAccess register_name, short value );
00206 
00207     /** Read 1 byte data from a register
00208      *
00209      *  Setting data into a register
00210      *
00211      *  @param register_name the register name: data reading from
00212      *  @return read 8 bits data from the register
00213      */
00214     char read( RegisterName register_name );
00215 
00216     /** Read 2 byte data from registers
00217      *
00218      *  Setting data into a register
00219      *
00220      *  @param register_name the register name: data writing into (it can be "SROTN_", "CWPW_", "CCWPW_", "CWRCOUNT_", "CCWSCOUNT_", "CWRCOUNT_" or "CCWRCOUNT_" )
00221      *  @return read 16 bits data from the registers
00222      */
00223     short read( RegisterNameFor16bitAccess register_name );
00224 
00225     /** Read 4 byte data from registers
00226      *
00227      *  Setting data into a register
00228      *
00229      *  @param register_name the register name: data writing into (it can be "STEPCOUNT")
00230      *  @return read 32 bits data from the registers
00231      */
00232     long read( RegisterNameFor32bitAccess register_name );
00233 
00234     /** Motor start
00235      *
00236      *  Start command
00237      *  This function starts motor operation with hard-stop flag and rotation+step enabled, no repeat will be performed
00238      *  If custom start is required, use "write( PCA9629A::MCNTL, 0xXX  )" to control each bits.
00239      *
00240      *  @param dir rotate direction ("CW" or "CCW")
00241      */
00242     void start( Direction dir );
00243 
00244     /** Motor stop
00245      *
00246      *  Stop command
00247      *
00248      */
00249     void stop( void );
00250 
00251     /** Set PPS
00252      *
00253      *  Setting PulsePerSecond
00254      *  This interface can be used to set CWPWx or CCWPWx registers
00255      *
00256      *  @param dir rotate direction ("CW" or "CCW")
00257      *  @param pulse_per_second pps defineds pulse width for the motor. The pulse width will be 1/pps
00258      *  @return 16 bit data that what set to the CWPWx or CCWPWx registers
00259      */
00260     short pps( Direction dir, float pulse_per_second );
00261 
00262     /** Set steps count
00263      *
00264      *  Setting step count
00265      *  This interfaces CWSCOUNTx and CCWSCOUNTx registers
00266      *
00267      *  @param dir rotate direction ("CW" or "CCW")
00268      *  @param step_count sets number of steps with 16 bit value
00269      */
00270     void steps( Direction dir, int step_count );
00271 
00272     /** Ramp-up rate
00273      *
00274      *  Setting ramp-up rate
00275      *
00276      *  @param rate set acceleration ratio. From 0 to 13. 0 for disable. Bigger valure accelerate faster.
00277      */
00278     void ramp_up( char rate );
00279 
00280     /** Ramp-down rate
00281      *
00282      *  Setting ramp-down rate
00283      *
00284      *  @param rate set deceleration ratio. From 0 to 13. 0 for disable. Bigger valure decelerate faster.
00285      */
00286     void ramp_down( char rate );
00287 
00288     /** Register dump
00289      *
00290      *  Dumping all register data to serial console
00291      *
00292      */
00293     void register_dump( void );
00294 
00295     /** Register dump
00296      *
00297      *  Dumping all register data to serial console
00298      *
00299      */
00300     void speed_change( unsigned short pw );
00301 
00302     /** Register dump
00303      *
00304      *  Dumping all register data to serial console
00305      *
00306      */
00307     void calibration( float coefficient );
00308 
00309 
00310 private:
00311     /* plescaler range setting */
00312     typedef enum {
00313         PRESCALER_FROM_40_TO_333333,    /*< Prescaler range from   3us(333333pps) to   24.576ms(40   pps) */
00314         PRESCALER_FROM_20_TO_166667,    /*< Prescaler range from   6us(166667pps) to   49.152ms(20   pps) */
00315         PRESCALER_FROM_10_TO_83333,     /*< Prescaler range from  12us( 83333pps) to   98.304ms(10   pps) */
00316         PRESCALER_FROM_5_TO_41667,      /*< Prescaler range from  24us( 41667pps) to  196.608ms( 5   pps) */
00317         PRESCALER_FROM_2_5_TO_20833,    /*< Prescaler range from  48us( 20833pps) to  393.216ms( 2.5 pps) */
00318         PRESCALER_FROM_1_27_TO_10416,   /*< Prescaler range from  96us( 10416pps) to  786.432ms( 1.27pps) */
00319         PRESCALER_FROM_0_64_TO_5208,    /*< Prescaler range from 192us(  5208pps) to 1572.864ms( 0.64pps) */
00320         PRESCALER_FROM_0_32_TO_2604,    /*< Prescaler range from 384us(  2604pps) to 3145.728ms( 0.32pps) */
00321     } PrescalerRange;
00322 
00323     /* Set PPS
00324      *
00325      *  Setting PulsePerSecond
00326      *  This interface can be used to set CWPWx or CCWPWx registers
00327      *
00328      *  @param dir rotate direction ("CW" or "CCW")
00329      *  @param prescaler prescaler setting (for 3 bits setting range from 0 to 0x7. See datasheet)
00330      *  @param pulse_per_second pps defineds pulse width for the motor. The pulse width will be 1/pps
00331      *  @return 16 bit data that what set to the CWPWx or CCWPWx registers
00332      */
00333     short pps( Direction dir, PrescalerRange prescaler, int pulse_per_second );
00334 
00335     I2C     *i2c_p;
00336     I2C     &i2c;
00337     char    i2c_addr;
00338 };
00339 
00340 #endif  //  MBED_PCA9629A