From d6876b1f77b306ed05d8baa88afb2fcb8825a3a1 Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Fri, 1 Sep 2017 23:01:56 +0000 Subject: [PATCH] f3:i2c: use printf and update docs Preparations for updating the tree was making sure the existing example worked :) --- .../stm32/f3/stm32f3-discovery/i2c/README.md | 7 ++- examples/stm32/f3/stm32f3-discovery/i2c/i2c.c | 45 ++++++++----------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/examples/stm32/f3/stm32f3-discovery/i2c/README.md b/examples/stm32/f3/stm32f3-discovery/i2c/README.md index 58f234f..3a99212 100644 --- a/examples/stm32/f3/stm32f3-discovery/i2c/README.md +++ b/examples/stm32/f3/stm32f3-discovery/i2c/README.md @@ -1,5 +1,8 @@ # README -I2C example reading from the stm32f3discovery accelerometer. - +UART TX on PA2 @ 115200/8n1 +This example reads the onboard accelerometer and dumps the raw value +of the ACC_OUT_X_L_A/ACC_OUT_X_H_A registers. (you should see ~0 for flat, +and positive/negative values for tipping the board along it's long axis, +ranging up to plus/minus 16k or so for vertical. diff --git a/examples/stm32/f3/stm32f3-discovery/i2c/i2c.c b/examples/stm32/f3/stm32f3-discovery/i2c/i2c.c index 29ef661..5c3176d 100644 --- a/examples/stm32/f3/stm32f3-discovery/i2c/i2c.c +++ b/examples/stm32/f3/stm32f3-discovery/i2c/i2c.c @@ -20,6 +20,8 @@ * along with this library. If not, see . */ +#include +#include #include #include #include @@ -97,34 +99,24 @@ static void gpio_setup(void) GPIO14 | GPIO15); } -static void my_usart_print_int(uint32_t usart, int32_t value) +int _write(int file, char *ptr, int len) { - int8_t i; - int8_t nr_digits = 0; - char buffer[25]; + int i; - if (value < 0) { - usart_send_blocking(usart, '-'); - value = value * -1; - } - - if (value == 0) { - usart_send_blocking(usart, '0'); - } - - while (value > 0) { - buffer[nr_digits++] = "0123456789"[value % 10]; - value /= 10; - } - - for (i = nr_digits-1; i >= 0; i--) { - usart_send_blocking(usart, buffer[i]); - } - - usart_send_blocking(usart, '\r'); - usart_send_blocking(usart, '\n'); + if (file == 1) { + for (i = 0; i < len; i++) { + if (ptr[i] == '\n') { + usart_send_blocking(USART2, '\r'); + } + usart_send_blocking(USART2, ptr[i]); + } + return i; + } + errno = EIO; + return -1; } + static void clock_setup(void) { rcc_clock_setup_hsi(&rcc_hsi_8mhz[RCC_CLOCK_64MHZ]); @@ -150,13 +142,14 @@ int main(void) clock_setup(); gpio_setup(); usart_setup(); + printf("Hello, we're running\n"); i2c_setup(); /*uint8_t data[1]={(0x4 << ACC_CTRL_REG1_A_ODR_SHIFT) | ACC_CTRL_REG1_A_XEN};*/ uint8_t data[1]={0x97}; write_i2c(I2C1, I2C_ACC_ADDR, ACC_CTRL_REG1_A, 1, data); data[0]=0x08; write_i2c(I2C1, I2C_ACC_ADDR, ACC_CTRL_REG4_A, 1, data); - uint16_t acc_x; + int16_t acc_x; while (1) { @@ -166,7 +159,7 @@ int main(void) acc_x=data[0]; read_i2c(I2C1, I2C_ACC_ADDR, ACC_OUT_X_H_A, 1, data); acc_x|=(data[0] << 8); - my_usart_print_int(USART2, (int16_t) acc_x); + printf("data was %d\n", acc_x); //int i; //for (i = 0; i < 800000; i++) /* Wait a bit. */ // __asm__("nop");