Browse Source

General cleanup of HAL code

Scott Lahteine 6 years ago
parent
commit
b13099de3f

+ 21
- 40
Marlin/src/HAL/HAL_LPC1768/Wire.cpp View File

75
   PINSEL_CFG_Type PinCfg;
75
   PINSEL_CFG_Type PinCfg;
76
   PinCfg.OpenDrain = 0;
76
   PinCfg.OpenDrain = 0;
77
   PinCfg.Pinmode = 0;
77
   PinCfg.Pinmode = 0;
78
-  #if ((USEDI2CDEV_M == 0))
78
+
79
+  #if USEDI2CDEV_M == 0
79
     PinCfg.Funcnum = 1;
80
     PinCfg.Funcnum = 1;
80
     PinCfg.Pinnum = 27;
81
     PinCfg.Pinnum = 27;
81
     PinCfg.Portnum = 0;
82
     PinCfg.Portnum = 0;
83
     PinCfg.Pinnum = 28;
84
     PinCfg.Pinnum = 28;
84
     PINSEL_ConfigPin(&PinCfg); // SCL0 / D58  AUX-1
85
     PINSEL_ConfigPin(&PinCfg); // SCL0 / D58  AUX-1
85
   #endif
86
   #endif
86
-  #if ((USEDI2CDEV_M == 1))
87
+
88
+  #if USEDI2CDEV_M == 1
87
     PinCfg.Funcnum = 3;
89
     PinCfg.Funcnum = 3;
88
     PinCfg.Pinnum = 0;
90
     PinCfg.Pinnum = 0;
89
     PinCfg.Portnum = 0;
91
     PinCfg.Portnum = 0;
91
     PinCfg.Pinnum = 1;
93
     PinCfg.Pinnum = 1;
92
     PINSEL_ConfigPin(&PinCfg);  // SCL1 / D21 SCL
94
     PINSEL_ConfigPin(&PinCfg);  // SCL1 / D21 SCL
93
   #endif
95
   #endif
94
-  #if ((USEDI2CDEV_M == 2))
96
+
97
+  #if USEDI2CDEV_M == 2
95
     PinCfg.Funcnum = 2;
98
     PinCfg.Funcnum = 2;
96
     PinCfg.Pinnum = 10;
99
     PinCfg.Pinnum = 10;
97
     PinCfg.Portnum = 0;
100
     PinCfg.Portnum = 0;
102
 
105
 
103
   // Initialize I2C peripheral
106
   // Initialize I2C peripheral
104
   I2C_Init(I2CDEV_M, 100000);
107
   I2C_Init(I2CDEV_M, 100000);
105
-  
108
+
106
   // Enable Master I2C operation
109
   // Enable Master I2C operation
107
   I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
110
   I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
108
 }
111
 }
109
 
112
 
110
 uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) {
113
 uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) {
111
   // clamp to buffer length
114
   // clamp to buffer length
112
-  if(quantity > BUFFER_LENGTH){
115
+  if (quantity > BUFFER_LENGTH)
113
     quantity = BUFFER_LENGTH;
116
     quantity = BUFFER_LENGTH;
114
-  }
115
-  
117
+
116
   // perform blocking read into buffer
118
   // perform blocking read into buffer
117
   I2C_M_SETUP_Type transferMCfg;
119
   I2C_M_SETUP_Type transferMCfg;
118
   transferMCfg.sl_addr7bit = address >> 1; // not sure about the right shift
120
   transferMCfg.sl_addr7bit = address >> 1; // not sure about the right shift
158
 	transferMCfg.rx_length = 0;
160
 	transferMCfg.rx_length = 0;
159
 	transferMCfg.retransmissions_max = 3;
161
 	transferMCfg.retransmissions_max = 3;
160
   Status status = I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING);
162
   Status status = I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING);
161
-  
163
+
162
   // reset tx buffer iterator vars
164
   // reset tx buffer iterator vars
163
   txBufferIndex = 0;
165
   txBufferIndex = 0;
164
   txBufferLength = 0;
166
   txBufferLength = 0;
166
   // indicate that we are done transmitting
168
   // indicate that we are done transmitting
167
   transmitting = 0;
169
   transmitting = 0;
168
 
170
 
169
-  if (status == SUCCESS)
170
-    return 0; // success
171
-  else
172
-    return 4; // other error
171
+  return status == SUCCESS ? 0 : 4;
173
 }
172
 }
174
 
173
 
175
 // must be called after beginTransmission(address)
174
 // must be called after beginTransmission(address)
