Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions firmware/Core/Inc/circ_buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* circ_buffer.h
*
* Created on: 16 มี.ค. 2569
* Author: pkuna
*/

#ifndef INC_CIRC_BUFFER_H_
#define INC_CIRC_BUFFER_H_

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

typedef struct {
uint8_t * const buffer;
int head;
int tail;
const int maxLen;
} circularBuffer;

#define circBuffDef(buff, size) \
uint8_t buff##_data_space[size]; \
circularBuffer buff = { \
.buffer = buff##_data_space, \
.head = 0, \
.tail = 0, \
.maxLen = size \
}

extern circularBuffer gpsBuffer;

bool checkCircBuff(circularBuffer *cb);
bool circBuffPush(circularBuffer *cb, uint8_t data);
bool circBuffPop(circularBuffer *cb, uint8_t *data);

#endif /* INC_CIRC_BUFFER_H_ */
22 changes: 22 additions & 0 deletions firmware/Core/Inc/m8n.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* m8n.h
*
* Created on: 16 มี.ค. 2569
* Author: pkuna
*/

#ifndef SRC_M8N_H_
#define SRC_M8N_H_

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include "circ_buffer.h"

uint8_t gpsData = 0x55;
volatile uint64_t CAN_TX;
int gpsDataSize = 128;

circBuffDef(gpsBuffer, 1000);

#endif /* SRC_M8N_H_ */
46 changes: 46 additions & 0 deletions firmware/Core/Src/circ_buffer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* circ_buffer.c
*
* Created on: 16 มี.ค. 2569
* Author: pkuna
*/

#include "circ_buffer.h"

bool checkCircBuff(circularBuffer *cb) {
if (cb->head == cb->tail) {
return true;
} else if ((cb->head + 1) == cb->tail) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Vava-2549 there seems to be a wrap around bug here. The full detection check is missing a modulo, so it breaks when the head is at the last index. Assume a 5 bit buffer, in your code if the head is at the last index (indicating full), 4 + 1 doesnot equal 0 (aka your tail is at 0). I think you might need (head + 1) % 5 here, so that (4 + 1 % 5) == 0 indicating the buffer is full. But if you referenced this code from somewhere do confirm and let me know

return false;
}
}

bool circBuffPush(circularBuffer *cb, uint8_t data) {
if (checkCircBuff(cb)) {
int next = cb->head + 1;
if (next >= cb->maxLen) {
next = 0;
}
cb->buffer[cb->head] = data;
cb->head = next;

return true;
} else {
return false;
}
}

bool circBuffPop(circularBuffer *cb, uint8_t *data) {
if (!checkCircBuff(cb)) {
int next = cb->tail + 1;
if (next >= cb->maxLen) {
next = 0;
}
*data = cb->buffer[cb->tail];
cb->tail = next;

return true;
} else {
return false;
}
}
28 changes: 28 additions & 0 deletions firmware/Core/Src/m8n.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* m8n.c
*
* Created on: 16 มี.ค. 2569
* Author: pkuna
*/

#include "main.h"
#include "m8n.h"

int readData(void) {
while (1) {
/* UART data receiver */
HAL_UART_Receive(&huart2, &gpsData, gpsDataSize, 1000);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_2);
HAL_Delay(1000);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This HAL_Delay has potential of causing massive errors cause you would be processing data at just about every 2 seconds which is definitely too slow for NMEA data coming in at 9600 baud, aka we might lose data cause of the HAL_Delay.


if (circBuffPush(&gpsBuffer, gpsData)) {
printf("yes");
}

if (circBuffPop(&gpsBuffer, &CAN_TX)) {
printf("yes");
}
}

return 0;
}