project additions/changes

This commit is contained in:
2022-02-18 10:21:19 -06:00
parent a7303fe5f9
commit debbd37501
26 changed files with 1906 additions and 690 deletions

28
labW9barnestr/Src/delay.c Normal file
View File

@@ -0,0 +1,28 @@
/*
* delay.c
*
* Created on: Dec 10, 2021
* Author: Trevor Barnes
*/
#include <inttypes.h>
#include "delay.h" //include declaration header file
void delay_1ms(uint32_t n){
// 1ms = 16,000 ticks
for (int i = n ; i > 0 ; i--) {
// Clear value register
*STK_VAL = 0x0000;
// Store 16,000 in STK_LOAD
*STK_LOAD = 16000;
// Enable clock, no prescaler, no interrupt
*STK_CTRL |= CLKSOURCE;
*STK_CTRL |= EN;
// Loop n times: Wait for countflag high
int flag;
do {
flag = ((*STK_CTRL & (1<<16))>>16);
} while (flag != 1);
}
}

147
labW9barnestr/Src/led.c Normal file
View File

@@ -0,0 +1,147 @@
/*
* led.c
*
* Created on: Dec 10, 2021
* Author: Trevor Barnes
*/
#include "led.h"
#include "delay.h"
#include <inttypes.h>
#include <stdio.h>
int ledSpeed = 5;
void led_init(){
// Initialize corresponding RCC and GPIO registers
*RCC_AHB1ENR |= (1<<GPIOBEN);
*GPIOB_MODER &= ~(0x3FFF<<10);
*GPIOB_MODER |= (0x555<<10);
*GPIOB_MODER &= ~(0xFF<<24);
*GPIOB_MODER |= (0x55<<24);
}
void led_allOn(){
// Set all LED Bits
*GPIOB_ODR |= ALL_LEDS;
}
void led_allOff(){
// Reset all LED bits
*GPIOB_ODR &= ~(ALL_LEDS);
}
void led_on(uint8_t ledIndex){
// Set individual LED based on passed in index
switch (ledIndex) {
case 0:
*GPIOB_BSRR = (1<<5);
break;
case 1:
*GPIOB_BSRR = (1<<6);
break;
case 2:
*GPIOB_BSRR = (1<<7);
break;
case 3:
*GPIOB_BSRR = (1<<8);
break;
case 4:
*GPIOB_BSRR = (1<<9);
break;
case 5:
*GPIOB_BSRR = (1<<10);
break;
case 6:
*GPIOB_BSRR = (1<<12);
break;
case 7:
*GPIOB_BSRR = (1<<13);
break;
case 8:
*GPIOB_BSRR = (1<<14);
break;
case 9:
*GPIOB_BSRR = (1<<15);
break;
default:
printf("LED index out of range\n\r");
break;
}
}
void led_off(uint8_t ledIndex){
// Reset individual LED based on passed in index
if (ledIndex < 6) {
*GPIOB_BSRR = (1<<(21+ledIndex));
} else if (ledIndex >= 6) {
// Add pin offset to index
*GPIOB_BSRR = (1<<(22+ledIndex));
} else {
printf("LED index out of range\n\r");
}
}
void led_scan(){
led_allOff();
// Right to left each LED
for (int i = 0; i <= 9 ; i++) {
// Scaled Delay
delay_1ms(50+(ledSpeed*50));
if(i != 0){
led_off(i-1);
}
led_on(i);
}
// Left to right each LED
for (int i = 9; i >= 0; i--) {
if(i != 9){
led_off(i+1);
}
led_on(i);
// Scaled Delay
delay_1ms(50+(ledSpeed*50));
}
led_off(0);
}
void led_flash(){
// Flash LED on and off 10 times at a speed between 0-1 seconds
for (int i = 0; i < 10; i++) {
led_allOn();
delay_1ms(100+(ledSpeed*100));
led_allOff();
delay_1ms(100+(ledSpeed*100));
}
}
void led_setSpeed(uint8_t speed){
ledSpeed = speed;
}
void led_incSpeed(){
if (ledSpeed == 0){
printf("Speed too fast\n\r");
} else {
ledSpeed--;
}
}
void led_decSpeed(){
if (ledSpeed == 9){
printf("Speed too slow\n\r");
} else {
ledSpeed++;
}
}
uint8_t getCurrentSpeed()
{
return ledSpeed;
}

View File

