-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops.c
113 lines (93 loc) · 3.54 KB
/
ops.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
/****************************************************************************
* *
* PICO-SST39SF0x0-FLASHER-FIRMWARE *
* Copyright (C) 2023 Ivo Filot <ivo@ivofilot.nl> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public license *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
****************************************************************************/
#include "ops.h"
/**
* Load address in 74HC573 chips
**/
void set_address(uint32_t addr) {
gpio_put(ROE, true); // disable address output
uint32_t addrl = addr & 0xFF;
uint32_t addrh = (addr >> 8) & 0xFF;
uint32_t addru = (addr >> 16) & 0xFF;
set_address_upper(addru);
set_address_high(addrh);
set_address_low(addrl);
gpio_put(ROE, false); // enable address output
}
/**
* @brief Read a single byte from address
*
* @param addr full 19-bit address
* @return uint8_t value at address
*/
uint8_t read_byte(uint32_t addr) {
// set pins 0-7 to input
gpio_set_dir_in_masked(0x000000FF);
set_address(addr);
gpio_put(CE, false);
gpio_put(OE, false);
sleep_us(DELAY_READ);
uint8_t val = gpio_get_all(); // read from lower 8 pins, discard rest
gpio_put(CE, true);
gpio_put(OE, true);
return val;
}
/**
* @brief Write a byte at address
*
* @param addr full 19-bit address
* @param val 8-bit value to write
*/
void write_byte(uint32_t addr, uint8_t val) {
// set pins 0-7 to output
gpio_set_dir_out_masked(0x000000FF);
// set address on pins
set_address(addr);
gpio_put_masked(0xFF, val);
gpio_put(CE, false);
gpio_put(PGM, false);
sleep_us(DELAY_WRITE);
gpio_put(PGM, true);
gpio_put(CE, true);
// set pins 0-7 to input
gpio_set_dir_in_masked(0x000000FF);
}
/**
* @brief Write byte at address while ignoring any upper bytes
*
* @param addr 16-bit combination of lower and higher address bits
* @param val 8-bit value to write
*
* Assumes that:
* - Pins are already set for output
* - Chip is already active (CE)
**/
void write_byte_ignore_upper_fast(uint16_t addr, uint8_t val) {
// set address on pins
uint32_t addrl = addr & 0xFF;
uint32_t addrh = (addr >> 8) & 0xFF;
set_address_high(addrh);
set_address_low(addrl);
gpio_put_masked(0xFF, val);
// toggle write
gpio_put(PGM, false);
sleep_us(DELAY_WRITE);
gpio_put(PGM, true);
}