Rev 2 | Rev 4 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 2 | Rev 3 | ||
|---|---|---|---|
| Line 30... | Line 30... | ||
| 30 | * |
30 | * |
| 31 | ****************************************************************************** |
31 | ****************************************************************************** |
| 32 | */ |
32 | */ |
| 33 | /* Includes ------------------------------------------------------------------*/ |
33 | /* Includes ------------------------------------------------------------------*/ |
| 34 | #include "stm32f1xx_hal.h" |
34 | #include "stm32f1xx_hal.h" |
| 35 | #include "usb_device.h" |
- | |
| 36 | 35 | ||
| 37 | /* USER CODE BEGIN Includes */ |
36 | /* USER CODE BEGIN Includes */ |
| 38 | #include "ap_math.h" |
37 | #include "ap_math.h" |
| 39 | #include "serial.h" |
38 | #include "serial.h" |
| 40 | #include "SSD1306.h" |
39 | #include "SSD1306.h" |
| Line 47... | Line 46... | ||
| 47 | /* Private variables ---------------------------------------------------------*/ |
46 | /* Private variables ---------------------------------------------------------*/ |
| 48 | ADC_HandleTypeDef hadc1; |
47 | ADC_HandleTypeDef hadc1; |
| 49 | 48 | ||
| 50 | SPI_HandleTypeDef hspi1; |
49 | SPI_HandleTypeDef hspi1; |
| 51 | 50 | ||
| - | 51 | UART_HandleTypeDef huart1; |
|
| 52 | UART_HandleTypeDef huart2; |
52 | UART_HandleTypeDef huart2; |
| 53 | IRDA_HandleTypeDef hirda3; |
- | |
| 54 | 53 | ||
| 55 | /* USER CODE BEGIN PV */ |
54 | /* USER CODE BEGIN PV */ |
| 56 | /* Private variables ---------------------------------------------------------*/ |
55 | /* Private variables ---------------------------------------------------------*/ |
| 57 | 56 | ||
| 58 | /* USER CODE END PV */ |
57 | /* USER CODE END PV */ |
| Line 61... | Line 60... | ||
| 61 | void SystemClock_Config(void); |
60 | void SystemClock_Config(void); |
| 62 | static void MX_GPIO_Init(void); |
61 | static void MX_GPIO_Init(void); |
| 63 | static void MX_ADC1_Init(void); |
62 | static void MX_ADC1_Init(void); |
| 64 | static void MX_SPI1_Init(void); |
63 | static void MX_SPI1_Init(void); |
| 65 | static void MX_USART2_UART_Init(void); |
64 | static void MX_USART2_UART_Init(void); |
| 66 | static void MX_USART3_IRDA_Init(void); |
65 | static void MX_USART1_UART_Init(void); |
| 67 | 66 | ||
| 68 | /* USER CODE BEGIN PFP */ |
67 | /* USER CODE BEGIN PFP */ |
| 69 | /* Private function prototypes -----------------------------------------------*/ |
68 | /* Private function prototypes -----------------------------------------------*/ |
| 70 | 69 | ||
| 71 | /* USER CODE END PFP */ |
70 | /* USER CODE END PFP */ |
| Line 85... | Line 84... | ||
| 85 | 84 | ||
| 86 | GPIO_InitTypeDef GPIO_InitStruct; |
85 | GPIO_InitTypeDef GPIO_InitStruct; |
| 87 | 86 | ||
| 88 | __HAL_RCC_SPI1_CLK_ENABLE() |
87 | __HAL_RCC_SPI1_CLK_ENABLE() |
| 89 | ; |
88 | ; |
| 90 | __HAL_RCC_USART2_CLK_ENABLE() |
89 | __HAL_RCC_USART1_CLK_ENABLE(); // PLX main port |
| 91 | ; |
- | |
| 92 | __HAL_RCC_USART3_CLK_ENABLE(); |
90 | __HAL_RCC_USART2_CLK_ENABLE(); // debug port |
| 93 | /* USER CODE END 1 */ |
91 | /* USER CODE END 1 */ |
| 94 | 92 | ||
| 95 | /* MCU Configuration----------------------------------------------------------*/ |
93 | /* MCU Configuration----------------------------------------------------------*/ |
| 96 | 94 | ||
| 97 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
95 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
| Line 103... | Line 101... | ||
| 103 | /* Initialize all configured peripherals */ |
101 | /* Initialize all configured peripherals */ |
| 104 | MX_GPIO_Init(); |
102 | MX_GPIO_Init(); |
| 105 | MX_ADC1_Init(); |
103 | MX_ADC1_Init(); |
| 106 | MX_SPI1_Init(); |
104 | MX_SPI1_Init(); |
| 107 | MX_USART2_UART_Init(); |
105 | MX_USART2_UART_Init(); |
| 108 | MX_USART3_IRDA_Init(); |
106 | MX_USART1_UART_Init(); |
| 109 | MX_USB_DEVICE_Init(); |
- | |
| 110 | 107 | ||
| 111 | /* USER CODE BEGIN 2 */ |
108 | /* USER CODE BEGIN 2 */ |
| 112 | /* Need to set AF mode for output pins DURR. */ |
109 | /* Need to set AF mode for output pins DURR. */ |
| 113 | /* SPI bus AF pin selects */ |
110 | /* SPI bus AF pin selects */ |
| 114 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
111 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
| Line 120... | Line 117... | ||
| 120 | /* USART2 AF pin selects */ |
117 | /* USART2 AF pin selects */ |
| 121 | GPIO_InitStruct.Pin = GPIO_PIN_2; |
118 | GPIO_InitStruct.Pin = GPIO_PIN_2; |
| 122 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
119 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 123 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
120 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 124 | 121 | ||
| 125 | /* USART3 AF pin selects */ |
122 | /* USART1 AF pin selects */ |
| 126 | GPIO_InitStruct.Pin = GPIO_PIN_10; |
123 | GPIO_InitStruct.Pin = GPIO_PIN_9; |
| 127 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
124 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 128 | 125 | ||
| 129 | /* Turn on USART2 IRQ */ |
126 | /* Turn on USART2 IRQ */ |
| 130 | HAL_NVIC_SetPriority(USART2_IRQn, 4, 0); |
127 | HAL_NVIC_SetPriority(USART2_IRQn, 4, 0); |
| 131 | HAL_NVIC_EnableIRQ(USART2_IRQn); |
128 | HAL_NVIC_EnableIRQ(USART2_IRQn); |
| 132 | 129 | ||
| 133 | /* Turn on USART3 IRQ */ |
130 | /* Turn on USART1 IRQ */ |
| 134 | HAL_NVIC_SetPriority(USART3_IRQn, 2, 0); |
131 | HAL_NVIC_SetPriority(USART1_IRQn, 2, 0); |
| 135 | HAL_NVIC_EnableIRQ(USART3_IRQn); |
132 | HAL_NVIC_EnableIRQ(USART1_IRQn); |
| 136 | 133 | ||
| 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 | 134 | ||
| 140 | /* setup the USART control blocks */ |
135 | /* setup the USART control blocks */ |
| 141 | init_usart_ctl(&uc2, huart2.Instance); |
136 | init_usart_ctl(&uc1, huart1.Instance); |
| 142 | init_usart_ctl(&uc3, hirda3.Instance); |
137 | init_usart_ctl(&uc2, hirda2.Instance); |
| 143 | 138 | ||
| - | 139 | EnableSerialRxInterrupt(&uc1); |
|
| 144 | EnableSerialRxInterrupt(&uc2); |
140 | EnableSerialRxInterrupt(&uc2); |
| 145 | EnableSerialRxInterrupt(&uc3); |
- | |
| 146 | 141 | ||
| 147 | ap_init(); // set up the approximate math library |
142 | ap_init(); // set up the approximate math library |
| 148 | 143 | ||
| 149 | ssd1306_begin(1, 0); |
144 | ssd1306_begin(1, 0); |
| 150 | clearDisplay(); |
145 | clearDisplay(); |
| Line 247... | Line 242... | ||
| 247 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
242 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
| 248 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; |
243 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; |
| 249 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
244 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
| 250 | HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2); |
245 | HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2); |
| 251 | 246 | ||
| 252 | PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC|RCC_PERIPHCLK_USB; |
247 | PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC; |
| 253 | PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6; |
248 | PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6; |
| 254 | PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5; |
- | |
| 255 | HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); |
249 | HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); |
| 256 | 250 | ||
| 257 | HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); |
251 | HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); |
| 258 | 252 | ||
| 259 | HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); |
253 | HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); |
| Line 306... | Line 300... | ||
| 306 | hspi1.Init.CRCPolynomial = 10; |
300 | hspi1.Init.CRCPolynomial = 10; |
| 307 | HAL_SPI_Init(&hspi1); |
301 | HAL_SPI_Init(&hspi1); |
| 308 | 302 | ||
| 309 | } |
303 | } |
| 310 | 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 | ||
| 311 | /* USART2 init function */ |
321 | /* USART2 init function */ |
| 312 | void MX_USART2_UART_Init(void) |
322 | void MX_USART2_UART_Init(void) |
| 313 | { |
323 | { |
| 314 | 324 | ||
| 315 | huart2.Instance = USART2; |
325 | huart2.Instance = USART2; |
| Line 322... | Line 332... | ||
| 322 | huart2.Init.OverSampling = UART_OVERSAMPLING_16; |
332 | huart2.Init.OverSampling = UART_OVERSAMPLING_16; |
| 323 | HAL_UART_Init(&huart2); |
333 | HAL_UART_Init(&huart2); |
| 324 | 334 | ||
| 325 | } |
335 | } |
| 326 | 336 | ||
| 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 |
337 | /** Configure pins as |
| 343 | * Analog |
338 | * Analog |
| 344 | * Input |
339 | * Input |
| 345 | * Output |
340 | * Output |
| 346 | * EVENT_OUT |
341 | * EVENT_OUT |
| Line 364... | Line 359... | ||
| 364 | HAL_GPIO_WritePin(GPIOC, SPI_RESET_Pin|SPI_NSS2_Pin|USART3_INVERT_Pin|USB_PWR_Pin, GPIO_PIN_RESET); |
359 | HAL_GPIO_WritePin(GPIOC, SPI_RESET_Pin|SPI_NSS2_Pin|USART3_INVERT_Pin|USB_PWR_Pin, GPIO_PIN_RESET); |
| 365 | 360 | ||
| 366 | /*Configure GPIO pins : SPI1_NSS1_Pin SPI1CD_Pin */ |
361 | /*Configure GPIO pins : SPI1_NSS1_Pin SPI1CD_Pin */ |
| 367 | GPIO_InitStruct.Pin = SPI1_NSS1_Pin|SPI1CD_Pin; |
362 | GPIO_InitStruct.Pin = SPI1_NSS1_Pin|SPI1CD_Pin; |
| 368 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
363 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 369 | GPIO_InitStruct.Speed = GPIO_SPEED_LOW; |
364 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
| 370 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
365 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 371 | 366 | ||
| 372 | /*Configure GPIO pins : SPI_RESET_Pin SPI_NSS2_Pin USART3_INVERT_Pin USB_PWR_Pin */ |
367 | /*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; |
368 | GPIO_InitStruct.Pin = SPI_RESET_Pin|SPI_NSS2_Pin|USART3_INVERT_Pin|USB_PWR_Pin; |
| 374 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
369 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 375 | GPIO_InitStruct.Speed = GPIO_SPEED_LOW; |
370 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
| 376 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
371 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
| 377 | 372 | ||
| 378 | /*Configure GPIO pins : SW1_PUSH_Pin SW1_I_Pin SW1_Q_Pin SW2_PUSH_Pin */ |
373 | /*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; |
374 | GPIO_InitStruct.Pin = SW1_PUSH_Pin|SW1_I_Pin|SW1_Q_Pin|SW2_PUSH_Pin; |
| 380 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
375 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |