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