aboutsummaryrefslogtreecommitdiffstats
path: root/tmk_core/ring_buffer.h
blob: 7bdebbcf34f6bc0854a329e275d6f7e2cf58f3e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef RING_BUFFER_H
#define RING_BUFFER_H
/*--------------------------------------------------------------------
 * Ring buffer to store scan codes from keyboard
 *------------------------------------------------------------------*/
#define RBUF_SIZE 32
static uint8_t rbuf[RBUF_SIZE];
static uint8_t rbuf_head = 0;
static uint8_t rbuf_tail = 0;
static inline void rbuf_enqueue(uint8_t data)
{
    uint8_t sreg = SREG;
    cli();
    uint8_t next = (rbuf_head + 1) % RBUF_SIZE;
    if (next != rbuf_tail) {
        rbuf[rbuf_head] = data;
        rbuf_head = next;
    } else {
        print("rbuf: full\n");
    }
    SREG = sreg;
}
static inline uint8_t rbuf_dequeue(void)
{
    uint8_t val = 0;

    uint8_t sreg = SREG;
    cli();
    if (rbuf_head != rbuf_tail) {
        val = rbuf[rbuf_tail];
        rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE;
    }
    SREG = sreg;

    return val;
}
static inline bool rbuf_has_data(void)
{
    uint8_t sreg = SREG;
    cli();
    bool has_data = (rbuf_head != rbuf_tail);
    SREG = sreg;
    return has_data;
}
static inline void rbuf_clear(void)
{
    uint8_t sreg = SREG;
    cli();
    rbuf_head = rbuf_tail = 0;
    SREG = sreg;
}

#endif  /* RING_BUFFER_H */
p">{ NULL, GPIOB, 12, SPI_CR1_BR_2 | SPI_CR1_BR_1, SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0 }; /* * SPI TX and RX buffers. */ static uint8_t txbuf[512]; static uint8_t rxbuf[512]; /* * SPI bus contender 1. */ static THD_WORKING_AREA(spi_thread_1_wa, 256); static THD_FUNCTION(spi_thread_1, p) { (void)p; chRegSetThreadName("SPI thread 1"); while (true) { spiAcquireBus(&SPID2); /* Acquire ownership of the bus. */ palSetPad(GPIOC, GPIOC_LED4); /* LED ON. */ spiStart(&SPID2, &hs_spicfg); /* Setup transfer parameters. */ spiSelect(&SPID2); /* Slave Select assertion. */ spiExchange(&SPID2, 512, txbuf, rxbuf); /* Atomic transfer operations. */ spiUnselect(&SPID2); /* Slave Select de-assertion. */ spiReleaseBus(&SPID2); /* Ownership release. */ } } /* * SPI bus contender 2. */ static THD_WORKING_AREA(spi_thread_2_wa, 256); static THD_FUNCTION(spi_thread_2, p) { (void)p; chRegSetThreadName("SPI thread 2"); while (true) { spiAcquireBus(&SPID2); /* Acquire ownership of the bus. */ palClearPad(GPIOC, GPIOC_LED4); /* LED OFF. */ spiStart(&SPID2, &ls_spicfg); /* Setup transfer parameters. */ spiSelect(&SPID2); /* Slave Select assertion. */ spiExchange(&SPID2, 512, txbuf, rxbuf); /* Atomic transfer operations. */ spiUnselect(&SPID2); /* Slave Select de-assertion. */ spiReleaseBus(&SPID2); /* Ownership release. */ } } /* * This is a periodic thread that does absolutely nothing except flashing * a LED. */ static THD_WORKING_AREA(blinker_wa, 128); static THD_FUNCTION(blinker, arg) { (void)arg; chRegSetThreadName("blinker"); while (true) { palSetPad(GPIOC, GPIOC_LED3); chThdSleepMilliseconds(500); palClearPad(GPIOC, GPIOC_LED3); chThdSleepMilliseconds(500); } } /* * Application entry point. */ int main(void) { unsigned i; /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. */ halInit(); chSysInit(); /* * SPI2 I/O pins setup. */ palSetPadMode(GPIOB, 13, PAL_MODE_ALTERNATE(0) | PAL_STM32_OSPEED_HIGHEST); /* New SCK. */ palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(0) | PAL_STM32_OSPEED_HIGHEST); /* New MISO. */ palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(0) | PAL_STM32_OSPEED_HIGHEST); /* New MOSI. */ palSetPad(GPIOB, 12); palSetPadMode(GPIOB, 12, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); /* New CS. */ /* * Prepare transmit pattern. */ for (i = 0; i < sizeof(txbuf); i++) txbuf[i] = (uint8_t)i; /* * Starting the transmitter and receiver threads. */ chThdCreateStatic(spi_thread_1_wa, sizeof(spi_thread_1_wa), NORMALPRIO + 1, spi_thread_1, NULL); chThdCreateStatic(spi_thread_2_wa, sizeof(spi_thread_2_wa), NORMALPRIO + 1, spi_thread_2, NULL); /* * Starting the blinker thread. */ chThdCreateStatic(blinker_wa, sizeof(blinker_wa), NORMALPRIO-1, blinker, NULL); /* * Normal main() thread activity, in this demo it does nothing. */ while (true) { chThdSleepMilliseconds(500); } return 0; }