serial experiment works
authorJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 27 Jan 2019 01:55:31 +0000 (03:55 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 27 Jan 2019 01:55:31 +0000 (03:55 +0200)
src/asm.h
src/gpio.h
src/libc/ctype.c [new file with mode: 0644]
src/libc/ctype.h [new file with mode: 0644]
src/libc/string.c
src/libc/string.h
src/main.c
src/serial.c
src/startup.s
src/uart.h

index f544000..61d29ac 100644 (file)
--- a/src/asm.h
+++ b/src/asm.h
@@ -5,11 +5,9 @@
 #define disable_intr() asm volatile ("cpsid i")
 #define mem_barrier()  asm volatile ("dmb" ::: "memory")
 
 #define disable_intr() asm volatile ("cpsid i")
 #define mem_barrier()  asm volatile ("dmb" ::: "memory")
 
-/*
 #define delay(x)  asm volatile ( \
                "0: subs %0, %0, #1\n\t" \
                "bne 0b\n\t" \
                :: "r"(x) : "cc")
 #define delay(x)  asm volatile ( \
                "0: subs %0, %0, #1\n\t" \
                "bne 0b\n\t" \
                :: "r"(x) : "cc")
-*/
 
 #endif /* ASM_H_ */
 
 #endif /* ASM_H_ */
index 3a2e253..01f3153 100644 (file)
@@ -84,8 +84,6 @@ static inline void gpio_fsel(int x, int f)
        }
 }
 
        }
 }
 
-void delay(uint32_t x);        /* in startup.s */
-
 static inline void gpio_pullups(uint32_t mask0, uint32_t mask1, int state)
 {
        REG_GPPUD = state;
 static inline void gpio_pullups(uint32_t mask0, uint32_t mask1, int state)
 {
        REG_GPPUD = state;
diff --git a/src/libc/ctype.c b/src/libc/ctype.c
new file mode 100644 (file)
index 0000000..9676441
--- /dev/null
@@ -0,0 +1,56 @@
+#include "ctype.h"
+
+int isalnum(int c)
+{
+       return isalpha(c) || isdigit(c);
+}
+
+int isalpha(int c)
+{
+       return isupper(c) || islower(c);
+}
+
+int isblank(int c)
+{
+       return c == ' ' || c == '\t';
+}
+
+int isdigit(int c)
+{
+       return c >= '0' && c <= '9';
+}
+
+int isupper(int c)
+{
+       return c >= 'A' && c <= 'Z';
+}
+
+int islower(int c)
+{
+       return c >= 'a' && c <= 'z';
+}
+
+int isgraph(int c)
+{
+       return c > ' ' && c <= '~';
+}
+
+int isprint(int c)
+{
+       return isgraph(c) || c == ' ';
+}
+
+int isspace(int c)
+{
+       return isblank(c) || c == '\f' || c == '\n' || c == '\r' || c == '\v';
+}
+
+int toupper(int c)
+{
+       return islower(c) ? (c + ('A' - 'a')) : c;
+}
+
+int tolower(int c)
+{
+       return isupper(c) ? (c + ('A' - 'a')) : c;
+}
diff --git a/src/libc/ctype.h b/src/libc/ctype.h
new file mode 100644 (file)
index 0000000..a36c694
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef CTYPES_H_
+#define CTYPES_H_
+
+int isalnum(int c);
+int isalpha(int c);
+#define isascii(c)     ((c) < 128)
+int isblank(int c);
+int isdigit(int c);
+int isupper(int c);
+int islower(int c);
+int isprint(int c);
+int isspace(int c);
+
+int toupper(int c);
+int tolower(int c);
+
+
+#endif /* CTYPES_H_ */
index 8431648..9ffb5ec 100644 (file)
@@ -18,3 +18,17 @@ void *memcpy(void *dest, void *src, int size)
        }
        return dest;
 }
        }
        return dest;
 }
+
+int strcmp(const char *a, const char *b)
+{
+       while(*a && *a == *b) {
+               a++;
+               b++;
+       }
+
+       if(!*a && !*b) return 0;
+
+       if(!*a) return -1;
+       if(!*b) return 1;
+       return *a - *b > 0 ? 1 : -1;
+}
index 2fde229..4a1812f 100644 (file)
@@ -4,4 +4,6 @@
 void *memset(void *ptr, int val, int size);
 void *memcpy(void *dest, void *src, int size);
 
 void *memset(void *ptr, int val, int size);
 void *memcpy(void *dest, void *src, int size);
 
+int strcmp(const char *a, const char *b);
+
 #endif /* LIBC_STRING_H_ */
 #endif /* LIBC_STRING_H_ */
index 97fcda7..8f85b1f 100644 (file)
@@ -2,30 +2,52 @@
 
 #include <string.h>
 #include <stdint.h>
 
 #include <string.h>
 #include <stdint.h>
+#include <ctype.h>
 #include "asm.h"
 #include "serial.h"
 
 void dbgled(int x);
 void exit(int x);
 
 #include "asm.h"
 #include "serial.h"
 
 void dbgled(int x);
 void exit(int x);
 
