-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlab4_depthtask
More file actions
199 lines (180 loc) · 5.84 KB
/
lab4_depthtask
File metadata and controls
199 lines (180 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "init.h"
#include "temperature.h"
char c1;
char c2;
short t;
char debug;
char result;
char dummybite = 0x00;
char V_MAJ = 0;
char V_MAJs = 0x02;
char V_MIN = 1;
char V_MINs = 0x03;
char CTL_REG = 2;
char CTL_REGs = 0x01;
char STS_REG =3;
char STS_REGs = 0x09;
char TMP_AVG =4;
char TMP_LO = 5;
char TMP_LOs = 0x00;
char TMP_HI =6;
char TMP_HIs = 0x00;
char CH_BUF =7;
char CH_BUFs = 0x00;
char DEVID =9;
char DEVIDs=0x11;
char db = 0x10;
SPI_HandleTypeDef spi2h;
UART_HandleTypeDef huart6;
void configureSPI()
{
spi2h.Instance = SPI2; // Please use SPI2!
spi2h.Init.Mode = SPI_MODE_SLAVE; // Set master mode
spi2h.Init.TIMode = SPI_TIMODE_DISABLE; // Use Motorola mode, not TI mode
spi2h.Init.DataSize = SPI_DATASIZE_8BIT;
spi2h.Init.CLKPolarity = SPI_POLARITY_LOW;
spi2h.Init.CLKPhase = SPI_PHASE_2EDGE;
spi2h.Init.Direction = SPI_DIRECTION_2LINES;//?
spi2h.Init.NSS = SPI_NSS_SOFT;//SS signal driven by the firmware
spi2h.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
spi2h.Init.FirstBit=SPI_FIRSTBIT_MSB;
spi2h.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
spi2h.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
HAL_SPI_Init(&spi2h);
/*
* ... You get the idea.
*/
//HAL_SPI_Init(&[SPIHandler here]);
//
// Note: HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi)
//
// HAL_SPI_Init() will call HAL_SPI_MspInit() after reading the properties of
// the passed SPI_HandleTypeDef. After finishing the MspInit call, it will set
// the SPI property bits. This is how all HAL_[peripheral]_Init() functions work.
}
void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
{
// SPI GPIO initialization structure here
GPIO_InitTypeDef GPIO_InitStruct;
if (hspi->Instance == SPI2)
{
__GPIOA_CLK_ENABLE();
//SPI2_SCK PA12 D13
GPIO_InitStruct.Pin = GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
__GPIOB_CLK_ENABLE();
//SPI2_MOSI PB15 D11
GPIO_InitStruct.Pin = GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
//MISO PB14 D12
GPIO_InitStruct.Pin = GPIO_PIN_14;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
//SPI2_NSS PA11 D10
GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
__HAL_RCC_SPI2_CLK_ENABLE();
// Enable SPI GPIO port clocks, set HAL GPIO init structure's values for each
// SPI-related port pin (SPI port pin configuration), enable SPI IRQs (if applicable), etc.
}
}
int main(void) {
Sys_Init();
configureSPI();
initTempSensor();
while(1) {
for (int i = 0; i < 30; i++) asm("nop");
if(!HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_11)) {
for (int i = 0; i < 10; i++) asm("nop");
if(!HAL_UART_Receive(&USB_UART, (uint8_t *)&c2, 1, 10)) {
for (int i = 0; i < 10; i++) asm("nop");
HAL_SPI_Transmit(&spi2h, (uint8_t *)&c2, 1, 10);
for (int i = 0; i < 10; i++) asm("nop");
}
HAL_SPI_Receive(&spi2h, (uint8_t *)&c1, 1, 10);
//for (int i = 0; i < 30; i++) asm("nop");
//HAL_UART_Transmit(&USB_UART, (uint8_t *)&c1, 1, 10);
//slave send a byte to master
if ( c1 == STS_REG ) {
//printf("STS_REG\r\n");
for (int i = 0; i < 10; i++) asm("nop");
HAL_SPI_Transmit(&spi2h, (uint8_t *)&STS_REGs, 1, 10);
for (int i = 0; i < 10; i++) asm("nop");
}
if ( c1 == CTL_REG ) {
//printf("CTL_REG\r\n");
for (int i = 0; i < 10; i++) asm("nop");
HAL_SPI_Transmit(&spi2h, (uint8_t *)&CTL_REGs, 1, 10);
for (int i = 0; i < 10; i++) asm("nop");
}
if ( c1 == V_MAJ ) {
//printf("V_MAJ\r\n");
for (int i = 0; i < 10; i++) asm("nop");
HAL_SPI_Transmit(&spi2h, (uint8_t *)&V_MAJs, 1, 10);
for (int i = 0; i < 10; i++) asm("nop");
}
if ( c1 == V_MIN ) {
//printf("V_MIN\r\n");
for (int i = 0; i < 10; i++) asm("nop");
HAL_SPI_Transmit(&spi2h, (uint8_t *)&V_MINs, 1, 10);
for (int i = 0; i < 10; i++) asm("nop");
}
if ( c1 == DEVID ) {
for (int i = 0; i < 10; i++) asm("nop");
HAL_SPI_Transmit(&spi2h, (uint8_t *)&DEVIDs, 1, 10);
for (int i = 0; i < 10; i++) asm("nop");
}
if ( c1 == TMP_HI) {
//CTL_REGs |= 0x02;//request a temp read
//STS_REGs |= 0x04;//temperature measurement underway
//STS_REGs &= 0xf7;//temp measure not ready
t = 0x0000;
for (int i= 0; i < 32; i++) {
t += getTemperature();
}
t = t/32;
//STS_REGs &= 0xfb;//temp measure not underway
//STS_REGs |= 0x08;//temp measure ready
TMP_HIs = t >> 6;//get the high byte
for (int i = 0; i < 10; i++) asm("nop");
HAL_SPI_Transmit(&spi2h, (uint8_t *)&TMP_HIs, 1, 10);//or could we just transmit tmp_hi instead?
for (int i = 0; i < 10; i++) asm("nop");
//CTL_REGs &= 0xfd;//not request
}
if ( c1 == TMP_LO) {
//printf("TMP_LO\r\n");
//CTL_REGs |= 0x02;//request a temp read
//STS_REGs |= 0x04;//temperature measurement underway
//STS_REGs &= 0xf7;//temp measure not ready
t = 0x0000;
for (int i= 0; i < 32; i++) {
t += getTemperature();
}
t = t/32;
//STS_REGs &= 0xfb;//temp measure not underway
//STS_REGs |= 0x08;//temp measure ready
TMP_LOs = t & 0x00FF;// get the low byte
for (int i = 0; i < 10; i++) asm("nop");
HAL_SPI_Transmit(&spi2h, (uint8_t *)&TMP_LOs, 1, 10);//or could we just transmit tmp_hi instead?
for (int i = 0; i < 10; i++) asm("nop");
//CTL_REGs &= 0xfd;//not request
}
if (c1 == CH_BUF) {
//printf("char\r\n");
if(!HAL_UART_Receive(&USB_UART, (uint8_t *)&c2, 1, 10)) {
for (int i = 0; i < 10; i++) asm("nop");
HAL_SPI_Transmit(&spi2h, (uint8_t *)&c2, 1, 10);
for (int i = 0; i < 10; i++) asm("nop");
}
}
for (int i = 0; i < 30; i++) asm("nop");
// transmit the char to the master if there is any
}
}
}