]> mj.ucw.cz Git - home-hw.git/blob - ssr/Src/main.c
3a3f0341944cb5ab0e841951b74bae71c0ea5633
[home-hw.git] / ssr / Src / main.c
1
2 /**
3   ******************************************************************************
4   * @file           : main.c
5   * @brief          : Main program body
6   ******************************************************************************
7   ** This notice applies to any and all portions of this file
8   * that are not between comment pairs USER CODE BEGIN and
9   * USER CODE END. Other portions of this file, whether 
10   * inserted by the user or by software development tools
11   * are owned by their respective copyright owners.
12   *
13   * COPYRIGHT(c) 2018 STMicroelectronics
14   *
15   * Redistribution and use in source and binary forms, with or without modification,
16   * are permitted provided that the following conditions are met:
17   *   1. Redistributions of source code must retain the above copyright notice,
18   *      this list of conditions and the following disclaimer.
19   *   2. Redistributions in binary form must reproduce the above copyright notice,
20   *      this list of conditions and the following disclaimer in the documentation
21   *      and/or other materials provided with the distribution.
22   *   3. Neither the name of STMicroelectronics nor the names of its contributors
23   *      may be used to endorse or promote products derived from this software
24   *      without specific prior written permission.
25   *
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36   *
37   ******************************************************************************
38   */
39 /* Includes ------------------------------------------------------------------*/
40 #include "main.h"
41 #include "stm32f1xx_hal.h"
42
43 /* USER CODE BEGIN Includes */
44 #include "util.h"
45 #include "usb.h"
46 #include "app.h"
47
48 #include <string.h>
49
50 /* USER CODE END Includes */
51
52 /* Private variables ---------------------------------------------------------*/
53
54 PCD_HandleTypeDef hpcd_USB_FS;
55
56 /* USER CODE BEGIN PV */
57 /* Private variables ---------------------------------------------------------*/
58 struct usb usb;
59
60 /* USER CODE END PV */
61
62 /* Private function prototypes -----------------------------------------------*/
63 void SystemClock_Config(void);
64 static void MX_GPIO_Init(void);
65 static void MX_USB_PCD_Init(void);
66 static void MX_TIM4_Init(void);
67 static void MX_USART1_UART_Init(void);
68
69 /* USER CODE BEGIN PFP */
70 /* Private function prototypes -----------------------------------------------*/
71
72 /* USER CODE END PFP */
73
74 /* USER CODE BEGIN 0 */
75
76 /* USER CODE END 0 */
77
78 /**
79   * @brief  The application entry point.
80   *
81   * @retval None
82   */
83 int main(void)
84 {
85   /* USER CODE BEGIN 1 */
86
87   /* USER CODE END 1 */
88
89   /* MCU Configuration----------------------------------------------------------*/
90
91   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
92   HAL_Init();
93
94   /* USER CODE BEGIN Init */
95
96   /* USER CODE END Init */
97
98   /* Configure the system clock */
99   SystemClock_Config();
100
101   /* USER CODE BEGIN SysInit */
102   usb_init(&usb, &hpcd_USB_FS);
103
104   /* USER CODE END SysInit */
105
106   /* Initialize all configured peripherals */
107   MX_GPIO_Init();
108
109   // A hack to let USB host reset us
110   LL_GPIO_InitTypeDef gpio;
111   gpio.Pin = LL_GPIO_PIN_12 | LL_GPIO_PIN_13;
112   gpio.Mode = LL_GPIO_MODE_OUTPUT;
113   gpio.Speed = LL_GPIO_SPEED_FREQ_HIGH;
114   gpio.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
115   LL_GPIO_Init(GPIOA, &gpio);
116   LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_12);
117   LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_13);
118   LL_mDelay(1000);
119
120   MX_USB_PCD_Init();
121   MX_TIM4_Init();
122   MX_USART1_UART_Init();
123   /* USER CODE BEGIN 2 */
124   usb_start(&usb);
125
126   LL_TIM_EnableCounter(TIM4);
127   LL_TIM_EnableIT_UPDATE(TIM4);
128   LL_TIM_GenerateEvent_UPDATE(TIM4);
129
130   /* USER CODE END 2 */
131
132   /* Infinite loop */
133   /* USER CODE BEGIN WHILE */
134   while (1)
135   {
136     static byte led_state;
137     if (led_state)
138       {
139         LL_GPIO_SetOutputPin(LED_GPIO_Port, LED_Pin);
140         LL_GPIO_SetOutputPin(SSR1_GPIO_Port, SSR1_Pin);
141         LL_GPIO_SetOutputPin(SSR2_GPIO_Port, SSR2_Pin);
142         LL_GPIO_SetOutputPin(SSR3_GPIO_Port, SSR3_Pin);
143         LL_GPIO_SetOutputPin(SSR4_GPIO_Port, SSR4_Pin);
144       }
145     else
146       {
147         LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
148         static byte xxx;
149         switch (xxx)
150           {
151           case 0:
152             LL_GPIO_ResetOutputPin(SSR1_GPIO_Port, SSR1_Pin);
153             break;
154           case 1:
155             LL_GPIO_ResetOutputPin(SSR2_GPIO_Port, SSR2_Pin);
156             break;
157           case 2:
158             LL_GPIO_ResetOutputPin(SSR3_GPIO_Port, SSR3_Pin);
159             break;
160           case 3:
161             LL_GPIO_ResetOutputPin(SSR4_GPIO_Port, SSR4_Pin);
162             break;
163           }
164         xxx = (xxx+1) % 4;
165       }
166     led_state ^= 1;
167
168     if (rx_packet_state == 1 && !tx_packet_state)
169       {
170         tx_packet_state = 1;
171         put_u32_be(tx_packet, 42);
172         usb_ep_send(&usb, 0x82, tx_packet, 12);
173         rx_packet_state = 0;
174         usb_ep_receive(&usb, 0x01, rx_packet, 64);
175       }
176
177     // debug_printf("Counter = %d\n", cnt);
178
179     //__WFI();
180     LL_mDelay(1000);
181
182   /* USER CODE END WHILE */
183
184   /* USER CODE BEGIN 3 */
185
186   }
187   /* USER CODE END 3 */
188
189 }
190
191 /**
192   * @brief System Clock Configuration
193   * @retval None
194   */
195 void SystemClock_Config(void)
196 {
197
198   LL_FLASH_SetLatency(LL_FLASH_LATENCY_2);
199
200    if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2)
201   {
202     Error_Handler();  
203   }
204   LL_RCC_HSE_Enable();
205
206    /* Wait till HSE is ready */
207   while(LL_RCC_HSE_IsReady() != 1)
208   {
209     
210   }
211   LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE_DIV_1, LL_RCC_PLL_MUL_9);
212
213   LL_RCC_PLL_Enable();
214
215    /* Wait till PLL is ready */
216   while(LL_RCC_PLL_IsReady() != 1)
217   {
218     
219   }
220   LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
221
222   LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
223
224   LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
225
226   LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
227
228    /* Wait till System clock is ready */
229   while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
230   {
231   
232   }
233   LL_Init1msTick(72000000);
234
235   LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
236
237   LL_SetSystemCoreClock(72000000);
238
239   LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_PLL_DIV_1_5);
240
241   /* SysTick_IRQn interrupt configuration */
242   NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
243 }
244
245 /* TIM4 init function */
246 static void MX_TIM4_Init(void)
247 {
248
249   LL_TIM_InitTypeDef TIM_InitStruct;
250   LL_TIM_OC_InitTypeDef TIM_OC_InitStruct;
251
252   /* Peripheral clock enable */
253   LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4);
254
255   /* TIM4 interrupt Init */
256   NVIC_SetPriority(TIM4_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
257   NVIC_EnableIRQ(TIM4_IRQn);
258
259   TIM_InitStruct.Prescaler = 7200;
260   TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
261   TIM_InitStruct.Autoreload = 1000;
262   TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
263   LL_TIM_Init(TIM4, &TIM_InitStruct);
264
265   LL_TIM_DisableARRPreload(TIM4);
266
267   LL_TIM_SetClockSource(TIM4, LL_TIM_CLOCKSOURCE_INTERNAL);
268
269   TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_FROZEN;
270   TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
271   TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
272   TIM_OC_InitStruct.CompareValue = 0;
273   TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
274   LL_TIM_OC_Init(TIM4, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
275
276   LL_TIM_OC_DisableFast(TIM4, LL_TIM_CHANNEL_CH1);
277
278   LL_TIM_SetTriggerOutput(TIM4, LL_TIM_TRGO_RESET);
279
280   LL_TIM_DisableMasterSlaveMode(TIM4);
281
282 }
283
284 /* USART1 init function */
285 static void MX_USART1_UART_Init(void)
286 {
287
288   LL_USART_InitTypeDef USART_InitStruct;
289
290   LL_GPIO_InitTypeDef GPIO_InitStruct;
291
292   /* Peripheral clock enable */
293   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
294   
295   /**USART1 GPIO Configuration  
296   PA9   ------> USART1_TX
297   PA10   ------> USART1_RX 
298   */
299   GPIO_InitStruct.Pin = LL_GPIO_PIN_9;
300   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
301   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
302   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
303   LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
304
305   GPIO_InitStruct.Pin = LL_GPIO_PIN_10;
306   GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING;
307   LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
308
309   USART_InitStruct.BaudRate = 115200;
310   USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
311   USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
312   USART_InitStruct.Parity = LL_USART_PARITY_NONE;
313   USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
314   USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
315   LL_USART_Init(USART1, &USART_InitStruct);
316
317   LL_USART_ConfigAsyncMode(USART1);
318
319   LL_USART_Enable(USART1);
320
321 }
322
323 /* USB init function */
324 static void MX_USB_PCD_Init(void)
325 {
326
327   hpcd_USB_FS.Instance = USB;
328   hpcd_USB_FS.Init.dev_endpoints = 8;
329   hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
330   hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_64;
331   hpcd_USB_FS.Init.low_power_enable = DISABLE;
332   hpcd_USB_FS.Init.lpm_enable = DISABLE;
333   hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
334   if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK)
335   {
336     _Error_Handler(__FILE__, __LINE__);
337   }
338
339 }
340
341 /** Configure pins as 
342         * Analog 
343         * Input 
344         * Output
345         * EVENT_OUT
346         * EXTI
347 */
348 static void MX_GPIO_Init(void)
349 {
350
351   LL_GPIO_InitTypeDef GPIO_InitStruct;
352
353   /* GPIO Ports Clock Enable */
354   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC);
355   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOD);
356   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
357   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
358
359   /* LED output is OC, active low, defaults to high */
360   LL_GPIO_SetOutputPin(LED_GPIO_Port, LED_Pin);
361
362   /* SSR outputs are OC, active low, default to high */
363   LL_GPIO_SetOutputPin(GPIOB, SSR3_Pin|SSR4_Pin|SSR1_Pin|SSR2_Pin);
364
365   /**/
366   GPIO_InitStruct.Pin = LED_Pin;
367   GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
368   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
369   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
370   LL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
371
372   /**/
373   GPIO_InitStruct.Pin = SSR3_Pin|SSR4_Pin|SSR1_Pin|SSR2_Pin;
374   GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
375   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
376   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
377   LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
378
379 }
380
381 /* USER CODE BEGIN 4 */
382
383 /* USER CODE END 4 */
384
385 /**
386   * @brief  This function is executed in case of error occurrence.
387   * @param  file: The file name as string.
388   * @param  line: The line in file as a number.
389   * @retval None
390   */
391 void _Error_Handler(char *file, int line)
392 {
393   /* USER CODE BEGIN Error_Handler_Debug */
394   /* User can add his own implementation to report the HAL error return state */
395   while(1)
396   {
397   }
398   /* USER CODE END Error_Handler_Debug */
399 }
400
401 #ifdef  USE_FULL_ASSERT
402 /**
403   * @brief  Reports the name of the source file and the source line number
404   *         where the assert_param error has occurred.
405   * @param  file: pointer to the source file name
406   * @param  line: assert_param error line source number
407   * @retval None
408   */
409 void assert_failed(uint8_t* file, uint32_t line)
410
411   /* USER CODE BEGIN 6 */
412   /* User can add his own implementation to report the file name and line number,
413      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
414   /* USER CODE END 6 */
415 }
416 #endif /* USE_FULL_ASSERT */
417
418 /**
419   * @}
420   */
421
422 /**
423   * @}
424   */
425
426 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/