Created
July 9, 2015 16:52
-
-
Save maknoll/7780fbcb8fb3c842ab12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// This file is part of the GNU ARM Eclipse distribution. | |
// Copyright (c) 2014 Liviu Ionescu. | |
// | |
// ---------------------------------------------------------------------------- | |
#include <stdio.h> | |
#include "diag/Trace.h" | |
#include <stm32f30x.h> | |
#include <stm32f30x_i2c.h> | |
#include <stm32f30x_rcc.h> | |
#include <stm32f30x_gpio.h> | |
// ---------------------------------------------------------------------------- | |
// | |
// Standalone STM32F3 empty sample (trace via ITM). | |
// | |
// Trace support is enabled by adding the TRACE macro definition. | |
// By default the trace messages are forwarded to the ITM output, | |
// but can be rerouted to any device or completely suppressed, by | |
// changing the definitions required in system/src/diag/trace_impl.c | |
// (currently OS_USE_TRACE_ITM, OS_USE_TRACE_SEMIHOSTING_DEBUG/_STDOUT). | |
// | |
// ----- main() --------------------------------------------------------------- | |
// Sample pragmas to cope with warnings. Please note the related line at | |
// the end of this function, used to pop the compiler diagnostics status. | |
#pragma GCC diagnostic push | |
#pragma GCC diagnostic ignored "-Wunused-parameter" | |
#pragma GCC diagnostic ignored "-Wmissing-declarations" | |
#pragma GCC diagnostic ignored "-Wreturn-type" | |
static const int I2C_TIMEOUT = 65536; | |
unsigned char i2c_buffer[256]; | |
uint8_t i2c_read(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t len) | |
{ | |
uint32_t timeout = I2C_TIMEOUT; | |
while(I2C_GetFlagStatus(I2C1, I2C_ISR_BUSY) != RESET) | |
{ | |
if((timeout--) == 0) return -1; | |
} | |
I2C_TransferHandling(I2C1, DeviceAddr<<1, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); | |
timeout = I2C_TIMEOUT; | |
while(I2C_GetFlagStatus(I2C1, I2C_ISR_TXIS) == RESET) | |
{ | |
if((timeout--) == 0) return -1; | |
} | |
I2C_SendData(I2C1, RegAddr); | |
timeout = I2C_TIMEOUT; | |
while(I2C_GetFlagStatus(I2C1, I2C_ISR_TC) == RESET) | |
{ | |
if((timeout--) == 0) return -1; | |
} | |
I2C_TransferHandling(I2C1, DeviceAddr<<1, len, I2C_AutoEnd_Mode, I2C_Generate_Start_Read); | |
for (int i = 0; i < len; ++i) { | |
while(I2C_GetFlagStatus(I2C1, I2C_ISR_RXNE) == RESET) | |
{ | |
if((timeout--) == 0) return -1; | |
} | |
pBuffer[i] = I2C_ReceiveData(I2C1); | |
} | |
timeout = I2C_TIMEOUT; | |
while(I2C_GetFlagStatus(I2C1, I2C_ISR_STOPF) == RESET) | |
{ | |
if((timeout--) == 0) return -1; | |
} | |
I2C_ClearFlag(I2C1, I2C_ICR_STOPCF); | |
return 0; | |
} | |
void init_i2c(void) | |
{ | |
GPIO_InitTypeDef GPIO_InitStructure; | |
I2C_InitTypeDef I2C_InitStructure; | |
RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK); | |
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); | |
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); | |
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_4); | |
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_4); | |
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; | |
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; | |
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; | |
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; | |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; | |
GPIO_Init(GPIOB, &GPIO_InitStructure); | |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; | |
GPIO_Init(GPIOB, &GPIO_InitStructure); | |
I2C_DeInit(I2C1); | |
I2C_StructInit(&I2C_InitStructure); | |
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; | |
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable; | |
I2C_InitStructure.I2C_DigitalFilter = 0x00; | |
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; | |
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; | |
I2C_InitStructure.I2C_Timing = 0xC062121F; | |
I2C_Init(I2C1, &I2C_InitStructure); | |
I2C_Cmd(I2C1, ENABLE); | |
} | |
int | |
main(int argc, char* argv[]) | |
{ | |
// At this stage the system clock should have already been configured | |
// at high speed. | |
init_i2c(); | |
// Infinite loop | |
while (1) | |
{ | |
i2c_read(0x42, 0x00, i2c_buffer, 22); | |
} | |
} | |
#pragma GCC diagnostic pop | |
// ---------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment