Subversion Repositories DashDisplay

Rev

Rev 22 | Rev 24 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ******************************************************************************
  6.   *
  7.   * COPYRIGHT(c) 2017 STMicroelectronics
  8.   *
  9.   * Redistribution and use in source and binary forms, with or without modification,
  10.   * are permitted provided that the following conditions are met:
  11.   *   1. Redistributions of source code must retain the above copyright notice,
  12.   *      this list of conditions and the following disclaimer.
  13.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  14.   *      this list of conditions and the following disclaimer in the documentation
  15.   *      and/or other materials provided with the distribution.
  16.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  17.   *      may be used to endorse or promote products derived from this software
  18.   *      without specific prior written permission.
  19.   *
  20.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.   *
  31.   ******************************************************************************
  32.   */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f1xx_hal.h"
  35.  
  36. /* USER CODE BEGIN Includes */
  37. #include "ap_math.h"
  38. #include "serial.h"
  39. #include "SSD1306.h"
  40. #include "Font.h"
  41. #include "dials.h"
  42. #include "switches.h"
  43. #include <math.h>
  44. #include "plx.h"
  45. #include "eeprom.h"
  46. #include "displayinfo.h"
  47.  
  48. /* USER CODE END Includes */
  49.  
  50. /* Private variables ---------------------------------------------------------*/
  51. SPI_HandleTypeDef hspi1;
  52.  
  53. UART_HandleTypeDef huart1;
  54. UART_HandleTypeDef huart2;
  55. UART_HandleTypeDef huart3;
  56.  
  57. /* USER CODE BEGIN PV */
  58. /* Private variables ---------------------------------------------------------*/
  59. #define MAXRDG 32
  60.  
  61. int OldObservation[2] =
  62. { -1, -1 }; // illegal initial value
  63. int OldObservationIndex[2] =
  64. { -1, -1 }; // if more than one sensor this will be printed
  65. int16_t dial0[2] =
  66. { 0, 0 };
  67. int16_t dial1[2] =
  68. { -1, -1 };
  69.  
  70. uint16_t dial_timer[2] =
  71. { 0, 0 };
  72.  
  73. static const int DialTimeout = 50; // about 20 seconds after twiddle, save the dial position.
  74.  
  75. /* Virtual address defined by the user: 0xFFFF value is prohibited */
  76. uint16_t VirtAddVarTab[NumbOfVar] = {0x1111,0x2222 }  ;
  77.  
  78. union
  79. {
  80.         PLX_SensorInfo Sensor[MAXRDG];
  81.         char Bytes[MAXRDG * sizeof(PLX_SensorInfo)];
  82. } Data;
  83. int Max[MAXRDG];
  84. int Min[MAXRDG];
  85. int PLXItems;
  86. /* USER CODE END PV */
  87.  
  88. /* Private function prototypes -----------------------------------------------*/
  89. void SystemClock_Config(void);
  90. void Error_Handler(void);
  91. static void MX_GPIO_Init(void);
  92. static void MX_SPI1_Init(void);
  93. static void MX_USART2_UART_Init(void);
  94. static void MX_USART1_UART_Init(void);
  95. static void MX_USART3_UART_Init(void);
  96.  
  97. /* USER CODE BEGIN PFP */
  98. /* Private function prototypes -----------------------------------------------*/
  99.  
  100. /* USER CODE END PFP */
  101.  
  102. /* USER CODE BEGIN 0 */
  103. /* dummy function */
  104. void _init(void)
  105. {
  106.  
  107. }
  108. // the dial is the switch number we are using.
  109. // suppress is the ItemIndex we wish to suppress on this display
  110. int  DisplayCurrent(int dial,int suppress)
  111. {
  112.         char buff[10];
  113.         int i;
  114.         int rc;
  115.         select_display(dial); // pick the display we are using
  116.         int ItemIndex = dial_pos[dial]/4;
  117.  
  118.         // wrap around count if dial too far to the right
  119.         if (ItemIndex >= PLXItems)
  120.         {
  121.                 dial_pos[dial] = 0;
  122.                 ItemIndex = 0;
  123.         }
  124.         if (ItemIndex < 0)
  125.         {
  126.                 ItemIndex = PLXItems-1;
  127.                 dial_pos[dial] = (PLXItems-1)*4;
  128.         }
  129.  
  130.  
  131.  
  132.         // check for item suppression
  133.         if(ItemIndex == suppress)
  134.         {
  135.                 dial1[dial] = -1;
  136.                 OldObservation[dial] = -1;
  137.                 OldObservationIndex[dial] = -1;
  138.  
  139.                 clearDisplay();
  140.                 display();
  141.                 return -1; // we suppressed this display
  142.         }
  143.         // do not try to convert if no items in buffer
  144.         if (PLXItems > 0)
  145.         {
  146.                 int DataVal = ConvPLX(Data.Sensor[ItemIndex].ReadingH,
  147.                                 Data.Sensor[ItemIndex].ReadingL); // data reading
  148.                 int Observation = ConvPLX(Data.Sensor[ItemIndex].AddrH,
  149.                                 Data.Sensor[ItemIndex].AddrL);
  150.                 int ObservationIndex = ConvPLX(0, Data.Sensor[ItemIndex].Instance);
  151.                 // now to convert the readings and format strings
  152.                 // find out limits
  153.                 char * msg;
  154.                 int len;
  155.  
  156.                 // if the user presses the dial then reset min/max to current value
  157.                 if(push_pos[dial] == 1)
  158.                 {
  159.                                 Max[ItemIndex] = DataVal;
  160.                                 Min[ItemIndex] = DataVal; // 12 bit max value
  161.                 }
  162.  
  163.  
  164.  
  165.                 if (Observation < PLX_MAX_OBS)
  166.                 {
  167.                         if (Observation != OldObservation[dial]
  168.                                         || ObservationIndex != OldObservationIndex[dial])
  169.                         {
  170.  
  171.                 dial_timer[dial] = DialTimeout;
  172.  
  173.                                 dial1[dial] = -1;
  174.                                 clearDisplay();
  175.                                 dial_draw_scale(
  176.                                                 DisplayInfo[Observation].Low,
  177.                                                 DisplayInfo[Observation].High,
  178.                                             12, 1,DisplayInfo[Observation].TickScale);
  179.  
  180.                                 msg = DisplayInfo[Observation].name;
  181.                                 len = 7;
  182.                                 int len1  = ObservationIndex > 0 ? len-1: len;
  183.                                 for (i = 0; i < len1 && msg[i]; i++)
  184.                                 {
  185.                                         buff[i] = msg[i];
  186.                                 }
  187.                                 if (ObservationIndex > 0 && i<len)
  188.                                 {
  189.                                         buff[i++] = ObservationIndex + '1';
  190.                                 }
  191.  
  192.                                 print_large_string(buff, 64-i*4, 48, i); // this prints spaces for \0 at end of string
  193.  
  194.                                 OldObservation[dial] = Observation;
  195.                                 OldObservationIndex[dial] = ObservationIndex;
  196.                                 //
  197.                                 display();
  198.  
  199.                         }
  200.                         else
  201.                         {
  202.                                 // check for timer timeout on consistent timer
  203.                                 if(dial_timer[dial])
  204.                                 {
  205.                                         dial_timer[dial]--;
  206.  
  207.                                         if(dial_timer[dial]==0  )
  208.                                         {
  209.                                                 uint16_t curr_val = dial_pos[dial];
  210.                                                 rc = EE_ReadVariable(VirtAddVarTab[dial],&curr_val);
  211.                                                 if((rc !=0) || (curr_val != dial_pos[dial]))
  212.                                                 {
  213.                                                         //__disable_irq();
  214.                                                         HAL_FLASH_Unlock();
  215.  
  216.                                                         rc = EE_WriteVariable(VirtAddVarTab[dial],dial_pos[dial]);
  217.                                                         HAL_FLASH_Lock();
  218.                                                         //__enable_irq();
  219.                                                 }
  220.                                         }
  221.                                 }
  222.  
  223.                         }
  224.  
  225.                         double max_rdg;
  226.                         double min_rdg;
  227.                         double cur_rdg;
  228.                         int int_rdg;
  229.                         int int_max;
  230.                         int int_min;
  231.  
  232.                         max_rdg = ConveriMFDRaw2Data(Observation,
  233.                                         DisplayInfo[Observation].Units, Max[ItemIndex]);
  234.                         min_rdg = ConveriMFDRaw2Data(Observation,
  235.                                         DisplayInfo[Observation].Units, Min[ItemIndex]);
  236.                         cur_rdg = ConveriMFDRaw2Data(Observation,
  237.                                         DisplayInfo[Observation].Units, DataVal);
  238.  
  239.                         int dp_pos;  // where to print the decimal place
  240.                         float scale = 1.0;
  241.                         switch (DisplayInfo[Observation].DP)
  242.                         {
  243.                         case 0:
  244.                                 scale = 1.0;
  245.                                 dp_pos = 100;
  246.                                 break;
  247.                         case 1:
  248.                                 scale = 10.0;
  249.                                 dp_pos = 1;
  250.                                 break;
  251.                         case 2:
  252.                                 scale = 100.0;
  253.                                 dp_pos = 2;
  254.                                 break;
  255.                         }
  256.                         int_rdg = (int) (cur_rdg * scale);
  257.                         int_max = (int) (max_rdg * scale);
  258.                         int_min = (int) (min_rdg * scale);
  259.  
  260.                         cur_rdg -= DisplayInfo[Observation].Low;
  261.                         cur_rdg = 100 * cur_rdg
  262.                                         / (DisplayInfo[Observation].High
  263.                                                         - DisplayInfo[Observation].Low);
  264.  
  265.                         dial0[dial] = (int) cur_rdg  ;
  266.  
  267.                         /* old needle un-draw */
  268.                         if (dial1[dial] >= 0)
  269.                         {
  270.                                 dial_draw_needle(dial1[dial]);
  271.                         }
  272.                         dial_draw_needle(dial0[dial]);
  273.                         // print value overlaid by needle
  274.                         // this is actual reading
  275.                         print_digits(30, 30, 5, dp_pos, int_rdg);
  276.                         font_gotoxy(0,0);
  277.                         font_digits(5,dp_pos,int_min);
  278.  
  279.                         font_gotoxy(0,1);
  280.                         font_puts("Min");
  281.  
  282.                         font_gotoxy(15,0);
  283.                         font_digits(5,dp_pos,int_max);
  284.                         font_gotoxy(18,1);
  285.                         font_puts("Max");
  286.  
  287.                         dial1[dial] = dial0[dial];
  288.  
  289.                         display();
  290.  
  291.                 }
  292.         }
  293.         return ItemIndex;
  294. }
  295. /* USER CODE END 0 */
  296.  
  297. int main(void)
  298. {
  299.  
  300.   /* USER CODE BEGIN 1 */
  301.  
  302.         GPIO_InitTypeDef GPIO_InitStruct;
  303.  
  304.         __HAL_RCC_SPI1_CLK_ENABLE();
  305.         __HAL_RCC_USART1_CLK_ENABLE(); // PLX main port
  306.         __HAL_RCC_USART2_CLK_ENABLE(); // debug port
  307.         __HAL_RCC_USART3_CLK_ENABLE(); // Bluetooth port
  308.  
  309.   /* USER CODE END 1 */
  310.  
  311.   /* MCU Configuration----------------------------------------------------------*/
  312.  
  313.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  314.   HAL_Init();
  315.  
  316.   /* Configure the system clock */
  317.   SystemClock_Config();
  318.  
  319.   /* Initialize all configured peripherals */
  320.   MX_GPIO_Init();
  321.   MX_SPI1_Init();
  322.   MX_USART2_UART_Init();
  323.   MX_USART1_UART_Init();
  324.   MX_USART3_UART_Init();
  325.  
  326.   /* USER CODE BEGIN 2 */
  327.  
  328.  
  329.  
  330.         /* Turn on USART1 IRQ */
  331.         HAL_NVIC_SetPriority(USART1_IRQn, 2, 0);
  332.         HAL_NVIC_EnableIRQ(USART1_IRQn);
  333.  
  334.     /* Turn on USART2 IRQ  */
  335.         HAL_NVIC_SetPriority(USART2_IRQn, 4, 0);
  336.         HAL_NVIC_EnableIRQ(USART2_IRQn);
  337.  
  338.         /* turn on USART3 IRQ */
  339.         HAL_NVIC_SetPriority(USART3_IRQn, 4, 0);
  340.         HAL_NVIC_EnableIRQ(USART3_IRQn);
  341.  
  342.  
  343.  
  344.         /* setup the USART control blocks */
  345.         init_usart_ctl(&uc1, huart1.Instance);
  346.         init_usart_ctl(&uc2, huart2.Instance);
  347.         init_usart_ctl(&uc3, huart3.Instance);
  348.  
  349.         EnableSerialRxInterrupt(&uc1);
  350.         EnableSerialRxInterrupt(&uc2);
  351.         EnableSerialRxInterrupt(&uc3);
  352.  
  353.         /* Unlock the Flash to enable the flash control register access *************/
  354.         HAL_FLASH_Unlock();
  355.  
  356.         //__disable_irq();
  357.         EE_Init();
  358.         //__enable_irq();
  359.  
  360.     HAL_FLASH_Lock();
  361.  
  362.  
  363.         InitSwitches();
  364.  
  365.         int i;
  366.         uint16_t rc;
  367.         for(i=0;i<2;i++)
  368.         {
  369.           uint16_t val;
  370.  
  371.           uint16_t rc =  EE_ReadVariable(VirtAddVarTab[i], &val);
  372.  
  373.           if (rc == 0)
  374.           {
  375.                   dial_pos[i] = val;
  376.           }
  377.           else
  378.           {
  379.                   break;
  380.           }
  381.         }
  382.  
  383.  
  384.  
  385.         ap_init(); // set up the approximate math library
  386.  
  387.         int disp;
  388.  
  389.         ssd1306_begin(1, 0);
  390.         dial_origin(64, 60);
  391.         dial_size(60);
  392.  
  393.  
  394.         // sort out the switch positions
  395.  
  396.  
  397.  
  398.  
  399.         for (disp = 0; disp < 2; disp++)
  400.         {
  401.                 select_display(disp);
  402.                 clearDisplay();
  403.                 dim(0);
  404.                 //font_puts(
  405.                 //              "Hello world !!\rThis text is a test of the text rendering library in a 5*7 font");
  406.  
  407.                 dial_draw_scale(0, 10, 12, 5,1);
  408.                 char  buffer[] = "Display  ";
  409.                 buffer[8] = disp+'1';
  410.                 print_large_string(buffer, 20,30, 9);
  411.  
  412.                 display();
  413.  
  414.         }
  415.  
  416.  
  417.  
  418.   /* USER CODE END 2 */
  419.  
  420.   /* Infinite loop */
  421.   /* USER CODE BEGIN WHILE */
  422.         uint32_t Ticks = HAL_GetTick() + 100;
  423.  
  424.     uint32_t timeout = 0;  //
  425.         // PLX decoder protocol
  426.         char PLXPacket = 0;
  427.         for (i = 0; i < MAXRDG; i++)
  428.         {
  429.                 Max[i] = 0;
  430.                 Min[i] = 0xFFF; // 12 bit max value
  431.         }
  432.  
  433.         int PLXPtr = 0;
  434.  
  435.         while (1)
  436.         {
  437. // poll switches
  438.                 HandleSwitches();
  439. // Handle the bluetooth pairing function by pressing both buttons.
  440.                 if((push_pos[0] == 1) && (push_pos[1] == 1))
  441.                 {
  442.                   HAL_GPIO_WritePin(BT_BUTTON_GPIO_Port, BT_BUTTON_Pin, GPIO_PIN_SET);
  443.                 }
  444.                 else
  445.                 {
  446.                   HAL_GPIO_WritePin(BT_BUTTON_GPIO_Port, BT_BUTTON_Pin, GPIO_PIN_RESET);
  447.  
  448.                 }
  449.  
  450.  
  451.                 uint16_t cc = SerialCharsReceived(&uc1);
  452.                 int chr;
  453.                 if(cc==0)
  454.                 {
  455.                         timeout++;
  456.                         if (timeout % 1000 ==0)
  457.                         {
  458.                                 PutCharSerial(&uc3,'+');
  459.                         }
  460.                         if(timeout > 60000)
  461.                         {
  462.                                 // do turn off screen
  463.                         }
  464.  
  465.                 }
  466.                 for (chr = 0; chr < cc; chr++)
  467.                 {
  468.                         char c = GetCharSerial(&uc1);
  469.                         timeout = 0;
  470.                         PutCharSerial(&uc3,c);
  471.  
  472.                         if (c == PLX_Start) // at any time if the start byte appears, reset the pointers
  473.                         {
  474.                                 PLXPtr = 0;    // reset the pointer
  475.                                 PLXPacket = 1;
  476.                         }
  477.                         else if (c == PLX_Stop)
  478.                         {
  479.                                 if (PLXPacket)
  480.                                 {
  481.                                         // we can now decode the selected parameter
  482.                                         PLXItems = PLXPtr / sizeof(PLX_SensorInfo); // total
  483.                                         // saturate the rotary switch position
  484.  
  485.                                         int DataVal;
  486.                                         // process min/max
  487.                                         for (i = 0; i < PLXItems; i++)
  488.                                         {
  489.                                                 DataVal = ConvPLX(Data.Sensor[i].ReadingH,
  490.                                                                 Data.Sensor[i].ReadingL);
  491.                                                 if (DataVal > Max[i])
  492.                                                 {
  493.                                                         Max[i] = DataVal;
  494.                                                 }
  495.                                                 if (DataVal < Min[i])
  496.                                                 {
  497.                                                         Min[i] = DataVal;
  498.                                                 }
  499.                                         }
  500.  
  501.                                         // now to display the information
  502.                                     int suppress = DisplayCurrent(0,-1);
  503.                                         DisplayCurrent(1, suppress);
  504.                                 }
  505.                                 PLXPtr = 0;
  506.                                 PLXPacket = 0;
  507.                         }
  508.                         else if (c > PLX_Stop) // illegal char, restart reading
  509.                         {
  510.                                 PLXPacket = 0;
  511.                                 PLXPtr = 0;
  512.                         }
  513.                         else if (PLXPtr < sizeof(Data.Bytes))
  514.                         {
  515.                                 Data.Bytes[PLXPtr++] = c;
  516.                         }
  517.                 }
  518.  
  519.                 HAL_Delay(1);
  520.         }
  521.   /* USER CODE END WHILE */
  522.  
  523.   /* USER CODE BEGIN 3 */
  524.  
  525.  
  526.   /* USER CODE END 3 */
  527.  
  528. }
  529.  
  530. /** System Clock Configuration
  531. */
  532. void SystemClock_Config(void)
  533. {
  534.  
  535.   RCC_OscInitTypeDef RCC_OscInitStruct;
  536.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  537.  
  538.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  539.   RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  540.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  541.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  542.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  543.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  544.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  545.   {
  546.     Error_Handler();
  547.   }
  548.  
  549.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  550.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  551.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  552.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  553.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  554.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  555.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  556.   {
  557.     Error_Handler();
  558.   }
  559.  
  560.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  561.  
  562.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  563.  
  564.   /* SysTick_IRQn interrupt configuration */
  565.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  566. }
  567.  
  568. /* SPI1 init function */
  569. static void MX_SPI1_Init(void)
  570. {
  571.  
  572.   hspi1.Instance = SPI1;
  573.   hspi1.Init.Mode = SPI_MODE_MASTER;
  574.   hspi1.Init.Direction = SPI_DIRECTION_1LINE;
  575.   hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  576.   hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
  577.   hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  578.   hspi1.Init.NSS = SPI_NSS_SOFT;
  579.   hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  580.   hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  581.   hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  582.   hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  583.   hspi1.Init.CRCPolynomial = 10;
  584.   if (HAL_SPI_Init(&hspi1) != HAL_OK)
  585.   {
  586.     Error_Handler();
  587.   }
  588.  
  589. }
  590.  
  591. /* USART1 init function */
  592. static void MX_USART1_UART_Init(void)
  593. {
  594.  
  595.   huart1.Instance = USART1;
  596.   huart1.Init.BaudRate = 19200;
  597.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  598.   huart1.Init.StopBits = UART_STOPBITS_1;
  599.   huart1.Init.Parity = UART_PARITY_NONE;
  600.   huart1.Init.Mode = UART_MODE_TX_RX;
  601.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  602.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  603.   if (HAL_UART_Init(&huart1) != HAL_OK)
  604.   {
  605.     Error_Handler();
  606.   }
  607.  
  608. }
  609.  
  610. /* USART2 init function */
  611. static void MX_USART2_UART_Init(void)
  612. {
  613.  
  614.   huart2.Instance = USART2;
  615.   huart2.Init.BaudRate = 115200;
  616.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  617.   huart2.Init.StopBits = UART_STOPBITS_1;
  618.   huart2.Init.Parity = UART_PARITY_NONE;
  619.   huart2.Init.Mode = UART_MODE_TX_RX;
  620.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  621.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  622.   if (HAL_UART_Init(&huart2) != HAL_OK)
  623.   {
  624.     Error_Handler();
  625.   }
  626.  
  627. }
  628.  
  629. /* USART3 init function */
  630. static void MX_USART3_UART_Init(void)
  631. {
  632.  
  633.   huart3.Instance = USART3;
  634.   huart3.Init.BaudRate = 19200;
  635.   huart3.Init.WordLength = UART_WORDLENGTH_8B;
  636.   huart3.Init.StopBits = UART_STOPBITS_1;
  637.   huart3.Init.Parity = UART_PARITY_NONE;
  638.   huart3.Init.Mode = UART_MODE_TX_RX;
  639.   huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  640.   huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  641.   if (HAL_UART_Init(&huart3) != HAL_OK)
  642.   {
  643.     Error_Handler();
  644.   }
  645.  
  646. }
  647.  
  648. /** Configure pins as
  649.         * Analog
  650.         * Input
  651.         * Output
  652.         * EVENT_OUT
  653.         * EXTI
  654. */
  655. static void MX_GPIO_Init(void)
  656. {
  657.  
  658.   GPIO_InitTypeDef GPIO_InitStruct;
  659.  
  660.   /* GPIO Ports Clock Enable */
  661.   __HAL_RCC_GPIOD_CLK_ENABLE();
  662.   __HAL_RCC_GPIOA_CLK_ENABLE();
  663.   __HAL_RCC_GPIOC_CLK_ENABLE();
  664.   __HAL_RCC_GPIOB_CLK_ENABLE();
  665.  
  666.   /*Configure GPIO pin Output Level */
  667.   HAL_GPIO_WritePin(SPI_NSS1_GPIO_Port, SPI_NSS1_Pin, GPIO_PIN_SET);
  668.  
  669.   /*Configure GPIO pin Output Level */
  670.   HAL_GPIO_WritePin(GPIOA, SPI1CD_Pin|BT_BUTTON_Pin, GPIO_PIN_RESET);
  671.  
  672.   /*Configure GPIO pin Output Level */
  673.   HAL_GPIO_WritePin(GPIOC, SPI_RESET_Pin|USART3_INVERT_Pin|USB_PWR_Pin, GPIO_PIN_RESET);
  674.  
  675.   /*Configure GPIO pin Output Level */
  676.   HAL_GPIO_WritePin(SPI_NSS2_GPIO_Port, SPI_NSS2_Pin, GPIO_PIN_SET);
  677.  
  678.   /*Configure GPIO pins : SPI_NSS1_Pin SPI1CD_Pin BT_BUTTON_Pin */
  679.   GPIO_InitStruct.Pin = SPI_NSS1_Pin|SPI1CD_Pin|BT_BUTTON_Pin;
  680.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  681.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  682.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  683.  
  684.   /*Configure GPIO pins : SPI_RESET_Pin SPI_NSS2_Pin USART3_INVERT_Pin USB_PWR_Pin */
  685.   GPIO_InitStruct.Pin = SPI_RESET_Pin|SPI_NSS2_Pin|USART3_INVERT_Pin|USB_PWR_Pin;
  686.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  687.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  688.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  689.  
  690.   /*Configure GPIO pins : SW1_PUSH_Pin SW1_I_Pin SW1_Q_Pin SW2_PUSH_Pin */
  691.   GPIO_InitStruct.Pin = SW1_PUSH_Pin|SW1_I_Pin|SW1_Q_Pin|SW2_PUSH_Pin;
  692.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  693.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  694.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  695.  
  696.   /*Configure GPIO pins : SW2_I_Pin SW2_Q_Pin */
  697.   GPIO_InitStruct.Pin = SW2_I_Pin|SW2_Q_Pin;
  698.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  699.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  700.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  701.  
  702. }
  703.  
  704. /* USER CODE BEGIN 4 */
  705.  
  706. /* USER CODE END 4 */
  707.  
  708. /**
  709.   * @brief  This function is executed in case of error occurrence.
  710.   * @param  None
  711.   * @retval None
  712.   */
  713. void Error_Handler(void)
  714. {
  715.   /* USER CODE BEGIN Error_Handler */
  716. /* User can add his own implementation to report the HAL error return state */
  717. while (1)
  718. {
  719. }
  720.   /* USER CODE END Error_Handler */
  721. }
  722.  
  723. #ifdef USE_FULL_ASSERT
  724.  
  725. /**
  726.    * @brief Reports the name of the source file and the source line number
  727.    * where the assert_param error has occurred.
  728.    * @param file: pointer to the source file name
  729.    * @param line: assert_param error line source number
  730.    * @retval None
  731.    */
  732. void assert_failed(uint8_t* file, uint32_t line)
  733. {
  734.   /* USER CODE BEGIN 6 */
  735. /* User can add his own implementation to report the file name and line number,
  736.  ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  737.   /* USER CODE END 6 */
  738.  
  739. }
  740.  
  741. #endif
  742.  
  743. /**
  744.   * @}
  745.   */
  746.  
  747. /**
  748.   * @}
  749. */
  750.  
  751. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  752.