Skip to content

Commit b11a138

Browse files
committed
PSOC6: update the HAL implementation to latest PDL
Integrate the latest fixes to the PSoC 6 HAL.
1 parent ef5cca7 commit b11a138

20 files changed

+1146
-607
lines changed

targets/TARGET_Cypress/TARGET_PSOC6/PeripheralPins.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
22
* mbed Microcontroller Library
33
* Copyright (c) 2017-2018 Future Electronics
4+
* Copyright (c) 2018-2019 Cypress Semiconductor Corporation
5+
* SPDX-License-Identifier: Apache-2.0
46
*
57
* Licensed under the Apache License, Version 2.0 (the "License");
68
* you may not use this file except in compliance with the License.

targets/TARGET_Cypress/TARGET_PSOC6/analogin_api.c

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
22
* mbed Microcontroller Library
33
* Copyright (c) 2017-2018 Future Electronics
4+
* Copyright (c) 2018-2019 Cypress Semiconductor Corporation
5+
* SPDX-License-Identifier: Apache-2.0
46
*
57
* Licensed under the Apache License, Version 2.0 (the "License");
68
* you may not use this file except in compliance with the License.
@@ -18,6 +20,7 @@
1820
#include "device.h"
1921
#include "analogin_api.h"
2022
#include "cy_sar.h"
23+
#include "cy_sysanalog.h"
2124
#include "psoc6_utils.h"
2225
#include "mbed_assert.h"
2326
#include "mbed_error.h"
@@ -41,6 +44,7 @@ const uint32_t SAR_BASE_CLOCK_HZ = 18000000; // 18 MHz or less
4144
CY_SAR_CHAN_SAMPLE_TIME_0 \
4245
)
4346

47+
#define CY_SAR_PORT_9 (9uL)
4448

4549
/** Global SAR configuration data, modified as channels are configured.
4650
*/
@@ -106,10 +110,14 @@ static void sar_init(analogin_t *obj)
106110
}
107111
Cy_SysClk_PeriphSetDivider(CY_SYSCLK_DIV_8_BIT,
108112
sar_clock_divider,
109-
((CY_CLK_PERICLK_FREQ_HZ + SAR_BASE_CLOCK_HZ / 2) / SAR_BASE_CLOCK_HZ) - 1);
113+
((cy_PeriClkFreqHz + SAR_BASE_CLOCK_HZ / 2) / SAR_BASE_CLOCK_HZ) - 1);
110114
Cy_SysClk_PeriphEnableDivider(CY_SYSCLK_DIV_8_BIT, sar_clock_divider);
111115
Cy_SysClk_PeriphAssignDivider(obj->clock, CY_SYSCLK_DIV_8_BIT, sar_clock_divider);
112116

117+
/* Init and Enable the Analog Reference for SAR ADC operation */
118+
Cy_SysAnalog_Init(&Cy_SysAnalog_Fast_Local);
119+
Cy_SysAnalog_Enable();
120+
113121
Cy_SAR_Init(obj->base, &sar_config);
114122
Cy_SAR_Enable(obj->base);
115123
}
@@ -123,21 +131,24 @@ void analogin_init(analogin_t *obj, PinName pin)
123131
MBED_ASSERT(obj);
124132
MBED_ASSERT(pin != (PinName)NC);
125133

