]> mj.ucw.cz Git - home-hw.git/blob - ssr/Src/main.c
da07b57dce35e694fb97dc40c7df7de1f1ccca6e
[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 volatile byte led_trigger;
56
57 /* USER CODE BEGIN PV */
58 /* Private variables ---------------------------------------------------------*/
59 struct usb usb;
60
61 /* USER CODE END PV */
62
63 /* Private function prototypes -----------------------------------------------*/
64 void SystemClock_Config(void);
65 static void MX_GPIO_Init(void);
66 static void MX_USB_PCD_Init(void);
67 static void MX_TIM4_Init(void);
68 static void MX_USART1_UART_Init(void);
69
70 /* USER CODE BEGIN PFP */
71 /* Private function prototypes -----------------------------------------------*/
72
73 /* USER CODE END PFP */
74
75 /* USER CODE BEGIN 0 */
76
77 static void process_packet(void)
78 {
79   uint cmd = get_u32_be(rx_packet);
80   uint arg = get_u32_be(rx_packet+4);
81   debug_printf("<< cmd %08x %08x\n", cmd, arg);
82
83   memset(tx_packet, 0, sizeof(tx_packet));
84   put_u32_be(tx_packet, 1);
85
86   switch (cmd)
87     {
88     case 1:
89       if (arg < 16)
90         {
91           if (arg & 1)
92             LL_GPIO_ResetOutputPin(SSR1_GPIO_Port, SSR1_Pin);
93           else
94             LL_GPIO_SetOutputPin(SSR1_GPIO_Port, SSR1_Pin);
95           if (arg & 2)
96             LL_GPIO_ResetOutputPin(SSR2_GPIO_Port, SSR2_Pin);
97           else
98             LL_GPIO_SetOutputPin(SSR2_GPIO_Port, SSR2_Pin);
99           if (arg & 4)
100             LL_GPIO_ResetOutputPin(SSR3_GPIO_Port, SSR3_Pin);
101           else
102             LL_GPIO_SetOutputPin(SSR3_GPIO_Port, SSR3_Pin);
103           if (arg & 8)
104             LL_GPIO_ResetOutputPin(SSR4_GPIO_Port, SSR4_Pin);
105           else
106             LL_GPIO_SetOutputPin(SSR4_GPIO_Port, SSR4_Pin);
107           put_u32_be(tx_packet, 0);
108         }
109       break;
110     }
111
112   debug_printf(">> status %08x\n", get_u32_be(tx_packet));
113 }
114
115 /* USER CODE END 0 */
116
117 /**
118   * @brief  The application entry point.
119   *
120   * @retval None
121   */
122 int main(void)
123 {
124   /* USER CODE BEGIN 1 */
125
126   /* USER CODE END 1 */
127
128   /* MCU Configuration----------------------------------------------------------*/
129
130   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
131   HAL_Init();
132
133   /* USER CODE BEGIN Init */
134
135   /* USER CODE END Init */
136
137   /* Configure the system clock */
138   SystemClock_Config();
139
140   /* USER CODE BEGIN SysInit */
141   usb_init(&usb, &hpcd_USB_FS);
142
143   /* USER CODE END SysInit */
144
145   /* Initialize all configured peripherals */
146   MX_GPIO_Init();
147
148   // A hack to let USB host reset us
149   LL_GPIO_InitTypeDef gpio;
150   gpio.Pin = LL_GPIO_PIN_12 | LL_GPIO_PIN_13;
151   gpio.Mode = LL_GPIO_MODE_OUTPUT;
152   gpio.Speed = LL_GPIO_SPEED_FREQ_HIGH;
153   gpio.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
154   LL_GPIO_Init(GPIOA, &gpio);
155   LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_12);
156   LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_13);
157   LL_mDelay(1000);
158
159   MX_USB_PCD_Init();
160   MX_TIM4_Init();
161   MX_USART1_UART_Init();
162   /* USER CODE BEGIN 2 */
163   usb_start(&usb);
164
165   LL_TIM_EnableCounter(TIM4);
166   LL_TIM_EnableIT_UPDATE(TIM4);
167   LL_TIM_GenerateEvent_UPDATE(TIM4);
168
169   /* USER CODE END 2 */
170
171   /* Infinite loop */
172   /* USER CODE BEGIN WHILE */
173   while (1)
174   {
175     if (led_trigger)
176       {
177         led_trigger = 0;
178         static byte led_state;
179         if (led_state)
180           LL_GPIO_SetOutputPin(LED_GPIO_Port, LED_Pin);
181         else
182           LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
183         led_state ^= 1;
184       }
185
186     if (rx_packet_state == 1 && !tx_packet_state)
187       {
188         tx_packet_state = 1;
189         process_packet();
190         usb_ep_send(&usb, 0x82, tx_packet, 12);
191         rx_packet_state = 0;
192         usb_ep_receive(&usb, 0x01, rx_packet, 64);
193       }
194
195 #if 0
196     static int cnt;
197     debug_printf("Counter = %d\n", cnt);
198     cnt++;
199 #endif
200
201     __WFI();
202     // LL_mDelay(1000);
203
204   /* USER CODE END WHILE */
205
206   /* USER CODE BEGIN 3 */
207
208   }
209   /* USER CODE END 3 */
210
211 }
212
213 /**
214   * @brief System Clock Configuration
215   * @retval None
216   */
217 void SystemClock_Config(void)
218 {
219
220   LL_FLASH_SetLatency(LL_FLASH_LATENCY_2);
221
222    if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2)
223   {
224     Error_Handler();  
225   }
226   LL_RCC_HSE_Enable();
227
228    /* Wait till HSE is ready */
229   while(LL_RCC_HSE_IsReady() != 1)
230   {
231     
232   }
233   LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE_DIV_1, LL_RCC_PLL_MUL_9);
234
235   LL_RCC_PLL_Enable();
236
237    /* Wait till PLL is ready */
238   while(LL_RCC_PLL_IsReady() != 1)
239   {
240     
241   }
242   LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
243
244   LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
245
246   LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
247
248   LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
249
250    /* Wait till System clock is ready */
251   while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
252   {
253   
254   }
255   LL_Init1msTick(72000000);
256
257   LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
258
259   LL_SetSystemCoreClock(72000000);
260
261   LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_PLL_DIV_1_5);
262
263   /* SysTick_IRQn interrupt configuration */
264   NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
265 }
266
267 /* TIM4 init function */
268 static void MX_TIM4_Init(void)
269 {
270
271   LL_TIM_InitTypeDef TIM_InitStruct;
272   LL_TIM_OC_InitTypeDef TIM_OC_InitStruct;
273
274   /* Peripheral clock enable */
275   LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4);
276
277   /* TIM4 interrupt Init */
278   NVIC_SetPriority(TIM4_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
279   NVIC_EnableIRQ(TIM4_IRQn);
280
281   TIM_InitStruct.Prescaler = 7200;
282   TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
283   TIM_InitStruct.Autoreload = 1000;
284   TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
285   LL_TIM_Init(TIM4, &TIM_InitStruct);
286
287   LL_TIM_DisableARRPreload(TIM4);
288
289   LL_TIM_SetClockSource(TIM4, LL_TIM_CLOCKSOURCE_INTERNAL);
290
291   TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_FROZEN;
292   TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
293   TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
294   TIM_OC_InitStruct.CompareValue = 0;
295   TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
296   LL_TIM_OC_Init(TIM4, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
297
298   LL_TIM_OC_DisableFast(TIM4, LL_TIM_CHANNEL_CH1);
299
300   LL_TIM_SetTriggerOutput(TIM4, LL_TIM_TRGO_RESET);
301
302   LL_TIM_DisableMasterSlaveMode(TIM4);
303
304 }
305
306 /* USART1 init function */
307 static void MX_USART1_UART_Init(void)
308 {
309
310   LL_USART_InitTypeDef USART_InitStruct;
311
312   LL_GPIO_InitTypeDef GPIO_InitStruct;
313
314   /* Peripheral clock enable */
315   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
316   
317   /**USART1 GPIO Configuration  
318   PA9   ------> USART1_TX
319   PA10   ------> USART1_RX 
320   */
321   GPIO_InitStruct.Pin = LL_GPIO_PIN_9;
322   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
323   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
324   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
325   LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
326
327   GPIO_InitStruct.Pin = LL_GPIO_PIN_10;
328   GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING;
329   LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
330
331   USART_InitStruct.BaudRate = 115200;
332   USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
333   USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
334   USART_InitStruct.Parity = LL_USART_PARITY_NONE;
335   USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
336   USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
337   LL_USART_Init(USART1, &USART_InitStruct);
338
339   LL_USART_ConfigAsyncMode(USART1);
340
341   LL_USART_Enable(USART1);
342
343 }
344
345 /* USB init function */
346 static void MX_USB_PCD_Init(void)
347 {
348
349   hpcd_USB_FS.Instance = USB;
350   hpcd_USB_FS.Init.dev_endpoints = 8;
351   hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
352   hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_64;
353   hpcd_USB_FS.Init.low_power_enable = DISABLE;
354   hpcd_USB_FS.Init.lpm_enable = DISABLE;
355   hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
356   if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK)
357   {
358     _Error_Handler(__FILE__, __LINE__);
359   }
360
361 }
362
363 /** Configure pins as 
364         * Analog 
365         * Input 
366         * Output
367         * EVENT_OUT
368         * EXTI
369 */
370 static void MX_GPIO_Init(void)
371 {
372
373   LL_GPIO_InitTypeDef GPIO_InitStruct;
374
375   /* GPIO Ports Clock Enable */
376   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC);
377   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOD);
378   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
379   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
380
381   /* LED output is OC, active low, defaults to high */
382   LL_GPIO_SetOutputPin(LED_GPIO_Port, LED_Pin);
383
384   /* SSR outputs are OC, active low, default to high */
385   LL_GPIO_SetOutputPin(GPIOB, SSR3_Pin|SSR4_Pin|SSR1_Pin|SSR2_Pin);
386
387   /**/
388   GPIO_InitStruct.Pin = LED_Pin;
389   GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
390   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
391   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
392   LL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
393
394   /**/
395   GPIO_InitStruct.Pin = SSR3_Pin|SSR4_Pin|SSR1_Pin|SSR2_Pin;
396   GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
397   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
398   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
399   LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
400
401 }
402
403 /* USER CODE BEGIN 4 */
404
405 /* USER CODE END 4 */
406
407 /**
408   * @brief  This function is executed in case of error occurrence.
409   * @param  file: The file name as string.
410   * @param  line: The line in file as a number.
411   * @retval None
412   */
413 void _Error_Handler(char *file, int line)
414 {
415   /* USER CODE BEGIN Error_Handler_Debug */
416   /* User can add his own implementation to report the HAL error return state */
417   while(1)
418   {
419   }
420   /* USER CODE END Error_Handler_Debug */
421 }
422
423 #ifdef  USE_FULL_ASSERT
424 /**
425   * @brief  Reports the name of the source file and the source line number
426   *         where the assert_param error has occurred.
427   * @param  file: pointer to the source file name
428   * @param  line: assert_param error line source number
429   * @retval None
430   */
431 void assert_failed(uint8_t* file, uint32_t line)
432
433   /* USER CODE BEGIN 6 */
434   /* User can add his own implementation to report the file name and line number,
435      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
436   /* USER CODE END 6 */
437 }
438 #endif /* USE_FULL_ASSERT */
439
440 /**
441   * @}
442   */
443
444 /**
445   * @}
446   */
447
448 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/