]> code.bitgloo.com Git - clyne/calculator.git/commitdiff
basic graphing
authorClyne Sullivan <tullivan99@gmail.com>
Wed, 7 Mar 2018 20:25:55 +0000 (15:25 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Wed, 7 Mar 2018 20:25:55 +0000 (15:25 -0500)
13 files changed:
include/display_draw.h
include/flash.h [new file with mode: 0644]
initrd/init
initrd/init2 [new file with mode: 0644]
initrd/init3 [new file with mode: 0644]
libinterp.a
src/display_draw.c
src/flash.c [new file with mode: 0644]
src/initrd.c
src/keypad.c.bak [new file with mode: 0644]
src/main.c
src/script.c
src/stdlib.c

index 5ed42247aab1ca4ee6f37c985d02ccbb6dd6be43..823c697fab39555cba4a580bdeb81ce2833acde3 100644 (file)
@@ -5,6 +5,7 @@
 
 void dsp_cursoron(void);
 
+void dsp_pixel(int x, int y, uint16_t color);
 void dsp_line(int x, int y, int i, int j, uint16_t color);
 void dsp_rect(int x, int y, int w, int h, uint16_t color);
 
diff --git a/include/flash.h b/include/flash.h
new file mode 100644 (file)
index 0000000..26fffd3
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef FLASH_H_
+#define FLASH_H_
+
+#include <stdint.h>
+
+void flash_init(void);
+
+void flash_read(char *buf, uint32_t addr, unsigned int count);
+void flash_write(const char *buf, uint32_t addr, unsigned int count);
+
+#endif // FLASH_H_
index 4351743673f4349e2b27499793738b739d5c6dbe..56e5212ae1897dcb1e566493e10a0f53b628f6e3 100644 (file)
@@ -1 +1,37 @@
-print "Hello, world!"
+func Fx
+       ret (arg0 * arg0)
+end
+
+# graph area
+set plotx 0
+set ploty 0
+set plotw 479
+set ploth 319
+
+# graph range
+set xmin (0 - 10)
+set xmax 10
+set ymin (0 - 10)
+set ymax 10
+
+set xinc (plotw / (xmax - xmin))
+set yinc (ploth / (ymax - ymin))
+
+# print axis
+line 240 0 240 319 32767
+line 0 160 479 160 32767
+
+# do function
+set x xmin
+set cx (plotx + (plotw / 2))
+set cy (ploty + (ploth / 2))
+do
+       Fx x > y
+       set y (0 - y)
+       if ((y > ymin) & (y < ymax))
+               pixel (cx + (x * xinc)) (cy + (y * yinc)) 511
+       end
+       set x (x + (1 / xinc))
+while (x < xmax)
+
+print "Done."
diff --git a/initrd/init2 b/initrd/init2
new file mode 100644 (file)
index 0000000..47dbae1
--- /dev/null
@@ -0,0 +1,21 @@
+do
+       getkey > input
+       print input
+       delay 1000
+while (1)
+
+#do
+#      getkey > input
+#      if (input & 4)
+#              rand 479 > x
+#              rand 319 > y
+#              rand 479 > i
+#              rand 319 > j
+#              rand 32767 > purple
+#              
+#              line x y i j purple
+#      end
+#while (1)
+#
+#print "done"
+
diff --git a/initrd/init3 b/initrd/init3
new file mode 100644 (file)
index 0000000..4351743
--- /dev/null
@@ -0,0 +1 @@
+print "Hello, world!"
index d52e2803bcf46de59182df5e14cd76914da2038a..a11013cf3c14896093240f36a613d8cb9782b74c 100644 (file)
Binary files a/libinterp.a and b/libinterp.a differ
index a315bf77f3ab01aab6175777ab7767d850fb8ba5..62578e31f28d339754869edd7f4d6b02e7276bc0 100644 (file)
@@ -100,6 +100,15 @@ void dsp_rect(int x, int y, int w, int h, uint16_t color)
        UNLOCK;
 }
 
