Subversion Repositories DashDisplay

Rev

Rev 3 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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