Blame view

DUREXLaunchpad/main.c 4.75 KB
Imanol-Mikel Barba Sabariego authored
1
2
3
4
/*
 * ============ Platform Configuration ============
 */
Imanol-Mikel Barba Sabariego authored
5
#include <msp430.h> 
Imanol-Mikel Barba Sabariego authored
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#define EAP_RX_BUF              UCA0RXBUF
#define EAP_TX_BUF              UCA0TXBUF

#define EAP_RX_VECTOR           USCIAB0RX_VECTOR
#define EAP_TX_VECTOR           PORT2_VECTOR

#define EAP_RX_ACK_CONFIG()     (P2DIR |= BIT0)
#define EAP_RX_ACK_SET()        (P2OUT |= BIT0)
#define EAP_RX_ACK_CLR()        (P2OUT &= ~BIT0)

#define EAP_TX_INT_CONFIG()     (P2DIR &= ~BIT1, P2IES |= BIT1, P2IFG &= BIT1, P2IE |= BIT1)
#define EAP_TX_INT_TST()        (P2IFG & BIT1)
#define EAP_TX_INT_CLR()        (P2IFG &= ~BIT1)
Imanol-Mikel Barba Sabariego authored
21
void init(void) 
Imanol-Mikel Barba Sabariego authored
22
23
24
25
26
27
28
29
30
31
32
33
{
    WDTCTL = WDTPW + WDTHOLD;
    BCSCTL2 = SELM_0 + DIVM_0 + DIVS_0;
    if (CALBC1_1MHZ != 0xFF)
    {
        DCOCTL = 0x00;
        BCSCTL1 = CALBC1_1MHZ;      /* Set DCO to 1MHz */
        DCOCTL = CALDCO_1MHZ;
    }
    BCSCTL1 |= XT2OFF + DIVA_0;
    BCSCTL3 = XT2S_0 + LFXT1S_2 + XCAP_1;
Imanol-Mikel Barba Sabariego authored
34
    P1DIR |= BIT0 + BIT6;  /* LED */
Imanol-Mikel Barba Sabariego authored
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
    P1OUT &= ~BIT0;

    UCA0CTL1 |= UCSWRST;

    P1SEL |= BIT1 + BIT2;
    P1SEL2 |= BIT1 + BIT2;

    EAP_RX_ACK_CONFIG();
    EAP_RX_ACK_SET();

    EAP_TX_INT_CONFIG();

    UCA0CTL1 = UCSSEL_2 + UCSWRST;
    UCA0MCTL = UCBRF_0 + UCBRS_6;
    UCA0BR0 = 8;
    UCA0CTL1 &= ~UCSWRST;

    IFG2 &= ~(UCA0RXIFG);
    IE2 |= UCA0RXIE;

    __enable_interrupt();
}

/*
 * ============ Serial Driver ============
 */

#include <Em_Message.h>

__attribute__((interrupt(EAP_RX_VECTOR)))
static void rxHandler(void)
{
    uint8_t b = EAP_RX_BUF;
    if (Em_Message_addByte(b))
    {
        Em_Message_dispatch();
    }
    EAP_RX_ACK_CLR();
    EAP_RX_ACK_SET();
}

__attribute__((interrupt(EAP_TX_VECTOR)))
static void txHandler(void)
{
    if (EAP_TX_INT_TST())
    {
        uint8_t b;
        if (Em_Message_getByte(&b))
        {
            EAP_TX_BUF = b;
        }
        EAP_TX_INT_CLR();
    }
}

void Em_Message_startSend()
{
    uint8_t b;
    if (Em_Message_getByte(&b))
    {
        UCA0TXBUF = b;
    }
}

uint8_t Em_Message_lock()
{
    uint8_t key;
    asm ("MOV r2, %0": "=r" (key));
    key &= 0x8;
    asm ("DINT");
    return key;
}

void Em_Message_unlock(uint8_t key)
{
    if (key)
    {
        asm ("EINT");
    }
    else
    {
        asm ("DINT");
    }
}

/*
 * Extra code and interrupts
 */
Imanol-Mikel Barba Sabariego authored
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
void led0_toggle(void)
{
	P1OUT ^= BIT0;
}

void led1_toggle(void)
{
	P1OUT ^= BIT6;
}

void led0_on(void)
{
	P1OUT |= BIT0;
}

void led0_off(void)
{
	P1OUT &= ~BIT0;
}

void led1_on(void)
{
	P1OUT |= BIT6;
}

void led1_off(void)
{
	P1OUT &= ~BIT6;
}
Imanol-Mikel Barba Sabariego authored
154
155
156
__attribute__((interrupt(TIMER0_A0_VECTOR)))
static void Timer_A (void)
{
Imanol-Mikel Barba Sabariego authored
157
158
	led0_toggle();					// Toggle LED
	//led1_toggle();
Imanol-Mikel Barba Sabariego authored
159
160
161
162
163
}

