Skip to content

Commit e756237

Browse files
committed
[LPC11U68] Fixed PLL lock issue etc
Fixed PLL lock issue Corrected system clock related code
1 parent 94f9d1f commit e756237

File tree

7 files changed

+193
-59
lines changed

7 files changed

+193
-59
lines changed

libraries/mbed/targets/cmsis/TARGET_NXP/TARGET_LPC11U6X/system_LPC11U6x.c

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
// <2=> P = 4
9292
// <3=> P = 8
9393
// </h>
94-
#define SYSPLLCTRL_Val 0x00000003 // Reset value: 0x000
94+
#define SYSPLLCTRL_Val 0x00000023 // Reset value: 0x000
9595
//
9696
// <o.0..1> Main Clock Source Select (MAINCLKSEL)
9797
// <0=> IRC Oscillator
@@ -445,6 +445,25 @@ void SystemCoreClockUpdate (void) /* Get Core Clock Frequency */
445445

446446
}
447447

448+
#define PDRUN_VALID_BITS 0x000025FFL
449+
#define PDRUN_RESERVED_ONE 0x0000C800L
450+
451+
static void power_down_config(uint32_t val)
452+
{
453+
volatile uint32_t tmp;
454+
tmp = (LPC_SYSCON->PDRUNCFG & PDRUN_VALID_BITS);
455+
tmp |= (val & PDRUN_VALID_BITS);
456+
LPC_SYSCON->PDRUNCFG = (tmp | PDRUN_RESERVED_ONE);
457+
}
458+
459+
static void power_up_config(uint32_t val)
460+
{
461+
volatile uint32_t tmp;
462+
tmp = (LPC_SYSCON->PDRUNCFG & PDRUN_VALID_BITS);
463+
tmp &= ~(val & PDRUN_VALID_BITS);
464+
LPC_SYSCON->PDRUNCFG = (tmp | PDRUN_RESERVED_ONE);
465+
}
466+
448467
/**
449468
* Initialize the system
450469
*
@@ -455,27 +474,30 @@ void SystemCoreClockUpdate (void) /* Get Core Clock Frequency */
455474
*/
456475
void SystemInit (void) {
457476
#if (CLOCK_SETUP)
458-
volatile uint32_t i;
477+
volatile uint32_t i, tmp;
459478
#endif
460479
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<16);
461480
LPC_SYSCON->SYSPLLCTRL = SYSPLLCTRL_Val;
462481

463-
#warning "should not return here, need to fix an issue with PLL lock"
464-
return;
465482
#if (CLOCK_SETUP) /* Clock Setup */
466483

467484
#if ((SYSPLLCLKSEL_Val & 0x03) == 1)
485+
// Initialize XTALIN/XTALOUT pins
486+
LPC_IOCON->PIO2_0 = 0x01;
487+
LPC_IOCON->PIO2_1 = 0x01;
488+
468489
LPC_SYSCON->SYSOSCCTRL = SYSOSCCTRL_Val;
469-
LPC_SYSCON->PDRUNCFG &= ~(1 << 5); /* Power-up sysosc */
470-
for (i = 0; i < 200; i++) __NOP(); /* Wait for osc to stabilize */
490+
power_up_config(1<<5); /* Power-up sysosc */
491+
for (i = 0; i < 2500; i++) __NOP(); /* Wait for osc to stabilize */
471492
#endif
493+
472494
#if ((SYSPLLCLKSEL_Val & 0x03) == 3)
473495
LPC_SYSCON->RTCOSCCTRL = (1 << 0); /* Enable 32 kHz output */
474496
for (i = 0; i < 200; i++) __NOP(); /* Wait for osc to stabilize */
475497
#endif
476498

