Karl Andersson пре 6 година
родитељ
комит
428c54f2ad

+ 571
- 0
Marlin/src/HAL/HAL_STM32F4/EEPROM_Emul/eeprom_emul.cpp Прегледај датотеку

@@ -0,0 +1,571 @@
1
+/**
2
+  ******************************************************************************
3
+  * @file    EEPROM/EEPROM_Emulation/src/eeprom.c
4
+  * @author  MCD Application Team
5
+  * @version V1.2.6
6
+  * @date    04-November-2016
7
+  * @brief   This file provides all the EEPROM emulation firmware functions.
8
+  ******************************************************************************
9
+  * @attention
10
+  *
11
+  * <h2><center>&copy; Copyright © 2016 STMicroelectronics International N.V.
12
+  * All rights reserved.</center></h2>
13
+  *
14
+  * Redistribution and use in source and binary forms, with or without
15
+  * modification, are permitted, provided that the following conditions are met:
16
+  *
17
+  * 1. Redistribution of source code must retain the above copyright notice,
18
+  *    this list of conditions and the following disclaimer.
19
+  * 2. Redistributions in binary form must reproduce the above copyright notice,
20
+  *    this list of conditions and the following disclaimer in the documentation
21
+  *    and/or other materials provided with the distribution.
22
+  * 3. Neither the name of STMicroelectronics nor the names of other
23
+  *    contributors to this software may be used to endorse or promote products
24
+  *    derived from this software without specific written permission.
25
+  * 4. This software, including modifications and/or derivative works of this
26
+  *    software, must execute solely and exclusively on microcontroller or
27
+  *    microprocessor devices manufactured by or for STMicroelectronics.
28
+  * 5. Redistribution and use of this software other than as permitted under
29
+  *    this license is void and will automatically terminate your rights under
30
+  *    this license.
31
+  *
32
+  * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
33
+  * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
34
+  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
35
+  * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
36
+  * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
37
+  * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
38
+  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39
+  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
40
+  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41
+  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42
+  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
43
+  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
+  *
45
+  ******************************************************************************
46
+  */
47
+/** @addtogroup EEPROM_Emulation
48
+  * @{
49
+  */
50
+#ifdef STM32F4
51
+
52
+/* Includes ------------------------------------------------------------------*/
53
+#include "eeprom_emul.h"
54
+
55
+/* Private typedef -----------------------------------------------------------*/
56
+/* Private define ------------------------------------------------------------*/
57
+/* Private macro -------------------------------------------------------------*/
58
+/* Private variables ---------------------------------------------------------*/
59
+
60
+/* Global variable used to store variable value in read sequence */
61
+uint16_t DataVar = 0;
62
+
63
+/* Virtual address defined by the user: 0xFFFF value is prohibited */
64
+uint16_t VirtAddVarTab[NB_OF_VAR];
65
+
66
+/* Private function prototypes -----------------------------------------------*/
67
+/* Private functions ---------------------------------------------------------*/
68
+static HAL_StatusTypeDef EE_Format(void);
69
+static uint16_t EE_FindValidPage(uint8_t Operation);
70
+static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data);
71
+static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data);
72
+static uint16_t EE_VerifyPageFullyErased(uint32_t Address);
73
+
74
+/**
75
+  * @brief  Restore the pages to a known good state in case of page's status
76
+  *   corruption after a power loss.
77
+  * @param  None.
78
+  * @retval - Flash error code: on write Flash error
79
+  *         - FLASH_COMPLETE: on success
80
+  */
81
+uint16_t EE_Initialise(void) {
82
+  uint16_t PageStatus0 = 6, PageStatus1 = 6;
83
+  uint16_t VarIdx = 0;
84
+  uint16_t EepromStatus = 0, ReadStatus = 0;
85
+  int16_t x = -1;
86
+  HAL_StatusTypeDef  FlashStatus;
87
+  uint32_t SectorError = 0;
88
+  FLASH_EraseInitTypeDef pEraseInit;
89
+
90
+
91
+  /* Get Page0 status */
92
+  PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS);
93
+  /* Get Page1 status */
94
+  PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
95
+
96
+  pEraseInit.TypeErase = TYPEERASE_SECTORS;
97
+  pEraseInit.Sector = PAGE0_ID;
98
+  pEraseInit.NbSectors = 1;
99
+  pEraseInit.VoltageRange = VOLTAGE_RANGE;
100
+
101
+  /* Check for invalid header states and repair if necessary */
102
+  switch (PageStatus0) {
103
+    case ERASED:
104
+      if (PageStatus1 == VALID_PAGE) { /* Page0 erased, Page1 valid */
105
+          /* Erase Page0 */
106
+        if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
107
+          FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
108
+          /* If erase operation was failed, a Flash error code is returned */
109
+          if (FlashStatus != HAL_OK) {
110
+            return FlashStatus;
111
+          }
112
+        }
113
+      }
114
+      else if (PageStatus1 == RECEIVE_DATA) { /* Page0 erased, Page1 receive */
115
+        /* Erase Page0 */
116
+        if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
117
+          FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
118
+          /* If erase operation was failed, a Flash error code is returned */
119
+          if (FlashStatus != HAL_OK) return FlashStatus;
120
+        }
121
+        /* Mark Page1 as valid */
122
+        FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE1_BASE_ADDRESS, VALID_PAGE);
123
+        /* If program operation was failed, a Flash error code is returned */
124
+        if (FlashStatus != HAL_OK) return FlashStatus;
125
+      }
126
+      else { /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */
127
+        /* Erase both Page0 and Page1 and set Page0 as valid page */
128
+        FlashStatus = EE_Format();
129
+        /* If erase/program operation was failed, a Flash error code is returned */
130
+        if (FlashStatus != HAL_OK) return FlashStatus;
131
+      }
132
+      break;
133
+
134
+    case RECEIVE_DATA:
135
+      if (PageStatus1 == VALID_PAGE) { /* Page0 receive, Page1 valid */
136
+        /* Transfer data from Page1 to Page0 */
137
+        for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) {
138
+          if (( *(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
139
+            x = VarIdx;
140
+          if (VarIdx != x) {
141
+            /* Read the last variables' updates */
142
+            ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
143
+            /* In case variable corresponding to the virtual address was found */
144
+            if (ReadStatus != 0x1) {
145
+              /* Transfer the variable to the Page0 */
146
+              EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
147
+              /* If program operation was failed, a Flash error code is returned */
148
+              if (EepromStatus != HAL_OK) return EepromStatus;
149
+            }
150
+          }
151
+        }
152
+        /* Mark Page0 as valid */
153
+        FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
154
+        /* If program operation was failed, a Flash error code is returned */
155
+        if (FlashStatus != HAL_OK) return FlashStatus;
156
+        pEraseInit.Sector = PAGE1_ID;
157
+        pEraseInit.NbSectors = 1;
158
+        pEraseInit.VoltageRange = VOLTAGE_RANGE;
159
+        /* Erase Page1 */
160
+        if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
161
+          FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
162
+          /* If erase operation was failed, a Flash error code is returned */
163
+          if (FlashStatus != HAL_OK) return FlashStatus;
164
+        }
165
+      }
166
+      else if (PageStatus1 == ERASED) { /* Page0 receive, Page1 erased */
167
+        pEraseInit.Sector = PAGE1_ID;
168
+        pEraseInit.NbSectors = 1;
169
+        pEraseInit.VoltageRange = VOLTAGE_RANGE;
170
+        /* Erase Page1 */
171
+        if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
172
+          FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
173
+          /* If erase operation was failed, a Flash error code is returned */
174
+          if (FlashStatus != HAL_OK) return FlashStatus;
175
+        }
176
+        /* Mark Page0 as valid */
177
+        FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
178
+        /* If program operation was failed, a Flash error code is returned */
179
+        if (FlashStatus != HAL_OK) return FlashStatus;
180
+      }
181
+      else { /* Invalid state -> format eeprom */
182
+        /* Erase both Page0 and Page1 and set Page0 as valid page */
183
+        FlashStatus = EE_Format();
184
+        /* If erase/program operation was failed, a Flash error code is returned */
185
+        if (FlashStatus != HAL_OK) return FlashStatus;
186
+      }
187
+      break;
188
+
189
+    case VALID_PAGE:
190
+      if (PageStatus1 == VALID_PAGE) { /* Invalid state -> format eeprom */
191
+        /* Erase both Page0 and Page1 and set Page0 as valid page */
192
+        FlashStatus = EE_Format();
193
+        /* If erase/program operation was failed, a Flash error code is returned */
194
+        if (FlashStatus != HAL_OK) return FlashStatus;
195
+      }
196
+      else if (PageStatus1 == ERASED) { /* Page0 valid, Page1 erased */
197
+        pEraseInit.Sector = PAGE1_ID;
198
+        pEraseInit.NbSectors = 1;
199
+        pEraseInit.VoltageRange = VOLTAGE_RANGE;
200
+        /* Erase Page1 */
201
+        if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
202
+          FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
203
+          /* If erase operation was failed, a Flash error code is returned */
204
+          if (FlashStatus != HAL_OK) return FlashStatus;
205
+        }
206
+      }
207
+      else { /* Page0 valid, Page1 receive */
208
+        /* Transfer data from Page0 to Page1 */
209
+        for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) {
210
+          if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
211
+            x = VarIdx;
212
+
213
+          if (VarIdx != x) {
214
+            /* Read the last variables' updates */
215
+            ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
216
+            /* In case variable corresponding to the virtual address was found */
217
+            if (ReadStatus != 0x1) {
218
+              /* Transfer the variable to the Page1 */
219
+              EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
220
+              /* If program operation was failed, a Flash error code is returned */
221
+              if (EepromStatus != HAL_OK) return EepromStatus;
222
+            }
223
+          }
224
+        }
225
+        /* Mark Page1 as valid */
226
+        FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE1_BASE_ADDRESS, VALID_PAGE);
227
+        /* If program operation was failed, a Flash error code is returned */
228
+        if (FlashStatus != HAL_OK) return FlashStatus;
229
+        pEraseInit.Sector = PAGE0_ID;
230
+        pEraseInit.NbSectors = 1;
231
+        pEraseInit.VoltageRange = VOLTAGE_RANGE;
232
+        /* Erase Page0 */
233
+        if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
234
+          FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
235
+          /* If erase operation was failed, a Flash error code is returned */
236
+          if (FlashStatus != HAL_OK) return FlashStatus;
237
+        }
238
+      }
239
+      break;
240
+
241
+    default:  /* Any other state -> format eeprom */
242
+      /* Erase both Page0 and Page1 and set Page0 as valid page */
243
+      FlashStatus = EE_Format();
244
+      /* If erase/program operation was failed, a Flash error code is returned */
245
+      if (FlashStatus != HAL_OK) return FlashStatus;
246
+      break;
247
+  }
248
+
249
+  return HAL_OK;
250
+}
251
+
252
+/**
253
+ * @brief  Verify if specified page is fully erased.
254
+ * @param  Address: page address
255
+ *   This parameter can be one of the following values:
256
+ *     @arg PAGE0_BASE_ADDRESS: Page0 base address
257
+ *     @arg PAGE1_BASE_ADDRESS: Page1 base address
258
+ * @retval page fully erased status:
259
+ *           - 0: if Page not erased
260
+ *           - 1: if Page erased
261
+ */
262
+uint16_t EE_VerifyPageFullyErased(uint32_t Address) {
263
+  uint32_t ReadStatus = 1;
264
+  uint16_t AddressValue = 0x5555;
265
+  /* Check each active page address starting from end */
266
+  while (Address <= PAGE0_END_ADDRESS) {
267
+    /* Get the current location content to be compared with virtual address */
268
+    AddressValue = (*(__IO uint16_t*)Address);
269
+    /* Compare the read address with the virtual address */
270
+    if (AddressValue != ERASED) {
271
+      /* In case variable value is read, reset ReadStatus flag */
272
+      ReadStatus = 0;
273
+      break;
274
+    }
275
+    /* Next address location */
276
+    Address += 4;
277
+  }
278
+  /* Return ReadStatus value: (0: Page not erased, 1: Sector erased) */
279
+  return ReadStatus;
280
+}
281
+
282
+/**
283
+ * @brief  Returns the last stored variable data, if found, which correspond to
284
+ *   the passed virtual address
285
+ * @param  VirtAddress: Variable virtual address
286
+ * @param  Data: Global variable contains the read variable value
287
+ * @retval Success or error status:
288
+ *           - 0: if variable was found
289
+ *           - 1: if the variable was not found
290
+ *           - NO_VALID_PAGE: if no valid page was found.
291
+ */
292
+uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data) {
293
+  uint16_t ValidPage = PAGE0;
294
+  uint16_t AddressValue = 0x5555, ReadStatus = 1;
295
+  uint32_t Address = EEPROM_START_ADDRESS, PageStartAddress = EEPROM_START_ADDRESS;
296
+
297
+  /* Get active Page for read operation */
298
+  ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
299
+
300
+  /* Check if there is no valid page */
301
+  if (ValidPage == NO_VALID_PAGE) return NO_VALID_PAGE;
302
+
303
+  /* Get the valid Page start Address */
304
+  PageStartAddress = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE));
305
+
306
+  /* Get the valid Page end Address */
307
+  Address = (uint32_t)((EEPROM_START_ADDRESS - 2) + (uint32_t)((1 + ValidPage) * PAGE_SIZE));
308
+
309
+  /* Check each active page address starting from end */
310
+  while (Address > (PageStartAddress + 2)) {
311
+    /* Get the current location content to be compared with virtual address */
312
+    AddressValue = (*(__IO uint16_t*)Address);
313
+
314
+    /* Compare the read address with the virtual address */
315
+    if (AddressValue == VirtAddress) {
316
+      /* Get content of Address-2 which is variable value */
317
+      *Data = (*(__IO uint16_t*)(Address - 2));
318
+      /* In case variable value is read, reset ReadStatus flag */
319
+      ReadStatus = 0;
320
+      break;
321
+    }
322
+    else /* Next address location */
323
+      Address -= 4;
324
+  }
325
+  /* Return ReadStatus value: (0: variable exist, 1: variable doesn't exist) */
326
+  return ReadStatus;
327
+}
328
+
329
+/**
330
+ * @brief  Writes/upadtes variable data in EEPROM.
331
+ * @param  VirtAddress: Variable virtual address
332
+ * @param  Data: 16 bit data to be written
333
+ * @retval Success or error status:
334
+ *           - FLASH_COMPLETE: on success
335
+ *           - PAGE_FULL: if valid page is full
336
+ *           - NO_VALID_PAGE: if no valid page was found
337
+ *           - Flash error code: on write Flash error
338
+ */
339
+uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data) {
340
+  /* Write the variable virtual address and value in the EEPROM */
341
+  uint16_t Status = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
342
+
343
+  /* In case the EEPROM active page is full */
344
+  if (Status == PAGE_FULL) /* Perform Page transfer */
345
+    Status = EE_PageTransfer(VirtAddress, Data);
346
+
347
+  /* Return last operation status */
348
+  return Status;
349
+}
350
+
351
+/**
352
+ * @brief  Erases PAGE and PAGE1 and writes VALID_PAGE header to PAGE
353
+ * @param  None
354
+ * @retval Status of the last operation (Flash write or erase) done during
355
+ *         EEPROM formating
356
+ */
357
+static HAL_StatusTypeDef EE_Format(void) {
358
+  HAL_StatusTypeDef FlashStatus = HAL_OK;
359
+  uint32_t SectorError = 0;
360
+  FLASH_EraseInitTypeDef pEraseInit;
361
+
362
+  pEraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
363
+  pEraseInit.Sector = PAGE0_ID;
364
+  pEraseInit.NbSectors = 1;
365
+  pEraseInit.VoltageRange = VOLTAGE_RANGE;
366
+  /* Erase Page0 */
367
+  if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
368
+    FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
369
+    /* If erase operation was failed, a Flash error code is returned */
370
+    if (FlashStatus != HAL_OK) return FlashStatus;
371
+  }
372
+  /* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */
373
+  FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
374
+  /* If program operation was failed, a Flash error code is returned */
375
+  if (FlashStatus != HAL_OK) return FlashStatus;
376
+
377
+  pEraseInit.Sector = PAGE1_ID;
378
+  /* Erase Page1 */
379
+  if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
380
+    FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
381
+    /* If erase operation was failed, a Flash error code is returned */
382
+    if (FlashStatus != HAL_OK) return FlashStatus;
383
+  }
384
+
385
+  return HAL_OK;
386
+}
387
+
388
+/**
389
+ * @brief  Find valid Page for write or read operation
390
+ * @param  Operation: operation to achieve on the valid page.
391
+ *   This parameter can be one of the following values:
392
+ *     @arg READ_FROM_VALID_PAGE: read operation from valid page
393
+ *     @arg WRITE_IN_VALID_PAGE: write operation from valid page
394
+ * @retval Valid page number (PAGE or PAGE1) or NO_VALID_PAGE in case
395
+ *   of no valid page was found
396
+ */
397
+static uint16_t EE_FindValidPage(uint8_t Operation) {
398
+  uint16_t PageStatus0 = 6, PageStatus1 = 6;
399
+
400
+  /* Get Page0 actual status */
401
+  PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS);
402
+
403
+  /* Get Page1 actual status */
404
+  PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
405
+
406
+  /* Write or read operation */
407
+  switch (Operation) {
408
+    case WRITE_IN_VALID_PAGE:   /* ---- Write operation ---- */
409
+      if (PageStatus1 == VALID_PAGE) {
410
+        /* Page0 receiving data */
411
+        if (PageStatus0 == RECEIVE_DATA) return PAGE0;         /* Page0 valid */
412
+        else                             return PAGE1;         /* Page1 valid */
413
+      }
414
+      else if (PageStatus0 == VALID_PAGE) {
415
+        /* Page1 receiving data */
416
+        if (PageStatus1 == RECEIVE_DATA) return PAGE1;         /* Page1 valid */
417
+        else                             return PAGE0;         /* Page0 valid */
418
+      }
419
+      else
420
+        return NO_VALID_PAGE;   /* No valid Page */
421
+
422
+    case READ_FROM_VALID_PAGE:  /* ---- Read operation ---- */
423
+      if (PageStatus0 == VALID_PAGE)
424
+        return PAGE0;           /* Page0 valid */
425
+      else if (PageStatus1 == VALID_PAGE)
426
+        return PAGE1;           /* Page1 valid */
427
+      else
428
+        return NO_VALID_PAGE;   /* No valid Page */
429
+
430
+    default:
431
+      return PAGE0;             /* Page0 valid */
432
+  }
433
+}
434
+
435
+/**
436
+ * @brief  Verify if active page is full and Writes variable in EEPROM.
437
+ * @param  VirtAddress: 16 bit virtual address of the variable
438
+ * @param  Data: 16 bit data to be written as variable value
439
+ * @retval Success or error status:
440
+ *           - FLASH_COMPLETE: on success
441
+ *           - PAGE_FULL: if valid page is full
442
+ *           - NO_VALID_PAGE: if no valid page was found
443
+ *           - Flash error code: on write Flash error
444
+ */
445
+static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data) {
446
+  HAL_StatusTypeDef FlashStatus = HAL_OK;
447
+  uint16_t ValidPage = PAGE0;
448
+  uint32_t Address = EEPROM_START_ADDRESS, PageEndAddress = EEPROM_START_ADDRESS+PAGE_SIZE;
449
+
450
+  /* Get valid Page for write operation */
451
+  ValidPage = EE_FindValidPage(WRITE_IN_VALID_PAGE);
452
+
453
+  /* Check if there is no valid page */
454
+  if (ValidPage == NO_VALID_PAGE) return NO_VALID_PAGE;
455
+
456
+  /* Get the valid Page start Address */
457
+  Address = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE));
458
+
459
+  /* Get the valid Page end Address */
460
+  PageEndAddress = (uint32_t)((EEPROM_START_ADDRESS - 1) + (uint32_t)((ValidPage + 1) * PAGE_SIZE));
461
+
462
+  /* Check each active page address starting from begining */
463
+  while (Address < PageEndAddress) {
464
+    /* Verify if Address and Address+2 contents are 0xFFFFFFFF */
465
+    if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF) {
466
+      /* Set variable data */
467
+      FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, Address, Data);
468
+      /* If program operation was failed, a Flash error code is returned */
469
+      if (FlashStatus != HAL_OK) return FlashStatus;
470
+      /* Set variable virtual address */
471
+      FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, Address + 2, VirtAddress);
472
+      /* Return program operation status */
473
+      return FlashStatus;
474
+    }
475
+    else /* Next address location */
476
+      Address += 4;
477
+  }
478
+
479
+  /* Return PAGE_FULL in case the valid page is full */
480
+  return PAGE_FULL;
481
+}
482
+
483
+/**
484
+ * @brief  Transfers last updated variables data from the full Page to
485
+ *   an empty one.
486
+ * @param  VirtAddress: 16 bit virtual address of the variable
487
+ * @param  Data: 16 bit data to be written as variable value
488
+ * @retval Success or error status:
489
+ *           - FLASH_COMPLETE: on success
490
+ *           - PAGE_FULL: if valid page is full
491
+ *           - NO_VALID_PAGE: if no valid page was found
492
+ *           - Flash error code: on write Flash error
493
+ */
494
+static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data) {
495
+  HAL_StatusTypeDef FlashStatus = HAL_OK;
496
+  uint32_t NewPageAddress = EEPROM_START_ADDRESS;
497
+  uint16_t OldPageId=0;
498
+  uint16_t ValidPage = PAGE0, VarIdx = 0;
499
+  uint16_t EepromStatus = 0, ReadStatus = 0;
500
+  uint32_t SectorError = 0;
501
+  FLASH_EraseInitTypeDef pEraseInit;
502
+
503
+  /* Get active Page for read operation */
504
+  ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
505
+
506
+  if (ValidPage == PAGE1) {     /* Page1 valid */
507
+    /* New page address where variable will be moved to */
508
+    NewPageAddress = PAGE0_BASE_ADDRESS;
509
+    /* Old page ID where variable will be taken from */
510
+    OldPageId = PAGE1_ID;
511
+  }
512
+  else if (ValidPage == PAGE0) { /* Page0 valid */
513
+    /* New page address  where variable will be moved to */
514
+    NewPageAddress = PAGE1_BASE_ADDRESS;
515
+    /* Old page ID where variable will be taken from */
516
+    OldPageId = PAGE0_ID;
517
+  }
518
+  else
519
+    return NO_VALID_PAGE;       /* No valid Page */
520
+
521
+  /* Set the new Page status to RECEIVE_DATA status */
522
+  FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, NewPageAddress, RECEIVE_DATA);
523
+  /* If program operation was failed, a Flash error code is returned */
524
+  if (FlashStatus != HAL_OK) return FlashStatus;
525
+
526
+  /* Write the variable passed as parameter in the new active page */
527
+  EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
528
+  /* If program operation was failed, a Flash error code is returned */
529
+  if (EepromStatus != HAL_OK) return EepromStatus;
530
+
531
+  /* Transfer process: transfer variables from old to the new active page */
532
+  for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) {
533
+    if (VirtAddVarTab[VarIdx] != VirtAddress) { /* Check each variable except the one passed as parameter */
534
+      /* Read the other last variable updates */
535
+      ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
536
+      /* In case variable corresponding to the virtual address was found */
537
+      if (ReadStatus != 0x1) {
538
+        /* Transfer the variable to the new active page */
539
+        EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
540
+        /* If program operation was failed, a Flash error code is returned */
541
+        if (EepromStatus != HAL_OK) return EepromStatus;
542
+      }
543
+    }
544
+  }
545
+
546
+  pEraseInit.TypeErase = TYPEERASE_SECTORS;
547
+  pEraseInit.Sector = OldPageId;
548
+  pEraseInit.NbSectors = 1;
549
+  pEraseInit.VoltageRange = VOLTAGE_RANGE;
550
+
551
+  /* Erase the old Page: Set old Page status to ERASED status */
552
+  FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
553
+  /* If erase operation was failed, a Flash error code is returned */
554
+  if (FlashStatus != HAL_OK) return FlashStatus;
555
+
556
+  /* Set new Page status to VALID_PAGE status */
557
+  FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, NewPageAddress, VALID_PAGE);
558
+  /* If program operation was failed, a Flash error code is returned */
559
+  if (FlashStatus != HAL_OK) return FlashStatus;
560
+
561
+  /* Return last operation flash status */
562
+  return FlashStatus;
563
+}
564
+
565
+#endif // STM32F4
566
+
567
+/**
568
+ * @}
569
+ */
570
+
571
+/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

+ 117
- 0
Marlin/src/HAL/HAL_STM32F4/EEPROM_Emul/eeprom_emul.h Прегледај датотеку

@@ -0,0 +1,117 @@
1
+/**
2
+  ******************************************************************************
3
+  * @file    EEPROM/EEPROM_Emulation/inc/eeprom.h
4
+  * @author  MCD Application Team
5
+  * @version V1.2.6
6
+  * @date    04-November-2016
7
+  * @brief   This file contains all the functions prototypes for the EEPROM
8
+  *          emulation firmware library.
9
+  ******************************************************************************
10
+  * @attention
11
+  *
12
+  * <h2><center>&copy; Copyright � 2016 STMicroelectronics International N.V.
13
+  * All rights reserved.</center></h2>
14
+  *
15
+  * Redistribution and use in source and binary forms, with or without
16
+  * modification, are permitted, provided that the following conditions are met:
17
+  *
18
+  * 1. Redistribution of source code must retain the above copyright notice,
19
+  *    this list of conditions and the following disclaimer.
20
+  * 2. Redistributions in binary form must reproduce the above copyright notice,
21
+  *    this list of conditions and the following disclaimer in the documentation
22
+  *    and/or other materials provided with the distribution.
23
+  * 3. Neither the name of STMicroelectronics nor the names of other
24
+  *    contributors to this software may be used to endorse or promote products
25
+  *    derived from this software without specific written permission.
26
+  * 4. This software, including modifications and/or derivative works of this
27
+  *    software, must execute solely and exclusively on microcontroller or
28
+  *    microprocessor devices manufactured by or for STMicroelectronics.
29
+  * 5. Redistribution and use of this software other than as permitted under
30
+  *    this license is void and will automatically terminate your rights under
31
+  *    this license.
32
+  *
33
+  * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
34
+  * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
35
+  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
36
+  * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
37
+  * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
38
+  * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
39
+  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40
+  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
41
+  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42
+  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43
+  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
44
+  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
+  *
46
+  ******************************************************************************
47
+  */
48
+
49
+/* Define to prevent recursive inclusion -------------------------------------*/
50
+#ifndef __EEEPROM_EMUL_H
51
+#define __EEEPROM_EMUL_H
52
+
53
+// --------------------------------------------------------------------------
54
+// Includes
55
+// --------------------------------------------------------------------------
56
+#include "../../../inc/MarlinConfig.h"
57
+#include "../HAL.h"
58
+
59
+/* Exported constants --------------------------------------------------------*/
60
+/* EEPROM emulation firmware error codes */
61
+#define EE_OK      (uint32_t)HAL_OK
62
+#define EE_ERROR   (uint32_t)HAL_ERROR
63
+#define EE_BUSY    (uint32_t)HAL_BUSY
64
+#define EE_TIMEOUT (uint32_t)HAL_TIMEOUT
65
+
66
+/* Define the size of the sectors to be used */
67
+#define PAGE_SIZE               (uint32_t)0x4000  /* Page size = 16KByte */
68
+
69
+/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
70
+   be done by word  */
71
+#define VOLTAGE_RANGE           (uint8_t)VOLTAGE_RANGE_3
72
+
73
+/* EEPROM start address in Flash */
74
+#define EEPROM_START_ADDRESS  ((uint32_t)0x08078000) /* EEPROM emulation start address:
75
+                                                  after 480KByte of used Flash memory */
76
+
77
+/* Pages 0 and 1 base and end addresses */
78
+#define PAGE0_BASE_ADDRESS    ((uint32_t)(EEPROM_START_ADDRESS + 0x0000))
79
+#define PAGE0_END_ADDRESS     ((uint32_t)(EEPROM_START_ADDRESS + (PAGE_SIZE - 1)))
80
+#define PAGE0_ID               FLASH_SECTOR_1
81
+
82
+#define PAGE1_BASE_ADDRESS    ((uint32_t)(EEPROM_START_ADDRESS + 0x4000))
83
+#define PAGE1_END_ADDRESS     ((uint32_t)(EEPROM_START_ADDRESS + (2 * PAGE_SIZE - 1)))
84
+#define PAGE1_ID               FLASH_SECTOR_2
85
+
86
+/* Used Flash pages for EEPROM emulation */
87
+#define PAGE0                 ((uint16_t)0x0000)
88
+#define PAGE1                 ((uint16_t)0x0001) /* Page nb between PAGE0_BASE_ADDRESS & PAGE1_BASE_ADDRESS*/
89
+
90
+/* No valid page define */
91
+#define NO_VALID_PAGE         ((uint16_t)0x00AB)
92
+
93
+/* Page status definitions */
94
+#define ERASED                ((uint16_t)0xFFFF)     /* Page is empty */
95
+#define RECEIVE_DATA          ((uint16_t)0xEEEE)     /* Page is marked to receive data */
96
+#define VALID_PAGE            ((uint16_t)0x0000)     /* Page containing valid data */
97
+
98
+/* Valid pages in read and write defines */
99
+#define READ_FROM_VALID_PAGE  ((uint8_t)0x00)
100
+#define WRITE_IN_VALID_PAGE   ((uint8_t)0x01)
101
+
102
+/* Page full define */
103
+#define PAGE_FULL             ((uint8_t)0x80)
104
+
105
+/* Variables' number */
106
+#define NB_OF_VAR             ((uint16_t)4096)
107
+
108
+/* Exported types ------------------------------------------------------------*/
109
+/* Exported macro ------------------------------------------------------------*/
110
+/* Exported functions ------------------------------------------------------- */
111
+uint16_t EE_Initialise(void);
112
+uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data);
113
+uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data);
114
+
115
+#endif /* __EEEPROM_H */
116
+
117
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 143
- 0
Marlin/src/HAL/HAL_STM32F4/EmulatedEeprom.cpp Прегледај датотеку

