Subversion Repositories svn.mios32

Rev

Rev 1199 | Rev 1508 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1156 tk 1
// $Id: mios32_board.c 1311 2011-08-21 18:34:54Z tk $
2
//! \defgroup MIOS32_BOARD
3
//!
4
//! Development Board specific functions for MIOS32
5
//!
6
//! \{
7
/* ==========================================================================
8
 *
9
 *  Copyright (C) 2011 Thorsten Klose (tk@midibox.org)
10
 *  Licensed for personal non-commercial use only.
11
 *  All other rights reserved.
12
 *
13
 * ==========================================================================
14
 */
15
 
16
/////////////////////////////////////////////////////////////////////////////
17
// Include files
18
/////////////////////////////////////////////////////////////////////////////
19
 
20
#include <mios32.h>
21
 
22
// this module can be optionally disabled in a local mios32_config.h file (included from mios32.h)
23
#if !defined(MIOS32_DONT_USE_BOARD)
24
 
25
 
26
/////////////////////////////////////////////////////////////////////////////
27
// J5 pin mapping
28
/////////////////////////////////////////////////////////////////////////////
29
 
1199 tk 30
#if defined(MIOS32_BOARD_MBHP_CORE_LPC17) || defined(MIOS32_BOARD_LPCXPRESSO)
1156 tk 31
// note: adaptions also have to be done in MIOS32_BOARD_J5_(Set/Get),
32
// since these functions access the ports directly
33
typedef struct {
1199 tk 34
  u8 port;
35
  u8 pin;
1156 tk 36
} j5_pin_t;
37
 
1199 tk 38
#define J5_NUM_PINS 8
1156 tk 39
static const j5_pin_t j5_pin[J5_NUM_PINS] = {
40
  // J5A
1199 tk 41
  { 0, 23 },
42
  { 0, 24 },
43
  { 0, 25 },
44
  { 0, 26 },
1156 tk 45
 
1199 tk 46
  // J5BB
47
  { 1, 30 },
48
  { 1, 31 },
49
  { 0,  3 },
50
  { 0,  2 },
1156 tk 51
};
52
 
53
#else
54
#define J5_NUM_PINS 0
1199 tk 55
#warning "No J5 pins defined for this MIOS32_BOARD"
1156 tk 56
#endif
57
 
58
 
59
/////////////////////////////////////////////////////////////////////////////
1199 tk 60
// J10 pin mapping
1156 tk 61
/////////////////////////////////////////////////////////////////////////////
62
 
1199 tk 63
#if defined(MIOS32_BOARD_MBHP_CORE_LPC17) || defined(MIOS32_BOARD_LPCXPRESSO)
64
// note: adaptions also have to be done in MIOS32_BOARD_J10_(Set/Get),
65
// since these functions access the ports directly
66
typedef struct {
67
  u8 port;
68
  u8 pin;
69
} j10_pin_t;
1156 tk 70
 
1199 tk 71
#define J10_NUM_PINS 8
72
static const j10_pin_t j10_pin[J10_NUM_PINS] = {
73
  // J10
74
  { 2, 2 },
75
  { 2, 3 },
76
  { 2, 4 },
77
  { 2, 5 },
78
  { 2, 6 },
79
  { 2, 7 },
80
  { 2, 8 },
81
  { 1, 18 },
82
};
1156 tk 83
 
1199 tk 84
#else
85
#define J10_NUM_PINS 0
86
#warning "No J10 pins defined for this MIOS32_BOARD"
87
#endif
1156 tk 88
 
89
 
1199 tk 90
/////////////////////////////////////////////////////////////////////////////
1311 tk 91
// J28 pin mapping
92
/////////////////////////////////////////////////////////////////////////////
93
 
94
#if defined(MIOS32_BOARD_MBHP_CORE_LPC17) || defined(MIOS32_BOARD_LPCXPRESSO)
95
// note: adaptions also have to be done in MIOS32_BOARD_J28_(Set/Get),
96
// since these functions access the ports directly
97
typedef struct {
98
  u8 port;
99
  u8 pin;
100
} j28_pin_t;
101
 
102
#define J28_NUM_PINS 4
103
static const j28_pin_t j28_pin[J28_NUM_PINS] = {
104
  // J28
105
  { 2, 13 }, // J28:SDA
106
  { 2, 11 }, // J28:SC
107
  { 2, 12 }, // J28:WS
108
  { 4, 29 }, // J28:MCLK
109
};
110
 
111
#else
112
#define J28_NUM_PINS 0
113
#warning "No J28 pins defined for this MIOS32_BOARD"
114
#endif
115
 
116
 
117
/////////////////////////////////////////////////////////////////////////////
1199 tk 118
// J15 (LCD) pin mapping
119
/////////////////////////////////////////////////////////////////////////////
1156 tk 120
 
1199 tk 121
#if defined(MIOS32_BOARD_MBHP_CORE_LPC17) || defined(MIOS32_BOARD_LPCXPRESSO)
1156 tk 122
 
1199 tk 123
#define J15_AVAILABLE 1
1156 tk 124
 
1199 tk 125
#define J15_SCLK_PORT      1
126
#define J15_SCLK_PIN       25
1156 tk 127
 
1199 tk 128
#define J15_RCLK_PORT      1
129
#define J15_RCLK_PIN       26
1156 tk 130
 
1199 tk 131
#define J15_SER_PORT       1        // also used as DC (data/command select) for serial interfaces
132
#define J15_SER_PIN        28
1156 tk 133
 
1199 tk 134
#define J15_E1_PORT        1        // also used to control SCLK of serial interfaces
135
#define J15_E1_PIN         29
1156 tk 136
 
1199 tk 137
#define J15_E2_PORT        3
138
#define J15_E2_PIN         25
1156 tk 139
 
1199 tk 140
#define J15_RW_PORT        1        // also used to control data output of serial interfaces
141
#define J15_RW_PIN         27
142
 
143
#define J15_D7_PORT        1
144
#define J15_D7_PIN         19
145
 
1156 tk 146
// following macros simplify the access to J15 pins
1199 tk 147
#define J15_PIN_SER(b)  MIOS32_SYS_LPC_PINSET(J15_SER_PORT, J15_SER_PIN, b)
148
#define J15_PIN_E1(b)   MIOS32_SYS_LPC_PINSET(J15_E1_PORT, J15_E1_PIN, b)
149
#define J15_PIN_E2(b)   MIOS32_SYS_LPC_PINSET(J15_E2_PORT, J15_E2_PIN, b)
150
#define J15_PIN_RW(b)   MIOS32_SYS_LPC_PINSET(J15_RW_PORT, J15_RW_PIN, b)
1156 tk 151
 
1199 tk 152
#define J15_PIN_SERLCD_DATAOUT(b) MIOS32_SYS_LPC_PINSET(J15_RW_PORT, J15_RW_PIN, b)
153
#define J15_PIN_SERLCD_SCLK_0     MIOS32_SYS_LPC_PINSET_0(J15_E1_PORT, J15_E1_PIN)
154
#define J15_PIN_SERLCD_SCLK_1     MIOS32_SYS_LPC_PINSET_1(J15_E1_PORT, J15_E1_PIN)
1156 tk 155
 
