stm32f3discovery: Adding i2c accelerometer example.
This commit is contained in:
committed by
Piotr Esden-Tempski
parent
63f3bd7713
commit
e1d5325736
24
examples/stm32/f3/stm32f3-discovery/i2c/Makefile
Normal file
24
examples/stm32/f3/stm32f3-discovery/i2c/Makefile
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
##
|
||||||
|
## This file is part of the libopencm3 project.
|
||||||
|
##
|
||||||
|
## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||||
|
##
|
||||||
|
## 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 <http://www.gnu.org/licenses/>.
|
||||||
|
##
|
||||||
|
|
||||||
|
BINARY = i2c
|
||||||
|
|
||||||
|
LDSCRIPT = ../stm32f3-discovery.ld
|
||||||
|
|
||||||
|
include ../../Makefile.include
|
||||||
7
examples/stm32/f3/stm32f3-discovery/i2c/README
Normal file
7
examples/stm32/f3/stm32f3-discovery/i2c/README
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
------------------------------------------------------------------------------
|
||||||
|
README
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
I2C example reading from the stm32f3discovery accelerometer.
|
||||||
|
|
||||||
|
|
||||||
250
examples/stm32/f3/stm32f3-discovery/i2c/i2c.c
Normal file
250
examples/stm32/f3/stm32f3-discovery/i2c/i2c.c
Normal file
@@ -0,0 +1,250 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the libopencm3 project.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||||||
|
* Copyright (C) 2011 Stephen Caudle <scaudle@doceme.com>
|
||||||
|
* Modified by Fernando Cortes <fermando.corcam@gmail.com>
|
||||||
|
* modified by Guillermo Rivera <memogrg@gmail.com>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <libopencm3/stm32/f3/rcc.h>
|
||||||
|
#include <libopencm3/stm32/f3/adc.h>
|
||||||
|
#include <libopencm3/stm32/f3/usart.h>
|
||||||
|
#include <libopencm3/stm32/f3/i2c.h>
|
||||||
|
#include <libopencm3/stm32/gpio.h>
|
||||||
|
|
||||||
|
#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<size; i++) {
|
||||||
|
wait=true;
|
||||||
|
while (wait) {
|
||||||
|
if (i2c_transmit_int_status(i2c)) {
|
||||||
|
wait=false;
|
||||||
|
}
|
||||||
|
while (i2c_nack(i2c)){}
|
||||||
|
}
|
||||||
|
i2c_send_data(i2c, data[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_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, 1);
|
||||||
|
i2c_set_7bit_address(i2c, i2c_addr);
|
||||||
|
i2c_set_write_transfer_dir(i2c);
|
||||||
|
i2c_disable_autoend(i2c);
|
||||||
|
/*start transfer*/
|
||||||
|
i2c_send_start(i2c);
|
||||||
|
|
||||||
|
wait=true;
|
||||||
|
while (wait)
|
||||||
|
{
|
||||||
|
if (i2c_transmit_int_status(i2c)) {
|
||||||
|
wait=false;
|
||||||
|
}
|
||||||
|
while (i2c_nack(i2c)){} /* Some error */
|
||||||
|
}
|
||||||
|
i2c_send_data(i2c, reg);
|
||||||
|
|
||||||
|
while (i2c_is_start(i2c) == 1) {}
|
||||||
|
/*Setting transfer properties*/
|
||||||
|
i2c_set_bytes_to_transfer(i2c, size);
|
||||||
|
i2c_set_7bit_address(i2c, i2c_addr);
|
||||||
|
i2c_set_read_transfer_dir(i2c);
|
||||||
|
i2c_enable_autoend(i2c);
|
||||||
|
/*start transfer*/
|
||||||
|
i2c_send_start(i2c);
|
||||||
|
|
||||||
|
for (i=0; i<size; i++) {
|
||||||
|
while (i2c_received_data(i2c) == 0) {}
|
||||||
|
data[i]=i2c_get_data(i2c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
uint16_t temp, inc=0;
|
||||||
|
int wait;
|
||||||
|
clock_setup();
|
||||||
|
gpio_setup();
|
||||||
|
usart_setup();
|
||||||
|
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);
|
||||||
|
uint8_t data_in[20];
|
||||||
|
uint16_t acc_x;
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
|
||||||
|
read_i2c(I2C1, I2C_ACC_ADDR, ACC_STATUS, 1, data);
|
||||||
|
/*my_usart_print_int(USART2, data[0]);*/
|
||||||
|
read_i2c(I2C1, I2C_ACC_ADDR, ACC_OUT_X_L_A, 1, data);
|
||||||
|
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);
|
||||||
|
//int i;
|
||||||
|
//for (i = 0; i < 800000; i++) /* Wait a bit. */
|
||||||
|
// __asm__("nop");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user