#include "twi_drv.h"
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 TWI_ARBITRATION_LOST 4 |
#define TWI_BUS_ERROR 1 |
#define TWI_CONFIG MSK_TWCR_TWEN |
#define TWI_HOST_ADR_NACK 2 |
#define TWI_NB_SLAVE_DATA 100 |
#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_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 |
typedef unsigned int Length_TWI_frame |
* This function can be used to receive an byte from a slave in polling fashion.
addr,: | slave to address with this message. |
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 }
This function can be used to send an byte to a slave in polling fashion.
addr,: | slave to address with this message. | |
byte,: | byte to transmit. |
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.
slave_addr,: | slave to address with this message. | |
rw,: | This field allows to indicate a read or write message
| |
nb_byte,: | This field gives the byte number to read or write. | |
*info,: | pointer to the data to be processed. |
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.
slave_addr,: | slave to address with this message. | |
rw,: | This field allows to indicate a read or write message
| |
nb_byte,: | This field gives the byte number to read or write. | |
*info,: | pointer to the data to be processed. |
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 }
void twi_slave_interrupt | ( | void | ) |
This function can be called to be able to answer another master request in interruption fashion.
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.
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 }
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] |