diff --git a/keyboards/handwired/onekey/sn32f240b/config.h b/keyboards/handwired/onekey/sn32f240b/config.h index 01b6a1141524..8259bf71119d 100644 --- a/keyboards/handwired/onekey/sn32f240b/config.h +++ b/keyboards/handwired/onekey/sn32f240b/config.h @@ -16,6 +16,9 @@ #pragma once +#define ADC_PIN C0 +#define ADC_RESOLUTION 12 + #define I2C_DRIVER I2CD0 #define I2C1_SCL_PIN A6 #define I2C1_SDA_PIN A7 diff --git a/keyboards/handwired/onekey/sn32f240b/halconf.h b/keyboards/handwired/onekey/sn32f240b/halconf.h new file mode 100644 index 000000000000..5d789cffeea1 --- /dev/null +++ b/keyboards/handwired/onekey/sn32f240b/halconf.h @@ -0,0 +1,21 @@ +/* Copyright 2025 Dimitris Mantzouranis + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#define HAL_USE_ADC TRUE +#define HAL_USE_I2C TRUE + +#include_next diff --git a/keyboards/handwired/onekey/sn32f240b/mcuconf.h b/keyboards/handwired/onekey/sn32f240b/mcuconf.h index 5d79da961a36..9389cc1c4532 100644 --- a/keyboards/handwired/onekey/sn32f240b/mcuconf.h +++ b/keyboards/handwired/onekey/sn32f240b/mcuconf.h @@ -15,5 +15,7 @@ */ #pragma once +#define SN32_ADC_USE_ADC1 TRUE + #define SN32_I2C_USE_I2C0 TRUE #include_next diff --git a/keyboards/handwired/onekey/sn32f240c/config.h b/keyboards/handwired/onekey/sn32f240c/config.h index 7d1bc0065fb1..c8fc6c5c5313 100644 --- a/keyboards/handwired/onekey/sn32f240c/config.h +++ b/keyboards/handwired/onekey/sn32f240c/config.h @@ -16,6 +16,9 @@ #pragma once +#define ADC_PIN C0 +#define ADC_RESOLUTION 12 + #define I2C_DRIVER I2CD0 #define I2C1_SCL_PIN A6 #define I2C1_SDA_PIN A7 diff --git a/keyboards/handwired/onekey/sn32f240c/halconf.h b/keyboards/handwired/onekey/sn32f240c/halconf.h new file mode 100644 index 000000000000..5d789cffeea1 --- /dev/null +++ b/keyboards/handwired/onekey/sn32f240c/halconf.h @@ -0,0 +1,21 @@ +/* Copyright 2025 Dimitris Mantzouranis + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#define HAL_USE_ADC TRUE +#define HAL_USE_I2C TRUE + +#include_next diff --git a/keyboards/handwired/onekey/sn32f240c/mcuconf.h b/keyboards/handwired/onekey/sn32f240c/mcuconf.h index 5d79da961a36..9389cc1c4532 100644 --- a/keyboards/handwired/onekey/sn32f240c/mcuconf.h +++ b/keyboards/handwired/onekey/sn32f240c/mcuconf.h @@ -15,5 +15,7 @@ */ #pragma once +#define SN32_ADC_USE_ADC1 TRUE + #define SN32_I2C_USE_I2C0 TRUE #include_next diff --git a/keyboards/handwired/onekey/sn32f290/config.h b/keyboards/handwired/onekey/sn32f290/config.h index 7d1bc0065fb1..c8fc6c5c5313 100644 --- a/keyboards/handwired/onekey/sn32f290/config.h +++ b/keyboards/handwired/onekey/sn32f290/config.h @@ -16,6 +16,9 @@ #pragma once +#define ADC_PIN C0 +#define ADC_RESOLUTION 12 + #define I2C_DRIVER I2CD0 #define I2C1_SCL_PIN A6 #define I2C1_SDA_PIN A7 diff --git a/keyboards/handwired/onekey/sn32f290/halconf.h b/keyboards/handwired/onekey/sn32f290/halconf.h new file mode 100644 index 000000000000..5d789cffeea1 --- /dev/null +++ b/keyboards/handwired/onekey/sn32f290/halconf.h @@ -0,0 +1,21 @@ +/* Copyright 2025 Dimitris Mantzouranis + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#define HAL_USE_ADC TRUE +#define HAL_USE_I2C TRUE + +#include_next diff --git a/keyboards/handwired/onekey/sn32f290/mcuconf.h b/keyboards/handwired/onekey/sn32f290/mcuconf.h index 5d79da961a36..9389cc1c4532 100644 --- a/keyboards/handwired/onekey/sn32f290/mcuconf.h +++ b/keyboards/handwired/onekey/sn32f290/mcuconf.h @@ -15,5 +15,7 @@ */ #pragma once +#define SN32_ADC_USE_ADC1 TRUE + #define SN32_I2C_USE_I2C0 TRUE #include_next diff --git a/platforms/chibios/drivers/analog.c b/platforms/chibios/drivers/analog.c index fb146df9367a..d856885913e4 100644 --- a/platforms/chibios/drivers/analog.c +++ b/platforms/chibios/drivers/analog.c @@ -22,7 +22,7 @@ # error "You need to set HAL_USE_ADC to TRUE in your halconf.h to use the ADC." #endif -#if !RP_ADC_USE_ADC1 && !STM32_ADC_USE_ADC1 && !STM32_ADC_USE_ADC2 && !STM32_ADC_USE_ADC3 && !STM32_ADC_USE_ADC4 && !WB32_ADC_USE_ADC1 +#if !RP_ADC_USE_ADC1 && !STM32_ADC_USE_ADC1 && !STM32_ADC_USE_ADC2 && !STM32_ADC_USE_ADC3 && !STM32_ADC_USE_ADC4 && !WB32_ADC_USE_ADC1 && !SN32_ADC_USE_ADC1 # error "You need to set one of the 'xxx_ADC_USE_ADCx' settings to TRUE in your mcuconf.h to use the ADC." #endif @@ -82,7 +82,7 @@ /* User configurable ADC options */ #ifndef ADC_COUNT -# if defined(RP2040) || defined(STM32F0XX) || defined(STM32F1XX) || defined(STM32F4XX) || defined(GD32VF103) || defined(WB32F3G71xx) || defined(WB32FQ95xx) +# if defined(RP2040) || defined(STM32F0XX) || defined(STM32F1XX) || defined(STM32F4XX) || defined(GD32VF103) || defined(WB32F3G71xx) || defined(WB32FQ95xx) || defined(SN32F2) # define ADC_COUNT 1 # elif defined(STM32F3XX) || defined(STM32G4XX) # define ADC_COUNT 4 @@ -109,7 +109,7 @@ #endif // For more sampling rate options, look at hal_adc_lld.h in ChibiOS -#if !defined(ADC_SAMPLING_RATE) && !defined(RP2040) +#if !defined(ADC_SAMPLING_RATE) && !defined(RP2040) && !defined(SN32F2) # if defined(ADC_SMPR_SMP_1P5) # define ADC_SAMPLING_RATE ADC_SMPR_SMP_1P5 # elif defined(ADC_SMPR_SMP_2P5) // STM32L4XX, STM32L4XXP, STM32G4XX, STM32WBXX @@ -128,6 +128,12 @@ # endif #endif +#if defined(SN32F2) +# ifndef ADC_VREF +# define ADC_VREF ADC_ADM_VREF_4_5V +# endif +#endif + static ADCConfig adcCfg = {}; static adcsample_t sampleBuffer[ADC_TOTAL_CHANNELS * ADC_BUFFER_DEPTH]; @@ -149,6 +155,15 @@ static ADCConversionGroup adcConversionGroup = { .smpr1 = ADC_SMPR1_SMP_AN10(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN11(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN12(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN13(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN14(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN15(ADC_SAMPLING_RATE), #elif defined(RP2040) // RP2040 does not have any extra config here +#elif defined(SN32F2) + .vhs = ADC_VREF, +# if (ADC_RESOLUTION == 12) + .adlen = 1, +# elif (ADC_RESOLUTION == 8) + .adlen = 0, +# else +# error "Invalid ADC_RESOLUTION for this MCU." +# endif #else .cfgr = ADC_CFGR_CONT | ADC_RESOLUTION, .smpr = {ADC_SMPR1_SMP_AN0(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN1(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN2(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN3(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN4(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN5(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN6(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN7(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN8(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN9(ADC_SAMPLING_RATE), ADC_SMPR2_SMP_AN10(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN11(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN12(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN13(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN14(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN15(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN16(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN17(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN18(ADC_SAMPLING_RATE)}, @@ -334,6 +349,23 @@ __attribute__((weak)) adc_mux pinToMux(pin_t pin) { case 27U: return TO_MUX(1, 0); case 28U: return TO_MUX(2, 0); case 29U: return TO_MUX(3, 0); +#elif defined(SN32F2) + case C0: return TO_MUX( 0, 0 ); + case C1: return TO_MUX( 1, 0 ); + case C2: return TO_MUX( 2, 0 ); + case C3: return TO_MUX( 3, 0 ); + case C4: return TO_MUX( 4, 0 ); + case C5: return TO_MUX( 5, 0 ); + case C6: return TO_MUX( 6, 0 ); + case C7: return TO_MUX( 7, 0 ); + case C8: return TO_MUX( 8, 0 ); + case C9: return TO_MUX( 9, 0 ); + case C10: return TO_MUX( 10, 0 ); + case C11: return TO_MUX( 11, 0 ); + case C12: return TO_MUX( 12, 0 ); + case C13: return TO_MUX( 13, 0 ); + case C14: return TO_MUX( 14, 0 ); + case C15: return TO_MUX( 15, 0 ); #endif } @@ -344,7 +376,7 @@ __attribute__((weak)) adc_mux pinToMux(pin_t pin) { static inline ADCDriver* intToADCDriver(uint8_t adcInt) { switch (adcInt) { -#if RP_ADC_USE_ADC1 || STM32_ADC_USE_ADC1 || WB32_ADC_USE_ADC1 +#if RP_ADC_USE_ADC1 || STM32_ADC_USE_ADC1 || WB32_ADC_USE_ADC1 || SN32_ADC_USE_ADC1 case 0: return &ADCD1; #endif @@ -394,6 +426,8 @@ int16_t adc_read(adc_mux mux) { adcConversionGroup.sqr3 = ADC_SQR3_SQ1_N(mux.input); #elif defined(RP2040) adcConversionGroup.channel_mask = 1 << mux.input; +#elif defined(SN32F2) + adcConversionGroup.chs = 1 << mux.input; #else adcConversionGroup.sqr[0] = ADC_SQR1_SQ1_N(mux.input) # if ADC_DUMMY_CONVERSIONS_AT_START >= 1