Driver API for TWIHS (plib_twihs0.h) More...
Typedefs | |
| typedef void(* | TWIHS_CALLBACK_HANDLER) (void) |
| Function pointer type for TWI event callback handler. | |
Functions | |
| void | TWIHS0_Host_Enable (void) |
| Enables the TWIHS Host mode operation. | |
| void | TWIHS0_Host_Disable (void) |
| Disables the TWIHS Host mode operation. | |
| void | TWIHS0_Host_SendStop (void) |
| Sends the I2C STOP condition on the bus by writing into the STOP bit in TWIHS_CR register. | |
| void | TWIHS0_Host_SendStart (void) |
| Sends the I2C START condition on the bus by writing into the START bit in TWIHS_CR register. A frame beginning with a START bit is transmitted according to the features defined in the TWIHS Host Mode Register (TWIHS_MMR). | |
| void | TWIHS0_Host_SendStartAndStop (void) |
| Sets both the I2C START and STOP condition START and the STOP bits in TWIHS_CR register. | |
| void | TWIHS0_Host_SetTransferDirection (TWIHS_TRANSFER_DIR transfer_direction) |
| Sets the TWIHS host transfer direction. | |
| void | TWIHS0_Host_SendAddress (uint32_t address) |
| Sets the target client address by writing the DADR bits in TWIHS_MMR register. | |
| void | TWIHS0_Host_SetInternalAddressSize (TWIHS_HOST_INTERNAL_ADDRESS_SIZE internal_address_size) |
| Configures the internal address size for the transfer based on the the size of the client address. Set to 1, 2 or 3 when client address exceeds 7 bits. Set to 0 when client address is within 7 bits. | |
| TWIHS_HOST_HWREG_STATUS | TWIHS0_SetBaudRate (uint32_t srcClkFreq, uint32_t i2cClkSpeed) |
| Sets the TWIHS clock speed by writing CKDIV, CHDIV and CLDIV bits in the TWIHS_CWGR register. | |
| void | TWIHS0_Client_Enable (void) |
| Enables the TWIHS Client mode operation. | |
| void | TWIHS0_Client_Disable (void) |
| Disables the TWIHS Client mode operation. | |
| void | TWIHS0_Client_Set7bitAddress (uint8_t addr) |
| Sets the 7-bit client address by writing into SADR bits in the TWIHS_SMR register. The Client device address is used in Client mode in order to be accessed by Host devices in Read orWrite mode. | |
| void | TWIHS0_Client_Set7bitAddressMask (uint8_t addr_mask) |
| Sets the 7-bit client address mask by writing into MASK bits in the TWIHS_SMR register. A mask can be applied on the Client device address in Client mode in order to allow multiple address answer. For each bit of the MASK field set to 1, the corresponding SADR bit is masked. If the MASK field value is 0, no mask is applied to the SADR field. | |
| TWIHS_TRANSFER_DIR | TWIHS0_Client_TransferDirGet (void) |
| Returns the current client transfer direction. This bit is used in Client mode only. When SVACC is low (no Client access has been detected) SVREAD is irrelevant. | |
| void | TWIHS0_Client_NACKDataPhase (bool isNACKEnable) |
| Sets the NACK value to be returned in the ACK cycle of the data phase by setting the NACKEN bit in TWIHS_SMR register. NACKEN bit can only be set during the client mode operation. | |
| TWIHS_CLIENT_ACK_STATUS | TWIHS0_LastByteAckStatusGet (void) |
| Returns the acknowledge status of the last received byte. | |
| void | TWIHS0_ResetSoftware (void) |
| Perform a software reset of the TWIHS module. | |
| void | TWIHS0_ClearTransmitHoldRegister (void) |
| Clears the Transmit Holding Register and sets TXRDY, TXCOMP flags. This api is called before enabling the TWIHS peripheral either in host or client mode. | |
| void | TWIHS0_EnableInterrupts (TWIHS_INTERRUPT_ENABLE interrupts) |
| Enable a specific TWIHS interrupt source. | |
| void | TWIHS0_DisableInterrupts (TWIHS_INTERRUPT_DISABLE interrupts) |
| Disable a specific TWI interrupt source. | |
| void | TWIHS0_SendData (uint8_t data) |
| Send a single data word via TWI. Writes data to the TWIHS_THR (Transmit Holding register) to initiate a data transfer. | |
| uint8_t | TWIHS0_ReceiveData (void) |
| Receive a single data word via SPI. Reads data from the TWIHS_RHR (Receive Holding register) that was received from the TWI bus. | |
| uint32_t | TWIHS0_GetBusStatus (void) |
| Returns the current TWIHS bus status. | |
| void | TWIHS0_SetCallbackHandler (TWIHS_CALLBACK_HANDLER callback) |
| Register an interrupt callback handler. | |
| uint32_t | TWIHS0_GetInterruptMask (void) |
| Returns the current interrupt mask register value. | |
Driver API for TWIHS (plib_twihs0.h)
| TWIHS_CALLBACK_HANDLER |
Function pointer type for TWI event callback handler.
Status codes for TWIHS hardware register operations.
TWIHS internal device address size selection.
TWIHS interrupt disable selection. Common for Host and Client.
TWIHS interrupt enable selection. Common for Host and Client.
| enum TWIHS_STATUS_EVENTS |
TWIHS hardware events. Common for Host and Client,.
| enum TWIHS_TRANSFER_DIR |
| void TWIHS0_ClearTransmitHoldRegister | ( | void | ) |
Clears the Transmit Holding Register and sets TXRDY, TXCOMP flags. This api is called before enabling the TWIHS peripheral either in host or client mode.
| None. |
| void TWIHS0_Client_Disable | ( | void | ) |
Disables the TWIHS Client mode operation.
Disables the TWIHS0 peripheral Client functionality by setting the SVDIS bit in the TWIHS_CR register. The function ensures that ongoing transfers are completed before shutdown.
| None. |
| void TWIHS0_Client_Enable | ( | void | ) |
Enables the TWIHS Client mode operation.
Enables the TWIHS0 peripheral Client functionality by setting the SVEN bit in the TWIHS_CR register. The peripheral clock must be enabled via PMC before calling this function.
| None. |
| void TWIHS0_Client_NACKDataPhase | ( | bool | isNACKEnable | ) |
Sets the NACK value to be returned in the ACK cycle of the data phase by setting the NACKEN bit in TWIHS_SMR register. NACKEN bit can only be set during the client mode operation.
| isNACKEnable | - Enable or disable NACK generation. |
| void TWIHS0_Client_Set7bitAddress | ( | uint8_t | addr | ) |
Sets the 7-bit client address by writing into SADR bits in the TWIHS_SMR register. The Client device address is used in Client mode in order to be accessed by Host devices in Read orWrite mode.
| addr | - 7-bit client address. |
| void TWIHS0_Client_Set7bitAddressMask | ( | uint8_t | addr_mask | ) |
Sets the 7-bit client address mask by writing into MASK bits in the TWIHS_SMR register. A mask can be applied on the Client device address in Client mode in order to allow multiple address answer. For each bit of the MASK field set to 1, the corresponding SADR bit is masked. If the MASK field value is 0, no mask is applied to the SADR field.
| addr | - 7-bit client address mask. |
| TWIHS_TRANSFER_DIR TWIHS0_Client_TransferDirGet | ( | void | ) |
Returns the current client transfer direction. This bit is used in Client mode only. When SVACC is low (no Client access has been detected) SVREAD is irrelevant.
| None. |
| void TWIHS0_DisableInterrupts | ( | TWIHS_INTERRUPT_DISABLE | interrupts | ) |
Disable a specific TWI interrupt source.
Disables the specified interrupt(s) by setting the corresponding bits in TWIHS_IDR (Interrupt Disable Register). When disabled, TWI interrupts of those types will not trigger the ISR or invoke the callback, but the interrupt flag(s) may still be set in TWIHS_SR and can be polled.
Available interrupt sources (TWIHS_INTERRUPT_ENABLE):
| interrupts | - One or more interrupt source(s) (OR'd together) to disable. Example: DisableInterrupt(TWIHS_INTERRUPT_NACK_DIS | TWIHS_INTERRUPT_ARBITERATION_LOST_DIS) |
| void TWIHS0_EnableInterrupts | ( | TWIHS_INTERRUPT_ENABLE | interrupts | ) |
Enable a specific TWIHS interrupt source.
Enables the specified interrupt(s) by setting the corresponding bits in TWIHS_IER (Interrupt Enable Register). When enabled, TWI interrupts will trigger the ISR, which invokes the registered callback (if set via SetCallbackHandler).
Available interrupt sources (TWIHS_INTERRUPT_ENABLE):
| interrupts | - One or more interrupt source(s) (OR'd together) to enable. Example: EnableInterrupt(TWIHS_INTERRUPT_NACK_EN | TWIHS_INTERRUPT_ARBITERATION_LOST_EN) |
| uint32_t TWIHS0_GetBusStatus | ( | void | ) |
Returns the current TWIHS bus status.
| None. |
Returns the current TWIHS bus status.
Available bus status (TWIHS_STATUS_EVENTS):
| None. |
| uint32_t TWIHS0_GetInterruptMask | ( | void | ) |
Returns the current interrupt mask register value.
| None. |
| void TWIHS0_Host_Disable | ( | void | ) |
Disables the TWIHS Host mode operation.
Disables the TWIHS0 peripheral Host functionality by setting the MSDIS bit in the TWIHS_CR register. The function ensures that ongoing transfers are completed before shutdown.
| None. |
| void TWIHS0_Host_Enable | ( | void | ) |
Enables the TWIHS Host mode operation.
Enables the TWIHS0 peripheral Host functionality by setting the MSEN bit in the TWIHS_CR register. The peripheral clock must be enabled via PMC before calling this function.
| None. |
| void TWIHS0_Host_SendAddress | ( | uint32_t | address | ) |
Sets the target client address by writing the DADR bits in TWIHS_MMR register.
Calling this api doesn't initiate the client address transfer directly , instead it sets the client address which will be sent on bus once the transfer is initiated by writing into the TWIHS_THR register in Host write mode or setting the START bit in TWIHS_THR register in Host read mode.
| address | - 7-bit or 10-bit client address. |
| void TWIHS0_Host_SendStart | ( | void | ) |
Sends the I2C START condition on the bus by writing into the START bit in TWIHS_CR register. A frame beginning with a START bit is transmitted according to the features defined in the TWIHS Host Mode Register (TWIHS_MMR).
This action is necessary when the TWIHS peripheral needs to read data from a Client. When configured in Host mode with a write operation, a frame is sent as soon as the user writes a character in the Transmit Holding Register (TWIHS_THR).
| None. |
| void TWIHS0_Host_SendStartAndStop | ( | void | ) |
Sets both the I2C START and STOP condition START and the STOP bits in TWIHS_CR register.
In single data byte Host read, both START and STOP must be set.
| None. |
| void TWIHS0_Host_SendStop | ( | void | ) |
Sends the I2C STOP condition on the bus by writing into the STOP bit in TWIHS_CR register.
In single data byte Host read, both START and STOP must be set. In multiple data bytes Host read, the STOP must be set after the last data received but one. In Host Read mode, if a NACK bit is received, the STOP is automatically performed. In Host data write operation, a STOP condition will be sent after the transmission of the current data is finished.
| None. |
| void TWIHS0_Host_SetInternalAddressSize | ( | TWIHS_HOST_INTERNAL_ADDRESS_SIZE | internal_address_size | ) |
Configures the internal address size for the transfer based on the the size of the client address. Set to 1, 2 or 3 when client address exceeds 7 bits. Set to 0 when client address is within 7 bits.
| internal_address_size | - Internal address size (set to zero for 7-bit addressing). |
| void TWIHS0_Host_SetTransferDirection | ( | TWIHS_TRANSFER_DIR | transfer_direction | ) |
Sets the TWIHS host transfer direction.
| transfer_direction | - Direction of transfer (Host read or write). |
| TWIHS_CLIENT_ACK_STATUS TWIHS0_LastByteAckStatusGet | ( | void | ) |
Returns the acknowledge status of the last received byte.
In Host mode NACK bit is set when a data or address byte has not been acknowledged by the Client component. Set at the same time as TXCOMP
In Client mode In Read mode, NACK bit is set when a data byte has not been acknowledged by the Host. When NACK is set, the user must not fill TWIHS_THR even if TXRDY is set, because it means that the Host stops the data transfer or re-initiate it.
| None. |
| uint8_t TWIHS0_ReceiveData | ( | void | ) |
Receive a single data word via SPI. Reads data from the TWIHS_RHR (Receive Holding register) that was received from the TWI bus.
In Host read mode, The read sequence begins by setting the START bit. After the START condition has been sent, the Host sends a 7-bit Client address to notify the Client device The bit following the Client address indicates the transfer direction, 0 in this case (MREAD = 0 in TWIHS_MMR). When TWIHS_SR.RXRDY is set, a character has been received in the Receive Holding register (TWIHS_RHR). The RXRDY bit is reset when reading the TWIHS_RHR.
In Client read mode, After a START or a REPEATED START, the decoding of the address starts. If the Client address is decoded, SVACC is set and SVREAD indicates the direction of the transfer (SVREAD is low in this case). Until a STOP or REPEATED START condition is detected, the TWIHS stores the received data in TWIHS_RHR. If a STOP condition or a REPEATED START + an address different from SADR is detected, SVACC is reset.
| void |
| void TWIHS0_ResetSoftware | ( | void | ) |
Perform a software reset of the TWIHS module.
Resets the TWI module to its initial state by setting the SWRST bit in TWIHS_CR register. After reset, all registers return to their default values:
After calling this function, the TWIHS peripheral must be reconfigured before use.
| None. |
| void TWIHS0_SendData | ( | uint8_t | data | ) |
Send a single data word via TWI. Writes data to the TWIHS_THR (Transmit Holding register) to initiate a data transfer.
In Host write mode, writing to TWIHS_THR register initiates a START condition, it sends a 7-bit Client address, configured in the Host Mode register (DADR in TWIHS_MMR), to notify the Client device. The bit following the Client address indicates the transfer direction, 0 in this case (MREAD = 0 in TWIHS_MMR).
In Client write mode, After a START or a REPEATED START condition is detected, the decoding of the address starts. If the Client address (SADR) is decoded, SVACC is set and SVREAD indicates the direction of the transfer. Until a STOP or REPEATED START condition is detected, the TWIHS continues sending data loaded in TWIHS_THR. If a STOP condition or a REPEATED START + an address different from SADR is detected, SVACC is reset.
| data | The data byte to transmit (8 bits;). |
| TWIHS_HOST_HWREG_STATUS TWIHS0_SetBaudRate | ( | uint32_t | srcClkFreq, |
| uint32_t | i2cClkSpeed ) |
Sets the TWIHS clock speed by writing CKDIV, CHDIV and CLDIV bits in the TWIHS_CWGR register.
This api internally calculates the register values to be configured taking the system clock and the required I2C clock as the input.
| srcClkFreq | - Source clock frequency in Hz. |
| i2cClkSpeed | - Desired I2C clock speed in Hz. |
| void TWIHS0_SetCallbackHandler | ( | TWIHS_CALLBACK_HANDLER | callback | ) |
Register an interrupt callback handler.
Registers a user-defined callback function to be invoked from the TWI interrupt service routine (ISR) when any enabled TWI interrupt occurs.
| callback | Function pointer to the callback handler: typedef void (*TWIHS_CALLBACK_HANDLER)(void); |