1
0

Hello TOR 2k24

Have fun 🚀
This commit is contained in:
2024-04-10 19:17:57 +02:00
commit f821a46ea0
62 changed files with 21518 additions and 0 deletions

View File

@ -0,0 +1,38 @@
#include "stm32f7xx_hal.h"
#include "clock_216.h"
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Setup system clock to 216MHz
//------------------------------------------------------------------------------
void SystemClock_Config (void) {
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
/* Enable HSE Oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 432;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 9;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
/* Activate the OverDrive to reach the 216 MHz Frequency */
HAL_PWREx_EnableOverDrive();
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7);
}

View File

@ -0,0 +1,17 @@
/************************************************************************//**
* \file clock_216.h
* \brief Function to set the system clock to 216 MHz
* \author pascal (dot) sartoretti (at) hevs (dot) ch
***************************************************************************/
#ifndef __CLOCK_216_H
#define __CLOCK_216_H
#include <stdint.h>
/************************************************************************//**
* \brief Inits the system clock to 216 MHz
***************************************************************************/
extern void SystemClock_Config (void);
#endif /* __CLOCK_216_H */

View File

@ -0,0 +1,58 @@
#include "stm32f7xx_hal.h"
#include "ext_buttons.h"
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
int32_t Ext_Buttons_Init (void) {
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOI_CLK_ENABLE();
/* Configure GPIO pin: PI2 (BTN_0) */
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);
/* Configure GPIO pin: PI3 (BTN_1) */
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);
/* Configure GPIO pin: PG7 (BTN_2) */
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
/* Configure GPIO pin: PG6 (BTN_3) */
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
return 0;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
uint32_t Ext_Buttons_GetState (void) {
uint32_t val = 0;
if (HAL_GPIO_ReadPin(GPIOI, GPIO_PIN_2) == GPIO_PIN_RESET) {
val |= 1;
}
if (HAL_GPIO_ReadPin(GPIOI, GPIO_PIN_3) == GPIO_PIN_RESET) {
val |= 2;
}
if (HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_7) == GPIO_PIN_RESET) {
val |= 4;
}
if (HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_6) == GPIO_PIN_RESET) {
val |= 8;
}
return val;
}

View File

@ -0,0 +1,23 @@
/************************************************************************//**
* \file ext_buttons.h
* \brief Function to use the extension uart
* \author pascal (dot) sartoretti (at) hevs (dot) ch
***************************************************************************/
#ifndef __EXT_BUTTONS_H
#define __EXT_BUTTONS_H
#include <stdint.h>
/************************************************************************//**
* \brief Inits the external buttons usage.
* \return Always #0
***************************************************************************/
extern int32_t Ext_Buttons_Init(void);
/************************************************************************//**
* \brief Reads the buttons status
* \return The binary state of the buttons (example 4 for button 2 pressed).
***************************************************************************/
extern uint32_t Ext_Buttons_GetState(void);
#endif /* __BOARD_BUTTONS_H */

View File

