Start of a microbit mpr121 library
Dependents: microbitmpr121-example
MicroBitMpr121.cpp@2:4e130924a398, 2017-01-16 (annotated)
- Committer:
- owenbrotherwood
- Date:
- Mon Jan 16 14:00:36 2017 +0000
- Revision:
- 2:4e130924a398
- Parent:
- 1:f6fed00a3ff2
- Child:
- 3:a91b1bb396ca
problems in library;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
owenbrotherwood | 0:fb4572fc4901 | 1 | #include "MicroBit.h" |
owenbrotherwood | 1:f6fed00a3ff2 | 2 | #include "MicroBitI2C.h" |
owenbrotherwood | 1:f6fed00a3ff2 | 3 | #include "MicroBitMpr121.h" |
owenbrotherwood | 0:fb4572fc4901 | 4 | |
owenbrotherwood | 0:fb4572fc4901 | 5 | #define DEBUG 1 |
owenbrotherwood | 1:f6fed00a3ff2 | 6 | |
owenbrotherwood | 1:f6fed00a3ff2 | 7 | //MicroBitMpr121::MicroBitMpr121(PinName sda, PinName scl, PinName irq, MPR121_ADDR addr) |
owenbrotherwood | 1:f6fed00a3ff2 | 8 | MicroBitMpr121::MicroBitMpr121() |
owenbrotherwood | 0:fb4572fc4901 | 9 | { |
owenbrotherwood | 2:4e130924a398 | 10 | I2C _i2c = MicroBitI2C(I2C_SDA0, I2C_SCL0); |
owenbrotherwood | 2:4e130924a398 | 11 | uint8_t addr = ADDR_SDA; |
owenbrotherwood | 1:f6fed00a3ff2 | 12 | // PinName _irq = irq; //TODO :: use |
owenbrotherwood | 1:f6fed00a3ff2 | 13 | uint8_t _i2c_addr = (addr << 1); // To get 8bit address ...? |
owenbrotherwood | 0:fb4572fc4901 | 14 | return; |
owenbrotherwood | 0:fb4572fc4901 | 15 | } |
owenbrotherwood | 0:fb4572fc4901 | 16 | |
owenbrotherwood | 1:f6fed00a3ff2 | 17 | void MicroBitMpr121::init(void) |
owenbrotherwood | 0:fb4572fc4901 | 18 | { |
owenbrotherwood | 1:f6fed00a3ff2 | 19 | P16.setPull(PullUp); //TODO use _irq and not hard code |
owenbrotherwood | 1:f6fed00a3ff2 | 20 | MicroBitI2C::write(SRST, 0x63); |
owenbrotherwood | 1:f6fed00a3ff2 | 21 | MicroBitI2C::write(MHDR, 0x1); |
owenbrotherwood | 1:f6fed00a3ff2 | 22 | MicroBitI2C::write(NHDR, 0x1); |
owenbrotherwood | 1:f6fed00a3ff2 | 23 | MicroBitI2C::write(NCLR, 0x0); |
owenbrotherwood | 1:f6fed00a3ff2 | 24 | MicroBitI2C::write(FDLR, 0x0); |
owenbrotherwood | 1:f6fed00a3ff2 | 25 | MicroBitI2C::write(MHDF, 0x1); |
owenbrotherwood | 1:f6fed00a3ff2 | 26 | MicroBitI2C::write(NHDF, 0x1); |
owenbrotherwood | 1:f6fed00a3ff2 | 27 | MicroBitI2C::write(NCLF, 0xFF); |
owenbrotherwood | 1:f6fed00a3ff2 | 28 | MicroBitI2C::write(FDLF, 0x2); |
owenbrotherwood | 1:f6fed00a3ff2 | 29 | for(int i=0; i<(12*2); i+=2) { |
owenbrotherwood | 1:f6fed00a3ff2 | 30 | MicroBitI2C::write(static_cast<MPR121_REGISTER>(E0TTH+i), 0x20); |
owenbrotherwood | 0:fb4572fc4901 | 31 | } |
owenbrotherwood | 1:f6fed00a3ff2 | 32 | for(int i=0; i<(12*2); i+=2) { |
owenbrotherwood | 1:f6fed00a3ff2 | 33 | MicroBitI2C::write(static_cast<MPR121_REGISTER>(E0RTH+i), 0x10); |
owenbrotherwood | 0:fb4572fc4901 | 34 | } |
owenbrotherwood | 1:f6fed00a3ff2 | 35 | MicroBitI2C::write(DT_DR, 0x11); |
owenbrotherwood | 1:f6fed00a3ff2 | 36 | MicroBitI2C::write(CDC_CONFIG, 0x10); |
owenbrotherwood | 1:f6fed00a3ff2 | 37 | MicroBitI2C::write(CDT_CONFIG, 0x20); |
owenbrotherwood | 1:f6fed00a3ff2 | 38 | MicroBitI2C::write(AUTO_CFG0, 0x33); |
owenbrotherwood | 1:f6fed00a3ff2 | 39 | MicroBitI2C::write(AUTO_CFG1, 0x07); |
owenbrotherwood | 1:f6fed00a3ff2 | 40 | MicroBitI2C::write(USL, 0xc9); |
owenbrotherwood | 1:f6fed00a3ff2 | 41 | MicroBitI2C::write(LSL, 0x83); |
owenbrotherwood | 1:f6fed00a3ff2 | 42 | MicroBitI2C::write(TL, 0xb5); |
owenbrotherwood | 1:f6fed00a3ff2 | 43 | MicroBitI2C::write(ECR, 0x8f); |
owenbrotherwood | 0:fb4572fc4901 | 44 | return; |
owenbrotherwood | 0:fb4572fc4901 | 45 | } |
owenbrotherwood | 0:fb4572fc4901 | 46 | |
owenbrotherwood | 1:f6fed00a3ff2 | 47 | void MicroBitMpr121::enable(void) |
owenbrotherwood | 0:fb4572fc4901 | 48 | { |
owenbrotherwood | 0:fb4572fc4901 | 49 | _button = 0; |
owenbrotherwood | 0:fb4572fc4901 | 50 | _button_has_changed = 0; |
owenbrotherwood | 1:f6fed00a3ff2 | 51 | MicroBitI2C::write(ECR, 0x8f); |
owenbrotherwood | 1:f6fed00a3ff2 | 52 | _irq->fall(this, &MicroBitMpr121::handler); |
owenbrotherwood | 0:fb4572fc4901 | 53 | return; |
owenbrotherwood | 0:fb4572fc4901 | 54 | } |
owenbrotherwood | 0:fb4572fc4901 | 55 | |
owenbrotherwood | 1:f6fed00a3ff2 | 56 | void MicroBitMpr121::disable(void) |
owenbrotherwood | 0:fb4572fc4901 | 57 | { |
owenbrotherwood | 0:fb4572fc4901 | 58 | _irq->fall(NULL); |
owenbrotherwood | 0:fb4572fc4901 | 59 | _button = 0; |
owenbrotherwood | 0:fb4572fc4901 | 60 | _button_has_changed = 0; |
owenbrotherwood | 1:f6fed00a3ff2 | 61 | MicroBitI2C::write(ECR, 0x0); |
owenbrotherwood | 1:f6fed00a3ff2 | 62 | MicroBitI2C::write(AUTO_CFG0, 0x0); |
owenbrotherwood | 1:f6fed00a3ff2 | 63 | MicroBitI2C::write(AUTO_CFG1, 0x0); |
owenbrotherwood | 0:fb4572fc4901 | 64 | return; |
owenbrotherwood | 0:fb4572fc4901 | 65 | } |
owenbrotherwood | 0:fb4572fc4901 | 66 | |
owenbrotherwood | 1:f6fed00a3ff2 | 67 | uint32_t MicroBitMpr121::isPressed(void) |
owenbrotherwood | 0:fb4572fc4901 | 68 | { |
owenbrotherwood | 0:fb4572fc4901 | 69 | return _button_has_changed; |
owenbrotherwood | 0:fb4572fc4901 | 70 | } |
owenbrotherwood | 0:fb4572fc4901 | 71 | |
owenbrotherwood | 1:f6fed00a3ff2 | 72 | uint16_t MicroBitMpr121::buttonPressed(void) |
owenbrotherwood | 0:fb4572fc4901 | 73 | { |
owenbrotherwood | 0:fb4572fc4901 | 74 | _button_has_changed = 0; |
owenbrotherwood | 0:fb4572fc4901 | 75 | return _button; |
owenbrotherwood | 0:fb4572fc4901 | 76 | } |
owenbrotherwood | 0:fb4572fc4901 | 77 | |
owenbrotherwood | 1:f6fed00a3ff2 | 78 | void MicroBitMpr121::registerDump() const |
owenbrotherwood | 0:fb4572fc4901 | 79 | { |
owenbrotherwood | 0:fb4572fc4901 | 80 | uint8_t reg_val = 0; |
owenbrotherwood | 1:f6fed00a3ff2 | 81 | |
owenbrotherwood | 1:f6fed00a3ff2 | 82 | for(int i=0; i<0x80; i++) { |
owenbrotherwood | 1:f6fed00a3ff2 | 83 | reg_val = MicroBitMpr121::read(static_cast<MPR121_REGISTER>(i)); |
owenbrotherwood | 1:f6fed00a3ff2 | 84 | printf("Reg 0x%02x: 0x%02x \n", i, reg_val); |
owenbrotherwood | 0:fb4572fc4901 | 85 | } |
owenbrotherwood | 1:f6fed00a3ff2 | 86 | |
owenbrotherwood | 0:fb4572fc4901 | 87 | return; |
owenbrotherwood | 0:fb4572fc4901 | 88 | } |
owenbrotherwood | 0:fb4572fc4901 | 89 | |
owenbrotherwood | 1:f6fed00a3ff2 | 90 | void MicroBitMpr121::handler(void) |
owenbrotherwood | 0:fb4572fc4901 | 91 | { |
owenbrotherwood | 0:fb4572fc4901 | 92 | uint16_t reg_val = 0, oor_val = 0; |
owenbrotherwood | 1:f6fed00a3ff2 | 93 | reg_val = MicroBitMpr121::read(ELE0_7_STAT); |
owenbrotherwood | 1:f6fed00a3ff2 | 94 | reg_val |= MicroBitMpr121::read(ELE8_11_STAT) << 8; |
owenbrotherwood | 1:f6fed00a3ff2 | 95 | oor_val = MicroBitMpr121::read(ELE0_7_OOR_STAT); |
owenbrotherwood | 1:f6fed00a3ff2 | 96 | oor_val |= MicroBitMpr121::read(ELE8_11_OOR_STAT) << 8; |
owenbrotherwood | 1:f6fed00a3ff2 | 97 | if((0 != oor_val) && DEBUG) { |
owenbrotherwood | 1:f6fed00a3ff2 | 98 | printf("MPR121 OOR failure - 0x%04x\n", oor_val); |
owenbrotherwood | 0:fb4572fc4901 | 99 | wait(0.1f); |
owenbrotherwood | 0:fb4572fc4901 | 100 | NVIC_SystemReset(); |
owenbrotherwood | 0:fb4572fc4901 | 101 | } |
owenbrotherwood | 0:fb4572fc4901 | 102 | _button = reg_val; |
owenbrotherwood | 0:fb4572fc4901 | 103 | _button_has_changed = 1; |
owenbrotherwood | 0:fb4572fc4901 | 104 | return; |
owenbrotherwood | 0:fb4572fc4901 | 105 | } |
owenbrotherwood | 0:fb4572fc4901 | 106 | |
owenbrotherwood | 1:f6fed00a3ff2 | 107 | void MicroBitI2C::write(MPR121_REGISTER const reg, uint8_t const data) const |
owenbrotherwood | 0:fb4572fc4901 | 108 | { |
owenbrotherwood | 0:fb4572fc4901 | 109 | char buf[2] = {reg, data}; |
owenbrotherwood | 0:fb4572fc4901 | 110 | uint8_t result = 0; |
owenbrotherwood | 0:fb4572fc4901 | 111 | result = _i2c->write(_i2c_addr, buf, 2); |
owenbrotherwood | 1:f6fed00a3ff2 | 112 | if(result && DEBUG) { |
owenbrotherwood | 1:f6fed00a3ff2 | 113 | printf("I2c write failed\n"); |
owenbrotherwood | 0:fb4572fc4901 | 114 | } |
owenbrotherwood | 0:fb4572fc4901 | 115 | return; |
owenbrotherwood | 0:fb4572fc4901 | 116 | } |
owenbrotherwood | 0:fb4572fc4901 | 117 | |
owenbrotherwood | 1:f6fed00a3ff2 | 118 | uint8_t MicroBitMpr121::read(MPR121_REGISTER const reg) const |
owenbrotherwood | 0:fb4572fc4901 | 119 | { |
owenbrotherwood | 0:fb4572fc4901 | 120 | char buf[1] = {reg}, data = 0; |
owenbrotherwood | 0:fb4572fc4901 | 121 | uint8_t result = 1; |
owenbrotherwood | 1:f6fed00a3ff2 | 122 | result &= _i2c->write(_i2c_addr, buf, 1, true); //TODO: Correct with true? |
owenbrotherwood | 1:f6fed00a3ff2 | 123 | result &= _i2c->read(_i2c_addr, &data, 1); |
owenbrotherwood | 1:f6fed00a3ff2 | 124 | if(result && DEBUG) { |
owenbrotherwood | 1:f6fed00a3ff2 | 125 | printf("I2c read failed\n"); |
owenbrotherwood | 0:fb4572fc4901 | 126 | } |
owenbrotherwood | 0:fb4572fc4901 | 127 | return data; |
owenbrotherwood | 0:fb4572fc4901 | 128 | } |