Skip to content

Commit 8215203

Browse files
committed
Merge pull request #264 from Sissors/master
K20 general and timer adjustments
2 parents 6b044ad + 62dcd84 commit 8215203

File tree

5 files changed

+169
-104
lines changed

5 files changed

+169
-104
lines changed

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_K20D5M/clk_freqs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extern "C" {
2525
* \return Bus frequency
2626
*/
2727
static inline uint32_t bus_frequency(void) {
28-
return SystemCoreClock / (((SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV4_MASK) >> SIM_CLKDIV1_OUTDIV4_SHIFT) + 1);
28+
return SystemCoreClock / (((SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV2_MASK) >> SIM_CLKDIV1_OUTDIV2_SHIFT) + 1);
2929
}
3030

3131
/*!

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_K20D5M/device.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#define DEVICE_LOCALFILESYSTEM 0
4646
#define DEVICE_ID_LENGTH 24
4747

48-
#define DEVICE_SLEEP 0
48+
#define DEVICE_SLEEP 1
4949

5050
#define DEVICE_DEBUG_AWARENESS 0
5151

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_K20D5M/gpio_irq_api.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,43 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
165165
// Interrupt configuration and clear interrupt
166166
port->PCR[obj->pin] = (port->PCR[obj->pin] & ~PORT_PCR_IRQC_MASK) | irq_settings | PORT_PCR_ISF_MASK;
167167
}
168+
169+
void gpio_irq_enable(gpio_irq_t *obj) {
170+
switch (obj->port) {
171+
case PortA:
172+
NVIC_EnableIRQ(PORTA_IRQn);
173+
break;
174+
case PortB:
175+
NVIC_EnableIRQ(PORTB_IRQn);
176+
break;
177+
case PortC:
178+
NVIC_EnableIRQ(PORTC_IRQn);
179+
break;
180+
case PortD:
181+
NVIC_EnableIRQ(PORTD_IRQn);
182+
break;
183+
case PortE:
184+
NVIC_EnableIRQ(PORTE_IRQn);
185+
break;
186+
}
187+
}
188+
189+
void gpio_irq_disable(gpio_irq_t *obj) {
190+
switch (obj->port) {
191+
case PortA:
192+
NVIC_DisableIRQ(PORTA_IRQn);
193+
break;
194+
case PortB:
195+
NVIC_DisableIRQ(PORTB_IRQn);
196+
break;
197+
case PortC:
198+
NVIC_DisableIRQ(PORTC_IRQn);
199+
break;
200+
case PortD:
201+
NVIC_DisableIRQ(PORTD_IRQn);
202+
break;
203+
case PortE:
204+
NVIC_DisableIRQ(PORTE_IRQn);
205+
break;
206+
}
207+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
#include "sleep_api.h"
17+
#include "cmsis.h"
18+
19+
//Normal wait mode
20+
void sleep(void)
21+
{
22+
SMC->PMPROT = SMC_PMPROT_AVLLS_MASK | SMC_PMPROT_ALLS_MASK | SMC_PMPROT_AVLP_MASK;
23+
24+
//Normal sleep mode for ARM core:
25+
SCB->SCR = 0;
26+
__WFI();
27+
}
28+
29+
//Very low-power stop mode
30+
void deepsleep(void)
31+
{
32+
//Check if PLL/FLL is enabled:
33+
uint32_t PLL_FLL_en = (MCG->C1 & MCG_C1_CLKS_MASK) == MCG_C1_CLKS(0);
34+
35+
SMC->PMPROT = SMC_PMPROT_AVLLS_MASK | SMC_PMPROT_ALLS_MASK | SMC_PMPROT_AVLP_MASK;
36+
SMC->PMCTRL = SMC_PMCTRL_STOPM(2);
37+
38+
//Deep sleep for ARM core:
39+
SCB->SCR = 1<<SCB_SCR_SLEEPDEEP_Pos;
40+
41+
__WFI();
42+
43+
//Switch back to PLL as clock source if needed
44+
//The interrupt that woke up the device will run at reduced speed
45+
if (PLL_FLL_en) {
46+
if (MCG->C6 & (1<<MCG_C6_PLLS_SHIFT) != 0) /* If PLL */
47+
while((MCG->S & MCG_S_LOCK0_MASK) == 0x00U); /* Wait until locked */
48+
MCG->C1 &= ~MCG_C1_CLKS_MASK;
49+
}
50+
51+
}

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_K20D5M/us_ticker.c

Lines changed: 76 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -18,150 +18,120 @@
1818
#include "PeripheralNames.h"
1919
#include "clk_freqs.h"
2020

21-
static void pit_init(void);
22-
static void lptmr_init(void);
21+
#define PIT_TIMER PIT->CHANNEL[0]
22+
#define PIT_TIMER_IRQ PIT0_IRQn
23+
#define PIT_TICKER PIT->CHANNEL[1]
24+
#define PIT_TICKER_IRQ PIT1_IRQn
25+
26+
static void timer_init(void);
27+
static void ticker_init(void);
2328

