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.
as5048spi.cpp@2:2958500883e0, 2014-12-04 (annotated)
- Committer:
- JSpikker
- Date:
- Thu Dec 04 10:00:42 2014 +0000
- Revision:
- 2:2958500883e0
- Parent:
- 1:f2ed22514649
Added burst read functionality
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JSpikker | 0:3edcf58e51e7 | 1 | #include "as5048spi.h" |
JSpikker | 0:3edcf58e51e7 | 2 | |
JSpikker | 0:3edcf58e51e7 | 3 | As5048Spi::As5048Spi(PinName mosi, PinName miso, PinName sclk, PinName chipselect, int ndevices) : |
JSpikker | 0:3edcf58e51e7 | 4 | _nDevices(ndevices), |
JSpikker | 0:3edcf58e51e7 | 5 | _chipSelectN(chipselect), |
JSpikker | 0:3edcf58e51e7 | 6 | _spi(mosi, miso, sclk) |
JSpikker | 0:3edcf58e51e7 | 7 | { |
JSpikker | 0:3edcf58e51e7 | 8 | _chipSelectN.write(1); |
JSpikker | 0:3edcf58e51e7 | 9 | // AS5048 needs 16-bits for is commands |
JSpikker | 0:3edcf58e51e7 | 10 | // Mode = 1: |
JSpikker | 0:3edcf58e51e7 | 11 | // clock polarity = 0 --> clock pulse is high |
JSpikker | 0:3edcf58e51e7 | 12 | // clock phase = 1 --> sample on falling edge of clock pulse |
JSpikker | 0:3edcf58e51e7 | 13 | _spi.format(16, 1); |
JSpikker | 0:3edcf58e51e7 | 14 | |
JSpikker | 0:3edcf58e51e7 | 15 | // Set clock frequency to 1 MHz (max is 10Mhz) |
JSpikker | 1:f2ed22514649 | 16 | _spi.frequency(1000000); |
JSpikker | 0:3edcf58e51e7 | 17 | |
JSpikker | 0:3edcf58e51e7 | 18 | _readBuffer = new int[ndevices]; |
JSpikker | 0:3edcf58e51e7 | 19 | } |
JSpikker | 0:3edcf58e51e7 | 20 | |
JSpikker | 0:3edcf58e51e7 | 21 | As5048Spi::~As5048Spi() |
JSpikker | 0:3edcf58e51e7 | 22 | { |
JSpikker | 0:3edcf58e51e7 | 23 | delete [] _readBuffer; |
JSpikker | 0:3edcf58e51e7 | 24 | } |
JSpikker | 0:3edcf58e51e7 | 25 | |
JSpikker | 0:3edcf58e51e7 | 26 | int As5048Spi::degrees(int sensor_result) |
JSpikker | 0:3edcf58e51e7 | 27 | { |
JSpikker | 0:3edcf58e51e7 | 28 | return mask(sensor_result) * 36000 / 0x4000; |
JSpikker | 0:3edcf58e51e7 | 29 | } |
JSpikker | 0:3edcf58e51e7 | 30 | |
JSpikker | 0:3edcf58e51e7 | 31 | |
JSpikker | 0:3edcf58e51e7 | 32 | int As5048Spi::radian(int sensor_result) |
JSpikker | 0:3edcf58e51e7 | 33 | { |
JSpikker | 0:3edcf58e51e7 | 34 | return mask(sensor_result) * 62832 / 0x4000; |
JSpikker | 0:3edcf58e51e7 | 35 | } |
JSpikker | 0:3edcf58e51e7 | 36 | |
JSpikker | 0:3edcf58e51e7 | 37 | bool As5048Spi::error(int device) |
JSpikker | 0:3edcf58e51e7 | 38 | { |
JSpikker | 0:3edcf58e51e7 | 39 | if( device == -1 ) { |
JSpikker | 0:3edcf58e51e7 | 40 | for(int i = 0; i < _nDevices; ++i) { |
JSpikker | 0:3edcf58e51e7 | 41 | if( _readBuffer[i] & 0x4000 ) { |
JSpikker | 0:3edcf58e51e7 | 42 | return true; |
JSpikker | 0:3edcf58e51e7 | 43 | } |
JSpikker | 0:3edcf58e51e7 | 44 | } |
JSpikker | 0:3edcf58e51e7 | 45 | } else if( device < _nDevices ) { |
JSpikker | 0:3edcf58e51e7 | 46 | return (_readBuffer[device] & 0x4000) == 0x4000; |
JSpikker | 0:3edcf58e51e7 | 47 | } |
JSpikker | 0:3edcf58e51e7 | 48 | return false; |
JSpikker | 0:3edcf58e51e7 | 49 | } |
JSpikker | 0:3edcf58e51e7 | 50 | |
JSpikker | 0:3edcf58e51e7 | 51 | |
JSpikker | 0:3edcf58e51e7 | 52 | void As5048Spi::frequency(int hz) |
JSpikker | 0:3edcf58e51e7 | 53 | { |
JSpikker | 0:3edcf58e51e7 | 54 | _spi.frequency(hz); |
JSpikker | 0:3edcf58e51e7 | 55 | } |
JSpikker | 0:3edcf58e51e7 | 56 | |
JSpikker | 0:3edcf58e51e7 | 57 | int As5048Spi::mask(int sensor_result) |
JSpikker | 0:3edcf58e51e7 | 58 | { |
JSpikker | 0:3edcf58e51e7 | 59 | return sensor_result & 0x3FFF; // return lowest 14-bits |
JSpikker | 0:3edcf58e51e7 | 60 | } |
JSpikker | 0:3edcf58e51e7 | 61 | |
JSpikker | 2:2958500883e0 | 62 | |
JSpikker | 2:2958500883e0 | 63 | void As5048Spi::mask(int* sensor_results, int n) |
JSpikker | 2:2958500883e0 | 64 | { |
JSpikker | 2:2958500883e0 | 65 | for(int i = 0; i < n; ++i) { |
JSpikker | 2:2958500883e0 | 66 | sensor_results[i] &= 0x3FFF; |
JSpikker | 2:2958500883e0 | 67 | } |
JSpikker | 2:2958500883e0 | 68 | } |
JSpikker | 2:2958500883e0 | 69 | |
JSpikker | 2:2958500883e0 | 70 | |
JSpikker | 0:3edcf58e51e7 | 71 | bool As5048Spi::parity_check(int sensor_result) |
JSpikker | 0:3edcf58e51e7 | 72 | { |
JSpikker | 0:3edcf58e51e7 | 73 | // Use the LSb of result to keep track of parity (0 = even, 1 = odd) |
JSpikker | 0:3edcf58e51e7 | 74 | int result = sensor_result; |
JSpikker | 0:3edcf58e51e7 | 75 | |
JSpikker | 0:3edcf58e51e7 | 76 | for(int i = 1; i <= 15; ++i) { |
JSpikker | 0:3edcf58e51e7 | 77 | sensor_result >>= 1; |
JSpikker | 0:3edcf58e51e7 | 78 | result ^= sensor_result; |
JSpikker | 0:3edcf58e51e7 | 79 | } |
JSpikker | 0:3edcf58e51e7 | 80 | // Parity should be even |
JSpikker | 0:3edcf58e51e7 | 81 | return (result & 0x0001) == 0; |
JSpikker | 0:3edcf58e51e7 | 82 | } |
JSpikker | 0:3edcf58e51e7 | 83 | |
JSpikker | 0:3edcf58e51e7 | 84 | const int* As5048Spi::read(As5048Command command) |
JSpikker | 0:3edcf58e51e7 | 85 | { |
JSpikker | 0:3edcf58e51e7 | 86 | _read(command); // Send command to device(s) |
JSpikker | 0:3edcf58e51e7 | 87 | return _read(AS_CMD_NOP); // Read-out device(s) |
JSpikker | 0:3edcf58e51e7 | 88 | } |
JSpikker | 0:3edcf58e51e7 | 89 | |
JSpikker | 0:3edcf58e51e7 | 90 | const int* As5048Spi::read_sequential(As5048Command command) |
JSpikker | 0:3edcf58e51e7 | 91 | { |
JSpikker | 0:3edcf58e51e7 | 92 | return _read(command); |
JSpikker | 0:3edcf58e51e7 | 93 | } |
JSpikker | 0:3edcf58e51e7 | 94 | |
JSpikker | 0:3edcf58e51e7 | 95 | const int* As5048Spi::read_angle() |
JSpikker | 0:3edcf58e51e7 | 96 | { |
JSpikker | 0:3edcf58e51e7 | 97 | _read(AS_CMD_ANGLE); // Send command to device(s) |
JSpikker | 0:3edcf58e51e7 | 98 | return _read(AS_CMD_NOP); // Read-out device(s) |
JSpikker | 0:3edcf58e51e7 | 99 | } |
JSpikker | 0:3edcf58e51e7 | 100 | |
JSpikker | 0:3edcf58e51e7 | 101 | const int* As5048Spi::read_angle_sequential() |
JSpikker | 0:3edcf58e51e7 | 102 | { |
JSpikker | 0:3edcf58e51e7 | 103 | return _read(AS_CMD_ANGLE); |
JSpikker | 0:3edcf58e51e7 | 104 | } |
JSpikker | 0:3edcf58e51e7 | 105 | |
JSpikker | 0:3edcf58e51e7 | 106 | |
JSpikker | 0:3edcf58e51e7 | 107 | int* As5048Spi::_read(As5048Command command) |
JSpikker | 0:3edcf58e51e7 | 108 | { |
JSpikker | 0:3edcf58e51e7 | 109 | if(_nDevices == 1) |
JSpikker | 0:3edcf58e51e7 | 110 | { |
JSpikker | 0:3edcf58e51e7 | 111 | // Give command to start reading the angle |
JSpikker | 0:3edcf58e51e7 | 112 | _chipSelectN.write(0); |
JSpikker | 0:3edcf58e51e7 | 113 | wait_us(1); // Wait at least 350ns after chip select |
JSpikker | 0:3edcf58e51e7 | 114 | _readBuffer[0] = _spi.write(command); |
JSpikker | 0:3edcf58e51e7 | 115 | _chipSelectN.write(1); |
JSpikker | 1:f2ed22514649 | 116 | wait_us(1); // Wait at least 350ns after chip select |
JSpikker | 0:3edcf58e51e7 | 117 | } else |
JSpikker | 0:3edcf58e51e7 | 118 | { |
JSpikker | 0:3edcf58e51e7 | 119 | // Enable the sensor on the chain |
JSpikker | 0:3edcf58e51e7 | 120 | _chipSelectN.write(0); |
JSpikker | 0:3edcf58e51e7 | 121 | wait_us(1); // Wait at least 350ns after chip select |
JSpikker | 0:3edcf58e51e7 | 122 | for(int i = 0; i < _nDevices; ++i) |
JSpikker | 0:3edcf58e51e7 | 123 | { |
JSpikker | 0:3edcf58e51e7 | 124 | _readBuffer[i] = _spi.write(command); |
JSpikker | 0:3edcf58e51e7 | 125 | } |
JSpikker | 0:3edcf58e51e7 | 126 | _chipSelectN.write(1); |
JSpikker | 0:3edcf58e51e7 | 127 | wait_us(1); // Wait at least 350ns after chip select |
JSpikker | 0:3edcf58e51e7 | 128 | } |
JSpikker | 0:3edcf58e51e7 | 129 | return _readBuffer; |
JSpikker | 0:3edcf58e51e7 | 130 | } |
JSpikker | 0:3edcf58e51e7 | 131 | |
JSpikker | 0:3edcf58e51e7 | 132 | |
JSpikker | 0:3edcf58e51e7 | 133 | |
JSpikker | 0:3edcf58e51e7 | 134 | |
JSpikker | 0:3edcf58e51e7 | 135 | |
JSpikker | 0:3edcf58e51e7 | 136 |