176
 size_t TwoWire::write(uint8_t data) {
175
 size_t TwoWire::write(uint8_t data) {
177
   if (transmitting) {
176
   if (transmitting) {
178
     // don't bother if buffer is full
177
     // don't bother if buffer is full
179
-    if (txBufferLength >= BUFFER_LENGTH) {
180
-      return 0;
181
-    }
178
+    if (txBufferLength >= BUFFER_LENGTH) return 0;
182
 
179
 
183
     // put byte in tx buffer
180
     // put byte in tx buffer
184
-    txBuffer[txBufferIndex] = data;
185
-    ++txBufferIndex;
181
+    txBuffer[txBufferIndex++] = data;
186
 
182
 
187
-    // update amount in buffer   
183
+    // update amount in buffer
188
     txBufferLength = txBufferIndex;
184
     txBufferLength = txBufferIndex;
189
   }
185
   }
190
 
186
 
195
 size_t TwoWire::write(const uint8_t *data, size_t quantity) {
191
 size_t TwoWire::write(const uint8_t *data, size_t quantity) {
196
   size_t sent = 0;
192
   size_t sent = 0;
197
   if (transmitting)
193
   if (transmitting)
198
-    for(sent = 0; sent < quantity; ++sent)
199
-      if (!write(data[sent]))
200
-        break;
194
+    for (sent = 0; sent < quantity; ++sent)
195
+      if (!write(data[sent])) break;
201
 
196
 
202
   return sent;
197
   return sent;
203
 }
198
 }
204
 
199
 
205
-// must be called after requestFrom(address, numBytes)
200
+// Must be called after requestFrom(address, numBytes)
206
 int TwoWire::available(void) {
201
 int TwoWire::available(void) {
207
   return rxBufferLength - rxBufferIndex;
202
   return rxBufferLength - rxBufferIndex;
208
 }
203
 }
209
 
204
 
210
-// must be called after requestFrom(address, numBytes)
205
+// Must be called after requestFrom(address, numBytes)
211
 int TwoWire::read(void) {
206
 int TwoWire::read(void) {
212
-  int value = -1;
213
-  
214
-  // get each successive byte on each call
215
-  if(rxBufferIndex < rxBufferLength) {
216
-    value = rxBuffer[rxBufferIndex];
217
-    ++rxBufferIndex;
218
-  }
219
-
220
-  return value;
207
+  return rxBufferIndex < rxBufferLength ? rxBuffer[rxBufferIndex++] : -1;
221
 }
208
 }
222
 
209
 
223
-// must be called after requestFrom(address, numBytes)
210
+// Must be called after requestFrom(address, numBytes)
224
 int TwoWire::peek(void) {
211
 int TwoWire::peek(void) {
225
-  int value = -1;
226
-  
227
-  if(rxBufferIndex < rxBufferLength){
228
-    value = rxBuffer[rxBufferIndex];
229
-  }
230
-
231
-  return value;
212
+  return rxBufferIndex < rxBufferLength ? rxBuffer[rxBufferIndex] : -1;
232
 }
213
 }
233
 
214
 
234
 // Preinstantiate Objects //////////////////////////////////////////////////////
215
 // Preinstantiate Objects //////////////////////////////////////////////////////

+ 3
- 3
Marlin/src/HAL/HAL_TEENSY35_36/HAL_spi_Teensy.cpp View File

91
   SPI.beginTransaction(spiConfig);
91
   SPI.beginTransaction(spiConfig);
92
   SPDR = token;
92
   SPDR = token;
93
   for (uint16_t i = 0; i < 512; i += 2) {
93
   for (uint16_t i = 0; i < 512; i += 2) {
94
-    while (!TEST(SPSR, SPIF)) { /* nada */ }; 
94
+    while (!TEST(SPSR, SPIF)) { /* nada */ };
95
     SPDR = buf[i];
95
     SPDR = buf[i];
96
-    while (!TEST(SPSR, SPIF)) { /* nada */ }; 
96
+    while (!TEST(SPSR, SPIF)) { /* nada */ };
97
     SPDR = buf[i + 1];
97
     SPDR = buf[i + 1];
98
   }
98
   }
99
-  while (!TEST(SPSR, SPIF)) { /* nada */ }; 
99
+  while (!TEST(SPSR, SPIF)) { /* nada */ };
100
   SPI.endTransaction();
100
   SPI.endTransaction();
101
 }
101
 }
102
 
102
 

+ 3
- 3
frameworks/CMSIS/LPC1768/lib/LiquidCrystal.h View File