@ -0,0 +1,579 @@
#include "stm32f7xx_hal.h"
#include "ext_keyboard.h"
#define TRUE 1
#define FALSE 0
#define NORMAL 0
#define SHIFT 1
#define CONTROL 2
#define ALT 3
#define TABULATOR 0x09
#define BACKSPACE 0x08
#define ESCAPE 0x1B
#define CR 0x0D
#define F1 0x10
#define F2 0x11
#define F3 0x12
#define F4 0x13
#define F5 0x14
#define F6 0x15
#define F7 0x16
#define F8 0x17
#define F9 0x18
#define F10 0x19
#define F11 0x1A
#define F12 0x1B
static SPI_HandleTypeDef keyboard; // SPI keyboard handle
uint8_t ext_kbChar; // exported to global use
uint8_t scan;
static uint32_t newChar=0; // internal flag
uint8_t release;
uint8_t state;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
uint8_t ScanCodeAnalyse(uint8_t scan)
{
switch(scan)
{
/*---------------------------------------------------------------------------*/
case 0xF0: release = TRUE; return 0 ; /* release key code */
/*---------------------------------------------------------------------------*/
case 0x14: if (release == FALSE)
state = CONTROL;
else
{
state = NORMAL;
release = FALSE;
}
return 0x00;
/*---------------------------------------------------------------------------*/
case 0x11: if (release == FALSE)
state = ALT;
else
{
state = NORMAL;
release = FALSE;
}
return 0x00;
/*---------------------------------------------------------------------------*/
case 0x12:
case 0x58:
case 0x59: if (release == FALSE)
state = SHIFT;
else
{
state = NORMAL;
release = FALSE;
}
return 0x00;
}
/*---------------------------------------------------------------------------*/
if (release == TRUE)
{
release = FALSE;
return 0x00;
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
switch(scan)
{
/*---------------------------------------------------------------------------*/
case 0x05: return F1; // 'F1' to 'F12'
/*---------------------------------------------------------------------------*/
case 0x06: return F2;
/*---------------------------------------------------------------------------*/
case 0x04: return F3;
/*---------------------------------------------------------------------------*/
case 0x0c: return F4;
/*---------------------------------------------------------------------------*/
case 0x03: return F5;
/*---------------------------------------------------------------------------*/
case 0x0B: return F6;
/*---------------------------------------------------------------------------*/
case 0x83: return F7;
/*---------------------------------------------------------------------------*/
case 0x0A: return F8;
/*---------------------------------------------------------------------------*/
case 0x01: return F9;
/*---------------------------------------------------------------------------*/
case 0x09: return F10;
/*---------------------------------------------------------------------------*/
case 0x78: return F11;
/*---------------------------------------------------------------------------*/
case 0x07: return F12;
/*---------------------------------------------------------------------------*/
case 0x16: switch (state) /* key '1' */
{
case NORMAL : return '1';
case CONTROL : return 0;
case SHIFT : return '!';
}
/*---------------------------------------------------------------------------*/
case 0x1E: switch (state) /* key '2' */
{
case NORMAL : return '2';
case CONTROL : return 0;
case SHIFT : return '@';
}
/*---------------------------------------------------------------------------*/
case 0x26: switch (state) /* key '3' */
{
case NORMAL : return '3';
case CONTROL : return 0;
case SHIFT : return '#';
}
/*---------------------------------------------------------------------------*/
case 0x25: switch (state) /* key '4' */
{
case NORMAL : return '4';
case CONTROL : return 0;
case SHIFT : return '$';
}
/*---------------------------------------------------------------------------*/
case 0x2E: switch (state) /* key '5' */
{
case NORMAL : return '5';
case CONTROL : return 0;
case SHIFT : return '%';
}
/*---------------------------------------------------------------------------*/
case 0x36: switch (state) /* key '6' */
{
case NORMAL : return '6';
case CONTROL : return 0;
case SHIFT : return '^';
}
/*---------------------------------------------------------------------------*/
case 0x6C:
case 0x3D: switch (state) /* key '7' */
{
case NORMAL : return '7';
case CONTROL : return 0;
case SHIFT : return '&';
}
/*---------------------------------------------------------------------------*/
case 0x75:
case 0x3E: switch (state) /* key '8' */
{
case NORMAL : return '8';
case CONTROL : return 0;
case SHIFT : return '*';
}
/*---------------------------------------------------------------------------*/
case 0x7D:
case 0x46: switch (state) /* key '9' */
{
case NORMAL : return '9';
case CONTROL : return 0;
case SHIFT : return '(';
}
/*---------------------------------------------------------------------------*/
case 0x7C:
case 0x45: switch (state) /* key '0' */
{
case NORMAL : return '0';
case CONTROL : return 0;
case SHIFT : return ')';
}
/*---------------------------------------------------------------------------*/
case 0x7E: switch (state) /* key '*' */
{
case NORMAL : return '*';
case CONTROL : return F11;
case SHIFT : return '?';
default : return 0xBF; /* ? vertical flipped */
}
/*---------------------------------------------------------------------------*/
case 0x4E: switch (state) /* key '-' */
{
case NORMAL : return '-';
case CONTROL : return 0;
case SHIFT : return '_';
default : return 0x00;
}
/*---------------------------------------------------------------------------*/
case 0x65: return 0x1B; /* End button -> Escape command */
/*---------------------------------------------------------------------------*/
case 0x15: switch (state) /* key 'Q' */
{
case SHIFT : return 'Q';
case CONTROL : return 0x11;
default : return 'q';
}
/*---------------------------------------------------------------------------*/
case 0x1D: switch (state) /* key 'W' */
{
case SHIFT : return 'W';
case CONTROL : return 0x17;
default : return 'w';
}
/*---------------------------------------------------------------------------*/
case 0x24: switch (state) /* key 'E' */
{
case SHIFT : return 'E';
case CONTROL : return 0x05;
default : return 'e';
}
/*---------------------------------------------------------------------------*/
case 0x2D: switch (state) /* key 'R' */
{
case SHIFT : return 'R';
case CONTROL : return 0x12;
default : return 'r';
}
/*---------------------------------------------------------------------------*/
case 0x2C: switch (state) /* key 'T' */
{
case SHIFT : return 'T';
case CONTROL : return 0x14;
default : return 't';
}
/*---------------------------------------------------------------------------*/
case 0x35: switch (state) /* key 'Z' */
{
case SHIFT : return 'Y';
case CONTROL : return 0x1A;
default : return 'y';
}
/*---------------------------------------------------------------------------*/
case 0x6B:
case 0x3C: switch (state) /* key 'U' */
{
case SHIFT : return 'U';
case CONTROL : return 0x15;
default : return 'u';
}
/*---------------------------------------------------------------------------*/
case 0x73:
case 0x43: switch (state) /* key 'I' */
{
case SHIFT : return 'I';
case CONTROL : return 0x09;
default : return 'i';
}
/*---------------------------------------------------------------------------*/
case 0x74:
case 0x44: switch (state) /* key 'O' */
{
case SHIFT : return 'O';
case CONTROL : return 0x0F;
default : return 'o';
}
/*---------------------------------------------------------------------------*/
case 0x7B:
case 0x4D: switch (state) /* key 'P' */
{
case SHIFT : return 'P';
case CONTROL : return 0x10;
default : return 'p';
}
/*---------------------------------------------------------------------------*/
case 0x5B: switch (state) /* key '<27>' */
{
case SHIFT : return '}';
case ALT : return 0;
default : return ']';
}
/*---------------------------------------------------------------------------*/
case 0x66: return 0x08; /* Backspace */
/*---------------------------------------------------------------------------*/
case 0x0D: return 0x09; /* Horizontal tabulator */
/*---------------------------------------------------------------------------*/
case 0x1C: switch (state) /* key 'A' */
{
case SHIFT : return 'A';
case CONTROL : return 0x01;
default : return 'a';
}
/*---------------------------------------------------------------------------*/
case 0x1B: switch (state) /* key 'S' */
{
case SHIFT : return 'S';
case CONTROL : return 0x13;
default : return 's';
}
/*---------------------------------------------------------------------------*/
case 0x23: switch (state) /* key 'D' */
{
case SHIFT : return 'D';
case CONTROL : return 0x04;
default : return 'd';
}
/*---------------------------------------------------------------------------*/
case 0x2B: switch (state) /* key 'F' */
{
case SHIFT : return 'F';
case CONTROL : return 0x06;
default : return 'f';
}
/*---------------------------------------------------------------------------*/
case 0x34: switch (state) /* key 'G' */
{
case SHIFT : return 'G';
case CONTROL : return 0x07;
default : return 'g';
}
/*---------------------------------------------------------------------------*/
case 0x33: switch (state) /* key 'H' */
{
case SHIFT : return 'H';
case CONTROL : return 0x08;
default : return 'h';
}
/*---------------------------------------------------------------------------*/
case 0x69:
case 0x3B: switch (state) /* key 'J' */
{
case SHIFT : return 'J';
case CONTROL : return 0x0A;
default : return 'j';
}
/*---------------------------------------------------------------------------*/
case 0x72:
case 0x42: switch (state) /* key 'K' */
{
case SHIFT : return 'K';
case CONTROL : return 0x0B;
default : return 'k';
}
/*---------------------------------------------------------------------------*/
case 0x7A:
case 0x4B: switch (state) /* key 'L' */
{
case SHIFT : return 'L';
case CONTROL : return 0x0C;
default : return 'l';
}
/*---------------------------------------------------------------------------*/
case 0x54: switch (state) /* key '[' */
{
case SHIFT : return '{';
case ALT : return 0; /* beta symbol */
default : return '[';
}
/*---------------------------------------------------------------------------*/
case 0x52: switch (state) /* key ''' */
{
case SHIFT : return '"';
case ALT : return 0;
default : return 0x27;
}
/*---------------------------------------------------------------------------*/
case 0x1A: switch (state) /* key 'Y' */
{
case SHIFT : return 'Z';
case CONTROL : return 0x19;
default : return 'z';
}
/*---------------------------------------------------------------------------*/
case 0x22: switch (state) /* key 'X' */
{
case SHIFT : return 'X';
case CONTROL : return 0x18;
default : return 'x';
}
/*---------------------------------------------------------------------------*/
case 0x21: switch (state) /* key 'C' */
{
case SHIFT : return 'C';
case CONTROL : return 0x03;
default : return 'c';
}
/*---------------------------------------------------------------------------*/
case 0x2A: switch (state) /* key 'V' */
{
case SHIFT : return 'V';
case CONTROL : return 0x16;
default : return 'v';
}
/*---------------------------------------------------------------------------*/
case 0x32: switch (state) /* key 'B' */
{
case SHIFT : return 'B';
case CONTROL : return 0x02;
default : return 'b';
}
/*---------------------------------------------------------------------------*/
case 0x31: switch (state) /* key 'N' */
{
case SHIFT : return 'N';
case CONTROL : return 0x0E;
default : return 'n';
}
/*---------------------------------------------------------------------------*/
case 0x70:
case 0x3A: switch (state) /* key 'M' */
{
case SHIFT : return 'M';
case CONTROL : return 0x0D;
default : return 'm';
}
/*---------------------------------------------------------------------------*/
case 0x41: switch (state) /* key ',' */
{
case SHIFT : return '<';
default : return ',';
}
/*---------------------------------------------------------------------------*/
case 0x49: switch (state) /* key '.' */
{
case SHIFT : return '>';
default : return '.';
}
/*---------------------------------------------------------------------------*/
case 0x5A: return 0x0D; /* Carriage return (Enter) */
/*---------------------------------------------------------------------------*/
case 0x29: return ' '; /* Spacebar */
/*---------------------------------------------------------------------------*/
case 0x63: switch (state) /* key 'UP ARROW' */
{
case SHIFT : return 'U';
default : return 'u';
}
/*---------------------------------------------------------------------------*/
case 0x60: switch (state) /* key 'DOWN ARROW' */
{
case SHIFT : return 'D';
default : return 'd';
}
/*---------------------------------------------------------------------------*/
case 0x61: switch (state) /* key 'LEFT ARROW' */
{
case SHIFT : return 'L';
default : return 'l';
}
/*---------------------------------------------------------------------------*/
case 0x6A: switch (state) /* key 'RIGHT ARROW' */
{
case SHIFT : return 'R';
default : return 'r';
}
/*---------------------------------------------------------------------------*/
case 0x0E: switch (state) /* key 'apostroph' */
{
case SHIFT : return '~';
default : return '`';
}
/*---------------------------------------------------------------------------*/
case 0x79:
case 0x4C: switch (state) /* key ';' */
{
case SHIFT : return ':';
default : return ';';
}
/*---------------------------------------------------------------------------*/
case 0x4A: switch (state) /* key '/' */
{
case SHIFT : return '?';
default : return '/';
}
/*---------------------------------------------------------------------------*/
case 0x5D: switch (state) /* key '\' */
{
case SHIFT : return '|';
default : return '\\';
}
/*---------------------------------------------------------------------------*/
case 0x55: switch (state) /* key '=' */
{
case SHIFT : return '+';
default : return '=';
}
}
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void EXTI9_5_IRQHandler(void)
{
// read keyboard data and store it on global variable
HAL_GPIO_WritePin(GPIOI, GPIO_PIN_0, GPIO_PIN_RESET); // SPI CS activate
HAL_SPI_Receive(&keyboard,&scan,1,100); // SPI read
HAL_GPIO_WritePin(GPIOI, GPIO_PIN_0, GPIO_PIN_SET); // SPI CS desactivate
// process irq
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_8); // clear irq and callback
ext_kbChar = ScanCodeAnalyse(scan);
if(ext_kbChar != 0)
{
newChar = 1;
}
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void Ext_Keyboard_Init(void)
{
volatile uint8_t dummy;
GPIO_InitTypeDef GPIO_InitStruct;
/* Peripheral clock enable */
__HAL_RCC_GPIOI_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_SPI2_CLK_ENABLE();
/**SPI2 GPIO Configuration
PI1 ------> SPI2_SCK
PB14 ------> SPI2_MISO
PB15 ------> SPI2_MOSI
PI0 ------> /SPI_CB_SS
PF8 ------> /SPI_INT
*/
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
keyboard.Instance = SPI2;
keyboard.Init.Mode = SPI_MODE_MASTER;
keyboard.Init.Direction = SPI_DIRECTION_2LINES;
keyboard.Init.DataSize = SPI_DATASIZE_8BIT;
keyboard.Init.CLKPolarity = SPI_POLARITY_HIGH;
keyboard.Init.CLKPhase = SPI_PHASE_1EDGE;
keyboard.Init.NSS = SPI_NSS_SOFT;
keyboard.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; //3.333MHz -> (Fosc/4) / 16
keyboard.Init.FirstBit = SPI_FIRSTBIT_MSB;
keyboard.Init.TIMode = SPI_TIMODE_DISABLE;
keyboard.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
keyboard.Init.CRCPolynomial = 7;
keyboard.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
keyboard.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
HAL_SPI_Init(&keyboard);
// dummy read to clear any glitch
HAL_GPIO_WritePin(GPIOI, GPIO_PIN_0, GPIO_PIN_RESET);
HAL_SPI_Receive(&keyboard,(uint8_t *)&dummy,1,10);
HAL_GPIO_WritePin(GPIOI, GPIO_PIN_0, GPIO_PIN_SET);
// enable interrupts on keyboard ISR
HAL_NVIC_SetPriority(EXTI9_5_IRQn,5,5);
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
uint8_t Ext_Keyboard_Read(void)
{
while(newChar == 0) // wait new char coming
{}
newChar = 0; // clear it
return ext_kbChar; // return read char
}

View File

@ -0,0 +1,53 @@
/************************************************************************//**
* \file ext_keyboard.h
* \brief Function to use the extension keyboard
* \author pascal (dot) sartoretti (at) hevs (dot) ch
***************************************************************************/
#ifndef __EXT_KEYBOARD_H
#define __EXT_KEYBOARD_H
#include <stdint.h>
#include "stm32f7xx_hal.h"
extern uint8_t ext_kbChar;
/************************************************************************//**
* \brief Inits the extension keyboard
* The extension keyboard use interrupt from keyboard (PF8)
*
* Read keyboard non blocking (interrupt):
* ---------------------------------------
* To read the keyboard, the callback function HAL_GPIO_EXTI_Callback has
* to be implemented.
* the example below send the keyboard char to the serial port
* void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
* {
* if(GPIO_Pin == GPIO_PIN_8)
* {
* HAL_UART_Transmit(&ext_uart, &ext_kbChar, 1,100);;
* }
* }
* Read keyboard blocking (pooling until key pressed):
* ---------------------------------------------------
* The functions below have to be used:
* pressed = Ext_Keyboard_Read();
*
* \warning The external interrupts (5,6,7,9) have to be implemented
* in the ext_keyboard.c file because they share the same processor irq
* EXTI9_5_IRQHandler.
***************************************************************************/
void Ext_Keyboard_Init(void);
/************************************************************************//**
* \brief Read the pressed key on extension keyboard
* \return The ASCII code of key pressed.
*
* \warning This function is blocking until a char is received
***************************************************************************/
uint8_t Ext_Keyboard_Read(void);
#endif /* __BOARD_LED_H */

224
RTE/Hesso_pack/ext_led.c Normal file
View File

@ -0,0 +1,224 @@
#include "stm32f7xx_hal.h"
#include "ext_led.h"
#include <stdint.h>
//------------------------------------------------------------------------------
static const uint8_t cie1931[101] =
{
0, 0, 1, 1, 1, 1, 2, 2, 2, 3,
3, 3, 4, 4, 4, 5, 5, 6, 6, 7,
8, 8, 9, 10, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 22, 23, 24, 26, 27,
29, 30, 32, 34, 35, 37, 39, 41, 43, 45,
47, 49, 51, 54, 56, 58, 61, 64, 66, 69,
72, 75, 78, 81, 84, 87, 90, 93, 97, 100,
104, 108, 111, 115, 119, 123, 127, 131, 136, 140,
145, 149, 154, 159, 163, 168, 173, 179, 184, 189,
195, 200, 206, 212, 217, 223, 230, 236, 242, 248,
255,
};
#if LIGHTNESS_PWM_STEP != 100
#error this cie1931 array supports only 100 steps, feel free to implement your own
#endif
//------------------------------------------------------------------------------
uint8_t lightness_to_pwm(uint8_t percentage)
{
if(percentage > (LIGHTNESS_PWM_STEP-1))
percentage = (LIGHTNESS_PWM_STEP-1);
return cie1931[percentage];
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
int32_t Ext_LED_Init(void) {
GPIO_InitTypeDef GPIO_InitStruct;
//----------------------------------------------------------------------------
// Configure GPIO pin: PA15 (LED0)
__HAL_RCC_GPIOA_CLK_ENABLE(); // enable GPIO timer
__HAL_RCC_TIM2_CLK_ENABLE(); // enable timer 2 clock
GPIO_InitStruct.Pin = GPIO_PIN_15; // used pin is PA15
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // alternate function use
GPIO_InitStruct.Pull = GPIO_NOPULL; // no pullup
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;// timer 2 is used
GPIO_InitStruct.Speed = GPIO_SPEED_FAST; // speed is fast
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
TIM2->CCER = TIM_CCER_CC1E; // compare for PWM usage
TIM2->PSC = 16; // timer prescaler
TIM2->ARR = 255; // max count value
TIM2->CCR1 = lightness_to_pwm(0); // duty cycle
TIM2->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1PE;
TIM2->EGR |= TIM_EGR_UG; // update register now
TIM2->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN; // start the timer
//----------------------------------------------------------------------------
// Configure GPIO pin: PH6 (LED1)
__HAL_RCC_GPIOH_CLK_ENABLE(); // enable GPIO timer
__HAL_RCC_TIM12_CLK_ENABLE(); // enable timer 12 clock
GPIO_InitStruct.Pin = GPIO_PIN_6; // used pin is PH6
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // alternate function use
GPIO_InitStruct.Pull = GPIO_NOPULL; // no pullup
GPIO_InitStruct.Alternate = GPIO_AF9_TIM12;// timer 12 is used
GPIO_InitStruct.Speed = GPIO_SPEED_FAST; // speed is fast
HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
TIM12->CCER = TIM_CCER_CC1E; // compare for PWM usage
TIM12->PSC = 16; // timer prescaler
TIM12->ARR = 255; // max count value
TIM12->CCR1 = lightness_to_pwm(0); // duty cycle
TIM12->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1PE;
TIM12->EGR |= TIM_EGR_UG; // update register now
TIM12->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN; // start the timer
//----------------------------------------------------------------------------
// Configure GPIO pin: PA8 (LED2)
__HAL_RCC_GPIOA_CLK_ENABLE(); // enable GPIO timer
__HAL_RCC_TIM1_CLK_ENABLE(); // enable timer 1 clock
GPIO_InitStruct.Pin = GPIO_PIN_8; // used pin is PA8
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // alternate function use
GPIO_InitStruct.Pull = GPIO_NOPULL; // no pullup
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;// timer 5 is used
GPIO_InitStruct.Speed = GPIO_SPEED_FAST; // speed is fast
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
TIM1->CCER = TIM_CCER_CC1E; // compare for PWM usage
TIM1->PSC = 16; // timer prescaler
TIM1->ARR = 255; // max count value
TIM1->CCR1 = lightness_to_pwm(0); // duty cycle
TIM1->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1PE;
TIM1->EGR |= TIM_EGR_UG; // update register now
TIM1->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN; // start the timer
TIM1->BDTR = TIM_BDTR_MOE; // master output enable
//----------------------------------------------------------------------------
// Configure GPIO pin: PB4 (LED3)
__HAL_RCC_GPIOB_CLK_ENABLE(); // enable GPIO timer
__HAL_RCC_TIM3_CLK_ENABLE(); // enable timer 3 clock
GPIO_InitStruct.Pin = GPIO_PIN_4; // used pin is PB4
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // alternate function use
GPIO_InitStruct.Pull = GPIO_NOPULL; // no pullup
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;// timer 3 is used
GPIO_InitStruct.Speed = GPIO_SPEED_FAST; // speed is fast
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
TIM3->CCER = TIM_CCER_CC1E; // compare for PWM usage
TIM3->PSC = 16; // timer prescaler
TIM3->ARR = 255; // max count value
TIM3->CCR1 = lightness_to_pwm(0); // duty cycle
TIM3->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1PE;
TIM3->EGR |= TIM_EGR_UG; // update register now
TIM3->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN; // start the timer
return 0;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
int32_t Ext_LED_On (uint32_t num) {
if((num & 1) != 0)
{
TIM2->CCR1 = lightness_to_pwm(LIGHTNESS_PWM_STEP);
}
if((num & 2) != 0)
{
TIM12->CCR1 = lightness_to_pwm(LIGHTNESS_PWM_STEP);
}
if((num & 4) != 0)
{
TIM1->CCR1 = lightness_to_pwm(LIGHTNESS_PWM_STEP);
}
if((num & 8) != 0)
{
TIM3->CCR1 = lightness_to_pwm(LIGHTNESS_PWM_STEP);
}
return 0;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
int32_t Ext_LED_Off (uint32_t num) {
if((num & 1) != 0)
{
TIM2->CCR1 = lightness_to_pwm(0);
}
if((num & 2) != 0)
{
TIM12->CCR1 = lightness_to_pwm(0);
}
if((num & 4) != 0)
{
TIM1->CCR1 = lightness_to_pwm(0);
}
if((num & 8) != 0)
{
TIM3->CCR1 = lightness_to_pwm(0);
}
return 0;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
int32_t Ext_LED_PWM (uint32_t num, uint32_t duty) {
if((num & 1) != 0)
{
TIM2->CCR1 = lightness_to_pwm(duty);
}
if((num & 2) != 0)
{
TIM12->CCR1 = lightness_to_pwm(duty);
}
if((num & 4) != 0)
{
TIM1->CCR1 = lightness_to_pwm(duty);
}
if((num & 8) != 0)
{
TIM3->CCR1 = lightness_to_pwm(duty);
}
return 0;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
int32_t Ext_LEDs(uint32_t num) {
if((num & 1) != 0)
{
TIM2->CCR1 = lightness_to_pwm(LIGHTNESS_PWM_STEP);
}
else
{
TIM2->CCR1 = lightness_to_pwm(0);
}
if((num & 2) != 0)
{
TIM12->CCR1 = lightness_to_pwm(LIGHTNESS_PWM_STEP);
}
else
{
TIM12->CCR1 = lightness_to_pwm(0);
}
if((num & 4) != 0)
{
TIM1->CCR1 = lightness_to_pwm(LIGHTNESS_PWM_STEP);
}
else
{
TIM1->CCR1 = lightness_to_pwm(0);
}
if((num & 8) != 0)
{
TIM3->CCR1 = lightness_to_pwm(LIGHTNESS_PWM_STEP);
}
else
{
TIM3->CCR1 = lightness_to_pwm(0);
}
return 0;
}

51
RTE/Hesso_pack/ext_led.h Normal file
View File

@ -0,0 +1,51 @@
/************************************************************************//**
* \file ext_led.h
* \brief Function to use the extension LEDs
* \author pascal (dot) sartoretti (at) hevs (dot) ch
***************************************************************************/
#ifndef __EXT_LED_H
#define __EXT_LED_H
#include <stdint.h>
#define LIGHTNESS_PWM_STEP 100
/************************************************************************//**
* \brief Inits the external Leds usage.
* \return Always #0
***************************************************************************/
extern int32_t Ext_LED_Init (void);
/************************************************************************//**
* \brief Turn on one led.
* \param num The led to turn on (1,2,4,8)
* \return Always 0
***************************************************************************/
extern int32_t Ext_LED_On (uint32_t num);
/************************************************************************//**
* \brief Turn off one led.
* \param num The led to turn off (1,2,4,8)
* \return Always 0
***************************************************************************/
extern int32_t Ext_LED_Off (uint32_t num);
/************************************************************************//**
* \brief Set a power on a led.
* \param num The led to turn set the power (1,2,4,8)
* \param duty The power of the led (0 to 255)
* \return Always 0
***************************************************************************/
extern int32_t Ext_LED_PWM (uint32_t num, uint32_t duty);
/************************************************************************//**
* \brief Set the state on all leds.
* \param val The binary state of the four leds (example 0b1101).
* \return Always 0
***************************************************************************/
extern int32_t Ext_LEDs(uint32_t val);
#endif /* __BOARD_LED_H */

57
RTE/Hesso_pack/ext_uart.c Normal file
View File

@ -0,0 +1,57 @@
#include "stm32f7xx_hal.h"
#include "ext_uart.h"
UART_HandleTypeDef ext_uart; // extension uart handler
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(huart->Instance==USART6)
{
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_USART6_CLK_ENABLE();
/**USART6 GPIO Configuration
PC7 ------> USART6_RX
PC6 ------> USART6_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void Ext_UART_Init(uint32_t speed)
{
ext_uart.Instance = USART6;
ext_uart.Init.BaudRate = speed;
ext_uart.Init.WordLength = UART_WORDLENGTH_8B;
ext_uart.Init.StopBits = UART_STOPBITS_1;
ext_uart.Init.Parity = UART_PARITY_NONE;
ext_uart.Init.Mode = UART_MODE_TX_RX;
ext_uart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
ext_uart.Init.OverSampling = UART_OVERSAMPLING_16;
ext_uart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
ext_uart.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
HAL_UART_Init(&ext_uart);
/* USART6 interrupt Init */
HAL_NVIC_SetPriority(USART6_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART6_IRQn);
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void USART6_IRQHandler(void)
{
HAL_UART_IRQHandler(&ext_uart);
}

44
RTE/Hesso_pack/ext_uart.h Normal file
View File

@ -0,0 +1,44 @@
/************************************************************************//**
* \file ext_uart.h
* \brief Function to use the extension uart
* \author pascal (dot) sartoretti (at) hevs (dot) ch
***************************************************************************/
#ifndef __EXT_UART_H
#define __EXT_UART_H
#include <stdint.h>
#include "stm32f7xx_hal.h"
extern UART_HandleTypeDef ext_uart; // extension uart handle
/************************************************************************//**
* \brief Inits the extension uart
* \param speed This si the uart speed selected for example 115200.
* The extension uart could be use with or without interrupts.
*
* Without interrupts:
* -------------------
* To send something on the uart, you have to use HAL_UART_Transmit function
* as the example below.
* error = HAL_UART_Transmit(&ext_uart, msg, sizeof(msg),50);
* To receive you have to use HAL_UART_Receive as example below.
* error = HAL_UART_Receive(&ext_uart, msg, sizeof(msg),HAL_MAX_DELAY);
* The HAL_MAX_DELAY waits until receive is finished.
*
* With interrupts:
* ----------------
* The functions below have to be used:
* HAL_UART_Transmit_IT(&ext_uart," Welcome\n\r", 10);
* HAL_UART_Receive_IT(&ext_uart,data,8);
*
* The callback functions above could be implemented for usage on interrupt
* mode when the full size is transmitted (or received).
* void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
* void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
*
***************************************************************************/
extern void Ext_UART_Init(uint32_t speed);
#endif /* __BOARD_LED_H */