From 998ab12de4bf217a12345dfb9c85da537c364660 Mon Sep 17 00:00:00 2001 From: gussy Date: Wed, 20 Jan 2010 23:48:04 +0000 Subject: [PATCH] Delays fixed. SD Card fixed and working, tested with 2Gb and 8Gb (SDHC) cards. Changed startup_stm32f10x_HD.S to work with SysTick_Handler, needs to be changed back for FreeRTOS to work. git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@132 ebee16cc-31ac-478f-84a7-5cbb03baadba --- flight/Libraries/FatFS/diskio.c | 10 +++--- flight/PiOS/Common/pios_sdcard.c | 22 ++++++++----- flight/PiOS/STM32F10x/pios_delay.c | 21 ++++++++++++ flight/PiOS/STM32F10x/pios_sys.c | 21 ++++++++---- flight/PiOS/STM32F10x/startup_stm32f10x_HD.S | 3 +- flight/PiOS/inc/pios_delay.h | 3 +- flight/PiOS/pios.c | 34 +++++++++++--------- 7 files changed, 77 insertions(+), 37 deletions(-) diff --git a/flight/Libraries/FatFS/diskio.c b/flight/Libraries/FatFS/diskio.c index 80a47fb41..531e1dc52 100644 --- a/flight/Libraries/FatFS/diskio.c +++ b/flight/Libraries/FatFS/diskio.c @@ -41,15 +41,15 @@ //Using DMA #define STM32_USE_DMA -//Setup for SPI1 on v1.2 hardware (CD is on GPIO_Pin_4) +//Setup for SPI1 on v1 hardware (CD is on GPIO_Pin_4) #define CARD_SUPPLY_SWITCHABLE 0 #define SOCKET_WP_CP_CONNECTED 0 #define SPI_SD SPI1 -//CS is on PC5 -#define GPIO_CS GPIOC -#define RCC_APB2Periph_GPIO_CS RCC_APB2Periph_GPIOC -#define GPIO_Pin_CS GPIO_Pin_5 +//CS is on PA4 +#define GPIO_CS GPIOA +#define RCC_APB2Periph_GPIO_CS RCC_APB2Periph_GPIOA +#define GPIO_Pin_CS GPIO_Pin_4 #define GPIO_SPI_SD GPIOA #define GPIO_Pin_SPI_SD_SCK GPIO_Pin_5 diff --git a/flight/PiOS/Common/pios_sdcard.c b/flight/PiOS/Common/pios_sdcard.c index 4920970fe..8baff12ff 100644 --- a/flight/PiOS/Common/pios_sdcard.c +++ b/flight/PiOS/Common/pios_sdcard.c @@ -41,26 +41,32 @@ FIL logfile; void PIOS_SDCARD_Init(void) { int result; - + /* Initialise FatFS disk */ if(f_mount(0, &Fatfs[0]) != FR_OK) { /* Failed to setup MicroSD memory structure, flash LED1 forever */ while(1) { - for(int i = 0; i < 100000; i++); + for(int i = 0; i < 1000000; i++); PIOS_LED_Toggle(LED1); } } else { - /* Try to Open Logging file */ - if ( f_open(&logfile, LOG_FILENAME, FA_CREATE_ALWAYS | FA_WRITE ) != FR_OK ) { + FRESULT foresult = f_open(&logfile, LOG_FILENAME, FA_CREATE_ALWAYS | FA_WRITE); + if (foresult != FR_OK) { /* Failed to mount MicroSD or create file, flash LED1 forever */ + PIOS_LED_Off(LED1); + PIOS_LED_Off(LED2); while(1) { - for(int i = 0; i < 100000; i++); + for(int i = 0; i < 1000000; i++); PIOS_LED_Toggle(LED1); + PIOS_LED_Toggle(LED2); } } else { - result = f_puts("Hello\n", &logfile ); - if ( result != EOF ) { result = f_puts("pios rocks!\n", &logfile ); } - f_close( &logfile ); + result = f_puts("Hello\n", &logfile); + if (result != EOF ) { + result = f_puts("pios rocks!\n", &logfile ); + } + f_close(&logfile); } } } + diff --git a/flight/PiOS/STM32F10x/pios_delay.c b/flight/PiOS/STM32F10x/pios_delay.c index 9dfd0d5a2..5bbc7e628 100644 --- a/flight/PiOS/STM32F10x/pios_delay.c +++ b/flight/PiOS/STM32F10x/pios_delay.c @@ -84,3 +84,24 @@ int32_t PIOS_DELAY_Wait_uS(uint16_t uS) /* No error */ return 0; } + +/** +* Waits for a specific number of mS
+* Example:
+* \code +* // Wait for 500 mS +* PIOS_DELAY_Wait_mS(500); +* \endcode +* \param[in] mS delay (1..65535 milliseconds) +* \return < 0 on errors +*/ +int32_t PIOS_DELAY_Wait_mS(uint16_t mS) +{ + uint16_t start = PIOS_DELAY_TIMER->CNT; + + /* Note that this event works on 16bit counter wrap-arounds */ + while((uint16_t)(PIOS_DELAY_TIMER->CNT - start) <= (mS * 1000)); + + /* No error */ + return 0; +} diff --git a/flight/PiOS/STM32F10x/pios_sys.c b/flight/PiOS/STM32F10x/pios_sys.c index b7217b0a4..c28dc4302 100644 --- a/flight/PiOS/STM32F10x/pios_sys.c +++ b/flight/PiOS/STM32F10x/pios_sys.c @@ -31,14 +31,13 @@ /* Private Function Prototypes */ void NVIC_Configuration(void); - +void SysTick_Handler(void); /** * Initializes all system peripherals */ void PIOS_SYS_Init(void) { - /* Setup STM32 system (RCC, clock, PLL and Flash configuration) - CMSIS Function */ SystemInit(); @@ -47,7 +46,6 @@ void PIOS_SYS_Init(void) /* Initialize LEDs */ PIOS_LED_Init(); - } @@ -63,7 +61,19 @@ void NVIC_Configuration(void) NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); /* Configure HCLK clock as SysTick clock source. */ - SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK ); + //SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); + + /* Set SysTick to 10mS tick */ + if(SysTick_Config(SystemFrequency / 100)) + { + /* Capture error */ + while (1); + } +} + +void SysTick_Handler(void) +{ + disk_timerproc(); } #ifdef USE_FULL_ASSERT @@ -86,10 +96,9 @@ void assert_failed(uint8_t* file, uint32_t line) /* Infinite loop */ while (1) { - for(int i = 0; i < 1000; i++); PIOS_LED_Toggle(LED1); - for(int i = 0; i < 1000; i++); PIOS_LED_Toggle(LED2); + for(int i = 0; i < 1000000; i++); } } #endif diff --git a/flight/PiOS/STM32F10x/startup_stm32f10x_HD.S b/flight/PiOS/STM32F10x/startup_stm32f10x_HD.S index 34caf921b..bd4ab1e39 100644 --- a/flight/PiOS/STM32F10x/startup_stm32f10x_HD.S +++ b/flight/PiOS/STM32F10x/startup_stm32f10x_HD.S @@ -155,7 +155,8 @@ g_pfnVectors: .word DebugMon_Handler .word 0 .word xPortPendSVHandler - .word xPortSysTickHandler + //.word xPortSysTickHandler + .word SysTick_Handler .word WWDG_IRQHandler .word PVD_IRQHandler .word TAMPER_IRQHandler diff --git a/flight/PiOS/inc/pios_delay.h b/flight/PiOS/inc/pios_delay.h index c58690d6a..d630c0a2d 100644 --- a/flight/PiOS/inc/pios_delay.h +++ b/flight/PiOS/inc/pios_delay.h @@ -30,6 +30,7 @@ /* Public Functions */ extern int32_t PIOS_DELAY_Init(void); extern int32_t PIOS_DELAY_Wait_uS(uint16_t uS); +extern int32_t PIOS_DELAY_Wait_mS(uint16_t mS); -#endif /* PIOS_DELAY_H */ \ No newline at end of file +#endif /* PIOS_DELAY_H */ diff --git a/flight/PiOS/pios.c b/flight/PiOS/pios.c index 2adb8c9b7..0e11a9c1f 100644 --- a/flight/PiOS/pios.c +++ b/flight/PiOS/pios.c @@ -41,8 +41,9 @@ static uint32_t IdleTimePercent = 0; void vApplicationIdleHook(void); /* Function Prototypes */ -void TestTask(void *pvParameters); +void TickTask(void *pvParameters); void Flashy(void); +void SysTick_Handler(void); /** * Main function @@ -57,14 +58,16 @@ int main() /* Delay system */ PIOS_DELAY_Init(); - Flashy(); - /* Enables the SDCard */ -// PIOS_SDCARD_Init(); - - /* Call LoadSettings which populates System Vars + PIOS_SDCARD_Init(); + + /* Call LoadSettings which populates System Vars so the rest of the hardware can be configured. */ -// PIOS_Settings_Load(); + //PIOS_Settings_Load(); + + for(;;) { + + } /* Com ports init */ // PIOS_COM_Init(); @@ -77,7 +80,7 @@ int main() /* Create a FreeRTOS task */ - xTaskCreate( TestTask, ( signed portCHAR * ) "Test", configMINIMAL_STACK_SIZE , NULL, 2, NULL ); + xTaskCreate(TickTask, (signed portCHAR *) "Test", configMINIMAL_STACK_SIZE , NULL, 2, NULL); /* Start the FreeRTOS scheduler */ vTaskStartScheduler(); @@ -105,19 +108,18 @@ void Flashy(void) } } -void TestTask( void *pvParameters ) +void TickTask(void *pvParameters) { -const portTickType xDelay = 500 / portTICK_RATE_MS; + const portTickType xDelay = 10 / portTICK_RATE_MS; - while(1) - { - PIOS_LED_Toggle(LED1); - vTaskDelay(xDelay); - } + for(;;) + { + PIOS_LED_Toggle(LED2); + vTaskDelay(xDelay); + } } - /** * Idle hook function */