/* * 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 #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 int _write(int file, char *ptr, int len); static void i2c_setup(void) { rcc_periph_clock_enable(RCC_I2C1); rcc_periph_clock_enable(RCC_GPIOB); 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, 0); /* HSI is at 8Mhz */ i2c_set_speed(I2C1, i2c_speed_sm_100k, 8); //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); } static void usart_setup(void) { /* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */ rcc_periph_clock_enable(RCC_USART2); rcc_periph_clock_enable(RCC_GPIOA); /* 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); } static void gpio_setup(void) { rcc_periph_clock_enable(RCC_GPIOE); gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO8 | GPIO9 | GPIO10 | GPIO11 | GPIO12 | GPIO13 | GPIO14 | GPIO15); } int _write(int file, char *ptr, int len) { int i; 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_configs[RCC_CLOCK_HSI_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 int main(void) { clock_setup(); gpio_setup(); usart_setup(); printf("Hello, we're running\n"); i2c_setup(); uint8_t cmd = ACC_CTRL_REG1_A; uint8_t data; i2c_transfer7(I2C1, I2C_ACC_ADDR, &cmd, 1, &data, 1); cmd = ACC_CTRL_REG4_A; i2c_transfer7(I2C1, I2C_ACC_ADDR, &cmd, 1, &data, 1); int16_t acc_x; while (1) { cmd = ACC_STATUS; i2c_transfer7(I2C1, I2C_ACC_ADDR, &cmd, 1, &data, 1); cmd = ACC_OUT_X_L_A; i2c_transfer7(I2C1, I2C_ACC_ADDR, &cmd, 1, &data, 1); acc_x = data; cmd = ACC_OUT_X_H_A; i2c_transfer7(I2C1, I2C_ACC_ADDR, &cmd, 1, &data, 1); acc_x |= ((uint16_t)data << 8); printf("data was %d\n", acc_x); } return 0; }