summaryrefslogtreecommitdiff
path: root/digital/zigbit/common/print.c
blob: 564b4482175d99402dbdd9e40b0daa115c62c839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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 <stdarg.h>
#include <string.h>
#include <usart.h>
#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);
}