Software APIs
hello_world.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #include "sw/device/examples/demos.h"
13 #include "sw/device/lib/testing/pinmux_testutils.h"
14 #include "sw/device/lib/testing/test_framework/check.h"
15 #include "sw/device/lib/testing/test_framework/ottf_test_config.h"
16 
18 
19 OTTF_DEFINE_TEST_CONFIG();
20 
21 static dif_gpio_t gpio;
22 static dif_pinmux_t pinmux;
23 static dif_uart_t uart;
24 
25 static dif_pinmux_index_t leds[] = {
30 };
31 
32 static dif_pinmux_index_t switches[] = {
37 };
38 
39 void configure_pinmux(void) {
40  pinmux_testutils_init(&pinmux);
41  // Hook up some LEDs.
42  for (size_t i = 0; i < ARRAYSIZE(leds); ++i) {
44  CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, leds[i], gpio));
45  }
46  // Hook up DIP switches.
47  for (size_t i = 0; i < ARRAYSIZE(switches); ++i) {
49  CHECK_DIF_OK(dif_pinmux_input_select(&pinmux, gpio, switches[i]));
50  }
51 }
52 
53 void _ottf_main(void) {
54  CHECK_DIF_OK(dif_pinmux_init(
56  configure_pinmux();
57 
58  CHECK_DIF_OK(dif_uart_init(
60 
61  CHECK(kUartBaudrate <= UINT32_MAX, "kUartBaudrate must fit in uint32_t");
62  CHECK(kClockFreqPeripheralHz <= UINT32_MAX,
63  "kClockFreqPeripheralHz must fit in uint32_t");
64  CHECK_DIF_OK(dif_uart_configure(
65  &uart, (dif_uart_config_t){
66  .baudrate = (uint32_t)kUartBaudrate,
67  .clk_freq_hz = (uint32_t)kClockFreqPeripheralHz,
68  .parity_enable = kDifToggleDisabled,
69  .parity = kDifUartParityEven,
70  .tx_enable = kDifToggleEnabled,
71  .rx_enable = kDifToggleEnabled,
72  }));
73  base_uart_stdout(&uart);
74 
75  CHECK_DIF_OK(
76  dif_gpio_init(mmio_region_from_addr(TOP_EARLGREY_GPIO_BASE_ADDR), &gpio));
77  // Enable GPIO: 0-3 is output; 8-11 is input.
78  CHECK_DIF_OK(dif_gpio_output_set_enabled_all(&gpio, 0xF));
79 
80  // Add DATE and TIME because I keep fooling myself with old versions
81  LOG_INFO("Hello World!");
82  LOG_INFO("Built at: " __DATE__ ", " __TIME__);
83 
84  demo_gpio_startup(&gpio);
85 
86  // Now have UART <-> Buttons/LEDs demo
87  // all LEDs off
88  CHECK_DIF_OK(dif_gpio_write_all(&gpio, 0x0000));
89  LOG_INFO("Try out USERDIP switches 0-thru-3 on the board");
90  LOG_INFO("or type anything into the console window.");
91  LOG_INFO(
92  "The LEDs show the lower nibble of the ASCII code of the last "
93  "character.");
94 
95  uint32_t gpio_state = 0;
96  while (true) {
97  busy_spin_micros(10 * 1000); // 10 ms
98  gpio_state = demo_gpio_to_log_echo(&gpio, gpio_state);
99  demo_uart_to_uart_and_gpio_echo(&uart, &gpio);
100  }
101 }