Skip to content

Instantly share code, notes, and snippets.

@pepijndevos
Last active May 3, 2020 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pepijndevos/b2a0b1088fdb504a0e2fbaee95d97e68 to your computer and use it in GitHub Desktop.
Save pepijndevos/b2a0b1088fdb504a0e2fbaee95d97e68 to your computer and use it in GitHub Desktop.
ILI9341 program for STM32F4DISCOVERY that draws a circle, slowly.
#![no_main]
#![no_std]
use panic_halt as _;
use stm32f407g_disc as board;
use crate::board::{
hal::stm32,
hal::spi::{Mode, Phase, Polarity, Spi},
hal::{delay::Delay, prelude::*},
};
use cortex_m::peripheral::Peripherals;
use cortex_m_rt::entry;
use embedded_graphics::{
fonts::{Font8x16, Text},
pixelcolor::Rgb565,
prelude::*,
primitives::{Circle, Rectangle},
style::{PrimitiveStyle, TextStyle},
};
use ili9341::{Ili9341, Orientation};
#[entry]
fn main() -> ! {
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
let rcc = p.RCC.constrain();
let gpioa = p.GPIOA.split();
let clocks = rcc
.cfgr
.use_hse(8.mhz())
.sysclk(48.mhz())
.pclk1(24.mhz())
.freeze();
// Configure pins for SPI
let sck = gpioa.pa5.into_alternate_af5();
let miso = gpioa.pa6.into_alternate_af5();
let mosi = gpioa.pa7.into_alternate_af5();
let spi_mode = Mode {
polarity: Polarity::IdleLow,
phase: Phase::CaptureOnFirstTransition,
};
let spi = Spi::spi1(
p.SPI1,
(sck, miso, mosi),
spi_mode,
16.mhz().into(),
clocks,
);
// Get delay provider
let mut delay = Delay::new(cp.SYST, clocks);
let mut display = Ili9341::new_spi(
spi,
gpioa.pa4.into_push_pull_output(),
gpioa.pa3.into_push_pull_output(),
gpioa.pa2.into_push_pull_output(),
&mut delay
).expect("display init failed");
display.set_orientation(Orientation::LandscapeFlipped);
let c = Circle::new(Point::new(20, 20), 50)
.into_styled(PrimitiveStyle::with_fill(Rgb565::RED));
let t = Text::new("Hello Rust!", Point::new(20, 16))
.into_styled(TextStyle::new(Font8x16, Rgb565::GREEN));
let r = Rectangle::new(Point::new(0, 0), Point::new(320, 240))
.into_styled(PrimitiveStyle::with_fill(Rgb565::BLACK));
r.draw(&mut display).expect("draw failed");
c.draw(&mut display).expect("draw failed");
t.draw(&mut display).expect("draw failed");
}
loop {
continue;
}
}
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
// For the Adafruit shield, these are the default.
#define TFT_DC 9
#define TFT_CS 10
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// If using the breakout, change pins as desired
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
void setup() {
Serial.begin(9600);
Serial.println("ILI9341 Test!");
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.fillCircle(20, 20, 50, ILI9341_RED);
tft.setCursor(20, 16);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println("Hello Arduino");
}
void loop(void) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment