Skip to content
Snippets Groups Projects
Commit d55d52db authored by Carlo Alessandro Nicolau's avatar Carlo Alessandro Nicolau
Browse files

Under Development

parent 0e2408fb
No related branches found
No related tags found
No related merge requests found
Showing with 191 additions and 69 deletions
// Auto-generated file: variables.h
// Generation timestamp: 2021-06-17 18:31:59.933877
// Generation timestamp: 2021-06-20 05:48:56.170376
#ifndef _AUTOGENERATED_VARIABLES_H
#define _AUTOGENERATED_VARIABLES_H
......
......@@ -17,18 +17,33 @@ void _manage_digital_variables(void);
void _manage_digital_variable(digital_variable_t* _p_variable);
// "slow" timer interrupt service routine
void irq_timer_slow(void) {
void irq_timer_slow(void) {
// __TODO LED remove user pin activity
// userpin_SWITCH_LED_D9_set_high();
// update the adcreader state machine
adcreader_tick_irq();
// update timing register
timing_tick_irq();
// __TODO LED remove user pin activity
// userpin_SWITCH_LED_D9_set_low();
}
// "fast" timer interrupt service routine
void irq_timer_fast(void) {
void irq_timer_fast(void) {
// __TODO LED remove user pin activity
// userpin_SWITCH_LED_D10_set_high();
// manage digital alarms
_manage_digital_variables();
_manage_digital_variables();
// __TODO LED remove user pin activity
// userpin_SWITCH_LED_D10_set_low();
}
void _manage_digital_variables(void) {
......@@ -38,6 +53,7 @@ void _manage_digital_variables(void) {
}
void _manage_digital_variable(digital_variable_t* _p_variable) {
// read digital variables
_p_variable->value = _p_variable->readfunc();
......@@ -45,37 +61,41 @@ void _manage_digital_variable(digital_variable_t* _p_variable) {
if(_p_variable->value > _p_variable->value_max) {
_p_variable->value_max = _p_variable->value;
}
// check if timout counter has to be incremented
if(_p_variable->value > 0) {
// increment counter
_p_variable->alarm.timeout_counter += 1;
// avoid wrapping on increment (not very elegant...)
if(_p_variable->alarm.timeout_counter == 0) {
_p_variable->alarm.timeout_counter -= 1;
}
// update alarm counter stats
if(_p_variable->alarm.timeout_counter_max < _p_variable->alarm.timeout_counter) {
_p_variable->alarm.timeout_counter_max = _p_variable->alarm.timeout_counter;
}
}else{
// or reset the counter
_p_variable->alarm.timeout_counter = 0;
}
// check if alarm has fired
if(_p_variable->alarm.enabled) {
// check if timeout was reached
if(_p_variable->alarm.timeout_counter > _p_variable->alarm.timeout) {
// perform the alarm action
_p_variable->alarm.callback();
// increment the fire counter
_p_variable->alarm.firecount += 1;
// skip alarm management if alarm not implemented on current variable
if(_p_variable->alarm.implement) {
// check if timout counter has to be incremented
if(_p_variable->value > 0) {
// increment counter
_p_variable->alarm.timeout_counter += 1;
// avoid wrapping on increment (not very elegant...)
if(_p_variable->alarm.firecount == 0) {
_p_variable->alarm.firecount -= 1;
if(_p_variable->alarm.timeout_counter == 0) {
_p_variable->alarm.timeout_counter -= 1;
}
// update alarm counter stats
if(_p_variable->alarm.timeout_counter_max < _p_variable->alarm.timeout_counter) {
_p_variable->alarm.timeout_counter_max = _p_variable->alarm.timeout_counter;
}
// reset alarm timeout counter
}else{
// or reset the counter
_p_variable->alarm.timeout_counter = 0;
}
// check if alarm has fired
if(_p_variable->alarm.enabled) {
// check if timeout was reached
if(_p_variable->alarm.timeout_counter > _p_variable->alarm.timeout) {
// perform the alarm action
_p_variable->alarm.callback();
// increment the fire counter
_p_variable->alarm.firecount += 1;
// avoid wrapping on increment (not very elegant...)
if(_p_variable->alarm.firecount == 0) {
_p_variable->alarm.firecount -= 1;
}
// reset alarm timeout counter
_p_variable->alarm.timeout_counter = 0;
}
}
}
}
......@@ -30,8 +30,36 @@ void alarms_enable(void);
void manage_incoming_data(void);
void _delay_seconds(uint8_t _seconds);
void main(void) {
return;
void main(void) {
// initialize the MCU
init_mcu();
// initialize the communication channel
init_comm();
// initialize the ADC reader
init_adcreader();
// start the interrupt system
start_irqs();
// acquire sensor offsets
sensors_acquire_offsets();
// enable alarms
alarms_enable();
// enter main loop
while(1) {
// manage auto-rescue
rescue_check();
// manage incoming communication data
manage_incoming_data();
}
}
void init_mcu(void) {
......@@ -76,17 +104,14 @@ void sensors_acquire_offsets(void) {
// Note that the ADC is 10 bit, but data are left aligned to 16 bit, we thus
// have an headroom of 6 bits = 32 counts.
// Note also that stored offset values are initialized to zero.
userpin_SWITCH_LED_D10_set_high();
userpin_SWITCH_LED_D9_set_high();
_delay_seconds(5); // wait 5 seconds to make signals steady
userpin_SWITCH_LED_D9_set_low();
_delay_seconds(5); // wait 5 seconds to make signals steady
for(uint8_t k = 0; k < 32; k++) {
__delay_ms(30); // 2.2 * 30 millis are (much) more than sufficient for ADC reader to acquire from all ADC channels
for(uint8_t varidx = 0; varidx < ANALOG_VARIABLES_COUNT; varidx++) {
analog_variables[varidx].offset += analog_variables[varidx].value/32;
}
}
userpin_SWITCH_LED_D10_set_low();
}
void alarms_enable(void) {
......@@ -97,12 +122,12 @@ void alarms_enable(void) {
void manage_incoming_data(void) {
// check if a new byte is present in serial port receive buffer
if(serialport_dataready) {
if(serialport_dataready) {
// a byte is present, read it and parse it
communication_parser_appendbyte(serialport_read());
}
// check if a complete command was received
if(communication_parser_commandready()) {
if(communication_parser_commandready()) {
// a new complete command was received
// execute the received command and verify result
err_t retval = communication_parser_executecommand();
......@@ -112,14 +137,14 @@ void manage_incoming_data(void) {
// (this function is called after the command was executed to be able to read its value)
timing_reset_seconds_since_last_command();
// send the response to the host
// (it was created by the [...]_executecommand function)
// (it was created by the [...]_executecommand function)
communication_transmitter_sendresponse();
}else{
// do nothing
error_notify(retval);
}
// reset the parser
communication_parser_reset();
communication_parser_reset();
}
}
......
......@@ -53,8 +53,8 @@
Section: Macro Declarations
*/
#define EUSART1_TX_BUFFER_SIZE 8
#define EUSART1_RX_BUFFER_SIZE 8
#define EUSART1_TX_BUFFER_SIZE 64
#define EUSART1_RX_BUFFER_SIZE 64
/**
Section: Global Variables
......
......@@ -54,8 +54,8 @@ void SYSTEM_Initialize(void)
PIN_MANAGER_Initialize();
OSCILLATOR_Initialize();
FVR_Initialize();
CCP5_Initialize();
ADC_Initialize();
CCP5_Initialize();
TMR2_Initialize();
TMR0_Initialize();
EUSART1_Initialize();
......
......@@ -56,8 +56,8 @@
#include "fvr.h"
#include "ccp5.h"
#include "tmr2.h"
#include "tmr0.h"
#include "adc.h"
#include "tmr0.h"
#include "eusart1.h"
......
......@@ -67,7 +67,7 @@ void PIN_MANAGER_Initialize(void)
TRISx registers
*/
TRISE = 0x07;
TRISA = 0x7F;
TRISA = 0x3F;
TRISB = 0xFF;
TRISC = 0xBF;
TRISD = 0x00;
......
......@@ -71,12 +71,12 @@ void TMR0_Initialize(void)
// TMR0H 0;
TMR0H = 0x00;
// TMR0L 106;
TMR0L = 0x6A;
// TMR0L 56;
TMR0L = 0x38;
// Load TMR0 value to the 8-bit reload variable
timer0ReloadVal = 106;
timer0ReloadVal = 56;
// Clear Interrupt flag before enabling the interrupt
INTCONbits.TMR0IF = 0;
......@@ -87,8 +87,8 @@ void TMR0_Initialize(void)
// Set Default Interrupt Handler
TMR0_SetInterruptHandler(TMR0_DefaultInterruptHandler);
// T0PS 1:32; T08BIT 8-bit; T0SE Increment_hi_lo; T0CS FOSC/4; TMR0ON enabled; PSA assigned;
T0CON = 0xD4;
// T0PS 1:64; T08BIT 8-bit; T0SE Increment_hi_lo; T0CS FOSC/4; TMR0ON enabled; PSA assigned;
T0CON = 0xD5;
}
void TMR0_StartTimer(void)
......
......@@ -65,8 +65,8 @@ void TMR2_Initialize(void)
{
// Set TMR2 to the options selected in the User Interface
// PR2 159;
PR2 = 0x9F;
// PR2 199;
PR2 = 0xC7;
// TMR2 0;
TMR2 = 0x00;
......@@ -80,8 +80,8 @@ void TMR2_Initialize(void)
// Set Default Interrupt Handler
TMR2_SetInterruptHandler(TMR2_DefaultInterruptHandler);
// T2CKPS 1:4; T2OUTPS 1:2; TMR2ON on;
T2CON = 0x0D;
// T2CKPS 1:4; T2OUTPS 1:4; TMR2ON on;
T2CON = 0x1D;
}
void TMR2_StartTimer(void)
......
......@@ -130,9 +130,9 @@
<targetDevice>PIC18F46K22</targetDevice>
<targetHeader></targetHeader>
<targetPluginBoard></targetPluginBoard>
<platformTool></platformTool>
<platformTool>noID</platformTool>
<languageToolchain>XC8</languageToolchain>
<languageToolchainVersion>2.00</languageToolchainVersion>
<languageToolchainVersion>2.20</languageToolchainVersion>
<platform>2</platform>
</toolsSet>
<packs>
......@@ -189,9 +189,9 @@
<property key="optimization-level" value="-O0"/>
<property key="optimization-speed" value="false"/>
<property key="optimization-stable-enable" value="false"/>
<property key="pack-struct" value="true"/>
<property key="preprocess-assembler" value="true"/>
<property key="short-enums" value="true"/>
<property key="tentative-definitions" value=""/>
<property key="undefine-macros" value=""/>
<property key="use-cci" value="false"/>
<property key="use-iar" value="false"/>
......@@ -247,6 +247,7 @@
</HI-TECH-LINK>
<XC8-CO>
<property key="coverage-enable" value=""/>
<property key="stack-guidance" value="false"/>
</XC8-CO>
<XC8-config-global>
<property key="advanced-elf" value="true"/>
......@@ -261,6 +262,7 @@
<property key="stack-size-main" value="auto"/>
<property key="stack-type" value="compiled"/>
<property key="user-pack-device-support" value=""/>
<property key="wpo-lto" value="false"/>
</XC8-config-global>
</conf>
<conf name="ARCA" type="2">
......@@ -269,7 +271,7 @@
<targetDevice>PIC18F46K22</targetDevice>
<targetHeader></targetHeader>
<targetPluginBoard></targetPluginBoard>
<platformTool></platformTool>
<platformTool>ICD3PlatformTool</platformTool>
<languageToolchain>XC8</languageToolchain>
<languageToolchainVersion>2.20</languageToolchainVersion>
<platform>2</platform>
......@@ -318,7 +320,7 @@
<property key="garbage-collect-functions" value="true"/>
<property key="identifier-length" value="255"/>
<property key="local-generation" value="false"/>
<property key="operation-mode" value="free"/>
<property key="operation-mode" value="pro"/>
<property key="opt-xc8-compiler-strict_ansi" value="false"/>
<property key="optimization-assembler" value="true"/>
<property key="optimization-assembler-files" value="true"/>
......@@ -326,11 +328,11 @@
<property key="optimization-invariant-enable" value="false"/>
<property key="optimization-invariant-value" value="16"/>
<property key="optimization-level" value="-O0"/>
<property key="optimization-speed" value="false"/>
<property key="optimization-speed" value="true"/>
<property key="optimization-stable-enable" value="false"/>
<property key="pack-struct" value="true"/>
<property key="preprocess-assembler" value="true"/>
<property key="short-enums" value="true"/>
<property key="tentative-definitions" value=""/>
<property key="undefine-macros" value=""/>
<property key="use-cci" value="false"/>
<property key="use-iar" value="false"/>
......@@ -384,8 +386,87 @@
<property key="program-the-device-with-default-config-words" value="true"/>
<property key="remove-unused-sections" value="true"/>
</HI-TECH-LINK>
<ICD3PlatformTool>
<property key="AutoSelectMemRanges" value="auto"/>
<property key="Freeze Peripherals" value="true"/>
<property key="SecureSegment.SegmentProgramming" value="FullChipProgramming"/>
<property key="ToolFirmwareFilePath"
value="Press to browse for a specific firmware version"/>
<property key="ToolFirmwareOption.UseLatestFirmware" value="true"/>
<property key="debugoptions.useswbreakpoints" value="false"/>
<property key="hwtoolclock.frcindebug" value="false"/>
<property key="memories.aux" value="false"/>
<property key="memories.bootflash" value="false"/>
<property key="memories.configurationmemory" value="true"/>
<property key="memories.configurationmemory2" value="true"/>
<property key="memories.dataflash" value="true"/>
<property key="memories.eeprom" value="true"/>
<property key="memories.flashdata" value="true"/>
<property key="memories.id" value="true"/>
<property key="memories.instruction.ram" value="true"/>
<property key="memories.instruction.ram.ranges"
value="${memories.instruction.ram.ranges}"/>
<property key="memories.programmemory" value="true"/>
<property key="memories.programmemory.ranges" value="0-ffff"/>
<property key="poweroptions.powerenable" value="false"/>
<property key="programoptions.donoteraseauxmem" value="false"/>
<property key="programoptions.eraseb4program" value="true"/>
<property key="programoptions.preservedataflash" value="false"/>
<property key="programoptions.preservedataflash.ranges" value=""/>
<property key="programoptions.preserveeeprom" value="false"/>
<property key="programoptions.preserveeeprom.ranges" value="0-3ff"/>
<property key="programoptions.preserveprogram.ranges" value=""/>
<property key="programoptions.preserveprogramrange" value="false"/>
<property key="programoptions.preserveuserid" value="false"/>
<property key="programoptions.programcalmem" value="false"/>
<property key="programoptions.programuserotp" value="false"/>
<property key="programoptions.testmodeentrymethod" value="VDDFirst"/>
<property key="programoptions.usehighvoltageonmclr" value="false"/>
<property key="programoptions.uselvpprogramming" value="false"/>
<property key="voltagevalue" value="5.0"/>
</ICD3PlatformTool>
<Tool>
<property key="AutoSelectMemRanges" value="auto"/>
<property key="Freeze Peripherals" value="true"/>
<property key="SecureSegment.SegmentProgramming" value="FullChipProgramming"/>
<property key="ToolFirmwareFilePath"
value="Press to browse for a specific firmware version"/>
<property key="ToolFirmwareOption.UseLatestFirmware" value="true"/>
<property key="debugoptions.useswbreakpoints" value="false"/>
<property key="hwtoolclock.frcindebug" value="false"/>
<property key="memories.aux" value="false"/>
<property key="memories.bootflash" value="false"/>
<property key="memories.configurationmemory" value="true"/>
<property key="memories.configurationmemory2" value="true"/>
<property key="memories.dataflash" value="true"/>
<property key="memories.eeprom" value="true"/>
<property key="memories.flashdata" value="true"/>
<property key="memories.id" value="true"/>
<property key="memories.instruction.ram" value="true"/>
<property key="memories.instruction.ram.ranges"
value="${memories.instruction.ram.ranges}"/>
<property key="memories.programmemory" value="true"/>
<property key="memories.programmemory.ranges" value="0-ffff"/>
<property key="poweroptions.powerenable" value="false"/>
<property key="programoptions.donoteraseauxmem" value="false"/>
<property key="programoptions.eraseb4program" value="true"/>
<property key="programoptions.preservedataflash" value="false"/>
<property key="programoptions.preservedataflash.ranges" value=""/>
<property key="programoptions.preserveeeprom" value="false"/>
<property key="programoptions.preserveeeprom.ranges" value="0-3ff"/>
<property key="programoptions.preserveprogram.ranges" value=""/>
<property key="programoptions.preserveprogramrange" value="false"/>
<property key="programoptions.preserveuserid" value="false"/>
<property key="programoptions.programcalmem" value="false"/>
<property key="programoptions.programuserotp" value="false"/>
<property key="programoptions.testmodeentrymethod" value="VDDFirst"/>
<property key="programoptions.usehighvoltageonmclr" value="false"/>
<property key="programoptions.uselvpprogramming" value="false"/>
<property key="voltagevalue" value="5.0"/>
</Tool>
<XC8-CO>
<property key="coverage-enable" value=""/>
<property key="stack-guidance" value="false"/>
</XC8-CO>
<XC8-config-global>
<property key="advanced-elf" value="true"/>
......@@ -400,6 +481,7 @@
<property key="stack-size-main" value="auto"/>
<property key="stack-type" value="compiled"/>
<property key="user-pack-device-support" value=""/>
<property key="wpo-lto" value="false"/>
</XC8-config-global>
</conf>
</confs>
......
......@@ -68,12 +68,7 @@ void timing_tick_irq(void) {
// increment microsecond counter by the number of microseconds per tick
_tick_counter_us += IRQ_TIMER_SLOW_PERIOD_US;
// check if a second has elapsed
if(_tick_counter_us >= 1000000) {
// TODO remove user pin activity
if(userpin_SWITCH_LED_D9_get() == PINSTATE_HIGH) userpin_SWITCH_LED_D9_set_low();
else userpin_SWITCH_LED_D9_set_high();
if(_tick_counter_us >= 1000000) {
// ONE second elapsed!
// increment number of seconds since last received command
_timing_seconds_lc += 1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment