Skip to content

Commit

Permalink
rev #5 part2
Browse files Browse the repository at this point in the history
  • Loading branch information
ursoft committed Jan 25, 2020
1 parent 9cabea9 commit ceb69e7
Show file tree
Hide file tree
Showing 16 changed files with 258 additions and 300 deletions.
2 changes: 1 addition & 1 deletion Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@
*
* :[ 'LCD', 'ONBOARD', 'CUSTOM_CABLE', 'LCD_AND_ONBOARD' ]
*/
#define SDCARD_CONNECTION LCD_AND_ONBOARD
//#define SDCARD_CONNECTION LCD
#endif

#endif // SDSUPPORT
Expand Down
139 changes: 139 additions & 0 deletions Marlin/src/HAL/HAL_LPC1768/HAL_SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,145 @@

#else

// Hardware SPI

#include <lpc17xx_pinsel.h>
#include <lpc17xx_ssp.h>
#include <lpc17xx_clkpwr.h>

// decide which HW SPI device to use
#ifndef LPC_HW_SPI_DEV
#if (SCK_PIN == P0_07 && MISO_PIN == P0_08 && MOSI_PIN == P0_09)
#define LPC_HW_SPI_DEV 1
#else
#if (SCK_PIN == P0_15 && MISO_PIN == P0_17 && MOSI_PIN == P0_18)
#define LPC_HW_SPI_DEV 0
#else
#error "Invalid pins selected for hardware SPI"
#endif
#endif
#endif
#if (LPC_HW_SPI_DEV == 0)
#define LPC_SSPn LPC_SSP0
#else
#define LPC_SSPn LPC_SSP1
#endif

void spiBegin() { // setup SCK, MOSI & MISO pins for SSP0
PINSEL_CFG_Type PinCfg; // data structure to hold init values
PinCfg.Funcnum = 2;
PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0;
PinCfg.Pinnum = LPC176x::pin_bit(SCK_PIN);
PinCfg.Portnum = LPC176x::pin_port(SCK_PIN);
PINSEL_ConfigPin(&PinCfg);
SET_OUTPUT(SCK_PIN);

PinCfg.Pinnum = LPC176x::pin_bit(MISO_PIN);
PinCfg.Portnum = LPC176x::pin_port(MISO_PIN);
PINSEL_ConfigPin(&PinCfg);
SET_INPUT(MISO_PIN);

PinCfg.Pinnum = LPC176x::pin_bit(MOSI_PIN);
PinCfg.Portnum = LPC176x::pin_port(MOSI_PIN);
PINSEL_ConfigPin(&PinCfg);
SET_OUTPUT(MOSI_PIN);
// divide PCLK by 2 for SSP0
CLKPWR_SetPCLKDiv(LPC_HW_SPI_DEV == 0 ? CLKPWR_PCLKSEL_SSP0 : CLKPWR_PCLKSEL_SSP1, CLKPWR_PCLKSEL_CCLK_DIV_2);
spiInit(0);
SSP_Cmd(LPC_SSPn, ENABLE); // start SSP running
}

void spiInit(uint8_t spiRate) {
// table to convert Marlin spiRates (0-5 plus default) into bit rates
uint32_t Marlin_speed[7]; // CPSR is always 2
Marlin_speed[0] = 8333333; //(SCR: 2) desired: 8,000,000 actual: 8,333,333 +4.2% SPI_FULL_SPEED
Marlin_speed[1] = 4166667; //(SCR: 5) desired: 4,000,000 actual: 4,166,667 +4.2% SPI_HALF_SPEED
Marlin_speed[2] = 2083333; //(SCR: 11) desired: 2,000,000 actual: 2,083,333 +4.2% SPI_QUARTER_SPEED
Marlin_speed[3] = 1000000; //(SCR: 24) desired: 1,000,000 actual: 1,000,000 SPI_EIGHTH_SPEED
Marlin_speed[4] = 500000; //(SCR: 49) desired: 500,000 actual: 500,000 SPI_SPEED_5
Marlin_speed[5] = 250000; //(SCR: 99) desired: 250,000 actual: 250,000 SPI_SPEED_6
Marlin_speed[6] = 125000; //(SCR:199) desired: 125,000 actual: 125,000 Default from HAL.h
// setup for SPI mode
SSP_CFG_Type HW_SPI_init; // data structure to hold init values
SSP_ConfigStructInit(&HW_SPI_init); // set values for SPI mode
HW_SPI_init.ClockRate = Marlin_speed[_MIN(spiRate, 6)]; // put in the specified bit rate
HW_SPI_init.Mode |= SSP_CR1_SSP_EN;
SSP_Init(LPC_SSPn, &HW_SPI_init); // puts the values into the proper bits in the SSP0 registers
}

