Index: asp3_tinet_ecnl_arm/trunk/asp3_dcre/mbed/targets/TARGET_RENESAS/TARGET_RZA1XX/pwmout_api.c
===================================================================
--- asp3_tinet_ecnl_arm/trunk/asp3_dcre/mbed/targets/TARGET_RENESAS/TARGET_RZ_A1H/pwmout_api.c	(revision 352)
+++ asp3_tinet_ecnl_arm/trunk/asp3_dcre/mbed/targets/TARGET_RENESAS/TARGET_RZA1XX/pwmout_api.c	(revision 374)
@@ -17,137 +17,119 @@
 #include "pwmout_api.h"
 #include "cmsis.h"
-#include "pinmap.h"
+#include "PeripheralPins.h"
 #include "RZ_A1_Init.h"
-#include "cpg_iodefine.h"
-#include "pwm_iodefine.h"
+#include "iodefine.h"
 #include "gpio_addrdefine.h"
-
-#define MTU2_PWM_NUM            22
+#include "mbed_drv_cfg.h"
+#include "mtu2.h"
+
+#define MTU2_PWM_OFFSET         0x20
+
+#ifdef FUNC_MOTOR_CTL_PWM
+typedef enum {
+    PWM1A  = 0,
+    PWM1B,
+    PWM1C,
+    PWM1D,
+    PWM1E,
+    PWM1F,
+    PWM1G,
+    PWM1H,
+    PWM2A  = 0x10,
+    PWM2B,
+    PWM2C,
+    PWM2D,
+    PWM2E,
+    PWM2F,
+    PWM2G,
+    PWM2H,
+} PWMType;
+
+static const PWMType PORT[] = {
+     PWM1A,          // PWM_PWM1A
+     PWM1B,          // PWM_PWM1B
+     PWM1C,          // PWM_PWM1C
+     PWM1D,          // PWM_PWM1D
+     PWM1E,          // PWM_PWM1E
+     PWM1F,          // PWM_PWM1F
+     PWM1G,          // PWM_PWM1G
+     PWM1H,          // PWM_PWM1H
+     PWM2A,          // PWM_PWM2A
+     PWM2B,          // PWM_PWM2B
+     PWM2C,          // PWM_PWM2C
+     PWM2D,          // PWM_PWM2D
+     PWM2E,          // PWM_PWM2E
+     PWM2F,          // PWM_PWM2F
+     PWM2G,          // PWM_PWM2G
+     PWM2H,          // PWM_PWM2H
+};
+
+static __IO uint16_t *PWM_MATCH[] = {
+    &PWMPWBFR_1A,    // PWM_PWM1A
+    &PWMPWBFR_1A,    // PWM_PWM1B
+    &PWMPWBFR_1C,    // PWM_PWM1C
+    &PWMPWBFR_1C,    // PWM_PWM1D
+    &PWMPWBFR_1E,    // PWM_PWM1E
+    &PWMPWBFR_1E,    // PWM_PWM1F
+    &PWMPWBFR_1G,    // PWM_PWM1G
+    &PWMPWBFR_1G,    // PWM_PWM1H
+    &PWMPWBFR_2A,    // PWM_PWM2A
+    &PWMPWBFR_2A,    // PWM_PWM2B
+    &PWMPWBFR_2C,    // PWM_PWM2C
+    &PWMPWBFR_2C,    // PWM_PWM2D
+    &PWMPWBFR_2E,    // PWM_PWM2E
+    &PWMPWBFR_2E,    // PWM_PWM2F
+    &PWMPWBFR_2G,    // PWM_PWM2G
+    &PWMPWBFR_2G,    // PWM_PWM2H
+};
+
+static uint16_t init_period_ch1 = 0;
+static uint16_t init_period_ch2 = 0;
+static int32_t  period_ch1 = 1;
+static int32_t  period_ch2 = 1;
+#endif
+
+#ifdef FUMC_MTU2_PWM
 #define MTU2_PWM_SIGNAL         2
