Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

130 lines
2.8 KiB

  1. #include <stm32l1xx_hal.h>
  2. #include <usbd_cdc_if.h>
  3. #include <usb_device.h>
  4. #include <board.h>
  5. static void gpio_init(void);
  6. static void clock_config(void);
  7. void
  8. board_init(void)
  9. {
  10. HAL_Init();
  11. clock_config();
  12. gpio_init();
  13. MX_USB_DEVICE_Init();
  14. }
  15. /*
  16. * Wait for a device to connect to the VCP
  17. */
  18. void
  19. wait_for_vcp(void)
  20. {
  21. for (;;) {
  22. if (vcp_status(&hUsbDeviceFS.request))
  23. break;
  24. }
  25. }
  26. void
  27. Error_Handler(void)
  28. {
  29. /* XXX - handle error */
  30. }
  31. static void
  32. clock_config(void)
  33. {
  34. RCC_OscInitTypeDef RCC_OscInitStruct;
  35. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  36. RCC_PeriphCLKInitTypeDef PeriphClkInit;
  37. /* Configure internal regulator output voltage */
  38. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  39. /* Initializes CPU, AHB and APB busses clocks */
  40. RCC_OscInitStruct = (RCC_OscInitTypeDef){
  41. .OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE,
  42. .HSEState = RCC_HSE_ON,
  43. .LSIState = RCC_LSI_ON,
  44. .PLL.PLLState = RCC_PLL_ON,
  45. .PLL.PLLSource = RCC_PLLSOURCE_HSE,
  46. .PLL.PLLMUL = RCC_PLL_MUL12,
  47. .PLL.PLLDIV = RCC_PLL_DIV3,
  48. };
  49. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  50. Error_Handler();
  51. }
  52. /* Initializes CPU, AHB and APB busses clocks */
  53. RCC_ClkInitStruct = (RCC_ClkInitTypeDef){
  54. .ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  55. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2,
  56. .SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK,
  57. .AHBCLKDivider = RCC_SYSCLK_DIV1,
  58. .APB1CLKDivider = RCC_HCLK_DIV1,
  59. .APB2CLKDivider = RCC_HCLK_DIV1,
  60. };
  61. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
  62. Error_Handler();
  63. }
  64. PeriphClkInit = (RCC_PeriphCLKInitTypeDef){
  65. .PeriphClockSelection = RCC_PERIPHCLK_RTC,
  66. .RTCClockSelection = RCC_RTCCLKSOURCE_LSI,
  67. };
  68. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
  69. Error_Handler();
  70. }
  71. }
  72. static void
  73. gpio_init(void)
  74. {
  75. GPIO_InitTypeDef GPIO_InitStruct;
  76. __HAL_RCC_GPIOC_CLK_ENABLE();
  77. __HAL_RCC_GPIOH_CLK_ENABLE();
  78. __HAL_RCC_GPIOA_CLK_ENABLE();
  79. __HAL_RCC_GPIOB_CLK_ENABLE();
  80. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3|GPIO_PIN_4, GPIO_PIN_RESET);
  81. /* configure PA3 and PA4 */
  82. GPIO_InitStruct = (GPIO_InitTypeDef){
  83. .Pin = GPIO_PIN_3|GPIO_PIN_4,
  84. .Mode = GPIO_MODE_OUTPUT_PP,
  85. .Pull = GPIO_NOPULL,
  86. .Speed = GPIO_SPEED_FREQ_LOW,
  87. };
  88. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  89. /* configure PB0, PB1, PB10 and PB11 */
  90. GPIO_InitStruct = (GPIO_InitTypeDef){
  91. .Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_10|GPIO_PIN_11,
  92. .Mode = GPIO_MODE_INPUT,
  93. .Pull = GPIO_NOPULL,
  94. .Speed = GPIO_SPEED_FREQ_LOW,
  95. };
  96. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  97. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
  98. /* configure PB8 */
  99. GPIO_InitStruct = (GPIO_InitTypeDef){
  100. .Pin = GPIO_PIN_8,
  101. .Mode = GPIO_MODE_OUTPUT_PP,
  102. .Pull = GPIO_NOPULL,
  103. .Speed = GPIO_SPEED_FREQ_LOW,
  104. };
  105. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  106. }