Feng Hong / Mbed OS Nucleo_rtos_basic
Revision:
19:0356e54240cc
Parent:
18:e6ed582f7022
Child:
20:ec9d4f6a16ac
--- a/main.cpp	Sat Jul 20 03:57:26 2019 +0000
+++ b/main.cpp	Tue Sep 24 14:05:58 2019 +0000
@@ -5,11 +5,35 @@
 //#include "digitLCD.h"
 #include "SB1602E.h"
 #include "yoda2.h"
+#include "TextLCD.h"
 
 EventFlags button_event_flags;
+EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C32);
+extern void eeprom_test(void);
+#ifdef YODA2
+// I2C Communication
+I2C i2c_lcd(PB_9, PB_8); // SDA, SCL
+TextLCD_I2C_N lcd(&i2c_lcd, ST7032_SA, TextLCD::LCD16x2, NC, TextLCD::ST7032_3V3);
+#endif
+#if 1
+int current_weight;
+//EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C256);
+int device_address = DEVICE_DEFAULT_ADDRESS;  // address 11 bits: last 8 bits is device_address
+int device_type;  //address 11 bits: first 3 bits is device_type
+int init_id = 0x00300000;  // first 11 bit is the address
+int init_filter_handle, broadcast_filter_handle;
+int broadcast_id = 0x1ffC0000;
+#endif
+
+
+#if 1
+
 EventFlags LCD_update_flags;
 extern void main_menu();
 extern void analyzePayload();
+extern Device_Type_d device_type_v;
+extern data_field_d can_txdata_frame;
+extern CANMessage tx_message;
 #if 1
 #ifdef STM32F207xx
 HX711 hx711(PB_11, PB_10);// data, clk
@@ -17,8 +41,13 @@
 
 #ifdef STM32F303xE
 //HX711 hx711(D8, D9);// data, clk
+#ifdef OWN_SOLDER_BOARD
 HX711 hx711(PA_14, PA_15);
 #endif
+#ifdef YODA2
+HX711 hx711(PA_1, PA_0);
+#endif
+#endif
 extern void scaleCalibration(bool release_led);
 extern void init_scale();
 Thread scale_thread;
@@ -27,7 +56,7 @@
 unsigned char rx_buffer[8], tx_buffer[8];
 unsigned char rx_length, tx_length;
 
-#define LCD_1602
+//#define LCD_1602
 #ifdef STM32F207xx
 SB1602E lcd( PB_9, PB_8 );  //  SDA, SCL
 CAN can1(PD_0, PD_1);
@@ -40,61 +69,59 @@
 //digitLCD lcd(PA_5,PA_4,PB_5); // WO, CS, DATA
 #endif
 #ifdef STM32F303xE
+
+#ifdef LCD_1602
 SB1602E lcd(D14, D15 );  //  SDA, SCL
+#endif
 CAN can1(PA_11, PA_12); // RD, TD
 DigitalOut led1(LED1);  // only one LED PA_5
 DigitalOut led2(LED2);  // only one LED PA_5
 #endif
 
+#if 0
 DigitalOut output1(PC_3);
 DigitalOut output2(PC_2);
 DigitalOut output3(PB_7);
 DigitalIn input1(PC_6);
 DigitalIn input2(PC_8);
-int current_weight;
-EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C256);
-extern void eeprom_test(void);
-int device_address = DEVICE_DEFAULT_ADDRESS;  // address 11 bits: last 8 bits is device_address
-int device_type;  //address 11 bits: first 3 bits is device_type
-int init_id = 0x00300000;  // first 11 bit is the address
-int init_filter_handle, broadcast_filter_handle;
-int broadcast_id = 0x1ffC0000;
+#endif
+
 
 uint8_t can_tx_data[8];
 uint8_t can_rx_data[8];
-
+#endif
 void print_char(char c = '*')
 {
     printf("%c\r\n", c);
     fflush(stdout);
 }
 
