Library for TI's DRV2667
DRV2667.cpp@2:101901b3f05e, 2018-02-08 (annotated)
- Committer:
- takuhachisu
- Date:
- Thu Feb 08 07:38:17 2018 +0000
- Revision:
- 2:101901b3f05e
- Parent:
- 1:a57042b30965
- Child:
- 3:870dc06a0e8a
Fixed minor bugs.
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 | 2:101901b3f05e | 7 | init(im, gn, to); |
takuhachisu | 0:faa5d89e9dac | 8 | } |
takuhachisu | 0:faa5d89e9dac | 9 | |
takuhachisu | 0:faa5d89e9dac | 10 | void DRV2667::i2cWriteByte(char reg, char value) |
takuhachisu | 0:faa5d89e9dac | 11 | { |
takuhachisu | 0:faa5d89e9dac | 12 | char buff[2] = {reg, value}; |
takuhachisu | 0:faa5d89e9dac | 13 | _i2c->write(SLAVE_ADDR_7_BIT<<1, buff, 2); |
takuhachisu | 0:faa5d89e9dac | 14 | } |
takuhachisu | 0:faa5d89e9dac | 15 | |
takuhachisu | 1:a57042b30965 | 16 | char DRV2667::i2cReadByte(char reg) |
takuhachisu | 0:faa5d89e9dac | 17 | { |
takuhachisu | 2:101901b3f05e | 18 | char result; |
takuhachisu | 0:faa5d89e9dac | 19 | _i2c->write(SLAVE_ADDR_7_BIT<<1, ®, 1, true); |
takuhachisu | 0:faa5d89e9dac | 20 | _i2c->read(SLAVE_ADDR_7_BIT<<1, &result, 1); |
takuhachisu | 0:faa5d89e9dac | 21 | |
takuhachisu | 0:faa5d89e9dac | 22 | return result; |
takuhachisu | 0:faa5d89e9dac | 23 | } |
takuhachisu | 0:faa5d89e9dac | 24 | |
takuhachisu | 2:101901b3f05e | 25 | void DRV2667::init(InputMux im, Gain gn, Timeout to) |
takuhachisu | 2:101901b3f05e | 26 | { |
takuhachisu | 2:101901b3f05e | 27 | i2cWriteByte(0x02, 0x00); // Exit low-power standby mode |
takuhachisu | 2:101901b3f05e | 28 | |
takuhachisu | 2:101901b3f05e | 29 | if(im != Digital || gn != GNx1){ |
takuhachisu | 2:101901b3f05e | 30 | char temp = (i2cReadByte(0x01) | im) | gn; |
takuhachisu | 2:101901b3f05e | 31 | i2cWriteByte(0x01, temp); |
takuhachisu | 2:101901b3f05e | 32 | } |
takuhachisu | 2:101901b3f05e | 33 | if(to != TOx1 || im != Digital){ |
takuhachisu | 2:101901b3f05e | 34 | char temp = (i2cReadByte(0x02) | to) | (im>>1); |
takuhachisu | 2:101901b3f05e | 35 | i2cWriteByte(0x02, temp); |
takuhachisu | 2:101901b3f05e | 36 | } |
takuhachisu | 2:101901b3f05e | 37 | } |
takuhachisu | 2:101901b3f05e | 38 | |
takuhachisu | 2:101901b3f05e | 39 | void DRV2667::reset(void) |
takuhachisu | 2:101901b3f05e | 40 | { |
takuhachisu | 2:101901b3f05e | 41 | i2cWriteByte(0x02, 0x80); |
takuhachisu | 2:101901b3f05e | 42 | } |
takuhachisu | 2:101901b3f05e | 43 | |
takuhachisu | 0:faa5d89e9dac | 44 | void DRV2667::play() |
takuhachisu | 0:faa5d89e9dac | 45 | { |
takuhachisu | 0:faa5d89e9dac | 46 | i2cWriteByte(0x02, i2cReadByte(0x02) | 1); |
takuhachisu | 0:faa5d89e9dac | 47 | } |
takuhachisu | 0:faa5d89e9dac | 48 | |
takuhachisu | 0:faa5d89e9dac | 49 | void DRV2667::stop() |
takuhachisu | 0:faa5d89e9dac | 50 | { |
takuhachisu | 0:faa5d89e9dac | 51 | i2cWriteByte(0x02, i2cReadByte(0x02) & ~1); |
takuhachisu | 0:faa5d89e9dac | 52 | } |
takuhachisu | 0:faa5d89e9dac | 53 | |
takuhachisu | 0:faa5d89e9dac | 54 | void DRV2667::setWaveform(char* id, char setNum) |
takuhachisu | 0:faa5d89e9dac | 55 | { |
takuhachisu | 0:faa5d89e9dac | 56 | for(int i = 0; i < setNum && i < 8; i++) |
takuhachisu | 0:faa5d89e9dac | 57 | i2cWriteByte(0x03 + i, id[i]); |
takuhachisu | 1:a57042b30965 | 58 | if(setNum < 8) |
takuhachisu | 0:faa5d89e9dac | 59 | i2cWriteByte(0x03 + setNum, 0); |
takuhachisu | 0:faa5d89e9dac | 60 | } |
takuhachisu | 0:faa5d89e9dac | 61 | |
takuhachisu | 1:a57042b30965 | 62 | //void DRV2667::loadFIFO(signed char data, char size) |
takuhachisu | 1:a57042b30965 | 63 | //{ |
takuhachisu | 1:a57042b30965 | 64 | // |
takuhachisu | 1:a57042b30965 | 65 | //} |
takuhachisu | 0:faa5d89e9dac | 66 | |
takuhachisu | 2:101901b3f05e | 67 | void DRV2667::setWSP(char data[][5], char waveNum) |
takuhachisu | 0:faa5d89e9dac | 68 | { |
takuhachisu | 0:faa5d89e9dac | 69 | i2cWriteByte(0xFF, 0x01); |
takuhachisu | 2:101901b3f05e | 70 | i2cWriteByte(0x00, waveNum / 5 * 5); |
takuhachisu | 2:101901b3f05e | 71 | for(int i = 0; i < waveNum / 5; i++){ |
takuhachisu | 0:faa5d89e9dac | 72 | i2cWriteByte(0x01 + i * 5, 0x81); |
takuhachisu | 0:faa5d89e9dac | 73 | i2cWriteByte(0x02 + i * 5, 0x00 + i * 4); |
takuhachisu | 0:faa5d89e9dac | 74 | i2cWriteByte(0x03 + i * 5, 0x01); |
takuhachisu | 0:faa5d89e9dac | 75 | i2cWriteByte(0x04 + i * 5, 0x03 + i * 4); |
takuhachisu | 2:101901b3f05e | 76 | i2cWriteByte(0x05 + i * 5, data[i][4]); |
takuhachisu | 0:faa5d89e9dac | 77 | } |
takuhachisu | 0:faa5d89e9dac | 78 | |
takuhachisu | 0:faa5d89e9dac | 79 | i2cWriteByte(0xFF, 0x02); |
takuhachisu | 2:101901b3f05e | 80 | for(int i = 0; i < waveNum / 5; i++){ |
takuhachisu | 0:faa5d89e9dac | 81 | i2cWriteByte(0x00 + i * 4, data[i][0]); |
takuhachisu | 0:faa5d89e9dac | 82 | i2cWriteByte(0x01 + i * 4, char(data[i][1] / 7.8125)); |
takuhachisu | 0:faa5d89e9dac | 83 | i2cWriteByte(0x02 + i * 4, data[i][2]); |
takuhachisu | 0:faa5d89e9dac | 84 | i2cWriteByte(0x03 + i * 4, data[i][3]); |
takuhachisu | 0:faa5d89e9dac | 85 | } |
takuhachisu | 0:faa5d89e9dac | 86 | |
takuhachisu | 0:faa5d89e9dac | 87 | i2cWriteByte(0xFF, 0x00); |
takuhachisu | 1:a57042b30965 | 88 | } |
takuhachisu | 1:a57042b30965 | 89 | |
takuhachisu | 1:a57042b30965 | 90 | //void DRV2667::setDPR(void) |
takuhachisu | 1:a57042b30965 | 91 | //{ |
takuhachisu | 1:a57042b30965 | 92 | // |
takuhachisu | 1:a57042b30965 | 93 | //} |