Giter Site home page Giter Site logo

bb_spi-tle5010-74hc165's Introduction

bb_spi-tle5010-74hc165

bitband spi for parallel load from 8 tle5010 and 6 shift registers 74hc165

Main idea is to read all connected devices at the same time, so all here we do not use standart SPI peripherals but connect all MOSI lines to uc pins, BB serial clocks and read all data from port at the same time.

Usual SPI read(not IT and DMA) take ~3500cycles, that implementation use 6 500 - 10 000cycles to read 9 devices(6 shift registers on one pin and 8 tle5010 each on theyr own pins). So, much faster sensors pooling can be done.

dont forget to properly initialize shift register sample pin as output, and its data pin as input.

typedef struct _raw_axis {
    int16_t sn;
    int16_t cs;
    uint8_t crc;
} raw_axis;

uint8_t * const axis1 = (uint8_t*)&raw_axises[0];
uint8_t * const axis2 = (uint8_t*)&raw_axises[1];
uint8_t * const axis3 = (uint8_t*)&raw_axises[2];
uint8_t * const axis4 = (uint8_t*)&raw_axises[3];
uint8_t * const axis5 = (uint8_t*)&raw_axises[4];
uint8_t * const axis6 = (uint8_t*)&raw_axises[5];
uint8_t * const axis7 = (uint8_t*)&raw_axises[6];
uint8_t * const axis8 = (uint8_t*)&raw_axises[7];

raw_axis raw_axises[8];
uint8_t  raw_buttons[6];



void spi_enable(void)
{
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET);
}

void spi_disable(void)
{
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET);
}


void tle_set_input(void) {
    GPIO_InitStruct.Pin = all_tle_data_pins;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(spi_port, &GPIO_InitStruct);
}

void tle_set_output(void) {
    GPIO_InitStruct.Pin = all_tle_data_pins;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(spi_port, &GPIO_InitStruct);
}




while(1)
{

            tle_set_output();

            HAL_GPIO_WritePin(spi_port, all_tle_data_pins, GPIO_PIN_RESET);
            HAL_GPIO_WritePin(spi_port, clk_pin, GPIO_PIN_RESET);

            spi_enable();

            for (uint8_t i = 0; i < 8; i++) {//cmd[0] = 0x00;
                HAL_GPIO_WritePin(spi_port, clk_pin, GPIO_PIN_SET);
                HAL_GPIO_WritePin(spi_port, clk_pin, GPIO_PIN_RESET);
            }



            //sample reg
            HAL_GPIO_WritePin(spi_port, reg_sample_pin, GPIO_PIN_SET);
            HAL_GPIO_WritePin(spi_port, reg_sample_pin, GPIO_PIN_RESET);
            HAL_GPIO_WritePin(spi_port, reg_sample_pin, GPIO_PIN_SET);

            const uint8_t cmd  = 0x8C;
            uint8_t btns0 = 0;
            for (uint8_t i = 0; i < 8; i++) {

                if(spi_port->IDR & reg_data_pin) btns0 |= 1 << i;

                HAL_GPIO_WritePin(spi_port, all_tle_data_pins, ((cmd << i) & 0b10000000) );

                HAL_GPIO_WritePin(spi_port, clk_pin, GPIO_PIN_SET);
                HAL_GPIO_WritePin(spi_port, clk_pin, GPIO_PIN_RESET);

                
            }


            tle_set_input();

            uint16_t bits;
            for(uint8_t b = 0; b < 5; b++) {

                axis1[b] = 0 ;
                axis2[b] = 0 ;
                axis3[b] = 0 ;
                axis4[b] = 0 ;
                axis5[b] = 0 ;
                axis6[b] = 0 ;
                axis7[b] = 0 ;
                axis8[b] = 0 ;
                raw_buttons[b] = 0 ;

                for (uint8_t i = 0; i < 8; i++) {

                    if(HAL_GPIO_ReadPin(spi_port, reg_data_pin)) raw_buttons[b] |= 1 << i;

                    HAL_GPIO_WritePin(spi_port, clk_pin, GPIO_PIN_SET);
                    HAL_GPIO_WritePin(spi_port, clk_pin, GPIO_PIN_RESET);

                    bits = spi_port->IDR & (all_tle_data_pins);

                    if(bits & tle_data_pins[0]) axis1[b] |= (0b10000000 >> i);
                    if(bits & tle_data_pins[1]) axis2[b] |= (0b10000000 >> i);
                    if(bits & tle_data_pins[2]) axis3[b] |= (0b10000000 >> i);
                    if(bits & tle_data_pins[3]) axis4[b] |= (0b10000000 >> i);
                    if(bits & tle_data_pins[4]) axis5[b] |= (0b10000000 >> i);
                    if(bits & tle_data_pins[5]) axis6[b] |= (0b10000000 >> i);
                    if(bits & tle_data_pins[6]) axis7[b] |= (0b10000000 >> i);
                    if(bits & tle_data_pins[7]) axis8[b] |= (0b10000000 >> i);

                    if(bits & reg_data_pin) raw_buttons[b] |= 1 << i;


                }
            }

            raw_buttons[5] = btns0;


            spi_disable();
}

bb_spi-tle5010-74hc165's People

Contributors

l3vgv avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.