Skip to content

Commit

Permalink
[sw] HAL: add TWI clock stretching
Browse files Browse the repository at this point in the history
  • Loading branch information
stnolting committed Apr 1, 2024
1 parent 4f08418 commit 91d9fc6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
3 changes: 2 additions & 1 deletion sw/lib/include/neorv32_twi.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ enum NEORV32_TWI_CTRL_enum {
TWI_CTRL_CDIV1 = 5, /**< TWI control register(5) (r/w): Clock divider bit 1 */
TWI_CTRL_CDIV2 = 6, /**< TWI control register(6) (r/w): Clock divider bit 2 */
TWI_CTRL_CDIV3 = 7, /**< TWI control register(7) (r/w): Clock divider bit 3 */
TWI_CTRL_CLKSTR = 8, /**< TWI control register(8) (r/w): Enable/allow clock stretching */

TWI_CTRL_FIFO_LSB = 15, /**< SPI control register(15) (r/-): log2(FIFO size), lsb */
TWI_CTRL_FIFO_MSB = 18, /**< SPI control register(18) (r/-): log2(FIFO size), msb */
Expand Down Expand Up @@ -100,7 +101,7 @@ enum NEORV32_TWI_DCMD_enum {
**************************************************************************/
/**@{*/
int neorv32_twi_available(void);
void neorv32_twi_setup(int prsc, int cdiv);
void neorv32_twi_setup(int prsc, int cdiv, int clkstr);
int neorv32_twi_get_fifo_depth(void);
void neorv32_twi_disable(void);
void neorv32_twi_enable(void);
Expand Down
10 changes: 6 additions & 4 deletions sw/lib/source/neorv32_twi.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,17 @@ int neorv32_twi_available(void) {
*
* @param[in] prsc Clock prescaler select (0..7). See #NEORV32_CLOCK_PRSC_enum.
* @param[in] cdiv Clock divider (0..15).
* @param[in] clkstr Enable (allow) clock stretching.
**************************************************************************/
void neorv32_twi_setup(int prsc, int cdiv) {
void neorv32_twi_setup(int prsc, int cdiv, int clkstr) {

NEORV32_TWI->CTRL = 0; // reset

uint32_t ctrl = 0;
ctrl |= ((uint32_t)( 1) << TWI_CTRL_EN);
ctrl |= ((uint32_t)(prsc & 0x07) << TWI_CTRL_PRSC0);
ctrl |= ((uint32_t)(cdiv & 0x0f) << TWI_CTRL_CDIV0);
ctrl |= ((uint32_t)( 0x1) << TWI_CTRL_EN);
ctrl |= ((uint32_t)(prsc & 0x7) << TWI_CTRL_PRSC0);
ctrl |= ((uint32_t)(cdiv & 0xf) << TWI_CTRL_CDIV0);
ctrl |= ((uint32_t)(clkstr & 0x1) << TWI_CTRL_CLKSTR);
NEORV32_TWI->CTRL = ctrl;
}

Expand Down
5 changes: 5 additions & 0 deletions sw/svd/neorv32.svd
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,11 @@
<bitRange>[7:4]</bitRange>
<description>TWI clock divider</description>
</field>
<field>
<name>TWI_CTRL_CLKSTR</name>
<bitRange>[8:8]</bitRange>
<description>Enable (allow) clock stretching</description>
</field>
<field>
<name>TWI_CTRL_FIFO</name>
<bitRange>[18:15]</bitRange>
Expand Down

0 comments on commit 91d9fc6

Please sign in to comment.