LNLS Driver Heaters DCM Firmware with two sockets: - HTTP server at port 80 - TCP socket at port 5757 for EPICS
Dependencies: mbed mbed-rtos AMC7812B EthernetInterface TextLCD
Firmware for DCM heaters driver - 8 channel, 12V/1.5A, PWM controlled with interlock and failure diagnostics
Mauricio Martins Donatti
Brazilian Synchrotron Light Laboratory
Electronics Support Group - GAE
mauricio.donatti@lnls.br
Extra scripts on Github page:
1. Driver_DCM_Socket.py: Open a socket and comunicate with the Driver.
2. Find_IP.py: Search a subdomain for devices (IPs) with a TCP Server Socket on the specified port.
3. firmware.txt: device firmware link (MBED).
4. index.html: HTTP server GET answer - html + JS.
Diff: main.cpp
- Revision:
- 3:820e3a42c06b
- Parent:
- 2:7a49ed074968
--- a/main.cpp Mon Apr 26 11:22:48 2021 +0000 +++ b/main.cpp Fri Aug 06 13:07:37 2021 +0000 @@ -19,24 +19,29 @@ *******************************************************************/ #define SUPPLY_24V +//#define CAL #include "mbed.h" #include "TextLCD.h" #include "EthernetInterface.h" #include "amc7812b.h" + +#define ERROR_INTERFACE +//#define DEBUG_INTERFACE + #include "definitions.h" #include "HTTP_SERVER.h" -#define VERSION "v3.0-2018-08\0" //Firmware Version +#define VERSION "v4.1-2021-07\0" //Firmware Version #define DEVICE_NAME "DH-8CH-001\0" //Device Name - Ethernet default hostname #define PORT 6767 //Socket Port (EPICS data) - - //SPI constructor SPI spi(MOSI,MISO,SCLK); //mosi, miso, sclk +Serial pc(USBTX, USBRX, 115200); + //Digital Output constructors DigitalOut cs_u4(CS_U4); DigitalOut cs_u5(CS_U5); @@ -87,7 +92,8 @@ uint16_t data; }rx,tx; -FILE *cfg_file; //Config file variable +FILE *cfg_file; //Config file variable +FILE *html_file; //Html file variable int tmp,i; //auxiliary integers int LCD_index=0; //LCD index (displayed channel changes from 1 to 8) @@ -168,10 +174,33 @@ volatile int save_cfg_time; +char html_page[HTML_MAX_SIZE]; +int html_page_size; + //main function int main() { //heartbeat.attach(&heart,HEARTBEAT_DELAY); //attach heartbeat ticker + t.start(); + // Reading webserver file + wait_ms(10); //Wait powerup + + html_file = fopen("/local/index.htm","r"); // Open "index.html" on the local file system for reading + if(html_file==NULL){ //If file doesnt exist + html_page_size = sprintf(html_page,"ERROR: File Not Found"); + ERROR_PRINTF("index.htm file not found at local file system!"); + fclose(html_file); //close html file + } + else + { //File exists + fseek(html_file, 0, SEEK_SET); + html_page_size = 0; + while (!feof(html_file)) + html_page[html_page_size++]= getc(html_file); + if(feof(html_file)) + html_page_size--; + fclose(html_file); //close html file + } amc_setup(); //Setup AMC7812B setup(); //Initialize LCD and variables @@ -190,6 +219,7 @@ else { clear_lcd(); display.printf("HW ERROR: eth conn"); //HW ethernet error - probably PHY broken + ERROR_PRINTF("Ethernet Connection Hardware Error!"); //Turn LED 1 Red and Green - ETH HW Error display.putc(0xFE); display.putc(0x5A); @@ -225,7 +255,7 @@ eth_led1 = 1; //turn on led1 if(server_init == 0) //initiate http server the first time { - httpsvr.init(); + httpsvr.init(html_page,html_page_size); server_init = 1; //turn flag on - Server initiated server_thread.start(server_loop); //run thread - monitoring port 80 } @@ -417,7 +447,7 @@ ch_msg_idx--; //decrement to index 0 to 7 if(socket_buffer[1]=='r') //read message { - n = sprintf(send_buffer,"OK>%d:\tV=%4.2f\tI=%3.2f\tC=%d\tIL=%3.2f\tF=%d\tOL=%d\tNL=%d\tE=%d\r\n",ch_msg_idx+1,\ + n = sprintf(send_buffer,"OK>%d:\tV=%4.2f\tI=%3.2f\tC=%.1f\tIL=%3.2f\tF=%d\tOL=%d\tNL=%d\tE=%d\r\n",ch_msg_idx+1,\ CH[ch_msg_idx].voltage,CH[ch_msg_idx].current,\ CH[ch_msg_idx].control,CH[ch_msg_idx].limit,\ CH[ch_msg_idx].failure,CH[ch_msg_idx].overload>ERROR_REP,\ @@ -616,14 +646,17 @@ fast_read_U4(); //Estimate control input (Duty cycle in %) - CH[i].control = FILTC*CH[i].control+FILTC2*(((rx.data*5.0/0xFFF))-VC_OFF)/VC_K; + CH[i].control = FILTC*CH[i].control+FILTC2*(rx.data*VC_K*5.0/0xFFF+VC_OFF); - //Verify limits - if(CH[i].control<0) - CH[i].control=0; - if(CH[i].control>100) - CH[i].control=100; + #ifndef CAL + //Verify limits + if(CH[i].control<0) + CH[i].control=0; + if(CH[i].control>100) + CH[i].control=100; + #endif } + DEBUG_PRINTF("Duty Cycle Calibration - Measured Voltage: %f",rx.data*5.0/0xFFF); } } } @@ -643,7 +676,7 @@ { while(1) { - httpsvr.run(&CH[0]); //wait for new client connection + httpsvr.run(&CH[0],dev_name); //wait for new client connection server_led = !server_led; //togle server led } } @@ -688,7 +721,7 @@ //Reset AMC7812B to make sure both are in the initial state amc_reset = 0; - wait_ms(1); + wait_ms(5); amc_reset = 1; // Setup the spi for 12 bit data, high steady state clock, @@ -696,7 +729,7 @@ spi.format(12,1); spi.frequency(10000000); - wait_ms(5); //Wait AMC7812B power up + wait_ms(10); //Wait AMC7812B power up //Disabling Power Down Register tx.data = 0x7FFE; @@ -955,6 +988,7 @@ fprintf(cfg_file,"%d %3.2f 1\n",i+1,DEFAULT_CURR_LIMIT); //Write to file CH[i].enable = 1; } + ERROR_PRINTF("lim.cfg file not found! Creating default file..."); } else //File exists :) { @@ -1039,7 +1073,7 @@ normal(); //Normal mode } clear_lcd(); //Clear LCD - display.printf("CH%d %5.2fV/%3.2fAC:%03d%% IL:%3.2fA",LCD_index+1,CH[LCD_index].voltage,CH[LCD_index].current,CH[LCD_index].control,CH[LCD_index].limit); + display.printf("CH%d %5.2fV/%3.2fAC:%03.0f%% IL:%3.2fA",LCD_index+1,CH[LCD_index].voltage,CH[LCD_index].current,CH[LCD_index].control,CH[LCD_index].limit); } //Heartbeat toogle MBED led function