pikeyd165 – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 // spiram.c
2 //
3 // Little library for accessing SPI RAM such as 23K256-I/P
4 // using bcm2835 library on Raspberry Pi
5 //
6 // Author: Mike McCauley
7 // Copyright (C) 2018 Mike McCauley
8 // This software is part of the bcm2835 library and is licensed under the same conditions
9 // $Id: $
10  
11 #include <bcm2835.h>
12 #include <string.h> // memcpy
13 #include "spiram.h"
14  
15 static uint8_t _mode = SPIRAM_MODE_INVALID;
16  
17 uint8_t spiram_read_sr()
18 {
19 uint8_t command[] = { SPIRAM_OPCODE_READ_SR, 0};
20 bcm2835_spi_transfern(command, sizeof(command));
21 return command[1];
22 }
23  
24 bool spiram_write_sr(uint8_t value)
25 {
26 uint8_t command[] = { SPIRAM_OPCODE_WRITE_SR, value};
27 bcm2835_spi_transfern(command, sizeof(command));
28 return true;
29 }
30  
31 bool spiram_set_mode(uint8_t mode)
32 {
33 if (mode != _mode)
34 {
35 spiram_write_sr(mode);
36 _mode = mode;
37 }
38 return true;
39 }
40  
41 bool spiram_begin()
42 {
43 _mode = SPIRAM_MODE_BYTE;
44  
45 bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default
46 bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default
47 bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default
48 bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
49 bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
50 return true;
51 }
52  
53 bool spiram_end()
54 {
55 bcm2835_spi_end();
56 return true;
57 }
58  
59  
60 uint8_t spiram_read_byte(uint16_t address)
61 {
62 spiram_set_mode(SPIRAM_MODE_BYTE);
63 uint8_t command[] = { SPIRAM_OPCODE_READ, (address >> 8) & 0xff, address & 0xff, 0xff };
64 bcm2835_spi_transfern(command, sizeof(command));
65 uint8_t ret = command[3];
66 }
67  
68 bool spiram_write_byte(uint16_t address, uint8_t value)
69 {
70 spiram_set_mode(SPIRAM_MODE_BYTE);
71 uint8_t command[] = { SPIRAM_OPCODE_WRITE, (address >> 8) & 0xff, address & 0xff, value };
72 bcm2835_spi_writenb(command, sizeof(command));
73 return true;
74 }
75  
76 bool spiram_read_page(uint16_t address, uint8_t *buf)
77 {
78 spiram_set_mode(SPIRAM_MODE_PAGE);
79 uint8_t command[3 + SPIRAM_PAGE_SIZE] = { SPIRAM_OPCODE_READ, (address >> 8) & 0xff, address & 0xff };
80 bcm2835_spi_transfern(command, sizeof(command));
81 memcpy(buf, command + 3, SPIRAM_PAGE_SIZE);
82 return true;
83 }
84  
85 bool spiram_write_page(uint16_t address, uint8_t *buf)
86 {
87 spiram_set_mode(SPIRAM_MODE_PAGE);
88 uint8_t command[3 + SPIRAM_PAGE_SIZE] = { SPIRAM_OPCODE_WRITE, (address >> 8) & 0xff, address & 0xff };
89 memcpy(command + 3, buf, SPIRAM_PAGE_SIZE);;
90 bcm2835_spi_writenb(command, sizeof(command));
91 return true;
92 }