Modules

Functions which handle the TWI. More...

Modules

 Advanced informations
 

Lock Functions

uint8_t twi_lock (void)
 Locks the TWI for the user. More...
 
uint8_t twi_lockGet (void)
 Gets if the TWI is only locked for the user. More...
 
void twi_unlock (void)
 Unlocks the TWI by the user. More...
 
void twi_lockForce (void)
 Locks the TWI for the user. More...
 

Setup Functions

void twi_reset (void)
 Resets the TWI software. More...
 
uint8_t twi_errorGet (void)
 Returns the error value of the TWI. More...
 
void twi_read (uint8_t count)
 Starts the reception of data over the TWI. More...
 
void twi_write (void)
 Starts the transmission of data over the TWI. More...
 
void twi_wait (void)
 Waits until the TWI is not busy anymore. More...
 
void twi_sendTo (uint8_t address)
 Sets the slave address for the next transfer. More...
 

Transfer Functions

uint8_t twiinp (void)
 Gets one byte from the receive buffer of the twi. More...
 
uint8_t twiinp_ir (void)
 Gets one byte from the receive buffer of the twi. More...
 
uint8_t twistat (void)
 Gets the number of bytes in the receive buffer of the twi. More...
 
void twiout (uint8_t send_data)
 Puts one byte in the transmit buffer of the twi. More...
 
void twistr (const char *send_data)
 Puts a string located in the RAM in the transmit buffer of the twi. More...
 
void twistr_p (const char *send_data)
 Puts a string located in the flash in the transmit buffer of the twi. More...
 

Detailed Description

Functions which handle the TWI.

example

/*******************************************************************************
* examples/twi.c *
* ============== *
* *
* Version: 1.0.6 *
* Date : 21.11.17 *
* Author : Peter Weissig *
* *
* If you are changing this file, you may also consider to update *
* demos/diagnosticTool/diagnosticTool.c *
*******************************************************************************/
//*********************************<Included files>*****************************
#include <tucbot/tucbot.h>
//*********************************<Methods>************************************
void testTwi_lcdhex(uint8_t data);
void testTwi(void);
int main(void);
//*********************************[testTwi_lcdhex]*****************************
void testTwi_lcdhex(uint8_t data) {
uint8_t a;
a = data >> 4;
if (a < 10) {
lcdout(a + 48); // 48 == '0'
} else {
lcdout(a + 55); // 55 == 'A' - 10
}
a = data & 0x0F;
if (a < 10) {
lcdout(a + 48); // 48 == '0'
} else {
lcdout(a + 55); // 55 == 'A' - 10
}
}
//*********************************[testTwi]************************************
void testTwi(void) {
uint8_t value = 0x05;
lcdclr();
lcdstr_p(PSTR("TWI err=##"));
while (1) {
mdelay(100);
lcdxy(4,0);
lcdstr_p(PSTR( "write" ));
lcdxy(0,1);
lcdstr_p(PSTR("S60A2>##<03P "));
// This is just a short info what we are going to send:
// S ... Start-Bit
// 60 ... TWI device address (driving module - write mode)
// A2 ... packet descriptor (kTwiPdMD_SensLineEnable)
// >##< ... the data we will be sending (first byte of package)
// this will set the threshold value for the line sensors
// 03 ... second byte of this package
// this will enable the all line sensors
// P ... Stop-Bit
if (!twi_lock()) {
lcdxy(14,0);
lcdout('l');
lcdout('!');
continue;
}
lcdxy(6,1);
testTwi_lcdhex(value);
lcdxy(14,0);
testTwi_lcdhex(twi_errorGet());
value+= 0x10;
mdelay(400);
} else if (buttons_get(kButtonRight)) {
mdelay(100);
lcdxy(4,0);
lcdstr_p(PSTR( "read " ));
lcdxy(0,1);
lcdstr_p(PSTR("S60A2PS61>##<03P"));
// This is just a short info what we are going to send and
// what we are receiving:
// S ... Start-Bit
// 60 ... TWI device address (driving module - write mode)
// A2 ... packet descriptor (kTwiPdMD_SensLineEnable)
// P ... Stop-Bit (by only sending the packet descriptor
// we force the driving module to upload the package with
// the next read command)
// S ... Start-Bit
// 61 ... TWI device address (driving module - read mode)
// A2 ... packet descriptor (kTwiPdMD_SensLineEnable)
// >##< ... the data we want to read (first byte of package)
// this is the current threshold value of the line sensors
// 03 ... second byte of this package
// P ... Stop-Bit
if (!twi_lock()) {
lcdxy(14,0);
lcdout('l');
lcdout('!');
continue;
}
if (twistat()) {
lcdxy(10,1);
testTwi_lcdhex(twiinp());
}
lcdxy(14,0);
testTwi_lcdhex(twi_errorGet());
mdelay(400);
}
}
}
//*********************************[main]***************************************
int main (void) {
init_tucbot(0xFF);
testTwi();
return (0);
}

