inv-sw-32u4/src/commands.c
Marco 3fd547f2ea Toggle PD6 instead of PC7 when timer compare interrupt occurs.
Move timer interrupt routine to timer.c
Change timer parameters
2022-05-13 01:24:38 +02:00

104 lines
1.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#include "include/commands.h"
cmd_t command = {
.buf = {0},
.index = 0
};
void cmd_mirror_char(char c)
{
printf("%c", c);
}
void cmd_collect_char(char c)
{
command.buf[command.index] = c;
cmd_mirror_char(c);
command.index++;
if (c == 0x3) {
/* React on Ctl-C */
command.index = 0;
printf("\r\n");
}
if (c == 0xD) {
/* Null-terminate the string */
command.buf[command.index-1] = '\0';
cmd_handle(&command);
command.index = 0;
}
}
void cmd_handle(cmd_t *command)
{
printf("\n\r");
char *tok1 = strtok(command->buf, " ");
char *tok2 = strtok(NULL, " ");
char *tok3 = strtok(NULL, " ");
if (0 == strcmp(tok1, "set"))
cmd_handle_set(tok2, tok3);
if (0 == strcmp(tok1, "switch"))
cmd_handle_switch();
}
void cmd_handle_set(char *op1, char *op2)
{
long int val;
char *end;
if (0 == strcmp(op1, "t0_top")) {
val = strtol(op2, &end, 10);
if (op2 != end) {
OCR0A = val;
printf("Setting Timer0 TOP to %li\r\n", val);
}
else
printf("No valid value for t0_top given\n\r");
}
else if (0 == strcmp(op1, "t0_ps")) {
val = strtol(op2, &end, 10);
TCCR0B &= ~((1 << CS00) | (1 << CS01) | (1 << CS02));
switch (val)
{
case 0:
TCCR0B |= (1 << CS00);
break;
case 8:
TCCR0B |= (1 << CS01);
break;
case 64:
TCCR0B |= (1 << CS00) | (1 << CS01);
break;
case 256:
TCCR0B |= (1 << CS02);
break;
case 1024:
TCCR0B |= (1 << CS02) | (1 << CS00);
break;
default:
printf("No valid prescaler value.\n\r");
break;
}
}
}
void cmd_handle_switch(void)
{
int state = 0;
PORTD ^= (1<<PD6);
state = PORTD & (1 << PD6);
printf("Pin state is %i\r\n", state);
}