|
| 1 | +//example for the advanced ADC API, this API is recommended for those already familiar with the ADC of STM32s |
| 2 | +//this example configures a regular group with software trigger, an injected group configured as auto injected, |
| 3 | +//and an analog comparator (ADC watchdog) |
| 4 | + |
| 5 | +//before creating a program using ADC from older ST families, such as in this case STM32F1 |
| 6 | +//be aware of possible hardware bugs and limitations |
| 7 | +//for example: |
| 8 | +// temperature sensor/VREF cannot be read in interleaved mode as it requires a sample time greater than 17 |
| 9 | +// readings after 1ms from the previous reading may contain more noise than expected |
| 10 | +//Voltage glitch on ADC input 0 |
| 11 | + |
| 12 | +const std = @import("std"); |
| 13 | +const microzig = @import("microzig"); |
| 14 | +const interrupt = microzig.interrupt; |
| 15 | + |
| 16 | +const RCC = microzig.chip.peripherals.RCC; |
| 17 | +const DMA = microzig.chip.peripherals.DMA1; |
| 18 | +const DMA_t = microzig.chip.types.peripherals.bdma_v1; |
| 19 | +const stm32 = microzig.hal; |
| 20 | +const timer = microzig.hal.timer.GPTimer.init(.TIM2); |
| 21 | + |
| 22 | +const uart = stm32.uart.UART.init(.USART1); |
| 23 | +const gpio = stm32.gpio; |
| 24 | +const TX = gpio.Pin.from_port(.A, 9); |
| 25 | + |
| 26 | +pub const microzig_options = microzig.Options{ |
| 27 | + .logFn = stm32.uart.log, |
| 28 | + .interrupts = .{ .ADC1_2 = .{ .c = watchdog_handler } }, |
| 29 | +}; |
| 30 | + |
| 31 | +const AdvancedADC = microzig.hal.adc.AdvancedADC; |
| 32 | +const adc = AdvancedADC.init(.ADC1); |
| 33 | +const ADC_pin1 = gpio.Pin.from_port(.A, 1); |
| 34 | +const ADC_pin2 = gpio.Pin.from_port(.A, 2); |
| 35 | +const ADC_pin3 = gpio.Pin.from_port(.A, 3); |
| 36 | + |
| 37 | +const v25 = 1.43; |
| 38 | +const avg_slope = 0.0043; //4.3mV/°C |
| 39 | + |
| 40 | +var ovf_flag: bool = false; |
| 41 | +pub fn watchdog_handler() callconv(.C) void { |
| 42 | + ovf_flag = true; |
| 43 | + adc.clear_flags(adc.read_flags()); //clear all active flags |
| 44 | +} |
| 45 | + |
| 46 | +fn adc_to_temp(val: usize) f32 { |
| 47 | + const temp_mv: f32 = (@as(f32, @floatFromInt(val)) / 4096) * 3.3; //convert to voltage |
| 48 | + return ((v25 - temp_mv) / avg_slope) + 25; //convert to celsius |
| 49 | +} |
| 50 | + |
| 51 | +fn DMA_init(arr_addr: u32, adc_addr: u32) void { |
| 52 | + const CH1: *volatile DMA_t.CH = @ptrCast(&DMA.CH); |
| 53 | + CH1.CR.raw = 0; //disable channel |
| 54 | + CH1.NDTR.raw = 0; |
| 55 | + CH1.CR.modify(.{ |
| 56 | + .DIR = DMA_t.DIR.FromPeripheral, |
| 57 | + .CIRC = 1, //disable circular mode |
| 58 | + .PL = DMA_t.PL.High, //high priority |
| 59 | + .MSIZE = DMA_t.SIZE.Bits16, |
| 60 | + .PSIZE = DMA_t.SIZE.Bits16, |
| 61 | + .MINC = 1, //memory increment mode |
| 62 | + .PINC = 0, //peripheral not incremented |
| 63 | + }); |
| 64 | + |
| 65 | + CH1.NDTR.modify(.{ .NDT = 4 }); //number of data to transfer, 4 samples |
| 66 | + CH1.PAR = adc_addr; //peripheral address |
| 67 | + CH1.MAR = arr_addr; //memory address |
| 68 | + CH1.CR.modify(.{ .EN = 1 }); //enable channel |
| 69 | +} |
| 70 | + |
| 71 | +pub fn main() !void { |
| 72 | + RCC.AHBENR.modify(.{ |
| 73 | + .DMA1EN = 1, |
| 74 | + }); |
| 75 | + RCC.APB1ENR.modify(.{ |
| 76 | + .TIM2EN = 1, |
| 77 | + }); |
| 78 | + RCC.APB2ENR.modify(.{ |
| 79 | + .AFIOEN = 1, |
| 80 | + .USART1EN = 1, |
| 81 | + .GPIOAEN = 1, |
| 82 | + .ADC1EN = 1, |
| 83 | + }); |
| 84 | + |
| 85 | + const counter = timer.into_counter(8_000_000); |
| 86 | + |
| 87 | + const adc_data_addr: u32 = @intFromPtr(&adc.regs.DR); |
| 88 | + var adc_buf: [10]u16 = .{0} ** 10; |
| 89 | + const adc_buf_addr: u32 = @intFromPtr(&adc_buf); |
| 90 | + const ref_ovf_flag: *volatile bool = &ovf_flag; |
| 91 | + |
| 92 | + //configure UART log |
| 93 | + |
| 94 | + TX.set_output_mode(.alternate_function_push_pull, .max_50MHz); |
| 95 | + uart.apply(.{ |
| 96 | + .baud_rate = 115200, |
| 97 | + .clock_speed = 8_000_000, |
| 98 | + }); |
| 99 | + stm32.uart.init_logger(&uart); |
| 100 | + |
| 101 | + //configure adc |
| 102 | + interrupt.enable(.ADC1_2); //enalbe ADC1 interrupt |
| 103 | + ADC_pin1.set_input_mode(.analog); |
| 104 | + ADC_pin2.set_input_mode(.analog); |
| 105 | + ADC_pin3.set_input_mode(.analog); |
| 106 | + |
| 107 | + //enable ADC and VREF/tempsensor |
| 108 | + adc.enable(true, &counter); |
| 109 | + adc.enable_reftemp(&counter); |
| 110 | + |
| 111 | + //regular group configuration |
| 112 | + try adc.configure_regular(.{ |
| 113 | + .dma = true, |
| 114 | + .trigger = .SWSTART, |
| 115 | + .mode = .{ |
| 116 | + .Single = .{ |
| 117 | + .seq = &.{ 16, 17, 2, 3 }, |
| 118 | + .channels_conf = &.{ |
| 119 | + .{ .channel = 17, .sample_rate = .@"239.5" }, //Vrefint |
| 120 | + .{ .channel = 16, .sample_rate = .@"239.5" }, //temperature sensor |
| 121 | + .{ .channel = 1, .sample_rate = .@"13.5" }, //ADC1 channel 1 |
| 122 | + .{ .channel = 3, .sample_rate = .@"13.5" }, //ADC1 channel 2 |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }); |
| 127 | + |
| 128 | + //injected group configuration, AUTO INJECTED mode starts right after the regular group and therefore does not require an external trigger |
| 129 | + try adc.configure_injected(.{ |
| 130 | + .mode = .{ |
| 131 | + .auto_injected = .{ |
| 132 | + .seq = &.{1}, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }); |
| 136 | + |
| 137 | + //despite the name, this is just an analog comparator |
| 138 | + adc.configure_watchdog( |
| 139 | + .{ |
| 140 | + .guard_mode = .{ .single_injected = 1 }, |
| 141 | + .interrupt = true, |
| 142 | + .high_treshold = 4095, |
| 143 | + .low_treshold = 800, |
| 144 | + }, |
| 145 | + ); |
| 146 | + |
| 147 | + std.log.info("start Advanced ADC scan", .{}); |
| 148 | + |
| 149 | + DMA_init(adc_buf_addr, adc_data_addr); |
| 150 | + |
| 151 | + while (true) { |
| 152 | + adc.software_trigger(); //start conversion |
| 153 | + counter.sleep_ms(100); |
| 154 | + std.log.info("\x1B[2J\x1B[H", .{}); // Clear screen and move cursor to 1,1 |
| 155 | + std.log.info("CPU temp: {d:.1}C", .{adc_to_temp(adc_buf[0])}); |
| 156 | + std.log.info("Vref: {d:0>4}", .{adc_buf[1]}); |
| 157 | + std.log.info("CH1: {d:0>4}", .{adc_buf[2]}); |
| 158 | + std.log.info("CH3 {d}", .{adc_buf[3]}); |
| 159 | + if (ref_ovf_flag.*) { |
| 160 | + std.log.info("Injected: OVERFLOW", .{}); |
| 161 | + ref_ovf_flag.* = false; |
| 162 | + continue; |
| 163 | + } |
| 164 | + std.log.info("Injected: {d}", .{adc.read_injected_data(0)}); |
| 165 | + } |
| 166 | +} |
0 commit comments