diff --git a/22_m1284p_WIZNET_MQTT/Application/loopback/loopback.c b/22_m1284p_WIZNET_MQTT/Application/loopback/loopback.c deleted file mode 100644 index 8396cf7..0000000 --- a/22_m1284p_WIZNET_MQTT/Application/loopback/loopback.c +++ /dev/null @@ -1,225 +0,0 @@ -#include -#include "loopback.h" -#include "socket.h" -#include "wizchip_conf.h" - -#if LOOPBACK_MODE == LOOPBACK_MAIN_NOBLCOK - -int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t port) -{ - int32_t ret; - uint16_t size = 0, sentsize=0; - -#ifdef _LOOPBACK_DEBUG_ - uint8_t destip[4]; - uint16_t destport; -#endif - - switch(getSn_SR(sn)) - { - case SOCK_ESTABLISHED : - if(getSn_IR(sn) & Sn_IR_CON) - { -#ifdef _LOOPBACK_DEBUG_ - getSn_DIPR(sn, destip); - destport = getSn_DPORT(sn); - - PRINTF("%d:Connected - %d.%d.%d.%d : %u\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport); -#endif - setSn_IR(sn,Sn_IR_CON); - } - if((size = getSn_RX_RSR(sn)) > 0) // Don't need to check SOCKERR_BUSY because it doesn't not occur. - { - if(size > LOOPBACK_DATA_BUF_SIZE) size = LOOPBACK_DATA_BUF_SIZE; - ret = recv(sn, buf, size); - - if(ret <= 0) return ret; // check SOCKERR_BUSY & SOCKERR_XXX. For showing the occurrence of SOCKERR_BUSY. - size = (uint16_t) ret; - sentsize = 0; - - while(size != sentsize) - { - ret = send(sn, buf+sentsize, size-sentsize); - if(ret < 0) - { - close(sn); - return ret; - } - sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero. - } - } - break; - case SOCK_CLOSE_WAIT : -#ifdef _LOOPBACK_DEBUG_ - //printf("%d:CloseWait\r\n",sn); -#endif - if((ret = disconnect(sn)) != SOCK_OK) return ret; -#ifdef _LOOPBACK_DEBUG_ - PRINTF("%d:Socket Closed\r\n", sn); -#endif - break; - case SOCK_INIT : -#ifdef _LOOPBACK_DEBUG_ - PRINTF("%d:Listen, TCP server loopback, port [%u]\r\n", sn, port); -#endif - if( (ret = listen(sn)) != SOCK_OK) return ret; - break; - case SOCK_CLOSED: -#ifdef _LOOPBACK_DEBUG_ - //printf("%d:TCP server loopback start\r\n",sn); -#endif - if((ret = socket(sn, Sn_MR_TCP, port, 0x00)) != sn) return ret; -#ifdef _LOOPBACK_DEBUG_ - //printf("%d:Socket opened\r\n",sn); -#endif - break; - default: - break; - } - return 1; -} - - -int32_t loopback_tcpc(uint8_t sn, uint8_t* buf, uint8_t* destip, uint16_t destport) -{ - int32_t ret; // return value for SOCK_ERRORs - uint16_t size = 0, sentsize=0; - - // Destination (TCP Server) IP info (will be connected) - // >> loopback_tcpc() function parameter - // >> Ex) - // uint8_t destip[4] = {192, 168, 0, 214}; - // uint16_t destport = 5000; - - // Port number for TCP client (will be increased) - static uint16_t any_port = 50000; - - // Socket Status Transitions - // Check the W5500 Socket n status register (Sn_SR, The 'Sn_SR' controlled by Sn_CR command or Packet send/recv status) - switch(getSn_SR(sn)) - { - case SOCK_ESTABLISHED : - if(getSn_IR(sn) & Sn_IR_CON) // Socket n interrupt register mask; TCP CON interrupt = connection with peer is successful - { -#ifdef _LOOPBACK_DEBUG_ - PRINTF("%d:Connected to - %d.%d.%d.%d : %u\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport); -#endif - setSn_IR(sn, Sn_IR_CON); // this interrupt should be write the bit cleared to '1' - } - - ////////////////////////////////////////////////////////////////////////////////////////////// - // Data Transaction Parts; Handle the [data receive and send] process - ////////////////////////////////////////////////////////////////////////////////////////////// - if((size = getSn_RX_RSR(sn)) > 0) // Sn_RX_RSR: Socket n Received Size Register, Receiving data length - { - if(size > LOOPBACK_DATA_BUF_SIZE) size = LOOPBACK_DATA_BUF_SIZE; // LOOPBACK_DATA_BUF_SIZE means user defined buffer size (array) - ret = recv(sn, buf, size); // Data Receive process (H/W Rx socket buffer -> User's buffer) - - if(ret <= 0) return ret; // If the received data length <= 0, receive failed and process end - size = (uint16_t) ret; - sentsize = 0; - - // Data sentsize control - while(size != sentsize) - { - ret = send(sn, buf+sentsize, size-sentsize); // Data send process (User's buffer -> Destination through H/W Tx socket buffer) - if(ret < 0) // Send Error occurred (sent data length < 0) - { - close(sn); // socket close - return ret; - } - sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero. - } - } - ////////////////////////////////////////////////////////////////////////////////////////////// - break; - - case SOCK_CLOSE_WAIT : -#ifdef _LOOPBACK_DEBUG_ - //printf("%d:CloseWait\r\n",sn); -#endif - if((ret=disconnect(sn)) != SOCK_OK) return ret; -#ifdef _LOOPBACK_DEBUG_ - PRINTF("%d:Socket Closed\r\n", sn); -#endif - break; - - case SOCK_INIT : -#ifdef _LOOPBACK_DEBUG_ - PRINTF("%d:Try to connect to the %d.%d.%d.%d : %u\r\n", sn, destip[0], destip[1], destip[2], destip[3], destport); -#endif - if( (ret = connect(sn, destip, destport)) != SOCK_OK) return ret; // Try to TCP connect to the TCP server (destination) - break; - - case SOCK_CLOSED: - close(sn); - if((ret=socket(sn, Sn_MR_TCP, any_port++, 0x00)) != sn){ - if(any_port == 0xffff) any_port = 50000; - return ret; // TCP socket open with 'any_port' port number - } -#ifdef _LOOPBACK_DEBUG_ - //printf("%d:TCP client loopback start\r\n",sn); - //printf("%d:Socket opened\r\n",sn); -#endif - break; - default: - break; - } - return 1; -} - - -int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t port) -{ - int32_t ret; - uint16_t size, sentsize; - uint8_t destip[4]; - uint16_t destport; - - switch(getSn_SR(sn)) - { - case SOCK_UDP : - if((size = getSn_RX_RSR(sn)) > 0) - { - if(size > LOOPBACK_DATA_BUF_SIZE) size = LOOPBACK_DATA_BUF_SIZE; - ret = recvfrom(sn, buf, size, destip, (uint16_t*)&destport); - if(ret <= 0) - { -#ifdef _LOOPBACK_DEBUG_ - PRINTF("%d: recvfrom error. %ld\r\n",sn,ret); -#endif - return ret; - } - size = (uint16_t) ret; - sentsize = 0; - while(sentsize != size) - { - ret = sendto(sn, buf+sentsize, size-sentsize, destip, destport); - if(ret < 0) - { -#ifdef _LOOPBACK_DEBUG_ - PRINTF("%d: sendto error. %ld\r\n",sn,ret); -#endif - return ret; - } - sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero. - } - } - break; - case SOCK_CLOSED: -#ifdef _LOOPBACK_DEBUG_ - //printf("%d:UDP loopback start\r\n",sn); -#endif - if((ret = socket(sn, Sn_MR_UDP, port, 0x00)) != sn) - return ret; -#ifdef _LOOPBACK_DEBUG_ - PRINTF("%d:Opened, UDP loopback, port [%u]\r\n", sn, port); -#endif - break; - default : - break; - } - return 1; -} - -#endif diff --git a/22_m1284p_WIZNET_MQTT/Application/loopback/loopback.h b/22_m1284p_WIZNET_MQTT/Application/loopback/loopback.h deleted file mode 100644 index 5df3cff..0000000 --- a/22_m1284p_WIZNET_MQTT/Application/loopback/loopback.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef _LOOPBACK_H_ -#define _LOOPBACK_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include -#include "../../globals.h" - -/* Loopback test debug message printout enable */ -#define _LOOPBACK_DEBUG_ - -/* DATA_BUF_SIZE define for Loopback example */ -#ifndef LOOPBACK_DATA_BUF_SIZE - #define LOOPBACK_DATA_BUF_SIZE 512 -#endif - -/************************/ -/* Select LOOPBACK_MODE */ -/************************/ -#define LOOPBACK_MAIN_NOBLOCK 0 -#define LOOPBACK_MODE LOOPBACK_MAIN_NOBLOCK - - -/* TCP server Loopback test example */ -int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t port); - -/* TCP client Loopback test example */ -int32_t loopback_tcpc(uint8_t sn, uint8_t* buf, uint8_t* destip, uint16_t destport); - -/* UDP Loopback test example */ -int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t port); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/22_m1284p_WIZNET_MQTT/globals.c b/22_m1284p_WIZNET_MQTT/globals.c deleted file mode 100644 index 98b2186..0000000 --- a/22_m1284p_WIZNET_MQTT/globals.c +++ /dev/null @@ -1,29 +0,0 @@ -/* - * globals.c - * - * Created on: 07 февр. 2019 г. - * Author: maxx - */ -#include "globals.h" - -#ifdef IP_WORK -//NIC metrics for WORK PC -wiz_NetInfo netInfo = { .mac = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef}, // Mac address - .ip = {192, 168, 0, 199}, // IP address - .sn = {255, 255, 255, 0}, // Subnet mask - .dns = {8,8,8,8}, // DNS address (google dns) - .gw = {192, 168, 0, 1}, // Gateway address - .dhcp = NETINFO_STATIC}; //Static IP configuration -uint8_t MQTT_targetIP[4] = {192, 168, 0, 100}; // IP брокера MQTT - -#else -//NIC metrics for another PC (second IP configuration) -wiz_NetInfo netInfo = { .mac = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef}, // Mac address - .ip = {192, 168, 1, 199}, // IP address - .sn = {255, 255, 255, 0}, // Subnet mask - .dns = {8,8,8,8}, // DNS address (google dns) - .gw = {192, 168, 1, 1}, // Gateway address - .dhcp = NETINFO_STATIC}; //Static IP configuration -uint8_t MQTT_targetIP[4] = {192, 168, 1, 81}; // IP брокера MQTT -#endif - diff --git a/22_m1284p_WIZNET_MQTT/globals.h b/22_m1284p_WIZNET_MQTT/globals.h deleted file mode 100644 index dacd544..0000000 --- a/22_m1284p_WIZNET_MQTT/globals.h +++ /dev/null @@ -1,77 +0,0 @@ -/* - * globals.h - * - * Created on: 29 нояб. 2018 г. - * Author: maxx - */ - -#ifndef GLOBALS_H_ -#define GLOBALS_H_ - -#include -#include -#include -#include "avr/wdt.h" // WatchDog - -#include "Ethernet/socket.h" -#include "Ethernet/wizchip_conf.h" - -//******************************* Fat FS declare related: BEGIN -/* -#include "string.h" -#include "ff.h" -#include "diskio.h" -#include "integer.h" -#include "Internet/httpServer_avr/httpParser.h" -static FATFS Fatfs; //File system object for each logical drive. >= 2 -//static FIL File; //File object. there are _FS_LOCK file objects available, >= 2 -*/ -//******************************* Fat FS declare related: END - - -//#define HTTPD_MAX_BUF_SIZE 2048 //For Mega1284p(16kb RAM)/Mega2560(8kb RAM) -//#define HTTPD_MAX_BUF_SIZE MAX_URI_SIZE+10 //For Mega644p(4kb RAM)/Mega128(4kb RAM) (ie. 512+10=522 bytes look at httpParser.h <_st_http_request> definition) - -#define LOOPBACK_DATA_BUF_SIZE 512 - -#define PRINTF_EN 1 -#if PRINTF_EN -#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args) -#else -#define PRINTF(...) -#endif - -#define SPRINTF(__S, FORMAT, args...) sprintf_P(__S, PSTR(FORMAT),##args) - -#define IP_WORK - -//SPI CLOCK 4 or 8Mhz -#define SPI_4_MHZ -//#define SPI_8_MHZ - -extern unsigned long millis(void); -extern int freeRam (void); - -//M644P/M1284p Users LEDS: -//LED1/PORTC.4- m644p/m1284p maxxir -#define led1_conf() DDRC |= (1< http://jump.to/fleury -File: $Id: uart.c,v 1.12 2014/01/08 21:58:12 peter Exp $ -Software: AVR-GCC 4.1, AVR Libc 1.4.6 or higher -Hardware: any AVR with built-in UART, -License: GNU General Public License - -DESCRIPTION: - An interrupt is generated when the UART has finished transmitting or - receiving a byte. The interrupt handling routines use circular buffers - for buffering received and transmitted data. - - The UART0[1]_RX_BUFFER_SIZE and UART0[1]_TX_BUFFER_SIZE variables define - the buffer size in bytes. Note that these variables must be a - power of 2. - -USAGE: - Refere to the header file uart.h for a description of the routines. - See also example test_uart.c. - -NOTES: - Based on Atmel Application Note AVR306 - -LICENSE: - Copyright (C) 2006 Peter Fleury - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - any later version. - - 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. - -*************************************************************************/ -#include -#include -#include -#include "uart_extd.h" - - -/* - * constants and macros - */ - -/* size of RX0/TX0 buffers */ -#define UART0_RX_BUFFER_MASK ( UART0_RX_BUFFER_SIZE - 1) -#define UART0_TX_BUFFER_MASK ( UART0_TX_BUFFER_SIZE - 1) - -#if ( UART0_RX_BUFFER_SIZE & UART0_RX_BUFFER_MASK ) -#error RX0 buffer size is not a power of 2 -#endif -#if ( UART0_TX_BUFFER_SIZE & UART0_TX_BUFFER_MASK ) -#error TX0 buffer size is not a power of 2 -#endif - -/* size of RX1/TX1 buffers */ -#define UART1_RX_BUFFER_MASK ( UART1_RX_BUFFER_SIZE - 1) -#define UART1_TX_BUFFER_MASK ( UART1_TX_BUFFER_SIZE - 1) - -#if ( UART1_RX_BUFFER_SIZE & UART1_RX_BUFFER_MASK ) -#error RX1 buffer size is not a power of 2 -#endif -#if ( UART1_TX_BUFFER_SIZE & UART1_TX_BUFFER_MASK ) -#error TX1 buffer size is not a power of 2 -#endif - - -#if defined(__AVR_AT90S2313__) \ - || defined(__AVR_AT90S4414__) || defined(__AVR_AT90S4434__) \ - || defined(__AVR_AT90S8515__) || defined(__AVR_AT90S8535__) \ - || defined(__AVR_ATmega103__) - /* old AVR classic or ATmega103 with one UART */ - #define AT90_UART - #define UART0_RECEIVE_INTERRUPT UART_RX_vect - #define UART0_TRANSMIT_INTERRUPT UART_UDRE_vect - #define UART0_STATUS USR - #define UART0_CONTROL UCR - #define UART0_DATA UDR - #define UART0_UDRIE UDRIE -#elif defined(__AVR_AT90S2333__) || defined(__AVR_AT90S4433__) - /* old AVR classic with one UART */ - #define AT90_UART - #define UART0_RECEIVE_INTERRUPT UART_RX_vect - #define UART0_TRANSMIT_INTERRUPT UART_UDRE_vect - #define UART0_STATUS UCSRA - #define UART0_CONTROL UCSRB - #define UART0_DATA UDR - #define UART0_UDRIE UDRIE -#elif defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__) \ - || defined(__AVR_ATmega323__) - /* ATmega with one USART */ - #define ATMEGA_USART - #define UART0_RECEIVE_INTERRUPT USART_RXC_vect - #define UART0_TRANSMIT_INTERRUPT USART_UDRE_vect - #define UART0_STATUS UCSRA - #define UART0_CONTROL UCSRB - #define UART0_DATA UDR - #define UART0_UDRIE UDRIE -#elif defined (__AVR_ATmega8515__) || defined(__AVR_ATmega8535__) - #define ATMEGA_USART - #define UART0_RECEIVE_INTERRUPT USART_RX_vect - #define UART0_TRANSMIT_INTERRUPT USART_UDRE_vect - #define UART0_STATUS UCSRA - #define UART0_CONTROL UCSRB - #define UART0_DATA UDR - #define UART0_UDRIE UDRIE -#elif defined(__AVR_ATmega163__) - /* ATmega163 with one UART */ - #define ATMEGA_UART - #define UART0_RECEIVE_INTERRUPT UART_RX_vect - #define UART0_TRANSMIT_INTERRUPT UART_UDRE_vect - #define UART0_STATUS UCSRA - #define UART0_CONTROL UCSRB - #define UART0_DATA UDR - #define UART0_UDRIE UDRIE -#elif defined(__AVR_ATmega162__) - /* ATmega with two USART */ - #define ATMEGA_USART0 - #define ATMEGA_USART1 - #define UART0_RECEIVE_INTERRUPT USART0_RXC_vect - #define UART1_RECEIVE_INTERRUPT USART1_RXC_vect - #define UART0_TRANSMIT_INTERRUPT USART0_UDRE_vect - #define UART1_TRANSMIT_INTERRUPT USART1_UDRE_vect - #define UART0_STATUS UCSR0A - #define UART0_CONTROL UCSR0B - #define UART0_DATA UDR0 - #define UART0_UDRIE UDRIE0 - #define UART1_STATUS UCSR1A - #define UART1_CONTROL UCSR1B - #define UART1_DATA UDR1 - #define UART1_UDRIE UDRIE1 -#elif defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__) - /* ATmega with two USART */ - #define ATMEGA_USART0 - #define ATMEGA_USART1 - #define UART0_RECEIVE_INTERRUPT USART0_RX_vect - #define UART1_RECEIVE_INTERRUPT USART1_RX_vect - #define UART0_TRANSMIT_INTERRUPT USART0_UDRE_vect - #define UART1_TRANSMIT_INTERRUPT USART1_UDRE_vect - #define UART0_STATUS UCSR0A - #define UART0_CONTROL UCSR0B - #define UART0_DATA UDR0 - #define UART0_UDRIE UDRIE0 - #define UART1_STATUS UCSR1A - #define UART1_CONTROL UCSR1B - #define UART1_DATA UDR1 - #define UART1_UDRIE UDRIE1 -#elif defined(__AVR_ATmega161__) - /* ATmega with UART */ - #error "AVR ATmega161 currently not supported by this libaray !" -#elif defined(__AVR_ATmega169__) - /* ATmega with one USART */ - #define ATMEGA_USART - #define UART0_RECEIVE_INTERRUPT USART0_RX_vect - #define UART0_TRANSMIT_INTERRUPT USART0_UDRE_vect - #define UART0_STATUS UCSRA - #define UART0_CONTROL UCSRB - #define UART0_DATA UDR - #define UART0_UDRIE UDRIE -#elif defined(__AVR_ATmega48__) || defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega48P__) || defined(__AVR_ATmega88P__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) \ - || defined(__AVR_ATmega3250__) || defined(__AVR_ATmega3290__) ||defined(__AVR_ATmega6450__) || defined(__AVR_ATmega6490__) - /* ATmega with one USART */ - #define ATMEGA_USART0 - #define UART0_RECEIVE_INTERRUPT USART_RX_vect - #define UART0_TRANSMIT_INTERRUPT USART_UDRE_vect - #define UART0_STATUS UCSR0A - #define UART0_CONTROL UCSR0B - #define UART0_DATA UDR0 - #define UART0_UDRIE UDRIE0 -#elif defined(__AVR_ATtiny2313__) - #define ATMEGA_USART - #define UART0_RECEIVE_INTERRUPT USART_RX_vect - #define UART0_TRANSMIT_INTERRUPT USART_UDRE_vect - #define UART0_STATUS UCSRA - #define UART0_CONTROL UCSRB - #define UART0_DATA UDR - #define UART0_UDRIE UDRIE -#elif defined(__AVR_ATmega329__) || \ - defined(__AVR_ATmega649__) || \ - defined(__AVR_ATmega325__) || \ - defined(__AVR_ATmega645__) - /* ATmega with one USART */ - #define ATMEGA_USART0 - #define UART0_RECEIVE_INTERRUPT USART0_RX_vect - #define UART0_TRANSMIT_INTERRUPT USART0_UDRE_vect - #define UART0_STATUS UCSR0A - #define UART0_CONTROL UCSR0B - #define UART0_DATA UDR0 - #define UART0_UDRIE UDRIE0 -#elif defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega640__) -/* ATmega with two USART */ - #define ATMEGA_USART0 - #define ATMEGA_USART1 - #define UART0_RECEIVE_INTERRUPT USART0_RX_vect - #define UART1_RECEIVE_INTERRUPT USART1_RX_vect - #define UART0_TRANSMIT_INTERRUPT USART0_UDRE_vect - #define UART1_TRANSMIT_INTERRUPT USART1_UDRE_vect - #define UART0_STATUS UCSR0A - #define UART0_CONTROL UCSR0B - #define UART0_DATA UDR0 - #define UART0_UDRIE UDRIE0 - #define UART1_STATUS UCSR1A - #define UART1_CONTROL UCSR1B - #define UART1_DATA UDR1 - #define UART1_UDRIE UDRIE1 -#elif defined(__AVR_ATmega644__) - /* ATmega with one USART */ - #define ATMEGA_USART0 - #define UART0_RECEIVE_INTERRUPT USART0_RX_vect - #define UART0_TRANSMIT_INTERRUPT USART0_UDRE_vect - #define UART0_STATUS UCSR0A - #define UART0_CONTROL UCSR0B - #define UART0_DATA UDR0 - #define UART0_UDRIE UDRIE0 -#elif defined(__AVR_ATmega164P__) || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__) - /* ATmega with two USART */ - #define ATMEGA_USART0 - #define ATMEGA_USART1 - #define UART0_RECEIVE_INTERRUPT USART0_RX_vect - #define UART1_RECEIVE_INTERRUPT USART1_RX_vect - #define UART0_TRANSMIT_INTERRUPT USART0_UDRE_vect - #define UART1_TRANSMIT_INTERRUPT USART1_UDRE_vect - #define UART0_STATUS UCSR0A - #define UART0_CONTROL UCSR0B - #define UART0_DATA UDR0 - #define UART0_UDRIE UDRIE0 - #define UART1_STATUS UCSR1A - #define UART1_CONTROL UCSR1B - #define UART1_DATA UDR1 - #define UART1_UDRIE UDRIE1 -#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1287__) - /* AT90USBxx with one USART */ - #define AT90USB_USART - #define UART0_RECEIVE_INTERRUPT USART1_RX_vect - #define UART0_TRANSMIT_INTERRUPT USART1_UDRE_vect - #define UART0_STATUS UCSR1A - #define UART0_CONTROL UCSR1B - #define UART0_DATA UDR1 - #define UART0_UDRIE UDRIE1 -#else - #error "no UART definition for MCU available" -#endif - - -/* - * module global variables - */ -static volatile unsigned char UART_TxBuf[UART0_TX_BUFFER_SIZE]; -static volatile unsigned char UART_RxBuf[UART0_RX_BUFFER_SIZE]; -static volatile unsigned char UART_TxHead; -static volatile unsigned char UART_TxTail; -static volatile unsigned char UART_RxHead; -static volatile unsigned char UART_RxTail; -static volatile unsigned char UART_LastRxError; - -#if defined( ATMEGA_USART1 ) -static volatile unsigned char UART1_TxBuf[UART1_TX_BUFFER_SIZE]; -static volatile unsigned char UART1_RxBuf[UART1_RX_BUFFER_SIZE]; -static volatile unsigned char UART1_TxHead; -static volatile unsigned char UART1_TxTail; -static volatile unsigned char UART1_RxHead; -static volatile unsigned char UART1_RxTail; -static volatile unsigned char UART1_LastRxError; -#endif - - - -ISR (UART0_RECEIVE_INTERRUPT) -/************************************************************************* -Function: UART Receive Complete interrupt -Purpose: called when the UART has received a character -**************************************************************************/ -{ - unsigned char tmphead; - unsigned char data; - unsigned char usr; - unsigned char lastRxError; - - - /* read UART status register and UART data register */ - usr = UART0_STATUS; - data = UART0_DATA; - - /* */ -#if defined( AT90_UART ) - lastRxError = (usr & (_BV(FE)|_BV(DOR)) ); -#elif defined( ATMEGA_USART ) - lastRxError = (usr & (_BV(FE)|_BV(DOR)) ); -#elif defined( ATMEGA_USART0 ) - lastRxError = (usr & (_BV(FE0)|_BV(DOR0)) ); -#elif defined ( ATMEGA_UART ) - lastRxError = (usr & (_BV(FE)|_BV(DOR)) ); -#elif defined( AT90USB_USART ) - lastRxError = (usr & (_BV(FE1)|_BV(DOR1)) ); -#endif - - /* calculate buffer index */ - tmphead = ( UART_RxHead + 1) & UART0_RX_BUFFER_MASK; - - if ( tmphead == UART_RxTail ) { - /* error: receive buffer overflow */ - lastRxError = UART_BUFFER_OVERFLOW >> 8; - }else{ - /* store new index */ - UART_RxHead = tmphead; - /* store received data in buffer */ - UART_RxBuf[tmphead] = data; - } - UART_LastRxError |= lastRxError; -} - - -ISR (UART0_TRANSMIT_INTERRUPT) -/************************************************************************* -Function: UART Data Register Empty interrupt -Purpose: called when the UART is ready to transmit the next byte -**************************************************************************/ -{ - unsigned char tmptail; - - - if ( UART_TxHead != UART_TxTail) { - /* calculate and store new buffer index */ - tmptail = (UART_TxTail + 1) & UART0_TX_BUFFER_MASK; - UART_TxTail = tmptail; - /* get one byte from buffer and write it to UART */ - UART0_DATA = UART_TxBuf[tmptail]; /* start transmission */ - }else{ - /* tx buffer empty, disable UDRE interrupt */ - UART0_CONTROL &= ~_BV(UART0_UDRIE); - } -} - - -/************************************************************************* -Function: uart_init() -Purpose: initialize UART and set baudrate -Input: baudrate using macro UART_BAUD_SELECT() -Returns: none -**************************************************************************/ -void uart_init(unsigned int baudrate) -{ - UART_TxHead = 0; - UART_TxTail = 0; - UART_RxHead = 0; - UART_RxTail = 0; - -#if defined( AT90_UART ) - /* set baud rate */ - UBRR = (unsigned char)baudrate; - - /* enable UART receiver and transmmitter and receive complete interrupt */ - UART0_CONTROL = _BV(RXCIE)|_BV(RXEN)|_BV(TXEN); - -#elif defined (ATMEGA_USART) - /* Set baud rate */ - if ( baudrate & 0x8000 ) - { - UART0_STATUS = (1<>8); - UBRRL = (unsigned char) baudrate; - - /* Enable USART receiver and transmitter and receive complete interrupt */ - UART0_CONTROL = _BV(RXCIE)|(1<>8); - UBRR0L = (unsigned char) baudrate; - - /* Enable USART receiver and transmitter and receive complete interrupt */ - UART0_CONTROL = _BV(RXCIE0)|(1<>8); - UBRR = (unsigned char) baudrate; - - /* Enable UART receiver and transmitter and receive complete interrupt */ - UART0_CONTROL = _BV(RXCIE)|(1<>8); - UBRR1L = (unsigned char) baudrate; - - /* Enable UART receiver and transmitter and receive complete interrupt */ - UART0_CONTROL = _BV(RXCIE1)|(1<> 8; - }else{ - /* store new index */ - UART1_RxHead = tmphead; - /* store received data in buffer */ - UART1_RxBuf[tmphead] = data; - } - UART1_LastRxError |= lastRxError; -} - - -ISR(UART1_TRANSMIT_INTERRUPT) -/************************************************************************* -Function: UART1 Data Register Empty interrupt -Purpose: called when the UART1 is ready to transmit the next byte -**************************************************************************/ -{ - unsigned char tmptail; - - - if ( UART1_TxHead != UART1_TxTail) { - /* calculate and store new buffer index */ - tmptail = (UART1_TxTail + 1) & UART1_TX_BUFFER_MASK; - UART1_TxTail = tmptail; - /* get one byte from buffer and write it to UART */ - UART1_DATA = UART1_TxBuf[tmptail]; /* start transmission */ - }else{ - /* tx buffer empty, disable UDRE interrupt */ - UART1_CONTROL &= ~_BV(UART1_UDRIE); - } -} - - -/************************************************************************* -Function: uart1_init() -Purpose: initialize UART1 and set baudrate -Input: baudrate using macro UART_BAUD_SELECT() -Returns: none -**************************************************************************/ -void uart1_init(unsigned int baudrate) -{ - UART1_TxHead = 0; - UART1_TxTail = 0; - UART1_RxHead = 0; - UART1_RxTail = 0; - - - /* Set baud rate */ - if ( baudrate & 0x8000 ) - { - UART1_STATUS = (1<>8); - UBRR1L = (unsigned char) baudrate; - - /* Enable USART receiver and transmitter and receive complete interrupt */ - UART1_CONTROL = _BV(RXCIE1)|(1< http://jump.to/fleury -File: $Id: uart.h,v 1.12 2012/11/19 19:52:27 peter Exp $ -Software: AVR-GCC 4.1, AVR Libc 1.4 -Hardware: any AVR with built-in UART, tested on AT90S8515 & ATmega8 at 4 Mhz -License: GNU General Public License -Usage: see Doxygen manual - -LICENSE: - Copyright (C) 2006 Peter Fleury - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - any later version. - - 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. - -************************************************************************/ - -/** - * @defgroup pfleury_uart UART Library - * @code #include @endcode - * - * @brief Interrupt UART library using the built-in UART with transmit and receive circular buffers. - * - * This library can be used to transmit and receive data through the built in UART. - * - * An interrupt is generated when the UART has finished transmitting or - * receiving a byte. The interrupt handling routines use circular buffers - * for buffering received and transmitted data. - * - * The UART0[1]_RX_BUFFER_SIZE and UART0[1]_TX_BUFFER_SIZE constants define - * the size of the circular buffers in bytes. Note that these constants must be a power of 2. - * You may need to adapt this constants to your target and your application by adding - * CDEFS += -DUART0[1]_RX_BUFFER_SIZE=nn -DUART0[1]_RX_BUFFER_SIZE=nn to your Makefile. - * - * @note Based on Atmel Application Note AVR306 - * @author Peter Fleury pfleury@gmx.ch http://jump.to/fleury - */ - -/**@{*/ - - -#if (__GNUC__ * 100 + __GNUC_MINOR__) < 304 -#error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !" -#endif - - -/* -** constants and macros -*/ - -/** @brief UART Baudrate Expression - * @param xtalcpu system clock in Mhz, e.g. 4000000UL for 4Mhz - * @param baudrate baudrate in bps, e.g. 1200, 2400, 9600 - */ -#define UART_BAUD_SELECT(baudRate,xtalCpu) (((xtalCpu) + 8UL * (baudRate)) / (16UL * (baudRate)) -1UL) - -/** @brief UART Baudrate Expression for ATmega double speed mode - * @param xtalcpu system clock in Mhz, e.g. 4000000UL for 4Mhz - * @param baudrate baudrate in bps, e.g. 1200, 2400, 9600 - */ -#define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate,xtalCpu) ( ((((xtalCpu) + 4UL * (baudRate)) / (8UL * (baudRate)) -1UL)) | 0x8000) - - -/** Size of the circular receive buffer UART0, must be power of 2 */ -#ifndef UART0_RX_BUFFER_SIZE -#define UART0_RX_BUFFER_SIZE 32 -#endif -/** Size of the circular transmit buffer UART0, must be power of 2 */ -#ifndef UART0_TX_BUFFER_SIZE -#define UART0_TX_BUFFER_SIZE 32 -#endif - -/** Size of the circular receive buffer UART1, must be power of 2 */ -#ifndef UART1_RX_BUFFER_SIZE -#define UART1_RX_BUFFER_SIZE 128 -#endif -/** Size of the circular transmit buffer UART1, must be power of 2 */ -#ifndef UART1_TX_BUFFER_SIZE -#define UART1_TX_BUFFER_SIZE 128 -#endif - -/* test if the size of the circular buffers fits into SRAM */ -#if ( (UART0_RX_BUFFER_SIZE+UART0_TX_BUFFER_SIZE+UART1_RX_BUFFER_SIZE+UART1_TX_BUFFER_SIZE) >= (RAMEND-0x60 ) ) -#error "size of UART0[1]_RX_BUFFER_SIZE + UART0[1]_TX_BUFFER_SIZE larger than size of SRAM" -#endif - -/* -** high byte error return code of uart_getc() -*/ -#define UART_FRAME_ERROR 0x1000 /* Framing Error by UART */ -#define UART_OVERRUN_ERROR 0x0800 /* Overrun condition by UART */ -#define UART_PARITY_ERROR 0x0400 /* Parity Error by UART */ -#define UART_BUFFER_OVERFLOW 0x0200 /* receive ringbuffer overflow */ -#define UART_NO_DATA 0x0100 /* no receive data available */ - - -/* -** function prototypes -*/ - -/** - @brief Initialize UART and set baudrate - @param baudrate Specify baudrate using macro UART_BAUD_SELECT() - @return none -*/ -extern void uart_init(unsigned int baudrate); - - -/** - * @brief Get received byte from ringbuffer - * - * Returns in the lower byte the received character and in the - * higher byte the last receive error. - * UART_NO_DATA is returned when no data is available. - * - * @param void - * @return lower byte: received byte from ringbuffer - * @return higher byte: last receive status - * - \b 0 successfully received data from UART - * - \b UART_NO_DATA - *
no receive data available - * - \b UART_BUFFER_OVERFLOW - *
Receive ringbuffer overflow. - * We are not reading the receive buffer fast enough, - * one or more received character have been dropped - * - \b UART_OVERRUN_ERROR - *
Overrun condition by UART. - * A character already present in the UART UDR register was - * not read by the interrupt handler before the next character arrived, - * one or more received characters have been dropped. - * - \b UART_FRAME_ERROR - *
Framing Error by UART - */ -extern unsigned int uart_getc(void); - - -/** - * @brief Put byte to ringbuffer for transmitting via UART - * @param data byte to be transmitted - * @return none - */ -extern void uart_putc(unsigned char data); - - -/** - * @brief Put string to ringbuffer for transmitting via UART - * - * The string is buffered by the uart library in a circular buffer - * and one character at a time is transmitted to the UART using interrupts. - * Blocks if it can not write the whole string into the circular buffer. - * - * @param s string to be transmitted - * @return none - */ -extern void uart_puts(const char *s ); - - -/** - * @brief Put string from program memory to ringbuffer for transmitting via UART. - * - * The string is buffered by the uart library in a circular buffer - * and one character at a time is transmitted to the UART using interrupts. - * Blocks if it can not write the whole string into the circular buffer. - * - * @param s program memory string to be transmitted - * @return none - * @see uart_puts_P - */ -extern void uart_puts_p(const char *s ); - -/** - * @brief Macro to automatically put a string constant into program memory - */ -#define uart_puts_P(__s) uart_puts_p(PSTR(__s)) - - - -/** @brief Initialize USART1 (only available on selected ATmegas) @see uart_init */ -extern void uart1_init(unsigned int baudrate); -/** @brief Get received byte of USART1 from ringbuffer. (only available on selected ATmega) @see uart_getc */ -extern unsigned int uart1_getc(void); -/** @brief Put byte to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_putc */ -extern void uart1_putc(unsigned char data); -/** @brief Put string to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts */ -extern void uart1_puts(const char *s ); -/** @brief Put string from program memory to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts_p */ -extern void uart1_puts_p(const char *s ); -/** @brief Macro to automatically put a string constant into program memory */ -#define uart1_puts_P(__s) uart1_puts_p(PSTR(__s)) - -/**@}*/ - - -#endif // UART_H -