Allocate memory for data packets on stack rather than heap

This commit is contained in:
Rihards Skuja 2018-01-23 23:22:14 +02:00
parent e7059c790e
commit 1633d3e78d
No known key found for this signature in database
GPG Key ID: 53FA13A3F7F8571B

View File

@ -11,16 +11,14 @@
#include "debug.h" #include "debug.h"
#endif /* DEBUG */ #endif /* DEBUG */
uint8_t rx_raw[sizeof(data_packet_t)];
data_packet_t rx_data;
int main(void) int main(void)
{ {
uint8_t *rx_raw;
data_packet_t *rx_data;
// Data packet is too large, should split in multiple packets // Data packet is too large, should split in multiple packets
if (sizeof(data_packet_t) > MAX_PAYLOAD_SIZE) if (sizeof(data_packet_t) > MAX_PAYLOAD_SIZE)
return R_PACKAGE_SIZE; return R_PACKAGE_SIZE;
rx_data = malloc(sizeof(data_packet_t));
rx_raw = malloc(sizeof(data_packet_t));
init_leds(); init_leds();
nrf24_init(); nrf24_init();
@ -29,41 +27,23 @@ int main(void)
nrf24_tx_address(node_mac); nrf24_tx_address(node_mac);
nrf24_rx_address(base_mac); nrf24_rx_address(base_mac);
// Use green LED as power LED
// TODO: need all LEDs for debugging, will use as power when UART debugger
// will arrive
/* SET(LED_PORT, LED_GREEN_BIT); */
// Set up TIMER1
TCNT1 = 0;
SET(TCCR1B, CS12); // Prescaler = 256
SET(TIMSK1, TOIE1); // Enable overflow interrupts
ICR1 = 15625; // Interrupt after 0.25s
sei();
while (1) { while (1) {
if (nrf24_dataReady()) { if (nrf24_dataReady()) {
memset(rx_raw, 0, sizeof(data_packet_t)); memset(rx_raw, 0, sizeof(data_packet_t));
nrf24_getData(rx_raw); nrf24_getData(rx_raw);
memcpy(rx_data, rx_raw, sizeof(data_packet_t)); memcpy(&rx_data, rx_raw, sizeof(data_packet_t));
if (rx_data->distance < 10 && rx_data->distance >= 0) if (rx_data.distance < 10)
SET(LED_PORT, LED_BLUE_BIT); SET(LED_PORT, LED_BLUE_BIT);
else else
CLR(LED_PORT, LED_BLUE_BIT); CLR(LED_PORT, LED_BLUE_BIT);
if (rx_data->temp > 23) if (rx_data.temp > 22)
SET(LED_PORT, LED_GREEN_BIT); SET(LED_PORT, LED_GREEN_BIT);
else else
CLR(LED_PORT, LED_GREEN_BIT); CLR(LED_PORT, LED_GREEN_BIT);
} }
} }
cli();
free(rx_data);
free(rx_raw);
return 0; return 0;
} }