maincpp.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * maincpp.cpp
  3. *
  4. * Created on: 22 Jun 2022
  5. * Author: Garth Seale
  6. */
  7. #include "main.h"
  8. #include "gpio.h"
  9. #include "tim.h"
  10. #include "extra.h"
  11. #include "usart.h"
  12. #include "string.h"
  13. #include "stdio.h"
  14. #include "math.h"
  15. /*Version Number*/
  16. #define MAJOR 22
  17. #define MINOR 8
  18. #define PATCH 0
  19. #define STREAMLENTH 8
  20. const uint32_t pclock = 36000000;
  21. uint8_t rxbuff[64];
  22. uint8_t txbuff[STREAMLENTH];
  23. enum {
  24. DATASIZE,
  25. PORT1STAT,
  26. PORT1VAL_H,
  27. PORT1VAL_L,
  28. PORT2STAT,
  29. PORT2VAL_H,
  30. PORT2VAL_L,
  31. CHECKSUM
  32. };
  33. struct PWMOUT {
  34. uint8_t portStat;
  35. uint32_t freq;
  36. uint32_t autoreloadr;
  37. uint32_t duty;
  38. uint32_t scale;
  39. TIM_HandleTypeDef timport;
  40. uint32_t timchan;
  41. void Init(TIM_HandleTypeDef *port, uint32_t tchan);
  42. void output(uint16_t freq);
  43. uint32_t setupoutput(uint32_t freq);
  44. };
  45. uint8_t chsum(uint8_t * data);
  46. uint8_t txdata(uint8_t * data);
  47. PWMOUT port1freq;
  48. PWMOUT port2freq;
  49. uint16_t numberofdigits = 0;
  50. uint8_t count = 0;
  51. //#include "STM_ENC28_J60.h"
  52. //volatile uint16_t pwm1freq = 0;
  53. //volatile uint16_t pwm1freqnew = 100;
  54. //volatile uint16_t pwm2freq = 0;
  55. //volatile uint16_t pwm2freqnew = 9999;
  56. //volatile uint16_t val;
  57. //volatile uint32_t pwm1counter;
  58. //volatile uint32_t pwm1period;
  59. void maincpp()
  60. {
  61. // numberofdigits = pwm2freqnew;
  62. // while(numberofdigits != 0) {
  63. // numberofdigits=numberofdigits/10;
  64. // count++;
  65. // }
  66. HAL_UARTEx_ReceiveToIdle_IT(&huart1, rxbuff, 64);
  67. port1freq.Init(&htim1, TIM_CHANNEL_1);
  68. port2freq.Init(&htim3, TIM_CHANNEL_4);
  69. // HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
  70. // HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
  71. port1freq.setupoutput(0);
  72. port2freq.setupoutput(100000);
  73. // port1freq.freq = (360000/10)-1;
  74. // port1freq.duty = port1freq.freq/2;
  75. // htim1.Instance->CCR1 = port1freq.duty;
  76. // htim1.Instance->ARR = port1freq.freq;
  77. // port2freq.freq = (360000/2000)-1;
  78. // port2freq.duty = port2freq.freq/2;
  79. // // htim3.Instance->CCR1
  80. // htim3.Instance->CCR4 = port2freq.duty;
  81. // htim3.Instance->ARR = port2freq.freq;
  82. // HAL_TIM_Base_Start_IT(&htim17);
  83. while(1)
  84. {
  85. }
  86. }
  87. void PWMOUT::Init(TIM_HandleTypeDef *port, uint32_t tchan) {
  88. timport = *port;
  89. timchan = tchan;
  90. HAL_TIM_PWM_Start(&timport, timchan);
  91. }
  92. uint32_t PWMOUT::setupoutput(uint32_t pwmfreq) {
  93. if(pwmfreq > 10000) pwmfreq = 10000;
  94. if(pwmfreq<100) timport.Instance->PSC = 999;
  95. else timport.Instance->PSC = 99;
  96. scale = pclock/((timport.Instance->PSC)+1);
  97. if(pwmfreq != 0) {
  98. portStat = 1;
  99. autoreloadr = scale/pwmfreq;
  100. duty = autoreloadr/2;
  101. }
  102. else {
  103. duty = 0;
  104. portStat = 0;
  105. }
  106. switch (timchan)
  107. {
  108. case TIM_CHANNEL_1:
  109. timport.Instance->CCR1 = duty;
  110. break;
  111. case TIM_CHANNEL_2:
  112. timport.Instance->CCR2 = duty;
  113. break;
  114. case TIM_CHANNEL_3:
  115. timport.Instance->CCR3 = duty;
  116. break;
  117. case TIM_CHANNEL_4:
  118. timport.Instance->CCR4 = duty;
  119. break;
  120. }
  121. timport.Instance->ARR = autoreloadr;
  122. freq = pwmfreq;
  123. return freq;
  124. }
  125. uint8_t txdata(uint8_t * data) {
  126. char s[64];
  127. sprintf(s, "$FUNGEN,%u,%lu,%u,%lu", port1freq.portStat, port1freq.freq, port2freq.portStat, port2freq.freq);
  128. char ch[6];
  129. sprintf(ch,"*%x\r\n", chsum((uint8_t*)s));
  130. strcat(s, ch);
  131. // data[DATASIZE] = STREAMLENTH;
  132. // data[PORT1STAT] = port1freq.portStat;
  133. // data[PORT1VAL_H] = port1freq.freq>>8;
  134. // data[PORT1VAL_L] = port1freq.freq & 0xFF;
  135. // data[PORT2STAT] = port2freq.portStat;
  136. // data[PORT2VAL_H] = port2freq.freq>>8;
  137. // data[PORT2VAL_L] = port2freq.freq & 0xFF;
  138. // data[CHECKSUM] = chsum(data);
  139. HAL_UART_Transmit(&huart1, (uint8_t*)s, strlen(s), HAL_MAX_DELAY);
  140. return 1;
  141. }
  142. uint8_t chsum(uint8_t * data) {
  143. uint8_t result = 0;
  144. for(int i = 0; i < STREAMLENTH-1; i++)
  145. {
  146. result ^= data[i];
  147. }
  148. return result;
  149. }
  150. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  151. {
  152. /* Prevent unused argument(s) compilation warning */
  153. // if(htim->Instance == TIM17)
  154. // {
  155. // pwm1counter++;
  156. // if(pwm1counter >= pwm1period)
  157. // {
  158. // // HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
  159. // pwm1counter = 0;
  160. // }
  161. // }
  162. }
  163. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  164. {
  165. /* Prevent unused argument(s) compilation warning */
  166. UNUSED(huart);
  167. __NOP();
  168. /* NOTE : This function should not be modified, when the callback is needed,
  169. the HAL_UART_RxCpltCallback can be implemented in the user file.
  170. */
  171. }
  172. void processrxbuff(uint8_t * data, uint16_t Size)
  173. {
  174. /*
  175. * Query ports if ?\r\n received *
  176. */
  177. if(Size == 3)
  178. {
  179. if(strstr((char*)data,"?\r\n"))
  180. {
  181. txdata(txbuff);
  182. }
  183. if(strstr((char*)data,"v\r\n"))
  184. {
  185. char s[48];
  186. sprintf(s, "ATP Function Gen\r\nVersion: %u.%u.%u\r\n", MAJOR, MINOR, PATCH);
  187. HAL_UART_Transmit(&huart1, (uint8_t*)s, strlen(s), HAL_MAX_DELAY);
  188. }
  189. }
  190. else if(strstr((char*)data, "PWM1="))
  191. {
  192. sscanf((char*)data, "PWM1=%lu", &port1freq.freq);
  193. port1freq.setupoutput(port1freq.freq);
  194. txdata(txbuff);
  195. }
  196. else if(strstr((char*)data, "PWM2="))
  197. {
  198. sscanf((char*)data, "PWM2=%lu", &port2freq.freq);
  199. port2freq.setupoutput(port2freq.freq);
  200. txdata(txbuff);
  201. }
  202. else
  203. {
  204. char s[] = "Invalid input!\r\n";
  205. HAL_UART_Transmit(&huart1, (uint8_t*)s, strlen(s), HAL_MAX_DELAY);
  206. }
  207. }
  208. void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
  209. {
  210. /* Prevent unused argument(s) compilation warning */
  211. __NOP();
  212. processrxbuff(rxbuff, Size);
  213. HAL_UARTEx_ReceiveToIdle_IT(&huart1, rxbuff, 64);
  214. /* NOTE : This function should not be modified, when the callback is needed,
  215. the HAL_UARTEx_RxEventCallback can be implemented in the user file.
  216. */
  217. }