Browse Source

LPC: Finish DMA transfer, use HW SPI class (#19191)

Victor Oliveira 4 years ago
parent
commit
160f70be63
No account linked to committer's email address

+ 33
- 76
Marlin/src/HAL/LPC1768/HAL_SPI.cpp View File

100
 
100
 
101
 #else
101
 #else
102
 
102
 
103
-  // decide which HW SPI device to use
104
-  #ifndef LPC_HW_SPI_DEV
105
-    #if (SCK_PIN == P0_07 && MISO_PIN == P0_08 && MOSI_PIN == P0_09)
106
-      #define LPC_HW_SPI_DEV 1
107
-    #else
108
-      #if (SCK_PIN == P0_15 && MISO_PIN == P0_17 && MOSI_PIN == P0_18)
109
-        #define LPC_HW_SPI_DEV 0
110
-      #else
111
-        #error "Invalid pins selected for hardware SPI"
112
-      #endif
113
-    #endif
114
-  #endif
115
-  #if LPC_HW_SPI_DEV == 0
116
-    #define LPC_SSPn LPC_SSP0
117
-  #else
118
-    #define LPC_SSPn LPC_SSP1
119
-  #endif
120
-
121
   void spiBegin() {  // setup SCK, MOSI & MISO pins for SSP0
103
   void spiBegin() {  // setup SCK, MOSI & MISO pins for SSP0
122
-    PINSEL_CFG_Type PinCfg;  // data structure to hold init values
123
-    PinCfg.Funcnum = 2;
124
-    PinCfg.OpenDrain = 0;
125
-    PinCfg.Pinmode = 0;
126
-    PinCfg.Pinnum = LPC176x::pin_bit(SCK_PIN);
127
-    PinCfg.Portnum = LPC176x::pin_port(SCK_PIN);
128
-    PINSEL_ConfigPin(&PinCfg);
129
-    SET_OUTPUT(SCK_PIN);
130
-
131
-    PinCfg.Pinnum = LPC176x::pin_bit(MISO_PIN);
132
-    PinCfg.Portnum = LPC176x::pin_port(MISO_PIN);
133
-    PINSEL_ConfigPin(&PinCfg);
134
-    SET_INPUT(MISO_PIN);
135
-
136
-    PinCfg.Pinnum = LPC176x::pin_bit(MOSI_PIN);
137
-    PinCfg.Portnum = LPC176x::pin_port(MOSI_PIN);
138
-    PINSEL_ConfigPin(&PinCfg);
139
-    SET_OUTPUT(MOSI_PIN);
140
-    // divide PCLK by 2 for SSP0
141
-    CLKPWR_SetPCLKDiv(LPC_HW_SPI_DEV == 0 ? CLKPWR_PCLKSEL_SSP0 : CLKPWR_PCLKSEL_SSP1, CLKPWR_PCLKSEL_CCLK_DIV_2);
142
-    spiInit(0);
143
-    SSP_Cmd(LPC_SSPn, ENABLE);  // start SSP running
104
+    spiInit(SPI_SPEED);
144
   }
105
   }
145
 
106
 
146
   void spiInit(uint8_t spiRate) {
107
   void spiInit(uint8_t spiRate) {
147
-    // table to convert Marlin spiRates (0-5 plus default) into bit rates
148
-    uint32_t Marlin_speed[7]; // CPSR is always 2
149
-    Marlin_speed[0] = 8333333; //(SCR:  2)  desired: 8,000,000  actual: 8,333,333  +4.2%  SPI_FULL_SPEED
150
-    Marlin_speed[1] = 4166667; //(SCR:  5)  desired: 4,000,000  actual: 4,166,667  +4.2%  SPI_HALF_SPEED
151
-    Marlin_speed[2] = 2083333; //(SCR: 11)  desired: 2,000,000  actual: 2,083,333  +4.2%  SPI_QUARTER_SPEED
152
-    Marlin_speed[3] = 1000000; //(SCR: 24)  desired: 1,000,000  actual: 1,000,000         SPI_EIGHTH_SPEED
153
-    Marlin_speed[4] =  500000; //(SCR: 49)  desired:   500,000  actual:   500,000         SPI_SPEED_5
154
-    Marlin_speed[5] =  250000; //(SCR: 99)  desired:   250,000  actual:   250,000         SPI_SPEED_6
155
-    Marlin_speed[6] =  125000; //(SCR:199)  desired:   125,000  actual:   125,000         Default from HAL.h
156
-    // setup for SPI mode
157
-    SSP_CFG_Type HW_SPI_init; // data structure to hold init values
158
-    SSP_ConfigStructInit(&HW_SPI_init);  // set values for SPI mode
159
-    HW_SPI_init.ClockRate = Marlin_speed[_MIN(spiRate, 6)]; // put in the specified bit rate
160
-    HW_SPI_init.Mode |= SSP_CR1_SSP_EN;
161
-    SSP_Init(LPC_SSPn, &HW_SPI_init);  // puts the values into the proper bits in the SSP0 registers
108
+    #if MISO_PIN == BOARD_SPI1_MISO_PIN
109
+      SPI.setModule(1);
110
+    #elif MISO_PIN == BOARD_SPI2_MISO_PIN
111
+      SPI.setModule(2);
112
+    #endif
113
+    SPI.setDataSize(DATA_SIZE_8BIT);
114
+    SPI.setDataMode(SPI_MODE0);
115
+
116
+    SPI.setClock(SPISettings::spiRate2Clock(spiRate));
117
+    SPI.begin();
162
   }
118
   }
163
 
119
 
164
   static uint8_t doio(uint8_t b) {
120
   static uint8_t doio(uint8_t b) {
165
-    /* send and receive a single byte */
166
-    SSP_SendData(LPC_SSPn, b & 0x00FF);
167
-    while (SSP_GetStatus(LPC_SSPn, SSP_STAT_BUSY));  // wait for it to finish
168
-    return SSP_ReceiveData(LPC_SSPn) & 0x00FF;
121
+    return SPI.transfer(b & 0x00FF) & 0x00FF;
169
   }
122
   }
170
 
123
 
171
   void spiSend(uint8_t b) { doio(b); }
124
   void spiSend(uint8_t b) { doio(b); }
224
   PINSEL_CFG_Type PinCfg;  // data structure to hold init values
177
   PINSEL_CFG_Type PinCfg;  // data structure to hold init values
225
   #if BOARD_NR_SPI >= 1
178
   #if BOARD_NR_SPI >= 1
226
     _settings[0].spi_d = LPC_SSP0;
179
     _settings[0].spi_d = LPC_SSP0;
180
+    _settings[0].dataMode = SPI_MODE0;
181
+    _settings[0].dataSize = DATA_SIZE_8BIT;
182
+    _settings[0].clock = SPI_CLOCK_MAX;
227
     // _settings[0].clockDivider = determine_baud_rate(_settings[0].spi_d, _settings[0].clock);
183
     // _settings[0].clockDivider = determine_baud_rate(_settings[0].spi_d, _settings[0].clock);
228
     PinCfg.Funcnum = 2;
184
     PinCfg.Funcnum = 2;
229
     PinCfg.OpenDrain = 0;
185
     PinCfg.OpenDrain = 0;
246
 
202
 
247
   #if BOARD_NR_SPI >= 2
203
   #if BOARD_NR_SPI >= 2
248
     _settings[1].spi_d = LPC_SSP1;
204
     _settings[1].spi_d = LPC_SSP1;
205
+    _settings[1].dataMode = SPI_MODE0;
206
+    _settings[1].dataSize = DATA_SIZE_8BIT;
207
+    _settings[1].clock = SPI_CLOCK_MAX;
249
     // _settings[1].clockDivider = determine_baud_rate(_settings[1].spi_d, _settings[1].clock);
208
     // _settings[1].clockDivider = determine_baud_rate(_settings[1].spi_d, _settings[1].clock);
250
     PinCfg.Funcnum = 2;
209
     PinCfg.Funcnum = 2;
251
     PinCfg.OpenDrain = 0;
210
     PinCfg.OpenDrain = 0;
320
   // Destination memory - Not used
279
   // Destination memory - Not used
321
   GPDMACfg.DstMemAddr = 0;
280
   GPDMACfg.DstMemAddr = 0;
322
   // Transfer size
281
   // Transfer size
323
-  GPDMACfg.TransferSize = (minc ? length : 1);
282
+  GPDMACfg.TransferSize = length;
324
   // Transfer width
283
   // Transfer width
325
   GPDMACfg.TransferWidth = (_currentSetting->dataSize == DATA_SIZE_16BIT) ? GPDMA_WIDTH_HALFWORD : GPDMA_WIDTH_BYTE;
284
   GPDMACfg.TransferWidth = (_currentSetting->dataSize == DATA_SIZE_16BIT) ? GPDMA_WIDTH_HALFWORD : GPDMA_WIDTH_BYTE;
326
   // Transfer type
285
   // Transfer type
335
   // Enable dma on SPI
294
   // Enable dma on SPI
336
   SSP_DMACmd(_currentSetting->spi_d, SSP_DMA_TX, ENABLE);
295
   SSP_DMACmd(_currentSetting->spi_d, SSP_DMA_TX, ENABLE);
337
 
296
 
338
-  // if minc=false, I'm repeating the same byte 'length' times, as I could not find yet how do GPDMA without memory increment
339
-  do {
340
-    // Setup channel with given parameter
341
-    GPDMA_Setup(&GPDMACfg);
297
+  // only increase memory if minc is true
298
+  GPDMACfg.MemoryIncrease = (minc ? GPDMA_DMACCxControl_SI : 0);
342
 
299
 
343
-    // enabled dma
344
-    GPDMA_ChannelCmd(0, ENABLE);
300
+  // Setup channel with given parameter
301
+  GPDMA_Setup(&GPDMACfg);
345
 
302
 
346
-    // wait data transfer
347
-    while (!GPDMA_IntGetStatus(GPDMA_STAT_INTTC, 0) && !GPDMA_IntGetStatus(GPDMA_STAT_INTERR, 0)) { }
303
+  // enabled dma
304
+  GPDMA_ChannelCmd(0, ENABLE);
348
 
305
 
349
-    // clear err and int
350
-    GPDMA_ClearIntPending (GPDMA_STATCLR_INTTC, 0);
351
-    GPDMA_ClearIntPending (GPDMA_STATCLR_INTERR, 0);
306
+  // wait data transfer
307
+  while (!GPDMA_IntGetStatus(GPDMA_STAT_RAWINTTC, 0) && !GPDMA_IntGetStatus(GPDMA_STAT_RAWINTERR, 0)) { }
352
 
308
 
353
-    // dma disable
354
-    GPDMA_ChannelCmd(0, DISABLE);
309
+  // clear err and int
310
+  GPDMA_ClearIntPending (GPDMA_STATCLR_INTTC, 0);
311
+  GPDMA_ClearIntPending (GPDMA_STATCLR_INTERR, 0);
355
 
312
 
356
-    --length;
357
-  } while (!minc && length > 0);
313
+  // dma disable
314
+  GPDMA_ChannelCmd(0, DISABLE);
358
 
315
 
359
   waitSpiTxEnd(_currentSetting->spi_d);
316
   waitSpiTxEnd(_currentSetting->spi_d);
360
 
317
 
382
 }