477499
LPC_SYSCON->SYSPLLCLKSEL = SYSPLLCLKSEL_Val; /* Select PLL Input */
478-
//LPC_SYSCON->SYSPLLCLKUEN = 0x01; /* Update Clock Source */
500+
LPC_SYSCON->SYSPLLCLKUEN = 0x01; /* Update Clock Source */
479501
LPC_SYSCON->SYSPLLCLKUEN = 0x00; /* Toggle Update Register */
480502
LPC_SYSCON->SYSPLLCLKUEN = 0x01;
481503
while (!(LPC_SYSCON->SYSPLLCLKUEN & 0x01)); /* Wait Until Updated */
@@ -485,13 +507,13 @@ void SystemInit (void) {
485507
#if (((MAINCLKSEL_Val & 0x03) == 2) )
486508
LPC_SYSCON->WDTOSCCTRL = WDTOSCCTRL_Val;
487509
LPC_SYSCON->PDRUNCFG &= ~(1 << 6); /* Power-up WDT Clock */
488-
for (i = 0; i < 2000; i++) __NOP(); /* Wait for osc to stabilize */
510+
for (i = 0; i < 2000; i++) __NOP(); /* Wait for osc to stabilize */
489511
#endif
490512

491513
#if ((MAINCLKSEL_Val & 0x03) == 3) /* Main Clock is PLL Out */
492-
LPC_SYSCON->PDRUNCFG |= (1 << 7); /* Power-down SYSPLL */
514+
power_down_config(1<<7); /* Power-down SYSPLL */
493515
LPC_SYSCON->SYSPLLCTRL = SYSPLLCTRL_Val;
494-
LPC_SYSCON->PDRUNCFG &= ~(1 << 7); /* Power-up SYSPLL */
516+
power_up_config(1<<7); /* Power-up SYSPLL */
495517
while (!(LPC_SYSCON->SYSPLLSTAT & 0x01)); /* Wait Until PLL Locked */
496518
#endif
497519

@@ -549,7 +571,4 @@ void SystemInit (void) {
549571

550572
#endif /* Clock Setup */
551573

552-
/* System clock to the IOCON needs to be enabled or
553-
most of the I/O related peripherals won't work. */
554-
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<16);
555574
}

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11U6X/PeripheralNames.h

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,18 @@ typedef enum {
3131
} UARTName;
3232

3333
typedef enum {
34-
ADC0_0 = 0,
35-
ADC0_1,
36-
ADC0_2,
37-
ADC0_3,
38-
ADC0_4,
39-
ADC0_5,
40-
ADC0_6,
41-
ADC0_7,
42-
ADC0_8,
43-
ADC0_9,
44-
ADC0_10,
45-
ADC0_11,
46-
ADC1_0,
47-
ADC1_1,
48-
ADC1_2,
49-
ADC1_3,
50-
ADC1_4,
51-
ADC1_5,
52-
ADC1_6,
53-
ADC1_7,
54-
ADC1_8,
55-
ADC1_9,
56-
ADC1_10,
57-
ADC1_11,
34+
ADC_0 = 0,
35+
ADC_1,
36+
ADC_2,
37+
ADC_3,
38+
ADC_4,
39+
ADC_5,
40+
ADC_6,
41+
ADC_7,
42+
ADC_8,
43+
ADC_9,
44+
ADC_10,
45+
ADC_11,
5846
} ADCName;
5947

