Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Template to create FreeRTOS tasks using the team's middleware
2. Go into the newly generated directory `cd AppCodeTemp`
1. Download the submodules `git submodule init && git submodule update`
1. In case any of the dependencies needs to be updated, simply do: `git submodule update --remote`
1. Open it in Visual Studio `code .`

## Having problems with git?:

Expand Down
51 changes: 51 additions & 0 deletions app/src/task_feeder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "stdint.h"
#include "stm32f1xx.h"

#define HEADLIGHT_PIN GPIO_PIN_0
#define MOTOR_PWM_PIN GPIO_PIN_1

void pwm_init() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Está padre que se haga con registros directo... Pero recuerda que por cuestiones de modularidad, usamos los tim handlers del STM32

Ocupa esos, jeje

RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;

GPIOA->CRL &= ~(GPIO_CRL_MODE1 | GPIO_CRL_CNF1);
GPIOA->CRL |= (GPIO_CRL_MODE1_1 | GPIO_CRL_MODE1_0 | GPIO_CRL_CNF1_1);

TIM2->PSC = 1 - 1;
TIM2->ARR = 255;
TIM2->CCMR1 |= TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2PE;
TIM2->CCER |= TIM_CCER_CC2E;
TIM2->CR1 |= TIM_CR1_CEN;
}

void set_motor_speed(uint8_t duty_cycle) { TIM2->CCR2 = duty_cycle; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Esto también debería de ser a través del handler


void gpio_init() {
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;

GPIOA->CRL &= ~(GPIO_CRL_MODE0 | GPIO_CRL_CNF0);
GPIOA->CRL |= GPIO_CRL_CNF0_1;
GPIOA->ODR |= HEADLIGHT_PIN;
}

void motor_control_task() {
if (!(GPIOA->IDR & HEADLIGHT_PIN)) {
set_motor_speed(178);
} else {
set_motor_speed(0);
}
}

int main(void) {
gpio_init();
pwm_init();

osThreadDef(THREAD_1, motor_control_task, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Esto está bien, sólo faltaría condicionar para que con el semaphore, se prenda y se apaque 👀

osThreadId LEDThread1Handle = osThreadCreate(osThread(THREAD_1), NULL);

osKernelStart();

while (1) {
motor_control_task();
}
}