1 | #include "stm32f1xx_hal.h"
|
2 | #include "stm32f1xx_hal_conf.h"
|
3 |
|
4 | RCC_OscInitTypeDef RCC_OscInitStruct;
|
5 | RCC_ClkInitTypeDef RCC_ClkInitStruct;
|
6 | GPIO_InitTypeDef GPIO_InitStruct;
|
7 | SPI_HandleTypeDef hspi1;
|
8 |
|
9 |
|
10 |
|
11 | uint8_t CRC8_Table[256]; // 8-bit table for PEC calc
|
12 |
|
13 | void CRC8_Init(void)
|
14 | {
|
15 | uint16_t i, j;
|
16 | uint8_t crc;
|
17 | for (i = 0; i < 256; i++)
|
18 | {
|
19 | crc = i;
|
20 | for (j = 0; j < 8; j ++)
|
21 | {
|
22 | crc = (crc << 1) ^ ((crc & 0x80) ? 0x07 : 0);
|
23 | }
|
24 | CRC8_Table[i] = crc & 0xFF;
|
25 | }
|
26 | }
|
27 |
|
28 |
|
29 | int main(void)
|
30 | {
|
31 |
|
32 | // Reset of all peripherals, Initializes the Flash interface and the Systick
|
33 | HAL_Init();
|
34 |
|
35 | // Configure external quarz as clock
|
36 | RCC_OscInitTypeDef RCC_OscInitStruct;
|
37 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
|
38 | RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
39 | RCC_OscInitStruct.HSICalibrationValue = 16;
|
40 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
41 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
|
42 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
|
43 | HAL_RCC_OscConfig(&RCC_OscInitStruct);
|
44 |
|
45 |
|
46 |
|
47 | //-------------------------------------------------------------------
|
48 | // Init SPI
|
49 | //-------------------------------------------------------------------
|
50 | // Peripheral clock enable
|
51 | __SPI1_CLK_ENABLE();
|
52 | __GPIOA_CLK_ENABLE();
|
53 | __GPIOB_CLK_ENABLE();
|
54 |
|
55 | hspi1.Instance = SPI1;
|
56 | hspi1.Init.Mode = SPI_MODE_MASTER;
|
57 | hspi1.Init.Direction = SPI_DIRECTION_2LINES;
|
58 | hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
|
59 | hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
|
60 | hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
|
61 | hspi1.Init.NSS = SPI_NSS_SOFT;
|
62 | hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
|
63 | hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
64 | hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
|
65 | HAL_SPI_Init(&hspi1);
|
66 |
|
67 | // SPI1 GPIO Configuration
|
68 | // PA5 ------> SPI1_SCK
|
69 | // PA6 ------> SPI1_MISO
|
70 | // PA7 ------> SPI1_MOSI
|
71 | // PB6 ------> SPI_1 CS (LTC6803_CS_PIN)
|
72 |
|
73 | GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7;
|
74 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
75 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
|
76 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
77 |
|
78 | GPIO_InitStruct.Pin = GPIO_PIN_6;
|
79 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
80 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
|
81 | GPIO_InitStruct.Pull = GPIO_PULLUP;
|
82 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
83 |
|
84 | // set CS to high (disable)
|
85 | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
|
86 |
|
87 | GPIO_InitStruct.Pin = GPIO_PIN_6;
|
88 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
89 | GPIO_InitStruct.Pull = GPIO_PULLUP;
|
90 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
|
91 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
92 |
|
93 |
|
94 | // Calculates the table for PEC data
|
95 | CRC8_Init();
|
96 |
|
97 |
|
98 | //-------------------------------------------------------------------
|
99 | // Write Configuration
|
100 | //-------------------------------------------------------------------
|
101 |
|
102 | // prepare WRCFG + WRPEC
|
103 | uint8_t send_buffer[9] = {0x01, 0xc7}; // WRCFG + WRPEC
|
104 |
|
105 | // set CS to low (enable)
|
106 | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
|
107 |
|
108 | // send WRCFG + WRPEC
|
109 | HAL_SPI_Transmit(&hspi1, send_buffer, 2, 1);
|
110 |
|
111 | // prepare CFG1R... + CFGPEC
|
112 | send_buffer[0] = 0x11; // CFG1R0
|
113 | send_buffer[1] = 0x00; // CFG1R1
|
114 | send_buffer[2] = 0x00; // CFG1R2
|
115 | send_buffer[3] = 0x00; // CFG1R3
|
116 | send_buffer[4] = 0x00; // CFG1R4
|
117 | send_buffer[5] = 0x00; // CFG1R5
|
118 | send_buffer[6] = 0x3B; // CFGPEC
|
119 |
|
120 | // send CFG1R... + CFGPEC
|
121 | HAL_SPI_Transmit(&hspi1, send_buffer, 7, 1);
|
122 |
|
123 | // set CS to high (disable)
|
124 | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
|
125 |
|
126 | //-------------------------------------------------------------------
|
127 | // Try to read Configuration into receive_Buffer
|
128 | //-------------------------------------------------------------------
|
129 |
|
130 | // Read LTC6803 configuration
|
131 | uint8_t receive_Buffer[7] = { 0 };
|
132 | // preapare RDCFG + RCPEC
|
133 | send_buffer[0] = 0x02; // RDCFG
|
134 | send_buffer[1] = 0xCE; // RCPEC
|
135 | send_buffer[2] = 0; // nothing to send, just receive
|
136 | send_buffer[3] = 0; // nothing to send, just receive
|
137 | send_buffer[4] = 0; // nothing to send, just receive
|
138 | send_buffer[5] = 0; // nothing to send, just receive
|
139 | send_buffer[6] = 0; // nothing to send, just receive
|
140 |
|
141 | // set CS to high (disable)
|
142 | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
|
143 |
|
144 | // send RDCFG + RCPEC and receive CFG1R... and CFGPEC
|
145 | HAL_SPI_TransmitReceive(&hspi1, send_buffer, receive_Buffer, 9, 1);
|
146 |
|
147 | // set CS to high (disable)
|
148 | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
|
149 |
|
150 | while (1) {}
|
151 | }
|