Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 1 | #ifndef CAPE_ENCODER_H_ |
| 2 | #define CAPE_ENCODER_H_ |
| 3 | |
| 4 | #include <stdint.h> |
| 5 | #include <limits.h> |
| 6 | |
| 7 | #include <STM32F2XX.h> |
| 8 | |
| 9 | void encoder_init(void); |
| 10 | |
| 11 | // Updates a signed 32-bit counter with a new 16-bit value. Assumes that the |
| 12 | // value will not more than half-wrap between updates. |
| 13 | // new is 32 bits so it doesn't have to get masked, but the value passed in must |
| 14 | // be <= UINT16_MAX. |
| 15 | // Useful for 16-bit encoder counters. |
| 16 | static inline void counter_update_s32_u16(int32_t *restrict counter, |
| 17 | uint32_t new) { |
| 18 | static const uint16_t kHalf = 0xFFFF / 2; |
| 19 | uint16_t old = *counter & 0xFFFF; |
| 20 | int32_t counter_top = *counter ^ old; |
Brian Silverman | 3e0a05b | 2013-12-22 11:33:42 -0800 | [diff] [blame] | 21 | int32_t delta = (int32_t)old - (int32_t)new; |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 22 | int32_t new_counter; |
| 23 | if (__builtin_expect(delta < -kHalf, 0)) { |
Brian Silverman | 3e0a05b | 2013-12-22 11:33:42 -0800 | [diff] [blame] | 24 | new_counter = (counter_top - 0x10000) ^ 0xFFFF; |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 25 | } else if (__builtin_expect(delta > kHalf, 0)) { |
| 26 | new_counter = counter_top + 0x10000; |
| 27 | } else { |
| 28 | new_counter = counter_top; |
| 29 | } |
| 30 | *counter = new_counter | new; |
| 31 | } |
| 32 | |
| 33 | // Updates an unsigned 64-bit counter with a new 16-bit value. Assumes that the |
| 34 | // value will not wrap more than once between updates. |
| 35 | // new is 32 bits so it doesn't have to get masked, but the value passed in must |
| 36 | // be <= UINT16_MAX. |
| 37 | // Useful for 16-bit timers being used for absolute timings. |
| 38 | static inline void counter_update_u64_u16(uint64_t *restrict counter, |
| 39 | uint32_t new) { |
| 40 | uint16_t old = *counter & 0xFFFF; |
| 41 | int64_t counter_top = *counter ^ old; |
Brian Silverman | 3e0a05b | 2013-12-22 11:33:42 -0800 | [diff] [blame] | 42 | int64_t new_counter; |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 43 | if (__builtin_expect(new < old, 0)) { |
Brian Silverman | 3e0a05b | 2013-12-22 11:33:42 -0800 | [diff] [blame] | 44 | new_counter = counter_top + 0x10000; |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 45 | } else { |
Brian Silverman | 3e0a05b | 2013-12-22 11:33:42 -0800 | [diff] [blame] | 46 | new_counter = counter_top; |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 47 | } |
Brian Silverman | 3e0a05b | 2013-12-22 11:33:42 -0800 | [diff] [blame] | 48 | *counter = new_counter | new; |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // number is the 0-indexed number on the silkscreen |
| 52 | static inline int32_t encoder_read(int number) { |
Brian Silverman | d4a64d9 | 2013-12-25 15:52:59 -0800 | [diff] [blame] | 53 | static int32_t value0, value6, value7; |
| 54 | extern volatile int32_t encoder1_value, encoder3_value, encoder4_value; |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 55 | switch (number) { |
| 56 | case 0: |
| 57 | counter_update_s32_u16(&value0, TIM8->CNT); |
| 58 | return value0; |
| 59 | case 1: |
| 60 | return encoder1_value; |
| 61 | case 2: |
| 62 | return TIM5->CNT; |
| 63 | case 3: |
| 64 | return encoder3_value; |
| 65 | case 4: |
Brian Silverman | d4a64d9 | 2013-12-25 15:52:59 -0800 | [diff] [blame] | 66 | return encoder4_value; |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 67 | case 5: |
| 68 | return TIM2->CNT; |
| 69 | case 6: |
| 70 | counter_update_s32_u16(&value6, TIM3->CNT); |
| 71 | return value6; |
| 72 | case 7: |
| 73 | counter_update_s32_u16(&value7, TIM4->CNT); |
| 74 | return value7; |
| 75 | default: |
| 76 | return INT32_MAX; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | #endif // CAPE_ENCODER_H_ |