Feng Hong / Mbed OS Nucleo_rtos_basic
Committer:
hi1000
Date:
Sat Oct 19 02:10:12 2019 +0000
Revision:
20:ec9d4f6a16ac
Parent:
19:0356e54240cc
Child:
21:5c6b3657c3cb
The first version of ROT-CANBUS R01/0819

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 18:e6ed582f7022 7 #include "yoda2.h"
hi1000 19:0356e54240cc 8 #include "TextLCD.h"
hi1000 20:ec9d4f6a16ac 9 #include "DS3231.h"
hi1000 20:ec9d4f6a16ac 10
hi1000 20:ec9d4f6a16ac 11
hi1000 20:ec9d4f6a16ac 12 DigitalOut relay4(RELAY_4);
hi1000 20:ec9d4f6a16ac 13 DigitalOut relay3(RELAY_3);
hi1000 20:ec9d4f6a16ac 14 DigitalOut relay2(RELAY_2);
hi1000 20:ec9d4f6a16ac 15 DigitalOut relay1(RELAY_1);
hi1000 20:ec9d4f6a16ac 16 DigitalIn din1(DIN1);
hi1000 20:ec9d4f6a16ac 17 DigitalIn din2(DIN2);
hi1000 20:ec9d4f6a16ac 18 DigitalIn din3(DIN3);
hi1000 20:ec9d4f6a16ac 19 DigitalIn din4(DIN4);
hi1000 20:ec9d4f6a16ac 20 DigitalIn din5(DIN5);
hi1000 20:ec9d4f6a16ac 21 DigitalIn din6(DIN6);
hi1000 20:ec9d4f6a16ac 22 DigitalIn din7(DIN7);
hi1000 20:ec9d4f6a16ac 23 DigitalIn din8(DIN8);
hi1000 20:ec9d4f6a16ac 24
hi1000 20:ec9d4f6a16ac 25 extern int moveMotor(int motornumber, int distance_mm, bool direction, bool freemove, bool gobackhome);
hi1000 20:ec9d4f6a16ac 26 extern int moveMotor1(int distance_mm, bool direction, bool freemove, bool gobackhome);
hi1000 20:ec9d4f6a16ac 27 extern int moveMotor2(int distance_mm, bool direction, bool freemove, bool gobackhome);
hi1000 20:ec9d4f6a16ac 28 extern int moveMotor3(int distance_mm, bool direction, bool freemove, bool gobackhome);
hi1000 20:ec9d4f6a16ac 29 extern int moveMotor3Until(bool direction, DigitalIn sensorpin);
hi1000 20:ec9d4f6a16ac 30 extern DigitalOut motor3_pul;
hi1000 20:ec9d4f6a16ac 31 extern DigitalOut motor3_en;
hi1000 20:ec9d4f6a16ac 32 extern DigitalOut motor3_dir;
hi1000 9:486f65124378 33
hi1000 14:2e17a27f56b2 34 EventFlags button_event_flags;
hi1000 19:0356e54240cc 35 EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C32);
hi1000 19:0356e54240cc 36 extern void eeprom_test(void);
hi1000 19:0356e54240cc 37 #ifdef YODA2
hi1000 19:0356e54240cc 38 // I2C Communication
hi1000 20:ec9d4f6a16ac 39 I2C i2c_lcd(LCD_SDA, LCD_SCL); // SDA, SCL
hi1000 19:0356e54240cc 40 TextLCD_I2C_N lcd(&i2c_lcd, ST7032_SA, TextLCD::LCD16x2, NC, TextLCD::ST7032_3V3);
hi1000 19:0356e54240cc 41 #endif
hi1000 19:0356e54240cc 42 #if 1
hi1000 19:0356e54240cc 43 int current_weight;
hi1000 19:0356e54240cc 44 //EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C256);
hi1000 19:0356e54240cc 45 int device_address = DEVICE_DEFAULT_ADDRESS; // address 11 bits: last 8 bits is device_address
hi1000 19:0356e54240cc 46 int device_type; //address 11 bits: first 3 bits is device_type
hi1000 19:0356e54240cc 47 int init_id = 0x00300000; // first 11 bit is the address
hi1000 19:0356e54240cc 48 int init_filter_handle, broadcast_filter_handle;
hi1000 19:0356e54240cc 49 int broadcast_id = 0x1ffC0000;
hi1000 19:0356e54240cc 50 #endif
hi1000 19:0356e54240cc 51
hi1000 19:0356e54240cc 52
hi1000 19:0356e54240cc 53 #if 1
hi1000 19:0356e54240cc 54
hi1000 14:2e17a27f56b2 55 EventFlags LCD_update_flags;
hi1000 14:2e17a27f56b2 56 extern void main_menu();
hi1000 7:e0c7e624c5fa 57 extern void analyzePayload();
hi1000 19:0356e54240cc 58 extern Device_Type_d device_type_v;
hi1000 19:0356e54240cc 59 extern data_field_d can_txdata_frame;
hi1000 19:0356e54240cc 60 extern CANMessage tx_message;
hi1000 11:8702316d7fc8 61 #if 1
hi1000 11:8702316d7fc8 62 #ifdef STM32F207xx
hi1000 11:8702316d7fc8 63 HX711 hx711(PB_11, PB_10);// data, clk
hi1000 11:8702316d7fc8 64 #endif
hi1000 11:8702316d7fc8 65
hi1000 11:8702316d7fc8 66 #ifdef STM32F303xE
hi1000 11:8702316d7fc8 67 //HX711 hx711(D8, D9);// data, clk
hi1000 19:0356e54240cc 68 #ifdef OWN_SOLDER_BOARD
hi1000 11:8702316d7fc8 69 HX711 hx711(PA_14, PA_15);
hi1000 11:8702316d7fc8 70 #endif
hi1000 19:0356e54240cc 71 #ifdef YODA2
hi1000 20:ec9d4f6a16ac 72 HX711 hx711(SCALE_SDA, SCALE_SCL);
hi1000 19:0356e54240cc 73 #endif
hi1000 19:0356e54240cc 74 #endif
hi1000 18:e6ed582f7022 75 extern void scaleCalibration(bool release_led);
hi1000 11:8702316d7fc8 76 extern void init_scale();
hi1000 11:8702316d7fc8 77 Thread scale_thread;
hi1000 11:8702316d7fc8 78 extern void scale_reading();
hi1000 9:486f65124378 79 #endif
hi1000 8:6105ffbaf237 80 unsigned char rx_buffer[8], tx_buffer[8];
hi1000 7:e0c7e624c5fa 81 unsigned char rx_length, tx_length;
hi1000 0:765cf978c3e5 82
hi1000 19:0356e54240cc 83 //#define LCD_1602
hi1000 9:486f65124378 84 #ifdef STM32F207xx
hi1000 5:4585215afd11 85 SB1602E lcd( PB_9, PB_8 ); // SDA, SCL
hi1000 0:765cf978c3e5 86 CAN can1(PD_0, PD_1);
hi1000 0:765cf978c3e5 87 CAN can2(PB_5, PB_6);
hi1000 1:eb499e2a1b9b 88 DigitalOut led1(LED1);
hi1000 1:eb499e2a1b9b 89 DigitalOut led2(LED2);
hi1000 1:eb499e2a1b9b 90 //FlashIAP flashIAP;
hi1000 1:eb499e2a1b9b 91
hi1000 5:4585215afd11 92 //#define LCD_1621
hi1000 5:4585215afd11 93 //digitLCD lcd(PA_5,PA_4,PB_5); // WO, CS, DATA
hi1000 9:486f65124378 94 #endif
hi1000 9:486f65124378 95 #ifdef STM32F303xE
hi1000 19:0356e54240cc 96
hi1000 19:0356e54240cc 97 #ifdef LCD_1602
hi1000 9:486f65124378 98 SB1602E lcd(D14, D15 ); // SDA, SCL
hi1000 19:0356e54240cc 99 #endif
hi1000 20:ec9d4f6a16ac 100 CAN can1(CAN_RD, CAN_TD); // RD, TD
hi1000 9:486f65124378 101 DigitalOut led1(LED1); // only one LED PA_5
hi1000 9:486f65124378 102 DigitalOut led2(LED2); // only one LED PA_5
hi1000 9:486f65124378 103 #endif
hi1000 7:e0c7e624c5fa 104
hi1000 19:0356e54240cc 105 #if 0
hi1000 12:5cb359f981f3 106 DigitalOut output1(PC_3);
hi1000 12:5cb359f981f3 107 DigitalOut output2(PC_2);
hi1000 12:5cb359f981f3 108 DigitalOut output3(PB_7);
hi1000 12:5cb359f981f3 109 DigitalIn input1(PC_6);
hi1000 12:5cb359f981f3 110 DigitalIn input2(PC_8);
hi1000 19:0356e54240cc 111 #endif
hi1000 19:0356e54240cc 112
hi1000 1:eb499e2a1b9b 113
hi1000 9:486f65124378 114 uint8_t can_tx_data[8];
hi1000 9:486f65124378 115 uint8_t can_rx_data[8];
hi1000 19:0356e54240cc 116 #endif
hi1000 0:765cf978c3e5 117 void print_char(char c = '*')
hi1000 0:765cf978c3e5 118 {
hi1000 0:765cf978c3e5 119 printf("%c\r\n", c);
hi1000 0:765cf978c3e5 120 fflush(stdout);
hi1000 0:765cf978c3e5 121 }
hi1000 0:765cf978c3e5 122
hi1000 19:0356e54240cc 123 #if 1
hi1000 8:6105ffbaf237 124 Thread can_receivethread;
hi1000 8:6105ffbaf237 125 Thread can_handlethread;
hi1000 14:2e17a27f56b2 126 Thread mainmenu_thread;
hi1000 19:0356e54240cc 127 bool can_register_success = false;
hi1000 0:765cf978c3e5 128 CANMessage msg;
hi1000 8:6105ffbaf237 129 MemoryPool<CANMessage, 16> can_mpool;
hi1000 8:6105ffbaf237 130 Queue<CANMessage, 16> can_queue;
hi1000 19:0356e54240cc 131 #endif
hi1000 12:5cb359f981f3 132 InterruptIn button0(USER_BUTTON);
hi1000 12:5cb359f981f3 133 volatile bool button0_pressed = false; // Used in the main loop
hi1000 12:5cb359f981f3 134 volatile bool button0_enabled = true; // Used for debouncing
hi1000 12:5cb359f981f3 135 Timeout button0_timeout; // Used for debouncing
hi1000 19:0356e54240cc 136 InterruptIn button1(BUTTON1);
hi1000 0:765cf978c3e5 137 volatile bool button1_pressed = false; // Used in the main loop
hi1000 0:765cf978c3e5 138 volatile bool button1_enabled = true; // Used for debouncing
hi1000 0:765cf978c3e5 139 Timeout button1_timeout; // Used for debouncing
hi1000 19:0356e54240cc 140 InterruptIn button2(BUTTON2);
hi1000 12:5cb359f981f3 141 volatile bool button2_pressed = false; // Used in the main loop
hi1000 12:5cb359f981f3 142 volatile bool button2_enabled = true; // Used for debouncing
hi1000 12:5cb359f981f3 143 Timeout button2_timeout; // Used for debouncing
hi1000 19:0356e54240cc 144 InterruptIn button3(BUTTON3);
hi1000 12:5cb359f981f3 145 volatile bool button3_pressed = false; // Used in the main loop
hi1000 12:5cb359f981f3 146 volatile bool button3_enabled = true; // Used for debouncing
hi1000 12:5cb359f981f3 147 Timeout button3_timeout; // Used for debouncing
hi1000 19:0356e54240cc 148 InterruptIn button4(BUTTON4);
hi1000 12:5cb359f981f3 149 volatile bool button4_pressed = false; // Used in the main loop
hi1000 12:5cb359f981f3 150 volatile bool button4_enabled = true; // Used for debouncing
hi1000 12:5cb359f981f3 151 Timeout button4_timeout; // Used for debouncing
hi1000 12:5cb359f981f3 152
hi1000 0:765cf978c3e5 153
hi1000 0:765cf978c3e5 154 // Enables button when bouncing is over
hi1000 12:5cb359f981f3 155 //button0
hi1000 12:5cb359f981f3 156 void button0_enabled_cb(void)
hi1000 12:5cb359f981f3 157 {
hi1000 12:5cb359f981f3 158 int button_status;
hi1000 12:5cb359f981f3 159 button_status = button0.read();
hi1000 12:5cb359f981f3 160 if (button_status == 0)
hi1000 12:5cb359f981f3 161 {
hi1000 12:5cb359f981f3 162 printf("button0 down\r\n");
hi1000 20:ec9d4f6a16ac 163 // scaleCalibration(true);
hi1000 20:ec9d4f6a16ac 164 moveMotor3(100, true, true, false);
hi1000 14:2e17a27f56b2 165 button_status = button0.read();
hi1000 14:2e17a27f56b2 166 if (button_status == 0)
hi1000 14:2e17a27f56b2 167 {
hi1000 14:2e17a27f56b2 168 printf("button0 hold\r\n");
hi1000 14:2e17a27f56b2 169 }
hi1000 14:2e17a27f56b2 170 else
hi1000 14:2e17a27f56b2 171 {
hi1000 14:2e17a27f56b2 172 printf("button0 press hold and release\r\n");
hi1000 14:2e17a27f56b2 173 }
hi1000 12:5cb359f981f3 174 }
hi1000 12:5cb359f981f3 175 else
hi1000 12:5cb359f981f3 176 printf("button0 released\r\n");
hi1000 12:5cb359f981f3 177 button0_enabled = true;
hi1000 12:5cb359f981f3 178 }
hi1000 12:5cb359f981f3 179
hi1000 12:5cb359f981f3 180 // ISR handling button pressed event
hi1000 12:5cb359f981f3 181 void button0_onpressed_cb(void)
hi1000 12:5cb359f981f3 182 {
hi1000 12:5cb359f981f3 183 if (button0_enabled) { // Disabled while the button is bouncing
hi1000 12:5cb359f981f3 184 button0_enabled = false;
hi1000 12:5cb359f981f3 185 button0_pressed = true; // To be read by the main loop
hi1000 12:5cb359f981f3 186 button0_timeout.attach(callback(button0_enabled_cb), 0.3); // Debounce time 300 ms
hi1000 12:5cb359f981f3 187 }
hi1000 12:5cb359f981f3 188 }
hi1000 12:5cb359f981f3 189 //button0--
hi1000 12:5cb359f981f3 190 //button1
hi1000 0:765cf978c3e5 191 void button1_enabled_cb(void)
hi1000 0:765cf978c3e5 192 {
hi1000 12:5cb359f981f3 193 int button_status;
hi1000 12:5cb359f981f3 194 button_status = button1.read();
hi1000 12:5cb359f981f3 195 if (button_status == 0)
hi1000 12:5cb359f981f3 196 {
hi1000 12:5cb359f981f3 197 printf("button1 down\r\n");
hi1000 14:2e17a27f56b2 198 button_event_flags.set(BUTTON1_HOLD_EVENT);
hi1000 12:5cb359f981f3 199 }
hi1000 12:5cb359f981f3 200 else
hi1000 14:2e17a27f56b2 201 {
hi1000 12:5cb359f981f3 202 printf("button1 released\r\n");
hi1000 14:2e17a27f56b2 203 button_event_flags.set(BUTTON1_PRESSED_EVENT);
hi1000 14:2e17a27f56b2 204 }
hi1000 0:765cf978c3e5 205 button1_enabled = true;
hi1000 0:765cf978c3e5 206 }
hi1000 0:765cf978c3e5 207
hi1000 0:765cf978c3e5 208 // ISR handling button pressed event
hi1000 0:765cf978c3e5 209 void button1_onpressed_cb(void)
hi1000 0:765cf978c3e5 210 {
hi1000 0:765cf978c3e5 211 if (button1_enabled) { // Disabled while the button is bouncing
hi1000 0:765cf978c3e5 212 button1_enabled = false;
hi1000 0:765cf978c3e5 213 button1_pressed = true; // To be read by the main loop
hi1000 0:765cf978c3e5 214 button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms
hi1000 0:765cf978c3e5 215 }
hi1000 0:765cf978c3e5 216 }
hi1000 12:5cb359f981f3 217 //button1--
hi1000 12:5cb359f981f3 218 //button2
hi1000 12:5cb359f981f3 219 void button2_enabled_cb(void)
hi1000 12:5cb359f981f3 220 {
hi1000 12:5cb359f981f3 221 int button_status;
hi1000 12:5cb359f981f3 222 button_status = button2.read();
hi1000 12:5cb359f981f3 223 if (button_status == 0)
hi1000 12:5cb359f981f3 224 {
hi1000 12:5cb359f981f3 225 printf("button2 down\r\n");
hi1000 14:2e17a27f56b2 226 button_event_flags.set(BUTTON2_HOLD_EVENT);
hi1000 12:5cb359f981f3 227 }
hi1000 12:5cb359f981f3 228 else
hi1000 14:2e17a27f56b2 229 {
hi1000 12:5cb359f981f3 230 printf("button2 released\r\n");
hi1000 14:2e17a27f56b2 231 button_event_flags.set(BUTTON2_PRESSED_EVENT);
hi1000 14:2e17a27f56b2 232 }
hi1000 12:5cb359f981f3 233 button2_enabled = true;
hi1000 12:5cb359f981f3 234 }
hi1000 0:765cf978c3e5 235
hi1000 12:5cb359f981f3 236 // ISR handling button pressed event
hi1000 12:5cb359f981f3 237 void button2_onpressed_cb(void)
hi1000 12:5cb359f981f3 238 {
hi1000 12:5cb359f981f3 239 if (button2_enabled) { // Disabled while the button is bouncing
hi1000 12:5cb359f981f3 240 button2_enabled = false;
hi1000 12:5cb359f981f3 241 button2_pressed = true; // To be read by the main loop
hi1000 12:5cb359f981f3 242 button2_timeout.attach(callback(button2_enabled_cb), 0.3); // Debounce time 300 ms
hi1000 12:5cb359f981f3 243 }
hi1000 12:5cb359f981f3 244 }
hi1000 12:5cb359f981f3 245 //button2--
hi1000 12:5cb359f981f3 246 //button3
hi1000 12:5cb359f981f3 247 void button3_enabled_cb(void)
hi1000 12:5cb359f981f3 248 {
hi1000 12:5cb359f981f3 249 int button_status;
hi1000 12:5cb359f981f3 250 button_status = button3.read();
hi1000 12:5cb359f981f3 251 if (button_status == 0)
hi1000 12:5cb359f981f3 252 {
hi1000 12:5cb359f981f3 253 printf("button3 down\r\n");
hi1000 14:2e17a27f56b2 254 button_event_flags.set(BUTTON3_HOLD_EVENT);
hi1000 12:5cb359f981f3 255 }
hi1000 12:5cb359f981f3 256 else
hi1000 14:2e17a27f56b2 257 {
hi1000 12:5cb359f981f3 258 printf("button3 released\r\n");
hi1000 14:2e17a27f56b2 259 button_event_flags.set(BUTTON3_PRESSED_EVENT);
hi1000 14:2e17a27f56b2 260 }
hi1000 12:5cb359f981f3 261 button3_enabled = true;
hi1000 12:5cb359f981f3 262 }
hi1000 12:5cb359f981f3 263
hi1000 12:5cb359f981f3 264 // ISR handling button pressed event
hi1000 12:5cb359f981f3 265 void button3_onpressed_cb(void)
hi1000 12:5cb359f981f3 266 {
hi1000 12:5cb359f981f3 267 if (button3_enabled) { // Disabled while the button is bouncing
hi1000 12:5cb359f981f3 268 button3_enabled = false;
hi1000 12:5cb359f981f3 269 button3_pressed = true; // To be read by the main loop
hi1000 12:5cb359f981f3 270 button3_timeout.attach(callback(button3_enabled_cb), 0.3); // Debounce time 300 ms
hi1000 12:5cb359f981f3 271 }
hi1000 12:5cb359f981f3 272 }
hi1000 12:5cb359f981f3 273 //button3--
hi1000 12:5cb359f981f3 274 //button4
hi1000 12:5cb359f981f3 275 void button4_enabled_cb(void)
hi1000 12:5cb359f981f3 276 {
hi1000 12:5cb359f981f3 277 int button_status;
hi1000 12:5cb359f981f3 278 button_status = button4.read();
hi1000 12:5cb359f981f3 279 if (button_status == 0)
hi1000 12:5cb359f981f3 280 {
hi1000 12:5cb359f981f3 281 printf("button4 down\r\n");
hi1000 14:2e17a27f56b2 282 button_event_flags.set(BUTTON4_HOLD_EVENT);
hi1000 12:5cb359f981f3 283 }
hi1000 12:5cb359f981f3 284 else
hi1000 14:2e17a27f56b2 285 {
hi1000 12:5cb359f981f3 286 printf("button4 released\r\n");
hi1000 14:2e17a27f56b2 287 button_event_flags.set(BUTTON4_PRESSED_EVENT);
hi1000 14:2e17a27f56b2 288 }
hi1000 12:5cb359f981f3 289 button4_enabled = true;
hi1000 12:5cb359f981f3 290 }
hi1000 12:5cb359f981f3 291
hi1000 12:5cb359f981f3 292 // ISR handling button pressed event
hi1000 12:5cb359f981f3 293 void button4_onpressed_cb(void)
hi1000 12:5cb359f981f3 294 {
hi1000 12:5cb359f981f3 295 if (button4_enabled) { // Disabled while the button is bouncing
hi1000 12:5cb359f981f3 296 button4_enabled = false;
hi1000 12:5cb359f981f3 297 button4_pressed = true; // To be read by the main loop
hi1000 12:5cb359f981f3 298 button4_timeout.attach(callback(button4_enabled_cb), 0.3); // Debounce time 300 ms
hi1000 12:5cb359f981f3 299 }
hi1000 12:5cb359f981f3 300 }
hi1000 12:5cb359f981f3 301 //button4--
hi1000 19:0356e54240cc 302 #if 1
hi1000 17:faa4d4976d22 303 void can_sendData(int can_id, uint8_t *tx_data, int length)
hi1000 17:faa4d4976d22 304 {
hi1000 17:faa4d4976d22 305 CANMessage txmsg;
hi1000 17:faa4d4976d22 306
hi1000 17:faa4d4976d22 307 txmsg.format = CANExtended;
hi1000 17:faa4d4976d22 308 txmsg.id = can_id;
hi1000 17:faa4d4976d22 309 txmsg.len = length;
hi1000 17:faa4d4976d22 310 txmsg.data[0] = tx_data[0];
hi1000 17:faa4d4976d22 311 txmsg.data[1] = tx_data[1];
hi1000 17:faa4d4976d22 312 txmsg.data[2] = tx_data[2];
hi1000 17:faa4d4976d22 313 txmsg.data[3] = tx_data[3];
hi1000 17:faa4d4976d22 314 txmsg.data[4] = tx_data[4];
hi1000 17:faa4d4976d22 315 txmsg.data[5] = tx_data[5];
hi1000 17:faa4d4976d22 316 txmsg.data[6] = tx_data[6];
hi1000 17:faa4d4976d22 317 txmsg.data[7] = tx_data[7];
hi1000 17:faa4d4976d22 318
hi1000 19:0356e54240cc 319 // printf("can_sendData can_id=0x%08x \r\n", can_id);
hi1000 17:faa4d4976d22 320 can1.write(txmsg);
hi1000 17:faa4d4976d22 321 }
hi1000 17:faa4d4976d22 322
hi1000 8:6105ffbaf237 323 void can_rxthread()
hi1000 0:765cf978c3e5 324 {
hi1000 9:486f65124378 325 int loop;
hi1000 0:765cf978c3e5 326 while (true) {
hi1000 9:486f65124378 327 #if 1
hi1000 0:765cf978c3e5 328 if(can1.read(msg)) {
hi1000 0:765cf978c3e5 329 print_char();
hi1000 0:765cf978c3e5 330 printf("got message id=%d 0x%08x\r\n", msg.id, msg.id);
hi1000 0:765cf978c3e5 331 // b = *reinterpret_cast<int*>(msg.data);
hi1000 9:486f65124378 332 for (loop = 0; loop < msg.len; loop++)
hi1000 8:6105ffbaf237 333 {
hi1000 9:486f65124378 334 can_rx_data[loop] = msg.data[loop];
hi1000 8:6105ffbaf237 335 }
hi1000 8:6105ffbaf237 336
hi1000 9:486f65124378 337 printf("got data: length:%d\r\n", msg.len);
hi1000 9:486f65124378 338 for (loop = 0; loop < msg.len; loop++)
hi1000 9:486f65124378 339 {
hi1000 9:486f65124378 340 printf("data[%d]=%d\r\n", loop, can_rx_data[loop]);
hi1000 9:486f65124378 341 }
hi1000 14:2e17a27f56b2 342 // if(msg.id == 1337)
hi1000 14:2e17a27f56b2 343 {
hi1000 9:486f65124378 344 //only queue the message belongs to you
hi1000 9:486f65124378 345 CANMessage *can_message = can_mpool.alloc();
hi1000 9:486f65124378 346 memcpy((void *)can_message, (void *)&msg, sizeof(msg));
hi1000 9:486f65124378 347 if (!can_queue.full())
hi1000 9:486f65124378 348 can_queue.put(can_message);
hi1000 9:486f65124378 349 else
hi1000 9:486f65124378 350 {
hi1000 9:486f65124378 351 printf("message queue is full. \r\n");
hi1000 9:486f65124378 352 }
hi1000 0:765cf978c3e5 353 led2 = !led2;
hi1000 0:765cf978c3e5 354 }
hi1000 0:765cf978c3e5 355 }
hi1000 9:486f65124378 356 #endif
hi1000 20:ec9d4f6a16ac 357 wait(0.2);
hi1000 0:765cf978c3e5 358 }
hi1000 0:765cf978c3e5 359 }
hi1000 19:0356e54240cc 360 #endif
hi1000 19:0356e54240cc 361 void i2c_scanner(PinName sda, PinName scl)
hi1000 19:0356e54240cc 362 {
hi1000 19:0356e54240cc 363 I2C i2c(sda, scl);
hi1000 19:0356e54240cc 364
hi1000 19:0356e54240cc 365 int error, address;
hi1000 19:0356e54240cc 366 int nDevices;
hi1000 19:0356e54240cc 367
hi1000 19:0356e54240cc 368 i2c.frequency(100000);
hi1000 19:0356e54240cc 369 printf("i2c_scanner sda=%d scl=%d \r\n", (int)sda, (int)scl);
hi1000 19:0356e54240cc 370 printf("Scanning...\r\n");
hi1000 19:0356e54240cc 371
hi1000 19:0356e54240cc 372 nDevices = 0;
hi1000 19:0356e54240cc 373
hi1000 19:0356e54240cc 374 for(address = 0; address < 127; address++ )
hi1000 19:0356e54240cc 375 {
hi1000 19:0356e54240cc 376 i2c.start();
hi1000 19:0356e54240cc 377 // error = i2c.write(address << 1); //We shift it left because mbed takes in 8 bit addreses
hi1000 19:0356e54240cc 378 error = i2c.write(address << 1, "1", 1, false);
hi1000 19:0356e54240cc 379 i2c.stop();
hi1000 19:0356e54240cc 380 if (error == 0)
hi1000 19:0356e54240cc 381 {
hi1000 19:0356e54240cc 382 printf("I2C device found at address 7bit:0x%X (8bit:0x%X)\r\n", address, (address<<1)); //Returns 8-bit addres
hi1000 19:0356e54240cc 383 nDevices++;
hi1000 19:0356e54240cc 384 }
hi1000 19:0356e54240cc 385 }
hi1000 19:0356e54240cc 386 if (nDevices == 0)
hi1000 19:0356e54240cc 387 printf("No I2C devices found\r\n");
hi1000 19:0356e54240cc 388 else
hi1000 19:0356e54240cc 389 printf("\r\ndone\r\n");
hi1000 19:0356e54240cc 390 }
hi1000 0:765cf978c3e5 391 int main()
hi1000 0:765cf978c3e5 392 {
hi1000 9:486f65124378 393 int loop = 0;
hi1000 9:486f65124378 394 int8_t ival;
hi1000 19:0356e54240cc 395 unsigned int can_id;
hi1000 20:ec9d4f6a16ac 396 int distance = 0;
hi1000 20:ec9d4f6a16ac 397 #if 0
hi1000 20:ec9d4f6a16ac 398 int hour;
hi1000 20:ec9d4f6a16ac 399 int minute;
hi1000 20:ec9d4f6a16ac 400 int second;
hi1000 20:ec9d4f6a16ac 401
hi1000 20:ec9d4f6a16ac 402 int dayOfWeek;
hi1000 20:ec9d4f6a16ac 403 int date;
hi1000 20:ec9d4f6a16ac 404 int month;
hi1000 20:ec9d4f6a16ac 405 int year;
hi1000 20:ec9d4f6a16ac 406 DS3231 rtc_test(SDA, SCL);
hi1000 20:ec9d4f6a16ac 407 // DS3231 rtc_test(PF_0, PF_1);
hi1000 20:ec9d4f6a16ac 408 printf("\r\n\nDS3231 Library test program\r\nremi cormier 2012\r\n\n");
hi1000 20:ec9d4f6a16ac 409
hi1000 20:ec9d4f6a16ac 410 rtc_test.setI2Cfrequency(400000);
hi1000 20:ec9d4f6a16ac 411
hi1000 20:ec9d4f6a16ac 412 //rtc_test.writeRegister(DS3231_Aging_Offset,0); // uncomment to set Aging Offset 1LSB = approx. 0.1 ppm according from datasheet = 0.05 ppm @ 21 °C from my measurments
hi1000 20:ec9d4f6a16ac 413
hi1000 20:ec9d4f6a16ac 414 rtc_test.convertTemperature();
hi1000 20:ec9d4f6a16ac 415
hi1000 20:ec9d4f6a16ac 416 int reg=rtc_test.readRegister(DS3231_Aging_Offset);
hi1000 20:ec9d4f6a16ac 417 if (reg>127)
hi1000 20:ec9d4f6a16ac 418 {reg=reg-256;}
hi1000 20:ec9d4f6a16ac 419 printf("Aging offset : %i\r\n",reg);
hi1000 20:ec9d4f6a16ac 420
hi1000 20:ec9d4f6a16ac 421 printf("OSF flag : %i",rtc_test.OSF());
hi1000 20:ec9d4f6a16ac 422 printf("\r\n");
hi1000 20:ec9d4f6a16ac 423
hi1000 20:ec9d4f6a16ac 424 rtc_test.readDate(&date,&month,&year);
hi1000 20:ec9d4f6a16ac 425 printf("date : %02i-%02i-%02i",date,month,year);
hi1000 20:ec9d4f6a16ac 426 printf("\r\n");
hi1000 20:ec9d4f6a16ac 427
hi1000 20:ec9d4f6a16ac 428 rtc_test.setTime(19,48,45); // uncomment to set time
hi1000 20:ec9d4f6a16ac 429
hi1000 20:ec9d4f6a16ac 430 rtc_test.readTime(&hour,&minute,&second);
hi1000 20:ec9d4f6a16ac 431 printf("time : %02i:%02i:%02i",hour,minute,second);
hi1000 20:ec9d4f6a16ac 432 printf("\r\n");
hi1000 20:ec9d4f6a16ac 433
hi1000 20:ec9d4f6a16ac 434 rtc_test.setDate(6,22,12,2012); // uncomment to set date
hi1000 20:ec9d4f6a16ac 435 #endif
hi1000 20:ec9d4f6a16ac 436
hi1000 14:2e17a27f56b2 437 printf("\n\n*** RTOS starts ***\r\n");
hi1000 20:ec9d4f6a16ac 438
hi1000 9:486f65124378 439 // wait(1);
hi1000 19:0356e54240cc 440 #if 1
hi1000 14:2e17a27f56b2 441 ep.read((uint32_t)EEPROM_DEVICE_ADDRESS_ADDRESS,device_address);
hi1000 14:2e17a27f56b2 442 printf("EEPROM: read device address:%d 0x%08x\r\n", device_address, device_address);
hi1000 14:2e17a27f56b2 443 if ((device_address == 0) || (device_address == 0xFFFFFFFF))
hi1000 14:2e17a27f56b2 444 device_address = DEVICE_DEFAULT_ADDRESS;
hi1000 16:f4277e9b8612 445 device_type = (device_address & 0x00000700) >> 8;
hi1000 19:0356e54240cc 446 device_type_v = (Device_Type_d)device_type;
hi1000 8:6105ffbaf237 447 #ifdef LCD_1621
hi1000 4:40bb33497de4 448 lcd.clear(); // clears display
hi1000 4:40bb33497de4 449 lcd.allsegson();
hi1000 5:4585215afd11 450
hi1000 4:40bb33497de4 451 // lcd.printf("ABCDEFGHI"); // Standard printf function, All ASCII characters will display
hi1000 5:4585215afd11 452 #endif
hi1000 19:0356e54240cc 453 #ifdef YODA2
hi1000 20:ec9d4f6a16ac 454 #if 1
hi1000 19:0356e54240cc 455 lcd.cls();
hi1000 19:0356e54240cc 456 lcd.setContrast(31);
hi1000 19:0356e54240cc 457 lcd.setCursor(TextLCD::CurOff_BlkOff);
hi1000 19:0356e54240cc 458 lcd.setAddress(0,0);
hi1000 19:0356e54240cc 459 lcd.printf("Hello Yoda2!");
hi1000 20:ec9d4f6a16ac 460 #endif
hi1000 19:0356e54240cc 461 #endif
hi1000 5:4585215afd11 462 #ifdef LCD_1602
hi1000 16:f4277e9b8612 463 // lcd.printf( 0, "Hello world!" ); // line# (0 or 1), string
hi1000 16:f4277e9b8612 464 // lcd.printf( 1, "pi = %.6f", 3.14159265 );
hi1000 16:f4277e9b8612 465 // lcd.putcxy(0x55, 5, 1);
hi1000 16:f4277e9b8612 466 // lcd.printf(5, 0, "UUU");
hi1000 16:f4277e9b8612 467 // lcd.printf(0, 0, "pressed!" );
hi1000 9:486f65124378 468 #endif
hi1000 11:8702316d7fc8 469
hi1000 19:0356e54240cc 470 // input1.mode(PullUp);
hi1000 19:0356e54240cc 471 // input2.mode(PullUp);
hi1000 13:9f581a090e53 472
hi1000 0:765cf978c3e5 473 // can1.reset();
hi1000 0:765cf978c3e5 474 // can2.reset();
hi1000 20:ec9d4f6a16ac 475
hi1000 20:ec9d4f6a16ac 476 #if 1
hi1000 16:f4277e9b8612 477 printf("device_address =0x%08x \r\n", (device_address<<18));
hi1000 2:61a0169765bf 478 can1.frequency(100000);
hi1000 14:2e17a27f56b2 479 can1.filter((device_address<<18), 0x1FFC0000, CANExtended, init_filter_handle); // 0x1FFC0000 to filter the last 18bits 0-17
hi1000 16:f4277e9b8612 480 device_address = (device_address & 0x000000FF);
hi1000 14:2e17a27f56b2 481 //only support one filter
hi1000 14:2e17a27f56b2 482 // can1.filter(broadcast_id, 0x1FFC0000, CANExtended, broadcast_filter_handle); // the broadcast id
hi1000 0:765cf978c3e5 483 // can2.frequency(100000);
hi1000 12:5cb359f981f3 484 //button0.mode(PullUp); // Activate pull-up
hi1000 13:9f581a090e53 485 can_receivethread.start(can_rxthread);
hi1000 13:9f581a090e53 486 can_handlethread.start(analyzePayload);
hi1000 20:ec9d4f6a16ac 487 #endif
hi1000 20:ec9d4f6a16ac 488
hi1000 19:0356e54240cc 489 #endif
hi1000 19:0356e54240cc 490 button1.mode(PullUp);
hi1000 19:0356e54240cc 491 button2.mode(PullUp);
hi1000 19:0356e54240cc 492 button3.mode(PullUp);
hi1000 19:0356e54240cc 493 button4.mode(PullUp);
hi1000 12:5cb359f981f3 494 button0.fall(callback(button0_onpressed_cb)); // Attach ISR to handle button press event
hi1000 12:5cb359f981f3 495 button0.rise(callback(button0_onpressed_cb)); // Attach ISR to handle button press event
hi1000 2:61a0169765bf 496 button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
hi1000 12:5cb359f981f3 497 button1.rise(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
hi1000 12:5cb359f981f3 498 button2.fall(callback(button2_onpressed_cb)); // Attach ISR to handle button press event
hi1000 12:5cb359f981f3 499 button2.rise(callback(button2_onpressed_cb)); // Attach ISR to handle button press event
hi1000 12:5cb359f981f3 500 button3.fall(callback(button3_onpressed_cb)); // Attach ISR to handle button press event
hi1000 12:5cb359f981f3 501 button3.rise(callback(button3_onpressed_cb)); // Attach ISR to handle button press event
hi1000 12:5cb359f981f3 502 button4.fall(callback(button4_onpressed_cb)); // Attach ISR to handle button press event
hi1000 12:5cb359f981f3 503 button4.rise(callback(button4_onpressed_cb)); // Attach ISR to handle button press event
hi1000 19:0356e54240cc 504 // i2c_scanner(PB_9, PB_8);
hi1000 11:8702316d7fc8 505 // eeprom_test();
hi1000 0:765cf978c3e5 506
hi1000 11:8702316d7fc8 507 #if 1
hi1000 18:e6ed582f7022 508 // scaleCalibration(true);
hi1000 19:0356e54240cc 509 switch (device_type_v)
hi1000 19:0356e54240cc 510 {
hi1000 19:0356e54240cc 511 case CupTrack:
hi1000 19:0356e54240cc 512 case JamTrack:
hi1000 19:0356e54240cc 513 case TeaTrack:
hi1000 19:0356e54240cc 514 break;
hi1000 19:0356e54240cc 515 case Tea:
hi1000 19:0356e54240cc 516 case Jam:
hi1000 19:0356e54240cc 517 init_scale();
hi1000 19:0356e54240cc 518 scale_thread.start(scale_reading);
hi1000 19:0356e54240cc 519 break;
hi1000 19:0356e54240cc 520 case Shaker:
hi1000 19:0356e54240cc 521 break;
hi1000 19:0356e54240cc 522 default:
hi1000 19:0356e54240cc 523 break;
hi1000 19:0356e54240cc 524 }
hi1000 19:0356e54240cc 525
hi1000 14:2e17a27f56b2 526 mainmenu_thread.start(main_menu);
hi1000 14:2e17a27f56b2 527
hi1000 11:8702316d7fc8 528 #endif
hi1000 20:ec9d4f6a16ac 529 moveMotor3(0, true, false, false);
hi1000 20:ec9d4f6a16ac 530 wait(2);
hi1000 14:2e17a27f56b2 531 while(1) {
hi1000 20:ec9d4f6a16ac 532 wait(2);
hi1000 20:ec9d4f6a16ac 533 #if 0
hi1000 20:ec9d4f6a16ac 534 distance = 0;
hi1000 20:ec9d4f6a16ac 535 distance = moveMotor3Until(true, din5);
hi1000 20:ec9d4f6a16ac 536 printf("F distance=%d \r\n", distance);
hi1000 20:ec9d4f6a16ac 537 printf("Sensors:\r\n");
hi1000 20:ec9d4f6a16ac 538 printf("%d %d %d %d \r\n", din1.read(), din2.read(), din3.read(), din4.read());
hi1000 20:ec9d4f6a16ac 539 printf("%d %d %d %d \r\n", din5.read(), din6.read(), din7.read(), din8.read());
hi1000 20:ec9d4f6a16ac 540 wait(1);
hi1000 20:ec9d4f6a16ac 541 distance = 0;
hi1000 20:ec9d4f6a16ac 542 distance = moveMotor3Until(false, din5);
hi1000 20:ec9d4f6a16ac 543 printf("Sensors:\r\n");
hi1000 20:ec9d4f6a16ac 544 printf("%d %d %d %d \r\n", din1.read(), din2.read(), din3.read(), din4.read());
hi1000 20:ec9d4f6a16ac 545 printf("%d %d %d %d \r\n", din5.read(), din6.read(), din7.read(), din8.read());
hi1000 20:ec9d4f6a16ac 546 printf("B distance=%d \r\n", distance);
hi1000 20:ec9d4f6a16ac 547 #endif
hi1000 20:ec9d4f6a16ac 548 #if 0
hi1000 20:ec9d4f6a16ac 549 relay4 = 1;
hi1000 20:ec9d4f6a16ac 550 printf("relay4 turn on\r\n");
hi1000 20:ec9d4f6a16ac 551 wait(2);
hi1000 20:ec9d4f6a16ac 552 relay3 = 1;
hi1000 20:ec9d4f6a16ac 553 printf("relay3 turn on\r\n");
hi1000 20:ec9d4f6a16ac 554 wait(2);
hi1000 20:ec9d4f6a16ac 555 relay2 = 1;
hi1000 20:ec9d4f6a16ac 556 printf("relay2 turn on\r\n");
hi1000 20:ec9d4f6a16ac 557 wait(2);
hi1000 20:ec9d4f6a16ac 558 relay1 = 1;
hi1000 20:ec9d4f6a16ac 559 printf("relay1 turn on\r\n");
hi1000 20:ec9d4f6a16ac 560 wait(2);
hi1000 20:ec9d4f6a16ac 561 relay4 = 0;
hi1000 20:ec9d4f6a16ac 562 relay3 = 0;
hi1000 20:ec9d4f6a16ac 563 relay2 = 0;
hi1000 20:ec9d4f6a16ac 564 relay1 = 0;
hi1000 20:ec9d4f6a16ac 565 #endif
hi1000 20:ec9d4f6a16ac 566 #if 0
hi1000 20:ec9d4f6a16ac 567 rtc_test.readDateTime(&dayOfWeek,&date,&month,&year,&hour,&minute,&second);
hi1000 20:ec9d4f6a16ac 568 printf("date time : %i / %02i-%02i-%02i %02i:%02i:%02i",dayOfWeek,date,month,year,hour,minute,second);
hi1000 20:ec9d4f6a16ac 569 printf("\r\n");
hi1000 20:ec9d4f6a16ac 570
hi1000 20:ec9d4f6a16ac 571 printf("temperature :%6.2f", rtc_test.readTemp());
hi1000 20:ec9d4f6a16ac 572 printf("\r\n");
hi1000 20:ec9d4f6a16ac 573 #endif
hi1000 20:ec9d4f6a16ac 574 #if 0
hi1000 20:ec9d4f6a16ac 575 moveMotor3(850, true, false, false);
hi1000 20:ec9d4f6a16ac 576 wait(5);
hi1000 20:ec9d4f6a16ac 577 moveMotor3(850, false, false, false);
hi1000 20:ec9d4f6a16ac 578 #endif
hi1000 20:ec9d4f6a16ac 579 #if 0
hi1000 20:ec9d4f6a16ac 580 moveMotor1(100, true, false, false);
hi1000 20:ec9d4f6a16ac 581 moveMotor2(100, true, false, false);
hi1000 20:ec9d4f6a16ac 582 moveMotor3(100, true, false, false);
hi1000 20:ec9d4f6a16ac 583 wait(1);
hi1000 20:ec9d4f6a16ac 584 moveMotor1(150, true, false, false);
hi1000 20:ec9d4f6a16ac 585 moveMotor2(150, true, false, false);
hi1000 20:ec9d4f6a16ac 586 moveMotor3(150, true, false, false);
hi1000 20:ec9d4f6a16ac 587 wait(1);
hi1000 20:ec9d4f6a16ac 588 moveMotor1(250, false, false, false);
hi1000 20:ec9d4f6a16ac 589 moveMotor2(250, false, false, false);
hi1000 20:ec9d4f6a16ac 590 moveMotor3(250, false, false, false);
hi1000 20:ec9d4f6a16ac 591 #endif
hi1000 20:ec9d4f6a16ac 592 #if 1
hi1000 19:0356e54240cc 593 if (!can_register_success)
hi1000 19:0356e54240cc 594 {
hi1000 19:0356e54240cc 595 can_txdata_frame.cmd = COMMAND_REGISTER;
hi1000 19:0356e54240cc 596 can_txdata_frame.value1 = 0;
hi1000 19:0356e54240cc 597 can_txdata_frame.value2 = 0;
hi1000 19:0356e54240cc 598 can_txdata_frame.value3 = 0;
hi1000 19:0356e54240cc 599 memcpy(can_tx_data, (unsigned char *)&can_txdata_frame, sizeof(can_tx_data));
hi1000 19:0356e54240cc 600 // 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));
hi1000 19:0356e54240cc 601 // 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]);
hi1000 19:0356e54240cc 602 can_id = (RASPBERRY_PI_CAN_ADDRESS << 18) | (((device_type << 8) |device_address) << 7) | 0x80000000;
hi1000 19:0356e54240cc 603 // printf("device_address = 0x%08x can_id=0x%08x \r\n", device_address, can_id);
hi1000 19:0356e54240cc 604 can_sendData(can_id, can_tx_data, 8);
hi1000 19:0356e54240cc 605 }
hi1000 19:0356e54240cc 606 #endif
hi1000 14:2e17a27f56b2 607 }
hi1000 14:2e17a27f56b2 608
hi1000 14:2e17a27f56b2 609 #if 0
hi1000 0:765cf978c3e5 610 int idx = 0; // Just for printf below
hi1000 9:486f65124378 611 can_tx_data[0] = 0;
hi1000 0:765cf978c3e5 612 while(1) {
hi1000 12:5cb359f981f3 613 if (button0_pressed) { // Set when button is pressed
hi1000 9:486f65124378 614 #if 0
hi1000 1:eb499e2a1b9b 615 printf("scale value %f. \r\n", hx711.getGram());
hi1000 9:486f65124378 616 #endif
hi1000 9:486f65124378 617 can_tx_data[1] = can_tx_data[0]+1;
hi1000 9:486f65124378 618 can_tx_data[2] = can_tx_data[1]+1;
hi1000 9:486f65124378 619 can_tx_data[3] = can_tx_data[2]+1;
hi1000 9:486f65124378 620 can_tx_data[4] = can_tx_data[3]+1;
hi1000 9:486f65124378 621 can_tx_data[5] = can_tx_data[4]+1;
hi1000 9:486f65124378 622 can_tx_data[6] = can_tx_data[5]+1;
hi1000 9:486f65124378 623 can_tx_data[7] = can_tx_data[6]+1;
hi1000 12:5cb359f981f3 624 button0_pressed = false;
hi1000 2:61a0169765bf 625 printf("Button pressed %d\r\n", idx++);
hi1000 9:486f65124378 626 printf("ID=%d data[0]=%d. \r\n", init_id + idx%10, can_tx_data[0]);
hi1000 9:486f65124378 627 #ifdef LCD_1602
hi1000 9:486f65124378 628 lcd.printf(0, 0, "%d ", idx ); // line# (0 or 1), string
hi1000 9:486f65124378 629 #endif
hi1000 17:faa4d4976d22 630 can1.write(CANMessage((init_id + idx%10), reinterpret_cast<char*>(can_tx_data), 8, CANData,CANExtended));
hi1000 0:765cf978c3e5 631 led1 = !led1;
hi1000 9:486f65124378 632 can_tx_data[0]++;
hi1000 0:765cf978c3e5 633 }
hi1000 0:765cf978c3e5 634 }
hi1000 14:2e17a27f56b2 635 #endif
hi1000 9:486f65124378 636
hi1000 0:765cf978c3e5 637 }