Function Documentation

◆ twi_errorGet()

uint8_t twi_errorGet ( void  )

Returns the error value of the TWI.

The return value corresponds to the last error.
If no error occured 0x00 will be returned.

Returns
8-bit unsigned integer
Examples
twi.c.

◆ twi_lock()

uint8_t twi_lock ( void  )

Locks the TWI for the user.

This function waits for the last internal transfer to be done.

If the internal transfer is not done within 100 milliseconds, this function will fail and return 0x00. Otherwise it returns 0xFF;

The locking and unlocking is done incrementally. The TWI must be unlocked as often as it was locked. Otherwise the library will not access the TWI again.

Returns
8-bit unsigned integer which is a boolean value
See also
twi_lock() and twi_unlock()
Examples
twi.c.

◆ twi_lockForce()

void twi_lockForce ( void  )

Locks the TWI for the user.

This function does not wait for the current internal transfer to be done.

The locking and unlocking is done incrementally. The TWI must be unlocked as often as it was locked. Otherwise the library will not access the TWI again.

See also
twi_lock() and twi_unlock()

◆ twi_lockGet()

uint8_t twi_lockGet ( void  )

Gets if the TWI is only locked for the user.

This function returns, if and how often the twi is locked for the user.

Note
If the library (still) locked the twi, this functions returns 0.
Returns
8-bit unsigned integer which represents the lock-count
See also
twi_lock() and twi_unlock()

◆ twi_read()

void twi_read ( uint8_t  count)

Starts the reception of data over the TWI.

This function initiates the reception of the data.

The count parameter sets the number of bytes to be read. Since the receive buffer is 16 bytes count must be between 0 and 15.

The corresponding slave is selected with the function twi_sendTo().

Parameters
count8-bit unsigned integer
See also
twiinp(), twiinp_ir(), twi_write() and twi_wait()
Examples
twi.c.

◆ twi_reset()

void twi_reset ( void  )

Resets the TWI software.

This function flushes the TWI buffers and clears the TWI error value.

See also
tucbot_twi_reset()
Examples
twi.c.

◆ twi_sendTo()

void twi_sendTo ( uint8_t  address)

Sets the slave address for the next transfer.

The lowest significant bit is masked out and automatically set by the twi hardware for each read operation.

Note
The address includes the read/write bit. The value of this bit will be ignored and it is automatically set depending on mode of access.
Parameters
address8-bit unsigned integer
See also
twi_read() and twi_write()

◆ twi_unlock()

void twi_unlock ( void  )

Unlocks the TWI by the user.

The locking and unlocking is done incrementally. The TWI must be unlocked as often as it was locked. Otherwise the library will not access the TWI again.

See also
twi_lock() and twi_lockForce()
Examples
twi.c.

◆ twi_wait()

void twi_wait ( void  )

Waits until the TWI is not busy anymore.

This function returns if no more data is going to be read or written and the last TWI commands have been executed. It also returns if an error occured.

Note
After kTwiWaitTimeOut milliseconds this function will be timed out.
See also
twi_read() and twi_write()
Examples
twi.c.

◆ twi_write()

void twi_write ( void  )

Starts the transmission of data over the TWI.

This function initiates the transmission of the data.

Since the transmit buffer is 16 bytes, no more than 16 bytes will be transmitted.

The corresponding slave is selected with the function twi_sendTo().

See also
twiout(), twi_read() and twi_wait()
Examples
twi.c.

◆ twiinp()

uint8_t twiinp ( void  )

Gets one byte from the receive buffer of the twi.

This function will return the first received byte in the receive buffer.

