void uartPuts(char *str) { char * p; for(p=str; *p!=0; p++) { uartPutChar(*p); } uartPutChar('\n'); uartPutChar('\r'); }
// gives a response of the form CK_UP 5:OK/18 static inline void respond( char* p_command, u16 p_page, char* p_response ) { uartPutString( p_command ); uartPutChar( ' ' ); itoa( p_page, s_tmpBuffer, 10 ); uartPutString( s_tmpBuffer ); uartPutChar( ':' ); uartPutString( p_response ); uartPutChar( '/' ); u16 bufferRemaining = uartReceiveBufferRemaining(); itoa( bufferRemaining, s_tmpBuffer, 10 ); uartPutStringCRLF( s_tmpBuffer ); }
// // send a character through the USART // void uartPutChar(char c) { if(c == '\r') // if character is 'caraige return', then uartPutChar('\n'); // send an additional 'new line' loop_until_bit_is_set(myUCSRA, myUDRE); // wait until transmit buffer is empty myUDR = c; // write character to transmit buffer }
int main(void) { /* stop the watchdog timer */ WDTCTL = WDTPW + WDTHOLD; /* set up the clocks for 1 mhz */ BCSCTL1 = CALBC1_1MHZ; // Set range DCOCTL = CALDCO_1MHZ; BCSCTL2 &= ~(DIVS_3); // SMCLK = DCO / 8 = 1Mhz /* LEDs off, but we can use them for debugging if we want */ P1DIR |= RED_LED+GRN_LED; P1OUT &= ~ (RED_LED + GRN_LED ); initUart(); /* Start listening for data */ UART_Start(); /* enable interrupts */ __bis_SR_register( GIE ); uartPrint("\n\rCli Started.\n\r"); cliHelp(); uartPrint(PROMPT); char in_char; while(1) { while(rx_size() > 0) { in_char = uartGetChar(); uartPutChar(in_char); cli_input(in_char); } /* go to sleep and wait for data */ __bis_SR_register( LPM0_bits ); } }
/**************************************************************************//** * @brief TIMER0_setup * Configures the TIMER *****************************************************************************/ void TIMER_setup(void) { /* Enable necessary clocks */ CMU_ClockEnable(cmuClock_TIMER0, true); CMU_ClockEnable(cmuClock_PRS, true); /* Select CC channel parameters */ TIMER_InitCC_TypeDef timerCCInit = { .eventCtrl = timerEventEveryEdge, /* Input capture event control */ .edge = timerEdgeBoth, /* Input capture on falling edge */ .prsSel = timerPRSSELCh5, /* Prs channel select channel 5*/ .cufoa = timerOutputActionNone, /* No action on counter underflow */ .cofoa = timerOutputActionNone, /* No action on counter overflow */ .cmoa = timerOutputActionNone, /* No action on counter match */ .mode = timerCCModeCapture, /* CC channel mode capture */ .filter = false, /* No filter */ .prsInput = true, /* CC channel PRS input */ .coist = false, /* Comparator output initial state */ .outInvert = false, /* No output invert */ }; /* Initialize TIMER0 CC0 channel */ TIMER_InitCC(HIJACK_RX_TIMER, 0, &timerCCInit); /* Select timer parameters */ const TIMER_Init_TypeDef timerInit = { .enable = false, /* Do not start counting when init complete */ .debugRun = false, /* Counter not running on debug halt */ .prescale = HIJACK_TIMER_RESOLUTION, /* Prescaler of 1 */ .clkSel = timerClkSelHFPerClk, /* TIMER0 clocked by the HFPERCLK */ .fallAction = timerInputActionReloadStart, /* Stop counter on falling edge */ .riseAction = timerInputActionReloadStart, /* Reload and start on rising edge */ .mode = timerModeUp, /* Counting up */ .dmaClrAct = false, /* No DMA */ .quadModeX4 = false, /* No quad decoding */ .oneShot = false, /* Counting up constinuously */ .sync = false, /* No start/stop/reload by other timers */ }; /* Initialize TIMER0 */ TIMER_Init(HIJACK_RX_TIMER, &timerInit); /* PRS setup */ /* Select ACMP as source and ACMP0OUT (ACMP0 OUTPUT) as signal */ PRS_SourceSignalSet(5, PRS_CH_CTRL_SOURCESEL_ACMP0, PRS_CH_CTRL_SIGSEL_ACMP0OUT, prsEdgeOff); /* Enable CC0 interrupt */ TIMER_IntEnable(HIJACK_RX_TIMER, TIMER_IF_CC0); /* Enable TIMER0 interrupt vector in NVIC */ NVIC_EnableIRQ(TIMER0_IRQn); } /**************************************************************************//** * @brief ACMP_setup * Configures and starts the ACMP *****************************************************************************/ static void ACMP_setup(void) { /* Enable necessary clocks */ CMU_ClockEnable(HIJACK_RX_ACMPCLK, true); CMU_ClockEnable(cmuClock_GPIO, true); /* Configure ACMP input pin. */ GPIO_PinModeSet(HIJACK_RX_GPIO_PORT, HIJACK_RX_GPIO_PIN, gpioModeInput, 0); /* Analog comparator parameters */ const ACMP_Init_TypeDef acmpInit = { .fullBias = false, /* No full bias current*/ .halfBias = true, /* No half bias current */ .biasProg = 2, /* Biasprog current 1.4 uA */ .interruptOnFallingEdge = false, /* Disable interrupt for falling edge */ .interruptOnRisingEdge = false, /* Disable interrupt for rising edge */ .warmTime = acmpWarmTime256, /* Warm-up time in clock cycles, should be >140 cycles for >10us warm-up @ 14MHz */ .hysteresisLevel = acmpHysteresisLevel7, /* Hysteresis level 0 - no hysteresis */ .inactiveValue = 1, /* Inactive comparator output value */ .lowPowerReferenceEnabled = false, /* Low power reference mode disabled */ .vddLevel = HIJACK_RX_ACMP_LEVEL, /* Vdd reference scaling of 32 */ }; /* Use ACMP0 output, PD6 . */ //GPIO_PinModeSet(gpioPortD, 6, gpioModePushPull, 0); //ACMP_GPIOSetup(ACMP0, 2, true, false); /* Init ACMP and set ACMP channel 4 as positive input and scaled Vdd as negative input */ ACMP_Init(HIJACK_RX_ACMP, &acmpInit); ACMP_ChannelSet(HIJACK_RX_ACMP, HIJACK_RX_ACMP_NEG, HIJACK_RX_ACMP_CH); ACMP_Enable(HIJACK_RX_ACMP); } /** * @brief calculate whether cnt is in 500us region * ticker = 64Mhz/128 = 2us * 475us < cnt < 510us * */ static chk_result_t IsTime2Detect(uint32_t inv) { chk_result_t ret; if( inv < HIJACK_DEC_NUM_TICKS_MIN){ offset = inv; ret = pass; } else if ( ( inv <= HIJACK_DEC_NUM_TICKS_MAX ) && ( inv >= HIJACK_DEC_NUM_TICKS_MIN ) ) { offset = 0; inv = 0; ret = suit; } else{ offset = 0; inv = 0; ret = error; } return ret; } /* * Find phase remain or phase reversal. */ static void dec_parser(uint8_t bit_msk, state_t state) { if ( ( suit == IsTime2Detect(inv) ) ){ //it's time to determine if( falling == cur_edge ){ dec.data &= ~(1 << bit_msk); #if DEC_DEBUG == 1 uartPutChar( '+' ) ; uartPutChar( '_' ) ; #endif//DEC_DEBUG == 1 } else{ dec.data |= (1 << bit_msk); #if DEC_DEBUG == 1 uartPutChar( '_' ) ; uartPutChar( '+' ) ; #endif//DEC_DEBUG == 1 dec.odd++; } dec.state = state; //state switch } else if ( error == IsTime2Detect(inv) ){ //wait for edge detection time dec.state = Waiting; //state switch } } /**************************************************************************//** * @brief decode state machine * Invoke in TIMER_ISR for decoding. *****************************************************************************/ void decode_machine(void) { inv = offset + cur_stamp; //update offset #if 0 if( dec.state > Waiting ){ USART_printHexBy16u(inv); if(cur_edge == rising){ uartPutChar( '\\' ) ; } else{ uartPutChar( '/' ) ; } } #endif switch (dec.state){ case Waiting: /* go to start bit if rising edge exist. */ if (rising == cur_edge) { dec.state = Sta0; offset = 0; inv = 0; } break; // case Sta0: if( ( suit == IsTime2Detect(inv) ) && ( falling == cur_edge ) ){ dec.data = 0; //clear data field for store new potential data dec.odd = 0; //clear odd field parity counter dec.state = Bit0; #if DEC_DEBUG == 1 uartPutChar( 'S' ) ; uartPutChar( '+' ) ; uartPutChar( '_' ) ; #endif } else{ dec.state = Waiting; } break; // case Bit0: #if DEC_DEBUG == 1 uartPutChar( '0' ) ; #endif dec_parser(BIT0, Bit1); break; // case Bit1: #if DEC_DEBUG == 1 uartPutChar( '1' ) ; #endif dec_parser(BIT1, Bit2); break; // case Bit2: #if DEC_DEBUG == 1 uartPutChar( '2' ) ; #endif dec_parser(BIT2, Bit3); break; // case Bit3: #if DEC_DEBUG == 1 uartPutChar( '3' ) ; #endif dec_parser(BIT3, Bit4); break; // case Bit4: #if DEC_DEBUG == 1 uartPutChar( '4' ) ; #endif dec_parser(BIT4, Bit5); break; // case Bit5: #if DEC_DEBUG == 1 uartPutChar( '5' ) ; #endif dec_parser(BIT5, Bit6); break; // case Bit6: #if DEC_DEBUG == 1 uartPutChar( '6' ) ; #endif dec_parser(BIT6, Bit7); break; // case Bit7: #if DEC_DEBUG == 1 uartPutChar( '7' ) ; #endif dec_parser(BIT7, Parity); break; // case Parity: if ( ( suit == IsTime2Detect(inv) ) ){ //it's time to determine if( rising == cur_edge ){ dec.odd++; #if DEC_DEBUG == 1 uartPutChar( '_' ) ; uartPutChar( '+' ) ; #endif } else{ #if DEC_DEBUG == 1 uartPutChar( '+' ) ; uartPutChar( '_' ) ; #endif } #if DEC_DEBUG == 1 uartPutChar( dec.odd + 0x30) ; #endif if( 1 == (dec.odd%2)){ //parity pass dec.state = Sto0; } else{ //parity failed dec.state = Waiting; } } else if ( error == IsTime2Detect(inv) ){ //wait for edge detection time dec.state = Waiting; } break; // case Sto0: if ( ( suit == IsTime2Detect(inv) ) ){ //it's time to determine if( rising == cur_edge ){ //stop bit is rising edge USART_txByte(dec.data); #if DEC_DEBUG == 1 uartPutChar( '_' ) ; uartPutChar( '+' ) ; #endif HIJACKPutData(&dec.data, &decBuf, sizeof(uint8_t)); } else{ #if DEC_DEBUG == 1 uartPutChar( '+' ) ; uartPutChar( '_' ) ; #endif } dec.state = Waiting; #if DEC_DEBUG == 1 uartPutChar( '\r' ) ; uartPutChar( '\n' ) ; #endif } else if ( error == IsTime2Detect(inv) ){ //wait for edge detection time dec.state = Waiting; } break; // default: break; // } }
void uartPutNewline(void) { uartPutChar('\r'); uartPutChar('\n'); }
// // parse the current s-record in the buffer // uint8_t parseSrecBuffer(char *thisBuffer) { uint8_t srecBytecount, srecChecksum, srecType; uint32_t srecAddress; uint8_t tmpAddress; char hi, lo; char c1, c2; uint16_t i; // check if current buffer is a data record (starts with S1, S2 or S3) if(*thisBuffer++ == 'S' ) { // get s-record type srecType = hexCharToInt(*thisBuffer++); // only process the record in case it's a data record if((srecType == 1) || (srecType == 2) || (srecType == 3)) { // get the byte count hi = *thisBuffer++; lo = *thisBuffer++; srecBytecount = hexByteToInt(hi, lo); // one could directly put *thisBuffer++ into the arguments, // but the arguments are put on stack last first, i.e. the // lo character is fetched from the *thisBuffer first and // this changes lo and hi character! Using seperate variables // hi and lo is more clear and readable than changing the // sequence in the hexByteToInt function. // check if byte count is larger than 0x43, i.e. we have more // than 64 bytes in the record -> not allowed if(srecBytecount > (0x43 + srecType - 1)) { uartPutChar('B'); // 'B' indicates this error return FALSE; } srecChecksum = srecBytecount; // add byte count to checksum srecAddress = 0; // reset s-record address // extract the address depending of s-record type for(i = 0; i <= srecType; ++i) { hi = *thisBuffer++; lo = *thisBuffer++; tmpAddress = hexByteToInt(hi, lo); // get next address byte srecAddress <<= 8; // shift existing address one byte left srecAddress += tmpAddress; // add new lower address byte srecChecksum += tmpAddress; // add address portion to checksum } // read all data bytes for(i = 0; i < (srecBytecount - 3 - (srecType - 1)); i += 2) { // assemble a 16 bit little endian data word and calculate checksum hi = *thisBuffer++; lo = *thisBuffer++; c1 = hexByteToInt(hi, lo); srecChecksum += c1; hi = *thisBuffer++; lo = *thisBuffer++; c2 = hexByteToInt(hi, lo); srecChecksum += c2; #ifdef EEPROM_CODE if(programThisMemory == FLASH) { #endif // write word to flash write buffer boot_page_fill(srecAddress + i, (c2 << 8) + c1); #ifdef EEPROM_CODE } else { // write hi and lo byte into eeprom buffer eepromWriteBuffer[srecAddress + i] = c1; eepromWriteBuffer[srecAddress + i + 1] = c2; } #endif // This counter decrements from SPM_PAGESIZE to two, // when two is reached, the flash page may be written. // If it goes below two, the bytecount of an s-record // was not a integer factor of the flash page size. // See description on top of this file. if((flashPageSizeCounter -= 2) == 0) { uartPutChar('P'); // 'P' indicates this error return FALSE; } } // get checksum and compare to 0xff hi = *thisBuffer++; lo = *thisBuffer++; srecChecksum += hexByteToInt(hi, lo); // compare checksum to 0xff if(srecChecksum != 0xff) { uartPutChar('C'); // if checksum is wrong, 'C' indicates this error return FALSE; } uartPutChar('.'); // '.' indicates some progress } // check if end of file record if((srecType == 9) || (srecType == 8) || (srecType == 7)) { srecEndOfFile = TRUE; } } // check if either page size counter is two (i.e. buffer is full) // or end of file was reached (i.e. the previously received // bytes must be written to flash) if((flashPageSizeCounter == 2) || (srecEndOfFile == TRUE)){ #ifdef EEPROM_CODE if(programThisMemory == FLASH) { #endif boot_page_erase(writeBaseAddress); // do a page erase boot_spm_busy_wait(); // wait for page erase done boot_page_write(writeBaseAddress); // do a page write boot_spm_busy_wait(); // wait for write completed boot_rww_enable(); // reenable rww section again #ifdef EEPROM_CODE } if(programThisMemory == EEPROM) { eeprom_busy_wait(); // wait for eeprom no longer busy eeprom_write_block(eepromWriteBuffer, (void *)(uint16_t)writeBaseAddress, SPM_PAGESIZE); // write the block to eeprom } #endif flashPageSizeCounter = SPM_PAGESIZE + 2; // set byte counter to correct value (see declaration at beginning) writeBaseAddress += SPM_PAGESIZE; } return TRUE; }
// // main function // int main(void) { char c; uint16_t loop = 0; uint8_t bootloaderEnableFlag = FALSE; // relocate interrupt vector table to bottom of flash // in case the bootloader will not be started myIVSELREG = _BV(IVCE); myIVSELREG = 0; // init USART myUBRRH = (F_CPU/(BAUDRATE*16L)-1) >> 8; // calculate baudrate and set high byte myUBRRL = (uint8_t)(F_CPU/(BAUDRATE*16L)-1); // and low byte myUCSRB = _BV(myTXEN) | _BV(myRXEN) | _BV(myRXCIE); // enable transmitter and receiver and receiver interrupt myUCSRC = myURSEL | _BV(myUCSZ1) | _BV(myUCSZ0); // 8 bit character size, 1 stop bit, no parity // the bootloader may be activated either if // the character 'i' (interactive mode) was received from USART // or the flash is (still) empty // poll USART receive complete flag 64k times to catch the 'i' reliably do { if(bit_is_set(myUCSRA, myRXC)) if(myUDR == 'i') bootloaderEnableFlag = TRUE; } while(--loop); // test if flash is empty (i.e. flash content is 0xff) if(pgm_read_byte_near(0x0000) == 0xFF) { bootloaderEnableFlag = TRUE; // set enable flag } // check enable flag and start application if FALSE if(bootloaderEnableFlag == FALSE) { myUCSRB = 0; // clear USART register to reset default startApplication(); // start application code } // // now the bootloader code begins // // welcome message and prompt uartPutChar('\r'); uartPutChar('>'); // loop until a valid character is received do { c = uartGetChar(); // read a character if(c == 'f') { // 'f' selects flash programming uartPutChar('f'); // echo the 'f' programThisMemory = FLASH; // set flag } #ifdef EEPROM_CODE if(c == 'e') { // 'e' selects eeprom programming uartPutChar('e'); // echo the 'e' programThisMemory = EEPROM; // set flag } #endif if(c == 'g') { // 'g' starts the application uartPutChar('g'); // echo the 'g' startApplication(); // and jump to 0x0000 } } while(!programThisMemory); // exit loop when a valid key was pressed // move interrupt vector table to boot loader area myIVSELREG = _BV(IVCE); myIVSELREG = _BV(IVSEL); uartPutChar('\r'); // set cursor to next line receiveBufferFull = FALSE; // reset full flag receiveBufferPointer = (char *)receiveBuffer; // reset buffer pointer to start of buffer // enable interrupts sei(); // endless loop while(1) { // if buffer is full, parse the buffer and write to flash if(receiveBufferFull) { cli(); // disable interrupts // if parsing produced an error, restart bootloader if(!parseSrecBuffer(receiveBuffer)) { uartPutChar('\r'); // set cursor to next line startBootloader(); // restart the bootloader } // was an end-of-file s-record found? if(srecEndOfFile) { uartPutChar('O'); // 'OK' indicates successful programming uartPutChar('K'); loop_until_bit_is_set(myUCSRA, myUDRE); // wait until character is transmitted startBootloader(); // restart the bootloader } receiveBufferFull = FALSE; // reset full flag receiveBufferPointer = (char *)receiveBuffer; // reset buffer pointer to start of buffer sei(); // enable interrupts } } }
/** * @fn int main( void ); * @brief Point d'entrée du programme * @return */ int main(void){ char ordre[50]; char cons[10]; int ref=18; int pwm=0; int fil=0; char BUF_RX[50]; char BUF_TX[1000]; int aux1; char *data_IL, *data_VO, *data_VI; //Optimisation du fonctionnement du CPU SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); init(); intConfig(); uartPutString(" ****** * * ******* ****** *** ****** ** ****** *** ***\r\n"); uartPutString("* **** * * *** *** * *** * * * * **** * * * **** * * * *\r\n"); uartPutString("* * * * * * * *** * * * * * * * ** * * * * * * * *\r\n"); uartPutString("* * * * * * * ** * * *** * * * * **** * * *** * ** ** *\r\n"); uartPutString("* * * * * * * * * * * * * * * * * * * * * * * * * *\r\n"); uartPutString("* **** * * * * * * * * * * * **** * * * * * **** * * * *\r\n"); uartPutString(" ****** * * * * * * * * *** ****** * * * * ****** * * * *\r\n"); uartPutString("citroen corp. with Ixchel Intelligent Systems patnership\r\n"); strcpy(BUF_TX,"boost converter module interface. Enter help to know the supported commands\r\nboost:~# \0"); uartPutString(BUF_TX); while (1) {; if (flagTraitement) { flagTraitement = 0; // Acquisition ads7885Pic32Read( CHN_SPI, data_IL, data_VO, data_VI ); mesure[0][pMesure]=atoi(data_IL); mesure[1][pMesure]=atoi(data_VO); mesure[2][pMesure]=atoi(data_VI); pMesure++; if (pMesure >= TAILLE_MESURE) pMesure = 0; gpioLed(); // Calcul commande[pCommande] = fTransfert(consigne, mesure[0], commande); // // // Commande // pwmSet(commande[pCommande]); // pCommande++; // if (pCommande >= TAILLE_COMMANDE) // pCommande = 0;*/ if (flagAuto == 0){ pwmSet(pwm); } } if (flagReception) { uartPutChar(aux); if(aux=='\b'){ if(i>0) i--; } else if(aux!='\r'){ BUF_RX[i]=aux; i++; } else { BUF_RX[i]='\0'; uartPutChar('\r'); uartPutChar('\n'); i=0; // On reçoit l'ordre while(BUF_RX[i]!=' '){ ordre[i]=BUF_RX[i]; i++; } ordre[i]='\0'; i++; int j=0; while(BUF_RX[i]!='\0'){ cons[j]=BUF_RX[i]; i++; j++; } cons[j]='\0'; i=0; if(strcmp(ordre, "auto")==0){ strcpy(BUF_TX,"automatic mode is running\n\rchange voltage reference value with ref command\n\r"); flagAuto = 1; ref=18; } else if(strcmp(ordre, "manual")==0){ strcpy(BUF_TX,"pwm mode is running\n\rchange duty cycle value with pwm command\n\r"); flagAuto = 0; pwm=0; } else if(strcmp(ordre, "ref")==0){ sscanf(cons,"%i",&aux1); if((aux1!=18)&&(aux1!=24)&&(aux1!=30)&&(aux1!=36)){ strcpy(BUF_TX,"error: failed value\n\r"); } else { ref=aux1; sprintf(BUF_TX,"output voltage value is updated to %d\n\r",ref); } } else if(strcmp(ordre, "pwm")==0){ sscanf(cons,"%i",&aux1); if((aux1>=0)&&(aux1<=100)&&(strcmp(cons, "")!=0)){ sprintf(BUF_TX,"the duty cycle value is updated to %d%\n\r",aux1); pwm= (aux1*T3_TICK)/100; } else { strcpy(BUF_TX,"error: failed value\n\r"); } } else if(strcmp(ordre, "means")==0);//{ // break; // } else if(strcmp(ordre, "can")==0){ uartPutString("can controller configuration :\n\r"); sprintf(BUF_TX,"bus speed 250000 bps\n\r"); uartPutString(BUF_TX); sprintf(BUF_TX,"data length 8 bytes\n\r"); uartPutString(BUF_TX); sprintf(BUF_TX,"filter 0x%03X\n\r",fil); uartPutString(BUF_TX); strcpy(BUF_TX,"filter mask 0xFFF\n\r"); } else if(strcmp(ordre, "filter")==0){ if(strlen(cons)==3){ sscanf(cons,"%X",&fil); sprintf(BUF_TX,"can filter value is updated to 0x%03X\n\r",fil); } else { sprintf(BUF_TX,"error: failed value size= %d\n\r",strlen(cons)); } } else if(strcmp(ordre, "help")==0){ uartPutString("auto start automatic mode and stop manual mode\r\n"); uartPutString(" update voltage reference value with ref command\r\n"); uartPutString("manual start manual mode and stop automatic mode\r\n"); uartPutString(" update duty cycle value with pwm command\r\n"); uartPutString("ref update output voltage reference value\r\n"); uartPutString("pwm update duty cycle value\r\n"); uartPutString("means print currents analog values\r\n"); uartPutString("can print can controller configuration\r\n"); uartPutString("filter update can filter\r\n"); sprintf(BUF_TX,"help print supported commands\r\n"); } else if(strcmp(ordre, "")==0){ strcpy(BUF_TX,"\r\n"); } else{ strcpy(BUF_TX,"error: enter help to know the supported commands\n\r"); } uartPutString(BUF_TX); strcpy(BUF_TX,"boost:~# \0"); uartPutString(BUF_TX); } flagReception = 0; if (consigne < 0) consigne = 0; else if (consigne > 262143) consigne = 262143; } } close(); }
/** * @fn void dialogueUART( void *pvParameters ) * @brief Reçoit des ordres depuis le port série * @brief Se réveille lors de l'arrivée de données sur l'UART * @param pvParameters Pointeur sur un paramètre passé à la tâche */ void dialogueUART( void *pvParameters ) { int i = 0; // compteurs de boucle int j; // compteurs de boucle char BUF_RX[50]; // buffer de reception via l'uart char BUF_TX[100]; // buffer de transmission via l'uart int aux1; // variable tampon pour vérifier la validité avant de mettre dans pwm char ordre[50]; // variable tampon pour récupérer l'ordre envoyé par l'uart char cons[10]; // variable tampon pour récupérer la consigne suivant l'ordre // boucle d'excecution for(;;) { vTaskSuspend(xDialogueUARTHandle); if (aux == 0x1B && i == 0) { i = strlen(ordre)+ 1 + strlen(cons) + 1; sprintf (BUF_TX,"%s %s\r\n", ordre, cons); uartPutString(BUF_RX); } else { uartPutChar(aux); // renvoie d'aquittement // prise en compte de correction if (aux == '\b') { if(i > 0) { i--; uartPutChar(' '); uartPutChar('\b'); } //recupération de l'ordre caractere par caractere tant qu'il n'y a pas de retour chariot } else if (aux != '\r') { BUF_RX[i] = aux; i++; } else { // si aux = \r BUF_RX[i] = '\0'; uartPutChar('\r'); uartPutChar('\n'); i = 0; // Séparation ordre-consigne while ((BUF_RX[i] != ' ')&&(BUF_RX[i] != '\0')) { ordre[i] = BUF_RX[i]; i++; } ordre[i] = '\0'; // transformation de ordre en string j = 0; while (BUF_RX[i] != '\0') { cons[j] = BUF_RX[i]; i++; j++; } cons[j] = '\0'; // transformation de ordre en string i = 0; if(strcmp(ordre, "auto") == 0){ strcpy(BUF_TX,"automatic mode is running\n\rchange voltage reference value with ref command\n\r"); flagAuto = TRUE; ref = 180; } else if (strcmp(ordre, "manual") == 0) { strcpy(BUF_TX,"pwm mode is running\n\rchange duty cycle value with pwm command\n\r"); flagAuto = FALSE; pwm = 0; } else if (strcmp(ordre, "ref") == 0) { sscanf(cons,"%i",&aux1); if (flagAuto == FALSE) sprintf(BUF_TX,"manual mode is running, write manual before sending a new value for ref\n\r"); else if ((aux1 != 18)&&(aux1 != 24)&&(aux1 != 30)&&(aux1 != 36)) strcpy(BUF_TX,"error: failed value\n\r"); else { ref = aux1*10; sprintf(BUF_TX,"output voltage value is updated to %d\n\r",ref); } } else if (strcmp(ordre, "pwm") == 0){ sscanf(cons,"%i",&aux1); if (flagAuto == TRUE) sprintf(BUF_TX,"automatic mode is running, write manual before sending a new value for pwm\n\r"); else if ((aux1 >= 0) && (aux1 <= 100) && (strcmp(cons, "") != 0)) { sprintf(BUF_TX,"the duty cycle value is updated to %d%\n\r",aux1); pwm = aux1; } else strcpy(BUF_TX,"error: failed value\n\r"); } else if (strcmp(ordre, "meas") == 0) { sprintf(BUF_TX,"Courant d'entree : IL = %d A\n\r",mesure[0][pMesure]); uartPutString(BUF_TX); sprintf(BUF_TX,"Tension d'entree : VI = %d V\n\r",mesure[2][pMesure]); uartPutString(BUF_TX); sprintf(BUF_TX,"Tension de sortie : VO = %d V\n\r",mesure[1][pMesure]); // uartPutString(BUF_TX); } else if(strcmp(ordre, "can") == 0) { uartPutString("can controller configuration :\n\r"); sprintf(BUF_TX,"bus speed 250000 bps\n\r"); uartPutString(BUF_TX); sprintf(BUF_TX,"data length 8 bytes\n\r"); uartPutString(BUF_TX); sprintf(BUF_TX,"filter 0x%03X\n\r",fil); uartPutString(BUF_TX); strcpy(BUF_TX,"filter mask 0x7FF\n\r"); } else if (strcmp(ordre, "filter") == 0) { if(strlen(cons) == 3) { sscanf(cons,"%X",&fil); sprintf(BUF_TX,"can filter value is updated to \ 0x%03X\n\r",fil); } else sprintf(BUF_TX,"error: failed value size= %d\n\r",strlen(cons)); } else if (strcmp(ordre, "help") == 0) { uartPutString("auto start automatic mode and stop manual mode\r\n"); uartPutString(" update voltage reference value with ref command\r\n"); uartPutString("manual start manual mode and stop automatic mode\r\n"); uartPutString(" update duty cycle value with pwm command\r\n"); uartPutString("ref update output voltage reference value\r\n"); uartPutString("pwm update duty cycle value\r\n"); uartPutString("meas print currents analog values\r\n"); uartPutString("can print can controller configuration\r\n"); uartPutString("filter update can filter\r\n"); sprintf(BUF_TX,"help print supported commands\r\n"); } else if (strcmp(ordre, "") == 0) strcpy(BUF_TX,"\r\n"); else strcpy(BUF_TX,"error: enter help to know the supported \ commands\n\r"); uartPutString(BUF_TX); strcpy(BUF_TX,"boost:~# \0"); uartPutString(BUF_TX); }
/** * @brief decode state machine. * */ void decode_machine(void) { inv = offset + cur_stamp; //update offset switch (dec.state){ case Waiting: /* go to start bit if rising edge exist. */ if (rising == cur_edge) { dec.state = Sta0; offset = 0; inv = 0; } break; // case Sta0: if( ( suit == IsTime2Detect(inv) ) && ( falling == cur_edge ) ){ dec.data = 0; //clear data field for store new potential data dec.odd = 0; //clear odd field parity counter dec.state = Bit0; } else if( error == IsTime2Detect(inv) ){ dec.state = Waiting; } break; // case Bit0: dec_parser(BIT0, Bit1); break; // case Bit1: dec_parser(BIT1, Bit2); break; // case Bit2: dec_parser(BIT2, Bit3); break; // case Bit3: dec_parser(BIT3, Bit4); break; // case Bit4: dec_parser(BIT4, Bit5); break; // case Bit5: dec_parser(BIT5, Bit6); break; // case Bit6: dec_parser(BIT6, Bit7); break; // case Bit7: dec_parser(BIT7, Parity); break; // case Parity: if ( ( suit == IsTime2Detect(inv) ) ){ //it's time to determine if( rising == cur_edge ){ dec.odd++; } if( 1 == (dec.odd%2)){ //parity pass dec.state = Sto0; } else{ //parity failed dec.state = Waiting; } } else if ( error == IsTime2Detect(inv) ){ //wait for edge detection time dec.state = Waiting; } break; // case Sto0: if ( ( suit == IsTime2Detect(inv) ) ){ //it's time to determine if( rising == cur_edge ){ //stop bit is rising edge uartPutChar(dec.data); } dec.state = Waiting; } else if ( error == IsTime2Detect(inv) ){ //wait for edge detection time dec.state = Waiting; } break; // default: break; // } }