Преглед на файлове

Standardize Marlin SPI (part 1) (#19989)

Victor Oliveira преди 3 години
родител
ревизия
85d094bbb4
No account linked to committer's email address

+ 1
- 0
.github/workflows/test-builds.yml Целия файл

@@ -83,6 +83,7 @@ jobs:
83 83
         - STM32F103RET6_creality
84 84
         - LERDGEX
85 85
         - mks_robin_nano35
86
+        - mks_robin_nano35_stm32
86 87
         - NUCLEO_F767ZI
87 88
 
88 89
         # Put lengthy tests last

+ 17
- 22
Marlin/src/HAL/LPC1768/HAL_SPI.cpp Целия файл

@@ -127,11 +127,9 @@
127 127
     for (uint16_t i = 0; i < nbyte; i++) doio(buf[i]);
128 128
   }
129 129
 
130
-  void spiSend(uint32_t chan, byte b) {
131
-  }
130
+  void spiSend(uint32_t chan, byte b) {}
132 131
 
133
-  void spiSend(uint32_t chan, const uint8_t* buf, size_t nbyte) {
134
-  }
132
+  void spiSend(uint32_t chan, const uint8_t* buf, size_t nbyte) {}
135 133
 
136 134
   // Read single byte from SPI
137 135
   uint8_t spiRec() { return doio(0xFF); }
@@ -143,9 +141,7 @@
143 141
     for (uint16_t i = 0; i < nbyte; i++) buf[i] = doio(0xFF);
144 142
   }
145 143
 
146
-  uint8_t spiTransfer(uint8_t b) {
147
-    return doio(b);
148
-  }
144
+  uint8_t spiTransfer(uint8_t b) { return doio(b); }
149 145
 
150 146
   // Write from buffer to SPI
