From 41ac81a66bc48ff4d34bc5013630f90d4fa561f5 Mon Sep 17 00:00:00 2001
From: NIIBE Yutaka <gniibe@fsij.org>
Date: Wed, 29 Jun 2016 16:37:09 +0900
Subject: Update example for fs-bb48

---
 example-fs-bb48/Makefile  |  2 +-
 example-fs-bb48/command.c | 29 +++++++++++++--
 example-fs-bb48/sample.c  |  4 +++
 example-fs-bb48/touch.c   | 89 +++++++++++++++++++++++++++++++++++++++++++++++
 example-fs-bb48/usb-cdc.c | 10 ++++--
 5 files changed, 129 insertions(+), 5 deletions(-)
 create mode 100644 example-fs-bb48/touch.c

(limited to 'example-fs-bb48')

diff --git a/example-fs-bb48/Makefile b/example-fs-bb48/Makefile
index bf1fed7..e4d0c73 100644
--- a/example-fs-bb48/Makefile
+++ b/example-fs-bb48/Makefile
@@ -6,7 +6,7 @@ PROJECT = sample
 
 CHOPSTX = ..
 LDSCRIPT= sample.ld
-CSRC = sample.c usb-cdc.c command.c
+CSRC = sample.c usb-cdc.c command.c touch.c
 CHIP=mkl27z
 
 USE_SYS = yes
diff --git a/example-fs-bb48/command.c b/example-fs-bb48/command.c
index 791854a..f498dc7 100644
--- a/example-fs-bb48/command.c
+++ b/example-fs-bb48/command.c
@@ -51,7 +51,6 @@ static char hexchar (uint8_t x)
     return '?';
 }
 
-#ifdef ENABLE_DECIMAL_OUTPUT
 static char *
 compose_decimal (char *s, int value)
 {
@@ -87,7 +86,7 @@ compose_decimal (char *s, int value)
 
   return s;
 }
-#endif
+
 
 static char *
 compose_hex (char *s, uint32_t v)
@@ -143,6 +142,31 @@ get_hex (struct tty *tty, const char *s, uint32_t *v_p)
 }
 
 
