MCPcopy Create free account
hub / github.com/CE-Programming/CEmu / spi_read

Function spi_read

core/spi.c:151–202  ·  view source on GitHub ↗

Read from the SPI range of ports */

Source from the content-addressed store, hash-verified

149
150/* Read from the SPI range of ports */
151static uint8_t spi_read(uint16_t addr, bool peek) {
152 static const uint16_t cr0_masks[16] = {
153 0xF18C, 0xF8EF, 0xF0AC, 0xF3FC, 0xF18C, 0xF4C0, 0xF3AC, 0xF3AC,
154 0xF18C, 0xFBEF, 0xF0AC, 0xF3FC, 0xF18C, 0xF4C0, 0xF3AC, 0xF3AC
155 };
156
157 uint32_t shift = (addr & 3) << 3, value = 0;
158 switch (addr >> 2) {
159 case 0x00 >> 2: // CR0
160 value = spi.cr0;
161 value &= cr0_masks[value >> 12 & 0xF];
162 break;
163 case 0x04 >> 2: // CR1
164 value = spi.cr1;
165 break;
166 case 0x08 >> 2: // CR2
167 value = spi.cr2;
168 break;
169 case 0x0C >> 2: // STATUS
170 value = spi.tfve << 12 | spi.rfve << 4 |
171 (spi.transferBits != 0) << 2 |
172 (spi.tfve != SPI_TXFIFO_DEPTH) << 1 | (spi.rfve == SPI_RXFIFO_DEPTH) << 0;
173 break;
174 case 0x10 >> 2: // INTCTRL
175 value = spi.intCtrl;
176 break;
177 case 0x14 >> 2: // INTSTATUS
178 value = spi.intStatus;
179 if (!peek) {
180 spi.intStatus &= ~(1 << 1 | 1 << 0);
181 intrpt_set(INT_SPI, spi.intCtrl & spi.intStatus);
182 }
183 break;
184 case 0x18 >> 2: // DATA
185 value = spi.rxFifo[spi.rfvi & (SPI_RXFIFO_DEPTH - 1)];
186 if (!peek && !shift && spi.rfve) {
187 spi.rfvi++;
188 spi.rfve--;
189 spi_update();
190 }
191 break;
192 case 0x60 >> 2: // REVISION
193 value = 0x01 << 16 | 0x21 << 8 | 0x00 << 0;
194 break;
195 case 0x1C >> 2:
196 case 0x64 >> 2: // FEATURE
197 value = SPI_FEATURES << 24 | (SPI_TXFIFO_DEPTH - 1) << 16 |
198 (SPI_RXFIFO_DEPTH - 1) << 8 | (SPI_WIDTH - 1) << 0;
199 break;
200 }
201 return value >> shift;
202}
203
204/* Write to the SPI range of ports */
205static void spi_write(uint16_t addr, uint8_t byte, bool poke) {

Callers

nothing calls this directly

Calls 2

intrpt_setFunction · 0.85
spi_updateFunction · 0.85

Tested by

no test coverage detected