181 lines
5.5 KiB
C
181 lines
5.5 KiB
C
/*
|
|
* MAIN Generated Driver File
|
|
*
|
|
* @file main.c
|
|
*
|
|
* @defgroup main MAIN
|
|
*
|
|
* @brief This is the generated driver implementation file for the MAIN driver.
|
|
*
|
|
* @version MAIN Driver Version 1.0.2
|
|
*
|
|
* @version Package Version: 3.1.2
|
|
*/
|
|
|
|
/*
|
|
© [2025] Microchip Technology Inc. and its subsidiaries.
|
|
|
|
Subject to your compliance with these terms, you may use Microchip
|
|
software and any derivatives exclusively with Microchip products.
|
|
You are responsible for complying with 3rd party license terms
|
|
applicable to your use of 3rd party software (including open source
|
|
software) that may accompany Microchip software. SOFTWARE IS ?AS IS.?
|
|
NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS
|
|
SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT,
|
|
MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
|
|
WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
|
|
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY
|
|
KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF
|
|
MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE
|
|
FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP?S
|
|
TOTAL LIABILITY ON ALL CLAIMS RELATED TO THE SOFTWARE WILL NOT
|
|
EXCEED AMOUNT OF FEES, IF ANY, YOU PAID DIRECTLY TO MICROCHIP FOR
|
|
THIS SOFTWARE.
|
|
*/
|
|
#include "mcc_generated_files/system/system.h"
|
|
#include <math.h>
|
|
#include "commons.h"
|
|
#include "proto.h"
|
|
|
|
static uint16_t led1 = 0, led2 = 0, moisture = 0, light = 0;
|
|
static errors_t retcode = SUCCESS;
|
|
static uint8_t in_rx, cmd;
|
|
static bool rx_over = false, update_sensor = false;
|
|
volatile bool tx_timed_out = false;
|
|
|
|
//==============================================================================
|
|
// Private Declarations
|
|
//==============================================================================
|
|
static void init();
|
|
static void read_adc(adc_channel_t channel, uint16_t* p_val);
|
|
static void convert_adc_lux();
|
|
static void show_error();
|
|
|
|
static void tmr_tx_cb();
|
|
static void tmr_sensor_cb();
|
|
|
|
//==============================================================================
|
|
// Public
|
|
//==============================================================================
|
|
/*=****************************************************************************/
|
|
int main(void)
|
|
{
|
|
init();
|
|
|
|
while(1)
|
|
{
|
|
if (RET_FAILURE(retcode))
|
|
show_error();
|
|
|
|
// Update sensor if it's time
|
|
if (update_sensor)
|
|
{
|
|
update_sensor = false;
|
|
#if (HAS_MOISTURE == 1)
|
|
read_adc(channel_ANC0, &moisture);
|
|
#endif
|
|
#if (HAS_LDR == 1)
|
|
read_adc(channel_ANA0, &light);
|
|
#endif
|
|
}
|
|
|
|
retcode = uart_rx_byte(&in_rx);
|
|
if (retcode == SUCCESS)
|
|
{
|
|
rx_over = byte_in(in_rx, &cmd, &led1, &led2);
|
|
if (rx_over)
|
|
{
|
|
switch (cmd)
|
|
{
|
|
case GET_MODEL_CMD_ID:
|
|
// Wait for Master to stop writing
|
|
__delay_ms(DELAY_TO_REPLY);
|
|
retcode = send_model(MY_ID, MODEL);
|
|
break;
|
|
|
|
case GET_CMD_ID:
|
|
// Wait for Master to stop writing
|
|
__delay_ms(DELAY_TO_REPLY);
|
|
retcode = send_data(MY_ID, moisture, light);
|
|
break;
|
|
|
|
case SET_CMD_ID:
|
|
#if (HAS_LED == 1)
|
|
PWM3_LoadDutyValue(led1);
|
|
PWM4_LoadDutyValue(led2);
|
|
__delay_ms(DELAY_TO_REPLY);
|
|
retcode = send_error(MY_ID, SUCCESS);
|
|
#endif
|
|
break;
|
|
|
|
default:
|
|
retcode = UNSUPPORTED_CMD;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
//==============================================================================
|
|
// Private
|
|
//==============================================================================
|
|
/*=****************************************************************************/
|
|
static void init()
|
|
{
|
|
SYSTEM_Initialize();
|
|
INTERRUPT_GlobalInterruptEnable();
|
|
INTERRUPT_PeripheralInterruptEnable();
|
|
|
|
ADC_Initialize();
|
|
|
|
// TMR callback
|
|
TMR_TX_OverflowCallbackRegister(tmr_tx_cb);
|
|
TMR_SENSOR_OverflowCallbackRegister(tmr_sensor_cb);
|
|
|
|
// Initial variables and pins
|
|
RS_MODE_SetLow();
|
|
TMR_TX_Stop();
|
|
tx_timed_out = false;
|
|
|
|
DEBUG_SetHigh();
|
|
__delay_ms(1000);
|
|
DEBUG_SetLow();
|
|
}
|
|
|
|
/*=****************************************************************************/
|
|
static void read_adc(adc_channel_t channel, uint16_t* p_val)
|
|
{
|
|
ADC_SelectChannel(channel);
|
|
ADC_StartConversion();
|
|
while(!ADC_IsConversionDone());
|
|
*p_val = ADC_GetConversionResult();
|
|
}
|
|
|
|
/*=****************************************************************************/
|
|
static void show_error()
|
|
{
|
|
uint16_t time = retcode * 100;
|
|
|
|
while(1)
|
|
{
|
|
DEBUG_Toggle();
|
|
|
|
for (uint16_t i = 0; i < time; ++i)
|
|
__delay_ms(1);
|
|
}
|
|
}
|
|
|
|
/*=****************************************************************************/
|
|
static void tmr_tx_cb()
|
|
{
|
|
tx_timed_out = true;
|
|
}
|
|
|
|
/*=****************************************************************************/
|
|
static void tmr_sensor_cb()
|
|
{
|
|
update_sensor = true;
|
|
} |