Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.

Upstream: https://github.com/ARMmbed/DAPLink

Committer:
Pawel Zarembski
Date:
Tue Apr 07 12:55:42 2020 +0200
Revision:
0:01f31e923fe2
hani: DAPLink with reset workaround

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pawel Zarembski 0:01f31e923fe2 1 /**
Pawel Zarembski 0:01f31e923fe2 2 * @file gpio.c
Pawel Zarembski 0:01f31e923fe2 3 * @brief
Pawel Zarembski 0:01f31e923fe2 4 *
Pawel Zarembski 0:01f31e923fe2 5 * DAPLink Interface Firmware
Pawel Zarembski 0:01f31e923fe2 6 * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
Pawel Zarembski 0:01f31e923fe2 7 * SPDX-License-Identifier: Apache-2.0
Pawel Zarembski 0:01f31e923fe2 8 *
Pawel Zarembski 0:01f31e923fe2 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Pawel Zarembski 0:01f31e923fe2 10 * not use this file except in compliance with the License.
Pawel Zarembski 0:01f31e923fe2 11 * You may obtain a copy of the License at
Pawel Zarembski 0:01f31e923fe2 12 *
Pawel Zarembski 0:01f31e923fe2 13 * http://www.apache.org/licenses/LICENSE-2.0
Pawel Zarembski 0:01f31e923fe2 14 *
Pawel Zarembski 0:01f31e923fe2 15 * Unless required by applicable law or agreed to in writing, software
Pawel Zarembski 0:01f31e923fe2 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Pawel Zarembski 0:01f31e923fe2 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Pawel Zarembski 0:01f31e923fe2 18 * See the License for the specific language governing permissions and
Pawel Zarembski 0:01f31e923fe2 19 * limitations under the License.
Pawel Zarembski 0:01f31e923fe2 20 */
Pawel Zarembski 0:01f31e923fe2 21
Pawel Zarembski 0:01f31e923fe2 22 #include "stm32f1xx.h"
Pawel Zarembski 0:01f31e923fe2 23 #include "DAP_config.h"
Pawel Zarembski 0:01f31e923fe2 24 #include "gpio.h"
Pawel Zarembski 0:01f31e923fe2 25 #include "daplink.h"
Pawel Zarembski 0:01f31e923fe2 26 #include "util.h"
Pawel Zarembski 0:01f31e923fe2 27
Pawel Zarembski 0:01f31e923fe2 28 static TIM_HandleTypeDef timer;
Pawel Zarembski 0:01f31e923fe2 29
Pawel Zarembski 0:01f31e923fe2 30 static void busy_wait(uint32_t cycles)
Pawel Zarembski 0:01f31e923fe2 31 {
Pawel Zarembski 0:01f31e923fe2 32 volatile uint32_t i;
Pawel Zarembski 0:01f31e923fe2 33 i = cycles;
Pawel Zarembski 0:01f31e923fe2 34
Pawel Zarembski 0:01f31e923fe2 35 while (i > 0) {
Pawel Zarembski 0:01f31e923fe2 36 i--;
Pawel Zarembski 0:01f31e923fe2 37 }
Pawel Zarembski 0:01f31e923fe2 38 }
Pawel Zarembski 0:01f31e923fe2 39
Pawel Zarembski 0:01f31e923fe2 40 static uint32_t tim1_clk_div(uint32_t apb2clkdiv)
Pawel Zarembski 0:01f31e923fe2 41 {
Pawel Zarembski 0:01f31e923fe2 42 switch (apb2clkdiv) {
Pawel Zarembski 0:01f31e923fe2 43 case RCC_CFGR_PPRE2_DIV2:
Pawel Zarembski 0:01f31e923fe2 44 return 1;
Pawel Zarembski 0:01f31e923fe2 45 case RCC_CFGR_PPRE2_DIV4:
Pawel Zarembski 0:01f31e923fe2 46 return 2;
Pawel Zarembski 0:01f31e923fe2 47 case RCC_CFGR_PPRE2_DIV8:
Pawel Zarembski 0:01f31e923fe2 48 return 4;
Pawel Zarembski 0:01f31e923fe2 49 default: // RCC_CFGR_PPRE2_DIV1
Pawel Zarembski 0:01f31e923fe2 50 return 1;
Pawel Zarembski 0:01f31e923fe2 51 }
Pawel Zarembski 0:01f31e923fe2 52 }
Pawel Zarembski 0:01f31e923fe2 53
Pawel Zarembski 0:01f31e923fe2 54 static void output_clock_enable(void)
Pawel Zarembski 0:01f31e923fe2 55 {
Pawel Zarembski 0:01f31e923fe2 56 HAL_StatusTypeDef ret;
Pawel Zarembski 0:01f31e923fe2 57 RCC_ClkInitTypeDef clk_init;
Pawel Zarembski 0:01f31e923fe2 58 TIM_OC_InitTypeDef pwm_config;
Pawel Zarembski 0:01f31e923fe2 59 uint32_t unused;
Pawel Zarembski 0:01f31e923fe2 60 uint32_t period;
Pawel Zarembski 0:01f31e923fe2 61 uint32_t source_clock;
Pawel Zarembski 0:01f31e923fe2 62
Pawel Zarembski 0:01f31e923fe2 63 HAL_RCC_GetClockConfig(&clk_init, &unused);
Pawel Zarembski 0:01f31e923fe2 64
Pawel Zarembski 0:01f31e923fe2 65 /* Compute the period value to have TIMx counter clock equal to 8000000 Hz */
Pawel Zarembski 0:01f31e923fe2 66 source_clock = SystemCoreClock / tim1_clk_div(clk_init.APB2CLKDivider);
Pawel Zarembski 0:01f31e923fe2 67 period = (uint32_t)(source_clock / 8000000) - 1;
Pawel Zarembski 0:01f31e923fe2 68
Pawel Zarembski 0:01f31e923fe2 69 /* Set TIMx instance */
Pawel Zarembski 0:01f31e923fe2 70 timer.Instance = TIM1;
Pawel Zarembski 0:01f31e923fe2 71
Pawel Zarembski 0:01f31e923fe2 72 timer.Init.Period = period;
Pawel Zarembski 0:01f31e923fe2 73 timer.Init.Prescaler = 0;
Pawel Zarembski 0:01f31e923fe2 74 timer.Init.ClockDivision = 0;
Pawel Zarembski 0:01f31e923fe2 75 timer.Init.CounterMode = TIM_COUNTERMODE_UP;
Pawel Zarembski 0:01f31e923fe2 76 timer.Init.RepetitionCounter = 0;//period / 2;
Pawel Zarembski 0:01f31e923fe2 77
Pawel Zarembski 0:01f31e923fe2 78 __HAL_RCC_TIM1_CLK_ENABLE();
Pawel Zarembski 0:01f31e923fe2 79
Pawel Zarembski 0:01f31e923fe2 80 ret = HAL_TIM_PWM_DeInit(&timer);
Pawel Zarembski 0:01f31e923fe2 81 if (ret != HAL_OK) {
Pawel Zarembski 0:01f31e923fe2 82 util_assert(0);
Pawel Zarembski 0:01f31e923fe2 83 return;
Pawel Zarembski 0:01f31e923fe2 84 }
Pawel Zarembski 0:01f31e923fe2 85
Pawel Zarembski 0:01f31e923fe2 86 ret = HAL_TIM_PWM_Init(&timer);
Pawel Zarembski 0:01f31e923fe2 87 if (ret != HAL_OK) {
Pawel Zarembski 0:01f31e923fe2 88 util_assert(0);
Pawel Zarembski 0:01f31e923fe2 89 return;
Pawel Zarembski 0:01f31e923fe2 90 }
Pawel Zarembski 0:01f31e923fe2 91
Pawel Zarembski 0:01f31e923fe2 92 pwm_config.OCMode = TIM_OCMODE_PWM2;
Pawel Zarembski 0:01f31e923fe2 93 pwm_config.Pulse = 0; // TODO - make sure this isn't used
Pawel Zarembski 0:01f31e923fe2 94 pwm_config.OCPolarity = TIM_OCPOLARITY_HIGH;
Pawel Zarembski 0:01f31e923fe2 95 pwm_config.OCNPolarity = TIM_OCPOLARITY_HIGH;
Pawel Zarembski 0:01f31e923fe2 96 pwm_config.OCFastMode = TIM_OCFAST_DISABLE;
Pawel Zarembski 0:01f31e923fe2 97 pwm_config.OCIdleState = TIM_OCIDLESTATE_RESET;
Pawel Zarembski 0:01f31e923fe2 98 pwm_config.OCNIdleState = TIM_OCIDLESTATE_RESET;
Pawel Zarembski 0:01f31e923fe2 99 ret = HAL_TIM_PWM_ConfigChannel(&timer, &pwm_config, TIM_CHANNEL_1);
Pawel Zarembski 0:01f31e923fe2 100 if (ret != HAL_OK) {
Pawel Zarembski 0:01f31e923fe2 101 util_assert(0);
Pawel Zarembski 0:01f31e923fe2 102 return;
Pawel Zarembski 0:01f31e923fe2 103 }
Pawel Zarembski 0:01f31e923fe2 104
Pawel Zarembski 0:01f31e923fe2 105 __HAL_TIM_SET_COMPARE(&timer, TIM_CHANNEL_1, period / 2);
Pawel Zarembski 0:01f31e923fe2 106 ret = HAL_TIM_PWM_Start(&timer, TIM_CHANNEL_1);
Pawel Zarembski 0:01f31e923fe2 107 if (ret != HAL_OK) {
Pawel Zarembski 0:01f31e923fe2 108 util_assert(0);
Pawel Zarembski 0:01f31e923fe2 109 return;
Pawel Zarembski 0:01f31e923fe2 110 }
Pawel Zarembski 0:01f31e923fe2 111
Pawel Zarembski 0:01f31e923fe2 112 return;
Pawel Zarembski 0:01f31e923fe2 113 }
Pawel Zarembski 0:01f31e923fe2 114
Pawel Zarembski 0:01f31e923fe2 115 void gpio_init(void)
Pawel Zarembski 0:01f31e923fe2 116 {
Pawel Zarembski 0:01f31e923fe2 117 GPIO_InitTypeDef GPIO_InitStructure;
Pawel Zarembski 0:01f31e923fe2 118 // enable clock to ports
Pawel Zarembski 0:01f31e923fe2 119 __HAL_RCC_GPIOA_CLK_ENABLE();
Pawel Zarembski 0:01f31e923fe2 120 __HAL_RCC_GPIOB_CLK_ENABLE();
Pawel Zarembski 0:01f31e923fe2 121 __HAL_RCC_GPIOC_CLK_ENABLE();
Pawel Zarembski 0:01f31e923fe2 122 __HAL_RCC_GPIOD_CLK_ENABLE();
Pawel Zarembski 0:01f31e923fe2 123 // Enable USB connect pin
Pawel Zarembski 0:01f31e923fe2 124 __HAL_RCC_AFIO_CLK_ENABLE();
Pawel Zarembski 0:01f31e923fe2 125 // Disable JTAG to free pins for other uses
Pawel Zarembski 0:01f31e923fe2 126 // Note - SWD is still enabled
Pawel Zarembski 0:01f31e923fe2 127 __HAL_AFIO_REMAP_SWJ_NOJTAG();
Pawel Zarembski 0:01f31e923fe2 128
Pawel Zarembski 0:01f31e923fe2 129 USB_CONNECT_PORT_ENABLE();
Pawel Zarembski 0:01f31e923fe2 130 USB_CONNECT_OFF();
Pawel Zarembski 0:01f31e923fe2 131 GPIO_InitStructure.Pin = USB_CONNECT_PIN;
Pawel Zarembski 0:01f31e923fe2 132 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
Pawel Zarembski 0:01f31e923fe2 133 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
Pawel Zarembski 0:01f31e923fe2 134 HAL_GPIO_Init(USB_CONNECT_PORT, &GPIO_InitStructure);
Pawel Zarembski 0:01f31e923fe2 135 // configure LEDs
Pawel Zarembski 0:01f31e923fe2 136 HAL_GPIO_WritePin(RUNNING_LED_PORT, RUNNING_LED_PIN, GPIO_PIN_SET);
Pawel Zarembski 0:01f31e923fe2 137 GPIO_InitStructure.Pin = RUNNING_LED_PIN;
Pawel Zarembski 0:01f31e923fe2 138 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
Pawel Zarembski 0:01f31e923fe2 139 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
Pawel Zarembski 0:01f31e923fe2 140 HAL_GPIO_Init(RUNNING_LED_PORT, &GPIO_InitStructure);
Pawel Zarembski 0:01f31e923fe2 141
Pawel Zarembski 0:01f31e923fe2 142 HAL_GPIO_WritePin(CONNECTED_LED_PORT, CONNECTED_LED_PIN, GPIO_PIN_SET);
Pawel Zarembski 0:01f31e923fe2 143 GPIO_InitStructure.Pin = CONNECTED_LED_PIN;
Pawel Zarembski 0:01f31e923fe2 144 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
Pawel Zarembski 0:01f31e923fe2 145 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
Pawel Zarembski 0:01f31e923fe2 146 HAL_GPIO_Init(CONNECTED_LED_PORT, &GPIO_InitStructure);
Pawel Zarembski 0:01f31e923fe2 147
Pawel Zarembski 0:01f31e923fe2 148 HAL_GPIO_WritePin(PIN_CDC_LED_PORT, PIN_CDC_LED, GPIO_PIN_SET);
Pawel Zarembski 0:01f31e923fe2 149 GPIO_InitStructure.Pin = PIN_CDC_LED;
Pawel Zarembski 0:01f31e923fe2 150 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
Pawel Zarembski 0:01f31e923fe2 151 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
Pawel Zarembski 0:01f31e923fe2 152 HAL_GPIO_Init(PIN_CDC_LED_PORT, &GPIO_InitStructure);
Pawel Zarembski 0:01f31e923fe2 153
Pawel Zarembski 0:01f31e923fe2 154 HAL_GPIO_WritePin(PIN_MSC_LED_PORT, PIN_MSC_LED, GPIO_PIN_SET);
Pawel Zarembski 0:01f31e923fe2 155 GPIO_InitStructure.Pin = PIN_MSC_LED;
Pawel Zarembski 0:01f31e923fe2 156 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
Pawel Zarembski 0:01f31e923fe2 157 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
Pawel Zarembski 0:01f31e923fe2 158 HAL_GPIO_Init(PIN_MSC_LED_PORT, &GPIO_InitStructure);
Pawel Zarembski 0:01f31e923fe2 159
Pawel Zarembski 0:01f31e923fe2 160 // reset button configured as gpio open drain output with a pullup
Pawel Zarembski 0:01f31e923fe2 161 HAL_GPIO_WritePin(nRESET_PIN_PORT, nRESET_PIN, GPIO_PIN_SET);
Pawel Zarembski 0:01f31e923fe2 162 GPIO_InitStructure.Pin = nRESET_PIN;
Pawel Zarembski 0:01f31e923fe2 163 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
Pawel Zarembski 0:01f31e923fe2 164 GPIO_InitStructure.Pull = GPIO_PULLUP;
Pawel Zarembski 0:01f31e923fe2 165 HAL_GPIO_Init(nRESET_PIN_PORT, &GPIO_InitStructure);
Pawel Zarembski 0:01f31e923fe2 166
Pawel Zarembski 0:01f31e923fe2 167 // Turn on power to the board. When the target is unpowered
Pawel Zarembski 0:01f31e923fe2 168 // it holds the reset line low.
Pawel Zarembski 0:01f31e923fe2 169 HAL_GPIO_WritePin(POWER_EN_PIN_PORT, POWER_EN_PIN, GPIO_PIN_RESET);
Pawel Zarembski 0:01f31e923fe2 170 GPIO_InitStructure.Pin = POWER_EN_PIN;
Pawel Zarembski 0:01f31e923fe2 171 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
Pawel Zarembski 0:01f31e923fe2 172 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
Pawel Zarembski 0:01f31e923fe2 173 HAL_GPIO_Init(POWER_EN_PIN_PORT, &GPIO_InitStructure);
Pawel Zarembski 0:01f31e923fe2 174
Pawel Zarembski 0:01f31e923fe2 175 // Setup the 8MHz MCO
Pawel Zarembski 0:01f31e923fe2 176 GPIO_InitStructure.Pin = GPIO_PIN_8;
Pawel Zarembski 0:01f31e923fe2 177 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
Pawel Zarembski 0:01f31e923fe2 178 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
Pawel Zarembski 0:01f31e923fe2 179 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
Pawel Zarembski 0:01f31e923fe2 180 output_clock_enable();
Pawel Zarembski 0:01f31e923fe2 181
Pawel Zarembski 0:01f31e923fe2 182 // Let the voltage rails stabilize. This is especailly important
Pawel Zarembski 0:01f31e923fe2 183 // during software resets, since the target's 3.3v rail can take
Pawel Zarembski 0:01f31e923fe2 184 // 20-50ms to drain. During this time the target could be driving
Pawel Zarembski 0:01f31e923fe2 185 // the reset pin low, causing the bootloader to think the reset
Pawel Zarembski 0:01f31e923fe2 186 // button is pressed.
Pawel Zarembski 0:01f31e923fe2 187 // Note: With optimization set to -O2 the value 1000000 delays for ~85ms
Pawel Zarembski 0:01f31e923fe2 188 busy_wait(1000000);
Pawel Zarembski 0:01f31e923fe2 189 }
Pawel Zarembski 0:01f31e923fe2 190
Pawel Zarembski 0:01f31e923fe2 191 void gpio_set_hid_led(gpio_led_state_t state)
Pawel Zarembski 0:01f31e923fe2 192 {
Pawel Zarembski 0:01f31e923fe2 193 // LED is active low
Pawel Zarembski 0:01f31e923fe2 194 HAL_GPIO_WritePin(PIN_HID_LED_PORT, PIN_HID_LED, state ? GPIO_PIN_RESET : GPIO_PIN_SET);
Pawel Zarembski 0:01f31e923fe2 195 }
Pawel Zarembski 0:01f31e923fe2 196
Pawel Zarembski 0:01f31e923fe2 197 void gpio_set_cdc_led(gpio_led_state_t state)
Pawel Zarembski 0:01f31e923fe2 198 {
Pawel Zarembski 0:01f31e923fe2 199 // LED is active low
Pawel Zarembski 0:01f31e923fe2 200 HAL_GPIO_WritePin(PIN_CDC_LED_PORT, PIN_CDC_LED, state ? GPIO_PIN_RESET : GPIO_PIN_SET);
Pawel Zarembski 0:01f31e923fe2 201 }
Pawel Zarembski 0:01f31e923fe2 202
Pawel Zarembski 0:01f31e923fe2 203 void gpio_set_msc_led(gpio_led_state_t state)
Pawel Zarembski 0:01f31e923fe2 204 {
Pawel Zarembski 0:01f31e923fe2 205 // LED is active low
Pawel Zarembski 0:01f31e923fe2 206 HAL_GPIO_WritePin(PIN_MSC_LED_PORT, PIN_MSC_LED, state ? GPIO_PIN_RESET : GPIO_PIN_SET);
Pawel Zarembski 0:01f31e923fe2 207 }
Pawel Zarembski 0:01f31e923fe2 208
Pawel Zarembski 0:01f31e923fe2 209 uint8_t gpio_get_reset_btn_no_fwrd(void)
Pawel Zarembski 0:01f31e923fe2 210 {
Pawel Zarembski 0:01f31e923fe2 211 return (nRESET_PIN_PORT->IDR & nRESET_PIN) ? 0 : 1;
Pawel Zarembski 0:01f31e923fe2 212 }
Pawel Zarembski 0:01f31e923fe2 213
Pawel Zarembski 0:01f31e923fe2 214 uint8_t gpio_get_reset_btn_fwrd(void)
Pawel Zarembski 0:01f31e923fe2 215 {
Pawel Zarembski 0:01f31e923fe2 216 return 0;
Pawel Zarembski 0:01f31e923fe2 217 }
Pawel Zarembski 0:01f31e923fe2 218
Pawel Zarembski 0:01f31e923fe2 219
Pawel Zarembski 0:01f31e923fe2 220 uint8_t GPIOGetButtonState(void)
Pawel Zarembski 0:01f31e923fe2 221 {
Pawel Zarembski 0:01f31e923fe2 222 return 0;
Pawel Zarembski 0:01f31e923fe2 223 }
Pawel Zarembski 0:01f31e923fe2 224
Pawel Zarembski 0:01f31e923fe2 225 void target_forward_reset(bool assert_reset)
Pawel Zarembski 0:01f31e923fe2 226 {
Pawel Zarembski 0:01f31e923fe2 227 // Do nothing - reset is forwarded in gpio_get_sw_reset
Pawel Zarembski 0:01f31e923fe2 228 }
Pawel Zarembski 0:01f31e923fe2 229
Pawel Zarembski 0:01f31e923fe2 230 void gpio_set_board_power(bool powerEnabled)
Pawel Zarembski 0:01f31e923fe2 231 {
Pawel Zarembski 0:01f31e923fe2 232 }