1 | #include <stdio.h>
|
2 | #include <stdlib.h>
|
3 | #include "diag/Trace.h"
|
4 | #include "stm32f4xx_hal.h"
|
5 | #include "stm32f4xx_hal_spi.h"
|
6 |
|
7 | #pragma GCC diagnostic ignored "-Wunused-parameter"
|
8 |
|
9 | static int volatile tick_count;
|
10 | static volatile bool tick;
|
11 |
|
12 | extern "C" void SysTick_Handler(void)
|
13 | {
|
14 | HAL_IncTick();
|
15 |
|
16 | if (tick_count==0)
|
17 | HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
|
18 | else if (tick_count==666)
|
19 | HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
|
20 |
|
21 | if (++tick_count==1000)
|
22 | {
|
23 | tick_count = 0;
|
24 | tick = true;
|
25 | }
|
26 | }
|
27 |
|
28 | extern "C" void HAL_SPI_MspInit(SPI_HandleTypeDef *sh)
|
29 | {
|
30 | GPIO_InitTypeDef s;
|
31 |
|
32 | __SPI2_CLK_ENABLE();
|
33 | __GPIOB_CLK_ENABLE();
|
34 |
|
35 | s.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
|
36 | s.Mode = GPIO_MODE_AF_PP;
|
37 | s.Speed = GPIO_SPEED_FAST;
|
38 | s.Pull = GPIO_NOPULL;
|
39 | s.Alternate = GPIO_AF5_SPI2;
|
40 | HAL_GPIO_Init(GPIOB, &s);
|
41 |
|
42 | s.Pin = GPIO_PIN_12;
|
43 | s.Mode = GPIO_MODE_OUTPUT_PP;
|
44 | s.Speed = GPIO_SPEED_FAST;
|
45 | s.Pull = GPIO_NOPULL;
|
46 | HAL_GPIO_Init(GPIOB, &s);
|
47 |
|
48 | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
|
49 | }
|
50 |
|
51 | int main(int argc, char* argv[])
|
52 | {
|
53 | HAL_Init();
|
54 | trace_printf("System clock: %uHz\n", SystemCoreClock);
|
55 |
|
56 | __GPIOD_CLK_ENABLE();
|
57 |
|
58 | GPIO_InitTypeDef GPIO_InitStructure;
|
59 | GPIO_InitStructure.Pin = GPIO_PIN_12;
|
60 | GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
|
61 | GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
|
62 | GPIO_InitStructure.Pull = GPIO_NOPULL;
|
63 | HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
|
64 |
|
65 | SPI_HandleTypeDef sh;
|
66 | sh.Instance = SPI2;
|
67 | sh.Init.Mode = SPI_MODE_MASTER;
|
68 | sh.Init.Direction = SPI_DIRECTION_2LINES;
|
69 | sh.Init.DataSize = SPI_DATASIZE_8BIT;
|
70 | sh.Init.CLKPolarity = SPI_POLARITY_LOW;
|
71 | sh.Init.CLKPhase = SPI_PHASE_1EDGE;
|
72 | sh.Init.NSS = SPI_NSS_SOFT;
|
73 | sh.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
|
74 | sh.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
75 | sh.Init.TIMode = SPI_TIMODE_DISABLE;
|
76 | sh.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
77 | sh.Init.CRCPolynomial = 1;
|
78 |
|
79 | HAL_SPI_Init(&sh);
|
80 |
|
81 | static uint8_t buffer[] = { 0xB6, 0x0A };
|
82 | static uint8_t rbuf[2];
|
83 |
|
84 | HAL_StatusTypeDef s = HAL_SPI_TransmitReceive(&sh, buffer, rbuf, 2, 100);
|
85 | HAL_Delay(10);
|
86 | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
|
87 | HAL_Delay(10);
|
88 | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
|
89 |
|
90 | trace_printf("SPI Status = %d (%02x, %02x)\n", s, rbuf[0], rbuf[1]);
|
91 |
|
92 | while(1)
|
93 | {
|
94 | if (tick)
|
95 | {
|
96 | tick = false;
|
97 |
|
98 | uint8_t h = buffer[0];
|
99 | buffer[0] = buffer[1];
|
100 | buffer[1] = h;
|
101 |
|
102 | s = HAL_SPI_TransmitReceive(&sh, buffer, rbuf, 2, 100);
|
103 | HAL_Delay(10);
|
104 | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
|
105 | HAL_Delay(10);
|
106 | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
|
107 |
|
108 | trace_printf("SPI Status = %d (%02x, %02x)\n", s, rbuf[0], rbuf[1]);
|
109 | }
|
110 | }
|
111 | }
|