Rev Author Line No. Line
1109 kaklik 1 #include <libopencm3/stm32/f1/rcc.h>
2 #include <libopencm3/stm32/f1/gpio.h>
3 #include <libopencm3/stm32/spi.h>
4 #include <libopencm3/stm32/usart.h>
5  
6 #include <stdio.h>
7 #include <errno.h>
8  
9 void gpio_setup(void)
10 {
11 rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPAEN);
12  
13 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
14 GPIO_CNF_OUTPUT_PUSHPULL, GPIO7);
15 }
16  
17 void usart_setup()
18 {
19 rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN
20 | RCC_APB2ENR_AFIOEN | RCC_APB2ENR_USART1EN);
21  
22 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
23  
24 usart_set_baudrate(USART1, 57600);
25 usart_set_databits(USART1, 8);
26 usart_set_stopbits(USART1, USART_STOPBITS_1);
27 usart_set_mode(USART1, USART_MODE_TX);
28 usart_set_parity(USART1, USART_PARITY_NONE);
29 usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
30  
31 usart_enable(USART1);
32 }
33  
34 void spi_setup()
35 {
36 rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_SPI1EN);
37 rcc_peripheral_enable_clock(&RCC_APB2ENR,
38 RCC_APB2ENR_AFIOEN | RCC_APB2ENR_IOPAEN);
39  
40 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
41 GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO5 | GPIO7);
42 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
43 GPIO_CNF_OUTPUT_PUSHPULL, GPIO4);
44  
45 spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE,
46 SPI_CR1_CPHA_CLK_TRANSITION_2, SPI_CR1_DFF_8BIT, SPI_CR1_MSBFIRST);
47 spi_enable_software_slave_management(SPI1);
48 spi_set_nss_high(SPI1);
49  
50 spi_enable(SPI1);
51 }
52  
53 int _write(int file, char *ptr, int len)
54 {
55 int i;
56  
57 if (file == 1) {
58 for (i = 0; i < len; i++) {
59 usart_send_blocking(USART1, ptr[i]);
60 }
61  
62 return i;
63 }
64  
65 errno = EIO;
66 return -1;
67 }
68  
69 void ncs_set()
70 {
71 gpio_clear(GPIOA, GPIO4);
72 }
73  
74 void ncs_clear()
75 {
76 gpio_set(GPIOA, GPIO4);
77 }
78  
79 uint8_t adns3080_read(uint8_t addr)
80 {
81 int i;
82  
83 ncs_set();
84  
85 for (i = 0; i < 200; i++)
86 __asm__("nop");
87  
88 spi_xfer(SPI1, addr);
89  
90 for (i = 0; i < 200; i++)
91 __asm__("nop");
92  
93 uint8_t val = spi_xfer(SPI1, 0x00);
94  
95 for (i = 0; i < 200; i++)
96 __asm__("nop");
97  
98 ncs_clear();
99  
100 return val;
101 }
102  
103 void adns3080_write(uint8_t addr, uint8_t val)
104 {
105 int i;
106  
107 ncs_set();
108  
109 for (i = 0; i < 200; i++)
110 __asm__("nop");
111  
112 spi_xfer(SPI1, 0x80 | addr);
113  
114 for (i = 0; i < 200; i++)
115 __asm__("nop");
116  
117 spi_xfer(SPI1, val);
118  
119 for (i = 0; i < 200; i++)
120 __asm__("nop");
121  
122 ncs_clear();
123 }
124  
125 int main(void)
126 {
127 int i;
128  
129 rcc_clock_setup_in_hse_8mhz_out_72mhz();
130  
131 setvbuf(stdin, NULL, _IONBF, 0);
132 setvbuf(stdout, NULL, _IONBF, 0);
133  
134 gpio_setup();
135 spi_setup();
136 usart_setup();
137  
138 ncs_clear();
139  
140 for (i = 0; i < 100000; i++)
141 __asm__("nop");
142  
143 while (true) {
144 adns3080_write(0x13, 0x83);
145  
146 for (i = 0; i < 20000; i++)
147 __asm__("nop");
148  
149 usart_send_blocking(USART1, 0xff);
150 usart_send_blocking(USART1, 0xff);
151 usart_send_blocking(USART1, 0xff);
152 usart_send_blocking(USART1, 0xff);
153  
154 for (i = 0; i < 900; i++)
155 usart_send_blocking(USART1, adns3080_read(0x13) & 0x3f);
156  
157 gpio_toggle(GPIOB, GPIO7);
158 }
159  
160 return 0;
161 }