1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Copyright (c) 2021, Pavel Pletenev
 *
 * This file is part of can_bit_timings project
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

extern crate proc_macro;
use can_bit_timings_core::CanBitTiming;
use proc_macro::TokenStream;
use proc_macro_error::{abort, abort_call_site, proc_macro_error};
use syn::{Expr, ExprAssign, ExprLit, ExprMethodCall, ExprPath, Ident, Lit, Token, parse_macro_input, spanned::Spanned};
use syn::parse::{Parse, ParseStream, Result};
use syn::punctuated::Punctuated;
use quote::quote;
use std::convert::TryFrom;



fn round_uint32(f: f64) -> u32 {
    let f_int = f as u32;
    if f - (f_int as f64) > 0.5 {
        f_int + 1
    }else{
        f_int
    }
}

struct CanBits(CanBitTiming, Percents);

#[derive(Clone, Copy)]
struct Frequency(u32);

#[derive(Clone, Copy)]
struct Ratio(f64);

#[derive(Clone, Copy)]
struct Percents(f64);

impl From<Percents> for Ratio{
    fn from(p: Percents) -> Self{
        Self(p.0 as f64 / 100.0)
    }
}

impl From<Ratio> for Percents{
    fn from(r: Ratio) -> Self{
        Self(r.0 as f64 * 100.0)
    }
}

struct CanBitsArgs{
    clk:Frequency, 
    bitrate : Frequency, 
    midpoint: Ratio, 
    tolerance: Percents
}

impl CanBits {
/*
 * Copyright (c) 2016, Antal Szabó
 * Copyright (c) 2016, Kevin Läufer
 * Copyright (c) 2016-2018, Niklas Hauser
 * Copyright (c) 2018, Christopher Durand
 *
 * The following algorithm was taken from the modm project and
 * translated into Rust by Pavel Pletenev
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
/**
 * CAN Bit Timing
 *
 * Example for CAN bit timing:
 *   CLK on APB1 = 36 MHz
 *   BaudRate = 125 kBPs = 1 / NominalBitTime
 *   NominalBitTime = 8uS = tq + tBS1 + tBS2
 * with:
 *   tBS1 = tq * (TS1[3:0] + 1) = 12 * tq
 *   tBS2 = tq * (TS2[2:0] + 1) = 5 * tq
 *   tq = (BRP[9:0] + 1) * tPCLK
 * where tq refers to the Time quantum
 *   tPCLK = time period of the APB clock = 1 / 36 MHz
 *
 * STM32F1xx   tPCLK = 1 / 36 MHz
 * STM32F20x   tPCLK = 1 / 30 MHz
 * STM32F40x   tPCLK = 1 / 42 MHz
 *
 *
 */
    fn best(clk:Frequency, bitrate : Frequency, midpoint:Ratio) -> (CanBitTiming, Percents){
        const MIN_BS1_BS2 : u8 = 8;
        const MAX_BS1_BS2 : u8 = 25;

        let mut min_error: f64 = 10_000.0;
        let mut best_prescaler: u16 = 0;
        let mut best_bs1_bs2:u8 = 0;

        for bs1_bs2 in MIN_BS1_BS2..(MAX_BS1_BS2+1) {
            let ideal_prescaler = (clk.0 as f64) / (
                (bitrate.0 as f64) * ((1 + bs1_bs2) as f64));
            let int_prescaler = round_uint32(ideal_prescaler);
            let error = (1. - (int_prescaler as f64)/ideal_prescaler).abs();
            if error <= min_error {
                // eprintln!("tqs: {}  psc: {} err: {}", bs1_bs2+1, int_prescaler, min_error);
                best_prescaler = int_prescaler as u16;
                min_error = error;
                best_bs1_bs2 = bs1_bs2;
            }
        }

        let bs2 = (midpoint.0 * (best_bs1_bs2 as f64 + 1.)).floor() as u8;
        let bs1 = best_bs1_bs2 - bs2;
        eprintln!(
            "Selecting for {} Hz {} Hz  tqs: {} {},  psc: {} err: {}", 
            clk.0, bitrate.0, bs1, bs2, best_prescaler, min_error);
        (
            CanBitTiming{bs1, bs2, sjw: 1, prescaler: best_prescaler}, 
            Percents(min_error * 100.0))
    }
    fn new(clk:Frequency, bitrate : Frequency, midpoint: Ratio, tolerance: Percents) -> Self{
        let (best, min_error) = CanBits::best(clk, bitrate, midpoint);
        // check assertions
        if (best.prescaler as i8) <= 0 {
            abort_call_site!("CAN bitrate is too high for standard bit timings!");
        }
        if best.prescaler > ((1 << 10) -1) {
            abort_call_site!("Prescaler value too large!");
        }
        if min_error.0 > tolerance.0 {
            abort_call_site!("Error is too high for this configuration ({} %)!", min_error.0);
        }
        CanBits(best, min_error)
    }
}

