void main_task ( uint_32 initial_data ) { DATE_STRUCT time_rtc; TIME_STRUCT time_mqx; if (_lwevent_create(&lwevent,0) != MQX_OK) { printf("\nMake event failed"); _task_block(); } printf ("\fStart time (MQX synchronized to RTC time during bsp init):\n\n"); /* initialize time */ time_rtc.YEAR = 2010; time_rtc.MONTH = 10; time_rtc.DAY = 15; time_rtc.HOUR = 10; time_rtc.MINUTE = 8; time_rtc.SECOND = 0; time_rtc.MILLISEC = 0; _time_from_date (&time_rtc, &time_mqx); _time_set( &time_mqx); if( _rtc_sync_with_mqx(FALSE) != MQX_OK ) { printf("\nError synchronize time!\n"); _task_block(); } _time_get (&time_mqx); _time_to_date (&time_mqx, &time_rtc); print_mqx_time(&time_rtc, &time_mqx); print_current_time(); /* except MPC5125 */ #ifndef BSP_TWRMPC5125 install_interrupt(); /* enable stopwatch */ install_stopwatch(); /* enable alarm */ install_alarm(); _lwevent_wait_ticks(&lwevent,LWE_ALARM,FALSE,0); _lwevent_clear(&lwevent,LWE_ALARM); printf ("\nALARM!\n"); print_current_time(); /* end of alarm */ printf ("Continue wasting time (2 minutes max) ...\n"); _lwevent_wait_ticks(&lwevent,LWE_STOPWATCH,FALSE,0); _lwevent_clear(&lwevent,LWE_STOPWATCH); printf ("\nSTOPWATCH!\n"); print_current_time(); printf ("\nClearing RTC:\n"); _rtc_init (RTC_INIT_FLAG_CLEAR | RTC_INIT_FLAG_ENABLE); print_current_time(); install_alarm(); _lwevent_wait_ticks(&lwevent,LWE_ALARM,FALSE,0); _lwevent_clear(&lwevent,LWE_ALARM); printf ("ALARM!\n"); print_current_time(); #else /* BSP_TWRMPC5125 */ printf ("Waste 10 seconds here\n"); _time_delay(10000); _rtc_get_time_mqxd (&time_rtc); print_rtc_time(&time_rtc, &time_mqx); #endif printf ("Synchronize RTC to MQX time again:\n"); _rtc_sync_with_mqx (FALSE); _rtc_get_time_mqxd (&time_rtc); _time_from_date (&time_rtc, &time_mqx); print_rtc_time(&time_rtc, &time_mqx); #if PSP_HAS_IRTC == 1 irtc_test(); #endif /* PSP_HAS_IRTC == 1 */ /* Test tamper event functionality on MCF51EMxx device */ #if PSP_MQX_CPU_IS_MCF51EM test_tamper(); #else printf ("Finish, press/hold reset to repeat.\n"); _task_block() ; #endif }
/////////////////////////////READ FRAME //////////////////////////////////////////// int read_frame(int fd, int status){ set_tentativas(3); install_alarm(); unsigned char SET[5],fragment; unsigned int state = 0; int read_all = 0; //ainda nao leu tudo SET[0] = FLAG; SET[1] = A_SET; SET[2] = C_SET; SET[3] = BCC_SET; SET[4] = FLAG; if (status == SENDER) { while(get_tentativas() > 0 && read_all==0){ write(fd,SET,5); if(get_alarm_flag()){ alarm(program.timeout); // activa alarme de 3s set_alarm_flag(0); } ///////STATE MACHINE FOR SENDER(UA) ////////////////////////////////////////////// unsigned int state = 0; while(STOP==FALSE && !get_alarm_flag()){ if(state < 5) read(fd,&fragment,1); switch(state){ case 0: if(fragment==FLAG){ state = 1; } break; case 1: if(fragment==A_SET){ state = 2; break; } if(fragment==FLAG) state = 1; else state = 0; break; case 2: if(fragment==C_SET){ state = 3; break; } if(fragment==FLAG) state = 1; else state = 0; break; case 3: if(fragment==BCC_SET){ state = 4; break; } if(fragment==FLAG) state = 1; else state = 0; break; case 4: if(fragment==FLAG){ state = 5; break;} else state = 0; break; case 5: STOP=TRUE; read_all = 1; alarm(0); break; } } } } else if (status == RECEIVER){ ///////STATE MACHINE FOR RECEIVER(SET)////////////////////////////////////////////// while(STOP==FALSE){ if(state < 5) read(fd,&fragment,1); switch(state){ case 0: if(fragment==FLAG){ state = 1; } break; case 1: if(fragment==A_SET){ state = 2; break;} if(fragment==FLAG) state = 1; else state = 0; break; case 2: if(fragment==C_SET){ state = 3; break;} if(fragment==FLAG) state = 1; else state = 0; break; case 3: if(fragment==BCC_SET){ state = 4; break;} if(fragment==FLAG) state = 1; else state = 0; break; case 4: if(fragment==FLAG){ state = 5; break; } else state = 0; break; case 5: write(fd,SET,5); STOP=TRUE; read_all = 1; break; } } } if(read_all == 0) return -1; }