PageRenderTime 31ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/main.c

https://gitlab.com/timofonic/smart-plug
C | 397 lines | 199 code | 77 blank | 121 comment | 5 complexity | 5d768e074f5cd44593a0c102774e6cf2 MD5 | raw file
  1. /******************************************************************************/
  2. /** \file main.c
  3. *******************************************************************************
  4. *
  5. * \brief This is the freeRTOS base project for STM32f103.
  6. *
  7. * \author fonts2
  8. *
  9. * \date 16.05.2013
  10. *
  11. * \remark Last Modification
  12. * \li fonts2, 16.05.2013 created
  13. *
  14. ******************************************************************************/
  15. /*
  16. * functions global:
  17. * main
  18. * functions local:
  19. * vCreateTasks
  20. *
  21. ******************************************************************************/
  22. //----- Header-Files -----------------------------------------------------------
  23. /* Standard includes. */
  24. #include <stdio.h>
  25. #include "stm32f10x_it.h"
  26. #include "Metering.h"
  27. #include "Connection.h"
  28. #include "Logger.h"
  29. #include <FreeRTOS.h>
  30. #include <task.h>
  31. #include <queue.h>
  32. #include <semphr.h>
  33. #include <timers.h>
  34. #include "constants.h"
  35. //----- Macros -----------------------------------------------------------------
  36. #define SIZE_OF_POWER_MESSAGE_QUEUE 2
  37. #define SIZE_OF_TASK_MESSAGE_QUEUE 20
  38. #define SIZE_OF_VALUE_ITEM sizeof(struct Value)
  39. #define SIZE_OF_MESSAGE_ITEM sizeof(struct Message)
  40. #define SIZE_OF_TIME_ITEM sizeof(struct Time)
  41. #define PRIORITY_METERING ( 7 ) /* Priority of METERING */
  42. #define PRIORITY_CONNECTION ( 7 ) /* Priority of CONNECTION */
  43. #define PRIORITY_LOGGER ( 7 ) /* Priority of LOGGER */
  44. #define STACKSIZE_TASK ( 256 ) /* Stacksize of Task1 [Number of Words] */
  45. //----- Data types -------------------------------------------------------------
  46. //----- Function prototypes ----------------------------------------------------
  47. static void prvSetupHardware( void );
  48. static void Init( void );
  49. static void vCreateTasks(void);
  50. //----- Data -------------------------------------------------------------------
  51. /*-----------------------------------------------------------*/
  52. /*******************************************************************************
  53. * function : main
  54. ******************************************************************************/
  55. /** \brief Initialize Hardware
  56. *
  57. * \type global
  58. *
  59. * \return error code
  60. *
  61. ******************************************************************************/
  62. int main( void )
  63. {
  64. //Messagequeues initialisieren
  65. xQueuePowerValue = xQueueCreate(SIZE_OF_POWER_MESSAGE_QUEUE, SIZE_OF_VALUE_ITEM);
  66. xQueuePowerMessage = xQueueCreate(SIZE_OF_POWER_MESSAGE_QUEUE, SIZE_OF_MESSAGE_ITEM);
  67. xQueueTime = xQueueCreate(SIZE_OF_POWER_MESSAGE_QUEUE, SIZE_OF_TIME_ITEM);
  68. prvSetupHardware();
  69. Init();
  70. /* Create all application tasks and start the scheduler --------------------*/
  71. vCreateTasks();
  72. vTaskStartScheduler();
  73. /* If there is no failure, then this code is never reached */
  74. while ( 1 )
  75. {
  76. }
  77. }
  78. /*******************************************************************************
  79. * function : vCreateTasks
  80. ******************************************************************************/
  81. /** \brief Create all application task
  82. *
  83. * \type local
  84. *
  85. * \return void
  86. *
  87. ******************************************************************************/
  88. static void vCreateTasks(void) {
  89. xTaskCreate(vMeteringTask,
  90. (signed char *)"vMeteringTask",
  91. STACKSIZE_TASK,
  92. NULL,
  93. PRIORITY_METERING,
  94. NULL);
  95. xTaskCreate(vConnectionTask,
  96. (signed char *)"vConnectionTask",
  97. STACKSIZE_TASK,
  98. NULL,
  99. PRIORITY_CONNECTION,
  100. NULL);
  101. xTaskCreate(vLoggerTask,
  102. (signed char *)"vLoggerTask",
  103. STACKSIZE_TASK,
  104. NULL,
  105. PRIORITY_LOGGER,
  106. NULL);
  107. }
  108. /*-----------------------------------------------------------*/
  109. /*******************************************************************************
  110. * function : init
  111. ******************************************************************************/
  112. /** \brief Initialize Hardware
  113. *
  114. * \type global
  115. *
  116. * \return error code
  117. *
  118. ******************************************************************************/
  119. static void Init( void )
  120. {
  121. /* Configure the clocks, GPIO and other peripherals */
  122. GPIO_InitTypeDef GPIO_InitStructure;
  123. SPI_InitTypeDef SPI_InitStructure;
  124. TIM_TimeBaseInitTypeDef TIM_TimeBase_InitStructure;
  125. NVIC_InitTypeDef NVIC_InitStructure;
  126. // enable SPI 1 & 2 peripheral clock and GPIO
  127. RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE );
  128. RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
  129. /****** LED and Switches *******/
  130. /*-----------------------------------------------*/
  131. // === INIT LED ===
  132. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  133. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  134. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  135. GPIO_Init(GPIOB, &GPIO_InitStructure);
  136. // === ZCR0 ===
  137. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  138. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  139. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  140. GPIO_Init(GPIOC, &GPIO_InitStructure);
  141. // === INIT Solid-State Relais ===
  142. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  143. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  144. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  145. GPIO_Init(GPIOB, &GPIO_InitStructure);
  146. // === INIT TASTER ===
  147. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  148. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  149. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  150. GPIO_Init(GPIOC, &GPIO_InitStructure);
  151. /****** nRF8001 Prepare pins and start SPI *******/
  152. /*-----------------------------------------------*/
  153. // === INIT reqn ===
  154. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  155. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  156. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  157. GPIO_Init(GPIOB, &GPIO_InitStructure);
  158. GPIO_SetBits(GPIOB, GPIO_Pin_9);
  159. // === INIT rdyn ===
  160. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  161. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  162. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  163. GPIO_Init(GPIOB, &GPIO_InitStructure);
  164. // === INIT reset ===
  165. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  166. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  167. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  168. GPIO_Init(GPIOB, &GPIO_InitStructure);
  169. GPIO_SetBits(GPIOB, GPIO_Pin_6);
  170. // === SPI1 ===
  171. /* configure SPI SCK -------------------------------------------------------*/
  172. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  173. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  174. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  175. GPIO_Init( GPIOA, &GPIO_InitStructure );
  176. /* configure SPI MISO ------------------------------------------------------*/
  177. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  178. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  179. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  180. GPIO_Init( GPIOA, &GPIO_InitStructure );
  181. /* configure SPI MOSI ------------------------------------------------------*/
  182. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  183. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  184. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  185. GPIO_Init( GPIOA, &GPIO_InitStructure );
  186. /* configure SPI1 as master-------------------------------------------------*/
  187. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  188. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  189. SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  190. SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  191. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  192. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
  193. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  194. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
  195. SPI_Init( SPI1, &SPI_InitStructure );
  196. /* SPI1 ENABLE -------------------------------------------------------------*/
  197. SPI_Cmd(SPI1, ENABLE);
  198. /****** STPM01 Prepare pins and start SPI *******/
  199. /*-----------------------------------------------*/
  200. // === SYN0 ===
  201. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  202. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  203. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  204. GPIO_Init(GPIOA, &GPIO_InitStructure);
  205. GPIO_SetBits(GPIOA, GPIO_Pin_9);
  206. // === SPI2 ===
  207. /* configure SPI NSS ------------------------------------------------------*/
  208. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  209. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  210. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  211. GPIO_Init( GPIOB, &GPIO_InitStructure );
  212. GPIO_SetBits(GPIOB, GPIO_Pin_12);
  213. /* configure SPI MOSI ------------------------------------------------------*/
  214. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
  215. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  216. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  217. GPIO_Init( GPIOB, &GPIO_InitStructure );
  218. /* configure SPI SCK -------------------------------------------------------*/
  219. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  220. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  221. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  222. GPIO_Init( GPIOB, &GPIO_InitStructure );
  223. /* configure SPI MISO ------------------------------------------------------*/
  224. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
  225. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  226. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  227. GPIO_Init( GPIOB, &GPIO_InitStructure );
  228. /* configure SPI2 as master-------------------------------------------------*/
  229. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  230. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  231. SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  232. SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  233. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  234. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  235. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  236. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;//18MHz
  237. SPI_Init( SPI2, &SPI_InitStructure );
  238. /* SPI2 ENABLE -------------------------------------------------------------*/
  239. SPI_Cmd(SPI2, ENABLE);
  240. // === Timer ===
  241. /* MCO ------------------------------------------------------*/
  242. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  243. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  244. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  245. GPIO_Init( GPIOA, &GPIO_InitStructure );
  246. RCC_MCOConfig(RCC_MCO_HSI);
  247. /* SCK ------------------------------------------------------*/
  248. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  249. TIM_TimeBase_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  250. TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  251. TIM_TimeBase_InitStructure.TIM_Period = 799;
  252. TIM_TimeBase_InitStructure.TIM_Prescaler = 0;
  253. TIM_TimeBaseInit(TIM2, &TIM_TimeBase_InitStructure);
  254. NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  255. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  256. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
  257. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
  258. NVIC_Init(&NVIC_InitStructure);
  259. }
  260. /*******************************************************************************
  261. * function : prvSetupHardware
  262. ******************************************************************************/
  263. /** \brief prvSetupHardware
  264. *
  265. * \type global
  266. *
  267. * \return error code
  268. *
  269. ******************************************************************************/
  270. static void prvSetupHardware( void )
  271. {
  272. /* Start with the clocks in their expected state. */
  273. RCC_DeInit();
  274. /* Enable HSE (high speed external clock). */
  275. RCC_HSEConfig( RCC_HSE_ON );
  276. /* Wait till HSE is ready. */
  277. while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
  278. {
  279. }
  280. /* 2 wait states required on the flash. */
  281. *( ( unsigned portLONG * ) 0x40022000 ) = 0x02;
  282. /* HCLK = SYSCLK */
  283. RCC_HCLKConfig( RCC_SYSCLK_Div1 );
  284. /* PCLK2 = HCLK */
  285. RCC_PCLK2Config( RCC_HCLK_Div1 );
  286. /* PCLK1 = HCLK/2 */
  287. RCC_PCLK1Config( RCC_HCLK_Div2 );
  288. /* PLLCLK = (16MHz / 2) * 9 = 72 MHz. */
  289. RCC_PLLConfig( RCC_PLLSource_HSE_Div2, RCC_PLLMul_9 );
  290. /* Enable PLL. */
  291. RCC_PLLCmd( ENABLE );
  292. /* Wait till PLL is ready. */
  293. while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  294. {
  295. }
  296. /* Select PLL as system clock source. */
  297. RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
  298. /* Wait till PLL is used as system clock source. */
  299. while( RCC_GetSYSCLKSource() != 0x08 )
  300. {
  301. }
  302. /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
  303. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
  304. RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
  305. /* Set the Vector Table base address at 0x08000000 */
  306. NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
  307. NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
  308. /* Configure HCLK clock as SysTick clock source. */
  309. SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
  310. }
  311. /*-----------------------------------------------------------*/
  312. #ifdef DEBUG
  313. /* Keep the linker happy. */
  314. void assert_failed( unsigned portCHAR* pcFile, unsigned portLONG ulLine )
  315. {
  316. for( ;; )
  317. {
  318. }
  319. }
  320. #endif