twi_lib.h File Reference

#include "twi_drv.h"

Include dependency graph for twi_lib.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  TWI_message

Defines

#define TWI_ARBITRATION_LOST   4
#define TWI_BUS_ERROR   1
#define TWI_CONFIG   MSK_TWCR_TWEN
#define TWI_HOST_ADR_NACK   2
#define TWI_HOST_DATA_NACK   3
#define TWI_MASTER
#define TWI_NB_SLAVE_DATA   100
#define TWI_NOT_FREE   6
#define TWI_OK   0
 This constant is used as return value for the functions to.
#define TWI_READ   1
 This constant is used as parameter value for the functions twi_send_message_xxx to select read operation.
#define TWI_SLAVE
#define TWI_UNKNOWN   5
#define TWI_WRITE   0
 This constant is used as parameter value for the functions twi_send_message_xxx to select write operation.

Typedefs

typedef unsigned int Length_TWI_frame

Functions

Uint16 twi_getchar (Byte addr)
 * This function can be used to receive an byte from a slave in polling fashion.
void twi_lib_init (void)
 This function allows to init the TWI controller.
Byte twi_putchar (Byte addr, Byte b)
 This function can be used to send an byte to a slave in polling fashion.
unsigned char twi_send_message_interrupt (unsigned char slave_addr, bit rw, Length_TWI_frame nbytes, Uchar *info)
 This function sends TWI message to a slave.
unsigned char twi_send_message_polling (unsigned char slave_addr, bit rw, Length_TWI_frame nbytes, Uchar *info)
 This function sends TWI message to a slave.
void twi_slave_interrupt (void)
 This function can be called to be able to answer another master request in interruption fashion.
unsigned char twi_slave_polling (void)
 This function can be called to be able to answer another master request in polling fashion.

Variables

volatile bit twi_busy
volatile unsigned char twi_err
volatile TWI_message xdata twi_message
volatile unsigned char xdata twi_slave_data [TWI_NB_SLAVE_DATA]


Define Documentation

#define TWI_ARBITRATION_LOST   4

Definition at line 55 of file twi_lib.h.

Referenced by twi_decode_status().

#define TWI_BUS_ERROR   1

Definition at line 52 of file twi_lib.h.

Referenced by twi_decode_status().

#define TWI_CONFIG   MSK_TWCR_TWEN

Definition at line 72 of file twi_lib.h.

Referenced by twi_lib_init().

#define TWI_HOST_ADR_NACK   2

Definition at line 53 of file twi_lib.h.

Referenced by twi_decode_status().

#define TWI_HOST_DATA_NACK   3

Definition at line 54 of file twi_lib.h.

#define TWI_MASTER

Definition at line 23 of file twi_lib.h.

#define TWI_NB_SLAVE_DATA   100

Definition at line 22 of file twi_lib.h.

Referenced by twi_decode_status().

#define TWI_NOT_FREE   6

Definition at line 57 of file twi_lib.h.

Referenced by twi_send_message_interrupt(), and twi_send_message_polling().

#define TWI_OK   0

This constant is used as return value for the functions to.

Definition at line 51 of file twi_lib.h.

Referenced by twi_decode_status(), twi_getchar(), twi_putchar(), twi_send_message_interrupt(), and twi_send_message_polling().

#define TWI_READ   1

This constant is used as parameter value for the functions twi_send_message_xxx to select read operation.

Definition at line 64 of file twi_lib.h.

Referenced by twi_decode_status().

#define TWI_SLAVE

Definition at line 24 of file twi_lib.h.

#define TWI_UNKNOWN   5

Definition at line 56 of file twi_lib.h.

Referenced by twi_decode_status(), twi_getchar(), and twi_putchar().

#define TWI_WRITE   0

This constant is used as parameter value for the functions twi_send_message_xxx to select write operation.

Definition at line 69 of file twi_lib.h.


Typedef Documentation

typedef unsigned int Length_TWI_frame

Definition at line 27 of file twi_lib.h.


Function Documentation

Uint16 twi_getchar ( Byte  addr  ) 

* This function can be used to receive an byte from a slave in polling fashion.

Parameters:
addr,: slave to address with this message.
Returns:
byte received

Definition at line 576 of file twi_lib.c.

References Twi_clear_aa, Twi_clear_si, Twi_clear_start, Twi_get_data, Twi_get_status, TWI_OK, Twi_set_aa, Twi_set_data, Twi_set_start, Twi_set_stop, TWI_UNKNOWN, Twi_wait_event, and Twi_wait_hw_stop.

00577 {
00578   Union16 data ret;
00579 
00580   ret.b[0] = TWI_UNKNOWN;
00581   ret.b[1] = 0;
00582   Twi_wait_hw_stop();
00583   Twi_set_start();
00584   Twi_wait_event();
00585   if (Twi_get_status() == 0x08)     //start sent
00586   {
00587     Twi_clear_start();
00588     Twi_set_data((addr<<1)+1);   //ADR+R
00589     Twi_set_aa(); //Also clear TWINT
00590     Twi_wait_event();
00591     if (Twi_get_status() == 0x40)   //ADR+R sent
00592     {
00593       Twi_clear_aa();    //only one byte t receive: NACK after
00594       Twi_clear_si();
00595       Twi_wait_event();
00596       if (Twi_get_status() == 0x58) //data received
00597       {
00598         ret.b[1] = Twi_get_data();
00599         Twi_set_stop(); //Also clear TWINT
00600         ret.b[0] = TWI_OK;
00601       } 
00602     }
00603    else
00604    {
00605    Twi_clear_start();
00606    Twi_set_stop(); //Also clear TWINT
00607    }
00608   }
00609   return ret.w;
00610 }

void twi_lib_init ( void   ) 

This function allows to init the TWI controller.

Definition at line 76 of file twi_lib.c.

References FOSC, TWI_CONFIG, Twi_init_hw, and Twi_set_baudrate.

00077 {
00078   Twi_set_baudrate(FOSC/TWI_BAUDRATE);
00079   Twi_init_hw(TWI_CONFIG);
00080 }

Byte twi_putchar ( Byte  addr,
Byte  b 
)

This function can be used to send an byte to a slave in polling fashion.

Parameters:
addr,: slave to address with this message.
byte,: byte to transmit.
Returns:
TWI error code state:
  • TWI_OK
  • TWI_BUS_ERROR
  • TWI_HOST_ADR_NACK
  • TWI_HOST_DATA_NACK
  • TWI_ARBITRATION_LOST
  • TWI_UNKNOWN
  • TWI_NOT_FREE

Definition at line 528 of file twi_lib.c.

References Twi_clear_si, Twi_clear_start, Twi_get_status, TWI_OK, Twi_set_aa, Twi_set_data, Twi_set_start, Twi_set_stop, TWI_UNKNOWN, Twi_wait_event, and Twi_wait_hw_stop.

00529 {
00530   Twi_wait_hw_stop();
00531   Twi_set_start();
00532   Twi_wait_event();
00533   if (Twi_get_status() == 0x08)     //start sent
00534   {
00535     Twi_clear_start();
00536     Twi_set_data(addr<<1);
00537     Twi_set_aa(); //Also clear TWINT
00538     Twi_wait_event();
00539     if (Twi_get_status() == 0x18)   //ADR+W sent
00540     {
00541       Twi_set_data(b); 
00542       Twi_clear_si();
00543       Twi_wait_event();
00544       if (Twi_get_status() == 0x28) //data sent
00545       {
00546         Twi_set_stop();
00547         Twi_clear_si();
00548         return TWI_OK;
00549       } 
00550     }
00551     else  
00552     {
00553       Twi_clear_start();
00554       Twi_set_stop(); //Also clear TWINT
00555     }
00556   }
00557   Twi_clear_start();
00558   Twi_clear_si();
00559   return TWI_UNKNOWN;
00560 }

unsigned char twi_send_message_interrupt ( unsigned char  slave_addr,
bit  rw,
Length_TWI_frame  nbytes,
Uchar info 
)

This function sends TWI message to a slave.


This transmition is managed in interruption fashion.

Parameters:
slave_addr,: slave to address with this message.
rw,: This field allows to indicate a read or write message
  • TWI_READ
  • TWI_WRITE
nb_byte,: This field gives the byte number to read or write.
*info,: pointer to the data to be processed.
Returns:
TWI error code state:
  • TWI_OK
  • TWI_BUS_ERROR
  • TWI_HOST_ADR_NACK
  • TWI_HOST_DATA_NACK
  • TWI_ARBITRATION_LOST
  • TWI_UNKNOWN
  • TWI_NOT_FREE
Precondition:
this function is available only if TWI_MASTER is defined in config.h and DO_NOT_USE_TWI_INTERRUPT is not defined.

Definition at line 437 of file twi_lib.c.

References TWI_message::address, TWI_message::buf, Enable_twi_interrupt, TWI_message::nbbytes, TWI_message::rw, twi_busy, twi_err, twi_message, twi_nb_transmited, TWI_NOT_FREE, TWI_OK, Twi_set_start, Twi_set_stop, and Twi_wait_hw_stop.

00438 {
00439   twi_message.address = slave_addr;
00440   twi_message.rw = rw;
00441   twi_message.nbbytes = nbytes;
00442   twi_message.buf = info;
00443   Twi_wait_hw_stop();
00444   twi_nb_transmited=0;
00445   if (!twi_busy)
00446   {
00447     twi_err = TWI_OK;
00448     twi_busy =1;
00449     Enable_twi_interrupt();
00450     Twi_set_start(); 
00451     return twi_err;
00452   }
00453   Twi_set_stop();
00454   return TWI_NOT_FREE;
00455 }

