Feng Hong / Mbed OS Nucleo_rtos_basic
Committer:
hi1000
Date:
Sat Mar 23 03:31:56 2019 +0000
Revision:
5:4585215afd11
Parent:
4:40bb33497de4
Child:
6:a9a03663fa23
LCD1602 works

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 5:4585215afd11 4 //#include "digitLCD.h"
hi1000 5:4585215afd11 5 #include "SB1602E.h"
hi1000 0:765cf978c3e5 6
hi1000 5:4585215afd11 7 #define LCD_1602
hi1000 5:4585215afd11 8 SB1602E lcd( PB_9, PB_8 ); // SDA, SCL
hi1000 0:765cf978c3e5 9 CAN can1(PD_0, PD_1);
hi1000 0:765cf978c3e5 10 CAN can2(PB_5, PB_6);
hi1000 1:eb499e2a1b9b 11 DigitalOut led1(LED1);
hi1000 1:eb499e2a1b9b 12 DigitalOut led2(LED2);
hi1000 1:eb499e2a1b9b 13 //FlashIAP flashIAP;
hi1000 1:eb499e2a1b9b 14
hi1000 5:4585215afd11 15 //#define LCD_1621
hi1000 5:4585215afd11 16 //digitLCD lcd(PA_5,PA_4,PB_5); // WO, CS, DATA
hi1000 1:eb499e2a1b9b 17
hi1000 2:61a0169765bf 18 #define EEPROM_ADDR 0x0 // I2c EEPROM address is 0x00
hi1000 2:61a0169765bf 19
hi1000 2:61a0169765bf 20 #define SDA PB_9 // I2C SDA pin
hi1000 2:61a0169765bf 21 #define SCL PB_8 // I2C SCL pin
hi1000 2:61a0169765bf 22
hi1000 2:61a0169765bf 23 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
hi1000 2:61a0169765bf 24 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
hi1000 2:61a0169765bf 25
hi1000 2:61a0169765bf 26 EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C256);
hi1000 1:eb499e2a1b9b 27 struct ScaleCalibrationData {
hi1000 1:eb499e2a1b9b 28 unsigned int calibrationWeight; // the weight (g) used for calibration for example 1000g or 10g. The maximum value is 3000.
hi1000 1:eb499e2a1b9b 29 long offsetValue; // the value for scale offset
hi1000 1:eb499e2a1b9b 30 float scaleValue; // the ADC increment for 1g
hi1000 2:61a0169765bf 31 uint8_t checksum;
hi1000 1:eb499e2a1b9b 32 };
hi1000 2:61a0169765bf 33 ScaleCalibrationData customVar;
hi1000 2:61a0169765bf 34
hi1000 1:eb499e2a1b9b 35 unsigned int calibration_ADC_value;
hi1000 1:eb499e2a1b9b 36 //#define CALIBRATION_VALUE 10 // 10g as the calibration weight
hi1000 1:eb499e2a1b9b 37 #define WEIGHT_DIFFERENCE 200 // 10g ADC value minimum difference
hi1000 1:eb499e2a1b9b 38 #define CALIBRATION_WEIGHT 2000 // calibration weight
hi1000 2:61a0169765bf 39 #define MAXIMUM_CALIBRATION_WEIGHT 5000
hi1000 2:61a0169765bf 40 #define MINIMUM_CALIBRATION_WEIGHT 100
hi1000 1:eb499e2a1b9b 41
hi1000 2:61a0169765bf 42 /* scale */
hi1000 2:61a0169765bf 43 HX711 hx711(PB_11, PB_10);
hi1000 1:eb499e2a1b9b 44
hi1000 1:eb499e2a1b9b 45 long zero_value;
hi1000 1:eb499e2a1b9b 46 long calibration_value;
hi1000 1:eb499e2a1b9b 47 unsigned int calibration_times; // must calibration 3 times
hi1000 1:eb499e2a1b9b 48 unsigned int calibration_done = 0;
hi1000 1:eb499e2a1b9b 49 float scale_value;
hi1000 2:61a0169765bf 50 int init_id = 0x537; // first 8 bit is the address
hi1000 1:eb499e2a1b9b 51
hi1000 1:eb499e2a1b9b 52 /* scale */
hi1000 2:61a0169765bf 53
hi1000 1:eb499e2a1b9b 54 void scaleCalibration()
hi1000 1:eb499e2a1b9b 55 {
hi1000 2:61a0169765bf 56 unsigned char eeprom_data[sizeof(customVar)];
hi1000 2:61a0169765bf 57 unsigned char checksum = 0;
hi1000 1:eb499e2a1b9b 58 printf("Start Calibration.\r\n");
hi1000 1:eb499e2a1b9b 59 calibration_done = 0;
hi1000 1:eb499e2a1b9b 60 if (!calibration_done)
hi1000 1:eb499e2a1b9b 61 {
hi1000 1:eb499e2a1b9b 62 led1 = 1;
hi1000 1:eb499e2a1b9b 63 led2 = 0;
hi1000 2:61a0169765bf 64 if ((customVar.calibrationWeight > MAXIMUM_CALIBRATION_WEIGHT)||(customVar.calibrationWeight < MINIMUM_CALIBRATION_WEIGHT))
hi1000 2:61a0169765bf 65 customVar.calibrationWeight = CALIBRATION_WEIGHT;
hi1000 1:eb499e2a1b9b 66 zero_value = hx711.averageValue(10); // skip first 10 readings
hi1000 1:eb499e2a1b9b 67 zero_value = hx711.averageValue(20);
hi1000 1:eb499e2a1b9b 68 printf("zero_value=%d \r\n", zero_value);
hi1000 1:eb499e2a1b9b 69 calibration_value = 0;
hi1000 1:eb499e2a1b9b 70 scale_value = 0;
hi1000 1:eb499e2a1b9b 71 calibration_times = 0;
hi1000 2:61a0169765bf 72 led2 = 1;
hi1000 1:eb499e2a1b9b 73 while (( calibration_times < 5))
hi1000 1:eb499e2a1b9b 74 {
hi1000 1:eb499e2a1b9b 75
hi1000 1:eb499e2a1b9b 76 calibration_value = hx711.averageValue(20);
hi1000 1:eb499e2a1b9b 77 if (calibration_value > (zero_value + WEIGHT_DIFFERENCE))
hi1000 1:eb499e2a1b9b 78 {
hi1000 1:eb499e2a1b9b 79 calibration_times++;
hi1000 1:eb499e2a1b9b 80 }
hi1000 1:eb499e2a1b9b 81 else
hi1000 1:eb499e2a1b9b 82 calibration_times = 0;
hi1000 1:eb499e2a1b9b 83 }
hi1000 1:eb499e2a1b9b 84 printf("calibration_value=%d calibration_times=%d\r\n", calibration_value, calibration_times);
hi1000 1:eb499e2a1b9b 85 if (calibration_times >=5)
hi1000 1:eb499e2a1b9b 86 {
hi1000 1:eb499e2a1b9b 87 // calibration is OK
hi1000 1:eb499e2a1b9b 88 calibration_times = 0;
hi1000 1:eb499e2a1b9b 89 scale_value = (calibration_value - zero_value) / customVar.calibrationWeight;
hi1000 1:eb499e2a1b9b 90 customVar.offsetValue = zero_value;
hi1000 1:eb499e2a1b9b 91 customVar.scaleValue = scale_value;
hi1000 1:eb499e2a1b9b 92 // EEPROM.put(0x00, customVar);
hi1000 1:eb499e2a1b9b 93 hx711.setOffset(zero_value);
hi1000 1:eb499e2a1b9b 94 hx711.setScale(scale_value); // this value is obtained by calibrating the scale with known weights; see the README for details
hi1000 2:61a0169765bf 95 memcpy(eeprom_data, &customVar, sizeof(customVar));
hi1000 2:61a0169765bf 96 for (int cnt = 0; cnt < (sizeof(customVar)-4); cnt++) // compiler bug need to -4 here
hi1000 2:61a0169765bf 97 {
hi1000 2:61a0169765bf 98 checksum += eeprom_data[cnt];
hi1000 2:61a0169765bf 99 }
hi1000 2:61a0169765bf 100 customVar.checksum = checksum;
hi1000 2:61a0169765bf 101 printf("EEPROM write calibration data: \r\n");
hi1000 2:61a0169765bf 102 printf("calibrationWeight=%d \r\n", customVar.calibrationWeight);
hi1000 2:61a0169765bf 103 printf("offsetValue=%d \r\n", customVar.offsetValue);
hi1000 2:61a0169765bf 104 printf("scaleValue=%f \r\n", customVar.scaleValue);
hi1000 2:61a0169765bf 105 printf("checksum=0x%02x \r\n", customVar.checksum);
hi1000 2:61a0169765bf 106 ep.write((uint32_t)0x00,(void *)&customVar,sizeof(customVar)); // write a structure eeprom_size - 32
hi1000 1:eb499e2a1b9b 107 calibration_done = 1;
hi1000 2:61a0169765bf 108 led1 = 1;
hi1000 2:61a0169765bf 109 printf("Calibration Done\r\n");
hi1000 1:eb499e2a1b9b 110 }
hi1000 1:eb499e2a1b9b 111 }
hi1000 1:eb499e2a1b9b 112 }
hi1000 0:765cf978c3e5 113
hi1000 2:61a0169765bf 114 void init_scale()
hi1000 2:61a0169765bf 115 {
hi1000 2:61a0169765bf 116 unsigned char eeprom_data;
hi1000 2:61a0169765bf 117 unsigned char checksum = 0;
hi1000 2:61a0169765bf 118 customVar.calibrationWeight = CALIBRATION_WEIGHT;
hi1000 2:61a0169765bf 119
hi1000 2:61a0169765bf 120 #if 1
hi1000 2:61a0169765bf 121 printf("sizeof(customVar)=%d \r\n", sizeof(customVar));
hi1000 2:61a0169765bf 122 ep.read((uint32_t)0,(void *)&customVar, sizeof(customVar));
hi1000 2:61a0169765bf 123 printf("EEPROM read calibration data: \r\n");
hi1000 2:61a0169765bf 124 printf("calibrationWeight=%d \r\n", customVar.calibrationWeight);
hi1000 2:61a0169765bf 125 printf("offsetValue=%d \r\n", customVar.offsetValue);
hi1000 2:61a0169765bf 126 printf("scaleValue=%f \r\n", customVar.scaleValue);
hi1000 2:61a0169765bf 127 printf("checksum=0x%02x \r\n", customVar.checksum);
hi1000 2:61a0169765bf 128 printf("\r\n calculate checksum: \r\n");
hi1000 2:61a0169765bf 129 for (int cnt = 0; cnt < (sizeof(customVar)-4); cnt++) // compiler bug need to -4 here
hi1000 2:61a0169765bf 130 {
hi1000 2:61a0169765bf 131 ep.read(cnt, (int8_t&)eeprom_data);
hi1000 2:61a0169765bf 132 printf("0x%02x ", eeprom_data);
hi1000 2:61a0169765bf 133 checksum += eeprom_data;
hi1000 2:61a0169765bf 134 printf("checksum=0x%02x\r\n", checksum);
hi1000 2:61a0169765bf 135 }
hi1000 2:61a0169765bf 136 printf("\r\ncalculated checksum=0x%02x \r\n", checksum);
hi1000 2:61a0169765bf 137
hi1000 2:61a0169765bf 138 if (checksum == customVar.checksum)
hi1000 2:61a0169765bf 139 {
hi1000 2:61a0169765bf 140 if ((customVar.calibrationWeight > MAXIMUM_CALIBRATION_WEIGHT) || (customVar.calibrationWeight < MINIMUM_CALIBRATION_WEIGHT))
hi1000 2:61a0169765bf 141 {
hi1000 2:61a0169765bf 142 customVar.calibrationWeight = CALIBRATION_WEIGHT;
hi1000 2:61a0169765bf 143 scaleCalibration();
hi1000 2:61a0169765bf 144 }
hi1000 2:61a0169765bf 145 if ((customVar.offsetValue < 10000))
hi1000 2:61a0169765bf 146 {
hi1000 2:61a0169765bf 147 customVar.offsetValue = 10000;
hi1000 2:61a0169765bf 148 scaleCalibration();
hi1000 2:61a0169765bf 149 }
hi1000 2:61a0169765bf 150 if ((customVar.scaleValue < 100))
hi1000 2:61a0169765bf 151 {
hi1000 2:61a0169765bf 152 customVar.scaleValue = 100;
hi1000 2:61a0169765bf 153 scaleCalibration();
hi1000 2:61a0169765bf 154 }
hi1000 2:61a0169765bf 155 // delay(200);
hi1000 2:61a0169765bf 156 hx711.setOffset(customVar.offsetValue);
hi1000 2:61a0169765bf 157 hx711.setScale(customVar.scaleValue);
hi1000 2:61a0169765bf 158 }
hi1000 2:61a0169765bf 159 else
hi1000 2:61a0169765bf 160 {
hi1000 2:61a0169765bf 161 scaleCalibration();
hi1000 2:61a0169765bf 162 }
hi1000 2:61a0169765bf 163 #endif
hi1000 2:61a0169765bf 164 }
hi1000 2:61a0169765bf 165
hi1000 2:61a0169765bf 166 /*scale end*/
hi1000 0:765cf978c3e5 167 int a = 0;
hi1000 0:765cf978c3e5 168 int b = 0;
hi1000 0:765cf978c3e5 169
hi1000 0:765cf978c3e5 170 void print_char(char c = '*')
hi1000 0:765cf978c3e5 171 {
hi1000 0:765cf978c3e5 172 printf("%c\r\n", c);
hi1000 0:765cf978c3e5 173 fflush(stdout);
hi1000 0:765cf978c3e5 174 }
hi1000 0:765cf978c3e5 175
hi1000 0:765cf978c3e5 176 Thread thread;
hi1000 0:765cf978c3e5 177
hi1000 1:eb499e2a1b9b 178
hi1000 0:765cf978c3e5 179 CANMessage msg;
hi1000 0:765cf978c3e5 180
hi1000 0:765cf978c3e5 181
hi1000 0:765cf978c3e5 182 InterruptIn button1(USER_BUTTON);
hi1000 0:765cf978c3e5 183 volatile bool button1_pressed = false; // Used in the main loop
hi1000 0:765cf978c3e5 184 volatile bool button1_enabled = true; // Used for debouncing
hi1000 0:765cf978c3e5 185 Timeout button1_timeout; // Used for debouncing
hi1000 0:765cf978c3e5 186
hi1000 0:765cf978c3e5 187 // Enables button when bouncing is over
hi1000 0:765cf978c3e5 188 void button1_enabled_cb(void)
hi1000 0:765cf978c3e5 189 {
hi1000 0:765cf978c3e5 190 button1_enabled = true;
hi1000 0:765cf978c3e5 191 }
hi1000 0:765cf978c3e5 192
hi1000 0:765cf978c3e5 193 // ISR handling button pressed event
hi1000 0:765cf978c3e5 194 void button1_onpressed_cb(void)
hi1000 0:765cf978c3e5 195 {
hi1000 0:765cf978c3e5 196 if (button1_enabled) { // Disabled while the button is bouncing
hi1000 0:765cf978c3e5 197 button1_enabled = false;
hi1000 0:765cf978c3e5 198 button1_pressed = true; // To be read by the main loop
hi1000 0:765cf978c3e5 199 button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms
hi1000 0:765cf978c3e5 200 }
hi1000 0:765cf978c3e5 201 }
hi1000 0:765cf978c3e5 202
hi1000 0:765cf978c3e5 203 void print_thread()
hi1000 0:765cf978c3e5 204 {
hi1000 0:765cf978c3e5 205 while (true) {
hi1000 0:765cf978c3e5 206 #if 1
hi1000 0:765cf978c3e5 207 if(can1.read(msg)) {
hi1000 0:765cf978c3e5 208 print_char();
hi1000 0:765cf978c3e5 209 printf("got message id=%d 0x%08x\r\n", msg.id, msg.id);
hi1000 0:765cf978c3e5 210 // b = *reinterpret_cast<int*>(msg.data);
hi1000 0:765cf978c3e5 211 b = msg.data[0];
hi1000 0:765cf978c3e5 212 printf("got data %d 0x%08x \r\n", b, b);
hi1000 0:765cf978c3e5 213 if(msg.id == 1337) {
hi1000 0:765cf978c3e5 214 led2 = !led2;
hi1000 0:765cf978c3e5 215
hi1000 0:765cf978c3e5 216 b = *reinterpret_cast<int*>(msg.data);
hi1000 0:765cf978c3e5 217 printf("got message %d\r\n", b);
hi1000 0:765cf978c3e5 218 if(b % 5 == 0)
hi1000 0:765cf978c3e5 219 led2 = !led2;
hi1000 0:765cf978c3e5 220 }
hi1000 0:765cf978c3e5 221 }
hi1000 0:765cf978c3e5 222 // wait(0.2);
hi1000 0:765cf978c3e5 223 #endif
hi1000 0:765cf978c3e5 224 }
hi1000 0:765cf978c3e5 225 }
hi1000 0:765cf978c3e5 226
hi1000 2:61a0169765bf 227 typedef struct _MyData {
hi1000 2:61a0169765bf 228 int16_t sdata;
hi1000 2:61a0169765bf 229 int32_t idata;
hi1000 2:61a0169765bf 230 float fdata;
hi1000 2:61a0169765bf 231 } MyData;
hi1000 2:61a0169765bf 232
hi1000 2:61a0169765bf 233 static void myerror(std::string msg)
hi1000 2:61a0169765bf 234 {
hi1000 2:61a0169765bf 235 printf("Error %s\n",msg.c_str());
hi1000 2:61a0169765bf 236 exit(1);
hi1000 2:61a0169765bf 237 }
hi1000 2:61a0169765bf 238
hi1000 2:61a0169765bf 239 void eeprom_test(void)
hi1000 2:61a0169765bf 240 {
hi1000 2:61a0169765bf 241 // EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C64); // 24C64 eeprom with sda = p9 and scl = p10
hi1000 2:61a0169765bf 242 uint8_t data[256],data_r[256];
hi1000 2:61a0169765bf 243 int8_t ival;
hi1000 2:61a0169765bf 244 uint16_t s;
hi1000 2:61a0169765bf 245 int16_t sdata,sdata_r;
hi1000 2:61a0169765bf 246 int32_t ldata[1024];
hi1000 2:61a0169765bf 247 int32_t eeprom_size,max_size;
hi1000 2:61a0169765bf 248 uint32_t addr;
hi1000 2:61a0169765bf 249 int32_t idata,idata_r;
hi1000 2:61a0169765bf 250 uint32_t i,j,k,l,t,id;
hi1000 2:61a0169765bf 251 float fdata,fdata_r;
hi1000 2:61a0169765bf 252 MyData md,md_r;
hi1000 2:61a0169765bf 253
hi1000 2:61a0169765bf 254 eeprom_size = ep.getSize();
hi1000 2:61a0169765bf 255 max_size = MIN(eeprom_size,256);
hi1000 2:61a0169765bf 256
hi1000 2:61a0169765bf 257 printf("Test EEPROM I2C model %s of %d bytes\r\n",ep.getName(),eeprom_size);
hi1000 2:61a0169765bf 258
hi1000 2:61a0169765bf 259 // Test sequential read byte (max_size first bytes)
hi1000 2:61a0169765bf 260 for(i = 0;i < max_size;i++) {
hi1000 2:61a0169765bf 261 ep.read(i,ival);
hi1000 2:61a0169765bf 262 data_r[i] = ival;
hi1000 2:61a0169765bf 263 if(ep.getError() != 0)
hi1000 2:61a0169765bf 264 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 265 }
hi1000 2:61a0169765bf 266
hi1000 2:61a0169765bf 267 printf("Test sequential read %d first bytes :\r\n",max_size);
hi1000 2:61a0169765bf 268 for(i = 0;i < max_size/16;i++) {
hi1000 2:61a0169765bf 269 for(j = 0;j < 16;j++) {
hi1000 2:61a0169765bf 270 addr = i * 16 + j;
hi1000 2:61a0169765bf 271 printf("%3d ",(uint8_t)data_r[addr]);
hi1000 2:61a0169765bf 272 }
hi1000 2:61a0169765bf 273 printf("\r\n");
hi1000 2:61a0169765bf 274 }
hi1000 2:61a0169765bf 275
hi1000 2:61a0169765bf 276 // Test sequential read byte (max_size last bytes)
hi1000 2:61a0169765bf 277 for(i = 0;i < max_size;i++) {
hi1000 2:61a0169765bf 278 addr = eeprom_size - max_size + i;
hi1000 2:61a0169765bf 279 ep.read(addr,ival);
hi1000 2:61a0169765bf 280 data_r[i] = ival;
hi1000 2:61a0169765bf 281 if(ep.getError() != 0)
hi1000 2:61a0169765bf 282 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 283 }
hi1000 2:61a0169765bf 284
hi1000 2:61a0169765bf 285 printf("\nTest sequential read %d last bytes :\r\n",max_size);
hi1000 2:61a0169765bf 286 for(i = 0;i < max_size/16;i++) {
hi1000 2:61a0169765bf 287 for(j = 0;j < 16;j++) {
hi1000 2:61a0169765bf 288 addr = i * 16 + j;
hi1000 2:61a0169765bf 289 printf("%3d ",(uint8_t)data_r[addr]);
hi1000 2:61a0169765bf 290 }
hi1000 2:61a0169765bf 291 printf("\r\n");
hi1000 2:61a0169765bf 292 }
hi1000 2:61a0169765bf 293
hi1000 2:61a0169765bf 294 // Test write byte (max_size first bytes)
hi1000 2:61a0169765bf 295 for(i = 0;i < max_size;i++)
hi1000 2:61a0169765bf 296 data[i] = i;
hi1000 2:61a0169765bf 297
hi1000 2:61a0169765bf 298 for(i = 0;i < max_size;i++) {
hi1000 2:61a0169765bf 299 ep.write(i,(int8_t)data[i]);
hi1000 2:61a0169765bf 300 if(ep.getError() != 0)
hi1000 2:61a0169765bf 301 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 302 }
hi1000 2:61a0169765bf 303
hi1000 2:61a0169765bf 304 // Test read byte (max_size first bytes)
hi1000 2:61a0169765bf 305 for(i = 0;i < max_size;i++) {
hi1000 2:61a0169765bf 306 ep.read(i,(int8_t&)ival);
hi1000 2:61a0169765bf 307 data_r[i] = (uint8_t)ival;
hi1000 2:61a0169765bf 308 if(ep.getError() != 0)
hi1000 2:61a0169765bf 309 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 310 }
hi1000 2:61a0169765bf 311
hi1000 2:61a0169765bf 312 printf("\nTest write and read %d first bytes :\r\n",max_size);
hi1000 2:61a0169765bf 313 for(i = 0;i < max_size/16;i++) {
hi1000 2:61a0169765bf 314 for(j = 0;j < 16;j++) {
hi1000 2:61a0169765bf 315 addr = i * 16 + j;
hi1000 2:61a0169765bf 316 printf("%3d ",(uint8_t)data_r[addr]);
hi1000 2:61a0169765bf 317 }
hi1000 2:61a0169765bf 318 printf("\r\n");
hi1000 2:61a0169765bf 319 }
hi1000 2:61a0169765bf 320
hi1000 2:61a0169765bf 321 // Test current address read byte (max_size first bytes)
hi1000 2:61a0169765bf 322 ep.read((uint32_t)0,(int8_t&)ival); // current address is 0
hi1000 2:61a0169765bf 323 data_r[0] = (uint8_t)ival;
hi1000 2:61a0169765bf 324 if(ep.getError() != 0)
hi1000 2:61a0169765bf 325 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 326
hi1000 2:61a0169765bf 327 for(i = 1;i < max_size;i++) {
hi1000 2:61a0169765bf 328 ep.read((int8_t&)ival);
hi1000 2:61a0169765bf 329 data_r[i] = (uint8_t)ival;
hi1000 2:61a0169765bf 330 if(ep.getError() != 0)
hi1000 2:61a0169765bf 331 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 332 }
hi1000 2:61a0169765bf 333
hi1000 2:61a0169765bf 334 printf("\nTest current address read %d first bytes :\r\n",max_size);
hi1000 2:61a0169765bf 335 for(i = 0;i < max_size/16;i++) {
hi1000 2:61a0169765bf 336 for(j = 0;j < 16;j++) {
hi1000 2:61a0169765bf 337 addr = i * 16 + j;
hi1000 2:61a0169765bf 338 printf("%3d ",(uint8_t)data_r[addr]);
hi1000 2:61a0169765bf 339 }
hi1000 2:61a0169765bf 340 printf("\n");
hi1000 2:61a0169765bf 341 }
hi1000 2:61a0169765bf 342
hi1000 2:61a0169765bf 343 // Test sequential read byte (first max_size bytes)
hi1000 2:61a0169765bf 344 ep.read((uint32_t)0,(int8_t *)data_r,(uint32_t) max_size);
hi1000 2:61a0169765bf 345 if(ep.getError() != 0)
hi1000 2:61a0169765bf 346 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 347
hi1000 2:61a0169765bf 348 printf("\nTest sequential read %d first bytes :\r\n",max_size);
hi1000 2:61a0169765bf 349 for(i = 0;i < max_size/16;i++) {
hi1000 2:61a0169765bf 350 for(j = 0;j < 16;j++) {
hi1000 2:61a0169765bf 351 addr = i * 16 + j;
hi1000 2:61a0169765bf 352 printf("%3d ",(uint8_t)data_r[addr]);
hi1000 2:61a0169765bf 353 }
hi1000 2:61a0169765bf 354 printf("\r\n");
hi1000 2:61a0169765bf 355 }
hi1000 2:61a0169765bf 356
hi1000 2:61a0169765bf 357 // Test write short, long, float
hi1000 2:61a0169765bf 358 sdata = -15202;
hi1000 2:61a0169765bf 359 addr = eeprom_size - 16;
hi1000 2:61a0169765bf 360 ep.write(addr,(int16_t)sdata); // short write at address eeprom_size - 16
hi1000 2:61a0169765bf 361 if(ep.getError() != 0)
hi1000 2:61a0169765bf 362 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 363
hi1000 2:61a0169765bf 364 idata = 45123;
hi1000 2:61a0169765bf 365 addr = eeprom_size - 12;
hi1000 2:61a0169765bf 366 ep.write(addr,(int32_t)idata); // long write at address eeprom_size - 12
hi1000 2:61a0169765bf 367 if(ep.getError() != 0)
hi1000 2:61a0169765bf 368 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 369
hi1000 2:61a0169765bf 370 fdata = -12.26;
hi1000 2:61a0169765bf 371 addr = eeprom_size - 8;
hi1000 2:61a0169765bf 372 ep.write(addr,(float)fdata); // float write at address eeprom_size - 8
hi1000 2:61a0169765bf 373 if(ep.getError() != 0)
hi1000 2:61a0169765bf 374 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 375
hi1000 2:61a0169765bf 376 // Test read short, long, float
hi1000 2:61a0169765bf 377 printf("\nTest write and read short (%d), long (%d), float (%f) :\r\n",
hi1000 2:61a0169765bf 378 sdata,idata,fdata);
hi1000 2:61a0169765bf 379
hi1000 2:61a0169765bf 380 ep.read((uint32_t)(eeprom_size - 16),(int16_t&)sdata_r);
hi1000 2:61a0169765bf 381 if(ep.getError() != 0)
hi1000 2:61a0169765bf 382 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 383 printf("sdata %d\r\n",sdata_r);
hi1000 2:61a0169765bf 384
hi1000 2:61a0169765bf 385 ep.read((uint32_t)(eeprom_size - 12),(int32_t&)idata_r);
hi1000 2:61a0169765bf 386 if(ep.getError() != 0)
hi1000 2:61a0169765bf 387 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 388 printf("idata %d\r\n",idata_r);
hi1000 2:61a0169765bf 389
hi1000 2:61a0169765bf 390 ep.read((uint32_t)(eeprom_size - 8),fdata_r);
hi1000 2:61a0169765bf 391 if(ep.getError() != 0)
hi1000 2:61a0169765bf 392 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 393 printf("fdata %f\r\n",fdata_r);
hi1000 2:61a0169765bf 394
hi1000 2:61a0169765bf 395 // Test read and write a structure
hi1000 2:61a0169765bf 396 md.sdata = -15203;
hi1000 2:61a0169765bf 397 md.idata = 45124;
hi1000 2:61a0169765bf 398 md.fdata = -12.27;
hi1000 2:61a0169765bf 399
hi1000 2:61a0169765bf 400 ep.write((uint32_t)(eeprom_size - 32),(void *)&md,sizeof(md)); // write a structure eeprom_size - 32
hi1000 2:61a0169765bf 401 if(ep.getError() != 0)
hi1000 2:61a0169765bf 402 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 403
hi1000 2:61a0169765bf 404 printf("\nTest write and read a structure (%d %d %f) :\r\n",md.sdata,md.idata,md.fdata);
hi1000 2:61a0169765bf 405
hi1000 2:61a0169765bf 406 ep.read((uint32_t)(eeprom_size - 32),(void *)&md_r,sizeof(md_r));
hi1000 2:61a0169765bf 407 if(ep.getError() != 0)
hi1000 2:61a0169765bf 408 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 409
hi1000 2:61a0169765bf 410 printf("md.sdata %d\r\n",md_r.sdata);
hi1000 2:61a0169765bf 411 printf("md.idata %d\r\n",md_r.idata);
hi1000 2:61a0169765bf 412 printf("md.fdata %f\r\n",md_r.fdata);
hi1000 2:61a0169765bf 413
hi1000 2:61a0169765bf 414 // Test read and write of an array of the first max_size bytes
hi1000 2:61a0169765bf 415 for(i = 0;i < max_size;i++)
hi1000 2:61a0169765bf 416 data[i] = max_size - i - 1;
hi1000 2:61a0169765bf 417
hi1000 2:61a0169765bf 418 ep.write((uint32_t)(0),data,(uint32_t)max_size);
hi1000 2:61a0169765bf 419 if(ep.getError() != 0)
hi1000 2:61a0169765bf 420 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 421
hi1000 2:61a0169765bf 422 ep.read((uint32_t)(0),data_r,(uint32_t)max_size);
hi1000 2:61a0169765bf 423 if(ep.getError() != 0)
hi1000 2:61a0169765bf 424 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 425
hi1000 2:61a0169765bf 426 printf("\nTest write and read an array of the first %d bytes :\r\n",max_size);
hi1000 2:61a0169765bf 427 for(i = 0;i < max_size/16;i++) {
hi1000 2:61a0169765bf 428 for(j = 0;j < 16;j++) {
hi1000 2:61a0169765bf 429 addr = i * 16 + j;
hi1000 2:61a0169765bf 430 printf("%3d ",(uint8_t)data_r[addr]);
hi1000 2:61a0169765bf 431 }
hi1000 2:61a0169765bf 432 printf("\r\n");
hi1000 2:61a0169765bf 433 }
hi1000 2:61a0169765bf 434 printf("\r\n");
hi1000 2:61a0169765bf 435 #if 0
hi1000 2:61a0169765bf 436 // Test write and read an array of int32
hi1000 2:61a0169765bf 437 s = eeprom_size / 4; // size of eeprom in int32
hi1000 2:61a0169765bf 438 int ldata_size = sizeof(ldata) / 4; // size of data array in int32
hi1000 2:61a0169765bf 439 l = s / ldata_size; // loop index
hi1000 2:61a0169765bf 440
hi1000 2:61a0169765bf 441 // size of read / write in bytes
hi1000 2:61a0169765bf 442 t = eeprom_size;
hi1000 2:61a0169765bf 443 if(t > ldata_size * 4)
hi1000 2:61a0169765bf 444 t = ldata_size * 4;
hi1000 2:61a0169765bf 445
hi1000 2:61a0169765bf 446 printf("Test write and read an array of %d int32 (write entire memory) :\r\n",t/4);
hi1000 2:61a0169765bf 447
hi1000 2:61a0169765bf 448 // Write entire eeprom
hi1000 2:61a0169765bf 449 if(l) {
hi1000 2:61a0169765bf 450 for(k = 0;k < l;k++) {
hi1000 2:61a0169765bf 451 for(i = 0;i < ldata_size;i++)
hi1000 2:61a0169765bf 452 ldata[i] = ldata_size * k + i;
hi1000 2:61a0169765bf 453
hi1000 2:61a0169765bf 454 addr = k * ldata_size * 4;
hi1000 2:61a0169765bf 455 ep.write(addr,(void *)ldata,t);
hi1000 2:61a0169765bf 456 if(ep.getError() != 0)
hi1000 2:61a0169765bf 457 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 458 }
hi1000 2:61a0169765bf 459
hi1000 2:61a0169765bf 460 printf("Write OK\n");
hi1000 2:61a0169765bf 461
hi1000 2:61a0169765bf 462 // Read entire eeprom
hi1000 2:61a0169765bf 463 id = 0;
hi1000 2:61a0169765bf 464 for(k = 0;k < l;k++) {
hi1000 2:61a0169765bf 465 addr = k * ldata_size * 4;
hi1000 2:61a0169765bf 466 ep.read(addr,(void *)ldata,t);
hi1000 2:61a0169765bf 467 if(ep.getError() != 0)
hi1000 2:61a0169765bf 468 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 469
hi1000 2:61a0169765bf 470 // format outputs with 8 words rows
hi1000 2:61a0169765bf 471 for(i = 0;i < ldata_size / 8;i++) {
hi1000 2:61a0169765bf 472 id++;
hi1000 2:61a0169765bf 473 printf("%4d ",id);
hi1000 2:61a0169765bf 474 for(j = 0;j < 8;j++) {
hi1000 2:61a0169765bf 475 addr = i * 8 + j;
hi1000 2:61a0169765bf 476 printf("%5d ",ldata[addr]);
hi1000 2:61a0169765bf 477 }
hi1000 2:61a0169765bf 478 printf("\n");
hi1000 2:61a0169765bf 479 }
hi1000 2:61a0169765bf 480 }
hi1000 2:61a0169765bf 481 }
hi1000 2:61a0169765bf 482 else {
hi1000 2:61a0169765bf 483 for(i = 0;i < s;i++)
hi1000 2:61a0169765bf 484 ldata[i] = i;
hi1000 2:61a0169765bf 485
hi1000 2:61a0169765bf 486 addr = 0;
hi1000 2:61a0169765bf 487 ep.write(addr,(void *)ldata,t);
hi1000 2:61a0169765bf 488 if(ep.getError() != 0)
hi1000 2:61a0169765bf 489 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 490
hi1000 2:61a0169765bf 491 printf("Write OK\n");
hi1000 2:61a0169765bf 492
hi1000 2:61a0169765bf 493 // Read entire eeprom
hi1000 2:61a0169765bf 494 id = 0;
hi1000 2:61a0169765bf 495
hi1000 2:61a0169765bf 496 addr = 0;
hi1000 2:61a0169765bf 497 ep.read(addr,(void *)ldata,t);
hi1000 2:61a0169765bf 498 if(ep.getError() != 0)
hi1000 2:61a0169765bf 499 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 500
hi1000 2:61a0169765bf 501 // format outputs with 8 words rows
hi1000 2:61a0169765bf 502 for(i = 0;i < s / 8;i++) {
hi1000 2:61a0169765bf 503 id++;
hi1000 2:61a0169765bf 504 printf("%4d ",id);
hi1000 2:61a0169765bf 505 for(j = 0;j < 8;j++) {
hi1000 2:61a0169765bf 506 addr = i * 8 + j;
hi1000 2:61a0169765bf 507 printf("%5d ",ldata[addr]);
hi1000 2:61a0169765bf 508 }
hi1000 2:61a0169765bf 509 printf("\n");
hi1000 2:61a0169765bf 510 }
hi1000 2:61a0169765bf 511 }
hi1000 2:61a0169765bf 512 #endif
hi1000 2:61a0169765bf 513 // clear eeprom
hi1000 2:61a0169765bf 514 printf("\nClear eeprom\n");
hi1000 2:61a0169765bf 515
hi1000 2:61a0169765bf 516 ep.clear();
hi1000 2:61a0169765bf 517 if(ep.getError() != 0)
hi1000 2:61a0169765bf 518 myerror(ep.getErrorMessage());
hi1000 2:61a0169765bf 519
hi1000 2:61a0169765bf 520 printf("End\n");
hi1000 2:61a0169765bf 521
hi1000 2:61a0169765bf 522 }
hi1000 2:61a0169765bf 523
hi1000 2:61a0169765bf 524
hi1000 0:765cf978c3e5 525 int main()
hi1000 0:765cf978c3e5 526 {
hi1000 2:61a0169765bf 527 wait(1);
hi1000 5:4585215afd11 528 #ifdef LCD_1621
hi1000 4:40bb33497de4 529 lcd.clear(); // clears display
hi1000 4:40bb33497de4 530 lcd.allsegson();
hi1000 5:4585215afd11 531
hi1000 4:40bb33497de4 532 // lcd.printf("ABCDEFGHI"); // Standard printf function, All ASCII characters will display
hi1000 5:4585215afd11 533 #endif
hi1000 2:61a0169765bf 534 printf("\n\n*** RTOS basic example ***\r\n");
hi1000 5:4585215afd11 535 #ifdef LCD_1602
hi1000 5:4585215afd11 536 lcd.printf( 0, "Hello world!" ); // line# (0 or 1), string
hi1000 5:4585215afd11 537 lcd.printf( 1, "pi = %.6f", 3.14159265 );
hi1000 5:4585215afd11 538 #endif
hi1000 2:61a0169765bf 539 init_scale();
hi1000 1:eb499e2a1b9b 540 thread.start(print_thread);
hi1000 0:765cf978c3e5 541
hi1000 1:eb499e2a1b9b 542 // flashIAP.init();
hi1000 1:eb499e2a1b9b 543 // printf("Flash start address: 0x%08x Flash Size: %d\r\n", flashIAP.get_flash_start(), flashIAP.get_flash_size());
hi1000 0:765cf978c3e5 544 // can1.reset();
hi1000 0:765cf978c3e5 545 // can2.reset();
hi1000 2:61a0169765bf 546 can1.frequency(100000);
hi1000 0:765cf978c3e5 547 // can2.frequency(100000);
hi1000 0:765cf978c3e5 548 //button1.mode(PullUp); // Activate pull-up
hi1000 2:61a0169765bf 549 button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
hi1000 2:61a0169765bf 550 // eeprom_test();
hi1000 0:765cf978c3e5 551
hi1000 0:765cf978c3e5 552 int idx = 0; // Just for printf below
hi1000 0:765cf978c3e5 553
hi1000 0:765cf978c3e5 554 while(1) {
hi1000 0:765cf978c3e5 555 if (button1_pressed) { // Set when button is pressed
hi1000 1:eb499e2a1b9b 556 printf("scale value %f. \r\n", hx711.getGram());
hi1000 0:765cf978c3e5 557 button1_pressed = false;
hi1000 2:61a0169765bf 558 printf("Button pressed %d\r\n", idx++);
hi1000 2:61a0169765bf 559 printf("ID=%d. \r\n", init_id + idx%10);
hi1000 2:61a0169765bf 560 can1.write(CANMessage((init_id + idx%10), reinterpret_cast<char*>(&a), 1));
hi1000 0:765cf978c3e5 561 led1 = !led1;
hi1000 0:765cf978c3e5 562 a++;
hi1000 0:765cf978c3e5 563 }
hi1000 0:765cf978c3e5 564 }
hi1000 0:765cf978c3e5 565 #if 0
hi1000 0:765cf978c3e5 566 while(1) {
hi1000 0:765cf978c3e5 567 // can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), sizeof(a)));
hi1000 0:765cf978c3e5 568 #if
hi1000 0:765cf978c3e5 569 can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), 1));
hi1000 0:765cf978c3e5 570 #endif
hi1000 0:765cf978c3e5 571 printf("loop a=%d\n", a);
hi1000 0:765cf978c3e5 572 led1 = !led1;
hi1000 0:765cf978c3e5 573 a++;
hi1000 0:765cf978c3e5 574 wait(0.2);
hi1000 0:765cf978c3e5 575 }
hi1000 0:765cf978c3e5 576 #endif
hi1000 0:765cf978c3e5 577 }