WathchDog

Dependencies:   mbed WDT

Committer:
orain3
Date:
Thu Aug 15 14:16:46 2019 +0000
Revision:
6:1948d19f854f
Parent:
5:efffadf6d0e0
Child:
7:f201e2d0ed4f
WDT 0xff*256/32/1000=2sec

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 6:1948d19f854f 8 WatchDog(uint32_t prescaler = IWDG_PRESCALER_256, uint32_t reload = 0xFF) {
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 4:cb6587253326 49 #define bufsize 1024
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 4:cb6587253326 85 unsigned char buf485[512];
orain3 3:b082ed02d08c 86 int cnt485;
orain3 3:b082ed02d08c 87
orain3 0:63c74ee8bffc 88 int main()
orain3 0:63c74ee8bffc 89 {
orain3 1:c896654c679b 90 float sum1,sum2,sum3,sum4;
orain3 1:c896654c679b 91 float ad1,ad2,ad3,ad4;
orain3 3:b082ed02d08c 92 static float temp;//,turb,s;
orain3 0:63c74ee8bffc 93 int i,cnt;
orain3 4:cb6587253326 94 int pulse;
orain3 0:63c74ee8bffc 95
orain3 6:1948d19f854f 96 WatchDog wdg(IWDG_PRESCALER_8, 0x0FFF);
orain3 6:1948d19f854f 97
orain3 1:c896654c679b 98 modem.baud(38400);
orain3 1:c896654c679b 99 modem.format(8,Serial::None,1);
orain3 3:b082ed02d08c 100 RxPtr = 0;
orain3 3:b082ed02d08c 101 RdPtr = 0;
orain3 3:b082ed02d08c 102 modem.attach(irqrecv,RawSerial::RxIrq);
orain3 3:b082ed02d08c 103 cnt485 = 0;
orain3 3:b082ed02d08c 104 buf485[0] = '\0';
orain3 0:63c74ee8bffc 105
orain3 0:63c74ee8bffc 106 pc.printf("\nSTM32 ADC internal channels reading example\r\n");
orain3 4:cb6587253326 107 pos1 = 1;
orain3 4:cb6587253326 108 pos2 = 1;
orain3 4:cb6587253326 109
orain3 0:63c74ee8bffc 110 temp = 0;
orain3 3:b082ed02d08c 111 // turb = 0;
orain3 3:b082ed02d08c 112 // s = 0;
orain3 1:c896654c679b 113 sokutei = 1;
orain3 4:cb6587253326 114 pulse_watcher.start();
orain3 4:cb6587253326 115 pulse = pulse1 || pulse2;
orain3 4:cb6587253326 116
orain3 4:cb6587253326 117 for (i = 0;i<40;i++) data[i] = 0;
orain3 4:cb6587253326 118
orain3 1:c896654c679b 119
orain3 0:63c74ee8bffc 120 while(1) {
orain3 4:cb6587253326 121 if (pulse_watcher.read() > 100) pulse_watcher.reset();
orain3 1:c896654c679b 122
orain3 4:cb6587253326 123 // if (jmp) sokutei = 1;
orain3 4:cb6587253326 124 // else sokutei = 0;
orain3 4:cb6587253326 125
orain3 4:cb6587253326 126 // if (jmp) {
orain3 4:cb6587253326 127 // pos1 = 1;
orain3 4:cb6587253326 128 // pos2 = 1;
orain3 4:cb6587253326 129 // } else {
orain3 4:cb6587253326 130 // pos1 = 0;
orain3 4:cb6587253326 131 // pos2 = 0;
orain3 4:cb6587253326 132 // }
orain3 1:c896654c679b 133
orain3 0:63c74ee8bffc 134 sum1 = 0;
orain3 0:63c74ee8bffc 135 sum2 = 0;
orain3 1:c896654c679b 136 sum3 = 0;
orain3 1:c896654c679b 137 sum4 = 0;
orain3 0:63c74ee8bffc 138 cnt = 0;
orain3 3:b082ed02d08c 139 for (i=0;i<1000;i++) {
orain3 4:cb6587253326 140 rs485_recv(temp,ad1,ad2,ad3,ad4);
orain3 3:b082ed02d08c 141
orain3 0:63c74ee8bffc 142 sum1 += adc_1.read();
orain3 0:63c74ee8bffc 143 sum2 += adc_2.read();
orain3 1:c896654c679b 144 sum3 += adc_3.read();
orain3 1:c896654c679b 145 sum4 += adc_4.read();
orain3 0:63c74ee8bffc 146 cnt += 1;
orain3 0:63c74ee8bffc 147 wait(0.001);
orain3 0:63c74ee8bffc 148
orain3 2:d69d10b670c5 149 // if (modem.readable()) {
orain3 2:d69d10b670c5 150 // pc.printf("%c\r",modem.getc());
orain3 2:d69d10b670c5 151 // wait(0.01);
orain3 2:d69d10b670c5 152 // modem.printf("=04,PVAL,%01.04f,0,\r",temp);
orain3 2:d69d10b670c5 153 // }
orain3 4:cb6587253326 154 // pulse watch
orain3 4:cb6587253326 155 if ( (pulse1 || pulse2) != pulse ) {
orain3 4:cb6587253326 156 pulse_watcher.reset();
orain3 4:cb6587253326 157 pos1 = 0;
orain3 4:cb6587253326 158 pos2 = 0;
orain3 4:cb6587253326 159 }
orain3 4:cb6587253326 160 pulse = pulse1 || pulse2;
orain3 4:cb6587253326 161 if ( pulse_watcher.read() > 10) {
orain3 4:cb6587253326 162 pos1 = 1;
orain3 4:cb6587253326 163 pos2 = 1;
orain3 4:cb6587253326 164 }
orain3 4:cb6587253326 165
orain3 4:cb6587253326 166 // pulsewatch
orain3 0:63c74ee8bffc 167 }
orain3 0:63c74ee8bffc 168 if(cnt != 0) {
orain3 0:63c74ee8bffc 169 ad1 = sum1/cnt;
orain3 0:63c74ee8bffc 170 ad2 = sum2/cnt;
orain3 1:c896654c679b 171 ad3 = sum3/cnt;
orain3 1:c896654c679b 172 ad4 = sum4/cnt;
orain3 0:63c74ee8bffc 173 } else {
orain3 0:63c74ee8bffc 174 ad1 = adc_1.read();
orain3 0:63c74ee8bffc 175 ad2 = adc_2.read();
orain3 1:c896654c679b 176 ad3 = adc_3.read();
orain3 1:c896654c679b 177 ad4 = adc_4.read();
orain3 0:63c74ee8bffc 178 }
orain3 0:63c74ee8bffc 179
orain3 3:b082ed02d08c 180 if (ad1 < 0.2435L) ad1 = 0.2435L;
orain3 3:b082ed02d08c 181 if (ad1 > 0.8085L) ad1 = 0.8085L;
orain3 3:b082ed02d08c 182
orain3 3:b082ed02d08c 183 temp = (double)(ad1 - 0.2435L) * 60.0L / (0.8085L - 0.2425L) - 10.0L;
orain3 3:b082ed02d08c 184 // pc.printf("ADC = %f %f %f %f %f \r\n", ad1,ad2,ad3,ad4,temp);
orain3 3:b082ed02d08c 185 // s = 0;
orain3 0:63c74ee8bffc 186
orain3 4:cb6587253326 187 // data set
orain3 4:cb6587253326 188 data[8] = ad1 * 0xffff;
orain3 4:cb6587253326 189 data[9] = ad2 * 0xffff;
orain3 4:cb6587253326 190 data[10] = ad3 * 0xffff;
orain3 4:cb6587253326 191 data[12] = ad4 * 0xffff;
orain3 6:1948d19f854f 192
orain3 6:1948d19f854f 193 wdg.kick(); // IWDGをリセット
orain3 0:63c74ee8bffc 194 }
orain3 0:63c74ee8bffc 195 }
orain3 3:b082ed02d08c 196
orain3 3:b082ed02d08c 197
orain3 4:cb6587253326 198 int rs485_recv(float temp,float ad1,float ad2,float ad3,float ad4) {
orain3 3:b082ed02d08c 199 char chr;
orain3 4:cb6587253326 200 // char idbuf[8];
orain3 4:cb6587253326 201 char sendbuf[512];
orain3 4:cb6587253326 202 // int i;
orain3 4:cb6587253326 203
orain3 3:b082ed02d08c 204 if (rslen()) {
orain3 3:b082ed02d08c 205
orain3 3:b082ed02d08c 206 chr = rsgetc();
orain3 3:b082ed02d08c 207 buf485[cnt485++] = chr;
orain3 3:b082ed02d08c 208 buf485[cnt485] = '\0';
orain3 3:b082ed02d08c 209 if (chr == '?') cnt485 = 0;
orain3 3:b082ed02d08c 210
orain3 4:cb6587253326 211 if (chr == 0x0a) {
orain3 5:efffadf6d0e0 212 // pc.printf("CR\r\n");
orain3 4:cb6587253326 213
orain3 4:cb6587253326 214 if( ('C' == buf485[0]) ) {
orain3 4:cb6587253326 215 if (kansyutu) data[0] = 1;
orain3 4:cb6587253326 216 else data[0] = 0;
orain3 3:b082ed02d08c 217
orain3 4:cb6587253326 218 // sokutei on/off
orain3 5:efffadf6d0e0 219 if( buf485[2] & 0x05 ) {
orain3 4:cb6587253326 220 sokutei = 1;
orain3 4:cb6587253326 221 data[0] = data[0] | 64 | 128;
orain3 4:cb6587253326 222 } else {
orain3 4:cb6587253326 223 sokutei = 0;
orain3 4:cb6587253326 224 }
orain3 5:efffadf6d0e0 225 if( buf485[2] & 0x0a ) {
orain3 4:cb6587253326 226 wait(0.1);
orain3 5:efffadf6d0e0 227 modem.printf("?00,WIPE,1,\r");
orain3 4:cb6587253326 228 } else {
orain3 4:cb6587253326 229 // answer
orain3 4:cb6587253326 230 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 231 data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9],
orain3 4:cb6587253326 232 data[10],data[11],data[12],data[13],data[14],data[15],data[16],data[17],data[18],data[19],
orain3 4:cb6587253326 233 data[20],data[21],data[22],data[23],data[24],data[25],data[26],data[27],data[28],data[29],
orain3 4:cb6587253326 234 data[30],data[31],data[32],data[33],data[34],data[35],data[36],data[37],data[38],data[39]
orain3 4:cb6587253326 235 );
orain3 4:cb6587253326 236 //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 237
orain3 4:cb6587253326 238 wait(0.1);
orain3 4:cb6587253326 239 modem.printf("%s",sendbuf);
orain3 4:cb6587253326 240 pc.printf("%s",sendbuf);
orain3 4:cb6587253326 241 }
orain3 3:b082ed02d08c 242 }
orain3 4:cb6587253326 243 RxPtr = 0;
orain3 4:cb6587253326 244 RdPtr = 0;
orain3 4:cb6587253326 245 RxBuff[0] = '\0';
orain3 4:cb6587253326 246 buf485[0] = '\0'; // clear
orain3 4:cb6587253326 247 cnt485 = 0;
orain3 3:b082ed02d08c 248 }
orain3 3:b082ed02d08c 249 }
orain3 3:b082ed02d08c 250 return(0);
orain3 3:b082ed02d08c 251 }
orain3 3:b082ed02d08c 252
orain3 3:b082ed02d08c 253