add projects
This commit is contained in:
66
SerialConsoleTemplate/Src/main.c
Normal file
66
SerialConsoleTemplate/Src/main.c
Normal file
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file main.c
|
||||
* @author Auto-generated by STM32CubeIDE
|
||||
* @version V1.0
|
||||
* @brief Default main function.
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "uart_driver.h"
|
||||
|
||||
#define F_CPU 16000000UL
|
||||
|
||||
#define ARSIZE 10
|
||||
|
||||
main(){
|
||||
init_usart2(57600,F_CPU);
|
||||
int ch_arr[ARSIZE],count1;
|
||||
int count2, stop, lastchar;
|
||||
|
||||
lastchar = 0;
|
||||
stop = 0;
|
||||
/*
|
||||
* Read characters into array.
|
||||
* Stop if end of line, or array full.
|
||||
*/
|
||||
while(stop != 1){
|
||||
ch_arr[lastchar] = getchar();
|
||||
if(ch_arr[lastchar] == '\n')
|
||||
stop = 1;
|
||||
else
|
||||
lastchar = lastchar + 1;
|
||||
if(lastchar == ARSIZE)
|
||||
stop = 1;
|
||||
}lastchar = lastchar-1;
|
||||
/*
|
||||
* Now the traditional bubble sort.
|
||||
*/
|
||||
count1 = 0;
|
||||
while(count1 < lastchar){
|
||||
count2 = count1 + 1;
|
||||
while(count2 <= lastchar){
|
||||
if(ch_arr[count1] > ch_arr[count2]){
|
||||
/* swap */
|
||||
int temp;
|
||||
temp = ch_arr[count1];
|
||||
ch_arr[count1] = ch_arr[count2];
|
||||
ch_arr[count2] = temp;
|
||||
}
|
||||
count2 = count2 + 1;
|
||||
}
|
||||
count1 = count1 + 1;
|
||||
}
|
||||
|
||||
count1 = 0;
|
||||
while(count1 <= lastchar){
|
||||
printf("%c\n", ch_arr[count1]);
|
||||
count1 = count1 + 1;
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
184
SerialConsoleTemplate/Src/syscalls.c
Normal file
184
SerialConsoleTemplate/Src/syscalls.c
Normal file
@@ -0,0 +1,184 @@
|
||||
/**
|
||||
*****************************************************************************
|
||||
**
|
||||
** 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>© 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;
|
||||
}
|
||||
83
SerialConsoleTemplate/Src/sysmem.c
Normal file
83
SerialConsoleTemplate/Src/sysmem.c
Normal file
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
*****************************************************************************
|
||||
**
|
||||
** 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>© 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;
|
||||
}
|
||||
|
||||
92
SerialConsoleTemplate/Src/uart_driver.c
Normal file
92
SerialConsoleTemplate/Src/uart_driver.c
Normal 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user