339
 }
383
 
340
 
384
 void SPIClass::setDataMode(uint8_t dataMode) {
341
 void SPIClass::setDataMode(uint8_t dataMode) {
385
-  _currentSetting->dataSize = dataMode;
342
+  _currentSetting->dataMode = dataMode;
386
 }
343
 }
387
 
344
 
388
 void SPIClass::setDataSize(uint32_t ds) {
345
 void SPIClass::setDataSize(uint32_t ds) {

+ 1
- 1
Marlin/src/HAL/LPC1768/inc/SanityCheck.h View File

24
 #if PIO_PLATFORM_VERSION < 1001
24
 #if PIO_PLATFORM_VERSION < 1001
25
   #error "nxplpc-arduino-lpc176x package is out of date, Please update the PlatformIO platforms, frameworks and libraries. You may need to remove the platform and let it reinstall automatically."
25
   #error "nxplpc-arduino-lpc176x package is out of date, Please update the PlatformIO platforms, frameworks and libraries. You may need to remove the platform and let it reinstall automatically."
26
 #endif
26
 #endif
27
-#if PIO_FRAMEWORK_VERSION < 2002
27
+#if PIO_FRAMEWORK_VERSION < 2005
28
   #error "framework-arduino-lpc176x package is out of date, Please update the PlatformIO platforms, frameworks and libraries."
28
   #error "framework-arduino-lpc176x package is out of date, Please update the PlatformIO platforms, frameworks and libraries."
29
 #endif
29
 #endif
30
 
30
 

+ 18
- 4
Marlin/src/HAL/LPC1768/include/SPI.h View File

61
 
61
 
62
 class SPISettings {
62
 class SPISettings {
63
 public:
63
 public:
64
-  SPISettings(uint32_t speed, int, int) : spi_speed(speed) {};
64
+  SPISettings(uint32_t spiRate, int inBitOrder, int inDataMode) {
65
+    init_AlwaysInline(spiRate2Clock(spiRate), inBitOrder, inDataMode, DATA_SIZE_8BIT);
66
+  }
65
   SPISettings(uint32_t inClock, uint8_t inBitOrder, uint8_t inDataMode, uint32_t inDataSize) {
67
   SPISettings(uint32_t inClock, uint8_t inBitOrder, uint8_t inDataMode, uint32_t inDataSize) {
66
     if (__builtin_constant_p(inClock))
68
     if (__builtin_constant_p(inClock))
67
       init_AlwaysInline(inClock, inBitOrder, inDataMode, inDataSize);
69
       init_AlwaysInline(inClock, inBitOrder, inDataMode, inDataSize);
72
     init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0, DATA_SIZE_8BIT);
74
     init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0, DATA_SIZE_8BIT);
73
   }
75
   }
74
 
76
 
75
-  uint32_t spiRate() const { return spi_speed; }
77
+  //uint32_t spiRate() const { return spi_speed; }
78
+
79
+  static inline uint32_t spiRate2Clock(uint32_t spiRate) {
80
+    uint32_t Marlin_speed[7]; // CPSR is always 2
81
+    Marlin_speed[0] = 8333333; //(SCR:  2)  desired: 8,000,000  actual: 8,333,333  +4.2%  SPI_FULL_SPEED
82
+    Marlin_speed[1] = 4166667; //(SCR:  5)  desired: 4,000,000  actual: 4,166,667  +4.2%  SPI_HALF_SPEED
83
+    Marlin_speed[2] = 2083333; //(SCR: 11)  desired: 2,000,000  actual: 2,083,333  +4.2%  SPI_QUARTER_SPEED
84
+    Marlin_speed[3] = 1000000; //(SCR: 24)  desired: 1,000,000  actual: 1,000,000         SPI_EIGHTH_SPEED
85
+    Marlin_speed[4] =  500000; //(SCR: 49)  desired:   500,000  actual:   500,000         SPI_SPEED_5
86
+    Marlin_speed[5] =  250000; //(SCR: 99)  desired:   250,000  actual:   250,000         SPI_SPEED_6
87
+    Marlin_speed[6] =  125000; //(SCR:199)  desired:   125,000  actual:   125,000         Default from HAL.h
88
+    return Marlin_speed[spiRate > 6 ? 6 : spiRate];
89
+  }
76
 
90
 
77
 private:
91
 private:
78
   void init_MightInline(uint32_t inClock, uint8_t inBitOrder, uint8_t inDataMode, uint32_t inDataSize) {
92
   void init_MightInline(uint32_t inClock, uint8_t inBitOrder, uint8_t inDataMode, uint32_t inDataSize) {
85
     dataSize = inDataSize;
99
     dataSize = inDataSize;
86
   }
100
   }
87
 
101
 
88
-  uint32_t spi_speed;
102
+  //uint32_t spi_speed;
89
   uint32_t clock;
103
   uint32_t clock;
90
   uint32_t dataSize;
104
   uint32_t dataSize;
91
   //uint32_t clockDivider;
105
   //uint32_t clockDivider;
122
   void end();
136
   void end();
123
 
137
 
124
   void beginTransaction(const SPISettings&);
138
   void beginTransaction(const SPISettings&);
125
-  void endTransaction() {};
139
+  void endTransaction() {}
126
 
140
 
127
   // Transfer using 1 "Data Size"
141
   // Transfer using 1 "Data Size"
128
   uint8_t transfer(uint16_t data);
142
   uint8_t transfer(uint16_t data);

+ 0
- 1
Marlin/src/HAL/LPC1768/tft/xpt2046.cpp View File

72
   if (!isTouched()) return false;
72
   if (!isTouched()) return false;
73
   *x = getRawData(XPT2046_X);
73
   *x = getRawData(XPT2046_X);
74
   *y = getRawData(XPT2046_Y);
74
   *y = getRawData(XPT2046_Y);
75
-  SERIAL_ECHOLNPAIR("X: ", *x, ", Y: ", *y);
76
   return isTouched();
75
   return isTouched();
77
 }
76
 }
