Herrpaule

MPR121 & Axoloti (i2c)

Mar 17th, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.05 KB | None | 0 0
  1. /*
  2.  MPR121 example to read touches
  3.  by Paul 2016 irieelectronics.de
  4.  
  5.  main MPR121 code is from: http://bildr.org/2011/05/mpr121_arduino/
  6.  */
  7.  
  8. // MPR121 Register Defines
  9. #define     MHD_R   0x2B
  10. #define     NHD_R   0x2C
  11. #define NCL_R   0x2D
  12. #define FDL_R   0x2E
  13. #define MHD_F   0x2F
  14. #define NHD_F   0x30
  15. #define NCL_F   0x31
  16. #define FDL_F   0x32
  17. #define ELE0_T  0x41
  18. #define ELE0_R  0x42
  19. #define ELE1_T  0x43
  20. #define ELE1_R  0x44
  21. #define ELE2_T  0x45
  22. #define ELE2_R  0x46
  23. #define ELE3_T  0x47
  24. #define ELE3_R  0x48
  25. #define ELE4_T  0x49
  26. #define ELE4_R  0x4A
  27. #define ELE5_T  0x4B
  28. #define ELE5_R  0x4C
  29. #define ELE6_T  0x4D
  30. #define ELE6_R  0x4E
  31. #define ELE7_T  0x4F
  32. #define ELE7_R  0x50
  33. #define ELE8_T  0x51
  34. #define ELE8_R  0x52
  35. #define ELE9_T  0x53
  36. #define ELE9_R  0x54
  37. #define ELE10_T 0x55
  38. #define ELE10_R 0x56
  39. #define ELE11_T 0x57
  40. #define ELE11_R 0x58
  41. #define FIL_CFG 0x5D
  42. #define ELE_CFG 0x5E
  43. #define     GPIO_CTRL0  0x73
  44. #define GPIO_CTRL1  0x74
  45. #define     GPIO_DATA       0x75
  46. #define GPIO_DIR        0x76
  47. #define GPIO_EN     0x77
  48. #define GPIO_SET        0x78
  49. #define GPIO_CLEAR  0x79
  50. #define GPIO_TOGGLE 0x7A
  51. #define ATO_CFG0    0x7B
  52. #define ATO_CFGU    0x7D
  53. #define ATO_CFGL    0x7E
  54. #define ATO_CFGT    0x7F
  55.  
  56.  
  57. // Global Constants
  58. #define     TOU_THRESH  0x06
  59. #define REL_THRESH  0x0A
  60.  
  61. uint8_t *rxbuf;
  62. uint8_t *txbuf;
  63.  
  64. // systime_t tmo = MS2ST(4);
  65.  
  66. uint8_t touchStates[12]; //to keep track of the previous touch states
  67.  
  68. void setup (void){
  69.     static uint8_t _txbuf[8] __attribute__ ((section (".sram2")));
  70.     static uint8_t _rxbuf[8] __attribute__ ((section (".sram2")));
  71.     txbuf = _txbuf;
  72.     rxbuf = _rxbuf;
  73.    
  74.     palSetPadMode(GPIOB, 1, PAL_MODE_INPUT_PULLUP);
  75.     /*
  76.     palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(4)|PAL_STM32_PUDR_PULLUP|PAL_STM32_OTYPE_OPENDRAIN);// SCL
  77.         palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(4)|PAL_STM32_PUDR_PULLUP|PAL_STM32_OTYPE_OPENDRAIN);// SDA
  78.        
  79.         static const I2CConfig i2cfg = {
  80.         OPMODE_I2C,
  81.         400000,
  82.         FAST_DUTY_CYCLE_2,
  83.     };
  84.    
  85.     i2cStart(&I2CD1, &i2cfg);   // start I2C
  86.     */
  87.     mpr121_setup(); // call MPR121 Setup
  88.  
  89.     LogTextMessage("READY");
  90. }
  91.  
  92. void loop (void){
  93.    
  94.     readTouchInputs();
  95.     chThdSleepMilliseconds(1);
  96.    
  97.  }
  98.  
  99. void readTouchInputs(void){
  100.   if(!checkInterrupt()){
  101.    
  102.     //read the touch state from the MPR121
  103.     msg_t backmsg = i2cMasterReceive(&I2CD1, 0x5A, rxbuf, 2);
  104.    
  105.     uint8_t LSB = rxbuf[0];
  106.     uint8_t MSB = rxbuf[1];
  107.    
  108.     uint16_t touched = ((MSB << 8) | LSB); //16bits that make up the touch states
  109.  
  110.    
  111.     for (int i=0; i < 12; i++){  // Check what electrodes were pressed
  112.       if(touched & (1<<i)){
  113.      
  114.         if(touchStates[i] == 0){
  115.           //pin i was just touched
  116.           LogTextMessage("pin: [%i] was just touched", i);          // LogTextMessage(" was just touched");
  117.        
  118.         }else if(touchStates[i] == 1){
  119.           //pin i is still being touched
  120.         }  
  121.      
  122.         touchStates[i] = 1;      
  123.       }else{
  124.         if(touchStates[i] == 1){
  125.           LogTextMessage("pin: [%i] is no longer being touched", i);    //pin i is no longer being touched
  126.        }      
  127.        touchStates[i] = 0;
  128.       }
  129.     }
  130.   }
  131. }
  132.  
  133.  
  134. void mpr121_setup(void){
  135.  
  136.   set_register(0x5A, ELE_CFG, 0x00);
  137.  
  138.   // Section A - Controls filtering when data is > baseline.
  139.   set_register(0x5A, MHD_R, 0x01);
  140.   set_register(0x5A, NHD_R, 0x01);
  141.   set_register(0x5A, NCL_R, 0x00);
  142.   set_register(0x5A, FDL_R, 0x00);
  143.  
  144.   // Section B - Controls filtering when data is < baseline.
  145.   set_register(0x5A, MHD_F, 0x01);
  146.   set_register(0x5A, NHD_F, 0x01);
  147.   set_register(0x5A, NCL_F, 0xFF);
  148.   set_register(0x5A, FDL_F, 0x02);
  149.  
  150.   // Section C - Sets touch and release thresholds for each electrode
  151.   set_register(0x5A, ELE0_T, TOU_THRESH);
  152.   set_register(0x5A, ELE0_R, REL_THRESH);
  153.  
  154.   set_register(0x5A, ELE1_T, TOU_THRESH);
  155.   set_register(0x5A, ELE1_R, REL_THRESH);
  156.  
  157.   set_register(0x5A, ELE2_T, TOU_THRESH);
  158.   set_register(0x5A, ELE2_R, REL_THRESH);
  159.  
  160.   set_register(0x5A, ELE3_T, TOU_THRESH);
  161.   set_register(0x5A, ELE3_R, REL_THRESH);
  162.  
  163.   set_register(0x5A, ELE4_T, TOU_THRESH);
  164.   set_register(0x5A, ELE4_R, REL_THRESH);
  165.  
  166.   set_register(0x5A, ELE5_T, TOU_THRESH);
  167.   set_register(0x5A, ELE5_R, REL_THRESH);
  168.  
  169.   set_register(0x5A, ELE6_T, TOU_THRESH);
  170.   set_register(0x5A, ELE6_R, REL_THRESH);
  171.  
  172.   set_register(0x5A, ELE7_T, TOU_THRESH);
  173.   set_register(0x5A, ELE7_R, REL_THRESH);
  174.  
  175.   set_register(0x5A, ELE8_T, TOU_THRESH);
  176.   set_register(0x5A, ELE8_R, REL_THRESH);
  177.  
  178.   set_register(0x5A, ELE9_T, TOU_THRESH);
  179.   set_register(0x5A, ELE9_R, REL_THRESH);
  180.  
  181.   set_register(0x5A, ELE10_T, TOU_THRESH);
  182.   set_register(0x5A, ELE10_R, REL_THRESH);
  183.  
  184.   set_register(0x5A, ELE11_T, TOU_THRESH);
  185.   set_register(0x5A, ELE11_R, REL_THRESH);
  186.  
  187.   // Section D
  188.   // Set the Filter Configuration
  189.   // Set ESI2
  190.   set_register(0x5A, FIL_CFG, 0x04);
  191.  
  192.   // Section E
  193.   // Electrode Configuration
  194.   // Set ELE_CFG to 0x00 to return to standby mode
  195.   set_register(0x5A, ELE_CFG, 0x0C);  // Enables all 12 Electrodes
  196.  
  197.  
  198.   // Section F
  199.   // Enable Auto Config and auto Reconfig
  200.   /*
  201.   set_register(0x5A, ATO_CFG0, 0x0B);
  202.   set_register(0x5A, ATO_CFGU, 0xC9);  // USL = (Vdd-0.7)/vdd*256 = 0xC9 @3.3V   set_register(0x5A, ATO_CFGL, 0x82);  // LSL = 0.65*USL = 0x82 @3.3V
  203.   set_register(0x5A, ATO_CFGT, 0xB5);  // Target = 0.9*USL = 0xB5 @3.3V
  204.   */
  205.   set_register(0x5A, ELE_CFG, 0x0C);
  206.  
  207. }
  208.  
  209.  
  210. void set_register(uint8_t address, uint8_t reg, uint8_t val){
  211.  
  212.     chThdSleepMilliseconds(1);
  213.     /*
  214.     if (chThdShouldTerminate()) {
  215.         i2cStop(&I2CD1);
  216.         return;
  217.     }
  218.     */
  219.     txbuf[0] = reg;
  220.     txbuf[1] = val;
  221.     msg_t status = RDY_OK;
  222.     i2cAcquireBus(&I2CD1);
  223.     msg_t msg = i2c_lld_master_transmit_timeout(&I2CD1, 0x5A, txbuf, 2, rxbuf, 0, TIME_INFINITE);
  224.     i2cReleaseBus(&I2CD1);
  225.  
  226.     if (msg != RDY_OK) {
  227.         LogTextMessage("error %i", i2cGetErrors(&I2CD1));
  228.         return;
  229.     }
  230.     else if (msg != RDY_OK) {
  231.         LogTextMessage("OK %i", i2cGetErrors(&I2CD1));
  232.        
  233.     }
  234.    
  235. }
  236.  
  237. uint8_t checkInterrupt(void){
  238.   return (palReadPad(GPIOB,1)<<27);
  239. }
Advertisement
Add Comment
Please, Sign In to add comment