test

Dependencies:   BLE_API nRF51822 mbed

Fork of KS7 by masaaki makabe

Revision:
10:95c1e5a218d5
Parent:
9:a7bd5ae66d6d
Child:
11:be439189e3f7
--- a/main.cpp	Tue Nov 24 09:57:19 2015 +0000
+++ b/main.cpp	Fri Dec 04 09:30:43 2015 +0000
@@ -37,12 +37,6 @@
 #define BRIGHTNESS_MINVALUE (0.0)
 #define BRIGHTNESS_MAXVALUE (1.0)
 
-// UART for debug
-#ifdef UART_DEBUG
-Serial pc(P0_9, P0_8);// TX=P0_9
-#define UART_BAUD_RATE   (9600UL)
-#endif
-
 // Properties
 io io;
 Ticker tk;
@@ -54,6 +48,16 @@
 int led_mode = MODE_OFF;
 float led_brightness = BRIGHTNESS_MINVALUE;
 
+// UART for debug
+
+#ifdef UART_DEBUG
+Serial pc(P0_9, P0_8);// TX=P0_9
+#define UART_BAUD_RATE   (9600UL)
+#define DEBUG(...)              { pc.printf(__VA_ARGS__); }
+#else
+#define DEBUG(...) {}
+#endif
+
 // BLE 
 BLEDevice ble;
 Gap::ConnectionParams_t connectionParams;
@@ -89,8 +93,8 @@
     bool pushing = false;
     float value_x,value_y;
     
-    value_x = io.get_x();
-    value_y = io.get_y();
+//    value_x = io.get_x();
+//    value_y = io.get_y();
     
     if(PUSH_MINTHRESHOLD >= value_x || PUSH_MAXTHRESHOLD <= value_x){
         pushing = true;
@@ -121,16 +125,37 @@
 
 void ticker_callback()
 {
+
+//        pc.printf("!!!\r\n");
+/*
+    while(1){
+    adc_dat[0] = 0x01; // config reg.
+//    adc_dat[1] = 0xc4 | (ch << 4); // single conv. & FS=+-2.048V & conv.
+    adc_dat[1] = 0xc4;
+    adc_dat[2] = 0x83; // 8SPS & disable comp.
+    pc.printf("adc_dat: %02x %02x\r\n", adc_dat[0], adc_dat[1]);
+//    i2c.write(I2C_ADDR_ADS1115, adc_dat, 3);
+    adc_dat[0] = 0x00;
+//    i2c.write(I2C_ADDR_ADS1115, adc_dat, 1);
+//    i2c.read(I2C_ADDR_ADS1115, adc_dat, 2);
+    uint8_t dh, dl;
+    dh = adc_dat[0];
+    dl = adc_dat[1];
+        pc.printf("%d %d\r\n", dh, dl);
+//    pc.printf("ADC result=%02x %02x\r\n", dh, dl);
+    }
+*/
+
     switch(led_mode){
         case MODE_OFF:
             if(check_joystick()){
-                io.display_value = 0;
+//                io.display_value = 0;
                 led_mode = MODE_START;
             }
         break;
         case MODE_START:
             led_brightness += BRIGHTNESS_ADDVALUE;
-            io.display(led_brightness);
+//            io.display(led_brightness);
             if(led_brightness >= BRIGHTNESS_MAXVALUE){
                 led_mode = MODE_ON;
                 ble.startAdvertising();
@@ -138,9 +163,9 @@
         break;
         case MODE_ON:
             if(!check_joystick()){
-                io.analog_pow(1); // turn analog power on
-                weight = io.get_weight() * 9999.0;
-                io.display_value = (uint16_t)weight; // dummy display
+//                io.analog_pow(1); // turn analog power on
+//                weight = io.get_weight() * 9999.0;
+//                io.display_value = (uint16_t)weight; // dummy display
                 if(++update_counter >= 5){
                     weight_data = quick_ieee11073_from_float(weight);
                     ble.updateCharacteristicValue(WeightMeasurement.getValueAttribute().getHandle(),
@@ -148,7 +173,7 @@
                                                                     sizeof(weight_data));
                     update_counter = 0;
                 }  
-                io.analog_pow(0); // turn analog power off 
+//                io.analog_pow(0); // turn analog power off 
             }else{
                 led_mode = MODE_END;
                 if(ble.getGapState().connected){
@@ -161,7 +186,7 @@
         break;
         case MODE_END:
             led_brightness -= BRIGHTNESS_ADDVALUE;
-            io.display(led_brightness);
+//            io.display(led_brightness);
             if(led_brightness <= BRIGHTNESS_MINVALUE){
                 led_mode = MODE_OFF;
             }
@@ -225,14 +250,48 @@
 
 int main()
 {
-    BleInitialize();
-    
 #ifdef UART_DEBUG
     pc.baud(UART_BAUD_RATE);
 #endif
+#ifndef PCB_VER1
+    // set XTAL=32MHz for TaiyoYuden's module
+    DEBUG("Initializing XTALFREQ\n\r");
+    SystemCoreClock = 32000000;
+    DEBUG("UICR->XTALFREQ=%x\r\n", NRF_UICR->XTALFREQ);
+    if (NRF_UICR->XTALFREQ == 0xffffffff){
+        DEBUG("updating UICR->XTALFREQ...");
+        NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos; 
+        while (NRF_NVMC->READY == NVMC_READY_READY_Busy){} 
+        *(uint32_t *)0x10001008 = 0xFFFFFF00; 
+        NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos; 
+        while (NRF_NVMC->READY == NVMC_READY_READY_Busy){} 
+        DEBUG("done");
+        NVIC_SystemReset();
+    } 
+    DEBUG("UICR->XTALFREQ=%x\r\n", NRF_UICR->XTALFREQ);
+#endif
+
+    BleInitialize();
     
     tk.attach_us(&ticker_callback, LED_INTERVAL_NSEC);
-    
+/*
+    io.display(0.5);
+    io.display_value = 1111;    
+    for (int i = 0; i < 10; i++){
+        wait(0.1);
+        io.display_value += 1111;
+    }
+*/
+    while(1){
+/*
+        int ch;
+        ch = 0; pc.printf("%02x\r\n", 0xc4 | (ch << 4));
+        ch = 1; pc.printf("%02x\r\n", 0xc4 | (ch << 4));
+        ch = 2; pc.printf("%02x\r\n", 0xc4 | (ch << 4));
+*/
+//        DEBUG("%f %f %f\r\n", io._get_adc(0), io._get_adc(1), io._get_adc(2));
+        DEBUG("%x %x %x\r\n", io._get_adc(0), io._get_adc(1), io._get_adc(2));
+    }
     for (;; ) {
         ble.waitForEvent();
     }