]> mj.ucw.cz Git - home-hw.git/blob - Src/main.c
9d3dbb8905792a7ba4634cfea9b5e7177b88bb3f
[home-hw.git] / 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 #include "usb.h"
43
44 /* USER CODE BEGIN Includes */
45
46 /* USER CODE END Includes */
47
48 /* Private variables ---------------------------------------------------------*/
49
50 PCD_HandleTypeDef hpcd_USB_FS;
51
52 /* USER CODE BEGIN PV */
53 /* Private variables ---------------------------------------------------------*/
54 struct usb usb;
55
56 /* USER CODE END PV */
57
58 /* Private function prototypes -----------------------------------------------*/
59 void SystemClock_Config(void);
60 static void MX_GPIO_Init(void);
61 static void MX_I2C1_Init(void);
62 static void MX_I2C2_Init(void);
63 static void MX_USB_PCD_Init(void);
64
65 /* USER CODE BEGIN PFP */
66 /* Private function prototypes -----------------------------------------------*/
67
68 /* USER CODE END PFP */
69
70 /* USER CODE BEGIN 0 */
71
72 #include <stdarg.h>
73 #include <stdint.h>
74 #include <string.h>
75
76 void semi_put_char(char c)
77 {
78   // This is tricky, we need to work around GCC bugs
79   volatile char cc = c;
80   asm volatile (
81     "mov r0, #0x03\n"   /* SYS_WRITEC */
82     "mov r1, %[msg]\n"
83     "bkpt #0xAB\n"
84     :
85     : [msg] "r" (&cc)
86     : "r0", "r1"
87   );
88 }
89
90 void semi_write_string(char *c)
91 {
92   asm volatile (
93     "mov r0, #0x04\n"   /* SYS_WRITE0 */
94     "mov r1, %[msg]\n"
95     "bkpt #0xAB\n"
96     :
97     : [msg] "r" (c)
98     : "r0", "r1"
99   );
100 }
101
102 void debug_putc(int c)
103 {
104   static char debug_buf[16];
105   static int debug_i;
106   debug_buf[debug_i++] = c;
107   if (c == '\n' || debug_i >= sizeof(debug_buf) - 1)
108     {
109       debug_buf[debug_i] = 0;
110       semi_write_string(debug_buf);
111       debug_i = 0;
112     }
113 }
114
115 void debug_puts(const char *s)
116 {
117   while (*s)
118     debug_putc(*s++);
119 }
120
121 enum printf_flags {
122   PF_ZERO_PAD = 1,
123   PF_SIGNED = 2,
124   PF_NEGATIVE = 4,
125   PF_UPPERCASE = 8,
126   PF_LEFT = 16,
127 };
128
129 static void printf_string(const char *s, uint width, uint flags)
130 {
131   uint len = strlen(s);
132   uint pad = (len < width) ? width - len : 0;
133   char pad_char = (flags & PF_ZERO_PAD) ? '0' : ' ';
134
135   if (flags & PF_LEFT)
136     debug_puts(s);
137   while (pad--)
138     debug_putc(pad_char);
139   if (!(flags & PF_LEFT))
140     debug_puts(s);
141 }
142
143 static void printf_number(uint i, uint width, uint flags, uint base)
144 {
145   char buf[16];
146   char *w = buf + sizeof(buf);
147
148   if (flags & PF_SIGNED)
149     {
150       if ((int) i < 0)
151         {
152           i = - (int) i;
153           flags |= PF_NEGATIVE;
154         }
155     }
156
157   *--w = 0;
158   do
159     {
160       uint digit = i % base;
161       if (digit < 10)
162         *--w = '0' + digit;
163       else
164         *--w = ((flags & PF_UPPERCASE) ? 'A' : 'a') + digit - 10;
165       i /= base;
166     }
167   while (i);
168
169   if (flags & PF_NEGATIVE)
170     *--w = '-';
171
172   printf_string(w, width, flags);
173 }
174
175 void debug_printf(const char *fmt, ...)
176 {
177   va_list args;
178   va_start(args, fmt);
179
180   while (*fmt)
181     {
182       int c = *fmt++;
183       if (c != '%')
184         {
185           debug_putc(c);
186           continue;
187         }
188
189       uint width = 0;
190       uint flags = 0;
191
192       if (*fmt == '-')
193         {
194           fmt++;
195           flags |= PF_LEFT;
196         }
197
198       if (*fmt == '0')
199         {
200           fmt++;
201           flags |= PF_ZERO_PAD;
202         }
203
204       while (*fmt >= '0' && *fmt <= '9')
205         width = 10*width + *fmt++ - '0';
206
207       c = *fmt++;
208       switch (c)
209         {
210         case 'd':
211           printf_number(va_arg(args, int), width, flags | PF_SIGNED, 10);
212           break;
213         case 'u':
214           printf_number(va_arg(args, int), width, flags, 10);
215           break;
216         case 'X':
217           flags |= PF_UPPERCASE;
218           // fall-thru
219         case 'x':
220           printf_number(va_arg(args, int), width, flags, 16);
221           break;
222         case 's':
223           printf_string(va_arg(args, char *), width, flags);
224           break;
225         default:
226           debug_putc(c);
227           continue;
228         }
229     }
230
231   va_end(args);
232 }
233
234 /* USER CODE END 0 */
235
236 /**
237   * @brief  The application entry point.
238   *
239   * @retval None
240   */
241 int main(void)
242 {
243   /* USER CODE BEGIN 1 */
244
245   /* USER CODE END 1 */
246
247   /* MCU Configuration----------------------------------------------------------*/
248
249   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
250   HAL_Init();
251
252   /* USER CODE BEGIN Init */
253
254   /* USER CODE END Init */
255
256   /* Configure the system clock */
257   SystemClock_Config();
258
259   /* USER CODE BEGIN SysInit */
260
261   /* USER CODE END SysInit */
262
263   /* Initialize all configured peripherals */
264   MX_GPIO_Init();
265   MX_I2C1_Init();
266   MX_I2C2_Init();
267   MX_USB_PCD_Init();
268   /* USER CODE BEGIN 2 */
269   usb_init(&usb, &hpcd_USB_FS);
270
271   /* USER CODE END 2 */
272
273   /* Infinite loop */
274   /* USER CODE BEGIN WHILE */
275   int cnt = 0;
276   while (1)
277   {
278     LL_GPIO_SetOutputPin(LED_GPIO_Port, LED_Pin);
279     LL_mDelay(500);
280     LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
281     LL_mDelay(500);
282     debug_printf("Counter = %d\n", cnt++);
283
284   /* USER CODE END WHILE */
285
286   /* USER CODE BEGIN 3 */
287
288   }
289   /* USER CODE END 3 */
290
291 }
292
293 /**
294   * @brief System Clock Configuration
295   * @retval None
296   */
297 void SystemClock_Config(void)
298 {
299
300   LL_FLASH_SetLatency(LL_FLASH_LATENCY_2);
301
302    if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2)
303   {
304     Error_Handler();  
305   }
306   LL_RCC_HSE_Enable();
307
308    /* Wait till HSE is ready */
309   while(LL_RCC_HSE_IsReady() != 1)
310   {
311     
312   }
313   LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE_DIV_1, LL_RCC_PLL_MUL_9);
314
315   LL_RCC_PLL_Enable();
316
317    /* Wait till PLL is ready */
318   while(LL_RCC_PLL_IsReady() != 1)
319   {
320     
321   }
322   LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
323
324   LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
325
326   LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
327
328   LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
329
330    /* Wait till System clock is ready */
331   while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
332   {
333   
334   }
335   LL_Init1msTick(72000000);
336
337   LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
338
339   LL_SetSystemCoreClock(72000000);
340
341   LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_PLL_DIV_1_5);
342
343   /* SysTick_IRQn interrupt configuration */
344   NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
345 }
346
347 /* I2C1 init function */
348 static void MX_I2C1_Init(void)
349 {
350
351   LL_I2C_InitTypeDef I2C_InitStruct;
352
353   LL_GPIO_InitTypeDef GPIO_InitStruct;
354
355   /**I2C1 GPIO Configuration  
356   PB6   ------> I2C1_SCL
357   PB7   ------> I2C1_SDA 
358   */
359   GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7;
360   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
361   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
362   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
363   LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
364
365   /* Peripheral clock enable */
366   LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
367
368     /**I2C Initialization 
369     */
370   I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
371   I2C_InitStruct.ClockSpeed = 100000;
372   I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2;
373   I2C_InitStruct.OwnAddress1 = 0;
374   I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
375   I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
376   LL_I2C_Init(I2C1, &I2C_InitStruct);
377
378   LL_I2C_SetOwnAddress2(I2C1, 0);
379
380   LL_I2C_DisableOwnAddress2(I2C1);
381
382   LL_I2C_DisableGeneralCall(I2C1);
383
384   LL_I2C_EnableClockStretching(I2C1);
385
386 }
387
388 /* I2C2 init function */
389 static void MX_I2C2_Init(void)
390 {
391
392   LL_I2C_InitTypeDef I2C_InitStruct;
393
394   LL_GPIO_InitTypeDef GPIO_InitStruct;
395
396   /**I2C2 GPIO Configuration  
397   PB10   ------> I2C2_SCL
398   PB11   ------> I2C2_SDA 
399   */
400   GPIO_InitStruct.Pin = LL_GPIO_PIN_10|LL_GPIO_PIN_11;
401   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
402   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
403   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
404   LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
405
406   /* Peripheral clock enable */
407   LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C2);
408
409     /**I2C Initialization 
410     */
411   I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
412   I2C_InitStruct.ClockSpeed = 400000;
413   I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2;
414   I2C_InitStruct.OwnAddress1 = 0;
415   I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
416   I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
417   LL_I2C_Init(I2C2, &I2C_InitStruct);
418
419   LL_I2C_SetOwnAddress2(I2C2, 0);
420
421   LL_I2C_DisableOwnAddress2(I2C2);
422
423   LL_I2C_DisableGeneralCall(I2C2);
424
425   LL_I2C_EnableClockStretching(I2C2);
426
427 }
428
429 /* USB init function */
430 static void MX_USB_PCD_Init(void)
431 {
432   hpcd_USB_FS.Instance = USB;
433   hpcd_USB_FS.Init.dev_endpoints = 8;
434   hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
435   hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_64;
436   hpcd_USB_FS.Init.low_power_enable = DISABLE;
437   hpcd_USB_FS.Init.lpm_enable = DISABLE;
438   hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
439   if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK)
440   {
441     _Error_Handler(__FILE__, __LINE__);
442   }
443 }
444
445 /** Configure pins as 
446         * Analog 
447         * Input 
448         * Output
449         * EVENT_OUT
450         * EXTI
451 */
452 static void MX_GPIO_Init(void)
453 {
454
455   LL_GPIO_InitTypeDef GPIO_InitStruct;
456
457   /* GPIO Ports Clock Enable */
458   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC);
459   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOD);
460   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
461   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
462
463   /**/
464   LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
465
466   /**/
467   GPIO_InitStruct.Pin = LED_Pin;
468   GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
469   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
470   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
471   LL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
472
473   /**/
474   GPIO_InitStruct.Pin = BMP_DONE_Pin;
475   GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING;
476   LL_GPIO_Init(BMP_DONE_GPIO_Port, &GPIO_InitStruct);
477
478 }
479
480 /* USER CODE BEGIN 4 */
481
482 /* USER CODE END 4 */
483
484 /**
485   * @brief  This function is executed in case of error occurrence.
486   * @param  file: The file name as string.
487   * @param  line: The line in file as a number.
488   * @retval None
489   */
490 void _Error_Handler(char *file, int line)
491 {
492   /* USER CODE BEGIN Error_Handler_Debug */
493   /* User can add his own implementation to report the HAL error return state */
494   while(1)
495   {
496   }
497   /* USER CODE END Error_Handler_Debug */
498 }
499
500 #ifdef  USE_FULL_ASSERT
501 /**
502   * @brief  Reports the name of the source file and the source line number
503   *         where the assert_param error has occurred.
504   * @param  file: pointer to the source file name
505   * @param  line: assert_param error line source number
506   * @retval None
507   */
508 void assert_failed(uint8_t* file, uint32_t line)
509
510   /* USER CODE BEGIN 6 */
511   /* User can add his own implementation to report the file name and line number,
512      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
513   /* USER CODE END 6 */
514 }
515 #endif /* USE_FULL_ASSERT */
516
517 /**
518   * @}
519   */
520
521 /**
522   * @}
523   */
524
525 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/