WathchDog

Dependencies:   mbed WDT

Committer:
orain3
Date:
Fri Aug 16 08:43:23 2019 +0000
Revision:
7:f201e2d0ed4f
Parent:
6:1948d19f854f
Child:
8:0c5a091410cf
WDG NGver notUsed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
orain3 0:63c74ee8bffc 1 #include "mbed.h"
orain3 0:63c74ee8bffc 2
orain3 6:1948d19f854f 3
orain3 6:1948d19f854f 4 class WatchDog {
orain3 6:1948d19f854f 5 private:
orain3 6:1948d19f854f 6 IWDG_HandleTypeDef hiwdg;
orain3 6:1948d19f854f 7 public:
orain3 7:f201e2d0ed4f 8 WatchDog(uint32_t prescaler = IWDG_PRESCALER_256, uint32_t reload = 0x0FFF) {
orain3 6:1948d19f854f 9 hiwdg.Instance = (IWDG_TypeDef*)0x40003000;
orain3 6:1948d19f854f 10 hiwdg.Init.Prescaler = prescaler;
orain3 6:1948d19f854f 11 hiwdg.Init.Reload = reload;
orain3 6:1948d19f854f 12 HAL_IWDG_Init(&hiwdg);
orain3 6:1948d19f854f 13 }
orain3 6:1948d19f854f 14 void kick() {
orain3 6:1948d19f854f 15 HAL_IWDG_Refresh(&hiwdg);
orain3 6:1948d19f854f 16 }
orain3 6:1948d19f854f 17 };
orain3 6:1948d19f854f 18
orain3 6:1948d19f854f 19
orain3 4:cb6587253326 20 //const int _ID = 4;
orain3 3:b082ed02d08c 21
orain3 3:b082ed02d08c 22 void irqrecv();
orain3 4:cb6587253326 23 int rs485_recv(float ,float ,float ,float ,float);
orain3 3:b082ed02d08c 24 char rsgetc();
orain3 3:b082ed02d08c 25 int rslen();
orain3 3:b082ed02d08c 26
orain3 0:63c74ee8bffc 27 Serial pc(SERIAL_TX,SERIAL_RX);
orain3 1:c896654c679b 28 Serial modem(D1,D0);
orain3 1:c896654c679b 29 AnalogIn adc_1(A0);
orain3 1:c896654c679b 30 AnalogIn adc_2(A1);
orain3 1:c896654c679b 31 AnalogIn adc_3(A2);
orain3 1:c896654c679b 32 AnalogIn adc_4(A3);
orain3 0:63c74ee8bffc 33
orain3 0:63c74ee8bffc 34 DigitalOut led(LED1);
orain3 1:c896654c679b 35 DigitalOut sokutei(D3);
orain3 4:cb6587253326 36 DigitalOut pos1(D4);
orain3 4:cb6587253326 37 DigitalOut pos2(D5);
orain3 1:c896654c679b 38 DigitalIn in1(D9,PullNone);
orain3 1:c896654c679b 39 DigitalIn in2(D10,PullNone);
orain3 1:c896654c679b 40 DigitalIn kansyutu(D6,PullNone);
orain3 1:c896654c679b 41 DigitalIn commandin(D7,PullNone);
orain3 1:c896654c679b 42 DigitalIn jmp(D2,PullUp);
orain3 4:cb6587253326 43 DigitalIn pulse1(D9,PullNone);
orain3 4:cb6587253326 44 DigitalIn pulse2(D10,PullNone);
orain3 4:cb6587253326 45
orain3 4:cb6587253326 46 Timer pulse_watcher;
orain3 0:63c74ee8bffc 47
orain3 0:63c74ee8bffc 48
orain3 7:f201e2d0ed4f 49 #define bufsize 2048
orain3 3:b082ed02d08c 50 char RxBuff[bufsize+1];
orain3 3:b082ed02d08c 51 int RxPtr;
orain3 3:b082ed02d08c 52 int RdPtr;
orain3 3:b082ed02d08c 53
orain3 4:cb6587253326 54 int data[40];
orain3 4:cb6587253326 55
orain3 4:cb6587253326 56
orain3 3:b082ed02d08c 57 char rsgetc() {
orain3 3:b082ed02d08c 58 // pc.printf("%c",RxBuff[RdPtr]);
orain3 3:b082ed02d08c 59
orain3 3:b082ed02d08c 60 RdPtr++;
orain3 3:b082ed02d08c 61 if(RdPtr >= bufsize-1) RdPtr = 0;
orain3 3:b082ed02d08c 62 return(RxBuff[RdPtr]);
orain3 3:b082ed02d08c 63 }
orain3 3:b082ed02d08c 64
orain3 3:b082ed02d08c 65 int rslen() {
orain3 3:b082ed02d08c 66 // if (RxPtr < RdPtr) {
orain3 3:b082ed02d08c 67 // return(bufsize + RxPtr - RdPtr);
orain3 3:b082ed02d08c 68 // } else {
orain3 3:b082ed02d08c 69 // return(RxPtr - RdPtr);
orain3 3:b082ed02d08c 70 // }
orain3 3:b082ed02d08c 71 return(RxPtr - RdPtr);
orain3 3:b082ed02d08c 72 }
orain3 3:b082ed02d08c 73
orain3 3:b082ed02d08c 74 void irqrecv() {
orain3 3:b082ed02d08c 75 if (modem.readable()) {
orain3 3:b082ed02d08c 76 if (RxPtr <= bufsize - 1) {
orain3 3:b082ed02d08c 77 RxBuff[++RxPtr] = modem.getc();
orain3 3:b082ed02d08c 78 } else {
orain3 3:b082ed02d08c 79 RxPtr = 0;
orain3 3:b082ed02d08c 80 RxBuff[RxPtr] = modem.getc();
orain3 3:b082ed02d08c 81 }
orain3 3:b082ed02d08c 82 }
orain3 3:b082ed02d08c 83 }
orain3 3:b082ed02d08c 84
orain3 7:f201e2d0ed4f 85 //unsigned char buf485[512];
orain3 7:f201e2d0ed4f 86 unsigned char buf485[2048];
orain3 3:b082ed02d08c 87 int cnt485;
orain3 3:b082ed02d08c 88
orain3 0:63c74ee8bffc 89 int main()
orain3 0:63c74ee8bffc 90 {
orain3 1:c896654c679b 91 float sum1,sum2,sum3,sum4;
orain3 1:c896654c679b 92 float ad1,ad2,ad3,ad4;
orain3 3:b082ed02d08c 93 static float temp;//,turb,s;
orain3 0:63c74ee8bffc 94 int i,cnt;
orain3 4:cb6587253326 95 int pulse;
orain3 0:63c74ee8bffc 96
orain3 7:f201e2d0ed4f 97 // WatchDog wdg(IWDG_PRESCALER_16, 0x0FFF);
orain3 6:1948d19f854f 98
orain3 1:c896654c679b 99 modem.baud(38400);
orain3 1:c896654c679b 100 modem.format(8,Serial::None,1);
orain3 3:b082ed02d08c 101 RxPtr = 0;
orain3 3:b082ed02d08c 102 RdPtr = 0;
orain3 3:b082ed02d08c 103 modem.attach(irqrecv,RawSerial::RxIrq);
orain3 3:b082ed02d08c 104 cnt485 = 0;
orain3 3:b082ed02d08c 105 buf485[0] = '\0';
orain3 0:63c74ee8bffc 106
orain3 0:63c74ee8bffc 107 pc.printf("\nSTM32 ADC internal channels reading example\r\n");
orain3 7:f201e2d0ed4f 108
orain3 7:f201e2d0ed4f 109 // wdg.kick(); // IWDGをリセット
orain3 7:f201e2d0ed4f 110
orain3 4:cb6587253326 111 pos1 = 1;
orain3 4:cb6587253326 112 pos2 = 1;
orain3 4:cb6587253326 113
orain3 0:63c74ee8bffc 114 temp = 0;
orain3 3:b082ed02d08c 115 // turb = 0;
orain3 3:b082ed02d08c 116 // s = 0;
orain3 1:c896654c679b 117 sokutei = 1;
orain3 4:cb6587253326 118 pulse_watcher.start();
orain3 4:cb6587253326 119 pulse = pulse1 || pulse2;
orain3 4:cb6587253326 120
orain3 7:f201e2d0ed4f 121 for (i = 0;i<40;i++) {
orain3 7:f201e2d0ed4f 122 data[i] = 0;
orain3 7:f201e2d0ed4f 123 // wdg.kick(); // IWDGをリセット
orain3 7:f201e2d0ed4f 124 }
orain3 1:c896654c679b 125
orain3 0:63c74ee8bffc 126 while(1) {
orain3 4:cb6587253326 127 if (pulse_watcher.read() > 100) pulse_watcher.reset();
orain3 1:c896654c679b 128
orain3 4:cb6587253326 129 // if (jmp) sokutei = 1;
orain3 4:cb6587253326 130 // else sokutei = 0;
orain3 4:cb6587253326 131
orain3 4:cb6587253326 132 // if (jmp) {
orain3 4:cb6587253326 133 // pos1 = 1;
orain3 4:cb6587253326 134 // pos2 = 1;
orain3 4:cb6587253326 135 // } else {
orain3 4:cb6587253326 136 // pos1 = 0;
orain3 4:cb6587253326 137 // pos2 = 0;
orain3 4:cb6587253326 138 // }
orain3 1:c896654c679b 139
orain3 0:63c74ee8bffc 140 sum1 = 0;
orain3 0:63c74ee8bffc 141 sum2 = 0;
orain3 1:c896654c679b 142 sum3 = 0;
orain3 1:c896654c679b 143 sum4 = 0;
orain3 0:63c74ee8bffc 144 cnt = 0;
orain3 3:b082ed02d08c 145 for (i=0;i<1000;i++) {
orain3 4:cb6587253326 146 rs485_recv(temp,ad1,ad2,ad3,ad4);
orain3 3:b082ed02d08c 147
orain3 0:63c74ee8bffc 148 sum1 += adc_1.read();
orain3 0:63c74ee8bffc 149 sum2 += adc_2.read();
orain3 1:c896654c679b 150 sum3 += adc_3.read();
orain3 1:c896654c679b 151 sum4 += adc_4.read();
orain3 0:63c74ee8bffc 152 cnt += 1;
orain3 0:63c74ee8bffc 153 wait(0.001);
orain3 0:63c74ee8bffc 154
orain3 2:d69d10b670c5 155 // if (modem.readable()) {
orain3 2:d69d10b670c5 156 // pc.printf("%c\r",modem.getc());
orain3 2:d69d10b670c5 157 // wait(0.01);
orain3 2:d69d10b670c5 158 // modem.printf("=04,PVAL,%01.04f,0,\r",temp);
orain3 2:d69d10b670c5 159 // }
orain3 4:cb6587253326 160 // pulse watch
orain3 4:cb6587253326 161 if ( (pulse1 || pulse2) != pulse ) {
orain3 4:cb6587253326 162 pulse_watcher.reset();
orain3 4:cb6587253326 163 pos1 = 0;
orain3 4:cb6587253326 164 pos2 = 0;
orain3 4:cb6587253326 165 }
orain3 4:cb6587253326 166 pulse = pulse1 || pulse2;
orain3 4:cb6587253326 167 if ( pulse_watcher.read() > 10) {
orain3 4:cb6587253326 168 pos1 = 1;
orain3 4:cb6587253326 169 pos2 = 1;
orain3 4:cb6587253326 170 }
orain3 4:cb6587253326 171
orain3 7:f201e2d0ed4f 172 // wdg.kick(); // IWDGをリセット
orain3 7:f201e2d0ed4f 173
orain3 4:cb6587253326 174 // pulsewatch
orain3 0:63c74ee8bffc 175 }
orain3 0:63c74ee8bffc 176 if(cnt != 0) {
orain3 0:63c74ee8bffc 177 ad1 = sum1/cnt;
orain3 0:63c74ee8bffc 178 ad2 = sum2/cnt;
orain3 1:c896654c679b 179 ad3 = sum3/cnt;
orain3 1:c896654c679b 180 ad4 = sum4/cnt;
orain3 0:63c74ee8bffc 181 } else {
orain3 0:63c74ee8bffc 182 ad1 = adc_1.read();
orain3 0:63c74ee8bffc 183 ad2 = adc_2.read();
orain3 1:c896654c679b 184 ad3 = adc_3.read();
orain3 1:c896654c679b 185 ad4 = adc_4.read();
orain3 0:63c74ee8bffc 186 }
orain3 0:63c74ee8bffc 187
orain3 3:b082ed02d08c 188 if (ad1 < 0.2435L) ad1 = 0.2435L;
orain3 3:b082ed02d08c 189 if (ad1 > 0.8085L) ad1 = 0.8085L;
orain3 3:b082ed02d08c 190
orain3 3:b082ed02d08c 191 temp = (double)(ad1 - 0.2435L) * 60.0L / (0.8085L - 0.2425L) - 10.0L;
orain3 3:b082ed02d08c 192 // pc.printf("ADC = %f %f %f %f %f \r\n", ad1,ad2,ad3,ad4,temp);
orain3 3:b082ed02d08c 193 // s = 0;
orain3 0:63c74ee8bffc 194
orain3 4:cb6587253326 195 // data set
orain3 4:cb6587253326 196 data[8] = ad1 * 0xffff;
orain3 4:cb6587253326 197 data[9] = ad2 * 0xffff;
orain3 4:cb6587253326 198 data[10] = ad3 * 0xffff;
orain3 4:cb6587253326 199 data[12] = ad4 * 0xffff;
orain3 6:1948d19f854f 200
orain3 7:f201e2d0ed4f 201 //WDT test
orain3 7:f201e2d0ed4f 202 // if (jmp == 0) while(1) ;
orain3 7:f201e2d0ed4f 203
orain3 7:f201e2d0ed4f 204 // wdg.kick(); // IWDGをリセット
orain3 0:63c74ee8bffc 205 }
orain3 0:63c74ee8bffc 206 }
orain3 3:b082ed02d08c 207
orain3 3:b082ed02d08c 208
orain3 4:cb6587253326 209 int rs485_recv(float temp,float ad1,float ad2,float ad3,float ad4) {
orain3 3:b082ed02d08c 210 char chr;
orain3 4:cb6587253326 211 // char idbuf[8];
orain3 4:cb6587253326 212 char sendbuf[512];
orain3 4:cb6587253326 213 // int i;
orain3 4:cb6587253326 214
orain3 3:b082ed02d08c 215 if (rslen()) {
orain3 3:b082ed02d08c 216
orain3 3:b082ed02d08c 217 chr = rsgetc();
orain3 3:b082ed02d08c 218 buf485[cnt485++] = chr;
orain3 3:b082ed02d08c 219 buf485[cnt485] = '\0';
orain3 3:b082ed02d08c 220 if (chr == '?') cnt485 = 0;
orain3 3:b082ed02d08c 221
orain3 4:cb6587253326 222 if (chr == 0x0a) {
orain3 5:efffadf6d0e0 223 // pc.printf("CR\r\n");
orain3 4:cb6587253326 224
orain3 4:cb6587253326 225 if( ('C' == buf485[0]) ) {
orain3 4:cb6587253326 226 if (kansyutu) data[0] = 1;
orain3 4:cb6587253326 227 else data[0] = 0;
orain3 3:b082ed02d08c 228
orain3 4:cb6587253326 229 // sokutei on/off
orain3 5:efffadf6d0e0 230 if( buf485[2] & 0x05 ) {
orain3 4:cb6587253326 231 sokutei = 1;
orain3 4:cb6587253326 232 data[0] = data[0] | 64 | 128;
orain3 4:cb6587253326 233 } else {
orain3 4:cb6587253326 234 sokutei = 0;
orain3 4:cb6587253326 235 }
orain3 5:efffadf6d0e0 236 if( buf485[2] & 0x0a ) {
orain3 4:cb6587253326 237 wait(0.1);
orain3 5:efffadf6d0e0 238 modem.printf("?00,WIPE,1,\r");
orain3 4:cb6587253326 239 } else {
orain3 4:cb6587253326 240 // answer
orain3 4:cb6587253326 241 sprintf(sendbuf,"=%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x,%04x\r\n",
orain3 4:cb6587253326 242 data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9],
orain3 4:cb6587253326 243 data[10],data[11],data[12],data[13],data[14],data[15],data[16],data[17],data[18],data[19],
orain3 4:cb6587253326 244 data[20],data[21],data[22],data[23],data[24],data[25],data[26],data[27],data[28],data[29],
orain3 4:cb6587253326 245 data[30],data[31],data[32],data[33],data[34],data[35],data[36],data[37],data[38],data[39]
orain3 4:cb6587253326 246 );
orain3 4:cb6587253326 247 //modem.printf("=%c%c,PVAL,%01.04f,%01.04f,%01.04f,%01.04f,%01.04f,%01d\r", 0x30 | idbuf[0],0x30 | idbuf[1],temp,ad1,ad2,ad3,ad4,kan);
orain3 4:cb6587253326 248
orain3 4:cb6587253326 249 wait(0.1);
orain3 4:cb6587253326 250 modem.printf("%s",sendbuf);
orain3 4:cb6587253326 251 pc.printf("%s",sendbuf);
orain3 4:cb6587253326 252 }
orain3 3:b082ed02d08c 253 }
orain3 4:cb6587253326 254 RxPtr = 0;
orain3 4:cb6587253326 255 RdPtr = 0;
orain3 4:cb6587253326 256 RxBuff[0] = '\0';
orain3 4:cb6587253326 257 buf485[0] = '\0'; // clear
orain3 4:cb6587253326 258 cnt485 = 0;
orain3 3:b082ed02d08c 259 }
orain3 3:b082ed02d08c 260 }
orain3 3:b082ed02d08c 261 return(0);
orain3 3:b082ed02d08c 262 }
orain3 3:b082ed02d08c 263
orain3 3:b082ed02d08c 264