Skip to content

Commit c5a2e6e

Browse files
committed
pwm, spi - pin definition, dac removed
1 parent 0e99a7c commit c5a2e6e

File tree

6 files changed

+62
-122
lines changed

6 files changed

+62
-122
lines changed

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,17 @@ typedef enum {
3737

3838
#define TPM_SHIFT 8
3939
typedef enum {
40-
PWM_1 = (0 << TPM_SHIFT) | (0), // TPM0 CH0
41-
PWM_2 = (0 << TPM_SHIFT) | (1), // TPM0 CH1
42-
PWM_3 = (0 << TPM_SHIFT) | (2), // TPM0 CH2
43-
PWM_4 = (0 << TPM_SHIFT) | (3), // TPM0 CH3
44-
PWM_5 = (0 << TPM_SHIFT) | (4), // TPM0 CH4
45-
PWM_6 = (0 << TPM_SHIFT) | (5), // TPM0 CH5
40+
PWM_1 = (0 << TPM_SHIFT) | (0), // FTM0 CH0
41+
PWM_2 = (0 << TPM_SHIFT) | (1), // FTM0 CH1
42+
PWM_3 = (0 << TPM_SHIFT) | (2), // FTM0 CH2
43+
PWM_4 = (0 << TPM_SHIFT) | (3), // FTM0 CH3
44+
PWM_5 = (0 << TPM_SHIFT) | (4), // FTM0 CH4
45+
PWM_6 = (0 << TPM_SHIFT) | (5), // FTM0 CH5
46+
PWM_7 = (0 << TPM_SHIFT) | (6), // FTM0 CH6
47+
PWM_8 = (0 << TPM_SHIFT) | (7), // FTM0 CH7
4648

47-
PWM_7 = (1 << TPM_SHIFT) | (0), // TPM1 CH0
48-
PWM_8 = (1 << TPM_SHIFT) | (1), // TPM1 CH1
49-
50-
PWM_9 = (2 << TPM_SHIFT) | (0), // TPM2 CH0
51-
PWM_10 = (2 << TPM_SHIFT) | (1) // TPM2 CH1
49+
PWM_9 = (1 << TPM_SHIFT) | (0), // FTM1 CH0
50+
PWM_10 = (1 << TPM_SHIFT) | (1), // FTM1 CH1
5251
} PWMName;
5352

5453
typedef enum {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#define DEVICE_INTERRUPTIN 1
2424

2525
#define DEVICE_ANALOGIN 1
26-
#define DEVICE_ANALOGOUT 1
26+
#define DEVICE_ANALOGOUT 0
2727

2828
#define DEVICE_SERIAL 1
2929

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ struct analogin_s {
5454
ADCName adc;
5555
};
5656

57-
struct dac_s {
58-
DACName dac;
59-
};
60-
6157
struct i2c_s {
6258
I2C_Type *i2c;
6359
};

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ void port_mode(port_t *obj, PinMode mode) {
5454

5555
void port_dir(port_t *obj, PinDirection dir) {
5656
switch (dir) {
57-
case PIN_INPUT : *obj->reg_dir &= ~obj->mask; break;
58-
case PIN_OUTPUT: *obj->reg_dir |= obj->mask; break;
57+
case PIN_INPUT :
58+
*obj->reg_dir &= ~obj->mask;
59+
break;
60+
case PIN_OUTPUT:
61+
*obj->reg_dir |= obj->mask;
62+
break;
5963
}
6064
}
6165

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

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,16 @@
2121

2222
static const PinMap PinMap_PWM[] = {
2323
// LEDs
24-
{LED_RED , PWM_9 , 3}, // PTB18, FTM2 CH0
25-
{LED_GREEN, PWM_10, 3}, // PTB19, FTM2 CH1
26-
{LED_BLUE , PWM_2 , 4}, // PTD1 , FTM0 CH1
24+
{LED_RED , PWM_3 , 3}, // PTC3, FTM0 CH2
25+
{LED_GREEN, PWM_5, 3}, // PTD4, FTM0 CH4
26+
{LED_BLUE , PWM_9 , 3}, // PTA2 , FTM0 CH7
2727

2828
// Arduino digital pinout
29-
{D0, PWM_9 , 3}, // PTA1 , FTM2 CH0
30-
{D1, PWM_10, 3}, // PTA2 , FTM2 CH1
31-
{D2, PWM_5 , 4}, // PTD4 , FTM0 CH4
32-
{D3, PWM_7 , 3}, // PTA12, FTM1 CH0
33-
{D4, PWM_2 , 3}, // PTA4 , FTM0 CH1
34-
{D5, PWM_3 , 3}, // PTA5 , FTM0 CH2
35-
{D6, PWM_5 , 3}, // PTC8 , FTM0 CH4
36-
{D7, PWM_6 , 3}, // PTC9 , FTM0 CH5
37-
{D8, PWM_8 , 3}, // PTA13, FTM1 CH1
38-
{D9, PWM_6 , 4}, // PTD5 , FTM0 CH5
39-
{D10, PWM_1 , 4}, // PTD0 , FTM0 CH0
40-
{D11, PWM_3 , 4}, // PTD2 , FTM0 CH2
41-
{D12, PWM_4 , 4}, // PTD3 , FTM0 CH3
42-
{D13, PWM_2 , 4}, // PTD1 , FTM0 CH1,
43-
44-
{PTA0, PWM_6, 3},
45-
{PTA3, PWM_1, 3},
46-
{PTB0, PWM_7, 3},
47-
{PTB1, PWM_8, 3},
48-
{PTB2, PWM_9, 3},
49-
{PTB3, PWM_10, 3},
50-
{PTC1, PWM_1, 4},
51-
{PTC2, PWM_2, 4},
52-
{PTC3, PWM_3, 4},
53-
{PTC4, PWM_4, 4},
54-
{PTE20, PWM_7, 3},
55-
{PTE21, PWM_8, 3},
56-
{PTE22, PWM_9, 3},
57-
{PTE23, PWM_10, 3},
58-
{PTE24, PWM_1, 3},
59-
{PTE25, PWM_2, 3},
60-
{PTE29, PWM_3, 3},
61-
{PTE30, PWM_4, 3},
62-
{PTE31, PWM_5, 3},
29+
{D3, PWM_5 , 3}, // PTA12, FTM0 CH4
30+
{D5, PWM_7 , 3}, // PTA1 , FTM0 CH6
31+
{D6, PWM_3 , 3}, // PTC3 , FTM0 CH2
32+
{D9, PWM_8 , 4}, // PTD2 , FTM0 CH7
33+
{D10, PWM_2 , 4}, // PTC2 , FTM0 CH1
6334

6435
{NC , NC , 0}
6536
};
@@ -78,7 +49,6 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
7849

7950
SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port);
8051
SIM->SCGC6 |= 1 << (SIM_SCGC6_FTM0_SHIFT + ftm_n);
81-
SIM->SOPT2 |= SIM_SOPT2_CLKOUTSEL(1); // Clock source: MCGFLLCLK or MCGPLLCLK
8252

8353
FTM_Type *ftm = (FTM_Type *)(FTM0_BASE + 0x1000 * ftm_n);
8454
ftm->SC = FTM_SC_CLKS(1) | FTM_SC_PS(6); // (48)MHz / 64 = (0.75)MHz

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

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -22,54 +22,26 @@
2222
#include "error.h"
2323

2424
static const PinMap PinMap_SPI_SCLK[] = {
25-
{PTA15, SPI_0, 2},
26-
{PTB11, SPI_0, 2},
27-
{PTC5, SPI_0, 2},
28-
{PTD1, SPI_0, 2},
29-
{PTD5, SPI_0, 2},
30-
{PTE2, SPI_0, 2},
25+
{PTC5, SPI_0, 2},
26+
{PTD1, SPI_0, 2},
3127
{NC , NC , 0}
3228
};
3329

3430
static const PinMap PinMap_SPI_MOSI[] = {
35-
{PTA16, SPI_0, 2},
36-
{PTA17, SPI_0, 5},
37-
{PTB16, SPI_0, 2},
38-
{PTB17, SPI_0, 5},
39-
{PTC6, SPI_0, 2},
40-
{PTC7, SPI_0, 5},
41-
{PTD2, SPI_0, 2},
42-
{PTD3, SPI_0, 5},
43-
{PTD6, SPI_0, 2},
44-
{PTD7, SPI_0, 5},
45-
{PTE1, SPI_0, 2},
46-
{PTE3, SPI_0, 5},
31+
{PTD2, SPI_0, 2},
32+
{PTC6, SPI_0, 2},
4733
{NC , NC , 0}
4834
};
4935

5036
static const PinMap PinMap_SPI_MISO[] = {
51-
{PTA16, SPI_0, 5},
52-
{PTA17, SPI_0, 2},
53-
{PTB16, SPI_0, 5},
54-
{PTB17, SPI_0, 2},
55-
{PTC6, SPI_0, 5},
56-
{PTC7, SPI_0, 2},
57-
{PTD2, SPI_0, 5},
58-
{PTD3, SPI_0, 2},
59-
{PTD6, SPI_0, 5},
60-
{PTD7, SPI_0, 2},
61-
{PTE1, SPI_0, 5},
62-
{PTE3, SPI_0, 2},
37+
{PTD3, SPI_0, 2},
38+
{PTC7, SPI_0, 2},
6339
{NC , NC , 0}
6440
};
6541

6642
static const PinMap PinMap_SPI_SSEL[] = {
67-
{PTA14, SPI_0, 2},
68-
{PTB10, SPI_0, 2},
69-
{PTC4, SPI_0, 2},
70-
{PTD0, SPI_0, 2},
71-
{PTD4, SPI_0, 2},
72-
{PTE4, SPI_0, 2},
43+
{PTD0, SPI_0, 2},
44+
{PTC4, SPI_0, 2},
7345
{NC , NC , 0}
7446
};
7547

@@ -90,7 +62,6 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
9062
// enable power and clocking
9163
switch ((int)obj->spi) {
9264
case SPI_0: SIM->SCGC5 |= 1 << 11; SIM->SCGC4 |= 1 << 22; break;
93-
//case SPI_1: SIM->SCGC5 |= 1 << 13; SIM->SCGC4 |= 1 << 23; break;
9465
}
9566

9667
// set default format and frequency
@@ -137,35 +108,35 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
137108
}
138109

139110
void spi_frequency(spi_t *obj, int hz) {
140-
// uint32_t error = 0;
141-
// uint32_t p_error = 0xffffffff;
142-
// uint32_t ref = 0;
143-
// uint8_t spr = 0;
144-
// uint8_t ref_spr = 0;
145-
// uint8_t ref_prescaler = 0;
146-
147-
// // bus clk
148-
// uint32_t PCLK = 48000000u;
149-
// uint8_t prescaler = 1;
150-
// uint8_t divisor = 2;
151-
152-
// for (prescaler = 1; prescaler <= 8; prescaler++) {
153-
// divisor = 2;
154-
// for (spr = 0; spr <= 8; spr++, divisor *= 2) {
155-
// ref = PCLK / (prescaler*divisor);
156-
// if (ref > (uint32_t)hz)
157-
// continue;
158-
// error = hz - ref;
159-
// if (error < p_error) {
160-
// ref_spr = spr;
161-
// ref_prescaler = prescaler - 1;
162-
// p_error = error;
163-
// }
164-
// }
165-
// }
166-
167-
// // set SPPR and SPR
168-
// obj->spi->CTAR = ((ref_prescaler & 0x7) << 4) | (ref_spr & 0xf);
111+
uint32_t error = 0;
112+
uint32_t p_error = 0xffffffff;
113+
uint32_t ref = 0;
114+
uint8_t spr = 0;
115+
uint8_t ref_spr = 0;
116+
uint8_t ref_prescaler = 0;
117+
118+
// bus clk
119+
uint32_t PCLK = 48000000u;
120+
uint8_t prescaler = 1;
121+
uint8_t divisor = 2;
122+
123+
for (prescaler = 1; prescaler <= 8; prescaler++) {
124+
divisor = 2;
125+
for (spr = 0; spr <= 8; spr++, divisor *= 2) {
126+
ref = PCLK / (prescaler*divisor);
127+
if (ref > (uint32_t)hz)
128+
continue;
129+
error = hz - ref;
130+
if (error < p_error) {
131+
ref_spr = spr;
132+
ref_prescaler = prescaler - 1;
133+
p_error = error;
134+
}
135+
}
136+
}
137+
138+
// set SPPR and SPR
139+
obj->spi->CTAR[0] = ((ref_prescaler & 0x7) << 4) | (ref_spr & 0xf);
169140
}
170141

171142
static inline int spi_writeable(spi_t * obj) {

0 commit comments

Comments
 (0)