Skip to content

Commit 3662215

Browse files
committed
SPI, RTC, Serial changes
- SPI - implementation - RTC - there's 32.768kHz crystal, use that as a source - Serial - 10bit transfer
1 parent 988894e commit 3662215

File tree

7 files changed

+148
-133
lines changed

7 files changed

+148
-133
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void analogin_init(analogin_t *obj, PinName pin) {
5252
| ADC_CFG1_MODE(3) // (16)bits Resolution
5353
| ADC_CFG1_ADICLK(1); // Input Clock: (Bus Clock)/2
5454

55-
ADC0->CFG2 = ADC_CFG2_MUXSEL_MASK // ADxxb or ADxxa channels
55+
ADC0->CFG2 = ADC_CFG2_MUXSEL_MASK // ADxxb or ADxxa channels
5656
| ADC_CFG2_ADACKEN_MASK // Asynchronous Clock Output Enable
5757
| ADC_CFG2_ADHSC_MASK // High-Speed Configuration
5858
| ADC_CFG2_ADLSTS(0); // Long Sample Time Select

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ uint32_t gpio_set(PinName pin) {
2222
}
2323

2424
void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
25-
if(pin == NC) return;
25+
if(pin == NC)
26+
return;
2627

2728
obj->pin = pin;
2829
obj->mask = gpio_set(pin);
@@ -37,8 +38,12 @@ void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
3738

3839
gpio_dir(obj, direction);
3940
switch (direction) {
40-
case PIN_OUTPUT: pin_mode(pin, PullNone); break;
41-
case PIN_INPUT : pin_mode(pin, PullUp); break;
41+
case PIN_OUTPUT:
42+
pin_mode(pin, PullNone);
43+
break;
44+
case PIN_INPUT :
45+
pin_mode(pin, PullUp);
46+
break;
4247
}
4348
}
4449

