Feng Hong / Mbed OS Nucleo_rtos_basic
Committer:
hi1000
Date:
Sat Apr 06 05:11:01 2019 +0000
Revision:
10:d1104e320de7
Parent:
9:486f65124378
Child:
11:8702316d7fc8
just backup;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hi1000 0:765cf978c3e5 1 #include "mbed.h"
hi1000 1:eb499e2a1b9b 2 #include <HX711.h>
hi1000 2:61a0169765bf 3 #include <eeprom.h>
hi1000 7:e0c7e624c5fa 4 #include "eeprom_cust.h"
hi1000 5:4585215afd11 5 //#include "digitLCD.h"
hi1000 5:4585215afd11 6 #include "SB1602E.h"
hi1000 9:486f65124378 7
hi1000 7:e0c7e624c5fa 8 extern void analyzePayload();
hi1000 9:486f65124378 9 #if 0
hi1000 7:e0c7e624c5fa 10 extern void scaleCalibration();
hi1000 7:e0c7e624c5fa 11 void init_scale();
hi1000 7:e0c7e624c5fa 12 extern HX711 hx711;
hi1000 9:486f65124378 13 #endif
hi1000 8:6105ffbaf237 14 unsigned char rx_buffer[8], tx_buffer[8];
hi1000 7:e0c7e624c5fa 15 unsigned char rx_length, tx_length;
hi1000 0:765cf978c3e5 16
hi1000 5:4585215afd11 17 #define LCD_1602
hi1000 9:486f65124378 18 #ifdef STM32F207xx
hi1000 5:4585215afd11 19 SB1602E lcd( PB_9, PB_8 ); // SDA, SCL
hi1000 0:765cf978c3e5 20 CAN can1(PD_0, PD_1);
hi1000 0:765cf978c3e5 21 CAN can2(PB_5, PB_6);
hi1000 1:eb499e2a1b9b 22 DigitalOut led1(LED1);
hi1000 1:eb499e2a1b9b 23 DigitalOut led2(LED2);
hi1000 1:eb499e2a1b9b 24 //FlashIAP flashIAP;
hi1000 1:eb499e2a1b9b 25
hi1000 5:4585215afd11 26 //#define LCD_1621
hi1000 5:4585215afd11 27 //digitLCD lcd(PA_5,PA_4,PB_5); // WO, CS, DATA
hi1000 9:486f65124378 28 #endif
hi1000 9:486f65124378 29 #ifdef STM32F303xE
hi1000 9:486f65124378 30 SB1602E lcd(D14, D15 ); // SDA, SCL
hi1000 9:486f65124378 31 CAN can1(PA_11, PA_12); // RD, TD
hi1000 9:486f65124378 32 DigitalOut led1(LED1); // only one LED PA_5
hi1000 9:486f65124378 33 DigitalOut led2(LED2); // only one LED PA_5
hi1000 9:486f65124378 34 #endif
hi1000 7:e0c7e624c5fa 35
hi1000 2:61a0169765bf 36
hi1000 10:d1104e320de7 37 EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C256);
hi1000 9:486f65124378 38 extern void eeprom_test(void);
hi1000 2:61a0169765bf 39 int init_id = 0x537; // first 8 bit is the address
hi1000 1:eb499e2a1b9b 40
hi1000 9:486f65124378 41 uint8_t can_tx_data[8];
hi1000 9:486f65124378 42 uint8_t can_rx_data[8];
hi1000 0:765cf978c3e5 43
hi1000 0:765cf978c3e5 44 void print_char(char c = '*')
hi1000 0:765cf978c3e5 45 {
hi1000 0:765cf978c3e5 46 printf("%c\r\n", c);
hi1000 0:765cf978c3e5 47 fflush(stdout);
hi1000 0:765cf978c3e5 48 }
hi1000 0:765cf978c3e5 49
hi1000 8:6105ffbaf237 50 Thread can_receivethread;
hi1000 8:6105ffbaf237 51 Thread can_handlethread;
hi1000 1:eb499e2a1b9b 52
hi1000 0:765cf978c3e5 53 CANMessage msg;
hi1000 8:6105ffbaf237 54 MemoryPool<CANMessage, 16> can_mpool;
hi1000 8:6105ffbaf237 55 Queue<CANMessage, 16> can_queue;
hi1000 0:765cf978c3e5 56
hi1000 0:765cf978c3e5 57 InterruptIn button1(USER_BUTTON);
hi1000 0:765cf978c3e5 58 volatile bool button1_pressed = false; // Used in the main loop
hi1000 0:765cf978c3e5 59 volatile bool button1_enabled = true; // Used for debouncing
hi1000 0:765cf978c3e5 60 Timeout button1_timeout; // Used for debouncing
hi1000 0:765cf978c3e5 61
hi1000 0:765cf978c3e5 62 // Enables button when bouncing is over
hi1000 0:765cf978c3e5 63 void button1_enabled_cb(void)
hi1000 0:765cf978c3e5 64 {
hi1000 0:765cf978c3e5 65 button1_enabled = true;
hi1000 0:765cf978c3e5 66 }
hi1000 0:765cf978c3e5 67
hi1000 0:765cf978c3e5 68 // ISR handling button pressed event
hi1000 0:765cf978c3e5 69 void button1_onpressed_cb(void)
hi1000 0:765cf978c3e5 70 {
hi1000 0:765cf978c3e5 71 if (button1_enabled) { // Disabled while the button is bouncing
hi1000 0:765cf978c3e5 72 button1_enabled = false;
hi1000 0:765cf978c3e5 73 button1_pressed = true; // To be read by the main loop
hi1000 0:765cf978c3e5 74 button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms
hi1000 0:765cf978c3e5 75 }
hi1000 0:765cf978c3e5 76 }
hi1000 0:765cf978c3e5 77
hi1000 8:6105ffbaf237 78 void can_rxthread()
hi1000 0:765cf978c3e5 79 {
hi1000 9:486f65124378 80 int loop;
hi1000 0:765cf978c3e5 81 while (true) {
hi1000 9:486f65124378 82 #if 1
hi1000 0:765cf978c3e5 83 if(can1.read(msg)) {
hi1000 0:765cf978c3e5 84 print_char();
hi1000 0:765cf978c3e5 85 printf("got message id=%d 0x%08x\r\n", msg.id, msg.id);
hi1000 0:765cf978c3e5 86 // b = *reinterpret_cast<int*>(msg.data);
hi1000 9:486f65124378 87 for (loop = 0; loop < msg.len; loop++)
hi1000 8:6105ffbaf237 88 {
hi1000 9:486f65124378 89 can_rx_data[loop] = msg.data[loop];
hi1000 8:6105ffbaf237 90 }
hi1000 8:6105ffbaf237 91
hi1000 9:486f65124378 92 printf("got data: length:%d\r\n", msg.len);
hi1000 9:486f65124378 93 for (loop = 0; loop < msg.len; loop++)
hi1000 9:486f65124378 94 {
hi1000 9:486f65124378 95 printf("data[%d]=%d\r\n", loop, can_rx_data[loop]);
hi1000 9:486f65124378 96 }
hi1000 0:765cf978c3e5 97 if(msg.id == 1337) {
hi1000 9:486f65124378 98 //only queue the message belongs to you
hi1000 9:486f65124378 99 CANMessage *can_message = can_mpool.alloc();
hi1000 9:486f65124378 100 memcpy((void *)can_message, (void *)&msg, sizeof(msg));
hi1000 9:486f65124378 101 if (!can_queue.full())
hi1000 9:486f65124378 102 can_queue.put(can_message);
hi1000 9:486f65124378 103 else
hi1000 9:486f65124378 104 {
hi1000 9:486f65124378 105 printf("message queue is full. \r\n");
hi1000 9:486f65124378 106 }
hi1000 0:765cf978c3e5 107 led2 = !led2;
hi1000 0:765cf978c3e5 108 }
hi1000 0:765cf978c3e5 109 }
hi1000 9:486f65124378 110 #endif
hi1000 0:765cf978c3e5 111 // wait(0.2);
hi1000 0:765cf978c3e5 112 }
hi1000 0:765cf978c3e5 113 }
hi1000 0:765cf978c3e5 114
hi1000 0:765cf978c3e5 115 int main()
hi1000 0:765cf978c3e5 116 {
hi1000 9:486f65124378 117 int loop = 0;
hi1000 9:486f65124378 118 int8_t ival;
hi1000 9:486f65124378 119 printf("\n\n*** RTOS basic example ***\r\n");
hi1000 9:486f65124378 120
hi1000 9:486f65124378 121 // wait(1);
hi1000 8:6105ffbaf237 122 #ifdef LCD_1621
hi1000 4:40bb33497de4 123 lcd.clear(); // clears display
hi1000 4:40bb33497de4 124 lcd.allsegson();
hi1000 5:4585215afd11 125
hi1000 4:40bb33497de4 126 // lcd.printf("ABCDEFGHI"); // Standard printf function, All ASCII characters will display
hi1000 5:4585215afd11 127 #endif
hi1000 9:486f65124378 128
hi1000 5:4585215afd11 129 #ifdef LCD_1602
hi1000 5:4585215afd11 130 lcd.printf( 0, "Hello world!" ); // line# (0 or 1), string
hi1000 5:4585215afd11 131 lcd.printf( 1, "pi = %.6f", 3.14159265 );
hi1000 6:a9a03663fa23 132 lcd.putcxy(0x55, 5, 1);
hi1000 6:a9a03663fa23 133 lcd.printf(5, 0, "UUU");
hi1000 9:486f65124378 134 lcd.printf(0, 0, "pressed!" );
hi1000 9:486f65124378 135 #endif
hi1000 9:486f65124378 136 #if 0
hi1000 2:61a0169765bf 137 init_scale();
hi1000 9:486f65124378 138 #endif
hi1000 9:486f65124378 139
hi1000 9:486f65124378 140 can_receivethread.start(can_rxthread);
hi1000 9:486f65124378 141 can_handlethread.start(analyzePayload);
hi1000 1:eb499e2a1b9b 142 // flashIAP.init();
hi1000 1:eb499e2a1b9b 143 // printf("Flash start address: 0x%08x Flash Size: %d\r\n", flashIAP.get_flash_start(), flashIAP.get_flash_size());
hi1000 0:765cf978c3e5 144 // can1.reset();
hi1000 0:765cf978c3e5 145 // can2.reset();
hi1000 2:61a0169765bf 146 can1.frequency(100000);
hi1000 0:765cf978c3e5 147 // can2.frequency(100000);
hi1000 0:765cf978c3e5 148 //button1.mode(PullUp); // Activate pull-up
hi1000 9:486f65124378 149
hi1000 2:61a0169765bf 150 button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
hi1000 10:d1104e320de7 151 eeprom_test();
hi1000 0:765cf978c3e5 152
hi1000 0:765cf978c3e5 153 int idx = 0; // Just for printf below
hi1000 9:486f65124378 154 can_tx_data[0] = 0;
hi1000 0:765cf978c3e5 155 while(1) {
hi1000 0:765cf978c3e5 156 if (button1_pressed) { // Set when button is pressed
hi1000 9:486f65124378 157 #if 0
hi1000 1:eb499e2a1b9b 158 printf("scale value %f. \r\n", hx711.getGram());
hi1000 9:486f65124378 159 #endif
hi1000 9:486f65124378 160 can_tx_data[1] = can_tx_data[0]+1;
hi1000 9:486f65124378 161 can_tx_data[2] = can_tx_data[1]+1;
hi1000 9:486f65124378 162 can_tx_data[3] = can_tx_data[2]+1;
hi1000 9:486f65124378 163 can_tx_data[4] = can_tx_data[3]+1;
hi1000 9:486f65124378 164 can_tx_data[5] = can_tx_data[4]+1;
hi1000 9:486f65124378 165 can_tx_data[6] = can_tx_data[5]+1;
hi1000 9:486f65124378 166 can_tx_data[7] = can_tx_data[6]+1;
hi1000 0:765cf978c3e5 167 button1_pressed = false;
hi1000 2:61a0169765bf 168 printf("Button pressed %d\r\n", idx++);
hi1000 9:486f65124378 169 printf("ID=%d data[0]=%d. \r\n", init_id + idx%10, can_tx_data[0]);
hi1000 9:486f65124378 170 #ifdef LCD_1602
hi1000 9:486f65124378 171 lcd.printf(0, 0, "%d ", idx ); // line# (0 or 1), string
hi1000 9:486f65124378 172 #endif
hi1000 9:486f65124378 173 can1.write(CANMessage((init_id + idx%10), reinterpret_cast<char*>(can_tx_data), 8));
hi1000 0:765cf978c3e5 174 led1 = !led1;
hi1000 9:486f65124378 175 can_tx_data[0]++;
hi1000 0:765cf978c3e5 176 }
hi1000 0:765cf978c3e5 177 }
hi1000 9:486f65124378 178
hi1000 9:486f65124378 179 #if 0
hi1000 0:765cf978c3e5 180 while(1) {
hi1000 0:765cf978c3e5 181 // can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), sizeof(a)));
hi1000 0:765cf978c3e5 182 #if
hi1000 0:765cf978c3e5 183 can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), 1));
hi1000 0:765cf978c3e5 184 #endif
hi1000 0:765cf978c3e5 185 printf("loop a=%d\n", a);
hi1000 0:765cf978c3e5 186 led1 = !led1;
hi1000 0:765cf978c3e5 187 a++;
hi1000 0:765cf978c3e5 188 wait(0.2);
hi1000 0:765cf978c3e5 189 }
hi1000 0:765cf978c3e5 190 #endif
hi1000 0:765cf978c3e5 191 }