Пример #1
0
/***********************************************************************************************************************
* Function Name: power_on_off
* Description  : Switches power to an MTU channel.  Required by FIT spec.
* Arguments    : channel -
*                   Which channel to use.
*                on_or_off -
*                   What it says.
* Return Value : none
***********************************************************************************************************************/
void power_on_off (uint8_t on_or_off)
{
    R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_LPC_CGC_SWR);

    MSTP(MTU) = on_or_off; // All channels are on the same module stop register.

    R_BSP_RegisterProtectEnable(BSP_REG_PROTECT_LPC_CGC_SWR);
}
Пример #2
0
/*******************************************************************************************************************//**
 * @brief Initializes VBTICTLR register based on pin configuration.
 * 
 * The VBTICTLR register may need to be modified based on the project's pin configuration. There is a set of pins that
 * needs to be checked. If one of these pins is found in the pin configuration table then it will be tested to see if
 * the appropriate VBTICTLR bit needs to be set or cleared. If one of the pins that is being searched for is not found
 * then the accompanying VBTICTLR bit is left as-is.
 **********************************************************************************************************************/
void bsp_vbatt_init (ioport_cfg_t const * const p_pin_cfg)
{
    uint32_t pin_index;
    uint32_t vbatt_index;
    uint8_t  local_vbtictlr_set;    ///< Will hold bits to set in VBTICTLR
    uint8_t  local_vbtictlr_clear;  ///< Will hold bits to set in VBTICTLR

    /** Make no changes unless required. */    
    local_vbtictlr_set = 0;
    local_vbtictlr_clear = 0;

    /** Must loop over all pins as pin configuration table is unordered. */
    for (pin_index = 0; pin_index < p_pin_cfg->number_of_pins; pin_index++)
    {
        /** Loop over VBATT input pins. */
        for (vbatt_index = 0; vbatt_index < (sizeof(g_vbatt_pins_input)/sizeof(g_vbatt_pins_input[0])); vbatt_index++)
        {
            if (p_pin_cfg->p_pin_cfg_data[pin_index].pin == g_vbatt_pins_input[vbatt_index])
            {
                /** Get PSEL value for pin. */
                uint32_t pfs_psel_value = p_pin_cfg->p_pin_cfg_data[pin_index].pin_cfg & BSP_PRV_PFS_PSEL_MASK;

                /** Check if pin is being used for RTC or AGT use. */
                if ((IOPORT_PERIPHERAL_AGT == pfs_psel_value) || (IOPORT_PERIPHERAL_CLKOUT_COMP_RTC == pfs_psel_value))
                {
                    /** Bit should be set to 1. */
                    local_vbtictlr_set |= (uint8_t)(1 << vbatt_index);
                }
                else
                {
                    /** Bit should be cleared to 0. */
                    local_vbtictlr_clear |= (uint8_t)(1 << vbatt_index);
                }
            }
        }
    }

    /** Disable write protection on VBTICTLR. */
    R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_OM_LPC_BATT);

    /** Read value, set and clear bits as needed and write back. */
    uint8_t local_vbtictlr = R_SYSTEM->VBTICTLR;
    local_vbtictlr |= local_vbtictlr_set;               ///< Set appropriate bits
    local_vbtictlr &= (uint8_t)~local_vbtictlr_clear;   ///< Clear appropriate bits

    R_SYSTEM->VBTICTLR = local_vbtictlr;

    /** Enable write protection on VBTICTLR. */
    R_BSP_RegisterProtectEnable(BSP_REG_PROTECT_OM_LPC_BATT);
}
Пример #3
0
/******************************************************************************
Function Name   : usb_mcu_init
Description     : USB pin function and port mode setting.
Arguments       : none
Return value    : none
******************************************************************************/
void usb_mcu_init(void)
{
    R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_LPC_CGC_SWR);
    R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_MPC);

  #if USB_FUNCSEL_USBIP0_PP == USB_HOST_PP

    PORT1.PMR.BIT.B4 = 1u;  /* P14 set USB0_OVRCURA */

    PORT1.PMR.BIT.B6 = 1u;  /* P16 set VBUS_USB */
    MPC.P16PFS.BYTE = 0x12; /* USB0_VBUSEN */
  #endif  /* USB_FUNCSEL_USBIP0_PP == USB_HOST_PP */

  #if USB_FUNCSEL_USBIP1_PP == USB_HOST_PP
    PORT1.PMR.BIT.B0    = 1u;
    MPC.P10PFS.BYTE = 0x15; /* USBHS_OVRCURA */
    PORT1.PMR.BIT.B1    = 1u;
    MPC.P11PFS.BYTE = 0x15; /* USBHS_VBUSEN */
  #endif  /* USB_FUNCSEL_USBIP1_PP == USB_HOST_PP */

    R_BSP_RegisterProtectEnable(BSP_REG_PROTECT_MPC);
    R_BSP_RegisterProtectEnable(BSP_REG_PROTECT_LPC_CGC_SWR);
}   /* eof usb_mcu_init() */
Пример #4
0
/***********************************************************************************************************************
* Function name: output_ports_configure
* Description  : Configures the port and pin direction settings, and sets the pin outputs to a safe level.
* Arguments    : none
* Return value : none
***********************************************************************************************************************/
static void output_ports_configure(void)
{
    /* Enable LEDs. */
    /* Start with LEDs off. */
    LED0 = LED_OFF;
    LED1 = LED_OFF;
    LED2 = LED_OFF;
    LED3 = LED_OFF;

    /* Set LED pins as outputs. */
    LED0_PDR = 1;
    LED1_PDR = 1;
    LED2_PDR = 1;
    LED3_PDR = 1;

    /* Enable switches. */
    /* Set pins as inputs. */
    SW1_PDR = 0;
    SW2_PDR = 0;
    SW3_PDR = 0;

    /* Set port mode registers for switches. */
    SW1_PMR = 0;
    SW2_PMR = 0;
    SW3_PMR = 0;
    
    /* Unlock MPC registers to enable writing to them. */
    R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_MPC);
    
    /* TXD5 is serial output to serial/USB interface */
    PORTA.PMR.BIT.B4 = 0;
    MPC.PA4PFS.BYTE  = 0x0A;
    PORTA.PDR.BIT.B4 = 1;
    PORTA.PMR.BIT.B4 = 1;
    /* RXD5 is serial input from serial/USB interface */
    PORTA.PMR.BIT.B3 = 0;
    MPC.PA3PFS.BYTE  = 0x0A;
    PORTA.PDR.BIT.B3 = 0;
    PORTA.PMR.BIT.B3 = 1;

    /* Configure the pin connected to the ADC Pot as an analog input */
    PORT4.PMR.BIT.B0 = 0;
    MPC.P40PFS.BYTE = 0x80;     //Set ASEL bit and clear the rest
    PORT4.PDR.BIT.B0 = 0;   

    /* Lock MPC registers. */
    R_BSP_RegisterProtectEnable(BSP_REG_PROTECT_MPC);
}
Пример #5
0
/*******************************************************************************************************************//**
 * @brief HW_ADC_Close
 *
 * The Close function stops power and clocking to the A/D peripheral, disables interrupts and turns off the channels and
 * sensors.
 *
 * @param[in]  Unit  : The ADC Unit to stop
 *
 * @retval  None
 **********************************************************************************************************************/