impl Frequency {
    fn mul_from_ident(i: &Ident) -> u32{
        let name = i.to_string();
        if name == "mhz" { 1_000_000 }
        else if name == "khz" { 1_000 }
        else if name == "hz" { 1 }
        else if name == "bps" { 1 }
        else { abort!(i, "Unknown multiplier") }
    }
}

impl TryFrom<&Expr> for Frequency{
    type Error = syn::Error;
    fn try_from(value: &Expr) -> Result<Self>{
        Ok(match value {
            Expr::Lit(ExprLit{lit: Lit::Int(lit),..}) => {
                Self(lit.base10_parse()?)
            }
            Expr::MethodCall(ExprMethodCall{
                receiver, method, args ,..
            }) => {
                let mut ret = Frequency::try_from(receiver.as_ref())?;
                if args.len() != 0 { abort!(args, "Expected no arguments!");}
                ret.0 *= Frequency::mul_from_ident(method);
                ret
            }
            _ => {abort!(value, "Expected int literal or `int.unit()` expression");}
        })
    }
}

impl TryFrom<&Expr> for Ratio{
    type Error = syn::Error;
    fn try_from(value: &Expr) -> Result<Self>{
        match value {
            Expr::Lit(ExprLit{lit,..}) => {
                match lit {
                    Lit::Int(lit) => Ok(Self(lit.base10_parse::<u32>()? as f64)),
                    Lit::Float(lit) => Ok(Self(lit.base10_parse()?)),
                    _ => Err(syn::Error::new(lit.span(), "Expected int or float literal"))
                }
            }
            _ => Err(syn::Error::new(value.span(), "Expected int or float literal"))
        }
    }
}

impl TryFrom<&Expr> for Percents{
    type Error = syn::Error;
    fn try_from(value: &Expr) -> Result<Self>{
        if let Ok(r) = Ratio::try_from(value){
            return Ok(r.into())
        }
        Ok(match value {
            Expr::MethodCall(ExprMethodCall{
                receiver, method, args ,..
            }) => {
                let ret = Ratio::try_from(receiver.as_ref())?;
                if args.len() != 0 { abort!(args, "Expected no arguments!");}
                if method.to_string() != "pct"{ abort!(method, "Expected pct!")}
                Percents(ret.0)
            }
            _ => {abort!(value, "Expected float literal");}
        })
    }
}


impl Parse for CanBitsArgs{
    fn parse(input: ParseStream) -> Result<Self> {
        let mut ret = Self{
            clk: Frequency(0), 
            bitrate: Frequency(0),
            midpoint: Ratio(0.175),
            tolerance: Percents(0.5),
        };
        let input= 
            Punctuated::<Expr, Token![,]>::parse_terminated(input)?;
        for (i, expr) in (0..(input.len())).zip(input.iter()){
            if let Expr::Assign(ExprAssign{left, right, ..}) = expr {
                if let Expr::Path(ExprPath{path,..}) = left.as_ref(){
                    let var = path.get_ident().unwrap_or_else(
                        || abort!(path, "This should be an indentifier"));
                    if var == "clk" {
                        ret.clk = Frequency::try_from(right.as_ref())?;
                    } else if var == "bitrate" {
                        ret.bitrate = Frequency::try_from(right.as_ref())?;
                    } else if var == "midpoint" {
                        ret.midpoint = Ratio::try_from(right.as_ref())?;
                    } else if var == "tolerance" {
                        ret.tolerance = Percents::try_from(right.as_ref())?;
                    }
                }else {
                    abort!(left, "Unknown type of expression!")
                }
            }else if let Ok(f) = Frequency::try_from(expr){
                if i == 0{
                    ret.clk = f;
                }else if i == 1{
                    ret.bitrate = f;
                };
            }
        }
        Ok(ret)
    }
}

