forked from chrissbarr/Marlin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from xC0000005/ADC-DMA-Support
Adc dma support
- Loading branch information
Showing
12 changed files
with
340 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
Marlin 3D Printer Firmware | ||
Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] | ||
Copyright (c) 2016 Bob Cousins [email protected] | ||
Copyright (c) 2017 Victor Perez | ||
Copyright (c) 2017 Victor Perez | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
|
@@ -34,6 +34,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |
// -------------------------------------------------------------------------- | ||
|
||
#include "../HAL.h" | ||
#include <STM32ADC.h> | ||
|
||
//#include <Wire.h> | ||
|
||
|
@@ -62,6 +63,47 @@ uint16_t HAL_adc_result; | |
// -------------------------------------------------------------------------- | ||
// Private Variables | ||
// -------------------------------------------------------------------------- | ||
USBSerial SerialUSB; | ||
STM32ADC adc(ADC1); | ||
|
||
uint8 adc_pins[] = { | ||
#if HAS_TEMP_0 | ||
TEMP_0_PIN, | ||
#endif | ||
#if HAS_TEMP_1 | ||
TEMP_1_PIN | ||
#endif | ||
#if HAS_TEMP_2 | ||
TEMP_2_PIN, | ||
#endif | ||
#if HAS_TEMP_3 | ||
TEMP_3_PIN, | ||
#endif | ||
#if HAS_TEMP_4 | ||
TEMP_4_PIN, | ||
#endif | ||
#if HAS_TEMP_BED | ||
TEMP_BED_PIN, | ||
#endif | ||
#if ENABLED(FILAMENT_WIDTH_SENSOR) | ||
FILWIDTH_PIN, | ||
#endif | ||
}; | ||
|
||
enum TEMP_PINS | ||
{ | ||
TEMP_0, | ||
TEMP_1, | ||
TEMP_2, | ||
TEMP_3, | ||
TEMP_4, | ||
TEMP_BED, | ||
FILWIDTH | ||
}; | ||
|
||
#define ADC_PIN_COUNT (sizeof(adc_pins)/sizeof(adc_pins[0])) | ||
uint16_t HAL_adc_results[ADC_PIN_COUNT]; | ||
|
||
|
||
// -------------------------------------------------------------------------- | ||
// Function prototypes | ||
|
@@ -129,9 +171,47 @@ static int freeMemory() { | |
// -------------------------------------------------------------------------- | ||
// ADC | ||
// -------------------------------------------------------------------------- | ||
// Init the AD in continuous capture mode | ||
void HAL_adc_init(void) | ||
{ | ||
// configure the ADC | ||
adc.calibrate(); | ||
adc.setSampleRate(ADC_SMPR_1_5); // ? | ||
adc.setPins(adc_pins, ADC_PIN_COUNT); | ||
adc.setDMA(HAL_adc_results, ADC_PIN_COUNT, (DMA_MINC_MODE | DMA_CIRC_MODE), NULL); | ||
adc.setScanMode(); | ||
adc.setContinuous(); | ||
adc.startConversion(); | ||
} | ||
|
||
void HAL_adc_start_conversion (uint8_t adc_pin) { | ||
HAL_adc_result = (analogRead(adc_pin) >> 2)& 0x3ff; // shift to get 10 bits only. | ||
TEMP_PINS pin_index = TEMP_0; | ||
if (adc_pin == TEMP_0_PIN){ | ||
pin_index = TEMP_0; | ||
} | ||
|
||
else if (adc_pin == TEMP_1_PIN) { | ||
pin_index = TEMP_1; | ||
} | ||
else if (adc_pin == TEMP_2_PIN) { | ||
pin_index = TEMP_2; | ||
} | ||
else if (adc_pin == TEMP_3_PIN) { | ||
pin_index = TEMP_3; | ||
} | ||
else if (adc_pin == TEMP_4_PIN) { | ||
pin_index = TEMP_4; | ||
} | ||
else if (adc_pin == TEMP_BED_PIN) { | ||
pin_index = TEMP_BED; | ||
} | ||
#if ENABLED(FILAMENT_WIDTH_SENSOR) | ||
else if (adc_pin == FILWIDTH_PIN) { | ||
pin_index = FILWIDTH; | ||
} | ||
#endif | ||
|
||
HAL_adc_result = (HAL_adc_results[(int)pin_index] >> 2)& 0x3ff; // shift to get 10 bits only. | ||
} | ||
|
||
uint16_t HAL_adc_get_result(void) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/** | ||
* Marlin 3D Printer Firmware | ||
* | ||
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] | ||
* Copyright (c) 2016 Bob Cousins [email protected] | ||
* Copyright (c) 2015-2016 Nico Tonnhofer [email protected] | ||
* Copyright (c) 2016 Victor Perez [email protected] | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
/** | ||
* Description: HAL for stm32duino and compatible (STM32F1) | ||
* Implementation of EEPROM settings in SDCard | ||
* | ||
* For __STM32F1__ | ||
*/ | ||
|
||
#ifdef __STM32F1__ | ||
|
||
#include "../../inc/MarlinConfig.h" | ||
|
||
// This is for EEPROM emulation in flash | ||
#if ENABLED(EEPROM_SETTINGS) && ENABLED(FLASH_EEPROM_EMULATION) | ||
|
||
#include "../persistent_store_api.h" | ||
|
||
#include <flash_stm32.h> | ||
#include <EEPROM.h> | ||
|
||
namespace HAL { | ||
namespace PersistentStore { | ||
|
||
// Store settings in the last two pages | ||
// Flash pages must be erased before writing, so keep track. | ||
bool firstWrite = false; | ||
uint32_t pageBase = EEPROM_START_ADDRESS; | ||
|
||
bool access_start() { | ||
firstWrite = true; | ||
return true; | ||
} | ||
|
||
|
||
bool access_finish(){ | ||
FLASH_Lock(); | ||
firstWrite = false; | ||
return true; | ||
} | ||
|
||
bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) { | ||
FLASH_Status status; | ||
|
||
if (firstWrite) | ||
{ | ||
FLASH_Unlock(); | ||
|
||
status = FLASH_ErasePage(EEPROM_PAGE0_BASE); | ||
if (status != FLASH_COMPLETE) | ||
{ | ||
return false; | ||
} | ||
|
||
status = FLASH_ErasePage(EEPROM_PAGE1_BASE); | ||
if (status != FLASH_COMPLETE) | ||
{ | ||
return false; | ||
} | ||
|
||
firstWrite = false; | ||
} | ||
|
||
// First write full words | ||
int i = 0; | ||
int wordsToWrite = size/sizeof(uint16_t); | ||
uint16_t* wordBuffer = (uint16_t *)value; | ||
while (wordsToWrite) | ||
{ | ||
status = FLASH_ProgramHalfWord(pageBase + pos + (i * 2), wordBuffer[i]); | ||
if (status != FLASH_COMPLETE) | ||
{ | ||
return false; | ||
} | ||
wordsToWrite--; | ||
i++; | ||
} | ||
|
||
// Now, write any remaining single byte | ||
if (size & 1) | ||
{ | ||
uint16_t temp = value[size-1]; | ||
|
||
status = FLASH_ProgramHalfWord(pageBase+pos+i, temp); | ||
if (status != FLASH_COMPLETE) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
crc16(crc, value, size); | ||
pos += ((size + 1) & ~1); | ||
return true; | ||
} | ||
|
||
void read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc) { | ||
for (int i = 0; i < size; i++) { | ||
byte* accessPoint = (byte*)(pageBase + pos + i); | ||
value[i] = *accessPoint; | ||
} | ||
|
||
crc16(crc, value, size); | ||
pos += ((size + 1) & ~1); | ||
} | ||
|
||
} | ||
} | ||
|
||
#endif // EEPROM_SETTINGS && EEPROM FLASH | ||
#endif // __STM32F1__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.