-#define MTU2_PWM_OFFSET         0x20
-
-//  PORT ID, PWM ID, Pin function
-static const PinMap PinMap_PWM[] = {
-    {P2_1     , MTU2_PWM0_PIN  , 6},
-    {P2_11    , MTU2_PWM1_PIN  , 5},
-    {P3_8     , MTU2_PWM2_PIN  , 6},
-    {P3_10    , MTU2_PWM3_PIN  , 6},
-    {P4_0     , MTU2_PWM4_PIN  , 2},
-    {P4_4     , MTU2_PWM5_PIN  , 3},
-    {P4_6     , MTU2_PWM6_PIN  , 3},
-    {P5_0     , MTU2_PWM7_PIN  , 6},
-    {P5_3     , MTU2_PWM8_PIN  , 6},
-    {P5_5     , MTU2_PWM9_PIN  , 6},
-    {P7_2     , MTU2_PWM10_PIN , 7},
-    {P7_4     , MTU2_PWM11_PIN , 7},
-    {P7_6     , MTU2_PWM12_PIN , 7},
-    {P7_10    , MTU2_PWM13_PIN , 7},
-    {P7_12    , MTU2_PWM14_PIN , 7},
-    {P7_14    , MTU2_PWM15_PIN , 7},
-    {P8_8     , MTU2_PWM16_PIN , 5},
-    {P8_10    , MTU2_PWM17_PIN , 4},
-    {P8_12    , MTU2_PWM18_PIN , 4},
-    {P8_14    , MTU2_PWM19_PIN , 4},
-    {P11_0    , MTU2_PWM20_PIN , 2},
-    {P11_2    , MTU2_PWM21_PIN , 2},
-    {P4_4     , PWM0_PIN       , 4},
-    {P3_2     , PWM1_PIN       , 7},
-    {P4_6     , PWM2_PIN       , 4},
-    {P4_7     , PWM3_PIN       , 4},
-    {P8_14    , PWM4_PIN       , 6},
-    {P8_15    , PWM5_PIN       , 6},
-    {P8_13    , PWM6_PIN       , 6},
-    {P8_11    , PWM7_PIN       , 6},
-    {P8_8     , PWM8_PIN       , 6},
-    {P10_0    , PWM9_PIN       , 3},
-    {P8_12    , PWM10_PIN      , 6},
-    {P8_9     , PWM11_PIN      , 6},
-    {P8_10    , PWM12_PIN      , 6},
-    {P4_5     , PWM13_PIN      , 4},
-    {NC       , NC             , 0}
-};
-
-static const PWMType PORT[] = {
-     PWM2E,          // PWM0_PIN
-     PWM2C,          // PWM1_PIN
-     PWM2G,          // PWM2_PIN
-     PWM2H,          // PWM3_PIN
-     PWM1G,          // PWM4_PIN
-     PWM1H,          // PWM5_PIN
-     PWM1F,          // PWM6_PIN
-     PWM1D,          // PWM7_PIN
-     PWM1A,          // PWM8_PIN
-     PWM2A,          // PWM9_PIN
-     PWM1E,          // PWM10_PIN
-     PWM1B,          // PWM11_PIN
-     PWM1C,          // PWM12_PIN
-     PWM2F,          // PWM13_PIN
-};
+
+typedef enum {
+    TIOC0A  = 0,
+    TIOC0B,
+    TIOC0C,
+    TIOC0D,
+    TIOC1A = 0x10,
+    TIOC1B,
+    TIOC2A = 0x20,
+    TIOC2B,
+    TIOC3A = 0x30,
+    TIOC3B,
+    TIOC3C,
+    TIOC3D,
+    TIOC4A = 0x40,
+    TIOC4B,
+    TIOC4C,
+    TIOC4D,
+} MTU2_PWMType;
 
 static const MTU2_PWMType MTU2_PORT[] = {
-     TIOC2A,         // MTU2_PWM0_PIN
-     TIOC1A,         // MTU2_PWM1_PIN
-     TIOC4A,         // MTU2_PWM2_PIN
-     TIOC4C,         // MTU2_PWM3_PIN
-     TIOC0A,         // MTU2_PWM4_PIN
-     TIOC4A,         // MTU2_PWM5_PIN
-     TIOC4C,         // MTU2_PWM6_PIN
-     TIOC0A,         // MTU2_PWM7_PIN
-     TIOC3C,         // MTU2_PWM8_PIN
-     TIOC0C,         // MTU2_PWM9_PIN
-     TIOC0C,         // MTU2_PWM10_PIN
-     TIOC1A,         // MTU2_PWM11_PIN
-     TIOC2A,         // MTU2_PWM12_PIN
-     TIOC3C,         // MTU2_PWM13_PIN
-     TIOC4A,         // MTU2_PWM14_PIN
-     TIOC4C,         // MTU2_PWM15_PIN
-     TIOC1A,         // MTU2_PWM16_PIN
-     TIOC3A,         // MTU2_PWM17_PIN
-     TIOC3C,         // MTU2_PWM18_PIN
-     TIOC2A,         // MTU2_PWM19_PIN
-     TIOC4A,         // MTU2_PWM20_PIN
-     TIOC4C,         // MTU2_PWM21_PIN
-};
-
-static __IO uint16_t *PWM_MATCH[] = {
-    &PWMPWBFR_2E,    // PWM0_PIN
-    &PWMPWBFR_2C,    // PWM1_PIN
-    &PWMPWBFR_2G,    // PWM2_PIN
-    &PWMPWBFR_2G,    // PWM3_PIN
-    &PWMPWBFR_1G,    // PWM4_PIN
-    &PWMPWBFR_1G,    // PWM5_PIN
-    &PWMPWBFR_1E,    // PWM6_PIN
-    &PWMPWBFR_1C,    // PWM7_PIN
-    &PWMPWBFR_1A,    // PWM8_PIN
-    &PWMPWBFR_2A,    // PWM9_PIN
-    &PWMPWBFR_1E,    // PWM10_PIN
-    &PWMPWBFR_1A,    // PWM11_PIN
-    &PWMPWBFR_1C,    // PWM12_PIN
-    &PWMPWBFR_2E,    // PWM13_PIN
-};
-
-static __IO uint16_t *MTU2_PWM_MATCH[MTU2_PWM_NUM][MTU2_PWM_SIGNAL] = {
-    { &MTU2TGRA_2, &MTU2TGRB_2 },       // MTU2_PWM0_PIN
-    { &MTU2TGRA_1, &MTU2TGRB_1 },       // MTU2_PWM1_PIN
-    { &MTU2TGRA_4, &MTU2TGRB_4 },       // MTU2_PWM2_PIN
-    { &MTU2TGRC_4, &MTU2TGRD_4 },       // MTU2_PWM3_PIN
-    { &MTU2TGRA_0, &MTU2TGRB_0 },       // MTU2_PWM4_PIN
-    { &MTU2TGRA_4, &MTU2TGRB_4 },       // MTU2_PWM5_PIN
-    { &MTU2TGRC_4, &MTU2TGRD_4 },       // MTU2_PWM6_PIN
-    { &MTU2TGRA_0, &MTU2TGRB_0 },       // MTU2_PWM7_PIN
-    { &MTU2TGRC_3, &MTU2TGRD_3 },       // MTU2_PWM8_PIN
-    { &MTU2TGRC_0, &MTU2TGRD_0 },       // MTU2_PWM9_PIN
-    { &MTU2TGRC_0, &MTU2TGRD_0 },       // MTU2_PWM10_PIN
-    { &MTU2TGRA_1, &MTU2TGRB_1 },       // MTU2_PWM11_PIN
-    { &MTU2TGRA_2, &MTU2TGRB_2 },       // MTU2_PWM12_PIN
-    { &MTU2TGRC_3, &MTU2TGRD_3 },       // MTU2_PWM13_PIN
-    { &MTU2TGRA_4, &MTU2TGRB_4 },       // MTU2_PWM14_PIN
-    { &MTU2TGRC_4, &MTU2TGRD_4 },       // MTU2_PWM15_PIN
-    { &MTU2TGRA_1, &MTU2TGRB_1 },       // MTU2_PWM16_PIN
-    { &MTU2TGRA_3, &MTU2TGRB_3 },       // MTU2_PWM17_PIN
-    { &MTU2TGRC_3, &MTU2TGRD_3 },       // MTU2_PWM18_PIN
-    { &MTU2TGRA_2, &MTU2TGRB_2 },       // MTU2_PWM19_PIN
-    { &MTU2TGRA_4, &MTU2TGRB_4 },       // MTU2_PWM20_PIN
-    { &MTU2TGRC_4, &MTU2TGRD_4 }        // MTU2_PWM21_PIN
+     TIOC0A,         // PWM_TIOC0A
+     TIOC0C,         // PWM_TIOC0C
+     TIOC1A,         // PWM_TIOC1A
+     TIOC2A,         // PWM_TIOC2A
+     TIOC3A,         // PWM_TIOC3A
+     TIOC3C,         // PWM_TIOC3C
+     TIOC4A,         // PWM_TIOC4A
+     TIOC4C,         // PWM_TIOC4C
+};
+
+static __IO uint16_t *MTU2_PWM_MATCH[][MTU2_PWM_SIGNAL] = {
+    { &MTU2TGRA_0, &MTU2TGRB_0 },       // PWM_TIOC0A
+    { &MTU2TGRC_0, &MTU2TGRD_0 },       // PWM_TIOC0C
+    { &MTU2TGRA_1, &MTU2TGRB_1 },       // PWM_TIOC1A
+    { &MTU2TGRA_2, &MTU2TGRB_2 },       // PWM_TIOC2A
+    { &MTU2TGRA_3, &MTU2TGRB_3 },       // PWM_TIOC3A
+    { &MTU2TGRC_3, &MTU2TGRD_3 },       // PWM_TIOC3C
+    { &MTU2TGRA_4, &MTU2TGRB_4 },       // PWM_TIOC4A
+    { &MTU2TGRC_4, &MTU2TGRD_4 },       // PWM_TIOC4C
 };
 