60
   void init(uint8_t fourbitmode, pin_t rs, pin_t rw, pin_t enable,
60
   void init(uint8_t fourbitmode, pin_t rs, pin_t rw, pin_t enable,
61
             pin_t d0, pin_t d1, pin_t d2, pin_t d3,
61
             pin_t d0, pin_t d1, pin_t d2, pin_t d3,
62
             pin_t d4, pin_t d5, pin_t d6, pin_t d7);
62
             pin_t d4, pin_t d5, pin_t d6, pin_t d7);
63
-    
63
+
64
   void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS);
64
   void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS);
65
 
65
 
66
   void clear();
66
   void clear();
81
 
81
 
82
   void setRowOffsets(int row1, int row2, int row3, int row4);
82
   void setRowOffsets(int row1, int row2, int row3, int row4);
83
   void createChar(uint8_t, uint8_t[]);
83
   void createChar(uint8_t, uint8_t[]);
84
-  void setCursor(uint8_t, uint8_t); 
84
+  void setCursor(uint8_t, uint8_t);
85
   virtual size_t write(uint8_t);
85
   virtual size_t write(uint8_t);
86
   void command(uint8_t);
86
   void command(uint8_t);
87
-  
87
+
88
   using Print::write;
88
   using Print::write;
89
 private:
89
 private:
90
   void send(uint8_t, uint8_t);
90
   void send(uint8_t, uint8_t);

+ 26
- 26
frameworks/CMSIS/LPC1768/lib/Print.cpp View File

1
 /*
1
 /*
2
  Print.cpp - Base class that provides print() and println()
2
  Print.cpp - Base class that provides print() and println()
3
  Copyright (c) 2008 David A. Mellis.  All right reserved.
3
  Copyright (c) 2008 David A. Mellis.  All right reserved.
4
- 
4
+
5
  This library is free software; you can redistribute it and/or
5
  This library is free software; you can redistribute it and/or
6
  modify it under the terms of the GNU Lesser General Public
6
  modify it under the terms of the GNU Lesser General Public
7
  License as published by the Free Software Foundation; either
7
  License as published by the Free Software Foundation; either
8
  version 2.1 of the License, or (at your option) any later version.
8
  version 2.1 of the License, or (at your option) any later version.
9
- 
9
+
10
  This library is distributed in the hope that it will be useful,
10
  This library is distributed in the hope that it will be useful,
11
  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
  Lesser General Public License for more details.
13
  Lesser General Public License for more details.
14
- 
14
+
15
  You should have received a copy of the GNU Lesser General Public
15
  You should have received a copy of the GNU Lesser General Public
16
  License along with this library; if not, write to the Free Software
16
  License along with this library; if not, write to the Free Software
17
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
- 
18
+
19
  Modified 23 November 2006 by David A. Mellis
19
  Modified 23 November 2006 by David A. Mellis
20
  Modified 03 August 2015 by Chuck Todd
20
  Modified 03 August 2015 by Chuck Todd
21
  */
21
  */
37
 /* default implementation: may be overridden */
37
 /* default implementation: may be overridden */
38
 size_t Print::write(const uint8_t *buffer, size_t size)
38
 size_t Print::write(const uint8_t *buffer, size_t size)