126-
127134
sar = pinmap_peripheral(pin, PinMap_ADC);
128135
if (sar != (uint32_t)NC) {
129-
if (cy_reserve_io_pin(pin)) {
136+
137+
if ((0 != cy_reserve_io_pin(pin)) && !sar_initialized) {
130138
error("ANALOG IN pin reservation conflict.");
131139
}
132-
obj->base = (SAR_Type*)CY_PERIPHERAL_BASE(sar);
140+
141+
/* Initialize object */
142+
obj->base = (SAR_Type *) CY_PERIPHERAL_BASE(sar);
133143
obj->pin = pin;
134144
obj->channel_mask = 1 << CY_PIN(pin);
135145

136-
// Configure clock.
146+
/* Configure SAR hardware */
137147
sar_function = pinmap_function(pin, PinMap_ADC);
138148
obj->clock = CY_PIN_CLOCK(sar_function);
139149
sar_init(obj);
140150
pin_function(pin, sar_function);
151+
141152
} else {
142153
error("ANALOG IN pinout mismatch.");
143154
}
@@ -153,16 +164,43 @@ float analogin_read(analogin_t *obj)
153164
uint16_t analogin_read_u16(analogin_t *obj)
154165
{
155166
uint32_t result = 0;
167+
uint32_t port = CY_PORT(obj->pin);
168+
GPIO_PRT_Type *portPrt = Cy_GPIO_PortToAddr(port);
156169

157170
Cy_SAR_SetChanMask(obj->base, obj->channel_mask);
158-
Cy_SAR_SetAnalogSwitch(obj->base, CY_SAR_MUX_SWITCH0, obj->channel_mask, CY_SAR_SWITCH_CLOSE);
171+
172+
/* The port 10 uses the direct connection to the pin */
173+
if (CY_SAR_PORT_9 != port) {
174+
/* Connect the SAR Vplus input to the pin directly */
175+
Cy_SAR_SetAnalogSwitch(obj->base, CY_SAR_MUX_SWITCH0, obj->channel_mask, CY_SAR_SWITCH_CLOSE);
176+
}
177+
else {
178+
/* Connect the SAR Vplus input to the AMUXA bus */
179+
Cy_SAR_SetAnalogSwitch(obj->base, CY_SAR_MUX_SWITCH0, SAR_MUX_SWITCH0_MUX_FW_AMUXBUSA_VPLUS_Msk, CY_SAR_SWITCH_CLOSE);
180+
181+
/* Connect the AMUXA bus to the pin */
182+
Cy_GPIO_SetHSIOM(portPrt, CY_PIN(obj->pin), HSIOM_SEL_AMUXA);
183+
}
184+
159185
Cy_SAR_StartConvert(obj->base, CY_SAR_START_CONVERT_SINGLE_SHOT);
160186
if (Cy_SAR_IsEndConversion(obj->base, CY_SAR_WAIT_FOR_RESULT) == CY_SAR_SUCCESS) {
161187
result = Cy_SAR_GetResult32(obj->base, CY_PIN(obj->pin));
162188
} else {
163189
error("ANALOG IN: measurement failed!");
164190
}
165-
Cy_SAR_SetAnalogSwitch(obj->base, CY_SAR_MUX_SWITCH0, obj->channel_mask, CY_SAR_SWITCH_OPEN);
191+
192+
if (CY_SAR_PORT_9 != port) {
193+
/* Disconnect the SAR Vplus input from the pin */
194+
Cy_SAR_SetAnalogSwitch(obj->base, CY_SAR_MUX_SWITCH0, obj->channel_mask, CY_SAR_SWITCH_OPEN);
195+
}
196+
else {
197+
/* Disconnect the AMUXA bus from the pin */
198+
Cy_GPIO_SetHSIOM(portPrt, CY_PIN(obj->pin), HSIOM_SEL_GPIO);
199+
200+
/* Disconnect the SAR Vplus input from the AMUXA bus */
201+
Cy_SAR_SetAnalogSwitch(obj->base, CY_SAR_MUX_SWITCH0, SAR_MUX_SWITCH0_MUX_FW_AMUXBUSA_VPLUS_Msk, CY_SAR_SWITCH_OPEN);
202+
}
203+
166204
// We are running 16x oversampling extending results to 16 bits.
167205
return (uint16_t)(result);
168206
}

targets/TARGET_Cypress/TARGET_PSOC6/analogout_api.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
22
* mbed Microcontroller Library
33
* Copyright (c) 2017-2018 Future Electronics
4+
* Copyright (c) 2018-2019 Cypress Semiconductor Corporation
5+
* SPDX-License-Identifier: Apache-2.0
46
*
57
* Licensed under the Apache License, Version 2.0 (the "License");
68
* you may not use this file except in compliance with the License.
@@ -66,11 +68,10 @@ static void ctdac_init(dac_t *obj)
6668
dac_clock_divider = cy_clk_allocate_divider(CY_SYSCLK_DIV_8_BIT);
6769
if (dac_clock_divider == CY_INVALID_DIVIDER) {
6870
error("CTDAC clock divider allocation failed.");
69-
return;
7071
}
7172
Cy_SysClk_PeriphSetDivider(CY_SYSCLK_DIV_8_BIT,
7273
dac_clock_divider,
73-
((CY_CLK_PERICLK_FREQ_HZ + CTDAC_BASE_CLOCK_HZ / 2) / CTDAC_BASE_CLOCK_HZ) - 1);
74+
((cy_PeriClkFreqHz + CTDAC_BASE_CLOCK_HZ / 2) / CTDAC_BASE_CLOCK_HZ) - 1);
7475
Cy_SysClk_PeriphEnableDivider(CY_SYSCLK_DIV_8_BIT, dac_clock_divider);
7576
Cy_SysClk_PeriphAssignDivider(obj->clock, CY_SYSCLK_DIV_8_BIT, dac_clock_divider);
7677

