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