Skip to content

Commit 823b90b

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

20 files changed

+394
-173
lines changed

cores/psoc6/Arduino.h

Lines changed: 10 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,9 @@ extern "C" {
7977
#endif
8078

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

8484
#include "pins_arduino.h"
8585

cores/psoc6/Uart.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Uart::~Uart() {
1616
end();
1717
}
1818

19+
Uart::~Uart() {
20+
end();
21+
}
22+
1923
void Uart::begin(unsigned long baud) {
2024
begin(baud, SERIAL_8N1);
2125
}
@@ -153,13 +157,21 @@ uart_error_t Uart::getLastError() {
153157
return last_error;
154158
}
155159

156-
void Uart::uart_event_handler(void* handler_arg, cyhal_uart_event_t event) {
157-
Uart* uart = static_cast<Uart*>(handler_arg);
160+
Uart::operator bool() {
161+
return serial_ready;
162+
}
163+
164+
uart_error_t Uart::getLastError() {
165+
return last_error;
166+
}
167+
168+
void Uart::uart_event_handler(void *handler_arg, cyhal_uart_event_t event) {
169+
Uart *uart = static_cast<Uart *>(handler_arg);
158170
uart->IrqHandler();
159171
}
160172

161173
void Uart::IrqHandler() {
162-
uint8_t c = 0;
174+
uint8_t c;
163175
size_t size = 1;
164176
while (cyhal_uart_readable(&uart_obj) > 0) {
165177
cyhal_uart_read(&uart_obj, &c, &size);

cores/psoc6/Uart.h

Lines changed: 1 addition & 2 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,7 +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);
35+
static void uart_event_handler(void *handler_arg, cyhal_uart_event_t event);
3736

3837
private:
3938

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)