Maple mini LED fading blink(pin 33)

Post your cool example code here.
navintiwari08
Posts: 16
Joined: Sat Aug 22, 2015 3:54 pm

Maple mini LED fading blink(pin 33)

Postby navintiwari08 » Sat Aug 13, 2016 2:03 pm

Hey,
Maple mini has LED at pin 33 which is a non PWM pin. Here is a little code that simulates PWM on pin 33 and produces a fading blink on that LED.

Code: Select all

#define LED_PIN 33    //pin 33 is the led pin on maple mini.

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 33 as output.
  pinMode(LED_PIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  int period = 600;             //the pulse time period (us)
  int pwm_pulses = 80;          //number of pwm pulses to be sent
  for (int j = 1; j < 100; j++) {   //duty cycle changing from 1% to 99%
    set_pwm(LED_PIN, period, j, pwm_pulses); //setting pwm on led pin
  }
  digitalWrite(LED_PIN, HIGH);
  delay(1000);
  for (int j = 99; j >= 1; j--) {
    set_pwm(LED_PIN, period, j, pwm_pulses);
  }
  digitalWrite(LED_PIN, LOW);
  delay(1000);
}

void set_pwm(int pin_num, int period, int duty_cycle, int pwm_pulses) {
  int on_time = (duty_cycle * period)/100; //on time(us)
  int off_time = period - on_time;      //off time(us)
  for (int i = 1; i <= pwm_pulses; i++) {  //sending 'pwm_pulses' pulses.
    digitalWrite(pin_num, HIGH);
    delayMicroseconds(on_time);
    digitalWrite(pin_num, LOW);
    delayMicroseconds(off_time); 
   
  }
}


Enjoy!
Navin

User avatar
RogerClark
Posts: 5615
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Maple mini LED fading blink(pin 33)

Postby RogerClark » Sat Aug 13, 2016 11:20 pm

Hi Navin

If you want to experiment with some more advanced code, to do the same thing, but which keeps running in the background

You could use one of the hardware timers to call a function which toggles the value of the pin.

Also, If you look on MBED there is a software PWM library for the nRF51 ( as this device only has PWM on 3 pins)

I considered porting that library to Arduino, but in the end I didnt need it.

Or there may be other software PWM libraries that would be easier to port

navintiwari08
Posts: 16
Joined: Sat Aug 22, 2015 3:54 pm

Re: Maple mini LED fading blink(pin 33)

Postby navintiwari08 » Mon Aug 15, 2016 7:16 am

Hi Roger,
You could use one of the hardware timers to call a function which toggles the value of the pin.

Does Arduino provide functions to directly control the hardware timers? It would be great if that can be done.

Also, If you look on MBED there is a software PWM library for the nRF51 ( as this device only has PWM on 3 pins)

I looked up on mbed.org but couldn't quite locate the library you were referring to.

Also, is there a way to change the system clock frequency of maple mini? I suppose its running on 72 MHz. I know this is the maximum for this MCU but can I slow it down?

User avatar
RogerClark
Posts: 5615
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Maple mini LED fading blink(pin 33)

Postby RogerClark » Mon Aug 15, 2016 7:38 am

STM32 is different from AVR Arduino, but STM32 does have multiple hardware timers which you can use to trigger interrupts and call Interrupt Service Routines (functions)

Look in the Examples menu under A_STM32 Examples -> Maple -> TimerInterrupts , there is an example of how to use the timer interrupts

Also, is there a way to change the system clock frequency of maple mini? I suppose its running on 72 MHz. I know this is the maximum for this MCU but can I slow it down?


You would need to modify the boards_setup.cpp file in the Mini Maple variant folder,

If you look in the GD32 variant, in boards_setup.cpp there is code like

Code: Select all

#if F_CPU==120000000
   #define BOARD_RCC_PLLMUL RCC_PLLMUL_10
#elif F_CPU==96000000
   #define BOARD_RCC_PLLMUL RCC_PLLMUL_8
#elif F_CPU==72000000
   #define BOARD_RCC_PLLMUL RCC_PLLMUL_6   
#elif F_CPU==48000000
   #define BOARD_RCC_PLLMUL RCC_PLLMUL_4      
#elif F_CPU==12000000
// Use 2 x pre-divider
   #define BOARD_RCC_PLLMUL RCC_PLLMUL_1