If the reveive buffer is empty and a read operation is still going on this function will wait until the next data byte is received. Otherwise 0x00 will be returned.

Returns
8-bit unsigned integer
See also
twiinp_ir(), twistat(), twi_write() and twiout()
Examples
twi.c.

◆ twiinp_ir()

uint8_t twiinp_ir ( void  )

Gets one byte from the receive buffer of the twi.

This function will return the first received byte in the receive buffer.

If the reveive buffer is empty this function will return 0x00.

Returns
8-bit unsigned integer
See also
twiinp(), twistat(), twi_write() and twiout()

◆ twiout()

void twiout ( uint8_t  send_data)

Puts one byte in the transmit buffer of the twi.

Parameters
send_data8-bit unsigned integer
See also
twi_write() and twiinp()
Examples
twi.c.

◆ twistat()

uint8_t twistat ( void  )

Gets the number of bytes in the receive buffer of the twi.

Returns
8-bit unsigned integer
See also
twiinp() and twiinp_ir()
Examples
twi.c.

◆ twistr()

void twistr ( const char *  send_data)

Puts a string located in the RAM in the transmit buffer of the twi.

This function loads the referenced string character by character and passes those to the twiout() function until a terminating \ 0-character is found.

Parameters
*send_data16-bit pointer to a character located in the RAM
See also
twistr_p() and twiout()

◆ twistr_p()

void twistr_p ( const char *  send_data)

Puts a string located in the flash in the transmit buffer of the twi.

This function loads the referenced string character by character and passes those to the twiout() function until a terminating \ 0-character is found.

Parameters
*send_data16-bit pointer to a character located in the flash
See also
twistr() and twiout()
leds_set
void leds_set(eLedMask bitmask, uint8_t bool)
Sets the selected leds on or off.
buttons_get
eButtonMask buttons_get(eButtonMask bitmask)
Returns the last stored value of the selected buttons.
kButtonMiddle
@ kButtonMiddle
bit mask (0x08): middle button
Definition: buttons.h:35
twi_reset
void twi_reset(void)
Resets the TWI software.
kLedOn
@ kLedOn
constant (0xFF): turn leds on
Definition: leds.h:63
kLedRight
@ kLedRight
bit mask (0x04): right led
Definition: leds.h:46
twi_wait
void twi_wait(void)
Waits until the TWI is not busy anymore.
kButtonRight
@ kButtonRight
bit mask (0x10): right button
Definition: buttons.h:37
twi_lock
uint8_t twi_lock(void)
Locks the TWI for the user.
buttons_request
void buttons_request(eRequestType request)
Requests new data from the buttons.
kRequestContinuous
@ kRequestContinuous
constant (3): request continuous updates
Definition: update.h:37
twiinp
uint8_t twiinp(void)
Gets one byte from the receive buffer of the twi.
kLedMiddle
@ kLedMiddle
bit mask (0x08): middle led
Definition: leds.h:44
kLedOff
@ kLedOff
constant (0x00): turn leds off
Definition: leds.h:61
lcdclr
void lcdclr(void)
Clears the display.
twi_read
void twi_read(uint8_t count)
Starts the reception of data over the TWI.
lcdxy
void lcdxy(uint8_t x, uint8_t y)
Moves the cursor to a postion on the display.
kTwiPdMD_SensLineEnable
@ kTwiPdMD_SensLineEnable
packet descriptor (0xA2): sMD_SensLineEnable
Definition: twi.h:856
twi_unlock
void twi_unlock(void)
Unlocks the TWI by the user.
twiout
void twiout(uint8_t send_data)
Puts one byte in the transmit buffer of the twi.
lcdstr_p
void lcdstr_p(const char *send_data)
Writes a string located in the flash to the display.
lcdout
void lcdout(uint8_t data)
Writes one byte to the display.
init_tucbot
void init_tucbot(uint8_t enable_interrupts)
Initiates the TUC-Bot.
mdelay
void mdelay(uint16_t mseconds)
Waits for the given time in milliseconds.
twi_errorGet
uint8_t twi_errorGet(void)
Returns the error value of the TWI.
twistat
uint8_t twistat(void)
Gets the number of bytes in the receive buffer of the twi.
twi_write
void twi_write(void)
Starts the transmission of data over the TWI.