1199 tk 156
#define J15_PIN_RCLK_0  MIOS32_SYS_LPC_PINSET_0(J15_RCLK_PORT, J15_RCLK_PIN)
157
#define J15_PIN_RCLK_1  MIOS32_SYS_LPC_PINSET_1(J15_RCLK_PORT, J15_RCLK_PIN)
1156 tk 158
 
1199 tk 159
#define J15_PIN_SCLK_0  MIOS32_SYS_LPC_PINSET_0(J15_SCLK_PORT, J15_SCLK_PIN)
160
#define J15_PIN_SCLK_1  MIOS32_SYS_LPC_PINSET_1(J15_SCLK_PORT, J15_SCLK_PIN)
1156 tk 161
 
1199 tk 162
#define J15_PIN_D7_IN   MIOS32_SYS_LPC_PINGET(J15_D7_PORT, J15_D7_PIN)
1156 tk 163
 
164
#else
165
#define J15_AVAILABLE 0
1199 tk 166
#warning "No J15 (LCD) port defined for this MIOS32_BOARD"
1156 tk 167
#endif
168
 
169
 
170
/////////////////////////////////////////////////////////////////////////////
171
// Local variables
172
/////////////////////////////////////////////////////////////////////////////
173
 
174
static u16 j5_enable_mask;
1199 tk 175
static u8  j10_enable_mask;
1311 tk 176
static u8  j28_enable_mask;
1156 tk 177
 
178
 
179
 
180
/////////////////////////////////////////////////////////////////////////////
181
//! Initializes MIOS32_BOARD driver
182
//! \param[in] mode currently only mode 0 supported
183
//! \return < 0 if initialisation failed
184
/////////////////////////////////////////////////////////////////////////////
185
s32 MIOS32_BOARD_Init(u32 mode)
186
{
187
  // currently only mode 0 supported
188
  if( mode != 0 )
189
    return -1; // unsupported mode
190
 
191
  j5_enable_mask = 0;
1199 tk 192
  j10_enable_mask = 0;
1311 tk 193
  j28_enable_mask = 0;
1156 tk 194
 
195
  return 0; // no error
196
}
197
 
198
 
199
/////////////////////////////////////////////////////////////////////////////
1311 tk 200
//! Internally used help function to initialize a pin
201
/////////////////////////////////////////////////////////////////////////////
202
static s32 MIOS32_BOARD_PinInitHlp(u8 port, u8 pin, mios32_board_pin_mode_t mode)
203
{
204
  u8 pinsel = 0;  // default: select GPIO
205
  u8 pinmode = 0; // default: enable pull-up
206
  u8 pindir = 0;  // default: input mode
207
  u8 pinod = 0;   // default: disable open drain
208
 
209
  switch( mode ) {
210
  case MIOS32_BOARD_PIN_MODE_ANALOG:
211
    pinsel = 1; // select ADC
212
    pinmode = 2; // set to floating... doesn't matter, but also doesn't hurt
213
    break;
214
  case MIOS32_BOARD_PIN_MODE_INPUT:
215
    pinmode = 2; // set to floating
216
    break;
217
  case MIOS32_BOARD_PIN_MODE_INPUT_PD:
218
    pinmode = 3; // enable pull-down
219
    break;
220
  case MIOS32_BOARD_PIN_MODE_INPUT_PU:
221
    pinmode = 0; // enable pull-up
222
    break;
223
  case MIOS32_BOARD_PIN_MODE_OUTPUT_PP:
224
    pindir = 1; // output mode
225
    break;
226
  case MIOS32_BOARD_PIN_MODE_OUTPUT_OD:
227
    pindir = 1; // output mode
228
    pinod = 1; // open drain
229
    break;
230
  default:
231
    return -1; // invalid pin mode
232
  }
233
 
234
  MIOS32_SYS_LPC_PINSEL(port, pin, pinsel);
235
  MIOS32_SYS_LPC_PINMODE(port, pin, pinmode);
236
  MIOS32_SYS_LPC_PINDIR(port, pin, pindir);
237
  MIOS32_SYS_LPC_PINMODE_OD(port, pin, pinod);
238
 
239
  return 0; // no error
240
}
241
 
242
 
243
/////////////////////////////////////////////////////////////////////////////
1156 tk 244
//! Initializes LEDs of the board
245
//! \param[in] leds mask contains a flag for each LED which should be initialized<BR>
246
//! <UL>
247
//!   <LI>MBHP_CORE_STM32: 1 LED (flag 0: green)
248
//!   <LI>STM32_PRIMER: 2 LEDs (flag 0: green, flag 1: red)
249
//! </UL>
250
//! \return 0 if initialisation passed
251
//! \return -1 if no LEDs specified for board
252
//! \return -2 if one or more LEDs not available on board
253
/////////////////////////////////////////////////////////////////////////////
254
s32 MIOS32_BOARD_LED_Init(u32 leds)
255
{
1191 tk 256
#if defined(MIOS32_BOARD_LPCXPRESSO) || defined(MIOS32_BOARD_MBHP_CORE_LPC17)
1156 tk 257
  if( leds & 1 ) {
258
    // select GPIO for P0.22
1311 tk 259
    MIOS32_BOARD_PinInitHlp(0, 22, MIOS32_BOARD_PIN_MODE_OUTPUT_PP);
1156 tk 260
  }
261
 
262
  if( leds & 0xfffffffe)
263
    return -2; // LED doesn't exist
264
 
265
  return 0; // no error
266
#else
1191 tk 267
#warning "no LED specified for this MIOS32_BOARD!"
268
  return -1;
1156 tk 269
#endif
270
}
271
 
272
 
273
/////////////////////////////////////////////////////////////////////////////
274
//! Sets one or more LEDs to the given value(s)
275
//! \param[in] leds mask contains a flag for each LED which should be changed
276
//! \param[in] value contains the value which should be set
277
//! \return 0 if initialisation passed
278
//! \return -1 if no LEDs specified for board
279
//! \return -2 if one or more LEDs not available on board
280
/////////////////////////////////////////////////////////////////////////////
281
s32 MIOS32_BOARD_LED_Set(u32 leds, u32 value)
282
{
1191 tk 283
#if defined(MIOS32_BOARD_LPCXPRESSO) || defined(MIOS32_BOARD_MBHP_CORE_LPC17)
1156 tk 284
  // only one LED, connected to P0.22
285
  if( leds & 1 ) {
1199 tk 286
    MIOS32_SYS_LPC_PINSET(0, 22, value & 1);
1156 tk 287
  }
288
 
289
  if( leds & 0xfffffffe)
290
    return -2; // LED doesn't exist
291
 
292
  return 0; // no error
293
#else
1191 tk 294
#warning "no LED specified for this MIOS32_BOARD!"
295
  return -1;
1156 tk 296
#endif
297
}
298
 
299
 