39
 {
39
 {
40
-	
40
+
41
   size_t n = 0;
41
   size_t n = 0;
42
   while (size--) {
42
   while (size--) {
43
     if (write(*buffer++)) n++;
43
     if (write(*buffer++)) n++;
49
 
49
 
50
 size_t Print::print(const char str[])
50
 size_t Print::print(const char str[])
51
 {
51
 {
52
-	
52
+
53
 	//while(1);
53
 	//while(1);
54
   return write(str);
54
   return write(str);
55
 }
55
 }
195
   return write(str);
195
   return write(str);
196
 }
196
 }
197
 
197
 
198
-size_t Print::printFloat(double number, uint8_t digits) 
199
-{ 
198
+size_t Print::printFloat(double number, uint8_t digits)
199
+{
200
   size_t n = 0;
200
   size_t n = 0;
201
-  
201
+
202
   if (isnan(number)) return print("nan");
202
   if (isnan(number)) return print("nan");
203
   if (isinf(number)) return print("inf");
203
   if (isinf(number)) return print("inf");
204
   if (number > 4294967040.0) return print ("ovf");  // constant determined empirically
204
   if (number > 4294967040.0) return print ("ovf");  // constant determined empirically
205
   if (number <-4294967040.0) return print ("ovf");  // constant determined empirically
205
   if (number <-4294967040.0) return print ("ovf");  // constant determined empirically
206
-  
206
+
207
   // Handle negative numbers
207
   // Handle negative numbers
208
   if (number < 0.0)
208
   if (number < 0.0)
209
   {
209
   {
215
   double rounding = 0.5;
215
   double rounding = 0.5;
216
   for (uint8_t i=0; i<digits; ++i)
216
   for (uint8_t i=0; i<digits; ++i)
217
     rounding /= 10.0;
217
     rounding /= 10.0;
218
-  
218
+
219
   number += rounding;
219
   number += rounding;
220
 
220
 
221
   // Extract the integer part of the number and print it
221
   // Extract the integer part of the number and print it
225
 
225
 
226
   // Print the decimal point, but only if there are digits beyond
226
   // Print the decimal point, but only if there are digits beyond
227
   if (digits > 0) {
227
   if (digits > 0) {
228
-    n += print("."); 
228
+    n += print(".");
229
   }
229
   }
230
 
230
 
231
   // Extract digits from the remainder one at a time
231
   // Extract digits from the remainder one at a time
234
     remainder *= 10.0;
234
     remainder *= 10.0;
235
     int toPrint = int(remainder);
235
     int toPrint = int(remainder);
236
     n += print(toPrint);
236
     n += print(toPrint);
237
-    remainder -= toPrint; 
238
-  } 
239
-  
237
+    remainder -= toPrint;
238
+  }
239
+
240
   return n;
240
   return n;
241
 }
241
 }
242
 
242
 
243
 
243
 
244
-#if (PrintfEnable == 1) 
244
+#if (PrintfEnable == 1)
245
 size_t Print::printf(const char *argList, ...)
245
 size_t Print::printf(const char *argList, ...)
246
 {
246
 {
247
     const char *ptr;
247
     const char *ptr;
279
             else
279
             else
280
             {
280
             {
281
                 numOfDigits = 0xff;
281
                 numOfDigits = 0xff;
282
-            }                
282
+            }
283
 
283
 
284
 
284
 
285
             switch(ch)       /* Decode the type of the argument */
285
             switch(ch)       /* Decode the type of the argument */
297
             case 'D':
297
             case 'D':
298
                 num_s32 = va_arg(argp, int);
298
                 num_s32 = va_arg(argp, int);
299
                 print(num_s32, 10);
299
                 print(num_s32, 10);
300
-                break;  
300
+                break;
301
 
301
 
302
 
302
 
303
             case 'u':
303
             case 'u':
304
             case 'U':    /* Argument type is of integer, hence read 32bit unsigend data */
304
             case 'U':    /* Argument type is of integer, hence read 32bit unsigend data */
305
                 num_u32 = va_arg(argp, uint32_t);
305
                 num_u32 = va_arg(argp, uint32_t);
306
-                print(num_u32, 10);               
307
-                break;            
306
+                print(num_u32, 10);
307
+                break;
308
 
308
 
309
 
309
 
310
 
310
 
312
             case 'x':
312
             case 'x':
313
             case 'X':  /* Argument type is of hex, hence hexadecimal data from the argp */
313
             case 'X':  /* Argument type is of hex, hence hexadecimal data from the argp */
314
                 num_u32 = va_arg(argp, uint32_t);
314
                 num_u32 = va_arg(argp, uint32_t);
315
-                print(num_u32, 16);                 
315
+                print(num_u32, 16);
316
                 break;
316
                 break;
317
 
317
 
318
 
318
 
319
             case 'b':
319
             case 'b':
320
             case 'B':  /* Argument type is of binary,Read int and convert to binary */
320
             case 'B':  /* Argument type is of binary,Read int and convert to binary */
321
-                num_u32 = va_arg(argp, uint32_t); 
322
-                print(num_u32, 2);                 
321
+                num_u32 = va_arg(argp, uint32_t);
322
+                print(num_u32, 2);
323
                 break;
323
                 break;
324
 
324
 
325
 
325
 
326
 
326
 
327
             case 'F':
327
             case 'F':
328
             case 'f': /* Argument type is of float, hence read double data from the argp */
328
             case 'f': /* Argument type is of float, hence read double data from the argp */
329
-                floatNum_f32 = va_arg(argp, double);              
329
+                floatNum_f32 = va_arg(argp, double);
330
                 printFloat(floatNum_f32,10);
330
                 printFloat(floatNum_f32,10);
331
                 break;
331
                 break;
332
 
332
 
335
             case 'S':
335
             case 'S':
336
             case 's': /* Argument type is of string, hence get the pointer to sting passed */
336
             case 's': /* Argument type is of string, hence get the pointer to sting passed */
337
                 str = va_arg(argp, char *);
337
                 str = va_arg(argp, char *);
338
-                print(str);                
338
+                print(str);
339
                 break;
339
                 break;
340
 
340
 
341
 
341
 
356
 }
356
 }
357
 
357
 
358
 
358
 
359
-#endif    
359
+#endif

Loading…
Cancel
Save