summaryrefslogblamecommitdiff
path: root/src/main.rs
blob: 8de0ba51773ab96b845fec4be3c054d753183279 (plain) (tree)
1
2
3
4
5
6
7
8
9
          
                


                                      


                                                                                           
                                        

          

           
          

                          
                                    


              







                            
                                            
                                        
                                              
                                            
                                    
                                                                
                                             
 








                                       
            
                                                                   






                                                                 


     



















                                                                             

                   
                                 


                                         
                                                                 
                                                                 
                                      

                                             
     

                    

 
                      
                        


                   
















                                             
                                      
                                 

                            
                                           
                                            
                                               


            
                                      

















                                                     
























                                




                                         
 



                                                 

                                      
                                                    

                                                     


                                                      

                                                  

                                        

                                             
 
                                     
                             


                                                

                               










                                                                
 
            


                                             
                                           
                                                   
 
                                                                   
                                                               
                                            
     
 
 






























                                                
                   
 
#![no_std]
#![feature(asm)]

#[macro_use] extern crate stm32f103xx;
extern crate cortex_m;

use stm32f103xx::{GPIOA, GPIOB, RCC, SYST, I2C1, EXTI, NVIC, Interrupt, AFIO, Peripherals};
use stm32f103xx::{gpioa};
use cortex_m::peripheral::SystClkSource;

mod mutex;
mod i2c;
mod ds3231;
mod at24c;

struct ShiftRegister<'a> {
    gpioa: &'a gpioa::RegisterBlock,
    width: u8,
}

struct Clock {
    sec: u8,
    min: u8,
    hr: u8,
    reset: u8
}

const RESET_PERIOD: u8 = 10;
static mut SR: Option<ShiftRegister> = None;
static mut I2C: Option<i2c::I2C> = None;
static mut RTC: Option<ds3231::DS3231> = None;
static mut ROM: Option<at24c::AT24C> = None;
static mut DIGITS: [u8; 6] = [0; 6];
static mut TIME: Clock = Clock{sec: 0, min: 0, hr: 0, reset: 0};
static mut PERIP: Option<Peripherals> = None;

fn digits2bcds(digs: &[u8]) -> u32 {
    let mut res: u32 = 0;
    for d in digs.iter().rev() {
        res = (res << 4) | (*d as u32);
    }
    res
}

fn digits_countup() {
    unsafe {
        SR.as_ref().unwrap().output_bits(digits2bcds(&DIGITS[..]));
        let mut i = 0;
        let mut carry = 1;
        while carry > 0 && i < DIGITS.len() {
            DIGITS[i] += carry;
            carry = if DIGITS[i] > 9 {DIGITS[i] = 0; 1} else {0};
            i += 1;
        }
    }
}

fn refresh_clock() {
    unsafe {
        SR.as_ref().unwrap().output_bits(digits2bcds(&DIGITS[..]));
    }
}

fn render_clock() {
    unsafe {
        if bs {
            DIGITS[1] = TIME.sec / 10; DIGITS[0] = TIME.sec - DIGITS[1] * 10;
            DIGITS[3] = TIME.min / 10; DIGITS[2] = TIME.min - DIGITS[3] * 10;
            DIGITS[5] = TIME.hr / 10; DIGITS[4] = TIME.hr - DIGITS[5] * 10;
        } else {
            for i in &mut DIGITS {
                *i