| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- /*
- * maincpp.cpp
- *
- * Created on: 22 Jun 2022
- * Author: Garth Seale
- */
- #include "main.h"
- #include "gpio.h"
- #include "tim.h"
- #include "extra.h"
- #include "usart.h"
- #include "string.h"
- #include "stdio.h"
- #include "math.h"
- /*Version Number*/
- #define MAJOR 22
- #define MINOR 8
- #define PATCH 0
- #define STREAMLENTH 8
- const uint32_t pclock = 36000000;
- uint8_t rxbuff[64];
- uint8_t txbuff[STREAMLENTH];
- enum {
- DATASIZE,
- PORT1STAT,
- PORT1VAL_H,
- PORT1VAL_L,
- PORT2STAT,
- PORT2VAL_H,
- PORT2VAL_L,
- CHECKSUM
- };
- struct PWMOUT {
- uint8_t portStat;
- uint32_t freq;
- uint32_t autoreloadr;
- uint32_t duty;
- uint32_t scale;
- TIM_HandleTypeDef timport;
- uint32_t timchan;
- void Init(TIM_HandleTypeDef *port, uint32_t tchan);
- void output(uint16_t freq);
- uint32_t setupoutput(uint32_t freq);
- };
- uint8_t chsum(uint8_t * data);
- uint8_t txdata(uint8_t * data);
- PWMOUT port1freq;
- PWMOUT port2freq;
- uint16_t numberofdigits = 0;
- uint8_t count = 0;
- //#include "STM_ENC28_J60.h"
- //volatile uint16_t pwm1freq = 0;
- //volatile uint16_t pwm1freqnew = 100;
- //volatile uint16_t pwm2freq = 0;
- //volatile uint16_t pwm2freqnew = 9999;
- //volatile uint16_t val;
- //volatile uint32_t pwm1counter;
- //volatile uint32_t pwm1period;
- void maincpp()
- {
- // numberofdigits = pwm2freqnew;
- // while(numberofdigits != 0) {
- // numberofdigits=numberofdigits/10;
- // count++;
- // }
- HAL_UARTEx_ReceiveToIdle_IT(&huart1, rxbuff, 64);
- port1freq.Init(&htim1, TIM_CHANNEL_1);
- port2freq.Init(&htim3, TIM_CHANNEL_4);
- // HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
- // HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
- // port1freq.setupoutput(0);
- // port2freq.setupoutput(0);
- // port1freq.freq = (360000/10)-1;
- // port1freq.duty = port1freq.freq/2;
- // htim1.Instance->CCR1 = port1freq.duty;
- // htim1.Instance->ARR = port1freq.freq;
- // port2freq.freq = (360000/2000)-1;
- // port2freq.duty = port2freq.freq/2;
- // // htim3.Instance->CCR1
- // htim3.Instance->CCR4 = port2freq.duty;
- // htim3.Instance->ARR = port2freq.freq;
- // HAL_TIM_Base_Start_IT(&htim17);
- while(1)
- {
- }
- }
- void PWMOUT::Init(TIM_HandleTypeDef *port, uint32_t tchan) {
- timport = *port;
- timchan = tchan;
- HAL_TIM_PWM_Start(&timport, timchan);
- setupoutput(0);
- }
- uint32_t PWMOUT::setupoutput(uint32_t pwmfreq) {
- if(pwmfreq > 10000) pwmfreq = 10000;
- if(pwmfreq<100) timport.Instance->PSC = 999;
- else timport.Instance->PSC = 99;
- scale = pclock/((timport.Instance->PSC)+1);
- if(pwmfreq != 0) {
- portStat = 1;
- autoreloadr = scale/pwmfreq;
- duty = autoreloadr/2;
- }
- else {
- duty = 0;
- portStat = 0;
- }
- switch (timchan)
- {
- case TIM_CHANNEL_1:
- timport.Instance->CCR1 = duty;
- break;
- case TIM_CHANNEL_2:
- timport.Instance->CCR2 = duty;
- break;
- case TIM_CHANNEL_3:
- timport.Instance->CCR3 = duty;
- break;
- case TIM_CHANNEL_4:
- timport.Instance->CCR4 = duty;
- break;
- }
- timport.Instance->ARR = autoreloadr;
- freq = pwmfreq;
- return freq;
- }
- uint8_t txdata(uint8_t * data) {
- char s[64];
- sprintf(s, "$FUNGEN,%u,%lu,%u,%lu", port1freq.portStat, port1freq.freq, port2freq.portStat, port2freq.freq);
- char ch[6];
- sprintf(ch,"*%x\r\n", chsum((uint8_t*)s));
- strcat(s, ch);
- // data[DATASIZE] = STREAMLENTH;
- // data[PORT1STAT] = port1freq.portStat;
- // data[PORT1VAL_H] = port1freq.freq>>8;
- // data[PORT1VAL_L] = port1freq.freq & 0xFF;
- // data[PORT2STAT] = port2freq.portStat;
- // data[PORT2VAL_H] = port2freq.freq>>8;
- // data[PORT2VAL_L] = port2freq.freq & 0xFF;
- // data[CHECKSUM] = chsum(data);
- HAL_UART_Transmit(&huart1, (uint8_t*)s, strlen(s), HAL_MAX_DELAY);
- return 1;
- }
- uint8_t chsum(uint8_t * data) {
- uint8_t result = 0;
- for(int i = 0; i < (int)strlen((char*)data); i++)
- {
- result ^= data[i];
- }
- return result;
- }
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- /* Prevent unused argument(s) compilation warning */
- // if(htim->Instance == TIM17)
- // {
- // pwm1counter++;
- // if(pwm1counter >= pwm1period)
- // {
- // // HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
- // pwm1counter = 0;
- // }
- // }
- }
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(huart);
- __NOP();
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_UART_RxCpltCallback can be implemented in the user file.
- */
- }
- void processrxbuff(uint8_t * data, uint16_t Size)
- {
- /*
- * Query ports if ?\r\n received *
- */
- if(Size == 3)
- {
- if(strstr((char*)data,"?\r\n"))
- {
- txdata(txbuff);
- }
- if(strstr((char*)data,"v\r\n"))
- {
- char s[48];
- sprintf(s, "ATP Function Gen\r\nVersion: %u.%u.%u\r\n", MAJOR, MINOR, PATCH);
- HAL_UART_Transmit(&huart1, (uint8_t*)s, strlen(s), HAL_MAX_DELAY);
- }
- }
- else if(strstr((char*)data, "PWM1="))
- {
- sscanf((char*)data, "PWM1=%lu", &port1freq.freq);
- port1freq.setupoutput(port1freq.freq);
- txdata(txbuff);
- }
- else if(strstr((char*)data, "PWM2="))
- {
- sscanf((char*)data, "PWM2=%lu", &port2freq.freq);
- port2freq.setupoutput(port2freq.freq);
- txdata(txbuff);
- }
- else
- {
- char s[] = "Invalid input!\r\n";
- HAL_UART_Transmit(&huart1, (uint8_t*)s, strlen(s), HAL_MAX_DELAY);
- }
- }
- void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
- {
- /* Prevent unused argument(s) compilation warning */
- __NOP();
- processrxbuff(rxbuff, Size);
- HAL_UARTEx_ReceiveToIdle_IT(&huart1, rxbuff, 64);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_UARTEx_RxEventCallback can be implemented in the user file.
- */
- }
|