/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
*
© Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
// es wird der GPIO PA8 verwendet
#define OUTHIGH {GPIOA->ODR |= (1<<8);GPIOA->CRH|=(0b10);}
#define OUTLOW {GPIOA->ODR &= ~(1<<8);GPIOA->CRH|=(0b10);}
#define RELEASE {GPIOA->CRH &= ~(0b11);}
#define INBIT ((GPIOA->IDR>>8)&1)
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
#define ANZAHLSENSOREN (2)
uint32_t sensor[2*ANZAHLSENSOREN]={
0x633cd5f6,0x48cd4428,
0x3c3cb5f6,0x48063e28
};
int32_t timeout=0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/*****************************************/
void raus1(char * buf)
/*****************************************/
{ int k = 0;
//volatile int m=0;
while(1){
if(buf[k]==0) break;
while(((USART1->SR>>7)&1) == 0);
//while(m++ < (1<<20)) if(((USART3->ISR>>7)&1) > 0) break;
USART1->DR = buf[k++];
}
}
/********************************************************/
void pause (int32_t us){
/********************************************************/
volatile int32_t k;
// Overhead sind ca. 2us
us -= 2;
k = (86*us)>>4; // skalieren
while(k>0) k--;
}
/********************************************************/
int32_t checkcrc (int anz , uint32_t * d){
/********************************************************/
volatile char h,b0=0,b1=0,b2=0,b3=0,b4=0,b5=0,b6=0,b7=0;
// Das geht besser aber so gehts' auch
uint32_t k;
for(k=0;k>(k%32))&1;
h=h^b0;b0=b1; b1=b2; b2=b3^h; b3=b4^h;
b4=b5;b5=b6;b6=b7;b7=h;
}
// 0 ist alles ok
return (b0|b1|b2|b3|b4|b5|b6|b7);
}
/********************************************************/
void ini_sensors (void ){
/********************************************************/
int n;
GPIOA->CRH &= ~ (0b1111); // PA8 CRH loeschen
GPIOA->CRH |= (0b0100); // PA8 Input
OUTLOW;pause((int32_t)(1.5f*480)); // 480us warten
RELEASE; pause(20); //warten
for(n=1000*1000;n>0;n--) //
if(INBIT==0) break;
if(n==0) timeout |= (1<<0);
for(n=1000*1000;n>0;n--) // warten dass er wieder oben ist
if(INBIT==1) break;
if(n==0) timeout |= (1<<1);
pause(10);
}
/********************************************************/
void write_byte (char cc){
/********************************************************/
int n;
// LSB first
for(n=0;n<8;n++) {
OUTLOW; pause(3); // warten, mindestens 3us low
if(((cc>>n)&1)==1){
pause((int32_t)(3)); // warten, 7us low
RELEASE;
pause(100);
}else{
pause(100-3); // warten, jetzt zusammen 100us low
RELEASE;
}
pause((int32_t)(10)); // warten, mindestens 3us high
}
}
/********************************************************/
void lese_bits (int anz , uint32_t * buf){
/********************************************************/
int n,k,m;
for(n=0;n<(anz/32)+1;n++) buf[n]=0;
for(n=0;n>1)&(~(1<<31)))|((indathigh&1)<<31);
//indathigh = ((indathigh>>1)&(~(1<<31)))|((INBIT)<<31);
pause(500);
}
}
/********************************************************/
void lese_seriennummer (void ){
/********************************************************/
int32_t k=0,m,n;
int8_t buf[100];
int8_t cc=0;
uint32_t indatlow,indathigh,bufbuf[10];
while (1)
{
ini_sensors();
write_byte(0x33);//READROM;
lese_bits (64 , bufbuf);
indatlow =bufbuf[0];
indathigh=bufbuf[1];
k++;
if(checkcrc(64,bufbuf)==0)
sprintf(buf,"%d %x %x CRC ok \r\n", k,indathigh,indatlow);
else sprintf(buf,"%d %x %x CRC nicht ok \r\n", k,indathigh,indatlow);
raus1(buf);
}
}
/********************************************************/
int16_t lese_irgendeinen_sensor (void ){
/********************************************************/
int n;
uint32_t bufbuf[10];
ini_sensors();
write_byte (0xcc); // skip ROM
write_byte (0x44); // convertT
for(n=0;n<10*1000*1000;n++){
lese_bits(1,bufbuf);
if((bufbuf[0]&1)==1) break;
}
if(n>9*1000*1000) timeout |= (1<<3);
ini_sensors();
write_byte (0xcc); // skip ROM
write_byte (0xbe); // read scratchpad
lese_bits(9*8,bufbuf);
// kommt in 1/10 Grad zurück
return (int16_t)(10.0f*(((int16_t)(bufbuf[0]&0xffff))/16.0f));
}
/********************************************************/
int16_t kickall_sensors (void ){
/********************************************************/
int n;
uint32_t bufbuf[30];
ini_sensors();
write_byte (0xcc); // skip ROM
write_byte (0x44); // convertT
for(n=0;n<10*1000;n++){
lese_bits(1,bufbuf);
if((bufbuf[0]&1)==1) break;
}
if(n==10*1000-1) timeout |= 1<<5;
}
/********************************************************/
int16_t read_sensor (int sensornummer ){
/********************************************************/
int n;
uint32_t bufbuf[30];
ini_sensors();
write_byte (0x55); // match ROM
//sensornummer=1;
write_byte((char)((sensor[sensornummer*2+1]>>(0*8))&0xff));
write_byte((char)((sensor[sensornummer*2+1]>>(1*8))&0xff));
write_byte((char)((sensor[sensornummer*2+1]>>(2*8))&0xff));
write_byte((char)((sensor[sensornummer*2+1]>>(3*8))&0xff));
write_byte((char)((sensor[sensornummer*2 ]>>(0*8))&0xff));
write_byte((char)((sensor[sensornummer*2 ]>>(1*8))&0xff));
write_byte((char)((sensor[sensornummer*2 ]>>(2*8))&0xff));
write_byte((char)((sensor[sensornummer*2 ]>>(3*8))&0xff));
write_byte (0xbe); // read scratchpad
lese_bits(9*8,bufbuf);
return (int16_t)(10.0f*(((int16_t)(bufbuf[0]&0xffff))/16.0f));
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
int32_t dacval=0; // PWM an PA8 in Promille
int32_t k=0,m,n,i1;
int8_t buf[100];
int8_t cc=0;
uint32_t indatlow,indathigh,bufbuf[100];
int16_t temp;
int sensornummer;
/*
*
* Doku for Onewire
Bitbanging auf A8
irgendwas macht er alle 3ms, mal die Interrupttable checken
Er läuft auf 64 MHz
*/
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
//lese_seriennummer(); // Darin ist infinite loop
// Der bleibt so
GPIOA->CRH &= ~ (0b1111); // PA8 CRH loeschen
GPIOA->CRH |= (0b0100); // PA8 Input
while (1)
{
k++;
if(((USART1->SR>>5)&1) == 1) cc=USART1->DR;
//if(checkcrc(72,bufbuf)==0)
kickall_sensors ();
sprintf(buf,"blg %d %d %d \r\n",k,0,read_sensor (0) ); raus1(buf);
sprintf(buf,"blg %d %d %d \r\n",k,1,read_sensor (1) ); raus1(buf);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 9600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);
/*Configure GPIO pin : PC13 */
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pin : PA8 */
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : PB9 */
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/