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

---
 contrib/adc-mkl27z.c       |  8 ++++----
 example-fs-bb48/command.c  | 40 +++++++++++++++++++++++++++++++++++++++-
 example-fs-bb48/crc32.c    |  2 +-
 example-fs-bb48/sample.c   | 22 +---------------------
 example-fs-bb48/touch.c    | 36 ++++++++++++++++++------------------
 mcu/clk_gpio_init-mkl27z.c |  6 +++---
 mcu/mkl27z.h               | 15 ++++++++-------
 mcu/sys-mkl27z.c           |  4 ++--
 mcu/usb-mkl27z.c           | 12 ++++++------
 9 files changed, 82 insertions(+), 63 deletions(-)

diff --git a/contrib/adc-mkl27z.c b/contrib/adc-mkl27z.c
index d5b679e..35f3e0b 100644
--- a/contrib/adc-mkl27z.c
+++ b/contrib/adc-mkl27z.c
@@ -40,7 +40,7 @@ struct DMAMUX {
   volatile uint32_t CHCFG2;
   volatile uint32_t CHCFG3;
 };
-static struct DMAMUX *const DMAMUX = (struct DMAMUX *const)0x40021000;
+static struct DMAMUX *const DMAMUX = (struct DMAMUX *)0x40021000;
 
 #define INTR_REQ_DMA0 0
 
@@ -50,8 +50,8 @@ struct DMA {
   volatile uint32_t DSR_BCR;
   volatile uint32_t DCR;
 };
-static struct DMA *const DMA0 = (struct DMA *const)0x40008100;
-static struct DMA *const DMA1 = (struct DMA *const)0x40008110;
+static struct DMA *const DMA0 = (struct DMA *)0x40008100;
+static struct DMA *const DMA1 = (struct DMA *)0x40008110;
 
 
 /* We don't use ADC interrupt.  Just for reference.  */
@@ -92,7 +92,7 @@ struct ADC {
   volatile uint32_t CLM1;
   volatile uint32_t CLM0;
 };
-static struct ADC *const ADC0 = (struct ADC *const)0x4003B000;
+static struct ADC *const ADC0 = (struct ADC *)0x4003B000;
 
 /* SC1 */
 #define ADC_SC1_DIFF            (1 << 5)
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;
 }
diff --git a/mcu/clk_gpio_init-mkl27z.c b/mcu/clk_gpio_init-mkl27z.c
index 894a8ad..d126d94 100644
--- a/mcu/clk_gpio_init-mkl27z.c
+++ b/mcu/clk_gpio_init-mkl27z.c
@@ -38,7 +38,7 @@ struct MCG {
   uint8_t reserved2[15]; /*                                    */
   volatile uint8_t MC;   /* MCG Miscellaneous Control Register */
 };
