From e8d69145f6cabfab056cd529bcd4adf979fe624d Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Sat, 18 Jun 2016 13:13:02 +0200 Subject: [PATCH] Adding partial support for State Configurable Timers (SCT) Use as one 32 bits counter for PWM implemented and tested --- pwm/Makefile | 12 ++++++ pwm/README | 29 +++++++++++++ pwm/main.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 pwm/Makefile create mode 100644 pwm/README create mode 100644 pwm/main.c diff --git a/pwm/Makefile b/pwm/Makefile new file mode 100644 index 0000000..097ff94 --- /dev/null +++ b/pwm/Makefile @@ -0,0 +1,12 @@ +# Makefile for example apps + +MODULE = $(shell basename $(shell cd .. && pwd && cd -)) +NAME = $(shell basename $(CURDIR)) + +.PHONY: $(NAME).bin +$(NAME).bin: + @make -C ../../.. --no-print-directory NAME=$(NAME) MODULE=$(MODULE) apps/$(MODULE)/$(NAME)/$@ + +clean mrproper: + @make -C ../../.. --no-print-directory $@ + diff --git a/pwm/README b/pwm/README new file mode 100644 index 0000000..9cd7701 --- /dev/null +++ b/pwm/README @@ -0,0 +1,29 @@ +PWM example + +Copyright 2016 Nathael Pajani + + +/* **************************************************************************** + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *************************************************************************** */ + +This example shows the support of the LPC82x PWM support using SCT timer by +controling three output as PWM. + +The selected period is 1s and each output has a different duty-cycle : +GPIO 0.8 as output 0 - duty cycle of 2/3 +GPIO 0.9 as output 1 - duty cycle of 1/30 +GPIO 0.3 as output 2 - duty cycle of 45/150 + diff --git a/pwm/main.c b/pwm/main.c new file mode 100644 index 0000000..74f5de5 --- /dev/null +++ b/pwm/main.c @@ -0,0 +1,117 @@ +/**************************************************************************** + * timers/main.c + * + * Timers example + * + * Copyright 2016 Nathael Pajani + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *************************************************************************** */ + +/* Timers send messages on UART0 at different intervals */ + +#include "lib/stdint.h" +#include "core/system.h" +#include "core/systick.h" +#include "core/pio.h" +#include "lib/stdio.h" +#include "drivers/serial.h" +#include "drivers/gpio.h" +#include "drivers/timers.h" + + +#define MODULE_VERSION 0x01 +#define MODULE_NAME "E-Xanh Gardener" + + +#define SELECTED_FREQ FREQ_SEL_36MHz + +/***************************************************************************** */ +/* Pins configuration */ +/* pins blocks are passed to set_pins() for pins configuration. + * Unused pin blocks can be removed safely with the corresponding set_pins() call + * All pins blocks may be safelly merged in a single block for single set_pins() call.. + */ +const struct pio_config common_pins[] = { + /* UART 0 */ + { LPC_GPIO_0_0, LPC_UART0_RX, 0 }, + { LPC_GPIO_0_4, LPC_UART0_TX, 0 }, + /* Timers */ + { LPC_GPIO_0_8, LPC_SCT_POUT0, 0 }, + { LPC_GPIO_0_9, LPC_SCT_POUT1, 0 }, + { LPC_GPIO_0_3, LPC_SCT_POUT2, 0 }, + /* GPIO */ + { LPC_GPIO_0_12, LPC_GPIO, 0 }, + ARRAY_LAST_PIO, +}; + +const struct pio button = LPC_GPIO_0_12; /* ISP button */ + +const struct lpc_timer_pwm_config pwm_conf = { + .nb_channels = 3, + .period = (150 * 1000), + .outputs_initial_state = 0x07, + .match_values = { (100 * 1000), 5000, 45000, }, + .outputs = { 0, 1, 2, }, +}; + +/***************************************************************************** */ +void system_init() +{ + system_set_default_power_state(); + clock_config(SELECTED_FREQ); + set_pins(common_pins); + gpio_on(); + /* System tick timer MUST be configured and running in order to use the sleeping + * functions */ + systick_timer_on(1); /* 1ms */ + systick_start(); +} + +/* Define our fault handler. This one is not mandatory, the dummy fault handler + * will be used when it's not overridden here. + * Note : The default one does a simple infinite loop. If the watchdog is deactivated + * the system will hang. + */ +void fault_info(const char* name, uint32_t len) +{ + uprintf(UART0, name); + while (1); +} + + +/***************************************************************************** */ +int main(void) +{ + system_init(); + uart_on(0, 115200, NULL); + timer_on(LPC_SCT, (150 * 1000), NULL); + + timer_pwm_config(LPC_SCT, &pwm_conf); + uprintf(UART0, "Config done\n"); + + timer_start(LPC_SCT); + uprintf(UART0, "Timer started\n"); + + while (1) { + msleep(100); + uprintf(UART0, "Timer value: %d\n", timer_get_counter_val(LPC_SCT)); + } + return 0; +} + + + -- 2.43.0