Library for TI's DRV2667
DRV2667.cpp@0:faa5d89e9dac, 2018-02-07 (annotated)
- Committer:
- takuhachisu
- Date:
- Wed Feb 07 09:20:34 2018 +0000
- Revision:
- 0:faa5d89e9dac
- Child:
- 1:a57042b30965
Library for DRV2667
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takuhachisu | 0:faa5d89e9dac | 1 | #include "DRV2667.h" |
takuhachisu | 0:faa5d89e9dac | 2 | |
takuhachisu | 0:faa5d89e9dac | 3 | DRV2667::DRV2667(I2C &i2c, InputMux im, Gain gn, Timeout to) |
takuhachisu | 0:faa5d89e9dac | 4 | { |
takuhachisu | 0:faa5d89e9dac | 5 | _i2c = &i2c; |
takuhachisu | 0:faa5d89e9dac | 6 | wait_ms(1); // Wait for 1 ms for the DRV2667 device to power-up before attempting an I2C write |
takuhachisu | 0:faa5d89e9dac | 7 | i2cWriteByte(0x02, 0x00); // Exit low-power standby mode |
takuhachisu | 0:faa5d89e9dac | 8 | |
takuhachisu | 0:faa5d89e9dac | 9 | if(im != Digital || gn != GNx1) { |
takuhachisu | 0:faa5d89e9dac | 10 | char temp = (i2cReadByte(0x01) | im) | gn; |
takuhachisu | 0:faa5d89e9dac | 11 | i2cWriteByte(0x01, temp); |
takuhachisu | 0:faa5d89e9dac | 12 | } |
takuhachisu | 0:faa5d89e9dac | 13 | |
takuhachisu | 0:faa5d89e9dac | 14 | if(to != TOx1 || im != Digital) { |
takuhachisu | 0:faa5d89e9dac | 15 | char temp = (i2cReadByte(0x02) | to) | (im>>1); |
takuhachisu | 0:faa5d89e9dac | 16 | i2cWriteByte(0x02, temp); |
takuhachisu | 0:faa5d89e9dac | 17 | } |
takuhachisu | 0:faa5d89e9dac | 18 | } |
takuhachisu | 0:faa5d89e9dac | 19 | |
takuhachisu | 0:faa5d89e9dac | 20 | void DRV2667::i2cWriteByte(char reg, char value) |
takuhachisu | 0:faa5d89e9dac | 21 | { |
takuhachisu | 0:faa5d89e9dac | 22 | char buff[2] = {reg, value}; |
takuhachisu | 0:faa5d89e9dac | 23 | _i2c->write(SLAVE_ADDR_7_BIT<<1, buff, 2); |
takuhachisu | 0:faa5d89e9dac | 24 | } |
takuhachisu | 0:faa5d89e9dac | 25 | |
takuhachisu | 0:faa5d89e9dac | 26 | uint8_t DRV2667::i2cReadByte(char reg) |
takuhachisu | 0:faa5d89e9dac | 27 | { |
takuhachisu | 0:faa5d89e9dac | 28 | char result; // Temp result storage |
takuhachisu | 0:faa5d89e9dac | 29 | _i2c->write(SLAVE_ADDR_7_BIT<<1, ®, 1, true); |
takuhachisu | 0:faa5d89e9dac | 30 | _i2c->read(SLAVE_ADDR_7_BIT<<1, &result, 1); |
takuhachisu | 0:faa5d89e9dac | 31 | |
takuhachisu | 0:faa5d89e9dac | 32 | return result; |
takuhachisu | 0:faa5d89e9dac | 33 | } |
takuhachisu | 0:faa5d89e9dac | 34 | |
takuhachisu | 0:faa5d89e9dac | 35 | void DRV2667::play() |
takuhachisu | 0:faa5d89e9dac | 36 | { |
takuhachisu | 0:faa5d89e9dac | 37 | i2cWriteByte(0x02, i2cReadByte(0x02) | 1); |
takuhachisu | 0:faa5d89e9dac | 38 | } |
takuhachisu | 0:faa5d89e9dac | 39 | |
takuhachisu | 0:faa5d89e9dac | 40 | void DRV2667::stop() |
takuhachisu | 0:faa5d89e9dac | 41 | { |
takuhachisu | 0:faa5d89e9dac | 42 | i2cWriteByte(0x02, i2cReadByte(0x02) & ~1); |
takuhachisu | 0:faa5d89e9dac | 43 | } |
takuhachisu | 0:faa5d89e9dac | 44 | |
takuhachisu | 0:faa5d89e9dac | 45 | void DRV2667::setWaveform(char* id, char setNum) |
takuhachisu | 0:faa5d89e9dac | 46 | { |
takuhachisu | 0:faa5d89e9dac | 47 | for(int i = 0; i < setNum && i < 8; i++) |
takuhachisu | 0:faa5d89e9dac | 48 | i2cWriteByte(0x03 + i, id[i]); |
takuhachisu | 0:faa5d89e9dac | 49 | if(setNum < 7) |
takuhachisu | 0:faa5d89e9dac | 50 | i2cWriteByte(0x03 + setNum, 0); |
takuhachisu | 0:faa5d89e9dac | 51 | } |
takuhachisu | 0:faa5d89e9dac | 52 | |
takuhachisu | 0:faa5d89e9dac | 53 | void DRV2667::loadFIFO(signed char data, char size) |
takuhachisu | 0:faa5d89e9dac | 54 | { |
takuhachisu | 0:faa5d89e9dac | 55 | |
takuhachisu | 0:faa5d89e9dac | 56 | } |
takuhachisu | 0:faa5d89e9dac | 57 | |
takuhachisu | 0:faa5d89e9dac | 58 | void DRV2667::setWSP(char data[][4], char waveNum) |
takuhachisu | 0:faa5d89e9dac | 59 | { |
takuhachisu | 0:faa5d89e9dac | 60 | i2cWriteByte(0xFF, 0x01); |
takuhachisu | 0:faa5d89e9dac | 61 | i2cWriteByte(0x00, waveNum / 4 * 5); |
takuhachisu | 0:faa5d89e9dac | 62 | for(int i = 0; i < waveNum / 4; i++){ |
takuhachisu | 0:faa5d89e9dac | 63 | i2cWriteByte(0x01 + i * 5, 0x81); |
takuhachisu | 0:faa5d89e9dac | 64 | i2cWriteByte(0x02 + i * 5, 0x00 + i * 4); |
takuhachisu | 0:faa5d89e9dac | 65 | i2cWriteByte(0x03 + i * 5, 0x01); |
takuhachisu | 0:faa5d89e9dac | 66 | i2cWriteByte(0x04 + i * 5, 0x03 + i * 4); |
takuhachisu | 0:faa5d89e9dac | 67 | i2cWriteByte(0x05 + i * 5, 1); |
takuhachisu | 0:faa5d89e9dac | 68 | } |
takuhachisu | 0:faa5d89e9dac | 69 | |
takuhachisu | 0:faa5d89e9dac | 70 | i2cWriteByte(0xFF, 0x02); |
takuhachisu | 0:faa5d89e9dac | 71 | for(int i = 0; i < waveNum / 4; i++){ |
takuhachisu | 0:faa5d89e9dac | 72 | i2cWriteByte(0x00 + i * 4, data[i][0]); |
takuhachisu | 0:faa5d89e9dac | 73 | i2cWriteByte(0x01 + i * 4, char(data[i][1] / 7.8125)); |
takuhachisu | 0:faa5d89e9dac | 74 | i2cWriteByte(0x02 + i * 4, data[i][2]); |
takuhachisu | 0:faa5d89e9dac | 75 | i2cWriteByte(0x03 + i * 4, data[i][3]); |
takuhachisu | 0:faa5d89e9dac | 76 | } |
takuhachisu | 0:faa5d89e9dac | 77 | |
takuhachisu | 0:faa5d89e9dac | 78 | i2cWriteByte(0xFF, 0x00); |
takuhachisu | 0:faa5d89e9dac | 79 | } |