microcontrollerlpc

LPC18S37 pin configuration


I have LPC18S37(TFBGA100) on a custom board. I need some basic pin configuration help. In order to prevent back and forth between several files, I've extracted a piece of code from pin configurations of a working application (see below).

LPC18xx.h

/* BSP */
#include "LPC18xx.h"

#define LPC_SCU_BASE              0x40086000
#define LPC_GPIO_PORT             ((LPC_GPIO_PORT_Type      *) LPC_GPIO_PORT_BASE)

/** Port offset definition */
#define PORT_OFFSET     0x80
/** Pin offset definition */
#define PIN_OFFSET      0x04

/* Pin modes */
#define MD_PUP  (0x0<<3)
#define MD_BUK  (0x1<<3)
#define MD_PLN  (0x2<<3)
#define MD_PDN  (0x3<<3)
#define MD_EHS  (0x1<<5)
#define MD_EZI  (0x1<<6)
#define MD_ZI   (0x1<<7)
#define MD_EHD0 (0x1<<8)
#define MD_EHD1 (0x1<<8)
#define MD_PLN_FAST (MD_PLN | MD_EZI | MD_ZI | MD_EHS)
// 0xF0

/* Pin function */
#define FUNC0           0x0             /** Function 0  */
#define FUNC1           0x1             /** Function 1  */
#define FUNC2           0x2             /** Function 2  */
#define FUNC3           0x3             /** Function 3  */
#define FUNC4           0x4
#define FUNC5           0x5
#define FUNC6           0x6
#define FUNC7           0x7

#define LPC_SCU_PIN(po, pi)   (*(volatile int         *) (LPC_SCU_BASE + ((po) * 0x80) + ((pi) * 0x4))    )
#define LPC_SCU_CLK(c)        (*(volatile int         *) (LPC_SCU_BASE + 0xC00 + ((c) * 0x4))    )

/*********************************************************************//**
 * @brief       Configure pin function
 * @param[in]   port    Port number, should be: 0..15
 * @param[in]   pin     Pin number, should be: 0..31
 * @param[in]   mode    Pin mode, should be:
 *                  - MD_PUP    :Pull-up enabled
 *                  - MD_BUK    :Plain input
 *                  - MD_PLN    :Repeater mode
 *                  - MD_PDN    :Pull-down enabled
 * @param[in]   func    Function mode, should be:
 *                  - FUNC0     :Function 0
 *                  - FUNC1     :Function 1
 *                  - FUNC2     :Function 2
 *                  - FUNC3     :Function 3
 * @return      None
 **********************************************************************/
void scu_pinmux(uint8_t port, uint8_t pin, uint8_t mode, uint8_t func)
{
  uint32_t * scu_base=(uint32_t*)(LPC_SCU_BASE);
  scu_base[(PORT_OFFSET*port+PIN_OFFSET*pin)/4]=mode+func;
} /* scu_pinmux */
void GPIO_SetDir(uint8_t portNum, uint32_t bitValue, uint8_t dir)
{
  if (dir)
  {
    LPC_GPIO_PORT->DIR[portNum] |= bitValue;
  } else
    {
      LPC_GPIO_PORT->DIR[portNum] &= ~bitValue;
    }
}


#define D3_SCU_CONFIG   0xD, 14, MD_PLN, FUNC4

#define D3_SCU_PIN 14
#define D3_SCU_PORT 0xD
#define D3_GPIO_PORT    6
#define D3_GPIO_PIN     28
#define D3_GPIO_MASK    (1 << D3_GPIO_PIN)

/* 
  This is where we set pins in the application.
*/
scu_pinmux(D3_SCU_CONFIG);
GPIO_SetDir(D3_GPIO_PORT, D3_GPIO_MASK, 1);

In the datasheet for pin description says;

On the LPC185x/3x/2x/1x, digital pins are grouped into 16 ports, named P0 to P9 and PA to PF, with up to 20 pins used per port. Each digital pin can support up to eight different digital functions, including General-Purpose I/O (GPIO), selectable through the SCU registers. The pin name is not indicative of the GPIO port assigned to it.

This description is not what I am used to comparing to my previous device (LPC1769), there was no such register called SCU.

Particularly I am confused between SCU_PIN(or PORT) and GPIO_PIN(or PORT). While I can understand GPIO_PIN, SCU_PIN highly confuses me.

enter image description here

I need to setup GPIO3[5] (bga pin-out is F8) as an output for a LED. How should I setup those definitions accordingly then?

#define D3_SCU_CONFIG   0xD, 14, MD_PLN, FUNC4

#define D3_SCU_PIN 14
#define D3_SCU_PORT 0xD
#define D3_GPIO_PORT    6
#define D3_GPIO_PIN     28
#define D3_GPIO_MASK    (1 << D3_GPIO_PIN)

So, I wonder where number 28 comes from as GPIO_PIN as shown above?! Any pointer regarding pin definition on this device highly appreciated.


Solution

  • Here is my own answer for whom might need the same clarification;

    As my question based on the confusion of the roles of the SCU and GPIO, I placed the definitions with correct figures in order to make a LED ON and OFF including initialization of the pin. Please use the code in my original post above for the device requirements and complete the LED pin part by the code below;

    /*******************************************************************************
     * LED on the pin; P6[9]_/GPIO3[5]/NC/NC/EXTBUS_nDYCS0
     ******************************************************************************/
    #define D3_SCU_CONFIG   0x6, 6, MD_PLN, FUNC4
    /* P6[9] */
    #define D3_SCU_PORT     0x6
    #define D3_SCU_PIN      9
    /* GPIO3[5] */
    #define D3_GPIO_PORT    3
    #define D3_GPIO_PIN     5
    #define D3_GPIO_MASK    (1 << D3_GPIO_PIN)
    
    /* 
      This is how we set pins in the application.
    */
    /* Init */
    scu_pinmux(D3_SCU_CONFIG);
    GPIO_SetDir(D3_GPIO_PORT, D3_GPIO_MASK, 1);
    /* Use in the application */
    /* Turn the LED OFF */
    GPIO_ClearValue(D3_GPIO_PORT, D3_GPIO_MASK);
    /* Turn the LED ON */
    GPIO_SetValue(D3_GPIO_PORT, D3_GPIO_MASK);