Esempio n. 1
0
void usart1_isr(void)
{
  if (usart_get_flag(USART1, USART_SR_RXNE)) {
    char c = usart_recv(USART1);
    if (c == '\n') {
      if (rx_head > 0) {
        rx_buf[rx_head] = 0;
        usart_on_line_recv(rx_buf, rx_head);
      }
      rx_head = 0;
    } else {
      rx_buf[rx_head] = c;
      rx_head = (rx_head + 1) % sizeof(rx_buf);
    }
  }

  if (usart_get_flag(USART1, USART_SR_TXE)) {
    if (waiting_tx_bytes() > 0) {
      usart_send(USART1, tx_buf[tx_tail]);
      tx_tail = (tx_tail + 1) % sizeof(tx_buf);
    } else {
      usart_disable_tx_interrupt(USART1);
    }
  }
}
Esempio n. 2
0
// Data Register Empty Interrupt handler
void serial_txint(void)
{
  uint32_t tail = serial_tx_buffer_tail; // Temporary serial_tx_buffer_tail (to optimize for volatile)

  #ifdef ENABLE_XONXOFF
    if (flow_ctrl == SEND_XOFF) {
      usart_send(USART2, XOFF_CHAR);
      flow_ctrl = XOFF_SENT;
    } else if (flow_ctrl == SEND_XON) {
      usart_send(USART2, XON_CHAR);
      flow_ctrl = XON_SENT;
    } else
  #endif
  {

    if (tail != serial_tx_buffer_head) 
    {
    // Send a byte from the buffer
    usart_send(USART2, serial_tx_buffer[tail]);
    // Update tail position
    tail++;
    if (tail == TX_BUFFER_SIZE) { tail = 0; }

    serial_tx_buffer_tail = tail;
    }
  }

  // Turn off Data Register Empty Interrupt to stop tx-streaming if this concludes the transfer
  //if (tail == serial_tx_buffer_head) { UCSR0B &= ~(1 << UDRIE0); }

    if (tail == serial_tx_buffer_head)  usart_disable_tx_interrupt(USART2);

  return;

}
Esempio n. 3
0
void usart1_isr(void)
{
	u8 ch;

	//if Receive interrupt
	if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) &&
			((USART_SR(USART1) & USART_SR_RXNE) != 0))
	{
		ch=usart_recv(USART1);
		buffer_put(&u1rx, ch);
	}

	if (((USART_CR1(USART1) & USART_CR1_TXEIE) != 0) &&
			((USART_SR(USART1) & USART_SR_TXE) != 0))
	{
		if (buffer_get(&u1tx, &ch) == SUCCESS)
		{
			//if char read from buffer
			usart_send(USART1, ch);
		}
		else	//if buffer empty
		{

			//disable Transmit Data Register empty interrupt
			usart_disable_tx_interrupt(USART1);
		}
	}
}
Esempio n. 4
0
/* ----------------------- Enable USART interrupts -----------------------------*/
void
vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
{
    /* If xRXEnable enable serial receive interrupts. If xTxENable enable
     * transmitter empty interrupts.
     */
    if (xRxEnable)
    {
        txen = false;
        usart_enable_rx_interrupt(MB_USART);
    }
    else
    {
        usart_disable_rx_interrupt(MB_USART);
    }

    if (xTxEnable)
    {
        txen = true;
        gpio_set(MB_USART_TXEN_PORT, MB_USART_TXEN_PIN);

        usart_enable_tx_interrupt(MB_USART);
    }
    else
    {
        txen = false;
        usart_disable_tx_interrupt(MB_USART);
    }
}
Esempio n. 5
0
void usart_setup(void)
{
/* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */
    rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN |
                    RCC_APB2ENR_AFIOEN | RCC_APB2ENR_USART1EN);
/* Enable the USART1 interrupt. */
    nvic_enable_irq(NVIC_USART1_IRQ);
/* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port A for transmit. */
    gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
              GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
/* Setup GPIO pin GPIO_USART1_RE_RX on GPIO port A for receive. */
    gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
              GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX);
/* Setup UART parameters. */
    usart_set_baudrate(USART1, 115200);
    usart_set_databits(USART1, 8);
    usart_set_stopbits(USART1, USART_STOPBITS_1);
    usart_set_parity(USART1, USART_PARITY_NONE);
    usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
    usart_set_mode(USART1, USART_MODE_TX_RX);
/* Enable USART1 receive interrupts. */
    usart_enable_rx_interrupt(USART1);
    usart_disable_tx_interrupt(USART1);
