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