1 /*
2  * Copyright (c) 2012 Travis Geiselbrecht
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 #include <lk/err.h>
9 #include <lk/debug.h>
10 #include <target.h>
11 #include <lk/compiler.h>
12 #include <dev/gpio.h>
13 #include <dev/i2c.h>
14 #include <platform/gpio.h>
15 #include <platform/stm32.h>
16 #include <platform/usbc.h>
17 #include <target/gpioconfig.h>
18 #include <target/usb.h>
19 #include <lk/console_cmd.h>
20 
target_early_init(void)21 void target_early_init(void) {
22     /* configure the usart2 pins */
23     gpio_config(GPIO(GPIO_PORT_A, 2), GPIO_STM32_AF | GPIO_STM32_AFn(1));
24     gpio_config(GPIO(GPIO_PORT_A, 3), GPIO_STM32_AF | GPIO_STM32_AFn(1));
25 
26     /* configure i2c pins */
27     gpio_config(GPIO(GPIO_PORT_B, 8), GPIO_PULLUP | GPIO_STM32_AF | GPIO_STM32_AFn(1));  // SCL
28     gpio_config(GPIO(GPIO_PORT_B, 9), GPIO_PULLUP | GPIO_STM32_AF | GPIO_STM32_AFn(1));  // SDA
29 
30 
31     stm32_debug_early_init();
32 
33     /* configure some status leds */
34     gpio_set(GPIO_LED0, 1);
35     gpio_config(GPIO_LED0, GPIO_OUTPUT);
36 
37     i2c_init_early();
38 }
39 
target_init(void)40 void target_init(void) {
41     stm32_debug_init();
42     i2c_init();
43     stm32_usbc_init();
44     target_usb_setup();
45 }
46 
target_set_debug_led(unsigned int led,bool on)47 void target_set_debug_led(unsigned int led, bool on) {
48     switch (led) {
49         case 0:
50             gpio_set(GPIO_LED0, on);
51             break;
52     }
53 }
54 
cmd_i2c_rb(int argc,const console_cmd_args * argv)55 static int cmd_i2c_rb(int argc, const console_cmd_args *argv) {
56 
57     if (argc != 3) {
58         printf("usage: i2c_rb <addr> <reg>\n");
59         return 1;
60     }
61 
62     uint8_t addr = argv[1].u;
63     uint8_t reg = argv[2].u;
64     uint8_t data;
65 
66     status_t ret = i2c_read_reg_bytes(1, addr, reg, &data, 1);
67     if (ret != NO_ERROR) {
68         printf("error: %d\n", ret);
69         return 1;
70     }
71 
72     printf("%02x[%02x]: %02x\n", addr, reg, data);
73     return 0;
74 }
75 
76 STATIC_COMMAND_START
77 STATIC_COMMAND("i2c_rb", "i2c_rb", &cmd_i2c_rb)
78 STATIC_COMMAND_END(nucleo);
79