示例#1
0
文件: uart.c 项目: nedos/ddk-arm
void UART3_IRQHandler(void)
{
    g_u3char_available=0;
    g_u3char = 0;

    unsigned int id = UART_GetIntId((LPC_UART_TypeDef *)FPGA_UART3_PORT);
    unsigned int temp = 0;

    switch(id) {
        case 6:
            // RX Line Status / Error
            temp = FPGA_UART3_PORT->LSR;
            break;

        case 4:
            g_u3char = UART_ReceiveByte((LPC_UART_TypeDef *)FPGA_UART3_PORT);
            g_u3char_available=1;
            break;

        case 0xc:
            // Character Time-out indication
            temp = FPGA_UART3_PORT->RBR;
            break;
        case 2:
            // THRE
            temp = FPGA_UART3_PORT->IIR;
            break;
        default:
            break;
    }

   return;
}
示例#2
0
void UART1_IRQHandler(void)
{
	char newData = (char)UART_ReceiveByte(UARTx);
	//TMP_BUFF[new++]=newData;

	if( (newData=='\r') & (iter!=0) )
	{
		GSM_RxBuff[iter] = '\r';
		getAtvResponse();
	}
	else if( (newData=='\n') & (iter!=0) )
	{
		GSM_RxBuff[iter]='\0';
		getServerResponse();
	}
	else if ( (newData=='>') & (iter == 0) )
	{
		promptReceived = 1;
	}
	else if( (newData != '\0') & (newData!='\360') & (newData!='\n') & (newData!='\r'))
	{
		GSM_RxBuff[iter++] = newData;
	}

NVIC_ClearPendingIRQ(UART1_IRQn);
}
示例#3
0
/**
 *	@brief  This functions recovers a response from RFTRANS_95HF device over UART bus
 *  @param  *pData : pointer on data received from RFTRANS_95HF device
 *  @retval None
 */
static void drv95HF_ReceiveUARTResponse(uint8_t *pData)
{
	/* Recover the "Command" byte */
	pData[RFTRANS_95HF_COMMAND_OFFSET] = UART_ReceiveByte(RFTRANS_95HF_UART);

	if(pData[RFTRANS_95HF_COMMAND_OFFSET] == ECHO)
		pData[RFTRANS_95HF_LENGTH_OFFSET]  = 0x00;
	
	else
	{
		/* Recover the "Length" byte */
		pData[RFTRANS_95HF_LENGTH_OFFSET]  = UART_ReceiveByte(RFTRANS_95HF_UART);
	
		/* Recover data */
		if(pData[RFTRANS_95HF_LENGTH_OFFSET]	!= 0)
			UART_ReceiveBuffer(RFTRANS_95HF_UART, &pData[RFTRANS_95HF_DATA_OFFSET], pData[RFTRANS_95HF_LENGTH_OFFSET]);
	}
}
示例#4
0
BOOL xMBMasterPortSerialGetByte(RS485_NUM RS485_X,CHAR * pucByte)
{
	switch(RS485_X)
	{
		case RS485_1:
			*pucByte=UART_ReceiveByte(LPC_UART0);
			break;
		case RS485_2:
			*pucByte=UART_ReceiveByte(LPC_UART2);
			break;
		case RS485_3:
			*pucByte=UART_ReceiveByte(LPC_UART3);
			break;
		case RS485_4:
			*pucByte=UART_ReceiveByte((LPC_UART_TypeDef *)LPC_UART4);
			break;
		default:
			return FALSE;
	}	
	return TRUE;
}
/*********************************************************************//**
 * @brief		Receive a block of data via UART peripheral
 * @param[in]	UARTx	Selected UART peripheral used to send data,
 * 				should be:
 *   			- LPC_UART0: UART0 peripheral
 * 				- LPC_UART1: UART1 peripheral
 * 				- LPC_UART2: UART2 peripheral
 * 				- LPC_UART3: UART3 peripheral
 * @param[out]	rxbuf 	Pointer to Received buffer
 * @param[in]	buflen 	Length of Received buffer
 * @param[in] 	flag 	Flag mode, should be NONE_BLOCKING or BLOCKING

 * @return 		Number of bytes received
 *
 * Note: when using UART in BLOCKING mode, a time-out condition is used
 * via defined symbol UART_BLOCKING_TIMEOUT.
 **********************************************************************/