300
/////////////////////////////////////////////////////////////////////////////
301
//! Returns the status of all LEDs
302
//! \return status of all LEDs
303
/////////////////////////////////////////////////////////////////////////////
304
u32 MIOS32_BOARD_LED_Get(void)
305
{
306
  u32 values = 0;
307
 
1191 tk 308
#if defined(MIOS32_BOARD_LPCXPRESSO) || defined(MIOS32_BOARD_MBHP_CORE_LPC17)
1156 tk 309
  // only one LED, connected to P0.22
1199 tk 310
  if( MIOS32_SYS_LPC_PINGET(0, 22) )
1156 tk 311
    values |= (1 << 0);
312
#else
1191 tk 313
#warning "no LED specified for this MIOS32_BOARD!"
314
  return -1;
1156 tk 315
#endif
316
 
317
  return values;
318
}
319
 
320
 
321
/////////////////////////////////////////////////////////////////////////////
322
//! Initializes a J5 pin
1199 tk 323
//! \param[in] pin the pin number (0..7)
1156 tk 324
//! \param[in] mode the pin mode
325
//!   <UL>
326
//!     <LI>MIOS32_BOARD_PIN_MODE_IGNORE: configuration shouldn't be touched
327
//!     <LI>MIOS32_BOARD_PIN_MODE_ANALOG: select analog input mode (default)
328
//!     <LI>MIOS32_BOARD_PIN_MODE_INPUT: pin is used as input w/o pull device (floating)
329
//!     <LI>MIOS32_BOARD_PIN_MODE_INPUT_PD: pin is used as input, internal pull down enabled
330
//!     <LI>MIOS32_BOARD_PIN_MODE_INPUT_PU: pin is used as input, internal pull up enabled
331
//!     <LI>MIOS32_BOARD_PIN_MODE_OUTPUT_PP: pin is used as output in push-pull mode
332
//!     <LI>MIOS32_BOARD_PIN_MODE_OUTPUT_OD: pin is used as output in open drain mode
333
//!   </UL>
334
//! \return < 0 if initialisation failed
335
/////////////////////////////////////////////////////////////////////////////
336
s32 MIOS32_BOARD_J5_PinInit(u8 pin, mios32_board_pin_mode_t mode)
337
{
338
#if J5_NUM_PINS == 0
339
  return -1; // MIOS32_BOARD_J5 not supported
340
#else
341
  if( pin >= J5_NUM_PINS )
342
    return -1; // pin not supported
343
 
344
  if( mode == MIOS32_BOARD_PIN_MODE_IGNORE ) {
345
    // don't touch
346
    j5_enable_mask &= ~(1 << pin);
347
  } else {
348
    // enable pin
349
    j5_enable_mask |= (1 << pin);
350
 
1199 tk 351
    j5_pin_t *p = (j5_pin_t *)&j5_pin[pin];
1156 tk 352
 
1311 tk 353
    if( MIOS32_BOARD_PinInitHlp(p->port, p->pin, mode) < 0 )
354
      return -2; // invalid pin mode
1156 tk 355
 
356
#if 0
357
    // set pin value to 0
358
    // This should be done before IO mode configuration, because
359
    // in input mode, this bit will control Pull Up/Down (configured
360
    // by GPIO_Init)
1199 tk 361
    MIOS32_SYS_LPC_PINSET(p->port, p->pin, 0);
1156 tk 362
    // TK: disabled since there are application which have to switch between Input/Output
363
    // without destroying the current pin value
364
#endif
365
  }
366
 
367
  return 0; // no error
368
#endif
369
}
370
 
371
 
372
/////////////////////////////////////////////////////////////////////////////
373
//! This function sets all pins of J5 at once
1199 tk 374
//! \param[in] value 8 bits which are forwarded to J5A/B
1156 tk 375
//! \return < 0 on errors
376
/////////////////////////////////////////////////////////////////////////////
377
s32 MIOS32_BOARD_J5_Set(u16 value)
378
{
379
#if J5_NUM_PINS == 0
380
  return -1; // MIOS32_BOARD_J5 not supported
381
#else
1199 tk 382
# if defined(MIOS32_BOARD_MBHP_CORE_LPC17) || defined(MIOS32_BOARD_LPCXPRESSO)
383
  // J5A[3:0] -> P0[26:23]
384
  // J5B[1:0] -> P1[31:30]
385
  // J5B[2] -> P0[3]
386
  // J5B[3] -> P0[2]
1156 tk 387
 
1199 tk 388
  LPC_GPIO0->FIOSET =
389
    (( value & j5_enable_mask & 0x000f) << 23) |  // set flags
390
    (( value & j5_enable_mask & 0x0040) >>  3) |  // set flags
391
    (( value & j5_enable_mask & 0x0080) >>  5);
1156 tk 392
 
1199 tk 393
  LPC_GPIO1->FIOSET =
394
    (( value & j5_enable_mask & 0x0030) << (30-4)); // set flags
1156 tk 395
 
1199 tk 396
  LPC_GPIO0->FIOCLR =
397
    ((~value & j5_enable_mask & 0x000f) << 23) |  // clear flags
398
    ((~value & j5_enable_mask & 0x0040) >>  3) |  // clear flags
399
    ((~value & j5_enable_mask & 0x0080) >>  5);
1156 tk 400
 
1199 tk 401
  LPC_GPIO1->FIOCLR =
402
    ((~value & j5_enable_mask & 0x0030) << (30-4)); // clear flags
1156 tk 403
 
404
  return 0; // no error
405
# else
1199 tk 406
# warning "Not prepared for this MIOS32_BOARD"
1156 tk 407
  return -2; // board not supported
408
# endif
409
#endif
410
}
411
 
412
 
413
/////////////////////////////////////////////////////////////////////////////
414
//! This function sets a single pin of J5
1199 tk 415
//! \param[in] pin the pin number (0..7)
1156 tk 416
//! \param[in] value the pin value (0 or 1)
417
//! \return < 0 on errors
418
/////////////////////////////////////////////////////////////////////////////
419
s32 MIOS32_BOARD_J5_PinSet(u8 pin, u8 value)
420
{
421
#if J5_NUM_PINS == 0
422
  return -1; // MIOS32_BOARD_J5 not supported
423
#else
424
  if( pin >= J5_NUM_PINS )
425
    return -1; // pin not supported
426
 
427
  if( !(j5_enable_mask & (1 << pin)) )
428
    return -2; // pin disabled
429
 
1199 tk 430
  j5_pin_t *p = (j5_pin_t *)&j5_pin[pin];
431
  MIOS32_SYS_LPC_PINSET(p->port, p->pin, value);
1156 tk 432
 
433
  return 0; // no error
434
#endif
435
}
436
 
437
 
