rpi midi recieve test master
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Fri, 17 Apr 2020 06:48:53 +0000 (09:48 +0300)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Fri, 17 Apr 2020 06:48:53 +0000 (09:48 +0300)
Makefile [new file with mode: 0644]
midirecv.c [new file with mode: 0644]
uart.c [new file with mode: 0644]
uart.h [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..ddd10d6
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,11 @@
+obj = midirecv.o uart.o
+bin = midirecv
+
+CFLAGS = -pedantic -Wall -g
+
+$(bin): $(obj)
+       $(CC) -o $@ $(obj) $(LDFLAGS)
+
+.PHONY: clean
+clean:
+       rm -f $(obj) $(bin)
diff --git a/midirecv.c b/midirecv.c
new file mode 100644 (file)
index 0000000..673e2d1
--- /dev/null
@@ -0,0 +1,29 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/select.h>
+#include "uart.h"
+
+int main(void)
+{
+       int i, fd, rdsz, count;
+       unsigned char buf[4096];
+
+       if((fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY)) == -1) {
+               perror("failed to open serial port /dev/ttyAMA0");
+               return 1;
+       }
+
+       uart_config(31250);
+       write(fd, "U", 1);
+
+       count = 0;
+       while((rdsz = read(fd, buf, sizeof buf)) > 0) {
+               for(i=0; i<rdsz; i++) {
+                       printf("%04d: %02x\n", count, (unsigned int)buf[i]);
+               }
+       }
+
+       close(fd);
+       return 0;
+}
diff --git a/uart.c b/uart.c
new file mode 100644 (file)
index 0000000..50dd093
--- /dev/null
+++ b/uart.c
@@ -0,0 +1,65 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include "uart.h"
+
+#define CLK                            3000000
+
+#define IOBASEADDR             0x3f000000
+#define IOSIZE                 16777216
+#define IOREGADDR(x)   ((x) | IOBASEADDR)
+
+#define REG_UART_FLAGS *(volatile uint32_t*)IOREGADDR(0x201018)
+#define        REG_UART_IBRD   *(volatile uint32_t*)IOREGADDR(0x201024)
+#define REG_UART_FBRD  *(volatile uint32_t*)IOREGADDR(0x201028)
+#define REG_UART_LCTL  *(volatile uint32_t*)IOREGADDR(0x20102c)
+#define REG_UART_CTL   *(volatile uint32_t*)IOREGADDR(0x201030)
+
+#define FLAGS_TXEMPTY  0x80
+#define FLAGS_RXFULL   0x40
+#define FLAGS_TXFULL   0x20
+#define FLAGS_RXEMPTY  0x10
+#define FLAGS_BUSY             0x08
+#define FLAGS_CTS              0x01
+
+#define LCTL_8BIT              0x60
+#define LCTL_FIFOEN            0x10
+#define LCTL_PAREN             0x02
+#define LCLT_BRK               0x01
+
+#define CTL_RXEN               0x200
+#define CTL_TXEN               0x100
+#define CTL_EN                 0x001
+
+int uart_config(int rate)
+{
+       int fd, div;
+       void *ptr;
+
+       if((fd = open("/dev/mem", O_RDWR)) == -1) {
+               perror("failed to open /dev/mem");
+               return 1;
+       }
+       if((ptr = mmap((void*)IOBASEADDR, IOSIZE, PROT_READ | PROT_WRITE,
+                                       MAP_SHARED | MAP_FIXED, fd, IOBASEADDR)) == (void*)-1) {
+               perror("failed to map IO space");
+               return 1;
+       }
+
+       REG_UART_CTL = 0;
+       while(REG_UART_FLAGS & FLAGS_BUSY);
+
+       div = (CLK << 6) / (rate << 4);
+       printf("setting divisor (i/f): %d %d\n", div >> 6, div & 0x3f);
+       REG_UART_IBRD = div >> 6;
+       REG_UART_FBRD = div & 0x3f;
+       REG_UART_LCTL = LCTL_8BIT | LCTL_FIFOEN;
+
+       REG_UART_CTL = CTL_RXEN | CTL_TXEN | CTL_EN;
+
+       munmap(ptr, IOSIZE);
+       close(fd);
+       return 0;
+}
diff --git a/uart.h b/uart.h
new file mode 100644 (file)
index 0000000..39a765c
--- /dev/null
+++ b/uart.h
@@ -0,0 +1,6 @@
+#ifndef UART_H_
+#define UART_H_
+
+int uart_config(int rate);
+
+#endif /* UART_H_ */