static uint8_t doio(uint8_t b) {
/* send and receive a single byte */
SSP_SendData(LPC_SSPn, b & 0x00FF);
while (SSP_GetStatus(LPC_SSPn, SSP_STAT_BUSY)); // wait for it to finish
return SSP_ReceiveData(LPC_SSPn) & 0x00FF;
}

void spiSend(uint8_t b) { doio(b); }

void spiSend(const uint8_t* buf, size_t nbyte) {
for (uint16_t i = 0; i < nbyte; i++) doio(buf[i]);
}

void spiSend(uint32_t chan, byte b) {
}

void spiSend(uint32_t chan, const uint8_t* buf, size_t nbyte) {
}

// Read single byte from SPI
uint8_t spiRec() { return doio(0xFF); }

uint8_t spiRec(uint32_t chan) { return 0; }

// Read from SPI into buffer
void spiRead(uint8_t *buf, uint16_t nbyte) {
for (uint16_t i = 0; i < nbyte; i++) buf[i] = doio(0xFF);
}

static uint8_t spiTransfer(uint8_t b) {
return doio(b);
}

// Write from buffer to SPI
void spiSendBlock(uint8_t token, const uint8_t* buf) {
(void)spiTransfer(token);
for (uint16_t i = 0; i < 512; i++)
(void)spiTransfer(buf[i]);
}

/** Begin SPI transaction, set clock, bit order, data mode */
void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
// TODO: to be implemented

}

#endif // ENABLED(LPC_SOFTWARE_SPI)

void SPIClass::begin() { spiBegin(); }

void SPIClass::beginTransaction(SPISettings cfg) {
uint8_t spiRate;
switch (cfg.spiRate()) {
case 8000000: spiRate = 0; break;
case 4000000: spiRate = 1; break;
case 2000000: spiRate = 2; break;
case 1000000: spiRate = 3; break;
case 500000: spiRate = 4; break;
case 250000: spiRate = 5; break;
case 125000: spiRate = 6; break;
default: spiRate = 2; break;
}
spiInit(spiRate);
}

uint8_t SPIClass::transfer(const uint8_t B) { return spiTransfer(B); }

uint16_t SPIClass::transfer16(const uint16_t data) {
return (transfer((data >> 8) & 0xFF) << 8)
| (transfer(data & 0xFF) & 0xFF);
}

SPIClass SPI;

#endif // TARGET_LPC1768
146 changes: 6 additions & 140 deletions Marlin/src/HAL/HAL_LPC1768/include/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,147 +36,13 @@ class SPISettings {
uint32_t spi_speed;
};

#ifdef NEED_SPICLASS
// decide which HW SPI device to use
#ifndef LPC_HW_SPI_DEV
#if (SCK_PIN == P0_07 && MISO_PIN == P0_08 && MOSI_PIN == P0_09)
#define _LPC_HW_SPI_DEV 1
#else
#if (SCK_PIN == P0_15 && MISO_PIN == P0_17 && MOSI_PIN == P0_18)
#define _LPC_HW_SPI_DEV 0
#else
#error "Invalid pins selected for hardware SPI"
#endif
#endif
#endif
#if (LPC_HW_SPI_DEV == 0)
#define _LPC_SSPn LPC_SSP0
#else
#define _LPC_SSPn LPC_SSP1
#endif

// Hardware SPI

#include <lpc17xx_pinsel.h>
#include <lpc17xx_ssp.h>
#include <lpc17xx_clkpwr.h>

