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); } } }
void Board_FY20AP::com_fini(void) { usart_disable(USART1); usart_disable_rx_interrupt(USART1); usart_disable_tx_interrupt(USART1); }
/* ----------------------- 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); } }
/* Enable serial data reception by providing a callback function to accept received character and status. Disable by calling with argument NULL. Callback executes in interrupt context and must be short. */ void UART_StartReceive(usart_callback_t *isr_callback) { rx_callback = isr_callback; if (isr_callback) { nvic_enable_irq(get_nvic_irq(UART_CFG.uart)); usart_enable_rx_interrupt(UART_CFG.uart); } else { usart_disable_rx_interrupt(UART_CFG.uart); nvic_disable_irq(get_nvic_irq(UART_CFG.uart)); } }
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); }
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; }
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; }