diff --git a/firmware/Makefile b/firmware/Makefile index 9a0f19f..c805113 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -35,7 +35,7 @@ OPT = s ########################################################################################################## # List C source files here. (C dependencies are automatically generated.) -SRC = main.c synth.c +SRC = main.c synth.c usart.c ########################################################################################################## diff --git a/firmware/main.c b/firmware/main.c index 04da2ca..8f91f70 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -6,7 +6,7 @@ #include "main.h" #include "synth.h" - +#include "usart.h" static void init_sampletimer(void) { @@ -74,6 +74,7 @@ int main(void) { /* hardware initialisation: */ init_leds(); + USART0_Init(); // init_motor(); init_pwm(); init_sampletimer(); @@ -94,5 +95,7 @@ ISR(TIMER0_COMPA_vect) { /* calculate next analog sample value in synth mixer:*/ OCR1B = synth_mix(); + USART0_put_uint16(0x1234); + USART0_crlf(); } diff --git a/firmware/usart.c b/firmware/usart.c new file mode 100644 index 0000000..0937238 --- /dev/null +++ b/firmware/usart.c @@ -0,0 +1,132 @@ +/* + * PentaFnord Firmware + * + * by Alexander Lorz + * + * + * The USART control code is derived by code from the 4CHLED project + * by sebseb7: https://github.com/sebseb7/eagle/tree/master/4CHLED/firmware + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include +#include + +//#include "main.h" +#include "usart.h" + +#define UART_RXBUFSIZE 32 + +volatile static uint8_t rxbuf0[UART_RXBUFSIZE]; +volatile static uint8_t *volatile rxhead0, *volatile rxtail0; +//volatile uint8_t xon = 0; + + +ISR (USART_RX_vect) +{ + UCSR0B &= ~(1 << RXCIE0); + asm volatile("sei"); + + int diff; + uint8_t c; + c=UDR0; + diff = rxhead0 - rxtail0; + if (diff < 0) diff += UART_RXBUFSIZE; + if (diff < UART_RXBUFSIZE -1) + { + *rxhead0 = c; + ++rxhead0; + if (rxhead0 == (rxbuf0 + UART_RXBUFSIZE)) rxhead0 = rxbuf0; +// if((diff > 100)&&(xon==0)) +// { +// xon=1; +// //set the CTS pin +// } + } + UCSR0B |= (1 << RXCIE0); +} + + +void USART0_Init (void) +{ + // set baudrate + #undef BAUD + #define BAUD 115200 + #include + UBRR0H = UBRRH_VALUE; + UBRR0L = UBRRL_VALUE; + + //#if USE_2X + UCSR0A |= (1 << U2X0); // enable double speed operation + //#else + // UCSR0A &= ~(1 << U2X0); // disable double speed operation + //#endif + + + // flush receive buffer + while ( UCSR0A & (1 << RXC0) ) UDR0; + + // set 8N1 + UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); + UCSR0B &= ~(1 << UCSZ02); + + UCSR0B |= (1 << RXEN0)|(1 << TXEN0); //enable send and receive + + UCSR0B |= (1 << RXCIE0); //enable receive interrup + + rxhead0 = rxtail0 = rxbuf0; + +} + + + +void USART0_putc (char c) +{ + + loop_until_bit_is_set(UCSR0A, UDRE0); + UDR0 = c; +} + + +uint8_t USART0_Getc_nb(uint8_t *c) +{ + if (rxhead0==rxtail0) return 0; + *c = *rxtail0; + if (++rxtail0 == (rxbuf0 + UART_RXBUFSIZE)) rxtail0 = rxbuf0; + +// uint8_t diff = rxhead0 - rxtail0; +// if((diff < 10)&&(xon==1)) +// { +// xon=0; +// //set the CTS pin +// } + + return 1; +} + +void USART0_crlf(){ + USART0_putc(0x0A); //newline + USART0_putc(0x0D); //carriage return +}; + +void USART0_put_uint8(uint8_t x){ + USART0_putc(((x & 0b11110000)>>4)+0x30); + USART0_putc((x & 0b00001111)+0x30); + +} +void USART0_put_uint16(uint16_t x){ + USART0_put_uint8 ((x & 0xFF00)>>8); + USART0_put_uint8 (x & 0x00FF); + +} diff --git a/firmware/usart.h b/firmware/usart.h new file mode 100644 index 0000000..d0ddebc --- /dev/null +++ b/firmware/usart.h @@ -0,0 +1,34 @@ +/* + * PentaFnord Firmware + * + * by Alexander Lorz + * + * + * The USART control code is derived by code from the 4CHLED project + * by sebseb7: https://github.com/sebseb7/eagle/tree/master/4CHLED/firmware + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef _USART_H +#define _USART_H + +void USART0_Init (void); +void USART0_putc (char c); +uint8_t USART0_Getc_nb(uint8_t*); +void USART0_put_uint8(uint8_t); +void USART0_crlf(); +void USART0_put_uint16(uint16_t); + +#endif +