diff --git a/src/ADC/ADC.c b/src/ADC/ADC.c new file mode 100644 index 0000000..364251c --- /dev/null +++ b/src/ADC/ADC.c @@ -0,0 +1,77 @@ +/* + * Author: username + * Date: 2024 + * filename: ADC.c + * description: module_purpose + */ + +#include "ADC.h" +#include "RegEdit.h" +#include "ch32fun.h" + +#define MAX_PIN_NUM 7 + +static bool IsInvalidPin(uint8_t pin_num) +{ + if (pin_num > MAX_PIN_NUM) + { + return true; + } + return false; +} + +void ADC_Setup(void) +{ +} + +void ADC_Init(uint8_t pin_num) +{ + if (IsInvalidPin(pin_num)) + { + return; + } +} + +void ADC_Enable(void) +{ + // Set the enable bit in the CTRLA register + // RegEdit_SetBit((void *)&ADC0.CTRLA, 0); +} + +void ADC_Disable(void) +{ + // 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); +} + +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)) + //{ + // ; + // } + + /* 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; + uint16_t adc_val = 0; + return adc_val; +} + +// 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 new file mode 100644 index 0000000..896c8ff --- /dev/null +++ b/src/ADC/ADC.h @@ -0,0 +1,68 @@ +/** + * @brief Interface to the AVR ADC hardware. + * @details This file is... + * @author Jake G + * @date 2024 + * @copyright None + * @file ADC.h + */ + +#ifndef ADC_H +#define ADC_H + +#include +#include + +/** + * @brief Initializes the AVR hardware in order to accept + * Input for ADC usage. + * @param pin_num The number of the pin 0-7 you are initializing. + * + * This function only makes use of PORTA by default. It sets the direction + * register to input, disables the pull-up resistor and also diables interrupts + * 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 + */ +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 + * before being called. + */ +extern uint16_t (*ADC_ReadValue)(uint8_t pin_num); + +/** + * @brief Sets up the ADC + * + * This function sets up the ADC to take and accumulate 32 samples. It also + * sets the inital delay to 32 ADC clock cycles, and sets the VREF to VDD or + * VCC. + * + * This function should only need to be called once. + */ +void ADC_Setup(void); + +/** + * @brief Sets the pin used in the MUX for ADC0. + * + * @param pin_num The number of the pin in Port A. + */ +void ADC_SetPin(uint8_t pin_num); + +#endif // ADC_H diff --git a/src/ADC/CMakeLists.txt b/src/ADC/CMakeLists.txt new file mode 100644 index 0000000..3b39d8c --- /dev/null +++ b/src/ADC/CMakeLists.txt @@ -0,0 +1,17 @@ +add_library(ADC STATIC + ADC.c +) +target_include_directories(ADC PUBLIC + ${CMAKE_CURRENT_LIST_DIR} +) + +if(UNIT_TESTING) + target_link_libraries(ADC + MockRegEdit + ) + +else() + target_link_libraries(ADC + RegEdit + ) +endif() diff --git a/src/RegEdit/CMakeLists.txt b/src/RegEdit/CMakeLists.txt new file mode 100644 index 0000000..8baf7a3 --- /dev/null +++ b/src/RegEdit/CMakeLists.txt @@ -0,0 +1,7 @@ +add_library(RegEdit STATIC + RegEdit.c +) + +target_include_directories(RegEdit PUBLIC + ${CMAKE_CURRENT_LIST_DIR} +) diff --git a/src/RegEdit/RegEdit.c b/src/RegEdit/RegEdit.c new file mode 100644 index 0000000..6042841 --- /dev/null +++ b/src/RegEdit/RegEdit.c @@ -0,0 +1,62 @@ +/* + * Author: username + * Date: 2024 + * filename: RegEdit.c + * description: module_purpose + */ + +#include "RegEdit.h" + +void RegEdit_SetRegister(void *reg) +{ + uint8_t *reg_ptr = (uint8_t *)reg; + *reg_ptr = 0xFF; +} + +void RegEdit_ClearRegister(void *reg) +{ + uint8_t *reg_ptr = (uint8_t *)reg; + *reg_ptr = 0x00; +} + +void RegEdit_SetBit(void *reg, uint8_t bit_num) +{ + uint8_t *reg_ptr = (uint8_t *)reg; + *reg_ptr |= (uint8_t)(1 << bit_num); +} + +void RegEdit_ClearBit(void *reg, uint8_t bit_num) +{ + uint8_t *reg_ptr = (uint8_t *)reg; + *reg_ptr &= ~(1 << bit_num); +} + +bool RegEdit_IsBitSet(void *reg, uint8_t bit_num) +{ + uint8_t *reg_ptr = (uint8_t *)reg; + return *reg_ptr & (1 << bit_num); +} + +void RegEdit_OR_Num(void *reg, uint8_t num) +{ + uint8_t *reg_ptr = (uint8_t *)reg; + *reg_ptr |= num; +} + +void RegEdit_AND_Num(void *reg, uint8_t num) +{ + uint8_t *reg_ptr = (uint8_t *)reg; + *reg_ptr &= num; +} + +void RegEdit_SetNum(void *reg, uint8_t num) +{ + uint8_t *reg_ptr = (uint8_t *)reg; + *reg_ptr = num; +} + +uint8_t RegEdit_ReadReg(void *reg) +{ + uint8_t *reg_ptr = (uint8_t *)reg; + return *reg_ptr; +} diff --git a/src/RegEdit/RegEdit.h b/src/RegEdit/RegEdit.h new file mode 100644 index 0000000..8ad2e91 --- /dev/null +++ b/src/RegEdit/RegEdit.h @@ -0,0 +1,77 @@ +/** + * @brief Register Editing Interface + * @details This file is an abstraction to all the bitwise operations + * @author Jake G + * @date 2024 + * @copyright None + * @file MockRegEdit.h + */ + +#ifndef REGEDIT_H +#define REGEDIT_H + +#include +#include + +/** + * + * @param reg The register address. + */ +void RegEdit_SetRegister(void *reg); + +/** + * + * @param reg The register address. + */ +void RegEdit_ClearRegister(void *reg); + +/** + * + * @param reg The register address. + * @param bit_num The bit location. + */ +void RegEdit_SetBit(void *reg, uint8_t bit_num); + +/** + * + * @param reg The register address. + * @param bit_num The bit location. + */ +void RegEdit_ClearBit(void *reg, uint8_t bit_num); + +/** + * + * @param reg The register address. + * @param bit_num The bit location. + * @return + */ +bool RegEdit_IsBitSet(void *reg, uint8_t bit_num); + +/** + * + * @param reg The register address. + * @param num The bit location. + */ +void RegEdit_OR_Num(void *reg, uint8_t num); + +/** + * + * @param reg The register address. + * @param num The bit location. + */ +void RegEdit_AND_Num(void *reg, uint8_t num); + +/** + * + * @param reg The register address. + * @param num The bit location. + */ +void RegEdit_SetNum(void *reg, uint8_t num); + +/** + * + * @param reg The register address. + */ +uint8_t RegEdit_ReadReg(void *reg); + +#endif // REGEDIT_H