24
24
#include <string.h>
25
25
26
26
#include "cmsis.h"
27
+ #include "mbed_power_mgmt.h"
27
28
#include "pinmap.h"
28
29
#include "fsl_uart.h"
29
30
#include "peripheral_clock_defines.h"
@@ -271,7 +272,7 @@ int serial_getc(serial_t *obj)
271
272
272
273
void serial_putc (serial_t * obj , int c )
273
274
{
274
- UART_Type * base = uart_addrs [obj -> index ];
275
+ UART_Type * base = uart_addrs [obj -> serial . index ];
275
276
276
277
while (!serial_writable (obj ));
277
278
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
550
551
/* Start the transfer */
551
552
serial_send_asynch (obj );
552
553
554
+ /* Can't enter deep sleep as long as UART transmit is active */
555
+ sleep_manager_lock_deep_sleep ();
556
+
553
557
return 0 ;
554
558
}
555
559
@@ -615,6 +619,9 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt
615
619
616
620
/* Start the transfer */
617
621
serial_receive_asynch (obj );
622
+
623
+ /* Can't enter deep sleep as long as UART transfer is active */
624
+ sleep_manager_lock_deep_sleep ();
618
625
}
619
626
620
627
uint8_t serial_tx_active (serial_t * obj )
@@ -647,11 +654,15 @@ int serial_irq_handler_asynch(serial_t *obj)
647
654
if ((obj -> serial .txstate != kUART_TxIdle ) && (obj -> serial .uart_dma_handle .txState == kUART_TxIdle )) {
648
655
obj -> serial .txstate = kUART_TxIdle ;
649
656
status |= SERIAL_EVENT_TX_COMPLETE ;
657
+ /* Transmit is complete, re-enable entry to deep sleep mode */
658
+ sleep_manager_unlock_deep_sleep ();
650
659
}
651
660
652
661
if ((obj -> serial .rxstate != kUART_RxIdle ) && (obj -> serial .uart_dma_handle .rxState == kUART_RxIdle )) {
653
662
obj -> serial .rxstate = kUART_RxIdle ;
654
663
status |= SERIAL_EVENT_RX_COMPLETE ;
664
+ /* Receive is complete, re-enable entry to deep sleep mode */
665
+ sleep_manager_unlock_deep_sleep ();
655
666
}
656
667
657
668
/* Release the dma channels if they were opportunistically allocated */
@@ -668,11 +679,15 @@ int serial_irq_handler_asynch(serial_t *obj)
668
679
if ((obj -> serial .txstate != kUART_TxIdle ) && (obj -> serial .uart_transfer_handle .txState == kUART_TxIdle )) {
669
680
obj -> serial .txstate = kUART_TxIdle ;
670
681
status |= SERIAL_EVENT_TX_COMPLETE ;
682
+ /* Transmit is complete, re-enable entry to deep sleep mode */
683
+ sleep_manager_unlock_deep_sleep ();
671
684
}
672
685
673
686
if ((obj -> serial .rxstate != kUART_RxIdle ) && (obj -> serial .uart_transfer_handle .rxState == kUART_RxIdle )) {
674
687
obj -> serial .rxstate = kUART_RxIdle ;
675
688
status |= SERIAL_EVENT_RX_COMPLETE ;
689
+ /* Receive is complete, re-enable entry to deep sleep mode */
690
+ sleep_manager_unlock_deep_sleep ();
676
691
}
677
692
}
678
693
#if 0
@@ -704,6 +719,11 @@ int serial_irq_handler_asynch(serial_t *obj)
704
719
705
720
void serial_tx_abort_asynch (serial_t * obj )
706
721
{
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
+
707
727
if (obj -> serial .uartDmaRx .dmaUsageState == DMA_USAGE_ALLOCATED || obj -> serial .uartDmaRx .dmaUsageState == DMA_USAGE_TEMPORARY_ALLOCATED ) {
708
728
UART_TransferAbortSendEDMA (uart_addrs [obj -> serial .index ], & obj -> serial .uart_dma_handle );
709
729
/* Release the dma channels if they were opportunistically allocated */
@@ -718,10 +738,20 @@ void serial_tx_abort_asynch(serial_t *obj)
718
738
} else {
719
739
UART_TransferAbortSend (uart_addrs [obj -> serial .index ], & obj -> serial .uart_transfer_handle );
720
740
}
741
+
742
+ obj -> serial .txstate = kUART_TxIdle ;
743
+
744
+ /* Re-enable entry to deep sleep mode */
745
+ sleep_manager_unlock_deep_sleep ();
721
746
}
722
747
723
748
void serial_rx_abort_asynch (serial_t * obj )
724
749
{
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
+
725
755
if (obj -> serial .uartDmaRx .dmaUsageState == DMA_USAGE_ALLOCATED || obj -> serial .uartDmaRx .dmaUsageState == DMA_USAGE_TEMPORARY_ALLOCATED ) {
726
756
UART_TransferAbortReceiveEDMA (uart_addrs [obj -> serial .index ], & obj -> serial .uart_dma_handle );
727
757
/* Release the dma channels if they were opportunistically allocated */
@@ -736,6 +766,11 @@ void serial_rx_abort_asynch(serial_t *obj)
736
766
} else {
737
767
UART_TransferAbortReceive (uart_addrs [obj -> serial .index ], & obj -> serial .uart_transfer_handle );
738
768
}
769
+
770
+ obj -> serial .rxstate = kUART_RxIdle ;
771
+
772
+ /* Re-enable entry to deep sleep mode */
773
+ sleep_manager_unlock_deep_sleep ();
739
774
}
740
775
741
776
#endif
0 commit comments