Saxion Lectoraat MT / AS5048
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers as5048spi.h Source File

as5048spi.h

00001 #include "mbed.h"
00002 typedef enum {
00003     AS_FLAG_PARITY = 0x8000,
00004     AS_FLAG_READ = 0x4000,
00005 } As5048Flag;
00006 
00007 typedef enum {
00008     AS_CMD_NOP = 0x0000,
00009     AS_CMD_ERROR = 0x0001 | AS_FLAG_READ,       // Reads error register of sensor and clear error flags
00010     AS_CMD_DIAGNOSTICS = 0x3FFD |  AS_FLAG_READ, // Reads automatic gain control and diagnostics info
00011     AS_CMD_MAGNITUDE = 0x3FFE | AS_FLAG_READ,
00012     
00013     AS_CMD_ANGLE = 0x3FFF| AS_FLAG_PARITY | AS_FLAG_READ,
00014 } As5048Command;
00015 
00016 // Masks for bits in the result of the AS_CMD_DIAGNOSTICS command
00017 typedef enum {
00018     AS_DIAG_CORDIC_OVERFLOW = 0x0200,
00019     AS_DIAG_HIGH_MAGNETIC = 0x0400,
00020     AS_DIAG_LOW_MAGNETIC = 0x0800,
00021 } As5048Diagnostics;
00022 
00023 
00024 //! Class for interfacing with the AMS AS5048A magnetic rotary sensor over the SPI-interface.
00025 class As5048Spi
00026 {
00027 public:
00028     As5048Spi(PinName mosi, PinName miso, PinName sclk, PinName chipselect, int nDevices = 1);
00029     ~As5048Spi();
00030     
00031     bool error(int device = -1);
00032     
00033     /// Sets the SPI clock frequency in Hz. Maximum tested frequency is 10MHz.
00034     void frequency(int frequency = 1000000);
00035     
00036     /// Sends a read command to the sensor.
00037     const int* read(As5048Command command);
00038     
00039     /// Sends a read command to the sensor. 
00040     /// A call to this function will not directly return the requested value. The
00041     /// requested value will be returned in a next read_sequential call. 
00042     /// Use this function to read sensor values with minimum speed impact on SPI-bus
00043     /// and microcontroller.
00044     const int* read_sequential(As5048Command command);
00045     
00046     /// Performs a single angle measurement on all sensors
00047     /// @return Array of raw angle data. To get the 14-bit value representing
00048     ///     the angle, apply the mask() to the result.
00049     const int* read_angle();
00050     
00051     /// Performs sequential angle measurements on all sensors. The first time this
00052     /// method is called the result is not usefull, the measurement data of the call
00053     /// will be returned by the next call to this method.
00054     /// @return Array of raw angle data. To get the 14-bit value representing
00055     ///     the angle, apply the mask() to the result.
00056     const int* read_angle_sequential();
00057 
00058     /// Returns lowest 14-bits
00059     static int mask(int sensor_result);
00060 
00061     /// Applies the mask to the first n bytes in the read buffer (for daisychained sensors).
00062     static void mask(int* sensor_results, int n);
00063     
00064     /// Checks if the return value from the sensor has the right parity
00065     /// @return true if ok
00066     static bool parity_check(int sensor_result);
00067     
00068     /// Returns an angle from 0 to 36000 (degrees times 100).
00069     /// @param sensor_result is one of the values returned by read_angle or read_angle_sequential
00070     static int degrees(int sensor_result);
00071     
00072     /// Returns an angle from 0 to 2*PI*100 
00073     /// @param sensor_result is one of the values returned by read_angle or read_angle_sequential
00074     static int radian(int sensor_result);
00075     
00076 
00077 protected:
00078     int _nDevices;
00079     DigitalOut _chipSelectN;
00080     SPI _spi;
00081     
00082     int* _readBuffer; // Stores the results of the last sequential read
00083     
00084     int* _read(As5048Command command);
00085 };
00086