Skip to content

Commit 95b618b

Browse files
committed
Added limit checking for low SPI baud rate.
1 parent 36289c1 commit 95b618b

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

pic32/libraries/SPI/SPI.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,18 @@ class SPISettings {
135135
* at runtime (dynamically) and so we need to re-compute the proper
136136
* BRG value each time we start a transaction to be safe. */
137137
uint16_t GenerateBRG(void) {
138-
/* Compute the baud rate divider for this frequency.
138+
/* Compute the baud rate divider for this frequency. If the
139+
* desired clock rate is too slow for our slowest possible
140+
* on this system, use the slowest possible.
139141
*/
140-
return (uint16_t)((__PIC32_pbClk / (2 * DesiredSPIClockFrequency)) - 1);
142+
if ((__PIC32_pbClk / (2 * DesiredSPIClockFrequency)) > 512)
143+
{
144+
return(511);
145+
}
146+
else
147+
{
148+
return (uint16_t)((__PIC32_pbClk / (2 * DesiredSPIClockFrequency)) - 1);
149+
}
141150
}
142151
uint32_t con;
143152
uint16_t brg;

0 commit comments

Comments
 (0)