@@ -0,0 +1,143 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License
16
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
+ *
18
+ */
19
+
20
+#ifdef STM32F4
21
+
22
+/**
23
+ * Description: functions for I2C connected external EEPROM.
24
+ * Not platform dependent.
25
+ */
26
+
27
+#include "../../inc/MarlinConfig.h"
28
+
29
+#if ENABLED(EEPROM_SETTINGS) && DISABLED(I2C_EEPROM) && DISABLED(SPI_EEPROM)
30
+
31
+// --------------------------------------------------------------------------
32
+// Includes
33
+// --------------------------------------------------------------------------
34
+
35
+#include "HAL.h"
36
+#include "EEPROM_Emul/eeprom_emul.h"
37
+
38
+
39
+// --------------------------------------------------------------------------
40
+// Externals
41
+// --------------------------------------------------------------------------
42
+
43
+// --------------------------------------------------------------------------
44
+// Local defines
45
+// --------------------------------------------------------------------------
46
+
47
+// --------------------------------------------------------------------------
48
+// Types
49
+// --------------------------------------------------------------------------
50
+
51
+// --------------------------------------------------------------------------
52
+// Variables
53
+// --------------------------------------------------------------------------
54
+
55
+// --------------------------------------------------------------------------
56
+// Public Variables
57
+// --------------------------------------------------------------------------
58
+
59
+// --------------------------------------------------------------------------
60
+// Private Variables
61
+// --------------------------------------------------------------------------
62
+static bool eeprom_initialised = false;
63
+// --------------------------------------------------------------------------
64
+// Function prototypes
65
+// --------------------------------------------------------------------------
66
+
67
+// --------------------------------------------------------------------------
68
+// Private functions
69
+// --------------------------------------------------------------------------
70
+
71
+// --------------------------------------------------------------------------
72
+// Public functions
73
+// --------------------------------------------------------------------------
74
+
75
+// FLASH_FLAG_PGSERR (Programming Sequence Error) was renamed to
76
+// FLASH_FLAG_ERSERR (Erasing Sequence Error) in STM32F4
77
+// #define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR
78
+
79
+// --------------------------------------------------------------------------
80
+// EEPROM
81
+// --------------------------------------------------------------------------
82
+
83
+
84
+void eeprom_init() {
85
+  if (!eeprom_initialised) {
86
+    HAL_FLASH_Unlock();
87
+
88
+    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
89
+
90
+    /* EEPROM Init */
91
+    if (EE_Initialise() != EE_OK)
92
+      for (;;) HAL_Delay(1); // Spin forever until watchdog reset
93
+
94
+    HAL_FLASH_Lock();
95
+    eeprom_initialised = true;
96
+  }
97
+}
98
+
99
+void eeprom_write_byte(unsigned char *pos, unsigned char value) {
100
+  uint16_t eeprom_address = (unsigned) pos;
101
+
102
+  eeprom_init();
103
+
104
+  HAL_FLASH_Unlock();
105
+  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
106
+
107
+  if (EE_WriteVariable(eeprom_address, (uint16_t) value) != EE_OK)
108
+      for (;;) HAL_Delay(1); // Spin forever until watchdog reset
109
+
110
+  HAL_FLASH_Lock();
111
+}
112
+
113
+unsigned char eeprom_read_byte(unsigned char *pos) {
114
+  uint16_t data = 0xFF;
115
+  uint16_t eeprom_address = (unsigned)pos;
116
+
117
+  eeprom_init();
118
+
119
+  if (EE_ReadVariable(eeprom_address, &data) != EE_OK) {
120
+    return (unsigned char)data;
121
+  }
122
+  return (unsigned char)data;
123
+}
124
+
125
+void eeprom_read_block(void *__dst, const void *__src, size_t __n) {
126
+  uint16_t data = 0xFF;
127
+  uint16_t eeprom_address = (unsigned) __src;
128
+
129
+  eeprom_init();
130
+
131
+  for (uint8_t c = 0; c < __n; c++) {
132
+    EE_ReadVariable(eeprom_address+c, &data);
133
+    *((uint8_t*)__dst + c) = data;
134
+  }
135
+}
136
+
137
+void eeprom_update_block(const void *__src, void *__dst, size_t __n) {
138
+
139
+}
140
+
141
+#endif // ENABLED(EEPROM_SETTINGS) && DISABLED(I2C_EEPROM) && DISABLED(SPI_EEPROM)
142
+#endif // STM32F4
143
+

+ 140
- 0
Marlin/src/HAL/HAL_STM32F4/HAL.cpp Прегледај датотеку

