From 89a43156ff056efb1f8f5204c4ef946d634a1199 Mon Sep 17 00:00:00 2001 From: cnlohr <lohr85@gmail.com> Date: Sun, 28 May 2023 05:26:17 -0400 Subject: [PATCH] Fix wiring and move it to extralibs. --- examples/GPIO/Makefile | 2 +- examples/GPIO_analogRead/Makefile | 2 +- examples/GPIO_analogRead/wiring.c | 238 -------------------------- examples/GPIO_analogRead/wiring.h | 99 ----------- {examples/GPIO => extralibs}/wiring.c | 2 +- {examples/GPIO => extralibs}/wiring.h | 0 6 files changed, 3 insertions(+), 340 deletions(-) delete mode 100644 examples/GPIO_analogRead/wiring.c delete mode 100644 examples/GPIO_analogRead/wiring.h rename {examples/GPIO => extralibs}/wiring.c (98%) rename {examples/GPIO => extralibs}/wiring.h (100%) diff --git a/examples/GPIO/Makefile b/examples/GPIO/Makefile index 399d16e..8734737 100644 --- a/examples/GPIO/Makefile +++ b/examples/GPIO/Makefile @@ -3,7 +3,7 @@ all : flash TARGET:=GPIO CFLAGS+=-DTINYVECTOR -ADDITIONAL_C_FILES+=wiring.c +ADDITIONAL_C_FILES+=../../extralibs/wiring.c include ../../ch32v003fun/ch32v003fun.mk diff --git a/examples/GPIO_analogRead/Makefile b/examples/GPIO_analogRead/Makefile index e47191e..a965d90 100644 --- a/examples/GPIO_analogRead/Makefile +++ b/examples/GPIO_analogRead/Makefile @@ -3,7 +3,7 @@ all : flash TARGET:=GPIO_analogRead CFLAGS+=-DTINYVECTOR -ADDITIONAL_C_FILES+=wiring.c +ADDITIONAL_C_FILES+=../../extralibs/wiring.c include ../../ch32v003fun/ch32v003fun.mk diff --git a/examples/GPIO_analogRead/wiring.c b/examples/GPIO_analogRead/wiring.c deleted file mode 100644 index e0acde1..0000000 --- a/examples/GPIO_analogRead/wiring.c +++ /dev/null @@ -1,238 +0,0 @@ -//#include <stdio.h> - -#include "wiring.h" -#include <stdint.h> - - - -enum GPIOports getPort (enum GPIOpins pin) { - if (pin <= pin_A2) { - return port_A; - } - else if (pin <= pin_C7) { - return port_C; - } - else if (pin <= pin_D7) { - return port_D; - } - return port_none; -} - - - -void portEnable(enum GPIOports port) { - // Enable GPIOs - switch (port) { - case port_A: - RCC->APB2PCENR |= RCC_APB2Periph_GPIOA; - break; - case port_C: - RCC->APB2PCENR |= RCC_APB2Periph_GPIOC; - break; - case port_D: - RCC->APB2PCENR |= RCC_APB2Periph_GPIOD; - break; - case port_none: - break; - } -} - - - -void pinMode(enum GPIOpins pin, enum GPIOpinMode mode) { - GPIO_TypeDef * GPIOx; - uint16_t PinOffset; - - GPIOx = GPIOA+(pin>>3); - PinOffset = (pin & 0x7)<<2; - - GPIOx->CFGLR &= ~(0b1111<<PinOffset); // zero the 4 configuration bits - - uint8_t target_pin_state = pinState_nochange; // by default, pin shall retain its state - - uint8_t modeMask = 0; // configuration mask - - switch (mode) { - case pinMode_I_floating: - modeMask = GPIO_CNF_IN_FLOATING; - break; - case pinMode_I_pullUp: - modeMask = GPIO_CNF_IN_PUPD; - target_pin_state = pinState_high; - break; - case pinMode_I_pullDown: - modeMask = GPIO_CNF_IN_PUPD; - target_pin_state = pinState_low; - break; - case pinMode_I_analog: - modeMask = GPIO_CNF_IN_ANALOG; - break; - case pinMode_O_pushPull: - modeMask = GPIO_Speed_10MHz | GPIO_CNF_OUT_PP; - break; - case pinMode_O_openDrain: - modeMask = GPIO_Speed_10MHz | GPIO_CNF_OUT_OD; - break; - case pinMode_O_pushPullMux: - modeMask = GPIO_Speed_10MHz | GPIO_CNF_OUT_PP_AF; - break; - case pinMode_O_openDrainMux: - modeMask = GPIO_Speed_10MHz | GPIO_CNF_OUT_OD_AF; - break; - } - - // wirte mask to CFGR - GPIOx->CFGLR |= modeMask<<PinOffset; - - // set pin state - if (target_pin_state > pinState_nochange) { - digitalWrite(pin, target_pin_state - 1); - } -} - - - -void digitalWrite(enum GPIOpins pin, uint8_t value) { - // no checks given whether pin is currently being toggled by timer! your output trannys are in your hands! beware the magic smoke! - GPIO_TypeDef * GPIOx; - uint16_t PinOffset = 0; - - if (pin <= pin_A2) { - GPIOx = GPIOA; - PinOffset = pin; - } - else if (pin <= pin_C7) { - GPIOx = GPIOC; - PinOffset = (pin - 16); - } - else if (pin <= pin_D7) { - GPIOx = GPIOD; - PinOffset = (pin - 24); - } - else { - return; - } - - if (value) { - GPIOx-> BSHR |= 1 << PinOffset; - } - else { - GPIOx-> BSHR |= 1 << (16 + PinOffset); - } -} - - - -uint8_t digitalRead(uint8_t pin) { - GPIO_TypeDef * GPIOx; - uint16_t PinOffset = 0; - - if (pin <= pin_A2) { - GPIOx = GPIOA; - PinOffset = pin; - } - else if (pin <= pin_C7) { - GPIOx = GPIOC; - PinOffset = (pin - 16); - } - else if (pin <= pin_D7) { - GPIOx = GPIOD; - PinOffset = (pin - 24); - } - else { - return 0; - } - - int8_t result = (GPIOx->INDR >> PinOffset) & 1; - return result; -} - - - - - -void ADCinit() { - // select ADC clock source - // ADCCLK = 24 MHz => RCC_ADCPRE = 0: divide by 2 - RCC->CFGR0 &= ~(0x1F<<11); - - // enable clock to the ADC - RCC->APB2PCENR |= RCC_APB2Periph_ADC1; - - // Reset the ADC to init all regs - RCC->APB2PRSTR |= RCC_APB2Periph_ADC1; - RCC->APB2PRSTR &= ~RCC_APB2Periph_ADC1; - - // set sampling time for all inputs to 241 cycles - for (uint8_t i = Ain0_A2; i <= AinVcal; i++) { - ADCsetSampletime(i, Ast_241cy_default); - } - - // set trigger to software - ADC1->CTLR2 |= ADC_EXTSEL; - - // pre-clear conversion queue - ADC1->RSQR1 = 0; - ADC1->RSQR2 = 0; - ADC1->RSQR3 = 0; - - // power the ADC - ADCsetPower(1); -} - - - -void ADCsetSampletime(enum ANALOGinputs input, enum ANALOGsampletimes time) { - // clear - ADC1->SAMPTR2 &= ~(0b111)<<(3*input); - // set - ADC1->SAMPTR2 |= time<<(3*input); // 0:7 => 3/9/15/30/43/57/73/241 cycles -} - - - -void ADCsetPower(uint8_t enable) { - if (enable) { - ADC1->CTLR2 |= ADC_ADON; - if (enable == 1) { - // auto-cal each time after turning on the ADC - // can be overridden by calling with enable > 1. - ADCcalibrate(); - } - } - else { - ADC1->CTLR2 &= ~(ADC_ADON); - } -} - - - -void ADCcalibrate() { - // reset calibration - ADC1->CTLR2 |= ADC_RSTCAL; - while(ADC1->CTLR2 & ADC_RSTCAL); - - // calibrate - ADC1->CTLR2 |= ADC_CAL; - while(ADC1->CTLR2 & ADC_CAL); -} - - - -// inspired by arduinos analogRead() -uint16_t analogRead(enum ANALOGinputs input) { - // set mux to selected input - ADC1->RSQR3 = input; - - // may need a delay right here for the mux to actually finish switching?? - // Arduino inserts a full ms delay right here! - - // start sw conversion (auto clears) - ADC1->CTLR2 |= ADC_SWSTART; - - // wait for conversion complete - while(!(ADC1->STATR & ADC_EOC)); - - // get result - return ADC1->RDATAR; -} diff --git a/examples/GPIO_analogRead/wiring.h b/examples/GPIO_analogRead/wiring.h deleted file mode 100644 index 3b7d0d7..0000000 --- a/examples/GPIO_analogRead/wiring.h +++ /dev/null @@ -1,99 +0,0 @@ -#ifndef WIRING_H -#define WIRING_H - -#include "ch32v003fun.h" - - - -enum lowhigh { - low, - high, -}; - - - -enum GPIOports{ - port_A, - port_C, - port_D, - port_none, -}; - -enum GPIOpins{ - pin_A1 = 1, - pin_A2, - pin_C0 = 16, - pin_C1, - pin_C2, - pin_C3, - pin_C4, - pin_C5, - pin_C6, - pin_C7, - pin_D0 = 24, - pin_D1, - pin_D2, - pin_D3, - pin_D4, - pin_D5, - pin_D6, - pin_D7, - pin_none, -}; - -enum GPIOpinMode { - pinMode_I_floating, - pinMode_I_pullUp, //pull-mode + ODR(1) - pinMode_I_pullDown, //pull-mode + ODR(0) - pinMode_I_analog, - pinMode_O_pushPull, - pinMode_O_openDrain, - pinMode_O_pushPullMux, - pinMode_O_openDrainMux, -}; - -enum GPIOpinState { - pinState_nochange, - pinState_low, - pinState_high, -}; - -enum ANALOGinputs { - Ain0_A2, - Ain1_A1, - Ain2_C4, - Ain3_D2, - Ain4_D3, - Ain5_D5, - Ain6_D6, - Ain7_D4, - AinVref, - AinVcal, -}; - -enum ANALOGsampletimes { - Ast_3cy, - Ast_9cy, - Ast_15cy, - Ast_30cy, - Ast_43cy, - Ast_57cy, - Ast_73cy, - Ast_241cy_default, -}; - - -enum GPIOports getPort (enum GPIOpins pin); - -void portEnable(enum GPIOports port); -void pinMode(enum GPIOpins pin, enum GPIOpinMode mode); -void digitalWrite(enum GPIOpins pin, uint8_t value); -uint8_t digitalRead(uint8_t pin); - -void ADCinit(); -void ADCsetPower(uint8_t enable); -void ADCsetSampletime(enum ANALOGinputs input, enum ANALOGsampletimes time); -void ADCcalibrate(); -uint16_t analogRead(enum ANALOGinputs input); - -#endif // WIRING_H diff --git a/examples/GPIO/wiring.c b/extralibs/wiring.c similarity index 98% rename from examples/GPIO/wiring.c rename to extralibs/wiring.c index e0acde1..b04f8e4 100644 --- a/examples/GPIO/wiring.c +++ b/extralibs/wiring.c @@ -43,7 +43,7 @@ void pinMode(enum GPIOpins pin, enum GPIOpinMode mode) { GPIO_TypeDef * GPIOx; uint16_t PinOffset; - GPIOx = GPIOA+(pin>>3); + GPIOx = (GPIO_TypeDef *)(((uint8_t*)(GPIOA))+(pin>>3)*0x0400); PinOffset = (pin & 0x7)<<2; GPIOx->CFGLR &= ~(0b1111<<PinOffset); // zero the 4 configuration bits diff --git a/examples/GPIO/wiring.h b/extralibs/wiring.h similarity index 100% rename from examples/GPIO/wiring.h rename to extralibs/wiring.h -- GitLab