void initTimer(void)
{
	CCTL0 = CCIE;						// CCR0 interrupt enabled
Imanol-Mikel Barba Sabariego authored
164
	CCR0 = 2048;						// 32kHz/8/4096 -> 1 sec
Imanol-Mikel Barba Sabariego authored
165
	TACTL = TASSEL_1 + ID_3 + MC_1;		// ACLK, /8, upmode
Imanol-Mikel Barba Sabariego authored
166
	led1_off();
Imanol-Mikel Barba Sabariego authored
167
168
169
170
171
172
173
174
175
176
177
178
}

void stopTimer(void)
{
	CCTL0 ^= CCTL0 ;
}

/*
 * ============ Application Program ============
 */

#include <DUREX.h>
Imanol-Mikel Barba Sabariego authored
179
180
181
182
183
184
185
#include <string.h>

DUREX_numBytes_t			numBytes = 0;
DUREX_data_t				data = "";
DUREX_numPackets_t 			numPackets = 0;
DUREX_messageAvailable_t 	messageAvailable = 0;
uint8_t						lastMessageAck = 1;
Imanol-Mikel Barba Sabariego authored
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

int main(int argc, char *argv[])
{
    volatile int dummy = 0;
    init();
    initTimer();
    DUREX_run();
    while (dummy == 0)
    {
        /* idle */
    }
    return 0;
}

void DUREX_connectHandler(void)
{
	stopTimer();
Imanol-Mikel Barba Sabariego authored
203
204
	led0_on();
	led1_off();
Imanol-Mikel Barba Sabariego authored
205
206
207
208
}

void DUREX_disconnectHandler(void)
{
Imanol-Mikel Barba Sabariego authored
209
210
	led0_off();
	led1_off();
Imanol-Mikel Barba Sabariego authored
211
212
213
    initTimer();
}
Imanol-Mikel Barba Sabariego authored
214
void DUREX_numBytes_fetch(DUREX_numBytes_t* const output)
Imanol-Mikel Barba Sabariego authored
215
{
Imanol-Mikel Barba Sabariego authored
216
    *output = numBytes;
Imanol-Mikel Barba Sabariego authored
217
218
}
Imanol-Mikel Barba Sabariego authored
219
void DUREX_numBytes_store(DUREX_numBytes_t* const input)
Imanol-Mikel Barba Sabariego authored
220
{
Imanol-Mikel Barba Sabariego authored
221
	numBytes = *input;
Imanol-Mikel Barba Sabariego authored
222
223
}
Imanol-Mikel Barba Sabariego authored
224
void DUREX_data_fetch(DUREX_data_t* const output)
Imanol-Mikel Barba Sabariego authored
225
{
Imanol-Mikel Barba Sabariego authored
226
	memcpy(output,data,numBytes);
Imanol-Mikel Barba Sabariego authored
227
228
}
Imanol-Mikel Barba Sabariego authored
229
void DUREX_data_store(DUREX_data_t* const input)
Imanol-Mikel Barba Sabariego authored
230
{
Imanol-Mikel Barba Sabariego authored
231
	memcpy(data,input,numBytes);
Imanol-Mikel Barba Sabariego authored
232
233
}
Imanol-Mikel Barba Sabariego authored
234
void DUREX_numPackets_fetch(DUREX_numPackets_t* const output)
Imanol-Mikel Barba Sabariego authored
235
{
Imanol-Mikel Barba Sabariego authored
236
	*output = numPackets;
Imanol-Mikel Barba Sabariego authored
237
238
}
Imanol-Mikel Barba Sabariego authored
239
void DUREX_numPackets_store(DUREX_numPackets_t* const input)
Imanol-Mikel Barba Sabariego authored
240
{
Imanol-Mikel Barba Sabariego authored
241
    numPackets = *input;
Imanol-Mikel Barba Sabariego authored
242
243
}
Imanol-Mikel Barba Sabariego authored
244
void DUREX_messageAvailable_fetch(DUREX_messageAvailable_t* const output)
Imanol-Mikel Barba Sabariego authored
245
{
Imanol-Mikel Barba Sabariego authored
246
    *output = messageAvailable;
Imanol-Mikel Barba Sabariego authored
247
248
}
Imanol-Mikel Barba Sabariego authored
249
void DUREX_messageAvailable_store(DUREX_messageAvailable_t* const input)
Imanol-Mikel Barba Sabariego authored
250
{
Imanol-Mikel Barba Sabariego authored
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
	messageAvailable = *input;
	if(messageAvailable == DUREX_TRUE)
	{
		led1_on();
		lastMessageAck = 0;
		messageAvailable = DUREX_FALSE;
		DUREX_messageAvailable_indicate();
		memcpy(data,"ACK",4);
		numPackets = 1;
		numBytes = 4;
		messageAvailable = DUREX_TRUE;
		DUREX_messageAvailable_indicate();
	}
	else if(messageAvailable == DUREX_FALSE)
	{
		led1_off();
		lastMessageAck = 1;
	}
Imanol-Mikel Barba Sabariego authored
269
}
Imanol-Mikel Barba Sabariego authored
270