| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- //Include Header-----------------------------------------------------
- #include"prog.h"
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- //Main-Program
- int main(int argc, const char * argv[]){
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- //Initializations
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- //Setting up Timer-Signal--------------------------------------------
- sigset_t signal;
- pthread_t thread;
- int signal_err, pthread_err;
- signal_err = sigemptyset(&signal);
- if(signal_err!=0){
- error_handler(signal_err, "Not a valid Signal");
- }
- signal_err = sigaddset(&signal, SIGALRM);
- if(signal_err!=0){
- error_handler(signal_err, "Not a valid Signal");
- }
- pthread_err = pthread_sigmask(SIG_BLOCK, &signal, NULL);
- if(pthread_err!=0){
- error_handler(pthread_err, "Error changing Signal-Mask");
- }
- //initializing USART at USB_PORT-------------------------------------
- int USB = open("/dev/ttyACM3", O_RDWR|O_NOCTTY|O_NONBLOCK);
- int USB_err = isatty(USB);
- if(USB_err!=1){
- error_handler(USB_err, "No File Descriptor");
- }
- struct termios * termios_ptr = (struct termios *)
- malloc(sizeof(struct termios));
- (*termios_ptr).c_iflag = IGNPAR; //ignore Parity-Err
- (*termios_ptr).c_oflag = 0;
- (*termios_ptr).c_cflag = CS8|CREAD; //set Size, enable RX
- (*termios_ptr).c_lflag = 0;
- (*termios_ptr).c_cc[VMIN] = 0; //set min Characters
- cfsetspeed(termios_ptr, B19200); //set Baudrate 19.2k
- tcsetattr(USB, TCSANOW ,termios_ptr);
- //initializing Stream for Source-Code--------------------------------
- FILE * file_stream = fopen("test.hex", "r");
- if(file_stream==NULL){
- fprintf(stderr, "No File \n");
- }
- int page_cnt = 0;
- int skip;
- int page_end = page_size;
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- //Programming cycle
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- //Loading and Masking Page-------------------------------------------
- while(page_end==page_size){
- skip=1;
- page_end = page_get(file_stream, page_cnt);
- printf("Page %d: \n", page_cnt);
- for(int i=0; i<page_end; i++, skip++){
- printf("%02X", page_buf[i]);
- if(skip==16){
- printf("\n");
- skip=0;
- }
- }
- printf("\n");
- if(skip!=1){
- printf("\n");
- }
- page_cnt++;
- printf("Page %d Masked: \n", page_cnt-1);
- unsigned char * page_ptr = page_mask(&page_buf[0], page_end);
- skip=1;
- for(int i=0; i<2*page_end; i++, skip++, page_ptr++){
- printf("%02X", *page_ptr);
- if(skip==16){
- printf("\n");
- skip=0;
- }
- }
- printf("\n");
- if(skip!=1){
- printf("\n");
- }
- page_ptr = page_ptr-2*page_end;
- printf("Page Ready \n");
- //Opening Connection to Programmer-----------------------------------
- char * cmd_ack_ptr = (char *)malloc(sizeof(char));
- char * frame_ptr = cmd_build(page_read, page_end);
- write(USB, frame_ptr, 3*sizeof(char));
- while(true){
- read(USB, cmd_ack_ptr, sizeof(char));
- if(*cmd_ack_ptr==page_end){
- break;
- }
- }
- tcflush(USB, TCIOFLUSH);
- printf("Acknowledged Page Send \n");
- set_timer(0, 200);
- pthread_err = pthread_create(&thread, NULL,
- &signal_thread, (void *)&signal);
- if(pthread_err!=0){
- error_handler(pthread_err, "Failed to create Thread");
- }
- while(timer_state);
- //Sending Page-------------------------------------------------------
- write(USB, page_ptr, 2*page_end*sizeof(char));
- char * page_rec_ptr = (char *)malloc(sizeof(char));
- while(true){
- read(USB, page_rec_ptr, sizeof(char));
- if(*page_rec_ptr==-112){ //Framemask
- break;
- }
- }
- printf("Page Send Complete \n");
- //Receiving Checksum-------------------------------------------------
- for(int i=0; i<32; i++){
- printf("-");
- }
- printf("\n");
- }
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- //Deintialization and Error Handling
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- close(USB);
- return EXIT_SUCCESS;
- }
|