aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorThomas Albers Raviola <thomas@thomaslabs.org>2024-11-21 15:55:03 +0100
committerThomas Albers Raviola <thomas@thomaslabs.org>2024-11-21 15:55:03 +0100
commit6d4ad089c5b758ad8af4f68bf385a26ec4e9653a (patch)
tree2fd65006b57d646b53e121aef42bba5ee5852840 /include
Initial commit
Diffstat (limited to 'include')
-rw-r--r--include/fifo.h45
-rw-r--r--include/hardware.h80
-rw-r--r--include/i2c.h24
-rw-r--r--include/input.h22
-rw-r--r--include/tft.h132
-rw-r--r--include/tty.h34
-rw-r--r--include/zeta.h67
7 files changed, 404 insertions, 0 deletions
diff --git a/include/fifo.h b/include/fifo.h
new file mode 100644
index 0000000..c6eee2d
--- /dev/null
+++ b/include/fifo.h
@@ -0,0 +1,45 @@
+#ifndef FIFO_H
+#define FIFO_H
+
+#include <zeta.h>
+
+#define FIFO_LEN 32
+
+struct fifo {
+ uint8_t head;
+ uint8_t tail;
+ uint8_t data[FIFO_LEN];
+};
+
+static inline uint8_t
+fifo_pop(struct fifo *fifo)
+{
+ uint8_t ret = fifo->data[fifo->head];
+ if (++fifo->head >= LENGTH(fifo->data))
+ fifo->head = 0;
+ return ret;
+}
+
+static inline void
+fifo_push(struct fifo *fifo, uint8_t v)
+{
+ fifo->data[fifo->tail] = v;
+
+ if (++fifo->tail >= LENGTH(fifo->data))
+ fifo->tail = 0;
+}
+
+static inline bool
+fifo_empty(const struct fifo *fifo)
+{
+ return (fifo->head == fifo->tail);
+}
+
+static inline void
+fifo_clear(struct fifo *fifo)
+{
+ fifo->head = 0;
+ fifo->tail = 0;
+}
+
+#endif // FIFO_H
diff --git a/include/hardware.h b/include/hardware.h
new file mode 100644
index 0000000..2e6c9da
--- /dev/null
+++ b/include/hardware.h
@@ -0,0 +1,80 @@
+#ifndef HARDWARE_H
+#define HARDWARE_H
+
+// ********************************************************
+#define CTC_CHANNEL_0 0x00
+#define CTC_CHANNEL_1 0x01
+#define CTC_CHANNEL_2 0x02
+#define CTC_CHANNEL_3 0x03
+
+#define CTC_CTRL_OR_VECTOR_BIT 0x01
+#define CTC_RST_BIT 0x02
+#define CTC_TIME_CONST_BIT 0x04
+#define CTC_TIME_TRG_BIT 0x08
+#define CTC_CLK_TRG_BIT 0x10
+#define CTC_PRESCALER_BIT 0x20
+#define CTC_MODE_BIT 0x40
+#define CTC_INT_BIT 0x80
+
+#define CTC_CTRL(x) ((x) | CTC_CTRL_OR_VECTOR_BIT)
+
+// ********************************************************
+#define PORT_A_CTRL 0x42
+#define PORT_A_DATA 0x40
+#define PORT_B_CTRL 0x43
+#define PORT_B_DATA 0x41
+
+#define PIO_MODE_3 0xCF
+
+#define PIO_INT_CTRL(x) ((x) | 0x07)
+#define PIO_OR 0x00
+#define PIO_AND 0x40
+#define PIO_LOW 0x00
+#define PIO_HIGH 0x20
+#define PIO_MASK 0x10
+#define PIO_INT_EN 0x80
+
+// ********************************************************
+#define SIO_A_CTRL 0x22
+#define SIO_A_DATA 0x20
+#define SIO_B_CTRL 0x23
+#define SIO_B_DATA 0x21
+
+#define SIO_EX_INT_EN 0x01
+#define SIO_TX_INT_EN 0x02
+#define SIO_STATUS_AFFECTS_VECTOR 0x04
+#define SIO_RX_INT_MD0 0x08
+#define SIO_RX_INT_MD1 0x10
+#define SIO_WAIT_RDY_ON_RX_TX 0x20
+#define SIO_WAIT_RDY_FTN 0x40
+#define SIO_WAIT_RDY_EN 0x80
+
+// ********************************************************
+#define TFT_CTRL 0x60
+#define TFT_DATA 0x61
+
+#ifndef ASSEMBLY
+__sfr __at(CTC_CHANNEL_0) ctc_channel_0;
+__sfr __at(CTC_CHANNEL_1) ctc_channel_1;
+__sfr __at(CTC_CHANNEL_2) ctc_channel_2;
+__sfr __at(CTC_CHANNEL_3) ctc_channel_3;
+
+__sfr __at(SIO_A_DATA) sio_a_data;
+__sfr __at(SIO_B_DATA) sio_b_data;
+__sfr __at(SIO_A_CTRL) sio_a_ctrl;
+__sfr __at(SIO_B_CTRL) sio_b_ctrl;
+
+__sfr __at(PORT_A_DATA) port_a_data;
+__sfr __at(PORT_B_DATA) port_b_data;
+__sfr __at(PORT_A_CTRL) port_a_ctrl;
+__sfr __at(PORT_B_CTRL) port_b_ctrl;
+
+__sfr __at(TFT_CTRL) tft_ctrl;
+__sfr __at(TFT_DATA) tft_data;
+
+#define IM(__mode) __asm__("im " #__mode)
+#define EI __asm__("ei")
+#define DI __asm__("di")
+#endif // ASSEMBLY
+
+#endif // HARDWARE_H
diff --git a/include/i2c.h b/include/i2c.h
new file mode 100644
index 0000000..18fa5e9
--- /dev/null
+++ b/include/i2c.h
@@ -0,0 +1,24 @@
+#ifndef I2C_H
+#define I2C_H
+
+#include <stdint.h>
+
+#define NACK 0
+#define ACK 1
+
+void
+i2c_start_condition(void);
+
+void
+i2c_restart_condition(void);
+
+void
+i2c_stop_condition(void);
+
+void
+i2c_send(uint8_t b);
+
+uint8_t
+i2c_recv(uint8_t ack);
+
+#endif // I2C_H
diff --git a/include/input.h b/include/input.h
new file mode 100644
index 0000000..a2f03e2
--- /dev/null
+++ b/include/input.h
@@ -0,0 +1,22 @@
+#ifndef INPUT_H
+#define INPUT_H
+
+#include <fifo.h>
+
+static inline uint8_t
+poll_keys(void)
+{
+ return (port_b_data & 0x7C) >> 2;
+}
+
+enum keys {
+ KEY1 = 0x01,
+ KEY2 = 0x02,
+ KEY3 = 0x04,
+ KEY4 = 0x08,
+ KEY5 = 0x10
+};
+
+extern struct fifo input_fifo;
+
+#endif // INPUT_H
diff --git a/include/tft.h b/include/tft.h
new file mode 100644
index 0000000..22303a1
--- /dev/null
+++ b/include/tft.h
@@ -0,0 +1,132 @@
+#ifndef TFT_H
+#define TFT_H
+
+#include <hardware.h>
+#include <stdint.h>
+
+#define TFT_WIDTH 480
+#define TFT_HEIGHT 320
+
+enum tft_command {
+ NOP = 0x00,
+ SWRESET = 0x01,
+ RDDID = 0x04,
+ RDNUMPE = 0x05,
+ RDRED = 0x06,
+ RDGREEN = 0x07,
+ RDBLUE = 0x08,
+ RDDPM = 0x0A,
+ RDDMADCTL = 0x0B,
+ RDDCOLMOD = 0x0C,
+ RDDIM = 0x0D,
+ RDDSM = 0x0E,
+ RDDSDR = 0x0F,
+ SPLIN = 0x10,
+ SLPOUT = 0x11,
+ PTLON = 0x12,
+ NORON = 0x13,
+ INVOFF = 0x20,
+ INVON = 0x21,
+ ALLPOFF = 0x22,
+ ALLPON = 0x23,
+ GAMSET = 0x26,
+ DISPOFF = 0x28,
+ DISPON = 0x29,
+ CASET = 0x2A,
+ PASET = 0x2B,
+ RAMWR = 0x2C,
+ RAMRD = 0x2E,
+ PLTAR = 0x30,
+ VSCRDEF = 0x33,
+ TEOFF = 0x34,
+ TEON = 0x35,
+ MADCTL = 0x36,
+ VSCRSADD = 0x37,
+ IDMOFF = 0x38,
+ IDMON = 0x39,
+ COLMOD = 0x3A,
+ CONRAMWR = 0x3C,
+ CONRAMRD = 0x3E,
+ TESL = 0x44,
+ GETSL = 0x45,
+ WRDISBV = 0x51,
+ RDDISBV = 0x52,
+ WRCTRLD = 0x53,
+ RDCTRLD = 0x54,
+ WRCABC = 0x55,
+ RDCABC = 0x56,
+ WRCABCMB = 0x5E,
+ RDCABCMB = 0x5F,
+ RDABVCSDR = 0x68,
+ RDBWLK = 0x70,
+ RDBKX = 0x71,
+ RDBKY = 0x72,
+ RDWX = 0x73,
+ RDWY = 0x74,
+ RDRGLB = 0x75,
+ RDRX = 0x76,
+ RDRY = 0x77,
+ RDGX = 0x78,
+ RDGY = 0x79,
+ RDBALB = 0x7A,
+ RDBX = 0x7B,
+ RDBY = 0x7C,
+ RDAX = 0x7D,
+ RDAY = 0x7E,
+
+ RDID1 = 0xDA,
+ RDID2 = 0xDB,
+ RDID3 = 0xDC,
+ SETOSC = 0xB0,
+ SETPOWER = 0xB1,
+ SETDISCTRL = 0xB2,
+ SETRGB = 0xB3,
+ SETCYC = 0xB4,
+ SETBGP = 0xB5,
+ SETCOM = 0xB6,
+ SETOTP = 0xB7,
+ SETEXTC = 0xB9,
+ SETSTBA = 0xC0,
+ SETDGC = 0xC1,
+ SETID = 0xC3,
+ SETDDB = 0xC4,
+ SETCABC = 0xC9,
+ SETPANEL = 0xCC,
+ SETGAMMA = 0xE0,
+ SETIMAGE = 0xE9,
+ SETMESSI = 0xEA,
+ SETCOLOR = 0xEB
+};
+
+enum pixel_format {
+ BIT12 = 0x03,
+ BIT16 = 0x05,
+ BIT18 = 0x06,
+ BIT24 = 0x07
+};
+
+static inline void
+tft_ram_wr(void)
+{
+ tft_ctrl = RAMWR;
+}
+
+static inline void
+tft_pixel(uint8_t r, uint8_t g, uint8_t b)
+{
+ tft_data = r;
+ tft_data = g;
+ tft_data = b;
+}
+
+void
+tft_set_area(unsigned int x, unsigned int y, unsigned int w, unsigned int h);
+
+void
+tft_init(void);
+
+void
+clear_screen(void);
+
+
+#endif // TFT_H
diff --git a/include/tty.h b/include/tty.h
new file mode 100644
index 0000000..ea73e5f
--- /dev/null
+++ b/include/tty.h
@@ -0,0 +1,34 @@
+#ifndef TTY_H
+#define TTY_H
+
+#include <tft.h>
+#define TTY_WIDTH (TFT_WIDTH / 8)
+#define TTY_HEIGHT (TFT_HEIGHT / 8)
+
+void
+addch(char c);
+
+void
+addstr(const char *str);
+
+void
+setcur(unsigned int ncol, unsigned int nrow);
+
+void
+swap_colors(void);
+
+static inline void
+mvaddch(int y, int x, char c)
+{
+ setcur(x, y);
+ addch(c);
+}
+
+static inline void
+mvaddstr(int y, int x, const char *str)
+{
+ setcur(x, y);
+ addstr(str);
+}
+
+#endif // TTY_H
diff --git a/include/zeta.h b/include/zeta.h
new file mode 100644
index 0000000..291a2ff
--- /dev/null
+++ b/include/zeta.h
@@ -0,0 +1,67 @@
+#ifndef ZETA_H
+#define ZETA_H
+
+#include <stdint.h>
+#include <stdbool.h>
+
+typedef int8_t s8;
+typedef int16_t s16;
+typedef int32_t s32;
+typedef int64_t s64;
+
+typedef uint8_t u8;
+typedef uint16_t u16;
+typedef uint32_t u32;
+typedef uint64_t u64;
+
+#define LENGTH(x) (sizeof(x) / sizeof(x[0]))
+#define MAX(x, y) ((x) > (y) ? (x) : (y))
+#define MIN(x, y) ((x) < (y) ? (x) : (y))
+
+// Corrent version, but the compiler does not recognize this as a constant
+// expression
+// #define ISR_ADDRESS(x) (((u16)&x) & 0xFF)
+#define ISR_ADDRESS(x) ((u16)&x)
+
+extern void *rx_isr_ptr;
+extern void *ctc0_isr_ptr;
+extern void *ctc1_isr_ptr;
+extern void *ctc2_isr_ptr;
+extern void *ctc3_isr_ptr;
+extern void *port_a_isr_ptr;
+extern void *port_b_isr_ptr;
+
+void
+bootloader(void);
+
+uint16_t
+crc16(const void *buf, size_t len);
+
+void
+_menu(void);
+
+static inline int
+max(int a, int b)
+{
+ return a > b ? a : b;
+}
+
+static inline int
+min(int a, int b)
+{
+ return a < b ? a : b;
+}
+
+static inline int
+clamp(int v, int low, int high)
+{
+ return min(max(v, low), high);
+}
+
+static inline uint8_t
+bcd(uint8_t x)
+{
+ return ((x / 10) << 4) | (x % 10);
+}
+
+#endif // ZETA_H