Skip to content

Commit

Permalink
Major update including work on the usb driver, the console, the loggi…
Browse files Browse the repository at this point in the history
…ng subsystem, button input handling code and updates for the new board layout.
  • Loading branch information
Dimitris Papavasiliou committed Feb 25, 2015
1 parent 9811e6e commit cd4a20c
Show file tree
Hide file tree
Showing 21 changed files with 841 additions and 290 deletions.
69 changes: 69 additions & 0 deletions button.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "mk20dx128.h"
#include "usbserial.h"
#include "button.h"

static uint8_t down;
static button_callback callback;

__attribute__((interrupt ("IRQ"))) void lptmr_isr(void)
{
if (!down) {
down = 1;

/* Set the lop power timer for active-low to catch the button
* release. */

LPTMR0_CSR &= ~LPTMR_CSR_TEN;
LPTMR0_CSR |= LPTMR_CSR_TPP;
LPTMR0_CSR |= LPTMR_CSR_TEN;
} else {
down = 0;

if (callback) {
callback();
}

/* Set the lop power timer for active-low to catch the next
* button press. */

LPTMR0_CSR &= ~LPTMR_CSR_TEN;
LPTMR0_CSR &= ~LPTMR_CSR_TPP;
LPTMR0_CSR |= LPTMR_CSR_TEN;
}

LPTMR0_CSR |= LPTMR_CSR_TCF;
}

void button_set_callback(button_callback new_callback)
{
callback = new_callback;
}

void button_initialize()
{
/* Enable the low power timer and port C modules. */

SIM_SCGC5 |= SIM_SCGC5_PORTC | SIM_SCGC5_LPTIMER;
PORTC_PCR5 = PORT_PCR_MUX(3) | PORT_PCR_PFE | PORT_PCR_PE;

/* Reset the low power timer module. */

LPTMR0_CSR = 0;

/* Configure for single pulse counting, clocked by the 1kHz LPO
* clock and set the glitch filter for 16 rising clock edges. */

LPTMR0_CSR = LPTMR_CSR_TPS(2) | LPTMR_CSR_TMS | LPTMR_CSR_TIE;
LPTMR0_CMR = 0;
LPTMR0_PSR = LPTMR_PSR_PCS(1) | LPTMR_PSR_PRESCALE(4);

/* Enable the timer and the corresponding interrupt. */

LPTMR0_CSR |= LPTMR_CSR_TEN;

/* Enable the button interrupt and set its priority as low as it
* gets. */

enable_interrupt(39);
prioritize_interrupt(39, 15)
}
9 changes: 9 additions & 0 deletions button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef __BUTTON__
#define __BUTTON__

typedef void (*button_callback)();

void button_initialize();
void button_set_callback(button_callback new_callback);

#endif
85 changes: 85 additions & 0 deletions console
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/bin/bash

history -r .console_history
quit=false

# Suppress error messages to get rid of kill notifications.

#exec 2>/dev/null

while ! $quit; do
device=$(ls -1 /dev | grep ttyACM | head -n 1)

# Wait for the file to become available, if necessary.

if [ ! $device ]; then
echo "Waiting for device to come on-line..."

while file=$(inotifywait /dev/ -qe create --format %f); do
if [[ $file = ttyACM* ]]; then
device=$file
break;
fi
done

while [ ! -w /dev/$device -o ! -w /dev/$device ]; do
sleep 1
done
fi

echo "Using device $device."

# Set up a redirect from stdin to fd 3.

exec 3<&0

# Start the console in a subprocess and let it source its input from
# fd 3.

stty 4000000 -icrnl -onlcr -isig -icanon -opost -echo < /dev/$device

cat /dev/$device &
catpid=$!
disown $catpid

(
while read -e line; do
history -s "$line"
echo "$line" > /dev/$device
done
) 0<&3 &

readpid=$!

trap "kill $readpid" SIGINT

# Wait for the device to disappear.

(
while file=$(inotifywait /dev/ -qe delete --format %f); do
if [ "$file" = "$device" ]; then
kill $readpid 2>/dev/null
break
fi
done
) &

waitpid=$!
disown $waitpid

if wait $readpid 2>/dev/null; then
quit=true
else
echo "Device went off-line."
fi

trap - SIGINT
pkill -P $readpid,$catpid,$waitpid 2>/dev/null
kill $readpid $catpid $waitpid 2>/dev/null

# Restore stdin and close fd 3.

exec 0<&3 3<&-
done

history -w .console_history
Loading

0 comments on commit cd4a20c

Please sign in to comment.