Skip to content

Commit 7000505

Browse files
bugfix
1 parent 6dd30ba commit 7000505

File tree

1 file changed

+99
-52
lines changed

1 file changed

+99
-52
lines changed

Core/Src/main.c

Lines changed: 99 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/* Private includes ----------------------------------------------------------*/
2323
/* USER CODE BEGIN Includes */
2424
#include "stdio.h"
25-
25+
#include "string.h"
2626

2727
/* USER CODE END Includes */
2828

@@ -73,6 +73,8 @@ volatile uint16_t sampleIndex = 0;
7373
volatile uint8_t recording = 0;
7474
volatile uint8_t triggered = 0;
7575
volatile uint8_t startSampling = 0;
76+
#define CAN_ID_HEADER 0x321
77+
#define CAN_DLC 8
7678
/* USER CODE END PFP */
7779

7880
/* Private user code ---------------------------------------------------------*/
@@ -93,51 +95,7 @@ uint8_t calculate_crc8(uint8_t *data, uint16_t length)
9395
}
9496
return crc;
9597
}
96-
void sendAccelDataViaCAN(void)
97-
{
98-
uint8_t flatBuffer[SAMPLE_COUNT * 6];
99-
for (uint16_t i = 0; i < SAMPLE_COUNT; i++) {
100-
flatBuffer[i * 6 + 0] = accelBuffer[i].x & 0xFF;
101-
flatBuffer[i * 6 + 1] = (accelBuffer[i].x >> 8) & 0xFF;
102-
flatBuffer[i * 6 + 2] = accelBuffer[i].y & 0xFF;
103-
flatBuffer[i * 6 + 3] = (accelBuffer[i].y >> 8) & 0xFF;
104-
flatBuffer[i * 6 + 4] = accelBuffer[i].z & 0xFF;
105-
flatBuffer[i * 6 + 5] = (accelBuffer[i].z >> 8) & 0xFF;
106-
}
107-
108-
uint16_t totalLength = SAMPLE_COUNT * 6 + 1; // +1 für CRC
109-
110-
// Header senden mit Datenlänge
111-
FDCAN_TxHeaderTypeDef txHeader;
112-
txHeader.Identifier = 0x321;
113-
txHeader.IdType = FDCAN_STANDARD_ID;
114-
txHeader.TxFrameType = FDCAN_DATA_FRAME;
115-
txHeader.DataLength = FDCAN_DLC_BYTES_8;
116-
txHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
117-
txHeader.BitRateSwitch = FDCAN_BRS_OFF;
118-
txHeader.FDFormat = FDCAN_CLASSIC_CAN;
119-
txHeader.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
120-
txHeader.MessageMarker = 0;
121-
122-
uint8_t headerPayload[8] = { totalLength >> 8, totalLength & 0xFF };
123-
HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &txHeader, headerPayload);
124-
HAL_Delay(1);
12598

126-
// Nutzdaten in 8-Byte Blöcken übertragen
127-
for (uint16_t i = 0; i < totalLength - 1; i += 8)
128-
{
129-
uint8_t chunk[8] = {0xFF};
130-
uint8_t len = (totalLength - 1 - i >= 8) ? 8 : (totalLength - 1 - i);
131-
memcpy(chunk, &flatBuffer[i], len);
132-
HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &txHeader, chunk);
133-
HAL_Delay(1);
134-
}
135-
136-
// CRC8 berechnen und einzeln senden
137-
uint8_t crc = calculate_crc8(flatBuffer, SAMPLE_COUNT * 6);
138-
uint8_t crcPayload[8] = { crc, 0xAA, 0x55, 0, 0, 0, 0, 0 }; // Rest optional
139-
HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &txHeader, crcPayload);
140-
}
14199
uint8_t ADXL345_ReadRegister(uint8_t reg)
142100
{
143101
uint8_t tx[] = { 0x80 | reg, 0x00 }; // 0x80 = Lesezugriff
@@ -202,7 +160,78 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
202160
}
203161
}
204162

163+
void send_data_over_can(void)
164+
{
165+
FDCAN_TxHeaderTypeDef txHeader = {0};
166+
txHeader.Identifier = CAN_ID_HEADER;
167+
txHeader.IdType = FDCAN_STANDARD_ID;
168+
txHeader.TxFrameType = FDCAN_DATA_FRAME;
169+
txHeader.DataLength = FDCAN_DLC_BYTES_8;
170+
txHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
171+
txHeader.BitRateSwitch = FDCAN_BRS_OFF;
172+
txHeader.FDFormat = FDCAN_CLASSIC_CAN;
173+
txHeader.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
174+
txHeader.MessageMarker = 0;
175+
176+
uint8_t txData[8];
177+
uint32_t totalBytes = SAMPLE_COUNT * sizeof(AccelSample); // 1000 × 6 = 6000
178+
uint16_t totalLen = totalBytes + 1; // +1 CRC
205179

180+
// 1. Header senden (2 Byte Länge)
181+
txData[0] = (uint8_t)(totalLen >> 8);
182+
txData[1] = (uint8_t)(totalLen & 0xFF);
183+
memset(&txData[2], 0xFF, 6); // Rest füllen
184+
HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &txHeader, txData);
185+
HAL_Delay(1); // Optional: Bus entlasten
186+
187+
// 2. Payload vorbereiten
188+
uint8_t rawData[totalLen];
189+
memcpy(rawData, (uint8_t*)accelBuffer, totalBytes);
190+
191+
// CRC8 berechnen
192+
uint8_t crc = 0;
193+
for (uint16_t i = 0; i < totalBytes; i++)
194+
{
195+
crc ^= rawData[i];
196+
for (uint8_t j = 0; j < 8; j++)
197+
{
198+
if (crc & 0x80)
199+
crc = (crc << 1) ^ 0x07;
200+
else
201+
crc <<= 1;
202+
}
203+
}
204+
rawData[totalBytes] = crc;
205+
206+
// 📤 3. In 8-Byte-Pakete aufteilen und senden
207+
for (uint16_t i = 0; i < totalLen; i += 8)
208+
{
209+
uint8_t len = (totalLen - i >= 8) ? 8 : (totalLen - i);
210+
memset(txData, 0xFF, 8);
211+
memcpy(txData, &rawData[i], len);
212+
213+
HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &txHeader, txData);
214+
HAL_Delay(1); // Optional für Stabilität
215+
}
216+
217+
printf("CAN-Übertragung abgeschlossen (%d Bytes)\r\n", totalLen);
218+
}
219+
220+
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
221+
{
222+
if (RxFifo0ITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE)
223+
{
224+
FDCAN_RxHeaderTypeDef rxHeader;
225+
uint8_t rxData[8];
226+
HAL_FDCAN_GetRxMessage(hfdcan, FDCAN_RX_FIFO0, &rxHeader, rxData);
227+
228+
if (rxHeader.Identifier == 0x123 && rxData[0] == 0xAB)
229+
{
230+
printf("Anfrage vom MTS erhalten – sende Daten...\r\n");
231+
send_data_over_can(); // oder sendAccelDataViaCAN();
232+
}
233+
}
234+
}
206235

207236
/* USER CODE END 0 */
208237

@@ -240,7 +269,10 @@ int main(void)
240269
MX_FDCAN1_Init();
241270
/* USER CODE BEGIN 2 */
242271
HAL_TIM_Base_Start_IT(&htim2);
243-
272+
if (HAL_FDCAN_Start(&hfdcan1) != HAL_OK)
273+
{
274+
Error_Handler();
275+
}
244276
/* USER CODE END 2 */
245277

246278
/* Initialize leds */
@@ -277,7 +309,7 @@ int main(void)
277309
// Trigger prüfen
278310
if (triggered == 0 && HAL_GPIO_ReadPin(Trigger_GPIO_Port, Trigger_Pin) == GPIO_PIN_SET)
279311
{
280-
printf("⬆️ Trigger erkannt\n");
312+
printf(" Trigger erkannt\n");
281313
triggered = 1;
282314
startSampling = 1;
283315
}
@@ -303,7 +335,8 @@ int main(void)
303335
HAL_Delay(1); // optional: UART entlasten
304336
}
305337
// CAN Senden
306-
sendAccelDataViaCAN();
338+
send_data_over_can();
339+
307340
sampleIndex = 0;
308341
triggered = 0;
309342
// LED AUS (Aufzeichnung + Senden fertig)
@@ -382,7 +415,7 @@ static void MX_FDCAN1_Init(void)
382415
hfdcan1.Instance = FDCAN1;
383416
hfdcan1.Init.ClockDivider = FDCAN_CLOCK_DIV1;
384417
hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC;
385-
hfdcan1.Init.Mode = FDCAN_MODE_NORMAL;
418+
hfdcan1.Init.Mode = FDCAN_MODE_EXTERNAL_LOOPBACK;
386419
hfdcan1.Init.AutoRetransmission = ENABLE;
387420
hfdcan1.Init.TransmitPause = DISABLE;
388421
hfdcan1.Init.ProtocolException = DISABLE;
@@ -394,15 +427,23 @@ static void MX_FDCAN1_Init(void)
394427
hfdcan1.Init.DataSyncJumpWidth = 1;
395428
hfdcan1.Init.DataTimeSeg1 = 1;
396429
hfdcan1.Init.DataTimeSeg2 = 1;
397-
hfdcan1.Init.StdFiltersNbr = 0;
430+
hfdcan1.Init.StdFiltersNbr = 1;
398431
hfdcan1.Init.ExtFiltersNbr = 0;
399432
hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
400433
if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK)
401434
{
402435
Error_Handler();
403436
}
404437
/* USER CODE BEGIN FDCAN1_Init 2 */
405-
438+
FDCAN_FilterTypeDef sFilter;
439+
sFilter.IdType = FDCAN_STANDARD_ID;
440+
sFilter.FilterIndex = 0;
441+
sFilter.FilterType = FDCAN_FILTER_MASK;
442+
sFilter.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
443+
sFilter.FilterID1 = 0x123;
444+
sFilter.FilterID2 = 0x7FF; // Alle Bits vergleichen
445+
HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilter);
446+
HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0);
406447
/* USER CODE END FDCAN1_Init 2 */
407448

408449
}
@@ -527,7 +568,13 @@ static void MX_GPIO_Init(void)
527568
HAL_GPIO_Init(Trigger_GPIO_Port, &GPIO_InitStruct);
528569

529570
/* USER CODE BEGIN MX_GPIO_Init_2 */
530-
571+
/* --- FDCAN1 TX / RX ------------------------------------------------------- */
572+
GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_11; // PA12 = TX, PA11 = RX
573+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
574+
GPIO_InitStruct.Pull = GPIO_NOPULL;
575+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
576+
GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN1; // *** WICHTIG ***
577+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
531578
/* USER CODE END MX_GPIO_Init_2 */
532579
}
533580

0 commit comments

Comments
 (0)