Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.
Upstream: https://github.com/ARMmbed/DAPLink
source/hic_hal/stm32/stm32f103xb/flash.c@0:01f31e923fe2, 2020-04-07 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pawel Zarembski |
0:01f31e923fe2 | 1 | /** |
Pawel Zarembski |
0:01f31e923fe2 | 2 | * @file flash_hal_stm32f103xb.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-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 "flash_hal.h" // FlashOS Structures |
Pawel Zarembski |
0:01f31e923fe2 | 23 | #include "target_config.h" // target_device |
Pawel Zarembski |
0:01f31e923fe2 | 24 | #include "stm32f1xx.h" |
Pawel Zarembski |
0:01f31e923fe2 | 25 | #include "util.h" |
Pawel Zarembski |
0:01f31e923fe2 | 26 | #include "string.h" |
Pawel Zarembski |
0:01f31e923fe2 | 27 | #include "target_board.h" |
Pawel Zarembski |
0:01f31e923fe2 | 28 | |
Pawel Zarembski |
0:01f31e923fe2 | 29 | /********************************************************************* |
Pawel Zarembski |
0:01f31e923fe2 | 30 | * |
Pawel Zarembski |
0:01f31e923fe2 | 31 | * Static code |
Pawel Zarembski |
0:01f31e923fe2 | 32 | * |
Pawel Zarembski |
0:01f31e923fe2 | 33 | ********************************************************************** |
Pawel Zarembski |
0:01f31e923fe2 | 34 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 35 | |
Pawel Zarembski |
0:01f31e923fe2 | 36 | /********************************************************************* |
Pawel Zarembski |
0:01f31e923fe2 | 37 | * |
Pawel Zarembski |
0:01f31e923fe2 | 38 | * Public code |
Pawel Zarembski |
0:01f31e923fe2 | 39 | * |
Pawel Zarembski |
0:01f31e923fe2 | 40 | ********************************************************************** |
Pawel Zarembski |
0:01f31e923fe2 | 41 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 42 | uint32_t Init(uint32_t adr, uint32_t clk, uint32_t fnc) |
Pawel Zarembski |
0:01f31e923fe2 | 43 | { |
Pawel Zarembski |
0:01f31e923fe2 | 44 | // |
Pawel Zarembski |
0:01f31e923fe2 | 45 | // No special init required |
Pawel Zarembski |
0:01f31e923fe2 | 46 | // |
Pawel Zarembski |
0:01f31e923fe2 | 47 | return (0); |
Pawel Zarembski |
0:01f31e923fe2 | 48 | } |
Pawel Zarembski |
0:01f31e923fe2 | 49 | |
Pawel Zarembski |
0:01f31e923fe2 | 50 | uint32_t UnInit(uint32_t fnc) |
Pawel Zarembski |
0:01f31e923fe2 | 51 | { |
Pawel Zarembski |
0:01f31e923fe2 | 52 | // |
Pawel Zarembski |
0:01f31e923fe2 | 53 | // No special uninit required |
Pawel Zarembski |
0:01f31e923fe2 | 54 | // |
Pawel Zarembski |
0:01f31e923fe2 | 55 | return (0); |
Pawel Zarembski |
0:01f31e923fe2 | 56 | } |
Pawel Zarembski |
0:01f31e923fe2 | 57 | |
Pawel Zarembski |
0:01f31e923fe2 | 58 | uint32_t EraseChip(void) |
Pawel Zarembski |
0:01f31e923fe2 | 59 | { |
Pawel Zarembski |
0:01f31e923fe2 | 60 | FLASH_EraseInitTypeDef erase_init; |
Pawel Zarembski |
0:01f31e923fe2 | 61 | uint32_t error; |
Pawel Zarembski |
0:01f31e923fe2 | 62 | uint32_t ret = 0; // O.K. |
Pawel Zarembski |
0:01f31e923fe2 | 63 | if (g_board_info.target_cfg) { |
Pawel Zarembski |
0:01f31e923fe2 | 64 | HAL_FLASH_Unlock(); |
Pawel Zarembski |
0:01f31e923fe2 | 65 | //bootloader, interface flashing only concerns 1 flash region |
Pawel Zarembski |
0:01f31e923fe2 | 66 | util_assert((g_board_info.target_cfg->flash_regions[0].end - g_board_info.target_cfg->flash_regions[0].start) % |
Pawel Zarembski |
0:01f31e923fe2 | 67 | FLASH_PAGE_SIZE == 0); |
Pawel Zarembski |
0:01f31e923fe2 | 68 | memset(&erase_init, 0, sizeof(erase_init)); |
Pawel Zarembski |
0:01f31e923fe2 | 69 | erase_init.TypeErase = FLASH_TYPEERASE_PAGES; |
Pawel Zarembski |
0:01f31e923fe2 | 70 | erase_init.PageAddress = g_board_info.target_cfg->flash_regions[0].start; |
Pawel Zarembski |
0:01f31e923fe2 | 71 | erase_init.NbPages = (g_board_info.target_cfg->flash_regions[0].end - g_board_info.target_cfg->flash_regions[0].start) % FLASH_PAGE_SIZE; |
Pawel Zarembski |
0:01f31e923fe2 | 72 | if (HAL_FLASHEx_Erase(&erase_init, &error) != HAL_OK) { |
Pawel Zarembski |
0:01f31e923fe2 | 73 | ret = 1; |
Pawel Zarembski |
0:01f31e923fe2 | 74 | } |
Pawel Zarembski |
0:01f31e923fe2 | 75 | |
Pawel Zarembski |
0:01f31e923fe2 | 76 | HAL_FLASH_Lock(); |
Pawel Zarembski |
0:01f31e923fe2 | 77 | }else{ |
Pawel Zarembski |
0:01f31e923fe2 | 78 | ret = 1; |
Pawel Zarembski |
0:01f31e923fe2 | 79 | } |
Pawel Zarembski |
0:01f31e923fe2 | 80 | return ret; |
Pawel Zarembski |
0:01f31e923fe2 | 81 | } |
Pawel Zarembski |
0:01f31e923fe2 | 82 | |
Pawel Zarembski |
0:01f31e923fe2 | 83 | uint32_t EraseSector(uint32_t adr) |
Pawel Zarembski |
0:01f31e923fe2 | 84 | { |
Pawel Zarembski |
0:01f31e923fe2 | 85 | FLASH_EraseInitTypeDef erase_init; |
Pawel Zarembski |
0:01f31e923fe2 | 86 | uint32_t error; |
Pawel Zarembski |
0:01f31e923fe2 | 87 | uint32_t ret = 0; // O.K. |
Pawel Zarembski |
0:01f31e923fe2 | 88 | |
Pawel Zarembski |
0:01f31e923fe2 | 89 | HAL_FLASH_Unlock(); |
Pawel Zarembski |
0:01f31e923fe2 | 90 | |
Pawel Zarembski |
0:01f31e923fe2 | 91 | memset(&erase_init, 0, sizeof(erase_init)); |
Pawel Zarembski |
0:01f31e923fe2 | 92 | erase_init.TypeErase = FLASH_TYPEERASE_PAGES; |
Pawel Zarembski |
0:01f31e923fe2 | 93 | erase_init.PageAddress = adr; |
Pawel Zarembski |
0:01f31e923fe2 | 94 | erase_init.NbPages = 1; |
Pawel Zarembski |
0:01f31e923fe2 | 95 | if (HAL_FLASHEx_Erase(&erase_init, &error) != HAL_OK) { |
Pawel Zarembski |
0:01f31e923fe2 | 96 | ret = 1; |
Pawel Zarembski |
0:01f31e923fe2 | 97 | } |
Pawel Zarembski |
0:01f31e923fe2 | 98 | |
Pawel Zarembski |
0:01f31e923fe2 | 99 | HAL_FLASH_Lock(); |
Pawel Zarembski |
0:01f31e923fe2 | 100 | return ret; |
Pawel Zarembski |
0:01f31e923fe2 | 101 | } |
Pawel Zarembski |
0:01f31e923fe2 | 102 | |
Pawel Zarembski |
0:01f31e923fe2 | 103 | uint32_t ProgramPage(uint32_t adr, uint32_t sz, uint32_t *buf) |
Pawel Zarembski |
0:01f31e923fe2 | 104 | { |
Pawel Zarembski |
0:01f31e923fe2 | 105 | uint32_t i; |
Pawel Zarembski |
0:01f31e923fe2 | 106 | uint32_t ret = 0; // O.K. |
Pawel Zarembski |
0:01f31e923fe2 | 107 | |
Pawel Zarembski |
0:01f31e923fe2 | 108 | HAL_FLASH_Unlock(); |
Pawel Zarembski |
0:01f31e923fe2 | 109 | |
Pawel Zarembski |
0:01f31e923fe2 | 110 | util_assert(sz % 4 == 0); |
Pawel Zarembski |
0:01f31e923fe2 | 111 | for (i = 0; i < sz / 4; i++) { |
Pawel Zarembski |
0:01f31e923fe2 | 112 | if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, adr + i * 4, buf[i]) != HAL_OK) { |
Pawel Zarembski |
0:01f31e923fe2 | 113 | ret = 1; |
Pawel Zarembski |
0:01f31e923fe2 | 114 | break; |
Pawel Zarembski |
0:01f31e923fe2 | 115 | } |
Pawel Zarembski |
0:01f31e923fe2 | 116 | } |
Pawel Zarembski |
0:01f31e923fe2 | 117 | |
Pawel Zarembski |
0:01f31e923fe2 | 118 | HAL_FLASH_Lock(); |
Pawel Zarembski |
0:01f31e923fe2 | 119 | return ret; |
Pawel Zarembski |
0:01f31e923fe2 | 120 | } |