@@ -213,10 +195,7 @@
 } MTU2Signal;
 
-static uint16_t init_period_ch1 = 0;
-static uint16_t init_period_ch2 = 0;
 static uint16_t init_mtu2_period_ch[5] = {0};
-static int32_t  period_ch1 = 1;
-static int32_t  period_ch2 = 1;
 static int32_t  mtu2_period_ch[5] = {1, 1, 1, 1, 1};
+#endif
 
 void pwmout_init(pwmout_t* obj, PinName pin) {
@@ -226,9 +205,10 @@
 
     if (pwm >= MTU2_PWM_OFFSET) {
+#ifdef FUMC_MTU2_PWM
         /* PWM by MTU2 */
         int tmp_pwm;
         
         // power on
-        CPGSTBCR3 &= ~(CPG_STBCR3_BIT_MSTP33);
+        mtu2_init();
         
         obj->pwm = pwm;
@@ -261,5 +241,7 @@
             init_mtu2_period_ch[obj->ch] = 1;
         }
+#endif
     } else {
+#ifdef FUNC_MOTOR_CTL_PWM
         /* PWM */
         // power on
@@ -269,8 +251,8 @@
         if (((uint32_t)PORT[obj->pwm] & 0x00000010) == 0x00000010) {
             obj->ch  = 2;
-            PWMPWPR_2_BYTE_L = 0x00;
+            PWMPWPR_2 = 0x00;
         } else {
             obj->ch  = 1;
-            PWMPWPR_1_BYTE_L = 0x00;
+            PWMPWPR_1 = 0x00;
         }
 
@@ -288,4 +270,5 @@
             init_period_ch1 = 1;
         }
