Saxion Lectoraat MT / AS5048
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers as5048spi.cpp Source File

as5048spi.cpp

00001 #include "as5048spi.h"
00002 
00003 As5048Spi::As5048Spi(PinName mosi, PinName miso, PinName sclk, PinName chipselect, int ndevices) :
00004     _nDevices(ndevices),
00005     _chipSelectN(chipselect),
00006     _spi(mosi, miso, sclk)
00007 {
00008     _chipSelectN.write(1);
00009     // AS5048 needs 16-bits for is commands
00010     // Mode = 1: 
00011     //  clock polarity = 0 --> clock pulse is high
00012     //  clock phase = 1 --> sample on falling edge of clock pulse
00013     _spi.format(16, 1);
00014     
00015     // Set clock frequency to 1 MHz (max is 10Mhz)
00016     _spi.frequency(1000000);
00017     
00018     _readBuffer = new int[ndevices];
00019 }
00020 
00021 As5048Spi::~As5048Spi()
00022 {
00023     delete [] _readBuffer;
00024 }
00025 
00026 int As5048Spi::degrees(int sensor_result)
00027 {
00028     return mask(sensor_result) * 36000 / 0x4000;
00029 }
00030 
00031 
00032 int As5048Spi::radian(int sensor_result)
00033 {
00034     return mask(sensor_result) * 62832 / 0x4000;
00035 }
00036 
00037 bool As5048Spi::error(int device)
00038 {
00039     if( device == -1 ) {
00040         for(int i = 0; i < _nDevices; ++i) {
00041             if( _readBuffer[i] & 0x4000 ) {
00042                 return true;
00043             }
00044         }
00045     } else if( device < _nDevices ) {
00046         return (_readBuffer[device] & 0x4000) == 0x4000;
00047     }
00048     return false; 
00049 }
00050 
00051 
00052 void As5048Spi::frequency(int hz) 
00053 {
00054     _spi.frequency(hz);
00055 }
00056 
00057 int As5048Spi::mask(int sensor_result)
00058 {
00059     return sensor_result & 0x3FFF; // return lowest 14-bits
00060 }
00061 
00062 
00063 void As5048Spi::mask(int* sensor_results, int n)
00064 {
00065     for(int i = 0; i < n; ++i) {
00066         sensor_results[i] &= 0x3FFF;
00067     }
00068 }
00069 
00070 
00071 bool As5048Spi::parity_check(int sensor_result)
00072 {
00073     // Use the LSb of result to keep track of parity (0 = even, 1 = odd)
00074     int result = sensor_result;
00075     
00076     for(int i = 1; i <= 15; ++i) {
00077         sensor_result >>= 1;
00078         result ^= sensor_result;
00079     }
00080     // Parity should be even
00081     return (result & 0x0001) == 0;
00082 }
00083 
00084 const int* As5048Spi::read(As5048Command command)
00085 {
00086     _read(command); // Send command to device(s)
00087     return _read(AS_CMD_NOP); // Read-out device(s)
00088 }
00089 
00090 const int*  As5048Spi::read_sequential(As5048Command command)
00091 {
00092     return _read(command);
00093 }
00094 
00095 const int*  As5048Spi::read_angle()
00096 {  
00097     _read(AS_CMD_ANGLE); // Send command to device(s)
00098     return _read(AS_CMD_NOP); // Read-out device(s)
00099 }
00100 
00101 const int*  As5048Spi::read_angle_sequential()
00102 {
00103     return _read(AS_CMD_ANGLE); 
00104 }
00105 
00106 
00107 int* As5048Spi::_read(As5048Command command)
00108 {
00109     if(_nDevices == 1)
00110     {
00111         // Give command to start reading the angle
00112         _chipSelectN.write(0);
00113         wait_us(1); // Wait at least 350ns after chip select
00114         _readBuffer[0] = _spi.write(command);
00115         _chipSelectN.write(1);
00116         wait_us(1); // Wait at least 350ns after chip select       
00117     } else
00118     {
00119         // Enable the sensor on the chain
00120         _chipSelectN.write(0);
00121          wait_us(1); // Wait at least 350ns after chip select
00122         for(int i = 0; i < _nDevices; ++i)
00123         {
00124             _readBuffer[i] = _spi.write(command);
00125         }
00126         _chipSelectN.write(1);
00127         wait_us(1); // Wait at least 350ns after chip select
00128     }
00129     return _readBuffer;
00130 }
00131 
00132 
00133 
00134 
00135 
00136