Skip to content

Commit b73b57d

Browse files
committed
I2C, pullup
1 parent c9a029c commit b73b57d

File tree

6 files changed

+43
-55
lines changed

6 files changed

+43
-55
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ typedef enum {
4545
PWM_6 = (0 << TPM_SHIFT) | (5), // FTM0 CH5
4646
PWM_7 = (0 << TPM_SHIFT) | (6), // FTM0 CH6
4747
PWM_8 = (0 << TPM_SHIFT) | (7), // FTM0 CH7
48-
49-
PWM_9 = (1 << TPM_SHIFT) | (0), // FTM1 CH0
50-
PWM_10 = (1 << TPM_SHIFT) | (1), // FTM1 CH1
48+
PWM_9 = (1 << TPM_SHIFT) | (0), // FTM1 CH0
49+
PWM_10 = (1 << TPM_SHIFT) | (1), // FTM1 CH1
5150
} PWMName;
5251

5352
typedef enum {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,11 @@ typedef enum {
235235
NC = (int)0xFFFFFFFF
236236
} PinName;
237237

238-
/* PullDown not available for KL05 */
238+
239239
typedef enum {
240240
PullNone = 0,
241-
PullUp = 2,
241+
PullDown = 2,
242+
PullUp = 3,
242243
} PinMode;
243244

244245
#ifdef __cplusplus

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ typedef enum {
3131
#ifdef __cplusplus
3232
}
3333
#endif
34+
3435
#endif

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

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,14 @@
2020
#include "error.h"
2121

2222
static const PinMap PinMap_I2C_SDA[] = {
23-
{PTE25, I2C_0, 5},
24-
{PTC9, I2C_0, 2},
25-
{PTE0, I2C_0, 6},
2623
{PTB1, I2C_0, 2},
2724
{PTB3, I2C_0, 2},
28-
{PTC11, I2C_0, 2},
29-
{PTC2, I2C_0, 2},
30-
{PTA4, I2C_0, 2},
3125
{NC , NC , 0}
3226
};
3327

3428
static const PinMap PinMap_I2C_SCL[] = {
35-
{PTE24, I2C_0, 5},
36-
{PTC8, I2C_0, 2},
37-
{PTE1, I2C_0, 6},
3829
{PTB0, I2C_0, 2},
3930
{PTB2, I2C_0, 2},
40-
{PTC10, I2C_0, 2},
41-
{PTC1, I2C_0, 2},
4231
{NC , NC, 0}
4332
};
4433

@@ -66,15 +55,11 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
6655
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
6756
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
6857
obj->i2c = (I2C_Type*)pinmap_merge(i2c_sda, i2c_scl);
69-
if ((int)obj->i2c == NC) {
58+
if ((int)obj->i2c == NC)
7059
error("I2C pin mapping failed");
71-
}
7260

73-
// enable power
74-
switch ((int)obj->i2c) {
75-
case I2C_0: SIM->SCGC5 |= 1 << 13; SIM->SCGC4 |= 1 << 6; break;
76-
//case I2C_1: SIM->SCGC5 |= 1 << 11; SIM->SCGC4 |= 1 << 7; break;
77-
}
61+
SIM->SCGC4 |= SIM_SCGC4_I2C0_MASK;
62+
SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK;
7863

7964
// set default frequency at 100k
8065
i2c_frequency(obj, 100000);
@@ -84,12 +69,12 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
8469

8570
pinmap_pinout(sda, PinMap_I2C_SDA);
8671
pinmap_pinout(scl, PinMap_I2C_SCL);
87-
72+
8873
first_read = 1;
8974
}
9075