6048
typedef enum {
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2013 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "analogin_api.h"
18+
#include "cmsis.h"
19+
#include "pinmap.h"
20+
#include "error.h"
21+
22+
#ifdef DEVICE_ANALOGIN
23+
24+
#define ANALOGIN_MEDIAN_FILTER 1
25+
26+
#define ADC_10BIT_RANGE 0x3FF
27+
#define ADC_12BIT_RANGE 0xFFF
28+
#define PDRUN_VALID_BITS 0x000025FFL
29+
#define PDRUN_RESERVED_ONE 0x0000C800L
30+
31+
#define ADC_RANGE ADC_12BIT_RANGE
32+
33+
static const PinMap PinMap_ADC[] = {
34+
{P1_9 , ADC_0, 3},
35+
{P0_23, ADC_1, 1},
36+
{P0_16, ADC_2, 1},
37+
{P0_15, ADC_3, 3},
38+
{P1_22, ADC_4, 3},
39+
{P1_3 , ADC_5, 4},
40+
{P0_14, ADC_6, 2},
41+
{P0_13, ADC_7, 2},
42+
{P0_12, ADC_8, 2},
43+
{P0_11, ADC_9, 2},
44+
{P1_29, ADC_10,4},
45+
{P0_22, ADC_11,1},
46+
{NC , NC ,0}
47+
};
48+
49+
50+
void analogin_init(analogin_t *obj, PinName pin) {
51+
volatile uint32_t tmp;
52+
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
53+
if (obj->adc == (uint32_t)NC) {
54+
error("ADC pin mapping failed");
55+
}
56+
pinmap_pinout(pin, PinMap_ADC);
57+
58+
// ADC Powered
59+
tmp = (LPC_SYSCON->PDRUNCFG & PDRUN_VALID_BITS);
60+
tmp &= ~((1 << 4) & PDRUN_VALID_BITS);
61+
LPC_SYSCON->PDRUNCFG = (tmp | PDRUN_RESERVED_ONE);
62+
63+
// Enable clock for ADC
64+
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 13);
65+
66+
// Start ADC self-calibration
67+
LPC_ADC->CTRL = (1UL << 30) || (100);
68+
do {
69+
tmp = LPC_ADC->CTRL;
70+
} while ((tmp & (1UL << 30)) != 0);
71+
72+
LPC_ADC->CTRL = 100;
73+
}
74+
75+
static inline uint32_t adc_read(analogin_t *obj) {
76+
77+
// select channel
78+
LPC_ADC->SEQA_CTRL &= ~(0xFFF);
79+
LPC_ADC->SEQA_CTRL |= (1UL << obj->adc);
80+
81+
// start conversion, sequence enable with async mode
82+
LPC_ADC->SEQA_CTRL |= ((1UL << 26) | (1UL << 31) | (1UL << 29) /*| (1UL << 19)*/);
83+
84+
// Repeatedly get the sample data until DONE bit
85+
volatile uint32_t data;
86+
do {
87+
data = LPC_ADC->SEQA_GDAT;
88+
} while ((data & (1UL << 31)) == 0);
89+
data = LPC_ADC->DAT[obj->adc];
90+
91+
// Stop conversion
92+
LPC_ADC->SEQA_CTRL &= ~(1UL << 31);
93+
94+
return ((data >> 4) & ADC_RANGE);
95+
}
96+
97+
static inline void order(uint32_t *a, uint32_t *b) {
98+
if (*a > *b) {
99+
uint32_t t = *a;
100+
*a = *b;
101+
*b = t;
102+
}
103+
}
104+
105+
static inline uint32_t adc_read_u32(analogin_t *obj) {
106+
uint32_t value;
107+
#if ANALOGIN_MEDIAN_FILTER
108+
uint32_t v1 = adc_read(obj);
109+
uint32_t v2 = adc_read(obj);
110+
uint32_t v3 = adc_read(obj);
111+
order(&v1, &v2);
112+
order(&v2, &v3);
113+
order(&v1, &v2);
114+
value = v2;
115+
#else
116+
value = adc_read(obj);
117+
#endif
118+
return value;
119+
}
120+
121+
uint16_t analogin_read_u16(analogin_t *obj) {
122+
uint32_t value = adc_read_u32(obj);
123+
return (value << 4) | ((value >> 8) & 0x000F); // 12 bit
124+
}
125+
126+
float analogin_read(analogin_t *obj) {
127+
uint32_t value = adc_read_u32(obj);
128+
return (float)value * (1.0f / (float)ADC_RANGE);
129+
}
130+
131+
#endif

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11U6X/device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222

2323
#define DEVICE_INTERRUPTIN 1
2424

25-
#define DEVICE_ANALOGIN 0
25+
#define DEVICE_ANALOGIN 1
2626
#define DEVICE_ANALOGOUT 0
2727

2828
#define DEVICE_SERIAL 1
2929
#define DEVICE_SERIAL_FC 1
3030

3131
#define DEVICE_I2C 1
32-
#define DEVICE_I2CSLAVE 0
32+
#define DEVICE_I2CSLAVE 1
3333

