forked from sdjngyq/MFDLCD18_H
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPI_RAM.cpp
162 lines (128 loc) · 3.93 KB
/
SPI_RAM.cpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "SPI_RAM.h"
#include "mbed.h"
#include "MicroBitPin.h"
//SPI
SPI ram_spi(MOSI, MISO, SCK);
#define SPIRAM_SPI_Write_Byte(value) ram_spi.write(value)
#define SPIRAM_SPI_Read_Byte(value) ram_spi.write(value)
//SPIRAM
DigitalOut SPIRAM_CS(MICROBIT_PIN_P2);
#define SPIRAM_CS_0 SPIRAM_CS = 0
#define SPIRAM_CS_1 SPIRAM_CS = 1
/*********************************************
function:
Initialization system
*********************************************/
void SPIRAM::SPIRAM_SPI_Init(void)
{
ram_spi.format(8,0);
ram_spi.frequency(9000000);
SPIRAM_CS_1;
}
/*******************************************************************************
function:
// Mode handling
*******************************************************************************/
void SPIRAM::SPIRAM_Set_Mode(BYTE mode)
{
SPIRAM_CS_0;
SPIRAM_SPI_Write_Byte(CMD_WRSR);
SPIRAM_SPI_Write_Byte(mode);
SPIRAM_CS_1;
}
/*******************************************************************************
function:
// Write and read byte
*******************************************************************************/
BYTE SPIRAM::SPIRAM_RD_Byte(WORD Addr)
{
BYTE RD_Byte;
SPIRAM_CS_0;
SPIRAM_SPI_Write_Byte(CMD_READ);
SPIRAM_SPI_Write_Byte(0X00);
SPIRAM_SPI_Write_Byte((BYTE)(Addr >> 8));
SPIRAM_SPI_Write_Byte((BYTE)Addr);
RD_Byte = SPIRAM_SPI_Read_Byte(0x00);
SPIRAM_CS_1;
return RD_Byte;
}
void SPIRAM::SPIRAM_WR_Byte(WORD Addr, const BYTE Data)
{
// Write Addr,data
SPIRAM_CS_0;
SPIRAM_SPI_Write_Byte(CMD_WRITE);
SPIRAM_SPI_Write_Byte(0X00);
SPIRAM_SPI_Write_Byte((BYTE)(Addr >> 8));
SPIRAM_SPI_Write_Byte((BYTE)Addr);
SPIRAM_SPI_Write_Byte(Data);
SPIRAM_CS_1;
}
/*******************************************************************************
function:
// Page transfer functions. Bound to current page. Passing the boundary
// will wrap to the beginning
*******************************************************************************/
void SPIRAM::SPIRAM_RD_Page(WORD Addr, BYTE *pBuf)
{
WORD i;
// Write Addr, read data
SPIRAM_CS_0;
SPIRAM_SPI_Write_Byte(CMD_READ);
SPIRAM_SPI_Write_Byte(0X00);
SPIRAM_SPI_Write_Byte((BYTE)(Addr >> 8));
SPIRAM_SPI_Write_Byte((BYTE)Addr);
for (i = 0; i < 32; i++) {
*pBuf = SPIRAM_SPI_Read_Byte(0x00);
pBuf++;
}
SPIRAM_CS_1;
}
void SPIRAM::SPIRAM_WR_Page(WORD Addr, BYTE *pBuf)
{
WORD i;
// Write Addr, read data
SPIRAM_CS_0;
SPIRAM_SPI_Write_Byte(CMD_WRITE);
SPIRAM_SPI_Write_Byte(0X00);
SPIRAM_SPI_Write_Byte((BYTE)(Addr >> 8));
SPIRAM_SPI_Write_Byte((BYTE)Addr);
for (i = 0; i < 32; i++) {
SPIRAM_SPI_Write_Byte(*pBuf);
pBuf++;
}
SPIRAM_CS_1;
}
/*******************************************************************************
function:
// Write and read Len
*******************************************************************************/
void SPIRAM::SPIRAM_RD_Stream(WORD Addr, BYTE *pBuf, unsigned long Len)
{
WORD i;
// Write Addr, read data
SPIRAM_CS_0;
SPIRAM_SPI_Write_Byte(CMD_READ);
SPIRAM_SPI_Write_Byte(0X00);
SPIRAM_SPI_Write_Byte((BYTE)(Addr >> 8));
SPIRAM_SPI_Write_Byte((BYTE)Addr);
for (i = 0; i < Len; i++) {
*pBuf = SPIRAM_SPI_Read_Byte(0x00);
pBuf++;
}
SPIRAM_CS_1;
}
void SPIRAM::SPIRAM_WR_Stream(WORD Addr, BYTE *pBuf, unsigned long Len)
{
WORD i;
// Write Addr, read data
SPIRAM_CS_0;
SPIRAM_SPI_Write_Byte(CMD_WRITE);
SPIRAM_SPI_Write_Byte(0X00);
SPIRAM_SPI_Write_Byte((BYTE)(Addr >> 8));
SPIRAM_SPI_Write_Byte((BYTE)Addr);
for (i = 0; i < Len; i++) {
SPIRAM_SPI_Write_Byte(*pBuf);
pBuf++;
}
SPIRAM_CS_1;
}