-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
278 lines (228 loc) · 9.09 KB
/
Copy pathmain.cpp
File metadata and controls
278 lines (228 loc) · 9.09 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "diag/trace.h"
#include "es_wifi_io.h"
#include "http_client.h"
#include "sensor/magnetometer_stream.h"
#include "stm32f4xx_hal.h"
#include <stdio.h>
#include <stm32f413h_discovery.h>
#include <string.h>
#include <wifi.h>
#include "error_handler.hpp"
#include "wifi_conf.hpp"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#pragma GCC diagnostic ignored "-Wreturn-type"
/* Private variables ---------------------------------------------------------*/
static uint8_t http_buffer[HTTP_BUFFER_SIZE];
static uint8_t server_ip[4] = SERVER_IP_BYTES;
/* Flag set by TIM3 ISR to trigger an HTTP POST */
volatile uint8_t http_send_flag = 0;
/* Flags used by WiFi driver ISR (defined here to satisfy externs in es_wifi_io.h) */
volatile uint8_t http_process_flag = 0;
volatile uint8_t server_maintenance_flag = 0;
/* Private function prototypes -----------------------------------------------*/
static void SystemClock_Config(void);
static void LED_Init(void);
static void TIM3_Init(void);
static void WiFi_InitAndConnect(void);
static void HTTP_SendHeading(void);
static float simple_atan2(float y, float x);
[[noreturn]]
int main(int argc, char *argv[]) {
HAL_Init();
SystemClock_Config();
LED_Init();
Error_Handler_Init();
trace_printf("\n\n");
trace_printf("================================\n");
trace_printf(" STM32F413H WiFi HTTP Client\n");
trace_printf("================================\n");
trace_printf("Initializing...\n\n");
/* Initialize magnetometer streaming module */
trace_printf("Step 1: Initializing magnetometer...\n");
if (Magnetometer_Init() != 0) {
trace_printf("ERROR: Magnetometer initialization failed!\n");
Error_Handler("Magnetometer Init Failed");
}
trace_printf(" -> Magnetometer initialized\n\n");
WiFi_InitAndConnect();
trace_printf("\nStep 4: Starting magnetometer sampling...\n");
Magnetometer_StartSampling();
trace_printf(" -> Magnetometer sampling started at 20Hz\n");
trace_printf("\nStep 5: Starting HTTP send timer (every %d ms)...\n",
HTTP_SEND_INTERVAL_MS);
TIM3_Init();
trace_printf(" -> TIM3 configured for %d ms periodic HTTP POST\n",
HTTP_SEND_INTERVAL_MS);
trace_printf("\n======================\n");
trace_printf(" Client Ready!\n");
trace_printf("======================\n");
trace_printf("Sending heading to http://%d.%d.%d.%d:%d%s\n",
server_ip[0], server_ip[1], server_ip[2], server_ip[3],
SERVER_PORT, SERVER_ENDPOINT);
trace_printf("CPU will enter low-power mode (WFI) between sends\n\n");
BSP_LED_On(LED_GREEN);
while (1) {
if (http_send_flag) {
http_send_flag = 0;
HTTP_SendHeading();
}
__WFI();
}
}
/**
* @brief Connect to WiFi network (no server startup)
*/
static void WiFi_InitAndConnect(void) {
WIFI_Status_t status;
trace_printf("Step 2: Initializing WiFi module...\n");
status = WIFI_Init();
if (status != WIFI_STATUS_OK) {
Error_Handler("ERROR: WiFi initialization failed! (Status: %d)\n",
status);
}
trace_printf(" -> WiFi module initialized\n");
trace_printf("\nStep 3: Connecting to network '%s'...\n", WIFI_SSID);
status = WIFI_Connect(WIFI_SSID, WIFI_PASSWORD, WIFI_ECN_WPA2_PSK);
if (status != WIFI_STATUS_OK) {
Error_Handler("ERROR: WiFi connection failed! (Status: %d)\nCheck SSID "
"and password in include/wifi/wifi_conf.hpp",
status);
}
trace_printf(" -> Connected to WiFi network\n");
}
/**
* @brief Build HTTP POST body and send heading to the Python server
*/
static void HTTP_SendHeading(void) {
MagSample_t sample;
char degree_str[16] = "0.0";
char body[32];
int body_len;
int request_len;
uint16_t sent_len = 0;
WIFI_Status_t status;
BSP_LED_Off(LED_GREEN);
/* Read the latest magnetometer sample */
if (Magnetometer_GetAvailableSamples() > 0 &&
Magnetometer_ReadSample(&sample)) {
float heading =
simple_atan2(sample.y, sample.x) * 180.0f / 3.14159265f;
if (heading < 0) {
heading += 360.0f;
}
snprintf(degree_str, sizeof(degree_str), "%.1f", heading);
}
/* Build POST body: "degree=<value>" */
body_len = snprintf(body, sizeof(body), "degree=%s", degree_str);
/* Build server IP string for the Host header */
char host_str[24];
snprintf(host_str, sizeof(host_str), "%d.%d.%d.%d",
server_ip[0], server_ip[1], server_ip[2], server_ip[3]);
/* Build full HTTP POST request */
request_len = snprintf((char *)http_buffer, HTTP_BUFFER_SIZE,
HTTP_POST_REQUEST_FMT,
host_str,
body_len,
degree_str);
trace_printf("[SEND] Heading: %s deg -> %s:%d\n", degree_str, host_str,
SERVER_PORT);
/* Open TCP connection to server */
status = WIFI_OpenClientConnection(0, WIFI_TCP_PROTOCOL, "HTTP",
(char *)server_ip, SERVER_PORT, 0);
if (status != WIFI_STATUS_OK) {
trace_printf("[SEND] ERROR: Failed to open connection (Status: %d)\n",
status);
BSP_LED_On(LED_GREEN);
return;
}
/* Send HTTP POST request */
status = WIFI_SendData(0, http_buffer, (uint16_t)request_len, &sent_len);
if (status == WIFI_STATUS_OK) {
trace_printf("[SEND] OK (%d bytes)\n", sent_len);
} else {
trace_printf("[SEND] ERROR: Send failed (Status: %d)\n", status);
}
/* Close TCP connection */
WIFI_CloseClientConnection();
BSP_LED_On(LED_GREEN);
}
/**
* @brief Initialize TIM3 to fire every HTTP_SEND_INTERVAL_MS milliseconds
*/
static void TIM3_Init(void) {
static TIM_HandleTypeDef htim3; /* static: handle must outlive this function */
memset(&htim3, 0, sizeof(htim3));
/* TIM3 clock: 16 MHz, prescaler 16000-1 → 1 kHz tick */
__HAL_RCC_TIM3_CLK_ENABLE();
htim3.Instance = TIM3;
htim3.Init.Prescaler = (16000 - 1);
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = (HTTP_SEND_INTERVAL_MS - 1);
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK) {
Error_Handler("TIM3 Init Failed");
}
HAL_NVIC_SetPriority(TIM3_IRQn, 6, 0);
HAL_NVIC_EnableIRQ(TIM3_IRQn);
HAL_TIM_Base_Start_IT(&htim3);
}
static void LED_Init(void) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
BSP_LED_Init(LED_GREEN);
BSP_LED_Init(LED_RED);
BSP_LED_Off(LED_GREEN);
BSP_LED_Off(LED_RED);
__HAL_RCC_GPIOE_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_4, GPIO_PIN_RESET);
}
static void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {.ClockType = 0,
.SYSCLKSource = 0,
.AHBCLKDivider = 0,
.APB1CLKDivider = 0,
.APB2CLKDivider = 0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler("Clock Config Failed - HSI");
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 16 MHz
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; // 16 MHz
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 16 MHz
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
Error_Handler("Clock Config Failed - Buses");
}
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
/* Simple atan2 approximation for heading calculation */
static float simple_atan2(float y, float x) {
float abs_y = (y < 0) ? -y : y;
float angle;
if (x >= 0) {
float r = (x - abs_y) / (x + abs_y);
angle = 0.785398163f - 0.785398163f * r;
} else {
float r = (x + abs_y) / (abs_y - x);
angle = 2.356194490f - 0.785398163f * r;
}
return (y < 0) ? -angle : angle;
}
#pragma GCC diagnostic pop