@@ -1,17 +1,23 @@
/**
******************************************************************************
* @file main.c
* @author Auto-generated by STM32CubeIDE
* @version V1.0
* @brief Default main function.
******************************************************************************
*/
* @file main.c
* @author Trevor Barnes
* @brief
* @version 0.1
* @date 2022-02-11
*
* @copyright Copyright (c) 2022
*
*/
#if !defined(__SOFT_FP__) && defined(__ARM_FP)
#warning "FPU is not initialized, but the project is compiling for an FPU. Please initialize the FPU before use."
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "uart_driver.h"
#include "delay.h"
#include "led.h"
#include "tasker.h"
int main(void) {
int main(void)
{
for(;;);
}

View File

@@ -1,184 +0,0 @@
/**
*****************************************************************************
**
** File : syscalls.c
**
** Author : Auto-generated by STM32CubeIDE
**
** Abstract : STM32CubeIDE Minimal System calls file
**
** For more information about which c-functions
** need which of these lowlevel functions
** please consult the Newlib libc-manual
**
** Environment : STM32CubeIDE MCU
**
** Distribution: The file is distributed as is, without any warranty
** of any kind.
**
*****************************************************************************
**
** <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
**
** Redistribution and use in source and binary forms, with or without modification,
** are permitted provided that the following conditions are met:
** 1. Redistributions of source code must retain the above copyright notice,
** this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright notice,
** this list of conditions and the following disclaimer in the documentation
** and/or other materials provided with the distribution.
** 3. Neither the name of STMicroelectronics nor the names of its contributors
** may be used to endorse or promote products derived from this software
** without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
**
*****************************************************************************
*/
/* Includes */
#include <sys/stat.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>
/* Variables */
//#undef errno
extern int errno;
extern int __io_putchar(int ch) __attribute__((weak));
extern int __io_getchar(void) __attribute__((weak));
register char * stack_ptr asm("sp");
char *__env[1] = { 0 };
char **environ = __env;
/* Functions */
void initialise_monitor_handles()
{
}
int _getpid(void)
{
return 1;
}
int _kill(int pid, int sig)
{
errno = EINVAL;
return -1;
}
void _exit (int status)
{
_kill(status, -1);
while (1) {} /* Make sure we hang here */
}
__attribute__((weak)) int _read(int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
*ptr++ = __io_getchar();
}
return len;
}
__attribute__((weak)) int _write(int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
__io_putchar(*ptr++);
}
return len;
}
int _close(int file)
{
return -1;
}
int _fstat(int file, struct stat *st)
{
st->st_mode = S_IFCHR;
return 0;
}
int _isatty(int file)
{
return 1;
}
int _lseek(int file, int ptr, int dir)
{
return 0;
}
int _open(char *path, int flags, ...)
{
/* Pretend like we always fail */
return -1;
}
int _wait(int *status)
{
errno = ECHILD;
return -1;
}
int _unlink(char *name)
{
errno = ENOENT;
return -1;
}
int _times(struct tms *buf)
{
return -1;
}
int _stat(char *file, struct stat *st)
{
st->st_mode = S_IFCHR;
return 0;
}
int _link(char *old, char *new)
{
errno = EMLINK;
return -1;
}
int _fork(void)
{
errno = EAGAIN;
return -1;
}
int _execve(char *name, char **argv, char **env)
{
errno = ENOMEM;
return -1;
}

View File

@@ -1,83 +0,0 @@
/**
*****************************************************************************
**
** File : sysmem.c
**
** Author : Auto-generated by STM32CubeIDE
**
** Abstract : STM32CubeIDE Minimal System Memory calls file
**
** For more information about which c-functions
** need which of these lowlevel functions
** please consult the Newlib libc-manual
**
** Environment : STM32CubeIDE MCU
**
** Distribution: The file is distributed as is, without any warranty
** of any kind.
**
*****************************************************************************
**
** <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
**
** Redistribution and use in source and binary forms, with or without modification,
** are permitted provided that the following conditions are met:
** 1. Redistributions of source code must retain the above copyright notice,
** this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright notice,
** this list of conditions and the following disclaimer in the documentation
** and/or other materials provided with the distribution.
** 3. Neither the name of STMicroelectronics nor the names of its contributors
** may be used to endorse or promote products derived from this software
** without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
**
*****************************************************************************
*/
/* Includes */
#include <errno.h>
#include <stdio.h>
/* Variables */
extern int errno;
register char * stack_ptr asm("sp");
/* Functions */
/**
_sbrk
Increase program data space. Malloc and related functions depend on this
**/
caddr_t _sbrk(int incr)
{
extern char end asm("end");
static char *heap_end;
char *prev_heap_end;
if (heap_end == 0)
heap_end = &end;
prev_heap_end = heap_end;
if (heap_end + incr > stack_ptr)
{
errno = ENOMEM;
return (caddr_t) -1;
}
heap_end += incr;
return (caddr_t) prev_heap_end;
}

View File

