Index: asp3_tinet_ecnl_arm/trunk/asp3_dcre/mbed/targets/TARGET_RENESAS/TARGET_RZA1XX/serial_api.c
===================================================================
--- asp3_tinet_ecnl_arm/trunk/asp3_dcre/mbed/targets/TARGET_RENESAS/TARGET_RZ_A1H/serial_api.c	(revision 352)
+++ asp3_tinet_ecnl_arm/trunk/asp3_dcre/mbed/targets/TARGET_RENESAS/TARGET_RZA1XX/serial_api.c	(revision 374)
@@ -22,42 +22,48 @@
 #include "serial_api.h"
 #include "cmsis.h"
-#include "pinmap.h"
+#include "PeripheralPins.h"
 #include "gpio_api.h"
-
-#include "scif_iodefine.h"
-#include "cpg_iodefine.h"
+#include "RZ_A1_Init.h"
+
+#include "iodefine.h"
+#include "mbed_drv_cfg.h"
+#include "mbed_critical.h"
 
 /******************************************************************************
  * INITIALIZATION
  ******************************************************************************/
-#define PCLK (66666666)     // Define the peripheral clock P1 frequency.
-
+#if defined(TARGET_RZA1H)
 #define UART_NUM    8
+#else
+#define UART_NUM    5
+#endif
 #define IRQ_NUM     4
 
 static void uart0_tx_irq(void);
+static void uart0_rx_irq(void);
+static void uart0_er_irq(void);
 static void uart1_tx_irq(void);
+static void uart1_rx_irq(void);
+static void uart1_er_irq(void);
 static void uart2_tx_irq(void);
+static void uart2_rx_irq(void);
+static void uart2_er_irq(void);
 static void uart3_tx_irq(void);
+static void uart3_rx_irq(void);
+static void uart3_er_irq(void);
 static void uart4_tx_irq(void);
+static void uart4_rx_irq(void);
+static void uart4_er_irq(void);
+#if defined(TARGET_RZA1H)
 static void uart5_tx_irq(void);
+static void uart5_rx_irq(void);
+static void uart5_er_irq(void);
 static void uart6_tx_irq(void);
+static void uart6_rx_irq(void);
+static void uart6_er_irq(void);
 static void uart7_tx_irq(void);
-static void uart0_rx_irq(void);
-static void uart1_rx_irq(void);
-static void uart2_rx_irq(void);
-static void uart3_rx_irq(void);
-static void uart4_rx_irq(void);
-static void uart5_rx_irq(void);
-static void uart6_rx_irq(void);
 static void uart7_rx_irq(void);
-static void uart0_er_irq(void);
-static void uart1_er_irq(void);
-static void uart2_er_irq(void);
-static void uart3_er_irq(void);
-static void uart4_er_irq(void);
-static void uart5_er_irq(void);
-static void uart6_er_irq(void);
 static void uart7_er_irq(void);
+#endif
 
 static void serial_put_done(serial_t *obj);
@@ -65,59 +71,6 @@
 static void serial_irq_err_set(serial_t *obj, uint32_t enable);
 
-static const PinMap PinMap_UART_TX[] = {
-    {P2_14 , UART0, 6},
-    {P2_5  , UART1, 6},
-    {P4_12 , UART1, 7},
-    {P6_3  , UART2, 7},
-    {P4_14 , UART2, 7},
-    {P5_3  , UART3, 5},
-    {P8_8  , UART3, 7},
-    {P5_0  , UART4, 5},
-    {P8_14 , UART4, 7},
-    {P8_13 , UART5, 5},
-    {P11_10, UART5, 3},
-    {P6_6  , UART5, 5},
-    {P5_6  , UART6, 5},
-    {P11_1 , UART6, 4},
-    {P7_4  , UART7, 4},
-    {NC    , NC   , 0}
-};
-
-static const PinMap PinMap_UART_RX[] = {
-    {P2_15 , UART0, 6},
-    {P2_6  , UART1, 6},
-    {P4_13 , UART1, 7},
-    {P6_2  , UART2, 7},
-    {P4_15 , UART2, 7},
-    {P5_4  , UART3, 5},
-    {P8_9  , UART3, 7},
-    {P5_1  , UART4, 5},
-    {P8_15 , UART4, 7},
-    {P8_11 , UART5, 5},
-    {P11_11, UART5, 3},
-    {P6_7  , UART5, 5},
-    {P5_7  , UART6, 5},
-    {P11_2 , UART6, 4},
-    {P7_5  , UART7, 4},
-    {NC    , NC   , 0}
-};
-
-static const PinMap PinMap_UART_CTS[] = {
-    {P2_3  , UART1, 6},
-    {P11_7 , UART5, 3},
-    {P7_6  , UART7, 4},
-    {NC    , NC   , 0}
-};
-static const PinMap PinMap_UART_RTS[] = {
-    {P2_7  , UART1, 6},
-    {P11_8 , UART5, 3},
-    {P7_7  , UART7, 4},
-    {NC    , NC   , 0}
-};
-
-
-
 static const struct st_scif *SCIF[] = SCIF_ADDRESS_LIST;
