Alexandre Pirotte
/
AS5048
cc
Fork of AS5048 by
as5048spi.cpp@1:f2ed22514649, 2014-09-18 (annotated)
- Committer:
- JSpikker
- Date:
- Thu Sep 18 19:28:25 2014 +0000
- Revision:
- 1:f2ed22514649
- Parent:
- 0:3edcf58e51e7
- Child:
- 2:2958500883e0
Default SPI clock frequency is now 1MHz
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 | 0:3edcf58e51e7 | 62 | bool As5048Spi::parity_check(int sensor_result) |
JSpikker | 0:3edcf58e51e7 | 63 | { |
JSpikker | 0:3edcf58e51e7 | 64 | // Use the LSb of result to keep track of parity (0 = even, 1 = odd) |
JSpikker | 0:3edcf58e51e7 | 65 | int result = sensor_result; |
JSpikker | 0:3edcf58e51e7 | 66 | |
JSpikker | 0:3edcf58e51e7 | 67 | for(int i = 1; i <= 15; ++i) { |
JSpikker | 0:3edcf58e51e7 | 68 | sensor_result >>= 1; |
JSpikker | 0:3edcf58e51e7 | 69 | result ^= sensor_result; |
JSpikker | 0:3edcf58e51e7 | 70 | } |
JSpikker | 0:3edcf58e51e7 | 71 | // Parity should be even |
JSpikker | 0:3edcf58e51e7 | 72 | return (result & 0x0001) == 0; |
JSpikker | 0:3edcf58e51e7 | 73 | } |
JSpikker | 0:3edcf58e51e7 | 74 | |
JSpikker | 0:3edcf58e51e7 | 75 | const int* As5048Spi::read(As5048Command command) |
JSpikker | 0:3edcf58e51e7 | 76 | { |
JSpikker | 0:3edcf58e51e7 | 77 | _read(command); // Send command to device(s) |
JSpikker | 0:3edcf58e51e7 | 78 | return _read(AS_CMD_NOP); // Read-out device(s) |
JSpikker | 0:3edcf58e51e7 | 79 | } |
JSpikker | 0:3edcf58e51e7 | 80 | |
JSpikker | 0:3edcf58e51e7 | 81 | const int* As5048Spi::read_sequential(As5048Command command) |
JSpikker | 0:3edcf58e51e7 | 82 | { |
JSpikker | 0:3edcf58e51e7 | 83 | return _read(command); |
JSpikker | 0:3edcf58e51e7 | 84 | } |
JSpikker | 0:3edcf58e51e7 | 85 | |
JSpikker | 0:3edcf58e51e7 | 86 | const int* As5048Spi::read_angle() |
JSpikker | 0:3edcf58e51e7 | 87 | { |
JSpikker | 0:3edcf58e51e7 | 88 | _read(AS_CMD_ANGLE); // Send command to device(s) |
JSpikker | 0:3edcf58e51e7 | 89 | return _read(AS_CMD_NOP); // Read-out device(s) |
JSpikker | 0:3edcf58e51e7 | 90 | } |
JSpikker | 0:3edcf58e51e7 | 91 | |
JSpikker | 0:3edcf58e51e7 | 92 | const int* As5048Spi::read_angle_sequential() |
JSpikker | 0:3edcf58e51e7 | 93 | { |
JSpikker | 0:3edcf58e51e7 | 94 | return _read(AS_CMD_ANGLE); |
JSpikker | 0:3edcf58e51e7 | 95 | } |
JSpikker | 0:3edcf58e51e7 | 96 | |
JSpikker | 0:3edcf58e51e7 | 97 | |
JSpikker | 0:3edcf58e51e7 | 98 | int* As5048Spi::_read(As5048Command command) |
JSpikker | 0:3edcf58e51e7 | 99 | { |
JSpikker | 0:3edcf58e51e7 | 100 | if(_nDevices == 1) |
JSpikker | 0:3edcf58e51e7 | 101 | { |
JSpikker | 0:3edcf58e51e7 | 102 | // Give command to start reading the angle |
JSpikker | 0:3edcf58e51e7 | 103 | _chipSelectN.write(0); |
JSpikker | 0:3edcf58e51e7 | 104 | wait_us(1); // Wait at least 350ns after chip select |
JSpikker | 0:3edcf58e51e7 | 105 | _readBuffer[0] = _spi.write(command); |
JSpikker | 0:3edcf58e51e7 | 106 | _chipSelectN.write(1); |
JSpikker | 1:f2ed22514649 | 107 | wait_us(1); // Wait at least 350ns after chip select |
JSpikker | 0:3edcf58e51e7 | 108 | } else |
JSpikker | 0:3edcf58e51e7 | 109 | { |
JSpikker | 0:3edcf58e51e7 | 110 | // Enable the sensor on the chain |
JSpikker | 0:3edcf58e51e7 | 111 | _chipSelectN.write(0); |
JSpikker | 0:3edcf58e51e7 | 112 | wait_us(1); // Wait at least 350ns after chip select |
JSpikker | 0:3edcf58e51e7 | 113 | for(int i = 0; i < _nDevices; ++i) |
JSpikker | 0:3edcf58e51e7 | 114 | { |
JSpikker | 0:3edcf58e51e7 | 115 | _readBuffer[i] = _spi.write(command); |
JSpikker | 0:3edcf58e51e7 | 116 | } |
JSpikker | 0:3edcf58e51e7 | 117 | _chipSelectN.write(1); |
JSpikker | 0:3edcf58e51e7 | 118 | wait_us(1); // Wait at least 350ns after chip select |
JSpikker | 0:3edcf58e51e7 | 119 | } |
JSpikker | 0:3edcf58e51e7 | 120 | return _readBuffer; |
JSpikker | 0:3edcf58e51e7 | 121 | } |
JSpikker | 0:3edcf58e51e7 | 122 | |
JSpikker | 0:3edcf58e51e7 | 123 | |
JSpikker | 0:3edcf58e51e7 | 124 | |
JSpikker | 0:3edcf58e51e7 | 125 | |
JSpikker | 0:3edcf58e51e7 | 126 | |
JSpikker | 0:3edcf58e51e7 | 127 |