@@ -0,0 +1,76 @@
/**
* @file tasker.c
* @author Trevor Barnes
* @brief
* @version 0.1
* @date 2022-02-11
*
* @copyright Copyright (c) 2022
*
*/
#include <stdio.h>
#include <inttypes.h>
#include "tasker.h"
void SysTick_Init() {
}
void init_task(uint32_t task_num, uint32_t stacksize, void(*entry_point)(void), uint32_t ticks) {
tasks[task_num].stack_pointer = (uint32_t*)malloc(stacksize*sizeof(uint32_t));
tasks[task_num].stack_pointer += stacksize;
*(--tasks[task_num].stack_pointer) = 0x01000000; // PSR
*(--tasks[task_num].stack_pointer) = ((uint32_t)entry_point);
*(--tasks[task_num].stack_pointer) = 0xFFFFFFFF;
*(--tasks[task_num].stack_pointer) = 0; // R12
*(--tasks[task_num].stack_pointer) = 0; // R3
*(--tasks[task_num].stack_pointer) = 0; // R2
*(--tasks[task_num].stack_pointer) = 0; // R1
*(--tasks[task_num].stack_pointer) = 0; // R0
*(--tasks[task_num].stack_pointer) = 0xFFFFFFF9; // ISR LR
*(--tasks[task_num].stack_pointer) = 0; // R11
*(--tasks[task_num].stack_pointer) = 0; // R10
*(--tasks[task_num].stack_pointer) = 0; // R9
*(--tasks[task_num].stack_pointer) = 0; // R8
*(--tasks[task_num].stack_pointer) = 0; // R7
*(--tasks[task_num].stack_pointer) = 0; // R6
*(--tasks[task_num].stack_pointer) = 0; // R5
*(--tasks[task_num].stack_pointer) = 0; // R4
tasks[task_num].state = ACTIVE;
tasks[task_num].ticks_starting = ticks;
tasks[task_num].ticks_remaining = 0;
}
void init_tasker(uint32_t total_tasks, uint32_t main_ticks) {
num_tasks = total_tasks;
*tasks = calloc(total_tasks, sizeof(Task));
}
void tasker_tick() {
tasks[current_task].ticks_remaining--;
if(tasks[current_task].ticks_remaining == 0) {
int i = 1;
while(tasks[(next_task = (current_task + i)%num_tasks)].state != ACTIVE) {
i++;
}
tasks[next_task].ticks_remaining = tasks[next_task].ticks_starting;
//*scb_icsr |= 1<<PENDSVSET;
}
}
void SysTick_Handler(void) {
}
void PendSV_Handler(void) {
register uint32_t* stack_pointer asm("r13");
asm volatile("push {r4-r11,lr}");
tasks[current_task].stack_pointer = stack_pointer;
current_task = next_task;
stack_pointer = tasks[current_task].stack_pointer;
asm volatile("pop {r4-r11,lr}\n\t"
"bx lr");
}

View File

@@ -0,0 +1,92 @@
/*
* uart_driver.c
*
* Created on: Nov 8, 2016
* Author: barnekow
*/
#include "uart_driver.h"
#include <inttypes.h>
#include <stdio.h>
// These will override _read and _write in syscalls.c, which are
// prototyped as weak
int _read(int file, char *ptr, int len)
{
int DataIdx;
// Modified the for loop in order to get the correct behavior for fgets
int byteCnt = 0;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
//*ptr++ = __io_getchar();
byteCnt++;
//*ptr++ = usart2_getch();
*ptr = usart2_getch();
if(*ptr == '\n') break;
ptr++;
}
//return len;
return byteCnt; // Return byte count
}
int _write(int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
usart2_putch(*ptr++);
}
return len;
}
char usart2_getch(){
char c;
while((*(USART_SR)&(1<<RXNE)) != (1<<RXNE));
c = ((char) *USART_DR); // Read character from usart
usart2_putch(c); // Echo back
if (c == '\r'){ // If character is CR
usart2_putch('\n'); // send it
c = '\n'; // Return LF. fgets is terminated by LF
}
return c;
}
void usart2_putch(char c){
while((*(USART_SR)&(1<<TXE)) != (1<<TXE));
*(USART_DR) = c;
}
void init_usart2(uint32_t baud, uint32_t sysclk){
// Enable clocks for GPIOA and USART2
*(RCC_AHB1ENR) |= (1<<GPIOAEN);
*(RCC_APB1ENR) |= (1<<USART2EN);
// Function 7 of PORTA pins is USART
*(GPIOA_AFRL) &= (0xFFFF00FF); // Clear the bits associated with PA3 and PA2
*(GPIOA_AFRL) |= (0b01110111<<8); // Choose function 7 for both PA3 and PA2
*(GPIOA_MODER) &= (0xFFFFFF0F); // Clear mode bits for PA3 and PA2
*(GPIOA_MODER) |= (0b1010<<4); // Both PA3 and PA2 in alt function mode
// Set up USART2
//USART2_init(); //8n1 no flow control
// over8 = 0..oversample by 16
// M = 0..1 start bit, data size is 8, 1 stop bit
// PCE= 0..Parity check not enabled
// no interrupts... using polling
*(USART_CR1) = (1<<UE)|(1<<TE)|(1<<RE); // Enable UART, Tx and Rx
*(USART_CR2) = 0; // This is the default, but do it anyway
*(USART_CR3) = 0; // This is the default, but do it anyway
*(USART_BRR) = sysclk/baud;
/* I'm not sure if this is needed for standard IO*/
//setvbuf(stderr, NULL, _IONBF, 0);
//setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
}