void HW_ADC_Close(uint16_t unit)
{
    aregs_t     *p_regs = GET_REGS_PTR(unit);

    /** Stop triggers & conversions, and disable peripheral interrupts */
#if (BSP_IRQ_CFG_ADC0_SCAN_END != BSP_IRQ_DISABLED)
    NVIC_DisableIRQ (ADC0_SCAN_END_IRQn) ;         /*** Disable interrupts in ICU    */
    NVIC_ClearPendingIRQ (ADC0_SCAN_END_IRQn) ;    /*** Clear interrupt flag         */
    R_BSP_IrqStatusClear (ADC0_SCAN_END_IRQn) ;
#endif
#if (BSP_IRQ_CFG_ADC0_SCAN_END_B != BSP_IRQ_DISABLED)
    NVIC_DisableIRQ (ADC0_SCAN_END_B_IRQn) ;       /*** Disable interrupts in ICU    */
    NVIC_ClearPendingIRQ (ADC0_SCAN_END_B_IRQn) ;  /*** Clear interrupt flag         */
    R_BSP_IrqStatusClear (ADC0_SCAN_END_B_IRQn);
#endif

    /** If Group Priority was enabled, the disable it*/
    if (1 == p_regs->ADGSPCR_b.PGS)
    {
        p_regs->ADGSPCR_b.PGS = 0UL;
    }

    /** If Group Mode was enabled, then disable the triggers for both groups */
    if (1 == p_regs->ADCSR_b.ADCS)
    {
        p_regs->ADSTRGR = 0x3F3F;
    }
    /** If Group Mode was not enabled, then disable the trigger for normal mode */
    else
    {
        p_regs->ADSTRGR_b.TRSA = 0x3F;
    }

    /** Clear all configurations*/
    p_regs->ADCSR = 0UL;

    /** Power down peripheral   */
    R_BSP_RegisterProtectDisable (BSP_REG_PROTECT_OM_LPC_BATT) ;

    if (ADC_UNIT_0 == unit)
    {
        R_MSTP->MSTPCRD_b.MSTPD16 = 1 ; /*** ADC 0 power off      */
    }

    R_BSP_RegisterProtectEnable (BSP_REG_PROTECT_OM_LPC_BATT) ;

}
Пример #6
0
void HW_ADC_SensorCfgTemperature(uint16_t const unit, adc_channel_cfg_t const * const p_cfg)
{
	/** Used to prevent compiler warning */
      SSP_PARAMETER_NOT_USED(unit);

    aregs_t *p_regs = GET_REGS_PTR(unit);

    R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_OM_LPC_BATT);
    R_BSP_RegisterProtectEnable(BSP_REG_PROTECT_OM_LPC_BATT);

    if (p_cfg->scan_mask & ADC_MASK_TEMPERATURE)
    {
        p_regs->ADEXICR_b.TSSA = 1UL;      /** Select temperature output GroupA */
    }

    /** Enable temperature addition mode if set */
    p_regs->ADEXICR_b.TSSAD = (p_cfg->add_mask & ADC_MASK_TEMPERATURE) ? 1 : 0;

}
Пример #7
0
/******************************************************************************
* Function Name: adc_open
* Description  : This function applies power to the A/D peripheral, sets the
*                operational mode, trigger sources, interrupt priority, and
*                configurations common to all channels and sensors. If interrupt
*                priority is non-zero, the function takes a callback function
*                pointer for notifying the user at interrupt level whenever a
*                scan has completed.
*
* NOTE: The temperature sensor on the RX210 functionally behaves like a
*       regular software trigger. But instead of using the ADST bit, it has its
*       own PGAEN bit. For this bit to work, you must also have TRSA=0x0A,
*       TRGE=1, MSTP(TEMPS)=0, and TSEN=1.
*       The ADST bit will work with this configuration and will also work with
*       just MSTP(TEMPS)=0 and TSEN=1. However, the value read does not include
*       the temperature sensor gain value. This behavior is not documented in
*       the HW Manual, and accuracy is unknown.
*       Because of this, and portability concerns, the driver API is written
*       to look like the temp sensor runs on a normal software trigger.
*
*       -Gain values will always be read.
*       -ADST could be used to check for scan complete when using PGAEN.
*
* Arguments    : mode-
*                    Operational mode (see enumeration below)
*                p_cfg-
*                    Pointer to configuration structure (see below)
*                p_callback-
*                    Optional pointer to function called from interrupt when
*                    a scan completes
* Return Value : ADC_SUCCESS-
*                    Successful
*                ADC_ERR_AD_LOCKED-
*                    Open() call is in progress elsewhere
*                ADC_ERR_AD_NOT_CLOSED-
*                    Peripheral is still running in another mode; Perform
*                    R_ADC_Close() first
*                ADC_ERR_INVALID_ARG-
*                    mode or element of p_cfg structure has invalid value.
*                ADC_ERR_ILLEGAL_ARG-
*                    an argument is illegal based upon mode
*                ADC_ERR_MISSING_PTR-
*                    p_cfg pointer is FIT_NO_PTR/NULL
*******************************************************************************/
adc_err_t adc_open(adc_mode_t const       mode,
                   adc_cfg_t * const      p_cfg,
                   void         (* const  p_callback)(void *p_args))
{


#if ADC_CFG_PARAM_CHECKING_ENABLE == 1
    if ((p_cfg == NULL) || (p_cfg == FIT_NO_PTR))
    {
        return ADC_ERR_MISSING_PTR;
    }

    /* Check for valid argument values */
    if ((mode >= ADC_MODE_MAX)
     || ((p_cfg->trigger >= ADC_TRIG_HW_MAX) && (p_cfg->trigger != ADC_TRIG_SOFTWARE))
     || (p_cfg->priority > BSP_MCU_IPL_MAX)
     || (p_cfg->add_cnt >= ADC_ADD_MAX)
     || (p_cfg->trigger == ADC_TRIG_PLACEHOLDER)
     || ((p_cfg->clearing != ADC_CLEAR_AFTER_READ_OFF) && (p_cfg->clearing != ADC_CLEAR_AFTER_READ_ON)))

    {
        return ADC_ERR_INVALID_ARG;
    }

    /* If interrupt driven, must have callback function */
    if ((p_cfg->priority != 0)
     && ((p_callback == NULL) || (p_callback == FIT_NO_FUNC)))
    {
        return ADC_ERR_ILLEGAL_ARG;
    }

    if (p_cfg->add_cnt == ADC_ADD_OFF)
    {
        /* Check alignment values only if addition is off */
        if ((p_cfg->alignment != ADC_ALIGN_LEFT) && (p_cfg->alignment != ADC_ALIGN_RIGHT))
        {
            return ADC_ERR_INVALID_ARG;
        }
    }
    else // addition on
    {
        /* Addition not allowed with temperature sensor on RX210 */
        if (mode == ADC_MODE_SS_TEMPERATURE)
        {
            return ADC_ERR_ILLEGAL_ARG;
        }
    }

    /* For portability, only allow software trigger because that is the functional
     * behavior of the sensor. Will map to actual "synchronous temperature trigger"
     * (0x0A) later. */
    if ((mode == ADC_MODE_SS_TEMPERATURE) && (p_cfg->trigger != ADC_TRIG_SOFTWARE))
    {
        return ADC_ERR_ILLEGAL_ARG;
    }


    /* In double trigger mode, SW and async triggers not allowed */
    if ((mode == ADC_MODE_SS_ONE_CH_DBLTRIG)
     && ((p_cfg->trigger == ADC_TRIG_SOFTWARE) || (p_cfg->trigger == ADC_TRIG_ASYNC_ADTRG0)))
    {
        return ADC_ERR_ILLEGAL_ARG;
    }

    /* Group checking; only synchronous triggers allowed; must be unique */
    if ((mode == ADC_MODE_SS_MULTI_CH_GROUPED) || (mode == ADC_MODE_SS_MULTI_CH_GROUPED_DBLTRIG_A))
    {
        if ((p_cfg->trigger == ADC_TRIG_ASYNC_ADTRG0)
         || (p_cfg->trigger_groupb == ADC_TRIG_ASYNC_ADTRG0)
         || (p_cfg->trigger_groupb == ADC_TRIG_PLACEHOLDER)
         || (p_cfg->trigger == p_cfg->trigger_groupb)
         || (p_cfg->trigger == ADC_TRIG_SOFTWARE)
         || (p_cfg->trigger_groupb == ADC_TRIG_SOFTWARE))
        {
            return ADC_ERR_ILLEGAL_ARG;
        }

        if ((p_cfg->priority_groupb > BSP_MCU_IPL_MAX)
         || (p_cfg->trigger >= ADC_TRIG_HW_MAX)
         || (p_cfg->trigger_groupb >= ADC_TRIG_HW_MAX))
        {
            return ADC_ERR_INVALID_ARG;
        }

        if ((p_cfg->priority_groupb != 0)   // interrupt driven; must have callback func
         && ((p_callback == NULL) || (p_callback == FIT_NO_FUNC)))
        {
            return ADC_ERR_ILLEGAL_ARG;
        }
    }
#endif // parameter checking


    if (g_dcb.opened == true)
    {
        return ADC_ERR_AD_NOT_CLOSED;
    }
    if (R_BSP_HardwareLock(BSP_LOCK_S12AD) == false)
    {
        return ADC_ERR_AD_LOCKED;
    }


    /* APPLY POWER TO PERIPHERAL */

    R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_LPC_CGC_SWR);
    MSTP(S12AD) = 0;
    if (mode == ADC_MODE_SS_TEMPERATURE)
    {
        MSTP(TEMPS) = 0;
    }
    R_BSP_RegisterProtectEnable(BSP_REG_PROTECT_LPC_CGC_SWR);

    S12AD.ADCSR.WORD = 0;
    S12AD.ADEXICR.WORD = 0;
    TEMPS.TSCR.BYTE = 0;


    /* SET MODE RELATED REGISTER FIELDS */

    g_dcb.mode = mode;
    if ((mode == ADC_MODE_SS_MULTI_CH_GROUPED)
     || (mode == ADC_MODE_SS_MULTI_CH_GROUPED_DBLTRIG_A))
    {
        S12AD.ADCSR.BIT.ADCS = ADC_ADCS_GROUP_SCAN;
    }
    else
    {
        if ((mode == ADC_MODE_CONT_ONE_CH) || (mode == ADC_MODE_CONT_MULTI_CH))
        {
            S12AD.ADCSR.BIT.ADCS = ADC_ADCS_CONT_SCAN;
        }
        // other modes have ADCS=0
    }

    if ((mode == ADC_MODE_SS_ONE_CH_DBLTRIG)
     || (mode == ADC_MODE_SS_MULTI_CH_GROUPED_DBLTRIG_A))
    {
        S12AD.ADCSR.BIT.DBLE = 1;                   // enable double trigger
    }


    /* SET TRIGGER AND INTERRUPT PRIORITY REGISTER FIELDS */

    if (mode == ADC_MODE_SS_TEMPERATURE)
    {
        S12AD.ADSTRGR.BIT.TRSA = 0x0A;  // synchronous temperature trigger
    }
    if (p_cfg->trigger != ADC_TRIG_SOFTWARE)
    {
        S12AD.ADSTRGR.BIT.TRSA = p_cfg->trigger;
    }
    if (p_cfg->trigger == ADC_TRIG_ASYNC_ADTRG0)
    {
        S12AD.ADCSR.BIT.EXTRG = 1;      // set ext trigger for async trigger
    }
    if (S12AD.ADCSR.BIT.ADCS == ADC_ADCS_GROUP_SCAN)
    {
        S12AD.ADSTRGR.BIT.TRSB = p_cfg->trigger_groupb;
        IPR(S12AD,GBADI) = p_cfg->priority_groupb;
    }
    IPR(S12AD,S12ADI0) = p_cfg->priority;


    /* SET REGISTER FIELDS FOR REMAINING PARAMETERS */

    S12AD.ADADC.BIT.ADC = p_cfg->add_cnt;
    S12AD.ADCER.WORD = (uint16_t) (p_cfg->alignment | p_cfg->clearing);

    /* SAVE CALLBACK FUNCTION POINTER */
    g_dcb.callback = p_callback;


    /* MARK DRIVER AS OPENED */
    g_dcb.opened = true;
    R_BSP_HardwareUnlock(BSP_LOCK_S12AD);

    return ADC_SUCCESS;
}
Пример #8
0
/***********************************************************************************************************************
* Function Name: R_LVD_Open
* Description  : This function configures the LVD channel including voltage level, trigger type and sets the callback
*                function. This function can only be called once for each channel after which R_LVD_Close has to be
*                called before attempting to reconfigure the LVD channel.
* Arguments    : lvd_channel_t e_channel
*                       -Specify the channel
*                lvd_voltage_level_t e_voltage_level
*                       -Specify the LVD voltage level
*                lvd_event_action_t e_event
*                       -Specify the LVD event that should occur when the trigger specified in e-trigger is met.
*                lvd_trigger_t e_trigger
*                       -Specify the LVD voltage level triggering type. It can be configured for when the voltage
*                       level rises above the triggering level,  or when it falls below or both conditions.
*                void (*pcallback)(void)
*                       -callback function pointer. This can be a null if e_event is LVD_EVENT_RESET or LVD_EVENT_POLL
* Return Value :LVD_SUCCESS
*               LVD_ERR_ILL_PARAM
*               LVD_ERR_ILL_REINITIALIZATION
*               LVD_ERR_VDET
*
***********************************************************************************************************************/
lvd_err_t R_LVD_Open(lvd_channel_t e_channel, lvd_config_t *p_cfg, void (*pcallback)(void *))
{
#if (LVD_CFG_PARAM_CHECKING_ENABLE == 1)
    if (e_channel >= LVD_CHANNEL_INVALID)
    {
        return LVD_ERR_ILL_PARAM;
    }

    if (LVD_CHANNEL_2 == e_channel)
    {
        if ((p_cfg->e_voltage_level < LVD_VOLTAGE_CH2_MIN) || (p_cfg->e_voltage_level > LVD_VOLTAGE_CH2_MAX))
        {
            return LVD_ERR_VDET;
        }
    }
    else if (LVD_CHANNEL_1 == e_channel)
    {
        if ((p_cfg->e_voltage_level < LVD_VOLTAGE_CH1_MIN) || (p_cfg->e_voltage_level > LVD_VOLTAGE_CH1_MAX))
        {
            return LVD_ERR_VDET;
        }
    }

    if (p_cfg->e_action >= LVD_ACTION_INVALID)
    {
        return LVD_ERR_ILL_PARAM;
    }

    if(p_cfg->e_trigger >= LVD_TRIGGER_INVALID)
    {
        return LVD_ERR_ILL_PARAM;
    }

    if (((pcallback == FIT_NO_FUNC) || (pcallback == NULL)) &&
         ((LVD_ACTION_RESET != p_cfg->e_action) && (LVD_ACTION_POLL != p_cfg->e_action)))
    {
        return LVD_ERR_ILL_PARAM;
    }

#endif

    if (LVD_CHANNEL_1 == e_channel)
    {
#if (LVD_CFG_CHANNEL_1_USED == 1)
        if (g_lvd_ch1_open == true)
        {
            return LVD_ERR_ILL_REINITIALIZATION;
        }

        R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_LVD);      // unlock LVD control registers
        SYSTEM.LVDLVLR.BIT.LVD1LVL = p_cfg->e_voltage_level;    //set voltage level
#if (BSP_MCU_RX210 == 1)
        SYSTEM.LVCMPCR.BIT.EXVREFINP1 = 0;          // Select internal reference voltage
        SYSTEM.LVCMPCR.BIT.EXVCCINP1 = 0;           // Select Vcc to compare
        SYSTEM.LVD1CR0.BIT.LVD1DFDIS = 1;           // Disable digital filter
#endif
        if (LVD_ACTION_RESET == p_cfg->e_action)
        {
            /*Disable Interrupt/RESET. Enable RESET on Vcc falling to or below Vdet1, Set Reset Negation Stabilization*/
            SYSTEM.LVD1CR0.BYTE = (uint8_t)( 0x40 | (LVD_CFG_STABILIZATION_CHANNEL_1 << 7));
            SYSTEM.LVD1SR.BIT.LVD1DET = 0;          // Clear the passage detection status bit
            // At least 2 PCLKB cycles req'd before LVDnRIE can be set after clearing. This is just an easy way to do it\
               The SFR registers are read over the peripheral bus and thus clocked by PCLKB
            if (SYSTEM.LVD1CR0.BIT.LVD1RIE == 0)
            {
                SYSTEM.LVD1SR.BIT.LVD1DET = 0;      // 2 PCLKB delay
            }
            SYSTEM.LVD1CR0.BIT.LVD1RIE = 1;         // Enable LVD1 Interrupt/RESET
            SYSTEM.LVCMPCR.BIT.LVD1E = 1;           // Enable the LVD1 circuit
            lvd_delay(300);                         // Implement 300usec wait
            SYSTEM.LVD1CR0.BIT.LVD1CMPE = 1;        // Enable comparison output
        }
#if ((BSP_MCU_RX111 == 1) || (BSP_MCU_RX110 == 1) || (BSP_MCU_RX113 == 1) || \
     (BSP_MCU_RX210 == 1) || (BSP_MCU_RX231 == 1))
        else if(LVD_ACTION_IRQ == p_cfg->e_action)
