From 781b3c61880e94bf6861490baa3eabb957260297 Mon Sep 17 00:00:00 2001 From: "bernet.andre@gmail.com" Date: Wed, 20 Mar 2013 22:39:21 +0000 Subject: [PATCH] [X9D] SD card write error fixed, DMA enabled (thanks Adela), SPORT debug code --- src/Makefile | 2 +- src/main_views.cpp | 3 + src/open9x.cpp | 15 +- src/x9d/board_x9d.h | 1 + src/x9d/diskio.cpp | 285 ++++++++++++++++++++------------------ src/x9d/hal.h | 65 ++++----- src/x9d/pulses_driver.cpp | 1 + src/x9d/pwr_driver.cpp | 2 +- src/x9d/sport_driver.cpp | 18 ++- 9 files changed, 204 insertions(+), 188 deletions(-) diff --git a/src/Makefile b/src/Makefile index 9829b3d7d..b9a97cbf1 100644 --- a/src/Makefile +++ b/src/Makefile @@ -531,7 +531,7 @@ ifeq ($(PCB), X9D) SRC += x9d/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_gpio.c SRC += x9d/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_rcc.c SRC += x9d/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_spi.c - # SRC += x9d/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_dma.c + SRC += x9d/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_dma.c SRC += x9d/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_rtc.c SRC += x9d/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_pwr.c SRC += x9d/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_usart.c diff --git a/src/main_views.cpp b/src/main_views.cpp index cc19ea45a..49d413a1d 100644 --- a/src/main_views.cpp +++ b/src/main_views.cpp @@ -297,6 +297,9 @@ void displayTopBar() /* The inside of the RSSI gauge */ count = min(frskyData.rssi[0].value, frskyData.rssi[1].value) / 10; displayTopBarGauge(BAR_RSSI_X+10, count, (frskyData.rssi[0].value < getRssiAlarmValue(0)) || (frskyData.rssi[1].value < getRssiAlarmValue(1))); +// Debug - SPORTT receive counter +extern uint16_t tmp; +lcd_outdezAtt(120,2,tmp); } #endif diff --git a/src/open9x.cpp b/src/open9x.cpp index 18a6befb6..50ab23f43 100644 --- a/src/open9x.cpp +++ b/src/open9x.cpp @@ -3956,11 +3956,6 @@ inline void open9xInit(OPEN9X_INIT_ARGS) // TODO startPulses should be started after the first doMixerCalculations() #endif -#if defined(PCBX9D) - // TODO remove this line, it only allows the Mixer to run (until pulses are there!) - s_current_protocol = PROTO_PPM; -#endif - #if !defined(CPUARM) doMixerCalculations(); #endif @@ -3980,12 +3975,10 @@ void mixerTask(void * pdata) if (!s_pulses_paused) { uint16_t t0 = getTmr2MHz(); - if (s_current_protocol < PROTO_NONE) { - CoEnterMutexSection(mixerMutex); - bool tick10ms = doMixerCalculations(); - CoLeaveMutexSection(mixerMutex); - if (tick10ms) checkTrims(); - } + CoEnterMutexSection(mixerMutex); + bool tick10ms = doMixerCalculations(); + CoLeaveMutexSection(mixerMutex); + if (tick10ms) checkTrims(); if (heartbeat == HEART_TIMER_PULSES+HEART_TIMER10ms) { wdt_reset(); diff --git a/src/x9d/board_x9d.h b/src/x9d/board_x9d.h index de3a8e2f5..9b60d4e40 100644 --- a/src/x9d/board_x9d.h +++ b/src/x9d/board_x9d.h @@ -44,6 +44,7 @@ #include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/stm32f2xx_spi.h" #include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/stm32f2xx_rtc.h" #include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/stm32f2xx_pwr.h" +#include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/stm32f2xx_dma.h" #include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/stm32f2xx_usart.h" #include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/CMSIS/Device/ST/STM32F2xx/Include/stm32f2xx.h" extern "C" { diff --git a/src/x9d/diskio.cpp b/src/x9d/diskio.cpp index 70ff1f74a..8502f37c0 100644 --- a/src/x9d/diskio.cpp +++ b/src/x9d/diskio.cpp @@ -38,6 +38,7 @@ #include "../FatFs/diskio.h" #include "../FatFs/ff.h" #include "../CoOS/kernel/CoOS.h" +#include "hal.h" /* Definitions for MMC/SDC command */ #define CMD0 (0x40+0) /* GO_IDLE_STATE */ @@ -254,87 +255,95 @@ void release_spi (void) /*-----------------------------------------------------------------------*/ static void stm32_dma_transfer( - BOOL receive, /* FALSE for buff->SPI, TRUE for SPI->buff */ - const BYTE *buff, /* receive TRUE : 512 byte data block to be transmitted - receive FALSE : Data buffer to store received data */ - UINT btr /* receive TRUE : Byte count (must be multiple of 2) - receive FALSE : Byte count (must be 512) */ + BOOL receive, /* FALSE for buff->SPI, TRUE for SPI->buff */ + const BYTE *buff, /* receive TRUE : 512 byte data block to be transmitted + receive FALSE : Data buffer to store received data */ + UINT btr /* receive TRUE : Byte count (must be multiple of 2) + receive FALSE : Byte count (must be 512) */ ) { - DMA_InitTypeDef DMA_InitStructure; - WORD rw_workbyte[] = { 0xffff }; + DMA_InitTypeDef DMA_InitStructure; + WORD rw_workbyte[] = { 0xffff }; - /* shared DMA configuration values */ - DMA_InitStructure.DMA_PeripheralBaseAddr = (DWORD)(&(SPI_SD->DR)); - DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; - DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; - DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; - DMA_InitStructure.DMA_BufferSize = btr; - DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; - DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; - DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; + DMA_DeInit(DMA_Channel_SPI_SD_RX); + DMA_DeInit(DMA_Channel_SPI_SD_TX); + + /* shared DMA configuration values between SPI2 RX & TX*/ + DMA_InitStructure.DMA_Channel = DMA_Channel_SPI2_TX;//the same channel + DMA_InitStructure.DMA_PeripheralBaseAddr = (DWORD)(&(SPI_SD->DR)); + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; + DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_BufferSize = btr; + DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; + DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; + + DMA_InitStructure.DMA_FIFOMode =DMA_FIFOMode_Enable; + DMA_InitStructure.DMA_FIFOThreshold =DMA_FIFOThreshold_Full; + DMA_InitStructure.DMA_MemoryBurst =DMA_MemoryBurst_Single; + DMA_InitStructure.DMA_PeripheralBurst =DMA_PeripheralBurst_Single; - DMA_DeInit(DMA_Channel_SPI_SD_RX); - DMA_DeInit(DMA_Channel_SPI_SD_TX); + //seperate RX & TX + if ( receive ) { //true =read - if ( receive ) { + /* DMA1 channel3 configuration SPI2 RX ---------------------------------------------*/ + DMA_InitStructure.DMA_Memory0BaseAddr = (DWORD)buff; + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_Init(DMA_Channel_SPI_SD_RX, &DMA_InitStructure); - /* DMA1 channel2 configuration SPI1 RX ---------------------------------------------*/ - /* DMA1 channel4 configuration SPI2 RX ---------------------------------------------*/ - DMA_InitStructure.DMA_MemoryBaseAddr = (DWORD)buff; - DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; - DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; - DMA_Init(DMA_Channel_SPI_SD_RX, &DMA_InitStructure); + /* DMA1 channel4 configuration SPI2 TX ---------------------------------------------*/ + DMA_InitStructure.DMA_Memory0BaseAddr = (DWORD)rw_workbyte; + DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; + DMA_Init(DMA_Channel_SPI_SD_TX, &DMA_InitStructure); - /* DMA1 channel3 configuration SPI1 TX ---------------------------------------------*/ - /* DMA1 channel5 configuration SPI2 TX ---------------------------------------------*/ - DMA_InitStructure.DMA_MemoryBaseAddr = (DWORD)rw_workbyte; - DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; - DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; - DMA_Init(DMA_Channel_SPI_SD_TX, &DMA_InitStructure); + } else {//false = write - } else { +#if _FS_READONLY == 0 //READ AND WRITE = write enabled. + /* DMA1 channel2 configuration SPI1 RX ---------------------------------------------*/ + /* DMA1 channel4 configuration SPI2 RX ---------------------------------------------*/ + DMA_InitStructure.DMA_Memory0BaseAddr = (DWORD)rw_workbyte; + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; + DMA_Init(DMA_Channel_SPI_SD_RX, &DMA_InitStructure); -#if _FS_READONLY == 0 - /* DMA1 channel2 configuration SPI1 RX ---------------------------------------------*/ - /* DMA1 channel4 configuration SPI2 RX ---------------------------------------------*/ - DMA_InitStructure.DMA_MemoryBaseAddr = (DWORD)rw_workbyte; - DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; - DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; - DMA_Init(DMA_Channel_SPI_SD_RX, &DMA_InitStructure); - - /* DMA1 channel3 configuration SPI1 TX ---------------------------------------------*/ - /* DMA1 channel5 configuration SPI2 TX ---------------------------------------------*/ - DMA_InitStructure.DMA_MemoryBaseAddr = (DWORD)buff; - DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; - DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; - DMA_Init(DMA_Channel_SPI_SD_TX, &DMA_InitStructure); + /* DMA1 channel3 configuration SPI1 TX ---------------------------------------------*/ + /* DMA1 channel5 configuration SPI2 TX ---------------------------------------------*/ + DMA_InitStructure.DMA_Memory0BaseAddr = (DWORD)buff; + DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_Init(DMA_Channel_SPI_SD_TX, &DMA_InitStructure); #endif - } + } - /* Enable DMA RX Channel */ - DMA_Cmd(DMA_Channel_SPI_SD_RX, ENABLE); - /* Enable DMA TX Channel */ - DMA_Cmd(DMA_Channel_SPI_SD_TX, ENABLE); + /* Enable DMA RX Channel */ + DMA_Cmd(DMA_Channel_SPI_SD_RX, ENABLE); + /* Enable DMA TX Channel */ + DMA_Cmd(DMA_Channel_SPI_SD_TX, ENABLE); - /* Enable SPI TX/RX request */ - SPI_I2S_DMACmd(SPI_SD, SPI_I2S_DMAReq_Rx | SPI_I2S_DMAReq_Tx, ENABLE); + /* Enable SPI TX/RX request */ + SPI_I2S_DMACmd(SPI_SD, SPI_I2S_DMAReq_Rx | SPI_I2S_DMAReq_Tx, ENABLE); - /* Wait until DMA1_Channel 3 Transfer Complete */ - /// not needed: while (DMA_GetFlagStatus(DMA_FLAG_SPI_SD_TC_TX) == RESET) { ; } - /* Wait until DMA1_Channel 2 Receive Complete */ - while (DMA_GetFlagStatus(DMA_FLAG_SPI_SD_TC_RX) == RESET) { ; } - // same w/o function-call: - // while ( ( ( DMA1->ISR ) & DMA_FLAG_SPI_SD_TC_RX ) == RESET ) { ; } + /* Wait until DMA1_Channel 3 Transfer Complete */ + + while (DMA_GetFlagStatus(DMA_Channel_SPI_SD_TX,DMA_FLAG_SPI_SD_TC_TX) == RESET) { ; } + + /* Wait until DMA1_Channel 2 Receive Complete */ + + while (DMA_GetFlagStatus(DMA_Channel_SPI_SD_RX,DMA_FLAG_SPI_SD_TC_RX) == RESET) { ; } - /* Disable DMA RX Channel */ - DMA_Cmd(DMA_Channel_SPI_SD_RX, DISABLE); - /* Disable DMA TX Channel */ - DMA_Cmd(DMA_Channel_SPI_SD_TX, DISABLE); + // same w/o function-call: + // while ( ( ( DMA1->ISR ) & DMA_FLAG_SPI_SD_TC_RX ) == RESET ) { ; } - /* Disable SPI RX/TX request */ - SPI_I2S_DMACmd(SPI_SD, SPI_I2S_DMAReq_Rx | SPI_I2S_DMAReq_Tx, DISABLE); + /* Disable DMA RX Channel */ + DMA_Cmd(DMA_Channel_SPI_SD_RX, DISABLE); + /* Disable DMA TX Channel */ + DMA_Cmd(DMA_Channel_SPI_SD_TX, DISABLE); + + /* Disable SPI RX/TX request */ + SPI_I2S_DMACmd(SPI_SD, SPI_I2S_DMAReq_Rx | SPI_I2S_DMAReq_Tx, DISABLE); } #endif /* STM32_SD_USE_DMA */ @@ -346,94 +355,100 @@ void stm32_dma_transfer( static void power_on (void) { - SPI_InitTypeDef SPI_InitStructure; - GPIO_InitTypeDef GPIO_InitStructure; + SPI_InitTypeDef SPI_InitStructure; + GPIO_InitTypeDef GPIO_InitStructure; + volatile BYTE dummyread; - /* Enable GPIO clock for CS */ - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIO_CS, ENABLE); - /* Enable SPI clock, SPI1: APB2, SPI2: APB1 */ - RCC_APBPeriphClockCmd_SPI_SD(RCC_APBPeriph_SPI_SD, ENABLE); + /* Enable GPIO clock for CS */ + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIO_SD, ENABLE); + /* Enable SPI clock, SPI1: APB2, SPI2: APB1 */ + RCC_APBPeriphClockCmd_SPI_SD(RCC_APBPeriph_SPI_SD, ENABLE); + + + card_power(1); + + socket_cp_init();//Empty return + socket_wp_init();//Empty return. - GPIO_PinAFConfig(GPIO_SPI_SD,GPIO_PinSource_SCK ,GPIO_AF_SD); - GPIO_PinAFConfig(GPIO_SPI_SD,GPIO_PinSource_MISO,GPIO_AF_SD); - GPIO_PinAFConfig(GPIO_SPI_SD,GPIO_PinSource_MOSI,GPIO_AF_SD); + for (uint32_t Timer = 25000; Timer>0;Timer--); /* Wait for 250ms */ - card_power(1); + /* Configure I/O for Flash Chip select */ + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SPI_SD_CS; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(GPIO_SPI_SD, &GPIO_InitStructure); - socket_cp_init();//Empty return - socket_wp_init();//Empty return. + /* De-select the Card: Chip Select high */ + DESELECT(); - CoTickDelay(125); - // for (Timer1 = 25; Timer1; ); /* Wait for 250ms */ - - /* SPI configuration */ - SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; - SPI_InitStructure.SPI_Mode = SPI_Mode_Master; - SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; - SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; - SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; - SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; - SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_SPI_SD; // - SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; - SPI_InitStructure.SPI_CRCPolynomial = 7; - - SPI_Init(SPI_SD, &SPI_InitStructure); - SPI_CalculateCRC(SPI_SD, DISABLE); - SPI_Cmd(SPI_SD, ENABLE); - - /* De-select the Card: Chip Select high */ - DESELECT(); - - /* Configure I/O for Flash Chip select */ - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SPI_SD_CS; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_Init(GPIO_SPI_SD, &GPIO_InitStructure); - - /* Configure SPI pins: SCK MISO and MOSI with alternate function push-down */ - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SPI_SD_SCK | GPIO_Pin_SPI_SD_MOSI | GPIO_Pin_SPI_SD_MISO; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; - GPIO_Init(GPIO_SPI_SD, &GPIO_InitStructure); + /* Configure SPI pins: SCK MISO and MOSI with alternate function push-down */ + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SPI_SD_SCK | GPIO_Pin_SPI_SD_MOSI|GPIO_Pin_SPI_SD_MISO; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; + GPIO_Init(GPIO_SPI_SD, &GPIO_InitStructure); + GPIO_PinAFConfig(GPIO_SPI_SD,GPIO_PinSource_SCK ,GPIO_AF_SD); + GPIO_PinAFConfig(GPIO_SPI_SD,GPIO_PinSource_MISO,GPIO_AF_SD); + GPIO_PinAFConfig(GPIO_SPI_SD,GPIO_PinSource_MOSI,GPIO_AF_SD); + + + /* SPI configuration */ + SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; + SPI_InitStructure.SPI_Mode = SPI_Mode_Master; + SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; + SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; + SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; + SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; + SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_SPI_SD; + SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; + SPI_InitStructure.SPI_CRCPolynomial = 7; + + SPI_Init(SPI_SD, &SPI_InitStructure); + SPI_CalculateCRC(SPI_SD, DISABLE); + SPI_Cmd(SPI_SD, ENABLE); + + /* drain SPI */ + while (SPI_I2S_GetFlagStatus(SPI_SD, SPI_I2S_FLAG_TXE) == RESET) { ; } + dummyread = SPI_I2S_ReceiveData(SPI_SD); + #ifdef STM32_SD_USE_DMA - /* enable DMA clock */ - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); + /* enable DMA clock */ + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA_SD, ENABLE); #endif } - static void power_off (void) { - GPIO_InitTypeDef GPIO_InitStructure; + + GPIO_InitTypeDef GPIO_InitStructure; - if (!(Stat & STA_NOINIT)) { - SELECT(); - wait_ready(); - release_spi(); - } + if (!(Stat & STA_NOINIT)) { + SELECT(); + wait_ready(); + release_spi(); + } - SPI_I2S_DeInit(SPI_SD); - SPI_Cmd(SPI_SD, DISABLE); - RCC_APBPeriphClockCmd_SPI_SD(RCC_APBPeriph_SPI_SD, DISABLE); + SPI_I2S_DeInit(SPI_SD); + SPI_Cmd(SPI_SD, DISABLE); + RCC_APBPeriphClockCmd_SPI_SD(RCC_APBPeriph_SPI_SD, DISABLE); + + //All SPI-Pins to input with weak internal pull-downs + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SPI_SD_SCK | GPIO_Pin_SPI_SD_MISO | GPIO_Pin_SPI_SD_MOSI; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; + GPIO_Init(GPIO_SPI_SD, &GPIO_InitStructure); - //All SPI-Pins to input with weak internal pull-downs // - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SPI_SD_SCK | GPIO_Pin_SPI_SD_MISO | GPIO_Pin_SPI_SD_MOSI; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; - GPIO_Init(GPIO_SPI_SD, &GPIO_InitStructure); + card_power(0); - card_power(0); - - Stat |= STA_NOINIT; /* Set STA_NOINIT */ + Stat |= STA_NOINIT; /* Set STA_NOINIT */ + } - /*-----------------------------------------------------------------------*/ /* Receive a data packet from MMC */ /*-----------------------------------------------------------------------*/ diff --git a/src/x9d/hal.h b/src/x9d/hal.h index 26b1df9f3..a53020ea2 100644 --- a/src/x9d/hal.h +++ b/src/x9d/hal.h @@ -273,10 +273,6 @@ #endif // SD - SPI2 -#define SPI_SD SPI2 -#define GPIO_AF_SD GPIO_AF_SPI2 -#define RCC_AHB1Periph_GPIO_CS RCC_AHB1Periph_GPIOB -#define GPIO_SPI_SD GPIOB #define GPIO_Pin_SPI_SD_CS GPIO_Pin_12 //PB.12 #define GPIO_Pin_SPI_SD_SCK GPIO_Pin_13 //PB.13 #define GPIO_Pin_SPI_SD_MISO GPIO_Pin_14 //PB.14 @@ -285,15 +281,35 @@ #define GPIO_PinSource_SCK GPIO_PinSource13 #define GPIO_PinSource_MISO GPIO_PinSource14 #define GPIO_PinSource_MOSI GPIO_PinSource15 + +#define GPIO_SPI_SD GPIOB +#define SPI_SD SPI2 +#define GPIO_AF_SD GPIO_AF_SPI2 +#define RCC_AHB1Periph_GPIO_SD RCC_AHB1Periph_GPIOB #define RCC_APBPeriphClockCmd_SPI_SD RCC_APB1PeriphClockCmd #define RCC_APBPeriph_SPI_SD RCC_APB1Periph_SPI2 -#define RCC_AHBxPeriph_GPIO_WP RCC_AHB1Periph_GPIOD -#define RCC_AHBxPeriph_GPIO_CP RCC_AHB1Periph_GPIOD -#define GPIO_Mode_WP GPIO_Mode_OUT//lock? -#define GPIO_Mode_CP GPIO_Mode_IN // -#define GPIO_Pin_WP GPIO_Pin_8 //PD.08 -#define GPIO_Pin_CP GPIO_Pin_9 //PD.09 -#define GPIO_CTL_SD GPIOD +#define RCC_AHB1Periph_DMA_SD RCC_AHB1Periph_DMA1 +#define SPI_BaudRatePrescaler_SPI_SD SPI_BaudRatePrescaler_4 //10.5<20MHZ,make sure < 20MHZ + +//disabled io_ctl and cp/wp +#define CARD_SUPPLY_SWITCHABLE 0 +#define SOCKET_WP_CONNECTED 0 +#define SOCKET_CP_CONNECTED 0 +/* set to 1 to provide a disk_ioctrl function even if not needed by the FatFs */ +#define STM32_SD_DISK_IOCTRL_FORCE 0 + +//DMA +#define STM32_SD_USE_DMA //Enable the DMA for SD + +#ifdef STM32_SD_USE_DMA +#define DMA_Channel_SPI_SD_RX DMA1_Stream3 +#define DMA_Channel_SPI_SD_TX DMA1_Stream4 + +#define DMA_FLAG_SPI_SD_TC_RX DMA_FLAG_TCIF3 +#define DMA_FLAG_SPI_SD_TC_TX DMA_FLAG_TCIF4 +#define DMA_Channel_SPI2_TX DMA_Channel_0 +#define DMA_Channel_SPI2_RX DMA_Channel_0 +#endif // Audio----I2S3-----SPI3 #define CODEC_MCLK_DISABLED @@ -307,29 +323,4 @@ #define I2C_EEPROM_ADDRESS 0xA2 #define I2C_CAT5137_ADDRESS 0x5C //0101110 -// SD card----SPI2 -// demo uses a command line option to define this (see Makefile): -// #define STM32_SD_USE_DMA -#ifdef STM32_SD_USE_DMA -// #warning "Information only: using DMA" -#pragma message "*** Using DMA ***" -#endif - -/* set to 1 to provide a disk_ioctrl function even if not needed by the FatFs */ -#define STM32_SD_DISK_IOCTRL_FORCE (0) -#define CARD_SUPPLY_SWITCHABLE (0) // power on not depend any IO -#define SOCKET_WP_CONNECTED (0) -#define SOCKET_CP_CONNECTED (0) - -#define SPI_BaudRatePrescaler_SPI_SD SPI_BaudRatePrescaler_2 // - for SPI2 and full-speed APB1: 30MHz/2 =15MHZ < 20MHZ - -// Selectable -// DMA -#define DMA_Channel_SPI_SD_RX DMA1_Channel2 -#define DMA_Channel_SPI_SD_TX DMA1_Channel3 -#define DMA_FLAG_SPI_SD_TC_RX DMA1_FLAG_TC2 -#define DMA_FLAG_SPI_SD_TC_TX DMA1_FLAG_TC3 -#define DMA_Channel_SPI1_TX DMA_Channel_3 -#define DMA_Stream_SPI1_TX DMA2_Stream3 - -#endif +#endif \ No newline at end of file diff --git a/src/x9d/pulses_driver.cpp b/src/x9d/pulses_driver.cpp index a100350d2..61502e4ef 100644 --- a/src/x9d/pulses_driver.cpp +++ b/src/x9d/pulses_driver.cpp @@ -105,6 +105,7 @@ void init_pxx() TIM1->SR &= ~TIM_SR_CC2IF ; // Clear flag TIM1->DIER |= TIM_DIER_CC2IE ; // Enable this interrupt TIM1->CR1 |= TIM_CR1_CEN ; +// NVIC_SetPriority(TIM1_CC_IRQn, 1); // Debug - high priority, doesn't help NVIC_EnableIRQ(TIM1_CC_IRQn) ; } diff --git a/src/x9d/pwr_driver.cpp b/src/x9d/pwr_driver.cpp index 661d5ee62..7ddf15f5d 100644 --- a/src/x9d/pwr_driver.cpp +++ b/src/x9d/pwr_driver.cpp @@ -100,7 +100,7 @@ void pwrInit() GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOTRNDET, &GPIO_InitStructure); #endif diff --git a/src/x9d/sport_driver.cpp b/src/x9d/sport_driver.cpp index 84e533c9d..d9f05f470 100644 --- a/src/x9d/sport_driver.cpp +++ b/src/x9d/sport_driver.cpp @@ -36,6 +36,8 @@ #include "../open9x.h" +uint16_t tmp=0; // Debug - received bytes counter + void sportInit(void) { USART_InitTypeDef USART_InitStructure; @@ -89,10 +91,20 @@ void sportPutc(const char c) extern "C" void USART2_IRQHandler() { uint16_t data; - - while(USART_GetFlagStatus(SPORT, USART_FLAG_RXNE)){ + + tmp++; // Debug - received bytes counter + + if(USART_GetFlagStatus(SPORT, USART_FLAG_RXNE)) data = USART_ReceiveData(SPORT); - processSerialData((uint8_t) data); + + USART_ClearFlag(SPORT,USART_FLAG_RXNE); // Just in case, should be cleared automatically by the read above, and we shouldn't enter the interrupt if no char received - doesn't help + + if(SPORT->SR & USART_FLAG_ORE) // Debug - never happened once yet + { + data = (int8_t)(SPORT->SR & (uint8_t)0xFF); + tmp=0; } + + processSerialData((uint8_t) data); } #endif