@@ -90,25 +91,37 @@ void analogout_init(dac_t *obj, PinName pin)
9091

9192
dac = pinmap_peripheral(pin, PinMap_DAC);
9293
if (dac != (uint32_t)NC) {
93-
if (cy_reserve_io_pin(pin)) {
94+
95+
if ((0 != cy_reserve_io_pin(pin)) && !ctdac_initialized) {
9496
error("ANALOG OUT pin reservation conflict.");
9597
}
96-
obj->base = (CTDAC_Type*)CY_PERIPHERAL_BASE(dac);
98+
99+
/* Initialize object */
100+
obj->base = (CTDAC_Type *)CY_PERIPHERAL_BASE(dac);
97101
obj->pin = pin;
98102

99-
// Configure clock.
103+
/* Configure CTDAC hardware */
100104
dac_function = pinmap_function(pin, PinMap_DAC);
101105
obj->clock = CY_PIN_CLOCK(dac_function);
102106
pin_function(pin, dac_function);
107+
108+
if (AOUT != pin) {
109+
const PinName directOutput = AOUT;
110+
111+
/* Connect AOUT to the AMUXA bus to drive output */
112+
Cy_GPIO_SetHSIOM(Cy_GPIO_PortToAddr(CY_PORT(directOutput)), CY_PIN(directOutput), HSIOM_SEL_AMUXA);
113+
}
114+
103115
ctdac_init(obj);
116+
104117
} else {
105118
error("ANALOG OUT pinout mismatch.");
106119
}
107120
}
108121

109122
void analogout_free(dac_t *obj)
110123
{
111-
// Not supported yet.
124+
/* MBED AnalogIn driver does not call this function in destructor */
112125
}
113126

114127
void analogout_write(dac_t *obj, float value)

targets/TARGET_Cypress/TARGET_PSOC6/flash_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#include "device.h"
1818
#include "flash_api.h"
19-
#include "drivers/peripheral/flash/cy_flash.h"
19+
#include "cy_flash.h"
2020

2121
#if DEVICE_FLASH
2222

targets/TARGET_Cypress/TARGET_PSOC6/gpio_api.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
22
* mbed Microcontroller Library
33
* Copyright (c) 2017-2018 Future Electronics
4+
* Copyright (c) 2018-2019 Cypress Semiconductor Corporation
5+
* SPDX-License-Identifier: Apache-2.0
46
*
57
* Licensed under the Apache License, Version 2.0 (the "License");
68
* you may not use this file except in compliance with the License.
@@ -60,19 +62,14 @@ void gpio_init(gpio_t *obj, PinName pin)
6062

6163
MBED_ASSERT(CY_PIN(obj->pin) < 8); // PSoC6 architecture supports 8 pins per port.
6264

