qmk

QMK Firmware
git clone git://git.z3bra.org/qmk.git
Log | Files | Refs | Submodules | LICENSE

split_scomm.c (2675B)


      1 #ifdef USE_SERIAL
      2 #ifdef SERIAL_USE_MULTI_TRANSACTION
      3 /* --- USE flexible API (using multi-type transaction function) --- */
      4 
      5 #include <stdbool.h>
      6 #include <stdint.h>
      7 #include <stddef.h>
      8 #include <split_scomm.h>
      9 #include "serial.h"
     10 #ifdef CONSOLE_ENABLE
     11   #include <print.h>
     12 #endif
     13 
     14 uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
     15 uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
     16 uint8_t volatile status_com = 0;
     17 uint8_t volatile status1 = 0;
     18 uint8_t slave_buffer_change_count = 0;
     19 uint8_t s_change_old = 0xff;
     20 uint8_t s_change_new = 0xff;
     21 
     22 SSTD_t transactions[] = {
     23 #define GET_SLAVE_STATUS 0
     24     /* master buffer not changed, only recive slave_buffer_change_count */
     25     { (uint8_t *)&status_com,
     26       0, NULL,
     27       sizeof(slave_buffer_change_count), &slave_buffer_change_count,
     28     },
     29 #define PUT_MASTER_GET_SLAVE_STATUS 1
     30     /* master buffer changed need send, and recive slave_buffer_change_count  */
     31     { (uint8_t *)&status_com,
     32       sizeof(serial_master_buffer), (uint8_t *)serial_master_buffer,
     33       sizeof(slave_buffer_change_count), &slave_buffer_change_count,
     34     },
     35 #define GET_SLAVE_BUFFER 2
     36     /* recive serial_slave_buffer */
     37     { (uint8_t *)&status1,
     38       0, NULL,
     39       sizeof(serial_slave_buffer), (uint8_t *)serial_slave_buffer
     40     }
     41 };
     42 
     43 void serial_master_init(void)
     44 {
     45     soft_serial_initiator_init(transactions, TID_LIMIT(transactions));
     46 }
     47 
     48 void serial_slave_init(void)
     49 {
     50     soft_serial_target_init(transactions, TID_LIMIT(transactions));
     51 }
     52 
     53 // 0 => no error
     54 // 1 => slave did not respond
     55 // 2 => checksum error
     56 int serial_update_buffers(int master_update)
     57 {
     58     int status, smatstatus;
     59     static int need_retry = 0;
     60 
     61     if( s_change_old != s_change_new ) {
     62         smatstatus = soft_serial_transaction(GET_SLAVE_BUFFER);
     63         if( smatstatus == TRANSACTION_END ) {
     64             s_change_old = s_change_new;
     65 #ifdef CONSOLE_ENABLE
     66             uprintf("slave matrix = %b %b %b %b\n",
     67                     serial_slave_buffer[0], serial_slave_buffer[1],
     68                     serial_slave_buffer[2], serial_slave_buffer[3]);
     69 #endif
     70         }
     71     } else {
     72         // serial_slave_buffer dosen't change
     73         smatstatus = TRANSACTION_END; // dummy status
     74     }
     75 
     76     if( !master_update && !need_retry) {
     77         status = soft_serial_transaction(GET_SLAVE_STATUS);
     78     } else {
     79         status = soft_serial_transaction(PUT_MASTER_GET_SLAVE_STATUS);
     80     }
     81     if( status == TRANSACTION_END ) {
     82         s_change_new = slave_buffer_change_count;
     83         need_retry = 0;
     84     } else {
     85         need_retry = 1;
     86     }
     87     return smatstatus;
     88 }
     89 
     90 #endif // SERIAL_USE_MULTI_TRANSACTION
     91 #endif /* USE_SERIAL */