libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
flash.c
Go to the documentation of this file.
1/** @defgroup flash_file Flash peripheral API
2 * @ingroup peripheral_apis
3 * @brief SWM050 Flash API.
4 * LGPL License Terms @ref lgpl_license
5 * @author @htmlonly © @endhtmlonly 2019
6 * Caleb Szalacinski <contact@skiboy.net>
7 */
8/*
9 * This file is part of the libopencm3 project.
10 *
11 * Copyright (C) 2019 Caleb Szalacinski <contact@skiboy.net>
12 *
13 * This library is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this library. If not, see <http://www.gnu.org/licenses/>.
25 */
26/**@{*/
28
29/* Internal function pointers to the ROM flash API */
30#define IAP_WR (void *)(0x1000AB)
31#define IAP_E (void *)(0x100127)
32
33uint32_t (*iap_write_read)(uint32_t *, uint32_t *, uint8_t, uint8_t) = IAP_WR;
34uint32_t (*iap_erase)(void) = IAP_E;
35
36/*---------------------------------------------------------------------------*/
37/** @brief Write to the user flash
38
39Writes words to the 0.5k user flash area.
40Must be performed only when the system clock is 18Mhz.
41
42@param[in] dest Destination address
43 The memory area to copy to.
44 From 0x00 - 0x1FC, as long as it is word-aligned
45
46@param[in] src Source address
47 The memory area to copy from.
48
49@param[in] cnt Number of words to write
50 From 1-128 as long as (dest + (cnt * 4)) < 0x200
51
52@return 1 if successful, 0 if error
53
54*/
55uint32_t flash_write(uint32_t *dest, uint32_t *src, uint8_t cnt)
56{
57 return iap_write_read(dest, src, cnt, 1);
58}
59
60/*---------------------------------------------------------------------------*/
61/** @brief Read from the user flash
62
63Reads words from the 0.5k user flash area.
64Must be performed only when the system clock is 18Mhz.
65
66@param[in] src Source address
67 The memory area to copy from.
68 From 0x00 - 0x1FC, as long as it is word-aligned
69
70@param[out] dest Destination address
71 The memory area to copy to.
72
73@param[in] cnt Number of words to read
74 From 1 - 128 as long as (src + (cnt * 4)) < 0x200
75
76@return 1 if successful, 0 if error
77
78*/
79uint32_t flash_read(uint32_t *src, uint32_t *dest, uint8_t cnt)
80{
81 return iap_write_read(src, dest, cnt, 0);
82}
83
84/*---------------------------------------------------------------------------*/
85/** @brief Erase the user flash
86
87Erases the entire 0.5k user flash area.
88Must be performed only when the system clock is 18Mhz.
89
90@return 1 if successful, 0 if error
91
92*/
93uint32_t flash_erase(void)
94{
95 return iap_erase();
96}
97/**@}*/
#define IAP_E
Definition: flash.c:31
uint32_t flash_write(uint32_t *dest, uint32_t *src, uint8_t cnt)
Write to the user flash.
Definition: flash.c:55
#define IAP_WR
Definition: flash.c:30
uint32_t(* iap_write_read)(uint32_t *, uint32_t *, uint8_t, uint8_t)
Definition: flash.c:33
uint32_t flash_read(uint32_t *src, uint32_t *dest, uint8_t cnt)
Read from the user flash.
Definition: flash.c:79
uint32_t flash_erase(void)
Erase the user flash.
Definition: flash.c:93
uint32_t(* iap_erase)(void)
Definition: flash.c:34