You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB

2 years ago
  1. /*
  2. * Macros.h
  3. *
  4. * Created on: 04.01.2018
  5. * Author: LH
  6. */
  7. #ifndef TMC_MACROS_H_
  8. #define TMC_MACROS_H_
  9. /* Cast a n bit signed int to a 32 bit signed int
  10. * This is done by checking the MSB of the signed int (Bit n).
  11. * If it is 1, the value is negative and the Bits 32 to n+1 are set to 1
  12. * If it is 0, the value remains unchanged
  13. */
  14. #define CAST_Sn_TO_S32(value, n) ((value) | (((value) & ((uint32_t)1<<((n)-1)))? ~(((uint32_t)1<<(n))-1) : 0 ))
  15. // Min/Max macros
  16. #ifndef MIN
  17. #define MIN(a,b) (((a)<(b)) ? (a) : (b))
  18. #endif
  19. #ifndef MAX
  20. #define MAX(a,b) (((a)>(b)) ? (a) : (b))
  21. #endif
  22. // Static Array length
  23. #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
  24. // Generic mask/shift macros
  25. #define FIELD_GET(data, mask, shift) \
  26. (((data) & (mask)) >> (shift))
  27. #define FIELD_SET(data, mask, shift, value) \
  28. (((data) & (~(mask))) | (((value) << (shift)) & (mask)))
  29. // Register read/write/update macros using Mask/Shift:
  30. #define FIELD_READ(read, motor, address, mask, shift) \
  31. FIELD_GET(read(motor, address), mask, shift)
  32. #define FIELD_WRITE(write, motor, address, mask, shift, value) \
  33. (write(motor, address, ((value)<<(shift)) & (mask)))
  34. #define FIELD_UPDATE(read, write, motor, address, mask, shift, value) \
  35. (write(motor, address, FIELD_SET(read(motor, address), mask, shift, value)))
  36. // Macro to surpress unused parameter warnings
  37. // Memory access helpers
  38. // Force the compiler to access a location exactly once
  39. #define ACCESS_ONCE(x) *((volatile typeof(x) *) (&x))
  40. // Macro to remove write bit for shadow register array access
  41. #define TMC_ADDRESS(x) ((x) & (TMC_ADDRESS_MASK))
  42. #endif /* TMC_MACROS_H_ */