From 85d5cebb5a6c5a513cb75da04c3c8a92555d7318 Mon Sep 17 00:00:00 2001 From: jakeg00dwin Date: Mon, 2 Sep 2024 14:08:03 -0700 Subject: [PATCH] Auto formatting appied to the newly added modules. --- src/ADC/ADC.c | 129 ++++++++---------- src/ADC/ADC.h | 24 ++-- src/CMakeLists.txt | 4 - src/load/load.c | 94 ++++++------- src/load/load.h | 8 +- .../zero_cross_detection.c | 61 ++++----- .../zero_cross_detection.h | 21 ++- 7 files changed, 151 insertions(+), 190 deletions(-) diff --git a/src/ADC/ADC.c b/src/ADC/ADC.c index be0ff82..e9329e8 100644 --- a/src/ADC/ADC.c +++ b/src/ADC/ADC.c @@ -1,6 +1,6 @@ /* * Author: username - * Date: 2024 + * Date: 2024 * filename: ADC.c * description: module_purpose */ @@ -15,98 +15,85 @@ #define MAX_PIN_NUM 7 -static bool IsInvalidPin(uint8_t pin_num){ - if(pin_num > MAX_PIN_NUM){ - return true; - } - return false; +static bool IsInvalidPin(uint8_t pin_num) { + if (pin_num > MAX_PIN_NUM) { + return true; + } + return false; } +void ADC_Setup(void) { + // Clears control register A for ADC0 + RegEdit_SetNum((void *)&ADC0.CTRLA, 0x00); -void ADC_Setup(void) -{ - //Clears control register A for ADC0 - RegEdit_SetNum((void *) &ADC0.CTRLA, 0x00); + // Sets The sample accumulation number to 32. + RegEdit_SetNum((void *)&ADC0.CTRLB, 0x5); - //Sets The sample accumulation number to 32. - RegEdit_SetNum((void *) &ADC0.CTRLB, 0x5); + // Sets the voltage reference to VDD or VCC. + RegEdit_SetBit((void *)&ADC0.CTRLC, 4); - //Sets the voltage reference to VDD or VCC. - RegEdit_SetBit((void *) &ADC0.CTRLC, 4); + // Sets the pre-scalar for the adc sample rate. + RegEdit_SetBit((void *)&ADC0.CTRLC, 2); - //Sets the pre-scalar for the adc sample rate. - RegEdit_SetBit((void *) &ADC0.CTRLC, 2); + // Setup an Initalization delay. + RegEdit_OR_Num((void *)&ADC0.CTRLD, (2 << 5)); - //Setup an Initalization delay. - RegEdit_OR_Num((void *) &ADC0.CTRLD, (2<<5)); - - //Set the bit for ADC variation during readings. - RegEdit_SetBit((void *) &ADC0.CTRLD, 4); + // Set the bit for ADC variation during readings. + RegEdit_SetBit((void *)&ADC0.CTRLD, 4); } -void ADC_Init(uint8_t pin_num) -{ - - if(IsInvalidPin(pin_num)){return;} +void ADC_Init(uint8_t pin_num) { + if (IsInvalidPin(pin_num)) { + return; + } - //set the direction to input - RegEdit_ClearBit((void *) &PORTA.DIR, pin_num); + // set the direction to input + RegEdit_ClearBit((void *)&PORTA.DIR, pin_num); - //Disable the pull-up resistor - RegEdit_ClearBit((void *) &PORTA.OUT, pin_num); - - //Disable input buffer - //We do some kinda nasty address addition but it saves - //memory and means we don't need a switch statment. - RegEdit_SetBit( - (void *) (&PORTA.PIN0CTRL)+pin_num, - PORT_ISC_INPUT_DISABLE_gc - ); - + // Disable the pull-up resistor + RegEdit_ClearBit((void *)&PORTA.OUT, pin_num); + + // Disable input buffer + // We do some kinda nasty address addition but it saves + // memory and means we don't need a switch statment. + RegEdit_SetBit((void *)(&PORTA.PIN0CTRL) + pin_num, + PORT_ISC_INPUT_DISABLE_gc); } - -void ADC_Enable(void) -{ - //Set the enable bit in the CTRLA register - RegEdit_SetBit((void *) &ADC0.CTRLA, 0); +void ADC_Enable(void) { + // Set the enable bit in the CTRLA register + RegEdit_SetBit((void *)&ADC0.CTRLA, 0); } - -void ADC_Disable() -{ - //Clear the enable ADC flag - RegEdit_ClearBit((void *) &ADC0.CTRLA, 0); +void ADC_Disable() { + // Clear the enable ADC flag + RegEdit_ClearBit((void *)&ADC0.CTRLA, 0); } - -void ADC_SetPin(uint8_t pin_num) -{ - if(IsInvalidPin(pin_num)){return;} - RegEdit_ClearRegister((void *) &ADC0.MUXPOS); - RegEdit_SetNum((void *) &ADC0.MUXPOS, pin_num); +void ADC_SetPin(uint8_t pin_num) { + if (IsInvalidPin(pin_num)) { + return; + } + RegEdit_ClearRegister((void *)&ADC0.MUXPOS); + RegEdit_SetNum((void *)&ADC0.MUXPOS, pin_num); } +uint16_t ADC_ReadValue_Impl(uint8_t pin_num) { + RegEdit_SetNum((void *)&ADC0.COMMAND, ADC_STCONV_bm); -uint16_t ADC_ReadValue_Impl(uint8_t pin_num) -{ - RegEdit_SetNum((void *) &ADC0.COMMAND, ADC_STCONV_bm); + /* Wait until ADC conversion done */ + while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) { + ; + } - /* Wait until ADC conversion done */ - while ( !(ADC0.INTFLAGS & ADC_RESRDY_bm) ) - { - ; - } - - /* Clear the interrupt flag by writing 1: */ - ADC0.INTFLAGS = ADC_RESRDY_bm; - - uint16_t adc_val = (uint16_t) ADC0.RES; - adc_val = adc_val >> 5; - return adc_val; + /* Clear the interrupt flag by writing 1: */ + ADC0.INTFLAGS = ADC_RESRDY_bm; + + uint16_t adc_val = (uint16_t)ADC0.RES; + adc_val = adc_val >> 5; + return adc_val; } - -//Set the default for the function pointer. +// Set the default for the function pointer. uint16_t (*ADC_ReadValue)(uint8_t pin_num) = ADC_ReadValue_Impl; diff --git a/src/ADC/ADC.h b/src/ADC/ADC.h index 3792eab..896c8ff 100644 --- a/src/ADC/ADC.h +++ b/src/ADC/ADC.h @@ -1,8 +1,8 @@ /** - * @brief Interface to the AVR ADC hardware. + * @brief Interface to the AVR ADC hardware. * @details This file is... - * @author Jake G - * @date 2024 + * @author Jake G + * @date 2024 * @copyright None * @file ADC.h */ @@ -10,10 +10,8 @@ #ifndef ADC_H #define ADC_H -#include #include - - +#include /** * @brief Initializes the AVR hardware in order to accept @@ -25,29 +23,26 @@ * alongside the input buffer(digital). * * This in turn helps reduce noise when using the ADC. - * + * */ void ADC_Init(uint8_t pin_num); - /** - * @brief Enables the ADC + * @brief Enables the ADC */ void ADC_Enable(void); - /** * @brief Disables the ADC */ void ADC_Disable(); - /** * @brief Reads ADC value into variable * * @param pin_num The bin number of the ADC pin being read. * - * This function depends on the ADC already being initialized and enabled + * This function depends on the ADC already being initialized and enabled * before being called. */ extern uint16_t (*ADC_ReadValue)(uint8_t pin_num); @@ -61,8 +56,7 @@ extern uint16_t (*ADC_ReadValue)(uint8_t pin_num); * * This function should only need to be called once. */ -void ADC_Setup(void); - +void ADC_Setup(void); /** * @brief Sets the pin used in the MUX for ADC0. @@ -71,4 +65,4 @@ void ADC_Setup(void); */ void ADC_SetPin(uint8_t pin_num); -#endif //ADC_H +#endif // ADC_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 96e9ac1..8ce8208 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -49,12 +49,8 @@ endif() - - add_subdirectory(RegEdit) -add_subdirectory(usart) add_subdirectory(timer) - add_subdirectory(SuperLoop) add_subdirectory(Relays) add_subdirectory(zero_cross_detection) diff --git a/src/load/load.c b/src/load/load.c index 66787ff..33ebebb 100644 --- a/src/load/load.c +++ b/src/load/load.c @@ -1,6 +1,6 @@ /* - * Author: Jake G - * Date: 2024 + * Author: Jake G + * Date: 2024 * filename: load.c * description: module_purpose */ @@ -9,67 +9,57 @@ #define __AVR_ATtiny404__ #endif - -#include #include "load.h" - +#include #ifndef UNIT_TESTING - #include "ADC.h" - #include "RegEdit.h" +#include "ADC.h" +#include "RegEdit.h" #else - #include "MockADC/MockADC.h" - #include "MockRegEdit.h" +#include "MockADC/MockADC.h" +#include "MockRegEdit.h" #endif - - -//These two arrays allow us to track the A and B ports to know when -//one has been disabled before. +// These two arrays allow us to track the A and B ports to know when +// one has been disabled before. static bool porta_disabled[8] = {0}; static bool portb_disabled[8] = {0}; - -static bool valid_load(uint16_t val) -{ - if(val > 527 && val < 1000) { - return true; - } - return false; +static bool valid_load(uint16_t val) { + if (val > 527 && val < 1000) { + return true; + } + return false; } -void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_pin) -{ - ADC_Init(adc_pin); - ADC_Enable(); - ADC_SetPin(adc_pin); - uint16_t val = ADC_ReadValue(adc_pin); - - ADC_Disable(); - if(valid_load(val) && !porta_disabled[adc_pin]){ - RegEdit_SetBit((void *) &PORTA.DIR, out_pin); - RegEdit_SetBit((void *) &PORTA.OUT, out_pin); - } - else{ - RegEdit_ClearBit((void *) &PORTA.OUT, out_pin); - porta_disabled[adc_pin] = true; - } +void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_pin) { + ADC_Init(adc_pin); + ADC_Enable(); + ADC_SetPin(adc_pin); + uint16_t val = ADC_ReadValue(adc_pin); + + ADC_Disable(); + if (valid_load(val) && !porta_disabled[adc_pin]) { + RegEdit_SetBit((void *)&PORTA.DIR, out_pin); + RegEdit_SetBit((void *)&PORTA.OUT, out_pin); + } else { + RegEdit_ClearBit((void *)&PORTA.OUT, out_pin); + porta_disabled[adc_pin] = true; + } } -void Load_HandleLoadPortB(uint8_t adc_pin, uint8_t out_pin) -{ - ADC_Init(adc_pin); - ADC_Enable(); - ADC_SetPin(adc_pin); - uint16_t val = ADC_ReadValue(adc_pin); - - ADC_Disable(); - if(valid_load(val) && !portb_disabled[adc_pin]){ - RegEdit_SetBit((void *) &PORTB.DIR, out_pin); - RegEdit_SetBit((void *) &PORTB.OUT, out_pin); - } - else{ - RegEdit_ClearBit((void *) &PORTB.OUT, out_pin); - portb_disabled[adc_pin] = true; - } +void Load_HandleLoadPortB(uint8_t adc_pin, uint8_t out_pin) { + ADC_Init(adc_pin); + ADC_Enable(); + ADC_SetPin(adc_pin); + uint16_t val = ADC_ReadValue(adc_pin); + + ADC_Disable(); + if (valid_load(val) && !portb_disabled[adc_pin]) { + RegEdit_SetBit((void *)&PORTB.DIR, out_pin); + RegEdit_SetBit((void *)&PORTB.OUT, out_pin); + } else { + RegEdit_ClearBit((void *)&PORTB.OUT, out_pin); + portb_disabled[adc_pin] = true; + } } diff --git a/src/load/load.h b/src/load/load.h index 1b3112f..0df7398 100644 --- a/src/load/load.h +++ b/src/load/load.h @@ -8,7 +8,9 @@ */ #ifndef LOAD_H -#define LOAD_H +#define LOAD_H + +#include /** * @brief Low Threshold @@ -27,6 +29,4 @@ void Load_HandleLoadPortB(uint8_t adc_pin, uint8_t out_pin); void Load_HandlePinLoads(void); - -#endif /* LOAD_H */ - +#endif /* LOAD_H */ diff --git a/src/zero_cross_detection/zero_cross_detection.c b/src/zero_cross_detection/zero_cross_detection.c index 7b81e1e..1f64fe1 100644 --- a/src/zero_cross_detection/zero_cross_detection.c +++ b/src/zero_cross_detection/zero_cross_detection.c @@ -1,13 +1,13 @@ /* - * Author: Jake G - * Date: 2024 + * Author: Jake G + * Date: 2024 * filename: zero_cross_detection.c - * description: + * description: */ #include "zero_cross_detection.h" -#define TRIGGERVAL 512 +#define TRIGGERVAL 512 #ifndef UNIT_TESTING #include "ADC.h" @@ -17,38 +17,35 @@ #define ZCD_PIN 0x07 -void ZCD_Setup(void) -{ - ADC_Init(ZCD_PIN); - ADC_SetPin(ZCD_PIN); - ADC_Enable(); +void ZCD_Setup(void) { + ADC_Init(ZCD_PIN); + ADC_SetPin(ZCD_PIN); + ADC_Enable(); } -bool ZCD_IsTriggered() -{ - uint16_t first, second; - ZCD_Setup(); - first = ADC_ReadValue(ZCD_PIN); - ADC_Disable(); - - ZCD_Setup(); - second = ADC_ReadValue(ZCD_PIN); - ADC_Disable(); +bool ZCD_IsTriggered() { + uint16_t first, second; + ZCD_Setup(); + first = ADC_ReadValue(ZCD_PIN); + ADC_Disable(); - if(second < TRIGGERVAL){ - return false; - } - if(second < first){ - return false; - } - return true; + ZCD_Setup(); + second = ADC_ReadValue(ZCD_PIN); + ADC_Disable(); + + if (second < TRIGGERVAL) { + return false; + } + if (second < first) { + return false; + } + return true; } -void ZCD_Poll(void) -{ - while(true){ - if(ZCD_IsTriggered()){ - break; - } +void ZCD_Poll(void) { + while (true) { + if (ZCD_IsTriggered()) { + break; } + } } diff --git a/src/zero_cross_detection/zero_cross_detection.h b/src/zero_cross_detection/zero_cross_detection.h index 16bf9af..d0d392c 100644 --- a/src/zero_cross_detection/zero_cross_detection.h +++ b/src/zero_cross_detection/zero_cross_detection.h @@ -2,21 +2,21 @@ * @file zero_cross_detection.h * @author Jake G * @date 16 June 2024 - * @brief File contains the zero cross detection functions + * @brief File contains the zero cross detection functions * * This file holds all the code/functions needed to read the value of the AC * waveform. It uses the trigger value from the `config.h` file to evaluate the * state. * - * This module depends on the ADC.h module to get readings from the ADC + * This module depends on the ADC.h module to get readings from the ADC * hardware. */ #ifndef ZERO_CROSS_DETECTION #define ZERO_CROSS_DETECTION -#include #include +#include /** * @brief Zero Cross Detection Setup @@ -25,25 +25,22 @@ * */ void ZCD_Setup(void); -//int ZCD_Setup(volatile uint8_t *ddrx, uint8_t *port, uint8_t pin); - +// int ZCD_Setup(volatile uint8_t *ddrx, uint8_t *port, uint8_t pin); /** * @brief Checks if ZCD should trigger on value * * The function checks for a positive edge first using the ZCD_IsPositiveEdge - * function. If a positive edge was found, then it takes an addition set of - * samples and averages them; only triggering(returning true) in the case - * that the average is higher than the previous sample. + * function. If a positive edge was found, then it takes an addition set of + * samples and averages them; only triggering(returning true) in the case + * that the average is higher than the previous sample. */ bool ZCD_IsTriggered(void); - /** - * @brief Function blocks execution until ZCD is triggered. + * @brief Function blocks execution until ZCD is triggered. * */ void ZCD_Poll(void); - -#endif //ZERO_CROSS_DETECTION +#endif // ZERO_CROSS_DETECTION