Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.0.x] Multiple updates to STM32F1 HAL #8733

Merged
merged 19 commits into from
Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion Marlin/src/HAL/HAL_STM32F1/HAL_Stm32f1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
// --------------------------------------------------------------------------

#include "../HAL.h"
#include <STM32ADC.h>

//#include <Wire.h>

Expand Down Expand Up @@ -60,6 +61,45 @@ uint16_t HAL_adc_result;
// --------------------------------------------------------------------------
// Private Variables
// --------------------------------------------------------------------------
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 @@ -126,9 +166,33 @@ extern "C" {
// --------------------------------------------------------------------------
// ADC
// --------------------------------------------------------------------------
// Init the AD in continuous capture mode
void HAL_adc_init(void) {
// configure the ADC
adc.calibrate();
adc.setSampleRate(ADC_SMPR_41_5); // ?
adc.setPins(adc_pins, ADC_PIN_COUNT);
adc.setDMA(HAL_adc_results, (uint16_t)ADC_PIN_COUNT, (uint32_t)(DMA_MINC_MODE | DMA_CIRC_MODE), (void (*)())NULL);
adc.setScanMode();
adc.setContinuous();
adc.startConversion();
}

void HAL_adc_start_conversion(const uint8_t adc_pin) {
HAL_adc_result = (analogRead(adc_pin) >> 2) & 0x3ff; // shift to get 10 bits only.
TEMP_PINS pin_index;
switch (adc_pin) {
default:
case TEMP_0_PIN: pin_index = TEMP_0; break;
case TEMP_1_PIN: pin_index = TEMP_1; break;
case TEMP_2_PIN: pin_index = TEMP_2; break;
case TEMP_3_PIN: pin_index = TEMP_3; break;
case TEMP_4_PIN: pin_index = TEMP_4; break;
case TEMP_BED_PIN: pin_index = TEMP_BED; break;
#if ENABLED(FILAMENT_WIDTH_SENSOR)
case FILWIDTH_PIN: pin_index = FILWIDTH; break;
#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
2 changes: 1 addition & 1 deletion Marlin/src/HAL/HAL_STM32F1/HAL_Stm32f1.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,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)
#define HAL_READ_ADC HAL_adc_result
Expand Down
38 changes: 20 additions & 18 deletions Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,33 +96,35 @@ Timer_clock4: Prescaler 128 -> 656.25kHz
void HAL_timer_start(uint8_t timer_num, uint32_t frequency) {
switch (timer_num) {
case STEP_TIMER_NUM:
StepperTimer.pause();
StepperTimer.setCount(0);
StepperTimer.setPrescaleFactor(STEPPER_TIMER_PRESCALE);
StepperTimer.setOverflow(0xFFFF);
StepperTimer.setCompare(STEP_TIMER_CHAN, uint32_t(HAL_STEPPER_TIMER_RATE) / frequency);
StepperTimer.refresh();
StepperTimer.resume();
timer_pause(STEP_TIMER_DEV);
timer_set_count(STEP_TIMER_DEV, 0);
timer_set_prescaler(STEP_TIMER_DEV, (uint16)(STEPPER_TIMER_PRESCALE - 1));
timer_set_reload(STEP_TIMER_DEV, 0xFFFF);
timer_set_compare(STEP_TIMER_DEV, STEP_TIMER_CHAN, min(HAL_TIMER_TYPE_MAX, (HAL_STEPPER_TIMER_RATE / frequency)));
timer_attach_interrupt(STEP_TIMER_DEV, STEP_TIMER_CHAN, stepTC_Handler);
timer_generate_update(STEP_TIMER_DEV);
timer_resume(STEP_TIMER_DEV);
break;
case TEMP_TIMER_NUM:
TempTimer.pause();
TempTimer.setCount(0);
TempTimer.setPrescaleFactor(TEMP_TIMER_PRESCALE);
TempTimer.setOverflow(0xFFFF);
TempTimer.setCompare(TEMP_TIMER_CHAN, (F_CPU) / (TEMP_TIMER_PRESCALE) / frequency);
TempTimer.refresh();
TempTimer.resume();
timer_pause(TEMP_TIMER_DEV);
timer_set_count(TEMP_TIMER_DEV, 0);
timer_set_prescaler(TEMP_TIMER_DEV, (uint16)(TEMP_TIMER_PRESCALE - 1));
timer_set_reload(TEMP_TIMER_DEV, 0xFFFF);
timer_set_compare(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, min(HAL_TIMER_TYPE_MAX, ((F_CPU / TEMP_TIMER_PRESCALE) / frequency)));
timer_attach_interrupt(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, tempTC_Handler);
timer_generate_update(TEMP_TIMER_DEV);
timer_resume(TEMP_TIMER_DEV);
break;
}
}

void HAL_timer_enable_interrupt(uint8_t timer_num) {
switch (timer_num) {
case STEP_TIMER_NUM:
StepperTimer.attachInterrupt(STEP_TIMER_CHAN, stepTC_Handler);
timer_enable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN);
break;
case TEMP_TIMER_NUM:
TempTimer.attachInterrupt(STEP_TIMER_CHAN, tempTC_Handler);
timer_enable_irq(TEMP_TIMER_DEV, TEMP_TIMER_CHAN);
break;
default:
break;
Expand All @@ -132,10 +134,10 @@ void HAL_timer_enable_interrupt(uint8_t timer_num) {
void HAL_timer_disable_interrupt(uint8_t timer_num) {
switch (timer_num) {
case STEP_TIMER_NUM:
StepperTimer.detachInterrupt(STEP_TIMER_CHAN);
timer_disable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN);
break;
case TEMP_TIMER_NUM:
TempTimer.detachInterrupt(STEP_TIMER_CHAN);
timer_disable_irq(TEMP_TIMER_DEV, TEMP_TIMER_CHAN);
break;
default:
break;
Expand Down
12 changes: 8 additions & 4 deletions Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@
typedef uint16_t hal_timer_t;
#define HAL_TIMER_TYPE_MAX 0xFFFF

#define STEP_TIMER_NUM 5 // index of timer to use for stepper
#ifdef MCU_STM32F103CB || defined(MCU_STM32F103C8)
#define STEP_TIMER_NUM 4 // For C8/CB boards, use timer 4
#else
#define STEP_TIMER_NUM 5 // for other boards, five is fine.
#endif

#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 Expand Up @@ -84,10 +88,10 @@ extern "C" void stepTC_Handler(void);
// --------------------------------------------------------------------------
// Public Variables
// --------------------------------------------------------------------------

/*
static HardwareTimer StepperTimer(STEP_TIMER_NUM);
static HardwareTimer TempTimer(TEMP_TIMER_NUM);

*/
// --------------------------------------------------------------------------
// Public functions
// --------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/HAL/HAL_STM32F1/fastio_Stm32f1.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@

#include <libmaple/gpio.h>

#define READ(IO) (gpio_read_bit(PIN_MAP[IO].gpio_device, PIN_MAP[IO].gpio_bit) ? HIGH : LOW)
#define WRITE(IO, v) do{ gpio_write_bit(PIN_MAP[IO].gpio_device, PIN_MAP[IO].gpio_bit, v); } while (0)
#define TOGGLE(IO) do{ gpio_toggle_bit(PIN_MAP[IO].gpio_device, PIN_MAP[IO].gpio_bit); } while (0)
#define READ(IO) (PIN_MAP[IO].gpio_device->regs->IDR & (1U << PIN_MAP[IO].gpio_bit) ? HIGH : LOW)
#define WRITE(IO, v) (PIN_MAP[IO].gpio_device->regs->BSRR = (1U << PIN_MAP[IO].gpio_bit) << (16 * !(bool)v))
#define TOGGLE(IO) (PIN_MAP[IO].gpio_device->regs->ODR = PIN_MAP[IO].gpio_device->regs->ODR ^ (1U << PIN_MAP[IO].gpio_bit))
#define WRITE_VAR(IO, v) WRITE(io, v)

#define _GET_MODE(IO) (gpio_get_mode(PIN_MAP[IO].gpio_device, PIN_MAP[IO].gpio_bit))
Expand Down
110 changes: 110 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,110 @@
/**
* 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/>.
*
*/

/**
* persistent_store_flash.cpp
* HAL for stm32duino and compatible (STM32F1)
* Implementation of EEPROM settings in SDCard
*/

#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 (uint16_t i = 0; i < size; i++) {
byte* accessPoint = (byte*)(pageBase + pos + i);
value[i] = *accessPoint;
}

crc16(crc, value, size);
pos += ((size + 1) & ~1);
}

} // PersistentStore
} // HAL

#endif // EEPROM_SETTINGS && EEPROM FLASH
#endif // __STM32F1__
2 changes: 1 addition & 1 deletion Marlin/src/HAL/HAL_STM32F1/persistent_store_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#include "../../inc/MarlinConfig.h"

#if ENABLED(EEPROM_SETTINGS)
#if ENABLED(EEPROM_SETTINGS) && DISABLED(FLASH_EEPROM_EMULATION)

#include "../persistent_store_api.h"

Expand Down
32 changes: 32 additions & 0 deletions Marlin/src/HAL/HAL_STM32F1/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This HAL is for STM32F103 boards used with libmaple/stm32duino Arduino core.

# This HAL is in development and has not been tested with an actual printer.

### The stm32 core needs a modification in the file util.h to avoid conflict with Marlin macros for Debug.
Since only 1 file needs change in the stm32duino core, it's preferable over making changes to Marlin.


After these lines:
<>
#else
#define ASSERT_FAULT(exp) (void)((0))
#endif
<>

Add the following 3 lines:
<>
#undef DEBUG_NONE
#undef DEBUG_FAULT
#undef DEBUG_ALL
<>

### Main developers:
Victorpv


### Most up to date repository for this HAL:
https://github.com/victorpv/Marlin/tree/bugfix-2.0.x

PRs should be first sent to that fork, and once tested merged to Marlin bugfix-2.0.x branch.


Loading