Skip to content
This repository has been archived by the owner on Jul 6, 2019. It is now read-only.

SPI on STM32F4 + example #343

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions examples/spi_stm32f4/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "spi_stm32f4"
version = "0.0.1"

[features]
default = ["mcu_stm32f4"]
mcu_stm32f4 = ["zinc/mcu_stm32f4"]

[dependencies]
zinc = { path = "../.." }
macro_zinc = { path = "../../macro_zinc" }
core = { git = "https://github.com/hackndev/rust-libcore" }
82 changes: 82 additions & 0 deletions examples/spi_stm32f4/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#![feature(core_intrinsics)]
#![feature(plugin, no_std)]
#![no_std]
#![plugin(macro_zinc)]


extern crate zinc;

use zinc::hal::timer::Timer;
use zinc::hal::pin::Gpio;
use zinc::hal::stm32f4::{pin, timer, spi};
use zinc::hal::spi::Spi;
use core::intrinsics::abort;

#[zinc_main]
pub fn main() {
zinc::hal::mem_init::init_stack();
zinc::hal::mem_init::init_data();

let led1 = pin::Pin::new (
pin::Port::PortD,
13u8,
pin::Mode::GpioOut(pin::OutputType::OutPushPull, pin::Speed::VeryLow),
pin::PullType::PullNone
);

let spi1Sck = pin::Pin::new (
pin::Port::PortA,
5u8,
pin::Mode::AltFunction(pin::AltMode::AfSpi1_Spi2, pin::OutputType::OutPushPull, pin::Speed::High),
pin::PullType::PullNone
);

let spi1Miso = pin::Pin::new (
pin::Port::PortA,
6u8,
pin::Mode::AltFunction(pin::AltMode::AfSpi1_Spi2, pin::OutputType::OutPushPull, pin::Speed::High),
pin::PullType::PullNone
);

let spi1Mosi = pin::Pin::new (
pin::Port::PortA,
7u8,
pin::Mode::AltFunction(pin::AltMode::AfSpi1_Spi2, pin::OutputType::OutPushPull, pin::Speed::High),
pin::PullType::PullNone
);

let spi1Ss = pin::Pin::new(
pin::Port::PortA,
4u8,
pin::Mode::GpioOut(pin::OutputType::OutPushPull, pin::Speed::VeryLow),
pin::PullType::PullNone
);

let timer = timer::Timer::new(timer::TimerPeripheral::Timer2, 25u32);

let spi1 = match spi::Spi::new(
spi::Peripheral::Spi1,
spi::Direction::FullDuplex,
spi::Role::Master,
spi::DataSize::U8,
spi::DataFormat::MsbFirst,
6) {
Ok(e) => e,
_ => unsafe {abort ()},
};

spi1Ss.set_high();

loop {
led1.set_high();
spi1Ss.set_low();
spi1.write('H' as u8);
spi1.write('e' as u8);
spi1.write('l' as u8);
spi1.write('l' as u8);
spi1.write('o' as u8);
spi1.write('!' as u8);
//led1.set_low();
//spi1Ss.set_high();
}
}
4 changes: 3 additions & 1 deletion src/hal/layout_common.ld
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
__aeabi_unwind_cpp_pr0 = abort;
__aeabi_unwind_cpp_pr1 = abort;
__aeabi_unwind_cpp_pr2 = abort;
__exidx_start = abort;
__exidx_end = abort;

__aeabi_memclr4 = __aeabi_memclr;

Expand Down Expand Up @@ -60,6 +62,6 @@ SECTIONS
*(.rel.*) /* dynamic relocations */
*(.ARM.exidx*) /* index entries for section unwinding */
*(.ARM.extab*) /* exception unwinding information */
*(.debug_gdb_scripts)
/**(.debug_gdb_scripts)*/
}
}
7 changes: 7 additions & 0 deletions src/hal/stm32f4/iomem.ld
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ stm32f4_iomem_GPIOF = 0x40021400;
stm32f4_iomem_GPIOG = 0x40021800;
stm32f4_iomem_GPIOH = 0x40021c00;
stm32f4_iomem_GPIOI = 0x40022000;

stm32f4_iomem_SPI1 = 0x40013000;
stm32f4_iomem_SPI2 = 0x40003800;
stm32f4_iomem_SPI3 = 0x40003C00;
stm32f4_iomem_SPI4 = 0x40013400;
stm32f4_iomem_SPI5 = 0x40015000;
stm32f4_iomem_SPI6 = 0x40015400;
2 changes: 2 additions & 0 deletions src/hal/stm32f4/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ pub mod init;
pub mod peripheral_clock;
pub mod pin;
pub mod timer;
#[allow(missing_docs)]
pub mod spi;
2 changes: 1 addition & 1 deletion src/hal/stm32f4/peripheral_clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl PeripheralClock {
SDIOClock|SPI1Clock|SYSCFGClock|TIM9Clock|TIM10Clock|
TIM11Clock => {
let val = reg::RCC.APB2ENR();
reg::RCC.set_AHB1ENR((val & mask) | bit);
reg::RCC.set_APB2ENR((val & mask) | bit);
},
}

Expand Down
Loading