@@ -0,0 +1,140 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ *
4
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
5
+ * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
6
+ * Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
7
+ * Copyright (c) 2017 Victor Perez
8
+ *
9
+ * This program is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+
25
+#ifdef STM32F4
26
+
27
+// --------------------------------------------------------------------------
28
+// Includes
29
+// --------------------------------------------------------------------------
30
+
31
+#include "HAL.h"
32
+
33
+//#include <Wire.h>
34
+
35
+// --------------------------------------------------------------------------
36
+// Externals
37
+// --------------------------------------------------------------------------
38
+
39
+// --------------------------------------------------------------------------
40
+// Local defines
41
+// --------------------------------------------------------------------------
42
+
43
+// --------------------------------------------------------------------------
44
+// Types
45
+// --------------------------------------------------------------------------
46
+
47
+// --------------------------------------------------------------------------
48
+// Variables
49
+// --------------------------------------------------------------------------
50
+
51
+// --------------------------------------------------------------------------
52
+// Public Variables
53
+// --------------------------------------------------------------------------
54
+
55
+uint16_t HAL_adc_result;
56
+
57
+// --------------------------------------------------------------------------
58
+// Private Variables
59
+// --------------------------------------------------------------------------
60
+
61
+// --------------------------------------------------------------------------
62
+// Function prototypes
63
+// --------------------------------------------------------------------------
64
+
65
+// --------------------------------------------------------------------------
66
+// Private functions
67
+// --------------------------------------------------------------------------
68
+
69
+// --------------------------------------------------------------------------
70
+// Public functions
71
+// --------------------------------------------------------------------------
72
+
73
+/* VGPV Done with defines
74
+// disable interrupts
75
+void cli(void) { noInterrupts(); }
76
+
77
+// enable interrupts
78
+void sei(void) { interrupts(); }
79
+*/
80
+
81
+void HAL_clear_reset_source(void) { __HAL_RCC_CLEAR_RESET_FLAGS(); }
82
+
83
+uint8_t HAL_get_reset_source (void) {
84
+  if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET)
85
+    return RST_WATCHDOG;
86
+
87
+  if (__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST) != RESET)
88
+    return RST_SOFTWARE;
89
+
90
+  if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET)
91
+    return RST_EXTERNAL;
92
+
93
+  if (__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET)
94
+    return RST_POWER_ON;
95
+  return 0;
96
+}
97
+
98
+void _delay_ms(const int delay_ms) { delay(delay_ms); }
99
+
100
+extern "C" {
101
+  extern unsigned int _ebss; // end of bss section
102
+}
103
+
104
+// return free memory between end of heap (or end bss) and whatever is current
105
+
106
+/*
107
+#include "wirish/syscalls.c"
108
+//extern caddr_t _sbrk(int incr);
109
+#ifndef CONFIG_HEAP_END
110
+extern char _lm_heap_end;
111
+#define CONFIG_HEAP_END ((caddr_t)&_lm_heap_end)
112
+#endif
113
+
114
+extern "C" {
115
+  static int freeMemory() {
116
+    char top = 't';
117
+    return &top - reinterpret_cast<char*>(sbrk(0));
118
+  }
119
+  int freeMemory() {
120
+    int free_memory;
121
+    int heap_end = (int)_sbrk(0);
122
+    free_memory = ((int)&free_memory) - ((int)heap_end);
123
+    return free_memory;
124
+  }
125
+}
126
+*/
127
+
128
+// --------------------------------------------------------------------------
129
+// ADC
130
+// --------------------------------------------------------------------------
131
+
132
+void HAL_adc_start_conversion(const uint8_t adc_pin) {
133
+  HAL_adc_result = analogRead(adc_pin);
134
+}
135
+
136
+uint16_t HAL_adc_get_result(void) {
137
+  return HAL_adc_result;
138
+}
139
+
140
+#endif // STM32F4

+ 249
- 0
Marlin/src/HAL/HAL_STM32F4/HAL.h Прегледај датотеку

@@ -0,0 +1,249 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ *
4
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
5
+ * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
6
+ * Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
7
+ * Copyright (c) 2017 Victor Perez
8
+ *
9
+ * This program is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+
25
+
26
+#ifndef _HAL_STM32F4_H
27
+#define _HAL_STM32F4_H
28
+
29
+#define CPU_32_BIT
30
+#undef DEBUG_NONE
31
+
32
+#ifndef vsnprintf_P
33
+  #define vsnprintf_P vsnprintf
34
+#endif
35
+
36
+// --------------------------------------------------------------------------
37
+// Includes
38
+// --------------------------------------------------------------------------
39
+
40
+#include <stdint.h>
41
+
42
+#include "Arduino.h"
43
+
44
+#include "../math_32bit.h"
45
+#include "../HAL_SPI.h"
46
+#include "fastio_STM32F4.h"
47
+#include "watchdog_STM32F4.h"
48
+
49
+#include "HAL_timers_STM32F4.h"
50
+
51
+
52
+// --------------------------------------------------------------------------
53
+// Defines
54
+// --------------------------------------------------------------------------
55
+
56
+//Serial override
57
+//extern HalSerial usb_serial;
58
+
59
+#if SERIAL_PORT == 0
60
+  #error "Serial port 0 does not exist"
61
+#endif
62
+
63
+#if !WITHIN(SERIAL_PORT, -1, 6)
64
+  #error "SERIAL_PORT must be from -1 to 6"
65
+#endif
66
+#if SERIAL_PORT == -1
67
+  #define MYSERIAL0 SerialUSB
68
+#elif SERIAL_PORT == 1
69
+  #define MYSERIAL0 SerialUART1
70
+#elif SERIAL_PORT == 2
71
+  #define MYSERIAL0 SerialUART2
72
+#elif SERIAL_PORT == 3
73
+  #define MYSERIAL0 SerialUART3
74
+#elif SERIAL_PORT == 4
75
+  #define MYSERIAL0 SerialUART4
76
+#elif SERIAL_PORT == 5
77
+  #define MYSERIAL0 SerialUART5
78
+#elif SERIAL_PORT == 6
79
+  #define MYSERIAL0 SerialUART6
80
+#endif
81
+
82
+#ifdef SERIAL_PORT_2
83
+  #if SERIAL_PORT_2 == 0
84
+    #error "Serial port 0 does not exist"
85
+  #endif
86
+
87
+  #if !WITHIN(SERIAL_PORT_2, -1, 6)
88
+    #error "SERIAL_PORT_2 must be from -1 to 6"
89
+  #elif SERIAL_PORT_2 == SERIAL_PORT
90
+    #error "SERIAL_PORT_2 must be different than SERIAL_PORT"
91
+  #endif
92
+  #define NUM_SERIAL 2
93
+  #if SERIAL_PORT_2 == -1
94
+    #define MYSERIAL1 SerialUSB
95
+  #elif SERIAL_PORT_2 == 1
96
+    #define MYSERIAL1 SerialUART1
97
+  #elif SERIAL_PORT_2 == 2
98
+    #define MYSERIAL1 SerialUART2
99
+  #elif SERIAL_PORT_2 == 3
100
+    #define MYSERIAL1 SerialUART3
101
+  #elif SERIAL_PORT_2 == 4
102
+    #define MYSERIAL1 SerialUART4
103
+  #elif SERIAL_PORT_2 == 5
104
+    #define MYSERIAL1 SerialUART5
105
+  #elif SERIAL_PORT_2 == 6
106
+    #define MYSERIAL1 SerialUART6
107
+  #endif
108
+#else
109
+  #define NUM_SERIAL 1
110
+#endif
111
+
112
+#define _BV(b) (1 << (b))
113
+
114
+/**
115
+ * TODO: review this to return 1 for pins that are not analog input
116
+ */
117
+#ifndef analogInputToDigitalPin
118
+  #define analogInputToDigitalPin(p) (p)
119
+#endif
120
+
121
+#define CRITICAL_SECTION_START  noInterrupts();
122
+#define CRITICAL_SECTION_END    interrupts();
123
+
124
+// On AVR this is in math.h?
125
+#define square(x) ((x)*(x))
126
+
127
+#ifndef strncpy_P
128
+  #define strncpy_P(dest, src, num) strncpy((dest), (src), (num))
129
+#endif
130
+
131
+// Fix bug in pgm_read_ptr
132
+#undef pgm_read_ptr
133
+#define pgm_read_ptr(addr) (*(addr))
134
+
135
+#define RST_POWER_ON   1
136
+#define RST_EXTERNAL   2
137
+#define RST_BROWN_OUT  4
138
+#define RST_WATCHDOG   8
139
+#define RST_JTAG       16
140
+#define RST_SOFTWARE   32
141
+#define RST_BACKUP     64
142
+
143
+// --------------------------------------------------------------------------
144
+// Types
145
+// --------------------------------------------------------------------------
146
+
147
+typedef int8_t pin_t;
148
+
149
+#define HAL_SERVO_LIB libServo
150
+
151
+// --------------------------------------------------------------------------
152
+// Public Variables
153
+// --------------------------------------------------------------------------
154
+
155
+/** result of last ADC conversion */
156
+extern uint16_t HAL_adc_result;
157
+
158
+// --------------------------------------------------------------------------
159
+// Public functions
160
+// --------------------------------------------------------------------------
161
+
162
+// Disable interrupts
163
+#define cli() do {  DISABLE_TEMPERATURE_INTERRUPT(); DISABLE_STEPPER_DRIVER_INTERRUPT(); } while(0)
164
+
165
+// Enable interrupts
166
+#define sei() do {  ENABLE_TEMPERATURE_INTERRUPT(); ENABLE_STEPPER_DRIVER_INTERRUPT(); } while(0)
167
+
168
+// Memory related
169
+#define __bss_end __bss_end__
170
+
171
+/** clear reset reason */
172
+void HAL_clear_reset_source (void);
173
+
174
+/** reset reason */
175
+uint8_t HAL_get_reset_source (void);
176
+
177
+void _delay_ms(const int delay);
178
+
179
+/*
180
+extern "C" {
181
+  int freeMemory(void);
182
+}
183
+*/
184
+
185
+extern "C" char* _sbrk(int incr);
186
+/*
187
+static int freeMemory() {
188
+  volatile int top;
189
+  top = (int)((char*)&top - reinterpret_cast<char*>(_sbrk(0)));
190
+  return top;
191
+}
192
+*/
193
+static int freeMemory() {
194
+  volatile char top;
195
+  return &top - reinterpret_cast<char*>(_sbrk(0));
196
+}
197
+
198
+// SPI: Extended functions which take a channel number (hardware SPI only)
199
+/** Write single byte to specified SPI channel */
200
+void spiSend(uint32_t chan, byte b);
201
+/** Write buffer to specified SPI channel */
202
+void spiSend(uint32_t chan, const uint8_t* buf, size_t n);
203
+/** Read single byte from specified SPI channel */
204
+uint8_t spiRec(uint32_t chan);
205
+
206
+
207
+// EEPROM
208
+
209
+/**
210
+ * TODO: Write all this eeprom stuff. Can emulate eeprom in flash as last resort.
211
+ * Wire library should work for i2c eeproms.
212
+ */
213
+void eeprom_write_byte(unsigned char *pos, unsigned char value);
214
+unsigned char eeprom_read_byte(unsigned char *pos);
215
+void eeprom_read_block (void *__dst, const void *__src, size_t __n);
216
+void eeprom_update_block (const void *__src, void *__dst, size_t __n);
217
+
218
+// ADC
219
+
220
+#define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT)
221
+
222
+inline void HAL_adc_init(void) {}
223
+
224
+#define HAL_START_ADC(pin)  HAL_adc_start_conversion(pin)
225
+#define HAL_READ_ADC        HAL_adc_result
226
+
227
+void HAL_adc_start_conversion(const uint8_t adc_pin);
228
+
229
+uint16_t HAL_adc_get_result(void);
230
+
231
+/* Todo: Confirm none of this is needed.
232
+uint16_t HAL_getAdcReading(uint8_t chan);
233
+
234
+void HAL_startAdcConversion(uint8_t chan);
235
+uint8_t HAL_pinToAdcChannel(int pin);
236
+
237
+uint16_t HAL_getAdcFreerun(uint8_t chan, bool wait_for_conversion = false);
238
+//uint16_t HAL_getAdcSuperSample(uint8_t chan);
239
+
240
+void HAL_enable_AdcFreerun(void);
241
+//void HAL_disable_AdcFreerun(uint8_t chan);
242
+
243
+*/
244
+
245
+#define GET_PIN_MAP_PIN(index) index
246
+#define GET_PIN_MAP_INDEX(pin) pin
247
+#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
248
+
249
+#endif // _HAL_STM32F4_H

+ 53
- 0
Marlin/src/HAL/HAL_STM32F4/HAL_Servo_STM32F4.cpp Прегледај датотеку

