Skip to content

Commit cefead3

Browse files
cores/psoc6: docs: Implement read resolution API and document changes.
Signed-off-by: Ramya Subramanyam <[email protected]>
1 parent d954ab1 commit cefead3

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

cores/psoc6/Arduino.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extern bool gpio_initialized[];
4545
#define digitalPinToInterrupt(p) ((p) < GPIO_PIN_COUNT ? (p) : -1)
4646
#define PWM_FREQUENCY_HZ 1000 // 1 kHz
4747
void analogWriteResolution(int res);
48+
void analogReadResolution(int res);
4849

4950
#undef LITTLE_ENDIAN
5051

cores/psoc6/analog_io.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ static cyhal_adc_t adc_obj = {0};
3333
static cyhal_adc_vref_t desiredVRef = CYHAL_ADC_REF_VDDA;
3434
static bool adc_initialized = false;
3535
static adc_channel_t adc_channel[ADC_HOWMANY] = {0};
36+
static int desiredReadResolution = ADC_RESOLUTION;
3637
static float desiredWriteResolution = PWM_RESOLUTION_8_BIT;
3738
static pwm_t pwm[PWM_HOWMANY] = {0};
3839

@@ -79,6 +80,24 @@ static cy_rslt_t initialize_adc_channel(pin_size_t pinNumber, uint8_t adc_index)
7980
return status;
8081
}
8182

83+
void analogReadResolution(int res) {
84+
if (res < 1) {
85+
desiredReadResolution = 1; // Minimum resolution
86+
} else {
87+
desiredReadResolution = res;
88+
}
89+
}
90+
91+
static inline uint32_t map_adc_value(uint32_t adc_value) {
92+
uint8_t adc_res = ADC_RESOLUTION - 1; // 11-bit ADC resolution
93+
if (desiredReadResolution == adc_res) {
94+
return adc_value; // already in desired resolution
95+
} else if (desiredReadResolution < adc_res) {
96+
return adc_value >> (adc_res - desiredReadResolution); // reduce resolution
97+
}
98+
return adc_value << (desiredReadResolution - adc_res); // increase resolution
99+
}
100+
82101
int analogRead(pin_size_t pinNumber) {
83102
int adc_value = 0;
84103
uint8_t adc_index = 0;
@@ -101,6 +120,12 @@ int analogRead(pin_size_t pinNumber) {
101120
if (adc_index < ADC_HOWMANY) {
102121
adc_value = cyhal_adc_read(&adc_channel[adc_index].chan_obj);
103122
}
123+
124+
if (adc_value <= 0) {
125+
adc_value = 0; // Ensure non-negative value
126+
} else {
127+
adc_value = map_adc_value((uint32_t)adc_value);
128+
}
104129
return adc_value;
105130
}
106131

docs/arduino-deviations.rst

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,15 @@ Analog IO
2828

2929
Once a pin is configured and used as an ADC input, it may no longer be available for use with any other peripherals (e.g., PWM, I2C, SPI, GPIO, etc.).
3030

31-
The `PSOC6-for-Arduino` core does not support the following Arduino APIs:
32-
33-
3431
.. code-block:: cpp
3532
3633
void analogReadResolution(uint8_t bits)
3734
38-
This API is **not implemented** because the resolution is hardware-defined and fixed at 11 bits and the resolution cannot be dynamically reconfigured by firmware.
39-
40-
For applications requiring lower effective resolutions (e.g., 8-bit or 10-bit), users can manually scale the 11-bit ADC result in their application code by discarding the least significant bits (LSBs).
35+
The default ADC_RESOLUTION is fixed at 11 bits (range: 0–2047) due to hardware limitations.
36+
Depending on the bits parameter, the function modifies the resolution of the ADC value:
4137

38+
- If bits < ADC_RESOLUTION: The resolution of the ADC value is reduced by truncating the least significant bits (LSBs).
39+
- If bits > ADC_RESOLUTION: The resolution of the ADC value is increased by appending zeros to the least significant bits (LSBs).
4240

4341
.. code-block:: cpp
4442

0 commit comments

Comments
 (0)