+static void
+cmd_touch (struct tty *tty, const char *line)
+{
+  int i;
+  extern uint16_t touch_get (void);
+
+  (void)line;
+  put_line (tty, "Please touch the bear, type Enter to finish.\r\n");
+
+  for (i = 0; i < 20; i++)
+    {
+      uint16_t v;
+      char output[8];
+      char *s;
+
+      chopstx_usec_wait (1000*1000);
+      v = touch_get ();
+      s = compose_decimal (output, v);
+      *s++ = '\r';
+      *s++ = '\n';
+      tty_send (tty, output, s - output);
+    }
+}
+
+
 static void
 cmd_mdw (struct tty *tty, const char *line)
 {
@@ -425,6 +449,7 @@ cmd_help (struct tty *tty, const char *line)
 
 
 struct command_table command_table[] = {
+  { "touch", cmd_touch },
   { "mdw", cmd_mdw },
   { "mww", cmd_mww },
   { "fes", cmd_fes },
diff --git a/example-fs-bb48/sample.c b/example-fs-bb48/sample.c
index 7d6a3be..1738566 100644
--- a/example-fs-bb48/sample.c
+++ b/example-fs-bb48/sample.c
@@ -117,6 +117,8 @@ static char hexchar (uint8_t x)
 }
 
 
+extern void touch_init (void);
+
 int
 main (int argc, const char *argv[])
 {
@@ -144,6 +146,8 @@ main (int argc, const char *argv[])
 
   u = 1;
 
+  touch_init ();
+
   tty = tty_open ();
   tty_wait_configured (tty);
 
diff --git a/example-fs-bb48/touch.c b/example-fs-bb48/touch.c
new file mode 100644
index 0000000..895c6bd
--- /dev/null
+++ b/example-fs-bb48/touch.c
@@ -0,0 +1,89 @@
+#include <stdint.h>
+#include <stdlib.h>
+#include <chopstx.h>
+#include <mcu/mkl27z.h>
+
+struct TPM {
+  volatile uint32_t SC;
+  volatile uint32_t CNT;
+  volatile uint32_t MOD;
+  volatile uint32_t C0SC;
+  volatile uint32_t C0V;
+  volatile uint32_t C1SC;
+  volatile uint32_t C1V;
+  uint32_t rsvd0[14];
+  volatile uint32_t STATUS;
+  uint32_t rsvd1[7];
+  volatile uint32_t POL;
+  uint32_t rsvd2[4];
+  volatile uint32_t CONF;
+};
+
+static struct TPM *const TPM1 = (struct TPM *const)0x40039000;
+
+static chopstx_intr_t tpm1_intr;
+#define INTR_REQ_TPM1 18
+
+void
+touch_init (void)
+{
+  chopstx_claim_irq (&tpm1_intr, INTR_REQ_TPM1);
+
+  PORTB->PCR1 = (1<<3) /* TPM1_CH1              */
+              | (0<<6) /* DriveStrengthEnable=0 */
+              | (0<<4) /* PassiveFilterEnable=0 */
+              | (1<<2) /* SlewRateEnable = slow */
+              | (0<<1) /* pull enable = 0       */ 
+              | (0<<0) /* puddselect= 0         */
+              ;
+
+  /* TOF clear, TOIE=1, CPWMS=0, CMOD=1, PS=000.  */
+  TPM1->SC = 0xc4;
+
+  /* Input capture mode: MSB = 0, MSA = 0 */
+  /*        Rising edge: ELSB=0 ELSA=1 */
+  TPM1->C1SC = 0x82;
+  TPM1->POL=0;
+
+  /* Triggered by TPM1_CH1.   */
+  /*   channel 1: TRGSEL=0010 */
+  /*   external:  TRGSRC=0    */
+  /*   active low:TRGPOL=1    */
+  /* stop on overflow: CSOO=1 */ 
+  /* start on trigger: CSOT=1 */
+  TPM1->CONF = 0x02c30000;
+
+  /* Wait overflow.  */
+  chopstx_intr_wait (&tpm1_intr);
+
+  /* Clear overflow.  */
+  TPM1->SC |= 0x80;
+}
+
+uint16_t
+touch_get (void)
+{
+  /* Assert LOW.  */ 
+  PORTB->PCR1 = (1<<8) /* GPIO                  */
+              | (0<<6) /* DriveStrengthEnable=0 */
+              | (0<<4) /* PassiveFilterEnable=0 */
+              | (1<<2) /* SlewRateEnable = slow */
+              | (0<<1) /* pull enable = 0       */ 
+              | (0<<0) /* puddselect= 0         */
+              ;
+
+  /* Let the register to pull it up.  */
+  PORTB->PCR1 = (1<<3) /* TPM1_CH1              */
+              | (0<<6) /* DriveStrengthEnable=0 */
+              | (0<<4) /* PassiveFilterEnable=0 */
+              | (1<<2) /* SlewRateEnable = slow */
+              | (0<<1) /* pull enable = 0       */ 
+              | (0<<0) /* puddselect= 0         */
+              ;
+
+  chopstx_intr_wait (&tpm1_intr);
+  /* Clear overflow.  */
+  TPM1->SC |= 0x80;
+
+  return TPM1->C1V;
+}
diff --git a/example-fs-bb48/usb-cdc.c b/example-fs-bb48/usb-cdc.c
index 0936a40..38b16ea 100644
--- a/example-fs-bb48/usb-cdc.c
+++ b/example-fs-bb48/usb-cdc.c
@@ -686,7 +686,10 @@ tty_main (void *arg)
 
   while (1)
     {
-      chopstx_poll (NULL, 1, &usb_intr);
+      struct chx_poll_head *pd_array[1] = {
+	(struct chx_poll_head *)&usb_intr
+      };
+      chopstx_poll (NULL, 1, pd_array);
       if (usb_intr.ready)
 	{
 	  uint8_t ep_num;
@@ -919,7 +922,10 @@ tty_recv (struct tty *t, char *buf, uint32_t *timeout)
 
   while (1)
     {
-      chopstx_poll (timeout, 1, &poll_desc);
+      struct chx_poll_head *pd_array[1] = {
+	(struct chx_poll_head *)&poll_desc
+      };
+      chopstx_poll (timeout, 1, pd_array);
       chopstx_mutex_lock (&t->mtx);
       r = check_rx (t);
       chopstx_mutex_unlock (&t->mtx);
-- 
cgit v1.2.3