@@ -0,0 +1,53 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ * Copyright (C) 2017 Victor Perez
8
+ *
9
+ * This program is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+#ifdef STM32F4
25
+
26
+#include "../../inc/MarlinConfig.h"
27
+
28
+#if HAS_SERVOS
29
+
30
+#include "HAL_Servo_STM32F4.h"
31
+
32
+int8_t libServo::attach(const int pin) {
33
+  return Servo::attach(pin);
34
+}
35
+
36
+int8_t libServo::attach(const int pin, const int min, const int max) {
37
+  return Servo::attach(pin, min, max);
38
+}
39
+
40
+void libServo::move(const int value) {
41
+  constexpr uint16_t servo_delay[] = SERVO_DELAY;
42
+  static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long."); 
43
+  if (this->attach(0) >= 0) {
44
+    this->write(value);
45
+    safe_delay(servo_delay[this->servoIndex]);
46
+    #if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE)
47
+      this->detach();
48
+    #endif
49
+  }
50
+}
51
+#endif // HAS_SERVOS
52
+
53
+#endif // STM32F4

+ 41
- 0
Marlin/src/HAL/HAL_STM32F4/HAL_Servo_STM32F4.h Прегледај датотеку

@@ -0,0 +1,41 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ * Copyright (C) 2017 Victor Perez
8
+ *
9
+ * This program is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+#ifndef HAL_SERVO_STM32F4_H
25
+#define HAL_SERVO_STM32F4_H
26
+
27
+#include <Servo.h>
28
+
29
+// Inherit and expand on the official library
30
+class libServo : public Servo {
31
+public:
32
+    int8_t attach(const int pin);
33
+    int8_t attach(const int pin, const int min, const int max);
34
+    void move(const int value);
35
+private:
36
+    uint16_t min_ticks;
37
+    uint16_t max_ticks;
38
+    uint8_t servoIndex;               // index into the channel data for this servo
39
+};
40
+
41
+#endif // HAL_SERVO_STM32F4_H

+ 165
- 0
Marlin/src/HAL/HAL_STM32F4/HAL_spi_STM32F4.cpp Прегледај датотеку

@@ -0,0 +1,165 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ * Copyright (C) 2017 Victor Perez
8
+ *
9
+ * This program is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+/**
25
+ * Software SPI functions originally from Arduino Sd2Card Library
26
+ * Copyright (C) 2009 by William Greiman
27
+ */
28
+
29
+/**
30
+ * Adapted to the STM32F4 HAL
31
+ */
32
+
33
+#ifdef STM32F4
34
+
35
+// --------------------------------------------------------------------------
36
+// Includes
37
+// --------------------------------------------------------------------------
38
+
39
+#include "HAL.h"
40
+#include "../HAL_SPI.h"
41
+#include "pins_arduino.h"
42
+#include "spi_pins.h"
43
+#include "../../core/macros.h"
44
+#include <SPI.h>
45
+
46
+// --------------------------------------------------------------------------
47
+// Public Variables
48
+// --------------------------------------------------------------------------
49
+
50
+static SPISettings spiConfig;
51
+
52
+// --------------------------------------------------------------------------
53
+// Public functions
54
+// --------------------------------------------------------------------------
55
+
56
+#if ENABLED(SOFTWARE_SPI)
57
+  // --------------------------------------------------------------------------
58
+  // Software SPI
59
+  // --------------------------------------------------------------------------
60
+  #error "Software SPI not supported for STM32F4. Use hardware SPI."
61
+
62
+#else
63
+
64
+// --------------------------------------------------------------------------
65
+// Hardware SPI
66
+// --------------------------------------------------------------------------
67
+
68
+/**
69
+ * VGPV SPI speed start and F_CPU/2, by default 72/2 = 36Mhz
70
+ */
71
+
72
+/**
73
+ * @brief  Begin SPI port setup
74
+ *
75
+ * @return Nothing
76
+ *
77
+ * @details Only configures SS pin since libmaple creates and initialize the SPI object
78
+ */
79
+void spiBegin(void) {
80
+  #if !PIN_EXISTS(SS)
81
+    #error SS_PIN not defined!
82
+  #endif
83
+
84
+  SET_OUTPUT(SS_PIN);
85
+  WRITE(SS_PIN, HIGH);
86
+}
87
+
88
+/** Configure SPI for specified SPI speed */
89
+void spiInit(uint8_t spiRate) {
90
+  // Use datarates Marlin uses
91
+  uint32_t clock;
92
+  switch (spiRate) {
93
+  case SPI_FULL_SPEED:    clock = 20000000; break; // 13.9mhz=20000000  6.75mhz=10000000  3.38mhz=5000000  .833mhz=1000000
94
+  case SPI_HALF_SPEED:    clock =  5000000; break;
95
+  case SPI_QUARTER_SPEED: clock =  2500000; break;
96
+  case SPI_EIGHTH_SPEED:  clock =  1250000; break;
97
+  case SPI_SPEED_5:       clock =   625000; break;
98
+  case SPI_SPEED_6:       clock =   300000; break;
99
+  default:
100
+    clock = 4000000; // Default from the SPI libarary
101
+  }
102
+  spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0);
103
+  SPI.begin();
104
+}
105
+
106
+/**
107
+ * @brief  Receives a single byte from the SPI port.
108
+ *
109
+ * @return Byte received
110
+ *
111
+ * @details
112
+ */
113
+uint8_t spiRec(void) {
114
+  SPI.beginTransaction(spiConfig);
115
+  uint8_t returnByte = SPI.transfer(0xFF);
116
+  SPI.endTransaction();
117
+  return returnByte;
118
+}
119
+
120
+/**
121
+ * @brief  Receives a number of bytes from the SPI port to a buffer
122
+ *
123
+ * @param  buf   Pointer to starting address of buffer to write to.
124
+ * @param  nbyte Number of bytes to receive.
125
+ * @return Nothing
126
+ *
127
+ * @details Uses DMA
128
+ */
129
+void spiRead(uint8_t* buf, uint16_t nbyte) {
130
+  SPI.beginTransaction(spiConfig);
131
+  SPI.dmaTransfer(0, const_cast<uint8_t*>(buf), nbyte);
132
+  SPI.endTransaction();
133
+}
134
+
135
+/**
136
+ * @brief  Sends a single byte on SPI port
137
+ *
138
+ * @param  b Byte to send
139
+ *
140
+ * @details
141
+ */
142
+void spiSend(uint8_t b) {
143
+  SPI.beginTransaction(spiConfig);
144
+  SPI.transfer(b);
145
+  SPI.endTransaction();
146
+}
147
+
148
+/**
149
+ * @brief  Write token and then write from 512 byte buffer to SPI (for SD card)
150
+ *
151
+ * @param  buf   Pointer with buffer start address
152
+ * @return Nothing
153
+ *
154
+ * @details Use DMA
155
+ */
156
+void spiSendBlock(uint8_t token, const uint8_t* buf) {
157
+  SPI.beginTransaction(spiConfig);
158
+  SPI.transfer(token);
159
+  SPI.dmaSend(const_cast<uint8_t*>(buf), 512);
160
+  SPI.endTransaction();
161
+}
162
+
163
+#endif // SOFTWARE_SPI
164
+
165
+#endif // STM32F4

+ 156
- 0
Marlin/src/HAL/HAL_STM32F4/HAL_timers_STM32F4.cpp Прегледај датотеку

@@ -0,0 +1,156 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ *
4
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
5
+ * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
6
+ * Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+#ifdef STM32F4
24
+
25
+// --------------------------------------------------------------------------
26
+// Includes
27
+// --------------------------------------------------------------------------
28
+
29
+#include "HAL.h"
30
+
31
+#include "HAL_timers_STM32F4.h"
32
+
33
+// --------------------------------------------------------------------------
34
+// Externals
35
+// --------------------------------------------------------------------------
36
+
37
+// --------------------------------------------------------------------------
38
+// Local defines
39
+// --------------------------------------------------------------------------
40
+
41
+#define NUM_HARDWARE_TIMERS 2
42
+
43
+//#define PRESCALER 1
44
+// --------------------------------------------------------------------------
45
+// Types
46
+// --------------------------------------------------------------------------
47
+
48
+// --------------------------------------------------------------------------
49
+// Public Variables
50
+// --------------------------------------------------------------------------
51
+
52
+// --------------------------------------------------------------------------
53
+// Private Variables
54
+// --------------------------------------------------------------------------
55
+
56
+tTimerConfig timerConfig[NUM_HARDWARE_TIMERS];
57
+
58
+// --------------------------------------------------------------------------
59
+// Function prototypes
60
+// --------------------------------------------------------------------------
61
+
62
+// --------------------------------------------------------------------------
63
+// Private functions
64
+// --------------------------------------------------------------------------
65
+
66
+// --------------------------------------------------------------------------
67
+// Public functions
68
+// --------------------------------------------------------------------------
69
+
70
+bool timers_initialised[NUM_HARDWARE_TIMERS] = {false};
71
+
72
+void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
73
+
74
+  if (!timers_initialised[timer_num]) {
75
+    switch (timer_num) {
76
+    case STEP_TIMER_NUM:
77
+      //STEPPER TIMER TIM5 //use a 32bit timer
78
+      __HAL_RCC_TIM5_CLK_ENABLE();
79
+      timerConfig[0].timerdef.Instance            = TIM5;
80
+      timerConfig[0].timerdef.Init.Prescaler      = (STEPPER_TIMER_PRESCALE);
81
+      timerConfig[0].timerdef.Init.CounterMode    = TIM_COUNTERMODE_UP;
82
+      timerConfig[0].timerdef.Init.ClockDivision  = TIM_CLOCKDIVISION_DIV1;
83
+      timerConfig[0].IRQ_Id = TIM5_IRQn;
84
+      timerConfig[0].callback = (uint32_t)TC5_Handler;
85
+      HAL_NVIC_SetPriority(timerConfig[0].IRQ_Id, 1, 0);
86
+      break;
87
+    case TEMP_TIMER_NUM:
88
+      //TEMP TIMER TIM7 // any available 16bit Timer (1 already used for PWM)
89
+      __HAL_RCC_TIM7_CLK_ENABLE();
90
+      timerConfig[1].timerdef.Instance            = TIM7;
91
+      timerConfig[1].timerdef.Init.Prescaler      = (TEMP_TIMER_PRESCALE);
92
+      timerConfig[1].timerdef.Init.CounterMode    = TIM_COUNTERMODE_UP;
93
+      timerConfig[1].timerdef.Init.ClockDivision  = TIM_CLOCKDIVISION_DIV1;
94
+      timerConfig[1].IRQ_Id = TIM7_IRQn;
95
+      timerConfig[1].callback = (uint32_t)TC7_Handler;
96
+      HAL_NVIC_SetPriority(timerConfig[1].IRQ_Id, 2, 0);
97
+      break;
98
+    }
99
+    timers_initialised[timer_num] = true;
100
+  }
101
+
102
+  timerConfig[timer_num].timerdef.Init.Period = (((HAL_TIMER_RATE) / timerConfig[timer_num].timerdef.Init.Prescaler) / frequency) - 1;
103
+
104
+  if (HAL_TIM_Base_Init(&timerConfig[timer_num].timerdef) == HAL_OK)
105
+    HAL_TIM_Base_Start_IT(&timerConfig[timer_num].timerdef);
106
+}
107
+
108
+//forward the interrupt
109
+extern "C" void TIM5_IRQHandler() {
110
+  ((void(*)(void))timerConfig[0].callback)();
111
+}
112
+extern "C" void TIM7_IRQHandler() {
113
+  ((void(*)(void))timerConfig[1].callback)();
114
+}
115
+
116
+void HAL_timer_set_compare(const uint8_t timer_num, const uint32_t compare) {
117
+  __HAL_TIM_SetAutoreload(&timerConfig[timer_num].timerdef, compare);
118
+}
119
+
120
+void HAL_timer_enable_interrupt(const uint8_t timer_num) {
121
+  HAL_NVIC_EnableIRQ(timerConfig[timer_num].IRQ_Id);
122
+}
123
+
124
+void HAL_timer_disable_interrupt(const uint8_t timer_num) {
125
+  HAL_NVIC_DisableIRQ(timerConfig[timer_num].IRQ_Id);
126
+}
127
+
128
+hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
129
+  return __HAL_TIM_GetAutoreload(&timerConfig[timer_num].timerdef);
130
+}
131
+
132
+uint32_t HAL_timer_get_count(const uint8_t timer_num) {
133
+  return __HAL_TIM_GetCounter(&timerConfig[timer_num].timerdef);
134
+}
135
+
136
+void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_ticks) {
137
+  const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_ticks;
138
+  if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp);
139
+}
140
+
141
+void HAL_timer_isr_prologue(const uint8_t timer_num) {
142
+  if (__HAL_TIM_GET_FLAG(&timerConfig[timer_num].timerdef, TIM_FLAG_UPDATE) == SET) {
143
+    __HAL_TIM_CLEAR_FLAG(&timerConfig[timer_num].timerdef, TIM_FLAG_UPDATE);
144
+  }
145
+}
146
+
147
+bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
148
+  if (NVIC->ISER[(uint32_t)((int32_t)timerConfig[timer_num].IRQ_Id) >> 5] & (uint32_t)(1 << ((uint32_t)((int32_t)timerConfig[timer_num].IRQ_Id) & (uint32_t)0x1F))) {
149
+    return true;
150
+  }
151
+  else {
152
+    return false;
153
+  }
154
+}
155
+
156
+#endif // STM32F4

