Subversion Repositories DashDisplay

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ******************************************************************************
  6.   *
  7.   * COPYRIGHT(c) 2015 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. #include "usb_device.h"
  36. #include "serial.h"
  37. #include "SSD1306.h"
  38. #include <math.h>
  39.  
  40. /* USER CODE BEGIN Includes */
  41.  
  42. /* USER CODE END Includes */
  43.  
  44. /* Private variables ---------------------------------------------------------*/
  45. ADC_HandleTypeDef hadc1;
  46.  
  47. SPI_HandleTypeDef hspi1;
  48.  
  49. UART_HandleTypeDef huart2;
  50. IRDA_HandleTypeDef hirda3;
  51.  
  52. /* USER CODE BEGIN PV */
  53. /* Private variables ---------------------------------------------------------*/
  54.  
  55. /* USER CODE END PV */
  56.  
  57. /* Private function prototypes -----------------------------------------------*/
  58. void SystemClock_Config(void);
  59. static void MX_GPIO_Init(void);
  60. static void MX_ADC1_Init(void);
  61. static void MX_SPI1_Init(void);
  62. static void MX_USART2_UART_Init(void);
  63. static void MX_USART3_IRDA_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. /* dummy function */
  72. void _init(void)
  73. {
  74.  
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81. /* USER CODE END 0 */
  82.  
  83. int main(void)
  84. {
  85.  
  86.  
  87.   /* USER CODE BEGIN 1 */
  88.  
  89.         GPIO_InitTypeDef GPIO_InitStruct;
  90.  
  91.         __HAL_RCC_SPI1_CLK_ENABLE()
  92.         ;
  93.         __HAL_RCC_USART2_CLK_ENABLE()
  94.         ;
  95.         __HAL_RCC_USART3_CLK_ENABLE();
  96.         /* USER CODE END 1 */
  97.  
  98.         /* MCU Configuration----------------------------------------------------------*/
  99.  
  100.         /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  101.         HAL_Init();
  102.  
  103.         /* Configure the system clock */
  104.         SystemClock_Config();
  105.  
  106.         /* Initialize all configured peripherals */
  107.         MX_GPIO_Init();
  108.         MX_ADC1_Init();
  109.         MX_SPI1_Init();
  110.         MX_USART2_UART_Init();
  111.         MX_USART3_IRDA_Init();
  112.         MX_USB_DEVICE_Init();
  113.  
  114.         /* USER CODE BEGIN 2 */
  115.         /* Need to set AF mode for output pins DURR. */
  116.         /* SPI bus AF pin selects */
  117.         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  118.  
  119.         GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_7;
  120.         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  121.         HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  122.  
  123.         /* USART2 AF pin selects */
  124.         GPIO_InitStruct.Pin = GPIO_PIN_2;
  125.         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  126.         HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  127.  
  128.         /* USART3 AF pin selects */
  129.         GPIO_InitStruct.Pin = GPIO_PIN_10;
  130.         HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  131.  
  132.         /* Turn on USART2 IRQ  */
  133.         HAL_NVIC_SetPriority(USART2_IRQn, 4, 0);
  134.         HAL_NVIC_EnableIRQ(USART2_IRQn);
  135.  
  136.         /* Turn on USART3 IRQ */
  137.         HAL_NVIC_SetPriority(USART3_IRQn, 2, 0);
  138.         HAL_NVIC_EnableIRQ(USART3_IRQn);
  139.  
  140.         /* Setup for IRDA so make USART3 transmission for IRDA inverted - pulses are '0'  */
  141.         HAL_GPIO_WritePin(USART3_INVERT_GPIO_Port, USART3_INVERT_Pin, GPIO_PIN_SET);
  142.  
  143.         /* setup the USART control blocks */
  144.         init_usart_ctl(&uc2, huart2.Instance);
  145.         init_usart_ctl(&uc3, hirda3.Instance);
  146.  
  147.         EnableSerialRxInterrupt(&uc2);
  148.         EnableSerialRxInterrupt(&uc3);
  149.  
  150.         SSD1306_begin(1, 0);
  151.         clearDisplay();
  152.         font_gotoxy(0, 0);
  153.         dim(1);
  154.         font_puts(
  155.                         "Hello world !!\rThis text is a test of the text rendering library in a 5*7 font");
  156.  
  157.         display();
  158.  
  159.         /* USER CODE END 2 */
  160.  
  161.         /* Infinite loop */
  162.         /* USER CODE BEGIN WHILE */
  163.         int cnt = 0;
  164.         /* max characters to forward to USB */
  165.  
  166.         while (1) {
  167.  
  168.                 /* bit of IRDA stuff */
  169.                 uint16_t chars = CDC_Poll_UART();
  170.  
  171.                 /* end of USB stuff */
  172.  
  173.                 print_digits(32, 48, 6, chars);
  174.  
  175.                 print_digits(32,0,  7, hirda3.Init.BaudRate);
  176.  
  177.                 display();
  178. #if 0
  179.                 int ang = cnt % 360;
  180.                 int si = 32 * sin(ang / 180.0 * 3.14159);
  181.                 int co = 32 * cos(ang / 180.0 * 3.14159);
  182.  
  183.                 uint8_t c = (cnt / 360) & 1;
  184.                 drawLine(32, 32, 32 + co, 32 + si, c);
  185. #endif
  186.                 cnt++;
  187.  
  188.                 /* USER CODE END WHILE */
  189.  
  190.                 /* USER CODE BEGIN 3 */
  191.  
  192.         }
  193.         /* USER CODE END 3 */
  194.  
  195. }
  196.  
  197. /** System Clock Configuration
  198. */
  199. void SystemClock_Config(void)
  200. {
  201.  
  202.   RCC_OscInitTypeDef RCC_OscInitStruct;
  203.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  204.   RCC_PeriphCLKInitTypeDef PeriphClkInit;
  205.  
  206.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  207.   RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  208.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  209.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  210.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  211.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  212.   HAL_RCC_OscConfig(&RCC_OscInitStruct);
  213.  
  214.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
  215.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  216.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  217.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  218.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  219.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
  220.  
  221.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC|RCC_PERIPHCLK_USB;
  222.   PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  223.   PeriphClkInit.UsbClockSelection = RCC_USBPLLCLK_DIV1_5;
  224.   HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
  225.  
  226.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  227.  
  228.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  229.  
  230.   /* SysTick_IRQn interrupt configuration */
  231.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  232. }
  233.  
  234. /* ADC1 init function */
  235. void MX_ADC1_Init(void)
  236. {
  237.  
  238.   ADC_ChannelConfTypeDef sConfig;
  239.  
  240.     /**Common config
  241.     */
  242.   hadc1.Instance = ADC1;
  243.   hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  244.   hadc1.Init.ContinuousConvMode = DISABLE;
  245.   hadc1.Init.DiscontinuousConvMode = DISABLE;
  246.   hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  247.   hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  248.   hadc1.Init.NbrOfConversion = 1;
  249.   HAL_ADC_Init(&hadc1);
  250.  
  251.     /**Configure Regular Channel
  252.     */
  253.   sConfig.Channel = ADC_CHANNEL_0;
  254.   sConfig.Rank = 1;
  255.   sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  256.   HAL_ADC_ConfigChannel(&hadc1, &sConfig);
  257.  
  258. }
  259.  
  260. /* SPI1 init function */
  261. void MX_SPI1_Init(void)
  262. {
  263.  
  264.   hspi1.Instance = SPI1;
  265.   hspi1.Init.Mode = SPI_MODE_MASTER;
  266.   hspi1.Init.Direction = SPI_DIRECTION_1LINE;
  267.   hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  268.   hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
  269.   hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  270.   hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT;
  271.   hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  272.   hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  273.   hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
  274.   hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
  275.   hspi1.Init.CRCPolynomial = 10;
  276.   HAL_SPI_Init(&hspi1);
  277.  
  278. }
  279.  
  280. /* USART2 init function */
  281. void MX_USART2_UART_Init(void)
  282. {
  283.  
  284.   huart2.Instance = USART2;
  285.   huart2.Init.BaudRate = 115200;
  286.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  287.   huart2.Init.StopBits = UART_STOPBITS_1;
  288.   huart2.Init.Parity = UART_PARITY_NONE;
  289.   huart2.Init.Mode = UART_MODE_TX_RX;
  290.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  291.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  292.   HAL_UART_Init(&huart2);
  293.  
  294. }
  295.  
  296. /* USART3 init function */
  297. void MX_USART3_IRDA_Init(void)
  298. {
  299.  
  300.   hirda3.Instance = USART3;
  301.   hirda3.Init.BaudRate = 57600; // match apparent baud rate seen
  302.   hirda3.Init.WordLength = IRDA_WORDLENGTH_8B;
  303.   hirda3.Init.Parity = IRDA_PARITY_NONE;
  304.   hirda3.Init.Mode = IRDA_MODE_TX_RX;
  305.   hirda3.Init.Prescaler = 1;
  306.   hirda3.Init.IrDAMode = IRDA_POWERMODE_NORMAL;
  307.   HAL_IRDA_Init(&hirda3);
  308.  
  309. }
  310.  
  311. /** Configure pins as
  312.         * Analog
  313.         * Input
  314.         * Output
  315.         * EVENT_OUT
  316.         * EXTI
  317. */
  318. void MX_GPIO_Init(void)
  319. {
  320.  
  321.   GPIO_InitTypeDef GPIO_InitStruct;
  322.  
  323.   /* GPIO Ports Clock Enable */
  324.   __GPIOD_CLK_ENABLE();
  325.   __GPIOA_CLK_ENABLE();
  326.   __GPIOC_CLK_ENABLE();
  327.   __GPIOB_CLK_ENABLE();
  328.  
  329.   /*Configure GPIO pin : SPI1CD_Pin */
  330.   GPIO_InitStruct.Pin = SPI1CD_Pin;
  331.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  332.   GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  333.   HAL_GPIO_Init(SPI1CD_GPIO_Port, &GPIO_InitStruct);
  334.  
  335.   /*Configure GPIO pins : SPI_RESET_Pin USART3_INVERT_Pin USB_PWR_Pin */
  336.   GPIO_InitStruct.Pin = SPI_RESET_Pin|USART3_INVERT_Pin|USB_PWR_Pin;
  337.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  338.   GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  339.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  340.  
  341. }
  342.  
  343. /* USER CODE BEGIN 4 */
  344.  
  345. /* USER CODE END 4 */
  346.  
  347. #ifdef USE_FULL_ASSERT
  348.  
  349. /**
  350.    * @brief Reports the name of the source file and the source line number
  351.    * where the assert_param error has occurred.
  352.    * @param file: pointer to the source file name
  353.    * @param line: assert_param error line source number
  354.    * @retval None
  355.    */
  356. void assert_failed(uint8_t* file, uint32_t line)
  357. {
  358.   /* USER CODE BEGIN 6 */
  359.   /* User can add his own implementation to report the file name and line number,
  360.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  361.   /* USER CODE END 6 */
  362.  
  363. }
  364.  
  365. #endif
  366.  
  367. /**
  368.   * @}
  369.   */
  370.  
  371. /**
  372.   * @}
  373. */
  374.  
  375. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  376.