-static uart_irq_handler irq_handler;
+//static uart_irq_handler irq_handler;
 
 int stdio_uart_inited = 0;
@@ -125,7 +78,7 @@
 
 struct serial_global_data_s {
+    /*add*/uart_irq_handler irq_handler;
     uint32_t serial_irq_id;
     gpio_t sw_rts, sw_cts;
-    uint8_t rx_irq_set_flow, rx_irq_set_api;
     serial_t *tranferring_obj, *receiving_obj;
     uint32_t async_tx_callback, async_rx_callback;
@@ -141,7 +94,9 @@
     {SCIFRXI3_IRQn, SCIFTXI3_IRQn, SCIFBRI3_IRQn, SCIFERI3_IRQn},
     {SCIFRXI4_IRQn, SCIFTXI4_IRQn, SCIFBRI4_IRQn, SCIFERI4_IRQn},
+#if defined(TARGET_RZA1H)
     {SCIFRXI5_IRQn, SCIFTXI5_IRQn, SCIFBRI5_IRQn, SCIFERI5_IRQn},
     {SCIFRXI6_IRQn, SCIFTXI6_IRQn, SCIFBRI6_IRQn, SCIFERI6_IRQn},
-    {SCIFRXI7_IRQn, SCIFTXI7_IRQn, SCIFBRI7_IRQn, SCIFERI7_IRQn}
+    {SCIFRXI7_IRQn, SCIFTXI7_IRQn, SCIFBRI7_IRQn, SCIFERI7_IRQn},
+#endif
 };
 
@@ -152,7 +107,9 @@
     {uart3_rx_irq, uart3_tx_irq, uart3_er_irq, uart3_er_irq},
     {uart4_rx_irq, uart4_tx_irq, uart4_er_irq, uart4_er_irq},
+#if defined(TARGET_RZA1H)
     {uart5_rx_irq, uart5_tx_irq, uart5_er_irq, uart5_er_irq},
     {uart6_rx_irq, uart6_tx_irq, uart6_er_irq, uart6_er_irq},
-    {uart7_rx_irq, uart7_tx_irq, uart7_er_irq, uart7_er_irq}
+    {uart7_rx_irq, uart7_tx_irq, uart7_er_irq, uart7_er_irq},
+#endif
 };
 
@@ -163,7 +120,9 @@
     &SCSCR_3,
     &SCSCR_4,
+#if defined(TARGET_RZA1H)
     &SCSCR_5,
     &SCSCR_6,
     &SCSCR_7,
+#endif
 };
 
@@ -174,7 +133,9 @@
     &SCFSR_3,
     &SCFSR_4,
+#if defined(TARGET_RZA1H)
     &SCFSR_5,
     &SCFSR_6,
     &SCFSR_7,
+#endif
 };
 
@@ -192,30 +153,5 @@
     obj->serial.uart = (struct st_scif *)SCIF[uart];
     // enable power
