Quantcast
Channel: LinkSprite Learning Center
Viewing all articles
Browse latest Browse all 562

Sliding rheostat

$
0
0

This demo will read the voltage of sliding rheostat and show the value on the terminal.

  1. 96board
  2. Liner Base Mezzanine Card
  3. Sliding rheostat module of Linker Kit
  4. 4-pin cable

Connect 

Connect the sliding rheostat module to ADC1 port on Linker Base Mezzanine Card.

5

 

 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <linux/spi/spidev.h>
#include <linux/types.h>
#include <inttypes.h>

#include "libsoc_spi.h"
#include "libsoc_gpio.h"
#include "libsoc_debug.h"

#define GPIO_CS 18

static uint8_t tx[3],rx[3];

int main()
{
    int adc_value;
    gpio *gpio_cs;
    libsoc_set_debug(0);
    gpio_cs = libsoc_gpio_request(GPIO_CS,LS_SHARED);
    if(gpio_cs == NULL)
    {
	goto fail;
    }
    libsoc_gpio_set_direction(gpio_cs,OUTPUT);
    if(libsoc_gpio_get_direction(gpio_cs) != OUTPUT)
    {
	printf("Failed to set direction to OUTPUT!\n");
	goto fail;	
    }
    spi *spi_dev = libsoc_spi_init(32766,0);   
    
   if(!spi_dev)
   {
	printf("Failed to get spidev device!\n");
   	return EXIT_FAILURE;
   }
   libsoc_spi_set_mode(spi_dev,MODE_0);
   libsoc_spi_get_mode(spi_dev);
   libsoc_spi_set_speed(spi_dev,10000);
   libsoc_spi_get_speed(spi_dev);
   libsoc_spi_set_bits_per_word(spi_dev,BITS_8);
   libsoc_spi_get_bits_per_word(spi_dev);

   tx[0] = 0x01;
   tx[1] = 0x80;
   tx[2] = 0x00;
   while(1)
   {
	libsoc_gpio_set_level(gpio_cs,HIGH);
	usleep(10);
	libsoc_gpio_set_level(gpio_cs,LOW);
     	libsoc_spi_rw(spi_dev,tx,rx,3);
   	libsoc_gpio_set_level(gpio_cs,HIGH);

	adc_value = (rx[1]<<8)&0b1100000000;
	adc_value |= (rx[2]&0xff);

	printf("adc_value:%d\n",adc_value);
	sleep(1);
   }
   free:
   libsoc_spi_free(spi_dev);
   fail:
   if(gpio_cs)
   {
	libsoc_gpio_free(gpio_cs);
   }
   return EXIT_SUCCESS;
}

Introduce the functions in source code

Function:spi* libsoc_spi_init (uint16_t spidev_device, uint8_t chip_select);

Description:brief opens the spidev character device and intitialises a new spi struct

param uint8_t spidev_device – the major spidev number

param uint8_t spidev_device – the minor spidev number

return spi* struct pointer or NULL on failure

 

Function:int libsoc_spi_set_mode(spi* spi, spi_mode mode);

Description:fn int libsoc_spi_set_mode(spi* spi, spi_mode mode)

brief sets the spi mode for the spi device

param spi* spi – valid spi struct pointer

param enum spi_mode – valid modes are as follows:-MODE_0, MODE_1, MODE_2,    MODE_3

Essential Reading:

– https://www.kernel.org/doc/Documentation/spi/spi-summary

– http://www.diolan.com/dln_doc/spi-transfer-modes.html

– http://blackfin.uclinux.org/doku.php?id=spi

return EXIT_SUCCESS or EXIT_FAILURE

 

Function:spi_mode libsoc_spi_get_mode(spi* spi);

Description:brief gets the current mode of the spi bus

param spi* spi – valid spi struct pointer

return enum spi_mode – MODE_0/1/2/3 on success, MODE_ERROR on fail

 

Function:uint32_t libsoc_spi_get_speed(spi* spi);

Description:brief gets the current speed of the spi bus

param spi* spi – valid spi struct pointer

return uint32 – current speed of spi bus in Hz

 

Function:uint32_t libsoc_spi_get_speed(spi* spi);

Description:brief gets the current speed of the spi bus

param spi* spi – valid spi struct pointer

return uint32 – current speed of spi bus in Hz

 

Function:int libsoc_spi_set_bits_per_word(spi* spi, spi_bpw bpw);

Description:brief sets the bits per word of the spi transfer, either 8 or 16

param spi* spi – valid spi struct pointer

param enum spi_bpw – bits per word eith BITS_8 or BITS_16

return EXIT_SUCCESS or EXIT_FAILURE

 

Function:spi_bpw libsoc_spi_get_bits_per_word(spi* spi);

Description:brief gets the current bits per word of the spi bus

param spi* spi – valid spi struct pointer

return enum spi_bpw – BITS_8/16 on success, BITS_ERROR on fail

 

Function:int libsoc_spi_rw(spi* spi, uint8_t* tx, uint8_t* rx, uint32_t len);

Description:brief duplex read/write to the spi bus, allows writing data and then reading back in one  single transaction, or writing and reading at the same time.

param spi* spi – valid spi struct pointer

param uint8_t* tx – array of bytes to send on the bus

param uint8_t* rx – array of bytes to populate with data from the bus param uint32_t len –  the length of the transfer in bytes

return EXIT_SUCCESS or EXIT_FAILURE

 

Function:int libsoc_spi_free(spi* spi);

Description:brief frees the malloced spi struct

param spi* spi – valid spi struct pointer

return EXIT_SUCCESS or EXIT_FAILURE

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

gcc spi_test.c -o spi_test -lsoc
sudo ./spi_test

On the terminal, it will print the adc value reading from the voltage of sliding rheostat.

5-1

 


Viewing all articles
Browse latest Browse all 562

Trending Articles