/* Finally enable the USART. */
    usart_enable(USART1);
}
Esempio n. 6
0
void
Board_FY20AP::com_fini(void)
{
	usart_disable(USART1);
	usart_disable_rx_interrupt(USART1);
	usart_disable_tx_interrupt(USART1);
}
Esempio n. 7
0
/******************************************************************************
Initialize the hardware to receive (USART based) CAN messages and start the
timer for the CANopen stack.
INPUT	bitrate		bitrate in kilobit (fixed at 115200)
OUTPUT	1 if successful	
******************************************************************************/
unsigned char canInit(unsigned int bitrate)
{
	msg_recv_status = 0;
	msg_received = 0;
/* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */
	rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN |
				    RCC_APB2ENR_AFIOEN | RCC_APB2ENR_USART1EN);
/* Enable the USART1 interrupt. */
	nvic_enable_irq(NVIC_USART1_IRQ);
/* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port A for transmit. */
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
		      GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
/* Setup GPIO pin GPIO_USART1_RE_RX on GPIO port A for receive. */
	gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
		      GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX);
/* Setup USART parameters. */
	usart_set_baudrate(USART1, 115200);
	usart_set_databits(USART1, 8);
	usart_set_stopbits(USART1, USART_STOPBITS_1);
	usart_set_parity(USART1, USART_PARITY_NONE);
	usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
	usart_set_mode(USART1, USART_MODE_TX_RX);
/* Enable USART1 receive interrupts. */
	usart_enable_rx_interrupt(USART1);
	usart_disable_tx_interrupt(USART1);
/* Finally enable the USART. */
	usart_enable(USART1);

