Skip to content

Commit d68d800

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 72e1f41 commit d68d800

File tree

1 file changed

+35
-0
lines changed
  • targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F

1 file changed

+35
-0
lines changed

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/serial_api.c

Lines changed: 35 additions & 0 deletions
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"
@@ -543,6 +544,9 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
543544
/* Start the transfer */
544545
serial_send_asynch(obj);
545546

547+
/* Can't enter deep sleep as long as UART transmit is active */
548+
sleep_manager_lock_deep_sleep();
549+
546550
return 0;
547551
}
548552

@@ -608,6 +612,9 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt
608612

609613
/* Start the transfer */
610614
serial_receive_asynch(obj);
615+
616+
/* Can't enter deep sleep as long as UART transfer is active */
617+
sleep_manager_lock_deep_sleep();
611618
}
612619

613620
uint8_t serial_tx_active(serial_t *obj)
@@ -640,11 +647,15 @@ int serial_irq_handler_asynch(serial_t *obj)
640647
if ((obj->serial.txstate != kUART_TxIdle) && (obj->serial.uart_dma_handle.txState == kUART_TxIdle)) {
641648
obj->serial.txstate = kUART_TxIdle;
642649
status |= SERIAL_EVENT_TX_COMPLETE;
650+
/* Transmit is complete, re-enable entry to deep sleep mode */
651+
sleep_manager_unlock_deep_sleep();
643652
}
644653

645654
if ((obj->serial.rxstate != kUART_RxIdle) && (obj->serial.uart_dma_handle.rxState == kUART_RxIdle)) {
646655
obj->serial.rxstate = kUART_RxIdle;
647656
status |= SERIAL_EVENT_RX_COMPLETE;
657+
/* Receive is complete, re-enable entry to deep sleep mode */
658+
sleep_manager_unlock_deep_sleep();
648659
}
649660

650661
/* Release the dma channels if they were opportunistically allocated */
@@ -661,11 +672,15 @@ int serial_irq_handler_asynch(serial_t *obj)
661672
if ((obj->serial.txstate != kUART_TxIdle) && (obj->serial.uart_transfer_handle.txState == kUART_TxIdle)) {
662673
obj->serial.txstate = kUART_TxIdle;
663674
status |= SERIAL_EVENT_TX_COMPLETE;
675+
/* Transmit is complete, re-enable entry to deep sleep mode */
676+
sleep_manager_unlock_deep_sleep();
664677
}
665678

666679
if ((obj->serial.rxstate != kUART_RxIdle) && (obj->serial.uart_transfer_handle.rxState == kUART_RxIdle)) {
667680
obj->serial.rxstate = kUART_RxIdle;
668681
status |= SERIAL_EVENT_RX_COMPLETE;
682+
/* Receive is complete, re-enable entry to deep sleep mode */
683+
sleep_manager_unlock_deep_sleep();
669684
}
670685
}
671686
#if 0
@@ -697,6 +712,11 @@ int serial_irq_handler_asynch(serial_t *obj)
697712

698713
void serial_tx_abort_asynch(serial_t *obj)
699714
{
715+
// If we're not currently transferring, then there's nothing to do here
716+
if (serial_tx_active(obj) == 0) {
717+
return;
718+
}
719+
700720
if (obj->serial.uartDmaRx.dmaUsageState == DMA_USAGE_ALLOCATED || obj->serial.uartDmaRx.dmaUsageState == DMA_USAGE_TEMPORARY_ALLOCATED) {
701721
UART_TransferAbortSendEDMA(uart_addrs[obj->serial.index], &obj->serial.uart_dma_handle);
702722
/* Release the dma channels if they were opportunistically allocated */
@@ -711,10 +731,20 @@ void serial_tx_abort_asynch(serial_t *obj)
711731
} else {
712732
UART_TransferAbortSend(uart_addrs[obj->serial.index], &obj->serial.uart_transfer_handle);
713733
}
734+
735+
obj->serial.txstate = kUART_TxIdle;
736+
737+
/* Re-enable entry to deep sleep mode */
738+
sleep_manager_unlock_deep_sleep();
714739
}
715740

716741
void serial_rx_abort_asynch(serial_t *obj)
717742
{
743+
// If we're not currently transferring, then there's nothing to do here
744+
if (serial_rx_active(obj) == 0) {
745+
return;
746+
}
747+
718748
if (obj->serial.uartDmaRx.dmaUsageState == DMA_USAGE_ALLOCATED || obj->serial.uartDmaRx.dmaUsageState == DMA_USAGE_TEMPORARY_ALLOCATED) {
719749
UART_TransferAbortReceiveEDMA(uart_addrs[obj->serial.index], &obj->serial.uart_dma_handle);
720750
/* Release the dma channels if they were opportunistically allocated */
@@ -729,6 +759,11 @@ void serial_rx_abort_asynch(serial_t *obj)
729759
} else {
730760
UART_TransferAbortReceive(uart_addrs[obj->serial.index], &obj->serial.uart_transfer_handle);
731761
}
762+
763+
obj->serial.rxstate = kUART_RxIdle;
764+
765+
/* Re-enable entry to deep sleep mode */
766+
sleep_manager_unlock_deep_sleep();
732767
}
733768

734769
void serial_wait_tx_complete(uint32_t uart_index)

0 commit comments

Comments
 (0)