Feng Hong / Mbed OS Nucleo_rtos_basic
Committer:
hi1000
Date:
Sat Apr 13 05:55:40 2019 +0000
Revision:
11:8702316d7fc8
Parent:
10:d1104e320de7
Child:
12:5cb359f981f3
Pin assignment is confirmed;

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