prog.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //Include Header-----------------------------------------------------
  2. #include"prog.h"
  3. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  4. //Main-Program
  5. int main(int argc, const char * argv[]){
  6. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  7. //Initializations
  8. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  9. //Setting up Timer-Signal--------------------------------------------
  10. sigset_t signal;
  11. pthread_t thread;
  12. int signal_err, pthread_err;
  13. signal_err = sigemptyset(&signal);
  14. if(signal_err!=0){
  15. error_handler(signal_err, "Not a valid Signal");
  16. }
  17. signal_err = sigaddset(&signal, SIGALRM);
  18. if(signal_err!=0){
  19. error_handler(signal_err, "Not a valid Signal");
  20. }
  21. pthread_err = pthread_sigmask(SIG_BLOCK, &signal, NULL);
  22. if(pthread_err!=0){
  23. error_handler(pthread_err, "Error changing Signal-Mask");
  24. }
  25. //initializing USART at USB_PORT-------------------------------------
  26. int USB = open("/dev/ttyACM3", O_RDWR|O_NOCTTY|O_NONBLOCK);
  27. int USB_err = isatty(USB);
  28. if(USB_err!=1){
  29. error_handler(USB_err, "No File Descriptor");
  30. }
  31. struct termios * termios_ptr = (struct termios *)
  32. malloc(sizeof(struct termios));
  33. (*termios_ptr).c_iflag = IGNPAR; //ignore Parity-Err
  34. (*termios_ptr).c_oflag = 0;
  35. (*termios_ptr).c_cflag = CS8|CREAD; //set Size, enable RX
  36. (*termios_ptr).c_lflag = 0;
  37. (*termios_ptr).c_cc[VMIN] = 0; //set min Characters
  38. cfsetspeed(termios_ptr, B19200); //set Baudrate 19.2k
  39. tcsetattr(USB, TCSANOW ,termios_ptr);
  40. //initializing Stream for Source-Code--------------------------------
  41. FILE * file_stream = fopen("test.hex", "r");
  42. if(file_stream==NULL){
  43. fprintf(stderr, "No File \n");
  44. }
  45. int page_cnt = 0;
  46. int skip;
  47. int page_end = page_size;
  48. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  49. //Programming cycle
  50. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  51. //Loading and Masking Page-------------------------------------------
  52. while(page_end==page_size){
  53. skip=1;
  54. page_end = page_get(file_stream, page_cnt);
  55. printf("Page %d: \n", page_cnt);
  56. for(int i=0; i<page_end; i++, skip++){
  57. printf("%02X", page_buf[i]);
  58. if(skip==16){
  59. printf("\n");
  60. skip=0;
  61. }
  62. }
  63. printf("\n");
  64. if(skip!=1){
  65. printf("\n");
  66. }
  67. page_cnt++;
  68. printf("Page %d Masked: \n", page_cnt-1);
  69. unsigned char * page_ptr = page_mask(&page_buf[0], page_end);
  70. skip=1;
  71. for(int i=0; i<2*page_end; i++, skip++, page_ptr++){
  72. printf("%02X", *page_ptr);
  73. if(skip==16){
  74. printf("\n");
  75. skip=0;
  76. }
  77. }
  78. printf("\n");
  79. if(skip!=1){
  80. printf("\n");
  81. }
  82. page_ptr = page_ptr-2*page_end;
  83. printf("Page Ready \n");
  84. //Opening Connection to Programmer-----------------------------------
  85. char * cmd_ack_ptr = (char *)malloc(sizeof(char));
  86. char * frame_ptr = cmd_build(page_read, page_end);
  87. write(USB, frame_ptr, 3*sizeof(char));
  88. while(true){
  89. read(USB, cmd_ack_ptr, sizeof(char));
  90. if(*cmd_ack_ptr==page_end){
  91. break;
  92. }
  93. }
  94. tcflush(USB, TCIOFLUSH);
  95. printf("Acknowledged Page Send \n");
  96. set_timer(0, 200);
  97. pthread_err = pthread_create(&thread, NULL,
  98. &signal_thread, (void *)&signal);
  99. if(pthread_err!=0){
  100. error_handler(pthread_err, "Failed to create Thread");
  101. }
  102. while(timer_state);
  103. //Sending Page-------------------------------------------------------
  104. write(USB, page_ptr, 2*page_end*sizeof(char));
  105. char * page_rec_ptr = (char *)malloc(sizeof(char));
  106. while(true){
  107. read(USB, page_rec_ptr, sizeof(char));
  108. if(*page_rec_ptr==-112){ //Framemask
  109. break;
  110. }
  111. }
  112. printf("Page Send Complete \n");
  113. //Receiving Checksum-------------------------------------------------
  114. for(int i=0; i<32; i++){
  115. printf("-");
  116. }
  117. printf("\n");
  118. }
  119. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  120. //Deintialization and Error Handling
  121. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  122. close(USB);
  123. return EXIT_SUCCESS;
  124. }