3434
#define DEVICE_SPI 1
3535
#define DEVICE_SPISLAVE 0

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11U6X/i2c_api.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ static const PinMap PinMap_I2C_SCL[] = {
4444
#define I2C_SCLL(x, val) (x->i2c->SCLL = val)
4545
#define I2C_SCLH(x, val) (x->i2c->SCLH = val)
4646

47-
#warning [TODO] just copied from LPC11UXX code, need to check
4847
static const uint32_t I2C_addr_offset[2][4] = {
49-
{0x0C, 0x20, 0x24, 0x28},
50-
{0x30, 0x34, 0x38, 0x3C}
48+
{0x0C, 0x20, 0x24, 0x28}, // slave address offset
49+
{0x30, 0x34, 0x38, 0x3C} // slave address mask offset
5150
};
5251

5352
static inline void i2c_conclr(i2c_t *obj, int start, int stop, int interrupt, int acknowledge) {
@@ -183,8 +182,7 @@ static inline int i2c_do_read(i2c_t *obj, int last) {
183182

184183
void i2c_frequency(i2c_t *obj, int hz) {
185184
// No peripheral clock divider on the M0
186-
#warning "[TODO] This should be fixed to handle system core clock correctly."
187-
uint32_t PCLK = 12000000; //SystemCoreClock;
185+
uint32_t PCLK = SystemCoreClock;
188186

189187
uint32_t pulse = PCLK / (hz * 2);
190188

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11U6X/serial_api.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,29 @@
2929
* INITIALIZATION
3030
******************************************************************************/
3131

32-
#define UART_NUM 5
32+
#define UART_NUM 1
3333

3434
static const PinMap PinMap_UART_TX[] = {
3535
{P0_19, UART_0, 1},
3636
{P1_18, UART_0, 2},
3737
{P1_27, UART_0, 2},
38-
{P1_18, UART_1, 2},
39-
{P1_0 , UART_2, 3},
40-
{P1_23, UART_2, 3},
41-
{P2_4 , UART_3, 1},
42-
{P2_12, UART_4, 1},
38+
// {P1_18, UART_1, 2},
39+
// {P1_0 , UART_2, 3},
40+
// {P1_23, UART_2, 3},
41+
// {P2_4 , UART_3, 1},
42+
// {P2_12, UART_4, 1},
4343
{ NC , NC , 0}
4444
};
4545

4646
static const PinMap PinMap_UART_RX[] = {
4747
{P0_18, UART_0, 1},
4848
{P1_17, UART_0, 2},
4949
{P1_26, UART_0, 2},
50-
{P1_2 , UART_1, 3},
51-
{P0_20, UART_2, 2},
52-
{P1_6 , UART_2, 2},
53-
{P2_3 , UART_3, 1},
54-
{P2_11, UART_4, 1},
50+
// {P1_2 , UART_1, 3},
51+
// {P0_20, UART_2, 2},
52+
// {P1_6 , UART_2, 2},
53+
// {P2_3 , UART_3, 1},
54+
// {P2_11, UART_4, 1},
5555
{NC , NC , 0}
5656
};
5757

@@ -129,8 +129,7 @@ void serial_free(serial_t *obj) {
129129
// set the baud rate, taking in to account the current SystemFrequency
130130
void serial_baud(serial_t *obj, int baudrate) {
131131
LPC_SYSCON->USART0CLKDIV = 0x1;
132-
#warning "[TODO] This should be fixed to handle system core clock correctly."
133-
uint32_t PCLK = 12000000; //SystemCoreClock;
132+
uint32_t PCLK = SystemCoreClock;
134133
// First we check to see if the basic divide with no DivAddVal/MulVal
135134
// ratio gives us an integer result. If it does, we set DivAddVal = 0,
136135
// MulVal = 1. Otherwise, we search the valid ratio value range to find

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11U6X/us_ticker.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ void us_ticker_init(void) {
2727
us_ticker_inited = 1;
2828

2929
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<10); // Clock CT32B1
30-
#warning "[TODO] this should read from SystemCoreClock grobal variable."
31-
uint32_t PCLK = 12000000;//SystemCoreClock;
30+
uint32_t PCLK = SystemCoreClock;
3231

3332
US_TICKER_TIMER->TCR = 0x2; // reset
3433

0 commit comments

Comments
 (0)