+#endif
     }
 }
@@ -293,11 +276,12 @@
 void pwmout_free(pwmout_t* obj) {
     pwmout_write(obj, 0);
+    mtu2_free();
 }
 
 void pwmout_write(pwmout_t* obj, float value) {
     uint32_t wk_cycle;
-    uint16_t v;
 
     if (obj->pwm >= MTU2_PWM_OFFSET) {
+#ifdef FUMC_MTU2_PWM
         /* PWM by MTU2 */
         int tmp_pwm;
@@ -313,6 +297,14 @@
         wk_cycle = *MTU2_PWM_MATCH[tmp_pwm][MTU2_PERIOD] & 0xffff;
         // set channel match to percentage
+        if (value == 1.0f) {
+            *MTU2_PWM_MATCH[tmp_pwm][MTU2_PULSE] = (uint16_t)(wk_cycle - 1);
+        } else {
         *MTU2_PWM_MATCH[tmp_pwm][MTU2_PULSE] = (uint16_t)((float)wk_cycle * value);
+        }
+#endif
     } else {
+#ifdef FUNC_MOTOR_CTL_PWM
+        uint16_t v;
+
         /* PWM */
         if (value < 0.0f) {
@@ -333,4 +325,5 @@
         v = (uint16_t)((float)wk_cycle * value);
         *PWM_MATCH[obj->pwm] = (v | ((PORT[obj->pwm] & 1) << 12));
+#endif
     }
 }
@@ -341,4 +334,5 @@
 
     if (obj->pwm >= MTU2_PWM_OFFSET) {
+#ifdef FUMC_MTU2_PWM
         /* PWM by MTU2 */
         uint32_t wk_pulse;
@@ -349,5 +343,7 @@
         wk_pulse = *MTU2_PWM_MATCH[tmp_pwm][MTU2_PULSE] & 0xffff;
         value = ((float)wk_pulse / (float)wk_cycle);
+#endif
     } else {
+#ifdef FUNC_MOTOR_CTL_PWM
         /* PWM */
         if (obj->ch == 2) {
@@ -357,4 +353,5 @@
         }
         value = ((float)(*PWM_MATCH[obj->pwm] & 0x03ff) / (float)wk_cycle);
+#endif
     }
 
@@ -370,4 +367,5 @@
 }
 
