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

« Back to documentation index

Show/hide line numbers target_reset_k32w_series.c Source File

target_reset_k32w_series.c

00001 /**
00002  * @file    target_reset_K32W_series.c
00003  * @brief   Target reset for the Kinetis K32W series
00004  *
00005  * DAPLink Interface Firmware
00006  * Copyright (c) 2016-2019, 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 "swd_host.h"
00023 #include "info.h"
00024 #include "target_family.h"
00025 
00026 #define MDM_STATUS  0x01000000
00027 #define MDM_CTRL    0x01000004
00028 #define MDM_IDR     0x010000fc
00029 #define MDM_ID      0x001c0040 // K32 series
00030 
00031 #define MDM_STATUS_FLASH_MASS_ERASE_ACKNOWLEDGE (1 << 0)
00032 #define MDM_STATUS_FLASH_READY (1 << 1)
00033 #define MDM_STATUS_SYSTEM_SECURITY (1 << 2)
00034 #define MDM_STATUS_MASS_ERASE_ENABLE (1 << 5)
00035 
00036 #define MDM_CTRL_FLASH_MASS_ERASE_IN_PROGRESS (1 << 0)
00037 #define MDM_CTRL_SYSTEM_RESET_REQUEST (1 << 3)
00038 
00039 #define TIMEOUT_COUNT (1000000)
00040 
00041 void target_before_init_debug(void)
00042 {
00043     swd_set_target_reset(1);
00044 }
00045 
00046 uint8_t target_unlock_sequence(void)
00047 {
00048     uint32_t val;
00049     uint32_t timeoutCounter = 0;
00050 
00051     // read the device ID
00052     if (!swd_read_ap(MDM_IDR, &val)) {
00053         return 0;
00054     }
00055 
00056     // verify the result
00057     if (val != MDM_ID) {
00058         return 0;
00059     }
00060 
00061     // Wait until flash is ready.
00062     do {
00063         if (!swd_read_ap(MDM_STATUS, &val)) {
00064             return 0;
00065         }
00066 
00067         if (++timeoutCounter > TIMEOUT_COUNT) {
00068             return 0;
00069         }
00070     } while (!(val & MDM_STATUS_FLASH_READY));
00071 
00072     // Check if security is enabled.
00073     if (!swd_read_ap(MDM_STATUS, &val)) {
00074         swd_set_target_reset(0);
00075         return 0;
00076     }
00077 
00078     // flash in secured mode
00079     if (val & MDM_STATUS_SYSTEM_SECURITY) {
00080         // Make sure mass erase is enabled.
00081         if (!(val & MDM_STATUS_MASS_ERASE_ENABLE)) {
00082             return 0;
00083         }
00084 
00085         // hold the device in reset
00086         swd_set_target_reset(1);
00087 
00088         // Write the mass-erase enable and system reset request bits.
00089         if (!swd_write_ap(MDM_CTRL, (MDM_CTRL_FLASH_MASS_ERASE_IN_PROGRESS | MDM_CTRL_SYSTEM_RESET_REQUEST))) {
00090             swd_set_target_reset(0);
00091             return 0;
00092         }
00093 
00094         // Verify mass erase has started.
00095         timeoutCounter = 0;
00096         do {
00097             // wait until mass erase is started
00098             if (!swd_read_ap(MDM_STATUS, &val)) {
00099                 swd_set_target_reset(0);
00100                 return 0;
00101             }
00102 
00103             if (++timeoutCounter > TIMEOUT_COUNT) {
00104                 swd_write_ap(MDM_CTRL, 0);
00105                 swd_set_target_reset(0);
00106                 return 0;
00107             }
00108         } while (!(val & MDM_STATUS_FLASH_MASS_ERASE_ACKNOWLEDGE));
00109 
00110         // Wait until mass erase completes.
00111         timeoutCounter = 0;
00112         do {
00113             // keep reading until procedure is complete
00114             if (!swd_read_ap(MDM_CTRL, &val)) {
00115                 swd_set_target_reset(0);
00116                 return 0;
00117             }
00118 
00119             if (++timeoutCounter > TIMEOUT_COUNT) {
00120                 swd_write_ap(MDM_CTRL, 0);
00121                 swd_set_target_reset(0);
00122                 return 0;
00123             }
00124         } while (val & MDM_CTRL_FLASH_MASS_ERASE_IN_PROGRESS);
00125 
00126         // Confirm the mass erase was successful.
00127         if (!swd_read_ap(MDM_STATUS, &val)) {
00128             swd_set_target_reset(0);
00129             return 0;
00130         }
00131 
00132         // Release the device from reset.
00133         swd_write_ap(MDM_CTRL, 0);
00134         swd_set_target_reset(0);
00135 
00136         if (val & MDM_STATUS_SYSTEM_SECURITY) {
00137             return 0;
00138         }
00139     }
00140 
00141     return 1;
00142 }
00143 
00144 const target_family_descriptor_t g_nxp_kinetis_k32w_series = {
00145     .family_id  = kNXP_KinetisK32W_FamilyID,
00146     .default_reset_type = kHardwareReset,
00147     .target_before_init_debug = target_before_init_debug,
00148     .target_unlock_sequence = target_unlock_sequence,
00149 };