msp4th: also make I/O functions configurable
authorDan White <dan@whiteaudio.com>
Tue, 7 May 2013 16:45:09 +0000 (11:45 -0500)
committerDan White <dan@whiteaudio.com>
Tue, 7 May 2013 16:45:09 +0000 (11:45 -0500)
This allows grafting in an interrupt-based uart in addition to giving
different-sized program and stack spaces.

msp4th/main.c
msp4th/msp4th.c
msp4th/msp4th.h
msp4th/test4th.c

index 4177b3031c81180b13ec2a3ddc6ec4d9caf7b321..24b7e5b0a3ecf6ed54bec7c06a847cdb17845efa 100644 (file)
@@ -98,16 +98,19 @@ int16_t __attribute__ ((section(".noinit"))) progArray[USER_PROG_SIZE];
 int16_t __attribute__ ((section(".noinit"))) progOpcodesArray[USER_OPCODE_MAPPING_SIZE];
 uint8_t __attribute__ ((section(".noinit"))) cmdListArray[USER_CMD_LIST_SIZE];
 
+void (*msp4th_putchar)(uint8_t);
+uint8_t (*msp4th_getchar)(void);
+void (*msp4th_puts)(uint8_t *);
 
 void config_default_msp4th(void)
 {
     int16_t i;
 
-    mathStackStartAddress = (uint16_t)&mathStackArray[MATH_STACK_SIZE - 1];
-    addrStackStartAddress = (uint16_t)&addrStackArray[ADDR_STACK_SIZE - 1];
-    progStartAddress = (uint16_t)&progArray[0];
-    progOpcodesStartAddress = (uint16_t)&progOpcodesArray[0];
-    cmdListStartAddress = (uint16_t)&cmdListArray[0];
+    mathStackStartAddress = &mathStackArray[MATH_STACK_SIZE - 1];
+    addrStackStartAddress = &addrStackArray[ADDR_STACK_SIZE - 1];
+    progStartAddress = &progArray[0];
+    progOpcodesStartAddress = &progOpcodesArray[0];
+    cmdListStartAddress = &cmdListArray[0];
 
 
     for (i=0; i < MATH_STACK_SIZE; i++) {
@@ -117,6 +120,10 @@ void config_default_msp4th(void)
     for (i=0; i < ADDR_STACK_SIZE; i++) {
         addrStackArray[i] = 0;
     }
+
+    msp4th_putchar = &uart_putchar;
+    msp4th_getchar = &uart_getchar;
+    msp4th_puts = &uart_puts;
 }
 
 
index 596d3e100b8ca6ad2d94584b1b813d6d7e6c7ec6..cd0e702b3e367d76cd09a9c90895fd307f7a6a38 100644 (file)
 
 
 #if defined(MSP430)
-/* use devboard uart */
+#include "ns430.h"
 #include "ns430-atoi.h"
-#include "ns430-uart.h"
 
 #else
-/* mixins to test msp4th on PC */
-#include <stdio.h>
+// mixins to test msp4th on PC
 #include <stdint.h>
 typedef uint8_t str_t;
-void uart_putchar(uint8_t c) { putchar(c); }
-uint8_t uart_getchar(void) { return (uint8_t)getchar(); }
-void uart_puts(uint8_t *s) { puts((char *)s); }
 #endif
 
 
@@ -389,24 +384,24 @@ void getLine()
 
     lineBufferIdx = 0;
 
-    uart_putchar('\r');
-    uart_putchar('\n');
-    uart_putchar('>');   // this is our prompt
+    msp4th_putchar('\r');
+    msp4th_putchar('\n');
+    msp4th_putchar('>');   // this is our prompt
 
     waiting = 1;
     while (waiting) {  // just hang in loop until we get CR
-        c = uart_getchar();
+        c = msp4th_getchar();
 
         if ((c == '\b') && (lineBufferIdx > 0)) {
-            uart_putchar('\b');
-            uart_putchar(' ');
-            uart_putchar('\b');
+            msp4th_putchar('\b');
+            msp4th_putchar(' ');
+            msp4th_putchar('\b');
             lineBufferIdx--;
         } else if ( ((c == 255) || (c == '\ 4')) && (lineBufferIdx == 0)) {
             xit = 1;
             waiting = 0;
         } else {
-            uart_putchar(c);
+            msp4th_putchar(c);
             if ( (c == '\r') ||
                  (c == '\n') ||
                  (lineBufferIdx >= (LINE_SIZE - 1))) { // prevent overflow of line buffer
@@ -416,7 +411,7 @@ void getLine()
             lineBuffer[lineBufferIdx] = 0;
         }
     }
-    uart_putchar('\n');
+    msp4th_putchar('\n');
     lineBufferIdx = 0;
 }
 
@@ -493,9 +488,9 @@ void getWord(void)
 
 void listFunction()
 {
-    uart_puts((str_t *)cmdListBi);
-    uart_puts((str_t *)cmdListBi2);
-    uart_puts((str_t *)cmdList);
+    msp4th_puts((str_t *)cmdListBi);
+    msp4th_puts((str_t *)cmdListBi2);
+    msp4th_puts((str_t *)cmdList);
 }
 
 
@@ -755,7 +750,7 @@ void printNumber(register int16_t n)
     uint8_t x[7];
 
     if (n < 0) {
-        uart_putchar('-');
+        msp4th_putchar('-');
         nu = -n;
     } else {
         nu = n;
@@ -771,10 +766,10 @@ void printNumber(register int16_t n)
 
     do{
         i = i - 1;
-        uart_putchar(x[i]);
+        msp4th_putchar(x[i]);
     } while (i > 0);
 
-    uart_putchar(' ');
+    msp4th_putchar(' ');
 }
 
 
@@ -784,7 +779,7 @@ void printHexChar(int16_t n){
     n += 7;
   }
   n += '0';
-  uart_putchar(n);
+  msp4th_putchar(n);
 }
 
 