-    switch (uart) {
-    case UART0:
-        CPG.STBCR4 &= ~(1 <<  7);
-        break;
-    case UART1:
-        CPG.STBCR4 &= ~(1 <<  6);
-        break;
-    case UART2:
-        CPG.STBCR4 &= ~(1 <<  5);
-        break;
-    case UART3:
-        CPG.STBCR4 &= ~(1 <<  4);
-        break;
-    case UART4:
-        CPG.STBCR4 &= ~(1 <<  3);
-        break;
-    case UART5:
-        CPG.STBCR4 &= ~(1 <<  2);
-        break;
-    case UART6:
-        CPG.STBCR4 &= ~(1 <<  1);
-        break;
-    case UART7:
-        CPG.STBCR4 &= ~(1 <<  0);
-        break;
-    }
+    CPG.STBCR4 &= ~(1 << (7 - uart));
     dummy = CPG.STBCR4;
 
@@ -227,5 +163,5 @@
     /* ---- FIFO control register (SCFCR) setting ---- */
     /* Transmit FIFO reset & Receive FIFO data register reset */
-    obj->serial.uart->SCFCR = 0x0006;
+    obj->serial.uart->SCFCR = 0x0006u;
 
     /* ---- Serial status register (SCFSR) setting ---- */
@@ -265,30 +201,6 @@
     pinmap_pinout(rx, PinMap_UART_RX);
 
-    switch (uart) {
-    case UART0:
-        obj->serial.index = 0;
-        break;
-    case UART1:
-        obj->serial.index = 1;
-        break;
-    case UART2:
-        obj->serial.index = 2;
-        break;
-    case UART3:
-        obj->serial.index = 3;
-        break;
-    case UART4:
-        obj->serial.index = 4;
-        break;
-    case UART5:
-        obj->serial.index = 5;
-        break;
-    case UART6:
-        obj->serial.index = 6;
-        break;
-    case UART7:
-        obj->serial.index = 7;
-        break;
-    }
+    obj->serial.index = uart;
+
     uart_data[obj->serial.index].sw_rts.pin = NC;
     uart_data[obj->serial.index].sw_cts.pin = NC;
@@ -313,49 +225,41 @@
 // set the baud rate, taking in to account the current SystemFrequency
 void serial_baud(serial_t *obj, int baudrate) {
-    uint16_t DL;
-
-    obj->serial.uart->SCSMR &= ~0x0003;
-
-    if (baudrate > 32552) {
+    uint32_t pclk_base;
+    uint32_t bgdm = 1;
+    uint32_t cks = 0;
+    uint32_t DL;
+
+    if (RZ_A1_IsClockMode0() == false) {
+        pclk_base = CM1_RENESAS_RZ_A1_P1_CLK;
+    } else {
+        pclk_base = CM0_RENESAS_RZ_A1_P1_CLK;
+    }
+
+    if (baudrate > (int)(pclk_base / 0x800)) {
+        obj->serial.uart->SCSMR &= ~0x0003;
         obj->serial.uart->SCEMR = 0x0081;  // BGDM = 1, ABCS = 1
-        DL = PCLK / (8 * baudrate);
+        DL = (pclk_base + (4 * baudrate)) / (8 * baudrate);  // Rounding
         if (DL > 0) {
             DL--;
         }
         obj->serial.uart->SCBRR = (uint8_t)DL;
-    } else if (baudrate > 16276) {
-        obj->serial.uart->SCEMR = 0x0080;  // BGDM = 1
-        obj->serial.uart->SCBRR = PCLK / (16 * baudrate) - 1;
-    } else if (baudrate > 8138) {
-        obj->serial.uart->SCEMR = 0x0000;
-        obj->serial.uart->SCBRR = PCLK / (32 * baudrate) - 1;
-    } else if (baudrate > 4169) {
-        obj->serial.uart->SCSMR |= 0x0001;
-        obj->serial.uart->SCEMR = 0x0080;  // BGDM = 1
-        obj->serial.uart->SCBRR = PCLK / (64 * baudrate) - 1;
-    } else if (baudrate > 2034) {
-        obj->serial.uart->SCSMR |= 0x0001;
-        obj->serial.uart->SCEMR = 0x0000;
-        obj->serial.uart->SCBRR = PCLK / (128 * baudrate) - 1;
-    } else if (baudrate > 1017) {
-        obj->serial.uart->SCSMR |= 0x0002;
-        obj->serial.uart->SCEMR = 0x0080;  // BGDM = 1
-        obj->serial.uart->SCBRR = PCLK / (256 * baudrate) - 1;
-    } else if (baudrate > 508) {
-        obj->serial.uart->SCSMR |= 0x0002;
-        obj->serial.uart->SCEMR = 0x0000;
-        obj->serial.uart->SCBRR = PCLK / (512 * baudrate) - 1;
-    } else if (baudrate > 254) {
-        obj->serial.uart->SCSMR |= 0x0003;
-        obj->serial.uart->SCEMR = 0x0080;  // BGDM = 1
-        obj->serial.uart->SCBRR = PCLK / (1024 * baudrate) - 1;
-    } else if (baudrate > 127) {
-        obj->serial.uart->SCSMR |= 0x0003;
-        obj->serial.uart->SCEMR = 0x0000;
-        obj->serial.uart->SCBRR = PCLK / (2048 * baudrate) - 1;
-    } else {
+    } else if (baudrate < (int)(pclk_base / 0x80000)) {
         obj->serial.uart->SCSMR |= 0x0003;
         obj->serial.uart->SCEMR = 0x0000;
         obj->serial.uart->SCBRR = 0xFFu;
+    } else {
+        DL = (pclk_base + (8 * baudrate)) / (16 * baudrate);  // Rounding
+        while (DL > 256) {
+            DL >>= 1;
+            if (bgdm == 1) {
+                bgdm = 0;
+            } else {
+                bgdm = 1;
+                cks++;
+            }
+        }
+        obj->serial.uart->SCSMR = (obj->serial.uart->SCSMR & ~0x0003) | (uint8_t)cks;
+        obj->serial.uart->SCEMR = (uint8_t)(bgdm << 7);
+        obj->serial.uart->SCBRR = (uint8_t)(DL - 1);
     }
 }
@@ -399,9 +303,8 @@
     }
 
