2020-03-24 01:52:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017-2018, Nuvoton Technology Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2017-06-30 12:37:17 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-02-26 02:26:09 +00:00
|
|
|
#include <arm_cmse.h>
|
2017-06-30 12:37:17 +00:00
|
|
|
#include "mbed_assert.h"
|
|
|
|
#include "pinmap.h"
|
|
|
|
#include "PortNames.h"
|
2019-07-18 09:42:43 +00:00
|
|
|
#include "PeripheralNames.h"
|
2017-06-30 12:37:17 +00:00
|
|
|
#include "mbed_error.h"
|
2018-07-03 05:56:02 +00:00
|
|
|
#include "partition_M2351.h"
|
2018-07-12 05:45:16 +00:00
|
|
|
#include "hal_secure.h"
|
2019-03-29 03:41:12 +00:00
|
|
|
#if defined(DOMAIN_NS) && (DOMAIN_NS == 1L) && (TFM_LVL > 0)
|
|
|
|
#include "tfm_ns_lock.h"
|
|
|
|
#endif
|
2017-06-30 12:37:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure pin multi-function
|
|
|
|
*/
|
|
|
|
void pin_function(PinName pin, int data)
|
|
|
|
{
|
2018-07-12 05:45:16 +00:00
|
|
|
pin_function_s(pin, data);
|
2017-06-30 12:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure pin pull-up/pull-down
|
|
|
|
*/
|
|
|
|
void pin_mode(PinName pin, PinMode mode)
|
|
|
|
{
|
|
|
|
MBED_ASSERT(pin != (PinName)NC);
|
|
|
|
uint32_t pin_index = NU_PINNAME_TO_PIN(pin);
|
|
|
|
uint32_t port_index = NU_PINNAME_TO_PORT(pin);
|
|
|
|
GPIO_T *gpio_base = NU_PORT_BASE(port_index);
|
2020-02-06 10:28:25 +00:00
|
|
|
|
2017-06-30 12:37:17 +00:00
|
|
|
uint32_t mode_intern = GPIO_MODE_INPUT;
|
2018-08-30 02:11:48 +00:00
|
|
|
|
2017-06-30 12:37:17 +00:00
|
|
|
switch (mode) {
|
2018-08-30 02:11:48 +00:00
|
|
|
case InputOnly:
|
2017-06-30 12:37:17 +00:00
|
|
|
mode_intern = GPIO_MODE_INPUT;
|
|
|
|
break;
|
2018-08-30 02:11:48 +00:00
|
|
|
|
|
|
|
case PushPullOutput:
|
2017-06-30 12:37:17 +00:00
|
|
|
mode_intern = GPIO_MODE_OUTPUT;
|
|
|
|
break;
|
2018-08-30 02:11:48 +00:00
|
|
|
|
2017-06-30 12:37:17 +00:00
|
|
|
case OpenDrain:
|
|
|
|
mode_intern = GPIO_MODE_OPEN_DRAIN;
|
|
|
|
break;
|
2018-08-30 02:11:48 +00:00
|
|
|
|
|
|
|
case QuasiBidirectional:
|
2017-06-30 12:37:17 +00:00
|
|
|
mode_intern = GPIO_MODE_QUASI;
|
|
|
|
break;
|
2018-08-30 02:11:48 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
/* H/W doesn't support separate configuration for input pull mode/direction.
|
|
|
|
* We expect upper layer would have translated input pull mode/direction
|
|
|
|
* to I/O mode */
|
|
|
|
return;
|
2017-06-30 12:37:17 +00:00
|
|
|
}
|
2018-08-30 02:11:48 +00:00
|
|
|
|
2017-06-30 12:37:17 +00:00
|
|
|
GPIO_SetMode(gpio_base, 1 << pin_index, mode_intern);
|
2018-08-30 02:11:48 +00:00
|
|
|
|
|
|
|
/* Invalid combinations of PinMode/PinDirection
|
|
|
|
*
|
|
|
|
* We assume developer would avoid the following combinations of PinMode/PinDirection
|
|
|
|
* which are invalid:
|
|
|
|
* 1. InputOnly/PIN_OUTPUT
|
|
|
|
* 2. PushPullOutput/PIN_INPUT
|
|
|
|
*/
|
2017-06-30 12:37:17 +00:00
|
|
|
}
|
2018-07-12 05:45:16 +00:00
|
|
|
|
2019-03-29 03:41:12 +00:00
|
|
|
#if defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
|
|
|
|
|
|
|
|
static void pin_function_impl(int32_t pin, int32_t data, bool nonsecure_caller)
|
2018-07-12 05:45:16 +00:00
|
|
|
{
|
|
|
|
MBED_ASSERT(pin != (PinName)NC);
|
|
|
|
uint32_t pin_index = NU_PINNAME_TO_PIN(pin);
|
|
|
|
uint32_t port_index = NU_PINNAME_TO_PORT(pin);
|
2020-02-06 10:28:25 +00:00
|
|
|
|
2018-07-12 05:45:16 +00:00
|
|
|
/* Guard access to secure GPIO from non-secure domain */
|
2020-02-06 10:28:25 +00:00
|
|
|
if (nonsecure_caller &&
|
2018-07-12 05:45:16 +00:00
|
|
|
(! (SCU_INIT_IONSSET_VAL & (1 << (port_index + 0))))) {
|
|
|
|
error("Non-secure domain tries to control secure or undefined GPIO.");
|
|
|
|
}
|
|
|
|
|
|
|
|
__IO uint32_t *GPx_MFPx = ((__IO uint32_t *) &SYS->GPA_MFPL) + port_index * 2 + (pin_index / 8);
|
|
|
|
uint32_t MFP_Msk = NU_MFP_MSK(pin_index);
|
2020-02-06 10:28:25 +00:00
|
|
|
|
2018-07-12 05:45:16 +00:00
|
|
|
// E.g.: SYS->GPA_MFPL = (SYS->GPA_MFPL & (~SYS_GPA_MFPL_PA0MFP_Msk) ) | SYS_GPA_MFPL_PA0MFP_SC0_CD ;
|
|
|
|
*GPx_MFPx = (*GPx_MFPx & (~MFP_Msk)) | data;
|
|
|
|
}
|
2019-03-29 03:41:12 +00:00
|
|
|
|
|
|
|
#if (TFM_LVL > 0)
|
|
|
|
__NONSECURE_ENTRY
|
|
|
|
int32_t pin_function_veneer(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
|
|
|
|
{
|
|
|
|
int32_t pin = (int32_t) arg0;
|
|
|
|
int32_t data = (int32_t) arg1;
|
|
|
|
pin_function_impl(pin, data, cmse_nonsecure_caller());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(DOMAIN_NS) && (DOMAIN_NS == 1) && (TFM_LVL > 0)
|
|
|
|
|
|
|
|
void pin_function_s(int32_t pin, int32_t data)
|
|
|
|
{
|
|
|
|
tfm_ns_lock_dispatch(pin_function_veneer, pin, data, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
|
|
|
|
|
|
|
|
#if (TFM_LVL == 0)
|
|
|
|
__NONSECURE_ENTRY
|
|
|
|
#endif
|
|
|
|
void pin_function_s(int32_t pin, int32_t data)
|
|
|
|
{
|
|
|
|
pin_function_impl(pin, data, cmse_nonsecure_caller());
|
|
|
|
}
|
|
|
|
|
2018-07-12 05:45:16 +00:00
|
|
|
#endif
|