-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMB85RS1MT.cpp
172 lines (132 loc) · 3.89 KB
/
MB85RS1MT.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
163
164
165
166
167
168
169
170
171
172
#include "MB85RS1MT.h"
#include <stdio.h>
#include <string.h>
#include "hardware/gpio.h"
#include "hardware/spi.h"
#include "pico/stdlib.h"
#define CS_HIGH gpio_put(cs_pin, 1);
#define CS_LOW gpio_put(cs_pin, 0);
#define HOLD_HIGH gpio_put(hold_pin, 1);
#define HOLD_LOW gpio_put(hold_pin, 0);
#define WP_HIGH gpio_put(wp_pin, 1);
#define WP_LOW gpio_put(wp_pin, 0);
// SPI Op-Code definitions
const uint8_t wren = 0x06; // Set write enable latch
const uint8_t wrdi = 0x04; // Reset write enable latch
const uint8_t rdsr = 0x05; // Read status register
const uint8_t wrsr = 0x01; // Write status register
const uint8_t read = 0x03; // Read memory code
const uint8_t write = 0x02; // Write memory code
const uint8_t rdid = 0x9F; // Read device ID
#define REV16(x) \
((0x00FF & x) << 8) | ((0xFF00 & x) >> 8) // for 16-bit numbers
#define REV24(x) \
((0x000000FF & x) << 16) | ((0x00FF0000 & x) >> 16) // for 24-bit numbers
int MB85RS1MT::set_wel(void) {
CS_LOW;
spi_write_blocking(spi, &wren, 1);
CS_HIGH;
return 0;
}
int MB85RS1MT::reset_wel() {
CS_LOW;
spi_write_blocking(spi, &wrdi, 1);
CS_HIGH;
return 0;
}
int MB85RS1MT::read_status_register() {
uint8_t buf = 0x01;
CS_LOW;
spi_write_blocking(spi, &rdsr, 1);
spi_read_blocking(spi, 0, &buf, 1);
CS_HIGH;
wpen = ((buf & WPEN_MASK) != 0) ? true : false;
bp1 = ((buf & BP1_MASK) != 0) ? true : false;
bp0 = ((buf & BP0_MASK) != 0) ? true : false;
wel = ((buf & WEL_MASK) != 0) ? true : false;
// printf("Device %d status register: %x\n", device_id, buf);
return 0;
}
int MB85RS1MT::write_status_register(uint8_t reg) {
// WP_HIGH;
CS_LOW;
spi_write_blocking(spi, &wrsr, 1);
spi_write_blocking(spi, ®, 1);
CS_HIGH;
// WP_LOW;
return 0;
}
int MB85RS1MT::read_memory(uint32_t addr, uint8_t *buf, uint len) {
uint32_t mod_addr = addr; // REV24(addr)
uint8_t addr_buf[] = {(0x00FF0000 & addr) >> 16, (0x0000FF00 & addr) >> 8,
0x000000FF & addr};
CS_LOW;
spi_write_blocking(spi, &read, 1);
spi_write_blocking(spi, addr_buf, 3);
spi_read_blocking(spi, 0, buf, len);
CS_HIGH;
return 0;
}
int MB85RS1MT::write_memory(uint32_t addr, uint8_t *buf, uint len) {
set_wel();
read_status_register();
if (!wel) {
printf("memory not writeable\n");
return -1;
}
if (addr > MAX_ADDR) {
printf("address too large");
return -1;
}
if (addr + len > MAX_ADDR) {
len = MAX_ADDR - addr;
}
uint32_t mod_addr = addr; // REV24(addr)
uint8_t addr_buf[] = {(0x00FF0000 & addr) >> 16, (0x0000FF00 & addr) >> 8,
0x000000FF & addr};
CS_LOW;
if (spi_write_blocking(spi, &write, 1) != 1) {
printf("Error writing WRITE op-code");
return -1;
}
if (spi_write_blocking(spi, addr_buf, 3) != 3) {
printf("Error writing memory address");
return -1;
}
if (spi_write_blocking(spi, buf, len) != len) {
printf("Error writing data");
return -1;
}
CS_HIGH;
return 0;
}
int MB85RS1MT::read_device_id() {
CS_LOW;
spi_write_blocking(spi, &rdid, 1);
spi_read_blocking(spi, 0, (uint8_t *)(&device_id), 4);
CS_HIGH;
return 0;
}
int MB85RS1MT::mem_init() {
gpio_set_function(sck_pin, GPIO_FUNC_SPI);
gpio_set_function(mosi_pin, GPIO_FUNC_SPI);
gpio_set_function(miso_pin, GPIO_FUNC_SPI);
gpio_init(cs_pin);
gpio_set_dir(cs_pin, GPIO_OUT);
gpio_put(cs_pin, 1);
if (wp_pin <= 29) {
gpio_init(wp_pin);
gpio_set_dir(wp_pin, GPIO_OUT);
gpio_put(wp_pin, 0);
}
if (hold_pin <= 29) {
gpio_init(hold_pin);
gpio_set_dir(hold_pin, GPIO_OUT);
gpio_put(hold_pin, 1);
}
device_id = 0;
read_device_id();
reset_wel();
// read_status_register();
return 0;
}