STM32F10x library
Data Structures | Macros | Functions | Variables
i2c.c File Reference

I2C master routines for. More...

#include <stm32f10x.h>
#include <stm32f10x_i2c.h>
#include <stm32f10x_rcc.h>
#include <stm32f10x_gpio.h>
#include "i2c.h"

Macros

#define i2c_waitfor(_cond, _ec)   { uint16_t _tmo = 0xffff; while( _cond ) { if (_tmo-- == 0) return _ec; } }
 

Functions

void i2c_init (uint8_t devnum, const uint32_t clkspd)
 Init I2C. More...
 
uint8_t i2c_rd (uint8_t devnum, uint8_t adr, uint8_t *buf, uint32_t nbyte)
 Read from I2C. More...
 
uint8_t i2c_wr (uint8_t devnum, uint8_t adr, const uint8_t *buf, uint32_t nbyte)
 Write to I2C. More...
 

Variables

struct I2C_DevDef I2C1_PinDef = {1, I2C1, RCC_APB1Periph_I2C1, GPIOB, RCC_APB2Periph_GPIOB, GPIO_Pin_6, GPIO_Pin_7}
 
struct I2C_DevDef I2C2_PinDef = {1, I2C2, RCC_APB1Periph_I2C2, GPIOB, RCC_APB2Periph_GPIOB, GPIO_Pin_10, GPIO_Pin_11}
 

Detailed Description

I2C master routines for.

Author
Matej Kogovsek (matej.nosp@m.@ham.nosp@m.radio.nosp@m..si)
Note
This file is part of mat-stm32f1-lib
This file was not written by me from scratch. It was adapted from code by Geoffrey Brown at https://github.com/geoffreymbrown/STM32-Template

Macro Definition Documentation

#define i2c_waitfor (   _cond,
  _ec 
)    { uint16_t _tmo = 0xffff; while( _cond ) { if (_tmo-- == 0) return _ec; } }

Convenience macro that waits for a zero result from a function with a timeout

Function Documentation

void i2c_init ( uint8_t  devnum,
const uint32_t  clkspd 
)

Init I2C.

Parameters
[in]devnumI2C peripheral number (1 or 2)
[in]clkspdBaudrate (i.e. 100000 for 100kHz), F_CPU independent
uint8_t i2c_rd ( uint8_t  devnum,
uint8_t  adr,
uint8_t *  buf,
uint32_t  nbyte 
)

Read from I2C.

Parameters
[in]devnumI2C peripheral number (1 or 2)
[in]adrI2C address
[out]bufpointer to caller allocated buffer for data
[in]nbytenumber of bytes to read (nbyte <= sizeof(buf))
Returns
0 on success, non-zero otherwise
uint8_t i2c_wr ( uint8_t  devnum,
uint8_t  adr,
const uint8_t *  buf,
uint32_t  nbyte 
)

Write to I2C.

Parameters
[in]devnumI2C peripheral number (1 or 2)
[in]adrI2C address
[in]bufpointer to data
[in]nbytenumber of bytes to write (nbyte <= sizeof(buf))
Returns
0 on success, non-zero otherwise

Variable Documentation

struct I2C_DevDef I2C1_PinDef = {1, I2C1, RCC_APB1Periph_I2C1, GPIOB, RCC_APB2Periph_GPIOB, GPIO_Pin_6, GPIO_Pin_7}

Register and pin defs for I2C1

struct I2C_DevDef I2C2_PinDef = {1, I2C2, RCC_APB1Periph_I2C2, GPIOB, RCC_APB2Periph_GPIOB, GPIO_Pin_10, GPIO_Pin_11}

Register and pin defs for I2C2