78
 
77
 

+ 8
- 9
Marlin/src/pins/lpc1768/pins_BTT_SKR_V1_4.h View File

275
     #define LCD_BACKLIGHT_PIN              -1
275
     #define LCD_BACKLIGHT_PIN              -1
276
 
276
 
277
   #elif HAS_SPI_TFT                               // Config for Classic UI (emulated DOGM) and Color UI
277
   #elif HAS_SPI_TFT                               // Config for Classic UI (emulated DOGM) and Color UI
278
-    #define SS_PIN                         -1
279
-    //#define ONBOARD_SD_CS_PIN            -1
280
-
281
     #define TFT_CS_PIN                     P1_22
278
     #define TFT_CS_PIN                     P1_22
282
     #define TFT_A0_PIN                     P1_23
279
     #define TFT_A0_PIN                     P1_23
283
     #define TFT_DC_PIN                     P1_23
280
     #define TFT_DC_PIN                     P1_23
285
     #define TFT_BACKLIGHT_PIN              P1_18
282
     #define TFT_BACKLIGHT_PIN              P1_18
286
     #define TFT_RESET_PIN                  P1_19
283
     #define TFT_RESET_PIN                  P1_19
287
 
284
 
288
-    #define LPC_HW_SPI_DEV                     0
289
     #define LCD_USE_DMA_SPI
