Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
amt22.c
Go to the documentation of this file.
1#include "amt22.h"
2
3
4#ifndef AMT22_SPI_CDIV
5#define AMT22_SPI_CDIV SPIDiv256
6#endif
7
8static bool amt22_checkbit(uint8_t p0, uint8_t p1);
9
10static void amt22_thd(void* arg);
11
13 amt->config = conf;
14 // Set up SPI peripheral and transaction
15 amt->trans.slave_idx = amt->config->slave_idx;
16 amt->trans.input_buf = amt->spi_input_buf;
17 amt->trans.output_buf = amt->spi_output_buf;
18 amt->trans.select = SPISelectUnselect;
19 amt->trans.cpol = SPICpolIdleLow;
20 amt->trans.cpha = SPICphaEdge1;
21 amt->trans.dss = SPIDss8bit;
22 amt->trans.bitorder = SPIMSBFirst;
23 amt->trans.cdiv = AMT22_SPI_CDIV;
24 amt->trans.before_cb = NULL;
25 amt->trans.after_cb = NULL;
26 amt->trans.status = SPITransDone;
27 amt->position = 0;
28 amt->turns = 0;
29
30
31 if(amt->config->type == AMT22_12_SINGLE || amt->config->type == AMT22_14_SINGLE) {
32 //single turn
33 amt->trans.output_buf[0] = 0x00;
34 amt->trans.output_buf[1] = 0x00;
35 amt->trans.output_length = 2;
36 amt->trans.input_length = 2;
37 } else {
38 //multi_turn
39 amt->trans.output_buf[0] = 0x00;
40 amt->trans.output_buf[1] = 0xA0;
41 amt->trans.output_buf[2] = 0x00;
42 amt->trans.output_buf[3] = 0x00;
43 amt->trans.output_length = 4;
44 amt->trans.input_length = 4;
45 }
46
47 pprz_bsem_init(&amt->bsem_amt22_read, true);
48 pprz_mtx_init(&amt->mtx);
49 pprz_thread_create(&amt->thd_handle, 512, "amt22", PPRZ_NORMAL_PRIO+1, amt22_thd, amt);
50}
51
53 // trigger read
54 pprz_bsem_signal(&amt->bsem_amt22_read);
55}
56
57
59 if(spi_blocking_transceive(amt->config->p, &amt->trans, 0.5) != SPITransSuccess) {
60 return false;
61 }
62
63 uint8_t p0 = amt->trans.input_buf[0];
64 uint8_t p1 = amt->trans.input_buf[1];
65
66 if(!amt22_checkbit(p0,p1)) {
67 return false;
68 }
69
70 uint16_t position = (p0 << 8 | p1) & 0x3fff;
71 int16_t turns = 0;
72
73 // 12 bits so shift 2
74 if(amt->config->type == AMT22_12_SINGLE || amt->config->type == AMT22_12_MULTI) {
75 position >>= 2;
76 }
77
78 if(amt->config->type == AMT22_12_MULTI || amt->config->type == AMT22_14_MULTI) {
79 uint8_t t0 = amt->trans.input_buf[2];
80 uint8_t t1 = amt->trans.input_buf[3];
81 turns = (t0 << 8 | t1);
82 }
83
84 pprz_mtx_lock(&amt->mtx);
85 amt->position = position;
86 amt->turns = turns;
87 pprz_mtx_unlock(&amt->mtx);
88 return true;
89}
90
91static bool amt22_checkbit(uint8_t p0, uint8_t p1) {
93 uint8_t odd = 0;
94 uint8_t even = 0;
95 while (data)
96 {
97 even ^= data & 1;
98 data >>= 1;
99 odd ^= data & 1;
100 data >>= 1;
101 }
102 even = !even;
103 odd = !odd;
104 if((even == ((p0 & 0x40) >> 6)) && (odd == ((p0 & 0x80) >> 7))) {
105 return 1;
106 } else {
107 return 0;
108 }
109}
110
112 pprz_mtx_lock(&amt->mtx);
113 uint16_t pos = amt->position;
114 pprz_mtx_unlock(&amt->mtx);
115 return pos;
116}
117
119 pprz_mtx_lock(&amt->mtx);
120 int16_t turns = amt->turns;
121 pprz_mtx_unlock(&amt->mtx);
122 return turns;
123}
124
125
126static void amt22_thd(void* arg) {
127 amt22_t* amt = (amt22_t*)arg;
128
129 while(true) {
130 pprz_bsem_wait(&amt->bsem_amt22_read);
132 }
133}
void amt22_periodic(amt22_t *amt)
Definition amt22.c:52
void amt22_init(amt22_t *amt, amt22_config_t *conf)
Definition amt22.c:12
static void amt22_thd(void *arg)
Definition amt22.c:126
bool amt22_read(amt22_t *amt)
Definition amt22.c:58
int16_t amt22_get_turns(amt22_t *amt)
Definition amt22.c:118
uint16_t amt22_get_position(amt22_t *amt)
Definition amt22.c:111
#define AMT22_SPI_CDIV
Definition amt22.c:5
static bool amt22_checkbit(uint8_t p0, uint8_t p1)
Definition amt22.c:91
@ AMT22_14_SINGLE
14-bits, single-turn
Definition amt22.h:8
@ AMT22_14_MULTI
14-bits, multi-turn
Definition amt22.h:10
@ AMT22_12_SINGLE
12-bits, single-turn
Definition amt22.h:7
@ AMT22_12_MULTI
12-bits, multi-turn
Definition amt22.h:9
struct Amt amt
int pprz_mtx_unlock(pprz_mutex_t *mtx)
void pprz_bsem_init(pprz_bsem_t *bsem, bool taken)
int pprz_thread_create(pprz_thread_t *thread, size_t size, const char *name, uint8_t prio, void(*pf)(void *), void *arg)
Creates a new thread whose stack is dynamically allocated.
int pprz_mtx_init(pprz_mutex_t *mtx)
int pprz_mtx_lock(pprz_mutex_t *mtx)
void pprz_bsem_signal(pprz_bsem_t *bsem)
void pprz_bsem_wait(pprz_bsem_t *bsem)
#define PPRZ_NORMAL_PRIO
enum SPITransactionStatus spi_blocking_transceive(struct spi_periph *p, struct spi_transaction *t, float timeout)
Perform a spi transaction (blocking).
Definition spi.c:169
@ SPICphaEdge1
CPHA = 0.
Definition spi.h:70
@ SPITransSuccess
Definition spi.h:95
@ SPITransDone
Definition spi.h:97
@ SPICpolIdleLow
CPOL = 0.
Definition spi.h:79
@ SPISelectUnselect
slave is selected before transaction and unselected after
Definition spi.h:59
@ SPIMSBFirst
Definition spi.h:108
@ SPIDss8bit
Definition spi.h:86
static float t0
static float t1
uint16_t foo
Definition main_demo5.c:58
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
short int16_t
Typedef defining 16 bit short type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.