@@ -48,7 +53,11 @@ void gpio_mode(gpio_t *obj, PinMode mode) {
4853

4954
void gpio_dir(gpio_t *obj, PinDirection direction) {
5055
switch (direction) {
51-
case PIN_INPUT : *obj->reg_dir &= ~obj->mask; break;
52-
case PIN_OUTPUT: *obj->reg_dir |= obj->mask; break;
56+
case PIN_INPUT :
57+
*obj->reg_dir &= ~obj->mask;
58+
break;
59+
case PIN_OUTPUT:
60+
*obj->reg_dir |= obj->mask;
61+
break;
5362
}
5463
}

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ static void handle_interrupt_in(PORT_Type *port, int ch_base) {
5353

5454
case IRQ_EITHER_EDGE:
5555
gpio += (port_num * 0x40);
56-
//gpio = (port == PORTA) ? (PTA) : (PTD);
5756
event = (gpio->PDIR & pmask) ? (IRQ_RISE) : (IRQ_FALL);
5857
break;
5958
}
@@ -82,23 +81,32 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
8281
IRQn_Type irq_n;
8382
switch (obj->port) {
8483
case PortA:
85-
ch_base = 0; irq_n = PORTA_IRQn; vector = (uint32_t)gpio_irqA;
84+
ch_base = 0;
85+
irq_n = PORTA_IRQn;
86+
vector = (uint32_t)gpio_irqA;
8687
break;
8788
case PortB:
88-
ch_base = 0; irq_n = PORTB_IRQn; vector = (uint32_t)gpio_irqB;
89+
ch_base = 32;
90+
irq_n = PORTB_IRQn;
91+
vector = (uint32_t)gpio_irqB;
8992
break;
9093
case PortC:
91-
ch_base = 0; irq_n = PORTC_IRQn; vector = (uint32_t)gpio_irqC;
94+
ch_base = 64;
95+
irq_n = PORTC_IRQn;
96+
vector = (uint32_t)gpio_irqC;
9297
break;
9398
case PortD:
94-
ch_base = 32; irq_n = PORTD_IRQn; vector = (uint32_t)gpio_irqD;
99+
ch_base = 96;
100+
irq_n = PORTD_IRQn; vector = (uint32_t)gpio_irqD;
95101
break;
96102
case PortE:
97-
ch_base = 0; irq_n = PORTE_IRQn; vector = (uint32_t)gpio_irqE;
103+
ch_base = 128;
104+
irq_n = PORTE_IRQn;
105+
vector = (uint32_t)gpio_irqE;
98106
break;
99107

100108
default:
101-
error("gpio_irq only supported on port A and D\n");
109+
error("gpio_irq only supported on port A-E.\n");
102110
break;
103111
}
104112
NVIC_SetVector(irq_n, vector);

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,17 @@ static void init(void) {
2222
// enable RTC clock
2323
SIM->SCGC6 |= SIM_SCGC6_RTC_MASK;
2424

25-
/*
26-
* configure PTC1 with alternate function 1: RTC_CLKIN
27-
* As the kl25z board does not have a 32kHz osc,
28-
* we use an external clock generated by the
29-
* interface chip
30-
*/
31-
PORTC->PCR[1] &= ~PORT_PCR_MUX_MASK;
32-
PORTC->PCR[1] = PORT_PCR_MUX(1);
33-
34-
// select RTC_CLKIN as RTC clock source
25+
// OSC32 as source
3526
SIM->SOPT1 &= ~SIM_SOPT1_OSC32KSEL_MASK;
36-
SIM->SOPT1 |= SIM_SOPT1_OSC32KSEL(2);
27+
SIM->SOPT1 |= SIM_SOPT1_OSC32KSEL(0);
3728
}
3829

3930
void rtc_init(void) {
4031
init();
4132

33+
// Enable the oscillator
34+
RTC_CR |= RTC_CR_OSCE_MASK;
35+
4236
//Configure the TSR. default value: 1
4337
RTC->TSR = 1;
4438

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

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,18 @@
2424
#include "pinmap.h"
2525
#include "error.h"
2626

27-
/******************************************************************************
28-
* INITIALIZATION
29-
******************************************************************************/
3027
static const PinMap PinMap_UART_TX[] = {
31-
{PTB17, UART_0, 3},
28+
{PTB17, UART_0, 3},
3229
{NC , NC , 0}
3330
};
3431

3532
static const PinMap PinMap_UART_RX[] = {
36-
{PTB16, UART_0, 3},
33+
{PTB16, UART_0, 3},
3734
{NC , NC , 0}
3835
};
3936

4037
#define UART_NUM 3
38+
4139
static uint32_t serial_irq_ids[UART_NUM] = {0};
4240
static uart_irq_handler irq_handler;
4341

@@ -49,9 +47,8 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
4947
UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
5048
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
5149
UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
52-
if ((int)uart == NC) {
50+
if ((int)uart == NC)
5351
error("Serial pinout mapping failed");
54-
}
5552

5653
obj->uart = (UART_Type *)uart;
5754
// enable clk
@@ -63,7 +60,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
6360
}
6461
// Disable UART before changing registers
6562
obj->uart->C2 &= ~(UART_C2_RE_MASK | UART_C2_TE_MASK);
66-
63+
6764
switch (uart) {
6865
case UART_0: obj->index = 0; break;
6966
case UART_1: obj->index = 1; break;
@@ -94,27 +91,13 @@ void serial_free(serial_t *obj) {
9491
serial_irq_ids[obj->index] = 0;
9592
}
9693

97-
// serial_baud
98-
//
99-
// set the baud rate, taking in to account the current SystemFrequency
100-
//
101-
// The LPC2300 and LPC1700 have a divider and a fractional divider to control the
102-
// baud rate. The formula is:
103-
//
104-
// Baudrate = (1 / PCLK) * 16 * DL * (1 + DivAddVal / MulVal)
105-
// where:
106-
// 1 < MulVal <= 15
107-
// 0 <= DivAddVal < 14
108-
// DivAddVal < MulVal
109-
//
11094
void serial_baud(serial_t *obj, int baudrate) {
111-
11295
// save C2 state
113-
uint8_t c2_state = (obj->uart->C2 & (UART_C2_RE_MASK | UART_C2_TE_MASK));
114-
96+
uint32_t c2_state = (obj->uart->C2 & (UART_C2_RE_MASK | UART_C2_TE_MASK));
97+
11598
// Disable UART before changing registers
11699
obj->uart->C2 &= ~(UART_C2_RE_MASK | UART_C2_TE_MASK);
117-
100+
118101
// [TODO] not hardcode this value
119102
uint32_t PCLK = (obj->uart == UART0) ? 48000000u : 24000000u;
120103

@@ -129,27 +112,26 @@ void serial_baud(serial_t *obj, int baudrate) {
129112
// set BDH and BDL
130113
obj->uart->BDH = (obj->uart->BDH & ~(0x1f)) | ((DL >> 8) & 0x1f);
131114
obj->uart->BDL = (obj->uart->BDL & ~(0xff)) | ((DL >> 0) & 0xff);
132-
115+
133116
// restore C2 state
134117
obj->uart->C2 |= c2_state;
135118
}
136119

137120
void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
138-
// uint8_t m10 = 0;
139-
121+
140122
// save C2 state
141-
uint8_t c2_state = (obj->uart->C2 & (UART_C2_RE_MASK | UART_C2_TE_MASK));
142-
123+
uint32_t c2_state = (obj->uart->C2 & (UART_C2_RE_MASK | UART_C2_TE_MASK));
124+
143125
// Disable UART before changing registers
144126
obj->uart->C2 &= ~(UART_C2_RE_MASK | UART_C2_TE_MASK);
145-
127+
146128
// 8 data bits = 0 ... 9 data bits = 1
147-
if ((data_bits < 8) || (data_bits > 9)) {
129+
if ((data_bits < 8) || (data_bits > 9))
148130
error("Invalid number of bits (%d) in serial format, should be 8..9\r\n", data_bits);
149-
}
131+
150132
data_bits -= 8;
151133

152-
uint8_t parity_enable, parity_select;
134+
uint32_t parity_enable, parity_select;
153135
switch (parity) {
154136
case ParityNone: parity_enable = 0; parity_select = 0; break;
155137
case ParityOdd : parity_enable = 1; parity_select = 1; data_bits++; break;
@@ -160,36 +142,36 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
160142
}
161143

162144
// 1 stop bits = 0, 2 stop bits = 1
163-
if ((stop_bits != 1) && (stop_bits != 2)) {
145+
if ((stop_bits != 1) && (stop_bits != 2))
164146
error("Invalid stop bits specified\r\n");
165-
}
166147
stop_bits -= 1;
167-
148+
149+
uint32_t m10 = 0;
150+
168151
// 9 data bits + parity
169152
if (data_bits == 2) {
170153
// only uart0 supports 10 bit communication
171-
if (obj->index != 0) {
154+
if (obj->index != 0)
172155
error("Invalid number of bits (9) to be used with parity\r\n");
173-
}
174156
data_bits = 0;
175-
//m10 = 1;
157+
m10 = 1;
176158
}
177159

178160
// data bits, parity and parity mode
179161
obj->uart->C1 = ((data_bits << 4)
180162
| (parity_enable << 1)
181163
| (parity_select << 0));
182-
183-
// enable 10bit mode if needed
184-
// if (obj->index == 0) {
185-
// obj->uart->C4 &= ~UARTLP_C4_M10_MASK;
186-
// obj->uart->C4 |= (m10 << UARTLP_C4_M10_SHIFT);
187-
// }
188-
164+
165+
//enable 10bit mode if needed
166+
if (obj->index == 0) {
167+
obj->uart->C4 &= ~UART_C4_M10_MASK;
168+
obj->uart->C4 |= (m10 << UART_C4_M10_SHIFT);
169+
}
170+
189171
// stop bits
190172
obj->uart->BDH &= ~UART_BDH_SBR_MASK;
191173
obj->uart->BDH |= (stop_bits << UART_BDH_SBR_SHIFT);
192-
174+
193175
// restore C2 state
194176
obj->uart->C2 |= c2_state;
195177
}
@@ -220,15 +202,28 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
220202
IRQn_Type irq_n = (IRQn_Type)0;
221203
uint32_t vector = 0;
222204
switch ((int)obj->uart) {
223-
case UART_0: irq_n=UART0_RX_TX_IRQn; vector = (uint32_t)&uart0_irq; break;
224-
case UART_1: irq_n=UART1_RX_TX_IRQn; vector = (uint32_t)&uart1_irq; break;
225-
case UART_2: irq_n=UART2_RX_TX_IRQn; vector = (uint32_t)&uart2_irq; break;
205+
case UART_0:
206+
irq_n=UART0_RX_TX_IRQn;
207+
vector = (uint32_t)&uart0_irq;
208+
break;
209+
case UART_1:
210+
irq_n=UART1_RX_TX_IRQn;
211+
vector = (uint32_t)&uart1_irq;
212+
break;
213+
case UART_2:
214+
irq_n=UART2_RX_TX_IRQn;
215+
vector = (uint32_t)&uart2_irq;
216+
break;
226217
}
227218

228219
if (enable) {
229220
switch (irq) {
230-
case RxIrq: obj->uart->C2 |= (UART_C2_RIE_MASK); break;
231-
case TxIrq: obj->uart->C2 |= (UART_C2_TIE_MASK); break;
221+
case RxIrq:
222+
obj->uart->C2 |= (UART_C2_RIE_MASK);
223+
break;
224+
case TxIrq:
225+
obj->uart->C2 |= (UART_C2_TIE_MASK);
226+
break;
232227
}
233228
NVIC_SetVector(irq_n, vector);
234229
NVIC_EnableIRQ(irq_n);
@@ -237,21 +232,26 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
237232
int all_disabled = 0;
238233
SerialIrq other_irq = (irq == RxIrq) ? (TxIrq) : (RxIrq);
239234
switch (irq) {
240-
case RxIrq: obj->uart->C2 &= ~(UART_C2_RIE_MASK); break;
241-
case TxIrq: obj->uart->C2 &= ~(UART_C2_TIE_MASK); break;
235+
case RxIrq:
236+
obj->uart->C2 &= ~(UART_C2_RIE_MASK);
237+
break;
238+
case TxIrq:
239+
obj->uart->C2 &= ~(UART_C2_TIE_MASK);
240+
break;
242241
}
243242
switch (other_irq) {
244-
case RxIrq: all_disabled = (obj->uart->C2 & (UART_C2_RIE_MASK)) == 0; break;
245-
case TxIrq: all_disabled = (obj->uart->C2 & (UART_C2_TIE_MASK)) == 0; break;
243+
case RxIrq:
244+
all_disabled = (obj->uart->C2 & (UART_C2_RIE_MASK)) == 0;
245+
break;
246+
case TxIrq:
247+
all_disabled = (obj->uart->C2 & (UART_C2_TIE_MASK)) == 0;
248+
break;
246249
}
247250
if (all_disabled)
248251
NVIC_DisableIRQ(irq_n);
249252
}
250253
}
251254

252-
/******************************************************************************
253-
* READ/WRITE
254-
******************************************************************************/
255255
int serial_getc(serial_t *obj) {
256256
while (!serial_readable(obj));
257257
return obj->uart->D;
@@ -263,7 +263,7 @@ void serial_putc(serial_t *obj, int c) {
263263
}
264264

265265
int serial_readable(serial_t *obj) {
266-
266+
267267
return (obj->uart->S1 & UART_S1_RDRF_MASK);
268268
}
269269

@@ -280,7 +280,7 @@ void serial_pinout_tx(PinName tx) {
280280
}
281281

282282
void serial_break_set(serial_t *obj) {
283-
obj->uart->C2 |= UART_C2_SBK_MASK;
283+
obj->uart->C2 |= UART_C2_SBK_MASK;
284284
}
285285

286286
void serial_break_clear(serial_t *obj) {

0 commit comments

Comments
 (0)