Skip to content

Commit

Permalink
Merge pull request #2 from xC0000005/ADC-DMA-Support
Browse files Browse the repository at this point in the history
Adc dma support
  • Loading branch information
victorpv authored Oct 3, 2017
2 parents e24ec4a + ea91e2e commit 56fd08f
Show file tree
Hide file tree
Showing 12 changed files with 340 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
*
* :[0, 1, 2, 3, 4, 5, 6, 7]
*/
#define SERIAL_PORT 0
#define SERIAL_PORT -1

/**
* This setting determines the communication speed of the printer.
Expand Down Expand Up @@ -282,7 +282,7 @@
*
* :{ '0': "Not used", '1':"100k / 4.7k - EPCOS", '2':"200k / 4.7k - ATC Semitec 204GT-2", '3':"Mendel-parts / 4.7k", '4':"10k !! do not use for a hotend. Bad resolution at high temp. !!", '5':"100K / 4.7k - ATC Semitec 104GT-2 (Used in ParCan & J-Head)", '6':"100k / 4.7k EPCOS - Not as accurate as Table 1", '7':"100k / 4.7k Honeywell 135-104LAG-J01", '8':"100k / 4.7k 0603 SMD Vishay NTCS0603E3104FXT", '9':"100k / 4.7k GE Sensing AL03006-58.2K-97-G1", '10':"100k / 4.7k RS 198-961", '11':"100k / 4.7k beta 3950 1%", '12':"100k / 4.7k 0603 SMD Vishay NTCS0603E3104FXT (calibrated for Makibox hot bed)", '13':"100k Hisens 3950 1% up to 300°C for hotend 'Simple ONE ' & hotend 'All In ONE'", '20':"PT100 (Ultimainboard V2.x)", '51':"100k / 1k - EPCOS", '52':"200k / 1k - ATC Semitec 204GT-2", '55':"100k / 1k - ATC Semitec 104GT-2 (Used in ParCan & J-Head)", '60':"100k Maker's Tool Works Kapton Bed Thermistor beta=3950", '66':"Dyze Design 4.7M High Temperature thermistor", '70':"the 100K thermistor found in the bq Hephestos 2", '71':"100k / 4.7k Honeywell 135-104LAF-J01", '147':"Pt100 / 4.7k", '1047':"Pt1000 / 4.7k", '110':"Pt100 / 1k (non-standard)", '1010':"Pt1000 / 1k (non standard)", '-3':"Thermocouple + MAX31855 (only for sensor 0)", '-2':"Thermocouple + MAX6675 (only for sensor 0)", '-1':"Thermocouple + AD595",'998':"Dummy 1", '999':"Dummy 2" }
*/
#define TEMP_SENSOR_0 1
#define TEMP_SENSOR_0 11
#define TEMP_SENSOR_1 0
#define TEMP_SENSOR_2 0
#define TEMP_SENSOR_3 0
Expand Down
84 changes: 82 additions & 2 deletions Marlin/src/HAL/HAL_STM32F1/HAL_Stm32f1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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>

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions Marlin/src/HAL/HAL_STM32F1/HAL_Stm32f1.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
// --------------------------------------------------------------------------

#if SERIAL_PORT == -1
extern USBSerial SerialUSB;

#define MYSERIAL SerialUSB
#elif SERIAL_PORT == 0
#define MYSERIAL Serial
Expand Down Expand Up @@ -175,7 +177,7 @@ void eeprom_update_block (const void *__src, void *__dst, size_t __n);

#define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT_ANALOG);

inline void HAL_adc_init(void) {}
void HAL_adc_init(void);


#define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
Expand Down Expand Up @@ -204,4 +206,3 @@ void HAL_enable_AdcFreerun(void);
// --------------------------------------------------------------------------

#endif // _HAL_STM32F1_H