impl Parse for CanBits {
    fn parse(input: ParseStream) -> Result<Self> {
        let CanBitsArgs { 
            clk, bitrate, midpoint, tolerance 
        } = input.parse::<CanBitsArgs>()?;
        
        Ok(CanBits::new(clk,bitrate,midpoint, tolerance))
    }
}

/// This macro generates bit timings struct for provided input frequency 
/// and output baudrate.
/// 
/// **Inputs**:
/// - *required positional argument:* input clock frequency in Hertz
/// - *required positional argument:* output baudrate in Hertz
/// - *optional named argument* **midpoint** - BS2 segment ratio (default is 0.175)
/// - *optional named argument* **tolerance** - error tolerance (default is 0.5%)
///
/// **Outputs** [can_bit_timings_core::CanBitTiming] struct into the invocation site.
/// 
/// Frequency can be specified as number or as 
/// a method  calls (`.mhz()`, `.khz()`, `.hz()`, `.bps()`). 
/// Percents are specified as a ratio (from 0 to 1.0) 
/// or as a method call (from `0.pct()` to `100.pct()`)
///
/// ```
/// # use can_bit_timings_proc_macro::can_timings;
/// # use can_bit_timings_core::CanBitTiming;
/// let timing = can_timings!(10.mhz(), 1.mhz());
/// assert_eq!(
///     timing, 
///     CanBitTiming{sjw: 1, bs1: 8, bs2: 1, prescaler: 1}
/// );
/// let timing = can_timings!(10_000_000, 125.khz());
/// assert_eq!(
///     timing, 
///     CanBitTiming{sjw: 1, bs1: 16, bs2: 3, prescaler: 4}
/// );
/// let timing = can_timings!(10_000_000, 125.khz(), midpoint=0.275, tolerance=0.1.pct());
/// assert_eq!(
///     timing, 
///     CanBitTiming{sjw: 1, bs1: 14, bs2: 5, prescaler: 4}
/// );
/// ```
#[proc_macro_error]
#[proc_macro]
pub fn can_timings(item: TokenStream) -> TokenStream {
    let CanBits(CanBitTiming { 
        bs1, bs2, sjw, prescaler, 
    }, _) = parse_macro_input!(item as CanBits);
    TokenStream::from(quote!(::can_bit_timings_core::CanBitTiming{
        sjw: #sjw,
        bs1: #bs1,
        bs2: #bs2,
        prescaler: #prescaler,
    }))
}

/// This macro generates `u32` register for bxcan for provided input frequency 
/// and output baudrate.
/// 
/// **Inputs**:
/// - *required positional argument:* input clock frequency in Hertz
/// - *required positional argument:* output baudrate in Hertz
/// - *optional named argument* **midpoint** - BS2 segment ratio (default is 0.175)
/// - *optional named argument* **tolerance** - error tolerance (default is 0.5%)
///
/// **Outputs** u32 into the invocation site.
/// 
/// Frequency can be specified as number or as 
/// a method  calls (`.mhz()`, `.khz()`, `.hz()`, `.bps()`). 
/// Percents are specified as a ratio (from 0 to 1.0) 
/// or as a method call (from `0.pct()` to `100.pct()`)
///
/// ```
/// # use can_bit_timings_proc_macro::can_timings_bxcan;
/// let timing = can_timings_bxcan!(10.mhz(), 1.mhz());
/// assert_eq!(timing, 0x70000);
/// let timing = can_timings_bxcan!(10_000_000, 125.khz());
/// assert_eq!(timing, 0x2f0003);
/// let timing = can_timings_bxcan!(10_000_000, 125.khz(), midpoint=0.275, tolerance=0.1.pct());
/// assert_eq!(timing, 0x4d0003);
/// ```
#[proc_macro_error]
#[proc_macro]
pub fn can_timings_bxcan(item: TokenStream) -> TokenStream {
    let rf = parse_macro_input!(item as CanBits);
    let result = rf.0.bxcan();
    TokenStream::from(quote!(#result))
}