template<int NT_SCK_PIN = SCK_PIN, int NT_MISO_PIN = MISO_PIN, int NT_MOSI_PIN = MOSI_PIN, int NT_LPC_HW_SPI_DEV = _LPC_HW_SPI_DEV>
class SPIClass {
public:
static void spiBegin() { // setup SCK, MOSI & MISO pins for SSP0
PINSEL_CFG_Type PinCfg; // data structure to hold init values
PinCfg.Funcnum = 2;
PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0;
PinCfg.Pinnum = LPC176x::pin_bit(NT_SCK_PIN);
PinCfg.Portnum = LPC176x::pin_port(NT_SCK_PIN);
PINSEL_ConfigPin(&PinCfg);
SET_OUTPUT(NT_SCK_PIN);

PinCfg.Pinnum = LPC176x::pin_bit(NT_MISO_PIN);
PinCfg.Portnum = LPC176x::pin_port(NT_MISO_PIN);
PINSEL_ConfigPin(&PinCfg);
SET_INPUT(NT_MISO_PIN);

PinCfg.Pinnum = LPC176x::pin_bit(NT_MOSI_PIN);
PinCfg.Portnum = LPC176x::pin_port(NT_MOSI_PIN);
PINSEL_ConfigPin(&PinCfg);
SET_OUTPUT(NT_MOSI_PIN);
// divide PCLK by 2 for SSP0
CLKPWR_SetPCLKDiv(NT_LPC_HW_SPI_DEV ? CLKPWR_PCLKSEL_SSP1 : CLKPWR_PCLKSEL_SSP0, CLKPWR_PCLKSEL_CCLK_DIV_2);
spiInit(0);
SSP_Cmd(NT_LPC_HW_SPI_DEV ? LPC_SSP1 : LPC_SSP0, ENABLE); // start SSP running
}

static void spiInit(uint8_t spiRate) {
// table to convert Marlin spiRates (0-5 plus default) into bit rates
uint32_t Marlin_speed[7]; // CPSR is always 2
Marlin_speed[0] = 8333333; //(SCR: 2) desired: 8,000,000 actual: 8,333,333 +4.2% SPI_FULL_SPEED
Marlin_speed[1] = 4166667; //(SCR: 5) desired: 4,000,000 actual: 4,166,667 +4.2% SPI_HALF_SPEED
Marlin_speed[2] = 2083333; //(SCR: 11) desired: 2,000,000 actual: 2,083,333 +4.2% SPI_QUARTER_SPEED
Marlin_speed[3] = 1000000; //(SCR: 24) desired: 1,000,000 actual: 1,000,000 SPI_EIGHTH_SPEED
Marlin_speed[4] = 500000; //(SCR: 49) desired: 500,000 actual: 500,000 SPI_SPEED_5
Marlin_speed[5] = 250000; //(SCR: 99) desired: 250,000 actual: 250,000 SPI_SPEED_6
Marlin_speed[6] = 125000; //(SCR:199) desired: 125,000 actual: 125,000 Default from HAL.h
// setup for SPI mode
SSP_CFG_Type HW_SPI_init; // data structure to hold init values
SSP_ConfigStructInit(&HW_SPI_init); // set values for SPI mode
HW_SPI_init.ClockRate = Marlin_speed[_MIN(spiRate, 6)]; // put in the specified bit rate
HW_SPI_init.Mode |= SSP_CR1_SSP_EN;
SSP_Init(NT_LPC_HW_SPI_DEV ? LPC_SSP1 : LPC_SSP0, &HW_SPI_init); // puts the values into the proper bits in the SSP0 registers
}

static uint8_t doio(uint8_t b) {
/* send and receive a single byte */
SSP_SendData(NT_LPC_HW_SPI_DEV ? LPC_SSP1 : LPC_SSP0, b & 0x00FF);
while (SSP_GetStatus(NT_LPC_HW_SPI_DEV ? LPC_SSP1 : LPC_SSP0, SSP_STAT_BUSY)); // wait for it to finish
return SSP_ReceiveData(NT_LPC_HW_SPI_DEV ? LPC_SSP1 : LPC_SSP0) & 0x00FF;
}

static void spiSend(uint8_t b) { doio(b); }

static void spiSend(const uint8_t* buf, size_t nbyte) {
for (uint16_t i = 0; i < nbyte; i++) doio(buf[i]);
}

static void spiSend(uint32_t chan, byte b) {}

static void spiSend(uint32_t chan, const uint8_t* buf, size_t nbyte) {}

// Read single byte from SPI
static uint8_t spiRec() { return doio(0xFF); }

static uint8_t spiRec(uint32_t chan) { return 0; }

// Read from SPI into buffer
static void spiRead(uint8_t *buf, uint16_t nbyte) {
for (uint16_t i = 0; i < nbyte; i++) buf[i] = doio(0xFF);
}

static uint8_t spiTransfer(uint8_t b) {
return doio(b);
}

// Write from buffer to SPI
static void spiSendBlock(uint8_t token, const uint8_t* buf) {
(void)spiTransfer(token);
for (uint16_t i = 0; i < 512; i++)
(void)spiTransfer(buf[i]);
}

/** Begin SPI transaction, set clock, bit order, data mode */
static void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
// TODO: to be implemented
}

public:
void begin() { spiBegin(); }
void beginTransaction(SPISettings cfg) {
uint8_t spiRate;
switch (cfg.spiRate()) {
case 8000000: spiRate = 0; break;
case 4000000: spiRate = 1; break;
case 2000000: spiRate = 2; break;
case 1000000: spiRate = 3; break;
case 500000: spiRate = 4; break;
case 250000: spiRate = 5; break;
case 125000: spiRate = 6; break;
default: spiRate = 2; break;
}
spiInit(spiRate);
}
void endTransaction() {}
uint8_t transfer(uint8_t B) { return spiTransfer(B); }
uint16_t transfer16(uint16_t data) {
return (transfer((data >> 8) & 0xFF) << 8)
| (transfer(data & 0xFF) & 0xFF);
}
void begin();
void beginTransaction(SPISettings);
void endTransaction() {};
uint8_t transfer(uint8_t data);
uint16_t transfer16(uint16_t data);
};