Пример #9
0
/***********************************************************************************************************************
* Function name: output_ports_configure
* Description  : Configures the port and pin direction settings, and sets the pin outputs to a safe level.
* Arguments    : none
* Return value : none
***********************************************************************************************************************/
static void output_ports_configure(void)
{
    /* Unlock MPC registers to enable writing to them. */
    R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_MPC);
    
    MSTP(EDMAC) = 0 ;                   /* Power up ethernet block */
    
    /* Port 0 - DAC & ethernet IRQ */
    PORT0.PODR.BYTE = 0x00 ;    /* All outputs low to start */
    PORT0.PDR.BYTE  = 0x10 ;    /* DA1 is an ouput, all others are inputs */
    
    /* Port 1 - I2C and USB over-current & pull-up control */
    PORT1.PODR.BYTE = 0x00 ;    /* All outputs low to start */
    PORT1.PDR.BYTE  = 0x80 ;    /* AUD_R (P1.7) is an output, all others are inputs (I2C lines setup by 
    *                              I2C driver later */
    
    /* Port 2 - USB control and some expansion signals */
    PORT2.PODR.BYTE = 0x02 ;    /* All outputs low to start except backlight enable */
    PORT2.PDR.BYTE  = 0x26 ;    /* All inputs except backlight enable - some will be overridden by USB driver later */
    
    /* Port 3 - Serial port & JTAG */
    PORT3.PODR.BYTE = 0x00 ;    /* All outputs low to start */
    PORT3.PDR.BIT.B2  = 0x01 ;  /* Transmit line for SCI6/ CAN 0 TxD is an output */
    
    /* Port 4 -  */
    PORT4.PODR.BYTE = 0x00 ;    /* These are all inputs */
    PORT4.PDR.BYTE  = 0x00 ;    /* Analog inputs and switches, all inputs */
    PORT4.PMR.BYTE  = 0x00 ;
    MPC.P42PFS.BYTE = 0x80 ;    /* P42 is used for AD input on pot. */

    /* Port 5 -  */
    PORT5.PODR.BYTE = 0x00 ;    /* All outputs low to start */
    PORT5.PDR.BYTE  = 0x13 ;    /* SCI 2 TxD, LCD_RS, PWMLP_OUT are outputs */
    PORT5.PMR.BYTE  = 0x00 ;    /* All GPIO for now */
    MPC.P50PFS.BYTE = 0x0A ;    /* P50 is TXD2. */
    MPC.P52PFS.BYTE = 0x0A ;    /* P52 is RXD2. */
    PORT5.PMR.BYTE  = 0x05 ;    /* P50 and P52 are used for SCI2. */

    /* Port A - Ethernet MDIO */
