From e7bd234a0daeff4c16ef80f1529aa1f9dd500697 Mon Sep 17 00:00:00 2001
From: NIIBE Yutaka <gniibe@fsij.org>
Date: Thu, 30 Jun 2016 16:16:39 +0900
Subject: Update for MKL27Z

---
 example-fs-bb48/command.c | 40 +++++++++++++++++++++++++++++++++++++++-
 example-fs-bb48/crc32.c   |  2 +-
 example-fs-bb48/sample.c  | 22 +---------------------
 example-fs-bb48/touch.c   | 36 ++++++++++++++++++------------------
 4 files changed, 59 insertions(+), 41 deletions(-)

(limited to 'example-fs-bb48')

diff --git a/example-fs-bb48/command.c b/example-fs-bb48/command.c
index f498dc7..1a6582c 100644
--- a/example-fs-bb48/command.c
+++ b/example-fs-bb48/command.c
@@ -142,6 +142,43 @@ get_hex (struct tty *tty, const char *s, uint32_t *v_p)
 }
 
 
+#define TOUCH_VALUE_HIGH 195
+#define TOUCH_VALUE_LOW  150
+static void
+cmd_button (struct tty *tty, const char *line)
+{
+  int i = 0;
+  extern uint16_t touch_get (void);
+  uint16_t v0 = 0;
+  int touched = 0;
+
+  (void)line;
+  put_line (tty, "Please touch the bear.\r\n");
+
+  while (i < 16)
+    {
+      uint16_t v = touch_get ();
+      v0 = (v0 * 2 + v)/3;
+
+      if (touched == 0 && v0 > TOUCH_VALUE_HIGH)
+	{
+	  tty_send (tty, "!", 1);
+	  touched = 1;
+	}
+      else if (touched == 1 && v0 < TOUCH_VALUE_LOW)
+	{
+	  tty_send (tty, ".", 1);
+	  touched = 0;
+	  i++;
+	}
+
+      chopstx_usec_wait (10*1000);
+    }
+
+  tty_send (tty, "\r\n", 2);
+}
+
+
 static void
 cmd_touch (struct tty *tty, const char *line)
 {
@@ -149,7 +186,7 @@ cmd_touch (struct tty *tty, const char *line)
   extern uint16_t touch_get (void);
 
   (void)line;
-  put_line (tty, "Please touch the bear, type Enter to finish.\r\n");
+  put_line (tty, "Please touch the bear.\r\n");
 
   for (i = 0; i < 20; i++)
     {
@@ -449,6 +486,7 @@ cmd_help (struct tty *tty, const char *line)
 
 
 struct command_table command_table[] = {
+  { "button", cmd_button },
   { "touch", cmd_touch },
   { "mdw", cmd_mdw },
   { "mww", cmd_mww },
diff --git a/example-fs-bb48/crc32.c b/example-fs-bb48/crc32.c
index 4335295..9535324 100644
--- a/example-fs-bb48/crc32.c
+++ b/example-fs-bb48/crc32.c
@@ -1,4 +1,4 @@
-const unsigned int *const crc32_table= (const unsigned int *const)0x00000480;
+const unsigned int *const crc32_table= (const unsigned int *)0x00000480;
 
 void
 crc32_init (unsigned int *p)
diff --git a/example-fs-bb48/sample.c b/example-fs-bb48/sample.c
index 1738566..3041d7d 100644
--- a/example-fs-bb48/sample.c
+++ b/example-fs-bb48/sample.c
@@ -7,27 +7,7 @@
 #include "tty.h"
 #include "board.h"
 #include "command.h"
-
-struct GPIO {
-  volatile uint32_t PDOR; /* Port Data Output Register    */
-  volatile uint32_t PSOR; /* Port Set Output Register     */
-  volatile uint32_t PCOR; /* Port Clear Output Register   */
-  volatile uint32_t PTOR; /* Port Toggle Output Register  */
-  volatile uint32_t PDIR; /* Port Data Input Register     */
-  volatile uint32_t PDDR; /* Port Data Direction Register */
-};
-static struct GPIO *const GPIOB = (struct GPIO *const)0x400FF040;
-static struct GPIO *const GPIOD = (struct GPIO *const)0x400FF0C0;
-static struct GPIO *const GPIOE = (struct GPIO *const)0x400FF100;
-
-static void
-set_led (int on)
-{
-  if (on)
-    GPIOB->PCOR = (1 << 0); /* PTB0: Clear: Light on  */
-  else
-    GPIOB->PSOR = (1 << 0); /* PTB0: Set  : Light off */
-}
+#include <sys.h>
 
 static chopstx_mutex_t mtx;
 static chopstx_cond_t cnd0;
diff --git a/example-fs-bb48/touch.c b/example-fs-bb48/touch.c
index 033da62..127be23 100644
--- a/example-fs-bb48/touch.c
+++ b/example-fs-bb48/touch.c
@@ -19,7 +19,7 @@ struct TPM {
   volatile uint32_t CONF;
 };
 
-static struct TPM *const TPM1 = (struct TPM *const)0x40039000;
+static struct TPM *const TPM1 = (struct TPM *)0x40039000;
 
 static chopstx_intr_t tpm1_intr;
 #define INTR_REQ_TPM1 18
@@ -36,8 +36,14 @@ touch_get (void)
               | (0<<0) /* puddselect= 0         */
               ;
 
+  /* 
+   * Start the timer's counter. 
+   * TOF clear, TOIE=1, CPWMS=0, CMOD=1, PS=011.
+   */
+  TPM1->SC = 0xcb;
+
   /* Let the register to pull it up.  */
-  PORTB->PCR1 = (1<<3) /* TPM1_CH1              */
+  PORTB->PCR1 = (3<<8) /* TPM1_CH1              */
               | (0<<6) /* DriveStrengthEnable=0 */
               | (0<<4) /* PassiveFilterEnable=0 */
               | (1<<2) /* SlewRateEnable = slow */
@@ -46,8 +52,10 @@ touch_get (void)
               ;
 
   chopstx_intr_wait (&tpm1_intr);
-  /* Clear overflow.  */
-  TPM1->SC |= 0x80;
+  /* Clear overflow and CH1 capture.  */
+  TPM1->STATUS = 0x102;
+  /* Stop the counter.  */
+  TPM1->SC = 0;
 
   return TPM1->C1V;
 }
@@ -58,21 +66,13 @@ touch_init (void)
 {
   chopstx_claim_irq (&tpm1_intr, INTR_REQ_TPM1);
 
-  /* 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;
+  /*   Rising edge: ELSB=0 ELSA=1 */
+  TPM1->C1SC = 0x84;
   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;
-
-  touch_get ();
+  /* No trigger.   */
+  /* Stop on overflow: CSOO=1 */ 
+  /* Run the timer in the debug mode */
+  TPM1->CONF = 0x000200c0;
 }
-- 
cgit v1.2.3