Subversion Repositories DashDisplay

Rev

Rev 2 | Rev 4 | 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) 2016 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 "dials.h"
  41. #include <math.h>
  42.  
  43.  
  44. /* USER CODE END Includes */
  45.  
  46. /* Private variables ---------------------------------------------------------*/
  47. ADC_HandleTypeDef hadc1;
  48.  
  49. SPI_HandleTypeDef hspi1;
  50.  
  51. UART_HandleTypeDef huart1;
  52. UART_HandleTypeDef huart2;
  53.  
  54. /* USER CODE BEGIN PV */
  55. /* Private variables ---------------------------------------------------------*/
  56.  
  57. /* USER CODE END PV */
  58.  
  59. /* Private function prototypes -----------------------------------------------*/
  60. void SystemClock_Config(void);
  61. static void MX_GPIO_Init(void);
  62. static void MX_ADC1_Init(void);
  63. static void MX_SPI1_Init(void);
  64. static void MX_USART2_UART_Init(void);
  65. static void MX_USART1_UART_Init(void);
  66.  
  67. /* USER CODE BEGIN PFP */
  68. /* Private function prototypes -----------------------------------------------*/
  69.  
  70. /* USER CODE END PFP */
  71.  
  72. /* USER CODE BEGIN 0 */
  73. /* dummy function */
  74. void _init(void) {
  75.  
  76. }
  77.  
  78. /* USER CODE END 0 */
  79.  
  80. int main(void)
  81. {
  82.  
  83.   /* USER CODE BEGIN 1 */
  84.  
  85.         GPIO_InitTypeDef GPIO_InitStruct;
  86.  
  87.         __HAL_RCC_SPI1_CLK_ENABLE()
  88.         ;
  89.         __HAL_RCC_USART1_CLK_ENABLE(); // PLX main port
  90.         __HAL_RCC_USART2_CLK_ENABLE(); // debug port
  91.   /* USER CODE END 1 */
  92.  
  93.   /* MCU Configuration----------------------------------------------------------*/
  94.  
  95.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  96.   HAL_Init();
  97.  
  98.   /* Configure the system clock */
  99.   SystemClock_Config();
  100.  
  101.   /* Initialize all configured peripherals */
  102.   MX_GPIO_Init();
  103.   MX_ADC1_Init();
  104.   MX_SPI1_Init();
  105.   MX_USART2_UART_Init();
  106.   MX_USART1_UART_Init();
  107.  
  108.   /* USER CODE BEGIN 2 */
  109.         /* Need to set AF mode for output pins DURR. */
  110.         /* SPI bus AF pin selects */
  111.         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  112.  
  113.         GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_7;
  114.         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  115.         HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  116.  
  117.         /* USART2 AF pin selects */
  118.         GPIO_InitStruct.Pin = GPIO_PIN_2;
  119.         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  120.         HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  121.  
  122.         /* USART1 AF pin selects */
  123.         GPIO_InitStruct.Pin = GPIO_PIN_9;
  124.         HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  125.  
  126.         /* Turn on USART2 IRQ  */
  127.         HAL_NVIC_SetPriority(USART2_IRQn, 4, 0);
  128.         HAL_NVIC_EnableIRQ(USART2_IRQn);
  129.  
  130.         /* Turn on USART1 IRQ */
  131.         HAL_NVIC_SetPriority(USART1_IRQn, 2, 0);
  132.         HAL_NVIC_EnableIRQ(USART1_IRQn);
  133.  
  134.  
  135.         /* setup the USART control blocks */
  136.         init_usart_ctl(&uc1, huart1.Instance);
  137.         init_usart_ctl(&uc2, hirda2.Instance);
  138.  
  139.         EnableSerialRxInterrupt(&uc1);
  140.         EnableSerialRxInterrupt(&uc2);
  141.  
  142.         ap_init(); // set up the approximate math library
  143.  
  144.         ssd1306_begin(1, 0);
  145.         clearDisplay();
  146.         dim(1);
  147.         //font_puts(
  148.         //              "Hello world !!\rThis text is a test of the text rendering library in a 5*7 font");
  149.  
  150.         static const xp = 128 - 42;
  151.         dial_origin(xp, 40);
  152.         dial_size(40);
  153.         dial_draw_scale(10, 20, 16 , 2);
  154.  
  155.         display();
  156.  
  157.   /* USER CODE END 2 */
  158.  
  159.   /* Infinite loop */
  160.   /* USER CODE BEGIN WHILE */
  161.         uint32_t Ticks = HAL_GetTick()+100;
  162.         int16_t dial0 = 0;
  163.         int16_t dial1 = -1;
  164.  
  165.         int c = 0;
  166.         int i;
  167.         char buff[10];
  168.  
  169.  
  170.         while (1) {
  171.  
  172.                 uint16_t cc = SerialCharsReceived(&uc3);
  173.                 HAL_Delay(1);
  174.                 CDC_Poll_UART(); /* keep the traffic flowing */
  175.  
  176.                 dial0 = (c * 2)% 100;
  177.                 uint32_t CurrTicks = HAL_GetTick();
  178.                 if (CurrTicks > Ticks) {
  179.                         Ticks = CurrTicks+100;
  180.                         /* old needle un-draw */
  181.                         if (dial1 >= 0) {
  182.                                 dial_draw_needle(dial1);
  183.                         }
  184.                         // print value overlaid by needle
  185.                         print_digits(xp-16, 48, 4, 3, c);
  186.  
  187.  
  188.  
  189.                         dial_draw_needle(dial0);
  190.                         dial1 = dial0;
  191.  
  192.  
  193.  
  194.                         c++;
  195.                         for(i=0; i< 5; i++)
  196.                         buff[i]=i+c;
  197.                         print_large_string(buff,0,0,5);
  198.                         //font_gotoxy(0, 2);
  199.                         //font_puts("baud\r\n");
  200.                         //char buff[10];
  201.                         //itoa(hirda3.Init.BaudRate, buff, 10);
  202.                         //char l = 6 - strlen(buff);
  203.                         /* pad with leading spaces */
  204.                         //while (l > 0) {
  205.                         //      font_putchar(' ');
  206.                         //      l--;
  207.                         //}
  208.  
  209.                         //font_puts(itoa(hirda3.Init.BaudRate, buff, 10));
  210.  
  211.                         display();
  212.                 }
  213.   /* USER CODE END WHILE */
  214.  
  215.   /* USER CODE BEGIN 3 */
  216.  
  217.         }
  218.   /* USER CODE END 3 */
  219.  
  220. }
  221.  
  222. /** System Clock Configuration
  223. */
  224. void SystemClock_Config(void)
  225. {
  226.  
  227.   RCC_OscInitTypeDef RCC_OscInitStruct;
  228.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  229.   RCC_PeriphCLKInitTypeDef PeriphClkInit;
  230.  
  231.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  232.   RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  233.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  234.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  235.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  236.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  237.   HAL_RCC_OscConfig(&RCC_OscInitStruct);
  238.  
  239.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  240.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  241.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  242.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  243.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  244.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  245.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
  246.  
  247.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  248.   PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  249.   HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
  250.  
  251.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  252.  
  253.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  254.  
  255.   /* SysTick_IRQn interrupt configuration */
  256.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  257. }
  258.  
  259. /* ADC1 init function */
  260. void MX_ADC1_Init(void)
  261. {
  262.  
  263.   ADC_ChannelConfTypeDef sConfig;
  264.  
  265.     /**Common config
  266.     */
  267.   hadc1.Instance = ADC1;
  268.   hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  269.   hadc1.Init.ContinuousConvMode = DISABLE;
  270.   hadc1.Init.DiscontinuousConvMode = DISABLE;
  271.   hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  272.   hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  273.   hadc1.Init.NbrOfConversion = 1;
  274.   HAL_ADC_Init(&hadc1);
  275.  
  276.     /**Configure Regular Channel
  277.     */
  278.   sConfig.Channel = ADC_CHANNEL_0;
  279.   sConfig.Rank = 1;
  280.   sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  281.   HAL_ADC_ConfigChannel(&hadc1, &sConfig);
  282.  
  283. }
  284.  
  285. /* SPI1 init function */
  286. void MX_SPI1_Init(void)
  287. {
  288.  
  289.   hspi1.Instance = SPI1;
  290.   hspi1.Init.Mode = SPI_MODE_MASTER;
  291.   hspi1.Init.Direction = SPI_DIRECTION_1LINE;
  292.   hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  293.   hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
  294.   hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  295.   hspi1.Init.NSS = SPI_NSS_SOFT;
  296.   hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  297.   hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  298.   hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  299.   hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  300.   hspi1.Init.CRCPolynomial = 10;
  301.   HAL_SPI_Init(&hspi1);
  302.  
  303. }
  304.  
  305. /* USART1 init function */
  306. void MX_USART1_UART_Init(void)
  307. {
  308.  
  309.   huart1.Instance = USART1;
  310.   huart1.Init.BaudRate = 115200;
  311.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  312.   huart1.Init.StopBits = UART_STOPBITS_1;
  313.   huart1.Init.Parity = UART_PARITY_NONE;
  314.   huart1.Init.Mode = UART_MODE_TX_RX;
  315.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  316.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  317.   HAL_UART_Init(&huart1);
  318.  
  319. }
  320.  
  321. /* USART2 init function */
  322. void MX_USART2_UART_Init(void)
  323. {
  324.  
  325.   huart2.Instance = USART2;
  326.   huart2.Init.BaudRate = 115200;
  327.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  328.   huart2.Init.StopBits = UART_STOPBITS_1;
  329.   huart2.Init.Parity = UART_PARITY_NONE;
  330.   huart2.Init.Mode = UART_MODE_TX_RX;
  331.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  332.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  333.   HAL_UART_Init(&huart2);
  334.  
  335. }
  336.  
  337. /** Configure pins as
  338.         * Analog
  339.         * Input
  340.         * Output
  341.         * EVENT_OUT
  342.         * EXTI
  343. */
  344. void MX_GPIO_Init(void)
  345. {
  346.  
  347.   GPIO_InitTypeDef GPIO_InitStruct;
  348.  
  349.   /* GPIO Ports Clock Enable */
  350.   __HAL_RCC_GPIOD_CLK_ENABLE();
  351.   __HAL_RCC_GPIOA_CLK_ENABLE();
  352.   __HAL_RCC_GPIOC_CLK_ENABLE();
  353.   __HAL_RCC_GPIOB_CLK_ENABLE();
  354.  
  355.   /*Configure GPIO pin Output Level */
  356.   HAL_GPIO_WritePin(GPIOA, SPI1_NSS1_Pin|SPI1CD_Pin, GPIO_PIN_RESET);
  357.  
  358.   /*Configure GPIO pin Output Level */
  359.   HAL_GPIO_WritePin(GPIOC, SPI_RESET_Pin|SPI_NSS2_Pin|USART3_INVERT_Pin|USB_PWR_Pin, GPIO_PIN_RESET);
  360.  
  361.   /*Configure GPIO pins : SPI1_NSS1_Pin SPI1CD_Pin */
  362.   GPIO_InitStruct.Pin = SPI1_NSS1_Pin|SPI1CD_Pin;
  363.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  364.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  365.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  366.  
  367.   /*Configure GPIO pins : SPI_RESET_Pin SPI_NSS2_Pin USART3_INVERT_Pin USB_PWR_Pin */
  368.   GPIO_InitStruct.Pin = SPI_RESET_Pin|SPI_NSS2_Pin|USART3_INVERT_Pin|USB_PWR_Pin;
  369.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  370.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  371.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  372.  
  373.   /*Configure GPIO pins : SW1_PUSH_Pin SW1_I_Pin SW1_Q_Pin SW2_PUSH_Pin */
  374.   GPIO_InitStruct.Pin = SW1_PUSH_Pin|SW1_I_Pin|SW1_Q_Pin|SW2_PUSH_Pin;
  375.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  376.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  377.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  378.  
  379.   /*Configure GPIO pins : SW2_I_Pin SW2_Q_Pin */
  380.   GPIO_InitStruct.Pin = SW2_I_Pin|SW2_Q_Pin;
  381.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  382.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  383.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  384.  
  385. }
  386.  
  387. /* USER CODE BEGIN 4 */
  388.  
  389. /* USER CODE END 4 */
  390.  
  391. #ifdef USE_FULL_ASSERT
  392.  
  393. /**
  394.    * @brief Reports the name of the source file and the source line number
  395.    * where the assert_param error has occurred.
  396.    * @param file: pointer to the source file name
  397.    * @param line: assert_param error line source number
  398.    * @retval None
  399.    */
  400. void assert_failed(uint8_t* file, uint32_t line)
  401. {
  402.   /* USER CODE BEGIN 6 */
  403.         /* User can add his own implementation to report the file name and line number,
  404.          ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  405.   /* USER CODE END 6 */
  406.  
  407. }
  408.  
  409. #endif
  410.  
  411. /**
  412.   * @}
  413.   */
  414.  
  415. /**
  416.   * @}
  417. */
  418.  
  419. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  420.