/* Initialise the send and receive buffers */
	buffer_init(send_buffer,BUFFER_SIZE);
	buffer_init(receive_buffer,BUFFER_SIZE);

 	return 1;
}
Esempio n. 8
0
void usart2_isr(void)
{
	static u8 data = 'A';

	/* Check if we were called because of RXNE. */
	if (((USART_CR1(USART2) & USART_CR1_RXNEIE) != 0) &&
	    ((USART_SR(USART2) & USART_SR_RXNE) != 0)) {

		/* Indicate that we got data. */
		gpio_toggle(GPIOD, GPIO12);

		/* Retrieve the data from the peripheral. */
		data = usart_recv(USART2);

		/* Enable transmit interrupt so it sends back the data. */
		usart_enable_tx_interrupt(USART2);
	}

	/* Check if we were called because of TXE. */
	if (((USART_CR1(USART2) & USART_CR1_TXEIE) != 0) &&
	    ((USART_SR(USART2) & USART_SR_TXE) != 0)) {

		/* Put data into the transmit register. */
		usart_send(USART2, data);

		/* Disable the TXE interrupt as we don't need it anymore. */
		usart_disable_tx_interrupt(USART2);
	}
}
Esempio n. 9
0
void mew_bluetooth_init(void) {
    gpio_mode_setup(MEW_BLUETOOTH_POWER_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, MEW_BLUETOOTH_POWER_PIN);
    gpio_set_output_options(MEW_BLUETOOTH_POWER_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, MEW_BLUETOOTH_POWER_PIN);
    gpio_clear(MEW_BLUETOOTH_POWER_PORT, MEW_BLUETOOTH_POWER_PIN);
    
    gpio_mode_setup(MEW_BLUETOOTH_RESET_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, MEW_BLUETOOTH_RESET_PIN);
    gpio_set_output_options(MEW_BLUETOOTH_RESET_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, MEW_BLUETOOTH_RESET_PIN);
    gpio_set(MEW_BLUETOOTH_RESET_PORT, MEW_BLUETOOTH_RESET_PIN);
    
    gpio_mode_setup(MEW_BLUETOOTH_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, MEW_BLUETOOTH_PIN);
    gpio_set_af(MEW_BLUETOOTH_PORT, MEW_BLUETOOTH_PORT_AF, MEW_BLUETOOTH_PIN);
    
    usart_disable(MEW_BLUETOOTH_USART);
    usart_set_baudrate(MEW_BLUETOOTH_USART, MEW_BLUETOOTH_SPEED);
    usart_set_databits(MEW_BLUETOOTH_USART, 8);
    usart_set_stopbits(MEW_BLUETOOTH_USART, USART_STOPBITS_1);
    usart_set_mode(MEW_BLUETOOTH_USART, USART_MODE_TX_RX);
    usart_set_parity(MEW_BLUETOOTH_USART, USART_PARITY_NONE);
    usart_set_flow_control(MEW_BLUETOOTH_USART, USART_FLOWCONTROL_NONE);
    usart_enable_rx_interrupt(MEW_BLUETOOTH_USART);
    usart_disable_tx_interrupt(MEW_BLUETOOTH_USART);
    usart_enable_tx_dma(MEW_BLUETOOTH_USART);
    usart_enable(MEW_BLUETOOTH_USART);
    
    nvic_enable_irq(MEW_BLUETOOTH_IRQ);
    nvic_enable_irq(MEW_BLUETOOTH_DMA_NVIC_TX);
    
    memset(_mew_bt_buffer, 0, MEW_BT_RECEIVE_BUFFER_SIZE);
}
Esempio n. 10
0
void usart3_isr(void)
{

	long xHigherPriorityTaskWoken = pdFALSE;
	char cChar;

	if (usart_get_flag(USART3, USART_SR_TXE) == true) {
		/* The interrupt was caused by the THR becoming empty.  Are there any
		 more characters to transmit? */
		if (xQueueReceiveFromISR(xCharsForTx[2], &cChar,
				&xHigherPriorityTaskWoken)) {
			gpio_set(GPIO_BANK_USART3_RTS, GPIO_USART3_RTS); // set RTS
			/* A character was retrieved from the buffer so can be sent to the THR now. */
			usart_send(USART3, (uint8_t) cChar);
		} else {
//			gpio_clear(GPIO_BANK_USART3_RTS, GPIO_USART3_RTS); // clear RTS
		}
	}

	if (usart_get_flag(USART3, USART_SR_RXNE) == true) {
		cChar = (char) usart_recv(USART3);
		xQueueSendFromISR(xRxedChars[2], &cChar, &xHigherPriorityTaskWoken);
	}

// ----- transmission complete:
	if (usart_get_flag(USART3, USART_SR_TC) == true) {
		gpio_clear(GPIO_BANK_USART3_RTS, GPIO_USART3_RTS); // clear RTS
		USART_SR(USART3) &= ~USART_SR_TC;	// reset flag TC
		usart_disable_tx_interrupt(USART3);
	}

	portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);

}
Esempio n. 11
0
void DBG_USART_ISR(void)
{
    /* Check if we were called because of RXNE. */
    if (((USART_CR1(DBG_USART) & USART_CR1_RXNEIE) != 0) && ((USART_SR(DBG_USART) & USART_SR_RXNE) != 0))
    {
        usart_data = usart_recv(DBG_USART);
        if( !dbg_fifo_write_byte( &usart_rx_buf, usart_data ) )
        {
            usart_disable_rx_interrupt(DBG_USART);
        }
    }
    /* Check if we were called because of TXE. */
    if (((USART_CR1(DBG_USART) & USART_CR1_TXEIE) != 0) && ((USART_SR(DBG_USART) & USART_SR_TXE) != 0))
    {
        /* Put data into the transmit register. */
        if( dbg_fifo_read_byte( &usart_tx_buf, &usart_data ) )
        {
            usart_send(DBG_USART, usart_data);
        }
        else
        {
            /* Disable the TXE interrupt as we don't need it anymore. */
            usart_disable_tx_interrupt(DBG_USART);
        }
    }
}
Esempio n. 12
0
void usart_init( int baudrate )
{
	gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX);
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);

#ifdef BUFFERED


	buffer_reset (&u1rx);
	buffer_reset (&u1tx);

	nvic_enable_irq(NVIC_USART1_IRQ);
	usart_enable_rx_interrupt(USART1);
	usart_disable_tx_interrupt(USART1);

#endif

	// usart peripheral confguration
	usart_set_baudrate(USART1, baudrate);
	usart_set_databits(USART1, 8);
	usart_set_parity(USART1, USART_PARITY_NONE);
	usart_set_mode(USART1, USART_MODE_TX_RX);
	usart_set_stopbits(USART1, USART_STOPBITS_1);
	usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);

	usart_enable(USART1);

}
Esempio n. 13
0
File: uart.c Progetto: ChuckM/libcpa
/*
 * Common USART/UART transmit and receive interrupts are
 * collated to here. Each serial port simply redirects to here
 * while passing in its BASE address in peripheral space and the
 * channel # it has been assigned. The channel number mapping is
 * established at initialization time.
 */
