|
|
@@ -11,14 +11,17 @@
|
|
|
#include "extra.h"
|
|
|
#include "usart.h"
|
|
|
#include "string.h"
|
|
|
+#include "stdio.h"
|
|
|
+#include "math.h"
|
|
|
|
|
|
/*Version Number*/
|
|
|
-#define MAJOR 0
|
|
|
-#define MINOR 1
|
|
|
+#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];
|
|
|
@@ -36,8 +39,10 @@ enum {
|
|
|
|
|
|
struct PWMOUT {
|
|
|
uint8_t portStat;
|
|
|
- uint16_t freq;
|
|
|
- uint16_t duty;
|
|
|
+ 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);
|
|
|
@@ -76,8 +81,8 @@ void maincpp()
|
|
|
// HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
|
|
|
// HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
|
|
|
|
|
|
- port1freq.setupoutput(50);
|
|
|
- port2freq.setupoutput(50);
|
|
|
+ port1freq.setupoutput(0);
|
|
|
+ port2freq.setupoutput(100000);
|
|
|
|
|
|
// port1freq.freq = (360000/10)-1;
|
|
|
// port1freq.duty = port1freq.freq/2;
|
|
|
@@ -94,24 +99,8 @@ void maincpp()
|
|
|
while(1)
|
|
|
{
|
|
|
|
|
|
- txdata(txbuff);
|
|
|
- HAL_Delay(1000);
|
|
|
- // if(pwm1freq != pwm1freqnew)
|
|
|
- // {
|
|
|
- // pwm1freq = pwm1freqnew;
|
|
|
- //// val = (map(pwm1freq, 1, 10000, 10000, 1))-1;
|
|
|
- // htim1.Instance->CCR1 = (pwm1freq/2);
|
|
|
- // htim1.Instance->ARR = pwm1freq;
|
|
|
- //
|
|
|
- // }
|
|
|
- // if(pwm2freq != pwm2freqnew)
|
|
|
- // {
|
|
|
- // val = (360000/pwm2freqnew)-1;
|
|
|
- // pwm1freqnew = val;
|
|
|
- // pwm2freq = pwm2freqnew;
|
|
|
- //
|
|
|
- //
|
|
|
- // }
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -122,23 +111,34 @@ void PWMOUT::Init(TIM_HandleTypeDef *port, uint32_t tchan) {
|
|
|
}
|
|
|
|
|
|
uint32_t PWMOUT::setupoutput(uint32_t pwmfreq) {
|
|
|
-
|
|
|
- uint32_t scale = 360000;
|
|
|
- uint32_t autoreloadr = scale/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 = autoreloadr/2;
|
|
|
+ timport.Instance->CCR1 = duty;
|
|
|
break;
|
|
|
case TIM_CHANNEL_2:
|
|
|
- timport.Instance->CCR2 = autoreloadr/2;
|
|
|
+ timport.Instance->CCR2 = duty;
|
|
|
break;
|
|
|
case TIM_CHANNEL_3:
|
|
|
- timport.Instance->CCR3 = autoreloadr/2;
|
|
|
+ timport.Instance->CCR3 = duty;
|
|
|
break;
|
|
|
case TIM_CHANNEL_4:
|
|
|
- timport.Instance->CCR4 = autoreloadr/2;
|
|
|
+ timport.Instance->CCR4 = duty;
|
|
|
break;
|
|
|
}
|
|
|
timport.Instance->ARR = autoreloadr;
|
|
|
@@ -147,15 +147,20 @@ uint32_t PWMOUT::setupoutput(uint32_t pwmfreq) {
|
|
|
}
|
|
|
|
|
|
uint8_t txdata(uint8_t * data) {
|
|
|
- txbuff[DATASIZE] = STREAMLENTH;
|
|
|
- txbuff[PORT1STAT] = port1freq.portStat;
|
|
|
- txbuff[PORT1VAL_H] = port1freq.freq>>8;
|
|
|
- txbuff[PORT1VAL_L] = port1freq.freq & 0x0F;
|
|
|
- txbuff[PORT2STAT] = port2freq.portStat;
|
|
|
- txbuff[PORT2VAL_H] = port2freq.freq>>8;
|
|
|
- txbuff[PORT2VAL_L] = port2freq.freq & 0x0F;
|
|
|
- txbuff[CHECKSUM] = chsum(data);
|
|
|
- HAL_UART_Transmit(&huart1, data, STREAMLENTH, HAL_MAX_DELAY);
|
|
|
+ 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;
|
|
|
}
|
|
|
|
|
|
@@ -195,12 +200,49 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
|
|
*/
|
|
|
}
|
|
|
|
|
|
+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 */
|
|
|
- UNUSED(huart);
|
|
|
- UNUSED(Size);
|
|
|
__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.
|