-    obj->serial.uart->SCSMR = (obj->serial.uart->SCSMR & ~0x0078)
-                       | (data_bits       << 6)
-                       | (parity_enable   << 5)
-                       | (parity_select   << 4)
-                       | (stop_bits       << 3);
+    obj->serial.uart->SCSMR = data_bits   << 6
+                       | parity_enable    << 5
+                       | parity_select    << 4
+                       | stop_bits        << 3;
 }
 
@@ -442,5 +345,6 @@
     }
     
-    irq_handler(uart_data[index].serial_irq_id, TxIrq);
+    if (uart_data[index].irq_handler != NULL)
+        uart_data[index].irq_handler(uart_data[index].serial_irq_id, TxIrq);
 }
 
@@ -506,10 +410,11 @@
     }
     
-    irq_handler(uart_data[index].serial_irq_id, RxIrq);
+    if (uart_data[index].irq_handler != NULL)
+        uart_data[index].irq_handler(uart_data[index].serial_irq_id, RxIrq);
 }
 
 static void uart_err_irq(IRQn_Type irq_num, uint32_t index) {
     serial_t *obj = uart_data[index].receiving_obj;
-    int was_masked, err_read;
+    int err_read;
     
     if (obj) {
@@ -526,9 +431,5 @@
         serial_rx_abort_asynch(obj);
         
-#if defined ( __ICCARM__ )
-        was_masked = __disable_irq_iar();
-#else
-        was_masked = __disable_irq();
-#endif /* __ICCARM__ */
+        core_util_critical_section_enter();
         if (obj->serial.uart->SCFSR & 0x93) {
             err_read = obj->serial.uart->SCFSR;
@@ -538,96 +439,92 @@
             obj->serial.uart->SCLSR = 0;
         }
-        if (!was_masked) {
-            __enable_irq();
-        }
-    }
-}
-
-/* TX handler */
+        core_util_critical_section_exit();
+        }
+    }
+
 static void uart0_tx_irq(void)  {
     uart_tx_irq(SCIFTXI0_IRQn, 0);
 }