+#ifdef FUNC_MOTOR_CTL_PWM
 static void set_duty_again(__IO uint16_t *p_pwmpbfr, uint16_t last_cycle, uint16_t new_cycle){
     uint16_t wk_pwmpbfr;
@@ -380,5 +378,7 @@
     *p_pwmpbfr = (v | (wk_pwmpbfr & 0x1000));
 }
-
+#endif
+
+#ifdef FUMC_MTU2_PWM
 static void set_mtu2_duty_again(__IO uint16_t *p_pwmpbfr, uint16_t last_cycle, uint16_t new_cycle){
     uint16_t wk_pwmpbfr;
@@ -389,15 +389,18 @@
     *p_pwmpbfr = (uint16_t)((float)new_cycle * value);
 }
+#endif
 
 // Set the PWM period, keeping the duty cycle the same.
 void pwmout_period_us(pwmout_t* obj, int us) {
-    uint64_t wk_cycle_mtu2;
     uint32_t pclk_base;
     uint32_t wk_cycle;
     uint32_t wk_cks = 0;
     uint16_t wk_last_cycle;
-    int      max_us = 0;
 
     if (obj->pwm >= MTU2_PWM_OFFSET) {
+#ifdef FUMC_MTU2_PWM
+        uint64_t wk_cycle_mtu2;
+        int      max_us = 0;
+
         /* PWM by MTU2 */
         int tmp_pwm;
@@ -469,5 +472,7 @@
         // Save for future use
         mtu2_period_ch[obj->ch] = us;
+#endif
     } else {
+#ifdef FUNC_MOTOR_CTL_PWM
         /* PWM */
         if (us > 491) {
@@ -494,5 +499,5 @@
         if (obj->ch == 2) {
             wk_last_cycle    = PWMPWCYR_2 & 0x03ff;
-            PWMPWCR_2_BYTE_L = 0xc0 | wk_cks;
+            PWMPWCR_2        = 0xc0 | wk_cks;
             PWMPWCYR_2       = (uint16_t)wk_cycle;
 
@@ -504,5 +509,5 @@
 
             // Counter Start
-            PWMPWCR_2_BYTE_L |= 0x08;
+            PWMPWCR_2 |= 0x08;
 
             // Save for future use
@@ -510,5 +515,5 @@
         } else {
             wk_last_cycle    = PWMPWCYR_1 & 0x03ff;
-            PWMPWCR_1_BYTE_L = 0xc0 | wk_cks;
+            PWMPWCR_1        = 0xc0 | wk_cks;
             PWMPWCYR_1       = (uint16_t)wk_cycle;
 
@@ -520,9 +525,10 @@
 
             // Counter Start
-            PWMPWCR_1_BYTE_L |= 0x08;
+            PWMPWCR_1 |= 0x08;
 
             // Save for future use
             period_ch1 = us;
         }
+#endif
     }
 }
@@ -540,9 +546,12 @@
 
     if (obj->pwm >= MTU2_PWM_OFFSET) {
+#ifdef FUMC_MTU2_PWM
         /* PWM by MTU2 */
         if (mtu2_period_ch[obj->ch] != 0) {
             value = (float)us / (float)mtu2_period_ch[obj->ch];
         }
+#endif
     } else {
+#ifdef FUNC_MOTOR_CTL_PWM
         /* PWM */
         if (obj->ch == 2) {
@@ -555,4 +564,5 @@
             }
         }
+#endif
     }
     pwmout_write(obj, value);
