Skip to content

Instantly share code, notes, and snippets.

@aw

aw/sram.rs

Created Jan 17, 2021
Embed
What would you like to do?
Rust embedded driver for Microchip 23x SRAM/NVSRAM
#![no_std]
#![no_main]
use panic_halt as _;
use embedded_graphics::fonts::{Font12x16, Text};
use embedded_graphics::pixelcolor::Rgb565;
use embedded_graphics::prelude::*;
use embedded_graphics::primitives::Rectangle;
use embedded_graphics::{primitive_style, text_style};
use gd32vf103xx_hal::pac;
use gd32vf103xx_hal::prelude::*;
use longan_nano::{lcd, lcd_pins};
use riscv_rt::entry;
use gd32vf103xx_hal::spi::{Spi, MODE_0};
use sram23x::*;
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
// Configure clocks
let mut rcu = dp
.RCU
.configure()
.ext_hf_clock(8.mhz())
.sysclk(108.mhz())
.freeze();
let mut afio = dp.AFIO.constrain(&mut rcu);
let gpioa = dp.GPIOA.split(&mut rcu);
let gpiob = dp.GPIOB.split(&mut rcu);
let lcd_pins = lcd_pins!(gpioa, gpiob);
let mut lcd = lcd::configure(dp.SPI0, lcd_pins, &mut afio, &mut rcu);
let (width, height) = (lcd.size().width as i32, lcd.size().height as i32);
// SRAM pins
let sck = gpiob.pb13.into_alternate_push_pull();
let miso = gpiob.pb14.into_floating_input();
let mosi = gpiob.pb15.into_alternate_push_pull();
let spi1 = Spi::spi1(dp.SPI1, (sck, miso, mosi), MODE_0, 4.mhz(), &mut rcu);
let hold = gpioa.pa0.into_push_pull_output();
let cs = gpioa.pa3.into_push_pull_output();
// Write 'abcdABCD' to SRAM at memory address 0x00
let mut sram = Sram23x::new(spi1, cs, hold, device_type::M23xv1024).unwrap();
sram.set_mode(OperatingMode::Sequential as u8).unwrap();
let mut data: [u8; 8] = ['a' as u8, 'b' as u8, 'c' as u8, 'd' as u8, 65, 66, 67, 68];
sram.write_sequential(0x00_u32, &mut data).unwrap();
// Read from SRAM at memory address 0x00
sram.read_sequential(0x00_u32, &mut data).unwrap();
// Convert the last 4 bytes into an str, to be displayed on the LCD
let sramstr = core::str::from_utf8(&data[4..]).unwrap();
// Clear screen
Rectangle::new(Point::new(0, 0), Point::new(width - 1, height - 1))
.into_styled(primitive_style!(fill_color = Rgb565::BLACK))
.draw(&mut lcd)
.unwrap();
let style = text_style!(font=Font12x16, text_color=Rgb565::YELLOW, background_color=Rgb565::BLACK);
// Create a text at position (20, 30) and draw it using style defined above
Text::new(&sramstr, Point::new(5, 5))
.into_styled(style)
.draw(&mut lcd)
.unwrap();
loop {}
}
@aw

This comment has been minimized.

Copy link
Owner Author

@aw aw commented Jan 17, 2021

Add this file to longan nano examples/sram.rs to build and flash this sram firmware.

Build with cargo build --example sram --features=lcd --release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment