Skip to content

Commit 589e3cb

Browse files
committed
PSOC6: reuse FUTURE_SEQUANA porting layer
Copy the porting layer from TARGET_PSOC6_FUTURE to TARGET_PSOC6. This commit is intended to make the history and changes applied easier to follow. ipcpipe_transport.c, ipcpipe_transport.h, rpc_api.h, rpc_defs.h are excluded (not used by Cypress port). PeripheralNames.h is moved to BSP layer introduced in subsequent commits (the peripheral names and count are board-specific).
1 parent 50f8255 commit 589e3cb

23 files changed

+5067
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* mbed Microcontroller Library
3+
* Copyright (c) 2017-2018 Future Electronics
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef MBED_PERIPHERALPINS_H
19+
#define MBED_PERIPHERALPINS_H
20+
21+
#include "pinmap.h"
22+
#include "PeripheralNames.h"
23+
24+
25+
// //*** I2C ***
26+
#if DEVICE_I2C
27+
extern const PinMap PinMap_I2C_SDA[];
28+
extern const PinMap PinMap_I2C_SCL[];
29+
#endif
30+
31+
//*** PWM ***
32+
#if DEVICE_PWMOUT
33+
extern const PinMap PinMap_PWM_OUT[];
34+
#endif
35+
36+
//*** SERIAL ***
37+
#if DEVICE_SERIAL
38+
extern const PinMap PinMap_UART_TX[];
39+
extern const PinMap PinMap_UART_RX[];
40+
extern const PinMap PinMap_UART_RTS[];
41+
extern const PinMap PinMap_UART_CTS[];
42+
#endif
43+
44+
//*** SPI ***
45+
#if DEVICE_SPI
46+
extern const PinMap PinMap_SPI_MOSI[];
47+
extern const PinMap PinMap_SPI_MISO[];
48+
extern const PinMap PinMap_SPI_SCLK[];
49+
extern const PinMap PinMap_SPI_SSEL[];
50+
#endif
51+
52+
//*** ADC ***
53+
#if DEVICE_ANALOGIN
54+
extern const PinMap PinMap_ADC[];
55+
#endif
56+
57+
//*** DAC ***
58+
#if DEVICE_ANALOGOUT
59+
extern const PinMap PinMap_DAC[];
60+
#endif
61+
62+
#endif
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* mbed Microcontroller Library
3+
* Copyright (c) 2017-2018 Future Electronics
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef MBED_PINNAMESTYPES_H
19+
#define MBED_PINNAMESTYPES_H
20+
21+
#include "cmsis.h"
22+
23+
typedef enum {
24+
PIN_INPUT = 0,
25+
PIN_OUTPUT
26+
} PinDirection;
27+
28+
typedef enum {
29+
PullNone = 0,
30+
PullUp = 1,
31+
PullDown = 2,
32+
OpenDrainDriveLow = 3,
33+
OpenDrainDriveHigh = 4,
34+
OpenDrain = OpenDrainDriveLow,
35+
PushPull = 5,
36+
AnalogMode = 6,
37+
PullDefault = PullNone
38+
} PinMode;
39+
40+
typedef struct {
41+
en_hsiom_sel_t hsiom : 8;
42+
en_clk_dst_t clock : 8;
43+
PinMode mode : 4;
44+
PinDirection dir : 1;
45+
} PinFunction;
46+
47+
// Encode pin function.
48+
// Output function
49+
#define CY_PIN_FUNCTION(hsiom, clock, mode, dir) (int)(((dir) << 20) | ((mode) << 16) | ((clock) << 8) | (hsiom))
50+
#define CY_PIN_OUT_FUNCTION(hsiom, clock) CY_PIN_FUNCTION(hsiom, clock, PushPull, PIN_OUTPUT)
51+
#define CY_PIN_OD_FUNCTION(hsiom, clock) CY_PIN_FUNCTION(hsiom, clock, OpenDrain, PIN_OUTPUT)
52+
#define CY_PIN_IN_FUNCTION(hsiom, clock) CY_PIN_FUNCTION(hsiom, clock, PullDefault, PIN_INPUT)
53+
#define CY_PIN_PULLUP_FUNCTION(hsiom, clock) CY_PIN_FUNCTION(hsiom, clock, PullUp, PIN_INPUT)
54+
#define CY_PIN_ANALOG_FUNCTION(clock) CY_PIN_FUNCTION(HSIOM_SEL_GPIO, clock, AnalogMode, 0)
55+
56+
// Create unique name to force 32-bit PWM usage on a pin.
57+
#define CY_PIN_FORCE_PWM_32(pin) ((uint32_t)(pin) + 0x8000)
58+
59+
static inline en_hsiom_sel_t CY_PIN_HSIOM(int function)
60+
{
61+
return (en_hsiom_sel_t)(function & 0xFF);
62+
}
63+
64+
static inline en_clk_dst_t CY_PIN_CLOCK(int function)
65+
{
66+
return (en_clk_dst_t)((function >> 8) & 0xFF);
67+
}
68+
69+
static inline PinMode CY_PIN_MODE(int function)
70+
{
71+
return (PinMode)((function >> 16) & 0x0F);
72+
}
73+
74+
static inline PinDirection CY_PIN_DIRECTION(int function)
75+
{
76+
return (PinDirection)((function >> 20) & 1);
77+
}
78+
79+
static inline int CY_PERIPHERAL_BASE(int peripheral)
80+
{
81+
return peripheral & 0xffff0000;
82+
}
83+
84+
#endif
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* mbed Microcontroller Library
3+
* Copyright (c) 2017-2018 Future Electronics
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef MBED_PORTNAMES_H
19+
#define MBED_PORTNAMES_H
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
// Port[15-0]
26+
typedef enum {
27+
Port0 = 0x0,
28+
Port1 = 0x1,
29+
Port2 = 0x2,
30+
Port3 = 0x3,
31+
Port4 = 0x4,
32+
Port5 = 0x5,
33+
Port6 = 0x6,
34+
Port7 = 0x7,
35+
Port8 = 0x8,
36+
Port9 = 0x9,
37+
Port10 = 0xA,
38+
Port11 = 0xB,
39+
Port12 = 0xC,
40+
Port13 = 0xD,
41+
Port14 = 0xE
42+
} PortName;
43+
44+
#ifdef __cplusplus
45+
}
46+
#endif
47+
#endif
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* mbed Microcontroller Library
3+
* Copyright (c) 2017-2018 Future Electronics
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "device.h"
19+
#include "analogin_api.h"
20+
#include "cy_sar.h"
21+
#include "psoc6_utils.h"
22+
#include "mbed_assert.h"
23+
#include "mbed_error.h"
24+
#include "pinmap.h"
25+
#include "PeripheralPins.h"
26+
#include "platform/mbed_error.h"
27+
28+
#if DEVICE_ANALOGIN
29+
30+
const uint16_t ADC_MAX_VALUE = 0x0fff;
31+
32+
const uint32_t SAR_BASE_CLOCK_HZ = 18000000; // 18 MHz or less
33+
34+
/** Default SAR channel configuration.
35+
* Notice, that because dynamic SAR MUX switching is disabled,
36+
* per-channel MUX configuration is ignored, thus not configured here.
37+
*/
38+
#define DEFAULT_CHANNEL_CONFIG ( \
39+
CY_SAR_CHAN_SINGLE_ENDED | \
40+
CY_SAR_CHAN_AVG_ENABLE | \
41+
CY_SAR_CHAN_SAMPLE_TIME_0 \
42+
)
43+
44+
45+
/** Global SAR configuration data, modified as channels are configured.
46+
*/
47+
static cy_stc_sar_config_t sar_config = {
48+
.ctrl = CY_SAR_VREF_SEL_VDDA_DIV_2 |
49+
CY_SAR_NEG_SEL_VREF |
50+
CY_SAR_CTRL_COMP_DLY_12 |
51+
CY_SAR_COMP_PWR_50 |
52+
CY_SAR_SARSEQ_SWITCH_DISABLE, /**< Control register */
53+
.sampleCtrl = CY_SAR_RIGHT_ALIGN |
54+
CY_SAR_SINGLE_ENDED_UNSIGNED |
55+
CY_SAR_AVG_CNT_16 |
56+
CY_SAR_AVG_MODE_SEQUENTIAL_FIXED |
57+
CY_SAR_TRIGGER_MODE_FW_ONLY, /**< Sample control register */
58+
.sampleTime01 = (4uL << CY_SAR_SAMPLE_TIME0_SHIFT) |
59+
(4uL << CY_SAR_SAMPLE_TIME1_SHIFT), /**< Sample time in ADC clocks for ST0 and ST1 */
60+
.sampleTime23 = (4uL << CY_SAR_SAMPLE_TIME2_SHIFT) |
61+
(4uL << CY_SAR_SAMPLE_TIME3_SHIFT), /**< Sample time in ADC clocks for ST2 and ST3 */
62+
.rangeThres = 0, /**< Range detect threshold register for all channels (unused)*/
63+
.rangeCond = 0, /**< Range detect mode for all channels (unused)*/
64+
.chanEn = 0, /**< Enable bits for the channels */
65+
.chanConfig = { /**< Channel configuration registers */
66+
DEFAULT_CHANNEL_CONFIG, // chn 0
67+
DEFAULT_CHANNEL_CONFIG, // chn 1
68+
DEFAULT_CHANNEL_CONFIG, // chn 2
69+
DEFAULT_CHANNEL_CONFIG, // chn 3
70+
DEFAULT_CHANNEL_CONFIG, // chn 4
71+
DEFAULT_CHANNEL_CONFIG, // chn 5
72+
DEFAULT_CHANNEL_CONFIG, // chn 6
73+
DEFAULT_CHANNEL_CONFIG, // chn 7
74+
DEFAULT_CHANNEL_CONFIG, // chn 8
75+
DEFAULT_CHANNEL_CONFIG, // chn 9
76+
DEFAULT_CHANNEL_CONFIG, // chn 10
77+
DEFAULT_CHANNEL_CONFIG, // chn 11
78+
DEFAULT_CHANNEL_CONFIG, // chn 12
79+
DEFAULT_CHANNEL_CONFIG, // chn 13
80+
DEFAULT_CHANNEL_CONFIG, // chn 14
81+
DEFAULT_CHANNEL_CONFIG, // chn 15
82+
},
83+
.intrMask = 0, /**< Interrupt enable mask */
84+
.satIntrMask = 0, /**< Saturate interrupt mask register */
85+
.rangeIntrMask = 0, /**< Range interrupt mask register */
86+
.muxSwitch = 0, /**< SARMUX firmware switches to connect analog signals to SAR */
87+
.muxSwitchSqCtrl = 0, /**< SARMUX Switch SAR sequencer control */
88+
.configRouting = false, /**< Configure or ignore routing related registers (muxSwitch, muxSwitchSqCtrl) */
89+
.vrefMvValue = 0, /**< Reference voltage in millivolts used in counts to volts conversion */
90+
};
91+
92+
static bool sar_initialized = false;
93+
94+
95+
static void sar_init(analogin_t *obj)
96+
{
97+
if (!sar_initialized) {
98+
uint32_t sar_clock_divider = CY_INVALID_DIVIDER;
99+
100+
sar_initialized = true;
101+
// Allocate and setup clock.
102+
sar_clock_divider = cy_clk_allocate_divider(CY_SYSCLK_DIV_8_BIT);
103+
if (sar_clock_divider == CY_INVALID_DIVIDER) {
104+
error("SAR clock divider allocation failed.");
105+
return;
106+
}
107+
Cy_SysClk_PeriphSetDivider(CY_SYSCLK_DIV_8_BIT,
108+
sar_clock_divider,
109+
((CY_CLK_PERICLK_FREQ_HZ + SAR_BASE_CLOCK_HZ / 2) / SAR_BASE_CLOCK_HZ) - 1);
110+
Cy_SysClk_PeriphEnableDivider(CY_SYSCLK_DIV_8_BIT, sar_clock_divider);
111+
Cy_SysClk_PeriphAssignDivider(obj->clock, CY_SYSCLK_DIV_8_BIT, sar_clock_divider);
112+
113+
Cy_SAR_Init(obj->base, &sar_config);
114+
Cy_SAR_Enable(obj->base);
115+
}
116+
}
117+
118+
void analogin_init(analogin_t *obj, PinName pin)
119+
{
120+
uint32_t sar = 0;
121+
uint32_t sar_function = 0;
122+
123+
MBED_ASSERT(obj);
124+
MBED_ASSERT(pin != (PinName)NC);
125+
126+
127+
sar = pinmap_peripheral(pin, PinMap_ADC);
128+
if (sar != (uint32_t)NC) {
129+
if (cy_reserve_io_pin(pin)) {
130+
error("ANALOG IN pin reservation conflict.");
131+
}
132+
obj->base = (SAR_Type*)CY_PERIPHERAL_BASE(sar);
133+
obj->pin = pin;
134+
obj->channel_mask = 1 << CY_PIN(pin);
135+
136+
// Configure clock.
137+
sar_function = pinmap_function(pin, PinMap_ADC);
138+
obj->clock = CY_PIN_CLOCK(sar_function);
139+
sar_init(obj);
140+
pin_function(pin, sar_function);
141+
} else {
142+
error("ANALOG IN pinout mismatch.");
143+
}
144+
}
145+
146+
float analogin_read(analogin_t *obj)
147+
{
148+
uint16_t result = analogin_read_u16(obj);
149+
150+
return (float)result * (1.0 / ADC_MAX_VALUE);
151+
}
152+
153+
uint16_t analogin_read_u16(analogin_t *obj)
154+
{
155+
uint32_t result = 0;
156+
157+
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);
159+
Cy_SAR_StartConvert(obj->base, CY_SAR_START_CONVERT_SINGLE_SHOT);
160+
if (Cy_SAR_IsEndConversion(obj->base, CY_SAR_WAIT_FOR_RESULT) == CY_SAR_SUCCESS) {
161+
result = Cy_SAR_GetResult32(obj->base, CY_PIN(obj->pin));
162+
} else {
163+
error("ANALOG IN: measurement failed!");
164+
}
165+
Cy_SAR_SetAnalogSwitch(obj->base, CY_SAR_MUX_SWITCH0, obj->channel_mask, CY_SAR_SWITCH_OPEN);
166+
// We are running 16x oversampling extending results to 16 bits.
167+
return (uint16_t)(result);
168+
}
169+
170+
#endif // DEVICE_ANALOGIN
171+

0 commit comments

Comments
 (0)