Skip to content

Commit

Permalink
UART custom pin selection (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruedli authored Sep 7, 2020
1 parent 63c9174 commit 8e9c55f
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 47 deletions.
59 changes: 38 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,43 @@ The framework aims to eventually be Arduino API compatible, though Arduino libra
this is a work in progress with most basic functionality available.

### Hardware Serial Ports
| Port | TX | RX |
| --- | --- | --- |
| Serial | P0_02 | P0_03 |
| Serial1 | P0_15 | P0_16 |
| Serial2 | P0_10 | P0_11 |
| Serial3 | P0_00 | P0_01 |

### AnalogRead
There are 8 ADC channels available
If your board layout does not use the default pins for UART RX/TX override them with the definitions below.

| Serial | TX | RX | FUNCNUM |
| --- | --- | --- | --- |
| Default | P0_02 | P0_03 | 1 |


| Serial1 | TX | RX | FUNCNUM |
| --- | --- | --- | --- |
| Default | P0_15 | P0_16 | 1 |
| `LPC_PINCFG_UART1_P2_00` | P2_00 | P2_01 | 2 |

| Serial2 | TX | RX | FUNCNUM |
| --- | --- | --- | --- |
| Default | P0_10 | P0_11 | 1 |
| `LPC_PINCFG_UART2_P2_08` | P2_08 | P2_09 | 2 |

| Serial3 | TX | RX | FUNCNUM |
| --- | --- | --- | --- |
| Default | P0_00 | P0_01 | 2 |
| `LPC_PINCFG_UART3_P0_25` | P0_25 | P0_26 | 3 |
| `LPC_PINCFG_UART3_P4_28` | P4_28 | P4_29 | 3 |

### AnalogRead
There are 8 ADC channels available

| Pin | Channel Number|
| --- | --- |
| P0_02 | 7 |
| P0_03 | 6 |
| P0_23 | 0 |
| P0_24 | 1 |
| P0_25 | 2 |
| P0_26 | 3 |
| P1_30 | 4 |
| P1_31 | 5 |
| P0_02 | 7 |
| P0_03 | 6 |
| P0_23 | 0 |
| P0_24 | 1 |
| P0_25 | 2 |
| P0_26 | 3 |
| P1_30 | 4 |
| P1_31 | 5 |

### AnalogWrite
Although all pins can be used for PWM (software mode) there are 6 Hardware PWM channels, if a hardware channel is
Expand All @@ -35,8 +52,8 @@ software and hardware share a period (default 20ms).
| Hardware channel | Attached pins |
| --- | --- |
| 1 | P1_18, P2_00 |
| 2 | P1_20, P2_01, P3_25 |
| 3 | P1_21, P2_02, P3_26 |
| 4 | P1_23, P2_03 |
| 5 | P1_24, P2_04 |
| 6 | P1_26, P2_05 |
| 2 | P1_20, P2_01, P3_25 |
| 3 | P1_21, P2_02, P3_26 |
| 4 | P1_23, P2_03 |
| 5 | P1_24, P2_04 |
| 6 | P1_26, P2_05 |
112 changes: 86 additions & 26 deletions cores/arduino/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ extern "C" {
#define SERIAL_RX_BUFFER_SIZE 256
#endif

#if !defined(LPC_UART_IRQ_PRIORITY)
#define LPC_UART_IRQ_PRIORITY 3
#endif

/*
UART pin configuration:
For UART0: pin 98 and 99, PORT 0-02/03, FUNC 1
For UART1: default is pin 62 and 63, PORT 0-15/16, FUNC 1
define LPC_PINCFG_UART1_P2_00 to use pin 75 and 74, PORT 2-00/01, FUNC 2
For UART2: default is pin 48 and 49, PORT 0-10/11, FUNC 1
define LPC_PINCFG_UART2_P2_08 to use pin 65 and 44, PORT 2-08/09, FUNC 2
For UART3: default is pin 46 and 47, PORT 0-00/01, FUNC 2
define LPC_PINCFG_UART3_P0_25 to use pin 7 and 6, PORT 0-25/26, FUNC 3
define LPC_PINCFG_UART3_P4_28 to use pin 82 and 85, PORT 4-28/29, FUNC 3
*/

template <uint32_t RXB_SIZE = SERIAL_RX_BUFFER_SIZE, uint32_t TXB_SIZE = SERIAL_TX_BUFFER_SIZE>
class HardwareSerial : public Stream {
private:
Expand Down Expand Up @@ -73,45 +93,85 @@ class HardwareSerial : public Stream {
if (Baudrate == baudrate) return; // No need to re-initialize

if (UARTx == LPC_UART0) {
// Initialize UART0 pin connect
PinCfg.Funcnum = 1;
// Initialize UART0 pin connect 98 and 99
PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0;
PinCfg.Pinnum = 2;
PinCfg.Funcnum = 1;
PinCfg.Portnum = 0;
PinCfg.Pinnum = 2;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 3;
PINSEL_ConfigPin(&PinCfg);
} else if ((LPC_UART1_TypeDef *) UARTx == LPC_UART1) {
// Initialize UART1 pin connect
PinCfg.Funcnum = 1;
PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0;
PinCfg.Pinnum = 15;
PinCfg.Portnum = 0;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 16;
PINSEL_ConfigPin(&PinCfg);

#if defined(LPC_PINCFG_UART1_P2_00)
PinCfg.Funcnum = 2;
PinCfg.Portnum = 2;
PinCfg.Pinnum = 0;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 1;
PINSEL_ConfigPin(&PinCfg);
#else
PinCfg.Funcnum = 1;
PinCfg.Portnum = 0;
PinCfg.Pinnum = 15;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 16;
PINSEL_ConfigPin(&PinCfg);
#endif

} else if (UARTx == LPC_UART2) {
// Initialize UART2 pin connect
PinCfg.Funcnum = 1;
PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0;
PinCfg.Pinnum = 10;
PinCfg.Portnum = 0;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 11;
PINSEL_ConfigPin(&PinCfg);

#if defined(LPC_PINCFG_UART2_P2_08)
PinCfg.Funcnum = 2;
PinCfg.Portnum = 2;
PinCfg.Pinnum = 8;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 9;
PINSEL_ConfigPin(&PinCfg);
#else
PinCfg.Funcnum = 1;
PinCfg.Pinnum = 10;
PinCfg.Portnum = 0;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 11;
PINSEL_ConfigPin(&PinCfg);
#endif

} else if (UARTx == LPC_UART3) {
// Initialize UART3 pin connect
PinCfg.Funcnum = 2;
PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0;
PinCfg.Pinnum = 0;
PinCfg.Portnum = 0;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 1;
PINSEL_ConfigPin(&PinCfg);

#if defined(LPC_PINCFG_UART3_P4_28)
PinCfg.Funcnum = 3;
PinCfg.Portnum = 4;
PinCfg.Pinnum = 28;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 29;
PINSEL_ConfigPin(&PinCfg);
#elif defined(LPC_PINCFG_UART3_P0_25)
PinCfg.Funcnum = 3;
PinCfg.Portnum = 0;
PinCfg.Pinnum = 25;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 26;
PINSEL_ConfigPin(&PinCfg);
#else
PinCfg.Funcnum = 2;
PinCfg.Portnum = 0;
PinCfg.Pinnum = 0;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 1;
PINSEL_ConfigPin(&PinCfg);
#endif

}

/* Initialize UART Configuration parameter structure to default state:
Expand Down Expand Up @@ -141,19 +201,19 @@ class HardwareSerial : public Stream {

// Set proper priority and enable interrupts
if (UARTx == LPC_UART0) {
NVIC_SetPriority(UART0_IRQn, NVIC_EncodePriority(0, 3, 0));
NVIC_SetPriority(UART0_IRQn, NVIC_EncodePriority(0, LPC_UART_IRQ_PRIORITY, 0));
NVIC_EnableIRQ(UART0_IRQn);
}
else if ((LPC_UART1_TypeDef *) UARTx == LPC_UART1) {
NVIC_SetPriority(UART1_IRQn, NVIC_EncodePriority(0, 3, 0));
NVIC_EnableIRQ(UART1_IRQn);
NVIC_SetPriority(UART1_IRQn, NVIC_EncodePriority(0, LPC_UART_IRQ_PRIORITY, 0));
NVIC_EnableIRQ(UART1_IRQn);
}
else if (UARTx == LPC_UART2) {
NVIC_SetPriority(UART2_IRQn, NVIC_EncodePriority(0, 3, 0));
NVIC_SetPriority(UART2_IRQn, NVIC_EncodePriority(0, LPC_UART_IRQ_PRIORITY, 0));
NVIC_EnableIRQ(UART2_IRQn);
}
else if (UARTx == LPC_UART3) {
NVIC_SetPriority(UART3_IRQn, NVIC_EncodePriority(0, 3, 0));
NVIC_SetPriority(UART3_IRQn, NVIC_EncodePriority(0, LPC_UART_IRQ_PRIORITY, 0));
NVIC_EnableIRQ(UART3_IRQn);
}

Expand Down

0 comments on commit 8e9c55f

Please sign in to comment.