serial.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * WARNING: be careful changing this code, it is very timing dependent
  3. */
  4. #ifndef F_CPU
  5. #define F_CPU 16000000
  6. #endif
  7. #include <avr/io.h>
  8. #include <avr/interrupt.h>
  9. #include <util/delay.h>
  10. #include <stddef.h>
  11. #include <stdbool.h>
  12. #include "serial.h"
  13. //#include <pro_micro.h>
  14. #ifdef USE_SERIAL
  15. #ifndef SERIAL_USE_MULTI_TRANSACTION
  16. /* --- USE Simple API (OLD API, compatible with let's split serial.c) */
  17. #if SERIAL_SLAVE_BUFFER_LENGTH > 0
  18. uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
  19. #endif
  20. #if SERIAL_MASTER_BUFFER_LENGTH > 0
  21. uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
  22. #endif
  23. uint8_t volatile status0 = 0;
  24. SSTD_t transactions[] = {
  25. { (uint8_t *)&status0,
  26. #if SERIAL_MASTER_BUFFER_LENGTH > 0
  27. sizeof(serial_master_buffer), (uint8_t *)serial_master_buffer,
  28. #else
  29. 0, (uint8_t *)NULL,
  30. #endif
  31. #if SERIAL_SLAVE_BUFFER_LENGTH > 0
  32. sizeof(serial_slave_buffer), (uint8_t *)serial_slave_buffer
  33. #else
  34. 0, (uint8_t *)NULL,
  35. #endif
  36. }
  37. };
  38. void serial_master_init(void)
  39. { soft_serial_initiator_init(transactions); }
  40. void serial_slave_init(void)
  41. { soft_serial_target_init(transactions); }
  42. // 0 => no error
  43. // 1 => slave did not respond
  44. // 2 => checksum error
  45. int serial_update_buffers()
  46. { return soft_serial_transaction(); }
  47. #endif // Simple API (OLD API, compatible with let's split serial.c)
  48. #define ALWAYS_INLINE __attribute__((always_inline))
  49. #define NO_INLINE __attribute__((noinline))
  50. #define _delay_sub_us(x) __builtin_avr_delay_cycles(x)
  51. // Serial pulse period in microseconds.
  52. #define TID_SEND_ADJUST 14
  53. #define SELECT_SERIAL_SPEED 1
  54. #if SELECT_SERIAL_SPEED == 0
  55. // Very High speed
  56. #define SERIAL_DELAY 4 // micro sec
  57. #define READ_WRITE_START_ADJUST 33 // cycles
  58. #define READ_WRITE_WIDTH_ADJUST 3 // cycles
  59. #elif SELECT_SERIAL_SPEED == 1
  60. // High speed
  61. #define SERIAL_DELAY 6 // micro sec
  62. #define READ_WRITE_START_ADJUST 30 // cycles
  63. #define READ_WRITE_WIDTH_ADJUST 3 // cycles
  64. #elif SELECT_SERIAL_SPEED == 2
  65. // Middle speed
  66. #define SERIAL_DELAY 12 // micro sec
  67. #define READ_WRITE_START_ADJUST 30 // cycles
  68. #define READ_WRITE_WIDTH_ADJUST 3 // cycles
  69. #elif SELECT_SERIAL_SPEED == 3
  70. // Low speed
  71. #define SERIAL_DELAY 24 // micro sec
  72. #define READ_WRITE_START_ADJUST 30 // cycles
  73. #define READ_WRITE_WIDTH_ADJUST 3 // cycles
  74. #elif SELECT_SERIAL_SPEED == 4
  75. // Very Low speed
  76. #define SERIAL_DELAY 50 // micro sec
  77. #define READ_WRITE_START_ADJUST 30 // cycles
  78. #define READ_WRITE_WIDTH_ADJUST 3 // cycles
  79. #else
  80. #error Illegal Serial Speed
  81. #endif
  82. #define SERIAL_DELAY_HALF1 (SERIAL_DELAY/2)
  83. #define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2)
  84. #define SLAVE_INT_WIDTH_US 1
  85. #ifndef SERIAL_USE_MULTI_TRANSACTION
  86. #define SLAVE_INT_RESPONSE_TIME SERIAL_DELAY
  87. #else
  88. #define SLAVE_INT_ACK_WIDTH_UNIT 2
  89. #define SLAVE_INT_ACK_WIDTH 4
  90. #endif
  91. static SSTD_t *Transaction_table = NULL;
  92. inline static
  93. void serial_delay(void) {
  94. _delay_us(SERIAL_DELAY);
  95. }
  96. inline static
  97. void serial_delay_half1(void) {
  98. _delay_us(SERIAL_DELAY_HALF1);
  99. }
  100. inline static
  101. void serial_delay_half2(void) {
  102. _delay_us(SERIAL_DELAY_HALF2);
  103. }
  104. inline static void serial_output(void) ALWAYS_INLINE;
  105. inline static
  106. void serial_output(void) {
  107. SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
  108. }
  109. // make the serial pin an input with pull-up resistor
  110. inline static void serial_input_with_pullup(void) ALWAYS_INLINE;
  111. inline static
  112. void serial_input_with_pullup(void) {
  113. SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
  114. SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
  115. }
  116. inline static
  117. uint8_t serial_read_pin(void) {
  118. return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
  119. }
  120. inline static void serial_low(void) ALWAYS_INLINE;
  121. inline static
  122. void serial_low(void) {
  123. SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
  124. }
  125. inline static void serial_high(void) ALWAYS_INLINE;
  126. inline static
  127. void serial_high(void) {
  128. SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
  129. }
  130. void soft_serial_initiator_init(SSTD_t *sstd_table)
  131. {
  132. Transaction_table = sstd_table;
  133. serial_output();
  134. serial_high();
  135. }
  136. void soft_serial_target_init(SSTD_t *sstd_table)
  137. {
  138. Transaction_table = sstd_table;
  139. serial_input_with_pullup();
  140. #if SERIAL_PIN_MASK == _BV(PD0)
  141. // Enable INT0
  142. EIMSK |= _BV(INT0);
  143. // Trigger on falling edge of INT0
  144. EICRA &= ~(_BV(ISC00) | _BV(ISC01));
  145. #elif SERIAL_PIN_MASK == _BV(PD2)
  146. // Enable INT2
  147. EIMSK |= _BV(INT2);
  148. // Trigger on falling edge of INT2
  149. EICRA &= ~(_BV(ISC20) | _BV(ISC21));
  150. #else
  151. #error unknown SERIAL_PIN_MASK value
  152. #endif
  153. }
  154. // Used by the sender to synchronize timing with the reciver.
  155. static void sync_recv(void) NO_INLINE;
  156. static
  157. void sync_recv(void) {
  158. for (uint8_t i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) {
  159. }
  160. // This shouldn't hang if the target disconnects because the
  161. // serial line will float to high if the target does disconnect.
  162. while (!serial_read_pin());
  163. }
  164. // Used by the reciver to send a synchronization signal to the sender.
  165. static void sync_send(void)NO_INLINE;
  166. static
  167. void sync_send(void) {
  168. serial_low();
  169. serial_delay();
  170. serial_high();
  171. }
  172. // Reads a byte from the serial line
  173. static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) NO_INLINE;
  174. static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) {
  175. uint8_t byte, i, p, pb;
  176. _delay_sub_us(READ_WRITE_START_ADJUST);
  177. for( i = 0, byte = 0, p = 0; i < bit; i++ ) {
  178. serial_delay_half1(); // read the middle of pulses
  179. if( serial_read_pin() ) {
  180. byte = (byte << 1) | 1; p ^= 1;
  181. } else {
  182. byte = (byte << 1) | 0; p ^= 0;
  183. }
  184. _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
  185. serial_delay_half2();
  186. }
  187. /* recive parity bit */
  188. serial_delay_half1(); // read the middle of pulses
  189. pb = serial_read_pin();
  190. _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
  191. serial_delay_half2();
  192. *pterrcount += (p != pb)? 1 : 0;
  193. return byte;
  194. }
  195. // Sends a byte with MSB ordering
  196. void serial_write_chunk(uint8_t data, uint8_t bit) NO_INLINE;
  197. void serial_write_chunk(uint8_t data, uint8_t bit) {
  198. uint8_t b, p;
  199. for( p = 0, b = 1<<(bit-1); b ; b >>= 1) {
  200. if(data & b) {
  201. serial_high(); p ^= 1;
  202. } else {
  203. serial_low(); p ^= 0;
  204. }
  205. serial_delay();
  206. }
  207. /* send parity bit */
  208. if(p & 1) { serial_high(); }
  209. else { serial_low(); }
  210. serial_delay();
  211. serial_low(); // sync_send() / senc_recv() need raise edge
  212. }
  213. static void serial_send_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
  214. static
  215. void serial_send_packet(uint8_t *buffer, uint8_t size) {
  216. for (uint8_t i = 0; i < size; ++i) {
  217. uint8_t data;
  218. data = buffer[i];
  219. sync_send();
  220. serial_write_chunk(data,8);
  221. }
  222. }
  223. static uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
  224. static
  225. uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) {
  226. uint8_t pecount = 0;
  227. for (uint8_t i = 0; i < size; ++i) {
  228. uint8_t data;
  229. sync_recv();
  230. data = serial_read_chunk(&pecount, 8);
  231. buffer[i] = data;
  232. }
  233. return pecount == 0;
  234. }
  235. inline static
  236. void change_sender2reciver(void) {
  237. sync_send(); //0
  238. serial_delay_half1(); //1
  239. serial_low(); //2
  240. serial_input_with_pullup(); //2
  241. serial_delay_half1(); //3
  242. }
  243. inline static
  244. void change_reciver2sender(void) {
  245. sync_recv(); //0
  246. serial_delay(); //1
  247. serial_low(); //3
  248. serial_output(); //3
  249. serial_delay_half1(); //4
  250. }
  251. // interrupt handle to be used by the target device
  252. ISR(SERIAL_PIN_INTERRUPT) {
  253. #ifndef SERIAL_USE_MULTI_TRANSACTION
  254. serial_low();
  255. serial_output();
  256. SSTD_t *trans = Transaction_table;
  257. #else
  258. // recive transaction table index
  259. uint8_t tid;
  260. uint8_t pecount = 0;
  261. sync_recv();
  262. tid = serial_read_chunk(&pecount,4);
  263. if(pecount> 0)
  264. return;
  265. serial_delay_half1();
  266. serial_high(); // response step1 low->high
  267. serial_output();
  268. _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT*SLAVE_INT_ACK_WIDTH);
  269. SSTD_t *trans = &Transaction_table[tid];
  270. serial_low(); // response step2 ack high->low
  271. #endif
  272. // target send phase
  273. if( trans->target2initiator_buffer_size > 0 )
  274. serial_send_packet((uint8_t *)trans->target2initiator_buffer,
  275. trans->target2initiator_buffer_size);
  276. // target switch to input
  277. change_sender2reciver();
  278. // target recive phase
  279. if( trans->initiator2target_buffer_size > 0 ) {
  280. if (serial_recive_packet((uint8_t *)trans->initiator2target_buffer,
  281. trans->initiator2target_buffer_size) ) {
  282. *trans->status = TRANSACTION_ACCEPTED;
  283. } else {
  284. *trans->status = TRANSACTION_DATA_ERROR;
  285. }
  286. } else {
  287. *trans->status = TRANSACTION_ACCEPTED;
  288. }
  289. sync_recv(); //weit initiator output to high
  290. }
  291. /////////
  292. // start transaction by initiator
  293. //
  294. // int soft_serial_transaction(int sstd_index)
  295. //
  296. // Returns:
  297. // TRANSACTION_END
  298. // TRANSACTION_NO_RESPONSE
  299. // TRANSACTION_DATA_ERROR
  300. // this code is very time dependent, so we need to disable interrupts
  301. #ifndef SERIAL_USE_MULTI_TRANSACTION
  302. int soft_serial_transaction(void) {
  303. SSTD_t *trans = Transaction_table;
  304. #else
  305. int soft_serial_transaction(int sstd_index) {
  306. SSTD_t *trans = &Transaction_table[sstd_index];
  307. #endif
  308. cli();
  309. // signal to the target that we want to start a transaction
  310. serial_output();
  311. serial_low();
  312. _delay_us(SLAVE_INT_WIDTH_US);
  313. #ifndef SERIAL_USE_MULTI_TRANSACTION
  314. // wait for the target response
  315. serial_input_with_pullup();
  316. _delay_us(SLAVE_INT_RESPONSE_TIME);
  317. // check if the target is present
  318. if (serial_read_pin()) {
  319. // target failed to pull the line low, assume not present
  320. serial_output();
  321. serial_high();
  322. *trans->status = TRANSACTION_NO_RESPONSE;
  323. sei();
  324. return TRANSACTION_NO_RESPONSE;
  325. }
  326. #else
  327. // send transaction table index
  328. sync_send();
  329. _delay_sub_us(TID_SEND_ADJUST);
  330. serial_write_chunk(sstd_index, 4);
  331. serial_delay_half1();
  332. // wait for the target response (step1 low->high)
  333. serial_input_with_pullup();
  334. while( !serial_read_pin() ) {
  335. _delay_sub_us(2);
  336. }
  337. // check if the target is present (step2 high->low)
  338. for( int i = 0; serial_read_pin(); i++ ) {
  339. if (i > SLAVE_INT_ACK_WIDTH + 1) {
  340. // slave failed to pull the line low, assume not present
  341. serial_output();
  342. serial_high();
  343. *trans->status = TRANSACTION_NO_RESPONSE;
  344. sei();
  345. return TRANSACTION_NO_RESPONSE;
  346. }
  347. _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT);
  348. }
  349. #endif
  350. // initiator recive phase
  351. // if the target is present syncronize with it
  352. if( trans->target2initiator_buffer_size > 0 ) {
  353. if (!serial_recive_packet((uint8_t *)trans->target2initiator_buffer,
  354. trans->target2initiator_buffer_size) ) {
  355. serial_output();
  356. serial_high();
  357. *trans->status = TRANSACTION_DATA_ERROR;
  358. sei();
  359. return TRANSACTION_DATA_ERROR;
  360. }
  361. }
  362. // initiator switch to output
  363. change_reciver2sender();
  364. // initiator send phase
  365. if( trans->initiator2target_buffer_size > 0 ) {
  366. serial_send_packet((uint8_t *)trans->initiator2target_buffer,
  367. trans->initiator2target_buffer_size);
  368. }
  369. // always, release the line when not in use
  370. sync_send();
  371. *trans->status = TRANSACTION_END;
  372. sei();
  373. return TRANSACTION_END;
  374. }
  375. #ifdef SERIAL_USE_MULTI_TRANSACTION
  376. int soft_serial_get_and_clean_status(int sstd_index) {
  377. SSTD_t *trans = &Transaction_table[sstd_index];
  378. cli();
  379. int retval = *trans->status;
  380. *trans->status = 0;;
  381. sei();
  382. return retval;
  383. }
  384. #endif
  385. #endif