From 52c3627bb23112797dfac6042fe33906ef1431f1 Mon Sep 17 00:00:00 2001 From: Sambas Date: Sun, 8 Jul 2012 21:17:18 +0300 Subject: [PATCH] OSD uSDcard support, settings save/load works (tested on 128MB card). booting takes some time for some reason. --- flight/OSD/Makefile | 5 +- flight/OSD/System/inc/pios_config.h | 4 +- flight/OSD/System/pios_board.c | 11 ++ flight/PiOS/Boards/STM32F4xx_OSD.h | 18 +++ flight/PiOS/Common/Libraries/dosfs/library.mk | 2 +- flight/PiOS/Common/pios_sdcard.c | 28 ++-- flight/PiOS/pios.h | 2 +- flight/board_hw_defs/osd/board_hw_defs.c | 128 ++++++++++++++++++ 8 files changed, 179 insertions(+), 19 deletions(-) diff --git a/flight/OSD/Makefile b/flight/OSD/Makefile index 023dc8bc4..9831a2d27 100644 --- a/flight/OSD/Makefile +++ b/flight/OSD/Makefile @@ -94,7 +94,7 @@ endif MODULES = Osd/osdgen MODULES += GPS MODULES += Osd/osdinput -MODULES += Extensions/MagBaro +#MODULES += Extensions/MagBaro MODULES += Telemetry MODULES += FirmwareIAP @@ -215,10 +215,11 @@ SRC += $(PIOSCOMMON)/pios_video.c SRC += $(PIOSCOMMON)/pios_usb_desc_hid_cdc.c SRC += $(PIOSCOMMON)/pios_usb_desc_hid_only.c SRC += $(PIOSCOMMON)/pios_usb_util.c +SRC += $(PIOSCOMMON)/pios_sdcard.c # optional component libraries include $(PIOSCOMMONLIB)/FreeRTOS/library.mk -#include $(PIOSCOMMONLIB)/dosfs/library.mk +include $(PIOSCOMMONLIB)/dosfs/library.mk include $(PIOSCOMMONLIB)/msheap/library.mk ## PIOS Hardware (STM32F4xx) diff --git a/flight/OSD/System/inc/pios_config.h b/flight/OSD/System/inc/pios_config.h index 54b90de32..89ffb59b4 100644 --- a/flight/OSD/System/inc/pios_config.h +++ b/flight/OSD/System/inc/pios_config.h @@ -40,7 +40,7 @@ #define PIOS_INCLUDE_LED #define PIOS_INCLUDE_IAP //#define PIOS_INCLUDE_SERVO -//#define PIOS_INCLUDE_SPI +#define PIOS_INCLUDE_SPI #define PIOS_INCLUDE_SYS #define PIOS_INCLUDE_USART //#define PIOS_INCLUDE_GPIO @@ -59,6 +59,7 @@ //#define PIOS_OVERO_SPI #define PIOS_INCLUDE_BMP085 #define PIOS_INCLUDE_HMC5883 +#define PIOS_INCLUDE_SDCARD /* Supported receiver interfaces */ //#define PIOS_INCLUDE_RCVR @@ -90,5 +91,6 @@ // This actually needs calibrating #define IDLE_COUNTS_PER_SEC_AT_NO_LOAD (8379692) +#define LOG_FILENAME "PIOS.LOG" #endif /* PIOS_CONFIG_H */ diff --git a/flight/OSD/System/pios_board.c b/flight/OSD/System/pios_board.c index 554361698..1962f2ad4 100644 --- a/flight/OSD/System/pios_board.c +++ b/flight/OSD/System/pios_board.c @@ -243,6 +243,17 @@ void PIOS_Board_Init(void) { PIOS_LED_Init(&pios_led_cfg); +#if defined(PIOS_INCLUDE_SPI) + /* Set up the SPI interface to the SD card */ + if (PIOS_SPI_Init(&pios_spi_sdcard_id, &pios_spi_sdcard_cfg)) { + PIOS_Assert(0); + } + + /* Enable and mount the SDCard */ + PIOS_SDCARD_Init(pios_spi_sdcard_id); + PIOS_SDCARD_MountFS(0); +#endif /* PIOS_INCLUDE_SPI */ + /* Initialize UAVObject libraries */ EventDispatcherInitialize(); UAVObjInitialize(); diff --git a/flight/PiOS/Boards/STM32F4xx_OSD.h b/flight/PiOS/Boards/STM32F4xx_OSD.h index cdbbcedd6..722c76d34 100644 --- a/flight/PiOS/Boards/STM32F4xx_OSD.h +++ b/flight/PiOS/Boards/STM32F4xx_OSD.h @@ -282,4 +282,22 @@ extern uint32_t pios_i2c_flexiport_adapter_id; //------------------------ #define PIOS_BMP085_OVERSAMPLING 3 + +/** + * glue macros for file IO + * STM32 uses DOSFS for file IO + */ +#define PIOS_FOPEN_READ(filename,file) DFS_OpenFile(&PIOS_SDCARD_VolInfo, (uint8_t *)filename, DFS_READ, PIOS_SDCARD_Sector, &file) != DFS_OK + +#define PIOS_FOPEN_WRITE(filename,file) DFS_OpenFile(&PIOS_SDCARD_VolInfo, (uint8_t *)filename, DFS_WRITE, PIOS_SDCARD_Sector, &file) != DFS_OK + +#define PIOS_FREAD(file,bufferadr,length,resultadr) DFS_ReadFile(file, PIOS_SDCARD_Sector, (uint8_t*)bufferadr, resultadr, length) != DFS_OK + +#define PIOS_FWRITE(file,bufferadr,length,resultadr) DFS_WriteFile(file, PIOS_SDCARD_Sector, (uint8_t*)bufferadr, resultadr, length) + +#define PIOS_FCLOSE(file) DFS_Close(&file) + +#define PIOS_FUNLINK(filename) DFS_UnlinkFile(&PIOS_SDCARD_VolInfo, (uint8_t *)filename, PIOS_SDCARD_Sector) + + #endif /* PIOS_BOARD_H */ diff --git a/flight/PiOS/Common/Libraries/dosfs/library.mk b/flight/PiOS/Common/Libraries/dosfs/library.mk index 83e618cb5..d3bf6e16b 100644 --- a/flight/PiOS/Common/Libraries/dosfs/library.mk +++ b/flight/PiOS/Common/Libraries/dosfs/library.mk @@ -3,6 +3,6 @@ # DOSFS_DIR := $(dir $(lastword $(MAKEFILE_LIST))) -SRC += $(wildcard $(DOSFS_DIR)/*.c) +SRC += $(wildcard $(DOSFS_DIR)*.c) EXTRAINCDIRS += $(DOSFS_DIR) diff --git a/flight/PiOS/Common/pios_sdcard.c b/flight/PiOS/Common/pios_sdcard.c index 8116a8ac4..c404fa718 100644 --- a/flight/PiOS/Common/pios_sdcard.c +++ b/flight/PiOS/Common/pios_sdcard.c @@ -126,7 +126,7 @@ int32_t PIOS_SDCARD_PowerOn(void) SDCARD_MUTEX_TAKE; /* Ensure that chip select line deactivated */ - PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 1); /* spi, pin_value */ + PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0, 1); /* spi, pin_value */ /* Init SPI port for slow frequency access (ca. 0.3 MBit/s) */ PIOS_SPI_SetClockSpeed(PIOS_SDCARD_SPI, PIOS_SPI_PRESCALER_256); @@ -137,7 +137,7 @@ int32_t PIOS_SDCARD_PowerOn(void) } /* Activate chip select */ - PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0); /* spi, pin_value */ + PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0, 0); /* spi, pin_value */ /* wait for 1 mS */ PIOS_DELAY_WaituS(1000); @@ -202,7 +202,7 @@ int32_t PIOS_SDCARD_PowerOn(void) error: /* Deactivate chip select */ - PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 1); /* spi, pin_value */ + PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0, 1); /* spi, pin_value */ /* Send dummy byte once deactivated to drop cards DO */ PIOS_SPI_TransferByte(PIOS_SDCARD_SPI, 0xff); @@ -272,7 +272,7 @@ int32_t PIOS_SDCARD_CheckAvailable(uint8_t was_available) PIOS_SPI_SetClockSpeed(PIOS_SDCARD_SPI, PIOS_SPI_PRESCALER_4); /* Activate chip select */ - PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0); /* spi, pin_value */ + PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0, 0); /* spi, pin_value */ /* Send STATUS command to check if media is available */ ret = PIOS_SDCARD_SendSDCCmd(SDCMD_SEND_STATUS, 0, SDCMD_SEND_STATUS_CRC); @@ -281,7 +281,7 @@ int32_t PIOS_SDCARD_CheckAvailable(uint8_t was_available) PIOS_SPI_SetClockSpeed(PIOS_SDCARD_SPI, PIOS_SPI_PRESCALER_256); /* Deactivate chip select */ - PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 1); /* spi, pin_value */ + PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0, 1); /* spi, pin_value */ /* send 80 clock cycles to start up */ uint8_t i; for (i = 0; i < 10; ++i) { @@ -289,7 +289,7 @@ int32_t PIOS_SDCARD_CheckAvailable(uint8_t was_available) } /* Activate chip select */ - PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0); /* spi, pin_value */ + PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0, 0); /* spi, pin_value */ /* Send CMD0 to reset the media */ if ((ret = (PIOS_SDCARD_SendSDCCmd(SDCMD_GO_IDLE_STATE, 0, SDCMD_GO_IDLE_STATE_CRC))) < 0) { @@ -297,7 +297,7 @@ int32_t PIOS_SDCARD_CheckAvailable(uint8_t was_available) } /* Deactivate chip select */ - PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 1); /* spi, pin_value */ + PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0, 1); /* spi, pin_value */ SDCARD_MUTEX_GIVE; /* Run power-on sequence (negative return = not available) */ @@ -306,7 +306,7 @@ int32_t PIOS_SDCARD_CheckAvailable(uint8_t was_available) not_available: /* Deactivate chip select */ - PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 1); /* spi, pin_value */ + PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0, 1); /* spi, pin_value */ /* Send dummy byte once deactivated to drop cards DO */ PIOS_SPI_TransferByte(PIOS_SDCARD_SPI, 0xff); SDCARD_MUTEX_GIVE; @@ -336,7 +336,7 @@ int32_t PIOS_SDCARD_SendSDCCmd(uint8_t cmd, uint32_t addr, uint8_t crc) } /* Activate chip select */ - PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0); /* spi, pin_value */ + PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0, 0); /* spi, pin_value */ /* Transfer to card */ PIOS_SPI_TransferByte(PIOS_SDCARD_SPI, (uint8_t) cmd); @@ -385,7 +385,7 @@ int32_t PIOS_SDCARD_SendSDCCmd(uint8_t cmd, uint32_t addr, uint8_t crc) /* Deactivate chip-select on timeout, and return error code */ if (timeout) { - PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 1); /* spi, pin_value */ + PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0, 1); /* spi, pin_value */ /* Send dummy byte once deactivated to drop cards DO */ PIOS_SPI_TransferByte(PIOS_SDCARD_SPI, 0xff); @@ -458,7 +458,7 @@ int32_t PIOS_SDCARD_SectorRead(uint32_t sector, uint8_t * buffer) error: /* Deactivate chip select */ - PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 1); // spi, pin_value + PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0, 1); // spi, pin_value /* Send dummy byte once deactivated to drop cards DO */ PIOS_SPI_TransferByte(PIOS_SDCARD_SPI, 0xff); @@ -540,7 +540,7 @@ int32_t PIOS_SDCARD_SectorWrite(uint32_t sector, uint8_t * buffer) error: /* Deactivate chip select */ - PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 1); /* spi, pin_value */ + PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0, 1); /* spi, pin_value */ /* Send dummy byte once deactivated to drop cards DO */ PIOS_SPI_TransferByte(PIOS_SDCARD_SPI, 0xff); @@ -629,7 +629,7 @@ int32_t PIOS_SDCARD_CIDRead(SDCARDCidTypeDef * cid) error: /* deactivate chip select */ - PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 1); /* spi, pin_value */ + PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0, 1); /* spi, pin_value */ /* Send dummy byte once deactivated to drop cards DO */ PIOS_SPI_TransferByte(PIOS_SDCARD_SPI, 0xff); SDCARD_MUTEX_GIVE; @@ -747,7 +747,7 @@ int32_t PIOS_SDCARD_CSDRead(SDCARDCsdTypeDef * csd) error: /* Deactivate chip select */ - PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 1); /* spi, pin_value */ + PIOS_SPI_RC_PinSet(PIOS_SDCARD_SPI, 0, 1); /* spi, pin_value */ /* Send dummy byte once deactivated to drop cards DO */ PIOS_SPI_TransferByte(PIOS_SDCARD_SPI, 0xff); SDCARD_MUTEX_GIVE; diff --git a/flight/PiOS/pios.h b/flight/PiOS/pios.h index 015985662..19b30b4c8 100644 --- a/flight/PiOS/pios.h +++ b/flight/PiOS/pios.h @@ -63,7 +63,7 @@ #include /* Mass Storage Device Includes */ -#include +//#include #endif /* Generic initcall infrastructure */ diff --git a/flight/board_hw_defs/osd/board_hw_defs.c b/flight/board_hw_defs/osd/board_hw_defs.c index 36a3b87eb..30c5484b9 100644 --- a/flight/board_hw_defs/osd/board_hw_defs.c +++ b/flight/board_hw_defs/osd/board_hw_defs.c @@ -71,6 +71,134 @@ const struct pios_led_cfg * PIOS_BOARD_HW_DEFS_GetLedCfg (uint32_t board_revisio #endif /* PIOS_INCLUDE_LED */ +#if defined(PIOS_INCLUDE_SPI) + +#include + +/* MicroSD Interface + * + * NOTE: Leave this declared as const data so that it ends up in the + * .rodata section (ie. Flash) rather than in the .bss section (RAM). + */ +void PIOS_SPI_sdcard_irq_handler(void); +void DMA1_Channel2_IRQHandler() __attribute__ ((alias ("PIOS_SPI_sdcard_irq_handler"))); +void DMA1_Channel3_IRQHandler() __attribute__ ((alias ("PIOS_SPI_sdcard_irq_handler"))); +static const struct pios_spi_cfg pios_spi_sdcard_cfg = { + .regs = SPI2, + .remap = GPIO_AF_SPI2, + .init = { + .SPI_Mode = SPI_Mode_Master, + .SPI_Direction = SPI_Direction_2Lines_FullDuplex, + .SPI_DataSize = SPI_DataSize_8b, + .SPI_NSS = SPI_NSS_Soft, + .SPI_FirstBit = SPI_FirstBit_MSB, + .SPI_CRCPolynomial = 7, + .SPI_CPOL = SPI_CPOL_High, + .SPI_CPHA = SPI_CPHA_2Edge, + .SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256, /* Maximum divider (ie. slowest clock rate) */ + }, + .use_crc = false, + .dma = { + .irq = { + .flags = (DMA_IT_TCIF3|DMA_IT_TEIF3|DMA_IT_HTIF3|DMA_IT_TCIF3), + .init = { + .NVIC_IRQChannel = DMA1_Stream3_IRQn, + .NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_MID, + .NVIC_IRQChannelSubPriority = 0, + .NVIC_IRQChannelCmd = ENABLE, + }, + }, + .rx = { + .channel = DMA1_Stream3, + .init = { + .DMA_Channel = DMA_Channel_0, + .DMA_PeripheralBaseAddr = (uint32_t)&(SPI2->DR), + .DMA_DIR = DMA_DIR_PeripheralToMemory, + .DMA_PeripheralInc = DMA_PeripheralInc_Disable, + .DMA_MemoryInc = DMA_MemoryInc_Enable, + .DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte, + .DMA_MemoryDataSize = DMA_MemoryDataSize_Byte, + .DMA_Mode = DMA_Mode_Normal, + .DMA_Priority = DMA_Priority_Medium, + .DMA_FIFOMode = DMA_FIFOMode_Disable, + .DMA_FIFOThreshold = DMA_FIFOThreshold_Full, + .DMA_MemoryBurst = DMA_MemoryBurst_Single, + .DMA_PeripheralBurst = DMA_PeripheralBurst_Single, + }, + }, + .tx = { + .channel = DMA1_Stream4, + .init = { + .DMA_Channel = DMA_Channel_0, + .DMA_PeripheralBaseAddr = (uint32_t)&(SPI2->DR), + .DMA_DIR = DMA_DIR_MemoryToPeripheral, + .DMA_PeripheralInc = DMA_PeripheralInc_Disable, + .DMA_MemoryInc = DMA_MemoryInc_Enable, + .DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte, + .DMA_MemoryDataSize = DMA_MemoryDataSize_Byte, + .DMA_Mode = DMA_Mode_Normal, + .DMA_Priority = DMA_Priority_Medium, + .DMA_FIFOMode = DMA_FIFOMode_Disable, + .DMA_FIFOThreshold = DMA_FIFOThreshold_Full, + .DMA_MemoryBurst = DMA_MemoryBurst_Single, + .DMA_PeripheralBurst = DMA_PeripheralBurst_Single, + }, + }, + }, + .sclk = { + .gpio = GPIOB, + .init = { + .GPIO_Pin = GPIO_Pin_13, + .GPIO_Speed = GPIO_Speed_50MHz, + .GPIO_Mode = GPIO_Mode_AF, + .GPIO_OType = GPIO_OType_PP, + .GPIO_PuPd = GPIO_PuPd_UP + }, + }, + .miso = { + .gpio = GPIOB, + .init = { + .GPIO_Pin = GPIO_Pin_14, + .GPIO_Speed = GPIO_Speed_50MHz, + .GPIO_Mode = GPIO_Mode_AF, + .GPIO_OType = GPIO_OType_PP, + .GPIO_PuPd = GPIO_PuPd_UP + }, + }, + .mosi = { + .gpio = GPIOB, + .init = { + .GPIO_Pin = GPIO_Pin_15, + .GPIO_Speed = GPIO_Speed_50MHz, + .GPIO_Mode = GPIO_Mode_AF, + .GPIO_OType = GPIO_OType_PP, + .GPIO_PuPd = GPIO_PuPd_UP + }, + }, + .slave_count = 1, + .ssel = {{ + .gpio = GPIOB, + .init = { + .GPIO_Pin = GPIO_Pin_12, + .GPIO_Speed = GPIO_Speed_50MHz, + .GPIO_Mode = GPIO_Mode_OUT, + .GPIO_OType = GPIO_OType_PP, + .GPIO_PuPd = GPIO_PuPd_UP + }, + }}, +}; + +static uint32_t pios_spi_sdcard_id; +void PIOS_SPI_sdcard_irq_handler(void) +{ + /* Call into the generic code to handle the IRQ for this specific device */ + PIOS_SPI_IRQ_Handler(pios_spi_sdcard_id); +} + +#endif + + + #include #if defined(PIOS_INCLUDE_GPS)