aboutsummaryrefslogtreecommitdiff
path: root/usbkbd/main.c
diff options
context:
space:
mode:
authorNathan Ringo <nathan@remexre.com>2024-10-05 13:44:15 -0500
committerNathan Ringo <nathan@remexre.com>2024-10-05 13:44:15 -0500
commit37507ecea3277a99e60c7bb077c6cd406bb80ddb (patch)
tree07432da9529cfc422cab680f2af5ba5a36533e70 /usbkbd/main.c
parent90661e174826684debe2532a260dcefb3b871067 (diff)
Start of the CH559 code, adds a NixOS module for udev rules for the boards.
Starter code from https://github.com/MatzElectronics/CH559sdccUSBHost
Diffstat (limited to 'usbkbd/main.c')
-rw-r--r--usbkbd/main.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/usbkbd/main.c b/usbkbd/main.c
new file mode 100644
index 0000000..9d5286d
--- /dev/null
+++ b/usbkbd/main.c
@@ -0,0 +1,55 @@
+#include "ch559.h"
+#include <compiler.h>
+#include <stdint.h>
+
+#ifndef FREQ_SYS
+#define FREQ_SYS 12000000 // System frequency is 12MHz
+#endif
+
+static inline void delay() {
+ uint32_t i;
+ for (i = 0; i < (120000UL); i++) {
+ }
+ __asm__("nop");
+}
+
+void initUART0(unsigned long baud, int alt) {
+ unsigned long x;
+ if (alt) {
+ PORT_CFG |= bP0_OC;
+ P0_DIR |= bTXD_;
+ P0_PU |= bTXD_ | bRXD_;
+ PIN_FUNC |= bUART0_PIN_X;
+ }
+
+ SM0 = 0;
+ SM1 = 1;
+ SM2 = 0;
+ REN = 1;
+ // RCLK = 0;
+ // TCLK = 0;
+ PCON |= SMOD;
+ x = (((unsigned long)FREQ_SYS / 8) / baud + 1) / 2;
+
+ TMOD = TMOD & ~bT1_GATE & ~bT1_CT & ~MASK_T1_MOD | bT1_M1;
+ T2MOD = T2MOD | bTMR_CLK | bT1_CLK;
+ TH1 = (256 - x) & 255;
+ TR1 = 1;
+ TI = 1;
+}
+
+void main() {
+ P4_DIR = 0b00001100;
+ P4_OUT = 0b00000100;
+
+ initUART0(9600, 1);
+
+ while (1) {
+ delay();
+ P4_OUT ^= 0b00001100;
+ SBUF = 'Z';
+ while (!TI)
+ ;
+ TI = 1;
+ }
+}