Commit 36a7426d authored by Max Becker's avatar Max Becker
Browse files

Fix buffer overflow corrupting other variables

parent 47ccb06e
......@@ -11,6 +11,7 @@ install_manifest.txt
compile_commands.json
CTestTestfile.cmake
cmake-build-debug
cmake-build-release
### C template
# Prerequisites
......
......@@ -12,7 +12,7 @@ SET(FREQ "8000000")
SET(CMAKE_C_COMPILER avr-gcc)
SET(CMAKE_CXX_COMPILER avr-g++)
SET(CMAKE_C_FLAGS "-Os -mmcu=${DEVICE} -DF_CPU=${FREQ}UL -std=gnu99 -Wl,--gc-sections")
SET(CMAKE_C_FLAGS "-Os -mmcu=${DEVICE} -DF_CPU=${FREQ}UL -std=gnu99 -Wl,-Map,linker.map,--gc-sections")
SET(CMAKE_CXX_FLAGS "-Os -mmcu=${DEVICE} -DF_CPU=${FREQ}UL -Wl,--gc-sections")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
......
Subproject commit 014fba08cfd326b86babf99c4497d637028cbfde
Subproject commit 68ad131c54a62a4cf9d81f5405cb8136e26e4db2
......@@ -18,7 +18,7 @@ const char gitversion[8] = GIT_COMMIT_HASH;
/**
* Detector status, see detector_control/protocol.h STATUS_*
*/
uint8_t status;
volatile uint8_t detector_status;
/**
......@@ -57,8 +57,9 @@ uint8_t status_receiver2;
*/
uint8_t errors = 0;
uint8_t i2c_errors = 0;
volatile uint8_t i2c_errors = 0;
volatile bool transmission_complete = false;
/***
......@@ -83,7 +84,7 @@ void i2cReceiveEvent(uint8_t addr, uint8_t data);
uint8_t i2cRequestEvent(uint8_t addr);
/*
* Updates status, toggle transmit pin and disable timer interrupts
* Updates detector_status, toggle transmit pin and disable timer interrupts
*/
void inline startTransmission();
......@@ -92,6 +93,9 @@ void inline startTransmission();
*/
void inline finishTransmission();
void inline onI2CError();
void inline onError();
/**
* ####### MAIN Code #######
......@@ -99,25 +103,29 @@ void inline finishTransmission();
void i2cReceiveEvent(uint8_t addr, uint8_t data) {
switch (addr) {
case I2C_STATUS_SET:
status = data;
if(addr == I2C_STATUS_SET){
if(data == STATUS_TRANSMIT || data> STATUS_TRANSMIT){ //Cannot be forced into transmit mode. Cannot set non existing mode
onI2CError();
}
else{
detector_status = data;
if (data == STATUS_DETECTING) {
TX_IND_PORT &= ~(1 << TX_IND);
reset();//Reset receive state machine
ENABLE_IR_IN_INTR;
} else {
TX_IND_PORT &= ~(1 << TX_IND);
DISABLE_IR_IN_INTR;
}
break;
default:
onI2CError();
break;
}
}
else{
onI2CError();
}
}
uint8_t i2cRequestEvent(uint8_t addr) {
if (status == STATUS_TRANSMIT) {
if (detector_status == STATUS_TRANSMIT) {
switch (addr) {
case I2C_GET_ALL:
return I2C_ALL_OK;
......@@ -130,7 +138,7 @@ uint8_t i2cRequestEvent(uint8_t addr) {
case I2C_D_STATUS_TWO_GET:
return status_receiver2;
case I2C_GET_ALL2:
finishTransmission();
transmission_complete=true;
return I2C_ALL_OK;
default:
break;
......@@ -138,11 +146,11 @@ uint8_t i2cRequestEvent(uint8_t addr) {
}
switch (addr) {
case I2C_STATUS_GET:
return status;
return detector_status;
case I2C_ERRORS_GET:
return errors & 0x7F;//Ensure we are not transmitting 0xFF (I2C_ERROR_BYTE)
return errors & 0x7Fu;//Ensure we are not transmitting 0xFF (I2C_ERROR_BYTE)
case I2C_ERRORS_I2C_GET:
return i2c_errors & 0x7F;//Ensure we are not transmitting 0xFF (I2C_ERROR_BYTE)
return i2c_errors & 0x7Fu;//Ensure we are not transmitting 0xFF (I2C_ERROR_BYTE)
case I2C_READ_VERSION1:
case I2C_READ_VERSION2:
case I2C_READ_VERSION3:
......@@ -179,15 +187,14 @@ void setup() {
i2c_onRequestPtr = i2cRequestEvent;
setupIRIn();
status = STATUS_PAUSED;
detector_status = STATUS_PAUSED;
}
bool temp=false;
void loop() {
if (status == STATUS_DETECTING) {
if (detector_status == STATUS_DETECTING) {
decode_results results;
uint8_t result = decode(&results);
......@@ -227,16 +234,18 @@ void loop() {
reset();//Reset state machine on error if signal is received. (However, if a signal successfully decoded the Timer interrupts are disabled.
}
}
if(temp){
PORTB &= ~(1 << PB2);
temp=false;
}
else{
PORTB |= (1 << PB2);
temp=true;
if(transmission_complete){
transmission_complete=false;
finishTransmission();
}
}
}
void inline onError(){
......@@ -252,13 +261,15 @@ void inline onI2CError(){
}
void inline startTransmission() {
status = STATUS_TRANSMIT;
detector_status = STATUS_TRANSMIT;
TX_IND_PORT |= (1 << TX_IND);
}
void inline finishTransmission() {
TX_IND_PORT &= ~(1 << TX_IND);
status = STATUS_DETECTING;
detector_status = STATUS_DETECTING;
//reset(); Already resetting after signal decoded
ENABLE_IR_IN_INTR;
}
......@@ -268,24 +279,6 @@ int main(void) {
delay(100);
//Test
DDRB |= (1 << PB1);
DDRB |= (1 << PB2);
PORTB |= (1<<PB1);
delay(300);
PORTB &= ~(1<<PB1);
delay(300);
PORTB |= (1<<PB1);
delay(300);
PORTB &= ~(1<<PB1);
delay(300);
DDRB &= ~(1 << PB1);
setup();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment