From 1d1c391094341cc9f4317b544dab647357814642 Mon Sep 17 00:00:00 2001 From: Purdea Andrei Date: Sat, 18 Apr 2020 04:56:03 +0300 Subject: [PATCH 01/11] TEENSY3_1: make teensy compile with rtos --- targets/targets.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/targets/targets.json b/targets/targets.json index 56b9d72ec52..2e4c147f65e 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -1712,10 +1712,12 @@ "SLEEP", "SPI", "SPISLAVE", - "STDIO_MESSAGES" + "STDIO_MESSAGES", + "USBDEVICE" ], "release_versions": [ - "2" + "2", + "5" ], "device_name": "MK20DX256xxx7" }, From 8faa5755f0631362e6c5c7438e5df4a08d8dadae Mon Sep 17 00:00:00 2001 From: Purdea Andrei Date: Mon, 20 Apr 2020 02:35:05 +0300 Subject: [PATCH 02/11] TEENSY3_1: this target has a us ticker already implemented. Declare it so it works correctly in mbed os 5 --- targets/targets.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/targets/targets.json b/targets/targets.json index 2e4c147f65e..58734e9a85d 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -1713,7 +1713,8 @@ "SPI", "SPISLAVE", "STDIO_MESSAGES", - "USBDEVICE" + "USBDEVICE", + "USTICKER" ], "release_versions": [ "2", From 526fb212f2bc008e9d909a2db97e105cea0513a1 Mon Sep 17 00:00:00 2001 From: Purdea Andrei Date: Mon, 20 Apr 2020 05:01:17 +0300 Subject: [PATCH 03/11] TARGET_Freescale/TARGET_K20XX: add check, and report error if the PWM period amount overflows --- targets/TARGET_Freescale/TARGET_K20XX/pwmout_api.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/targets/TARGET_Freescale/TARGET_K20XX/pwmout_api.c b/targets/TARGET_Freescale/TARGET_K20XX/pwmout_api.c index 75015aa638e..7c9dc9dfe15 100644 --- a/targets/TARGET_Freescale/TARGET_K20XX/pwmout_api.c +++ b/targets/TARGET_Freescale/TARGET_K20XX/pwmout_api.c @@ -100,7 +100,9 @@ void pwmout_period_ms(pwmout_t* obj, int ms) { // Set the PWM period, keeping the duty cycle the same. void pwmout_period_us(pwmout_t* obj, int us) { float dc = pwmout_read(obj); - *obj->MOD = (uint32_t)(pwm_clock * (float)us) - 1; + uint32_t new_mod = (uint32_t)(pwm_clock * (float)us) - 1; + if ((new_mod > 0xffff) || (new_mod < 0)) error("PWM period out of range"); + *obj->MOD = new_mod; *obj->SYNC |= FTM_SYNC_SWSYNC_MASK; pwmout_write(obj, dc); } From 60913937b1ef94ec7015b43926a17b1fbced9394 Mon Sep 17 00:00:00 2001 From: Purdea Andrei Date: Mon, 20 Apr 2020 06:14:43 +0300 Subject: [PATCH 04/11] TARGET_Freescale/TARGET_K20XX: bugfix: Fix setting of open drain for non-portB pins Note: previous code here used to say: "enable open drain for I2C pins, only port b available" And the proceeded to strip off the Port letter, and modify the open drain setting for a pin with the same number on PORT B. If the pin configured as i2c wasn't on PORTB, then the previous code was touching the open-drain settings for the wrong pin, which is just plain wrong. Anyway, the comment suggests that the other ports might not have open drain operation, however a review of documentation shows: 1) For the "MK20DX128VLH5" chip used by the K20D50M board, neither the datasheet nor the reference manual seems to say anything about inexistent open drain capability 2) For the "MK20DX256VLH7" chip used by the TEENSY3_1 board, there is a mention of a pin potentially not supporting open-drain, without listing which pins are like that, however it also says that the ODE bit would be read-only in that case. I have tested some I2C-enabled PORTC pins on TEENSY3_1, and the bits are not read-only, and changing them has an effect on the physical hardware. --- targets/TARGET_Freescale/TARGET_K20XX/i2c_api.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/targets/TARGET_Freescale/TARGET_K20XX/i2c_api.c b/targets/TARGET_Freescale/TARGET_K20XX/i2c_api.c index 6e3b8596a1e..df29a5dbcfa 100644 --- a/targets/TARGET_Freescale/TARGET_K20XX/i2c_api.c +++ b/targets/TARGET_Freescale/TARGET_K20XX/i2c_api.c @@ -64,11 +64,11 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) { pinmap_pinout(sda, PinMap_I2C_SDA); pinmap_pinout(scl, PinMap_I2C_SCL); - /* enable open drain for I2C pins, only port b available */ - uint32_t pin_n = (uint32_t)(sda & 0x7C) >> 2; - PORTB->PCR[pin_n] |= PORT_PCR_ODE_MASK; - pin_n = (uint32_t)(scl & 0x7C) >> 2; - PORTB->PCR[pin_n] |= PORT_PCR_ODE_MASK; + /* enable open drain for I2C pins */ + __IO uint32_t* pin_pcr = (__IO uint32_t*)(PORTA_BASE + sda); + *pin_pcr |= PORT_PCR_ODE_MASK; + pin_pcr = (__IO uint32_t*)(PORTA_BASE + scl); + *pin_pcr |= PORT_PCR_ODE_MASK; } int i2c_start(i2c_t *obj) { From 6737e80c5c0c32a43ba558ac276b78a02889d5d2 Mon Sep 17 00:00:00 2001 From: Purdea Andrei Date: Mon, 20 Apr 2020 06:29:09 +0300 Subject: [PATCH 05/11] TEENSY3_1: bugfix: Fixing function number on some i2c pins --- .../TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c b/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c index 90a5b1eadb9..21554a70898 100644 --- a/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c +++ b/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c @@ -46,7 +46,7 @@ const PinMap PinMap_DAC[] = { const PinMap PinMap_I2C_SDA[] = { {PTB1, I2C_0, 2}, {PTB3, I2C_0, 2}, - {PTE0, I2C_1, 2}, + {PTE0, I2C_1, 6}, {PTC11, I2C_1, 2}, {NC , NC , 0} }; @@ -54,7 +54,7 @@ const PinMap PinMap_I2C_SDA[] = { const PinMap PinMap_I2C_SCL[] = { {PTB0, I2C_0, 2}, {PTB2, I2C_0, 2}, - {PTE1, I2C_1, 2}, + {PTE1, I2C_1, 6}, {PTC10, I2C_1, 2}, {NC , NC, 0} }; From 6071d5745b2f6eb411ab9470e9682581884b4f6e Mon Sep 17 00:00:00 2001 From: Purdea Andrei Date: Mon, 20 Apr 2020 06:34:41 +0300 Subject: [PATCH 06/11] TARGET_Freescale/TARGET_K20XX: optimization: clean up how the address of the PCR register is calculated This makes it simmetric with how it's done in pin_mode(), and saves about 2 instructions too in pin_functions(). --- targets/TARGET_Freescale/TARGET_K20XX/pinmap.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/targets/TARGET_Freescale/TARGET_K20XX/pinmap.c b/targets/TARGET_Freescale/TARGET_K20XX/pinmap.c index 2971f9241e3..97b5e0bad0f 100644 --- a/targets/TARGET_Freescale/TARGET_K20XX/pinmap.c +++ b/targets/TARGET_Freescale/TARGET_K20XX/pinmap.c @@ -20,10 +20,9 @@ void pin_function(PinName pin, int function) { MBED_ASSERT(pin != (PinName)NC); uint32_t port_n = (uint32_t)pin >> PORT_SHIFT; - uint32_t pin_n = (uint32_t)(pin & 0x7C) >> 2; - SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port_n); - __IO uint32_t* pin_pcr = &(((PORT_Type *)(PORTA_BASE + 0x1000 * port_n)))->PCR[pin_n]; + + __IO uint32_t* pin_pcr = (__IO uint32_t*)(PORTA_BASE + pin); // pin mux bits: [10:8] -> 11100000000 = (0x700) *pin_pcr = (*pin_pcr & ~0x700) | (function << 8); From 4a8117391ec1d40f0696e9a9fd9c961325460ee4 Mon Sep 17 00:00:00 2001 From: Purdea Andrei Date: Mon, 20 Apr 2020 07:48:18 +0300 Subject: [PATCH 07/11] K20D50M: removed 4 duplicates from PWM pinmap. Some of these were wrong, but luckily they were never found, because they were not the first in the list with the same pin. --- .../TARGET_K20XX/TARGET_K20D50M/PeripheralPins.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/targets/TARGET_Freescale/TARGET_K20XX/TARGET_K20D50M/PeripheralPins.c b/targets/TARGET_Freescale/TARGET_K20XX/TARGET_K20D50M/PeripheralPins.c index 8981f6d3711..b58eab0068e 100644 --- a/targets/TARGET_Freescale/TARGET_K20XX/TARGET_K20D50M/PeripheralPins.c +++ b/targets/TARGET_Freescale/TARGET_K20XX/TARGET_K20D50M/PeripheralPins.c @@ -97,10 +97,10 @@ const PinMap PinMap_PWM[] = { {LED_BLUE , PWM_8 , 3}, // PTA2, FTM0 CH7 // Arduino digital pinout - {D3, PWM_5 , 4}, // PTD4, FTM0 CH4 + // Duplicate: {D3, PWM_5 , 4}, // PTD4, FTM0 CH4 // also green led {D5, PWM_7 , 3}, // PTA1, FTM0 CH6 - {D6, PWM_3 , 4}, // PTC3, FTM0 CH2 - {D9, PWM_6 , 4}, // PTD5, FTM0 CH6 + // Duplicate: {D6, PWM_3 , 4}, // PTC3, FTM0 CH2 // also red led + // Duplicate: {D9, PWM_8 , 3}, // PTA2, FTM0 CH7 // also blue led {D10, PWM_2 , 4}, // PTC2, FTM0 CH1 {PTA0, PWM_6 , 3}, // PTA0, FTM0 CH5 @@ -112,7 +112,7 @@ const PinMap PinMap_PWM[] = { {PTB0, PWM_9 , 3}, // PTB0, FTM1 CH0 {PTB1, PWM_10, 3}, // PTB1, FTM1 CH1 {PTC1, PWM_1 , 4}, // PTC1, FTM0 CH0 - {PTD4, PWM_4 , 4}, // PTD4, FTM0 CH3 + // Duplicate: {PTD4, PWM_5 , 4}, // PTD4, FTM0 CH4 (also known as D3, and green led) {PTD6, PWM_7 , 4}, // PTD6, FTM0 CH6 {PTD7, PWM_8 , 4}, // PTD7, FTM0 CH7 From 1cbe7d96912f4b35b06046db779fc5f93ec1d01b Mon Sep 17 00:00:00 2001 From: Purdea Andrei Date: Mon, 20 Apr 2020 07:49:03 +0300 Subject: [PATCH 08/11] K20D50M: add missing PWM to pinmap --- .../TARGET_K20XX/TARGET_K20D50M/PeripheralPins.c | 1 + 1 file changed, 1 insertion(+) diff --git a/targets/TARGET_Freescale/TARGET_K20XX/TARGET_K20D50M/PeripheralPins.c b/targets/TARGET_Freescale/TARGET_K20XX/TARGET_K20D50M/PeripheralPins.c index b58eab0068e..adcf6c96ab4 100644 --- a/targets/TARGET_Freescale/TARGET_K20XX/TARGET_K20D50M/PeripheralPins.c +++ b/targets/TARGET_Freescale/TARGET_K20XX/TARGET_K20D50M/PeripheralPins.c @@ -100,6 +100,7 @@ const PinMap PinMap_PWM[] = { // Duplicate: {D3, PWM_5 , 4}, // PTD4, FTM0 CH4 // also green led {D5, PWM_7 , 3}, // PTA1, FTM0 CH6 // Duplicate: {D6, PWM_3 , 4}, // PTC3, FTM0 CH2 // also red led + {D7, PWM_4 , 4}, // PTC4, FTM0_CH3 // Duplicate: {D9, PWM_8 , 3}, // PTA2, FTM0 CH7 // also blue led {D10, PWM_2 , 4}, // PTC2, FTM0 CH1 From ab51fe546a613f1efdadfc7d49288d0af8d05995 Mon Sep 17 00:00:00 2001 From: Purdea Andrei Date: Mon, 20 Apr 2020 07:53:59 +0300 Subject: [PATCH 09/11] TEENSY3_1: PWM pinmap: removing LEDs that don't exist, and the pins also don't have PWM. --- .../TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c b/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c index 21554a70898..ba82eaff944 100644 --- a/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c +++ b/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c @@ -111,11 +111,6 @@ const PinMap PinMap_SPI_SSEL[] = { // CS /************PWM***************/ const PinMap PinMap_PWM[] = { - // LEDs - {LED_RED , PWM_3 , 4}, // PTC3, FTM0 CH2 - {LED_GREEN, PWM_5, 4}, // PTD4, FTM0 CH4 - {LED_BLUE , PWM_8 , 3}, // PTA2, FTM0 CH7 - {PTA0, PWM_6 , 3}, // PTA0, FTM0 CH5 {PTA1, PWM_7 , 3}, // PTA1, FTM0 CH6 {PTA3, PWM_1 , 3}, // PTA3, FTM0 CH0 From caf4ba079474f85fed296cfc6afe83fb23e7ecf1 Mon Sep 17 00:00:00 2001 From: Purdea Andrei Date: Mon, 20 Apr 2020 07:10:14 +0300 Subject: [PATCH 10/11] TEENSY3_1: PWM pinmap: PTD4 had the wrong channel, both in code and in the comment, and PTD5's comment was wrong. Note: tested on hardware: PTD4 was not useable, and now it is. --- .../TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c b/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c index ba82eaff944..ef547154f1c 100644 --- a/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c +++ b/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c @@ -123,8 +123,8 @@ const PinMap PinMap_PWM[] = { {PTC1, PWM_1 , 4}, // PTC1, FTM0 CH0 {PTC2, PWM_2 , 4}, // PTC2, FTM0 CH1 {PTC3, PWM_3 , 4}, // PTC3, FTM0 CH2 - {PTD4, PWM_4 , 4}, // PTD4, FTM0 CH3 - {PTD5, PWM_6 , 4}, // PTD5, FTM0 CH6 + {PTD4, PWM_5 , 4}, // PTD4, FTM0 CH4 + {PTD5, PWM_6 , 4}, // PTD5, FTM0 CH5 {PTD6, PWM_7 , 4}, // PTD6, FTM0 CH6 {PTD7, PWM_8 , 4}, // PTD7, FTM0 CH7 From 3e2016eb2a135a92cd3c975e6e6d0ca108c2f392 Mon Sep 17 00:00:00 2001 From: Purdea Andrei Date: Mon, 20 Apr 2020 08:58:41 +0300 Subject: [PATCH 11/11] TARGET_TEENSY3_1: implement missing pwm outputs * added support for module FTM2 * added 4 missing pwm-compatible pins * renamed PWMn keys to match documentation from https://os.mbed.com/platforms/Teensy-3-1/ (this was done, rather then changing documentation, bedcause these pins weren't working anyway because of missing FTM2 support) * tested on hardware --- .../TARGET_TEENSY3_1/PeripheralNames.h | 2 ++ .../TARGET_TEENSY3_1/PeripheralPins.c | 4 ++++ .../TARGET_K20XX/TARGET_TEENSY3_1/PinNames.h | 4 ++-- .../TARGET_K20XX/pwmout_api.c | 20 +++++++++++++++++-- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralNames.h b/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralNames.h index 3cbb1530a94..a5d15d5d440 100644 --- a/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralNames.h +++ b/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralNames.h @@ -48,6 +48,8 @@ typedef enum { PWM_8 = (0 << TPM_SHIFT) | (7), // FTM0 CH7 PWM_9 = (1 << TPM_SHIFT) | (0), // FTM1 CH0 PWM_10 = (1 << TPM_SHIFT) | (1), // FTM1 CH1 + PWM_11 = (2 << TPM_SHIFT) | (0), // FTM2 CH0 + PWM_12 = (2 << TPM_SHIFT) | (1), // FTM2 CH1 } PWMName; typedef enum { diff --git a/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c b/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c index ef547154f1c..3dd0c7ec0cf 100644 --- a/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c +++ b/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PeripheralPins.c @@ -113,6 +113,7 @@ const PinMap PinMap_SPI_SSEL[] = { // CS const PinMap PinMap_PWM[] = { {PTA0, PWM_6 , 3}, // PTA0, FTM0 CH5 {PTA1, PWM_7 , 3}, // PTA1, FTM0 CH6 + {PTA2, PWM_8 , 3}, // PTA2, FTM0 CH7 {PTA3, PWM_1 , 3}, // PTA3, FTM0 CH0 {PTA4, PWM_2 , 3}, // PTA4, FTM0 CH1 {PTA5, PWM_3 , 3}, // PTA5, FTM0 CH2 @@ -120,9 +121,12 @@ const PinMap PinMap_PWM[] = { {PTA13, PWM_10, 3}, // PTA13, FTM1 CH1 {PTB0, PWM_9 , 3}, // PTB0, FTM1 CH0 {PTB1, PWM_10, 3}, // PTB1, FTM1 CH1 + {PTB18, PWM_11, 3}, // PTB18, FTM2 CH0 + {PTB19, PWM_12, 3}, // PTB19, FTM2 CH1 {PTC1, PWM_1 , 4}, // PTC1, FTM0 CH0 {PTC2, PWM_2 , 4}, // PTC2, FTM0 CH1 {PTC3, PWM_3 , 4}, // PTC3, FTM0 CH2 + {PTC4, PWM_4 , 4}, // PTC4, FTM0 CH3 {PTD4, PWM_5 , 4}, // PTD4, FTM0 CH4 {PTD5, PWM_6 , 4}, // PTD5, FTM0 CH5 {PTD6, PWM_7 , 4}, // PTD6, FTM0 CH6 diff --git a/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PinNames.h b/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PinNames.h index bdb55e80656..f1fc380f5c6 100644 --- a/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PinNames.h +++ b/targets/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/PinNames.h @@ -283,8 +283,8 @@ typedef enum { PWM7 = PTD6, PWM8 = PTC1, PWM9 = PTC2, - PWM10 = PTB19, - PWM11 = PTB18, + PWM10 = PTB18, + PWM11 = PTB19, DAC = DAC0_OUT, diff --git a/targets/TARGET_Freescale/TARGET_K20XX/pwmout_api.c b/targets/TARGET_Freescale/TARGET_K20XX/pwmout_api.c index 7c9dc9dfe15..2c71bfd17a9 100644 --- a/targets/TARGET_Freescale/TARGET_K20XX/pwmout_api.c +++ b/targets/TARGET_Freescale/TARGET_K20XX/pwmout_api.c @@ -44,9 +44,25 @@ void pwmout_init(pwmout_t* obj, PinName pin) { unsigned int ftm_n = (pwm >> TPM_SHIFT); unsigned int ch_n = (pwm & 0xFF); - SIM->SCGC6 |= 1 << (SIM_SCGC6_FTM0_SHIFT + ftm_n); - FTM_Type *ftm = (FTM_Type *)(FTM0_BASE + 0x1000 * ftm_n); + if (ftm_n < 2) + { + SIM->SCGC6 |= 1 << (SIM_SCGC6_FTM0_SHIFT + ftm_n); + } else { +#if defined(TARGET_K20DX256) + /* note: the above hashdefine appears to be a misnomer, the two chips supported by this folder are: + * * MK20DX256VLH7 (used by the TEENSY3_1 board) + * * MK20DX128VLH5 (used by the K20D50M board) + * The difference that matters seems to come from the suffix of 5 vs 7. */ + SIM->SCGC3 |= 1 << (SIM_SCGC3_FTM2_SHIFT); +#ifndef FTM2_BASE +// Note: FTM2_BASE is missing from TARGET_TEENSY3_1/device/MK20DX256.h +#define FTM2_BASE (0x400B8000) +#endif + ftm = (FTM_Type *)(FTM2_BASE); // placed at non-contiguous address +#endif + } + ftm->CONF |= FTM_CONF_BDMMODE(3); ftm->SC = FTM_SC_CLKS(1) | FTM_SC_PS(clkdiv); // (clock)MHz / clkdiv ~= (0.75)MHz ftm->CONTROLS[ch_n].CnSC = (FTM_CnSC_MSB_MASK | FTM_CnSC_ELSB_MASK); /* No Interrupts; High True pulses on Edge Aligned PWM */