-
+#if 1
 Thread can_receivethread;
 Thread can_handlethread;
 Thread mainmenu_thread;
-
+bool can_register_success = false; 
 CANMessage  msg;
 MemoryPool<CANMessage, 16> can_mpool;
 Queue<CANMessage, 16> can_queue;
-
+#endif
 InterruptIn button0(USER_BUTTON);
 volatile bool button0_pressed = false; // Used in the main loop
 volatile bool button0_enabled = true; // Used for debouncing
 Timeout button0_timeout; // Used for debouncing
-InterruptIn button1(PB_4);
+InterruptIn button1(BUTTON1);
 volatile bool button1_pressed = false; // Used in the main loop
 volatile bool button1_enabled = true; // Used for debouncing
 Timeout button1_timeout; // Used for debouncing
-InterruptIn button2(PC_1);
+InterruptIn button2(BUTTON2);
 volatile bool button2_pressed = false; // Used in the main loop
 volatile bool button2_enabled = true; // Used for debouncing
 Timeout button2_timeout; // Used for debouncing
-InterruptIn button3(PC_7);
+InterruptIn button3(BUTTON3);
 volatile bool button3_pressed = false; // Used in the main loop
 volatile bool button3_enabled = true; // Used for debouncing
 Timeout button3_timeout; // Used for debouncing
-InterruptIn button4(PA_9);
+InterruptIn button4(BUTTON4);
 volatile bool button4_pressed = false; // Used in the main loop
 volatile bool button4_enabled = true; // Used for debouncing
 Timeout button4_timeout; // Used for debouncing
@@ -247,6 +274,7 @@
     }
 }
 //button4--
+#if 1
 void can_sendData(int can_id, uint8_t *tx_data, int length)
 {
     CANMessage txmsg;
@@ -263,7 +291,7 @@
     txmsg.data[6] = tx_data[6];
     txmsg.data[7] = tx_data[7];                            
     
-    printf("can_sendData can_id=0x%08x \r\n", can_id);
+//    printf("can_sendData can_id=0x%08x \r\n", can_id);
     can1.write(txmsg);    
 }
 
@@ -304,27 +332,65 @@
 //        wait(0.2);
     }
 }
