Driver for AK7451 Angle Senor(SPI).

Fork of AK7451 by AKM Development Platform

Revision:
0:56b554871e05
Child:
1:536cf25b0eb4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ak7451.h	Thu Apr 28 19:20:32 2016 +0000
@@ -0,0 +1,228 @@
+#ifndef AK7451_H
+#define AK7451_H
+
+#include "mbed.h"
+
+/**
+ * This is a device driver of AK7451.
+ *
+ * @note AK7451 is a high speed angle sensor IC manufactured by AKM.
+ * 
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "ak7451.h"
+ * 
+ * 
+ * int main() {
+ *     // Creates an instance of SPI
+ * }
+ * @endcode
+ */
+class AK7451
+{
+public:
+
+    /**
+     * Available opration modes in AK7451.
+     */
+    typedef enum {
+        AK7451_NORMAL_MODE  = 0x0000,
+        AK7451_USER_MODE    = 0x050F,
+    } OperationMode;
+
+    /**
+     * Status of function. 
+     */
+    typedef enum {
+        SUCCESS,               /**< The function processed successfully. */
+        ERROR,                 /**< General Error */
+        ERROR_IN_USER_MODE,
+        ERROR_IN_NORMAL_MODE,
+        ERROR_PARITY,
+        ERROR_ABNORMAL_STRENGTH,
+    } Status;
+
+    /**
+     * Constructor.
+     *
+     */
+    AK7451();
+
+    /**
+     * Destructor.
+     *
+     */
+     ~AK7451();
+
+/*    
+    virtual AkmSensor::Status init();
+    virtual void eventCallback();
+    virtual bool isEvent();
+    virtual AkmSensor::Status startSensor();
+    virtual AkmSensor::Status startSensor(float sec);
+    virtual AkmSensor::Status stopSensor();
+    virtual AkmSensor::Status readSensorData(char* data);
+*/
+    /**
+     * begin
+     *
+     * @param *spi pointer to SPI instance
+     * @param *cs pointer to DigitalOut instance for CS
+     */
+    void begin(SPI *spi, DigitalOut *cs);
+    
+    /**
+     * Check the connection. 
+     *
+     * @note Connection check is performed by reading a register which has a fixed value and verify it.
+     *
+     * @return Returns SUCCESS when succeeded, otherwise returns another code.
+     */
+    Status checkConnection();
+
+    /** 
+     * Writes data to EEPROM on the device. 
+     * @param address EEPROM address
+     * @param data data to be written
+     * @return Returns SUCCESS when succeeded, otherwise returns another.
+     */
+    Status writeEEPROM(char address, const char *data);
+
+    /** 
+     *  Reads data from EEPROM on the device. 
+     * @param address EEPROM address
+     * @param data data to read
+     * @return Returns SUCCESS when succeeded, otherwise returns another.
+     */
+    Status readEEPROM(char address, char *data);
+
+    /** 
+     * Writes data to register on the device. 
+     * @param address register address
+     * @param data data to be written
+     * @return Returns SUCCESS when succeeded, otherwise returns another.
+     */
+    Status writeRegister(char address, const char *data);
+
+    /** 
+     *  Reads data from register on the device. 
+     * @param address register address
+     * @param data data to read
+     * @return Returns SUCCESS when succeeded, otherwise returns another.
+     */
+    Status readRegister(char address, char *data);
+
+    /**
+     * Sets device operation mode.
+     *
+     * @param mode device opration mode
+     *
+     * @return Returns SUCCESS when succeeded, otherwise returns another code.
+     */
+    Status setOperationMode(OperationMode mode);
+
+    /**
+     * Reads angle data in [deg] from the device.
+     *
+     * @param angle Pointer
+     *
+     * @return Returns SUCCESS when succeeded, otherwise returns another code.
+     */
+    Status readAngle(float *angle);
+
+    /**
+     * Reads angle data from the device.
+     *
+     * @param angle pointer to read angle data buffer
+     *
+     * @return Returns SUCCESS when succeeded, otherwise returns another code.
+     */
+    Status readAngle(char *angle);
+    
+    /**
+     * Measures and reads angle, magnetic flux density and abnormal state code while in the user mode.
+     *
+     * @param angle pointer to angle data buffer
+     * @param density magnetic flux density
+     * @param abnormal_state abnormal state
+     *
+     * @return Returns SUCCESS when succeeded, otherwise returns another code.
+     */
+    Status readAngleMeasureCommand(char *angle, char *density, char *abnormal_state);
+    
+    /**
+     * Measures current angle and sets the value to EEPROM as zero angle position.
+     *
+     * @return Returns SUCCESS when succeeded, otherwise returns another code.
+     */
+    Status setAngleZero();
+    
+    /**
+     * Sets the value to EEPROM as zero angle position.
+     *
+     * @param angle zero angle position
+     *
+     * @return Returns SUCCESS when succeeded, otherwise returns another code.
+     */
+    Status setAngleZero(const char *angle);
+
+    /**
+     * Sets the value to EEPROM as zero angle position.
+     *
+     * @param angle zero angle position in degree
+     *
+     * @return Returns SUCCESS when succeeded, otherwise returns another code.
+     */
+    Status setAngleZero(const float angle);
+
+private:        
+    /**
+     * Holds a pointer to an SPI object. 
+     */
+    SPI *_spi;
+
+    /**
+     * Holds a DigitalOut oblject for CS; 
+     */
+    DigitalOut *_cs;
+    
+    /**
+     * Holds current mode 
+     */
+    OperationMode operationMode;
+
+    /** 
+     *  Reads data from device. 
+     * @param buf buffer to store the read data
+     * @param length bytes to be read
+     * @return Returns SUCCESS when succeeded, otherwise returns another.
+     */
+    Status read(char operation_code, char address, char *data);
+
+    /** 
+     * Writes data to the device. 
+     * @param data data to be written
+     * @param length of the data
+     * @return Returns SUCCESS when succeeded, otherwise returns another.
+     */
+    Status write(char operation_code, char address, const char *data);
+
+    /** 
+     * Checks parity bits sub function
+     * @param data data 12bit read data
+     * @param parity parity bit status
+     * @param error error bit status
+     * @return Returns SUCCESS when succeeded, otherwise returns another.
+     */
+    Status parityCheckSub(const char data, const char parity, const char error);
+
+    /** 
+     * Checks parity bits
+     * @param data 2 byte read data to chek parity
+     * @return Returns SUCCESS when succeeded, otherwise returns another.
+     */
+    Status parityCheck(const char *data);
+};
+
+#endif
\ No newline at end of file