add i2c sample

This commit is contained in:
pvvx 2017-05-06 17:50:18 +03:00
parent 1752a87a8e
commit 60ffc5c30f
8 changed files with 929 additions and 8 deletions

View file

@ -0,0 +1,55 @@
/*
* i2c_drv.h
*
* Created on: 02/05/2017.
* Author: pvvx
*/
#ifndef PROJECT_INC_DRIVER_I2C_DRV_H_
#define PROJECT_INC_DRIVER_I2C_DRV_H_
#include "device.h"
typedef struct _I2C_HND_ {
signed char status;
unsigned char idx;
unsigned char io_sel;
unsigned char mode; // if(I2C_FIXED_SPEED_MODE != 0) user set -> i2c_mode_e
void * base_regs;
} i2c_drv_t, *i2c_drv_p;
typedef enum
{
DRV_I2C_OFF = 0, // IC I2C DeInit
DRV_I2C_OK = 0, // DRV ret Ok
DRV_I2C_IC_OFF = 1, // IC I2C Off
DRV_I2C_IC_ENABLE = 2, // IC I2C On
DRV_I2C_ERR = -1, // DRV ret err
DRV_I2C_ABORT = -1, // IC I2C Abort
DRV_I2C_TIMEOUT = -3 // IC I2C / DRV ret Timeout
} _i2c_status_e;
typedef enum
{
DRV_I2C_SS_MODE = 1, // Standard Mode (100 Kbps)
DRV_I2C_FS_MODE = 2, // Fast Mode (400 Kbps)
DRV_I2C_HS_MODE = 3 // Fast Mode Plus (1 Mbps)
} _i2c_mode_e;
#define DRV_I2C_POOL_TIMEOUT 16384
#define _i2c_deinit(p) _i2c_ic_off(p)
// Setup
int _i2c_setup(i2c_drv_t *pi2c, PinName sda, PinName scl, unsigned char mode); // _i2c_mode_e
int _i2c_set_speed(i2c_drv_t *pi2c, uint32 clk_hz);
// Work
int _i2c_init(i2c_drv_t *pi2c);
int _i2c_write(i2c_drv_t *pi2c, uint32 address, const char *data, int length, int stop);
int _i2c_read(i2c_drv_t *pi2c, uint32 address, const char *data, int length, int stop);
// Utils
int _i2c_break(i2c_drv_t *pi2c);
void _i2c_ic_off(i2c_drv_t *pi2c);
#endif /* PROJECT_INC_DRIVER_I2C_DRV_H_ */

View file