+static void cmdrun(char *cmd);
+
 int main(void)
 {
 int main(void)
 {
-       disable_intr();
+       int lastnl = 0;
+       static char cmdbuf[256];
+       static int cmdend;
 
        dbgled(2);
 
        init_serial(115200);
 
        dbgled(2);
 
        init_serial(115200);
-       ser_printstr("starting rpkern\n");
+       ser_printstr("starting rpikern\n");
 
        for(;;) {
                int c = ser_getchar();
 
        for(;;) {
                int c = ser_getchar();
-               if(c == -1) {
+
+               switch(c) {
+               case '\r':
+               case '\n':
+                       if(!lastnl) {
+                               ser_printstr("\r\n");
+                               cmdbuf[cmdend] = 0;
+                               cmdend = 0;
+                               cmdrun(cmdbuf);
+                       }
+                       lastnl = 1;
+                       break;
+
+               case -1:
+                       lastnl = 0;
                        ser_printstr("error!\n");
                        ser_printstr("error!\n");
-               } else {
-                       ser_printstr("got: ");
+                       break;
+
+               default:
+                       lastnl = 0;
                        ser_putchar(c);
                        ser_putchar(c);
-                       ser_putchar('\r');
-                       ser_putchar('\n');
+                       if(cmdend < sizeof cmdbuf) {
+                               cmdbuf[cmdend++] = c;
+                       }
                }
        }
 
                }
        }
 
@@ -37,3 +59,24 @@ void panic(void)
        dbgled(1);
        exit(0);
 }
        dbgled(1);
        exit(0);
 }
+
+static void cmdrun(char *cmd)
+{
+       char *ptr, *args;
+
+       while(*cmd && isspace(*cmd)) cmd++;
+       ptr = cmd;
+       while(*ptr && !isspace(*ptr)) ptr++;
+       *ptr = 0;
+       args = ptr + 1;
+
+       if(strcmp(cmd, "help") == 0) {
+               ser_printstr("help not implemented yet\n");
+       } else if(strcmp(cmd, "ver") == 0) {
+               ser_printstr("rpikern version 0.0\n");
+       } else {
+               ser_printstr("Unknown command: ");
+               ser_printstr(cmd);
+               ser_printstr("\n");
+       }
+}
index 465afc7..9d1b2be 100644 (file)
@@ -15,15 +15,15 @@ void init_serial(int baud)
        /* disable pullups for GPIO 14 & 15 */
        gpio_pullups(0xc000, 0, PUD_DISABLE);
        /* select alt0 function for GPIO 14 & 15 */
        /* disable pullups for GPIO 14 & 15 */
        gpio_pullups(0xc000, 0, PUD_DISABLE);
        /* select alt0 function for GPIO 14 & 15 */
-       gpio_fsel(14, FSEL_ALT0);
-       gpio_fsel(15, FSEL_ALT0);
+       /*gpio_fsel(14, FSEL_ALT0);
+       gpio_fsel(15, FSEL_ALT0);*/
 
        REG_ICR = 0x7ff;        /* clear pending interrupts */
 
        /* calculate baud rate divisor */
        bdiv_fp6 = (UART_CLK << 6) / (16 * baud);
 
        REG_ICR = 0x7ff;        /* clear pending interrupts */
 
        /* calculate baud rate divisor */
        bdiv_fp6 = (UART_CLK << 6) / (16 * baud);
-       REG_IBRD = 1;//(bdiv_fp6 >> 6) & 0xffff;        /* 16 bits integer part */
-       REG_FBRD = 40;//bdiv_fp6 & 0x3f;                /* 6 bits fractional precision */
+       REG_IBRD = (bdiv_fp6 >> 6) & 0xffff;    /* 16 bits integer part */
+       REG_FBRD = bdiv_fp6 & 0x3f;             /* 6 bits fractional precision */
 
        /* line control: fifo enable, 8n1 */
        REG_LCRH = LCRH_FIFOEN | LCRH_8BITS;
 
        /* line control: fifo enable, 8n1 */
        REG_LCRH = LCRH_FIFOEN | LCRH_8BITS;
index 308d4fa..d0b402f 100644 (file)
@@ -43,10 +43,4 @@ dbgled:
        streq r2, [r3, #0x28]   @ GPCLR0
        bx lr
 
        streq r2, [r3, #0x28]   @ GPCLR0
        bx lr
 
-       .global delay
-delay:
-       subs r0, r0, #1
-       bne delay
-       bx lr
-
 @ vi:set filetype=armasm:
 @ vi:set filetype=armasm:
index e330ae4..0c46691 100644 (file)
@@ -4,7 +4,7 @@
 #include "config.h"
 
 #define UART0_BASE             (IO_BASE | 0x201000)
 #include "config.h"
 
 #define UART0_BASE             (IO_BASE | 0x201000)
-#define UART0_REG(x)   *(volatile uint32_t*)(UART0_BASE | (x))
+#define UART0_REG(x)   (*(volatile uint32_t*)(UART0_BASE | (x)))
 
 #define REG_DR         UART0_REG(0x00) /* data register */
 #define REG_RSRECR     UART0_REG(0x04) /* receive status & error clear */
 
 #define REG_DR         UART0_REG(0x00) /* data register */
 #define REG_RSRECR     UART0_REG(0x04) /* receive status & error clear */