forked from lldavuull/PWM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PFM.c
149 lines (133 loc) · 3.52 KB
/
PFM.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: PMF.c
* Author: METEOR
*
* Created on 2017?6?12?, ?? 4:46
*/
#include <xc.h>
#include "PFM.h"
#include "DMX.h"
#include "RDM.h"
uint16_t PFM_Read(uint16_t AddrRd){
// CFGS=0; //Not configuration space
PMADR=AddrRd;
RD=1;
asm("nop");
asm("nop");
return PMDAT;
}
void PFM_Erase(uint16_t AddrErs){
GIE=0; //Disable interrupts so required sequences will execute properly
CFGS=0; //Not configuration space
PMADR=AddrErs;
//
FREE = 1; //Program Flash Erase Operation
WREN = 1;
PFM_Unlock();
//
WREN=0;
GIE=1;
}
void PFM_Write(uint16_t AddrWrt, uint16_t Data){
do{
GIE=0; //Disable interrupts so required sequences will execute properly
CFGS=0; //Not configuration space
PMADR=AddrWrt;
//
FREE = 1; //Program Flash Erase Operation
WREN = 1;
PMCON2=0x55;
PMCON2=0xAA;
WR=1;
asm("nop");//NOP instructions are forced as processor
asm("nop");
//
FREE = 0; //Program Flash Write Operation
LWLO = 1; //LWLO= load address and write on the next WR command ; WREN=Enable writes;
PMDAT=Data; //Load data byte
LWLO=0;
PMCON2=0x55;
PMCON2=0xAA;
WR=1;
asm("nop");//NOP instructions are forced as processor
asm("nop");
}while(PFM_Read(AddrWrt)!=Data); //Write Verify
WREN=0;
GIE=1;
//
// do{
// GIE=0; //Disable interrupts so required sequences will execute properly
// CFGS=0; //Not configuration space
// PMADR=AddrPFM;
// //
// FREE = 1; //Program Flash Erase Operation
// WREN = 1;
// PFM_Unlock();
// //
// FREE = 0; //Program Flash Write Operation
// LWLO = 1; //LWLO= load address and write on the next WR command ; WREN=Enable writes;
// PMDAT=Data; //Load data byte
// LWLO=0;
// //
// PFM_Unlock();
// //
// }while(PFM_Read(Addr)!=Data); //Write Verify
// WREN=0;
// GIE=1;
}
void PFM_write_String(uint16_t AddrWrtStr, uint8_t* u8ptr, uint8_t u8len){
GIE=0; //Disable interrupts so required sequences will execute properly
CFGS=0; //Not configuration space
PMADR=AddrWrtStr;
//
FREE = 1; //Program Flash Erase Operation
WREN = 1;
PMCON2=0x55;
PMCON2=0xAA;
WR=1;
asm("nop");//NOP instructions are forced as processor
asm("nop");
//
FREE = 0; //Program Flash Write Operation
LWLO = 1; //LWLO= load address and write on the next WR command ; WREN=Enable writes;
//
ReadCount=0;
while(1){
PMDAT=*u8ptr; //Load data byte
ReadCount++;
if(ReadCount<u8len){
PFM_Unlock();
u8ptr++;
PMADR++;
}else{
break;
}
}
//
LWLO=0;
PFM_Unlock();
WREN=0;
GIE=1;
}
uint8_t PFM_Read_String(uint16_t AddrRdStr, uint8_t* dataptr){
ReadCount=0;
while(ReadCount<32){
tmp16=PFM_Read(AddrRdStr);
if(tmp16==0x3fff){
break;
}else{
*dataptr=tmp16;
ReadCount++;
dataptr++;
AddrRdStr++;
}
}
return ReadCount;
}
void PFM_Unlock(){
PMCON2=0x55;
PMCON2=0xAA;
WR=1;
asm("nop");//NOP instructions are forced as processor
asm("nop");
}