aboutsummaryrefslogtreecommitdiffstats
path: root/src/gaudin/gaudin.c
blob: 1a3a3434c7dca72ae6e316fe4efd1aa15065794f (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* 
 * This source code form is a part of the ChibiOS/GFX project and stands
 * under the terms of the GFX License v1.0. If a copy of the license
 * was not distributed with this file, You can obtain one at: 
 * 
 * http://chibios-gfx.com/license.html
 *
 */

/**
 * @file    src/gaudin/gaudin.c
 * @brief   GAUDIN sub-system code.
 *
 * @addtogroup GAUDIN
 * @{
 */
#include "ch.h"
#include "hal.h"
#include "gfx.h"

#if GFX_USE_GAUDIN

/* Include the driver defines */
#include "gaudin/lld/gaudin_lld.h"

static gaudin_params	aud;
static BinarySemaphore	*paudSem;
static GEventAudioIn	*paudEvent;
static audin_sample_t	*lastbuffer;
static size_t			lastcount;
static uint16_t			audFlags;
	#define AUDFLG_RUNNING		0x0001
	#define AUDFLG_USE_EVENTS	0x0002

#if GFX_USE_GEVENT
	static GTIMER_DECL(AudGTimer);

	static void AudGTimerCallback(void *param) {
		(void) param;
		GSourceListener	*psl;
		GEventADC		*pe;

		psl = 0;
		while ((psl = geventGetSourceListener((GSourceHandle)(&aud), psl))) {
			if (!(pe = (GEventAudioIn *)geventGetEventBuffer(psl))) {
				// This listener is missing - save this.
				psl->srcflags |= GADC_AUDIO_IN_LOSTEVENT;
				continue;
			}

			pe->type = GEVENT_AUDIO_IN;
			pe->channel = aud.channel;
			pe->count = lastcount;
			pe->buffer = lastbuffer;
			pe->flags = psl->srcflags;
			psl->srcflags = 0;
			geventSendEvent(psl);
		}
	}
#endif

void GAUDIN_ISR_CompleteI(audin_sample_t *buffer, size_t n) {
	/* Save the details */
	lastcount = n;
	lastbuffer = buffer;

	/* Signal the user with the data */
	if (paudEvent) {
		#if GFX_USE_GEVENT
			paudEvent->type = GEVENT_AUDIO_IN;
		#endif
		paudEvent->channel = aud.channel;
		paudEvent->count = lastcount;
		paudEvent->buffer = lastbuffer;
		paudEvent->flags = 0;
	}

	/* Our two signalling mechanisms */
	if (paudSem)
		chBSemSignalI(paudSem);

	#if GFX_USE_GEVENT
		if (audFlags & AUDFLG_USE_EVENTS)
			gtimerJabI(&AudGTimer);
	#endif
}

void GAUDIN_ISR_ErrorI(void) {
	/* Ignore any errors for now */
}

bool_t gaudinInit(uint16_t channel, uint32_t frequency, audin_sample_t *buffer, size_t bufcount, size_t samplesPerEvent) {
	/* Check the channel is valid */
	if (channel >= GAUDIN_NUM_CHANNELS || frequency > GAUDIN_MAX_SAMPLE_FREQUENCY)
		return FALSE;

	/* Stop any existing transfers */
	if ((audFlags & AUDFLG_RUNNING))
		gadc_lld_stop();
	audFlags = 0;

	/* Initialise everything */
	aud.channel = channel;
	aud.frequency = frequency;
	aud.buffer = buffer;
	aud.bufcount = bufcount;
	aud.samplesPerEvent = samplesPerEvent;
	paudSem = 0;
	paudEvent = 0;

	/* Set up the low level driver */
	gaudin_lld_init(&aud);
	return TRUE;
}

#if GFX_USE_GEVENT
	GSourceHandle gaudinGetSource(void) {
		if (!gtimerIsActive(&AudGTimer))
			gtimerStart(&AudGTimer, AudGTimerCallback, NULL, TRUE, TIME_INFINITE);
		audFlags |= AUDFLG_USE_EVENTS;
		return (GSourceHandle)&aud;
	}
#endif

void gaudinSetBSem(BinarySemaphore *pbsem, GEventAudioIn *pEvent) {
	chSysLock();
	paudSem = pbsem;
	paudEvent = pEvent;
	chSysUnlock();
}

void gaudinStart(void) {
	if (!(audFlags & AUDFLG_RUNNING)) {
		audFlags |= AUDFLG_RUNNING;
		gadc_lld_start();
	}
}

void gaudinStop(void) {
	if ((audFlags & AUDFLG_RUNNING)) {
		gadc_lld_stop();
		audFlags &= ~AUDFLG_RUNNING;
	}
}

#endif /* GFX_USE_GAUDIN */
/** @} */