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