AS5048A SPI - Magnetic rotary encoder library

Library for communication over the SPI interface with the ams.com AS5048A magnetic rotary encoder.

Example:

Simple readout of a single angle measurement from a single sensor on the SPI-bus

#include "mbed.h"
#include <as5048spi.h>

// The sensors connection are attached to pins 5-8
As5048Spi sensor(p5, p6, p7, p8);
Serial pc(USBTX, USBRX); // tx, rx

int main() {
    while(1) {
        // 
        const int* angles = sensor.read_angle();
        int angle = angles[0];
        
        // The read angle returns the value returned over the SPI bus, including parity bit
        pc.printf("Read result: %x\r\n", angle);
        
        if( As5048Spi::parity_check(angle) )
        {
            // Convert range from 0 to 2^14-1 to 0 - 360 degrees
            int degrees = As5048Spi::degrees(angle)/100;
            pc.printf("Parity check succesfull.\r\n");
            pc.printf("Angle: %i degrees\r\n", degrees );
        }
        else
        {
            pc.printf("Parity check failed.\r\n");
        }
            
        wait_ms(500);
    }
}

The class supports daisy chaining multiple sensors on the SPI-bus as well. See: SPI-Daisy-Chaining.

Committer:
JSpikker
Date:
Tue Mar 17 14:56:30 2015 +0000
Revision:
4:06b89a41109e
Parent:
3:a8ad32e439d4
Fixed documentation.

Who changed what in which revision?

UserRevisionLine numberNew 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 4:06b89a41109e 24 //! Class for interfacing with the AMS AS5048A magnetic rotary sensor over the SPI-interface.
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 0:3edcf58e51e7 49 const int* read_angle();
JSpikker 2:2958500883e0 50
JSpikker 2:2958500883e0 51 /// Performs sequential angle measurements on all sensors. The first time this
JSpikker 2:2958500883e0 52 /// method is called the result is not usefull, the measurement data of the call
JSpikker 2:2958500883e0 53 /// will be returned by the next call to this method.
JSpikker 2:2958500883e0 54 /// @return Array of raw angle data. To get the 14-bit value representing
JSpikker 2:2958500883e0 55 /// the angle, apply the mask() to the result.
JSpikker 0:3edcf58e51e7 56 const int* read_angle_sequential();
JSpikker 0:3edcf58e51e7 57
JSpikker 2:2958500883e0 58 /// Returns lowest 14-bits
JSpikker 0:3edcf58e51e7 59 static int mask(int sensor_result);
JSpikker 3:a8ad32e439d4 60
JSpikker 3:a8ad32e439d4 61 /// Applies the mask to the first n bytes in the read buffer (for daisychained sensors).
JSpikker 2:2958500883e0 62 static void mask(int* sensor_results, int n);
JSpikker 4:06b89a41109e 63
JSpikker 2:2958500883e0 64 /// Checks if the return value from the sensor has the right parity
JSpikker 2:2958500883e0 65 /// @return true if ok
JSpikker 0:3edcf58e51e7 66 static bool parity_check(int sensor_result);
JSpikker 3:a8ad32e439d4 67
JSpikker 3:a8ad32e439d4 68 /// Returns an angle from 0 to 36000 (degrees times 100).
JSpikker 3:a8ad32e439d4 69 /// @param sensor_result is one of the values returned by read_angle or read_angle_sequential
JSpikker 0:3edcf58e51e7 70 static int degrees(int sensor_result);
JSpikker 3:a8ad32e439d4 71
JSpikker 3:a8ad32e439d4 72 /// Returns an angle from 0 to 2*PI*100
JSpikker 3:a8ad32e439d4 73 /// @param sensor_result is one of the values returned by read_angle or read_angle_sequential
JSpikker 0:3edcf58e51e7 74 static int radian(int sensor_result);
JSpikker 0:3edcf58e51e7 75
JSpikker 0:3edcf58e51e7 76
JSpikker 0:3edcf58e51e7 77 protected:
JSpikker 0:3edcf58e51e7 78 int _nDevices;
JSpikker 0:3edcf58e51e7 79 DigitalOut _chipSelectN;
JSpikker 0:3edcf58e51e7 80 SPI _spi;
JSpikker 0:3edcf58e51e7 81
JSpikker 0:3edcf58e51e7 82 int* _readBuffer; // Stores the results of the last sequential read
JSpikker 0:3edcf58e51e7 83
JSpikker 0:3edcf58e51e7 84 int* _read(As5048Command command);
JSpikker 0:3edcf58e51e7 85 };
JSpikker 0:3edcf58e51e7 86