+void dsp_pixel(int x, int y, uint16_t color)
+{
+       LOCK;
+       dsp_set_addr(x, y, x, y);
+       dsp_write_data(color >> 8);
+       dsp_write_data(color & 0xFF);
+       UNLOCK;
+}
+
 void dsp_line(int x, int y, int i, int j, uint16_t color)
 {
        int dx = i - x;
@@ -115,11 +124,7 @@ void dsp_line(int x, int y, int i, int j, uint16_t color)
        int e2;
 
        while (1) {
-               LOCK;
-               dsp_set_addr(x, y, x, y);
-               dsp_write_data(color >> 8);
-               dsp_write_data(color & 0xFF);
-               UNLOCK;
+               dsp_pixel(x, y, color);
                if (x == i && y == j)
                        break;
                e2 = err;
diff --git a/src/flash.c b/src/flash.c
new file mode 100644 (file)
index 0000000..3027ec0
--- /dev/null
@@ -0,0 +1,95 @@
+#include <stm32l476xx.h>
+#include <gpio.h>
+#include <clock.h>
+
+#define READ  0x03
+#define WRITE 0x02
+#define WREN  0x06
+#define WRDS  0x04
+
+#define NSS  GPIO_PORT(B, 12)
+#define SCK  GPIO_PORT(B, 13)
+#define MISO GPIO_PORT(B, 14)
+#define MOSI GPIO_PORT(B, 15)
+
+void flash_xchg(char *byte, int count);
+
+void flash_init(void)
+{
+       gpio_mode(NSS, ALTERNATE);
+       gpio_mode(SCK, ALTERNATE);
+       gpio_mode(MISO, ALTERNATE);
+       gpio_mode(MOSI, ALTERNATE);
+       GPIOB->AFR[1] |= 0x55550000; // alt mode SPI2
+
+       // clock enable
+       RCC->APB1ENR1 |= RCC_APB1ENR1_SPI2EN;
+
+       SPI2->CR1 &= ~(SPI_CR1_BR_Msk);
+       SPI2->CR1 |= (3 << SPI_CR1_BR_Pos);
+       SPI2->CR1 |= SPI_CR1_SSM | SPI_CR1_SSI;
+       SPI2->CR1 |= SPI_CR1_MSTR;
+       SPI2->CR2 &= ~(SPI_CR2_DS_Msk);
+       SPI2->CR2 |= (7 << SPI_CR2_DS_Pos);
+       SPI2->CR2 |= SPI_CR2_SSOE;
+       SPI2->CR2 |= SPI_CR2_FRXTH;
+       SPI2->CR1 |= SPI_CR1_SPE;
+
+       char buf[3];
+       buf[0] = READ;
+       buf[1] = 0;
+       buf[2] = 0;
+       buf[3] = 0;
+       flash_xchg(buf, 4);
+}
+
+void flash_xchg(char *byte, int count)
+{
+       uint32_t status = 0, dummy;
+       SPI2->CR1 &= ~(SPI_CR1_SSI);
+       while (SPI2->SR & SPI_SR_BSY);
+       for (int i = 0; i < count; i++) {
+               SPI2->DR = byte[i];
+               do status = SPI2->SR;   
+               while (status & SPI_SR_BSY);
+               // discard rx
+               while (status & SPI_SR_RXNE) {
+                       dummy = SPI2->DR;
+                       status = SPI2->SR;
+               }
+       }
+       do status = SPI2->SR;   
+       while (status & SPI_SR_BSY);
+
+       while (1) {
+               SPI2->DR = 0;
+               do status = SPI2->SR;   
+               while (status & SPI_SR_BSY);
+               // discard rx
+               while (status & SPI_SR_RXNE) {
+                       dummy = SPI2->DR;
+                       status = SPI2->SR;
+               }
+       }
+
+       SPI2->CR1 |= SPI_CR1_SSI;
+       (void)dummy;
+}
+
+void flash_read(char *buf, uint32_t addr, unsigned int count)
+{
+       (void)buf;
+       (void)addr;
+       (void)count;
+       if (buf == 0)
+               return;
+}
+
+void flash_write(const char *buf, uint32_t addr, unsigned int count)
+{
+       (void)buf;
+       (void)addr;
+       (void)count;
+       if (buf == 0)
+               return;
+}
index e90f9f028980f954e22573bf2e7100d3208b09b2..948811fb44db4a757680085237b9b6a369b3ae1c 100644 (file)
@@ -61,7 +61,7 @@ initrd_file *initrd_getfileptr(const char *name)
                        return file;
                uint32_t size = initrd_getsize(file) + sizeof(initrd_file);
                offset += size;
-               file += size;
+               file = (initrd_file *)((uint8_t *)file + size + 1);
        }
 
        return 0;
diff --git a/src/keypad.c.bak b/src/keypad.c.bak
new file mode 100644 (file)
index 0000000..f25d46f
--- /dev/null
@@ -0,0 +1,52 @@
+#include <stm32l476xx.h>
+#include <gpio.h>
+
+#define ADDR 0x20
+
+#define CONTROL 0x41
+#define ADDRESS 0x12
+
+void keypad_init(void)
+{
+       // clock init
+       RCC->CCIPR &= ~(RCC_CCIPR_I2C1SEL_Msk);
+       RCC->CCIPR |= 2 << RCC_CCIPR_I2C1SEL_Pos; 
+       RCC->APB1ENR1 |= RCC_APB1ENR1_I2C1EN;
+
+       // set times
+       // PRESC, SCLDEL, SDADEL, SCLH, SCLL
+       I2C1->TIMINGR = (0 << 28) | (2 << 20) | (0 << 16) | (2 << 8) | 4;
+
+       // gpio init
+       gpio_mode(GPIOB, 8, ALTERNATE);
+       gpio_mode(GPIOB, 9, ALTERNATE);
+       GPIOB->AFR[1] &= ~(0xFF);
+       GPIOB->AFR[1] |= 0x44;
+
+       // go go go
+       I2C1->CR1 |= I2C_CR1_PE;
+
+       I2C1->CR2 |= ADDR << 1;
+       //I2C1->CR2 |= I2C_CR2_RD_WRN;
+       I2C1->CR2 &= ~(I2C_CR2_NBYTES);
+       I2C1->CR2 |= 1 << I2C_CR2_NBYTES_Pos;
+       I2C1->CR2 |= I2C_CR2_RELOAD;
+       I2C1->CR2 |= I2C_CR2_START;
+
+       while (!(I2C1->ISR & I2C_ISR_TXE));
+
+       I2C1->TXDR = ADDRESS;
+
+       while (I2C1->ISR & I2C_ISR_BUSY);
+
+       I2C1->ICR |= 0x30;
+       I2C1->CR2 |= I2C_CR2_RD_WRN;
+       I2C1->CR2 |= I2C_CR2_RELOAD;
+       I2C1->CR2 |= I2C_CR2_START;
+       
+       while (1) {
+               while (!(I2C1->ISR & I2C_ISR_RXNE));
+               uint32_t v = I2C1->RXDR;
+               (void)v;
+       }
+}
index 74ae6b72b79749c422424997b1075189d8f04a25..71ce68fc172b08bff652faf0e497c158e6c454f2 100644 (file)
@@ -13,6 +13,7 @@
 #include <script.h>\r
 #include <random.h>\r
 #include <keypad.h>\r
+#include <flash.h>\r
 \r
 extern uint8_t _ebss;\r
 extern char *itoa(int, char *, int);\r
@@ -24,7 +25,7 @@ int main(void)
 {\r
        asm("cpsid i");\r
        // disable cached writes for precise debug info\r
-       //*((uint32_t *)0xE000E008) |= 2;\r
+       *((uint32_t *)0xE000E008) |= 2;\r
 \r
        // prepare flash latency for 80MHz operation\r
        FLASH->ACR &= ~(FLASH_ACR_LATENCY);\r
@@ -58,14 +59,24 @@ void kmain(void)
        dsp_cursoron();\r
        task_start(task_interpreter, 4096);\r
 \r
+       /*char buf[2];\r
+       flash_init();\r
+       buf[0] = 'A';\r
+       flash_write(buf, 0, 1);\r
+       buf[0] = 0;\r
+       flash_read(buf, 0x00000000, 1);\r
+       buf[0] += ' ';\r
+       buf[1] = '\0';\r
+       dsp_puts(buf);*/\r
+\r
        while (1) {\r
-               gpio_dout(GPIOA, 5,\r
-                       (keypad_isdown(K0)));\r
-               delay(10);\r
-               /*gpio_dout(GPIOA, 5, 1);\r
+               //gpio_dout(GPIOA, 5,\r
+               //      (keypad_isdown(K0)));\r
+               //delay(10);\r
+               gpio_dout(GPIOA, 5, 1);\r
                delay(250);\r
                gpio_dout(GPIOA, 5, 0);\r
-               delay(250);*/\r
+               delay(250);\r
        }\r
 }\r
 \r
@@ -77,7 +88,7 @@ void task_interpreter(void)
 \r
        char *s = initrd_getfile("init");\r
        if (s == 0) {\r
-               dsp_puts("init not found");\r
+               dsp_puts("can't find init");\r
                goto end;\r
        }\r
 \r
index 78165b0eaa723847e35c89da88798c33350c4402..c7b361427f85922bd60b73e147ec6346e370978d 100644 (file)
@@ -19,6 +19,7 @@ int script_line(interpreter *it);
 int script_color(interpreter *it);
 int script_rand(interpreter *it);
 int script_getkey(interpreter *it);
+int script_pixel(interpreter *it);
 
 void script_loadlib(interpreter *it)
 {
@@ -31,11 +32,12 @@ void script_loadlib(interpreter *it)
        inew_cfunc(it, "color", script_color);
        inew_cfunc(it, "rand", script_rand);
        inew_cfunc(it, "getkey", script_getkey);
+       inew_cfunc(it, "pixel", script_pixel);
 }
 
 int script_puts(interpreter *it)
 {
-       char *s = igetarg_string(it, 0);
+       const char *s = igetarg_string(it, 0);
        dsp_puts(s);
        //dsp_puts("\n");
        //asm("mov r0, %0; svc 2" :: "r" (s));
@@ -44,7 +46,8 @@ int script_puts(interpreter *it)
 
 int script_gets(interpreter *it)
 {
-       char *s = malloc(64), c[2] = {0, 0};
+       char *s = malloc(64);
+       char c[2] = {0, 0};
        uint16_t index = 0;
 
        do {
@@ -57,7 +60,7 @@ int script_gets(interpreter *it)
 
        variable *v = igetarg(it, 0);
        v->valtype = STRING;
-       v->svalue = s;
+       v->value.p = (uint32_t)s;
        return 0;
 }
 
@@ -98,39 +101,33 @@ int script_color(interpreter *it)
 {
        uint16_t c = dsp_color(igetarg_integer(it, 0), igetarg_integer(it, 1),
                igetarg_integer(it, 2));
-       variable v;
-       v.valtype = INTEGER;
-       INT(&v) = c;
-       v.svalue = 0;
-       isetstr(&v);
-       iret(it, &v);
+       variable *v = make_varn(0, (float)c);
+       iret(it, v);
+       free(v);
        return 0;
 }
 
 int script_rand(interpreter *it)
 {
-       variable *v = (variable *)malloc(sizeof(variable));
        unsigned int mod = igetarg_integer(it, 0);
        unsigned int val = random_get();
-       v->valtype = INTEGER;
-       INT(v) = val % mod;
-       v->svalue = 0;
-       isetstr(v);
+       variable *v = make_varn(0, (float)(mod % val));
        iret(it, v);
-       free(v->svalue);
        free(v);
        return 0;
 }
 
 int script_getkey(interpreter *it)
 {
-       variable *v = (variable *)malloc(sizeof(variable));
-       v->valtype = INTEGER;
-       INT(v) = keypad_get();
-       v->svalue = 0;
-       isetstr(v);
+       variable *v = make_varn(0, (float)keypad_get());
        iret(it, v);
-       free(v->svalue);
        free(v);
        return 0;
 }
+
+int script_pixel(interpreter *it)
+{
+       dsp_pixel(igetarg_integer(it, 0), igetarg_integer(it, 1),
+               igetarg_integer(it, 2));
+       return 0;
+}
index 303db4d23dc7101c37e32c07d47064c44399e4c3..ec67ff357082f5ab8bd05b9bdccafe632bdc7a50 100644 (file)
@@ -40,6 +40,7 @@ char *snprintf(char *buf, unsigned int max, const char *format, ...)
                                break;
                        case 'f':
                                itoa((int)va_arg(args, double), nbuf, 10);
+                               continue;
                                break;
                        default:
                                buf[off++] = format[i];
@@ -47,7 +48,7 @@ char *snprintf(char *buf, unsigned int max, const char *format, ...)
                                break;
                        }
                        if (nbuf[0] != '\0') {
-                               for (unsigned int j = 0; off < max &&
+                               for (unsigned int j = 0; off < max && j < 32 &&
                                        nbuf[j] != '\0'; off++, j++)
                                        buf[off] = nbuf[j];
                        }