Masayoshi Abe / Mbed 2 deprecated WireFSHandControl

Dependencies:   mbed mbed-rtos EthernetInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fdc.h Source File

fdc.h

00001 /*-----------------------------------------------------------------
00002  * fdc.h
00003  */
00004 // debug port
00005 DigitalOut d19(p19);
00006 DigitalOut d20(p20);
00007 DigitalOut d21(p21);
00008 DigitalOut d22(p22);
00009 DigitalOut d23(p23);
00010 DigitalOut d24(p24);
00011 DigitalOut d25(p25);
00012 DigitalOut d26(p26);
00013 
00014 //static const char * DST_ADDRESS = "192.168.140.2";
00015 //static const char * MY_ADDRESS  = "192.168.140.8";
00016 //static const int DST_PORT = 6008;
00017 //static const int MY_PORT  = 6008;
00018 //static const char * NETMASK = "255.255.0.0";
00019 //static const char * GATEWAY = "0.0.0.0";
00020  
00021 //static const int USB_BAUDRATE = 9600;
00022  
00023 static const uint8_t ADDR_I2C = 0xFF; //Address on FDC1004 for measurement register
00024 static const uint8_t REG_FDC  = 0xA0; //int REG_FDC = 80; //Address on FDC1004 for measurement register b1010000
00025 static const uint8_t REG_MUX  = 0xE0; //int REG_MEX =112; //Address on PCA9548 for measurement register b1110000
00026 static char FDC_CONFIG[] = {0x08, 0x09, 0x0A, 0x0B};
00027 static char FDC_MSB[]    = {0x00, 0x02, 0x04, 0x06};
00028 static char FDC_LSB[]    = {0x01, 0x03, 0x05, 0x07};
00029  
00030 static const int FDC_NUM1 = 2;
00031 static const int FDC_NUM2 = 1;
00032 static const int FDC_NUM = FDC_NUM1 + FDC_NUM2; //MUXに接続しているFDC1004の数、ch0から順に接続
00033 //static const int NUM_AXES = 4;
00034 static const int NUM_DEN = 4; // DENKYOKU
00035 //static const int UDP_TX_SIZE = sizeof(float) * FDC_NUM * NUM_AXES;
00036  
00037 //union UdpTx {
00038 //  float data[FDC_NUM][NUM_AXES];
00039 //  char bytes[4*FDC_NUM*NUM_AXES];
00040 //};
00041  
00042 //RawSerial pc(USBTX, USBRX);
00043 //I2C i2c(p9, p10);
00044 //DigitalOut led1(LED1);
00045  
00046 /*static void setupUdp(UDPSocket& sock) {
00047     EthernetInterface eth;
00048  
00049     pc.printf("Setup UDP\r\n");
00050  
00051     eth.init(MY_ADDRESS, NETMASK, GATEWAY);
00052     pc.printf("eth.init()\r\n");
00053     eth.connect(15000);
00054     pc.printf("eth.connect()\r\n");
00055  
00056     const char *ip = eth.getIPAddress();
00057     pc.printf("IP: %s\r\n", ip ? ip : "No IP");
00058  
00059     sock.bind(MY_PORT);
00060     pc.printf("Port: %d\r\n", MY_PORT);
00061 }*/
00062  
00063 /* PCA9548を使ったch切替  */
00064 static inline bool switchI2c(I2C *i2c, uint8_t ch) {
00065     bool ret = true;
00066     char cmd[1] = {1 << ch};
00067     int result = i2c->write(REG_MUX, cmd, 1, false);
00068     if (result != 0) { // Error
00069         //pc.printf("switchI2c error \r\n");
00070 //ERROR
00071 d22 = !d22;
00072 //wait(0.2);
00073 d22 = !d22;
00074         ret = false;
00075     }
00076     return ret;
00077 }
00078  
00079 /* FDC1004の設定をする関数  */
00080 static inline bool initFdc(I2C *i2c){
00081     char cmd1[3] = {0x0C, 0x80, 0x00};
00082     char cmd2[1] = {ADDR_I2C};
00083     i2c->write(REG_FDC, cmd1, 3, false);
00084     i2c->write(REG_FDC, cmd2, 1, false); // DeviceIDの確認
00085  
00086     //idチェック 16,4 と出たらOK。
00087     char res[2];
00088     i2c->read(REG_FDC, res, 2);
00089  
00090     return res[0] == 16 && res[1] == 4;
00091 }
00092  
00093 static inline uint8_t highByte(uint16_t x) {
00094     return (x >> 8) & 0xFF;
00095 }
00096  
00097 static inline uint8_t lowByte(uint16_t x) {
00098     return (x >> 0) & 0xFF;
00099 }
00100  
00101 static inline void config(I2C *i2c, int rate, int (&capdac)[NUM_DEN]){
00102     // rate設定 1:100S/s, 2:200S/s, 3:400S/s
00103  
00104     uint16_t conf_fdc, conf_meas;
00105  
00106     // conf_fdc 測定頻度と使うチャンネルを設定
00107     conf_fdc = (rate << 10) + (1 << 8) + (0b1111 << 4);
00108     char cmd[3] = {0x0C, highByte(conf_fdc), lowByte(conf_fdc)};
00109     i2c->write(REG_FDC, cmd, 3, false);
00110  
00111     // conf_meas CAPDACの設定
00112     for(int i=0; i<4;i++){
00113         if(capdac[i] >= 0){
00114             conf_meas = (i << 13) + (0b100 << 10) + (capdac[i] << 5);
00115         } else if (capdac[i] == -1){
00116             conf_meas = (i << 13) + (0b111 << 10);
00117         }
00118         cmd[0] = FDC_CONFIG[i];
00119         cmd[1] = highByte(conf_meas);
00120         cmd[2] = lowByte(conf_meas);
00121         int result = i2c->write(REG_FDC, cmd, 3, false);
00122     }
00123 }
00124  
00125 static inline int readRegister(I2C *i2c, char address) {
00126     char cmd[1] = {address};
00127     char res[2];
00128  
00129     i2c->write(REG_FDC, cmd, 1, true); // no stop
00130     i2c->read (REG_FDC, res, 2);
00131     return (int)((int)res[0] << 8) | res[1];
00132 }
00133  
00134 static inline int readMeas(I2C *i2c, int i) {
00135     int msb = readRegister(i2c, FDC_MSB[i]);
00136     return (msb << 8);
00137 }
00138  
00139 static inline void senseCapacitance(I2C *i2c, float (&c)[NUM_DEN], int (&capdac)[NUM_DEN]){
00140     for (int i=0; i<NUM_DEN; i++) {
00141         c[i] = ((float)readMeas(i2c, i)/0x80000);
00142         if (c[i] >= 16) {
00143             c[i] -= 32;
00144         }
00145         if (capdac[i] > 0) {
00146             c[i] += float(capdac[i]) * 3.125f / 0x80000;
00147         }
00148     }
00149 }
00150  
00151 // TODO リファクタリング後の関数(senseCapacitance)が、元の関数と同じ値になるか要検証
00152 /*static void senseCapacitance_orig(float (&c)[NUM_AXES], int (&capdac)[NUM_AXES]){
00153   unsigned int mbb1, mbb2, mbb3; // variables to store 16-bit unsigned integers
00154   char res1[2];
00155   char res2[1];
00156  
00157   for(int i=0; i<NUM_AXES; i++){
00158     i2c.write(REG_FDC, FDC_MSB + i, 1, true);
00159     i2c.read(REG_FDC, res1, 2);
00160  
00161     i2c.write(REG_FDC, FDC_LSB + i, 1, true);
00162     i2c.read(REG_FDC, res2, 1);
00163  
00164     // Below are the calculations to take the 24-bit measurement and convert into capacitance for MEAS1
00165     // * From datasheet: p.16 Section 8.6.1.1 shows the formula (all we need to do is shift the 24-bit value right by 19)
00166     // ** Shifting by 19 takes a 24 bit number and makes it a 5 bit number with 19 bits after the decimal point
00167     mbb1 = res1[0] * 256 + res1[1]; // Puts 16 most significant bits into one integer for left sensor measurement
00168     mbb2 = mbb1 >> 11; // Sets mbb2 to the 5 bits that exist before the decimal point pFの整数部分5bit
00169     mbb3 = 0b0000011111111111 & mbb1; // Sets mbb3 to the 11 bits that exist after the decimal point pFの小数点以下の11bit
00170  
00171     c[i] = (float)mbb2 + (float)mbb3 / 2048 + (float)res2[0] / 524288; // 単位pF
00172     if (capdac[i] > 0) {
00173       c[i] += float(capdac[i]) * float(3.125); //CAPDACによるoffset追加
00174     }
00175   }
00176 }*/
00177  
00178 /*static void displayValues(float (&values)[FDC_NUM][NUM_AXES]) {
00179     for (int j=0; j<FDC_NUM; j++) {
00180         for(int i=0; i<NUM_AXES; i++) {
00181             pc.printf("|%5.2f|", values[j][i]);
00182         }
00183     }
00184     pc.printf("\r\n");
00185 }*/