Advertisement
creat1001

RP2040 Bare SDK PWM example

Feb 7th, 2021
1,976
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.95 KB | None | 1 0
  1. /*
  2.     Raspberry Pi Pico PWM example
  3.  
  4.     Scott Beasley (2021) No Copyright.
  5.  
  6.     Code used by Damien P. George Copyright (c) 2020
  7.     From the RPI Pico MicroPython project. See below for copyright info.
  8.     https://github.com/raspberrypi/micropython/blob/pico/ports/rp2/machine_pwm.c
  9. */
  10.  
  11. #include "pico/stdlib.h"
  12. #include <stdio.h>
  13. #include "pico/time.h"
  14. #include "hardware/pwm.h"
  15. #include "hardware/clocks.h"
  16.  
  17. #define LED_PIN 25
  18. #define PWM_PIN 16
  19. #define DUTY_50_PCT 32767
  20. // Maximum "top" is set at 65534 to be able to achieve 100% duty with 65535.
  21. #define TOP_MAX 65534
  22.  
  23. int set_pwm_freq (uint slice, int freq, uint32_t *div, uint32_t *top);
  24. int set_pwm_duty (uint slice, uint channel, uint32_t top, uint32_t duty);
  25.  
  26. int main (void)
  27. {
  28.     uint32_t div = 0, top = 0;
  29.     uint slice_num, channel;
  30.  
  31.     // Use the LED for error notifications
  32.     gpio_init (LED_PIN);
  33.     gpio_set_dir (LED_PIN, GPIO_OUT);
  34.  
  35.     slice_num = pwm_gpio_to_slice_num (PWM_PIN);
  36.     channel = pwm_gpio_to_channel (PWM_PIN);
  37.     gpio_set_function (PWM_PIN, GPIO_FUNC_PWM);
  38.  
  39.     // Set up a 2khz freq PWM
  40.     if (set_pwm_freq (slice_num, (int)2000, &div, &top) != 0) {
  41.        gpio_put (LED_PIN, 1);
  42.     } else {
  43.        gpio_put (LED_PIN, 0);
  44.     }
  45.    
  46.     // Set the PWM counter wrap value to reset on
  47.     pwm_set_wrap (slice_num, top);
  48.  
  49.     while (1) {
  50.         tight_loop_contents ( );
  51.         set_pwm_duty (slice_num, channel, top, (uint32_t)DUTY_50_PCT);
  52.         sleep_ms(2000);
  53.         set_pwm_duty (slice_num, channel, top, (uint32_t)DUTY_50_PCT / 2);
  54.         sleep_ms(2000);
  55.         set_pwm_duty (slice_num, channel, top, (uint32_t)DUTY_50_PCT / 4);
  56.         sleep_ms(2000);
  57.         set_pwm_duty (slice_num, channel, top, (uint32_t)DUTY_50_PCT / 6);
  58.         sleep_ms(2000);
  59.     }
  60. }
  61.  
  62. //
  63. // These functions work ok up to 10khz, but get off values after
  64. // 11k and above. It could be type/casting issues, but I have not
  65. // investigated any further yey. I plan to test the Micropython
  66. // functions and see if they display the same issues.
  67. //
  68.  
  69. /*
  70.  * This file is part of the MicroPython project, http://micropython.org/
  71.  *
  72.  * The MIT License (MIT)
  73.  *
  74.  * Copyright (c) 2020 Damien P. George
  75.  *
  76.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  77.  * of this software and associated documentation files (the "Software"), to deal
  78.  * in the Software without restriction, including without limitation the rights
  79.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  80.  * copies of the Software, and to permit persons to whom the Software is
  81.  * furnished to do so, subject to the following conditions:
  82.  *
  83.  * The above copyright notice and this permission notice shall be included in
  84.  * all copies or substantial portions of the Software.
  85.  *
  86.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  87.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  88.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  89.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  90.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  91.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  92.  * THE SOFTWARE.
  93.  */
  94.  
  95. // Code used from https://github.com/raspberrypi/micropython/blob/pico/ports/rp2/machine_pwm.c
  96. // Function - machine_pwm_freq
  97. // Shaped for my needs (Scott Beasley) No Copyright.
  98. // MIT License (MIT) Damien P. George Copyright (c) 2020
  99. int set_pwm_freq (uint slice, int freq, uint32_t *div, uint32_t *top)
  100. {
  101.     // Set the frequency, making "top" as large as possible for maximum resolution.
  102.     *div = (uint32_t)(16 * clock_get_hz (clk_sys) / (uint32_t)freq);
  103.     *top = 1;
  104.     for (;;) {
  105.         // Try a few small prime factors to get close to the desired frequency.
  106.         if (*div >= 16 * 5 && *div % 5 == 0 && *top * 5 <= TOP_MAX) {
  107.             *div /= 5;
  108.             *top *= 5;
  109.         } else if (*div >= 16 * 3 && *div % 3 == 0 && *top * 3 <= TOP_MAX) {
  110.             *div /= 3;
  111.             *top *= 3;
  112.         } else if (*div >= 16 * 2 && *top * 2 <= TOP_MAX) {
  113.             *div /= 2;
  114.             *top *= 2;
  115.         } else {
  116.             break;
  117.         }
  118.     }
  119.  
  120.     if (*div < 16) {
  121.         *div = 0;
  122.         *top = 0;
  123.         return -1; // freq too large
  124.     } else if (*div >= 256 * 16) {
  125.         *div = 0;
  126.         *top = 0;
  127.         return -2; // freq too small
  128.     }
  129.  
  130.     return 0;
  131. }
  132.  
  133. // Code used from https://github.com/raspberrypi/micropython/blob/pico/ports/rp2/machine_pwm.c
  134. // Function - machine_pwm_duty_u16
  135. // Shaped for my needs (Scott Beasley) No Copyright.
  136. // MIT License (MIT) Damien P. George Copyright (c) 2020
  137. int set_pwm_duty (uint slice, uint channel, uint32_t top, uint32_t duty)
  138. {
  139.     // Set duty cycle.
  140.     uint32_t cc = duty * (top + 1) / 65535;
  141.     pwm_set_chan_level (slice, channel, cc);
  142.     pwm_set_enabled (slice, true);
  143.  
  144.     return 0;
  145. }
  146.  
  147.  
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement