Feng Hong / Mbed OS Nucleo_rtos_basic
Revision:
9:486f65124378
Parent:
8:6105ffbaf237
Child:
10:d1104e320de7
--- a/main.cpp	Sat Mar 23 09:18:03 2019 +0000
+++ b/main.cpp	Sat Apr 06 04:06:53 2019 +0000
@@ -4,15 +4,18 @@
 #include "eeprom_cust.h"
 //#include "digitLCD.h"
 #include "SB1602E.h"
+
 extern void analyzePayload();
+#if 0
 extern void scaleCalibration();
 void init_scale();
 extern HX711 hx711;
-
+#endif
 unsigned char rx_buffer[8], tx_buffer[8];
 unsigned char rx_length, tx_length;
 
 #define LCD_1602
+#ifdef STM32F207xx
 SB1602E lcd( PB_9, PB_8 );  //  SDA, SCL
 CAN can1(PD_0, PD_1);
 CAN can2(PB_5, PB_6);
@@ -22,14 +25,21 @@
 
 //#define LCD_1621
 //digitLCD lcd(PA_5,PA_4,PB_5); // WO, CS, DATA
-
+#endif
+#ifdef STM32F303xE
+SB1602E lcd(D14, D15 );  //  SDA, SCL
+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
 
 
 extern EEPROM ep;
+extern void eeprom_test(void);
 int init_id = 0x537;  // first 8 bit is the address
 
-int a = 0;
-int b = 0;
+uint8_t can_tx_data[8];
+uint8_t can_rx_data[8];
 
 void print_char(char c = '*')
 {
@@ -67,56 +77,68 @@
 
 void can_rxthread()
 {
+    int loop;
     while (true) {
- #if 1       
+ #if 1      
         if(can1.read(msg)) {
             print_char();
             printf("got message id=%d 0x%08x\r\n", msg.id, msg.id);
 //            b = *reinterpret_cast<int*>(msg.data);
-            b = msg.data[0];
-            CANMessage *can_message = can_mpool.alloc();
-            memcpy((void *)can_message, (void *)&msg, sizeof(msg)); 
-            if (!can_queue.full())
-                can_queue.put(can_message);
-            else
+            for (loop = 0; loop < msg.len; loop++)
             {
-                printf("message queue is full. \r\n");    
+                can_rx_data[loop] = msg.data[loop];
             }
             
-            printf("got data %d 0x%08x \r\n", b, b);
+            printf("got data: length:%d\r\n", msg.len);
+            for (loop = 0; loop < msg.len; loop++)
+            {
+                printf("data[%d]=%d\r\n", loop, can_rx_data[loop]);
+            }           
             if(msg.id == 1337) {
+                //only queue the message belongs to you
+                CANMessage *can_message = can_mpool.alloc();
+                memcpy((void *)can_message, (void *)&msg, sizeof(msg)); 
+                if (!can_queue.full())
+                    can_queue.put(can_message);
+                else
+                {
+                    printf("message queue is full. \r\n");    
+                }                
                 led2 = !led2;
- 
-                b = *reinterpret_cast<int*>(msg.data);
-                printf("got message %d\r\n", b);
-                if(b % 5 == 0)
-                    led2 = !led2;
              }
         }
+#endif
 //        wait(0.2);
-#endif
     }
 }
 
 int main()
 {
-    wait(1);
+    int loop = 0;
+    int8_t ival;
+    printf("\n\n*** RTOS basic example ***\r\n");
+  
+//    wait(1);
 #ifdef LCD_1621
     lcd.clear();            // clears display
     lcd.allsegson();
    
  //   lcd.printf("ABCDEFGHI"); // Standard printf function, All ASCII characters will display
 #endif  
-    printf("\n\n*** RTOS basic example ***\r\n");
+
 #ifdef LCD_1602
     lcd.printf( 0, "Hello world!" );    //  line# (0 or 1), string
     lcd.printf( 1, "pi = %.6f", 3.14159265 );
     lcd.putcxy(0x55, 5, 1);
     lcd.printf(5, 0, "UUU");
-#endif    
+    lcd.printf(0, 0, "pressed!" );      
+#endif   
+#if 0  
     init_scale();
-    can_receivethread.start(can_rxthread);
-    can_handlethread.start(analyzePayload);
+#endif
+    
+    can_receivethread.start(can_rxthread);  
+    can_handlethread.start(analyzePayload);   
 //    flashIAP.init();
 //    printf("Flash start address: 0x%08x Flash Size: %d\r\n", flashIAP.get_flash_start(), flashIAP.get_flash_size());
 //    can1.reset();
@@ -124,23 +146,38 @@
     can1.frequency(100000);
 //    can2.frequency(100000);
     //button1.mode(PullUp); // Activate pull-up
+
     button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
 //    eeprom_test();
 
     int idx = 0; // Just for printf below
-
+    can_tx_data[0] = 0;
     while(1) {
         if (button1_pressed) { // Set when button is pressed
+#if 0        
             printf("scale value %f. \r\n", hx711.getGram());
+#endif
+            can_tx_data[1] = can_tx_data[0]+1;
+            can_tx_data[2] = can_tx_data[1]+1;
+            can_tx_data[3] = can_tx_data[2]+1;
+            can_tx_data[4] = can_tx_data[3]+1;
+            can_tx_data[5] = can_tx_data[4]+1;
+            can_tx_data[6] = can_tx_data[5]+1;
+            can_tx_data[7] = can_tx_data[6]+1; 
             button1_pressed = false;
             printf("Button pressed %d\r\n", idx++);
-            printf("ID=%d. \r\n", init_id + idx%10);
-            can1.write(CANMessage((init_id + idx%10), reinterpret_cast<char*>(&a), 1));            
+            printf("ID=%d data[0]=%d. \r\n", init_id + idx%10, can_tx_data[0]);
+#ifdef LCD_1602
+            lcd.printf(0, 0, "%d ", idx );    //  line# (0 or 1), string
+#endif   
+            ep.read(0,ival);
+            can1.write(CANMessage((init_id + idx%10), reinterpret_cast<char*>(can_tx_data), 8));            
             led1 = !led1;
-            a++;
+            can_tx_data[0]++;
         }
     }
- #if 0
+  
+#if 0
     while(1) {
  //       can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), sizeof(a)));
 #if