This driver is the implementation of TWI module of megaRF device as master.
Master initiates and terminates a data transmission. The transmission is initiated when the Master issues a START condition on the bus, and it is terminated when the Master issues a STOP condition. This uses two bi-directional bus lines(PD0 and PD1) one for clock (SCL) and one for data (SDA)
Configuration is done in the configuration file : conf_twi.h
Configuration defines:
The twi module should be configured as master. The SCL period is controlled by settings in the TWI Bit Rate Register (TWBR) and the Prescaler bits in the TWI Status Register (TWSR). Enabling of TWI clock source and interrupt is done by calling the function
The include file is needed:
TWI transmission is to transmit a START condition. This is done by writing a specific value into TWCR, instructing the TWI hardware to transmit START condition and then transmit the data. Similarly in receive mode This is done by calling:
After initiating the transmission or reception the completion status will be indicated to the application code through the following function
Functions | |
static void | twi_master_addr_ack (void) |
Notification that address byte was written to the TWI and need to send ACK or NACK. More... | |
static void | twi_master_bus_reset (void) |
Reset TWI bus. More... | |
static void | twi_master_data_write (void) |
Sending data to twi bus. More... | |
status_code_t | twi_master_get_status (void) |
returns the status of TWI bus More... | |
status_code_t | twi_master_init (volatile void *twi, twi_master_options_t *opt) |
Inits TWI module as master. More... | |
static void | twi_master_internal_addr_write (void) |
Sending internal device address to twi bus. More... | |
status_code_t | twi_master_read (volatile void *twi, const twi_package_t *package) |
Reads the series of bytes from the TWI bus. More... | |
static void | twi_master_read_done (uint8_t data) |
Notification that byte was read by the TWI. More... | |
static void | twi_master_read_last_byte (uint8_t data) |
Notification that last byte was read from the TWI. More... | |
static void | twi_master_start (void) |
Notification about the start condition was sent. More... | |
status_code_t | twi_master_write (volatile void *twi, const twi_package_t *package) |
Perform a TWI master write transfer. More... | |
static void | twi_master_write_done (void) |
Notification that byte was written to the TWI. More... | |
|
static |
Notification that address byte was written to the TWI and need to send ACK or NACK.
none. |
References ERR_PROTOCOL, master_transfer, twi_master_bus_reset(), TWI_READ_DATA, and twi_send_ack().
Referenced by twi_interrupt_handler().
|
static |
Reset TWI bus.
none. |
References master_transfer, TWI_IDLE, twi_master_busy, and twi_reset().
Referenced by twi_interrupt_handler(), twi_master_addr_ack(), twi_master_read_done(), twi_master_read_last_byte(), twi_master_start(), and twi_master_write_done().
|
static |
Sending data to twi bus.
If last byte then send stop condition.
none. |
References master_transfer, STATUS_OK, TWI_IDLE, twi_master_busy, twi_send_stop(), and twi_write_byte().
Referenced by twi_master_write_done().
status_code_t twi_master_get_status | ( | void | ) |
returns the status of TWI bus
none |
References master_transfer.
Referenced by twi_master_read(), and twi_master_write().
status_code_t twi_master_init | ( | volatile void * | twi, |
twi_master_options_t * | opt | ||
) |
Inits TWI module as master.
This function is a TWI Master initialisation.
opt | twi setting options (see twi_master_options_t) |
References twi_master_options_t::baud_reg, cpu_irq_disable, cpu_irq_enable, MASTER, STATUS_OK, twi_interrupt_enable(), twi_mode, and TWI_PRESCALE_REG.
|
static |
Sending internal device address to twi bus.
none. |
References data, master_transfer, TWI_READ_DATA, twi_write_byte(), TWI_WRITE_DATA, and TWI_WRITE_IADDR_WRITE_DATA.
Referenced by twi_master_write_done().
status_code_t twi_master_read | ( | volatile void * | twi, |
const twi_package_t * | package | ||
) |
Reads the series of bytes from the TWI bus.
package | - Package information and data (see twi_package_t) |
References ERR_INVALID_ARG, twi_package_t::length, master_transfer, NULL, OPERATION_IN_PROGRESS, twi_master_busy, twi_master_get_status(), TWI_READ_DATA, twi_send_start(), TWI_SLAVE_NO_INTERNAL_ADDRESS, and TWI_WRITE_IADDR_READ_DATA.
Referenced by main(), and run_twi_master_recv_test().
|
static |
Notification that byte was read by the TWI.
data | - contains byte that was read. |
References data, ERR_PROTOCOL, master_transfer, twi_master_bus_reset(), TWI_READ_DATA, and twi_send_ack().
Referenced by twi_interrupt_handler().
|
static |
Notification that last byte was read from the TWI.
Needs to send STOP condition
data | - contains byte that was read. |
References data, ERR_PROTOCOL, master_transfer, STATUS_OK, TWI_IDLE, twi_master_bus_reset(), twi_master_busy, TWI_READ_DATA, and twi_send_stop().
Referenced by twi_interrupt_handler().
|
static |
Notification about the start condition was sent.
This function is a TWI Master start indication.
none |
References ERR_PROTOCOL, master_transfer, twi_master_bus_reset(), TWI_READ_DATA, TWI_READ_ENABLE, twi_write_byte(), TWI_WRITE_DATA, TWI_WRITE_ENABLE, TWI_WRITE_IADDR_READ_DATA, and TWI_WRITE_IADDR_WRITE_DATA.
Referenced by twi_interrupt_handler().
status_code_t twi_master_write | ( | volatile void * | twi, |
const twi_package_t * | package | ||
) |
Perform a TWI master write transfer.
This function is a TWI Master write transaction.
package | - Package information and data (see twi_package_t) |
References ERR_INVALID_ARG, master_transfer, NULL, OPERATION_IN_PROGRESS, twi_master_busy, twi_master_get_status(), twi_send_start(), TWI_SLAVE_NO_INTERNAL_ADDRESS, TWI_WRITE_DATA, and TWI_WRITE_IADDR_WRITE_DATA.
Referenced by main(), and run_twi_master_send_test().
|
static |
Notification that byte was written to the TWI.
result | - contains result of previous operation. |
References ERR_PROTOCOL, master_transfer, twi_master_bus_reset(), twi_master_data_write(), twi_master_internal_addr_write(), TWI_READ_DATA, twi_send_start(), TWI_WRITE_DATA, TWI_WRITE_IADDR_READ_DATA, and TWI_WRITE_IADDR_WRITE_DATA.
Referenced by twi_interrupt_handler().