Start of a microbit mpr121 library

Dependents:   microbitmpr121-example

Committer:
owenbrotherwood
Date:
Mon Jan 16 11:38:45 2017 +0000
Revision:
1:f6fed00a3ff2
Parent:
0:fb4572fc4901
Child:
2:4e130924a398
wip

Who changed what in which revision?

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