This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| TARGET=beep_libpca | |
| SOURCES=beep_libpca.c | |
| DEPS= | |
| COBJ=$(SOURCES:.c=.o) | |
| PCA_PREFIX=../pca | |
| CC=avr-gcc | |
| OBJC=avr-objcopy | |
| MCU=atmega328p |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void serial_init() { | |
| power_usart0_enable(); | |
| // configure ports double mode | |
| UCSR0A = _BV(U2X0); | |
| // configure the ports speed | |
| UBRR0H = 0x00; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| unsigned char serial_poll_recv(unsigned char *a_data, unsigned int a_size) { | |
| unsigned int cnt = a_size; | |
| while (a_size) { | |
| /* Wait for data to be received */ | |
| while ( !(UCSR0A & (1<<RXC0)) ); | |
| /* Get and return received data from buffer */ | |
| *a_data = UDR0; | |
| a_data++; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| typedef struct _t_buffer { | |
| /// data storage space for the ring buffer | |
| volatile unsigned char ring[SERIAL_RX_RING_SIZE]; | |
| /// head index | |
| volatile unsigned char head; | |
| /// tail index | |
| volatile unsigned char tail; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // the RX ring buffer | |
| static volatile t_buffer g_rx_buff; | |
| ISR(USART_RX_vect, ISR_BLOCK) { | |
| // no frame error | |
| // UCSR0A must be read before UDR0 !!! | |
| if (bit_is_clear(UCSR0A, FE0)) { | |
| /// must read the data in order to clear the interrupt flag | |
| volatile unsigned char data = UDR0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ISR(USART_UDRE_vect, ISR_BLOCK) { | |
| // proceed if there still is data to be send | |
| if (g_tx_buff.head != g_tx_buff.tail) { | |
| UDR0 = g_tx_buff.ring[g_tx_buff.tail]; | |
| g_tx_buff.tail = (g_tx_buff.tail + 1) % SERIAL_TX_RING_SIZE; | |
| } | |
| else { | |
| // mask the interrupt everything has been send | |
| UCSR0B &= ~_BV(UDRIE0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "pca.h" | |
| int main(int argc, char const *argv[]) | |
| { | |
| uint8_t c = 0x00; | |
| serial_init(E_BAUD_57600); | |
| serial_install_interrupts(E_FLAGS_SERIAL_RX_INTERRUPT); | |
| serial_flush(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void _serial_putc(char c, FILE *stream) { | |
| if ('\n' == c) { | |
| _serial_putc('\r', stream); | |
| } | |
| while (!serial_sendc(c)); | |
| } | |
| char _serial_getc(FILE *stream) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "pca.h" | |
| #include <util/delay.h> | |
| int main(void) | |
| { | |
| int cnt = 0; | |
| serial_init(E_BAUD_9600); | |
| serial_install_interrupts(E_FLAGS_SERIAL_RX_INTERRUPT); | |
| serial_flush(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @brief if there are no SEND/RECV routine definitions already existing | |
| * use the default definitions provided bellow | |
| */ | |
| #if !defined(SLIP_CHAR_SEND) || !defined(SLIP_CHAR_RECV) | |
| #include "config.h" | |
| #include "serial.h" | |
| /// redefine it, in your code to use different implementation of SEND routine |