438
/////////////////////////////////////////////////////////////////////////////
439
//! This function returns the state of all pins of J5
1199 tk 440
//! \return 8 bits which are forwarded from J5A/B
1156 tk 441
/////////////////////////////////////////////////////////////////////////////
442
s32 MIOS32_BOARD_J5_Get(void)
443
{
444
#if J5_NUM_PINS == 0
445
  return -1; // MIOS32_BOARD_J5 not supported
446
#else
1199 tk 447
# if defined(MIOS32_BOARD_MBHP_CORE_LPC17) || defined(MIOS32_BOARD_LPCXPRESSO)
448
  // J5A[3:0] -> P0[26:23]
449
  // J5B[1:0] -> P1[31:30]
450
  // J5B[2] -> P0[3]
451
  // J5B[3] -> P0[2]
1156 tk 452
 
1199 tk 453
  u32 p0 = LPC_GPIO0->FIOPIN;
454
  u32 p1 = LPC_GPIO1->FIOPIN;
1156 tk 455
  return
1199 tk 456
    (((p0 >> (23-0)) & 0x000f) |
457
     ((p0 >> (30-4)) & 0x0030) |
458
     ((p1 << (6-3))  & 0x0040) |
459
     ((p1 << (7-2))  & 0x0080));
1156 tk 460
# else
1199 tk 461
# warning "Not prepared for this MIOS32_BOARD"
1156 tk 462
  return -2; // board not supported
463
# endif
464
#endif
465
}
466
 
467
 
468
/////////////////////////////////////////////////////////////////////////////
469
//! This function returns the state of a single pin of J5
1199 tk 470
//! \param[in] pin the pin number (0..7)
1156 tk 471
//! \return < 0 if pin not available
472
//! \return >= 0: input state of pin
473
/////////////////////////////////////////////////////////////////////////////
474
s32 MIOS32_BOARD_J5_PinGet(u8 pin)
475
{
476
#if J5_NUM_PINS == 0
477
  return -1; // MIOS32_BOARD_J5 not supported
478
#else
479
  if( pin >= J5_NUM_PINS )
480
    return -1; // pin not supported
481
 
482
  if( !(j5_enable_mask & (1 << pin)) )
483
    return -2; // pin disabled
484
 
1199 tk 485
  j5_pin_t *p = (j5_pin_t *)&j5_pin[pin];
486
  return MIOS32_SYS_LPC_PINGET(p->port, p->pin) ? 1 : 0;
1156 tk 487
#endif
488
}
489
 
490
 
491
 
492
 
493
/////////////////////////////////////////////////////////////////////////////
1199 tk 494
//! Initializes a J10 pin
495
//! \param[in] pin the pin number (0..7)
496
//! \param[in] mode the pin mode
497
//!   <UL>
498
//!     <LI>MIOS32_BOARD_PIN_MODE_IGNORE: configuration shouldn't be touched
499
//!     <LI>MIOS32_BOARD_PIN_MODE_ANALOG: select analog input mode (default)
500
//!     <LI>MIOS32_BOARD_PIN_MODE_INPUT: pin is used as input w/o pull device (floating)
501
//!     <LI>MIOS32_BOARD_PIN_MODE_INPUT_PD: pin is used as input, internal pull down enabled
502
//!     <LI>MIOS32_BOARD_PIN_MODE_INPUT_PU: pin is used as input, internal pull up enabled
503
//!     <LI>MIOS32_BOARD_PIN_MODE_OUTPUT_PP: pin is used as output in push-pull mode
504
//!     <LI>MIOS32_BOARD_PIN_MODE_OUTPUT_OD: pin is used as output in open drain mode
505
//!   </UL>
506
//! \return < 0 if initialisation failed
507
/////////////////////////////////////////////////////////////////////////////
508
s32 MIOS32_BOARD_J10_PinInit(u8 pin, mios32_board_pin_mode_t mode)
509
{
510
#if J10_NUM_PINS == 0
511
  return -1; // MIOS32_BOARD_J10 not supported
512
#else
513
  if( pin >= J10_NUM_PINS )
514
    return -1; // pin not supported
515
 
516
  if( mode == MIOS32_BOARD_PIN_MODE_IGNORE ) {
517
    // don't touch
518
    j10_enable_mask &= ~(1 << pin);
519
  } else {
520
    // enable pin
521
    j10_enable_mask |= (1 << pin);
522
 
523
    j10_pin_t *p = (j10_pin_t *)&j10_pin[pin];
524
 
1311 tk 525
    if( MIOS32_BOARD_PinInitHlp(p->port, p->pin, mode) < 0 )
526
      return -2; // invalid pin mode
1199 tk 527
 
528
#if 0
529
    // set pin value to 0
530
    // This should be done before IO mode configuration, because
531
    // in input mode, this bit will control Pull Up/Down (configured
532
    // by GPIO_Init)
533
    MIOS32_SYS_LPC_PINSET(p->port, p->pin, 0);
534
    // TK: disabled since there are application which have to switch between Input/Output
535
    // without destroying the current pin value
536
#endif
537
  }
538
 
539
  return 0; // no error
540
#endif
541
}
542
 
543
 
544
/////////////////////////////////////////////////////////////////////////////
545
//! This function sets all pins of J10 at once
546
//! \param[in] value 8 bits which are forwarded to J10
547
//! \return < 0 on errors
548
/////////////////////////////////////////////////////////////////////////////
549
s32 MIOS32_BOARD_J10_Set(u16 value)
550
{
551
#if J10_NUM_PINS == 0
552
  return -1; // MIOS32_BOARD_J10 not supported
553
#else
554
# if defined(MIOS32_BOARD_MBHP_CORE_LPC17) || defined(MIOS32_BOARD_LPCXPRESSO)
555
  // J10[6:0] -> P2[8:2]
556
  // J10[7] -> P1[18]
557
 
558
  LPC_GPIO2->FIOSET =
559
    (( value & j10_enable_mask & 0x007f) << 2); // set flags
560
 
561
  LPC_GPIO1->FIOSET =
562
    (( value & j10_enable_mask & 0x0080) << (18-7)); // set flags
563
 
564
  LPC_GPIO2->FIOCLR =
565
    ((~value & j10_enable_mask & 0x007f) << 2); // clear flags
566
 
567
  LPC_GPIO1->FIOCLR =
568
    ((~value & j10_enable_mask & 0x0080) << (18-7)); // clear flags
569
 
570
  return 0; // no error
571
# else
572
# warning "Not prepared for this MIOS32_BOARD"
573
  return -2; // board not supported
574
# endif
575
#endif
576
}
577
 
578
 
579
/////////////////////////////////////////////////////////////////////////////
580
//! This function sets a single pin of J10
581
//! \param[in] pin the pin number (0..7)
582
//! \param[in] value the pin value (0 or 1)
583
//! \return < 0 on errors
584
/////////////////////////////////////////////////////////////////////////////
585
s32 MIOS32_BOARD_J10_PinSet(u8 pin, u8 value)
586
{
587
#if J10_NUM_PINS == 0
588
  return -1; // MIOS32_BOARD_J10 not supported
589
#else
590
  if( pin >= J10_NUM_PINS )
591
    return -1; // pin not supported
592
 
593
  if( !(j10_enable_mask & (1 << pin)) )
594
    return -2; // pin disabled
595
 
596
  j10_pin_t *p = (j10_pin_t *)&j10_pin[pin];
597
  MIOS32_SYS_LPC_PINSET(p->port, p->pin, value);
598
 
599
  return 0; // no error
600
#endif
601
}
602
 
