Advertisement
Guest User

main.c

a guest
Apr 26th, 2013
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.17 KB | None | 0 0
  1. /*******************************************************************************
  2. * File Name : main.c
  3. * Author : Martin Thomas, main-skeleton based on code from the
  4. * STMicroelectronics MCD Application Team
  5. * Version : see VERSION_STRING below
  6. * Date : see VERSION_STRING below
  7. * Description : Main program body for the SD-Card tests
  8. ********************************************************************************
  9. * License: 3BSD
  10. *******************************************************************************/
  11.  
  12. #define VERSION_STRING "V1.2.1 7/2010"
  13.  
  14. /* Includes ------------------------------------------------------------------*/
  15. #include <stdint.h>
  16. #include "stm32f10x.h"
  17. #include "platform_config.h"
  18. #include "main.h"
  19. #include "comm.h"
  20. #include "rtc.h"
  21. #include "diskio.h" /* disk_timerproc */
  22. #include "ff_test_term.h"
  23. #include "misc_test_term.h"
  24.  
  25.  
  26. /* Private typedef -----------------------------------------------------------*/
  27. typedef enum { APPSTATE_FF_TERM, APPSTATE_TESTMENU } AppState;
  28.  
  29. /* Private variables ---------------------------------------------------------*/
  30. USART_InitTypeDef USART_InitStructure;
  31.  
  32. const char welcome[] = "\r\nHello from a STM32 Demo-Application, M. Thomas\r\n" VERSION_STRING"\r\n";
  33. const char gimmick[] =
  34. " _____ _______ __ __ ____ ___ ______ __ ___\r\n"\
  35. " / ____|__ __| \\/ |___ \\__ \\| ____/_ |/ _ \\\r\n"\
  36. "| (___ | | | \\ / | __) | ) | |__ | | | | |_ __\r\n"\
  37. " \\___ \\ | | | |\\/| ||__ < / /| __| | | | | \\ \\/ /\r\n"\
  38. " ____) | | | | | | |___) / /_| | | | |_| |> <\r\n"\
  39. "|_____/ |_| |_| |_|____/____|_| |_|\\___//_/\\_\\\r\n";
  40.  
  41.  
  42. /* Private function prototypes -----------------------------------------------*/
  43. void Periph_Configuration(void);
  44. void GPIO_Configuration(void);
  45. void NVIC_Configuration(void);
  46.  
  47. void stdio_tests(void);
  48. void dcc_tests(void);
  49.  
  50. /* Public functions -- -------------------------------------------------------*/
  51.  
  52. /*******************************************************************************
  53. * Function Name : main_systick_action
  54. * Description : operations to be done every 1ms
  55. * Input : None
  56. * Output : None
  57. * Return : None
  58. * overrides weak SysTick_Handler in startup_stm32*.c
  59. * When a RAMFUNC every function called from inside the ISR must be
  60. * reachable. This can be achieved by using compiler-option -mlong-calls
  61. * which globally enables long-calls. Here this option has not been used
  62. * instead the unreachable functions GPIO_Set/ResetBits have been replaced
  63. * by direct register-writes and disk_timerproc has also been attributed
  64. * as RAMFUNC to be reachable.
  65. *******************************************************************************/
  66. RAMFUNC void SysTick_Handler(void)
  67. {
  68. static uint16_t cnt=0;
  69. static uint8_t flip=0, cntdiskio=0;
  70.  
  71. cnt++;
  72. if( cnt >= 700 ) {
  73. cnt = 0;
  74. /* alive sign */
  75. if ( flip ) {
  76. // GPIO_SetBits(GPIO_LED, GPIO_Pin_Stat );
  77. GPIO_LED->BSRR = GPIO_Pin_Stat;
  78. } else {
  79. // GPIO_ResetBits(GPIO_LED, GPIO_Pin_Stat );
  80. GPIO_LED->BRR = GPIO_Pin_Stat;
  81. }
  82. flip = !flip;
  83. }
  84.  
  85. cntdiskio++;
  86. if ( cntdiskio >= 10 ) {
  87. cntdiskio = 0;
  88. disk_timerproc(); /* to be called every 10ms */
  89. }
  90.  
  91. ff_test_term_timerproc(); /* to be called every ms */
  92. }
  93.  
  94. /*******************************************************************************
  95. * Function Name : main
  96. * Description : Main program
  97. * Input : None
  98. * Output : None
  99. * Return : None
  100. *******************************************************************************/
  101. int main(void)
  102. {
  103. AppState appState = APPSTATE_FF_TERM;
  104.  
  105. /* System Clocks Configuration */
  106. Periph_Configuration();
  107.  
  108. /* NVIC configuration */
  109. NVIC_Configuration();
  110.  
  111. /* Configure the GPIO ports */
  112. GPIO_Configuration();
  113.  
  114. /* Turn on/off LED(s) */
  115. GPIO_SetBits(GPIO_LED, GPIO_Pin_LED2 /*| GPIO_Pin_LED4*/);
  116.  
  117. /* Setup SysTick Timer for 1 millisecond interrupts, also enables Systick and Systick-Interrupt */
  118. if (SysTick_Config(SystemCoreClock / 1000))
  119. {
  120. /* Capture error */
  121. while (1);
  122. }
  123.  
  124. /* USART1 and USART2 configuration ------------------------------------------------------*/
  125. /* USART and USART2 configured as follow:
  126. - BaudRate = 9600 baud
  127. - Word Length = 8 Bits
  128. - One Stop Bit
  129. - No parity
  130. - Hardware flow control disabled (RTS and CTS signals)
  131. - Receive and transmit enabled
  132. */
  133. //USART_InitStructure.USART_BaudRate = 19200;
  134. USART_InitStructure.USART_BaudRate = 9600;
  135. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  136. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  137. USART_InitStructure.USART_Parity = USART_Parity_No;
  138. USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None;
  139. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  140.  
  141. /* Configure USART1 */
  142. USART_Init(USART1, &USART_InitStructure);
  143. /* Configure USART2 */
  144. USART_Init(USART2, &USART_InitStructure);
  145.  
  146. /* Enable the USART1 */
  147. USART_Cmd(USART1, ENABLE);
  148. /* Enable the USART2 */
  149. USART_Cmd(USART2, ENABLE);
  150.  
  151. comm_puts(welcome);
  152. comm_puts(gimmick);
  153.  
  154. #if STDIO_TEST
  155. stdio_tests();
  156. #endif
  157.  
  158. #if DCC_TEST
  159. dcc_tests();
  160. #endif
  161.  
  162. rtc_init();
  163.  
  164. while (1)
  165. {
  166. switch ( appState )
  167. {
  168. case APPSTATE_FF_TERM:
  169. /* ff_test_term is not reentrant, blocks until exit */
  170. if ( !ff_test_term() )
  171. {
  172. appState = APPSTATE_TESTMENU;
  173. }
  174. break;
  175. case APPSTATE_TESTMENU:
  176. /* misc_test_term is a state-machine, main-loop keeps running
  177. * but may be throttled by time-consuming routines */
  178. if ( !misc_test_term() )
  179. {
  180. appState = APPSTATE_FF_TERM;
  181. }
  182. break;
  183. default:
  184. appState = APPSTATE_TESTMENU;
  185. break;
  186. }
  187. }
  188.  
  189. }
  190.  
  191. /*******************************************************************************
  192. * Function Name : PeriphConfiguration
  193. * Description : Configures the different system clocks.
  194. * Input : None
  195. * Output : None
  196. * Return : None
  197. *******************************************************************************/
  198. void Periph_Configuration(void)
  199. {
  200. /* Enable USART1, GPIOA, GPIOD ,GPIOC and AFIO clocks */
  201. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOx
  202. | RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC , ENABLE);
  203. /* Enable USART2 clock */
  204. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  205.  
  206. /* Enable GPIO_LED clock */
  207. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_LED, ENABLE);
  208.  
  209. /* DMA1 clock enable */
  210. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  211.  
  212. }
  213.  
  214. /*******************************************************************************
  215. * Function Name : GPIO_Configuration
  216. * Description : Configures the different GPIO ports.
  217. * Input : None
  218. * Output : None
  219. * Return : None
  220. *******************************************************************************/
  221. void GPIO_Configuration(void)
  222. {
  223. GPIO_InitTypeDef GPIO_InitStructure;
  224.  
  225. #if defined(USE_STM3210B_EVAL) || defined(USE_EK_STM32F) // || defined(USE_STM32_P103)
  226. /* Enable the USART2 Pins Software Remapping */
  227. GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
  228. #endif
  229.  
  230. /* Configure USART1 TX (PA.09) as alternate function push-pull */
  231. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  232. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  233. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  234. GPIO_Init(GPIOA, &GPIO_InitStructure);
  235.  
  236. /* Configure USART2 TX as alternate function push-pull */
  237. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  238. GPIO_Init(GPIOA, &GPIO_InitStructure);
  239. //GPIO_Init(GPIOx, &GPIO_InitStructure);
  240. /* Configure USART1 RX (PA.10) as input floating */
  241. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  242. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  243. GPIO_Init(GPIOA, &GPIO_InitStructure);
  244.  
  245. /* Configure USART2 RX as input floating */
  246. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  247. //GPIO_Init(GPIOx, &GPIO_InitStructure);
  248. GPIO_Init(GPIOA, &GPIO_InitStructure);
  249.  
  250. /* Configure GPIO for LEDs as Output push-pull */
  251. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_LED1 | GPIO_Pin_LED2 | GPIO_Pin_LED3 | GPIO_Pin_LED4;
  252. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  253. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  254. GPIO_Init(GPIO_LED, &GPIO_InitStructure);
  255.  
  256.  
  257. /* Configure PC12 Stat led in output pushpull mode */
  258. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  259. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  260. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  261. GPIO_Init(GPIOC, &GPIO_InitStructure);
  262.  
  263.  
  264.  
  265. #if defined(USE_MINI_STM32)
  266. /* touch-controller's CS (PA4), SD-Card's CS (PB6) and DataFlash CS (PB7) high = unselect */
  267. /* PB6 and PB7 both have an external 4,7kOhm pull-up */
  268. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  269. #if 1
  270. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  271. GPIO_Init(GPIOA, &GPIO_InitStructure);
  272. #else
  273. GPIO_Init(GPIOA, &GPIO_InitStructure);
  274. GPIO_SetBits(GPIOA, GPIO_Pin_4);
  275. #endif
  276. #endif /* defined(USE_MINI_STM32) */
  277. }
  278.  
  279. /*******************************************************************************
  280. * Function Name : NVIC_Configuration
  281. * Description : Configures Vector Table base location.
  282. * Input : None
  283. * Output : None
  284. * Return : None
  285. *******************************************************************************/
  286. #ifdef VECT_TAB_RAM
  287. /* vector-offset (TBLOFF) from bottom of SRAM. defined in linker script */
  288. extern uint32_t _isr_vectorsram_offs;
  289. void NVIC_Configuration(void)
  290. {
  291. /* Set the Vector Table base location at 0x20000000+_isr_vectorsram_offs */
  292. NVIC_SetVectorTable(NVIC_VectTab_RAM, (uint32_t)&_isr_vectorsram_offs);
  293. }
  294. #else
  295. extern uint32_t _isr_vectorsflash_offs;
  296. void NVIC_Configuration(void)
  297. {
  298. /* Set the Vector Table base location at 0x08000000+_isr_vectorsflash_offs */
  299. NVIC_SetVectorTable(NVIC_VectTab_FLASH, (uint32_t)&_isr_vectorsflash_offs);
  300. }
  301. #endif /* VECT_TAB_RAM */
  302.  
  303.  
  304. /*******************************************************************************
  305. * Function Name : assert_failed
  306. * Description : called on failed assertion if compiled with USE_FULL_ASSERT
  307. * Input : pointers to char-arrays/strings: filename, function-name,
  308. * line in file
  309. * Output : via xprintf
  310. * Return : None
  311. *******************************************************************************/
  312. #ifdef USE_FULL_ASSERT
  313.  
  314. #include "term_io.h"
  315.  
  316. /**
  317. * @brief Reports the name of the source file and the source line number
  318. * where the assert_param error has occurred.
  319. * @param file: pointer to the source file name
  320. * @param line: assert_param error line source number
  321. * @retval : None
  322. */
  323. void assert_failed(const uint8_t* file, const uint8_t* function, uint32_t line)
  324. {
  325. /* User can add his own implementation to report the file name and line number,
  326. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  327.  
  328. xprintf("\n !!! Wrong parameter value detected\n");
  329. xprintf(" - file %s\n", file);
  330. xprintf(" - function %s\n", function);
  331. xprintf(" - line %lu\n", line);
  332.  
  333. #if 0
  334. /* Infinite loop */
  335. while (1)
  336. {
  337. }
  338. #endif
  339. }
  340. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement