fixed PWM

This commit is contained in:
Edward Emelianov 2023-05-03 22:45:37 +03:00
parent 02ce115968
commit e0352dde3f
8 changed files with 29 additions and 16 deletions

View File

@ -114,7 +114,7 @@ uint16_t getADCval(int nch){
int addr = nch, adval = NUMBER_OF_ADC1_CHANNELS; int addr = nch, adval = NUMBER_OF_ADC1_CHANNELS;
if(nch >= NUMBER_OF_ADC1_CHANNELS){ if(nch >= NUMBER_OF_ADC1_CHANNELS){
adval = NUMBER_OF_ADC2_CHANNELS; adval = NUMBER_OF_ADC2_CHANNELS;
addr += ADC2START; addr += ADC2START - NUMBER_OF_ADC1_CHANNELS;
} }
for(int i = 0; i < 9; ++i, addr += adval) // first we should prepare array for optmed for(int i = 0; i < 9; ++i, addr += adval) // first we should prepare array for optmed
p[i] = ADC_array[addr]; p[i] = ADC_array[addr];

View File

@ -73,8 +73,8 @@ TRUE_INLINE void gpio_setup(){
// PORT E // PORT E
//GPIOE->ODR = 0; //GPIOE->ODR = 0;
GPIOE->AFR[0] = 0; GPIOE->AFR[0] = AFRf(2, 2) | AFRf(2, 3) | AFRf(2, 4) | AFRf(2, 5);
GPIOE->AFR[1] = AFRf(2, 2) | AFRf(2, 3) | AFRf(2, 4) | AFRf(2, 5); GPIOE->AFR[1] = 0;
GPIOE->MODER = MODER_AF(2) | MODER_AF(3) | MODER_AF(4) | MODER_AF(5) | MODER_O(8) | MODER_O(9) | MODER_O(10) | MODER_O(11); GPIOE->MODER = MODER_AF(2) | MODER_AF(3) | MODER_AF(4) | MODER_AF(5) | MODER_O(8) | MODER_O(9) | MODER_O(10) | MODER_O(11);
GPIOE->OSPEEDR = 0; GPIOE->OSPEEDR = 0;
GPIOE->OTYPER = 0; GPIOE->OTYPER = 0;
@ -92,12 +92,15 @@ TRUE_INLINE void gpio_setup(){
TRUE_INLINE void pwm_setup(){ TRUE_INLINE void pwm_setup(){
TIM3->CR1 = TIM_CR1_ARPE; TIM3->CR1 = TIM_CR1_ARPE;
TIM3->PSC = 1999; // 48M/2000 = 24kHz TIM3->PSC = 7199; // 72M/7200 = 10kHz; PWMfreq=10k/100=100Hz
// PWM mode 1 (active -> inactive) // PWM mode 1 (active -> inactive)
TIM3->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2M_1; TIM3->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2M_1;
TIM3->CCMR2 = TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1 | TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4M_1; TIM3->CCMR2 = TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1 | TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4M_1;
TIM3->CCR1 = 0; TIM3->CCR1 = 0;
TIM3->ARR = 255; // 8bit PWM TIM3->CCR2 = 0;
TIM3->CCR3 = 0;
TIM3->CCR4 = 0;
TIM3->ARR = PWM_CCR_MAX-1; // 8bit PWM
TIM3->BDTR |= TIM_BDTR_MOE; // enable main output TIM3->BDTR |= TIM_BDTR_MOE; // enable main output
TIM3->CCER = TIM_CCER_CC1E | TIM_CCER_CC2E | TIM_CCER_CC3E | TIM_CCER_CC4E; TIM3->CCER = TIM_CCER_CC1E | TIM_CCER_CC2E | TIM_CCER_CC3E | TIM_CCER_CC4E;
TIM3->CR1 |= TIM_CR1_CEN; TIM3->CR1 |= TIM_CR1_CEN;
@ -138,7 +141,7 @@ void hw_setup(){
#endif #endif
} }
void setPWM(int nch, uint8_t val){ void setPWM(int nch, uint16_t val){
switch(nch){ switch(nch){
case 0: case 0:
TIM3->CCR1 = val; TIM3->CCR1 = val;

View File

@ -26,6 +26,9 @@
#define PCLK (72000000) #define PCLK (72000000)
#endif #endif
// Max PWM CCR1 value (->1)
#define PWM_CCR_MAX (100)
// USB pullup: PC9 // USB pullup: PC9
#define USBPU_port GPIOC #define USBPU_port GPIOC
#define USBPU_pin (1<<9) #define USBPU_pin (1<<9)
@ -77,5 +80,5 @@ extern int LEDsON;
uint8_t MSB(uint16_t val); uint8_t MSB(uint16_t val);
void hw_setup(); void hw_setup();
void setPWM(int nch, uint8_t val); void setPWM(int nch, uint16_t val);
uint8_t getPWM(int nch); uint8_t getPWM(int nch);

View File

@ -1,4 +1,4 @@
set FLASH_SIZE 0x20000 set FLASH_SIZE 0x20000
source [find interface/stlink-v2-1.cfg] source [find interface/stlink.cfg]
source [find target/stm32f3x.cfg] source [find target/stm32f3x.cfg]

View File

@ -29,7 +29,11 @@
static uint8_t I2Caddress = 0; static uint8_t I2Caddress = 0;
// parno - number of parameter (or -1); cargs - string with arguments (after '=') (==NULL for getter), iarg - integer argument // parno - number of parameter (or -1); cargs - string with arguments (after '=') (==NULL for getter), iarg - integer argument
static int goodstub(const char _U_ *cmd, int _U_ parno, const char _U_ *carg, int32_t _U_ iarg){ static int goodstub(const char *cmd, int parno, const char *carg, int32_t iarg){
USB_sendstr("cmd="); USB_sendstr(cmd);
USB_sendstr(", parno="); USB_sendstr(i2str(parno));
USB_sendstr(", args="); USB_sendstr(carg);
USB_sendstr(", intarg="); USB_sendstr(i2str(iarg)); newline();
return RET_GOOD; return RET_GOOD;
} }
@ -130,8 +134,11 @@ static int tms(const char _U_ *cmd, int _U_ parno, const char _U_ *c, int32_t _U
static int pwm(const char *cmd, int parno, const char *c, int32_t i){ static int pwm(const char *cmd, int parno, const char *c, int32_t i){
if(parno < 0 || parno > 3) return RET_WRONGPARNO; if(parno < 0 || parno > 3) return RET_WRONGPARNO;
if(c) setPWM(parno, (uint8_t)i); if(c){
sendkeyu(cmd, -1, getPWM(parno)); if(i < 0 || i > PWM_CCR_MAX) return RET_WRONGARG;
setPWM(parno, (uint16_t)i);
}
sendkeyu(cmd, parno, getPWM(parno));
return RET_GOOD; return RET_GOOD;
} }
@ -146,7 +153,7 @@ commands cmdlist[] = {
{NULL, "Different commands", NULL}, {NULL, "Different commands", NULL},
{buzzer, "buzzer", "get/set (0 - off, 1 - on) buzzer"}, {buzzer, "buzzer", "get/set (0 - off, 1 - on) buzzer"},
{leds, "LED", "LEDx=y; where x=0..3 to work with single LED (then y=1-set, 0-reset, 2-toggle), absent to work with all (y=0 - disable, 1-enable)"}, {leds, "LED", "LEDx=y; where x=0..3 to work with single LED (then y=1-set, 0-reset, 2-toggle), absent to work with all (y=0 - disable, 1-enable)"},
{pwm, "pwm", "set/get x channel (0..3) pwm value (0..255)"}, {pwm, "pwm", "set/get x channel (0..3) pwm value (0..100)"},
{reset, "reset", "reset MCU"}, {reset, "reset", "reset MCU"},
{tms, "tms", "print Tms"}, {tms, "tms", "print Tms"},
{NULL, "I2C commands", NULL}, {NULL, "I2C commands", NULL},

View File

@ -50,10 +50,10 @@ static uint16_t lastaddr = LASTADDR_DEFAULT;
int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)(ep_t ep)){ int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)(ep_t ep)){
if(number >= STM32ENDPOINTS) return 4; // out of configured amount if(number >= STM32ENDPOINTS) return 4; // out of configured amount
if(txsz > USB_BTABLE_SIZE || rxsz > USB_BTABLE_SIZE) return 1; // buffer too large if(txsz > USB_BTABLE_SIZE || rxsz > USB_BTABLE_SIZE) return 1; // buffer too large
if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE) return 2; // out of btable if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE/ACCESSZ) return 2; // out of btable
USB->EPnR[number] = (type << 9) | (number & USB_EPnR_EA); USB->EPnR[number] = (type << 9) | (number & USB_EPnR_EA);
USB->EPnR[number] ^= USB_EPnR_STAT_RX | USB_EPnR_STAT_TX_1; USB->EPnR[number] ^= USB_EPnR_STAT_RX | USB_EPnR_STAT_TX_1;
if(rxsz & 1 || rxsz > 512) return 3; // wrong rx buffer size if(rxsz & 1 || rxsz > USB_BTABLE_SIZE) return 3; // wrong rx buffer size
uint16_t countrx = 0; uint16_t countrx = 0;
if(rxsz < 64) countrx = rxsz / 2; if(rxsz < 64) countrx = rxsz / 2;
else{ else{

View File

@ -1,2 +1,2 @@
#define BUILD_NUMBER "52" #define BUILD_NUMBER "56"
#define BUILD_DATE "2023-04-10" #define BUILD_DATE "2023-05-03"