pikeyd165 – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | // spiram_test.c |
2 | // |
||
3 | // Example program for bcm2835 |
||
4 | // Shows how to interface with SPI RAM such as 23K256-I/P |
||
5 | // using the spiram little library |
||
6 | // |
||
7 | // Tested on RPI 3 Model B, Raspbian Jessie |
||
8 | // Tested at full speed over many hours with no errors. |
||
9 | // |
||
10 | // Connect RPi 3 B to 23K256-I/P like this: |
||
11 | // RPi pin Function 23K256-I/P pin (name) |
||
12 | // J1-6 GND 4 (VSS) |
||
13 | // J1-1 3.3V 8 (VCC) |
||
14 | // and 7 (/HOLD) |
||
15 | // J1-19 SPI0_MOSI 5 (SI) |
||
16 | // J1-21 SPI0_MISO 2 (SO) |
||
17 | // J1-23 SPI0_SCLK 6 (SCK) |
||
18 | // J1-24 SPI0_CE0_N 1 (/CS) |
||
19 | // |
||
20 | // After installing bcm2835, you can build this |
||
21 | // with something like: |
||
22 | // gcc -o spiram_test spiram.c spiram_test.c -l bcm2835 |
||
23 | // sudo ./spiram_test |
||
24 | // |
||
25 | // Or you can test it before installing with: |
||
26 | // gcc -o spiram_test -I ../../src ../../src/bcm2835.c spiram.c spiram_test.c |
||
27 | // sudo ./spiram_test |
||
28 | // |
||
29 | // Author: Mike McCauley |
||
30 | // Copyright (C) 2018 Mike McCauley |
||
31 | // $Id: $ |
||
32 | |||
33 | #include <bcm2835.h> |
||
34 | #include <stdio.h> |
||
35 | #include <string.h> // memcmp |
||
36 | #include "spiram.h" |
||
37 | |||
38 | int main(int argc, char **argv) |
||
39 | { |
||
40 | if (!bcm2835_init()) |
||
41 | { |
||
42 | printf("bcm2835_init failed. Are you running as root??\n"); |
||
43 | return 1; |
||
44 | } |
||
45 | |||
46 | if (!bcm2835_spi_begin()) |
||
47 | { |
||
48 | printf("bcm2835_spi_begin failed. Are you running as root??\n"); |
||
49 | return 1; |
||
50 | } |
||
51 | if (!spiram_begin()) |
||
52 | { |
||
53 | printf("spiram_begin failed.\n"); |
||
54 | return 1; |
||
55 | } |
||
56 | /* You can speed things up by selecting a faster SPI speed |
||
57 | // after spiram_begin, which defaults to BCM2835_SPI_CLOCK_DIVIDER_65536 = 6.1035156kHz on RPI3 |
||
58 | */ |
||
59 | bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64); // 6.25MHz on RPI3 |
||
60 | |||
61 | uint8_t value = 0; |
||
62 | uint16_t address = 0x0000; |
||
63 | while (1) |
||
64 | { |
||
65 | uint8_t ret; |
||
66 | |||
67 | /* ret = spiram_read_sr();*/ |
||
68 | spiram_write_byte(address, value); |
||
69 | ret = spiram_read_byte(address); |
||
70 | if (ret != value) |
||
71 | printf("ERROR: spiram_read_byte address %04x got %02x, expected %02x\n", address, ret, value); |
||
72 | #if 0 |
||
73 | printf("spiram_read_byte at address %04x got %02x\n", address, ret); |
||
74 | #endif |
||
75 | |||
76 | uint8_t write_page_buf[SPIRAM_PAGE_SIZE] = { 0, value, value, value }; |
||
77 | uint8_t read_page_buf[SPIRAM_PAGE_SIZE]; |
||
78 | spiram_write_page(address, write_page_buf); |
||
79 | |||
80 | spiram_read_page(address, read_page_buf); |
||
81 | if (memcmp(write_page_buf, read_page_buf, SPIRAM_PAGE_SIZE) != 0) |
||
82 | printf("ERROR: spiram_read_page at address %04x\n", address); |
||
83 | #if 0 |
||
84 | printf("spiram_read_page address %04x got ", address); |
||
85 | int i; |
||
86 | for (i = 0; i < SPIRAM_PAGE_SIZE; i++) |
||
87 | printf("%02x ", read_page_buf[i]); |
||
88 | printf("\n"); |
||
89 | #endif |
||
90 | /* sleep(1); */ |
||
91 | value++; |
||
92 | address++; |
||
93 | } |
||
94 | |||
95 | spiram_end(); |
||
96 | bcm2835_close(); |
||
97 | return 0; |
||
98 | } |
||
99 |