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 nrf5x.c
Pawel Zarembski 0:01f31e923fe2 3 * @brief board ID for the Nordic nRF5x developments boards
Pawel Zarembski 0:01f31e923fe2 4 *
Pawel Zarembski 0:01f31e923fe2 5 * DAPLink Interface Firmware
Pawel Zarembski 0:01f31e923fe2 6 * Copyright (c) 2009-2019, 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 "sam3u2c.h"
Pawel Zarembski 0:01f31e923fe2 23 #include "DAP_config.h"
Pawel Zarembski 0:01f31e923fe2 24 #include "target_config.h"
Pawel Zarembski 0:01f31e923fe2 25 #include "util.h"
Pawel Zarembski 0:01f31e923fe2 26 #include "flash_manager.h"
Pawel Zarembski 0:01f31e923fe2 27 #include "target_family.h"
Pawel Zarembski 0:01f31e923fe2 28 #include "target_board.h"
Pawel Zarembski 0:01f31e923fe2 29
Pawel Zarembski 0:01f31e923fe2 30 // BOARD_ID pointer will be set during run time to point to one of these
Pawel Zarembski 0:01f31e923fe2 31 const char *board_id_nrf51_mkit = "1070";
Pawel Zarembski 0:01f31e923fe2 32 const char *board_id_nrf51_dk = "1100";
Pawel Zarembski 0:01f31e923fe2 33 const char *board_id_nrf51_dongle = "1120";
Pawel Zarembski 0:01f31e923fe2 34 const char *board_id_nrf52_dk = "1101";
Pawel Zarembski 0:01f31e923fe2 35 const char *board_id_nrf52840_dk = "1102";
Pawel Zarembski 0:01f31e923fe2 36
Pawel Zarembski 0:01f31e923fe2 37 extern target_cfg_t target_device_nrf52;
Pawel Zarembski 0:01f31e923fe2 38 extern target_cfg_t target_device_nrf52840;
Pawel Zarembski 0:01f31e923fe2 39 static uint8_t device_type;
Pawel Zarembski 0:01f31e923fe2 40
Pawel Zarembski 0:01f31e923fe2 41 static void set_target_device(uint32_t device)
Pawel Zarembski 0:01f31e923fe2 42 {
Pawel Zarembski 0:01f31e923fe2 43 device_type = device;
Pawel Zarembski 0:01f31e923fe2 44 if (device == 0) {
Pawel Zarembski 0:01f31e923fe2 45 // Target device already set to nRF51
Pawel Zarembski 0:01f31e923fe2 46 }
Pawel Zarembski 0:01f31e923fe2 47 else if (device == 1) {
Pawel Zarembski 0:01f31e923fe2 48 target_device = target_device_nrf52;
Pawel Zarembski 0:01f31e923fe2 49 }
Pawel Zarembski 0:01f31e923fe2 50 else if (device == 2) {
Pawel Zarembski 0:01f31e923fe2 51 target_device = target_device_nrf52840;
Pawel Zarembski 0:01f31e923fe2 52 }
Pawel Zarembski 0:01f31e923fe2 53 }
Pawel Zarembski 0:01f31e923fe2 54
Pawel Zarembski 0:01f31e923fe2 55
Pawel Zarembski 0:01f31e923fe2 56 static void nrf_prerun_board_config(void)
Pawel Zarembski 0:01f31e923fe2 57 {
Pawel Zarembski 0:01f31e923fe2 58 // Work around for setting the correct board id based on GPIOs
Pawel Zarembski 0:01f31e923fe2 59 uint8_t bit1;
Pawel Zarembski 0:01f31e923fe2 60 uint8_t bit2;
Pawel Zarembski 0:01f31e923fe2 61 uint8_t bit3;
Pawel Zarembski 0:01f31e923fe2 62
Pawel Zarembski 0:01f31e923fe2 63 PIOB->PIO_PER = (1 << 1); // Enable PIO pin PB1
Pawel Zarembski 0:01f31e923fe2 64 PIOB->PIO_PER = (1 << 2); // Enable PIO pin PB2
Pawel Zarembski 0:01f31e923fe2 65 PIOB->PIO_PER = (1 << 3); // Enable PIO pin PB3
Pawel Zarembski 0:01f31e923fe2 66 PIOB->PIO_ODR = (1 << 1); // Disabe output
Pawel Zarembski 0:01f31e923fe2 67 PIOB->PIO_ODR = (1 << 2); // Disabe output
Pawel Zarembski 0:01f31e923fe2 68 PIOB->PIO_ODR = (1 << 3); // Disabe output
Pawel Zarembski 0:01f31e923fe2 69 PIOB->PIO_PUER = (1 << 1); // Enable pull-up
Pawel Zarembski 0:01f31e923fe2 70 PIOB->PIO_PUER = (1 << 2); // Enable pull-up
Pawel Zarembski 0:01f31e923fe2 71 PIOB->PIO_PUER = (1 << 3); // Enable pull-up
Pawel Zarembski 0:01f31e923fe2 72
Pawel Zarembski 0:01f31e923fe2 73 bit1 = (PIOB->PIO_PDSR >> 1) & 1; // Read PB1
Pawel Zarembski 0:01f31e923fe2 74 bit2 = (PIOB->PIO_PDSR >> 2) & 1; // Read PB2
Pawel Zarembski 0:01f31e923fe2 75 bit3 = (PIOB->PIO_PDSR >> 3) & 1; // Read PB3
Pawel Zarembski 0:01f31e923fe2 76
Pawel Zarembski 0:01f31e923fe2 77 /* pins translate to board-ids as follow
Pawel Zarembski 0:01f31e923fe2 78 *
Pawel Zarembski 0:01f31e923fe2 79 * PB3|PB2|PB1|BOARD ID| BOARD
Pawel Zarembski 0:01f31e923fe2 80 * ----------------------------------
Pawel Zarembski 0:01f31e923fe2 81 * 0 | 0 | 0 | 1120 | nRF51-Dongle
Pawel Zarembski 0:01f31e923fe2 82 * 0 | 0 | 1 | 1100 | nRF51-DK
Pawel Zarembski 0:01f31e923fe2 83 * 0 | 1 | 0 | 1101 | nRF52-DK
Pawel Zarembski 0:01f31e923fe2 84 * 0 | 1 | 1 | 1102 | nRF52840-DK
Pawel Zarembski 0:01f31e923fe2 85 * 1 | 1 | 1 | 1070 | older nRF51 (mkit)
Pawel Zarembski 0:01f31e923fe2 86 * 1 | 0 | 0 | undefined
Pawel Zarembski 0:01f31e923fe2 87 * 1 | 0 | 1 | undefined
Pawel Zarembski 0:01f31e923fe2 88 * 1 | 1 | 0 | undefined
Pawel Zarembski 0:01f31e923fe2 89 */
Pawel Zarembski 0:01f31e923fe2 90
Pawel Zarembski 0:01f31e923fe2 91
Pawel Zarembski 0:01f31e923fe2 92 if (bit3) {
Pawel Zarembski 0:01f31e923fe2 93 set_target_device(0);
Pawel Zarembski 0:01f31e923fe2 94 target_device.rt_family_id = kNordic_Nrf51_FamilyID;
Pawel Zarembski 0:01f31e923fe2 95 target_device.rt_board_id = board_id_nrf51_mkit; // 1070
Pawel Zarembski 0:01f31e923fe2 96 //Note only a setting of 111 is defined
Pawel Zarembski 0:01f31e923fe2 97 util_assert(bit2 && bit1);
Pawel Zarembski 0:01f31e923fe2 98 } else {
Pawel Zarembski 0:01f31e923fe2 99 if (!bit2 && bit1) {
Pawel Zarembski 0:01f31e923fe2 100 set_target_device(0);
Pawel Zarembski 0:01f31e923fe2 101 target_device.rt_family_id = kNordic_Nrf51_FamilyID;
Pawel Zarembski 0:01f31e923fe2 102 target_device.rt_board_id = board_id_nrf51_dk; // 1100
Pawel Zarembski 0:01f31e923fe2 103 }
Pawel Zarembski 0:01f31e923fe2 104 else if (!bit2 && !bit1) {
Pawel Zarembski 0:01f31e923fe2 105 set_target_device(0);
Pawel Zarembski 0:01f31e923fe2 106 target_device.rt_family_id = kNordic_Nrf51_FamilyID;
Pawel Zarembski 0:01f31e923fe2 107 target_device.rt_board_id = board_id_nrf51_dongle; // 1120
Pawel Zarembski 0:01f31e923fe2 108 }
Pawel Zarembski 0:01f31e923fe2 109 else if (bit2 && !bit1) {
Pawel Zarembski 0:01f31e923fe2 110 set_target_device(1);
Pawel Zarembski 0:01f31e923fe2 111 target_device.rt_family_id = kNordic_Nrf52_FamilyID;
Pawel Zarembski 0:01f31e923fe2 112 target_device.rt_board_id = board_id_nrf52_dk; // 1101
Pawel Zarembski 0:01f31e923fe2 113 } else { //(bit2 && bit1)
Pawel Zarembski 0:01f31e923fe2 114 set_target_device(2);
Pawel Zarembski 0:01f31e923fe2 115 target_device.rt_family_id = kNordic_Nrf52_FamilyID;
Pawel Zarembski 0:01f31e923fe2 116 target_device.rt_board_id = board_id_nrf52840_dk; // 1102
Pawel Zarembski 0:01f31e923fe2 117 }
Pawel Zarembski 0:01f31e923fe2 118 }
Pawel Zarembski 0:01f31e923fe2 119
Pawel Zarembski 0:01f31e923fe2 120 PIOB->PIO_PUDR = (1 << 1); // Disable pull-up
Pawel Zarembski 0:01f31e923fe2 121 PIOB->PIO_PUDR = (1 << 2); // Disable pull-up
Pawel Zarembski 0:01f31e923fe2 122 PIOB->PIO_PUDR = (1 << 3); // Disable pull-up
Pawel Zarembski 0:01f31e923fe2 123 }
Pawel Zarembski 0:01f31e923fe2 124
Pawel Zarembski 0:01f31e923fe2 125 static void nrf_swd_set_target_reset(uint8_t asserted){
Pawel Zarembski 0:01f31e923fe2 126 if (asserted && device_type == 0) {
Pawel Zarembski 0:01f31e923fe2 127 PIOA->PIO_OER = PIN_SWDIO;
Pawel Zarembski 0:01f31e923fe2 128 PIOA->PIO_OER = PIN_SWCLK;
Pawel Zarembski 0:01f31e923fe2 129 }else if(!asserted) {
Pawel Zarembski 0:01f31e923fe2 130 PIOA->PIO_MDER = PIN_SWDIO | PIN_SWCLK | PIN_nRESET;
Pawel Zarembski 0:01f31e923fe2 131 }
Pawel Zarembski 0:01f31e923fe2 132 }
Pawel Zarembski 0:01f31e923fe2 133
Pawel Zarembski 0:01f31e923fe2 134 const board_info_t g_board_info = {
Pawel Zarembski 0:01f31e923fe2 135 .info_version = kBoardInfoVersion,
Pawel Zarembski 0:01f31e923fe2 136 .flags = kEnablePageErase,
Pawel Zarembski 0:01f31e923fe2 137 .prerun_board_config = nrf_prerun_board_config,
Pawel Zarembski 0:01f31e923fe2 138 .swd_set_target_reset = nrf_swd_set_target_reset,
Pawel Zarembski 0:01f31e923fe2 139 .target_cfg = &target_device,
Pawel Zarembski 0:01f31e923fe2 140 };