Victor Oliveira
4 years ago
committed by
Scott Lahteine
17 changed files with 522 additions and 53 deletions
@ -0,0 +1,45 @@ |
|||||
|
/**
|
||||
|
* Marlin 3D Printer Firmware |
||||
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
||||
|
* |
||||
|
* Based on Sprinter and grbl. |
||||
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm |
||||
|
* |
||||
|
* 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 <https://www.gnu.org/licenses/>.
|
||||
|
* |
||||
|
*/ |
||||
|
#pragma once |
||||
|
|
||||
|
#include <SPI.h> |
||||
|
|
||||
|
/**
|
||||
|
* Marlin currently requires 3 SPI classes: |
||||
|
* |
||||
|
* SPIClass: |
||||
|
* This class is normally provided by frameworks and has a semi-default interface. |
||||
|
* This is needed because some libraries reference it globally. |
||||
|
* |
||||
|
* SPISettings: |
||||
|
* Container for SPI configs for SPIClass. As above, libraries may reference it globally. |
||||
|
* |
||||
|
* These two classes are often provided by frameworks so we cannot extend them to add |
||||
|
* useful methods for Marlin. |
||||
|
* |
||||
|
* MarlinSPI: |
||||
|
* Provides the default SPIClass interface plus some Marlin goodies such as a simplified |
||||
|
* interface for SPI DMA transfer. |
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
using MarlinSPI = SPIClass; |
@ -0,0 +1,161 @@ |
|||||
|
/**
|
||||
|
* Marlin 3D Printer Firmware |
||||
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
||||
|
* |
||||
|
* Based on Sprinter and grbl. |
||||
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm |
||||
|
* |
||||
|
* 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 <https://www.gnu.org/licenses/>.
|
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
#include "MarlinSPI.h" |
||||
|
|
||||
|
static void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb, uint32_t dataSize) { |
||||
|
spi_init(obj, speed, mode, msb); |
||||
|
// spi_init set 8bit always
|
||||
|
// TODO: copy the code from spi_init and handle data size, to avoid double init always!!
|
||||
|
if (dataSize != SPI_DATASIZE_8BIT) { |
||||
|
obj->handle.Init.DataSize = dataSize; |
||||
|
HAL_SPI_Init(&obj->handle); |
||||
|
__HAL_SPI_ENABLE(&obj->handle); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void MarlinSPI::setClockDivider(uint8_t _div) { |
||||
|
_speed = spi_getClkFreq(&_spi);// / _div;
|
||||
|
_clockDivider = _div; |
||||
|
} |
||||
|
|
||||
|
void MarlinSPI::begin(void) { |
||||
|
//TODO: only call spi_init if any parameter changed!!
|
||||
|
spi_init(&_spi, _speed, _dataMode, _bitOrder, _dataSize); |
||||
|
} |
||||
|
|
||||
|
void MarlinSPI::setupDma(SPI_HandleTypeDef &_spiHandle, DMA_HandleTypeDef &_dmaHandle, uint32_t direction, bool minc) { |
||||
|
_dmaHandle.Init.Direction = direction; |
||||
|
_dmaHandle.Init.PeriphInc = DMA_PINC_DISABLE; |
||||
|
_dmaHandle.Init.Mode = DMA_NORMAL; |
||||
|
_dmaHandle.Init.Priority = DMA_PRIORITY_LOW; |
||||
|
_dmaHandle.Init.MemInc = minc ? DMA_MINC_ENABLE : DMA_MINC_DISABLE; |
||||
|
|
||||
|
if (_dataSize == DATA_SIZE_8BIT) { |
||||
|
_dmaHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; |
||||
|
_dmaHandle.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; |
||||
|
} |
||||
|
else { |
||||
|
_dmaHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; |
||||
|
_dmaHandle.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; |
||||
|
} |
||||
|
#ifdef STM32F4xx |
||||
|
_dmaHandle.Init.Channel = DMA_CHANNEL_3; |
||||
|
_dmaHandle.Init.FIFOMode = DMA_FIFOMODE_DISABLE; |
||||
|
#endif |
||||
|
|
||||
|
// start DMA hardware
|
||||
|
// TODO: check if hardware is already enabled
|
||||
|
#ifdef SPI1_BASE |
||||
|
if (_spiHandle.Instance == SPI1) { |
||||
|
#ifdef STM32F1xx |
||||
|
__HAL_RCC_DMA1_CLK_ENABLE(); |
||||
|
_dmaHandle.Instance = (direction == DMA_MEMORY_TO_PERIPH) ? DMA1_Channel3 : DMA1_Channel2; |
||||
|
#elif defined(STM32F4xx) |
||||
|
__HAL_RCC_DMA2_CLK_ENABLE(); |
||||
|
_dmaHandle.Instance = DMA2_Stream3; |
||||
|
#endif |
||||
|
} |
||||
|
#endif |
||||
|
#ifdef SPI2_BASE |
||||
|
if (_spiHandle.Instance == SPI2) { |
||||
|
#ifdef STM32F1xx |
||||
|
__HAL_RCC_DMA1_CLK_ENABLE(); |
||||
|
_dmaHandle.Instance = (direction == DMA_MEMORY_TO_PERIPH) ? DMA1_Channel5 : DMA1_Channel4; |
||||
|
#elif defined(STM32F4xx) |
||||
|
//TODO: f4 dma config
|
||||
|
#endif |
||||
|
} |
||||
|
#endif |
||||
|
#ifdef SPI3_BASE |
||||
|
if (_spiHandle.Instance == SPI3) { |
||||
|
#ifdef STM32F1xx |
||||
|
__HAL_RCC_DMA2_CLK_ENABLE(); |
||||
|
_dmaHandle.Instance = (direction == DMA_MEMORY_TO_PERIPH) ? DMA2_Channel2 : DMA2_Channel1; |
||||
|
#elif defined(STM32F4xx) |
||||
|
//TODO: f4 dma config
|
||||
|
#endif |
||||
|
} |
||||
|
#endif |
||||
|
|
||||
|
HAL_DMA_Init(&_dmaHandle); |
||||
|
} |
||||
|
|
||||
|
byte MarlinSPI::transfer(uint8_t _data) { |
||||
|
uint8_t rxData = 0xFF; |
||||
|
HAL_SPI_TransmitReceive(&_spi.handle, &_data, &rxData, 1, HAL_MAX_DELAY); |
||||
|
return rxData; |
||||
|
} |
||||
|
|
||||
|
uint8_t MarlinSPI::dmaTransfer(const void *transmitBuf, void *receiveBuf, uint16_t length) { |
||||
|
const uint8_t ff = 0xFF; |
||||
|
|
||||
|
//if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) //only enable if disabled
|
||||
|
__HAL_SPI_ENABLE(&_spi.handle); |
||||
|
|
||||
|
if (receiveBuf) { |
||||
|
setupDma(_spi.handle, _dmaRx, DMA_PERIPH_TO_MEMORY, true); |
||||
|
HAL_DMA_Start(&_dmaRx, (uint32_t)&(_spi.handle.Instance->DR), (uint32_t)receiveBuf, length); |
||||
|
SET_BIT(_spi.handle.Instance->CR2, SPI_CR2_RXDMAEN); /* Enable Rx DMA Request */ |
||||
|
} |
||||
|
|
||||
|
// check for 2 lines transfer
|
||||
|
bool mincTransmit = true; |
||||
|
if (transmitBuf == nullptr && _spi.handle.Init.Direction == SPI_DIRECTION_2LINES && _spi.handle.Init.Mode == SPI_MODE_MASTER) { |
||||
|
transmitBuf = &ff; |
||||
|
mincTransmit = false; |
||||
|
} |
||||
|
|
||||
|
if (transmitBuf) { |
||||
|
setupDma(_spi.handle, _dmaTx, DMA_MEMORY_TO_PERIPH, mincTransmit); |
||||
|
HAL_DMA_Start(&_dmaTx, (uint32_t)transmitBuf, (uint32_t)&(_spi.handle.Instance->DR), length); |
||||
|
SET_BIT(_spi.handle.Instance->CR2, SPI_CR2_TXDMAEN); /* Enable Tx DMA Request */ |
||||
|
} |
||||
|
|
||||
|
if (transmitBuf) { |
||||
|
HAL_DMA_PollForTransfer(&_dmaTx, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY); |
||||
|
HAL_DMA_Abort(&_dmaTx); |
||||
|
HAL_DMA_DeInit(&_dmaTx); |
||||
|
} |
||||
|
|
||||
|
// while ((_spi.handle.Instance->SR & SPI_FLAG_RXNE) != SPI_FLAG_RXNE) {}
|
||||
|
|
||||
|
if (receiveBuf) { |
||||
|
HAL_DMA_PollForTransfer(&_dmaRx, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY); |
||||
|
HAL_DMA_Abort(&_dmaRx); |
||||
|
HAL_DMA_DeInit(&_dmaRx); |
||||
|
} |
||||
|
|
||||
|
return 1; |
||||
|
} |
||||
|
|
||||
|
uint8_t MarlinSPI::dmaSend(const void * transmitBuf, uint16_t length, bool minc) { |
||||
|
setupDma(_spi.handle, _dmaTx, DMA_MEMORY_TO_PERIPH, minc); |
||||
|
HAL_DMA_Start(&_dmaTx, (uint32_t)transmitBuf, (uint32_t)&(_spi.handle.Instance->DR), length); |
||||
|
__HAL_SPI_ENABLE(&_spi.handle); |
||||
|
SET_BIT(_spi.handle.Instance->CR2, SPI_CR2_TXDMAEN); /* Enable Tx DMA Request */ |
||||
|
HAL_DMA_PollForTransfer(&_dmaTx, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY); |
||||
|
HAL_DMA_Abort(&_dmaTx); |
||||
|
// DeInit objects
|
||||
|
HAL_DMA_DeInit(&_dmaTx); |
||||
|
return 1; |
||||
|
} |
@ -0,0 +1,107 @@ |
|||||
|
/**
|
||||
|
* Marlin 3D Printer Firmware |
||||
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
||||
|
* |
||||
|
* Based on Sprinter and grbl. |
||||
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm |
||||
|
* |
||||
|
* 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 <https://www.gnu.org/licenses/>.
|
||||
|
* |
||||
|
*/ |
||||
|
#pragma once |
||||
|
|
||||
|
#include "HAL.h" |
||||
|
#include <SPI.h> |
||||
|
|
||||
|
extern "C" { |
||||
|
#include <utility/spi_com.h> |
||||
|
} |
||||
|
|
||||
|
/**
|
||||
|
* Marlin currently requires 3 SPI classes: |
||||
|
* |
||||
|
* SPIClass: |
||||
|
* This class is normally provided by frameworks and has a semi-default interface. |
||||
|
* This is needed because some libraries reference it globally. |
||||
|
* |
||||
|
* SPISettings: |
||||
|
* Container for SPI configs for SPIClass. As above, libraries may reference it globally. |
||||
|
* |
||||
|
* These two classes are often provided by frameworks so we cannot extend them to add |
||||
|
* useful methods for Marlin. |
||||
|
* |
||||
|
* MarlinSPI: |
||||
|
* Provides the default SPIClass interface plus some Marlin goodies such as a simplified |
||||
|
* interface for SPI DMA transfer. |
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
#define DATA_SIZE_8BIT SPI_DATASIZE_8BIT |
||||
|
#define DATA_SIZE_16BIT SPI_DATASIZE_16BIT |
||||
|
|
||||
|
class MarlinSPI { |
||||
|
public: |
||||
|
MarlinSPI() : MarlinSPI(NC, NC, NC, NC) {} |
||||
|
|
||||
|
MarlinSPI(pin_t mosi, pin_t miso, pin_t sclk, pin_t ssel = (pin_t)NC) : _mosiPin(mosi), _misoPin(miso), _sckPin(sclk), _ssPin(ssel) { |
||||
|
_spi.pin_miso = digitalPinToPinName(_misoPin); |
||||
|
_spi.pin_mosi = digitalPinToPinName(_mosiPin); |
||||
|
_spi.pin_sclk = digitalPinToPinName(_sckPin); |
||||
|
_spi.pin_ssel = digitalPinToPinName(_ssPin); |
||||
|
_dataSize = DATA_SIZE_8BIT; |
||||
|
_bitOrder = MSBFIRST; |
||||
|
_dataMode = SPI_MODE_0; |
||||
|
_spi.handle.State = HAL_SPI_STATE_RESET; |
||||
|
setClockDivider(SPI_SPEED_CLOCK_DIV2_MHZ); |
||||
|
} |
||||
|
|
||||
|
void begin(void); |
||||
|
void end(void) {} |
||||
|
|
||||
|
byte transfer(uint8_t _data); |
||||
|
uint8_t dmaTransfer(const void *transmitBuf, void *receiveBuf, uint16_t length); |
||||
|
uint8_t dmaSend(const void * transmitBuf, uint16_t length, bool minc = true); |
||||
|
|
||||
|
/* These methods are deprecated and kept for compatibility.
|
||||
|
* Use SPISettings with SPI.beginTransaction() to configure SPI parameters. |
||||
|
*/ |
||||
|
void setBitOrder(BitOrder _order) { _bitOrder = _order; } |
||||
|
|
||||
|
void setDataMode(uint8_t _mode) { |
||||
|
switch (_mode) { |
||||
|
case SPI_MODE0: _dataMode = SPI_MODE_0; break; |
||||
|
case SPI_MODE1: _dataMode = SPI_MODE_1; break; |
||||
|
case SPI_MODE2: _dataMode = SPI_MODE_2; break; |
||||
|
case SPI_MODE3: _dataMode = SPI_MODE_3; break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void setClockDivider(uint8_t _div); |
||||
|
|
||||
|
private: |
||||
|
void setupDma(SPI_HandleTypeDef &_spiHandle, DMA_HandleTypeDef &_dmaHandle, uint32_t direction, bool minc = false); |
||||
|
|
||||
|
spi_t _spi; |
||||
|
DMA_HandleTypeDef _dmaTx; |
||||
|
DMA_HandleTypeDef _dmaRx; |
||||
|
BitOrder _bitOrder; |
||||
|
spi_mode_e _dataMode; |
||||
|
uint8_t _clockDivider; |
||||
|
uint32_t _speed; |
||||
|
uint32_t _dataSize; |
||||
|
pin_t _mosiPin; |
||||
|
pin_t _misoPin; |
||||
|
pin_t _sckPin; |
||||
|
pin_t _ssPin; |
||||
|
}; |
@ -0,0 +1,45 @@ |
|||||
|
/**
|
||||
|
* Marlin 3D Printer Firmware |
||||
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
||||
|
* |
||||
|
* Based on Sprinter and grbl. |
||||
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm |
||||
|
* |
||||
|
* 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 <https://www.gnu.org/licenses/>.
|
||||
|
* |
||||
|
*/ |
||||
|
#pragma once |
||||
|
|
||||
|
#include <SPI.h> |
||||
|
|
||||
|
/**
|
||||
|
* Marlin currently requires 3 SPI classes: |
||||
|
* |
||||
|
* SPIClass: |
||||
|
* This class is normally provided by frameworks and has a semi-default interface. |
||||
|
* This is needed because some libraries reference it globally. |
||||
|
* |
||||
|
* SPISettings: |
||||
|
* Container for SPI configs for SPIClass. As above, libraries may reference it globally. |
||||
|
* |
||||
|
* These two classes are often provided by frameworks so we cannot extend them to add |
||||
|
* useful methods for Marlin. |
||||
|
* |
||||
|
* MarlinSPI: |
||||
|
* Provides the default SPIClass interface plus some Marlin goodies such as a simplified |
||||
|
* interface for SPI DMA transfer. |
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
using MarlinSPI = SPIClass; |
@ -0,0 +1,69 @@ |
|||||
|
#!/usr/bin/env bash |
||||
|
# |
||||
|
# Build tests for MKS Robin nano |
||||
|
# (STM32F1 genericSTM32F103VE) |
||||
|
# |
||||
|
|
||||
|
# exit on first failure |
||||
|
set -e |
||||
|
|
||||
|
# |
||||
|
# MKS Robin nano v1.2 Emulated DOGM FSMC |
||||
|
# |
||||
|
use_example_configs Mks/Robin |
||||
|
opt_set MOTHERBOARD BOARD_MKS_ROBIN_NANO |
||||
|
exec_test $1 $2 "MKS Robin nano v1.2 Emulated DOGM FSMC" |
||||
|
|
||||
|
# |
||||
|
# MKS Robin v2 nano Emulated DOGM SPI |
||||
|
# (Robin v2 nano has no FSMC interface) |
||||
|
# |
||||
|
use_example_configs Mks/Robin |
||||
|
opt_set MOTHERBOARD BOARD_MKS_ROBIN_NANO_V2 |
||||
|
opt_disable TFT_INTERFACE_FSMC |
||||
|
opt_enable TFT_INTERFACE_SPI |
||||
|
exec_test $1 $2 "MKS Robin v2 nano Emulated DOGM SPI" |
||||
|
|
||||
|
# |
||||
|
# MKS Robin nano v1.2 LVGL FSMC |
||||
|
# |
||||
|
# use_example_configs Mks/Robin |
||||
|
# opt_set MOTHERBOARD BOARD_MKS_ROBIN_NANO |
||||
|
# opt_disable TFT_CLASSIC_UI TFT_COLOR_UI TOUCH_SCREEN TFT_RES_320x240 |
||||
|
# opt_enable TFT_LVGL_UI TFT_RES_480x320 |
||||
|
# exec_test $1 $2 "MKS Robin nano v1.2 LVGL FSMC" |
||||
|
|
||||
|
# |
||||
|
# MKS Robin v2 nano LVGL SPI |
||||
|
# (Robin v2 nano has no FSMC interface) |
||||
|
# |
||||
|
# use_example_configs Mks/Robin |
||||
|
# opt_set MOTHERBOARD BOARD_MKS_ROBIN_NANO_V2 |
||||
|
# opt_disable TFT_INTERFACE_FSMC TFT_COLOR_UI TOUCH_SCREEN TFT_RES_320x240 |
||||
|
# opt_enable TFT_INTERFACE_SPI TFT_LVGL_UI TFT_RES_480x320 |
||||
|
# exec_test $1 $2 "MKS Robin v2 nano LVGL SPI" |
||||
|
|
||||
|
# |
||||
|
# MKS Robin v2 nano New Color UI 480x320 SPI |
||||
|
# (Robin v2 nano has no FSMC interface) |
||||
|
# |
||||
|
use_example_configs Mks/Robin |
||||
|
opt_set MOTHERBOARD BOARD_MKS_ROBIN_NANO_V2 |
||||
|
opt_disable TFT_INTERFACE_FSMC TFT_RES_320x240 |
||||
|
opt_enable TFT_INTERFACE_SPI TFT_RES_480x320 |
||||
|
exec_test $1 $2 "MKS Robin v2 nano New Color UI 480x320 SPI" |
||||
|
|
||||
|
# |
||||
|
# MKS Robin v2 nano LVGL SPI + TMC |
||||
|
# (Robin v2 nano has no FSMC interface) |
||||
|
# |
||||
|
# use_example_configs Mks/Robin |
||||
|
# opt_set MOTHERBOARD BOARD_MKS_ROBIN_NANO_V2 |
||||
|
# opt_disable TFT_INTERFACE_FSMC TFT_COLOR_UI TOUCH_SCREEN TFT_RES_320x240 |
||||
|
# opt_enable TFT_INTERFACE_SPI TFT_LVGL_UI TFT_RES_480x320 |
||||
|
# opt_set X_DRIVER_TYPE TMC2209 |
||||
|
# opt_set Y_DRIVER_TYPE TMC2209 |
||||
|
# exec_test $1 $2 "MKS Robin v2 nano LVGL SPI + TMC" |
||||
|
|
||||
|
# cleanup |
||||
|
restore_configs |
Loading…
Reference in new issue