diff --git a/examples/stm32/f3/stm32f3-discovery/i2c/Makefile b/examples/stm32/f3/stm32f3-discovery/i2c/Makefile new file mode 100644 index 0000000..c9bd71c --- /dev/null +++ b/examples/stm32/f3/stm32f3-discovery/i2c/Makefile @@ -0,0 +1,24 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BINARY = i2c + +LDSCRIPT = ../stm32f3-discovery.ld + +include ../../Makefile.include diff --git a/examples/stm32/f3/stm32f3-discovery/i2c/README b/examples/stm32/f3/stm32f3-discovery/i2c/README new file mode 100644 index 0000000..4794c3a --- /dev/null +++ b/examples/stm32/f3/stm32f3-discovery/i2c/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +I2C example reading from the stm32f3discovery accelerometer. + + diff --git a/examples/stm32/f3/stm32f3-discovery/i2c/i2c.c b/examples/stm32/f3/stm32f3-discovery/i2c/i2c.c new file mode 100644 index 0000000..1b39e54 --- /dev/null +++ b/examples/stm32/f3/stm32f3-discovery/i2c/i2c.c @@ -0,0 +1,250 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Stephen Caudle + * Modified by Fernando Cortes + * modified by Guillermo Rivera + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include + +#define LBLUE GPIOE, GPIO8 +#define LRED GPIOE, GPIO9 +#define LORANGE GPIOE, GPIO10 +#define LGREEN GPIOE, GPIO11 +#define LBLUE2 GPIOE, GPIO12 +#define LRED2 GPIOE, GPIO13 +#define LORANGE2 GPIOE, GPIO14 +#define LGREEN2 GPIOE, GPIO15 + +#define LD4 GPIOE, GPIO8 +#define LD3 GPIOE, GPIO9 +#define LD5 GPIOE, GPIO10 +#define LD7 GPIOE, GPIO11 +#define LD9 GPIOE, GPIO12 +#define LD10 GPIOE, GPIO13 +#define LD8 GPIOE, GPIO14 +#define LD6 GPIOE, GPIO15 + +void i2c_setup(void) { + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_I2C1EN); + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPBEN); + rcc_set_i2c_clock_hsi(I2C1); + + i2c_reset(I2C1); + /* Setup GPIO pin GPIO_USART2_TX/GPIO9 on GPIO port A for transmit. */ + gpio_mode_setup(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO6 | GPIO7); + gpio_set_af(GPIOB, GPIO_AF4, GPIO6| GPIO7); + i2c_peripheral_disable(I2C1); + //configure ANFOFF DNF[3:0] in CR1 + i2c_enable_analog_filter(I2C1); + i2c_set_digital_filter(I2C1, I2C_CR1_DNF_DISABLED); + //Configure PRESC[3:0] SDADEL[3:0] SCLDEL[3:0] SCLH[7:0] SCLL[7:0] in TIMINGR + i2c_100khz_i2cclk8mhz(I2C1); + //configure No-Stretch CR1 (only relevant in slave mode) + i2c_enable_stretching(I2C1); + //addressing mode + i2c_set_7bit_addr_mode(I2C1); + i2c_peripheral_enable(I2C1); +} + +void usart_setup(void) { + /* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPAEN); + + /* Setup GPIO pin GPIO_USART2_TX/GPIO9 on GPIO port A for transmit. */ + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2 | GPIO3); + gpio_set_af(GPIOA, GPIO_AF7, GPIO2| GPIO3); + + /* Setup UART parameters. */ + usart_set_baudrate(USART2, 115200); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX_RX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +void gpio_setup(void) +{ + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPEEN); + gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO8| GPIO9| GPIO10| GPIO11| GPIO12| GPIO13| GPIO14| GPIO15); +} + +void my_usart_print_int(uint32_t usart, int32_t value) +{ + int8_t i; + int8_t nr_digits = 0; + char buffer[25]; + + 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'); +} + +void clock_setup(void) { + /*rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_44MHZ]);*/ + rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_64MHZ]); +} + +#define I2C_ACC_ADDR 0x19 +#define I2C_MAG_ADDR 0x1E +#define ACC_STATUS 0x27 +#define ACC_CTRL_REG1_A 0x20 +#define ACC_CTRL_REG1_A_ODR_SHIFT 4 +#define ACC_CTRL_REG1_A_ODR_MASK 0xF +#define ACC_CTRL_REG1_A_XEN (1 << 0) +#define ACC_CTRL_REG4_A 0x23 + +#define ACC_OUT_X_L_A 0x28 +#define ACC_OUT_X_H_A 0x29 + +// gpio_port_write(GPIOE, (I2C_ISR(i2c) & 0xFF) << 8); +// my_usart_print_int(USART2, (I2C_ISR(i2c) & 0xFF)); + + +void write_i2c(uint32_t i2c, uint8_t i2c_addr, uint8_t reg, uint8_t size, uint8_t *data) { + int wait; + int i; + while (i2c_busy(i2c) == 1) {} + while (i2c_is_start(i2c) == 1) {} + //Setting transfer properties + i2c_set_bytes_to_transfer(i2c, size+1); + i2c_set_7bit_address(i2c, (i2c_addr & 0x7F)); + i2c_set_write_transfer_dir(i2c); + i2c_enable_autoend(i2c); + //start transfer + i2c_send_start(i2c); + + wait=true; + while (wait) { + if (i2c_transmit_int_status(i2c)) { + wait=false; + } + while (i2c_nack(i2c)){} + } + i2c_send_data(i2c, reg); + for (i=0; i