-static struct MCG *const MCG = (struct MCG *const)0x40064000;
+static struct MCG *const MCG = (struct MCG *)0x40064000;
 
 struct USB_CLK_RECOVER {
   volatile uint8_t CTRL;           /* USB Clock             */
@@ -51,7 +51,7 @@ struct USB_CLK_RECOVER {
                                    /*     interrupt status  */
 };
 static struct USB_CLK_RECOVER *const USB_CLK_RECOVER =
-  (struct USB_CLK_RECOVER *const)0x40072140;
+  (struct USB_CLK_RECOVER *)0x40072140;
 
 static void __attribute__((used))
 clock_init (void)
@@ -71,7 +71,7 @@ clock_init (void)
 
   SIM->SCGC4 = (1 << 18);	/* Enable USB FS clock        */
   SIM->SCGC5 = (1 << 10);	/* Enable Port B clock        */
-  SIM->SCGC6 = (1 << 25);	/* Enable TPM1 clock          */
+  SIM->SCGC6 = (1 << 25)|1;	/* Enable TPM1 clock          */
   SIM->COPC = 0;		/* COP disabled               */
 
   /* Crystal-less USB setup.  */
diff --git a/mcu/mkl27z.h b/mcu/mkl27z.h
index ad3c54c..1ede77f 100644
--- a/mcu/mkl27z.h
+++ b/mcu/mkl27z.h
@@ -28,7 +28,6 @@ struct SIM {
   volatile uint32_t COPC;     /* COP Control Register                    */
   volatile uint32_t SRVCOP;   /* Service COP                             */
 };
-static struct SIM *const SIM = (struct SIM *const)0x40047000;
 
 /* Port control. */
 struct PORT {
@@ -52,9 +51,6 @@ struct PORT {
   uint32_t reserved[6];
   volatile uint32_t ISFR;
 };
-static struct PORT *const PORTB = (struct PORT *const)0x4004A000;
-static struct PORT *const PORTD = (struct PORT *const)0x4004C000;
-static struct PORT *const PORTE = (struct PORT *const)0x4004D000;
 
 struct GPIO {
   volatile uint32_t PDOR; /* Port Data Output Register    */
@@ -64,6 +60,11 @@ struct GPIO {
   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 struct SIM *const SIM = (struct SIM *)0x40047000;
+static struct PORT *const PORTB = (struct PORT *)0x4004A000;
+static struct PORT *const PORTD = (struct PORT *)0x4004C000;
+static struct PORT *const PORTE = (struct PORT *)0x4004D000;
+static struct GPIO *const GPIOB = (struct GPIO *)0x400FF040;
+static struct GPIO *const GPIOD = (struct GPIO *)0x400FF0C0;
+static struct GPIO *const GPIOE = (struct GPIO *)0x400FF100;
diff --git a/mcu/sys-mkl27z.c b/mcu/sys-mkl27z.c
index e44e0c2..84b38ab 100644
--- a/mcu/sys-mkl27z.c
+++ b/mcu/sys-mkl27z.c
@@ -118,7 +118,7 @@ struct FTFA {
   /* Note: addressing (3,2,1,0).  Use Bx macro.  */
   volatile uint8_t FPROT[4];
 };
-static struct FTFA *const FTFA = (struct FTFA *const)0x40020000;
+static struct FTFA *const FTFA = (struct FTFA *)0x40020000;
 
 #define FSTAT_CCIF 0x80
 #define B3 0
@@ -297,7 +297,7 @@ crc32_init  (unsigned int *p)
 }
 
 #ifdef ORIGINAL_IN_C
-const unsigned int *const crc32_table= (const unsigned int *const)0x00000500;
+const unsigned int *const crc32_table= (const unsigned int *)0x00000500;
 #endif
 
 void __attribute__ ((naked,section(".fixed_function.crc32_u8")))
diff --git a/mcu/usb-mkl27z.c b/mcu/usb-mkl27z.c
index ae7e8af..2d35385 100644
--- a/mcu/usb-mkl27z.c
+++ b/mcu/usb-mkl27z.c
@@ -50,12 +50,12 @@ struct USB_CONF {
   uint8_t rsvd2[3];          /*                                     */
   volatile uint8_t ADDINFO;  /* Peripheral Additional Info register */
 };
-static struct USB_CONF *const USB_CONF = (struct USB_CONF *const) 0x40072000;
+static struct USB_CONF *const USB_CONF = (struct USB_CONF *) 0x40072000;
 
 struct USB_CTRL0 {
   volatile uint8_t OTGCTL;   /* OTG Control register                */
 };
-static struct USB_CTRL0 *const USB_CTRL0 = (struct USB_CTRL0 *const)0x4007201c;
+static struct USB_CTRL0 *const USB_CTRL0 = (struct USB_CTRL0 *)0x4007201c;
 
 struct USB_CTRL1 {
   volatile uint8_t ISTAT;    /* Interrupt Status register           */
@@ -82,7 +82,7 @@ struct USB_CTRL1 {
   uint8_t rsvd15[3];         /*                                     */
   volatile uint8_t BDTPAGE3; /* BDT Page Register 3                 */
 };
-static struct USB_CTRL1 *const USB_CTRL1 = (struct USB_CTRL1 *const)0x40072080;
+static struct USB_CTRL1 *const USB_CTRL1 = (struct USB_CTRL1 *)0x40072080;
 
 /* Interrupt source bits */
 #define USB_IS_STALL  (1 << 7)
@@ -98,7 +98,7 @@ struct USB_ENDPT {
   volatile uint8_t EP;  /* Endpoint Control register */
   uint8_t rsvd17[3];
 };
-static struct USB_ENDPT *const USB_ENDPT = (struct USB_ENDPT *const)0x400720c0;
+static struct USB_ENDPT *const USB_ENDPT = (struct USB_ENDPT *)0x400720c0;
 
 struct USB_CTRL2 {
   volatile uint8_t USBCTRL;  /* USB Control register                */
@@ -111,7 +111,7 @@ struct USB_CTRL2 {
   uint8_t rsvd36[7];         /*                                     */
   volatile uint8_t USBFRMADJUST;           /* Frame Adjut Register  */
 };
-static struct USB_CTRL2 *const USB_CTRL2 = (struct USB_CTRL2 *const)0x40072100;
+static struct USB_CTRL2 *const USB_CTRL2 = (struct USB_CTRL2 *)0x40072100;
 
 /* Buffer Descriptor */
 struct BD {
@@ -136,7 +136,7 @@ struct BD {
 
 extern uint8_t __usb_bdt__;
 
-static struct BD *const BD_table = (struct BD *const)&__usb_bdt__;
+static struct BD *const BD_table = (struct BD *)&__usb_bdt__;
 
 static void
 kl27z_usb_init (void)
-- 
cgit v1.2.3