void
common_usart_isr(uint32_t usart, int channel) {
    if (USART_SR(usart) & USART_SR_RXNE) {
        recv_buf[channel][nxt_recv_ndx[channel]] = USART_DR(usart);
        nxt_recv_ndx[channel] = (nxt_recv_ndx[channel] + 1) % UART_BUF_SIZE;
    }
    if (USART_SR(usart) & USART_SR_TXE) {
        if (nxt_xmit_ndx[channel] == cur_xmit_ndx[channel]) {
            usart_disable_tx_interrupt(usart);  // nothing to send
        } else {
            USART_DR(usart) = xmit_buf[channel][cur_xmit_ndx[channel]];
            cur_xmit_ndx[channel] = (cur_xmit_ndx[channel] + 1) % UART_BUF_SIZE;
        }
    }
}
Esempio n. 14
0
/**
 * Interrupt handler for the USART1 peripheral.
 *
 * If the interrupt was caused by a received character, the handler adds the
 * chararacter to the rx_buffer. If the interrupt was caused by the peripheral
 * being ready to send a character, the handler takes one from the tx_buffer,
 * and writes it to the peripheral. In case the interrupt was caused by a USART
 * peripheral buffer overrun (typically happens during debugging), the handler
 * clears that interrupt flag.
 */
