File: flash.c

package info (click to toggle)
sdcc 4.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 100,120 kB
  • sloc: ansic: 935,524; cpp: 75,055; makefile: 57,615; sh: 30,106; asm: 14,243; perl: 12,136; yacc: 7,297; lisp: 1,672; python: 815; lex: 781; awk: 498; sed: 89
file content (141 lines) | stat: -rw-r--r-- 1,872 bytes parent folder | download | duplicates (2)
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
/* */

#include <string.h>

#include "stm8.h"

#include "flash.h"


// Lock, unklock

void
flash_punlock(void)
{
  FLASH->pukr= 0x56;
  FLASH->pukr= 0xae;
}

void
flash_dunlock(void)
{
  FLASH->pukr= 0xae;
  FLASH->pukr= 0x56;
}

void
flash_plock(void)
{
  FLASH->iapsr&= ~0x02;
}

void
flash_dlock(void)
{
  FLASH->iapsr&= ~0x08;
}

void
flash_lock(void)
{
  FLASH->iapsr&= ~0x0a;
}


// Set programing mode

void
flash_byte_mode(void)
{
  FLASH->cr2= 0;
#if (DEVICE & DEV_STM8SAF)
  FLASH->ncr2= 0xff;
#endif
}

void
flash_word_mode(void)
{
  FLASH->cr2= 0x40;
#if (DEVICE & DEV_STM8SAF)
  FLASH->ncr2= 0xbf;
#endif
}

void
flash_erase_mode(void)
{
  FLASH->cr2= 0x20;
#if (DEVICE & DEV_STM8SAF)
  FLASH->ncr2= 0xdf;
#endif
}


// Check the result

uint8_t
flash_wait_finish(void)
{
  unsigned long int timeout= 0xfffff;
  //volatile
  uint8_t r;

  r= FLASH->iapsr;
  while (((r & 0x05) == 0) &&
	 (timeout != 0))
    {
      timeout--;
      r= FLASH->iapsr;
    }
  if (r & 0x04)
    return 0;
  if (r & 0x01)
    return 1;
  if (timeout == 0)
    return 2;
  return 3;
}

uint8_t
flash_erase_fn(volatile uint8_t *addr, volatile uint8_t *iapsr)
{
  volatile uint8_t r;
  unsigned long timeout= 0xfffff;
  flash_erase_mode();
  *(addr++)= 0;
  *(addr++)= 0;
  *(addr++)= 0;
  *(addr)= 0;
  r= *iapsr;
  while (((r & 0x05) == 0) &&
	 (timeout != 0))
    {
      timeout--;
      r= *iapsr;
      GPIOD->odr^= 1;
    }
  if (r & 0x04)
    return 0;
  if (r & 0x01)
    return 1;
  if (timeout == 0)
    return 2;
  return 3;
}

uint8_t flash_op_in_ram[120];

uint8_t
flash_erase(volatile uint8_t *addr, volatile uint8_t *iapsr)
{
  uint8_t r;
  typedef uint8_t (*ft)(volatile uint8_t *addr, volatile uint8_t *iapsr);
  ft f= (ft)flash_op_in_ram;
  memcpy(flash_op_in_ram, &flash_erase_fn, 119);
  r= (*f)(addr, iapsr);
  return r;
}


/* End of stm8.src/test/flash.c */