Skip to content

Commit 98b5ec9

Browse files
cores/psoc6: ./libraries: Add all source files from main branch.
Signed-off-by: Ramya Subramanyam <[email protected]>
1 parent e19a02e commit 98b5ec9

20 files changed

+432
-242
lines changed

cores/psoc6/Arduino.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#ifndef Arduino_h
2121
#define Arduino_h
2222

23-
#include "ArduinoAPI.h"
23+
#include "api/ArduinoAPI.h"
2424
#include "cyhal_gpio.h"
2525

2626
#define RAMSTART (HMCRAMC0_ADDR)
@@ -40,32 +40,30 @@ extern "C" {
4040
extern const cyhal_gpio_t mapping_gpio_pin[];
4141
extern const uint8_t GPIO_PIN_COUNT;
4242

43-
enum {
44-
GPIO_INTERRUPT_PRIORITY = 3 // GPIO interrupt priority
45-
};
46-
43+
#define GPIO_INTERRUPT_PRIORITY 3 // GPIO interrupt priority
4744
#define digitalPinToInterrupt(p) ((p) < GPIO_PIN_COUNT ? (p) : -1)
45+
#define PWM_FREQUENCY_HZ 1000 // 1 kHz
46+
void analogWriteResolution(int res);
4847

4948
#undef LITTLE_ENDIAN
5049

5150
#define clockCyclesPerMicrosecond() (SystemCoreClock / 1000000L)
5251
#define clockCyclesToMicroseconds(a) (((a) * 1000L) / (SystemCoreClock / 1000L))
5352
#define microsecondsToClockCycles(a) ((a) * (SystemCoreClock / 1000000L))
5453

54+
typedef enum { DEFAULT } analog_reference_t;
55+
5556
#ifdef __cplusplus
5657
} // extern "C"
5758
#endif
5859

60+
// undefine stdlib's abs if encountered
5961
#ifdef abs
6062
#undef abs
6163
#endif // abs
6264

6365
#define abs(x) ((x) > 0 ? (x) : -(x))
6466

65-
#ifndef nullptr
66-
#define nullptr 0
67-
#endif
68-
6967
// Globally enable or disable interrupts
7068
#define interrupts() __enable_irq()
7169
#define noInterrupts() __disable_irq()
@@ -79,7 +77,7 @@ extern "C" {
7977
#endif
8078

8179
// ARM toolchain doesn't provide itoa etc, provide them
82-
#include "itoa.h"
80+
#include "api/itoa.h"
8381

8482
#include "pins_arduino.h"
8583

cores/psoc6/Uart.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
#include "Arduino.h"
21
#include "Uart.h"
2+
#include "Arduino.h"
33
#include "cyhal_gpio.h"
44

5-
#define uart_assert(cy_ret, ret_code) if (cy_ret != CY_RSLT_SUCCESS) { \
6-
last_error = ret_code; \
7-
return; \
8-
}
9-
10-
11-
Uart::Uart(pin_size_t tx, pin_size_t rx, pin_size_t cts, pin_size_t rts) : tx_pin(tx), rx_pin(rx), cts_pin(cts), rts_pin(rts) {
5+
#define uart_assert(cy_ret, ret_code) \
6+
if (cy_ret != CY_RSLT_SUCCESS) { \
7+
last_error = ret_code; \
8+
return; \
9+
}
1210

11+
Uart::Uart(pin_size_t tx, pin_size_t rx, pin_size_t cts, pin_size_t rts)
12+
: tx_pin(tx),
13+
rx_pin(rx),
14+
cts_pin(cts),
15+
rts_pin(rts) {
1316
}
1417

1518
Uart::~Uart() {
@@ -59,12 +62,13 @@ void Uart::begin(unsigned long baud, uint16_t config) {
5962
break;
6063
}
6164

62-
63-
/* If the user does not define them (or pass NC), they are not connected. No Arduino GPIO mapping */
65+
/* If the user does not define them (or pass NC), they are not connected. No Arduino GPIO
66+
* mapping */
6467
cyhal_gpio_t cy_cts_pin = (cts_pin == NC) ? NC : mapping_gpio_pin[cts_pin];
6568
cyhal_gpio_t cy_rts_pin = (rts_pin == NC) ? NC : mapping_gpio_pin[rts_pin];
6669

67-
cy_rslt_t ret = cyhal_uart_init(&uart_obj, mapping_gpio_pin[tx_pin], mapping_gpio_pin[rx_pin], cy_cts_pin, cy_rts_pin, NULL, &uart_config);
70+
cy_rslt_t ret = cyhal_uart_init(&uart_obj, mapping_gpio_pin[tx_pin], mapping_gpio_pin[rx_pin],
71+
cy_cts_pin, cy_rts_pin, NULL, &uart_config);
6872
uart_assert(ret, UART_ERROR_INIT_FAILED);
6973
ret = cyhal_uart_set_baud(&uart_obj, baud, &actualbaud);
7074
uart_assert(ret, UART_ERROR_SET_BAUD_FAILED);
@@ -103,14 +107,14 @@ void Uart::flush() {
103107

104108
int Uart::peek(void) {
105109
if (rx_buffer.available() == 0) {
106-
return -1; // Buffer is empty
110+
return -1; // Buffer is empty
107111
}
108112
return rx_buffer.peek();
109113
}
110114

111115
int Uart::read(void) {
112116
if (rx_buffer.available() == 0) {
113-
return -1; // Buffer is empty
117+
return -1; // Buffer is empty
114118
}
115119
return rx_buffer.read_char();
116120
}
@@ -130,7 +134,8 @@ size_t Uart::write(const uint8_t *buffer, size_t size) {
130134
*/
131135
do {
132136
uint32_t num_bytes_writable = cyhal_uart_writable(&uart_obj);
133-
size_t bytes_to_write = left_to_write > num_bytes_writable ? num_bytes_writable : left_to_write;
137+
size_t bytes_to_write =
138+
left_to_write > num_bytes_writable ? num_bytes_writable : left_to_write;
134139
/* Trying to write 0 size will throw an exception. */
135140
if (bytes_to_write > 0) {
136141
cy_rslt_t result = cyhal_uart_write(&uart_obj, (void *)buffer, &bytes_to_write);
@@ -153,13 +158,13 @@ uart_error_t Uart::getLastError() {
153158
return last_error;
154159
}
155160

156-
void Uart::uart_event_handler(void* handler_arg, cyhal_uart_event_t event) {
157-
Uart* uart = static_cast<Uart*>(handler_arg);
161+
void Uart::uart_event_handler(void *handler_arg, cyhal_uart_event_t event) {
162+
Uart *uart = static_cast<Uart *>(handler_arg);
158163
uart->IrqHandler();
159164
}
160165

161166
void Uart::IrqHandler() {
162-
uint8_t c = 0;
167+
uint8_t c;
163168
size_t size = 1;
164169
while (cyhal_uart_readable(&uart_obj) > 0) {
165170
cyhal_uart_read(&uart_obj, &c, &size);

cores/psoc6/Uart.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ typedef enum {
1212

1313
class Uart : public arduino::HardwareSerial {
1414
public:
15-
1615
Uart(pin_size_t tx, pin_size_t rx, pin_size_t cts = NC, pin_size_t rts = NC);
1716
~Uart();
1817
void begin(unsigned long baud);
@@ -33,10 +32,7 @@ class Uart : public arduino::HardwareSerial {
3332

3433
static void uart_event_handler(void *handler_arg, cyhal_uart_event_t event);
3534

36-
static void uart_event_handler(void* handler_arg, cyhal_uart_event_t event);
37-
3835
private:
39-
4036
pin_size_t tx_pin;
4137
pin_size_t rx_pin;
4238
pin_size_t cts_pin;
@@ -48,7 +44,7 @@ class Uart : public arduino::HardwareSerial {
4844
uart_error_t last_error = UART_ERROR_NONE;
4945

5046
static constexpr size_t BUFFER_LENGTH = 512;
51-
arduino::RingBufferN < BUFFER_LENGTH > rx_buffer;
47+
arduino::RingBufferN<BUFFER_LENGTH> rx_buffer;
5248

5349
void IrqHandler();
5450
};

cores/psoc6/analog_io.c

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#include "Arduino.h"
2+
#include "cyhal_adc.h"
3+
#include "cyhal_pwm.h"
4+
5+
// Constants and Macros
6+
7+
#define MIN_ACQUISITION_TIME 220
8+
#define adc_assert(cy_ret) \
9+
if (cy_ret != CY_RSLT_SUCCESS) { \
10+
return 0xff; \
11+
}
12+
#define pwm_assert(cy_ret) \
13+
if (cy_ret != CY_RSLT_SUCCESS) { \
14+
return; \
15+
}
16+
17+
#define PWM_RESOLUTION_8_BIT 255.0
18+
#define PWM_RESOLUTION_10_BIT 1023.0
19+
#define PWM_RESOLUTION_12_BIT 4095.0
20+
#define PWM_RESOLUTION_16_BIT 65535.0
21+
22+
// Type Definitions
23+
24+
typedef struct {
25+
pin_size_t pin;
26+
cyhal_adc_channel_t chan_obj;
27+
bool initialized;
28+
} adc_channel_t;
29+
30+
typedef struct {
31+
cyhal_pwm_t pwm_obj;
32+
pin_size_t pin;
33+
bool initialized;
34+
} pwm_t;
35+
36+
// Static Variables
37+
38+
static cyhal_adc_t adc_obj = {0};
39+
static cyhal_adc_vref_t desiredVRef = CYHAL_ADC_REF_VDDA;
40+
static bool adc_initialized = false;
41+
static adc_channel_t adc_channel[ADC_HOWMANY] = {0};
42+
static float desiredWriteResolution = PWM_RESOLUTION_8_BIT;
43+
static pwm_t pwm[PWM_HOWMANY] = {0};
44+
45+
static cy_rslt_t initialize_adc(pin_size_t pinNumber) {
46+
if (!adc_initialized) {
47+
cy_rslt_t status = cyhal_adc_init(&adc_obj, mapping_gpio_pin[pinNumber], NULL);
48+
adc_assert(status);
49+
adc_initialized = true;
50+
}
51+
52+
// Configure ADC settings
53+
cyhal_adc_config_t adc_config = {.continuous_scanning = false,
54+
.resolution = ADC_RESOLUTION,
55+
.average_count = 1,
56+
.vneg = CYHAL_ADC_VNEG_VSSA,
57+
.vref = desiredVRef,
58+
.ext_vref = NC,
59+
.bypass_pin = NC};
60+
61+
return cyhal_adc_configure(&adc_obj, &adc_config);
62+
}
63+
64+
static cy_rslt_t initialize_adc_channel(pin_size_t pinNumber, uint8_t adc_index) {
65+
// Ensure ADC hardware is initialized
66+
cy_rslt_t status = initialize_adc(pinNumber);
67+
adc_assert(status);
68+
69+
// Configure individual ADC channel
70+
cyhal_adc_channel_config_t chan_config = {
71+
.enabled = true, .enable_averaging = false, .min_acquisition_ns = MIN_ACQUISITION_TIME};
72+
73+
status = cyhal_adc_channel_init_diff(&adc_channel[adc_index].chan_obj, &adc_obj,
74+
mapping_gpio_pin[pinNumber], CYHAL_ADC_VNEG, &chan_config);
75+
adc_assert(status);
76+
77+
adc_channel[adc_index].pin = pinNumber;
78+
adc_channel[adc_index].initialized = true;
79+
80+
return status;
81+
}
82+
83+
int analogRead(pin_size_t pinNumber) {
84+
int adc_value = 0;
85+
uint8_t adc_index = 0;
86+
87+
// Find existing channel or initialize a new one
88+
for (adc_index = 0; adc_index < ADC_HOWMANY; adc_index++) {
89+
if (adc_channel[adc_index].initialized) {
90+
if (adc_channel[adc_index].pin == pinNumber) {
91+
// Found an existing channel for the pin
92+
break;
93+
}
94+
} else {
95+
// Found an uninitialized channel, initialize it
96+
cy_rslt_t result = initialize_adc_channel(pinNumber, adc_index);
97+
adc_assert(result);
98+
break;
99+
}
100+
}
101+
102+
if (adc_index < ADC_HOWMANY) {
103+
adc_value = cyhal_adc_read(&adc_channel[adc_index].chan_obj);
104+
}
105+
return adc_value;
106+
}
107+
108+
void analogReference(uint8_t mode) {
109+
desiredVRef = CYHAL_ADC_REF_VDDA;
110+
}
111+
112+
void analogWriteResolution(int res) {
113+
if (res > 12) {
114+
desiredWriteResolution = PWM_RESOLUTION_16_BIT;
115+
} else if (res > 10) {
116+
desiredWriteResolution = PWM_RESOLUTION_12_BIT;
117+
} else if (res > 8) {
118+
desiredWriteResolution = PWM_RESOLUTION_10_BIT;
119+
} else {
120+
desiredWriteResolution = PWM_RESOLUTION_8_BIT;
121+
}
122+
}
123+
124+
void analogWrite(pin_size_t pinNumber, int value) {
125+
uint8_t pwm_index = 0;
126+
cy_rslt_t result = CY_RSLT_TYPE_ERROR;
127+
128+
if (pinNumber > GPIO_PIN_COUNT) {
129+
return; // Invalid pin number
130+
}
131+
132+
// Find existing channel or initialize a new one
133+
for (pwm_index = 0; pwm_index < PWM_HOWMANY; pwm_index++) {
134+
if (pwm[pwm_index].initialized) {
135+
if (pwm[pwm_index].pin == pinNumber) {
136+
// Found an existing channel for the pin
137+
break;
138+
}
139+
} else {
140+
// Found an uninitialized channel, initialize it
141+
result = cyhal_pwm_init(&pwm[pwm_index].pwm_obj, mapping_gpio_pin[pinNumber], NULL);
142+
pwm_assert(result);
143+
pwm[pwm_index].pin = pinNumber;
144+
pwm[pwm_index].initialized = true;
145+
break;
146+
}
147+
}
148+
149+
if (value < 0) {
150+
value = 0;
151+
}
152+
if (value > desiredWriteResolution) {
153+
value = desiredWriteResolution;
154+
}
155+
156+
float duty_cycle_pertentage = (value / desiredWriteResolution) * 100.0f;
157+
158+
result =
159+
cyhal_pwm_set_duty_cycle(&pwm[pwm_index].pwm_obj, duty_cycle_pertentage, PWM_FREQUENCY_HZ);
160+
pwm_assert(result);
161+
162+
result = cyhal_pwm_start(&pwm[pwm_index].pwm_obj);
163+
pwm_assert(result);
164+
}

cores/psoc6/interrupts.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void attachInterrupt(pin_size_t interruptNumber, voidFuncPtr callback, PinStatus
5353
break;
5454
}
5555

56-
gpio_callback_data.callback = (void (*)(void*, cyhal_gpio_event_t))callback;
56+
gpio_callback_data.callback = (void (*)(void *, cyhal_gpio_event_t))callback;
5757
cyhal_gpio_register_callback(pin, &gpio_callback_data);
5858
cyhal_gpio_enable_event(pin, event, GPIO_INTERRUPT_PRIORITY, true);
5959
}

cores/psoc6/main.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "Arduino.h"
2222

2323
#include "cy_retarget_io.h"
24+
#include "cybsp.h"
2425

2526
#include <FreeRTOS.h>
2627
#include <task.h>
@@ -32,7 +33,7 @@
3233

3334
TaskHandle_t arduino_main_task_handle;
3435

35-
void arduino_main_task(void* arg);
36+
void arduino_main_task(void *arg);
3637

3738
// Weak empty variant initialization function.
3839
// May be redefined by variant files.
@@ -44,8 +45,8 @@ void initVariant() {
4445
/*
4546
* \brief Main entry point of Arduino application
4647
*/
47-
int main() {
48-
cy_rslt_t result = 0;
48+
int main(void) {
49+
cy_rslt_t result;
4950

5051
result = cybsp_init();
5152
if (result != CY_RSLT_SUCCESS) {
@@ -63,7 +64,7 @@ int main() {
6364
return 0;
6465
}
6566

66-
void arduino_main_task(void* arg) {
67+
void arduino_main_task(void *arg) {
6768

6869
/* Enable global interrupts */
6970
interrupts();
@@ -73,5 +74,8 @@ void arduino_main_task(void* arg) {
7374

7475
for (;;) {
7576
loop();
77+
if (arduino::serialEventRun) {
78+
arduino::serialEventRun();
79+
}
7680
}
7781
}

0 commit comments

Comments
 (0)