-static void uart1_tx_irq(void)  {
-    uart_tx_irq(SCIFTXI1_IRQn, 1);
-}
-static void uart2_tx_irq(void)  {
-    uart_tx_irq(SCIFTXI2_IRQn, 2);
-}
-static void uart3_tx_irq(void)  {
-    uart_tx_irq(SCIFTXI3_IRQn, 3);
-}
-static void uart4_tx_irq(void)  {
-    uart_tx_irq(SCIFTXI4_IRQn, 4);
-}
-static void uart5_tx_irq(void)  {
-    uart_tx_irq(SCIFTXI5_IRQn, 5);
-}
-static void uart6_tx_irq(void)  {
-    uart_tx_irq(SCIFTXI6_IRQn, 6);
-}
-static void uart7_tx_irq(void)  {
-    uart_tx_irq(SCIFTXI7_IRQn, 7);
-}
-/* RX handler */
 static void uart0_rx_irq(void)  {
     uart_rx_irq(SCIFRXI0_IRQn, 0);
 }
+static void uart0_er_irq(void) {
+    uart_err_irq(SCIFERI0_IRQn, 0);
+}
+
+static void uart1_tx_irq(void) {
+    uart_tx_irq(SCIFTXI1_IRQn, 1);
+}
 static void uart1_rx_irq(void)  {
     uart_rx_irq(SCIFRXI1_IRQn, 1);
 }
+static void uart1_er_irq(void) {
+    uart_err_irq(SCIFERI1_IRQn, 1);
+}
+
+static void uart2_tx_irq(void) {
+    uart_tx_irq(SCIFTXI2_IRQn, 2);
+}
 static void uart2_rx_irq(void)  {
     uart_rx_irq(SCIFRXI2_IRQn, 2);
 }
+static void uart2_er_irq(void) {
+    uart_err_irq(SCIFERI2_IRQn, 2);
+}
+
+static void uart3_tx_irq(void) {
+    uart_tx_irq(SCIFTXI3_IRQn, 3);
+}
 static void uart3_rx_irq(void)  {
     uart_rx_irq(SCIFRXI3_IRQn, 3);
 }
+static void uart3_er_irq(void) {
+    uart_err_irq(SCIFERI3_IRQn, 3);
+}
+
+static void uart4_tx_irq(void) {
+    uart_tx_irq(SCIFTXI4_IRQn, 4);
+}
 static void uart4_rx_irq(void)  {
     uart_rx_irq(SCIFRXI4_IRQn, 4);
 }
