irieelectronics

axoloti I2C MPR121

Aug 22nd, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.97 KB | None | 0 0
  1. // MPR121 Register Defines
  2. #define MHD_R 0x2B
  3. #define NHD_R 0x2C
  4. #define NCL_R   0x2D
  5. #define FDL_R 0x2E
  6. #define MHD_F 0x2F
  7. #define NHD_F 0x30
  8. #define NCL_F 0x31
  9. #define FDL_F 0x32
  10. #define ELE0_T  0x41
  11. #define ELE0_R  0x42
  12. #define ELE1_T  0x43
  13. #define ELE1_R  0x44
  14. #define ELE2_T  0x45
  15. #define ELE2_R  0x46
  16. #define ELE3_T  0x47
  17. #define ELE3_R  0x48
  18. #define ELE4_T  0x49
  19. #define ELE4_R  0x4A
  20. #define ELE5_T  0x4B
  21. #define ELE5_R  0x4C
  22. #define ELE6_T  0x4D
  23. #define ELE6_R  0x4E
  24. #define ELE7_T  0x4F
  25. #define ELE7_R  0x50
  26. #define ELE8_T  0x51
  27. #define ELE8_R  0x52
  28. #define ELE9_T  0x53
  29. #define ELE9_R  0x54
  30. #define ELE10_T 0x55
  31. #define ELE10_R 0x56
  32. #define ELE11_T 0x57
  33. #define ELE11_R 0x58
  34. #define FIL_CFG 0x5D
  35. #define ELE_CFG 0x5E
  36. #define GPIO_CTRL0  0x73
  37. #define GPIO_CTRL1  0x74
  38. #define GPIO_DATA 0x75
  39. #define GPIO_DIR  0x76
  40. #define GPIO_EN   0x77
  41. #define GPIO_SET  0x78
  42. #define GPIO_CLEAR  0x79
  43. #define GPIO_TOGGLE 0x7A
  44. #define ATO_CFG0  0x7B
  45. #define ATO_CFGU  0x7D
  46. #define ATO_CFGL  0x7E
  47. #define ATO_CFGT  0x7F
  48.  
  49. // Global Constants
  50. #define TOU_THRESH  0x0F
  51. #define REL_THRESH  0x0A
  52.  
  53. // I2C R/W Address of the MPR121. ADD is unconnected = 0x5A.
  54. #define MPR121_R 0xB5
  55. #define MPR121_W 0xB4
  56.  
  57. uint8_t rxbuf[8];
  58. uint8_t txbuf[8];
  59.  
  60. systime_t tmo = MS2ST(4);
  61.  
  62. void setup(void){
  63.     txbuf[0] = ELE_CFG;
  64.     txbuf[1] = 0x00;
  65.  
  66.     i2cAcquireBus(&I2CD1);
  67.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 2, rxbuf, 0, tmo);
  68.     i2cReleaseBus(&I2CD1);
  69.  
  70.     // Section A - Controls filtering when data is > baseline.
  71.     txbuf[0] = MHD_R;
  72.     txbuf[1] = 0x01;
  73.     txbuf[2] = NHD_R;
  74.     txbuf[3] = 0x01;
  75.     txbuf[4] = NCL_R;
  76.     txbuf[5] = 0x00;
  77.     txbuf[6] = FDL_R;
  78.     txbuf[7] = 0x00;
  79.  
  80.     i2cAcquireBus(&I2CD1);
  81.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 8, rxbuf, 0, tmo);
  82.     i2cReleaseBus(&I2CD1);
  83.  
  84.     // Section B - Controls filtering when data is < baseline.
  85.     txbuf[0] = MHD_F;
  86.     txbuf[1] = 0x01;
  87.     txbuf[2] = NHD_F;
  88.     txbuf[3] = 0x01;
  89.     txbuf[4] = NCL_F;
  90.     txbuf[5] = 0xFF;
  91.     txbuf[6] = FDL_F;
  92.     txbuf[7] = 0x02;
  93.  
  94.     i2cAcquireBus(&I2CD1);
  95.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 8, rxbuf, 0, tmo);
  96.     i2cReleaseBus(&I2CD1);
  97.      
  98.     // Section C - Sets touch and release thresholds for each electrode
  99.     txbuf[0] = ELE0_T;
  100.     txbuf[1] = TOU_THRESH;
  101.     txbuf[2] = ELE0_R;
  102.     txbuf[3] = REL_THRESH;
  103.  
  104.     i2cAcquireBus(&I2CD1);
  105.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
  106.     i2cReleaseBus(&I2CD1);
  107.  
  108.     txbuf[0] = ELE1_T;
  109.     txbuf[1] = TOU_THRESH;
  110.     txbuf[2] = ELE1_R;
  111.     txbuf[3] = REL_THRESH;
  112.  
  113.     i2cAcquireBus(&I2CD1);
  114.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
  115.     i2cReleaseBus(&I2CD1);
  116.      
  117.     txbuf[0] = ELE2_T;
  118.     txbuf[1] = TOU_THRESH;
  119.     txbuf[2] = ELE2_R;
  120.     txbuf[3] = REL_THRESH;
  121.  
  122.     i2cAcquireBus(&I2CD1);
  123.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
  124.     i2cReleaseBus(&I2CD1);
  125.  
  126.     txbuf[0] = ELE3_T;
  127.     txbuf[1] = TOU_THRESH;
  128.     txbuf[2] = ELE3_R;
  129.     txbuf[3] = REL_THRESH;
  130.  
  131.     i2cAcquireBus(&I2CD1);
  132.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
  133.     i2cReleaseBus(&I2CD1);
  134.  
  135.     txbuf[0] = ELE4_T;
  136.     txbuf[1] = TOU_THRESH;
  137.     txbuf[2] = ELE4_R;
  138.     txbuf[3] = REL_THRESH;
  139.  
  140.     i2cAcquireBus(&I2CD1);
  141.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
  142.     i2cReleaseBus(&I2CD1);
  143.  
  144.     txbuf[0] = ELE5_T;
  145.     txbuf[1] = TOU_THRESH;
  146.     txbuf[2] = ELE5_R;
  147.     txbuf[3] = REL_THRESH;
  148.  
  149.     i2cAcquireBus(&I2CD1);
  150.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
  151.     i2cReleaseBus(&I2CD1);
  152.  
  153.     txbuf[0] = ELE6_T;
  154.     txbuf[1] = TOU_THRESH;
  155.     txbuf[2] = ELE6_R;
  156.     txbuf[3] = REL_THRESH;
  157.  
  158.     i2cAcquireBus(&I2CD1);
  159.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
  160.     i2cReleaseBus(&I2CD1);
  161.  
  162.     txbuf[0] = ELE7_T;
  163.     txbuf[1] = TOU_THRESH;
  164.     txbuf[2] = ELE7_R;
  165.     txbuf[3] = REL_THRESH;
  166.  
  167.     i2cAcquireBus(&I2CD1);
  168.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
  169.     i2cReleaseBus(&I2CD1);
  170.  
  171.     txbuf[0] = ELE8_T;
  172.     txbuf[1] = TOU_THRESH;
  173.     txbuf[2] = ELE8_R;
  174.     txbuf[3] = REL_THRESH;
  175.  
  176.     i2cAcquireBus(&I2CD1);
  177.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
  178.     i2cReleaseBus(&I2CD1);
  179.  
  180.     txbuf[0] = ELE9_T;
  181.     txbuf[1] = TOU_THRESH;
  182.     txbuf[2] = ELE9_R;
  183.     txbuf[3] = REL_THRESH;
  184.  
  185.     i2cAcquireBus(&I2CD1);
  186.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
  187.     i2cReleaseBus(&I2CD1);
  188.  
  189.     txbuf[0] = ELE10_T;
  190.     txbuf[1] = TOU_THRESH;
  191.     txbuf[2] = ELE10_R;
  192.     txbuf[3] = REL_THRESH;
  193.  
  194.     i2cAcquireBus(&I2CD1);
  195.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
  196.     i2cReleaseBus(&I2CD1);
  197.  
  198.     txbuf[0] = ELE11_T;
  199.     txbuf[1] = TOU_THRESH;
  200.     txbuf[2] = ELE11_R;
  201.     txbuf[3] = REL_THRESH;
  202.  
  203.     i2cAcquireBus(&I2CD1);
  204.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
  205.     i2cReleaseBus(&I2CD1);
  206.      
  207.     // Section D
  208.     // Set the Filter Configuration
  209.     // Set ESI2
  210.     txbuf[0] = FIL_CFG;
  211.     txbuf[1] = 0x04;
  212.  
  213.     i2cAcquireBus(&I2CD1);
  214.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 2, rxbuf, 0, tmo);
  215.     i2cReleaseBus(&I2CD1);
  216.      
  217.     // Section E
  218.     // Electrode Configuration
  219.     // Set ELE_CFG to 0x00 to return to standby mode
  220.     txbuf[0] = ELE_CFG;
  221.     txbuf[1] = 0x0C;
  222.  
  223.     i2cAcquireBus(&I2CD1);
  224.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 2, rxbuf, 0, tmo);
  225.     i2cReleaseBus(&I2CD1);
  226.      
  227.     // Section F
  228.     // Enable Auto Config and auto Reconfig
  229.    
  230.  
  231.     txbuf[0] = ATO_CFG0;
  232.     txbuf[1] = 0x0B;
  233.     txbuf[2] = ATO_CFGU;        // USL = (Vdd-0.7)/vdd*256 = 0xC9 @3.3V   set_register(0x5A, ATO_CFGL, 0x82);  // LSL = 0.65*USL = 0x82 @3.3V
  234.     txbuf[3] = 0xC9;
  235.     txbuf[4] = ATO_CFGT;        // Target = 0.9*USL = 0xB5 @3.3V
  236.     txbuf[5] = 0xB5;
  237.  
  238.     i2cAcquireBus(&I2CD1);
  239.     i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 6, rxbuf, 0, tmo);
  240.     i2cReleaseBus(&I2CD1);
  241. }
  242.  
  243. void loop(void){
  244.     chThdSleepMilliseconds(10);
  245.     i2cAcquireBus(&I2CD1);
  246.     i2cMasterReceiveTimeout(&I2CD1, MPR121_R, rxbuf, 2, tmo);
  247.     i2cReleaseBus(&I2CD1);
  248. }
Advertisement
Add Comment
Please, Sign In to add comment