[nucleo-f072rb] Enable i2c and add i2c_rb command.

This commit is contained in:
Erik Gilling
2017-10-26 12:21:54 -07:00
committed by Erik Gilling
parent d89fd06507
commit 0c97b7f6a1
2 changed files with 36 additions and 0 deletions

View File

@@ -25,6 +25,8 @@
#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>
@@ -37,16 +39,24 @@ void target_early_init(void)
gpio_config(GPIO(GPIO_PORT_A, 2), GPIO_STM32_AF | GPIO_STM32_AFn(1));
gpio_config(GPIO(GPIO_PORT_A, 3), GPIO_STM32_AF | GPIO_STM32_AFn(1));
/* configure i2c pins */
gpio_config(GPIO(GPIO_PORT_B, 8), GPIO_PULLUP | GPIO_STM32_AF | GPIO_STM32_AFn(1)); // SCL
gpio_config(GPIO(GPIO_PORT_B, 9), GPIO_PULLUP | GPIO_STM32_AF | GPIO_STM32_AFn(1)); // SDA
stm32_debug_early_init();
/* configure some status leds */
gpio_set(GPIO_LED0, 1);
gpio_config(GPIO_LED0, GPIO_OUTPUT);
i2c_init_early();
}
void target_init(void)
{
stm32_debug_init();
i2c_init();
stm32_usbc_init();
target_usb_setup();
}
@@ -60,3 +70,27 @@ void target_set_debug_led(unsigned int led, bool on)
}
}
static int cmd_i2c_rb(int argc, const cmd_args *argv) {
if (argc != 3) {
printf("usage: i2c_rb <addr> <reg>\n");
return 1;
}
uint8_t addr = argv[1].u;
uint8_t reg = argv[2].u;
uint8_t data;
status_t ret = i2c_read_reg_bytes(1, addr, reg, &data, 1);
if (ret != NO_ERROR) {
printf("error: %d\n", ret);
return 1;
}
printf("%02x[%02x]: %02x\n", addr, reg, data);
return 0;
}
STATIC_COMMAND_START
STATIC_COMMAND("i2c_rb", "i2c_rb", &cmd_i2c_rb)
STATIC_COMMAND_END(nucleo);

View File

@@ -8,6 +8,8 @@ PLATFORM := stm32f0xx
GLOBAL_DEFINES += \
ENABLE_UART2=1 \
ENABLE_I2C1=1 \
ENABLE_I2C2=1 \
TARGET_HAS_DEBUG_LED=1
MODULE_SRCS += \