| // 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 <app/zedmon/ina.h> |
| #include <lk/err.h> |
| #include <lk/debug.h> |
| #include <dev/gpio.h> |
| #include <dev/i2c.h> |
| #include <lib/console.h> |
| #include <platform/gpio.h> |
| #include <platform/stm32.h> |
| #include <platform/timer_capture.h> |
| #include <platform/usbc.h> |
| #include <target.h> |
| #include <target/gpioconfig.h> |
| #include <target/usb.h> |
| |
| static uint8_t board_id; |
| static stm32_timer_capture_t zedmon_alert_tc; |
| |
| static bool zedmon_alert0_capture_irq(uint64_t val) { |
| return ina_alert_irq(0, val); |
| } |
| |
| uint64_t ina_get_current_time(void) { |
| return stm32_timer_capture_get_counter(&zedmon_alert_tc); |
| } |
| |
| 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_ALERT_HIGH, GPIO_STM32_AF | GPIO_STM32_AFn(1)); |
| gpio_config(GPIO_ALERT_LOW, GPIO_STM32_AF | GPIO_STM32_AFn(1)); |
| |
| gpio_config(GPIO_BOARD_ID0, GPIO_INPUT | GPIO_PULLDOWN); |
| gpio_config(GPIO_BOARD_ID1, GPIO_INPUT | GPIO_PULLDOWN); |
| gpio_config(GPIO_BOARD_ID2, GPIO_INPUT | GPIO_PULLDOWN); |
| gpio_config(GPIO_BOARD_ID3, GPIO_INPUT | GPIO_PULLDOWN); |
| |
| 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); |
| |
| gpio_set(GPIO_POWER_ENABLE_J, 0); |
| gpio_config(GPIO_POWER_ENABLE_J, GPIO_OUTPUT); |
| |
| 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(); |
| |
| zedmon_alert_tc.chan[0] = (stm32_timer_capture_channel_t){ |
| .flags = STM32_TIMER_CAPTURE_CHAN_FLAG_FALLING | STM32_TIMER_CAPTURE_CHAN_FLAG_ENABLE, |
| .cb = zedmon_alert0_capture_irq, |
| }; |
| |
| stm32_timer_capture_setup(&zedmon_alert_tc, 3, 48); |
| } |
| |
| 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; |
| } |
| } |
| |
| static int cmd_led(int argc, const cmd_args *argv) { |
| if (argc != 4) { |
| printf("usage: led <r> <g> <b>\n"); |
| return 1; |
| } |
| |
| gpio_set(GPIO_LED_RED, !argv[1].b); |
| gpio_set(GPIO_LED_GREEN, !argv[2].b); |
| gpio_set(GPIO_LED_BLUE, !argv[3].b); |
| |
| return 0; |
| } |
| |
| |
| STATIC_COMMAND_START |
| STATIC_COMMAND("led", "set led r, g, b.", &cmd_led) |
| STATIC_COMMAND_END(zedmon); |