-static void uart5_rx_irq(void)  {
+static void uart4_er_irq(void) {
+    uart_err_irq(SCIFERI4_IRQn, 4);
+}
+
+#if defined(TARGET_RZA1H)
+static void uart5_tx_irq(void) {
+    uart_tx_irq(SCIFTXI5_IRQn, 5);
+}
+static void uart5_rx_irq(void) {
     uart_rx_irq(SCIFRXI5_IRQn, 5);
 }
-static void uart6_rx_irq(void)  {
+static void uart5_er_irq(void) {
+    uart_err_irq(SCIFERI5_IRQn, 5);
+}
+
+static void uart6_tx_irq(void) {
+    uart_tx_irq(SCIFTXI6_IRQn, 6);
+}
+static void uart6_rx_irq(void) {
     uart_rx_irq(SCIFRXI6_IRQn, 6);
 }
-static void uart7_rx_irq(void)  {
+static void uart6_er_irq(void) {
+    uart_err_irq(SCIFERI6_IRQn, 6);
+}
+
+static void uart7_tx_irq(void) {
+    uart_tx_irq(SCIFTXI7_IRQn, 7);
+}
+static void uart7_rx_irq(void) {
     uart_rx_irq(SCIFRXI7_IRQn, 7);
 }
-/* Error handler */
-static void uart0_er_irq(void)
-{
-    uart_err_irq(SCIFERI0_IRQn, 0);
-}
-static void uart1_er_irq(void)
-{
-    uart_err_irq(SCIFERI0_IRQn, 1);
-}
-static void uart2_er_irq(void)
-{
-    uart_err_irq(SCIFERI0_IRQn, 2);
-}
-static void uart3_er_irq(void)
-{
-    uart_err_irq(SCIFERI0_IRQn, 3);
-}
-static void uart4_er_irq(void)
-{
-    uart_err_irq(SCIFERI0_IRQn, 4);
-}
-static void uart5_er_irq(void)
-{
-    uart_err_irq(SCIFERI0_IRQn, 5);
-}
-static void uart6_er_irq(void)
-{
-    uart_err_irq(SCIFERI0_IRQn, 6);
-}
-static void uart7_er_irq(void)
-{
-    uart_err_irq(SCIFERI0_IRQn, 7);
-}
+static void uart7_er_irq(void) {
+    uart_err_irq(SCIFERI7_IRQn, 7);
+}
+#endif
 
 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) {
-    irq_handler = handler;
+    uart_data[obj->serial.index].irq_handler = handler;
     uart_data[obj->serial.index].serial_irq_id = id;
 }
@@ -644,5 +541,11 @@
 }
 
-static void serial_irq_set_internal(serial_t *obj, SerialIrq irq, uint32_t enable) {
+static void serial_irq_err_set(serial_t *obj, uint32_t enable)
+{
+    serial_irq_set_irq(irq_set_tbl[obj->serial.index][2], hander_set_tbl[obj->serial.index][2], enable);
+    serial_irq_set_irq(irq_set_tbl[obj->serial.index][3], hander_set_tbl[obj->serial.index][3], enable);
+}
+
+void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
     IRQn_Type IRQn;
     IRQHandler handler;
@@ -654,22 +557,4 @@
         serial_irq_set_irq(IRQn, handler, enable);
     }
-}
-
-static void serial_irq_err_set(serial_t *obj, uint32_t enable)
-{
-    serial_irq_set_irq(irq_set_tbl[obj->serial.index][2], hander_set_tbl[obj->serial.index][2], enable);
-    serial_irq_set_irq(irq_set_tbl[obj->serial.index][3], hander_set_tbl[obj->serial.index][3], enable);
-}
-
-void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
-    if (RxIrq == irq) {
-        uart_data[obj->serial.index].rx_irq_set_api = enable;
-    }
-    serial_irq_set_internal(obj, irq, enable);
-}
-
-static void serial_flow_irq_set(serial_t *obj, uint32_t enable) {
-    uart_data[obj->serial.index].rx_irq_set_flow = enable;
-    serial_irq_set_internal(obj, RxIrq, enable);
 }
 
@@ -680,11 +565,6 @@
     uint16_t err_read;
     int data;
-    int was_masked;
-
-#if defined ( __ICCARM__ )
-    was_masked = __disable_irq_iar();
-#else
-    was_masked = __disable_irq();
-#endif /* __ICCARM__ */
+
+    core_util_critical_section_enter();
     if (obj->serial.uart->SCFSR & 0x93) {
         err_read = obj->serial.uart->SCFSR;
@@ -692,7 +572,5 @@
     }
     obj->serial.uart->SCSCR |= 0x0040;     // Set RIE
