44 * Copyright (c) 2014 by Matthijs Kooijman <matthijs@stdin.nl> (SPISettings AVR)
55 * Copyright (c) 2014 by Andrew J. Kroll <xxxajk@gmail.com> (atomicity fixes)
66 * Copyright (c) 2015 by Majenko Technologies <matt@majenko.co.uk> (port to chipKIT)
7- * SPI Master library for arduino .
7+ * SPI Master library for Arduino .
88 *
99 * This file is free software; you can redistribute it and/or modify
1010 * it under the terms of either the GNU General Public License version 2
@@ -99,10 +99,10 @@ class SPISettings {
9999 DesiredSPIClockFrequency = clock;
100100
101101 // By including the needed flag here, we have a speed optimization.
102- // We will always set the Master Enable bit, becuase we are always the SPI master
102+ // We will always set the Master Enable bit, because we are always the SPI master
103103 // We will always set the ON bit because we always want the SPI peripheral turned on
104104 //
105- // The _SPICON_SMP bit makes the SPI preph actually follow the
105+ // The _SPICON_SMP bit makes the SPI peripheral actually follow the
106106 // general SPI rules that everyone else uses. Normally an SPI master
107107 // samples just before the next cycle starts.
108108 // At high data rates it becomes very important to sample later
@@ -127,50 +127,21 @@ class SPISettings {
127127 }
128128 }
129129 /* This function computes the proper value for the BRG register
130- * (baudrate generator divisor). This computation can't be done
131- * in this object's intializer , because the value __PIC32_pbClk
132- * isn't guaranteed to be correct until after all obejcts have
133- * been intialized and the init() call in wiring.c has had a
130+ * (baud rate generator divisor). This computation can't be done
131+ * in this object's initializer , because the value __PIC32_pbClk
132+ * isn't guaranteed to be correct until after all objects have
133+ * been initialized and the init() call in wiring.c has had a
134134 * chance to run. Also, this value can (theoretically) be changed
135135 * at runtime (dynamically) and so we need to re-compute the proper
136- * BRG value each time we start a tranasaction to be safe. */
136+ * BRG value each time we start a transaction to be safe. */
137137 uint16_t GenerateBRG (void ) {
138138 /* Compute the baud rate divider for this frequency.
139139 */
140- switch (DesiredSPIClockFrequency) {
141- case SPI_CLOCK_DIV2:
142- return (__PIC32_pbClk / 16000000 ) * 1 ;
143- break ;
144-
145- case SPI_CLOCK_DIV4:
146- return (__PIC32_pbClk / 16000000 ) * 3 ;
147- break ;
148-
149- case SPI_CLOCK_DIV8:
150- return (__PIC32_pbClk / 16000000 ) * 7 ;
151- break ;
152-
153- case SPI_CLOCK_DIV16:
154- return (__PIC32_pbClk / 16000000 ) * 15 ;
155- break ;
156-
157- case SPI_CLOCK_DIV32:
158- return (__PIC32_pbClk / 16000000 ) * 31 ;
159- break ;
160-
161- case SPI_CLOCK_DIV64:
162- return (__PIC32_pbClk / 16000000 ) * 63 ;
163- break ;
164-
165- case SPI_CLOCK_DIV128:
166- return (__PIC32_pbClk / 16000000 ) * 127 ;
167- break ;
168- }
169- return (__PIC32_pbClk / 16000000 ) * 4 ;
140+ return (uint16_t )((__PIC32_pbClk / (2 * DesiredSPIClockFrequency)) - 1 );
170141 }
171142 uint32_t con;
172143 uint16_t brg;
173- /* A cached copy of the desired SPI clock freqency set at object creation */
144+ /* A cached copy of the desired SPI clock frequency set at object creation */
174145 uint32_t DesiredSPIClockFrequency;
175146 friend class SPIClass ;
176147};
@@ -242,7 +213,7 @@ class SPIClass {
242213 // pspi->sxCon.clr = (1 << _SPICON_ON); // This line can cause glitches on the CLOCK output
243214 /* Compute and set the proper BRG register value based on our desired SPI clock rate */
244215 pspi->sxBrg .reg = settings.GenerateBRG ();
245- /* Copy over the proper value of the CON regsiter for this set of settings, turning SPI peripheral back on */
216+ /* Copy over the proper value of the CON register for this set of settings, turning SPI peripheral back on */
246217 pspi->sxCon .reg = settings.con ;
247218 softBitOrder = settings.softBitOrder ;
248219 }
0 commit comments