Zigbee Module Examples

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Macros
hal_bit_bang_i2c.c File Reference

Software I2C interface. Contains individual methods (start/stop/in/out) as well as full methods (read, write) etc. More...

`#include "hal_launchpad.h"`
`#include "../Common/printf.h"`
`#include "hal_bit_bang_i2c.h"`

## Macros
#define | READ_BIT 0x01
#define | BIT_TIME_5_US 4
#define | BIT_TIME_2_US 2
#define | DELAY_FULL() (__delay_cycles(TICKS_PER_US*BIT_TIME_5_US))
#define | DELAY_PART() (__delay_cycles(TICKS_PER_US*BIT_TIME_2_US))
#define | DELAY_HALF() (__delay_cycles(TICKS_PER_US))
#define | I2C_ADDRESS_NOT_DEFINED 0xFF
#define | I2C_INTERFACE_NOT_INTIALIZED() (address == I2C_ADDRESS_NOT_DEFINED)
#define | IS_VALID_I2C_ADDRESS(addr) ((addr > 0x07) && (addr < 0x78))
#define | SET_SDA_HIGH() (I2C_SDA_DIR_PORT &= ~I2C_MASTER_SDA)
#define | SET_SCL_HIGH() (I2C_SCL_DIR_PORT &= ~I2C_MASTER_SCL)
#define | SET_SDA_LOW() (I2C_SDA_DIR_PORT |= I2C_MASTER_SDA)
#define | SET_SCL_LOW() (I2C_SCL_DIR_PORT |= I2C_MASTER_SCL)
#define | READ_SDA() ( I2C_SDA_IN_PORT & I2C_MASTER_SDA )
#define | CONFIGURE_SDA_OUTPUT_LOW() (I2C_SDA_OUT_PORT &= ~I2C_MASTER_SDA)
#define | CONFIGURE_SCL_OUTPUT_LOW() (I2C_SCL_OUT_PORT &= ~I2C_MASTER_SCL)
uint8_t | address = I2C_ADDRESS_NOT_DEFINED
int8_t | i2cInit (uint8_t i2cAddress)
void | displayI2cError (unsigned long i2cErr)
void | i2cStart (void)
uint8_t | i2cOut (uint8_t data)
void | i2cStartTx ()
void | i2cStartRx ()
uint8_t | i2cIn ()
void | i2cStop (void)
uint8_t | i2cRead (uint8_t *bytes, uint8_t numBytes)
uint8_t | i2cReadOneByte (uint8_t *value)
uint8_t | i2cAddressTest (uint8_t addressToTest)
void | i2cAddressSearch (void)
uint8_t | i2cWrite (uint8_t *bytes, uint8_t numBytes)

## Detailed Description

Software I2C interface. Contains individual methods (start/stop/in/out) as well as full methods (read, write) etc.

Uses the Hardware Abstraction Layer (hal).

I2C Notes:

- I2C direction bit is the last bit of the address byte, right before ACK bit. ZERO indicates a write to the slave and ONE indicates a read from the slave. After the direction bit and ACKNOWLEDGE, master will send the data to the slave or read data from the slave depending on the direction. The data transfer will be terminated when master sets STOP condition on the bus.
- According to the I2C standard, when communicating on bus the master never outputs a logic high. It only pulls the bus down. Logic high is set by allowing external pull-ups to pull the bus up.

Rev:
  1528

Author:
  dsmith

Date:
  2012-08-30 15:58:08 -0700 (Thu, 30 Aug 2012)

# Support

Please refer to the wiki at www.anaren.com/air-wiki-zigbee for more information. Additional support is available via email at the following addresses:

- Questions on how to use the product: AIR@a.nosp@m.nare.nosp@m.n.com
- Feature requests, comments, and improvements: featu.nosp@m.rere.nosp@m.quest.nosp@m.s@te.nosp@m.slaco.nosp@m.ntro.nosp@m.ls.co.nosp@m.m
- Consulting engagements: sales.nosp@m.@tes.nosp@m.lacon.nosp@m.trol.nosp@m.s.com

# License

Copyright (c) 2012 Tesla Controls. All rights reserved. This Software may only be used with an Anaren A2530E24AZ1, A2530E24CZ1, A2530R24AZ1, or A2530R24CZ1 module. Redistribution and use in source and binary forms, with or without modification, are subject to the Software License Agreement in the file "anaren_eula.txt"

YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL ANAREN MICROWAVE OR TESLA CONTROLS BE LIABLE OR OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSE INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.

## Macro Definition Documentation

#define READ_SDA | ( |  | ) | ( I2C_SDA_IN_PORT & I2C_MASTER_SDA )