@@ -1025,11 +1020,11 @@ void execN(int16_t opcode){
       break;
 
     case 38: // pwrd  ( -- ) \ print word buffer
-      uart_puts((str_t *)wordBuffer);
+      msp4th_puts((str_t *)wordBuffer);
       break;
 
     case 39: // emit  ( c -- )
-      uart_putchar(popMathStack());
+      msp4th_putchar(popMathStack());
       break;
 
     case 40: // ;  ( pcnt -a- )
@@ -1126,12 +1121,12 @@ void execN(int16_t opcode){
       break;
       
     case 52: // key  ( -- c ) \ get a key from input .... (wait for it)
-      pushMathStack(uart_getchar());
+      pushMathStack(msp4th_getchar());
       break;
 
     case 53: // cr  ( -- )
-      uart_putchar(0x0D);
-      uart_putchar(0x0A);
+      msp4th_putchar(0x0D);
+      msp4th_putchar(0x0A);
       break;
 
     case 54: // *2  ( a -- a<<1 )
index 92505677dd18839b365cbeff7df72cde91afbd7b..2f4f16360101bcf7ac28dbf555aec10e6ddb5fab 100644 (file)
@@ -2,19 +2,27 @@
 #define __MSP4TH
 
 #if defined(MSP430)
-extern volatile uint16_t mathStackStartAddress asm("__mathStackStartAddress");
-extern volatile uint16_t addrStackStartAddress asm("__addrStackStartAddress");
-extern volatile uint16_t progStartAddress asm("__progStartAddress");
-extern volatile uint16_t progOpcodesStartAddress asm("__progOpcodesStartAddress");
-extern volatile uint16_t cmdListStartAddress asm("__cmdListStartAddress");
+extern volatile int16_t *mathStackStartAddress asm("__mathStackStartAddress");
+extern volatile int16_t *addrStackStartAddress asm("__addrStackStartAddress");
+extern volatile int16_t *progStartAddress asm("__progStartAddress");
+extern volatile int16_t *progOpcodesStartAddress asm("__progOpcodesStartAddress");
+extern volatile uint8_t *cmdListStartAddress asm("__cmdListStartAddress");
 #else
-extern volatile uint16_t mathStackStartAddress;
-extern volatile uint16_t addrStackStartAddress;
-extern volatile uint16_t progStartAddress;
-extern volatile uint16_t progOpcodesStartAddress;
-extern volatile uint16_t cmdListStartAddress;
+extern volatile int16_t *mathStackStartAddress;
+extern volatile int16_t *addrStackStartAddress;
+extern volatile int16_t *progStartAddress;
+extern volatile int16_t *progOpcodesStartAddress;
+extern volatile uint8_t *cmdListStartAddress;
 #endif
 
+/*
+ * Provide definitions for these functions in user code and assign the function
+ * pointer before calling init_msp4th()
+ */
+extern void (*msp4th_putchar)(uint8_t c);
+extern uint8_t (*msp4th_getchar)(void);
+extern void (*msp4th_puts)(uint8_t *s);
+
 void init_msp4th(void);
 void processLoop(void);
 
index 0432846bf42dce73ee941ce08b296f641f1f11c8..8520e50da010dec2f6d56bda107184b3b24984b8 100644 (file)
@@ -1,8 +1,13 @@
 
+#include <stdio.h>
+#include <stdint.h>
 
-#include "ns430.h"
 #include "msp4th.h"
 
+void (*msp4th_putchar)(uint8_t);
+uint8_t (*msp4th_getchar)(void);
+void (*msp4th_puts)(uint8_t *);
+
 
 #define MATH_STACK_SIZE 32
 #define ADDR_STACK_SIZE 64
@@ -16,22 +21,42 @@ int16_t progArray[USR_OPCODE_SIZE];
 int16_t progOpcodesArray[USR_OPCODE_SIZE];
 uint8_t cmdListArray[CMD_LIST_SIZE];
 
-volatile uint16_t mathStackStartAddress;
-volatile uint16_t addrStackStartAddress;
-volatile uint16_t progStartAddress;
-volatile uint16_t progOpcodesStartAddress;
-volatile uint16_t cmdListStartAddress;
+volatile int16_t *mathStackStartAddress;
+volatile int16_t *addrStackStartAddress;
+volatile int16_t *progStartAddress;
+volatile int16_t *progOpcodesStartAddress;
+volatile uint8_t *cmdListStartAddress;
+
+
+
+void my_putchar(uint8_t c)
+{
+    putchar((char)c);
+}
+
+
+uint8_t my_getchar(void)
+{
+    return (uint8_t)getchar();
+}
+
+
+void my_puts(uint8_t *s)
+{
+    puts((char *)s);
+}
+
 
 
 void config_msp4th(void)
 {
     int16_t i;
 
-    mathStackStartAddress = (uint16_t)&mathStackArray[MATH_STACK_SIZE - 1];
-    addrStackStartAddress = (uint16_t)&addrStackArray[ADDR_STACK_SIZE - 1];
-    progStartAddress = (uint16_t)&progArray[0];
-    progOpcodesStartAddress = (uint16_t)&progOpcodesArray[0];
-    cmdListStartAddress = (uint16_t)&cmdListArray[0];
+    mathStackStartAddress = &mathStackArray[MATH_STACK_SIZE - 1];
+    addrStackStartAddress = &addrStackArray[ADDR_STACK_SIZE - 1];
+    progStartAddress = &progArray[0];
+    progOpcodesStartAddress = &progOpcodesArray[0];
+    cmdListStartAddress = &cmdListArray[0];
 
     for (i=0; i < MATH_STACK_SIZE; i++) {
         mathStackArray[i] = 0;
@@ -40,6 +65,10 @@ void config_msp4th(void)
     for (i=0; i < ADDR_STACK_SIZE; i++) {
         addrStackArray[i] = 0;
     }
+
+    msp4th_putchar = &my_putchar;
+    msp4th_getchar = &my_getchar;
+    msp4th_puts = &my_puts;
 }