+ 105
- 0
Marlin/src/HAL/HAL_STM32F4/HAL_timers_STM32F4.h Прегледај датотеку

@@ -0,0 +1,105 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ *
4
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
5
+ * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
6
+ * Copyright (c) 2017 Victor Perez
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+#ifndef _HAL_TIMERS_STM32F4_H
24
+#define _HAL_TIMERS_STM32F4_H
25
+
26
+// --------------------------------------------------------------------------
27
+// Includes
28
+// --------------------------------------------------------------------------
29
+
30
+#include <stdint.h>
31
+
32
+// --------------------------------------------------------------------------
33
+// Defines
34
+// --------------------------------------------------------------------------
35
+
36
+#define FORCE_INLINE __attribute__((always_inline)) inline
37
+
38
+#define hal_timer_t uint32_t  // TODO: One is 16-bit, one 32-bit - does this need to be checked?
39
+#define HAL_TIMER_TYPE_MAX 0xFFFF
40
+
41
+#define STEP_TIMER_NUM 0  // index of timer to use for stepper
42
+#define TEMP_TIMER_NUM 1  // index of timer to use for temperature
43
+
44
+#define HAL_TIMER_RATE         (HAL_RCC_GetSysClockFreq() / 2)  // frequency of timer peripherals
45
+#define STEPPER_TIMER_PRESCALE 54            // was 40,prescaler for setting stepper timer, 2Mhz
46
+#define HAL_STEPPER_TIMER_RATE (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)   // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
47
+#define HAL_TICKS_PER_US       ((HAL_STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
48
+
49
+#define PULSE_TIMER_NUM STEP_TIMER_NUM
50
+#define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
51
+
52
+#define TEMP_TIMER_PRESCALE     1000 // prescaler for setting Temp timer, 72Khz
53
+#define TEMP_TIMER_FREQUENCY    1000 // temperature interrupt frequency
54
+
55
+#define STEP_TIMER_MIN_INTERVAL    8 // minimum time in µs between stepper interrupts
56
+
57
+#define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
58
+#define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
59
+#define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(STEP_TIMER_NUM)
60
+
61
+#define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(TEMP_TIMER_NUM)
62
+#define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(TEMP_TIMER_NUM)
63
+
64
+#define HAL_ENABLE_ISRs() do { if (thermalManager.in_temp_isr)DISABLE_TEMPERATURE_INTERRUPT(); else ENABLE_TEMPERATURE_INTERRUPT(); ENABLE_STEPPER_DRIVER_INTERRUPT(); } while(0)
65
+// TODO change this
66
+
67
+
68
+extern void TC5_Handler();
69
+extern void TC7_Handler();
70
+#define HAL_STEP_TIMER_ISR  void TC5_Handler()
71
+#define HAL_TEMP_TIMER_ISR  void TC7_Handler()
72
+
73
+// --------------------------------------------------------------------------
74
+// Types
75
+// --------------------------------------------------------------------------
76
+
77
+typedef struct {
78
+  TIM_HandleTypeDef timerdef;
79
+  IRQn_Type   IRQ_Id;
80
+  uint32_t callback;
81
+} tTimerConfig;
82
+
83
+// --------------------------------------------------------------------------
84
+// Public Variables
85
+// --------------------------------------------------------------------------
86
+
87
+//extern const tTimerConfig timerConfig[];
88
+
89
+// --------------------------------------------------------------------------
90
+// Public functions
91
+// --------------------------------------------------------------------------
92
+
93
+void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency);
94
+void HAL_timer_enable_interrupt(const uint8_t timer_num);
95
+void HAL_timer_disable_interrupt(const uint8_t timer_num);
96
+bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
97
+
98
+void HAL_timer_set_compare(const uint8_t timer_num, const uint32_t compare);
99
+hal_timer_t HAL_timer_get_compare(const uint8_t timer_num);
100
+uint32_t HAL_timer_get_count(const uint8_t timer_num);
101
+void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_ticks);
102
+
103
+void HAL_timer_isr_prologue(const uint8_t timer_num);
104
+
105
+#endif // _HAL_TIMERS_STM32F4_H

+ 12
- 0
Marlin/src/HAL/HAL_STM32F4/README.md Прегледај датотеку

@@ -0,0 +1,12 @@
1
+# This HAL is for the STM32F407 MCU used with STM32Generic Arduino core by danieleff.
2
+
3
+# Arduino core is located at:
4
+
5
+https://github.com/danieleff/STM32GENERIC
6
+
7
+Unzip it into [Arduino]/hardware folder
8
+
9
+# This HAL is in development.
10
+
11
+This HAL is a modified version of Chris Barr's Picoprint STM32F4 HAL.
12
+

+ 66
- 0
Marlin/src/HAL/HAL_STM32F4/SanityCheck.h Прегледај датотеку

@@ -0,0 +1,66 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016, 2017 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+/**
24
+ * Test Re-ARM specific configuration values for errors at compile-time.
25
+ */
26
+#if ENABLED(SPINDLE_LASER_ENABLE)
27
+  #if !PIN_EXISTS(SPINDLE_LASER_ENABLE)
28
+    #error "SPINDLE_LASER_ENABLE requires SPINDLE_LASER_ENABLE_PIN."
29
+  #elif SPINDLE_DIR_CHANGE && !PIN_EXISTS(SPINDLE_DIR)
30
+    #error "SPINDLE_DIR_PIN not defined."
31
+  #elif ENABLED(SPINDLE_LASER_PWM) && PIN_EXISTS(SPINDLE_LASER_PWM)
32
+    #if !PWM_PIN(SPINDLE_LASER_PWM_PIN)
33
+      #error "SPINDLE_LASER_PWM_PIN not assigned to a PWM pin."
34
+    #elif !(SPINDLE_LASER_PWM_PIN == 4 || SPINDLE_LASER_PWM_PIN == 6 || SPINDLE_LASER_PWM_PIN == 11)
35
+      #error "SPINDLE_LASER_PWM_PIN must use SERVO0, SERVO1 or SERVO3 connector"
36
+    #elif SPINDLE_LASER_POWERUP_DELAY < 1
37
+      #error "SPINDLE_LASER_POWERUP_DELAY must be greater than 0."
38
+    #elif SPINDLE_LASER_POWERDOWN_DELAY < 1
39
+      #error "SPINDLE_LASER_POWERDOWN_DELAY must be greater than 0."
40
+    #elif !defined(SPINDLE_LASER_PWM_INVERT)
41
+      #error "SPINDLE_LASER_PWM_INVERT missing."
42
+    #elif !defined(SPEED_POWER_SLOPE) || !defined(SPEED_POWER_INTERCEPT) || !defined(SPEED_POWER_MIN) || !defined(SPEED_POWER_MAX)
43
+      #error "SPINDLE_LASER_PWM equation constant(s) missing."
44
+    #elif PIN_EXISTS(CASE_LIGHT) && SPINDLE_LASER_PWM_PIN == CASE_LIGHT_PIN
45
+      #error "SPINDLE_LASER_PWM_PIN is used by CASE_LIGHT_PIN."
46
+    #elif PIN_EXISTS(E0_AUTO_FAN) && SPINDLE_LASER_PWM_PIN == E0_AUTO_FAN_PIN
47
+      #error "SPINDLE_LASER_PWM_PIN is used by E0_AUTO_FAN_PIN."
48
+    #elif PIN_EXISTS(E1_AUTO_FAN) && SPINDLE_LASER_PWM_PIN == E1_AUTO_FAN_PIN
49
+      #error "SPINDLE_LASER_PWM_PIN is used by E1_AUTO_FAN_PIN."
50
+    #elif PIN_EXISTS(E2_AUTO_FAN) && SPINDLE_LASER_PWM_PIN == E2_AUTO_FAN_PIN
51
+      #error "SPINDLE_LASER_PWM_PIN is used by E2_AUTO_FAN_PIN."
52
+    #elif PIN_EXISTS(E3_AUTO_FAN) && SPINDLE_LASER_PWM_PIN == E3_AUTO_FAN_PIN
53
+      #error "SPINDLE_LASER_PWM_PIN is used by E3_AUTO_FAN_PIN."
54
+    #elif PIN_EXISTS(E4_AUTO_FAN) && SPINDLE_LASER_PWM_PIN == E4_AUTO_FAN_PIN
55
+      #error "SPINDLE_LASER_PWM_PIN is used by E4_AUTO_FAN_PIN."
56
+    #elif PIN_EXISTS(FAN) && SPINDLE_LASER_PWM_PIN == FAN_PIN
57
+      #error "SPINDLE_LASER_PWM_PIN is used FAN_PIN."
58
+    #elif PIN_EXISTS(FAN1) && SPINDLE_LASER_PWM_PIN == FAN1_PIN
59
+      #error "SPINDLE_LASER_PWM_PIN is used FAN1_PIN."
60
+    #elif PIN_EXISTS(FAN2) && SPINDLE_LASER_PWM_PIN == FAN2_PIN
61
+      #error "SPINDLE_LASER_PWM_PIN is used FAN2_PIN."
62
+    #elif PIN_EXISTS(CONTROLLERFAN) && SPINDLE_LASER_PWM_PIN == CONTROLLERFAN_PIN
63
+      #error "SPINDLE_LASER_PWM_PIN is used by CONTROLLERFAN_PIN."
64
+    #endif
65
+  #endif
66
+#endif // SPINDLE_LASER_ENABLE

+ 77
- 0
Marlin/src/HAL/HAL_STM32F4/endstop_interrupts.h Прегледај датотеку