285
     #define LCD_USE_DMA_SPI
290
 
286
 
291
     #define TOUCH_INT_PIN                  P1_21
287
     #define TOUCH_INT_PIN                  P1_21
297
       #define GRAPHICAL_TFT_UPSCALE            3
293
       #define GRAPHICAL_TFT_UPSCALE            3
298
     #endif
294
     #endif
299
 
295
 
300
-    // SPI 1
301
-    #define SCK_PIN                        P0_15
302
-    #define MISO_PIN                       P0_17
303
-    #define MOSI_PIN                       P0_18
304
-
305
     // Disable any LCD related PINs config
296
     // Disable any LCD related PINs config
306
     #define LCD_PINS_ENABLE                -1
297
     #define LCD_PINS_ENABLE                -1
307
     #define LCD_PINS_RS                    -1
298
     #define LCD_PINS_RS                    -1
308
 
299
 
300
+    // Emulated DOGM have xpt calibration values independent of display resolution
301
+    #if ENABLED(SPI_GRAPHICAL_TFT)
302
+      #define XPT2046_X_CALIBRATION      -11245
303
+      #define XPT2046_Y_CALIBRATION        8629
304
+      #define XPT2046_X_OFFSET              685
305
+      #define XPT2046_Y_OFFSET             -285
306
+    #endif
307
+
309
   #else
308
   #else
310
 
309
 
311
     #define BTN_ENC                        P0_28  // (58) open-drain
310
     #define BTN_ENC                        P0_28  // (58) open-drain

+ 1
- 0
platformio.ini View File

623
 #
623
 #
624
 [common_LPC]
624
 [common_LPC]
625
 platform          = https://github.com/p3p/pio-nxplpc-arduino-lpc176x/archive/0.1.3.zip
625
 platform          = https://github.com/p3p/pio-nxplpc-arduino-lpc176x/archive/0.1.3.zip
626
+platform_packages = framework-arduino-lpc176x@^0.2.5
626
 board             = nxp_lpc1768
627
 board             = nxp_lpc1768
627
 lib_ldf_mode      = off
628
 lib_ldf_mode      = off
628
 lib_compat_mode   = strict
629
 lib_compat_mode   = strict

Loading…
Cancel
Save