4 changes: 2 additions & 2 deletions Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void HAL_timer_enable_interrupt (uint8_t timer_num) {
StepperTimer.attachInterrupt(STEP_TIMER_CHAN, stepTC_Handler);
break;
case TEMP_TIMER_NUM:
TempTimer.attachInterrupt(STEP_TIMER_CHAN, tempTC_Handler);
TempTimer.attachInterrupt(TEMP_TIMER_CHAN, tempTC_Handler);
break;
default:
break;
Expand All @@ -137,7 +137,7 @@ void HAL_timer_disable_interrupt (uint8_t timer_num) {
StepperTimer.detachInterrupt(STEP_TIMER_CHAN);
break;
case TEMP_TIMER_NUM:
TempTimer.detachInterrupt(STEP_TIMER_CHAN);
TempTimer.detachInterrupt(TEMP_TIMER_CHAN);
break;
default:
break;
Expand Down
5 changes: 3 additions & 2 deletions Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@
#define HAL_TIMER_TYPE uint16_t
#define HAL_TIMER_TYPE_MAX 0xFFFF

#define STEP_TIMER_NUM 5 // index of timer to use for stepper
// index of timer to use for stepper
#define STEP_TIMER_NUM 4

#define STEP_TIMER_CHAN 1 // Channel of the timer to use for compare and interrupts
#define TEMP_TIMER_NUM 2 // index of timer to use for temperature
#define TEMP_TIMER_CHAN 1 // Channel of the timer to use for compare and interrupts


#define HAL_TIMER_RATE (F_CPU) // frequency of timers peripherals
#define STEPPER_TIMER_PRESCALE 36 // prescaler for setting stepper timer, 2Mhz
#define HAL_STEPPER_TIMER_RATE (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE) // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
Expand Down
131 changes: 131 additions & 0 deletions Marlin/src/HAL/HAL_STM32F1/persistent_store_flash.cpp
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__
7 changes: 7 additions & 0 deletions Marlin/src/Marlin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,13 @@ void setup() {
Max7219_init();
#endif

// DO NOT COMMIT!
// Set up for Re-init USB
pinMode(BOARD_USB_DISC_BIT, OUTPUT);
digitalWrite(BOARD_USB_DISC_BIT, LOW);
SerialUSB.end();
SerialUSB.begin();

#ifdef DISABLE_JTAG
// Disable JTAG on AT90USB chips to free up pins for IO
MCUCR = 0x80;
Expand Down
1 change: 1 addition & 0 deletions Marlin/src/core/boards.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
#define BOARD_RAMPS_14_RE_ARM_EEF 1746 // Re-ARM with RAMPS 1.4 (Power outputs: Hotend0, Hotend1, Fan)
#define BOARD_RAMPS_14_RE_ARM_SF 1748 // Re-ARM with RAMPS 1.4 (Power outputs: Spindle, Controller Fan)
#define BOARD_STM32F1R 1800 // STM3R Libmaple based stm32f1 controller
#define BOARD_MALYAN_M200 1801 // STM32C8T6 Libmaple based stm32f1 controller

#define MB(board) (MOTHERBOARD==BOARD_##board)

Expand Down
5 changes: 4 additions & 1 deletion Marlin/src/module/configuration_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,11 @@ void MarlinSettings::postprocess() {
EEPROM_START();

eeprom_error = false;

#if ENABLED(FLASH_EEPROM_EMULATION)
EEPROM_SKIP(ver); // Flash doesn't allow rewriting without erase
#else
EEPROM_WRITE(ver); // invalidate data first
#endif
EEPROM_SKIP(working_crc); // Skip the checksum slot

working_crc = 0; // clear before first "real data"
Expand Down
4 changes: 3 additions & 1 deletion Marlin/src/pins/pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#define IS_RAMPS_EFF
#elif MB(RAMPS_13_EEF) || MB(RAMPS_14_EEF) || MB(RAMPS_14_RE_ARM_EEF) || MB(RAMPS_SMART_EEF) || MB(RAMPS_DUO_EEF) || MB(RAMPS4DUE_EEF)
#define IS_RAMPS_EEF
#elif MB(RAMPS_13_SF) || MB(RAMPS_14_SF) || MB(RAMPS_14_RE_ARM_SF) || MB(RAMPS_SMART_SF) || MB(RAMPS_DUO_SF) || MB(RAMPS4DUE_SF)
#elif MB(RAMPS_13_SF) || MB(RAMPS_14_SF) || MB(RAMPS_14_RE_ARM_SF) || MB(RAMPS_SMART_SF) || MB(RAMPS_DUO_SF) || MB(RAMPS4DUE_SF)
#define IS_RAMPS_SF
#endif

Expand Down Expand Up @@ -300,6 +300,8 @@
#include "pins_ALLIGATOR_R2.h"
#elif MB(STM32F1R)
#include "pins_STM32F1R.h"
#elif MB(MALYAN_M200)
#include "pins_MALYAN_M200.h"
#else
#error "Unknown MOTHERBOARD value set in Configuration.h"
#endif
Expand Down
Loading

0 comments on commit 56fd08f

Please sign in to comment.