Alexandre Pirotte
/
AS5048
cc
Fork of AS5048 by
as5048spi.h@3:a8ad32e439d4, 2015-03-17 (annotated)
- Committer:
- JSpikker
- Date:
- Tue Mar 17 14:46:45 2015 +0000
- Revision:
- 3:a8ad32e439d4
- Parent:
- 2:2958500883e0
- Child:
- 4:06b89a41109e
Added documentation strings.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JSpikker | 0:3edcf58e51e7 | 1 | #include "mbed.h" |
JSpikker | 0:3edcf58e51e7 | 2 | typedef enum { |
JSpikker | 0:3edcf58e51e7 | 3 | AS_FLAG_PARITY = 0x8000, |
JSpikker | 0:3edcf58e51e7 | 4 | AS_FLAG_READ = 0x4000, |
JSpikker | 0:3edcf58e51e7 | 5 | } As5048Flag; |
JSpikker | 0:3edcf58e51e7 | 6 | |
JSpikker | 0:3edcf58e51e7 | 7 | typedef enum { |
JSpikker | 0:3edcf58e51e7 | 8 | AS_CMD_NOP = 0x0000, |
JSpikker | 3:a8ad32e439d4 | 9 | AS_CMD_ERROR = 0x0001 | AS_FLAG_READ, // Reads error register of sensor and clear error flags |
JSpikker | 0:3edcf58e51e7 | 10 | AS_CMD_DIAGNOSTICS = 0x3FFD | AS_FLAG_READ, // Reads automatic gain control and diagnostics info |
JSpikker | 0:3edcf58e51e7 | 11 | AS_CMD_MAGNITUDE = 0x3FFE | AS_FLAG_READ, |
JSpikker | 0:3edcf58e51e7 | 12 | |
JSpikker | 0:3edcf58e51e7 | 13 | AS_CMD_ANGLE = 0x3FFF| AS_FLAG_PARITY | AS_FLAG_READ, |
JSpikker | 0:3edcf58e51e7 | 14 | } As5048Command; |
JSpikker | 0:3edcf58e51e7 | 15 | |
JSpikker | 2:2958500883e0 | 16 | // Masks for bits in the result of the AS_CMD_DIAGNOSTICS command |
JSpikker | 2:2958500883e0 | 17 | typedef enum { |
JSpikker | 2:2958500883e0 | 18 | AS_DIAG_CORDIC_OVERFLOW = 0x0200, |
JSpikker | 2:2958500883e0 | 19 | AS_DIAG_HIGH_MAGNETIC = 0x0400, |
JSpikker | 2:2958500883e0 | 20 | AS_DIAG_LOW_MAGNETIC = 0x0800, |
JSpikker | 2:2958500883e0 | 21 | } As5048Diagnostics; |
JSpikker | 2:2958500883e0 | 22 | |
JSpikker | 0:3edcf58e51e7 | 23 | |
JSpikker | 0:3edcf58e51e7 | 24 | |
JSpikker | 0:3edcf58e51e7 | 25 | class As5048Spi |
JSpikker | 0:3edcf58e51e7 | 26 | { |
JSpikker | 0:3edcf58e51e7 | 27 | public: |
JSpikker | 0:3edcf58e51e7 | 28 | As5048Spi(PinName mosi, PinName miso, PinName sclk, PinName chipselect, int nDevices = 1); |
JSpikker | 0:3edcf58e51e7 | 29 | ~As5048Spi(); |
JSpikker | 0:3edcf58e51e7 | 30 | |
JSpikker | 0:3edcf58e51e7 | 31 | bool error(int device = -1); |
JSpikker | 3:a8ad32e439d4 | 32 | |
JSpikker | 3:a8ad32e439d4 | 33 | /// Sets the SPI clock frequency in Hz. Maximum tested frequency is 10MHz. |
JSpikker | 0:3edcf58e51e7 | 34 | void frequency(int frequency = 1000000); |
JSpikker | 3:a8ad32e439d4 | 35 | |
JSpikker | 3:a8ad32e439d4 | 36 | /// Sends a read command to the sensor. |
JSpikker | 0:3edcf58e51e7 | 37 | const int* read(As5048Command command); |
JSpikker | 3:a8ad32e439d4 | 38 | |
JSpikker | 3:a8ad32e439d4 | 39 | /// Sends a read command to the sensor. |
JSpikker | 3:a8ad32e439d4 | 40 | /// A call to this function will not directly return the requested value. The |
JSpikker | 3:a8ad32e439d4 | 41 | /// requested value will be returned in a next read_sequential call. |
JSpikker | 3:a8ad32e439d4 | 42 | /// Use this function to read sensor values with minimum speed impact on SPI-bus |
JSpikker | 3:a8ad32e439d4 | 43 | /// and microcontroller. |
JSpikker | 0:3edcf58e51e7 | 44 | const int* read_sequential(As5048Command command); |
JSpikker | 0:3edcf58e51e7 | 45 | |
JSpikker | 2:2958500883e0 | 46 | /// Performs a single angle measurement on all sensors |
JSpikker | 2:2958500883e0 | 47 | /// @return Array of raw angle data. To get the 14-bit value representing |
JSpikker | 2:2958500883e0 | 48 | /// the angle, apply the mask() to the result. |
JSpikker | 2:2958500883e0 | 49 | /// To get an angle in degrees divide 360 by the result of the call to mask(). |
JSpikker | 0:3edcf58e51e7 | 50 | const int* read_angle(); |
JSpikker | 2:2958500883e0 | 51 | |
JSpikker | 2:2958500883e0 | 52 | /// Performs sequential angle measurements on all sensors. The first time this |
JSpikker | 2:2958500883e0 | 53 | /// method is called the result is not usefull, the measurement data of the call |
JSpikker | 2:2958500883e0 | 54 | /// will be returned by the next call to this method. |
JSpikker | 2:2958500883e0 | 55 | /// @return Array of raw angle data. To get the 14-bit value representing |
JSpikker | 2:2958500883e0 | 56 | /// the angle, apply the mask() to the result. |
JSpikker | 2:2958500883e0 | 57 | /// To get an angle in degrees divide 360 by the result of the call to mask(). |
JSpikker | 0:3edcf58e51e7 | 58 | const int* read_angle_sequential(); |
JSpikker | 0:3edcf58e51e7 | 59 | |
JSpikker | 2:2958500883e0 | 60 | /// Returns lowest 14-bits |
JSpikker | 0:3edcf58e51e7 | 61 | static int mask(int sensor_result); |
JSpikker | 3:a8ad32e439d4 | 62 | |
JSpikker | 3:a8ad32e439d4 | 63 | /// Applies the mask to the first n bytes in the read buffer (for daisychained sensors). |
JSpikker | 2:2958500883e0 | 64 | static void mask(int* sensor_results, int n); |
JSpikker | 2:2958500883e0 | 65 | /// Checks if the return value from the sensor has the right parity |
JSpikker | 2:2958500883e0 | 66 | /// @return true if ok |
JSpikker | 0:3edcf58e51e7 | 67 | static bool parity_check(int sensor_result); |
JSpikker | 3:a8ad32e439d4 | 68 | |
JSpikker | 3:a8ad32e439d4 | 69 | /// Returns an angle from 0 to 36000 (degrees times 100). |
JSpikker | 3:a8ad32e439d4 | 70 | /// @param sensor_result is one of the values returned by read_angle or read_angle_sequential |
JSpikker | 0:3edcf58e51e7 | 71 | static int degrees(int sensor_result); |
JSpikker | 3:a8ad32e439d4 | 72 | |
JSpikker | 3:a8ad32e439d4 | 73 | /// Returns an angle from 0 to 2*PI*100 |
JSpikker | 3:a8ad32e439d4 | 74 | /// @param sensor_result is one of the values returned by read_angle or read_angle_sequential |
JSpikker | 0:3edcf58e51e7 | 75 | static int radian(int sensor_result); |
JSpikker | 0:3edcf58e51e7 | 76 | |
JSpikker | 0:3edcf58e51e7 | 77 | |
JSpikker | 0:3edcf58e51e7 | 78 | protected: |
JSpikker | 0:3edcf58e51e7 | 79 | int _nDevices; |
JSpikker | 0:3edcf58e51e7 | 80 | DigitalOut _chipSelectN; |
JSpikker | 0:3edcf58e51e7 | 81 | SPI _spi; |
JSpikker | 0:3edcf58e51e7 | 82 | |
JSpikker | 0:3edcf58e51e7 | 83 | int* _readBuffer; // Stores the results of the last sequential read |
JSpikker | 0:3edcf58e51e7 | 84 | |
JSpikker | 0:3edcf58e51e7 | 85 | int* _read(As5048Command command); |
JSpikker | 0:3edcf58e51e7 | 86 | }; |
JSpikker | 0:3edcf58e51e7 | 87 |