63-
/*
64-
* Perform i/o reservation only if this is called outside of critical section/interrupt context.
65-
* This is a workaround for mbed_die() implementation, which configures LED1 inside critical section.
66-
* Normally user is advised to perform all of the i/o configuration at the program beginning,
67-
* or elsewhere in the running thread context. when we detect that we are in the wrong context here,
68-
* we assume it's explicitly called from mbed_die() or other fault handling, so eventual forcing
69-
* of the pin mode is deliberate and should not cause more problems.
70-
*/
65+
/* Ignore pin reservation result because there is not possibility to release
66+
* reserved HW resource. The MBED does not provide proper destructors for
67+
* doing that.
68+
*/
7169
if (!(IsIrqMode() || IsIrqMasked())) {
72-
if (cy_reserve_io_pin(pin)) {
73-
error("GPIO pin reservation conflict.");
74-
}
70+
(void) cy_reserve_io_pin(pin);
7571
}
72+
7673
obj->port = Cy_GPIO_PortToAddr(CY_PORT(obj->pin));
7774

7875
const uint32_t outputVal = 0;

targets/TARGET_Cypress/TARGET_PSOC6/gpio_irq_api.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
22
* mbed Microcontroller Library
33
* Copyright (c) 2017-2018 Future Electronics
4+
* Copyright (c) 2018-2019 Cypress Semiconductor Corporation
5+
* SPDX-License-Identifier: Apache-2.0
46
*
57
* Licensed under the Apache License, Version 2.0 (the "License");
68
* you may not use this file except in compliance with the License.
@@ -48,7 +50,15 @@ static void gpio_irq_dispatcher(uint32_t port_id)
4850
gpio_irq_t *obj = irq_objects[port_id][pin];
4951
MBED_ASSERT(obj);
5052
Cy_GPIO_ClearInterrupt(port, pin);
51-
event = (obj->mode == IRQ_FALL)? IRQ_FALL : IRQ_RISE;
53+
/* event = (obj->mode == IRQ_FALL)? IRQ_FALL : IRQ_RISE; */
54+
55+
/* Read pin to determine the edge to support "both" mode */
56+
event = (0UL != Cy_GPIO_Read(port, pin)) ? IRQ_RISE : IRQ_FALL;
57+
if (0UL == (obj->mode & event)) {
58+
/* In case of very short pulse, actually both edges are occurred, so indicating only the supported one */
59+
event = obj->mode;
60+
} /* Otherwise the determined edge is supported (0UL != (obj->mode & event)), so indicating it as is */
61+
5262
obj->handler(obj->id_arg, event);
5363
}
5464
}
@@ -182,10 +192,11 @@ static int gpio_irq_setup_channel(gpio_irq_t *obj)
182192
irq_config.cm0pSrc = obj->cm0p_irq_src;
183193
#endif
184194
if (Cy_SysInt_Init(&irq_config, irq_dispatcher_table[obj->port_id]) != CY_SYSINT_SUCCESS) {
185-
return(-1);
195+
return (-1);
186196
}
187197

188198
irq_port_usage[obj->port_id].pin_mask |= (1 << obj->pin);
199+
gpio_irq_enable(obj);
189200
NVIC_EnableIRQ(irqn);
190201
}
191202

@@ -204,6 +215,9 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
204215
}
205216
obj->handler = handler;
206217
obj->id_arg = id;
218+
/* Save reference to current object */
219+
irq_objects[obj->port_id][obj->pin] = obj;
220+
207221
return gpio_irq_setup_channel(obj);
208222
} else {
209223
return (-1);

targets/TARGET_Cypress/TARGET_PSOC6/gpio_object.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
22
* mbed Microcontroller Library
33
* Copyright (c) 2017-2018 Future Electronics
4+
* Copyright (c) 2018-2019 Cypress Semiconductor Corporation
5+
* SPDX-License-Identifier: Apache-2.0
46
*
57
* Licensed under the Apache License, Version 2.0 (the "License");
68
* you may not use this file except in compliance with the License.
@@ -22,7 +24,7 @@
2224
#include "PinNamesTypes.h"
2325
#include "PinNames.h"
2426

25-
#include "drivers/peripheral/gpio/cy_gpio.h"
27+
#include "cy_gpio.h"
2628

2729
#ifdef __cplusplus
2830
extern "C" {

0 commit comments

Comments
 (0)