-
+#endif
+void i2c_scanner(PinName sda, PinName scl)
+{
+    I2C i2c(sda, scl); 
+    
+    int error, address;
+    int nDevices;
+    
+    i2c.frequency(100000);
+    printf("i2c_scanner sda=%d scl=%d \r\n", (int)sda, (int)scl); 
+    printf("Scanning...\r\n");
+    
+    nDevices = 0;
+    
+    for(address = 0; address < 127; address++ ) 
+    {
+        i2c.start();
+//        error = i2c.write(address << 1); //We shift it left because mbed takes in 8 bit addreses
+        error = i2c.write(address << 1, "1", 1, false);
+        i2c.stop();
+        if (error == 0)
+        {
+          printf("I2C device found at address 7bit:0x%X (8bit:0x%X)\r\n", address, (address<<1)); //Returns 8-bit addres
+          nDevices++;
+        }
+    }
+    if (nDevices == 0)
+        printf("No I2C devices found\r\n");
+    else
+        printf("\r\ndone\r\n");
+}
 int main()
 {
     int loop = 0;
     int8_t ival;
+    unsigned int can_id;
     printf("\n\n*** RTOS starts ***\r\n");
   
 //    wait(1);
+#if 1
     ep.read((uint32_t)EEPROM_DEVICE_ADDRESS_ADDRESS,device_address); 
     printf("EEPROM: read device address:%d 0x%08x\r\n", device_address, device_address);
     if ((device_address == 0) || (device_address == 0xFFFFFFFF))
         device_address = DEVICE_DEFAULT_ADDRESS;
     device_type = (device_address & 0x00000700) >> 8;
-    
+    device_type_v = (Device_Type_d)device_type;
 #ifdef LCD_1621
     lcd.clear();            // clears display
     lcd.allsegson();
    
  //   lcd.printf("ABCDEFGHI"); // Standard printf function, All ASCII characters will display
 #endif  
-
+#ifdef YODA2
+    lcd.cls();
+    lcd.setContrast(31);
+    lcd.setCursor(TextLCD::CurOff_BlkOff);
+    lcd.setAddress(0,0);
+    lcd.printf("Hello Yoda2!");
+#endif
 #ifdef LCD_1602
 //    lcd.printf( 0, "Hello world!" );    //  line# (0 or 1), string
 //   lcd.printf( 1, "pi = %.6f", 3.14159265 );
@@ -333,11 +399,9 @@
 //    lcd.printf(0, 0, "pressed!" );      
 #endif   
 
-    input1.mode(PullUp);
-    input2.mode(PullUp);
+//    input1.mode(PullUp);
+//    input2.mode(PullUp);
 
-//    flashIAP.init();
-//    printf("Flash start address: 0x%08x Flash Size: %d\r\n", flashIAP.get_flash_start(), flashIAP.get_flash_size());
 //    can1.reset();
 //    can2.reset();
     printf("device_address =0x%08x \r\n", (device_address<<18));
@@ -350,7 +414,11 @@
     //button0.mode(PullUp); // Activate pull-up
     can_receivethread.start(can_rxthread);  
     can_handlethread.start(analyzePayload);   
-
+#endif
+    button1.mode(PullUp);
+    button2.mode(PullUp);
+    button3.mode(PullUp);
+    button4.mode(PullUp);
     button0.fall(callback(button0_onpressed_cb)); // Attach ISR to handle button press event
     button0.rise(callback(button0_onpressed_cb)); // Attach ISR to handle button press event
     button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
@@ -361,17 +429,48 @@
     button3.rise(callback(button3_onpressed_cb)); // Attach ISR to handle button press event
     button4.fall(callback(button4_onpressed_cb)); // Attach ISR to handle button press event
     button4.rise(callback(button4_onpressed_cb)); // Attach ISR to handle button press event    
+//    i2c_scanner(PB_9, PB_8);  
 //    eeprom_test();
 
 #if 1  
 //    scaleCalibration(true);
-    init_scale();
-    scale_thread.start(scale_reading);
+    switch (device_type_v)
+    {
+        case CupTrack:
+        case JamTrack:
+        case TeaTrack:
+            break;
+        case Tea:
+        case Jam:
+            init_scale();
+            scale_thread.start(scale_reading);
+            break;
+        case Shaker:
+            break;
+        default:
+            break;        
+    }
+
     mainmenu_thread.start(main_menu);
 
 #endif
     while(1) {
-        wait(1);
+        wait(2);    
+#if 1               
+        if (!can_register_success)
+        {    
+            can_txdata_frame.cmd = COMMAND_REGISTER;
+            can_txdata_frame.value1 = 0;
+            can_txdata_frame.value2 = 0;
+            can_txdata_frame.value3 = 0;
+            memcpy(can_tx_data, (unsigned char *)&can_txdata_frame, sizeof(can_tx_data));
+//            printf("cmd=0x%08x value1=0x%08x size=%d %d\r\n", can_txdata_frame.cmd, can_txdata_frame.value1, sizeof(can_tx_data), sizeof(can_txdata_frame));
+//            printf("data 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x \r\n", can_tx_data[0], can_tx_data[1], can_tx_data[2], can_tx_data[3], can_tx_data[4], can_tx_data[5], can_tx_data[6], can_tx_data[7]);  
+            can_id = (RASPBERRY_PI_CAN_ADDRESS << 18) | (((device_type << 8) |device_address) << 7) | 0x80000000;
+//            printf("device_address = 0x%08x can_id=0x%08x \r\n", device_address, can_id);
+            can_sendData(can_id, can_tx_data, 8);        
+        }
+#endif        
     }
 
 #if 0