9176
int i2c_start(i2c_t *obj) {
92-
uint8_t temp;
77+
uint32_t temp;
9378
volatile int i;
9479
// if we are in the middle of a transaction
9580
// activate the repeat_start flag
@@ -118,19 +103,20 @@ int i2c_stop(i2c_t *obj) {
118103
// when there is no waiting time after a STOP.
119104
// This wait is also included on the samples
120105
// code provided with the freedom board
121-
for (n = 0; n < 100; n++) __NOP();
106+
for (n = 0; n < 100; n++)
107+
__NOP();
122108
first_read = 1;
123109
return 0;
124110
}
125111

126112
static int timeout_status_poll(i2c_t *obj, uint32_t mask) {
127113
uint32_t i, timeout = 1000;
128-
114+
129115
for (i = 0; i < timeout; i++) {
130116
if (obj->i2c->S & mask)
131117
return 0;
132118
}
133-
119+
134120
return 1;
135121
}
136122

@@ -139,14 +125,14 @@ static int timeout_status_poll(i2c_t *obj, uint32_t mask) {
139125
// 1: OK ack not received
140126
// 2: failure
141127
static int i2c_wait_end_tx_transfer(i2c_t *obj) {
142-
128+
143129
// wait for the interrupt flag
144130
if (timeout_status_poll(obj, I2C_S_IICIF_MASK)) {
145131
return 2;
146132
}
147-
133+
148134
obj->i2c->S |= I2C_S_IICIF_MASK;
149-
135+
150136
// wait transfer complete
151137
if (timeout_status_poll(obj, I2C_S_TCF_MASK)) {
152138
return 2;
@@ -164,9 +150,9 @@ static int i2c_wait_end_rx_transfer(i2c_t *obj) {
164150
if (timeout_status_poll(obj, I2C_S_IICIF_MASK)) {
165151
return 1;
166152
}
167-
153+
168154
obj->i2c->S |= I2C_S_IICIF_MASK;
169-
155+
170156
return 0;
171157
}
172158

@@ -301,33 +287,33 @@ void i2c_reset(i2c_t *obj) {
301287

302288
int i2c_byte_read(i2c_t *obj, int last) {
303289
char data;
304-
290+
305291
// set rx mode
306292
obj->i2c->C1 &= ~I2C_C1_TX_MASK;
307-
293+
308294
if(first_read) {
309295
// first dummy read
310296
i2c_do_read(obj, &data, 0);
311297
first_read = 0;
312298
}
313-
299+
314300
if (last) {
315301
// set tx mode
316302
obj->i2c->C1 |= I2C_C1_TX_MASK;
317303
return obj->i2c->D;
318304
}
319-
305+
320306
i2c_do_read(obj, &data, last);
321-
307+
322308
return data;
323309
}
324310

325311
int i2c_byte_write(i2c_t *obj, int data) {
326312
first_read = 1;
327-
313+
328314
// set tx mode
329315
obj->i2c->C1 |= I2C_C1_TX_MASK;
330-
316+
331317
return !i2c_do_write(obj, (data & 0xFF));
332318
}
333319

@@ -348,10 +334,10 @@ int i2c_slave_receive(i2c_t *obj) {
348334
switch(obj->i2c->S) {
349335
// read addressed
350336
case 0xE6: return 1;
351-
337+
352338
// write addressed
353339
case 0xE2: return 3;
354-
340+
355341
default: return 0;
356342
}
357343
}
@@ -360,59 +346,59 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
360346
uint8_t dummy_read;
361347
uint8_t * ptr;
362348
int count;
363-
349+
364350
// set rx mode
365351
obj->i2c->C1 &= ~I2C_C1_TX_MASK;
366-
352+
367353
// first dummy read
368354
dummy_read = obj->i2c->D;
369355
if(i2c_wait_end_rx_transfer(obj)) {
370356
return 0;
371357
}
372-
358+
373359
// read address
374360
dummy_read = obj->i2c->D;
375361
if(i2c_wait_end_rx_transfer(obj)) {
376362
return 0;
377363
}
378-
364+
379365
// read (length - 1) bytes
380366
for (count = 0; count < (length - 1); count++) {
381367
data[count] = obj->i2c->D;
382-
if(i2c_wait_end_rx_transfer(obj)) {
368+
if (i2c_wait_end_rx_transfer(obj)) {
383369
return count;
384370
}
385371
}
386372

387373
// read last byte
388374
ptr = (length == 0) ? &dummy_read : (uint8_t *)&data[count];
389375
*ptr = obj->i2c->D;
390-
376+
391377
return (length) ? (count + 1) : 0;
392378
}
393379

394380
int i2c_slave_write(i2c_t *obj, const char *data, int length) {
395381
int i, count = 0;
396-
382+
397383
// set tx mode
398384
obj->i2c->C1 |= I2C_C1_TX_MASK;
399-
385+
400386
for (i = 0; i < length; i++) {
401387
if(i2c_do_write(obj, data[count++]) == 2) {
402388
return i;
403389
}
404390
}
405-
391+
406392
// set rx mode
407393
obj->i2c->C1 &= ~I2C_C1_TX_MASK;
408-
394+
409395
// dummy rx transfer needed
410396
// otherwise the master cannot generate a stop bit
411397
obj->i2c->D;
412398
if(i2c_wait_end_rx_transfer(obj) == 2) {
413399
return count;
414400
}
415-
401+
416402
return count;
417403
}
418404

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#include "error.h"
1818

1919
void pin_function(PinName pin, int function) {
20-
if (pin == (PinName)NC) return;
20+
if (pin == (PinName)NC)
21+
return;
2122

2223
uint32_t port_n = (uint32_t)pin >> PORT_SHIFT;
2324
uint32_t pin_n = (uint32_t)(pin & 0x7C) >> 2;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void rtc_init(void) {
3131
init();
3232

3333
// Enable the oscillator
34-
RTC_CR |= RTC_CR_OSCE_MASK;
34+
RTC->CR |= RTC_CR_OSCE_MASK;
3535

3636
//Configure the TSR. default value: 1
3737
RTC->TSR = 1;

0 commit comments

Comments
 (0)