Advertisement
Guest User

Untitled

a guest
Oct 26th, 2021
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. #include <stdint.h>
  2. #include "pru_uart.h"
  3. #include "pru_ecap.h"
  4.  
  5. struct SharedVars {
  6. uint32_t position;
  7. int32_t force;
  8. };
  9.  
  10. // strcuture holding PRU variables from the loop
  11. far struct SharedVars volatile shared_pru1_vars __attribute__((location(0x10000)));
  12.  
  13. #define position_var ((uint32_t const volatile *)0x00002000)
  14.  
  15. #define EPWM1A ( *(uint16_t volatile *)0x48302212 )
  16. #define EPWM1B ( *(uint16_t volatile *)0x48302214 )
  17.  
  18.  
  19. // receive byte from uart
  20. static inline char uart_recv_byte()
  21. {
  22. for(;;) {
  23. uint8_t lsr = CT_UART.LSR;
  24. if( lsr & 0x1e )
  25. __halt(); // receive-error occurred
  26. if( lsr & 0x01 )
  27. return (char) CT_UART.RBR;
  28. }
  29. }
  30.  
  31. // receive CR-terminated line from uart
  32. static inline uint8_t uart_recv_line( char volatile msg[], uint8_t maxlen )
  33. {
  34. uint8_t len = 0;
  35. for(;;) {
  36. char c = uart_recv_byte();
  37. if( c == '\r' )
  38. break; // found end of line
  39. if( len == maxlen )
  40. __halt(); // line does not fit in buffer
  41. msg[ len++ ] = c;
  42. }
  43. return len;
  44. }
  45.  
  46.  
  47.  
  48. // receive and parse measurement message from load cell
  49. int32_t receive_measurement()
  50. {
  51. // allocate buffer at fixed address for debugging convenience
  52. static char volatile msg[8] __attribute__((location(0x1f00)));
  53. // receive line from uart
  54. uint8_t len = uart_recv_line( msg, sizeof msg );
  55. // verify length and prefix
  56. if( len != 8 || msg[0] != 'N' || (msg[1] != '+' && msg[1] != '-'))
  57. __halt();
  58. // parse the remainder as integer
  59. int32_t value = 0;
  60. uint8_t i;
  61. for( i = 2; i < len; i++ ) {
  62. if( msg[i] < '0' || msg[i] > '9' )
  63. __halt();
  64. value = value * 10 + ( msg[i] - '0' );
  65. }
  66. if (msg[1] == '-')
  67. value *= -1;
  68.  
  69. return value;
  70. }
  71.  
  72. void main()
  73. {
  74. uint32_t position =0;
  75. int32_t force = 0;
  76. int32_t period = 100;
  77. CT_UART.THR = 'S';
  78. CT_UART.THR = 'N';
  79. CT_UART.THR = '\r';
  80. EPWM1A = period;
  81. EPWM1B = 0;
  82.  
  83. for(;;) {
  84. // parse and interpret message from load cell
  85. force = receive_measurement();
  86. // read position value from decoder
  87. position = *position_var;
  88. // update sharedVars for GUI access
  89. shared_pru1_vars.position = position;
  90. shared_pru1_vars.force = force;
  91. }
  92. }
  93.  
  94.  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement