Skip to content

Commit 85df635

Browse files
committed
Unhiding twi functionality
1 parent c8a1dd9 commit 85df635

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

Diff for: libraries/Wire/src/utility/twi.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -175,35 +175,35 @@ uint8_t TWI_MasterReady(void)
175175
* Sets the baud rate used by TWI Master.
176176
*
177177
* \param frequency The required baud.
178+
*
179+
* \retval 0 If baud set correctly
180+
* \retval 1 If baud not set due to being too high. > 1MHz
178181
*/
179-
void TWI_MasterSetBaud(uint32_t frequency){
182+
uint8_t TWI_MasterSetBaud(uint32_t frequency){
180183

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

185188
uint16_t t_rise;
186189

187-
if(frequency < 200000){
188-
frequency = 100000;
190+
if(frequency <= 100000){
189191
t_rise = 1000;
190192

191-
} else if (frequency < 800000){
192-
frequency = 400000;
193+
} else if (frequency <= 400000){
193194
t_rise = 300;
194195

195-
} else if (frequency < 1200000){
196-
frequency = 1000000;
196+
} else if (frequency <= 1000000){
197197
t_rise = 120;
198198

199199
} else {
200-
frequency = 100000;
201-
t_rise = 1000;
200+
return 1;
202201
}
203202

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

206+
return 0;
207207
}
208208

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

782782
ISR(TWI0_TWIS_vect){
783783
TWI_SlaveInterruptHandler();
784-
}
784+
}

Diff for: libraries/Wire/src/utility/twi.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void TWI_Flush(void);
7575
void TWI_Disable(void);
7676
TWI_BUSSTATE_t TWI_MasterState(void);
7777
uint8_t TWI_MasterReady(void);
78-
void TWI_MasterSetBaud(uint32_t frequency);
78+
uint8_t TWI_MasterSetBaud(uint32_t frequency);
7979
uint8_t TWI_MasterWrite(uint8_t slave_address,
8080
uint8_t *write_data,
8181
uint8_t bytes_to_write,

0 commit comments

Comments
 (0)