#endif


Actually this is my update code that I am testing as I want to run the GD32 at 12Mhz (its lowest speed) but similar code could be used for the Maple mini e.g.


Code: Select all

#ifndef BOARD_RCC_PLLMUL

#if F_CPU==72000000
   #define BOARD_RCC_PLLMUL RCC_PLLMUL_9   
#elif F_CPU==48000000
   #define BOARD_RCC_PLLMUL RCC_PLLMUL_6      
#endif

#endif


Note. USB will only work at 72Mhz and 48Mhz on the Maple mini as the USB clock control only has 2 possible divider settings (and will not multiply the clock value)

GD32 has 4 USB Clock divider settings (but won't operated below 48Mhz either)

Also, there is a small bug in the master code at the moment, which I only found yesterday where the USB divider for Maple mini is set to 1.5 x for 48Mhz but should be 1.0 x
So Maple mini boards_setup.cpp should be

Code: Select all

/******************************************************************************
 * The MIT License
 *
 * Copyright (c) 2012 LeafLabs, LLC.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
*****************************************************************************/

/**
 * @file wirish/stm32f1/boards_setup.cpp
 * @author Marti Bolivar <mbolivar@leaflabs.com>
 * @brief STM32F1 chip setup.
 *
 * This file controls how init() behaves on the STM32F1. Be very
 * careful when changing anything here. Many of these values depend
 * upon each other.
 */

#include "boards_private.h"

#include <libmaple/gpio.h>
#include <libmaple/timer.h>

#include <boards.h>
#include <usb_serial.h>

// Allow boards to provide a PLL multiplier. This is useful for
// e.g. STM32F100 value line MCUs, which use slower multipliers.
// (We're leaving the default to RCC_PLLMUL_9 for now, since that
// works for F103 performance line MCUs, which is all that LeafLabs
// currently officially supports).
/*
#ifndef BOARD_RCC_PLLMUL
#define BOARD_RCC_PLLMUL RCC_PLLMUL_9
#endif
*/

#ifndef BOARD_RCC_PLLMUL

#if F_CPU==72000000
   #define BOARD_RCC_PLLMUL RCC_PLLMUL_9   
#elif F_CPU==48000000
   #define BOARD_RCC_PLLMUL RCC_PLLMUL_6      
#endif

#endif

namespace wirish {
    namespace priv {

        static stm32f1_rcc_pll_data pll_data = {BOARD_RCC_PLLMUL};
        __weak rcc_pll_cfg w_board_pll_cfg = {RCC_PLLSRC_HSE, &pll_data};
        __weak adc_prescaler w_adc_pre = ADC_PRE_PCLK2_DIV_6;
        __weak adc_smp_rate w_adc_smp = ADC_SMPR_55_5;

        __weak void board_reset_pll(void) {
            // TODO
        }

        __weak void board_setup_clock_prescalers(void) {
            rcc_set_prescaler(RCC_PRESCALER_AHB, RCC_AHB_SYSCLK_DIV_1);
            rcc_set_prescaler(RCC_PRESCALER_APB1, RCC_APB1_HCLK_DIV_2);
            rcc_set_prescaler(RCC_PRESCALER_APB2, RCC_APB2_HCLK_DIV_1);
         rcc_clk_disable(RCC_USB);
         #if F_CPU == 72000000
         rcc_set_prescaler(RCC_PRESCALER_USB, RCC_USB_SYSCLK_DIV_1_5);
         #elif F_CPU == 48000000
         rcc_set_prescaler(RCC_PRESCALER_USB, RCC_USB_SYSCLK_DIV_1);         
         #endif            
        }

        __weak void board_setup_gpio(void) {
            gpio_init_all();
        }

        __weak void board_setup_usb(void) {
#ifdef SERIAL_USB
//          SerialUSB.begin();
         Serial.begin();// Roger Clark. Changed SerialUSB to Serial for Arduino sketch compatibility
#endif
      }

        __weak void series_init(void) {
            // Initialize AFIO here, too, so peripheral remaps and external
            // interrupts work out of the box.
            afio_init();
        }

    }
}


Also for my tests I had changed boards.txt maple mini to add a speed selection menu like this

Code: Select all

##############################################################
mapleMini.name=Maple Mini

mapleMini.build.board=MAPLE_MINI
mapleMini.vid.0=0x1EAF
mapleMini.pid.0=0x0004
mapleMini.build.core=maple
mapleMini.build.cpu_flags=-DMCU_STM32F103CB -DSERIAL_USB
mapleMini.build.variant=maple_mini
mapleMini.upload.usbID=1EAF:0003

mapleMini.upload.tool=maple_upload
mapleMini.upload.protocol=maple_dfu
mapleMini.upload.use_1200bps_touch=false
mapleMini.upload.file_type=bin
mapleMini.upload.auto_reset=true

mapleMini.menu.bootloader_version.original = Original (17k RAM,108k Flash)
mapleMini.menu.bootloader_version.original.build.vect=VECT_TAB_ADDR=0x8005000
mapleMini.menu.bootloader_version.original.build.ldscript=ld/flash.ld
mapleMini.menu.bootloader_version.original.upload.ram.maximum_size=17408
mapleMini.menu.bootloader_version.original.upload.flash.maximum_size=110592
mapleMini.menu.bootloader_version.original.upload.maximum_size=110592
mapleMini.menu.bootloader_version.original.upload.altID=1

mapleMini.menu.bootloader_version.bootloader20 = Bootloader 2.0 (20k RAM,120k Flash)
mapleMini.menu.bootloader_version.bootloader20.build.vect=VECT_TAB_ADDR=0x8002000
mapleMini.menu.bootloader_version.bootloader20.build.ldscript=ld/bootloader_20.ld
mapleMini.menu.bootloader_version.bootloader20.upload.ram.maximum_size=20480
mapleMini.menu.bootloader_version.bootloader20.upload.flash.maximum_size=122880
mapleMini.menu.bootloader_version.bootloader20.upload.maximum_size=122880
mapleMini.menu.bootloader_version.bootloader20.upload.altID=2


mapleMini.menu.cpu_speed.speed_72mhz=72Mhz
mapleMini.menu.cpu_speed.speed_72mhz.build.f_cpu=72000000L
mapleMini.menu.cpu_speed.speed_48mhz=48Mhz (lower power with USB)
mapleMini.menu.cpu_speed.speed_48mhz.build.f_cpu=48000000L


If you update those 2 files you can select 48Mhz and the USB will still work

You can make the Maple mini run at 8Mhz, but USB will not work for Serial and you will need to attach a USB to Serial adaptor and send debug messages via Serial1

Also upload will not work unless you press reset just before the IDE will upload the sketch


I may add this new menu (72Mhz / 48Mhz ) to the Maple mini, but I'm not sure its much use if you are running from USB as saving power by running the clock slower would not be necessary as the board is powered from the host USB

navintiwari08
Posts: 16
Joined: Sat Aug 22, 2015 3:54 pm

Re: Maple mini LED fading blink(pin 33)

Postby navintiwari08 » Wed Aug 17, 2016 3:48 am

Thanks Roger!
Got the timer working. I guess I'll run it at 72 MHz only for now. I don't really see a point in slowing it down to 48 MHz.
I'll try to experiment and interface few peripherals at hand. And probably set up a little serial com to control things from my PC's keyboard.

keep it burning!
Navin

User avatar
RogerClark
Posts: 5615
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Maple mini LED fading blink(pin 33)

Postby RogerClark » Wed Aug 17, 2016 4:39 am

Navin

I agree.
navintiwari08 wrote: I don't really see a point in slowing it down to 48 MHz.


I don't see any point in running at 48Mhz either, as if the STM32 is bring powered from the USB host, there is no need to save 10mA by reducing the clock speed.

The only benefit in reducing the clock speed is if you run at the minimum of 8Mhz (some boards have 12Mhz crystal but most , including the Maple Mini have a 8Mhz crystal).

There is another current thread about using the STM32 as a LUX (light colour) meter, and it switches the board to use HSE (High Speed External) direct for clock, rather than HSE -> PLL - > master clock

This seems to be the easiest way to switch to a low clock speed as it bypasses the main / system clock PLL

I have done some tests on a 12Mhz board and power consumption is reduced from 40mA to 10mA if clock is changed from 72Mhz to 8Mhz (and no other changes made)

10mA is still quite high for battery power operation, but this seems to be the minimum achievable without turning off GPIO etc


Return to “Code snipplets”

Who is online

Users browsing this forum: No registered users and 1 guest