Subversion Repositories svn.mios32

Rev

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