-    if (!was_masked) {
-        __enable_irq();
-    }
+    core_util_critical_section_exit();
 
     if (obj->serial.uart->SCLSR & 0x0001) {
@@ -703,14 +581,8 @@
     data = obj->serial.uart->SCFRDR & 0xff;
 
-#if defined ( __ICCARM__ )
-    was_masked = __disable_irq_iar();
-#else
-    was_masked = __disable_irq();
-#endif /* __ICCARM__ */
+    core_util_critical_section_enter();
     err_read = obj->serial.uart->SCFSR;
     obj->serial.uart->SCFSR = (err_read & 0xfffD);     // Clear RDF
-    if (!was_masked) {
-        __enable_irq();
-    }
+    core_util_critical_section_exit();
 
     if (err_read & 0x80) {
@@ -728,19 +600,12 @@
 static void serial_put_done(serial_t *obj)
 {
-    int was_masked;
     volatile uint16_t dummy_read;
     
-#if defined ( __ICCARM__ )
-    was_masked = __disable_irq_iar();
-#else
-    was_masked = __disable_irq();
-#endif /* __ICCARM__ */
+    core_util_critical_section_enter();
     dummy_read = obj->serial.uart->SCFSR;
     obj->serial.uart->SCFSR = (dummy_read & 0xff9f);  // Clear TEND/TDFE
     obj->serial.uart->SCSCR |= 0x0080;     // Set TIE
-    if (!was_masked) {
-        __enable_irq();
-    }
-}
+    core_util_critical_section_exit();
+    }
 
 int serial_readable(serial_t *obj) {
@@ -753,19 +618,12 @@
 
 void serial_clear(serial_t *obj) {
-    int was_masked;
-#if defined ( __ICCARM__ )
-    was_masked = __disable_irq_iar();
-#else
-    was_masked = __disable_irq();
-#endif /* __ICCARM__ */
-
-    obj->serial.uart->SCFCR |=  0x06;          // TFRST = 1, RFRST = 1
-    obj->serial.uart->SCFCR &= ~0x06;          // TFRST = 0, RFRST = 0
+    core_util_critical_section_enter();
+
+    obj->serial.uart->SCFCR |=  0x0006u;       // TFRST = 1, RFRST = 1
+    obj->serial.uart->SCFCR &= ~0x0006u;       // TFRST = 0, RFRST = 0
     obj->serial.uart->SCFSR &= ~0x0093u;       // ER, BRK, RDF, DR = 0
 
-    if (!was_masked) {
-        __enable_irq();
-    }
-}
+    core_util_critical_section_exit();
+    }
 
 void serial_pinout_tx(PinName tx) {
@@ -774,62 +632,35 @@
 
 void serial_break_set(serial_t *obj) {
-    int was_masked;
-#if defined ( __ICCARM__ )
-    was_masked = __disable_irq_iar();
-#else
-    was_masked = __disable_irq();
-#endif /* __ICCARM__ */
+    core_util_critical_section_enter();
     // TxD Output(L)
     obj->serial.uart->SCSPTR &= ~0x0001u;  // SPB2DT = 0
     obj->serial.uart->SCSCR &= ~0x0020u;   // TE = 0 (Output disable)
-    if (!was_masked) {
-        __enable_irq();
-    }
-}
+    core_util_critical_section_exit();
+    }
 
 void serial_break_clear(serial_t *obj) {
-    int was_masked;
-#if defined ( __ICCARM__ )
-    was_masked = __disable_irq_iar();
-#else
-    was_masked = __disable_irq();
-#endif /* __ICCARM__ */
+    core_util_critical_section_enter();
     obj->serial.uart->SCSCR |= 0x0020u; // TE = 1 (Output enable)
     obj->serial.uart->SCSPTR |= 0x0001u; // SPB2DT = 1
-    if (!was_masked) {
-        __enable_irq();
-    }
-}
-
+    core_util_critical_section_exit();
+    }
+
+#if DEVICE_SERIAL_FC
 void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow) {
     // determine the UART to use
-    int was_masked;
-
-    serial_flow_irq_set(obj, 0);
 
     if (type == FlowControlRTSCTS) {
-#if defined ( __ICCARM__ )
-        was_masked = __disable_irq_iar();
-#else
-        was_masked = __disable_irq();
-#endif /* __ICCARM__ */
-        obj->serial.uart->SCFCR = 0x0008u;   // CTS/RTS enable
-        if (!was_masked) {
-            __enable_irq();
-        }
+        core_util_critical_section_enter();
+        obj->serial.uart->SCFCR |= 0x0008u;   // CTS/RTS enable
+        core_util_critical_section_exit();
         pinmap_pinout(rxflow, PinMap_UART_RTS);
         pinmap_pinout(txflow, PinMap_UART_CTS);
     } else {
-#if defined ( __ICCARM__ )
-        was_masked = __disable_irq_iar();
-#else
-        was_masked = __disable_irq();
-#endif /* __ICCARM__ */
-        obj->serial.uart->SCFCR = 0x0000u; // CTS/RTS diable
-        if (!was_masked) {
-            __enable_irq();
-        }
-    }
-}
+        core_util_critical_section_enter();
+        obj->serial.uart->SCFCR &= ~0x0008u; // CTS/RTS diable
+        core_util_critical_section_exit();
+    }
+}
+#endif
 
 static uint8_t serial_available_buffer(serial_t *obj)