unsigned char twi_send_message_polling ( unsigned char  slave_addr,
bit  rw,
Length_TWI_frame  nbytes,
Uchar info 
)

This function sends TWI message to a slave.


This transmition is managed in polling fashion.

Parameters:
slave_addr,: slave to address with this message.
rw,: This field allows to indicate a read or write message
  • TWI_READ
  • TWI_WRITE
nb_byte,: This field gives the byte number to read or write.
*info,: pointer to the data to be processed.
Returns:
TWI error code state:
  • TWI_OK
  • TWI_BUS_ERROR
  • TWI_HOST_ADR_NACK
  • TWI_HOST_DATA_NACK
  • TWI_ARBITRATION_LOST
  • TWI_UNKNOWN
  • TWI_NOT_FREE
Precondition:
this function is available only if TWI_MASTER is define in config.h

Definition at line 385 of file twi_lib.c.

References TWI_message::address, TWI_message::buf, Disable_twi_interrupt, TWI_message::nbbytes, TWI_message::rw, twi_busy, twi_decode_status(), twi_err, twi_message, twi_nb_transmited, TWI_NOT_FREE, TWI_OK, Twi_set_start, Twi_set_stop, Twi_wait_event, and Twi_wait_hw_stop.

00386 {
00387   twi_message.address = slave_addr;
00388   twi_message.rw = rw;
00389   twi_message.nbbytes = nbbytes;
00390   twi_message.buf = info;
00391   Twi_wait_hw_stop();
00392   Disable_twi_interrupt(); //FIXME
00393   twi_nb_transmited=0;
00394   if (!twi_busy)
00395   {
00396     twi_busy =1;
00397     twi_err = TWI_OK;
00398     Twi_set_start();
00399     while (twi_busy)
00400         {
00401         Twi_wait_event();
00402         twi_decode_status();
00403 //        Twi_clear_si(); //REMOVE FROM MAIN LOOP, ADDED IN ALL NECESARY CASE (See JSB /RLE)
00404         }
00405    // Twi_set_stop(); RLE
00406     return twi_err;
00407   }
00408   Twi_set_stop();
00409   return TWI_NOT_FREE;
00410 }

Here is the call graph for this function:

void twi_slave_interrupt ( void   ) 

This function can be called to be able to answer another master request in interruption fashion.

Returns:
TWI error code state:
  • TWI_OK
  • TWI_BUS_ERROR
  • TWI_HOST_ADR_NACK
  • TWI_HOST_DATA_NACK
  • TWI_ARBITRATION_LOST
  • TWI_UNKNOWN
  • TWI_NOT_FREE
Precondition:
this function is available only if TWI_SLAVE is defined in config.h

Definition at line 507 of file twi_lib.c.

References Enable_twi_interrupt.

00508 {
00509   Enable_interrupt();
00510   Enable_twi_interrupt();
00511 }

unsigned char twi_slave_polling ( void   ) 

This function can be called to be able to answer another master request in polling fashion.

Returns:
TWI error code state:
  • TWI_OK
  • TWI_BUS_ERROR
  • TWI_HOST_ADR_NACK
  • TWI_HOST_DATA_NACK
  • TWI_ARBITRATION_LOST
  • TWI_UNKNOWN
  • TWI_NOT_FREE
Precondition:
this function is available only if TWI_SLAVE is defined in config.h

Definition at line 477 of file twi_lib.c.

References twi_busy, Twi_clear_si, twi_decode_status(), twi_err, and Twi_wait_event.

00478 {
00479   Twi_wait_event();
00480   twi_decode_status();
00481   Twi_clear_si();
00482   while (twi_busy)
00483   {
00484     Twi_wait_event();
00485     twi_decode_status();
00486     // Twi_clear_si();//REMOVE FROM MAIN LOOP, ADDED IN ALL NECESARY CASE (See JSB /RLE)
00487   }
00488   return twi_err;
00489 }

Here is the call graph for this function:


Variable Documentation

volatile bit twi_busy

Definition at line 22 of file twi_lib.c.

Referenced by twi_decode_status(), twi_send_message_interrupt(), twi_send_message_polling(), and twi_slave_polling().

volatile unsigned char twi_err

Definition at line 23 of file twi_lib.c.

Referenced by twi_decode_status(), twi_send_message_interrupt(), twi_send_message_polling(), and twi_slave_polling().

volatile TWI_message xdata twi_message

Definition at line 41 of file twi_lib.c.

Referenced by twi_decode_status(), twi_send_message_interrupt(), and twi_send_message_polling().

volatile unsigned char xdata twi_slave_data[TWI_NB_SLAVE_DATA]

Definition at line 33 of file twi_lib.c.

Referenced by twi_decode_status().


Generated on Mon Dec 1 11:12:22 2008 for AVR498 : Atmel BLDC control on ATAVRMC301 with ATtiny861 by  doxygen 1.5.4