static void rx_dma_callback(DMA_HANDLE handle, uint8_t status, void *arg) { /* * We are here because DMA completed, or UART reception stopped and * we think we have a packet in the buffer. */ perf_begin(pc_txns); /* disable UART DMA */ rCR3 &= ~(USART_CR3_DMAT | USART_CR3_DMAR); /* handle the received packet */ rx_handle_packet(); /* re-set DMA for reception first, so we are ready to receive before we start sending */ dma_reset(); /* send the reply to the just-processed request */ dma_packet.crc = 0; dma_packet.crc = crc_packet(&dma_packet); stm32_dmasetup( tx_dma, (uint32_t)&rDR, (uint32_t)&dma_packet, PKT_SIZE(dma_packet), DMA_CCR_DIR | DMA_CCR_MINC | DMA_CCR_PSIZE_8BITS | DMA_CCR_MSIZE_8BITS); stm32_dmastart(tx_dma, NULL, NULL, false); rCR3 |= USART_CR3_DMAT; perf_end(pc_txns); }
static void rx_dma_callback(DMA_HANDLE handle, uint8_t status, void *arg) { uint16_t sr = rSR; stm32_dmastop(rx_dma); stm32_dmastop(tx_dma); /* handle the received packet */ rx_handle_packet(); /* re-set DMA for reception first, so we are ready to receive before we start sending */ if (!(sr & SPI_SR_BSY)) { dma_reset(); } /* send the reply to the just-processed request */ dma_packet.crc = 0; dma_packet.crc = crc_packet(&dma_packet); stm32_dmasetup( tx_dma, (uint32_t)&rDR, (uint32_t)&dma_packet, PKT_SIZE(dma_packet), DMA_CCR_DIR | DMA_CCR_MINC | DMA_CCR_PSIZE_8BITS | DMA_CCR_MSIZE_8BITS | DMA_CCR_PRIMED); stm32_dmastart(tx_dma, NULL, NULL, false); perf_end(pc_txns); }
void PX4IO_serial_f4::_do_interrupt() { uint32_t sr = rSR; /* get UART status register */ (void)rDR; /* read DR to clear status */ if (sr & (USART_SR_ORE | /* overrun error - packet was too big for DMA or DMA was too slow */ USART_SR_NE | /* noise error - we have lost a byte due to noise */ USART_SR_FE)) { /* framing error - start/stop bit lost or line break */ /* * If we are in the process of listening for something, these are all fatal; * abort the DMA with an error. */ if (_rx_dma_status == _dma_status_waiting) { _abort_dma(); perf_count(_pc_uerrs); /* complete DMA as though in error */ _do_rx_dma_callback(DMA_STATUS_TEIF); return; } /* XXX we might want to use FE / line break as an out-of-band handshake ... handle it here */ /* don't attempt to handle IDLE if it's set - things went bad */ return; } if (sr & USART_SR_IDLE) { /* if there is DMA reception going on, this is a short packet */ if (_rx_dma_status == _dma_status_waiting) { /* verify that the received packet is complete */ size_t length = sizeof(*_current_packet) - stm32_dmaresidual(_rx_dma); if ((length < 1) || (length < PKT_SIZE(*_current_packet))) { perf_count(_pc_badidle); /* stop the receive DMA */ stm32_dmastop(_rx_dma); /* complete the short reception */ _do_rx_dma_callback(DMA_STATUS_TEIF); return; } perf_count(_pc_idle); /* stop the receive DMA */ stm32_dmastop(_rx_dma); /* complete the short reception */ _do_rx_dma_callback(DMA_STATUS_TCIF); } } }
static int serial_interrupt(int irq, void *context, FAR void *arg) { static bool abort_on_idle = false; uint32_t sr = rSR; /* get UART status register */ (void)rDR; /* required to clear any of the interrupt status that brought us here */ if (sr & (USART_SR_ORE | /* overrun error - packet was too big for DMA or DMA was too slow */ USART_SR_NE | /* noise error - we have lost a byte due to noise */ USART_SR_FE)) { /* framing error - start/stop bit lost or line break */ perf_count(pc_errors); if (sr & USART_SR_ORE) { perf_count(pc_ore); } if (sr & USART_SR_NE) { perf_count(pc_ne); } if (sr & USART_SR_FE) { perf_count(pc_fe); } /* send a line break - this will abort transmission/reception on the other end */ rCR1 |= USART_CR1_SBK; /* when the line goes idle, abort rather than look at the packet */ abort_on_idle = true; } if (sr & USART_SR_IDLE) { /* * If we saw an error, don't bother looking at the packet - it should have * been aborted by the sender and will definitely be bad. Get the DMA reconfigured * ready for their retry. */ if (abort_on_idle) { abort_on_idle = false; dma_reset(); return 0; } /* * The sender has stopped sending - this is probably the end of a packet. * Check the received length against the length in the header to see if * we have something that looks like a packet. */ unsigned length = sizeof(dma_packet) - stm32_dmaresidual(rx_dma); if ((length < 1) || (length < PKT_SIZE(dma_packet))) { /* it was too short - possibly truncated */ perf_count(pc_badidle); dma_reset(); return 0; } /* * Looks like we received a packet. Stop the DMA and go process the * packet. */ perf_count(pc_idle); stm32_dmastop(rx_dma); rx_dma_callback(rx_dma, DMA_STATUS_TCIF, NULL); } return 0; }
int PX4IO_serial_f4::_bus_exchange(IOPacket *_packet) { _current_packet = _packet; /* clear any lingering error status */ (void)rSR; (void)rDR; /* start RX DMA */ perf_begin(_pc_txns); perf_begin(_pc_dmasetup); /* DMA setup time ~3µs */ _rx_dma_status = _dma_status_waiting; /* * Note that we enable circular buffer mode as a workaround for * there being no API to disable the DMA FIFO. We need direct mode * because otherwise when the line idle interrupt fires there * will be packet bytes still in the DMA FIFO, and we will assume * that the idle was spurious. * * XXX this should be fixed with a NuttX change. */ stm32_dmasetup( _rx_dma, PX4IO_SERIAL_BASE + STM32_USART_DR_OFFSET, reinterpret_cast<uint32_t>(_current_packet), sizeof(*_current_packet), DMA_SCR_CIRC | /* XXX see note above */ DMA_SCR_DIR_P2M | DMA_SCR_MINC | DMA_SCR_PSIZE_8BITS | DMA_SCR_MSIZE_8BITS | DMA_SCR_PBURST_SINGLE | DMA_SCR_MBURST_SINGLE); stm32_dmastart(_rx_dma, _dma_callback, this, false); rCR3 |= USART_CR3_DMAR; /* start TX DMA - no callback if we also expect a reply */ /* DMA setup time ~3µs */ stm32_dmasetup( _tx_dma, PX4IO_SERIAL_BASE + STM32_USART_DR_OFFSET, reinterpret_cast<uint32_t>(_current_packet), PKT_SIZE(*_current_packet), DMA_SCR_DIR_M2P | DMA_SCR_MINC | DMA_SCR_PSIZE_8BITS | DMA_SCR_MSIZE_8BITS | DMA_SCR_PBURST_SINGLE | DMA_SCR_MBURST_SINGLE); stm32_dmastart(_tx_dma, nullptr, nullptr, false); //rCR1 &= ~USART_CR1_TE; //rCR1 |= USART_CR1_TE; rCR3 |= USART_CR3_DMAT; perf_end(_pc_dmasetup); /* compute the deadline for a 10ms timeout */ struct timespec abstime; clock_gettime(CLOCK_REALTIME, &abstime); abstime.tv_nsec += 10 * 1000 * 1000; if (abstime.tv_nsec >= 1000 * 1000 * 1000) { abstime.tv_sec++; abstime.tv_nsec -= 1000 * 1000 * 1000; } /* wait for the transaction to complete - 64 bytes @ 1.5Mbps ~426µs */ int ret; for (;;) { ret = sem_timedwait(&_completion_semaphore, &abstime); if (ret == OK) { /* check for DMA errors */ if (_rx_dma_status & DMA_STATUS_TEIF) { perf_count(_pc_dmaerrs); ret = -EIO; break; } /* check packet CRC - corrupt packet errors mean IO receive CRC error */ uint8_t crc = _current_packet->crc; _current_packet->crc = 0; if ((crc != crc_packet(_current_packet)) || (PKT_CODE(*_current_packet) == PKT_CODE_CORRUPT)) { perf_count(_pc_crcerrs); ret = -EIO; break; } /* successful txn (may still be reporting an error) */ break; } if (errno == ETIMEDOUT) { /* something has broken - clear out any partial DMA state and reconfigure */ _abort_dma(); perf_count(_pc_timeouts); perf_cancel(_pc_txns); /* don't count this as a transaction */ break; } /* we might? see this for EINTR */ syslog(LOG_ERR, "unexpected ret %d/%d\n", ret, errno); } /* reset DMA status */ _rx_dma_status = _dma_status_inactive; /* update counters */ perf_end(_pc_txns); return ret; }