uint32_t UART_Receive(LPC_UART_TypeDef *UARTx, uint8_t *rxbuf, \
		uint32_t buflen, TRANSFER_BLOCK_Type flag)
{
	uint32_t bToRecv, bRecv, timeOut = 100000000;
	uint8_t *pChar = rxbuf;

	bToRecv = buflen;

	// Blocking mode
	if (flag == BLOCKING) {
		bRecv = 0;
		while (bToRecv){
			timeOut = UART_BLOCKING_TIMEOUT;
			while (!(UARTx->LSR & UART_LSR_RDR)){
				if (timeOut == 0) break;
				timeOut--;
			}
			// Time out!
			if(timeOut == 0) break;
			// Get data from the buffer
			(*pChar++) = UART_ReceiveByte(UARTx);
			bToRecv--;
			bRecv++;
		}
	}
	// None blocking mode
	else {
		bRecv = 0;
		while (bToRecv) {
			if (!(UARTx->LSR & UART_LSR_RDR)) {
				break;
			} else {
				(*pChar++) = UART_ReceiveByte(UARTx);
				bRecv++;
				bToRecv--;
			}
		}
	}
	return bRecv;
}
示例#6
0
char hal_uart_receive(int uart_num)
{
  tUartControl *pControl;
//  LPC_UART_TypeDef *pUart;

  pControl = _get_uart_control (uart_num);
  if (pControl == NULL)
    return 0;
//  pUart = pControl->uart_def_p;
	
#ifdef USE_INTERRUPT
  if (str_buf_is_empty (&pControl->rx_buffer))
    return 0;
  else
    return str_buf_getc (&pControl->rx_buffer);
#else
  return UART_ReceiveByte(pUart);
#endif
}
static char *getsAfterHistory(char *buffer, int length)
{
  u8 ucByte;


  while(1)
  {
    if (UART_ReceiveByte(UART_DEBUG, &ucByte))
    {
      UART_WriteToHost( UART_DEBUG, &ucByte, 1 );
      if((ucByte == '\n') || (ucByte == '\r'))
      {
        // 
        // "carriage return" received, finished getting the string, put '\0' and return
        //
        buffer[length] = '\0';
        ucByte = '\n';
        UART_WriteToHost( UART_DEBUG, &ucByte, 1 );
        return buffer;
      }

      if(ucByte == '\b' && length > 0)
      {
        //
        // Backspace character
        //
        length--;
        ucByte = ' ';
        UART_WriteToHost( UART_DEBUG, &ucByte, 1 );
        ucByte = '\b';
        UART_WriteToHost( UART_DEBUG, &ucByte, 1 );
      }

      else
      {
        buffer[length] = ucByte;
        length ++;
      }
    }
    OS_Sleep(1); 
  }
  return NULL; // We never ging to be here
}
示例#8
0
/*----------------------------------------------------------------------------
  Line Editor
 *----------------------------------------------------------------------------*/
