Add initial zedmon firmware.

Change-Id: Ib62f2917c68fe15f6cfd6eea8278e5b67d3e0e87
diff --git a/firmware/lk_inc.mk b/firmware/lk_inc.mk
new file mode 100644
index 0000000..b009e40
--- /dev/null
+++ b/firmware/lk_inc.mk
@@ -0,0 +1,7 @@
+LOCAL_DIR := firmware
+LKMAKEROOT := ..
+LKROOT := lk
+LKINC := $(LOCAL_DIR)
+DEFAULT_PROJECT ?= zedmon
+BUILDROOT ?= $(LOCAL_DIR)
+TOOLCHAIN_PREFIX :=
diff --git a/firmware/makefile b/firmware/makefile
new file mode 120000
index 0000000..35f19ce
--- /dev/null
+++ b/firmware/makefile
@@ -0,0 +1 @@
+../lk/makefile
\ No newline at end of file
diff --git a/firmware/project/zedmon.mk b/firmware/project/zedmon.mk
new file mode 100644
index 0000000..a8323af
--- /dev/null
+++ b/firmware/project/zedmon.mk
@@ -0,0 +1,7 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULES += \
+	app/shell \
+	lib/debugcommands
+
+TARGET := zedmon
diff --git a/firmware/target/zedmon/include/target/debugconfig.h b/firmware/target/zedmon/include/target/debugconfig.h
new file mode 100644
index 0000000..562fe53
--- /dev/null
+++ b/firmware/target/zedmon/include/target/debugconfig.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2017 The Fuchsia Authors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#ifndef __TARGET_DEBUGCONFIG_H
+#define __TARGET_DEBUGCONFIG_H
+
+#define DEBUG_UART 1
+
+#endif
diff --git a/firmware/target/zedmon/include/target/gpioconfig.h b/firmware/target/zedmon/include/target/gpioconfig.h
new file mode 100644
index 0000000..6e8145f
--- /dev/null
+++ b/firmware/target/zedmon/include/target/gpioconfig.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2017 The Fuchsia Authors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#ifndef __TARGET_GPIOCONFIG_H
+#define __TARGET_GPIOCONFIG_H
+
+#include <platform/gpio.h>
+
+#define GPIO_LED_RED   GPIO(GPIO_PORT_B, 13)
+#define GPIO_LED_GREEN GPIO(GPIO_PORT_B, 14)
+#define GPIO_LED_BLUE  GPIO(GPIO_PORT_B, 15)
+
+#define GPIO_SCL       GPIO(GPIO_PORT_B, 10)
+#define GPIO_SDA       GPIO(GPIO_PORT_B, 11)
+
+#define GPIO_BOARD_ID0 GPIO(GPIO_PORT_A, 0)
+#define GPIO_BOARD_ID1 GPIO(GPIO_PORT_A, 1)
+#define GPIO_BOARD_ID2 GPIO(GPIO_PORT_A, 2)
+#define GPIO_BOARD_ID3 GPIO(GPIO_PORT_A, 3)
+
+#define GPIO_UART_TX   GPIO(GPIO_PORT_A, 9)
+#define GPIO_UART_RX   GPIO(GPIO_PORT_A, 10)
+
+#endif
diff --git a/firmware/target/zedmon/include/target/usb.h b/firmware/target/zedmon/include/target/usb.h
new file mode 100644
index 0000000..99522d0
--- /dev/null
+++ b/firmware/target/zedmon/include/target/usb.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2017 The Fuchsia Authors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef __TARGET_USB_H
+#define __TARGET_USB_H
+
+void target_usb_setup(void);
+
+#endif
diff --git a/firmware/target/zedmon/init.c b/firmware/target/zedmon/init.c
new file mode 100644
index 0000000..9a6b4e7
--- /dev/null
+++ b/firmware/target/zedmon/init.c
@@ -0,0 +1,90 @@
+// Copyright 2017 The Fuchsia Authors. All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//    * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+//    * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+//    * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include <err.h>
+#include <debug.h>
+#include <target.h>
+#include <compiler.h>
+#include <dev/gpio.h>
+#include <dev/i2c.h>
+#include <lib/console.h>
+#include <platform/gpio.h>
+#include <platform/stm32.h>
+#include <platform/usbc.h>
+#include <target/gpioconfig.h>
+#include <target/usb.h>
+
+static uint8_t board_id;
+
+void target_early_init(void)
+{
+    gpio_config(GPIO_UART_TX, GPIO_STM32_AF | GPIO_STM32_AFn(1));
+    gpio_config(GPIO_UART_RX, GPIO_STM32_AF | GPIO_STM32_AFn(1));
+
+    gpio_config(GPIO_SCL, GPIO_STM32_AF | GPIO_STM32_AFn(1));
+    gpio_config(GPIO_SDA, GPIO_STM32_AF | GPIO_STM32_AFn(1));
+
+    gpio_config(GPIO_BOARD_ID0, GPIO_INPUT);
+    gpio_config(GPIO_BOARD_ID1, GPIO_INPUT);
+    gpio_config(GPIO_BOARD_ID2, GPIO_INPUT);
+    gpio_config(GPIO_BOARD_ID3, GPIO_INPUT);
+
+    gpio_set(GPIO_LED_RED, 1);
+    gpio_set(GPIO_LED_GREEN, 1);
+    gpio_set(GPIO_LED_BLUE, 1);
+    gpio_config(GPIO_LED_RED, GPIO_OUTPUT | GPIO_STM32_OD);
+    gpio_config(GPIO_LED_GREEN, GPIO_OUTPUT | GPIO_STM32_OD);
+    gpio_config(GPIO_LED_BLUE, GPIO_OUTPUT | GPIO_STM32_OD);
+
+    board_id = gpio_get(GPIO_BOARD_ID0)
+        | gpio_get(GPIO_BOARD_ID1) << 1
+        | gpio_get(GPIO_BOARD_ID2) << 2
+        | gpio_get(GPIO_BOARD_ID3) << 3;
+
+    stm32_debug_early_init();
+    i2c_init_early();
+}
+
+void target_init(void)
+{
+    stm32_debug_init();
+    i2c_init();
+    stm32_usbc_init();
+    target_usb_setup();
+
+    printf("zedmon online.  board_id=%1x\n", board_id);
+}
+
+void target_set_debug_led(unsigned int led, bool on)
+{
+    switch (led) {
+        case 0:
+            gpio_set(GPIO_LED_RED, !on);
+            break;
+    }
+}
diff --git a/firmware/target/zedmon/rules.mk b/firmware/target/zedmon/rules.mk
new file mode 100644
index 0000000..3cdd4c1
--- /dev/null
+++ b/firmware/target/zedmon/rules.mk
@@ -0,0 +1,23 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULE := $(LOCAL_DIR)
+
+STM32_CHIP := stm32f072_xB
+
+PLATFORM := stm32f0xx
+
+GLOBAL_DEFINES += \
+	ENABLE_UART2=1 \
+	ENABLE_I2C1=1 \
+	ENABLE_I2C2=1 \
+	TARGET_HAS_DEBUG_LED=1
+
+MODULE_SRCS += \
+	$(LOCAL_DIR)/init.c \
+	$(LOCAL_DIR)/usb.c
+
+MODULE_DEPS += \
+    dev/usb/class/cdcserial
+
+include make/module.mk
+
diff --git a/firmware/target/zedmon/usb.c b/firmware/target/zedmon/usb.c
new file mode 100644
index 0000000..3e98cf6
--- /dev/null
+++ b/firmware/target/zedmon/usb.c
@@ -0,0 +1,224 @@
+/*
+ * Copyright (c) 2013-2015 Travis Geiselbrecht
+ * Copyright (c) 2017 The Fuchsia Authors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <err.h>
+#include <debug.h>
+#include <stdio.h>
+#include <trace.h>
+#include <target.h>
+#include <compiler.h>
+#include <dev/usb.h>
+#include <dev/usbc.h>
+#include <dev/usb/class/cdcserial.h>
+#include <hw/usb.h>
+#include <lk/init.h>
+
+#define LOCAL_TRACE 0
+
+#define W(w) (w & 0xff), (w >> 8)
+#define W3(w) (w & 0xff), ((w >> 8) & 0xff), ((w >> 16) & 0xff)
+
+/* top level device descriptor */
+static const uint8_t dev_descr[] = {
+    0x12,           /* descriptor length */
+    DEVICE,         /* Device Descriptor type */
+    W(0x0200),      /* USB Version */
+    239,           /* class */
+    2,           /* subclass */
+    1,           /* protocol */
+    64,             /* max packet size, ept0 */
+    W(0x18d1),      /* vendor */
+    W(0xaf00),      /* product */
+    W(0x0000),      /* release */
+    0x2,            /* manufacturer string */
+    0x1,            /* product string */
+    0x0,            /* serialno string */
+    0x1,            /* num configs */
+};
+
+/* high/low speed device qualifier */
+static const uint8_t devqual_descr[] = {
+    0x0a,           /* len */
+    DEVICE_QUALIFIER, /* Device Qualifier type */
+    W(0x0200),      /* USB version */
+    0x00,           /* class */
+    0x00,           /* subclass */
+    0x00,           /* protocol */
+    64,             /* max packet size, ept0 */
+    0x01,           /* num configs */
+    0x00            /* reserved */
+};
+
+static const uint8_t cfg_descr[] = {
+    0x09,           /* Length of Cfg Descr */
+    CONFIGURATION,  /* Type of Cfg Descr */
+    W(0x09),        /* Total Length (incl ifc, ept) */
+    0x00,           /* # Interfaces */
+    0x01,           /* Cfg Value */
+    0x00,           /* Cfg String */
+    0xc0,           /* Attributes -- self powered */
+    250,            /* Power Consumption - 500mA */
+};
+
+static const uchar langid[] = { 0x04, 0x03, 0x09, 0x04 };
+
+static const uint8_t if_descriptor_lowspeed[] = {
+    0x09,           /* length */
+    INTERFACE,      /* type */
+    0x01,           /* interface num */
+    0x00,           /* alternates */
+    0x02,           /* endpoint count */
+    0xff,           /* interface class */
+    0xff,           /* interface subclass */
+    0x00,           /* interface protocol */
+    0x00,           /* string index */
+
+    /* endpoint 1 IN */
+    0x07,           /* length */
+    ENDPOINT,       /* type */
+    0x83,           /* address: 1 IN */
+    0x02,           /* type: bulk */
+    W(64),          /* max packet size: 64 */
+    00,             /* interval */
+
+    /* endpoint 1 OUT */
+    0x07,           /* length */
+    ENDPOINT,       /* type */
+    0x03,           /* address: 1 OUT */
+    0x02,           /* type: bulk */
+    W(64),          /* max packet size: 64 */
+    00,             /* interval */
+};
+
+usb_config config = {
+    .lowspeed = {
+        .device = USB_DESC_STATIC(dev_descr),
+        .device_qual = USB_DESC_STATIC(devqual_descr),
+        .config = USB_DESC_STATIC(cfg_descr),
+    },
+    .highspeed = {
+        .device = USB_DESC_STATIC(dev_descr),
+        .device_qual = USB_DESC_STATIC(devqual_descr),
+        .config = USB_DESC_STATIC(cfg_descr),
+    },
+
+    .langid = USB_DESC_STATIC(langid),
+};
+
+static status_t ep_cb_rx(ep_t endpoint, usbc_transfer_t *t);
+static status_t ep_cb_tx(ep_t endpoint, usbc_transfer_t *t);
+
+static void queue_rx(void)
+{
+    static usbc_transfer_t transfer;
+    static uint8_t buf[512];
+
+    transfer.callback = &ep_cb_rx;
+    transfer.result = 0;
+    transfer.buf = &buf;
+    transfer.buflen = sizeof(buf);
+    transfer.bufpos = 0;
+    transfer.extra = 0;
+
+    usbc_queue_rx(3, &transfer);
+}
+
+static void queue_tx(void)
+{
+    static usbc_transfer_t transfer;
+    static uint8_t buf[512];
+
+    for (uint i = 0; i < sizeof(buf); i++) {
+        buf[i] = ~i;
+    }
+
+    transfer.callback = &ep_cb_tx;
+    transfer.result = 0;
+    transfer.buf = &buf;
+    transfer.buflen = sizeof(buf);
+    transfer.bufpos = 0;
+    transfer.extra = 0;
+
+    usbc_queue_tx(3, &transfer);
+}
+
+static status_t ep_cb_rx(ep_t endpoint, usbc_transfer_t *t)
+{
+#if LOCAL_TRACE
+    LTRACEF("ep %u transfer %p\n", endpoint, t);
+    usbc_dump_transfer(t);
+
+    if (t->result >= 0) {
+        hexdump8(t->buf, t->bufpos);
+    }
+#endif
+
+    if (t->result >= 0)
+        queue_rx();
+
+    return NO_ERROR;
+}
+
+static status_t ep_cb_tx(ep_t endpoint, usbc_transfer_t *t)
+{
+#if LOCAL_TRACE
+    LTRACEF("ep %u transfer %p\n", endpoint, t);
+    usbc_dump_transfer(t);
+#endif
+
+    if (t->result >= 0)
+        queue_tx();
+
+    return NO_ERROR;
+}
+
+static status_t usb_cb(void *cookie, usb_callback_op_t op, const union usb_callback_args *args)
+{
+    LTRACEF("cookie %p, op %u, args %p\n", cookie, op, args);
+
+    if (op == USB_CB_ONLINE) {
+        usbc_setup_endpoint(3, USB_IN, 0x40, USB_BULK);
+        usbc_setup_endpoint(3, USB_OUT, 0x40, USB_BULK);
+
+        queue_rx();
+        queue_tx();
+    }
+    return NO_ERROR;
+}
+
+void target_usb_setup(void)
+{
+    usb_setup(&config);
+    printf("appending interfaces\n");
+    cdcserial_create_channel(0x1, 0x2);
+    cdcserial_init();
+
+    usb_append_interface_lowspeed(if_descriptor_lowspeed, sizeof(if_descriptor_lowspeed));
+    usb_append_interface_highspeed(if_descriptor_lowspeed, sizeof(if_descriptor_lowspeed));
+    usb_register_callback(&usb_cb, NULL);
+
+    usb_add_string("Zedmon", 1);
+    usb_add_string("Google Inc.", 2);
+
+    usb_start();
+}