603
 
604
/////////////////////////////////////////////////////////////////////////////
605
//! This function returns the state of all pins of J10
606
//! \return 8 bits which are forwarded from J10
607
/////////////////////////////////////////////////////////////////////////////
608
s32 MIOS32_BOARD_J10_Get(void)
609
{
610
#if J10_NUM_PINS == 0
611
  return -1; // MIOS32_BOARD_J10 not supported
612
#else
613
# if defined(MIOS32_BOARD_MBHP_CORE_LPC17) || defined(MIOS32_BOARD_LPCXPRESSO)
614
  // J10[6:0] -> P2[8:2]
615
  // J10[7] -> P1[18]
616
 
617
  u32 p2 = LPC_GPIO2->FIOPIN;
618
  u32 p1 = LPC_GPIO1->FIOPIN;
619
  return
620
    (((p2 >> (2)) & 0x007f) |
621
     ((p1 >> (18-7)) & 0x0080));
622
# else
623
# warning "Not prepared for this MIOS32_BOARD"
624
  return -2; // board not supported
625
# endif
626
#endif
627
}
628
 
629
 
630
/////////////////////////////////////////////////////////////////////////////
631
//! This function returns the state of a single pin of J10
632
//! \param[in] pin the pin number (0..7)
633
//! \return < 0 if pin not available
634
//! \return >= 0: input state of pin
635
/////////////////////////////////////////////////////////////////////////////
636
s32 MIOS32_BOARD_J10_PinGet(u8 pin)
637
{
638
#if J10_NUM_PINS == 0
639
  return -1; // MIOS32_BOARD_J10 not supported
640
#else
641
  if( pin >= J10_NUM_PINS )
642
    return -1; // pin not supported
643
 
644
  if( !(j10_enable_mask & (1 << pin)) )
645
    return -2; // pin disabled
646
 
647
  j10_pin_t *p = (j10_pin_t *)&j10_pin[pin];
648
  return MIOS32_SYS_LPC_PINGET(p->port, p->pin) ? 1 : 0;
649
#endif
650
}
651
 
652
 
653
 
654
 
655
/////////////////////////////////////////////////////////////////////////////
1311 tk 656
//! Initializes a J28 pin
657
//! \param[in] pin the pin number (0..3)
658
//! \param[in] mode the pin mode
659
//!   <UL>
660
//!     <LI>MIOS32_BOARD_PIN_MODE_IGNORE: configuration shouldn't be touched
661
//!     <LI>MIOS32_BOARD_PIN_MODE_ANALOG: select analog input mode (default)
662
//!     <LI>MIOS32_BOARD_PIN_MODE_INPUT: pin is used as input w/o pull device (floating)
663
//!     <LI>MIOS32_BOARD_PIN_MODE_INPUT_PD: pin is used as input, internal pull down enabled
664
//!     <LI>MIOS32_BOARD_PIN_MODE_INPUT_PU: pin is used as input, internal pull up enabled
665
//!     <LI>MIOS32_BOARD_PIN_MODE_OUTPUT_PP: pin is used as output in push-pull mode
666
//!     <LI>MIOS32_BOARD_PIN_MODE_OUTPUT_OD: pin is used as output in open drain mode
667
//!   </UL>
668
//! \return < 0 if initialisation failed
669
/////////////////////////////////////////////////////////////////////////////
670
s32 MIOS32_BOARD_J28_PinInit(u8 pin, mios32_board_pin_mode_t mode)
671
{
672
#if J28_NUM_PINS == 0
673
  return -1; // MIOS32_BOARD_J28 not supported
674
#else
675
  if( pin >= J28_NUM_PINS )
676
    return -1; // pin not supported
677
 
678
  if( mode == MIOS32_BOARD_PIN_MODE_IGNORE ) {
679
    // don't touch
680
    j28_enable_mask &= ~(1 << pin);
681
  } else {
682
    // enable pin
683
    j28_enable_mask |= (1 << pin);
684
 
685
    j28_pin_t *p = (j28_pin_t *)&j28_pin[pin];
686
 
687
    if( MIOS32_BOARD_PinInitHlp(p->port, p->pin, mode) < 0 )
688
      return -2; // invalid pin mode
689
 
690
#if 0
691
    // set pin value to 0
692
    // This should be done before IO mode configuration, because
693
    // in input mode, this bit will control Pull Up/Down (configured
694
    // by GPIO_Init)
695
    MIOS32_SYS_LPC_PINSET(p->port, p->pin, 0);
696
    // TK: disabled since there are application which have to switch between Input/Output
697
    // without destroying the current pin value
698
#endif
699
  }
700
 
701
  return 0; // no error
702
#endif
703
}
704
 
705
 
706
/////////////////////////////////////////////////////////////////////////////
707
//! This function sets all pins of J28 at once
708
//! \param[in] value 8 bits which are forwarded to J28
709
//! \return < 0 on errors
710
/////////////////////////////////////////////////////////////////////////////
711
s32 MIOS32_BOARD_J28_Set(u16 value)
712
{
713
#if J28_NUM_PINS == 0
714
  return -1; // MIOS32_BOARD_J28 not supported
715
#else
716
# if defined(MIOS32_BOARD_MBHP_CORE_LPC17) || defined(MIOS32_BOARD_LPCXPRESSO)
717
  LPC_GPIO2->FIOSET =
718
    (( value & j28_enable_mask & 0x0001) << 13) | // set flags
719
    (( value & j28_enable_mask & 0x0006) << (11-1));
720
 
721
  LPC_GPIO4->FIOSET =
722
    (( value & j28_enable_mask & 0x0008) << (29-3)); // set flags
723
 
724
  LPC_GPIO2->FIOCLR =
725
    ((~value & j28_enable_mask & 0x0001) << 13) | // clear flags
726
    ((~value & j28_enable_mask & 0x0006) << (11-1));
727
 
728
  LPC_GPIO4->FIOCLR =
729
    ((~value & j28_enable_mask & 0x0008) << (29-3)); // clear flags
730
 
731
  return 0; // no error
732
# else
733
# warning "Not prepared for this MIOS32_BOARD"
734
  return -2; // board not supported
735
# endif
736
#endif
737
}
738
 
739
 
740
/////////////////////////////////////////////////////////////////////////////
741
//! This function sets a single pin of J28
742
//! \param[in] pin the pin number (0..7)
743
//! \param[in] value the pin value (0 or 1)
744
//! \return < 0 on errors
745
/////////////////////////////////////////////////////////////////////////////
746
s32 MIOS32_BOARD_J28_PinSet(u8 pin, u8 value)
747
{
748
#if J28_NUM_PINS == 0
749
  return -1; // MIOS32_BOARD_J28 not supported
750
#else
751
  if( pin >= J28_NUM_PINS )
752
    return -1; // pin not supported
753
 
754
  if( !(j28_enable_mask & (1 << pin)) )
755
    return -2; // pin disabled
756
 
757
  j28_pin_t *p = (j28_pin_t *)&j28_pin[pin];
758
  MIOS32_SYS_LPC_PINSET(p->port, p->pin, value);
759
 
760
  return 0; // no error
761
#endif
762
}
763
 
