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

« Back to documentation index

Show/hide line numbers gpio.c Source File

gpio.c

00001 /* CMSIS-DAP Interface Firmware
00002  * Copyright (c) 2009-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "max32625.h"
00018 #include "clkman_regs.h"
00019 #include "gpio_regs.h"
00020 #include "IO_Config.h"
00021 #include "gpio.h"
00022 #include "adc_regs.h"
00023 #include "pwrman_regs.h"
00024 #include "ioman_regs.h"
00025 
00026 // For channels 4 and 5; the target VIO inputs
00027 // 1.6V is the lowest VIO can operate at,
00028 // so set this as minimum acceptable voltage.
00029 // round(AIN/(5 * 1.2)*(2^10-1))
00030 #define VIO_ADC_MIN 273
00031 
00032 // Bitband pointers
00033 volatile uint32_t *tck_in;
00034 volatile uint32_t *tck_out;
00035 volatile uint32_t *tms_in;
00036 volatile uint32_t *tms_out;
00037 volatile uint32_t *rst_in;
00038 volatile uint32_t *rst_out;
00039 
00040 uint32_t swdio_port;
00041 uint32_t swdio_pin;
00042 uint32_t swclk_port;
00043 uint32_t swclk_pin;
00044 uint32_t nreset_port;
00045 uint32_t nreset_pin;
00046 
00047 int32_t uart_set_instance(uint32_t inst);
00048 
00049 /******************************************************************************/
00050 static inline void use_vddioh(int port, int pin)
00051 {
00052     if (port > 3) {
00053         MXC_IOMAN->use_vddioh_1 |= 1U << (((port - 4) * 8) + pin);
00054     } else {
00055         MXC_IOMAN->use_vddioh_0 |= 1U << ((port * 8) + pin);
00056     }
00057 }
00058 
00059 /******************************************************************************/
00060 static uint16_t readADC(uint8_t ch)
00061 {
00062     uint32_t ctrl_tmp;
00063 
00064     // Select channel
00065     ctrl_tmp = MXC_ADC->ctrl;
00066     ctrl_tmp &= ~MXC_F_ADC_CTRL_ADC_CHSEL;
00067     ctrl_tmp |= ((ch << MXC_F_ADC_CTRL_ADC_CHSEL_POS) & MXC_F_ADC_CTRL_ADC_CHSEL);
00068 
00069     // Clear channel configuration
00070     ctrl_tmp &= ~(MXC_F_ADC_CTRL_ADC_REFSCL | MXC_F_ADC_CTRL_ADC_SCALE | MXC_F_ADC_CTRL_BUF_BYPASS);
00071 
00072     // ADC reference scaling
00073     ctrl_tmp |= MXC_F_ADC_CTRL_ADC_REFSCL;
00074 
00075     if ((ch == SWD_VIO_CH) || (ch == HDR_VIO_CH)) {
00076         ctrl_tmp |= MXC_F_ADC_CTRL_ADC_SCALE;
00077     }
00078 
00079     // Do not bypass buffer, except when measuring near ground.
00080     // This may be the case for measuring 1-Wire ground detect on channels 2 and 3.
00081     if ((ch == SWD_GNDDET_CH) || (ch == HDR_GNDDET_CH)) {
00082         ctrl_tmp |= MXC_F_ADC_CTRL_BUF_BYPASS;
00083     }
00084 
00085     // Write this configuration
00086     MXC_ADC->ctrl = ctrl_tmp;
00087 
00088     // Clear conversion done interrupt flag
00089     MXC_ADC->intr = MXC_F_ADC_INTR_ADC_DONE_IF;
00090 
00091     // Start conversion
00092     MXC_ADC->ctrl |= MXC_F_ADC_CTRL_CPU_ADC_START;
00093 
00094     while (!(MXC_ADC->intr & MXC_F_ADC_INTR_ADC_DONE_IF));
00095 
00096     return (uint16_t)(MXC_ADC->data);
00097 }
00098 
00099 /******************************************************************************/
00100 void target_set_interface(TARGET_INTERFACE mode)
00101 {
00102     switch (mode) {
00103         case IO_SWD_EXT:
00104             swdio_port = PIN_SWDIO_PORT;
00105             swdio_pin = PIN_SWDIO_PIN;
00106             swclk_port = PIN_SWCLK_PORT;
00107             swclk_pin = PIN_SWCLK_PIN;
00108             nreset_port = PIN_nRESET_PORT;
00109             nreset_pin = PIN_nRESET_PIN;
00110 
00111             MXC_CLRBIT(&MXC_GPIO->out_val[EN_VDDIOH_PORT], EN_VDDIOH_PIN);      // Low to disable SWOUT
00112             MXC_SETBIT(&MXC_GPIO->out_val[IOH_OW_EN_PORT], IOH_OW_EN_PIN);      // High to power MAX14689
00113             MXC_SETBIT(&MXC_GPIO->out_val[SWD_DIP_SEL_PORT], SWD_DIP_SEL_PIN);  // High to connect Bn <-> Cn
00114             uart_set_instance(CDC_ACM_UART_SWD);
00115             break;
00116         case IO_DIP_EXT:
00117             swdio_port = PIN_DIP_SWDIO_PORT;
00118             swdio_pin = PIN_DIP_SWDIO_PIN;
00119             swclk_port = PIN_DIP_SWCLK_PORT;
00120             swclk_pin = PIN_DIP_SWCLK_PIN;
00121             nreset_port = PIN_DIP_nRESET_PORT;
00122             nreset_pin = PIN_DIP_nRESET_PIN;
00123 
00124             MXC_CLRBIT(&MXC_GPIO->out_val[EN_VDDIOH_PORT], EN_VDDIOH_PIN);      // Low to disable SWOUT
00125             MXC_SETBIT(&MXC_GPIO->out_val[IOH_OW_EN_PORT], IOH_OW_EN_PIN);      // High to power MAX14689
00126             MXC_CLRBIT(&MXC_GPIO->out_val[SWD_DIP_SEL_PORT], SWD_DIP_SEL_PIN);  // Low to connect Bn <-> An
00127             uart_set_instance(CDC_ACM_UART_DIP);
00128             break;
00129     }
00130 }
00131 
00132 /******************************************************************************/
00133 void gpio_init(void)
00134 {
00135     int i;
00136     uint32_t out_mode;
00137 
00138     // Ensure that the GPIO clock is enabled
00139     if (MXC_CLKMAN->sys_clk_ctrl_6_gpio == MXC_S_CLKMAN_CLK_SCALE_DISABLED) {
00140         MXC_CLKMAN->sys_clk_ctrl_6_gpio = MXC_S_CLKMAN_CLK_SCALE_DIV_1;
00141     }
00142 
00143     // Make all GPIO pins readable
00144     for (i = 0; i < MXC_GPIO_NUM_PORTS; i++) {
00145         MXC_GPIO->in_mode[i] = 0x00000000;
00146     }
00147 
00148     // Set to enable the board to power VDDIOH and in turn the target micro
00149     // Clear to disable the board from powering VDDIOH
00150     MXC_CLRBIT(&MXC_GPIO->out_val[EN_VDDIOH_PORT], EN_VDDIOH_PIN);
00151     out_mode = MXC_GPIO->out_mode[EN_VDDIOH_PORT];
00152     out_mode &= ~(0xFU << (4 * EN_VDDIOH_PIN));
00153     out_mode |= (MXC_V_GPIO_OUT_MODE_NORMAL << (4 * EN_VDDIOH_PIN));
00154     MXC_GPIO->out_mode[EN_VDDIOH_PORT] = out_mode;
00155 
00156     // LED initial state off
00157     MXC_GPIO->out_val[PIN_DAP_LED_PORT] |= (1 << PIN_DAP_LED_PIN);
00158     MXC_GPIO->out_val[PIN_MSD_LED_PORT] |= (1 << PIN_MSD_LED_PIN);
00159     MXC_GPIO->out_val[PIN_CDC_LED_PORT] |= (1 << PIN_CDC_LED_PIN);
00160 
00161     // LED outputs
00162     out_mode = MXC_GPIO->out_mode[PIN_DAP_LED_PORT];
00163     out_mode &= ~(0xFU << (4 * PIN_DAP_LED_PIN));
00164     out_mode |= (MXC_V_GPIO_OUT_MODE_OPEN_DRAIN << (4 * PIN_DAP_LED_PIN));
00165     MXC_GPIO->out_mode[PIN_DAP_LED_PORT] = out_mode;
00166 
00167     out_mode = MXC_GPIO->out_mode[PIN_MSD_LED_PORT];
00168     out_mode &= ~(0xFU << (4 * PIN_MSD_LED_PIN));
00169     out_mode |= (MXC_V_GPIO_OUT_MODE_OPEN_DRAIN << (4 * PIN_MSD_LED_PIN));
00170     MXC_GPIO->out_mode[PIN_MSD_LED_PORT] = out_mode;
00171 
00172     out_mode = MXC_GPIO->out_mode[PIN_CDC_LED_PORT];
00173     out_mode &= ~(0xFU << (4 * PIN_CDC_LED_PIN));
00174     out_mode |= (MXC_V_GPIO_OUT_MODE_OPEN_DRAIN << (4 * PIN_CDC_LED_PIN));
00175     MXC_GPIO->out_mode[PIN_CDC_LED_PORT] = out_mode;
00176 
00177     // Button Input
00178     out_mode = MXC_GPIO->out_mode[PIN_RESET_IN_NO_FWRD_PORT];
00179     out_mode &= ~(0xFU << (4 * PIN_RESET_IN_NO_FWRD_PIN));
00180     out_mode |= (MXC_V_GPIO_OUT_MODE_OPEN_DRAIN_WEAK_PULLUP << (4 * PIN_RESET_IN_NO_FWRD_PIN));
00181     MXC_GPIO->out_mode[PIN_RESET_IN_NO_FWRD_PORT] = out_mode;
00182     MXC_GPIO->out_val[PIN_RESET_IN_NO_FWRD_PORT] |= (0x1U << PIN_RESET_IN_NO_FWRD_PIN);
00183 
00184     // IOH_1W_EN (must be configured for strong drive)
00185     MXC_SETBIT(&MXC_GPIO->out_val[IOH_OW_EN_PORT], IOH_OW_EN_PIN);
00186     out_mode = MXC_GPIO->out_mode[IOH_OW_EN_PORT];
00187     out_mode &= ~(0xFU << (4 * IOH_OW_EN_PIN));
00188     out_mode |= (MXC_V_GPIO_OUT_MODE_FAST_DRIVE << (4 * IOH_OW_EN_PIN));
00189     MXC_GPIO->out_mode[IOH_OW_EN_PORT] = out_mode;
00190 
00191     // SWD_DIP_SEL (must be configured for strong drive)
00192     MXC_CLRBIT(&MXC_GPIO->out_val[SWD_DIP_SEL_PORT], SWD_DIP_SEL_PIN);
00193     out_mode = MXC_GPIO->out_mode[SWD_DIP_SEL_PORT];
00194     out_mode &= ~(0xFU << (4 * SWD_DIP_SEL_PIN));
00195     out_mode |= (MXC_V_GPIO_OUT_MODE_FAST_DRIVE << (4 * SWD_DIP_SEL_PIN));
00196     MXC_GPIO->out_mode[SWD_DIP_SEL_PORT] = out_mode;
00197 
00198     // Strong pull-up disable
00199     MXC_SETBIT(&MXC_GPIO->out_val[OWM_SUP_PORT], OWM_SUP_PIN);
00200     out_mode = MXC_GPIO->out_mode[OWM_SUP_PORT];
00201     out_mode &= ~(0xFU << (4 * OWM_SUP_PIN));
00202     out_mode |= (MXC_V_GPIO_OUT_MODE_NORMAL << (4 * OWM_SUP_PIN));
00203     MXC_GPIO->out_mode[OWM_SUP_PORT] = out_mode;
00204 
00205     // VDDIOH driver isn't strong enough with the strong pull-up
00206     MXC_IOMAN->use_vddioh_1 &= ~(1U << (((OWM_PORT - 4) * 8) + OWM_PIN));
00207     // 1V8 VDDIO is not high enough to turn off the FET if VDDIOH is 3V3
00208     MXC_IOMAN->use_vddioh_1 |= (1U << (((OWM_PORT - 4) * 8) + OWM_SUP_PIN));
00209 
00210     use_vddioh(PIN_nRESET_PORT, PIN_nRESET_PIN);
00211     use_vddioh(PIN_DIP_nRESET_PORT, PIN_DIP_nRESET_PIN);
00212     use_vddioh(PIN_SWCLK_PORT, PIN_SWCLK_PIN);
00213     use_vddioh(PIN_DIP_SWCLK_PORT, PIN_DIP_SWCLK_PIN);
00214     use_vddioh(PIN_SWDIO_PORT, PIN_SWDIO_PIN);
00215     use_vddioh(PIN_DIP_SWDIO_PORT, PIN_DIP_SWDIO_PIN);
00216     use_vddioh(PIN_TX_PORT, PIN_TX_PIN);
00217     use_vddioh(PIN_RX_PORT, PIN_RX_PIN);
00218     use_vddioh(PIN_DIP_TX_PORT, PIN_DIP_TX_PIN);
00219     use_vddioh(PIN_DIP_RX_PORT, PIN_DIP_RX_PIN);
00220 
00221     // Setup the ADC; read the ADC to set IO interface
00222     MXC_PWRMAN->pwr_rst_ctrl |= MXC_F_PWRMAN_PWR_RST_CTRL_AFE_POWERED;
00223     MXC_CLKMAN->clk_ctrl |= MXC_F_CLKMAN_CLK_CTRL_ADC_CLOCK_ENABLE;
00224 
00225     MXC_ADC->ctrl = (MXC_F_ADC_CTRL_ADC_PU |
00226                      MXC_F_ADC_CTRL_ADC_CLK_EN |
00227                      MXC_F_ADC_CTRL_BUF_PU |
00228                      MXC_F_ADC_CTRL_ADC_REFBUF_PU |
00229                      MXC_F_ADC_CTRL_ADC_CHGPUMP_PU);
00230 
00231     // Set IO interface
00232     if (readADC(SWD_VIO_CH) > VIO_ADC_MIN) {
00233         target_set_interface(IO_SWD_EXT);
00234     } else if (readADC(HDR_VIO_CH) > VIO_ADC_MIN) {
00235         target_set_interface(IO_DIP_EXT);
00236     } else {
00237         // Default to SWD interface
00238         target_set_interface(IO_SWD_EXT);
00239     }
00240 
00241 }
00242 
00243 /******************************************************************************/
00244 void gpio_set_hid_led(gpio_led_state_t state)
00245 {
00246     if (state == GPIO_LED_ON) {
00247         MXC_CLRBIT(&MXC_GPIO->out_val[PIN_DAP_LED_PORT], PIN_DAP_LED_PIN);
00248     } else {
00249         MXC_SETBIT(&MXC_GPIO->out_val[PIN_DAP_LED_PORT], PIN_DAP_LED_PIN);
00250     }
00251 }
00252 
00253 /******************************************************************************/
00254 void gpio_set_msc_led(gpio_led_state_t state)
00255 {
00256     if (state == GPIO_LED_ON) {
00257         MXC_CLRBIT(&MXC_GPIO->out_val[PIN_MSD_LED_PORT], PIN_MSD_LED_PIN);
00258     } else {
00259         MXC_SETBIT(&MXC_GPIO->out_val[PIN_MSD_LED_PORT], PIN_MSD_LED_PIN);
00260     }
00261 }
00262 
00263 /******************************************************************************/
00264 void gpio_set_cdc_led(gpio_led_state_t state)
00265 {
00266     if (state == GPIO_LED_ON) {
00267         MXC_CLRBIT(&MXC_GPIO->out_val[PIN_CDC_LED_PORT], PIN_CDC_LED_PIN);
00268     } else {
00269         MXC_SETBIT(&MXC_GPIO->out_val[PIN_CDC_LED_PORT], PIN_CDC_LED_PIN);
00270     }
00271 }
00272 
00273 /******************************************************************************/
00274 uint8_t gpio_get_reset_btn_no_fwrd(void)
00275 {
00276     return !MXC_GETBIT(&MXC_GPIO->in_val[PIN_RESET_IN_NO_FWRD_PORT], PIN_RESET_IN_NO_FWRD_PIN);
00277 }
00278 
00279 /******************************************************************************/
00280 uint8_t gpio_get_reset_btn_fwrd(void)
00281 {
00282     return 0;
00283 }
00284 
00285 /******************************************************************************/
00286 void gpio_set_board_power(bool powerEnabled)
00287 {
00288 }