Read the state of the SDA line

## Function Documentation

void displayI2cError | ( | unsigned long | i2cErr | ) |

Displays the i2c error(s) based on the contents of bitfield i2cErr.

void i2cAddressSearch | ( | void |  | ) |

    Searches the entire i2c address space (0x00 through 0x7F) for devices which return an ACK.

Displays to console which addresses have ACK'd.

Precondition:
  I2C bus has been initialized using i2cInit(). Address used does not matter.

uint8_t i2cAddressTest | ( | uint8_t | addressToTest | ) |

    Writes to the specified address, looks to see if it was ACK'd.

Returns:
  1 if that address responded to an ACK, else 0

Parameters:

  addressToTest | the address to check

uint8_t i2cIn | ( |  | ) |

    Reads one byte via I2C

Precondition:
  start has been sent, or this is not the first byte

Returns:
  the value read from the I2C bus

int8_t i2cInit | ( | uint8_t | i2cAddress | ) |

    Initialization code. This must be called before any other method. Stores the I2C address we wish

to talk to.

Precondition:
  SDA and SCL were configured as GPIOs.

Parameters:

  address | which I2C address we will be communicating with. Must be a valid I2C address, between 0x08 and 0x77, inclusive. For example, a TI TMP006 has an address of 0x40; the TAOS TCS3414 has an address of 0x39.

Note:
  some manufacturers give a "read address" and "write address". To get the true I2C address, discard the LSB and shift this one bit to the right.

Returns:
  0 if success, else -1 if invalid i2c Address.

uint8_t i2cOut | ( | uint8_t | data | ) |

    Transmits one byte via I2C

Precondition:
  start has been sent, or this is not the first byte
  both SCL & SDA are low

Returns:
  true if ack was received

uint8_t i2cRead | ( | uint8_t * | bytes,
|  | uint8_t | numBytes
| ) |  |

    Reads the specified number of bytes from the I2C port.

Precondition:
  bytes is large enough to hold count number of bytes.

Note:
  Assumes the IC2_MASTER_SCL is low
  Order: Start, ADDRESS, first byte ... last byte, NAK,stop

Parameters:

  bytes | where the received bytes will be written to
  numBytes | the number of bytes to be read

Postcondition:
  bytes contains the bytes read from the I2C interface

uint8_t i2cReadOneByte | ( | uint8_t * | value | ) |

    Special method for generating a NAK after only one byte is read. Use with caution.

Order: Start, ADDRESS, byte, NAK, stop.

Note:
  this can be used in place of legacy method i2cReadCurrentAddress().

Parameters:

  value | where to read the byte into

Returns:
  0 if success, else an error code

void i2cStart | ( | void |  | ) |

    Sends an I2C Start condition. A HIGH to LOW transition on the SDA line while SCL is HIGH

indicates a START condition.

Note:
  Only a master can establish START condition on the bus. The slaves should detect the START condition and be ready to receive the ADDRESS and DATA. The bus is considered to be busy from the point a master establishes START. The bus is released by using STOP condition.

See also:
  http://www.i2cbus.com/theprotocol/start.html

Postcondition:
  both SDA & SCL are low

void i2cStartRx | ( |  | ) |

    Sends an I2C Start condition and sends the address of the device we want to talk to, for reading.

For reading, the LSB is set to 1.

Precondition:
  i2c address was configured. This method does not check.

See also:
  i2cStart()

void i2cStartTx | ( |  | ) |

    Sends an I2C Start condition and sends the address of the device we want to talk to, for writing.

For writing, the LSB is set to 0.

Precondition:
  i2c address was configured. This method does not check.

See also:
  i2cStart()

void i2cStop | ( | void |  | ) |

    Sends an I2C Stop condition. A LOW to HIGH transition on the SDA line while SCL is HIGH

indicates a STOP condition.

Note:
  Master devices establish STOP condition on the bus to let the slaves know that the message is finished. The bus is considered to be busy until master sets STOP condition. After the STOP, the bus is free and any other masters can claim the bus for communication adhering to the arbitration process.

Precondition:
  SDA is low

uint8_t i2cWrite | ( | uint8_t * | bytes,
|  | uint8_t | numBytes
| ) |  |

    Writes the specified number of bytes out the I2C port.

Note:
  Order: Start, ADDRESS, first byte ... last byte, stop

Precondition:
  bytes contains the bytes to be written

Parameters:

  bytes | where the received bytes will be written to
  numBytes | the number of bytes to be read

## Variable Documentation

uint8_t address = I2C_ADDRESS_NOT_DEFINED

Stores the address of the slave device we wish to talk to. Must be configured before use.