void usart1_isr(void)
{
	if (usart_get_flag(USART1, USART_ISR_RXNE)) {
		char ch = (char) usart_recv(USART1);
		os_char_buffer_write_ch(&bsp_rx_buffer, ch);

	} else if (usart_get_flag(USART1, USART_ISR_TXE)) {
		char ch = '\0';
		if (os_char_buffer_read_ch(&bsp_tx_buffer, &ch)) {
			usart_send(USART1, (uint8_t) ch);
		} else {
			usart_disable_tx_interrupt(USART1);
		}

	} else if (usart_get_flag(USART1, USART_ISR_ORE)) {
		USART_ICR(USART1) |= USART_ICR_ORECF;
	}
}
Esempio n. 15
0
void usart2_isr(void)
{
	static uint16_t data;

	/* Check if we were called because of RXNE. */
	if (usart_get_flag(USART2,USART_SR_RXNE))
	{
		/* If buffer full we'll just drop it */
		buffer_put(receiveBuffer, (uint8_t) usart_recv(USART2));
	}
	/* Check if we were called because of TXE. */
	if (usart_get_flag(USART2,USART_SR_TXE))
	{
		/* If buffer empty, disable the tx interrupt */
		data = buffer_get(sendBuffer);
		if ((data & 0xFF00) > 0) usart_disable_tx_interrupt(USART2);
		else usart_send(USART2, (data & 0xFF));
	}
}
Esempio n. 16
0
void Serial_stm32::irq_handler(void)
{
    uint8_t data = 0;

    // Check if we were called because of RXNE
    if (((USART_CR1(config_.device) & USART_CR1_RXNEIE) != 0) &&
            ((USART_SR(config_.device)  & USART_SR_RXNE)    != 0))
    {

        // Retrieve the data from the peripheral
        data = usart_recv(config_.device);
        rx_buffer_.put_lossy(data);

        // Enable transmit interrupt so it sends back the data
        usart_enable_tx_interrupt(config_.device);
    }

    // Check if we were called because of TXE
    if (((USART_CR1(config_.device) & USART_CR1_TXEIE) != 0) &&
            ((USART_SR(config_.device)  & USART_SR_TXE)    != 0))
    {
        if (tx_buffer_.readable() > 0)
        {
            // Get data from output buffer
            tx_buffer_.get(data);

            // Put data into the transmit register
            usart_send(config_.device, data);
        }
        else
        {
            // Disable the TXE interrupt as we don't need it anymore
            usart_disable_tx_interrupt(config_.device);
        }
    }

    // Call callback function if attached
    if (irq_callback != 0)
    {
        irq_callback(this);
    }
}
Esempio n. 17
0
void usart1_isr(void)
{
	static UNS16 data;

/* Check if we were called because of RXNE. */
	if (usart_get_flag(USART1,USART_SR_RXNE))
	{
/* Put to temporary message buffer */
		data = (UNS8) usart_recv(USART1);
		message_temp[msg_recv_status] = (UNS8) data;
		msg_recv_status++;
/* At this point we should check for integrity of the message, but there is no
way to recover if the message length sent is wrong. So we'll do what we can and
hope we can catch up eventually. */
		if ((msg_recv_status > 3) && (message_temp[3] > 8))
		{
/* Bad message length - abort and restart a new message */
			msg_recv_status = 0;
		}
/* Check if we are finished (message length is in element 3) */
		else if (msg_recv_status == 4 + message_temp[3])
		{
			msg_recv_status = 0;
			msg_received++;			/* Signal another message arrived */
			UNS8 i;
/* Dump to buffer; if full we'll just have to drop it */
			for (i=0; i < 4 + message_temp[3]; i++)
				buffer_put(receive_buffer,message_temp[i]);
		}
	}
/* Check if we were called because of TXE. */
	else if (usart_get_flag(USART1,USART_SR_TXE))
	{
		data = buffer_get(send_buffer);
/* If buffer empty, disable the tx interrupt */
		if ((data & 0xFF00) > 0) usart_disable_tx_interrupt(USART1);
		else
        {
                usart_send(USART1, (data & 0xFF));
        }
	}
}
Esempio n. 18
0
void
usart_init(int usart, int irq, int baudrate, int over8) {
  /* Setup USART parameters. */
  nvic_disable_irq(irq);
  usart_disable_rx_interrupt(usart);
  usart_disable_tx_interrupt(usart);
  usart_disable(usart);
  USART_CR1(usart) |= over8;  /* This doubles the listed baudrate. */
  usart_set_baudrate(usart, baudrate);
  usart_set_databits(usart, 8);
  usart_set_stopbits(usart, USART_STOPBITS_1);
  usart_set_mode(usart, USART_MODE_TX_RX);
  usart_set_parity(usart, USART_PARITY_NONE);
  usart_set_flow_control(usart, USART_FLOWCONTROL_NONE);
  /* Finally enable the USART. */
  usart_enable(usart);
  usart_enable_rx_interrupt(usart);
  usart_enable_tx_interrupt(usart);
  nvic_enable_irq(irq);
}
Esempio n. 19
0
void usart1_isr(void)
{
	char c;
	if (usart_get_flag(USART1, USART_SR_TXE)) {
		if (!queue_is_empty(&uart_tx_buf)) {
			queue_dequeue(&uart_tx_buf, &c);
			usart_send(USART1, (uint16_t)c);
		} else {
			usart_disable_tx_interrupt(USART1);
		}
	}


	if (usart_get_flag(USART1, USART_SR_RXNE)) {
		if (!queue_is_full(&uart_rx_buf)) {
			c = usart_recv(USART1);
			queue_enqueue(&uart_rx_buf, &c);
		}
	}
}
Esempio n. 20
0
OS_INTERRUPT void
usart1_isr(void)
{
	OS::scmRTOS_ISRW_TYPE ISR;

	/* receiver not empty? */
	if (usart_get_flag(USART1, USART_SR_RXNE))
		board_fy20ap.com_rx(usart_recv(USART1));

	/* transmitter ready? */
	if (usart_get_flag(USART1, USART_SR_TXE)) {

		uint8_t c;

		if (board_fy20ap.com_tx(c)) {
			usart_send(USART1, c);

		} else {
			/* clear TX empty interrupt as we have no data */
			usart_disable_tx_interrupt(USART1);
		}
	}
}
/* USART ISR */
void usart1_isr(void)
{
/* Find out what interrupted and get or send data as appropriate */
	/* Check if we were called because of RXNE. */
	if (usart_get_flag(USART1,USART_SR_RXNE))
	{
		/* If buffer full we'll just drop it */
		buffer_put(receive_buffer, (uint8_t) usart_recv(USART1));
	}
	/* Check if we were called because of TXE. */
	if (usart_get_flag(USART1,USART_SR_TXE))
	{
		/* If buffer empty, disable the tx interrupt */
		uint16_t data = buffer_get(send_buffer);
		if (data == BUFFER_EMPTY)
		{
			usart_disable_tx_interrupt(USART1);
		}
		else
		{
			usart_send(USART1, data);
		}
	}
}
Esempio n. 22
0
BOOL
xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
{
    BOOL bStatus;
    rcc_periph_clock_enable(RCC_AFIO            );
    rcc_periph_clock_enable(MB_USART_PERIPH     );
    rcc_periph_clock_enable(MB_USART_TX_PERIPH  );
#if (MB_USART_TX_PERIPH != MB_USART_RX_PERIPH)
    rcc_periph_clock_enable(MB_USART_RX_PERIPH  );
#endif
#if (MB_USART_TX_PERIPH != MB_USART_RX_PERIPH)
    rcc_periph_clock_enable(MB_USART_TXEN_PERIPH);
#endif
    /*Setup TxEN pin*/
    gpio_set_mode(MB_USART_TXEN_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, MB_USART_TXEN_PIN);
    gpio_clear(MB_USART_TXEN_PORT, MB_USART_TXEN_PIN);
    /*Setup TxD pin*/
    gpio_set_mode(MB_USART_TX_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, MB_USART_TX_PIN);
    /*Setup RxD pin*/
    gpio_set_mode(MB_USART_RX_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, MB_USART_RX_PIN);
    /* Enable the MB_USART interrupt. */
    nvic_enable_irq(MB_USART_VECTOR);
    /* Setup UART parameters. */
    usart_set_baudrate    (MB_USART, ulBaudRate            );
    usart_set_stopbits    (MB_USART, USART_STOPBITS_1      );
    usart_set_flow_control(MB_USART, USART_FLOWCONTROL_NONE);
    usart_set_mode        (MB_USART, USART_MODE_TX_RX      );

    bStatus = TRUE;
    switch (eParity)
    {
    case MB_PAR_NONE:
        usart_set_parity(MB_USART, USART_PARITY_NONE);
        break;
    case MB_PAR_ODD:
        usart_set_parity(MB_USART, USART_PARITY_ODD);
        break;
    case MB_PAR_EVEN:
        usart_set_parity(MB_USART, USART_PARITY_EVEN);
        break;
    default:
        bStatus = FALSE;
        break;
    }

    /* Oddity of STM32F series: word length includes parity. 7 bits no parity
    not possible */
    CHAR wordLength;
    switch (ucDataBits)
    {
    case 8:
        if (eParity == MB_PAR_NONE)
        {
            wordLength = 8;
        }
        else
        {
            wordLength = 9;
        }
        usart_set_databits(MB_USART,wordLength);
        break;
    case 7:
        if (eParity == MB_PAR_NONE)
        {
            bStatus = FALSE;
        }
        else
        {
            usart_set_databits(MB_USART,8);
        }
        break;
    default:
        bStatus = FALSE;
    }

    if( bStatus == TRUE )
    {
        /* Finally enable the USART. */
        usart_disable_rx_interrupt(MB_USART);
        usart_disable_tx_interrupt(MB_USART);
        usart_enable(MB_USART);
    }
    return bStatus;
}
Esempio n. 23
0
void commsPrintChar(char *ch)
{
    usart_disable_tx_interrupt(USART2);
    buffer_put(sendBuffer,*ch);
    usart_enable_tx_interrupt(USART2);
}
Esempio n. 24
0
void usart3_isr(void)
{
    static uint8_t rx_checksum = 0x00;
    static uint8_t tx_checksum = 0x00;
    static xbee_packet_t rx_pkt;
    static xbee_packet_t tx_pkt;

    // Check if we were called because of RXNE.
    if (((USART_CR1(USART3) & USART_CR1_RXNEIE) != 0)
            && ((USART_SR(USART3) & USART_SR_RXNE) != 0)) {

        uint8_t data = usart_recv(USART3);
        static uint16_t rx_index = 0;

        switch (xbee_rx_state) {
            case XBEE_START:
                if (data == 0x7E) {
                    rx_index = 0;
                    rx_checksum = 0;
                    xbee_rx_state = XBEE_LENGTH_HIGH;
                }
                break;
            case XBEE_LENGTH_HIGH:
                rx_pkt.length = ((uint16_t) data << 8) & 0xFF00;
                xbee_rx_state = XBEE_LENGTH_LOW;
                break;
            case XBEE_LENGTH_LOW:
                rx_pkt.length = rx_pkt.length | data;
                xbee_rx_state = XBEE_TYPE;
                break;
            case XBEE_TYPE:
                rx_pkt.type = data;
                rx_checksum += data;
                xbee_rx_state = XBEE_MESSAGE;
                break;
            case XBEE_MESSAGE:
                rx_pkt.payload[rx_index] = data;
                rx_checksum += data;
                ++rx_index;
                if (rx_index == rx_pkt.length - 1) {
                    xbee_rx_state = XBEE_CHECKSUM;
                }
                break;
            case XBEE_CHECKSUM:
                if (data == 0xFF - rx_checksum) {
                    xbee_rx_queue.enqueue(rx_pkt);
                }
                xbee_rx_state = XBEE_START;
                break;
        }
    }

    // Check if we were called because of TXE.
    if (((USART_CR1(USART3) & USART_CR1_TXEIE) != 0)
            && ((USART_SR(USART3) & USART_SR_TXE) != 0)) {

        static uint16_t tx_index = 0;

        switch (xbee_tx_state) {
            case XBEE_START:
                if (xbee_tx_queue.isEmpty()) {
                    usart_disable_tx_interrupt(USART3);
                    break;
                } else {
                    tx_pkt = xbee_tx_queue.dequeue();
                }
                usart_send(USART3, 0x7E); // XBEE start
                tx_index = 0;
                tx_checksum = 0;
                xbee_tx_state = XBEE_LENGTH_HIGH;
                break;
            case XBEE_LENGTH_HIGH:
                usart_send(USART3, (tx_pkt.length >> 8) & 0xFF);
                xbee_tx_state = XBEE_LENGTH_LOW;
                break;
            case XBEE_LENGTH_LOW:
                usart_send(USART3, (tx_pkt.length) & 0xFF);
                xbee_tx_state = XBEE_TYPE;
                break;
            case XBEE_TYPE:
                usart_send(USART3, tx_pkt.type);
                tx_checksum += tx_pkt.type;
                xbee_tx_state = XBEE_MESSAGE;
                break;
            case XBEE_MESSAGE:
                usart_send(USART3, tx_pkt.payload[tx_index]);
                tx_checksum += tx_pkt.payload[tx_index];
                ++tx_index;
                if (tx_index == tx_pkt.length - 1) {
                    xbee_tx_state = XBEE_CHECKSUM;
                }
                break;
            case XBEE_CHECKSUM:
                usart_send(USART3, 0xFF - tx_checksum);
                xbee_tx_state = XBEE_START;
                break;
        }
    }
}
Esempio n. 25
0
void usart2_isr(void)
{
    static uint8_t rx_checksum = 0x00;
    static uint8_t tx_checksum = 0x00;
    static gps_packet_t rx_pkt;
    static gps_packet_t tx_pkt;

    // Check if we were called because of RXNE.
    if (((USART_CR1(USART2) & USART_CR1_RXNEIE) != 0)
            && ((USART_SR(USART2) & USART_SR_RXNE) != 0)) {

        volatile uint8_t data = usart_recv(USART2);
        static uint16_t rx_index = 0;

        //usart_send(USART1, data);

        switch (gps_rx_state) {
            case GPS_START1:
                if (data == 0xA0) {
                    rx_index = 0;
                    rx_checksum = 0;
                    gps_rx_state = GPS_START2;
                }
                break;
            case GPS_START2:
                if (data == 0xA1) {
                    gps_rx_state = GPS_LENGTH_HIGH;
                }
                break;
            case GPS_LENGTH_HIGH:
                rx_pkt.length = ((uint16_t) data << 8) & 0xFF00;
                gps_rx_state = GPS_LENGTH_LOW;
                break;
            case GPS_LENGTH_LOW:
                rx_pkt.length = rx_pkt.length | data;
                gps_rx_state = GPS_MESSAGE;
                break;
            case GPS_MESSAGE:
                rx_pkt.payload[rx_index] = data;
                rx_checksum ^= data;
                ++rx_index;
                if (rx_index == rx_pkt.length) {
                    gps_rx_state = GPS_CHECKSUM;
                }
                break;
            case GPS_CHECKSUM:
                if (data == rx_checksum) {
                    gps_rx_queue.enqueue(rx_pkt);
                }
                gps_rx_state = GPS_END1; // XXX: should this go to GPS_START1 and just let that state throw away the 0x0D 0x0A?
                break;
            case GPS_END1:
                if (data == 0x0D) {
                    gps_rx_state = GPS_END2;
                }
                break;
            case GPS_END2:
                if (data == 0x0A) {
                    gps_rx_state = GPS_START1;
                }
                break;
        }
    }

    // Check if we were called because of TXE.
    if (((USART_CR1(USART2) & USART_CR1_TXEIE) != 0)
            && ((USART_SR(USART2) & USART_SR_TXE) != 0)) {

        static uint16_t tx_index = 0;

        switch (gps_tx_state) {
            case GPS_START1:
                if (gps_tx_queue.isEmpty()) {
                    usart_disable_tx_interrupt(USART2);
                    break;
                } else {
                    tx_pkt = gps_tx_queue.dequeue();
                }
                usart_send(USART2, 0xA0);
                tx_index = 0;
                tx_checksum = 0;
                gps_tx_state = GPS_START2;
                break;
            case GPS_START2:
                usart_send(USART2, 0xA1);
                gps_tx_state = GPS_LENGTH_HIGH;
                break;
            case GPS_LENGTH_HIGH:
                usart_send(USART2, (tx_pkt.length >> 8) & 0xFF);
                gps_tx_state = GPS_LENGTH_LOW;
                break;
            case GPS_LENGTH_LOW:
                usart_send(USART2, (tx_pkt.length) & 0xFF);
                gps_tx_state = GPS_MESSAGE;
                break;
            case GPS_MESSAGE:
                usart_send(USART2, tx_pkt.payload[tx_index]);
                tx_checksum ^= tx_pkt.payload[tx_index];
                ++tx_index;
                if (tx_index == tx_pkt.length) {
                    gps_tx_state = GPS_CHECKSUM;
                }
                break;
            case GPS_CHECKSUM:
                usart_send(USART2, tx_checksum);
                gps_tx_state = GPS_END1;
                break;
            case GPS_END1:
                usart_send(USART2, 0x0D);
                gps_tx_state = GPS_END2;
                break;
            case GPS_END2:
                usart_send(USART2, 0x0A);
                gps_tx_state = GPS_START1;
                break;
        }
    }
}
Esempio n. 26
0
int main(void)
{
    float voltage;
    uint8_t skytraq_packet_id = 0;

    clock_setup();
    gpio_setup();
    usart_setup();
    adc_setup();
    
    delay_ms(100);
    gps_tx_queue.enqueue(binary_rate);
    usart_enable_tx_interrupt(USART2); // enable GPS TX interrupt
    
    while (1) {
        voltage = get_voltage();
        if (voltage < 6.2) {
        //    char buf[100];
        //    sprintf(buf, "%f\r\n", voltage);
        //    for(int i = 0; i < strlen(buf); ++i){
        //        usart_send_blocking(USART1, buf[i]);
        //    }
            usart_disable_tx_interrupt(USART2);
            usart_disable_rx_interrupt(USART2);
            gpio_clear(GPIOB, GPIO2 | GPIO12); // hold both GPS and XBEE in reset
            usart_disable_tx_interrupt(USART3);
            usart_disable_rx_interrupt(USART3);
            while(1); // and spin
        } 
        xbee_packet_t xbee_pkt;

        if (!gps_rx_queue.isEmpty()) {
            gps_packet_t gps_pkt = gps_rx_queue.dequeue();

            if (gps_pkt.payload[0] == GPS_ACK
                    || gps_pkt.payload[0] == GPS_NACK) { // ignore ACK and NACK
                continue;
            }

            ++skytraq_packet_id;
            // length/max_length + 1 more if remainder is bigger than 0
            uint8_t fragments_needed = (gps_pkt.length / MAX_FRAG_LENGTH)
                    + (((gps_pkt.length % MAX_FRAG_LENGTH) > 0) ? 1 : 0);

            xbee_build_header(xbee_pkt);
            //xbee_set_dest_addr(xbee_pkt, 0x0013A20040AD1B15, 0xFFFE);
            xbee_set_dest_addr(xbee_pkt, 0x000000000000FFFF, 0xFFFE);
            uint8_t current_fragment = 1;
            char* src_ptr = &(gps_pkt.payload[0]);

            while (current_fragment <= fragments_needed) {
                xbee_pkt.payload[INDEX_TYPE] = TYPE_SKYTRAQ;
                xbee_pkt.payload[INDEX_ID] = skytraq_packet_id;
                xbee_pkt.payload[INDEX_X] = current_fragment;
                xbee_pkt.payload[INDEX_Y] = fragments_needed;
                size_t bytes_to_copy = (
                        (current_fragment == fragments_needed) ?
                                (gps_pkt.length % MAX_FRAG_LENGTH) :
                                MAX_FRAG_LENGTH);
                memcpy(&(xbee_pkt.payload[INDEX_SKYTRAQ_START]), src_ptr,
                        bytes_to_copy);
                if (current_fragment == fragments_needed) {
                    xbee_pkt.length = INDEX_SKYTRAQ_START
                            + (gps_pkt.length % MAX_FRAG_LENGTH) + 1;
                } else {
                    xbee_pkt.length = INDEX_SKYTRAQ_START + MAX_FRAG_LENGTH + 1;
                }
                xbee_tx_queue.enqueue(xbee_pkt);
                usart_enable_tx_interrupt(USART3); // enable XBEE TX interrupt
                ++current_fragment;
                src_ptr += bytes_to_copy;
            }
        }

        if (!xbee_rx_queue.isEmpty()) {
            xbee_rx_queue.dequeue();
        }
    } // while(1)

    return 0;
}