@@ -0,0 +1,77 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ * Copyright (C) 2017 Victor Perez
8
+ *
9
+ * This program is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+#ifndef _ENDSTOP_INTERRUPTS_H_
25
+#define _ENDSTOP_INTERRUPTS_H_
26
+
27
+volatile uint8_t e_hit = 0; // Different from 0 when the endstops should be tested in detail.
28
+                            // Must be reset to 0 by the test function when finished.
29
+
30
+// This is what is really done inside the interrupts.
31
+FORCE_INLINE void endstop_ISR_worker( void ) {
32
+  e_hit = 2; // Because the detection of a e-stop hit has a 1 step debouncer it has to be called at least twice.
33
+}
34
+
35
+// One ISR for all EXT-Interrupts
36
+void endstop_ISR(void) { endstop_ISR_worker(); }
37
+
38
+void setup_endstop_interrupts(void) {
39
+  #if HAS_X_MAX
40
+    pinMode(X_MAX_PIN, INPUT);
41
+    attachInterrupt(X_MAX_PIN, endstop_ISR, CHANGE); // assign it
42
+  #endif
43
+  #if HAS_X_MIN
44
+    pinMode(X_MIN_PIN, INPUT);
45
+    attachInterrupt(X_MIN_PIN, endstop_ISR, CHANGE);
46
+  #endif
47
+  #if HAS_Y_MAX
48
+    pinMode(Y_MAX_PIN, INPUT);
49
+    attachInterrupt(Y_MAX_PIN, endstop_ISR, CHANGE);
50
+  #endif
51
+  #if HAS_Y_MIN
52
+    pinMode(Y_MIN_PIN, INPUT);
53
+    attachInterrupt(Y_MIN_PIN, endstop_ISR, CHANGE);
54
+  #endif
55
+  #if HAS_Z_MAX
56
+    pinMode(Z_MAX_PIN, INPUT);
57
+    attachInterrupt(Z_MAX_PIN, endstop_ISR, CHANGE);
58
+  #endif
59
+  #if HAS_Z_MIN
60
+    pinMode(Z_MIN_PIN, INPUT);
61
+    attachInterrupt(Z_MIN_PIN, endstop_ISR, CHANGE);
62
+  #endif
63
+  #if HAS_Z2_MAX
64
+    pinMode(Z2_MAX_PIN, INPUT);
65
+    attachInterrupt(Z2_MAX_PIN, endstop_ISR, CHANGE);
66
+  #endif
67
+  #if HAS_Z2_MIN
68
+    pinMode(Z2_MIN_PIN, INPUT);
69
+    attachInterrupt(Z2_MIN_PIN, endstop_ISR, CHANGE);
70
+  #endif
71
+  #if HAS_Z_MIN_PROBE_PIN
72
+    pinMode(Z_MIN_PROBE_PIN, INPUT);
73
+    attachInterrupt(Z_MIN_PROBE_PIN, endstop_ISR, CHANGE);
74
+  #endif
75
+}
76
+
77
+#endif //_ENDSTOP_INTERRUPTS_H_

+ 54
- 0
Marlin/src/HAL/HAL_STM32F4/fastio_STM32F4.h Прегледај датотеку

@@ -0,0 +1,54 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ * Copyright (C) 2017 Victor Perez
8
+ *
9
+ * This program is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+/**
25
+ * Fast I/O interfaces for STM32F4
26
+ * These use GPIO functions instead of Direct Port Manipulation, as on AVR.
27
+ */
28
+
29
+#ifndef _FASTIO_STM32F4_H
30
+#define _FASTIO_STM32F4_H
31
+
32
+#define _BV(b) (1 << (b))
33
+
34
+#define READ(IO)              digitalRead(IO)
35
+#define WRITE(IO, v)          digitalWrite(IO,v)
36
+#define TOGGLE(IO)            do{ _SET_OUTPUT(IO); digitalWrite(IO,!digitalRead(IO)); }while(0)
37
+#define WRITE_VAR(IO, v)      digitalWrite(IO,v)
38
+
39
+#define _GET_MODE(IO)
40
+#define _SET_MODE(IO,M)       pinMode(IO, M)
41
+#define _SET_OUTPUT(IO)       pinMode(IO, OUTPUT)                               /*!< Output Push Pull Mode & GPIO_NOPULL   */
42
+
43
+#define SET_INPUT(IO)         _SET_MODE(IO, INPUT)                              /*!< Input Floating Mode                   */
44
+#define SET_INPUT_PULLUP(IO)  _SET_MODE(IO, INPUT_PULLUP)                       /*!< Input with Pull-up activation         */
45
+#define SET_INPUT_PULLDOW(IO) _SET_MODE(IO, INPUT_PULLDOWN)                     /*!< Input with Pull-down activation       */
46
+#define SET_OUTPUT(IO)        do{ _SET_OUTPUT(IO); WRITE(IO, LOW); }while(0)
47
+
48
+#define GET_INPUT(IO)
49
+#define GET_OUTPUT(IO)
50
+#define GET_TIMER(IO)
51
+
52
+#define OUT_WRITE(IO, v) { _SET_OUTPUT(IO); WRITE(IO, v); }
53
+
54
+#endif // _FASTIO_STM32F4_H

+ 75
- 0
Marlin/src/HAL/HAL_STM32F4/persistent_store_impl.cpp Прегледај датотеку

@@ -0,0 +1,75 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ *
4
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
5
+ * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
6
+ * Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
7
+ * Copyright (c) 2016 Victor Perez victor_pv@hotmail.com
8
+ *
9
+ * This program is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+#ifdef STM32F4
25
+
26
+#include "../persistent_store_api.h"
27
+
28
+#include "../../inc/MarlinConfig.h"
29
+
30
+#if ENABLED(EEPROM_SETTINGS)
31
+
32
+namespace HAL {
33
+namespace PersistentStore {
34
+
35
+bool access_start() { return true; }
36
+
37
+bool access_finish() { return true; }
38
+
39
+bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
40
+  while (size--) {
41
+    uint8_t * const p = (uint8_t * const)pos;
42
+    uint8_t v = *value;
43
+    // EEPROM has only ~100,000 write cycles,
44
+    // so only write bytes that have changed!
45
+    if (v != eeprom_read_byte(p)) {
46
+      eeprom_write_byte(p, v);
47
+      if (eeprom_read_byte(p) != v) {
48
+        SERIAL_ECHO_START();
49
+        SERIAL_ECHOLNPGM(MSG_ERR_EEPROM_WRITE);
50
+        return true;
51
+      }
52
+    }
53
+    crc16(crc, &v, 1);
54
+    pos++;
55
+    value++;
56
+  };
57
+  return false;
58
+}
59
+
60
+bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const bool writing) {
61
+  do {
62
+    uint8_t c = eeprom_read_byte((unsigned char*)pos);
63
+    if (writing) *value = c;
64
+    crc16(crc, &c, 1);
65
+    pos++;
66
+    value++;
67
+  } while (--size);
68
+  return false;
69
+}
70
+
71
+} // PersistentStore
72
+} // HAL
73
+
74
+#endif // EEPROM_SETTINGS
75
+#endif // STM32F4

+ 1
- 0
Marlin/src/HAL/HAL_STM32F4/pinsDebug.h Прегледај датотеку

@@ -0,0 +1 @@
1
+#error Debug pins is not supported on this Platform!

+ 41
- 0
Marlin/src/HAL/HAL_STM32F4/spi_pins.h Прегледај датотеку

@@ -0,0 +1,41 @@
1
+/**
2
+* Marlin 3D Printer Firmware
3
+* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+*
5
+* This program is free software: you can redistribute it and/or modify
6
+* it under the terms of the GNU General Public License as published by
7
+* the Free Software Foundation, either version 3 of the License, or
8
+* (at your option) any later version.
9
+*
10
+* This program is distributed in the hope that it will be useful,
11
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+* GNU General Public License for more details.
14
+*
15
+* You should have received a copy of the GNU General Public License
16
+* along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
+*
18
+*/
19
+
20
+#ifndef SPI_PINS_H_
21
+#define SPI_PINS_H_
22
+
23
+
24
+/**
25
+ * Define SPI Pins: SCK, MISO, MOSI, SS
26
+ *
27
+ */
28
+#ifndef SCK_PIN
29
+    #define SCK_PIN   PA5
30
+#endif
31
+#ifndef MISO_PIN
32
+    #define MISO_PIN  PA6
33
+#endif
34
+#ifndef MOSI_PIN
35
+    #define MOSI_PIN  PA7
36
+#endif
37
+#ifndef SS_PIN
38
+    #define SS_PIN    PA8
39
+#endif
40
+
41
+#endif // SPI_PINS_H_

+ 57
- 0
Marlin/src/HAL/HAL_STM32F4/watchdog_STM32F4.cpp Прегледај датотеку

@@ -0,0 +1,57 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+#ifdef STM32F4
24
+
25
+#include "../../inc/MarlinConfig.h"
26
+
27
+#if ENABLED(USE_WATCHDOG)
28
+
29
+  #include "watchdog_STM32F4.h"
30
+
31
+  IWDG_HandleTypeDef hiwdg;
32
+
33
+  void watchdog_init() {
34
+    hiwdg.Instance = IWDG;
35
+    hiwdg.Init.Prescaler = IWDG_PRESCALER_32; //32kHz LSI clock and 32x prescalar = 1024Hz IWDG clock
36
+    hiwdg.Init.Reload = 4095;           //4095 counts = 4 seconds at 1024Hz
37
+    if (HAL_IWDG_Init(&hiwdg) != HAL_OK) {
38
+      //Error_Handler();
39
+    }
40
+  }
41
+
42
+  void watchdog_reset() {
43
+    /* Refresh IWDG: reload counter */
44
+    if (HAL_IWDG_Refresh(&hiwdg) != HAL_OK) {
45
+      /* Refresh Error */
46
+      //Error_Handler();
47
+    }
48
+    else {
49
+    #if PIN_EXISTS(LED)
50
+      TOGGLE(LED_PIN);  // heart beat indicator
51
+    #endif
52
+    }
53
+  }
54
+
55
+#endif // USE_WATCHDOG
56
+
57
+#endif // STM32F4

+ 33
- 0
Marlin/src/HAL/HAL_STM32F4/watchdog_STM32F4.h Прегледај датотеку

@@ -0,0 +1,33 @@
1
+/**
2
+* Marlin 3D Printer Firmware
3
+* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+*
5
+* Based on Sprinter and grbl.
6
+* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+*
8
+* This program is free software: you can redistribute it and/or modify
9
+* it under the terms of the GNU General Public License as published by
10
+* the Free Software Foundation, either version 3 of the License, or
11
+* (at your option) any later version.
12
+*
13
+* This program is distributed in the hope that it will be useful,
14
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+* GNU General Public License for more details.
17
+*
18
+* You should have received a copy of the GNU General Public License
19
+* along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+*
21
+*/
22
+
23
+#ifndef WATCHDOG_STM32F4_H
24
+#define WATCHDOG_STM32F4_H
25
+
26
+#include "../../inc/MarlinConfig.h"
27
+
28
+extern IWDG_HandleTypeDef hiwdg;
29
+
30
+void watchdog_init();
31
+void watchdog_reset();
32
+
33
+#endif // WATCHDOG_STM32F1_H

+ 2
- 0
Marlin/src/HAL/platforms.h Прегледај датотеку

@@ -13,6 +13,8 @@
13 13
   #define HAL_PLATFORM HAL_LPC1768
14 14
 #elif defined(__STM32F1__) || defined(TARGET_STM32F1)
15 15
   #define HAL_PLATFORM HAL_STM32F1
16
+#elif defined(STM32F4)
17
+  #define HAL_PLATFORM HAL_STM32F4  
16 18
 #elif defined(STM32F7)
17 19
   #define HAL_PLATFORM HAL_STM32F7
18 20
 #else

+ 1
- 1
Marlin/src/HAL/servo.cpp Прегледај датотеку

@@ -54,7 +54,7 @@
54 54
 
55 55
 #include "../inc/MarlinConfig.h"
56 56
 
57
-#if HAS_SERVOS && !(IS_32BIT_TEENSY || defined(TARGET_LPC1768))
57
+#if HAS_SERVOS && !(IS_32BIT_TEENSY || defined(TARGET_LPC1768) || defined(STM32F4))
58 58
 
59 59
 //#include <Arduino.h>
60 60
 #include "servo.h"

+ 2
- 1
Marlin/src/HAL/servo.h Прегледај датотеку

@@ -74,7 +74,8 @@
74 74
 
75 75
 #elif defined(TARGET_LPC1768)
76 76
   #include "HAL_LPC1768/LPC1768_Servo.h"
77
-
77
+#elif defined(STM32F4)
78
+  #include "HAL_STM32F4/HAL_Servo_STM32F4.h"
78 79
 #else
79 80
   #include <stdint.h>
80 81
 

+ 11
- 0
Marlin/src/backtrace/unwmemaccess.cpp Прегледај датотеку