void getline (char *line, int n) {
  int  cnt = 0;
  char c;

  do {
    if ((c = UART_ReceiveByte(LPC_UART0)) == CR) c = LF;  /* read character                 */
    if (c == BACKSPACE  ||  c == DEL) {     /* process backspace              */
      if (cnt != 0)  {
        cnt--;                              /* decrement count                */
        line--;                             /* and line pointer               */
        putchar (BACKSPACE);                /* echo backspace                 */
        putchar (' ');
        putchar (BACKSPACE);
      }
    }
    else if (c != CNTLQ && c != CNTLS) {    /* ignore Control S/Q             */
      putchar (*line = c);                  /* echo and store character       */
      line++;                               /* increment line pointer         */
      cnt++;                                /* and count                      */
    }
  } while (cnt < n - 1  &&  c != LF);       /* check limit and line feed      */
  *(line - 1) = 0;                          /* mark end of string             */
}
示例#9
0
static void uart3_isr(void)
{
	rt_ubase_t level, iir;
    struct dev_uart* uart = &s_dev_uart;

    /* read IIR and clear it */
	iir = UART_GetIntId(LPC_DEV_UART);
	iir &= UART_IIR_INTID_MASK;

	if ((iir == UART_IIR_INTID_RDA) || (iir == UART_IIR_INTID_CTI))
	{
		/* Receive Data Available */
		while (LPC_DEV_UART->LSR & UART_LSR_RDR)
		{
			uart->rx_buffer[uart->save_index] = UART_ReceiveByte(LPC_DEV_UART);

			level = rt_hw_interrupt_disable();
			uart->save_index ++;
			if (uart->save_index >= RT_UART_RX_BUFFER_SIZE)
				uart->save_index = 0;
			rt_hw_interrupt_enable(level);
		}

		/* invoke callback */
		if(uart->parent.rx_indicate != RT_NULL)
		{
		    rt_size_t length;
		    if (uart->read_index > uart->save_index)
                length = RT_UART_RX_BUFFER_SIZE - uart->read_index + uart->save_index;
            else
                length = uart->save_index - uart->read_index;

            uart->parent.rx_indicate(&uart->parent, length);
		}
	}	
}
示例#10
0
/*-------------------------MAIN FUNCTION------------------------------
**********************************************************************/
int c_entry(void)
{	
	InitIR2110();  // Use for Driver PWM IC IR2110
//	EnableIR2110();
	DisableIR2110(); 

	flag_soft_start = 1;

	#ifdef RUN_OK
	Delayms(3000);
	#endif
//	InitDAC();
			//	 Initialize debug via UART0
//	  – 9600bps
//	  – 8 data bit
//	  – No parity
//	  – 1 stop bit
//	  – No flow control	 
	debug_frmwrk_init();
	SetOutputDTR();
	ClearDTR();  //data receive
	InitLedAlarmBattery();
	InitLedAlarmOverload();
	InitPWM(PWM_FREQUENCY,DEADTIME);
	SetDutyPWM(0,0); 
	InitADC(ADC_FREQUENCY); 
	
	InitTimerADCRead();
	
	InitTimerProcessSinwave();
	
	InitTimerProcessSensorRMSValue();
	InitTimerMainProcess();
	InitBuzzer(10000);
	
	#ifdef RUN_OK
	while(fist_startup)
	{
		if(f_Vin_RMS > VOLTAGE_MIN/*Vol*/)	
		{
			flag_voltage_low = 0;
			flag_current_high = 0;
			fist_startup = 0;
		}
	}
	#endif
	while (1)
	{	
		if ((LPC_UART1->LSR & UART_LSR_RDR)) 
		{
			buff[0] = UART_ReceiveByte((LPC_UART_TypeDef *)LPC_UART1);
			if(buff[0] == 'I')
			{
				Delayms(100);
				SetDTR();	  //data transmission
				printf_dbg("I,%d,\r",(uint32_t)(f_Vin_RMS*f_Iin_RMS));
				Delayms(10);
				ClearDTR();	  //data receive
				buff[0] = 0;
			}
		}
	}
    return 1;
}
示例#11
0
char uart_receive(void)
{
	return (UART_ReceiveByte((LPC_UART_TypeDef *)LPC_UART0));
}
示例#12
0
/* Main function */
int main(void)
{
	sc_time_t my_timer;

	P5OUT = CAN_CS;
	dint();
	WDTCTL = WDTCTL_INIT;               //Init watchdog timer
	init_ports();
	init_clock();
	sc_init_timer();
	UART_Init();
	scandal_init(); 
	eint();
  
	/* Set the tyremaster to use  4800 baud */
	UART_baud_rate(2400, CLOCK_SPEED);
	
	my_timer = sc_get_timer();

	while(1) {
		handle_scandal();
		
		if(UART_is_received()) {
			uint8_t preamble, tyre_id, air_temp, batt_voltage, chksum, chksum_data, tyre_pressure_0, tyre_pressure_1;
			uint16_t tyre_pressure;

			preamble = UART_ReceiveByte();

			if(preamble == 0xAA) {
				
				tyre_id = UART_ReceiveByte(); 			// the tyre
				tyre_pressure_0 = UART_ReceiveByte();		// pressure low byte
				tyre_pressure_1 = UART_ReceiveByte();		// pressure hi byte
				air_temp = UART_ReceiveByte();			// air temp
				batt_voltage = UART_ReceiveByte();		// batt voltage
				chksum = UART_ReceiveByte();			// chksum
			
				chksum_data = tyre_id + air_temp + tyre_pressure_0 + tyre_pressure_1 + batt_voltage;

				if(chksum == chksum_data) { 
					tyre_pressure = (tyre_pressure_1 << 8) | tyre_pressure_0;
					
					toggle_red_led();
					scandal_send_channel(TELEM_LOW, tyre_id + TYREMASTER_PRESSURE,
							((uint32_t)tyre_pressure)*2970);
					scandal_send_channel(TELEM_LOW, tyre_id + TYREMASTER_AIR_TEMP,
							((uint32_t)(air_temp-50))*1000);
					scandal_send_channel(TELEM_LOW, tyre_id + TYREMASTER_BATT_VOLTAGE,
							((double)batt_voltage)*18.4+1730);
				} else {
					scandal_send_channel(TELEM_LOW, tyre_id + 4,
						(int)chksum);
					scandal_send_channel(TELEM_LOW, tyre_id + 5,
						(int)chksum_data);
				}
			}
		}

		if(sc_get_timer() >= my_timer + 1000) {
			my_timer = sc_get_timer();
			toggle_yellow_led();
		}

	}

}
示例#13
0
void UART0_IRQHandler(void)
{
	static char *line = &cmdbuf[0];
	static uint16_t frequency=1;
	float results[2];
	uint32_t temp;
	uint32_t U0IIR = 0;
	uint32_t baudrate;
	uint32_t result[2];
	
	extern uint8_t CorrectIndexesOverride;
	extern uint8_t CorrectIndexesBrute;
	extern uint8_t EqualIndexes;
	extern uint16_t FirstOverrideIndex;
	extern uint16_t SecondOverrideIndex;
	char *EndFromStrtoul;
	
	U0IIR=UART_GetIntId(LPC_UART0);
	if ((U0IIR & 0xE) == 0x4)
	{
		c = UART_ReceiveByte(LPC_UART0);
    if (c == CR) c = LF;  /* read character                 */
		if (c != LF)
		{
			if (c == BACKSPACE  ||  c == DEL) {     /* process backspace              */
				if (uart_rcv_len_cnt != 0)  {
					uart_rcv_len_cnt--;                              /* decrement count                */
					line--;                             /* and line pointer               */
					putchar (BACKSPACE);                /* echo backspace                 */
					putchar (' ');
					putchar (BACKSPACE);
				}
			}
			else if (c != CNTLQ && c != CNTLS) {    /* ignore Control S/Q             */
				putchar (*line = c);                  /* echo and store character       */
				line++;                               /* increment line pointer         */
				uart_rcv_len_cnt++;                                /* and count                      */
			}
			if (uart_rcv_len_cnt == sizeof(cmdbuf))
			{
				printf("\nError.");
				printf("\nBuffer full.");
			}
		}
		else
		{
			line = &cmdbuf[0];
			UART_pressed_enter = 1;
			if (command == none)
			{
				if ( (strcmp(cmdbuf, "cal") == 0) && uart_rcv_len_cnt == 3)
				{
					command = Calibrate;
					printf("\n Entered into calibrating state. Type calibrating command.\n>");
					UART_pressed_enter = 0;
				}
				else if ( (strcmp(cmdbuf, "m") == 0) && uart_rcv_len_cnt == 1)
				{
					wait(1);
					Measure(results, frequency);
					printf("\n Magnitude of impedance is: ");
					printf("%.1f Ohm.", results[0]);
					printf("\n Phase of impedance is: ");
					printf("%.2f graduses.", results[1]*57.295779513);
					printf("\nType next command.\n>");
					UART_pressed_enter = 0;
				}
				else if (strncmp(cmdbuf, "f ", 2) == 0)
				{
					frequency = atoi(&(cmdbuf[2]));
					AD9833_SetFreq(frequency*1000);
					AD9833_Start();
					printf("\n New DDS frequency is %u kHz. ", frequency);
					printf("\n Type next command.\n>");
					UART_pressed_enter = 0;
				}
				else if (strncmp(cmdbuf, "fmin ", 5) == 0)
				{
					f_min = atoi(&(cmdbuf[5]));
					printf("\n f_min are %u kHz.", f_min);
					printf("\n Type next command.\n>");
					UART_pressed_enter = 0;
				}
				else if (strncmp(cmdbuf, "fmax ", 5) == 0)
				{
					f_max = atoi(&(cmdbuf[5]));
					printf("\n f_max are %u kHz.", f_max);
					printf("\n Type next command.\n>");
					UART_pressed_enter = 0;
				}
				else if (strncmp(cmdbuf, "nf ", 3) == 0)
				{
					n_F = atoi(&(cmdbuf[3]));
					printf("\n n_F are %u.", n_F);
					printf("\n Type next command.\n>");
					UART_pressed_enter = 0;
				}
				else if ((strncmp(cmdbuf, "bg", 2) == 0) && uart_rcv_len_cnt == 2)
				{
					bg_flag = 1;
					UART_pressed_enter = 0;
				}
				else if (strncmp(cmdbuf, "r ", 2) == 0)
				{
					temp = atoi(&(cmdbuf[2]));
					AD7793_SetRate(temp);
					printf("\n AD7793 rate field = are %u.", temp);
					printf("\n Type next command.\n>");
					UART_pressed_enter = 0;
				}
				else if ( uart_rcv_len_cnt == 0)
				{
					StartADC=1;
					UART_pressed_enter = 0;
				}
				else if (strncmp(cmdbuf, "g", 1) == 0)
				{
					ADC_RUN(result);
					printf("\n  Magnitude are %u.", result[0]);
					printf("\n  Phase are %u.", result[1]);
					printf("\n Type next command.\n>");
					UART_pressed_enter = 0;
				}
				else if (strncmp(cmdbuf, "d ", 2) == 0)
				{
					debug_mode = atoi(&(cmdbuf[2]));
					if (debug_mode == 0)
					{
						printf("\n Debug mode disabled.");
					}
					else
					{
						printf("\n Debug mode enabled.");
					}
					printf("\nType next command.\n>");
					UART_pressed_enter = 0;
				}
				else if (strncmp(cmdbuf, "raw ", 4) == 0)
				{
					raw_data_mode = atoi(&(cmdbuf[4]));
					if (raw_data_mode == 0)
					{
						printf("\n Raw data mode disabled.");
					}
					else
					{
						printf("\n Raw data mode enabled.");
					}
					printf("\nType next command.\n>");
					UART_pressed_enter = 0;
				}
				else if (strncmp(cmdbuf, "UartFifo ", 9) == 0)
				{
					UartWithFifo = atoi(&(cmdbuf[9]));
					if (UartWithFifo == 0)
					{
						printf("\n Software UART FIFO disabled.");
					}
					else
					{
						printf("\n Software UART FIFO enabled.");
					}
					printf("\nType next command.\n>");
					UART_pressed_enter = 0;
				}
				else if (strncmp(cmdbuf, "UART ", 5) == 0)
				{
					baudrate = atoi(&(cmdbuf[5]));
					SER_Init(baudrate);
					printf("\n UART baudrate are %u bps.", baudrate);
					printf("\nType next command.\n>");
					UART_pressed_enter = 0;
				}
				else if (strncmp(cmdbuf, "CIO ", 4) == 0)
				{
					CorrectIndexesOverride = atoi(&(cmdbuf[4]));
					if (CorrectIndexesOverride == 1)
					{
						printf("\nEnter correct override indexes:\n>");
						command = CorrectIndexesPutting;
					}
					else
					{
						printf("\n Index overriding disabled.");
						printf("\nType next command.\n>");
					}
					UART_pressed_enter = 0;
				}
				else if (strncmp(cmdbuf, "CIB ", 4) == 0)
				{
					CorrectIndexesBrute = atoi(&(cmdbuf[4]));
					if (CorrectIndexesBrute == 1)
					{
						printf("\n Correct calibrating curves brute forcing enabled.");
						printf("\nType next command.\n>");
					}
					else
					{
						printf("\n Correct calibrating curves brute force disabled.");
						printf("\nType next command.\n>");
					}
					UART_pressed_enter = 0;
				}
				else if (strncmp(cmdbuf, "EI ", 3) == 0)
				{
					EqualIndexes = atoi(&(cmdbuf[3]));
					if (EqualIndexes == 1)
					{
						printf("\n Equal calibrating indexes for mag and phase enabled.");
						printf("\nType next command.\n>");
					}
					else
					{
						printf("\n Equal calibrating indexes for mag and phase disabled.");
						printf("\nType next command.\n>");
					}
					UART_pressed_enter = 0;
				}
				else
				{
					printf("\nWrong command. Please type right command.\n");
					UART_pressed_enter = 0;
				}
				memset(cmdbuf,0,15);
				uart_rcv_len_cnt=0;
			}
			else if (command == CorrectIndexesPutting)
			{
				FirstOverrideIndex = strtoul(cmdbuf, &EndFromStrtoul, 10);
				SecondOverrideIndex = atoi(EndFromStrtoul);
				printf("\n FirstCorrectIndex = %u", FirstOverrideIndex);
				printf("\n SecondCorrectIndex = %u", SecondOverrideIndex);
				printf("\nType next command.\n>");
				UART_pressed_enter = 0;
				command = none;
				memset(cmdbuf,0,15);
				uart_rcv_len_cnt=0;
			}
		}
	}
	else if ((U0IIR & 0xE) == 0x2)
	{
		if (SW_UART_FIFO_STRUCT.count!=0)
		{
			LPC_UART0->THR = SW_UART_pop();
		}
		else
			UART_IntConfig(LPC_UART0,UART_INTCFG_THRE,DISABLE);			//Disable UART0 THRE Interrupt
	}
}
示例#14
0
char uart_receive(void)
{
	return (UART_ReceiveByte(DBG_UART));
}
示例#15
0
uint8_t USB_readByte()
{
	return(UART_ReceiveByte((LPC_UART_TypeDef *)LPC_UART0));
}