764
 
765
/////////////////////////////////////////////////////////////////////////////
766
//! This function returns the state of all pins of J28
767
//! \return 8 bits which are forwarded from J28
768
/////////////////////////////////////////////////////////////////////////////
769
s32 MIOS32_BOARD_J28_Get(void)
770
{
771
#if J28_NUM_PINS == 0
772
  return -1; // MIOS32_BOARD_J28 not supported
773
#else
774
# if defined(MIOS32_BOARD_MBHP_CORE_LPC17) || defined(MIOS32_BOARD_LPCXPRESSO)
775
  u32 p2 = LPC_GPIO2->FIOPIN;
776
  u32 p4 = LPC_GPIO4->FIOPIN;
777
  return
778
    ((p2 >> (13)) & 0x0001) |
779
    ((p2 >> (11-1)) & 0x0006) |
780
    ((p4 >> (29-3)) & 0x0008);
781
# else
782
# warning "Not prepared for this MIOS32_BOARD"
783
  return -2; // board not supported
784
# endif
785
#endif
786
}
787
 
788
 
789
/////////////////////////////////////////////////////////////////////////////
790
//! This function returns the state of a single pin of J28
791
//! \param[in] pin the pin number (0..7)
792
//! \return < 0 if pin not available
793
//! \return >= 0: input state of pin
794
/////////////////////////////////////////////////////////////////////////////
795
s32 MIOS32_BOARD_J28_PinGet(u8 pin)
796
{
797
#if J28_NUM_PINS == 0
798
  return -1; // MIOS32_BOARD_J28 not supported
799
#else
800
  if( pin >= J28_NUM_PINS )
801
    return -1; // pin not supported
802
 
803
  if( !(j28_enable_mask & (1 << pin)) )
804
    return -2; // pin disabled
805
 
806
  j28_pin_t *p = (j28_pin_t *)&j28_pin[pin];
807
  return MIOS32_SYS_LPC_PINGET(p->port, p->pin) ? 1 : 0;
808
#endif
809
}
810
 
811
 
812
 
813
 
814
/////////////////////////////////////////////////////////////////////////////
1156 tk 815
//! Initializes the J15 port
816
//! \param[in] mode 
817
//! <UL>
818
//!   <LI>0: J15 pins are configured in Push Pull Mode (3.3V)
819
//!   <LI>1: J15 pins are configured in Open Drain mode (perfect for 3.3V->5V levelshifting)
820
//! </UL>
821
//! \return < 0 if initialisation failed
822
/////////////////////////////////////////////////////////////////////////////
823
s32 MIOS32_BOARD_J15_PortInit(u32 mode)
824
{
825
#if J15_AVAILABLE == 0
826
  return -1; // LCD port not available
827
#else
828
  // currently only mode 0 and 1 supported
829
  if( mode != 0 && mode != 1 )
830
    return -1; // unsupported mode
831
 
832
  J15_PIN_SCLK_0;
833
  J15_PIN_RCLK_0;
834
  J15_PIN_RW(0);
835
  J15_PIN_E1(0);
836
  J15_PIN_E2(0);
837
 
1311 tk 838
  // configure pins
839
  MIOS32_BOARD_PinInitHlp(J15_SCLK_PORT, J15_SCLK_PIN, MIOS32_BOARD_PIN_MODE_OUTPUT_PP);
840
  MIOS32_BOARD_PinInitHlp(J15_RCLK_PORT, J15_RCLK_PIN, MIOS32_BOARD_PIN_MODE_OUTPUT_PP);
841
  MIOS32_BOARD_PinInitHlp(J15_SER_PORT,  J15_SER_PIN,  mode ? MIOS32_BOARD_PIN_MODE_OUTPUT_OD : MIOS32_BOARD_PIN_MODE_OUTPUT_PP);
842
  MIOS32_BOARD_PinInitHlp(J15_E1_PORT,   J15_E1_PIN,   mode ? MIOS32_BOARD_PIN_MODE_OUTPUT_OD : MIOS32_BOARD_PIN_MODE_OUTPUT_PP);
843
  MIOS32_BOARD_PinInitHlp(J15_E2_PORT,   J15_E2_PIN,   mode ? MIOS32_BOARD_PIN_MODE_OUTPUT_OD : MIOS32_BOARD_PIN_MODE_OUTPUT_PP);
844
  MIOS32_BOARD_PinInitHlp(J15_RW_PORT,   J15_RW_PIN,   mode ? MIOS32_BOARD_PIN_MODE_OUTPUT_OD : MIOS32_BOARD_PIN_MODE_OUTPUT_PP);
1156 tk 845
 
846
  // configure "busy" input with pull-up
1311 tk 847
  MIOS32_BOARD_PinInitHlp(J15_D7_PORT,   J15_D7_PIN,   MIOS32_BOARD_PIN_MODE_INPUT_PU);
1156 tk 848
 
849
  return 0; // no error
850
#endif
851
}
852
 
853
 
854
/////////////////////////////////////////////////////////////////////////////
855
//! This function is used by LCD drivers under $MIOS32_PATH/modules/app_lcd
856
//! to output an 8bit value on the data lines
857
//! \param[in] data the 8bit value
858
//! \return < 0 if access to data port not supported by board
859
/////////////////////////////////////////////////////////////////////////////
860
s32 MIOS32_BOARD_J15_DataSet(u8 data)
861
{
862
#if J15_AVAILABLE == 0
863
  return -1; // LCD port not available
864
#else
865
  // shift in 8bit data
866
  // whole function takes ca. 1.5 uS @ 72MHz
867
  // thats acceptable for a (C)LCD, which is normaly busy after each access for ca. 20..40 uS
868
 
869
  J15_PIN_SER(data & 0x80); // D7
870
  J15_PIN_SCLK_0; // setup delay
871
  J15_PIN_SCLK_1;
872
  J15_PIN_SER(data & 0x40); // D6
873
  J15_PIN_SCLK_0; // setup delay
874
  J15_PIN_SCLK_1;
875
  J15_PIN_SER(data & 0x20); // D5
876
  J15_PIN_SCLK_0; // setup delay
877
  J15_PIN_SCLK_1;
878
  J15_PIN_SER(data & 0x10); // D4
879
  J15_PIN_SCLK_0; // setup delay
880
  J15_PIN_SCLK_1;
881
  J15_PIN_SER(data & 0x08); // D3
882
  J15_PIN_SCLK_0; // setup delay
883
  J15_PIN_SCLK_1;
884
  J15_PIN_SER(data & 0x04); // D2
885
  J15_PIN_SCLK_0; // setup delay
886
  J15_PIN_SCLK_1;
887
  J15_PIN_SER(data & 0x02); // D1
888
  J15_PIN_SCLK_0; // setup delay
889
  J15_PIN_SCLK_1;
890
  J15_PIN_SER(data & 0x01); // D0
891
  J15_PIN_SCLK_0; // setup delay
892
  J15_PIN_SCLK_1;
893
 
894
  // transfer to output register
895
  J15_PIN_RCLK_1;
896
  J15_PIN_RCLK_1;
897
  J15_PIN_RCLK_1;
898
  J15_PIN_RCLK_0;
899
 
900
  return 0; // no error
901
#endif
902
}
903
 
904
 
905
/////////////////////////////////////////////////////////////////////////////
906
//! This function is used by LCD drivers under $MIOS32_PATH/modules/app_lcd
907
//! to shift an 8bit data value to LCDs with serial interface
908
//! (SCLK connected to J15A:E, Data line connected to J15A:RW)
909
//! \param[in] data the 8bit value
910
//! \return < 0 if access to data port not supported by board
911
/////////////////////////////////////////////////////////////////////////////
912
s32 MIOS32_BOARD_J15_SerDataShift(u8 data)
913
{
914
#if J15_AVAILABLE == 0
915
  return -1; // LCD port not available
916
#else
917
  J15_PIN_SERLCD_DATAOUT(data & 0x80); // D7
918
  J15_PIN_SERLCD_SCLK_0; // setup delay
1199 tk 919
  J15_PIN_SERLCD_SCLK_0;
920
  J15_PIN_SERLCD_SCLK_0;
921
  J15_PIN_SERLCD_SCLK_0;
1156 tk 922
  J15_PIN_SERLCD_SCLK_1;
1199 tk 923
  J15_PIN_SERLCD_SCLK_1;
1156 tk 924
  J15_PIN_SERLCD_DATAOUT(data & 0x40); // D6
925
  J15_PIN_SERLCD_SCLK_0; // setup delay
1199 tk 926
  J15_PIN_SERLCD_SCLK_0;
927
  J15_PIN_SERLCD_SCLK_0;
928
  J15_PIN_SERLCD_SCLK_0;
1156 tk 929
  J15_PIN_SERLCD_SCLK_1;
1199 tk 930
  J15_PIN_SERLCD_SCLK_1;
1156 tk 931
  J15_PIN_SERLCD_DATAOUT(data & 0x20); // D5
932
  J15_PIN_SERLCD_SCLK_0; // setup delay
1199 tk 933
  J15_PIN_SERLCD_SCLK_0;
934
  J15_PIN_SERLCD_SCLK_0;
935
  J15_PIN_SERLCD_SCLK_0;
1156 tk 936
  J15_PIN_SERLCD_SCLK_1;
1199 tk 937
  J15_PIN_SERLCD_SCLK_1;
1156 tk 938
  J15_PIN_SERLCD_DATAOUT(data & 0x10); // D4
939
  J15_PIN_SERLCD_SCLK_0; // setup delay
1199 tk 940
  J15_PIN_SERLCD_SCLK_0;
941
  J15_PIN_SERLCD_SCLK_0;
942
  J15_PIN_SERLCD_SCLK_0;
1156 tk 943
  J15_PIN_SERLCD_SCLK_1;
1199 tk 944
  J15_PIN_SERLCD_SCLK_1;
1156 tk 945
  J15_PIN_SERLCD_DATAOUT(data & 0x08); // D3
946
  J15_PIN_SERLCD_SCLK_0; // setup delay
1199 tk 947
  J15_PIN_SERLCD_SCLK_0;
948
  J15_PIN_SERLCD_SCLK_0;
949
  J15_PIN_SERLCD_SCLK_0;
1156 tk 950
  J15_PIN_SERLCD_SCLK_1;
1199 tk 951
  J15_PIN_SERLCD_SCLK_1;
1156 tk 952
  J15_PIN_SERLCD_DATAOUT(data & 0x04); // D2
953
  J15_PIN_SERLCD_SCLK_0; // setup delay
1199 tk 954
  J15_PIN_SERLCD_SCLK_0;
955
  J15_PIN_SERLCD_SCLK_0;
956
  J15_PIN_SERLCD_SCLK_0;
1156 tk 957
  J15_PIN_SERLCD_SCLK_1;
1199 tk 958
  J15_PIN_SERLCD_SCLK_1;
1156 tk 959
  J15_PIN_SERLCD_DATAOUT(data & 0x02); // D1
960
  J15_PIN_SERLCD_SCLK_0; // setup delay
1199 tk 961
  J15_PIN_SERLCD_SCLK_0;
962
  J15_PIN_SERLCD_SCLK_0;
963
  J15_PIN_SERLCD_SCLK_0;
1156 tk 964
  J15_PIN_SERLCD_SCLK_1;
1199 tk 965
  J15_PIN_SERLCD_SCLK_1;
1156 tk 966
  J15_PIN_SERLCD_DATAOUT(data & 0x01); // D0
967
  J15_PIN_SERLCD_SCLK_0; // setup delay
1199 tk 968
  J15_PIN_SERLCD_SCLK_0;
969
  J15_PIN_SERLCD_SCLK_0;
970
  J15_PIN_SERLCD_SCLK_0;
1156 tk 971
  J15_PIN_SERLCD_SCLK_1;
972
 
973
  return 0; // no error
974
#endif
975
}
976
 
977
 
978
/////////////////////////////////////////////////////////////////////////////
979
//! This function is used by LCD drivers under $MIOS32_PATH/modules/app_lcd
980
//! to set the RS pin
981
//! \param[in] rs state of the RS pin
982
//! \return < 0 if access to RS pin not supported by board
983
/////////////////////////////////////////////////////////////////////////////
984
s32 MIOS32_BOARD_J15_RS_Set(u8 rs)
985
{
986
#if J15_AVAILABLE == 0
987
  return -1; // LCD port not available
988
#else
989
  J15_PIN_SER(rs);
990
 
991
  return 0; // no error
992
#endif
993
}
994
 
995
/////////////////////////////////////////////////////////////////////////////
996
//! This function is used by LCD drivers under $MIOS32_PATH/modules/app_lcd
997
//! to set the RW pin
998
//! \param[in] rw state of the RW pin
999
//! \return < 0 if access to RW pin not supported by board
1000
/////////////////////////////////////////////////////////////////////////////
1001
s32 MIOS32_BOARD_J15_RW_Set(u8 rw)
1002
{
1003
#if J15_AVAILABLE == 0
1004
  return -1; // LCD port not available
1005
#else
1006
  J15_PIN_RW(rw);
1007
 
1008
  return 0; // no error
1009
#endif
1010
}
1011
 
1012
/////////////////////////////////////////////////////////////////////////////
1013
//! This function is used by LCD drivers under $MIOS32_PATH/modules/app_lcd
1014
//! to set the E pin
1015
//! \param[in] lcd display port (0=J15A, 1=J15B)
1016
//! \param[in] e state of the E pin
1017
//! \return < 0 if access to E pin not supported by board
1018
/////////////////////////////////////////////////////////////////////////////
1019
s32 MIOS32_BOARD_J15_E_Set(u8 lcd, u8 e)
1020
{
1021
#if J15_AVAILABLE == 0
1022
  return -1; // LCD port not available
1023
#else
1024
  switch( lcd ) {
1025
    case 0:
1026
      J15_PIN_E1(e);
1027
      return 0; // no error
1028
 
1029
    case 1:
1030
      J15_PIN_E2(e);
1031
      return 0; // no error
1032
  }
1033
 
1034
  return -1; // pin not available
1035
#endif
1036
}
1037
 