2429

2530
static int us_ticker_inited = 0;
26-
static uint32_t pit_ldval = 0;
31+
static uint32_t clk_mhz;
2732

2833
void us_ticker_init(void) {
2934
if (us_ticker_inited)
3035
return;
3136
us_ticker_inited = 1;
32-
33-
pit_init();
34-
lptmr_init();
35-
}
36-
37-
static uint32_t pit_us_ticker_counter = 0;
38-
39-
void pit0_isr(void) {
40-
pit_us_ticker_counter++;
41-
PIT->CHANNEL[0].LDVAL = pit_ldval; // 1us
42-
PIT->CHANNEL[0].TFLG = 1;
37+
38+
SIM->SCGC6 |= SIM_SCGC6_PIT_MASK; // Clock PIT
39+
PIT->MCR = 0; // Enable PIT
40+
41+
clk_mhz = bus_frequency() / 1000000;
42+
43+
timer_init();
44+
ticker_init();
4345
}
4446

4547
/******************************************************************************
4648
* Timer for us timing.
49+
*
50+
* The K20D5M does not have a prescaler on its PIT timer nor the option
51+
* to chain timers, which is why a software timer is required to get 32-bit
52+
* word length.
4753
******************************************************************************/
48-
static void pit_init(void) {
49-
SIM->SCGC6 |= SIM_SCGC6_PIT_MASK; // Clock PIT
50-
PIT->MCR = 0; // Enable PIT
54+
static volatile uint32_t msb_counter = 0;
55+
static uint32_t timer_ldval = 0;
56+
57+
static void timer_isr(void) {
58+
msb_counter++;
59+
PIT_TIMER.TFLG = 1;
60+
}
5161

52-
pit_ldval = bus_frequency() / 1000000;
62+
static void timer_init(void) {
63+
//CLZ counts the leading zeros, returning number of bits not used by clk_mhz
64+
timer_ldval = clk_mhz << __CLZ(clk_mhz);
5365

54-
PIT->CHANNEL[0].LDVAL = pit_ldval; // 1us
55-
PIT->CHANNEL[0].TCTRL |= PIT_TCTRL_TIE_MASK;
56-
PIT->CHANNEL[0].TCTRL |= PIT_TCTRL_TEN_MASK; // Start timer 1
66+
PIT_TIMER.LDVAL = timer_ldval; // 1us
67+
PIT_TIMER.TCTRL |= PIT_TCTRL_TIE_MASK;
68+
PIT_TIMER.TCTRL |= PIT_TCTRL_TEN_MASK; // Start timer 0
5769

58-
NVIC_SetVector(PIT0_IRQn, (uint32_t)pit0_isr);
59-
NVIC_EnableIRQ(PIT0_IRQn);
70+
NVIC_SetVector(PIT_TIMER_IRQ, (uint32_t)timer_isr);
71+
NVIC_EnableIRQ(PIT_TIMER_IRQ);
6072
}
6173

6274
uint32_t us_ticker_read() {
6375
if (!us_ticker_inited)
6476
us_ticker_init();
77+
78+
uint32_t retval;
79+
__disable_irq();
80+
retval = (timer_ldval - PIT_TIMER.CVAL) / clk_mhz; //Hardware bits
81+
retval |= msb_counter << __CLZ(clk_mhz); //Software bits
82+
83+
if (PIT_TIMER.TFLG == 1) { //If overflow bit is set, force it to be handled
84+
timer_isr(); //Handle IRQ, read again to make sure software/hardware bits are synced
85+
NVIC_ClearPendingIRQ(PIT_TIMER_IRQ);
86+
return us_ticker_read();
87+
}
6588

66-
return pit_us_ticker_counter;
89+
__enable_irq();
90+
return retval;
6791
}
6892

6993
/******************************************************************************
7094
* Timer Event
7195
*
7296
* It schedules interrupts at given (32bit)us interval of time.
73-
* It is implemented used the 16bit Low Power Timer that remains powered in all
74-
* power modes.
97+
* It is implemented using PIT channel 1, since no prescaler is available,
98+
* some bits are implemented in software.
7599
******************************************************************************/
76-
static void lptmr_isr(void);
77-
78-
static void lptmr_init(void) {
79-
/* Clock the timer */
80-
SIM->SCGC5 |= SIM_SCGC5_LPTIMER_MASK;
81-
82-
/* Reset */
83-
LPTMR0->CSR = 0;
100+
static void ticker_isr(void);
84101

102+
static void ticker_init(void) {
85103
/* Set interrupt handler */
86-
NVIC_SetVector(LPTimer_IRQn, (uint32_t)lptmr_isr);
87-
NVIC_EnableIRQ(LPTimer_IRQn);
88-
89-
/* Clock at (1)MHz -> (1)tick/us */
90-
/* Check if the external oscillator can be divided to 1MHz */
91-
uint32_t extosc = extosc_frequency();
92-
93-
if (extosc != 0) { //If external oscillator found
94-
OSC0->CR |= OSC_CR_ERCLKEN_MASK;
95-
if (extosc % 1000000u == 0) { //If it is a multiple if 1MHz
96-
extosc /= 1000000;
97-
if (extosc == 1) { //1MHz, set timerprescaler in bypass mode
98-
LPTMR0->PSR = LPTMR_PSR_PCS(3) | LPTMR_PSR_PBYP_MASK;
99-
return;
100-
} else { //See if we can divide it to 1MHz
101-
uint32_t divider = 0;
102-
extosc >>= 1;
103-
while (1) {
104-
if (extosc == 1) {
105-
LPTMR0->PSR = LPTMR_PSR_PCS(3) | LPTMR_PSR_PRESCALE(divider);
106-
return;
107-
}
108-
if (extosc % 2 != 0) //If we can't divide by two anymore
109-
break;
110-
divider++;
111-
extosc >>= 1;
112-
}
113-
}
114-
}
115-
}
116-
//No suitable external oscillator clock -> Use fast internal oscillator (4MHz)
117-
MCG->C1 |= MCG_C1_IRCLKEN_MASK;
118-
MCG->C2 |= MCG_C2_IRCS_MASK;
119-
LPTMR0->PSR = LPTMR_PSR_PCS(0) | LPTMR_PSR_PRESCALE(1);
104+
NVIC_SetVector(PIT_TICKER_IRQ, (uint32_t)ticker_isr);
105+
NVIC_EnableIRQ(PIT_TICKER_IRQ);
120106
}
121107

122108
void us_ticker_disable_interrupt(void) {
123-
LPTMR0->CSR &= ~LPTMR_CSR_TIE_MASK;
109+
PIT_TICKER.TCTRL &= ~PIT_TCTRL_TIE_MASK;
124110
}
125111

126112
void us_ticker_clear_interrupt(void) {
127113
// we already clear interrupt in lptmr_isr
128114
}
129115

130116
static uint32_t us_ticker_int_counter = 0;
131-
static uint16_t us_ticker_int_remainder = 0;
132-
133-
static void lptmr_set(unsigned short count) {
134-
/* Reset */
135-
LPTMR0->CSR = 0;
136-
137-
/* Set the compare register */
138-
LPTMR0->CMR = count;
139117

140-
/* Enable interrupt */
141-
LPTMR0->CSR |= LPTMR_CSR_TIE_MASK;
142-
143-
/* Start the timer */
144-
LPTMR0->CSR |= LPTMR_CSR_TEN_MASK;
118+
inline static void ticker_set(uint32_t count) {
119+
PIT_TICKER.TCTRL = 0;
120+
PIT_TICKER.LDVAL = count;
121+
PIT_TICKER.TCTRL = PIT_TCTRL_TIE_MASK | PIT_TCTRL_TEN_MASK;
145122
}
146123

147-
static void lptmr_isr(void) {
148-
// write 1 to TCF to clear the LPT timer compare flag
149-
LPTMR0->CSR |= LPTMR_CSR_TCF_MASK;
124+
static void ticker_isr(void) {
125+
// Clear IRQ flag
126+
PIT_TICKER.TFLG = 1;
150127

151128
if (us_ticker_int_counter > 0) {
152-
lptmr_set(0xFFFF);
129+
ticker_set(0xFFFFFFFF);
153130
us_ticker_int_counter--;
154-
155131
} else {
156-
if (us_ticker_int_remainder > 0) {
157-
lptmr_set(us_ticker_int_remainder);
158-
us_ticker_int_remainder = 0;
159-
160-
} else {
161-
// This function is going to disable the interrupts if there are
162-
// no other events in the queue
163-
us_ticker_irq_handler();
164-
}
132+
// This function is going to disable the interrupts if there are
133+
// no other events in the queue
134+
us_ticker_irq_handler();
165135
}
166136
}
167137

@@ -173,13 +143,17 @@ void us_ticker_set_interrupt(unsigned int timestamp) {
173143
return;
174144
}
175145

176-
us_ticker_int_counter = (uint32_t)(delta >> 16);
177-
us_ticker_int_remainder = (uint16_t)(0xFFFF & delta);
178-
if (us_ticker_int_counter > 0) {
179-
lptmr_set(0xFFFF);
146+
//Calculate how much falls outside the 32-bit after multiplying with clk_mhz
147+
//We shift twice 16-bit to keep everything within the 32-bit variable
148+
us_ticker_int_counter = (uint32_t)(delta >> 16);
149+
us_ticker_int_counter *= clk_mhz;
150+
us_ticker_int_counter >>= 16;
151+
152+
uint32_t us_ticker_int_remainder = (uint32_t)delta * clk_mhz;
153+
if (us_ticker_int_remainder == 0) {
154+
ticker_set(0xFFFFFFFF);
180155
us_ticker_int_counter--;
181156
} else {
182-
lptmr_set(us_ticker_int_remainder);
183-
us_ticker_int_remainder = 0;
157+
ticker_set(us_ticker_int_remainder);
184158
}
185159
}

0 commit comments

Comments
 (0)