|
@@ -100,72 +100,25 @@
|
100
|
100
|
|
101
|
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
|
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
|
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
|
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
|
124
|
void spiSend(uint8_t b) { doio(b); }
|
|
@@ -224,6 +177,9 @@ SPIClass::SPIClass(uint8_t device) {
|
224
|
177
|
PINSEL_CFG_Type PinCfg; // data structure to hold init values
|
225
|
178
|
#if BOARD_NR_SPI >= 1
|
226
|
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
|
183
|
// _settings[0].clockDivider = determine_baud_rate(_settings[0].spi_d, _settings[0].clock);
|
228
|
184
|
PinCfg.Funcnum = 2;
|
229
|
185
|
PinCfg.OpenDrain = 0;
|
|
@@ -246,6 +202,9 @@ SPIClass::SPIClass(uint8_t device) {
|
246
|
202
|
|
247
|
203
|
#if BOARD_NR_SPI >= 2
|
248
|
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
|
208
|
// _settings[1].clockDivider = determine_baud_rate(_settings[1].spi_d, _settings[1].clock);
|
250
|
209
|
PinCfg.Funcnum = 2;
|
251
|
210
|
PinCfg.OpenDrain = 0;
|
|
@@ -320,7 +279,7 @@ void SPIClass::dmaSend(void *buf, uint16_t length, bool minc) {
|
320
|
279
|
// Destination memory - Not used
|
321
|
280
|
GPDMACfg.DstMemAddr = 0;
|
322
|
281
|
// Transfer size
|
323
|
|
- GPDMACfg.TransferSize = (minc ? length : 1);
|
|
282
|
+ GPDMACfg.TransferSize = length;
|
324
|
283
|
// Transfer width
|
325
|
284
|
GPDMACfg.TransferWidth = (_currentSetting->dataSize == DATA_SIZE_16BIT) ? GPDMA_WIDTH_HALFWORD : GPDMA_WIDTH_BYTE;
|
326
|
285
|
// Transfer type
|
|
@@ -335,26 +294,24 @@ void SPIClass::dmaSend(void *buf, uint16_t length, bool minc) {
|
335
|
294
|
// Enable dma on SPI
|
336
|
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
|
316
|
waitSpiTxEnd(_currentSetting->spi_d);
|
360
|
317
|
|
|
@@ -382,7 +339,7 @@ void SPIClass::setBitOrder(uint8_t bitOrder) {
|
382
|
339
|
}
|
383
|
340
|
|
384
|
341
|
void SPIClass::setDataMode(uint8_t dataMode) {
|
385
|
|
- _currentSetting->dataSize = dataMode;
|
|
342
|
+ _currentSetting->dataMode = dataMode;
|
386
|
343
|
}
|
387
|
344
|
|
388
|
345
|
void SPIClass::setDataSize(uint32_t ds) {
|