Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhiding twi functionality #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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