@ -0,0 +1,91 @@
/**************************************************************************/
/**************************************************************************/
#ifndef _INA219_H_
#define _INA219_H_
/*=========================================================================
I2C ADDRESS/BITS
-----------------------------------------------------------------------*/
#define INA219_ADDRESS (0x80) // 1000000r (A0+A1=GND)
#define INA219_READ (0x01)
/*=========================================================================*/
/*=========================================================================
CONFIG REGISTER (R/W)
-----------------------------------------------------------------------*/
#define INA219_REG_CONFIG (0x00)
/*---------------------------------------------------------------------*/
#define INA219_CONFIG_RESET (0x8000) // Reset Bit
#define INA219_CONFIG_BVOLTAGERANGE_MASK (0x2000) // Bus Voltage Range Mask
#define INA219_CONFIG_BVOLTAGERANGE_16V (0x0000) // 0-16V Range
#define INA219_CONFIG_BVOLTAGERANGE_32V (0x2000) // 0-32V Range
#define INA219_CONFIG_GAIN_MASK (0x1800) // Gain Mask
#define INA219_CONFIG_GAIN_1_40MV (0x0000) // Gain 1, 40mV Range
#define INA219_CONFIG_GAIN_2_80MV (0x0800) // Gain 2, 80mV Range
#define INA219_CONFIG_GAIN_4_160MV (0x1000) // Gain 4, 160mV Range
#define INA219_CONFIG_GAIN_8_320MV (0x1800) // Gain 8, 320mV Range
#define INA219_CONFIG_BADCRES_MASK (0x0780) // Bus ADC Resolution Mask
#define INA219_CONFIG_BADCRES_9BIT (0x0080) // 9-bit bus res = 0..511
#define INA219_CONFIG_BADCRES_10BIT (0x0100) // 10-bit bus res = 0..1023
#define INA219_CONFIG_BADCRES_11BIT (0x0200) // 11-bit bus res = 0..2047
#define INA219_CONFIG_BADCRES_12BIT (0x0400) // 12-bit bus res = 0..4097
#define INA219_CONFIG_SADCRES_MASK (0x0078) // Shunt ADC Resolution and Averaging Mask
#define INA219_CONFIG_SADCRES_9BIT_1S_84US (0x0000) // 1 x 9-bit shunt sample
#define INA219_CONFIG_SADCRES_10BIT_1S_148US (0x0008) // 1 x 10-bit shunt sample
#define INA219_CONFIG_SADCRES_11BIT_1S_276US (0x0010) // 1 x 11-bit shunt sample
#define INA219_CONFIG_SADCRES_12BIT_1S_532US (0x0018) // 1 x 12-bit shunt sample
#define INA219_CONFIG_SADCRES_12BIT_1S_532US_ (0x0040) // 1 x 12-bit shunt sample
#define INA219_CONFIG_SADCRES_12BIT_2S_1060US (0x0048) // 2 x 12-bit shunt samples averaged together
#define INA219_CONFIG_SADCRES_12BIT_4S_2130US (0x0050) // 4 x 12-bit shunt samples averaged together
#define INA219_CONFIG_SADCRES_12BIT_8S_4260US (0x0058) // 8 x 12-bit shunt samples averaged together
#define INA219_CONFIG_SADCRES_12BIT_16S_8510US (0x0060) // 16 x 12-bit shunt samples averaged together
#define INA219_CONFIG_SADCRES_12BIT_32S_17MS (0x0068) // 32 x 12-bit shunt samples averaged together
#define INA219_CONFIG_SADCRES_12BIT_64S_34MS (0x0070) // 64 x 12-bit shunt samples averaged together
#define INA219_CONFIG_SADCRES_12BIT_128S_69MS (0x0078) // 128 x 12-bit shunt samples averaged together
#define INA219_CONFIG_MODE_MASK (0x0007) // Operating Mode Mask
#define INA219_CONFIG_MODE_POWERDOWN (0x0000)
#define INA219_CONFIG_MODE_SVOLT_TRIGGERED (0x0001)
#define INA219_CONFIG_MODE_BVOLT_TRIGGERED (0x0002)
#define INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED (0x0003)
#define INA219_CONFIG_MODE_ADCOFF (0x0004)
#define INA219_CONFIG_MODE_SVOLT_CONTINUOUS (0x0005)
#define INA219_CONFIG_MODE_BVOLT_CONTINUOUS (0x0006)
#define INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS (0x0007)
/*=========================================================================*/
/*=========================================================================
SHUNT VOLTAGE REGISTER (R)
-----------------------------------------------------------------------*/
#define INA219_REG_SHUNTVOLTAGE (0x01)
/*=========================================================================*/
/*=========================================================================
BUS VOLTAGE REGISTER (R)
-----------------------------------------------------------------------*/
#define INA219_REG_BUSVOLTAGE (0x02)
/*=========================================================================*/
/*=========================================================================
POWER REGISTER (R)
-----------------------------------------------------------------------*/
#define INA219_REG_POWER (0x03)
/*=========================================================================*/
/*=========================================================================
CURRENT REGISTER (R)
-----------------------------------------------------------------------*/
#define INA219_REG_CURRENT (0x04)
/*=========================================================================*/
/*=========================================================================
CALIBRATION REGISTER (R/W)
-----------------------------------------------------------------------*/
#define INA219_REG_CALIBRATION (0x05)
/*=========================================================================*/
#endif // _INA219_H_

View file

@ -0,0 +1,43 @@
/**************************************************************************
* Драйвер для INA219
**************************************************************************/
#ifndef _INA219DRV_H_
#define _INA219DRV_H_
#include "ina219/ina219.h"
#include "device.h"
#include "timer_api.h"
#include "driver/i2c_drv.h"
typedef struct _ina219data {
signed short voltage; // Voltage, 1mV
signed short current; // Current, 50uA?
signed short power; // Power, 1mW
unsigned short shunt; // 10uV
} INA219DATA, *PINA219DATA;
typedef struct _ina219drv {
unsigned char status;
unsigned char addr;
signed char init;
unsigned char tmp;
unsigned short config; // регистр конфигурации INA219
unsigned short calibration; // коэф. шунта для INA219
unsigned int count;
unsigned int errs;
gtimer_t timer;
union { // буфер
unsigned char uc[4];
unsigned short us[2];
signed short ss[2];
unsigned int ui;
} buf_i2c;
volatile i2c_drv_t i2c;
} INA219DRV, *PINA219DRV;
#define INA219_I2C_PIN_SDA PC_4
#define INA219_I2C_PIN_SCL PC_5
#define INA219_I2C_BUS_CLK 300000 //hz
#define INA219_TIMER TIMER3 // используемый таймер
#endif // _INA219DRV_H_

View file

@ -43,11 +43,8 @@ enum srvconn_state {
#define TCP_SRV_SERVER_PORT 80
#endif
/* уровень вывода отладочной инфы по умолчанию
#ifndef DEBUGSOO
#define DEBUGSOO 2
#endif
*/
#define SRV_WDGREFESH_IN_POOL // использовать WDGRefresh() в tcpsrv_poll()
// время (сек), по умолчанию, ожидания запроса (передачи пакета) от клиента, до авто-закрытия соединения,
// при = 0 заменяется на эти 5 сек.