Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers gpio.c Source File

gpio.c

00001 /**
00002  * @file    gpio.c
00003  * @brief
00004  *
00005  * DAPLink Interface Firmware
00006  * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
00007  * SPDX-License-Identifier: Apache-2.0
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00010  * not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  * http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00017  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 #include "gpio.h"
00023 
00024 static void busy_wait(uint32_t cycles)
00025 {
00026     volatile uint32_t i;
00027     i = cycles;
00028 
00029     while (i > 0) {
00030         i--;
00031     }
00032 }
00033 
00034 void gpio_init(void)
00035 {
00036     SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTE_MASK;
00037     // configure pin as GPIO
00038     PIN_HID_LED_PORT->PCR[PIN_HID_LED_BIT] = PORT_PCR_MUX(1);
00039     PIN_MSC_LED_PORT->PCR[PIN_MSC_LED_BIT] = PORT_PCR_MUX(1);
00040     PIN_CDC_LED_PORT->PCR[PIN_CDC_LED_BIT] = PORT_PCR_MUX(1);
00041     PIN_SW_RESET_PORT->PCR[PIN_SW_RESET_BIT] = PORT_PCR_MUX(1);
00042     PIN_POWER_EN_PORT->PCR[PIN_POWER_EN_BIT] = PORT_PCR_MUX(1);
00043     // led off
00044     gpio_set_hid_led(GPIO_LED_OFF);
00045     gpio_set_cdc_led(GPIO_LED_OFF);
00046     gpio_set_msc_led(GPIO_LED_OFF);
00047     // power regulator on
00048     PIN_POWER_EN_GPIO->PDOR |= PIN_POWER_EN;
00049     // set as output
00050     PIN_HID_LED_GPIO->PDDR |= PIN_HID_LED;
00051     PIN_MSC_LED_GPIO->PDDR |= PIN_MSC_LED;
00052     PIN_CDC_LED_GPIO->PDDR |= PIN_CDC_LED;
00053     PIN_POWER_EN_GPIO->PDDR |= PIN_POWER_EN;
00054     // set as input
00055     PIN_SW_RESET_GPIO->PDDR &= ~PIN_SW_RESET;
00056 
00057     // Let the voltage rails stabilize.  This is especailly important
00058     // during software resets, since the target's 3.3v rail can take
00059     // 20-50ms to drain.  During this time the target could be driving
00060     // the reset pin low, causing the bootloader to think the reset
00061     // button is pressed.
00062     // Note: With optimization set to -O2 the value 1000000 delays for ~85ms
00063     busy_wait(1000000);
00064 }
00065 
00066 void gpio_set_hid_led(gpio_led_state_t state)
00067 {
00068     (GPIO_LED_ON == state) ? (PIN_HID_LED_GPIO->PCOR = PIN_HID_LED) : (PIN_HID_LED_GPIO->PSOR = PIN_HID_LED);
00069 }
00070 
00071 void gpio_set_cdc_led(gpio_led_state_t state)
00072 {
00073     (GPIO_LED_ON == state) ? (PIN_CDC_LED_GPIO->PCOR = PIN_CDC_LED) : (PIN_CDC_LED_GPIO->PSOR = PIN_CDC_LED);
00074 }
00075 
00076 void gpio_set_msc_led(gpio_led_state_t state)
00077 {
00078     (GPIO_LED_ON == state) ? (PIN_MSC_LED_GPIO->PCOR = PIN_MSC_LED) : (PIN_MSC_LED_GPIO->PSOR = PIN_MSC_LED);
00079 }
00080 
00081 uint8_t gpio_get_reset_btn_no_fwrd(void)
00082 {
00083     return 0;
00084 }
00085 
00086 uint8_t gpio_get_reset_btn_fwrd(void)
00087 {
00088     return (PIN_SW_RESET_GPIO->PDIR & PIN_SW_RESET) ? 0 : 1;
00089 }
00090 
00091 void gpio_set_board_power(bool powerEnabled)
00092 {
00093 }