Skip to content

Commit 54537df

Browse files
committed
K64F: Update UART aynchronous API for tickless implementation
Do not allow entry to deep sleep mode when UART transfer is active Signed-off-by: Mahesh Mahadevan <[email protected]>
1 parent 75d8c0f commit 54537df

File tree

1 file changed

+36
-1
lines changed
  • targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F

1 file changed

+36
-1
lines changed

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/serial_api.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <string.h>
2525

2626
#include "cmsis.h"
27+
#include "mbed_power_mgmt.h"
2728
#include "pinmap.h"
2829
#include "fsl_uart.h"
2930
#include "peripheral_clock_defines.h"
@@ -271,7 +272,7 @@ int serial_getc(serial_t *obj)
271272

272273
void serial_putc(serial_t *obj, int c)
273274
{
274-
UART_Type *base = uart_addrs[obj->index];
275+
UART_Type *base = uart_addrs[obj->serial.index];
275276

276277
while (!serial_writable(obj));
277278
UART_WriteByte(base, (uint8_t)c);
@@ -550,6 +551,9 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
550551
/* Start the transfer */
551552
serial_send_asynch(obj);
552553

554+
/* Can't enter deep sleep as long as UART transmit is active */
555+
sleep_manager_lock_deep_sleep();
556+
553557
return 0;
554558
}
555559

@@ -615,6 +619,9 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt
615619

616620
/* Start the transfer */
617621
serial_receive_asynch(obj);
622+
623+
/* Can't enter deep sleep as long as UART transfer is active */
624+
sleep_manager_lock_deep_sleep();
618625
}
619626

620627
uint8_t serial_tx_active(serial_t *obj)
@@ -647,11 +654,15 @@ int serial_irq_handler_asynch(serial_t *obj)
647654
if ((obj->serial.txstate != kUART_TxIdle) && (obj->serial.uart_dma_handle.txState == kUART_TxIdle)) {
648655
obj->serial.txstate = kUART_TxIdle;
649656
status |= SERIAL_EVENT_TX_COMPLETE;
657+
/* Transmit is complete, re-enable entry to deep sleep mode */
658+
sleep_manager_unlock_deep_sleep();
650659
}
651660

652661
if ((obj->serial.rxstate != kUART_RxIdle) && (obj->serial.uart_dma_handle.rxState == kUART_RxIdle)) {
653662
obj->serial.rxstate = kUART_RxIdle;
654663
status |= SERIAL_EVENT_RX_COMPLETE;
664+
/* Receive is complete, re-enable entry to deep sleep mode */
665+
sleep_manager_unlock_deep_sleep();
655666
}
656667

657668
/* Release the dma channels if they were opportunistically allocated */
@@ -668,11 +679,15 @@ int serial_irq_handler_asynch(serial_t *obj)
668679
if ((obj->serial.txstate != kUART_TxIdle) && (obj->serial.uart_transfer_handle.txState == kUART_TxIdle)) {
669680
obj->serial.txstate = kUART_TxIdle;
670681
status |= SERIAL_EVENT_TX_COMPLETE;
682+
/* Transmit is complete, re-enable entry to deep sleep mode */
683+
sleep_manager_unlock_deep_sleep();
671684
}
672685

673686
if ((obj->serial.rxstate != kUART_RxIdle) && (obj->serial.uart_transfer_handle.rxState == kUART_RxIdle)) {
674687
obj->serial.rxstate = kUART_RxIdle;
675688
status |= SERIAL_EVENT_RX_COMPLETE;
689+
/* Receive is complete, re-enable entry to deep sleep mode */
690+
sleep_manager_unlock_deep_sleep();
676691
}
677692
}
678693
#if 0
@@ -704,6 +719,11 @@ int serial_irq_handler_asynch(serial_t *obj)
704719

705720
void serial_tx_abort_asynch(serial_t *obj)
706721
{
722+
// If we're not currently transferring, then there's nothing to do here
723+
if (serial_tx_active(obj) == 0) {
724+
return;
725+
}
726+
707727
if (obj->serial.uartDmaRx.dmaUsageState == DMA_USAGE_ALLOCATED || obj->serial.uartDmaRx.dmaUsageState == DMA_USAGE_TEMPORARY_ALLOCATED) {
708728
UART_TransferAbortSendEDMA(uart_addrs[obj->serial.index], &obj->serial.uart_dma_handle);
709729
/* Release the dma channels if they were opportunistically allocated */
@@ -718,10 +738,20 @@ void serial_tx_abort_asynch(serial_t *obj)
718738
} else {
719739
UART_TransferAbortSend(uart_addrs[obj->serial.index], &obj->serial.uart_transfer_handle);
720740
}
741+
742+
obj->serial.txstate = kUART_TxIdle;
743+
744+
/* Re-enable entry to deep sleep mode */
745+
sleep_manager_unlock_deep_sleep();
721746
}
722747

723748
void serial_rx_abort_asynch(serial_t *obj)
724749
{
750+
// If we're not currently transferring, then there's nothing to do here
751+
if (serial_rx_active(obj) == 0) {
752+
return;
753+
}
754+
725755
if (obj->serial.uartDmaRx.dmaUsageState == DMA_USAGE_ALLOCATED || obj->serial.uartDmaRx.dmaUsageState == DMA_USAGE_TEMPORARY_ALLOCATED) {
726756
UART_TransferAbortReceiveEDMA(uart_addrs[obj->serial.index], &obj->serial.uart_dma_handle);
727757
/* Release the dma channels if they were opportunistically allocated */
@@ -736,6 +766,11 @@ void serial_rx_abort_asynch(serial_t *obj)
736766
} else {
737767
UART_TransferAbortReceive(uart_addrs[obj->serial.index], &obj->serial.uart_transfer_handle);
738768
}
769+
770+
obj->serial.rxstate = kUART_RxIdle;
771+
772+
/* Re-enable entry to deep sleep mode */
773+
sleep_manager_unlock_deep_sleep();
739774
}
740775

741776
#endif

0 commit comments

Comments
 (0)