Skip to content

Commit

Permalink
Fix SDIO stability problem
Browse files Browse the repository at this point in the history
  • Loading branch information
jmz52 committed Mar 27, 2019
1 parent cc39d62 commit e1a29e3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 48 deletions.
5 changes: 5 additions & 0 deletions Marlin/src/HAL/HAL_STM32F1/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ void HAL_init();
#define analogInputToDigitalPin(p) (p)
#endif

#ifndef digitalPinHasPWM
#define digitalPinHasPWM(P) (PIN_MAP[P].timer_device != NULL)
#endif


#define CRITICAL_SECTION_START uint32_t primask = __get_primask(); (void)__iCliRetVal()
#define CRITICAL_SECTION_END if (!primask) (void)__iSeiRetVal()
#define ISRS_ENABLED() (!__get_primask())
Expand Down
17 changes: 16 additions & 1 deletion Marlin/src/HAL/HAL_STM32F1/HAL_sdio_STM32F1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ bool SDIO_Init(void) {

sdio_set_dbus_width(SDIO_CLKCR_WIDBUS_4BIT);
sdio_set_clock(SDIO_CLOCK);

return true;
}

bool SDIO_ReadBlock(uint32_t blockAddress, uint8_t *data) {
bool SDIO_ReadBlock_DMA(uint32_t blockAddress, uint8_t *data) {
if (SDIO_GetCardState() != SDIO_CARD_TRANSFER) return false;
if (blockAddress >= SdCard.LogBlockNbr) return false;
if ((0x03 & (uint32_t)data)) return false; // misaligned data
Expand All @@ -100,6 +101,12 @@ bool SDIO_ReadBlock(uint32_t blockAddress, uint8_t *data) {

dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);

if (SDIO->STA & SDIO_STA_RXDAVL) {
while (SDIO->STA & SDIO_STA_RXDAVL) (void) SDIO->FIFO;
SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
return false;
}

if (SDIO_GET_FLAG(SDIO_STA_TRX_ERROR_FLAGS)) {
SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
return false;
Expand All @@ -108,6 +115,14 @@ bool SDIO_ReadBlock(uint32_t blockAddress, uint8_t *data) {
return true;
}

bool SDIO_ReadBlock(uint32_t blockAddress, uint8_t *data) {
uint32_t retries = 3;
while (retries--) {
if (SDIO_ReadBlock_DMA(blockAddress, data)) return true;
}
return false;
}

bool SDIO_WriteBlock(uint32_t blockAddress, const uint8_t *data) {
if (SDIO_GetCardState() != SDIO_CARD_TRANSFER) return false;
if (blockAddress >= SdCard.LogBlockNbr) return false;
Expand Down
103 changes: 58 additions & 45 deletions Marlin/src/HAL/HAL_STM32F1/u8g_com_stm32duino_fsmc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "../../inc/MarlinConfig.h"

#if HAS_GRAPHICAL_LCD
#if defined(STM32F1) || defined(STM32F1xx)
#if defined(ARDUINO_ARCH_STM32F1) && (defined(STM32_HIGH_DENSITY) || defined(STM32_XL_DENSITY))

#include "U8glib.h"
#include "libmaple/fsmc.h"
Expand Down Expand Up @@ -109,26 +109,30 @@ __attribute__((always_inline)) __STATIC_INLINE void __DSB(void) {
}

#define FSMC_CS_NE1 PD7
#define FSMC_CS_NE2 PG9
#define FSMC_CS_NE3 PG10
#define FSMC_CS_NE4 PG12

#define FSMC_RS_A0 PF0
#define FSMC_RS_A1 PF1
#define FSMC_RS_A2 PF2
#define FSMC_RS_A3 PF3
#define FSMC_RS_A4 PF4
#define FSMC_RS_A5 PF5
#define FSMC_RS_A6 PF12
#define FSMC_RS_A7 PF13
#define FSMC_RS_A8 PF14
#define FSMC_RS_A9 PF15
#define FSMC_RS_A10 PG0
#define FSMC_RS_A11 PG1
#define FSMC_RS_A12 PG2
#define FSMC_RS_A13 PG3
#define FSMC_RS_A14 PG4
#define FSMC_RS_A15 PG5

#ifdef STM32_XL_DENSITY
#define FSMC_CS_NE2 PG9
#define FSMC_CS_NE3 PG10
#define FSMC_CS_NE4 PG12

#define FSMC_RS_A0 PF0
#define FSMC_RS_A1 PF1
#define FSMC_RS_A2 PF2
#define FSMC_RS_A3 PF3
#define FSMC_RS_A4 PF4
#define FSMC_RS_A5 PF5
#define FSMC_RS_A6 PF12
#define FSMC_RS_A7 PF13
#define FSMC_RS_A8 PF14
#define FSMC_RS_A9 PF15
#define FSMC_RS_A10 PG0
#define FSMC_RS_A11 PG1
#define FSMC_RS_A12 PG2
#define FSMC_RS_A13 PG3
#define FSMC_RS_A14 PG4
#define FSMC_RS_A15 PG5
#endif

#define FSMC_RS_A16 PD11
#define FSMC_RS_A17 PD12
#define FSMC_RS_A18 PD13
Expand All @@ -137,8 +141,11 @@ __attribute__((always_inline)) __STATIC_INLINE void __DSB(void) {
#define FSMC_RS_A21 PE5
#define FSMC_RS_A22 PE6
#define FSMC_RS_A23 PE2
#define FSMC_RS_A24 PG13
#define FSMC_RS_A25 PG14

#ifdef STM32_XL_DENSITY
#define FSMC_RS_A24 PG13
#define FSMC_RS_A25 PG14
#endif

static uint8_t fsmcInit = 0;

Expand All @@ -157,31 +164,35 @@ void LCD_IO_Init(uint8_t cs, uint8_t rs) {

switch(cs) {
case FSMC_CS_NE1: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION1; break;
case FSMC_CS_NE2: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION2; break;
case FSMC_CS_NE3: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION3; break;
case FSMC_CS_NE4: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION4; break;
#ifdef STM32_XL_DENSITY
case FSMC_CS_NE2: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION2; break;
case FSMC_CS_NE3: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION3; break;
case FSMC_CS_NE4: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION4; break;
#endif
default: return;
}

#define _ORADDR(N) controllerAddress |= (_BV32(N) - 2)

switch(rs) {
case FSMC_RS_A0: _ORADDR( 1); break;
case FSMC_RS_A1: _ORADDR( 2); break;
case FSMC_RS_A2: _ORADDR( 3); break;
case FSMC_RS_A3: _ORADDR( 4); break;
case FSMC_RS_A4: _ORADDR( 5); break;
case FSMC_RS_A5: _ORADDR( 6); break;
case FSMC_RS_A6: _ORADDR( 7); break;
case FSMC_RS_A7: _ORADDR( 8); break;
case FSMC_RS_A8: _ORADDR( 9); break;
case FSMC_RS_A9: _ORADDR(10); break;
case FSMC_RS_A10: _ORADDR(11); break;
case FSMC_RS_A11: _ORADDR(12); break;
case FSMC_RS_A12: _ORADDR(13); break;
case FSMC_RS_A13: _ORADDR(14); break;
case FSMC_RS_A14: _ORADDR(15); break;
case FSMC_RS_A15: _ORADDR(16); break;
#ifdef STM32_XL_DENSITY
case FSMC_RS_A0: _ORADDR( 1); break;
case FSMC_RS_A1: _ORADDR( 2); break;
case FSMC_RS_A2: _ORADDR( 3); break;
case FSMC_RS_A3: _ORADDR( 4); break;
case FSMC_RS_A4: _ORADDR( 5); break;
case FSMC_RS_A5: _ORADDR( 6); break;
case FSMC_RS_A6: _ORADDR( 7); break;
case FSMC_RS_A7: _ORADDR( 8); break;
case FSMC_RS_A8: _ORADDR( 9); break;
case FSMC_RS_A9: _ORADDR(10); break;
case FSMC_RS_A10: _ORADDR(11); break;
case FSMC_RS_A11: _ORADDR(12); break;
case FSMC_RS_A12: _ORADDR(13); break;
case FSMC_RS_A13: _ORADDR(14); break;
case FSMC_RS_A14: _ORADDR(15); break;
case FSMC_RS_A15: _ORADDR(16); break;
#endif
case FSMC_RS_A16: _ORADDR(17); break;
case FSMC_RS_A17: _ORADDR(18); break;
case FSMC_RS_A18: _ORADDR(19); break;
Expand All @@ -190,8 +201,10 @@ void LCD_IO_Init(uint8_t cs, uint8_t rs) {
case FSMC_RS_A21: _ORADDR(22); break;
case FSMC_RS_A22: _ORADDR(23); break;
case FSMC_RS_A23: _ORADDR(24); break;
case FSMC_RS_A24: _ORADDR(25); break;
case FSMC_RS_A25: _ORADDR(26); break;
#ifdef STM32_XL_DENSITY
case FSMC_RS_A24: _ORADDR(25); break;
case FSMC_RS_A25: _ORADDR(26); break;
#endif
default: return;
}

Expand Down Expand Up @@ -266,5 +279,5 @@ void LCD_IO_WriteSequence(uint16_t *data, uint16_t length) {
dma_disable(FSMC_DMA_DEV, FSMC_DMA_CHANNEL);
}

#endif // STM32F1 || STM32F1xx
#endif // ARDUINO_ARCH_STM32F1 && (STM32_HIGH_DENSITY || STM32_XL_DENSITY)
#endif // HAS_GRAPHICAL_LCD
2 changes: 1 addition & 1 deletion Marlin/src/lcd/dogm/HAL_LCD_com_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
uint8_t u8g_com_HAL_LPC1768_ssd_hw_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr);
#define U8G_COM_SSD_I2C_HAL u8g_com_arduino_ssd_i2c_fn

#if defined(STM32F1) || defined(STM32F1xx)
#if defined(ARDUINO_ARCH_STM32F1)
uint8_t u8g_com_stm32duino_fsmc_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr);
#define U8G_COM_HAL_FSMC_FN u8g_com_stm32duino_fsmc_fn
#else
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ board = genericSTM32F103ZE
extra_scripts = buildroot/share/PlatformIO/scripts/mks_robin.py
build_flags = !python Marlin/src/HAL/HAL_STM32F1/STM32F1_flag_script.py
${common.build_flags}
-DSTM32F1 -DdigitalPinHasPWM(P)='(PIN_MAP[P].timer_device != NULL)'
-DSTM32_XL_DENSITY
src_filter = ${common.default_src_filter} +<src/HAL/HAL_STM32F1>
lib_deps = ${common.lib_deps}
lib_ignore = c1921b4
Expand Down

0 comments on commit e1a29e3

Please sign in to comment.