Skip to content

Commit

Permalink
Unhiding twi functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
apozharski committed Aug 4, 2020
1 parent c8a1dd9 commit 1ea49be
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
20 changes: 10 additions & 10 deletions libraries/Wire/src/utility/twi.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,35 +175,35 @@ uint8_t TWI_MasterReady(void)
* Sets the baud rate used by TWI Master.
*
* \param frequency The required baud.
*
* \retval 0 If baud set correctly
* \retval 1 If baud not set due to being too high. > 1MHz
*/
void TWI_MasterSetBaud(uint32_t frequency){
uint8_t TWI_MasterSetBaud(uint32_t frequency){

// Formula is: BAUD = ((F_CLKPER/frequency) - F_CLKPER*T_RISE - 10)/2;
// Where T_RISE varies depending on operating frequency...
// From 1617 DS: 1000ns @ 100kHz / 300ns @ 400kHz / 120ns @ 1MHz

uint16_t t_rise;

if(frequency < 200000){
frequency = 100000;
if(frequency <= 100000){
t_rise = 1000;

} else if (frequency < 800000){
frequency = 400000;
} else if (frequency <= 400000){
t_rise = 300;

} else if (frequency < 1200000){
frequency = 1000000;
} else if (frequency <= 1000000){
t_rise = 120;

} else {
frequency = 100000;
t_rise = 1000;
return 1
}

uint32_t baud = ((F_CPU_CORRECTED/frequency) - (((F_CPU_CORRECTED*t_rise)/1000)/1000)/1000 - 10)/2;
TWI0.MBAUD = (uint8_t)baud;

return 0;
}

/*! \brief TWI write transaction.
Expand Down Expand Up @@ -781,4 +781,4 @@ ISR(TWI0_TWIM_vect){

ISR(TWI0_TWIS_vect){
TWI_SlaveInterruptHandler();
}
}
2 changes: 1 addition & 1 deletion libraries/Wire/src/utility/twi.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void TWI_Flush(void);
void TWI_Disable(void);
TWI_BUSSTATE_t TWI_MasterState(void);
uint8_t TWI_MasterReady(void);
void TWI_MasterSetBaud(uint32_t frequency);
uint8_t TWI_MasterSetBaud(uint32_t frequency);
uint8_t TWI_MasterWrite(uint8_t slave_address,
uint8_t *write_data,
uint8_t bytes_to_write,
Expand Down

0 comments on commit 1ea49be

Please sign in to comment.