Commit 9e18cc23 authored by Sebastian Neuser's avatar Sebastian Neuser

Implement BatDis driver

parent 2a820d23
Pipeline #663 failed with stages
in 1 second
///\file
///\brief Driver for the battery disable in-/output on the ProgBob board
#include <stdbool.h>
#include <stdint.h>
#include <util/delay.h>
#include "BatDis.h"
#include "LEDs.h"
///\brief Prescaler for BATDIS sampling
static uint32_t counter = 1;
///\brief BATDIS sampling disable state
static bool sampling_disabled = false;
///\brief Samples BATDIS
///\details Configures BATDIS as input, waits a few microseconds, reads the pin's logic level and
/// sets it back to output mode.
///\returns the BATDIS logical level
static inline bool sample_batdis(void)
{
BATDIS_DDR &= ~BATDIS_MASK;
_delay_us(20);
bool level = (BATDIS_PIN & BATDIS_MASK) / BATDIS_MASK;
BATDIS_DDR |= BATDIS_MASK;
return level;
}
void batdis_disable_sampling(const bool disable)
{
sampling_disabled = disable;
if (!sampling_disabled) {
counter = 1;
}
}
void batdis_init(void)
{
BATDIS_DDR |= BATDIS_MASK;
BATDIS_PORT |= BATDIS_MASK;
}
void batdis_task(void)
{
if (sampling_disabled) {
return;
}
--counter;
if (counter > 0) {
return;
}
counter = BATDIS_SCAN_INTERVAL;
bool connected = !sample_batdis();
uint32_t color = connected ? LEDS_COLOR_BOB3_CONNECTED : LEDS_COLOR_BOB3_NOT_CONNECTED;
leds_set_color(LEDS_LED_RIGHT, color);
}
///\file
///\brief Driver for the battery disable in-/output on the ProgBob board
#ifndef BATDIS_H
#define BATDIS_H
#include <stdbool.h>
#include <stdint.h>
#include <avr/io.h>
///\brief Bitmask for BATDIS pin
#define BATDIS_MASK (1 << 7)
///\brief DDR register for BATDIS in-/output
#define BATDIS_DDR DDRC
///\brief PORT register for BATDIS in-/output
#define BATDIS_PIN PINC
///\brief PORT register for BATDIS in-/output
#define BATDIS_PORT PORTC
///\brief BATDIS sample interval in [number of invocations]
///\see batdis_task
#define BATDIS_SCAN_INTERVAL 60000
///\brief En-/disables periodic sampling of BATDIS
///\param disable
/// if `true` sampling is disabled
///\see batdis_task
void batdis_disable_sampling(bool disable);
///\brief Initializes the BATDIS driver
///\details Configures the BATDIS GPIO as output and sets it high
void batdis_init(void);
///\brief Periodically samples BATDIS and indicates if a BOB3 board is connected
///\details Temporarily configures the BATDIS GPIO as input and reads its logic state. If the GPIO
/// is pulled low, the right LED's color is set to indicate that a BOB3 board is connected.
///\see BATDIS_SCAN_INTERVAL
///\see batdis_sampling_enabled
void batdis_task(void);
#endif
......@@ -18,7 +18,7 @@ F_CPU = 16000000
F_USB = $(F_CPU)
OPTIMIZATION = s
TARGET = ProgBob
SRC = $(TARGET).c LEDs.c AVRISPDescriptors.c \
SRC = $(TARGET).c BatDis.c LEDs.c AVRISPDescriptors.c \
Lib/V2Protocol.c Lib/V2ProtocolParams.c \
Lib/ISP/ISPProtocol.c Lib/ISP/ISPTarget.c\
Lib/XPROG/XPROGProtocol.c Lib/XPROG/XPROGTarget.c Lib/XPROG/XMEGANVM.c Lib/XPROG/TINYNVM.c \
......
......@@ -45,6 +45,7 @@
#include "Lib/V2Protocol.h"
#include "AVRISPDescriptors.h"
#include "BatDis.h"
#include "LEDs.h"
......@@ -66,6 +67,7 @@ static void avrisp_task(void)
if (Endpoint_IsOUTReceived())
{
if (timeout == 0) {
batdis_disable_sampling(true);
leds_disable_fading(true);
leds_set_color(LEDS_LED_RIGHT, LEDS_COLOR_BOB3_PROGRAMMING);
}
......@@ -81,6 +83,7 @@ static void avrisp_task(void)
}
--timeout;
if (timeout == 0) {
batdis_disable_sampling(false);
leds_disable_fading(false);
}
}
......@@ -98,6 +101,7 @@ int main(void)
clock_prescale_set(clock_div_1);
// Hardware initialization
batdis_init();
leds_init();
leds_set_color(LEDS_LED_LEFT, LEDS_COLOR_USB_NOT_READY);
leds_set_color(LEDS_LED_RIGHT, LEDS_COLOR_BOB3_NOT_CONNECTED);
......@@ -111,6 +115,7 @@ int main(void)
{
USB_USBTask();
avrisp_task();
batdis_task();
}
return 0;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment