Skip to content

Commit f8552ba

Browse files
authored
Add files via upload
1 parent e06b57b commit f8552ba

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

SPI/Agon Light SPI eZ&0.asm

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
; Initialize SPI port
2+
ld a, 0x90 ; Set up CR1 register (bits 7-4: baud rate, bit 2: master mode)
3+
ld [spi_cr1], a
4+
5+
ld a, 0x02 ; Set up CR2 register (bit 1: software slave management, bit 0: SPI enable)
6+
ld [spi_cr2], a
7+
8+
ld a, 0x00 ; Set up CR3 register (no interrupt)
9+
ld [spi_cr3], a
10+
11+
; Send data over SPI
12+
ld hl, tx_data ; Load transmit data address into HL register
13+
ld de, rx_data ; Load receive data address into DE register
14+
ld bc, tx_len ; Load transmit length into BC register
15+
16+
ld a, 0x40 ; Set up CR1 register (bits 7-4: baud rate, bit 2: master mode, bit 6: enable transmit)
17+
ld [spi_cr1], a
18+
19+
.loop:
20+
ld a, [hl] ; Load next byte of transmit data into A register
21+
out (0x00), a ; Send byte over SPI
22+
23+
ld a, [spi_sr] ; Wait for transmit buffer to be empty
24+
bit 1, a
25+
jr nz, .loop
26+
27+
in a, (0x00) ; Read received byte from SPI
28+
ld [de], a ; Store received byte in receive buffer
29+
inc hl ; Increment transmit buffer pointer
30+
inc de ; Increment receive buffer pointer
31+
dec bc ; Decrement transmit length
32+
jr nz, .loop ; Repeat until all bytes have been sent/received
33+
34+
ld a, 0x00 ; Clear CR1 register (disable transmit)
35+
ld [spi_cr1], a

SPI/Agon Light SPI.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdint.h>
4+
#include <fcntl.h>
5+
#include <unistd.h>
6+
#include <linux/spi/spidev.h>
7+
8+
#define SPI_DEVICE "/dev/spidev0.0" // SPI device node
9+
#define SPI_MODE SPI_MODE_0 // SPI mode
10+
#define SPI_BITS_PER_WORD 8 // Number of bits per word
11+
#define SPI_SPEED_HZ 1000000 // SPI clock speed in Hz
12+
13+
int main() {
14+
int spi_fd;
15+
uint8_t tx_data[3] = {0x12, 0x34, 0x56}; // Data to send
16+
uint8_t rx_data[3]; // Data to receive
17+
18+
// Open the SPI device
19+
spi_fd = open(SPI_DEVICE, O_RDWR);
20+
if (spi_fd < 0) {
21+
perror("Error opening SPI device");
22+
exit(1);
23+
}
24+
25+
// Set SPI mode
26+
if (ioctl(spi_fd, SPI_IOC_WR_MODE, &SPI_MODE) < 0) {
27+
perror("Error setting SPI mode");
28+
exit(1);
29+
}
30+
31+
// Set number of bits per word
32+
if (ioctl(spi_fd, SPI_IOC_WR_BITS_PER_WORD, &SPI_BITS_PER_WORD) < 0) {
33+
perror("Error setting bits per word");
34+
exit(1);
35+
}
36+
37+
// Set SPI clock speed
38+
if (ioctl(spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &SPI_SPEED_HZ) < 0) {
39+
perror("Error setting SPI clock speed");
40+
exit(1);
41+
}
42+
43+
// Send data over SPI
44+
struct spi_ioc_transfer transfer = {
45+
.tx_buf = (unsigned long)tx_data,
46+
.rx_buf = (unsigned long)rx_data,
47+
.len = sizeof(tx_data),
48+
.speed_hz = SPI_SPEED_HZ,
49+
.bits_per_word = SPI_BITS_PER_WORD
50+
};
51+
if (ioctl(spi_fd, SPI_IOC_MESSAGE(1), &transfer) < 0) {
52+
perror("Error sending SPI message");
53+
exit(1);
54+
}
55+
56+
// Print received data
57+
printf("Received: 0x%02X 0x%02X 0x%02X\n", rx_data[0], rx_data[1], rx_data[2]);
58+
59+
// Close the SPI device
60+
close(spi_fd);
61+
62+
return 0;
63+
}

SPI/Set up the SPI port Z80.asm

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
; Set up the SPI port
2+
LD A, 0x00 ; Set SPI control register to mode 0
3+
LD (0xF001), A ; Write to control register
4+
LD A, 0x08 ; Set SPI control register to enable SPI
5+
LD (0xF001), A ; Write to control register
6+
LD A, 0x01 ; Set SPI clock divisor to 1
7+
LD (0xF002), A ; Write to clock divisor register
8+
9+
; Send data over the SPI port
10+
LD HL, data ; Load the address of the data to send
11+
LD BC, 3 ; Set the number of bytes to send
12+
LOOP:
13+
LD A, (HL) ; Load the byte to send
14+
LD (0xF000), A ; Write the byte to the SPI data register
15+
INC HL ; Increment the source address
16+
DJNZ LOOP ; Loop until all bytes are sent
17+
18+
; Receive data from the SPI port
19+
LD HL, buffer ; Load the address of the buffer to receive data
20+
LD BC, 3 ; Set the number of bytes to receive
21+
LOOP2:
22+
LD A, 0x00 ; Dummy write to the SPI data register to initiate the read
23+
LD (0xF000), A ; Write the byte to the SPI data register
24+
LD A, (0xF000) ; Read the received byte from the SPI data register
25+
LD (HL), A ; Store the received byte in the buffer
26+
INC HL ; Increment the destination address
27+
DJNZ LOOP2 ; Loop until all bytes are received
28+
29+
; Disable the SPI port
30+
LD A, 0x00 ; Clear the enable bit in the SPI control register
31+
LD (0xF001), A ; Write to control register

0 commit comments

Comments
 (0)