mirror of
https://github.com/sigmasternchen/chaOS
synced 2025-03-15 21:58:53 +00:00
first commit
This commit is contained in:
parent
8a815e2e35
commit
d0d099720d
15 changed files with 445 additions and 0 deletions
48
buffer.c
Normal file
48
buffer.c
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "buffer.h"
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
void initBuffer(Buffer* b, tiny length) {
|
||||||
|
b->maxLength = length;
|
||||||
|
allocBuffer(b);
|
||||||
|
b->beginPointer = 0;
|
||||||
|
b->readPointer = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int overflowIndex (tiny length, tiny index) {
|
||||||
|
return index % length;
|
||||||
|
}
|
||||||
|
|
||||||
|
tiny getBufferLength(Buffer* b) {
|
||||||
|
return overflowIndex(b->maxLength, b->beginPointer - b->readPointer);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool readBuffer (Buffer* b, char* c) {
|
||||||
|
if (!(getBufferLength(b) > 0))
|
||||||
|
return false;
|
||||||
|
*c = b->array[b->readPointer];
|
||||||
|
b->readPointer = overflowIndex(b->maxLength, b->readPointer + 1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool writeFIFO (Buffer* b, char c) {
|
||||||
|
if (!(getBufferLength(b) < (b->maxLength - 1)))
|
||||||
|
return false;
|
||||||
|
b->array[b->beginPointer] = c;
|
||||||
|
b->beginPointer = overflowIndex(b->maxLength, (b->beginPointer + 1));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool writeLIFO (Buffer* b, char c) {
|
||||||
|
if (!(getBufferLength(b) < (b->maxLength - 1)))
|
||||||
|
return false;
|
||||||
|
b->array[b->readPointer - 1] = c;
|
||||||
|
b->readPointer = overflowIndex(b->maxLength, (b->readPointer - 1));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void allocBuffer (Buffer* b) {
|
||||||
|
free(b->array);
|
||||||
|
b->array = (char*) malloc (b->maxLength * sizeof(char));
|
||||||
|
}
|
22
buffer.h
Normal file
22
buffer.h
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#ifndef BUFFER_H
|
||||||
|
#define BUFFER_H
|
||||||
|
|
||||||
|
#include "buffer.h"
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
tiny maxLength;
|
||||||
|
tiny beginPointer;
|
||||||
|
tiny readPointer;
|
||||||
|
char *array;
|
||||||
|
} Buffer;
|
||||||
|
|
||||||
|
void initBuffer(Buffer*, tiny);
|
||||||
|
int overflowIndex (tiny, tiny);
|
||||||
|
tiny getBufferLength(Buffer*);
|
||||||
|
bool readBuffer (Buffer*, char*);
|
||||||
|
bool writeFIFO (Buffer* , char );
|
||||||
|
bool writeLIFO (Buffer*, char);
|
||||||
|
void allocBuffer (Buffer*);
|
||||||
|
|
||||||
|
#endif
|
70
cmd/ash.c
Normal file
70
cmd/ash.c
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <avr/io.h>
|
||||||
|
|
||||||
|
#include "ash.h"
|
||||||
|
#include "../stdio.h"
|
||||||
|
#include "../types.h"
|
||||||
|
#include "../string.h"
|
||||||
|
|
||||||
|
void interpret(char***, tiny*, char*, tiny);
|
||||||
|
|
||||||
|
#define clLength 64
|
||||||
|
|
||||||
|
int ashMain () {
|
||||||
|
char input[clLength];
|
||||||
|
|
||||||
|
char *prompt = "ash> ";
|
||||||
|
|
||||||
|
tiny position = 0;
|
||||||
|
|
||||||
|
puts(prompt);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
char c = getch();
|
||||||
|
if (c == '\r') {
|
||||||
|
input[position] = '\0';
|
||||||
|
puts(nl);
|
||||||
|
char **args;
|
||||||
|
tiny argc;
|
||||||
|
interpret(&args, &argc, input, position);
|
||||||
|
position = 0;
|
||||||
|
puts(args[0]);
|
||||||
|
puts(nl);
|
||||||
|
puts(prompt);
|
||||||
|
free(args);
|
||||||
|
PORTB |= 2;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (position >= clLength - 1)
|
||||||
|
continue;
|
||||||
|
putc(c);
|
||||||
|
input[position] = c;
|
||||||
|
position++;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void interpret(char*** args, tiny* argc, char* input, tiny length) {
|
||||||
|
(*argc) = 0;
|
||||||
|
(*args) = (char**) malloc(sizeof(char*));
|
||||||
|
(*args)[0] = (char*) malloc(sizeof(char));
|
||||||
|
tiny paramLength = 0;
|
||||||
|
for (tiny i = 0; i < length; i++) {
|
||||||
|
if (input[i] == ' ') {
|
||||||
|
if (paramLength == 0)
|
||||||
|
continue;
|
||||||
|
(*args)[(*argc)] = (char*) realloc((*args)[(*argc)], (paramLength + 1) * sizeof(char));
|
||||||
|
(*args)[(*argc)][paramLength] = '\0';
|
||||||
|
(*argc)++;
|
||||||
|
(*args) = (char**) realloc((*args), ((*argc) + 1) * sizeof(char*));
|
||||||
|
(*args)[(*argc)] = (char*) malloc(sizeof(char));
|
||||||
|
paramLength = 0;
|
||||||
|
}
|
||||||
|
(*args)[(*argc)] = (char*) realloc((*args)[(*argc)], (paramLength + 1) * sizeof(char));
|
||||||
|
(*args)[(*argc)][paramLength] = input[i];
|
||||||
|
paramLength++;
|
||||||
|
}
|
||||||
|
(*args)[(*argc)] = (char*) realloc((*args)[(*argc)], (paramLength + 1) * sizeof(char));
|
||||||
|
(*args)[(*argc)][paramLength] = '\0';
|
||||||
|
}
|
6
cmd/ash.h
Normal file
6
cmd/ash.h
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef ASH_H
|
||||||
|
#define ASH_H
|
||||||
|
|
||||||
|
int ashMain();
|
||||||
|
|
||||||
|
#endif
|
49
main.c
Normal file
49
main.c
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
#include <avr/io.h>
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
|
||||||
|
#include "stdio.h"
|
||||||
|
#include "types.h"
|
||||||
|
#include "string.h"
|
||||||
|
#include "serial.h"
|
||||||
|
#include "cmd/ash.h"
|
||||||
|
|
||||||
|
void setup () {
|
||||||
|
DDRB = 0xff;
|
||||||
|
PORTB = 0;
|
||||||
|
initSerial(b9600, d8, even, s1);
|
||||||
|
initOutput();
|
||||||
|
sei(); // FACEPALM!!!!
|
||||||
|
|
||||||
|
puts(nl);
|
||||||
|
puts("init [serial]");
|
||||||
|
puts(nl);
|
||||||
|
puts("init [output]");
|
||||||
|
puts(nl);
|
||||||
|
|
||||||
|
initInput();
|
||||||
|
puts("init [input]");
|
||||||
|
puts(nl);
|
||||||
|
|
||||||
|
TIMSK = 0;
|
||||||
|
puts("reset timer interrupts");
|
||||||
|
puts(nl);
|
||||||
|
|
||||||
|
puts("watchdog flag: ");
|
||||||
|
putc(((MCUCSR & WDRF) >> WDRF) + '0');
|
||||||
|
puts(nl);
|
||||||
|
|
||||||
|
MCUCSR &= ~(1 << WDRF);
|
||||||
|
puts("watchdog flag reset");
|
||||||
|
puts(nl);
|
||||||
|
|
||||||
|
PORTB |= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main () {
|
||||||
|
setup();
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
ashMain();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
72
serial.c
Normal file
72
serial.c
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
#include "serial.h"
|
||||||
|
|
||||||
|
void initSerial(enum Baudrate baudrate, enum Databits data, enum Parity parity, enum Stopbits stop) {
|
||||||
|
switch (baudrate) {
|
||||||
|
case b2400:
|
||||||
|
UBRRL = 416 & 0xff;
|
||||||
|
UBRRH = 416 >> 8;
|
||||||
|
break;
|
||||||
|
case b4800:
|
||||||
|
UBRRL = 207 & 0xff;
|
||||||
|
UBRRH = 207 >> 8;
|
||||||
|
break;
|
||||||
|
case b9600:
|
||||||
|
UBRRL = 103 & 0xff;
|
||||||
|
UBRRH = 103 >> 8;
|
||||||
|
break;
|
||||||
|
case b19200:
|
||||||
|
UBRRL = 51 & 0xff;
|
||||||
|
UBRRH = 51 >> 8;
|
||||||
|
break;
|
||||||
|
case b38400:
|
||||||
|
UBRRL = 25 & 0xff;
|
||||||
|
UBRRH = 25 >> 8;
|
||||||
|
break;
|
||||||
|
case b57600:
|
||||||
|
UBRRL = 16 & 0xff;
|
||||||
|
UBRRH = 16 >> 8;
|
||||||
|
break;
|
||||||
|
case b155200:
|
||||||
|
UBRRL = 8 & 0xff;
|
||||||
|
UBRRH = 8 >> 8;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
UCSRC &= ~((0 << URSEL) | (1 << USBS));
|
||||||
|
if (stop == s2)
|
||||||
|
UCSRC |= (1 << URSEL) | (1 << USBS);
|
||||||
|
|
||||||
|
UCSRB &= ~((0 << URSEL) | (1 << UCSZ2));
|
||||||
|
UCSRC &= ~((0 << URSEL) | (1 << UCSZ1) | (1 << UCSZ0));
|
||||||
|
|
||||||
|
switch (data) {
|
||||||
|
case d8:
|
||||||
|
UCSRB |= (0 << UCSZ2);
|
||||||
|
UCSRC |= ((1 << URSEL) | (1 << UCSZ1) | (1 << UCSZ0));
|
||||||
|
break;
|
||||||
|
case d9:
|
||||||
|
UCSRB |= (1 << UCSZ2);
|
||||||
|
UCSRC |= ((1 << URSEL) | (1 << UCSZ1) | (1 << UCSZ0));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
UCSRC &= ~((0 << URSEL) | (1 << UPM1) | (1 << UPM0));
|
||||||
|
|
||||||
|
switch (parity) {
|
||||||
|
case no:
|
||||||
|
UCSRC |= (0 << UPM1) | (0 << UPM0);
|
||||||
|
break;
|
||||||
|
case even:
|
||||||
|
UCSRC |= (1 << UPM1) | (0 << UPM0);
|
||||||
|
break;
|
||||||
|
case odd:
|
||||||
|
UCSRC |= (1 << UPM1) | (1 << UPM0);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
34
serial.h
Normal file
34
serial.h
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
#ifndef SERIAL_H
|
||||||
|
#define SERIAL_H
|
||||||
|
|
||||||
|
#include <avr/io.h>
|
||||||
|
|
||||||
|
enum Parity {no, even, odd};
|
||||||
|
enum Baudrate {b2400, b4800, b9600, b19200, b38400, b57600, b155200};
|
||||||
|
enum Stopbits {s1, s2};
|
||||||
|
enum Databits {d8, d9};
|
||||||
|
|
||||||
|
void initSerial(enum Baudrate, enum Databits, enum Parity, enum Stopbits);
|
||||||
|
|
||||||
|
#define enableTransmitter() (UCSRB |= (1 << TXEN))
|
||||||
|
#define disableTransmitter() (UCSRB &= ~(1 << TXEN))
|
||||||
|
|
||||||
|
#define enableReceiver() (UCSRB |= (1 << RXEN))
|
||||||
|
#define disableReceiver() (UCSRB &= ~(1 << RXEN))
|
||||||
|
|
||||||
|
#define enableReceiveInterrupt() (UCSRB |= (1 << RXCIE))
|
||||||
|
#define disableReceiveInterrupt() (UCSRB &= ~(1 << RXCIE))
|
||||||
|
|
||||||
|
#define enableTransmitCompleteInterrupt() (UCSRB |= (1 << TXCIE))
|
||||||
|
#define disableTransmitCompleteInterrupt() (UCSRB &= ~(1 << TXCIE))
|
||||||
|
|
||||||
|
#define enableDataRegisterEmptyInterrupt() (UCSRB |= (1 << UDRIE))
|
||||||
|
#define disableDataRegisterEmptyInterrupt() (UCSRB &= ~(1 << UDRIE))
|
||||||
|
|
||||||
|
#define characterGot() (UCSRA & (1 << RXC))
|
||||||
|
#define dataRegisterEmpty() (UCSRA & (1 << UDRE))
|
||||||
|
|
||||||
|
#define sRead() (UDR)
|
||||||
|
#define sWrite(c) (UDR = c)
|
||||||
|
|
||||||
|
#endif
|
7
stdio.h
Normal file
7
stdio.h
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#ifndef STDIO_H
|
||||||
|
#define STDIO_H
|
||||||
|
|
||||||
|
#include "stdio/input.h"
|
||||||
|
#include "stdio/output.h"
|
||||||
|
|
||||||
|
#endif
|
36
stdio/input.c
Normal file
36
stdio/input.c
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
|
||||||
|
#include "input.h"
|
||||||
|
#include "../serial.h"
|
||||||
|
|
||||||
|
#define safe 3
|
||||||
|
|
||||||
|
Buffer stdin;
|
||||||
|
|
||||||
|
void initInput () {
|
||||||
|
initBuffer(&stdin, 64);
|
||||||
|
enableReceiver();
|
||||||
|
enableReceiveInterrupt();
|
||||||
|
}
|
||||||
|
|
||||||
|
char getch() {
|
||||||
|
char c;
|
||||||
|
while (!(readBuffer(&stdin, &c)));
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ungetc(char c) {
|
||||||
|
while (!writeLIFO(&stdin, c));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool kbhit() {
|
||||||
|
return getBufferLength(&stdin);
|
||||||
|
}
|
||||||
|
|
||||||
|
// on serial receive
|
||||||
|
ISR (USART_RXC_vect) {
|
||||||
|
PORTB |= 8;
|
||||||
|
if (getBufferLength(&stdin) < stdin.maxLength - safe) {
|
||||||
|
writeFIFO(&stdin, sRead());
|
||||||
|
}
|
||||||
|
}
|
13
stdio/input.h
Normal file
13
stdio/input.h
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#ifndef INPUT_H
|
||||||
|
#define INPUT_H
|
||||||
|
|
||||||
|
#include "../buffer.h"
|
||||||
|
|
||||||
|
extern Buffer stdin;
|
||||||
|
|
||||||
|
void initInput ();
|
||||||
|
char getch();
|
||||||
|
void ungetc(char c);
|
||||||
|
bool kbhit();
|
||||||
|
|
||||||
|
#endif
|
47
stdio/output.c
Normal file
47
stdio/output.c
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
|
||||||
|
#include "output.h"
|
||||||
|
#include "../string.h"
|
||||||
|
#include "../serial.h"
|
||||||
|
|
||||||
|
#define safe 3
|
||||||
|
|
||||||
|
Buffer stdout;
|
||||||
|
|
||||||
|
void initOutput () {
|
||||||
|
initBuffer(&stdout, 64);
|
||||||
|
enableTransmitter();
|
||||||
|
enableTransmitCompleteInterrupt();
|
||||||
|
enableDataRegisterEmptyInterrupt();
|
||||||
|
}
|
||||||
|
|
||||||
|
void putc (char c) {
|
||||||
|
if (dataRegisterEmpty()) {
|
||||||
|
sWrite(c);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
while (getBufferLength(&stdout) > stdout.maxLength - safe);
|
||||||
|
writeFIFO(&stdout, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
void puts (char* string) {
|
||||||
|
int length = getStringLength(string);
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
putc(string[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// on serial transmit
|
||||||
|
ISR (USART_TXC_vect) {
|
||||||
|
}
|
||||||
|
|
||||||
|
// on data register empty
|
||||||
|
ISR (USART_UDRE_vect) {
|
||||||
|
if (getBufferLength(&stdout)) {
|
||||||
|
char c;
|
||||||
|
readBuffer(&stdout, &c);
|
||||||
|
sWrite(c);
|
||||||
|
}
|
||||||
|
}
|
12
stdio/output.h
Normal file
12
stdio/output.h
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#ifndef OUTPUT_H
|
||||||
|
#define OUTPUT_H
|
||||||
|
|
||||||
|
#include "../buffer.h"
|
||||||
|
|
||||||
|
extern Buffer stdout;
|
||||||
|
|
||||||
|
void initOutput ();
|
||||||
|
void putc (char);
|
||||||
|
void puts (char*);
|
||||||
|
|
||||||
|
#endif
|
9
string.c
Normal file
9
string.c
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
int getStringLength (char* string) {
|
||||||
|
for (int i = 0; ; i++) {
|
||||||
|
if (string[i] == '\0')
|
||||||
|
return i + 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
8
string.h
Normal file
8
string.h
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef STRING_H
|
||||||
|
#define STRING_H
|
||||||
|
|
||||||
|
#define nl "\r\n"
|
||||||
|
|
||||||
|
int getStringLength (char*);
|
||||||
|
|
||||||
|
#endif
|
12
types.h
Normal file
12
types.h
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#ifndef TYPES_H
|
||||||
|
#define TYPES_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define bool uint8_t
|
||||||
|
#define false 0
|
||||||
|
#define true !false
|
||||||
|
|
||||||
|
#define tiny uint8_t
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in a new issue