Advertisement
Guest User

main.c

a guest
Apr 24th, 2013
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.01 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 >= 500 ) {
  73. cnt = 0;
  74. /* alive sign */
  75. if ( flip ) {
  76. // GPIO_SetBits(GPIO_LED, GPIO_Pin_LED2 );
  77. GPIO_LED->BSRR = GPIO_Pin_LED2;
  78. } else {
  79. // GPIO_ResetBits(GPIO_LED, GPIO_Pin_LED2 );
  80. GPIO_LED->BRR = GPIO_Pin_LED2;
  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_Stat /*| 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 = 19200 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 = 115200;
  134. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  135. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  136. USART_InitStructure.USART_Parity = USART_Parity_No;
  137. USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None;
  138. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  139.  
  140. /* Configure USART1 */
  141. USART_Init(USART1, &USART_InitStructure);
  142. /* Configure USART2 */
  143. USART_Init(USART2, &USART_InitStructure);
  144.  
  145. /* Enable the USART1 */
  146. USART_Cmd(USART1, ENABLE);
  147. /* Enable the USART2 */
  148. USART_Cmd(USART2, ENABLE);
  149.  
  150. comm_puts(welcome);
  151. comm_puts(gimmick);
  152.  
  153. #if STDIO_TEST
  154. stdio_tests();
  155. #endif
  156.  
  157. #if DCC_TEST
  158. dcc_tests();
  159. #endif
  160.  
  161. rtc_init();
  162.  
  163. while (1)
  164. {
  165. switch ( appState )
  166. {
  167. case APPSTATE_FF_TERM:
  168. /* ff_test_term is not reentrant, blocks until exit */
  169. if ( !ff_test_term() )
  170. {
  171. appState = APPSTATE_TESTMENU;
  172. }
  173. break;
  174. case APPSTATE_TESTMENU:
  175. /* misc_test_term is a state-machine, main-loop keeps running
  176. * but may be throttled by time-consuming routines */
  177. if ( !misc_test_term() )
  178. {
  179. appState = APPSTATE_FF_TERM;
  180. }
  181. break;
  182. default:
  183. appState = APPSTATE_TESTMENU;
  184. break;
  185. }
  186. }
  187.  
  188. }
  189.  
  190. /*******************************************************************************
  191. * Function Name : PeriphConfiguration
  192. * Description : Configures the different system clocks.
  193. * Input : None
  194. * Output : None
  195. * Return : None
  196. *******************************************************************************/
  197. void Periph_Configuration(void)
  198. {
  199. /* Enable USART1, GPIOA, GPIOD ,GPIOC and AFIO clocks */
  200. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOx
  201. | RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC , ENABLE);
  202. /* Enable USART2 clock */
  203. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  204.  
  205. /* Enable GPIO_LED clock */
  206. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_LED, ENABLE);
  207.  
  208. /* DMA1 clock enable */
  209. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  210.  
  211. }
  212.  
  213. /*******************************************************************************
  214. * Function Name : GPIO_Configuration
  215. * Description : Configures the different GPIO ports.
  216. * Input : None
  217. * Output : None
  218. * Return : None
  219. *******************************************************************************/
  220. void GPIO_Configuration(void)
  221. {
  222. GPIO_InitTypeDef GPIO_InitStructure;
  223.  
  224. #if defined(USE_STM3210B_EVAL) || defined(USE_EK_STM32F)
  225. /* Enable the USART2 Pins Software Remapping */
  226. GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
  227. #endif
  228.  
  229. /* Configure USART1 TX (PA.09) as alternate function push-pull */
  230. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  231. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  232. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  233. GPIO_Init(GPIOA, &GPIO_InitStructure);
  234.  
  235. /* Configure USART2 TX as alternate function push-pull */
  236. GPIO_InitStructure.GPIO_Pin = GPIO_TxPin;
  237. GPIO_Init(GPIOx, &GPIO_InitStructure);
  238.  
  239. /* Configure USART1 RX (PA.10) as input floating */
  240. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  241. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  242. GPIO_Init(GPIOA, &GPIO_InitStructure);
  243.  
  244. /* Configure USART2 RX as input floating */
  245. GPIO_InitStructure.GPIO_Pin = GPIO_RxPin;
  246. GPIO_Init(GPIOx, &GPIO_InitStructure);
  247.  
  248. /* Configure GPIO for LEDs as Output push-pull */
  249. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_LED1 | GPIO_Pin_LED2 | GPIO_Pin_LED3 | GPIO_Pin_LED4;
  250. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  251. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  252. GPIO_Init(GPIO_LED, &GPIO_InitStructure);
  253.  
  254.  
  255. /* Configure PC12 in output pushpull mode */
  256. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  257. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  258. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  259. GPIO_Init(GPIOC, &GPIO_InitStructure);
  260.  
  261.  
  262.  
  263. #if defined(USE_MINI_STM32)
  264. /* touch-controller's CS (PA4), SD-Card's CS (PB6) and DataFlash CS (PB7) high = unselect */
  265. /* PB6 and PB7 both have an external 4,7kOhm pull-up */
  266. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  267. #if 1
  268. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  269. GPIO_Init(GPIOA, &GPIO_InitStructure);
  270. #else
  271. GPIO_Init(GPIOA, &GPIO_InitStructure);
  272. GPIO_SetBits(GPIOA, GPIO_Pin_4);
  273. #endif
  274. #endif /* defined(USE_MINI_STM32) */
  275. }
  276.  
  277. /*******************************************************************************
  278. * Function Name : NVIC_Configuration
  279. * Description : Configures Vector Table base location.
  280. * Input : None
  281. * Output : None
  282. * Return : None
  283. *******************************************************************************/
  284. #ifdef VECT_TAB_RAM
  285. /* vector-offset (TBLOFF) from bottom of SRAM. defined in linker script */
  286. extern uint32_t _isr_vectorsram_offs;
  287. void NVIC_Configuration(void)
  288. {
  289. /* Set the Vector Table base location at 0x20000000+_isr_vectorsram_offs */
  290. NVIC_SetVectorTable(NVIC_VectTab_RAM, (uint32_t)&_isr_vectorsram_offs);
  291. }
  292. #else
  293. extern uint32_t _isr_vectorsflash_offs;
  294. void NVIC_Configuration(void)
  295. {
  296. /* Set the Vector Table base location at 0x08000000+_isr_vectorsflash_offs */
  297. NVIC_SetVectorTable(NVIC_VectTab_FLASH, (uint32_t)&_isr_vectorsflash_offs);
  298. }
  299. #endif /* VECT_TAB_RAM */
  300.  
  301.  
  302. /*******************************************************************************
  303. * Function Name : assert_failed
  304. * Description : called on failed assertion if compiled with USE_FULL_ASSERT
  305. * Input : pointers to char-arrays/strings: filename, function-name,
  306. * line in file
  307. * Output : via xprintf
  308. * Return : None
  309. *******************************************************************************/
  310. #ifdef USE_FULL_ASSERT
  311.  
  312. #include "term_io.h"
  313.  
  314. /**
  315. * @brief Reports the name of the source file and the source line number
  316. * where the assert_param error has occurred.
  317. * @param file: pointer to the source file name
  318. * @param line: assert_param error line source number
  319. * @retval : None
  320. */
  321. void assert_failed(const uint8_t* file, const uint8_t* function, uint32_t line)
  322. {
  323. /* User can add his own implementation to report the file name and line number,
  324. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  325.  
  326. xprintf("\n !!! Wrong parameter value detected\n");
  327. xprintf(" - file %s\n", file);
  328. xprintf(" - function %s\n", function);
  329. xprintf(" - line %lu\n", line);
  330.  
  331. #if 0
  332. /* Infinite loop */
  333. while (1)
  334. {
  335. }
  336. #endif
  337. }
  338. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement