diff --git a/src/LedController/LedController.c b/src/LedController/LedController.c index b14c181..b7900f3 100644 --- a/src/LedController/LedController.c +++ b/src/LedController/LedController.c @@ -1,11 +1,10 @@ /* - * Author: Jake G - * Date: 2024 + * Author: Jake G + * Date: 2024 * filename: LedController.c - * description: Abstract LED interface and control. + * description: Abstract LED interface and control. */ - #ifndef __AVR_ATtiny404__ #define __AVR_ATtiny404__ #endif @@ -13,75 +12,71 @@ #include "LedController.h" #include "avr/io.h" -#define PA_B1 (1<<2) -#define PA_B2 (1<<3) -#define PA_B3 (1<<6) -#define PA_B4 (1<<7) +#define PA_B1 (1 << 2) +#define PA_B2 (1 << 3) +#define PA_B3 (1 << 6) +#define PA_B4 (1 << 7) -#define BITS_IN_BYTE (1<<2)|(1<<3)|(1<<6)|(1<<7) +#define BITS_IN_BYTE (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7) -#define HALF_BYTE_BM +#define HALF_BYTE_BM -typedef struct LedController { - void *port; - uint8_t pins[BITS_IN_BYTE]; -}LedController; - -static LedController controller; +//static LedController controller; void LedControler_SetPortADefault(void); - /* * Uses pins: 9, 8, 12, 13 */ void LedControler_SetPortBDefault(void) { - //controller.port = &PORTA; + // controller.port = &PORTA; } -void LedController_SetLedBitNum(void * port, uint8_t pin, uint8_t bit); +void LedController_SetLedBitNum(void *port, uint8_t pin, uint8_t bit); void LedController_ShowByte(uint8_t byte); - void LedControler_ShowHalfByte(uint8_t byte) { byte = byte & 0x0F; - //PORTA.DIR |= (1<<2)|(1<<3)|(1<<6)|(1<<7); + // PORTA.DIR |= (1<<2)|(1<<3)|(1<<6)|(1<<7); PORTA.DIR |= PA_B1; PORTA.DIR |= PA_B2; PORTA.DIR |= PA_B3; PORTA.DIR |= PA_B4; - //PORTA.DIR |= HALF_BYTE_BM; + // PORTA.DIR |= HALF_BYTE_BM; // 15 = 0b1111 - //PORTA.OUT |= (byte & 0x0F); + // PORTA.OUT |= (byte & 0x0F); - if(byte & 0x01){ + if (byte & 0x01) + { PORTA.OUT |= PA_B1; } - if(byte & 0x02){ + if (byte & 0x02) + { PORTA.OUT |= PA_B2; } - if(byte & 0x04){ + if (byte & 0x04) + { PORTA.OUT |= PA_B3; } - if(byte & 0x08){ + if (byte & 0x08) + { PORTA.OUT |= PA_B4; } } - void LedControler_ClearHalfByte(void) { - //PORTA.OUT &= HALF_BYTE_BM; - //PORTA.OUT &= ~((1<<2)|(1<<3)|(1<<6)|(1<<7)); + // PORTA.OUT &= HALF_BYTE_BM; + // PORTA.OUT &= ~((1<<2)|(1<<3)|(1<<6)|(1<<7)); PORTA.OUT &= ~PA_B1; PORTA.OUT &= ~PA_B2; PORTA.OUT &= ~PA_B3; PORTA.OUT &= ~PA_B4; - //PORTA.OUT = 0x00; + // PORTA.OUT = 0x00; } diff --git a/src/LedController/LedController.h b/src/LedController/LedController.h index 0107a46..36ccf0c 100644 --- a/src/LedController/LedController.h +++ b/src/LedController/LedController.h @@ -1,8 +1,8 @@ /** - * @brief Led Controller module - * @details This file outputs a byte of data to the pins for led indication. - * @author Jake G - * @date 2024-08-21 + * @brief Led Controller module + * @details This file outputs a byte of data to the pins for led indication. + * @author Jake G + * @date 2024-08-21 * @copyright None * @file LEDCONTROLLER.h */ @@ -10,8 +10,20 @@ #ifndef LEDCONTROLLER #define LEDCONTROLLER - #include +#include "stdbool.h" + + +/** + * A structure representing an LED + */ +typedef struct Led +{ + uint8_t *port; + uint8_t pin_num; + bool state; +}Led; + /** * Sets the default PORTB pins for output. @@ -23,30 +35,27 @@ void LedControler_SetPortADefault(void); */ void LedControler_SetPortBDefault(void); - /** * Allows the setting or changing of which pins represent which bits. * @param port The address of the port. * @param pin The pin number for the port. * @param bit The bit that the pin should represent. */ -void LedController_SetLedBitNum(void * port, uint8_t pin, uint8_t bit); +void LedController_SetLedBitNum(void *port, uint8_t pin, uint8_t bit); /** * Displays the byte of data via led pins. */ void LedController_ShowByte(uint8_t byte); - /** * Displays the the first 4 bits of a byte. */ void LedControler_ShowHalfByte(uint8_t byte); - /** * Clears out the halfbyte led representation */ void LedControler_ClearHalfByte(void); -#endif //LEDCONTROLLER +#endif // LEDCONTROLLER diff --git a/src/main.c b/src/main.c index 8987f0f..cc09d54 100644 --- a/src/main.c +++ b/src/main.c @@ -15,36 +15,34 @@ // This can prevent issues with utils/delay.h library with the gcc toolchain #define __DELAY_BACKWARD_COMPATIBLE__ -#include "RegEdit.h" #include "LedController.h" +#include "RegEdit.h" #include "config.h" #include "timer.h" #include /* Required header file */ #include #include -#define SW1PIN (1<<5) -#define SW2PIN (1<<4) -#define RELAYPIN (1<<1) -#define RELAYREADINGPIN (1<<2) //It would be better to use PA7 so USART worked +#define SW1PIN (1 << 5) +#define SW2PIN (1 << 4) +#define RELAYPIN (1 << 1) +#define RELAYREADINGPIN (1 << 2) // It would be better to use PA7 so USART worked - -//Set the function pointer for the delay func +// Set the function pointer for the delay func void (*Delay_MicroSeconds)(double us) = _delay_us; void SW1_Wait(void) -{ - //poll the input. - while(PORTA.IN & SW1PIN){} +{ + // poll the input. + while (PORTA.IN & SW1PIN) {} } void SW2_Wait(void) -{ - //poll the input. - while(PORTA.IN & SW2PIN){} +{ + // poll the input. + while (PORTA.IN & SW2PIN) {} } - void Activate_Relay(void) { PORTA.OUT |= RELAYPIN; @@ -55,18 +53,22 @@ void Deactivate_Relay(void) PORTA.OUT &= ~RELAYPIN; } - void WaitForRelayConnect(void) { - while(!(PORTB.IN & RELAYREADINGPIN)){;} + while (!(PORTB.IN & RELAYREADINGPIN)) + { + ; + } } void WaitForRelayDisconnect(void) { - while(PORTB.IN & RELAYREADINGPIN){;} + while (PORTB.IN & RELAYREADINGPIN) + { + ; + } } - int main(int argc, char **argv) { PORTA.DIR |= RELAYPIN; @@ -74,30 +76,30 @@ int main(int argc, char **argv) PORTA.DIR &= ~SW1PIN; PORTA.DIR &= ~SW2PIN; - - uint16_t make_time = 0; + uint16_t make_time = 0; uint16_t break_time = 0; - uint8_t temp = 0; + uint8_t temp = 0; - while(true) { + while (true) + { SW1_Wait(); Activate_Relay(); Timer_Start(); WaitForRelayConnect(); - Timer_Disable(); + Timer_Disable(); make_time = Timer_GetOverflowCount(); - //Output the Make time via LEDS - //temp = (uint8_t)(make_time & 0x0F); + // Output the Make time via LEDS + // temp = (uint8_t)(make_time & 0x0F); temp = 0x01; LedControler_ShowHalfByte(temp); _delay_ms(1000); LedControler_ClearHalfByte(); - //temp = (uint8_t)((make_time & 0xF0)>>4); + // temp = (uint8_t)((make_time & 0xF0)>>4); temp = 0x02; LedControler_ShowHalfByte(temp); @@ -114,15 +116,15 @@ int main(int argc, char **argv) break_time = Timer_GetOverflowCount(); - //Output the Break time via LEDS - //temp = (uint8_t)(break_time & 0x0F); + // Output the Break time via LEDS + // temp = (uint8_t)(break_time & 0x0F); temp = 0x04; LedControler_ShowHalfByte(temp); _delay_ms(1000); LedControler_ClearHalfByte(); - //temp = (uint8_t)((break_time & 0xF0)>>4); + // temp = (uint8_t)((break_time & 0xF0)>>4); temp = 0x08; LedControler_ShowHalfByte(temp); @@ -130,13 +132,11 @@ int main(int argc, char **argv) LedControler_ClearHalfByte(); - - /* USART0_sendString(maketime_msg); - USART0_sendChar((uint8_t)(0xFF & make_time)); + USART0_sendChar((uint8_t)(0xFF & make_time)); USART0_sendString(breaktime_msg); - USART0_sendChar((uint8_t)(0xFF & break_time)); + USART0_sendChar((uint8_t)(0xFF & break_time)); */ } } diff --git a/src/timer/timer.c b/src/timer/timer.c index f2a16e0..29e45d0 100644 --- a/src/timer/timer.c +++ b/src/timer/timer.c @@ -13,68 +13,65 @@ #endif #include "timer.h" -#include #include +#include #define FCLK_PER 3333333UL #define DIV8 0x3 -#define PERIOD_VALUE 40 +#define PERIOD_VALUE 40 -//These are expiremential found values that account for overhead -//for smaller durations. +// These are expiremential found values that account for overhead +// for smaller durations. #define OVERHEAD_ONE 226 #define OVERHEAD_TWO 151 #define OVERHEAD_THREE 75 - static uint16_t overflow_count = 0; - -uint16_t Timer_GetOverflowCount(void) +uint16_t Timer_GetOverflowCount(void) { return overflow_count; } void Timer_Start(void) { - //clear the overflow event count + // clear the overflow event count overflow_count = 0; sei(); - //Enable the overflow Interrupt. + // Enable the overflow Interrupt. TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm; - //set Normal mode. + // set Normal mode. TCA0.SINGLE.CTRLB = TCA_SINGLE_WGMODE_NORMAL_gc; - //Disable event counting. + // Disable event counting. TCA0.SINGLE.EVCTRL &= ~(TCA_SINGLE_CNTEI_bm); - //Set the Period Value + // Set the Period Value TCA0.SINGLE.PER = PERIOD_VALUE; - //set the Timer to divide FCLK_PER by 8. - TCA0.SINGLE.CTRLA |= (DIV8<<1); - - //Enable the Timer + // set the Timer to divide FCLK_PER by 8. + TCA0.SINGLE.CTRLA |= (DIV8 << 1); + + // Enable the Timer TCA0.SINGLE.CTRLA |= TCA_SINGLE_ENABLE_bm; } - void Timer_Disable(void) { cli(); - TCA0.SINGLE.CTRLA &= ~(1<<0); - TCA0.SINGLE.CTRLESET |= ((0x3)<<2); + TCA0.SINGLE.CTRLA &= ~(1 << 0); + TCA0.SINGLE.CTRLESET |= ((0x3) << 2); } -//Triggered on the overflow of the timer A's counter. +// Triggered on the overflow of the timer A's counter. ISR(TCA0_OVF_vect) { cli(); - //Increment the Overflow counter. + // Increment the Overflow counter. overflow_count += 1; // The interrupt flag has to be cleared manually diff --git a/src/timer/timer.h b/src/timer/timer.h index b65eccd..9899818 100644 --- a/src/timer/timer.h +++ b/src/timer/timer.h @@ -10,18 +10,17 @@ #ifndef TIMER #define TIMER -#include "stdbool.h" #include "inttypes.h" +#include "stdbool.h" /** * * @param a The first argument */ +void Timer_Start(void); -void Timer_Start(void); - -void Timer_Disable(void); +void Timer_Disable(void); uint16_t Timer_GetOverflowCount(void); diff --git a/src/usart/usart.c b/src/usart/usart.c index 264e5e2..6c19f81 100644 --- a/src/usart/usart.c +++ b/src/usart/usart.c @@ -5,7 +5,6 @@ * description: module_purpose */ - #ifndef __AVR_ATtiny404__ #define __AVR_ATtiny404__ #endif @@ -14,51 +13,48 @@ #include #include -//These are technically correct, but the libc requires F_CPU for delay stuff -//that doesn't match the actual F_CPU because it uses the value after the -//clock is divided. - -//#define F_CPU 3333333UL -//#define F_PER F_CPU / 6 +// These are technically correct, but the libc requires F_CPU for delay stuff +// that doesn't match the actual F_CPU because it uses the value after the +// clock is divided. +// #define F_CPU 3333333UL +// #define F_PER F_CPU / 6 #define F_PER 3333333UL #define USART0_BAUD_RATE(BAUD_RATE) ((float)(F_PER * 64 / (16 * (float)BAUD_RATE)) + 0.5) - -//RX PIN6, TX PIN7 -//ALT: RX PIN12 TX PIN11 +// RX PIN6, TX PIN7 +// ALT: RX PIN12 TX PIN11 void USART0_Init(void) { - //Config TxD as output, and rx as input? - PORTB.DIR &= ~(1<<3); - PORTB.DIR |= (1<<2); + // Config TxD as output, and rx as input? + PORTB.DIR &= ~(1 << 3); + PORTB.DIR |= (1 << 2); - //It says to set the TX pin high? - //PORTB.OUT |= (1<<2); + // It says to set the TX pin high? + // PORTB.OUT |= (1<<2); - //set buad rate. + // set buad rate. USART0.BAUD = (uint16_t)USART0_BAUD_RATE(9600); - //USART0.BAUD = 1388; + // USART0.BAUD = 1388; - //set the frame format. - USART0.CTRLC = 0x3; //setting 8-bit mode. + // set the frame format. + USART0.CTRLC = 0x3; // setting 8-bit mode. - //Enable transmitter and receiver (USARTn.CTRLB) - //USART0.CTRLB |= (1<<7)|(1<<6); - USART0.CTRLB |= USART_TXEN_bm; + // Enable transmitter and receiver (USARTn.CTRLB) + // USART0.CTRLB |= (1<<7)|(1<<6); + USART0.CTRLB |= USART_TXEN_bm; } void USART0_Alt_Init(void) { - - //setup Alternate pints on PA1 and PA2 + // setup Alternate pints on PA1 and PA2 PORTMUX.CTRLB |= PORTMUX_USART0_ALTERNATE_gc; - PORTA.DIR |= (1<<1); - PORTA.DIR &= ~(1<<2); + PORTA.DIR |= (1 << 1); + PORTA.DIR &= ~(1 << 2); - //It says to set the TX pin high? - //PORTA.OUT |= (1<<11); + // It says to set the TX pin high? + // PORTA.OUT |= (1<<11); // set buad rate. USART0.BAUD = (uint16_t)USART0_BAUD_RATE(9600); diff --git a/src/usart/usart.h b/src/usart/usart.h index 5d34546..1670450 100644 --- a/src/usart/usart.h +++ b/src/usart/usart.h @@ -15,9 +15,8 @@ */ void USART0_Init(void); - /** - * @brief Initializes the USART0 peripheral on Alternate pins. + * @brief Initializes the USART0 peripheral on Alternate pins. */ void USART0_Alt_Init(void);