bsp_pwm_led_gpio.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "sdk_common.h"
  2. #include "SEGGER_RTT.h"
  3. #include "nrf_drv_pwm.h"
  4. #include "usr_config.h"
  5. #include "system.h"
  6. #include "nrf_gpio.h"
  7. #include "bsp_pwm.h"
  8. /********************** ±äÁ¿Çø *************************/
  9. static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);
  10. static const uint16_t pwm_const = 1250;
  11. static nrf_pwm_values_individual_t m_demo1_seq_values={0,0,0};
  12. static nrf_pwm_sequence_t const m_demo1_seq =
  13. {
  14. .values.p_individual = &m_demo1_seq_values,
  15. .length = NRF_PWM_VALUES_LENGTH(m_demo1_seq_values),
  16. .repeats = 0,
  17. .end_delay = 0
  18. };
  19. void Pwm_update_duty(uint8_t duty_1,uint8_t duty_2,uint8_t duty_3)
  20. {
  21. if(duty_1 > 100)duty_1 =100;
  22. if(duty_2 > 100)duty_2 =100;
  23. if(duty_3 > 100)duty_3 =100;
  24. if(PCB_VERSION == 1 || PCB_VERSION == 3){
  25. duty_1 = 100-duty_1;
  26. duty_2 = 100-duty_2;
  27. duty_3 = 100-duty_3;
  28. }
  29. uint16_t duty1_t = (uint16_t)(duty_1*pwm_const/100);
  30. uint16_t duty2_t = (uint16_t)(duty_2*pwm_const/100);
  31. uint16_t duty3_t = (uint16_t)(duty_3*pwm_const/100);
  32. m_demo1_seq_values.channel_0 = duty1_t;
  33. m_demo1_seq_values.channel_1 = duty2_t;
  34. m_demo1_seq_values.channel_2 = duty3_t;
  35. //DEBUG_LOG("led debug %d,%d,%d\n",m_demo1_seq_values.channel_0,m_demo1_seq_values.channel_1,m_demo1_seq_values.channel_2);
  36. }
  37. static void Pwm_init(void)
  38. {
  39. nrf_drv_pwm_config_t const config0 =
  40. {
  41. .output_pins =
  42. {
  43. PIN_LED_R, // channel 0
  44. PIN_LED_G, // channel 1
  45. PIN_LED_B, // channel 2
  46. NRF_DRV_PWM_PIN_NOT_USED
  47. },
  48. .irq_priority = APP_IRQ_PRIORITY_LOWEST,
  49. .base_clock = NRF_PWM_CLK_125kHz,
  50. .count_mode = NRF_PWM_MODE_UP,
  51. .top_value = pwm_const,
  52. .load_mode = NRF_PWM_LOAD_INDIVIDUAL,
  53. .step_mode = NRF_PWM_STEP_AUTO
  54. };
  55. APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config0, NULL));
  56. m_demo1_seq_values.channel_0 = pwm_const;
  57. m_demo1_seq_values.channel_1 = pwm_const;
  58. m_demo1_seq_values.channel_2 = pwm_const;
  59. (void)nrf_drv_pwm_simple_playback(&m_pwm0, &m_demo1_seq, 1,
  60. NRF_DRV_PWM_FLAG_LOOP);
  61. }
  62. static void cb_pwmWakeup(uint32_t t)
  63. {
  64. Pwm_init();
  65. }
  66. static void cb_pwmSleep(uint32_t t)
  67. {
  68. nrfx_pwm_uninit(&m_pwm0);
  69. nrf_gpio_cfg_input(PIN_LED_R, NRF_GPIO_PIN_NOPULL);//IO
  70. nrf_gpio_cfg_input(PIN_LED_G, NRF_GPIO_PIN_NOPULL);
  71. nrf_gpio_cfg_input(PIN_LED_B, NRF_GPIO_PIN_NOPULL);
  72. }
  73. void Pwm_Initialize(void){
  74. Pwm_init();
  75. Wakeup_Regist(cb_pwmWakeup);
  76. Sleep_Regist(cb_pwmSleep);
  77. }