1 | #include <asf.h>
|
2 | #include "sam_gpio\sam_gpio.h"
|
3 |
|
4 | void init_spi(void)
|
5 | {
|
6 |
|
7 | spi_flags_t spi_flags = SPI_MODE; // MODE 1, 100KHz
|
8 | board_spi_select_id_t spi_select_id = SPI_DEVICE_ID;
|
9 | spi_master_init(SPI0);
|
10 | spi_master_setup_device(SPI0, &device, spi_flags, SPI_SPEED, spi_select_id);
|
11 | spi_enable(SPI0);
|
12 | }
|
13 |
|
14 | void send_data(uint8_t Register, uint8_t Data, Pio *pinlocation, uint32_t pin)
|
15 | {
|
16 | uint8_t command[2] = {Register,Data};
|
17 | pio_clear(pinlocation,pin);// Clear CS Pin
|
18 | spi_write_packet(SPI0, command, sizeof(command));
|
19 | while(!(SPI0->SPI_SR & SPI_SR_TXEMPTY));
|
20 | pio_set(pinlocation,pin);// Set CS Pin
|
21 | }
|
22 |
|
23 | uint16_t receive_data_16bit(uint8_t address, Pio *pinlocation, uint32_t pin)
|
24 | {
|
25 | uint8_t rx[2] = {0,0};
|
26 | pio_clear(pinlocation,pin);// Clear CS Pin
|
27 | spi_write_packet(SPI0, &address, sizeof(address));
|
28 | while(!(SPI0->SPI_SR & SPI_SR_TXEMPTY));// Wait till transfer is done
|
29 | spi_read_packet(SPI0,rx,sizeof(rx));
|
30 | pio_set(pinlocation,pin);// Set CS Pin
|
31 | return ((uint16_t)rx[0]<<8) | rx[1];// Return shifted Value (MSB first)
|
32 | }
|
33 |
|
34 | int main (void)
|
35 | {
|
36 | board_init();
|
37 | sysclk_init();
|
38 | pmc_enable_periph_clk(ID_PIOA);// Enable GPIO Clock
|
39 | pio_set_output(PIOA, PIO_PA11, HIGH, DISABLE, ENABLE);// Set CS Pin to output and high
|
40 | pio_set_input(PIOA,PIO_PA12,IOPORT_MODE_PULLDOWN);// Set MISO as input
|
41 | init_spi();
|
42 | uint16_t rx=0;
|
43 | while (1) {
|
44 | delay_us(10);
|
45 | send_data(0x80,0x01, PIOA, PIO_PA11);// Send 50Hz filter to config Register
|
46 | delay_us(10);
|
47 | rx = receive_data_16bit(0x00, PIOA, PIO_PA11);// Read config Register
|
48 | rx = receive_data_16bit(0x03, PIOA, PIO_PA11);// Read High Fault Threshold Register
|
49 | }
|
50 | }
|