forked from unfrozen/stm8_libs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib_flash.c
149 lines (133 loc) · 2.6 KB
/
lib_flash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* File name: lib_flash.c
* Date first: code clipped from lib_log
* Date last: 10/18/2018
*
* Description: Library for Flash memory functions
*
* Author: Richard Hodges
*
* Copyright (C) 2018 Richard Hodges. All rights reserved.
* Permission is hereby granted for any use.
*
******************************************************************************
*/
#include <string.h>
#include "stm8s_header.h"
#include "lib_flash.h"
#ifdef FLASH_BLOCK_ERASE
static char p_flash_erase(char *);
static char r_flash_erase[30];
#endif
#pragma disable_warning 59
/******************************************************************************
*
* Initialize Flash functions
* (Need to execute block erase from RAM)
*/
void flash_init(void)
{
#ifdef FLASH_BLOCK_ERASE
memcpy(r_flash_erase, p_flash_erase, 30);
#endif
}
#ifdef FLASH_BLOCK_ERASE
/******************************************************************************
*
* Erase FLASH block (64 or 128 bytes)
* in: block address
* out: zero = success
*/
char flash_erase(char *ptr)
{
ptr;
__asm
ldw x, (3, sp)
clr a
push cc
sim
call _r_flash_erase
pop cc
__endasm;
}
static char p_flash_erase(char *ptr)
{
ptr;
__asm
bset _FLASH_CR2, #5
bres _FLASH_NCR2, #5
ld (x), a
ld (1, x), a
ld (2, x), a
ld (3, x), a
ldw x, #4000*16/5
00001$:
decw x
jreq 00090$
btjt _FLASH_CR2, #5, 00001$
ret
00090$:
inc a
__endasm;
}
#endif /* FLASH_BLOCK_ERASE */
/******************************************************************************
*
* Unlock FLASH for writing
* out: zero = fail
*/
char flash_unlock(void)
{
__asm
mov _FLASH_PUKR, #0x56
mov _FLASH_PUKR, #0xae
clr a
00001$:
dec a
jreq 00090$
btjf _FLASH_IAPSR, #1, 00001$
00090$:
__endasm;
}
/******************************************************************************
*
* Lock FLASH from writing after write
*/
void flash_lock(void)
{
__asm
bres _FLASH_IAPSR, #1
__endasm;
}
/******************************************************************************
*
* Clear range of Flash memory
* in: block base, size in bytes
* out: zero = success
*/
char flash_clear(char *ptr, int size)
{
char retval;
retval = flash_unlock();
if (!retval)
return 1;
#ifdef FLASH_BLOCK_ERASE
while (((int)ptr & (FLASH_BLOCK - 1)) && size) {
*ptr = 0;
ptr++;
size--;
}
while (size >= FLASH_BLOCK) {
retval |= flash_erase(ptr);
ptr += FLASH_BLOCK;
size -= FLASH_BLOCK;
}
#endif
while (size) {
*ptr = 0;
ptr++;
size--;
}
flash_lock();
return retval;
}