File : USI_TWI_Slave.h Compiler : IAR EWAAVR 4.11A Revision :
Support mail : avr@atmel.com
Supported devices : All device with USI module can be used. The example is written for the ATmega169, ATtiny26 & ATtiny2313
AppNote : AVR312 - Using the USI module as a TWI slave
Description : Header file for USI_TWI driver
Definition in file USI_TWI_Slave.h.
Go to the source code of this file.
Defines | |
#define | SET_USI_TO_READ_ACK() |
#define | SET_USI_TO_READ_DATA() |
#define | SET_USI_TO_SEND_ACK() |
Device dependent defines. | |
#define | SET_USI_TO_SEND_DATA() |
#define | SET_USI_TO_TWI_START_CONDITION_MODE() |
#define | TWI_RX_BUFFER_MASK ( TWI_RX_BUFFER_SIZE - 1 ) |
#define | TWI_RX_BUFFER_SIZE (16) |
#define | TWI_TX_BUFFER_MASK ( TWI_TX_BUFFER_SIZE - 1 ) |
#define | TWI_TX_BUFFER_SIZE (16) |
#define | USI_SLAVE_CHECK_ADDRESS (0x00) |
#define | USI_SLAVE_CHECK_REPLY_FROM_SEND_DATA (0x03) |
#define | USI_SLAVE_GET_DATA_AND_SEND_ACK (0x05) |
#define | USI_SLAVE_REQUEST_DATA (0x04) |
#define | USI_SLAVE_REQUEST_REPLY_FROM_SEND_DATA (0x02) |
#define | USI_SLAVE_SEND_DATA (0x01) |
Typedefs | |
typedef unsigned char | uint8_t |
Functions | |
void | Timer_Init (void) |
unsigned char | USI_TWI_Data_In_Receive_Buffer (void) |
Check if there is data in the receive buffer. | |
unsigned char | USI_TWI_Receive_Byte (void) |
Returns a byte from the receive buffer. Waits if buffer is empty. | |
void | USI_TWI_Slave_Initialise (unsigned char) |
Prototypes. | |
void | USI_TWI_Transmit_Byte (unsigned char byte_data) |
Puts data in the transmission buffer, Waits if buffer is full. |
#define SET_USI_TO_READ_ACK | ( | ) |
Value:
{ \ DDR_USI &= ~(1<<PORT_USI_SDA); /* Set SDA as intput */ \ USIDR = 0; /* Prepare ACK */ \ USISR = (0<<USI_START_COND_INT)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| /* Clear all flags, except Start Cond */ \ (0x0E<<USICNT0); /* set USI counter to shift 1 bit. */ \ }
Definition at line 160 of file USI_TWI_Slave.h.
Referenced by USI_Counter_Overflow_ISR().
#define SET_USI_TO_READ_DATA | ( | ) |
Value:
{ \ DDR_USI &= ~(1<<PORT_USI_SDA); /* Set SDA as input */ \ USISR = (0<<USI_START_COND_INT)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| /* Clear all flags, except Start Cond */ \ (0x0<<USICNT0); /* set USI to shift out 8 bits */ \ }
Definition at line 185 of file USI_TWI_Slave.h.
Referenced by USI_Counter_Overflow_ISR().
#define SET_USI_TO_SEND_ACK | ( | ) |
Value:
{ \ USIDR = 0; /* Prepare ACK */ \ DDR_USI |= (1<<PORT_USI_SDA); /* Set SDA as output */ \ USISR = (0<<USI_START_COND_INT)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| /* Clear all flags, except Start Cond */ \ (0x0E<<USICNT0); /* set USI counter to shift 1 bit. */ \ }
Device dependent defines Functions implemented as macros
Definition at line 152 of file USI_TWI_Slave.h.
Referenced by USI_Counter_Overflow_ISR().
#define SET_USI_TO_SEND_DATA | ( | ) |
Value:
{ \ DDR_USI |= (1<<PORT_USI_SDA); /* Set SDA as output */ \ USISR = (0<<USI_START_COND_INT)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| /* Clear all flags, except Start Cond */ \ (0x0<<USICNT0); /* set USI to shift out 8 bits */ \ }
Definition at line 178 of file USI_TWI_Slave.h.
Referenced by USI_Counter_Overflow_ISR().
#define SET_USI_TO_TWI_START_CONDITION_MODE | ( | ) |
Value:
{ \ USICR = (1<<USISIE)|(0<<USIOIE)| /* Enable Start Condition Interrupt. Disable Overflow Interrupt.*/ \ (1<<USIWM1)|(0<<USIWM0)| /* Set USI in Two-wire mode. No USI Counter overflow hold. */ \ (1<<USICS1)|(0<<USICS0)|(0<<USICLK)| /* Shift Register Clock Source = External, positive edge */ \ (0<<USITC); \ USISR = (0<<USI_START_COND_INT)|(1<<USIOIF)|(1<<USIPF)|(1<<USIDC)| /* Clear all flags, except Start Cond */ \ (0x0<<USICNT0); \ }
Definition at line 168 of file USI_TWI_Slave.h.
Referenced by USI_Counter_Overflow_ISR().
#define TWI_RX_BUFFER_MASK ( TWI_RX_BUFFER_SIZE - 1 ) |
Definition at line 42 of file USI_TWI_Slave.h.
Referenced by USI_Counter_Overflow_ISR(), and USI_TWI_Receive_Byte().
#define TWI_RX_BUFFER_SIZE (16) |
Definition at line 41 of file USI_TWI_Slave.h.
#define TWI_TX_BUFFER_MASK ( TWI_TX_BUFFER_SIZE - 1 ) |
Definition at line 51 of file USI_TWI_Slave.h.
Referenced by USI_Counter_Overflow_ISR(), and USI_TWI_Transmit_Byte().
#define TWI_TX_BUFFER_SIZE (16) |
Definition at line 50 of file USI_TWI_Slave.h.
#define USI_SLAVE_CHECK_ADDRESS (0x00) |
Definition at line 59 of file USI_TWI_Slave.h.
Referenced by USI_Counter_Overflow_ISR(), and USI_Start_Condition_ISR().
#define USI_SLAVE_CHECK_REPLY_FROM_SEND_DATA (0x03) |
#define USI_SLAVE_GET_DATA_AND_SEND_ACK (0x05) |
#define USI_SLAVE_REQUEST_DATA (0x04) |
#define USI_SLAVE_REQUEST_REPLY_FROM_SEND_DATA (0x02) |
#define USI_SLAVE_SEND_DATA (0x01) |
typedef unsigned char uint8_t |
Definition at line 34 of file USI_TWI_Slave.h.
void Timer_Init | ( | void | ) |
unsigned char USI_TWI_Data_In_Receive_Buffer | ( | void | ) |
Check if there is data in the receive buffer.
Definition at line 104 of file USI_TWI_Slave.c.
References TWI_RxHead, and TWI_RxTail.
00105 { 00106 unsigned char tmpRxTail; // Temporary variable to store volatile 00107 tmpRxTail = TWI_RxTail; // Not necessary, but prevents warnings 00108 return ( TWI_RxHead != tmpRxTail ); // Return 0 (FALSE) if the receive buffer is empty. 00109 }
unsigned char USI_TWI_Receive_Byte | ( | void | ) |
Returns a byte from the receive buffer. Waits if buffer is empty.
Definition at line 91 of file USI_TWI_Slave.c.
References TWI_RX_BUFFER_MASK, TWI_RxBuf, TWI_RxHead, and TWI_RxTail.
00092 { 00093 unsigned char tmptail; 00094 unsigned char tmpRxTail; // Temporary variable to store volatile 00095 tmpRxTail = TWI_RxTail; // Not necessary, but prevents warnings 00096 while ( TWI_RxHead == tmpRxTail ); 00097 tmptail = ( TWI_RxTail + 1 ) & TWI_RX_BUFFER_MASK; // Calculate buffer index 00098 TWI_RxTail = tmptail; // Store new index 00099 return TWI_RxBuf[tmptail]; // Return data from the buffer. 00100 }
void USI_TWI_Slave_Initialise | ( | unsigned char | TWI_ownAddress | ) |
Prototypes.
Definition at line 58 of file USI_TWI_Slave.c.
References Flush_TWI_Buffers(), and TWI_slaveAddress.
00059 { 00060 Flush_TWI_Buffers(); 00061 00062 TWI_slaveAddress = TWI_ownAddress; 00063 00064 PORT_USI |= (1<<PORT_USI_SCL); // Set SCL high 00065 PORT_USI |= (1<<PORT_USI_SDA); // Set SDA high 00066 DDR_USI |= (1<<PORT_USI_SCL); // Set SCL as output 00067 DDR_USI &= ~(1<<PORT_USI_SDA); // Set SDA as input 00068 USICR = (1<<USISIE)|(0<<USIOIE)| // Enable Start Condition Interrupt. Disable Overflow Interrupt. 00069 (1<<USIWM1)|(0<<USIWM0)| // Set USI in Two-wire mode. No USI Counter overflow prior 00070 // to first Start Condition (potentail failure) 00071 (1<<USICS1)|(0<<USICS0)|(0<<USICLK)| // Shift Register Clock Source = External, positive edge 00072 (0<<USITC); 00073 USISR = 0xF0; // Clear all flags and reset overflow counter 00074 }
void USI_TWI_Transmit_Byte | ( | unsigned char | byte_data | ) |
Puts data in the transmission buffer, Waits if buffer is full.
Definition at line 79 of file USI_TWI_Slave.c.
References TWI_TX_BUFFER_MASK, TWI_TxBuf, TWI_TxHead, and TWI_TxTail.
00080 { 00081 unsigned char tmphead; 00082 00083 tmphead = ( TWI_TxHead + 1 ) & TWI_TX_BUFFER_MASK; // Calculate buffer index. 00084 while ( tmphead == TWI_TxTail ); // Wait for free space in buffer. 00085 TWI_TxBuf[tmphead] = byte_data; // Store data in buffer. 00086 TWI_TxHead = tmphead; // Store new index. 00087 }