A
download i2c.h
Language: C
LOC: 77
Project Info
paparazzi
Server: Savannah NonGNU
Type: cvs
...zzi\paparazzi3\sw\airborne\
   3dmg.c
   3dmg.h
   actuators.c
   actuators.h
   adc.h
   adc_generic.c
   adc_generic.h
   ahrs.c
   ahrs.h
   ahrs_new.c
   ahrs_new.h
   anemotaxis.c
   anemotaxis.h
   ap_downlink.h
   autopilot.h
   bomb.c
   bomb.h
   cam.c
   cam.h
   cam_roll.c
   cam_roll.h
   chemo_detect.c
   chemo_detect.h
   chemotaxis.c
   chemotaxis.h
   commands.c
   commands.h
   control_grz.c
   control_grz.h
   datalink.c
   datalink.h
   dc.c
   dc.h
   discsurvey.c
   discsurvey.h
   downlink.c
   downlink.h
   dpicco.c
   dpicco.h
   enose.c
   enose.h
   estimator.c
   estimator.h
   fbw_downlink.h
   frames.h
   fw_h_ctl.c
   fw_h_ctl.h
   fw_v_ctl.c
   fw_v_ctl.h
   gps.c
   gps.h
   gps_ubx.c
   gps_ubx.h
   gyro.c
   gyro.h
   i2c.c
   i2c.h
   imu.c
   imu.h
   imu_v3.c
   imu_v3.h
   infrared.c
   infrared.h
   inter_mcu.c
   inter_mcu.h
   latlong.c
   latlong.h
   led.h
   link_imu.c
   link_imu.h
   link_mcu.c
   link_mcu.h
   main.c
   main_antenna.c
   main_ap.c
   main_ap.h
   main_demo1.c
   main_demo2.c
   main_demo3.c
   main_demo4.c
   main_demo5.c
   main_demo6.c
   main_fbw.c
   main_fbw.h
   main_grz.c
   main_imu.c
   main_motor_bench.c
   modem.h
   nav.c
   nav.h
   nav_line.c
   nav_line.h
   nav_survey_rectangle.c
   nav_survey_rectangle.h
   paparazzi.h
   point.c
   point.h
   ppm.h
   pprz_transport.c
   pprz_transport.h
   print.h
   radio_control.c
   radio_control.h
   rc_settings.c
   rc_settings.h
   setup_actuators.c
   spi.c
   spi.h
   srf08.c
   srf08.h
   sys_time.c
   sys_time.h
   test_adcs.c
   traffic_info.c
   traffic_info.h
   uart.h
   usb_serial.h
   wavecard.c
   wavecard.h
   xbee.c
   xbee.h

#ifndef I2C_H
#define I2C_H

#include "std.h"

#ifndef SITL
#include "i2c_hw.h"
#endif

extern void i2c_init(void);
extern void i2c_receive(uint8_t slave_addr, uint8_t len, volatile bool_t* finished);
extern void i2c_transmit(uint8_t slave_addr, uint8_t len, volatile bool_t* finished);

#define I2C_START        0x08
#define I2C_RESTART      0x10
#define I2C_MT_SLA_ACK   0x18
#define I2C_MT_SLA_NACK  0x20
#define I2C_MT_DATA_ACK  0x28
#define I2C_MR_SLA_ACK   0x40
#define I2C_MR_SLA_NACK  0x48
#define I2C_MR_DATA_ACK  0x50
#define I2C_MR_DATA_NACK 0x58


#define I2C_IDLE 0
#define I2C_BUSY 1

extern volatile uint8_t i2c_status;

#ifndef I2C_BUF_LEN
#define I2C_BUF_LEN 8
#endif

extern volatile uint8_t i2c_buf[I2C_BUF_LEN];
extern volatile uint8_t i2c_len;
extern volatile uint8_t i2c_index;
extern volatile uint8_t i2c_slave_addr;

extern volatile bool_t* i2c_finished;

#define I2cAutomaton(state) {						\
    switch (state) {							\
    case I2C_START:							\
    case I2C_RESTART:							\
      I2cSendByte(i2c_slave_addr);					\
      I2cClearStart();							\
      i2c_index = 0;							\
      break;								\
    case I2C_MR_DATA_ACK:						\
      if (i2c_index < i2c_len) {					\
	i2c_buf[i2c_index] = I2C_DATA_REG;				\
	i2c_index++;							\
	I2cReceive(i2c_index < i2c_len - 1);				\
      }									\
      else {								\
	/* error , we should have got NACK */				\
	I2cSendStop();							\
      }									\
      break;								\
    case I2C_MR_SLA_ACK: /* At least one char */			\
      /* Wait and reply with ACK or NACK */				\
      I2cReceive(i2c_index < i2c_len - 1);				\
      break;								\
    case I2C_MR_SLA_NACK:						\
    case I2C_MT_SLA_NACK:						\
      I2cSendStart();							\
      break;								\
    case I2C_MT_SLA_ACK:						\
    case I2C_MT_DATA_ACK:						\
      if (i2c_index < i2c_len) {					\
	I2cSendByte(i2c_buf[i2c_index]);				\
	i2c_index++;							\
      } else {								\
	I2cSendStop();							\
      }									\
      break;								\
    case I2C_MR_DATA_NACK:						\
      if (i2c_index < i2c_len) {					\
	i2c_buf[i2c_index] = I2C_DATA_REG;				\
      }									\
      I2cSendStop();							\
      break;								\
    default:								\
      I2cSendStop();							\
      /* LED_ON(2); FIXME log error */					\
    }									\
  }									\
   
#endif /* I2C_H */

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us