151 147
   void spiSendBlock(uint8_t token, const uint8_t* buf) {
@@ -201,6 +197,15 @@ SPIClass::SPIClass(uint8_t device) {
201 197
   GPDMA_Init();
202 198
 }
203 199
 
200
+SPIClass::SPIClass(pin_t mosi, pin_t miso, pin_t sclk, pin_t ssel) {
201
+  #if BOARD_NR_SPI >= 1
202
+    if (mosi == BOARD_SPI1_MOSI_PIN) SPIClass(1);
203
+  #endif
204
+  #if BOARD_NR_SPI >= 2
205
+    if (mosi == BOARD_SPI2_MOSI_PIN) SPIClass(2);
206
+  #endif
207
+}
208
+
204 209
 void SPIClass::begin() {
205 210
   // Init the SPI pins in the first begin call
206 211
   if ((_currentSetting->spi_d == LPC_SSP0 && spiInitialised[0] == false) ||
@@ -331,25 +336,15 @@ void SPIClass::read(uint8_t *buf, uint32_t len) {
331 336
   for (uint16_t i = 0; i < len; i++) buf[i] = transfer(0xFF);
332 337
 }
333 338
 
334
-void SPIClass::setClock(uint32_t clock) {
335
-  _currentSetting->clock = clock;
336
-}
339
+void SPIClass::setClock(uint32_t clock) { _currentSetting->clock = clock; }
337 340
 
338
-void SPIClass::setModule(uint8_t device) {
339
-  _currentSetting = &_settings[device - 1];// SPI channels are called 1 2 and 3 but the array is zero indexed
340
-}
341
+void SPIClass::setModule(uint8_t device) { _currentSetting = &_settings[device - 1]; } // SPI channels are called 1, 2, and 3 but the array is zero-indexed
341 342
 
342
-void SPIClass::setBitOrder(uint8_t bitOrder) {
343
-  _currentSetting->bitOrder = bitOrder;
344
-}
343
+void SPIClass::setBitOrder(uint8_t bitOrder) { _currentSetting->bitOrder = bitOrder; }
345 344
 
346
-void SPIClass::setDataMode(uint8_t dataMode) {
347
-  _currentSetting->dataMode = dataMode;
348
-}
345
+void SPIClass::setDataMode(uint8_t dataMode) { _currentSetting->dataMode = dataMode; }
349 346
 
350
-void SPIClass::setDataSize(uint32_t ds) {
351
-  _currentSetting->dataSize = ds;
352
-}
347
+void SPIClass::setDataSize(uint32_t dataSize) { _currentSetting->dataSize = dataSize; }
353 348
 
354 349
 /**
355 350
  * Set up/tear down

+ 45
- 0
Marlin/src/HAL/LPC1768/MarlinSPI.h Целия файл

@@ -0,0 +1,45 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+#pragma once
23
+
24
+#include <SPI.h>
25
+
26
+/**
27
+ * Marlin currently requires 3 SPI classes:
28
+ *
29
+ * SPIClass:
30
+ *  This class is normally provided by frameworks and has a semi-default interface.
31
+ *  This is needed because some libraries reference it globally.
32
+ *
33
+ * SPISettings:
34
+ *  Container for SPI configs for SPIClass. As above, libraries may reference it globally.
35
+ *
36
+ * These two classes are often provided by frameworks so we cannot extend them to add
37
+ * useful methods for Marlin.
38
+ *
39
+ * MarlinSPI:
40
+ *  Provides the default SPIClass interface plus some Marlin goodies such as a simplified
41
+ *  interface for SPI DMA transfer.
42
+ *
43
+ */
44
+
45
+using MarlinSPI = SPIClass;

+ 5
- 0
Marlin/src/HAL/LPC1768/include/SPI.h Целия файл

@@ -127,6 +127,11 @@ public:
127 127
   SPIClass(uint8_t spiPortNumber);
128 128
 
129 129
   /**
130
+   * Init using pins
131
+   */
132
+  SPIClass(pin_t mosi, pin_t miso, pin_t sclk, pin_t ssel = (pin_t)-1);
133
+
134
+  /**
130 135
    * Select and configure the current selected SPI device to use
131 136
    */
132 137
   void begin();

+ 161
- 0
Marlin/src/HAL/STM32/MarlinSPI.cpp Целия файл

@@ -0,0 +1,161 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+#include "MarlinSPI.h"
24
+
25
+static void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb, uint32_t dataSize) {
26
+  spi_init(obj, speed, mode, msb);
27
+  // spi_init set 8bit always
28
+  // TODO: copy the code from spi_init and handle data size, to avoid double init always!!
29
+  if (dataSize != SPI_DATASIZE_8BIT) {
30
+    obj->handle.Init.DataSize = dataSize;
31
+    HAL_SPI_Init(&obj->handle);
32
+    __HAL_SPI_ENABLE(&obj->handle);
33
+  }
34
+}
35
+
36
+void MarlinSPI::setClockDivider(uint8_t _div) {
37
+  _speed = spi_getClkFreq(&_spi);// / _div;
38
+  _clockDivider = _div;
39
+}
40
+
41
+void MarlinSPI::begin(void) {
42
+  //TODO: only call spi_init if any parameter changed!!
43
+  spi_init(&_spi, _speed, _dataMode, _bitOrder, _dataSize);
44
+}
45
+
46
+void MarlinSPI::setupDma(SPI_HandleTypeDef &_spiHandle, DMA_HandleTypeDef &_dmaHandle, uint32_t direction, bool minc) {
47
+  _dmaHandle.Init.Direction = direction;
48
+  _dmaHandle.Init.PeriphInc = DMA_PINC_DISABLE;
49
+  _dmaHandle.Init.Mode = DMA_NORMAL;
50
+  _dmaHandle.Init.Priority = DMA_PRIORITY_LOW;
51
+  _dmaHandle.Init.MemInc = minc ? DMA_MINC_ENABLE : DMA_MINC_DISABLE;
52
+
53
+  if (_dataSize == DATA_SIZE_8BIT) {
54
+    _dmaHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
55
+    _dmaHandle.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
56
+  }
57
+  else {
58
+    _dmaHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
59
+    _dmaHandle.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
60
+  }
61
+  #ifdef STM32F4xx
62
+    _dmaHandle.Init.Channel = DMA_CHANNEL_3;
63
+    _dmaHandle.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
64
+  #endif
65
+
66
+  // start DMA hardware
67
+  // TODO: check if hardware is already enabled
68
+  #ifdef SPI1_BASE
69
+    if (_spiHandle.Instance == SPI1) {
70
+      #ifdef STM32F1xx
71
+        __HAL_RCC_DMA1_CLK_ENABLE();
72
+        _dmaHandle.Instance = (direction == DMA_MEMORY_TO_PERIPH) ? DMA1_Channel3 : DMA1_Channel2;
73
+      #elif defined(STM32F4xx)
74
+        __HAL_RCC_DMA2_CLK_ENABLE();
75
+        _dmaHandle.Instance = DMA2_Stream3;
76
+      #endif
77
+    }
78
+  #endif
79
+  #ifdef SPI2_BASE
80
+    if (_spiHandle.Instance == SPI2) {
81
+      #ifdef STM32F1xx
82
+        __HAL_RCC_DMA1_CLK_ENABLE();
83
+        _dmaHandle.Instance = (direction == DMA_MEMORY_TO_PERIPH) ? DMA1_Channel5 : DMA1_Channel4;
84
+      #elif defined(STM32F4xx)
85
+        //TODO: f4 dma config
86
+      #endif
87
+    }
88
+  #endif
89
+  #ifdef SPI3_BASE
90
+    if (_spiHandle.Instance == SPI3) {
91
+      #ifdef STM32F1xx
92
+        __HAL_RCC_DMA2_CLK_ENABLE();
93
+        _dmaHandle.Instance = (direction == DMA_MEMORY_TO_PERIPH) ? DMA2_Channel2 : DMA2_Channel1;
94
+      #elif defined(STM32F4xx)
95
+        //TODO: f4 dma config
96
+      #endif
97
+    }
98
+  #endif
99
+
100
+  HAL_DMA_Init(&_dmaHandle);
101
+}
102
+
103
+byte MarlinSPI::transfer(uint8_t _data) {
104
+  uint8_t rxData = 0xFF;
105
+  HAL_SPI_TransmitReceive(&_spi.handle, &_data, &rxData, 1, HAL_MAX_DELAY);
106
+  return rxData;
107
+}
108
+
109
+uint8_t MarlinSPI::dmaTransfer(const void *transmitBuf, void *receiveBuf, uint16_t length) {
110
+  const uint8_t ff = 0xFF;
111
+
112
+  //if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) //only enable if disabled
113
+  __HAL_SPI_ENABLE(&_spi.handle);
114
+
115
+  if (receiveBuf) {
116
+    setupDma(_spi.handle, _dmaRx, DMA_PERIPH_TO_MEMORY, true);
117
+    HAL_DMA_Start(&_dmaRx, (uint32_t)&(_spi.handle.Instance->DR), (uint32_t)receiveBuf, length);
118
+    SET_BIT(_spi.handle.Instance->CR2, SPI_CR2_RXDMAEN); /* Enable Rx DMA Request */
119
+  }
120
+
121
+  // check for 2 lines transfer
122
+  bool mincTransmit = true;
123
+  if (transmitBuf == nullptr && _spi.handle.Init.Direction == SPI_DIRECTION_2LINES && _spi.handle.Init.Mode == SPI_MODE_MASTER) {
124
+    transmitBuf = &ff;
125
+    mincTransmit = false;
126
+  }
127
+
128
+  if (transmitBuf) {
129
+    setupDma(_spi.handle, _dmaTx, DMA_MEMORY_TO_PERIPH, mincTransmit);
130
+    HAL_DMA_Start(&_dmaTx, (uint32_t)transmitBuf, (uint32_t)&(_spi.handle.Instance->DR), length);
131
+    SET_BIT(_spi.handle.Instance->CR2, SPI_CR2_TXDMAEN);   /* Enable Tx DMA Request */
132
+  }
133
+
134
+  if (transmitBuf) {
135
+    HAL_DMA_PollForTransfer(&_dmaTx, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY);
136
+    HAL_DMA_Abort(&_dmaTx);
137
+    HAL_DMA_DeInit(&_dmaTx);
138
+  }
139
+
140
+  // while ((_spi.handle.Instance->SR & SPI_FLAG_RXNE) != SPI_FLAG_RXNE) {}
141
+
142
+  if (receiveBuf) {
143
+    HAL_DMA_PollForTransfer(&_dmaRx, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY);
144
+    HAL_DMA_Abort(&_dmaRx);
145
+    HAL_DMA_DeInit(&_dmaRx);
146
+  }
147
+
148
+  return 1;
149
+}
150
+
151
+uint8_t MarlinSPI::dmaSend(const void * transmitBuf, uint16_t length, bool minc) {
152
+  setupDma(_spi.handle, _dmaTx, DMA_MEMORY_TO_PERIPH, minc);
153
+  HAL_DMA_Start(&_dmaTx, (uint32_t)transmitBuf, (uint32_t)&(_spi.handle.Instance->DR), length);
154
+  __HAL_SPI_ENABLE(&_spi.handle);
155
+  SET_BIT(_spi.handle.Instance->CR2, SPI_CR2_TXDMAEN);   /* Enable Tx DMA Request */
156
+  HAL_DMA_PollForTransfer(&_dmaTx, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY);
157
+  HAL_DMA_Abort(&_dmaTx);
158
+  // DeInit objects
159
+  HAL_DMA_DeInit(&_dmaTx);
160
+  return 1;
161
+}

+ 107
- 0
Marlin/src/HAL/STM32/MarlinSPI.h Целия файл

@@ -0,0 +1,107 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+#pragma once
23
+
24
+#include "HAL.h"
25
+#include <SPI.h>
26
+
27
+extern "C" {
28
+  #include <utility/spi_com.h>
29
+}
30
+
31
+/**
32
+ * Marlin currently requires 3 SPI classes:
33
+ *
34
+ * SPIClass:
35
+ *  This class is normally provided by frameworks and has a semi-default interface.
36
+ *  This is needed because some libraries reference it globally.
37
+ *
38
+ * SPISettings:
39
+ *  Container for SPI configs for SPIClass. As above, libraries may reference it globally.
40
+ *
41
+ * These two classes are often provided by frameworks so we cannot extend them to add
42
+ * useful methods for Marlin.
43
+ *
44
+ * MarlinSPI:
45
+ *  Provides the default SPIClass interface plus some Marlin goodies such as a simplified
46
+ *  interface for SPI DMA transfer.
47
+ *
48
+ */
49
+
50
+#define DATA_SIZE_8BIT SPI_DATASIZE_8BIT
51
+#define DATA_SIZE_16BIT SPI_DATASIZE_16BIT
52
+
53
+class MarlinSPI {
54
+public:
55
+  MarlinSPI() : MarlinSPI(NC, NC, NC, NC) {}
56
+
57
+  MarlinSPI(pin_t mosi, pin_t miso, pin_t sclk, pin_t ssel = (pin_t)NC) : _mosiPin(mosi), _misoPin(miso), _sckPin(sclk), _ssPin(ssel) {
58
+    _spi.pin_miso = digitalPinToPinName(_misoPin);
59
+    _spi.pin_mosi = digitalPinToPinName(_mosiPin);
60
+    _spi.pin_sclk = digitalPinToPinName(_sckPin);
61
+    _spi.pin_ssel = digitalPinToPinName(_ssPin);
62
+    _dataSize = DATA_SIZE_8BIT;
63
+    _bitOrder = MSBFIRST;
64
+    _dataMode = SPI_MODE_0;
65
+    _spi.handle.State = HAL_SPI_STATE_RESET;
66
+    setClockDivider(SPI_SPEED_CLOCK_DIV2_MHZ);
67
+  }
68
+
69
+  void begin(void);
70
+  void end(void) {}
71
+
72
+  byte transfer(uint8_t _data);
73
+  uint8_t dmaTransfer(const void *transmitBuf, void *receiveBuf, uint16_t length);
74
+  uint8_t dmaSend(const void * transmitBuf, uint16_t length, bool minc = true);
75
+
76
+  /* These methods are deprecated and kept for compatibility.
77
+   * Use SPISettings with SPI.beginTransaction() to configure SPI parameters.
78
+   */
79
+  void setBitOrder(BitOrder _order) { _bitOrder = _order; }
80
+
81
+  void setDataMode(uint8_t _mode) {
82
+    switch (_mode) {
83
+      case SPI_MODE0: _dataMode = SPI_MODE_0; break;
84
+      case SPI_MODE1: _dataMode = SPI_MODE_1; break;
85
+      case SPI_MODE2: _dataMode = SPI_MODE_2; break;
86
+      case SPI_MODE3: _dataMode = SPI_MODE_3; break;
87
+    }
88
+  }
89
+
90
+  void setClockDivider(uint8_t _div);
91
+
92
+private:
93
+  void setupDma(SPI_HandleTypeDef &_spiHandle, DMA_HandleTypeDef &_dmaHandle, uint32_t direction, bool minc = false);
94
+
95
+  spi_t _spi;
96
+  DMA_HandleTypeDef _dmaTx;
97
+  DMA_HandleTypeDef _dmaRx;
98
+  BitOrder _bitOrder;
99
+  spi_mode_e _dataMode;
100
+  uint8_t _clockDivider;
101
+  uint32_t _speed;
102
+  uint32_t _dataSize;
103
+  pin_t _mosiPin;
104
+  pin_t _misoPin;
105
+  pin_t _sckPin;
106
+  pin_t _ssPin;
107
+};

+ 1
- 1
Marlin/src/HAL/STM32/timers.h Целия файл

@@ -105,7 +105,7 @@ void SetTimerInterruptPriorities();
105 105
 
106 106
 // FORCE_INLINE because these are used in performance-critical situations
107 107
 FORCE_INLINE bool HAL_timer_initialized(const uint8_t timer_num) {
108
-  return timer_instance[timer_num] != NULL;
108
+  return timer_instance[timer_num] != nullptr;
109 109
 }
110 110
 FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
111 111
   return HAL_timer_initialized(timer_num) ? timer_instance[timer_num]->getCount() : 0;

+ 45
- 0
Marlin/src/HAL/STM32F1/MarlinSPI.h Целия файл

@@ -0,0 +1,45 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+#pragma once
23
+
24
+#include <SPI.h>
25
+
26
+/**
27
+ * Marlin currently requires 3 SPI classes:
28
+ *
29
+ * SPIClass:
30
+ *  This class is normally provided by frameworks and has a semi-default interface.
31
+ *  This is needed because some libraries reference it globally.
32
+ *
33
+ * SPISettings:
34
+ *  Container for SPI configs for SPIClass. As above, libraries may reference it globally.
35
+ *
36
+ * These two classes are often provided by frameworks so we cannot extend them to add
37
+ * useful methods for Marlin.
38
+ *
39
+ * MarlinSPI:
40
+ *  Provides the default SPIClass interface plus some Marlin goodies such as a simplified
41
+ *  interface for SPI DMA transfer.
42
+ *
43
+ */
44
+
45
+using MarlinSPI = SPIClass;

+ 12
- 0
Marlin/src/HAL/STM32F1/SPI.cpp Целия файл

@@ -147,6 +147,18 @@ SPIClass::SPIClass(uint32_t spi_num) {
147 147
   _currentSetting->state = SPI_STATE_IDLE;
148 148
 }
149 149
 
150
+SPIClass::SPIClass(int8_t mosi, int8_t miso, int8_t sclk, int8_t ssel) {
151
+  #if BOARD_NR_SPI >= 1
152
+    if (mosi == BOARD_SPI1_MOSI_PIN) SPIClass(1);
153
+  #endif
154
+  #if BOARD_NR_SPI >= 2
155
+    if (mosi == BOARD_SPI2_MOSI_PIN) SPIClass(2);
156
+  #endif
157
+  #if BOARD_NR_SPI >= 3
158
+    if (mosi == BOARD_SPI3_MOSI_PIN) SPIClass(3);
159
+  #endif
160
+}
161
+
150 162
 /**
151 163
  * Set up/tear down
152 164
  */

+ 5
- 0
Marlin/src/HAL/STM32F1/SPI.h Целия файл

@@ -164,6 +164,11 @@ public:
164 164
   SPIClass(uint32_t spiPortNumber);
165 165
 
166 166
   /**
167
+   * Init using pins
168
+   */
169
+  SPIClass(int8_t mosi, int8_t miso, int8_t sclk, int8_t ssel=-1);
170
+
171
+  /**
167 172
    * @brief Equivalent to begin(SPI_1_125MHZ, MSBFIRST, 0).
168 173
    */
169 174
   void begin();

+ 6
- 1
Marlin/src/lcd/lcdprint.cpp Целия файл

@@ -32,7 +32,12 @@
32 32
 
33 33
 /**
34 34
  * lcd_put_u8str_ind_P
35
- * Print a string with an index substituted within it
35
+ *
36
+ * Print a string with an index substituted within it:
37
+ *
38
+ *   = displays  '0'....'10' for indexes 0 - 10
39
+ *   ~ displays  '1'....'11' for indexes 0 - 10
40
+ *   * displays 'E1'...'E11' for indexes 0 - 10 (By default. Uses LCD_FIRST_TOOL)
36 41
  */
37 42
 lcd_uint_t lcd_put_u8str_ind_P(PGM_P const pstr, const int8_t ind, PGM_P const inStr/*=nullptr*/, const lcd_uint_t maxlen/*=LCD_WIDTH*/) {
38 43
   uint8_t *p = (uint8_t*)pstr;

+ 22
- 16
Marlin/src/lcd/tft/tft_string.cpp Целия файл

@@ -44,21 +44,21 @@ void TFT_String::set_font(const uint8_t *font) {
44 44
 
45 45
   for (glyph = 0; glyph < 256; glyph++) glyphs[glyph] = nullptr;
46 46
 
47
-  DEBUG_ECHOLNPAIR("Format: ", font_header->Format);
48
-  DEBUG_ECHOLNPAIR("BBXWidth: ", font_header->BBXWidth);
49
-  DEBUG_ECHOLNPAIR("BBXHeight: ", font_header->BBXHeight);
50
-  DEBUG_ECHOLNPAIR("BBXOffsetX: ", font_header->BBXOffsetX);
51
-  DEBUG_ECHOLNPAIR("BBXOffsetY: ", font_header->BBXOffsetY);
52
-  DEBUG_ECHOLNPAIR("CapitalAHeight: ", font_header->CapitalAHeight);
53
-  DEBUG_ECHOLNPAIR("Encoding65Pos: ", font_header->Encoding65Pos);
54
-  DEBUG_ECHOLNPAIR("Encoding97Pos: ", font_header->Encoding97Pos);
47
+  DEBUG_ECHOLNPAIR("Format: ",            font_header->Format);
48
+  DEBUG_ECHOLNPAIR("BBXWidth: ",          font_header->BBXWidth);
49
+  DEBUG_ECHOLNPAIR("BBXHeight: ",         font_header->BBXHeight);
50
+  DEBUG_ECHOLNPAIR("BBXOffsetX: ",        font_header->BBXOffsetX);
51
+  DEBUG_ECHOLNPAIR("BBXOffsetY: ",        font_header->BBXOffsetY);
52
+  DEBUG_ECHOLNPAIR("CapitalAHeight: ",    font_header->CapitalAHeight);
53
+  DEBUG_ECHOLNPAIR("Encoding65Pos: ",     font_header->Encoding65Pos);
54
+  DEBUG_ECHOLNPAIR("Encoding97Pos: ",     font_header->Encoding97Pos);
55 55
   DEBUG_ECHOLNPAIR("FontStartEncoding: ", font_header->FontStartEncoding);
56
-  DEBUG_ECHOLNPAIR("FontEndEncoding: ", font_header->FontEndEncoding);
57
-  DEBUG_ECHOLNPAIR("LowerGDescent: ", font_header->LowerGDescent);
58
-  DEBUG_ECHOLNPAIR("FontAscent: ", font_header->FontAscent);
59
-  DEBUG_ECHOLNPAIR("FontDescent: ", font_header->FontDescent);
60
-  DEBUG_ECHOLNPAIR("FontXAscent: ", font_header->FontXAscent);
61
-  DEBUG_ECHOLNPAIR("FontXDescent: ", font_header->FontXDescent);
56
+  DEBUG_ECHOLNPAIR("FontEndEncoding: ",   font_header->FontEndEncoding);
57
+  DEBUG_ECHOLNPAIR("LowerGDescent: ",     font_header->LowerGDescent);
58
+  DEBUG_ECHOLNPAIR("FontAscent: ",        font_header->FontAscent);
59
+  DEBUG_ECHOLNPAIR("FontDescent: ",       font_header->FontDescent);
60
+  DEBUG_ECHOLNPAIR("FontXAscent: ",       font_header->FontXAscent);
61
+  DEBUG_ECHOLNPAIR("FontXDescent: ",      font_header->FontXDescent);
62 62
 
63 63
   add_glyphs(font);
64 64
 }
@@ -72,9 +72,8 @@ void TFT_String::add_glyphs(const uint8_t *font) {
72 72
       glyphs[glyph] = (glyph_t *)pointer;
73 73
       pointer += sizeof(glyph_t) + ((glyph_t *)pointer)->DataSize;
74 74
     }
75
-    else {
75
+    else
76 76
       pointer++;
77
-    }
78 77
   }
79 78
 }
80 79
 
@@ -86,6 +85,13 @@ void TFT_String::set() {
86 85
 
87 86
 uint8_t read_byte(uint8_t *byte) { return *byte; }
88 87
 
88
+/**
89
+ * Add a string, applying substitutions for the following characters:
90
+ *
91
+ *   = displays  '0'....'10' for indexes 0 - 10
92
+ *   ~ displays  '1'....'11' for indexes 0 - 10
93
+ *   * displays 'E1'...'E11' for indexes 0 - 10 (By default. Uses LCD_FIRST_TOOL)
94
+ */
89 95
 void TFT_String::add(uint8_t *string, int8_t index, uint8_t *itemString) {
90 96
   wchar_t wchar;
91 97
 

+ 18
- 12
Marlin/src/libs/W25Qxx.cpp Целия файл

@@ -25,7 +25,6 @@
25 25
 #if HAS_SPI_FLASH
26 26
 
27 27
 #include "W25Qxx.h"
28
-#include <SPI.h>
29 28
 
30 29
 W25QXXFlash W25QXX;
31 30
 
@@ -41,6 +40,11 @@ W25QXXFlash W25QXX;
41 40
 #ifndef SPI_FLASH_CS_PIN
42 41
   #define SPI_FLASH_CS_PIN   W25QXX_CS_PIN
43 42
 #endif
43
+#ifndef NC
44
+  #define NC -1
45
+#endif
46
+
47
+MarlinSPI W25QXXFlash::mySPI(SPI_FLASH_MOSI_PIN, SPI_FLASH_MISO_PIN, SPI_FLASH_SCK_PIN, NC);
44 48
 
45 49
 #define W25QXX_CS_H OUT_WRITE(SPI_FLASH_CS_PIN, HIGH)
46 50
 #define W25QXX_CS_L OUT_WRITE(SPI_FLASH_CS_PIN, LOW)
@@ -69,11 +73,11 @@ void W25QXXFlash::init(uint8_t spiRate) {
69 73
     case SPI_SPEED_6:       clock = SPI_CLOCK_DIV64; break;
70 74
     default:                clock = SPI_CLOCK_DIV2;// Default from the SPI library
71 75
   }
72
-  SPI.setModule(SPI_DEVICE);
73
-  SPI.begin();
74
-  SPI.setClockDivider(clock);
75
-  SPI.setBitOrder(MSBFIRST);
76
-  SPI.setDataMode(SPI_MODE0);
76
+
77
+  mySPI.setClockDivider(clock);
78
+  mySPI.setBitOrder(MSBFIRST);
79
+  mySPI.setDataMode(SPI_MODE0);
80
+  mySPI.begin();
77 81
 }
78 82
 
79 83
 /**
@@ -82,12 +86,12 @@ void W25QXXFlash::init(uint8_t spiRate) {
82 86
  * @return Byte received
83 87
  */
84 88
 uint8_t W25QXXFlash::spi_flash_Rec() {
85
-  const uint8_t returnByte = SPI.transfer(0xFF);
89
+  const uint8_t returnByte = mySPI.transfer(0xFF);
86 90
   return returnByte;
87 91
 }
88 92
 
89 93
 uint8_t W25QXXFlash::spi_flash_read_write_byte(uint8_t data) {
90
-  const uint8_t returnByte = SPI.transfer(data);
94
+  const uint8_t returnByte = mySPI.transfer(data);
91 95
   return returnByte;
92 96
 }
93 97
 
@@ -100,7 +104,9 @@ uint8_t W25QXXFlash::spi_flash_read_write_byte(uint8_t data) {
100 104
  *
101 105
  * @details Uses DMA
102 106
  */
103
-void W25QXXFlash::spi_flash_Read(uint8_t* buf, uint16_t nbyte) { SPI.dmaTransfer(0, const_cast<uint8_t*>(buf), nbyte); }
107
+void W25QXXFlash::spi_flash_Read(uint8_t* buf, uint16_t nbyte) {
108
+  mySPI.dmaTransfer(0, const_cast<uint8_t*>(buf), nbyte);
109
+}
104 110
 
105 111
 /**
106 112
  * @brief  Send a single byte on SPI port
@@ -109,7 +115,7 @@ void W25QXXFlash::spi_flash_Read(uint8_t* buf, uint16_t nbyte) { SPI.dmaTransfer
109 115
  *
110 116
  * @details
111 117
  */
112
-void W25QXXFlash::spi_flash_Send(uint8_t b) { SPI.send(b); }
118
+void W25QXXFlash::spi_flash_Send(uint8_t b) { mySPI.transfer(b); }
113 119
 
114 120
 /**
115 121
  * @brief  Write token and then write from 512 byte buffer to SPI (for SD card)
@@ -120,8 +126,8 @@ void W25QXXFlash::spi_flash_Send(uint8_t b) { SPI.send(b); }
120 126
  * @details Use DMA
121 127
  */
122 128
 void W25QXXFlash::spi_flash_SendBlock(uint8_t token, const uint8_t* buf) {
123
-  SPI.send(token);
124
-  SPI.dmaSend(const_cast<uint8_t*>(buf), 512);
129
+  mySPI.transfer(token);
130
+  mySPI.dmaSend(const_cast<uint8_t*>(buf), 512);
125 131
 }
126 132
 
127 133
 uint16_t W25QXXFlash::W25QXX_ReadID(void) {

+ 4
- 0
Marlin/src/libs/W25Qxx.h Целия файл

@@ -23,6 +23,8 @@
23 23
 
24 24
 #include <stdint.h>
25 25
 
26
+#include HAL_PATH(../HAL, MarlinSPI.h)
27
+
26 28
 #define W25X_WriteEnable        0x06
27 29
 #define W25X_WriteDisable       0x04
28 30
 #define W25X_ReadStatusReg      0x05
@@ -49,6 +51,8 @@
49 51
 #define SPI_FLASH_PerWritePageSize   256
50 52
 
51 53
 class W25QXXFlash {
54
+private:
55
+  static MarlinSPI mySPI;
52 56
 public:
53 57
   void init(uint8_t spiRate);
54 58
   static uint8_t spi_flash_Rec();

+ 3
- 0
Marlin/src/pins/stm32f1/pins_MKS_ROBIN_NANO.h Целия файл

@@ -191,6 +191,9 @@
191 191
   #define FSMC_DMA_DEV                      DMA2
192 192
   #define FSMC_DMA_CHANNEL               DMA_CH5
193 193
 
194
+  #define TFT_CS_PIN                 FSMC_CS_PIN
195
+  #define TFT_RS_PIN                 FSMC_RS_PIN
196
+
194 197
   #define TOUCH_BUTTONS_HW_SPI
195 198
   #define TOUCH_BUTTONS_HW_SPI_DEVICE          2
196 199
 

+ 1
- 1
buildroot/tests/mks_robin_nano35-tests Целия файл

@@ -15,7 +15,7 @@ opt_set MOTHERBOARD BOARD_MKS_ROBIN_NANO
15 15
 exec_test $1 $2 "MKS Robin nano v1.2 Emulated DOGM FSMC"
16 16
 
17 17
 #
18
-# MKS Robin v2 nano LVGL SPI
18
+# MKS Robin v2 nano Emulated DOGM SPI
19 19
 # (Robin v2 nano has no FSMC interface)
20 20
 #
21 21
 use_example_configs Mks/Robin

+ 69
- 0
buildroot/tests/mks_robin_nano35_stm32-tests Целия файл

@@ -0,0 +1,69 @@
1
+#!/usr/bin/env bash
2
+#
3
+# Build tests for MKS Robin nano
4
+# (STM32F1 genericSTM32F103VE)
5
+#
6
+
7
+# exit on first failure
8
+set -e
9
+
10
+#
11
+# MKS Robin nano v1.2 Emulated DOGM FSMC
12
+#
13
+use_example_configs Mks/Robin
14
+opt_set MOTHERBOARD BOARD_MKS_ROBIN_NANO
15
+exec_test $1 $2 "MKS Robin nano v1.2 Emulated DOGM FSMC"
16
+
17
+#
18
+# MKS Robin v2 nano Emulated DOGM SPI
19
+# (Robin v2 nano has no FSMC interface)
20
+#
21
+use_example_configs Mks/Robin
22
+opt_set MOTHERBOARD BOARD_MKS_ROBIN_NANO_V2
23
+opt_disable TFT_INTERFACE_FSMC
24
+opt_enable TFT_INTERFACE_SPI
25
+exec_test $1 $2 "MKS Robin v2 nano Emulated DOGM SPI"
26
+
27
+#
28
+# MKS Robin nano v1.2 LVGL FSMC
29
+#
30
+# use_example_configs Mks/Robin
31
+# opt_set MOTHERBOARD BOARD_MKS_ROBIN_NANO
32
+# opt_disable TFT_CLASSIC_UI TFT_COLOR_UI TOUCH_SCREEN TFT_RES_320x240
33
+# opt_enable TFT_LVGL_UI TFT_RES_480x320
34
+# exec_test $1 $2 "MKS Robin nano v1.2 LVGL FSMC"
35
+
36
+#
37
+# MKS Robin v2 nano LVGL SPI
38
+# (Robin v2 nano has no FSMC interface)
39
+#
40
+# use_example_configs Mks/Robin
41
+# opt_set MOTHERBOARD BOARD_MKS_ROBIN_NANO_V2
42
+# opt_disable TFT_INTERFACE_FSMC TFT_COLOR_UI TOUCH_SCREEN TFT_RES_320x240
43
+# opt_enable TFT_INTERFACE_SPI TFT_LVGL_UI TFT_RES_480x320
44
+# exec_test $1 $2 "MKS Robin v2 nano LVGL SPI"
45
+
46
+#
47
+# MKS Robin v2 nano New Color UI 480x320 SPI
48
+# (Robin v2 nano has no FSMC interface)
49
+#
50
+use_example_configs Mks/Robin
51
+opt_set MOTHERBOARD BOARD_MKS_ROBIN_NANO_V2
52
+opt_disable TFT_INTERFACE_FSMC TFT_RES_320x240
53
+opt_enable TFT_INTERFACE_SPI TFT_RES_480x320
54
+exec_test $1 $2 "MKS Robin v2 nano New Color UI 480x320 SPI"
55
+
56
+#
57
+# MKS Robin v2 nano LVGL SPI + TMC
58
+# (Robin v2 nano has no FSMC interface)
59
+#
60
+# use_example_configs Mks/Robin
61
+# opt_set MOTHERBOARD BOARD_MKS_ROBIN_NANO_V2
62
+# opt_disable TFT_INTERFACE_FSMC TFT_COLOR_UI TOUCH_SCREEN TFT_RES_320x240
63
+# opt_enable TFT_INTERFACE_SPI TFT_LVGL_UI TFT_RES_480x320
64
+# opt_set X_DRIVER_TYPE TMC2209
65
+# opt_set Y_DRIVER_TYPE TMC2209
66
+# exec_test $1 $2 "MKS Robin v2 nano LVGL SPI + TMC"
67
+
68
+# cleanup
69
+restore_configs

Loading…
Отказ
Запис