From 28f3e60286ae1e272b08d47bbf13f15082752039 Mon Sep 17 00:00:00 2001 From: Florent Duchon Date: Thu, 27 Dec 2012 16:23:47 +0100 Subject: digital/zigbit/common: add common uprintf function for zigbit projects --- .../Makefile_All_ZigBit_Atmega1281_Rf230_8Mhz_Gcc | 2 + digital/zigbit/common/print.c | 87 ++++++++++++++++++++++ digital/zigbit/common/print.h | 57 ++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 digital/zigbit/common/print.c create mode 100644 digital/zigbit/common/print.h diff --git a/digital/zigbit/bitcloud/makefiles/Makefile_All_ZigBit_Atmega1281_Rf230_8Mhz_Gcc b/digital/zigbit/bitcloud/makefiles/Makefile_All_ZigBit_Atmega1281_Rf230_8Mhz_Gcc index a6d216b4..3aae01ef 100644 --- a/digital/zigbit/bitcloud/makefiles/Makefile_All_ZigBit_Atmega1281_Rf230_8Mhz_Gcc +++ b/digital/zigbit/bitcloud/makefiles/Makefile_All_ZigBit_Atmega1281_Rf230_8Mhz_Gcc @@ -26,6 +26,7 @@ endif INCLUDES = \ -I$(PROJECT_BASE) \ + -I$(BITCLOUD_COMMON_SOURCES) \ -I$(BITCLOUD_PATH)/Components/BSP/MESHBEAN/include \ -I$(BITCLOUD_PATH)/lib \ -I$(BITCLOUD_PATH)/Components/HAL/include \ @@ -70,6 +71,7 @@ SRCS = \ $(BITCLOUD_PATH)/Components/ConfigServer/src/csPersistentMem.c \ $(BITCLOUD_PATH)/Components/ConfigServer/src/csMem.c \ $(BITCLOUD_PATH)/Components/ConfigServer/src/configServer.c \ + $(BITCLOUD_COMMON_SOURCES)/print.c \ $(APB_AVR_PATH)/modules/twi/twi_hard.avr.c \ $(APB_AVR_PATH)/modules/twi/twi.c diff --git a/digital/zigbit/common/print.c b/digital/zigbit/common/print.c new file mode 100644 index 00000000..564b4482 --- /dev/null +++ b/digital/zigbit/common/print.c @@ -0,0 +1,87 @@ +/* print.c */ +/* Zigbit common functions for debug {{{ + * + * Copyright (C) 2012 Florent Duchon + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * 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 + * (at your option) 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * }}} */ + +#include +#include +#include +#include "configuration.h" +#include "print.h" + + +extern HAL_UsartDescriptor_t appUsartDescriptor; // USART descriptor (required by stack) +extern uint8_t usartTxBuffer[APP_USART_TX_BUFFER_SIZE]; // USART Tx buffer + + +static TUSART_buffer_level TXbuffer_level = EMPTY; // TX buffer state +static TUSART_bus_state TXbus_state = FREE; // TX line state + +static uint16_t start_offset = 0; // Start offset for TX buffer +static uint16_t end_offset = 0; // Stop offset for TX buffer + + +/* TX USART Callback */ +void usartTXCallback(void) +{ + /* If buffer is not empty continue to send via USART line */ + if(TXbuffer_level != EMPTY) + { + WRITE_USART(&appUsartDescriptor,usartTxBuffer+start_offset,end_offset); + TXbuffer_level = EMPTY; + } + else + { + /* Bus is free so reset variables and flags */ + memset(usartTxBuffer,0,APP_USART_TX_BUFFER_SIZE); + start_offset = 0; + end_offset = 0; + TXbus_state = FREE; + } +} + +/* This function sends data string via the USART interface */ +void uprintf(char *format, ...) +{ + va_list args; + va_start(args,format); + + if(end_offset+strlen(format)+sizeof(args) < APP_USART_TX_BUFFER_SIZE) + { + vsprintf(usartTxBuffer+end_offset,format,args); + end_offset = strlen(usartTxBuffer); + + /* Check if the bus is busy */ + if(TXbus_state == FREE) + { + WRITE_USART(&appUsartDescriptor,usartTxBuffer+start_offset,end_offset); + start_offset = end_offset; + TXbus_state = BUSY; + } + else + { + TXbuffer_level = FILLED; + } + } + va_end(args); +} diff --git a/digital/zigbit/common/print.h b/digital/zigbit/common/print.h new file mode 100644 index 00000000..57106ad7 --- /dev/null +++ b/digital/zigbit/common/print.h @@ -0,0 +1,57 @@ +/* print.h */ +/* Zigbit common functions for debug {{{ + * + * Copyright (C) 2012 Florent Duchon + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * 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 + * (at your option) 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * }}} */ + +#ifndef _PRINT_H +#define _PRINT_H + +#include +#include +#include "configuration.h" + +#define OPEN_USART HAL_OpenUsart +#define CLOSE_USART HAL_CloseUsart +#define WRITE_USART HAL_WriteUsart +#define READ_USART HAL_ReadUsart +#define USART_CHANNEL APP_USART_CHANNEL + +typedef enum +{ + FREE, + BUSY +} TUSART_bus_state; + +typedef enum +{ + EMPTY, + FILLED +} TUSART_buffer_level; + +/* TX USART Callback */ +void usartTXCallback(void); + +/* This function sends data string via the USART interface */ +void uprintf(char *format, ...); + +#endif \ No newline at end of file -- cgit v1.2.3