SPIClass<> SPI;
#if SD_CONNECTION_IS(LCD_AND_ONBOARD)
SPIClass<SCK_PIN_OB, MISO_PIN_OB, MOSI_PIN_OB, 1> SPI_OB;
#endif

#endif
extern SPIClass SPI;
17 changes: 5 additions & 12 deletions Marlin/src/HAL/HAL_LPC1768/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void HAL_init() {
delay(1000); // Give OS time to notice
USB_Connect(TRUE);

#if (!BOTH(SHARED_SD_CARD, INIT_SDCARD_ON_BOOT) && DISABLED(NO_SD_HOST_DRIVE)) || SD_CONNECTION_IS(LCD_AND_ONBOARD)
#if !BOTH(SHARED_SD_CARD, INIT_SDCARD_ON_BOOT) && DISABLED(NO_SD_HOST_DRIVE)
MSC_SD_Init(0); // Enable USB SD card access
#endif

Expand Down Expand Up @@ -158,17 +158,10 @@ void HAL_idletask() {
// the disk if Marlin has it mounted. Unfortunately there is currently no way
// to unmount the disk from the LCD menu.
// if (IS_SD_PRINTING() || IS_SD_FILE_OPEN())
#if SD_CONNECTION_IS(LCD_AND_ONBOARD)
if (card.isMountedOnBoard())
MSC_Aquire_Lock(); // no onboard for usb
else
MSC_Release_Lock(); // onboard for usb
#else
if (card.isMounted())
MSC_Aquire_Lock();
else
MSC_Release_Lock();
#endif
if (card.isMounted())
MSC_Aquire_Lock();
else
MSC_Release_Lock();
#endif
// Perform USB stack housekeeping
MSC_RunDeferredCommands();
Expand Down
5 changes: 1 addition & 4 deletions Marlin/src/Marlin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -938,10 +938,7 @@ void setup() {
#endif

#if ENABLED(SDSUPPORT)
#if SD_CONNECTION_IS(LCD_AND_ONBOARD)
if(IS_EXT_SD_INSERTED()) //auto mount only if LCD card presents
#endif
card.mount(); // Mount the SD card before settings.first_load
card.mount(); // Mount the SD card before settings.first_load
#endif

// Load data from EEPROM if available (or use defaults)
Expand Down
1 change: 0 additions & 1 deletion Marlin/src/core/drivers.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,5 @@
#define _SDCARD_LCD 1
#define _SDCARD_ONBOARD 2
#define _SDCARD_CUSTOM_CABLE 3
#define _SDCARD_LCD_AND_ONBOARD 4
#define _SDCARD_ID(V) _CAT(_SDCARD_, V)
#define SD_CONNECTION_IS(V) (_SDCARD_ID(SDCARD_CONNECTION) == _SDCARD_ID(V))
9 changes: 1 addition & 8 deletions Marlin/src/feature/power_loss_recovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,7 @@ void PrintJobRecovery::check() {
if (!card.isMounted()) card.mount();
if (card.isMounted()) {
load();
if (!valid()) {
purge();
#if SD_CONNECTION_IS(LCD_AND_ONBOARD)
if(!IS_EXT_SD_INSERTED()) //no LCD SD card
card.release();
#endif
return;
}
if (!valid()) return purge();
queue.inject_P(PSTR("M1000 S"));
}
}
Expand Down
3 changes: 0 additions & 3 deletions Marlin/src/inc/Conditionals_post.h
Original file line number Diff line number Diff line change
Expand Up @@ -1779,9 +1779,6 @@
#if DISABLED(SHARED_SD_CARD)
#define INIT_SDCARD_ON_BOOT
#endif
#if SD_CONNECTION_IS(LCD_AND_ONBOARD) && DISABLED(NO_SD_HOST_DRIVE)
#define SHARED_SD_CARD
#endif
#endif

#if !NUM_SERIAL
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/language/language_en.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ namespace Language_en {
PROGMEM Language_Str MSG_FILAMENTUNLOAD = _UxGT("Unload Filament");
PROGMEM Language_Str MSG_FILAMENTUNLOAD_E = _UxGT("Unload Filament *");
PROGMEM Language_Str MSG_FILAMENTUNLOAD_ALL = _UxGT("Unload All");
PROGMEM Language_Str MSG_INIT_MEDIA = _UxGT("Mount Media");
PROGMEM Language_Str MSG_INIT_MEDIA = _UxGT("Init. Media");
PROGMEM Language_Str MSG_CHANGE_MEDIA = _UxGT("Change Media");
PROGMEM Language_Str MSG_RELEASE_MEDIA = _UxGT("Release Media");
PROGMEM Language_Str MSG_ZPROBE_OUT = _UxGT("Z Probe Past Bed");
Expand Down
Loading

0 comments on commit ceb69e7

Please sign in to comment.