@@ -62,6 +62,17 @@
62 62
 #define END_FLASH_ADDR    0x00080000
63 63
 #endif
64 64
 
65
+#ifdef STM32F4
66
+// For STM32F407VET
67
+//  SRAM  (0x20000000 - 0x20030000) (192kb)
68
+//  FLASH (0x08000000 - 0x08080000) (512kb)
69
+//
70
+#define START_SRAM_ADDR   0x20000000
71
+#define END_SRAM_ADDR     0x20030000
72
+#define START_FLASH_ADDR  0x08000000
73
+#define END_FLASH_ADDR    0x08080000
74
+#endif
75
+
65 76
 #ifdef STM32F7
66 77
 // For STM32F765 in BORG
67 78
 //  SRAM  (0x20000000 - 0x20080000) (512kb)

+ 1868
- 0
Marlin/src/config/examples/STM32F4/Configuration.h
Разлика између датотеке није приказан због своје велике величине
Прегледај датотеку


+ 1
- 0
Marlin/src/core/boards.h Прегледај датотеку

@@ -207,6 +207,7 @@
207 207
 #define BOARD_MALYAN_M200      1801   // STM32C8T6 Libmaple based stm32f1 controller
208 208
 #define BOARD_BEAST            1802   // STM32FxxxVxT6 Libmaple based stm32f4 controller
209 209
 #define BOARD_STM3R_MINI       1803   // STM32 Libmaple based stm32f1 controller
210
+#define BOARD_STM32F4          1804   // STM32 STM32GENERIC based STM32F4 controller
210 211
 
211 212
 //
212 213
 // ARM Cortex M7

+ 2
- 0
Marlin/src/pins/pins.h Прегледај датотеку

@@ -359,6 +359,8 @@
359 359
   #include "pins_COHESION3D_REMIX.h"
360 360
 #elif MB(COHESION3D_MINI)
361 361
   #include "pins_COHESION3D_MINI.h"
362
+#elif MB(STM32F4)
363
+  #include "pins_STM32F4.h"
362 364
 #else
363 365
   #error "Unknown MOTHERBOARD value set in Configuration.h"
364 366
 #endif

+ 204
- 0
Marlin/src/pins/pins_STM32F4.h Прегледај датотеку

@@ -0,0 +1,204 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+#if !defined(STM32F4)
24
+  #error "Oops!  Make sure you have an STM32F4 board selected from the 'Tools -> Boards' menu."
25
+#endif
26
+
27
+#define DEFAULT_MACHINE_NAME "STM32F407VET6"
28
+#define BOARD_NAME "Marlin for STM32"
29
+
30
+// #define I2C_EEPROM
31
+
32
+// #define LARGE_FLASH true
33
+
34
+#define E2END 0xFFF // EEPROM end address (4kB)
35
+
36
+// Ignore temp readings during develpment.
37
+// #define BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
38
+
39
+#if E_STEPPERS > 2 || HOTENDS > 2
40
+  #error "STM32F4 supports up to 2 hotends / E-steppers."
41
+#endif
42
+
43
+#define PORTA 0
44
+#define PORTB 1
45
+#define PORTC 2
46
+#define PORTD 3
47
+#define PORTE 4
48
+
49
+#define _STM32_PIN(_PORT,_PIN) ((_PORT * 16) + _PIN)
50
+
51
+//
52
+// Limit Switches
53
+//
54
+#define X_MIN_PIN          _STM32_PIN(PORTE, 0)
55
+#define X_MAX_PIN          -1
56
+#define Y_MIN_PIN          _STM32_PIN(PORTE, 1)
57
+#define Y_MAX_PIN          -1
58
+#define Z_MIN_PIN          _STM32_PIN(PORTE, 14)
59
+#define Z_MAX_PIN          -1
60
+
61
+//
62
+// Z Probe (when not Z_MIN_PIN)
63
+//
64
+
65
+// #ifndef Z_MIN_PROBE_PIN
66
+//   #define Z_MIN_PROBE_PIN  _STM32_PIN(PORTA, 4)
67
+// #endif
68
+
69
+//
70
+// Steppers
71
+//
72
+
73
+#define X_STEP_PIN         _STM32_PIN(PORTD, 3)
74
+#define X_DIR_PIN          _STM32_PIN(PORTD, 2)
75
+#define X_ENABLE_PIN       _STM32_PIN(PORTD, 0)
76
+// #ifndef X_CS_PIN
77
+//   #define X_CS_PIN         _STM32_PIN(PORTD, 1)
78
+// #endif
79
+
80
+#define Y_STEP_PIN         _STM32_PIN(PORTE, 11)
81
+#define Y_DIR_PIN          _STM32_PIN(PORTE, 10)
82
+#define Y_ENABLE_PIN       _STM32_PIN(PORTE, 13)
83
+// #ifndef Y_CS_PIN
84
+//   #define Y_CS_PIN         _STM32_PIN(PORTE, 12)
85
+// #endif
86
+
87
+#define Z_STEP_PIN         _STM32_PIN(PORTD, 6)
88
+#define Z_DIR_PIN          _STM32_PIN(PORTD, 7)
89
+#define Z_ENABLE_PIN       _STM32_PIN(PORTD, 4)
90
+// #ifndef Z_CS_PIN
91
+//   #define Z_CS_PIN         _STM32_PIN(PORTD, 5)
92
+// #endif
93
+
94
+#define E0_STEP_PIN        _STM32_PIN(PORTB, 5)
95
+#define E0_DIR_PIN         _STM32_PIN(PORTB, 6)
96
+#define E0_ENABLE_PIN      _STM32_PIN(PORTB, 3)
97
+// #ifndef E0_CS_PIN
98
+//   #define E0_CS_PIN         _STM32_PIN(PORTB, 4)
99
+// #endif
100
+
101
+#define E1_STEP_PIN        _STM32_PIN(PORTE, 4)
102
+#define E1_DIR_PIN         _STM32_PIN(PORTE, 2)
103
+#define E1_ENABLE_PIN      _STM32_PIN(PORTE, 3)
104
+// #ifndef E1_CS_PIN
105
+//   #define E1_CS_PIN         _STM32_PIN(PORTE, 5)
106
+// #endif
107
+
108
+#define SCK_PIN            _STM32_PIN(PORTA, 5)
109
+#define MISO_PIN           _STM32_PIN(PORTA, 6)
110
+#define MOSI_PIN           _STM32_PIN(PORTA, 7)
111
+
112
+//
113
+// Temperature Sensors
114
+//
115
+
116
+#define TEMP_0_PIN         _STM32_PIN(PORTC, 0)   // Analog Input
117
+#define TEMP_1_PIN         _STM32_PIN(PORTC, 1)   // Analog Input
118
+#define TEMP_BED_PIN       _STM32_PIN(PORTC, 2)   // Analog Input
119
+
120
+//
121
+// Heaters / Fans
122
+//
123
+
124
+#define HEATER_0_PIN       _STM32_PIN(PORTA, 1)
125
+#define HEATER_1_PIN       _STM32_PIN(PORTA, 2)
126
+#define HEATER_BED_PIN     _STM32_PIN(PORTA, 0)
127
+
128
+#define FAN_PIN            _STM32_PIN(PORTC, 6)
129
+#define FAN1_PIN           _STM32_PIN(PORTC, 7)
130
+#define FAN2_PIN           _STM32_PIN(PORTC, 8)
131
+
132
+#define ORIG_E0_AUTO_FAN_PIN  FAN1_PIN // Use this by NOT overriding E0_AUTO_FAN_PIN
133
+
134
+//
135
+// Misc. Functions
136
+//
137
+
138
+//#define CASE_LIGHT_PIN_CI _STM32_PIN(PORTF, 13)  
139
+//#define CASE_LIGHT_PIN_DO _STM32_PIN(PORTF, 14)  
140
+//#define NEOPIXEL_PIN      _STM32_PIN(PORTF, 13)
141
+
142
+//
143
+// Prusa i3 MK2 Multi Material Multiplexer Support
144
+//
145
+
146
+// #define E_MUX0_PIN         _STM32_PIN(PORTG, 3)
147
+// #define E_MUX1_PIN         _STM32_PIN(PORTG, 4)
148
+
149
+//
150
+// Servos
151
+//
152
+
153
+// #define SERVO0_PIN         _STM32_PIN(PORTE, 13)
154
+// #define SERVO1_PIN         _STM32_PIN(PORTE, 14)
155
+
156
+
157
+#define SDSS               _STM32_PIN(PORTE, 7)
158
+#define SS_PIN             _STM32_PIN(PORTE, 7)
159
+#define LED_PIN            _STM32_PIN(PORTB, 7)         //Alive
160
+#define PS_ON_PIN          _STM32_PIN(PORTA, 10)
161
+#define KILL_PIN           _STM32_PIN(PORTA, 8)     
162
+#define PWR_LOSS           _STM32_PIN(PORTA, 4)         //Power loss / nAC_FAULT
163
+
164
+//
165
+// LCD / Controller
166
+//
167
+
168
+#define SD_DETECT_PIN      _STM32_PIN(PORTA, 15)     
169
+#define BEEPER_PIN         _STM32_PIN(PORTC, 9)      
170
+#define LCD_PINS_RS        _STM32_PIN(PORTE, 9)      
171
+#define LCD_PINS_ENABLE    _STM32_PIN(PORTE, 8)      
172
+#define LCD_PINS_D4        _STM32_PIN(PORTB, 12)     
173
+#define LCD_PINS_D5        _STM32_PIN(PORTB, 13)     
174
+#define LCD_PINS_D6        _STM32_PIN(PORTB, 14)     
175
+#define LCD_PINS_D7        _STM32_PIN(PORTB, 15)     
176
+#define BTN_EN1            _STM32_PIN(PORTC, 4)      
177
+#define BTN_EN2            _STM32_PIN(PORTC, 5)      
178
+#define BTN_ENC            _STM32_PIN(PORTC, 3)      
179
+
180
+//
181
+// Filament runout
182
+//
183
+
184
+#define FIL_RUNOUT_PIN     _STM32_PIN(PORTA, 3)
185
+
186
+//
187
+// ST7920 Delays
188
+//
189
+
190
+#define STM_NOP __asm__("nop\n\t")
191
+#define STM_DELAY_SHORT { STM_NOP; STM_NOP; STM_NOP; STM_NOP; }
192
+#define STM_DELAY_LONG  { STM_DELAY_SHORT; STM_DELAY_SHORT; STM_NOP; STM_NOP; }
193
+
194
+#ifndef ST7920_DELAY_1
195
+  #define ST7920_DELAY_1 { STM_DELAY_SHORT; STM_DELAY_SHORT; }
196
+#endif
197
+
198
+#ifndef ST7920_DELAY_2
199
+  #define ST7920_DELAY_2 { STM_DELAY_SHORT; }
200
+#endif
201
+
202
+#ifndef ST7920_DELAY_3
203
+  #define ST7920_DELAY_3 { STM_DELAY_LONG; STM_DELAY_LONG; STM_DELAY_LONG; STM_DELAY_LONG; STM_DELAY_LONG; STM_DELAY_LONG; }
204
+#endif

+ 10
- 0
platformio.ini Прегледај датотеку

@@ -242,6 +242,16 @@ lib_ldf_mode = 1
242 242
 src_filter   = ${common.default_src_filter}
243 243
 monitor_baud = 250000
244 244
 
245
+[env:STM32F4]
246
+platform    = ststm32
247
+framework   = arduino
248
+board       = disco_f407vg
249
+build_flags = ${common.build_flags} -DUSE_STM32GENERIC -DMENU_USB_SERIAL -DMENU_SERIAL=SerialUSB
250
+lib_deps    = ${common.lib_deps}
251
+lib_ignore  = Adafruit NeoPixel, c1921b4, TMC2130Stepper
252
+src_filter  = ${common.default_src_filter}
253
+monitor_baud = 250000
254
+
245 255
 #
246 256
 # Teensy++ 2.0
247 257
 #

Loading…
Откажи
Сачувај