예제 #1
0
파일: comport.c 프로젝트: rbaldwin7/rhocode
/* Open the specified serial port, save old communication parameters, and   *
 * set initial communication parameters (default parameters are appropriate *
 * for the Barrett Hand).                                                   *
 * Returns: E_OKAY   if successful.                                         *
 *          E_OPEN   if the port could not be opened.                       *
 *          E_INUSE  if the port is already open by someone else.           *
 *          E_SYSTEM if any of several system calls failed.                 */
int ComOpen(ComPort_t *Port , const char *DevName) {
      int                    Stat;

    /* If already open, close the old one first. */
    ComClose(Port);

    Stat = E_OKAY;

    // Allocate timer and set SIGALRM processing for timeout detection. 
  /*  if (   ( (Port->Tmr = timer_create(CLOCK_REALTIME, NULL, 0)) >= 0)
	   && (signal(SIGALRM, AlrmTrap) != SIG_ERR                  ) ) {
      Stat = E_OKAY;
    } else {
      Stat = E_SYSTEM;
    }*/
    //

    /* Open the specified serial port. */
    if ((Stat == E_OKAY) && ( (Port->Fd = open(DevName, O_RDWR | O_NOCTTY )) < 0)) {
       Stat = E_OPEN;
    } 
 
    /* Get device info and check open_count to see if device already in     *
     * use.                                                                 */
    /*    if ((Stat == E_OKAY) && (dev_info(Port->Fd, &DevInfo) < 0))
        Stat = E_SYSTEM;
   
    if ((Stat == E_OKAY) && (DevInfo.open_count != 1))    Stat = E_INUSE;
    */
    /* Save old communication parameters and set new ones. */
    if ((Stat == E_OKAY) && (tcgetattr(Port->Fd, &(Port->OldTios)) < 0)) {
      Stat = E_SYSTEM;
    }

    if (Stat != E_OKAY) {
        /* An error occurred above. */
        if (Port->Fd >= 0) {  /* If device was opened, just close it. */
            close(Port->Fd);
            Port->Fd = -1;
        }
	if (Port->Tmr >= 0) { // If timer was allocated, deallocate it. 
	  //timer_delete(Port->Tmr);
            Port->Tmr = (timer_t) -1;
        }
        signal(SIGALRM, SIG_DFL);
    } else {
        /* No errors so far, so set communication parameters.  If error,    *
         * (try to) restore original parameters and close the device.       */
        Stat = ComInit(Port);
        if (Stat != E_OKAY) ComClose(Port);
    }
    //printf("ComOpen ret: %d, Port->Fd=%d\n", Stat, Port->Fd);
    
    return(Stat);
}
예제 #2
0
/****************************************************************************************
** NAME:           BootInit
** PARAMETER:      none
** RETURN VALUE:   none
** DESCRIPTION:    Initializes the bootloader core.
**
****************************************************************************************/
void BootInit(void)
{
  /* initialize the millisecond timer */
  TimerInit();
  /* initialize the non-volatile memory driver */
  NvmInit();
  /* initialize the communication module */
  ComInit();
  /* initialize the backdoor entry */
  BackDoorInit();
} /*** end of BootInit ***/
예제 #3
0
int main( void ) {
    SysInit();
    LedInit();
    BatteryInit();
    ComInit();
    TimerInit();
    INTEN
    while(1) {
        if(TimerGetTimeFlag() > 10) {
            TimerClearTimeFlag();
            BattertSleep();
        }
        if(TimerGetBatteryFlag() > 0x01) {
            TimerClearBatteryFlag();
            if(BatterGetFlag() == 0) {
                BatterSetFlag(1);
                BattertGet();
                ComWatchSendCmd(dce_powe,BattertGetLevel(),0x00,0x00);
            }
        }
        LedTimeService();
        if(ComWatchGetFlag() == 0x80) {
            ComWatchClearFlag();
            TimerClearTimeFlag();
            BattertOpen();
            BatterSetFlag(0);//可以获取电量
            if(ComWatchGetData(0) != 0x00) {
                BattertOpen();
            }
            LedSetModeFlicker(1);
            if(BattertGetLevel() > 0) {
                switch(ComWatchGetData(0)) {//电量不足
                    case front:
                        switch(ComWatchGetData(1)) {
                            case add_stal:ComRearSendCmd(add_stal,0x00,0x00,0x00);break;
                            case sub_stal:ComRearSendCmd(sub_stal,0x00,0x00,0x00);break;
                            case add_setp:ComRearSendCmd(add_setp,0x00,0x00,0x00);break;
                            case sub_setp:ComRearSendCmd(sub_setp,0x00,0x00,0x00);break;
                            case set_inti:ComRearSendCmd(set_inti,0x00,0x00,0x00);break;
                        }
                    break;
                    case behind:
                        switch(ComWatchGetData(1)) {
                            case exchange_stal: ComBehindSendCmd(exchange_stal,0x00,0x00,0x00);break;
                            case add_setp:ComBehindSendCmd(add_setp,0x00,0x00,0x00);break;
                            case sub_setp:ComBehindSendCmd(sub_setp,0x00,0x00,0x00);break;
                            case set_inti:ComBehindSendCmd(set_inti,0x00,0x00,0x00);break;
                        }
                    break;
                    default:break;
                }
            } else {
                
            }
        }
        if(ComRearGetFlag() == 0x80) {
            ComRearClearFlag();
            switch(ComRearGetData(0)) {
                case dce_gear:
                    ComBehindSendCmd(ask_rear,ComRearGetData(1),0x00,0x00);
                    ComWatchSendCmd(behind,dce_gear,ComRearGetData(1),BattertGetLevel());
                break;
                case stuck:
                    ComWatchSendCmd(stuck,dce_gear,ComRearGetData(1),BattertGetLevel());
                break;
                default:break;
            }
        }
        if(ComBehindGetFlag() == 0x80) {
            ComBehindClearFlag();
            switch(ComBehindGetData(0)) {
                case dce_gear:
                    ComWatchSendCmd(front,dce_gear,ComBehindGetData(1),BattertGetLevel());
                break;
                case stuck:
                    ComWatchSendCmd(stuck,dce_gear,ComBehindGetData(1),BattertGetLevel());
                break;
                default:break;
            }
        }
    }
}
예제 #4
0
int main( void ) {
    SysInit();
    EeepromInit();
    TimerInit();
    ComInit();
    Ds1302Init();
    HtlcdInit();
    BottonInit();
    LedInit();
    MenuInit();
    INTEN
    while(1) {
        u8 botton_bit = 0;
        if(TimerGetSec() > 20) {
            TimerSetSec(0);
            SysSleep();
        }
        if(TimerGetTimeFlag() > 0x01) {
            TimerSetTimeFlag(0);
            MenuRefreshTime();
        }
        MenuFlickerServerTime();
        botton_bit = BottonRead();
        if(botton_bit > 0x01) {
            TimerSetSec(0);
            SysOpen();
        }
        switch(botton_bit) {
            case 0x01:
                MenuSetFeatures(7);
            break;//后减档
            case 0x02:
                MenuSetFeatures(5);
            break;//后加档
            case 0x03:
                MenuSetFeatures(4);
            break;//前换挡
            case 0x11:
                MenuSetFeatures(1);
            break;//区域3
            case 0x12:
                MenuSetFeatures(6);//1
            break;//区域2
            case 0x13:
                MenuSetFeatures(8);
            break;//区域1
            case 0x14:
                MenuSetFeatures(2);
            break;//区域1
            case 0x15:
                MenuSetFeatures(3);
            break;//放开信号
        }
        
        if(ComGetFlag() == 0x80) {
            ComClearFlag();
            LedSet(0);
            switch(ComGetData(0)) {
                case front:
                    switch(ComGetData(1)) {
                        case dce_gear:
                            MenuSetStalls(front,ComGetData(2)+1);
                            MenuSetBattery(ComGetData(3));
                        break;
                    }
                break;
                case behind: 
                    switch(ComGetData(1)) {
                        case dce_gear:
                            MenuSetStalls(behind,ComGetData(2));
                            MenuSetBattery(ComGetData(3));
                        break;
                    }
                break;
                case dce_powe:
                    MenuSetBattery(ComGetData(1));
                break;
            }
        }
    }
}