//    PORTA.PODR.BYTE = 0x00 ;    /* */
//    PORTA.PMR.BYTE  = 0x00 ;    /* All GPIO for now */
//    MPC.PA3PFS.BYTE = 0x11 ;    /* PA3 is RMII MDIO */
//    MPC.PA4PFS.BYTE = 0x11 ;    /* PA4 is RMII MDC */
//    MPC.PA5PFS.BYTE = 0x11 ;    /* PA5 is RMII LINK_STA */
//    PORTA.PMR.BYTE  = 0x38 ;    /* PA3-5 are used by Ethernet peripheral */
//    PORTA.PDR.BYTE  = 0xFF ;    /* */
    
//    /* Port B - Ethernet signals */
//    PORTB.PODR.BYTE = 0x00 ;    /* */
//    PORTB.PMR.BYTE  = 0x00 ;    /* All GPIO for now */
//    MPC.PB0PFS.BYTE = 0x12 ;    /* PB0 is RMII_RXD1 */
//    MPC.PB1PFS.BYTE = 0x12 ;    /* PB1 is RMII_RXD0 */
//    MPC.PB2PFS.BYTE = 0x12 ;    /* PB2 is REF50CK */
//    MPC.PB3PFS.BYTE = 0x12 ;    /* PB3 is RMI_RX_ERR */
//    MPC.PB4PFS.BYTE = 0x12 ;    /* PB4 is RMII_TXD_EN */
//    MPC.PB5PFS.BYTE = 0x12 ;    /* PB5 is RMII_TXD0 */
//    MPC.PB6PFS.BYTE = 0x12 ;    /* PB6 is RMII_TXD1 */
//    MPC.PB7PFS.BYTE = 0x12 ;    /* PB7 is RMII_CRS_DV */
//    PORTB.PMR.BYTE  = 0xFF ;    /* All pins assigned to peripheral */
//    PORTB.PDR.BYTE  = 0xF0 ;    /* */
    PORTB.PDR.BYTE = 0xFF;
    PORTB.PODR.BIT.B5 = 1;
    /* Port C -  SPI signals, chip selects, peripheral reset */
    PORTC.PODR.BYTE = 0x00 ;    /* */
    PORTC.PMR.BYTE  = 0x00 ;    /* All GPIO for now */
    MPC.PC5PFS.BYTE = 0x0D ;    /* PC5 is RSPCKA */
    MPC.PC6PFS.BYTE = 0x0D ;    /* PC6 is MOSIA */
    MPC.PC7PFS.BYTE = 0x0D ;    /* PC7 is MISOA */
    PORTC.PMR.BYTE  = 0xE0 ;    /* PC5-7 assigned to SPI peripheral */
    PORTC.PODR.BYTE = 0x17 ;    /* All outputs low to start */
    PORTC.PDR.BYTE  = 0x7F ;    /* All outputs except MISO */

    /* Port D -  LED's */
    PORTD.PODR.BYTE = 0xFF ;    /* All outputs LED's off */
    PORTD.PDR.BYTE  = 0xFF ;    /* All outputs */

    /* Port E -  LED's, WiFi & PMOD control */
    PORTE.PODR.BYTE = 0xFF ;    /* All LED's off, all chip selects inactive */
    PORTE.PDR.BYTE  = 0x7F ;    /* All outputs except PMOD_MISO */

    /* Port J -  WiFi chip select */
    PORTJ.PODR.BYTE = 0x08 ;    /* WiFi CS de-asserted at power up */
    PORTJ.PDR.BYTE  = 0x08 ;    /* WiFi CS is an output */

    /* Lock MPC registers. */
    R_BSP_RegisterProtectEnable(BSP_REG_PROTECT_MPC);
}