// #################################################################################################
|
// #################################################################################################
|
// # << NEORV32: neorv32_twi.c - Two-Wire Interface Controller (TWI) HW Driver >> #
|
// # << NEORV32: neorv32_twi.c - Two-Wire Interface Controller (TWI) HW Driver >> #
|
// # ********************************************************************************************* #
|
// # ********************************************************************************************* #
|
// # BSD 3-Clause License #
|
// # BSD 3-Clause License #
|
// # #
|
// # #
|
// # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
// # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
// # #
|
// # #
|
// # Redistribution and use in source and binary forms, with or without modification, are #
|
// # Redistribution and use in source and binary forms, with or without modification, are #
|
// # permitted provided that the following conditions are met: #
|
// # permitted provided that the following conditions are met: #
|
// # #
|
// # #
|
// # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
// # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
// # conditions and the following disclaimer. #
|
// # conditions and the following disclaimer. #
|
// # #
|
// # #
|
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
// # conditions and the following disclaimer in the documentation and/or other materials #
|
// # conditions and the following disclaimer in the documentation and/or other materials #
|
// # provided with the distribution. #
|
// # provided with the distribution. #
|
// # #
|
// # #
|
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
// # endorse or promote products derived from this software without specific prior written #
|
// # endorse or promote products derived from this software without specific prior written #
|
// # permission. #
|
// # permission. #
|
// # #
|
// # #
|
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
// # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
// # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
// # ********************************************************************************************* #
|
// # ********************************************************************************************* #
|
// # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
// # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
// #################################################################################################
|
// #################################################################################################
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* @file neorv32_twi.c
|
* @file neorv32_twi.c
|
* @author Stephan Nolting
|
* @author Stephan Nolting
|
* @brief Two-Wire Interface Controller (TWI) HW driver source file.
|
* @brief Two-Wire Interface Controller (TWI) HW driver source file.
|
*
|
*
|
* @note These functions should only be used if the TWI unit was synthesized (IO_TWI_EN = true).
|
* @note These functions should only be used if the TWI unit was synthesized (IO_TWI_EN = true).
|
**************************************************************************/
|
**************************************************************************/
|
|
|
#include "neorv32.h"
|
#include "neorv32.h"
|
#include "neorv32_twi.h"
|
#include "neorv32_twi.h"
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* Check if TWI unit was synthesized.
|
* Check if TWI unit was synthesized.
|
*
|
*
|
* @return 0 if TWI was not synthesized, 1 if TWI is available.
|
* @return 0 if TWI was not synthesized, 1 if TWI is available.
|
**************************************************************************/
|
**************************************************************************/
|
int neorv32_twi_available(void) {
|
int neorv32_twi_available(void) {
|
|
|
if (SYSINFO_FEATURES & (1 << SYSINFO_FEATURES_IO_TWI)) {
|
if (NEORV32_SYSINFO.SOC & (1 << SYSINFO_SOC_IO_TWI)) {
|
return 1;
|
return 1;
|
}
|
}
|
else {
|
else {
|
return 0;
|
return 0;
|
}
|
}
|
}
|
}
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* Enable and configure TWI controller. The TWI control register bits are listed in #NEORV32_TWI_CT_enum.
|
* Enable and configure TWI controller. The TWI control register bits are listed in #NEORV32_TWI_CTRL_enum.
|
*
|
*
|
* @param[in] prsc Clock prescaler select (0..7). See #NEORV32_CLOCK_PRSC_enum.
|
* @param[in] prsc Clock prescaler select (0..7). See #NEORV32_CLOCK_PRSC_enum.
|
* @param[in] ckst_en Enable clock-stretching by peripherals when 1.
|
* @param[in] ckst_en Enable clock-stretching by peripherals when 1.
|
**************************************************************************/
|
**************************************************************************/
|
void neorv32_twi_setup(uint8_t prsc, uint8_t ckst_en) {
|
void neorv32_twi_setup(uint8_t prsc, uint8_t ckst_en) {
|
|
|
TWI_CT = 0; // reset
|
NEORV32_TWI.CTRL = 0; // reset
|
|
|
uint32_t ct_enable = 1;
|
uint32_t ct_enable = 1;
|
ct_enable = ct_enable << TWI_CT_EN;
|
ct_enable = ct_enable << TWI_CTRL_EN;
|
|
|
uint32_t ct_prsc = (uint32_t)(prsc & 0x07);
|
uint32_t ct_prsc = (uint32_t)(prsc & 0x07);
|
ct_prsc = ct_prsc << TWI_CT_PRSC0;
|
ct_prsc = ct_prsc << TWI_CTRL_PRSC0;
|
|
|
uint32_t ct_cksten = (uint32_t)(ckst_en & 0x01);
|
uint32_t ct_cksten = (uint32_t)(ckst_en & 0x01);
|
ct_cksten = ct_cksten << TWI_CT_CKSTEN;
|
ct_cksten = ct_cksten << TWI_CTRL_CKSTEN;
|
|
|
TWI_CT = ct_enable | ct_prsc | ct_cksten;
|
NEORV32_TWI.CTRL = ct_enable | ct_prsc | ct_cksten;
|
}
|
}
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* Disable TWI controller.
|
* Disable TWI controller.
|
**************************************************************************/
|
**************************************************************************/
|
void neorv32_twi_disable(void) {
|
void neorv32_twi_disable(void) {
|
|
|
TWI_CT &= ~((uint32_t)(1 << TWI_CT_EN));
|
NEORV32_TWI.CTRL &= ~((uint32_t)(1 << TWI_CTRL_EN));
|
}
|
}
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* Enable TWI controller.
|
* Enable TWI controller.
|
**************************************************************************/
|
**************************************************************************/
|
void neorv32_twi_enable(void) {
|
void neorv32_twi_enable(void) {
|
|
|
TWI_CT |= (uint32_t)(1 << TWI_CT_EN);
|
NEORV32_TWI.CTRL |= (uint32_t)(1 << TWI_CTRL_EN);
|
}
|
}
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* Activate sending ACKs by controller (MACK).
|
* Activate sending ACKs by controller (MACK).
|
**************************************************************************/
|
**************************************************************************/
|
void neorv32_twi_mack_enable(void) {
|
void neorv32_twi_mack_enable(void) {
|
|
|
TWI_CT |= ((uint32_t)(1 << TWI_CT_MACK));
|
NEORV32_TWI.CTRL |= ((uint32_t)(1 << TWI_CTRL_MACK));
|
}
|
}
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* Deacivate sending ACKs by controller (MACK).
|
* Deacivate sending ACKs by controller (MACK).
|
**************************************************************************/
|
**************************************************************************/
|
void neorv32_twi_mack_disable(void) {
|
void neorv32_twi_mack_disable(void) {
|
|
|
TWI_CT &= ~((uint32_t)(1 << TWI_CT_MACK));
|
NEORV32_TWI.CTRL &= ~((uint32_t)(1 << TWI_CTRL_MACK));
|
}
|
}
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* Check if TWI is busy.
|
* Check if TWI is busy.
|
*
|
*
|
* @note This function is blocking.
|
* @note This function is blocking.
|
*
|
*
|
* @return 0 if idle, 1 if busy
|
* @return 0 if idle, 1 if busy
|
**************************************************************************/
|
**************************************************************************/
|
int neorv32_twi_busy(void) {
|
int neorv32_twi_busy(void) {
|
|
|
if (TWI_CT & (1 << TWI_CT_BUSY)) {
|
if (NEORV32_TWI.CTRL & (1 << TWI_CTRL_BUSY)) {
|
return 1;
|
return 1;
|
}
|
}
|
return 0;
|
return 0;
|
}
|
}
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* Generate START condition and send first byte (address including R/W bit).
|
* Generate START condition and send first byte (address including R/W bit).
|
*
|
*
|
* @note Blocking function.
|
* @note Blocking function.
|
*
|
*
|
* @param[in] a Data byte including 7-bit address and R/W-bit (lsb).
|
* @param[in] a Data byte including 7-bit address and R/W-bit (lsb).
|
* @return 0: ACK received, 1: NACK received.
|
* @return 0: ACK received, 1: NACK received.
|
**************************************************************************/
|
**************************************************************************/
|
int neorv32_twi_start_trans(uint8_t a) {
|
int neorv32_twi_start_trans(uint8_t a) {
|
|
|
neorv32_twi_generate_start(); // generate START condition
|
neorv32_twi_generate_start(); // generate START condition
|
|
|
TWI_DATA = (uint32_t)a; // send address
|
NEORV32_TWI.DATA = (uint32_t)a; // send address
|
while(TWI_CT & (1 << TWI_CT_BUSY)); // wait until idle again
|
while(NEORV32_TWI.CTRL & (1 << TWI_CTRL_BUSY)); // wait until idle again
|
|
|
// check for ACK/NACK
|
// check for ACK/NACK
|
if (TWI_CT & (1 << TWI_CT_ACK))
|
if (NEORV32_TWI.CTRL & (1 << TWI_CTRL_ACK))
|
return 0; // ACK received
|
return 0; // ACK received
|
else
|
else
|
return 1; // NACK received
|
return 1; // NACK received
|
}
|
}
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* Send data byte and also receive data byte (can be read via neorv32_twi_get_data()).
|
* Send data byte and also receive data byte (can be read via neorv32_twi_get_data()).
|
*
|
*
|
* @note Blocking function.
|
* @note Blocking function.
|
*
|
*
|
* @param[in] d Data byte to be send.
|
* @param[in] d Data byte to be send.
|
* @return 0: ACK received, 1: NACK received.
|
* @return 0: ACK received, 1: NACK received.
|
**************************************************************************/
|
**************************************************************************/
|
int neorv32_twi_trans(uint8_t d) {
|
int neorv32_twi_trans(uint8_t d) {
|
|
|
TWI_DATA = (uint32_t)d; // send data
|
NEORV32_TWI.DATA = (uint32_t)d; // send data
|
while(TWI_CT & (1 << TWI_CT_BUSY)); // wait until idle again
|
while(NEORV32_TWI.CTRL & (1 << TWI_CTRL_BUSY)); // wait until idle again
|
|
|
// check for ACK/NACK
|
// check for ACK/NACK
|
if (TWI_CT & (1 << TWI_CT_ACK))
|
if (NEORV32_TWI.CTRL & (1 << TWI_CTRL_ACK))
|
return 0; // ACK received
|
return 0; // ACK received
|
else
|
else
|
return 1; // NACK received
|
return 1; // NACK received
|
}
|
}
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* Get received data from last transmission.
|
* Get received data from last transmission.
|
*
|
*
|
* @return 0: Last received data byte.
|
* @return 0: Last received data byte.
|
**************************************************************************/
|
**************************************************************************/
|
uint8_t neorv32_twi_get_data(void) {
|
uint8_t neorv32_twi_get_data(void) {
|
|
|
return (uint8_t)TWI_DATA; // get RX data from previous transmission
|
return (uint8_t)NEORV32_TWI.DATA; // get RX data from previous transmission
|
}
|
}
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* Generate STOP condition.
|
* Generate STOP condition.
|
*
|
*
|
* @note Blocking function.
|
* @note Blocking function.
|
**************************************************************************/
|
**************************************************************************/
|
void neorv32_twi_generate_stop(void) {
|
void neorv32_twi_generate_stop(void) {
|
|
|
TWI_CT |= (uint32_t)(1 << TWI_CT_STOP); // generate STOP condition
|
NEORV32_TWI.CTRL |= (uint32_t)(1 << TWI_CTRL_STOP); // generate STOP condition
|
while(TWI_CT & (1 << TWI_CT_BUSY)); // wait until idle again
|
while(NEORV32_TWI.CTRL & (1 << TWI_CTRL_BUSY)); // wait until idle again
|
}
|
}
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* Generate START condition.
|
* Generate START condition.
|
*
|
*
|
* @note Blocking function.
|
* @note Blocking function.
|
**************************************************************************/
|
**************************************************************************/
|
void neorv32_twi_generate_start(void) {
|
void neorv32_twi_generate_start(void) {
|
|
|
TWI_CT |= (1 << TWI_CT_START); // generate START condition
|
NEORV32_TWI.CTRL |= (1 << TWI_CTRL_START); // generate START condition
|
while(TWI_CT & (1 << TWI_CT_BUSY)); // wait until idle again
|
while(NEORV32_TWI.CTRL & (1 << TWI_CTRL_BUSY)); // wait until idle again
|
}
|
}
|
|
|