1038
 
1039
/////////////////////////////////////////////////////////////////////////////
1040
//! This function is used by LCD drivers under $MIOS32_PATH/modules/app_lcd
1041
//! to poll the busy bit (D7) of a LCD
1042
//! \param[in] lcd display port (0=J15A, 1=J15B)
1043
//! \param[in] time_out how many times should the busy bit be polled?
1044
//! \return -1 if LCD not available
1045
//! \return -2 on timeout
1046
//! return >= 0 on success
1047
/////////////////////////////////////////////////////////////////////////////
1048
s32 MIOS32_BOARD_J15_PollUnbusy(u8 lcd, u32 time_out)
1049
{
1050
#if J15_AVAILABLE == 0
1051
  return -1; // LCD port not available
1052
#else
1053
  u32 poll_ctr;
1054
  u32 delay_ctr;
1055
 
1056
  // select command register (RS=0)
1057
  MIOS32_BOARD_J15_RS_Set(0);
1058
 
1059
  // select read (will also disable output buffer of 74HC595)
1060
  MIOS32_BOARD_J15_RW_Set(1);
1061
 
1062
  // check if E pin is available
1063
  if( MIOS32_BOARD_J15_E_Set(lcd, 1) < 0 )
1064
    return -1; // LCD port not available
1065
 
1066
  // poll busy flag, timeout after 10 mS
1067
  // each loop takes ca. 4 uS @ 72MHz, this has to be considered when defining the time_out value
1068
  for(poll_ctr=time_out; poll_ctr>0; --poll_ctr) {
1069
    MIOS32_BOARD_J15_E_Set(lcd, 1);
1070
 
1071
    // due to slow slope we should wait at least for 1 uS
1072
    for(delay_ctr=0; delay_ctr<10; ++delay_ctr)
1073
      MIOS32_BOARD_J15_RW_Set(1);
1074
 
1075
    u32 busy = J15_PIN_D7_IN;
1076
    MIOS32_BOARD_J15_E_Set(lcd, 0);
1077
    if( !busy )
1078
      break;
1079
  }
1080
 
1081
  // deselect read (output buffers of 74HC595 enabled again)
1082
  MIOS32_BOARD_J15_RW_Set(0);
1083
 
1084
  // timeout?
1085
  if( poll_ctr == 0 )
1086
    return -2; // timeout error
1087
 
1088
  return 0; // no error
1089
#endif
1090
}
1091
 
1092
 
1093
 
1094
/////////////////////////////////////////////////////////////////////////////
1095
//! This function enables or disables one of the two DAC channels provided by 
1096
//! STM32F103RE (and not by STM32F103RB).
1097
//!
1098
//! <UL>
1099
//!  <LI>the first channel (chn == 0) is output at pin RA4 (J16:RC1 of the MBHP_CORE_STM32 module)
1100
//!  <LI>the second channel (chn == 1) is output at pin RA5 (J16:SC of the MBHP_CORE_STM32 module)
1101
//! </UL>
1102
//! 
1103
//! \param[in] chn channel number (0 or 1)
1104
//! \param[in] enable 0: channel disabled, 1: channel enabled.
1105
//! \return < 0 if DAC channel not supported (e.g. STM32F103RB)
1106
/////////////////////////////////////////////////////////////////////////////
1107
s32 MIOS32_BOARD_DAC_PinInit(u8 chn, u8 enable)
1108
{
1109
#if 1
1110
  return -1; // generally not supported. Try DAC access for all other processors
1111
#else
1112
  if( chn >= 2 )
1113
    return -1; // channel not supported
1114
 
1115
  if( enable ) {
1116
    // enable DAC clock
1117
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
1118
 
1119
    // Once the DAC channel is enabled, the corresponding GPIO pin is automatically 
1120
    // connected to the DAC converter. In order to avoid parasitic consumption, 
1121
    // the GPIO pin should be configured in analog
1122
    GPIO_InitTypeDef GPIO_InitStructure;
1123
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
1124
 
1125
    // init DAC
1126
    DAC_InitTypeDef            DAC_InitStructure;
1127
    DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software;
1128
    DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
1129
 
1130
    switch( chn ) {
1131
      case 0:
1132
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4;
1133
    GPIO_Init(GPIOA, &GPIO_InitStructure);
1134
    DAC_Init(DAC_Channel_1, &DAC_InitStructure);
1135
    DAC_Cmd(DAC_Channel_1, ENABLE);
1136
    break;
1137
 
1138
      case 1:
1139
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_5;
1140
    GPIO_Init(GPIOA, &GPIO_InitStructure);
1141
    DAC_Init(DAC_Channel_2, &DAC_InitStructure);
1142
    DAC_Cmd(DAC_Channel_2, ENABLE);
1143
    break;
1144
 
1145
      default:
1146
    return -2; // unexpected (chn already checked above)
1147
    }
1148
 
1149
  } else {
1150
    // disable DAC channel
1151
    switch( chn ) {
1152
      case 0: DAC_Cmd(DAC_Channel_1, DISABLE); break;
1153
      case 1: DAC_Cmd(DAC_Channel_2, DISABLE); break;
1154
      default:
1155
    return -2; // unexpected (chn already checked above)
1156
    }
1157
  }
1158
 
1159
  return 0; // no error
1160
#endif
1161
}
1162
 
1163
 
1164
/////////////////////////////////////////////////////////////////////////////
1165
//! This function sets an output channel to a given 16-bit value.
1166
//!
1167
//! Note that actually the DAC will work at 12-bit resolution. The lowest
1168
//! 4 bits are ignored (reserved for future STM chips).
1169
//! \param[in] chn channel number (0 or 1)
1170
//! \param[in] value the 16-bit value (0..65535). Lowest 4 bits are ignored.
1171
//! \return < 0 if DAC channel not supported (e.g. STM32F103RB)
1172
/////////////////////////////////////////////////////////////////////////////
1173
s32 MIOS32_BOARD_DAC_PinSet(u8 chn, u16 value)
1174
{
1175
#if 1
1176
  return -1; // generally not supported. Try DAC access for all other processors
1177
#else
1178
  switch( chn ) {
1179
    case 0:
1180
      DAC_SetChannel1Data(DAC_Align_12b_L, value);
1181
      DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);
1182
      break;
1183
 
1184
    case 1:
1185
      DAC_SetChannel2Data(DAC_Align_12b_L, value);
1186
      DAC_SoftwareTriggerCmd(DAC_Channel_2, ENABLE);
1187
      break;
1188
 
1189
    default:
1190
      return -1; // channel not supported
1191
  }
1192
 
1193
  return 0; // no error
1194
#endif
1195
}
1196
 
1197
//! \}
1198
 
1199
#endif /* MIOS32_DONT_USE_BOARD */