Skip to content

Commit

Permalink
working I2C
Browse files Browse the repository at this point in the history
- working I2C with autoend
- added dummy kinematic for test purposes
  • Loading branch information
Paciente8159 committed Mar 2, 2025
1 parent fca2a2c commit 4baa1a8
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 70 deletions.
45 changes: 45 additions & 0 deletions uCNC/src/hal/kinematics/kinematic_dummy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Name: kinematic_dummy.c
Description: Custom kinematics definitions for a dummy kinematics
Copyright: Copyright (c) João Martins
Author: João Martins
Date: 01-03-2025
µCNC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. Please see <http://www.gnu.org/licenses/>
µCNC is distributed WITHOUT ANY WARRANTY;
Also without the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
*/

#include "../../cnc.h"

#if (KINEMATIC == KINEMATIC_DUMMY)

void kinematics_init(void)
{
}

void kinematics_apply_inverse(float *axis, int32_t *steps)
{
}

void kinematics_apply_forward(int32_t *steps, float *axis)
{
}

uint8_t kinematics_home(void)
{
return STATUS_OK;
}

bool kinematics_check_boundaries(float *axis)
{
return true;
}

#endif
32 changes: 32 additions & 0 deletions uCNC/src/hal/kinematics/kinematic_dummy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Name: kinematic_dummy.h
Description: Custom kinematics definitions for a dummy kinematics
Copyright: Copyright (c) João Martins
Author: João Martins
Date: 01-03-2025
µCNC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. Please see <http://www.gnu.org/licenses/>
µCNC is distributed WITHOUT ANY WARRANTY;
Also without the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
*/

#ifndef KINEMATIC_DUMMY_H
#define KINEMATIC_DUMMY_H

#ifdef __cplusplus
extern "C"
{
#endif

#define KINEMATIC_TYPE_STR "DMY"

#ifdef __cplusplus
}
#endif
#endif
2 changes: 2 additions & 0 deletions uCNC/src/hal/kinematics/kinematicdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ extern "C"
#include "kinematic_delta.h"
#elif (KINEMATIC == KINEMATIC_SCARA)
#include "kinematic_scara.h"
#elif (KINEMATIC == KINEMATIC_DUMMY)
#include "kinematic_dummy.h"
#else
#error Kinematics not implemented
#endif
Expand Down
1 change: 1 addition & 0 deletions uCNC/src/hal/kinematics/kinematics.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern "C"
#define KINEMATIC_LINEAR_DELTA 3
#define KINEMATIC_DELTA 4
#define KINEMATIC_SCARA 5
#define KINEMATIC_DUMMY 99

#define COREXY_AXIS_XY 1
#define COREXY_AXIS_XZ 2
Expand Down
58 changes: 33 additions & 25 deletions uCNC/src/hal/mcus/stm32f0x/mcu_stm32f0x.c
Original file line number Diff line number Diff line change
Expand Up @@ -1371,14 +1371,16 @@ bool mcu_spi2_bulk_transfer(const uint8_t *tx_data, uint8_t *rx_data, uint16_t d
}
#endif

#ifndef USE_ARDUINO_I2C_LIBRARY
#ifdef MCU_HAS_I2C

#if I2C_ADDRESS == 0
#define STM_VAL2REG(val, X) (uint32_t)(((((uint32_t)val) << X##_Pos) & X##_Msk))

void mcu_i2c_stop(bool *stop)
{
//not working (using autostop)
while (!(I2C_REG->ISR & I2C_ISR_TC))
;
I2C_REG->CR2 |= I2C_CR2_STOP;
while (!(I2C_REG->ISR & I2C_ISR_STOPF))
;
Expand All @@ -1391,7 +1393,7 @@ void mcu_i2c_stop(bool *stop)
// master sends command to slave
uint8_t mcu_i2c_send(uint8_t address, uint8_t *data, uint8_t datalen, bool release, uint32_t ms_timeout)
{
bool stop __attribute__((__cleanup__(mcu_i2c_stop))) = release;
// bool stop __attribute__((__cleanup__(mcu_i2c_stop))) = release;

if (!data || !datalen)
{
Expand All @@ -1406,6 +1408,10 @@ uint8_t mcu_i2c_send(uint8_t address, uint8_t *data, uint8_t datalen, bool relea
I2C_REG->CR2 &= ~I2C_CR2_ADD10;
/*Set number to transfer to length for write operation*/
I2C_REG->CR2 |= (datalen << I2C_CR2_NBYTES_Pos);
if (release)
{
I2C_REG->CR2 |= I2C_CR2_AUTOEND;
}
/*Set the mode to write mode*/
I2C_REG->CR2 &= ~I2C_CR2_RD_WRN;
/*Generate start*/
Expand All @@ -1418,7 +1424,8 @@ uint8_t mcu_i2c_send(uint8_t address, uint8_t *data, uint8_t datalen, bool relea
{
if ((I2C_REG->ISR & I2C_ISR_NACKF))
{
stop = true;
// stop = true;
I2C_REG->CR2 |= I2C_CR2_STOP;
return I2C_NOTOK;
}

Expand All @@ -1430,7 +1437,7 @@ uint8_t mcu_i2c_send(uint8_t address, uint8_t *data, uint8_t datalen, bool relea

__TIMEOUT_ASSERT__(t)
{
stop = true;
I2C_REG->CR2 |= I2C_CR2_STOP;
return I2C_NOTOK;
}

Expand Down Expand Up @@ -1463,7 +1470,7 @@ uint8_t mcu_i2c_send(uint8_t address, uint8_t *data, uint8_t datalen, bool relea
// master receive response from slave
uint8_t mcu_i2c_receive(uint8_t address, uint8_t *data, uint8_t datalen, uint32_t ms_timeout)
{
bool stop __attribute__((__cleanup__(mcu_i2c_stop))) = true;
// bool stop __attribute__((__cleanup__(mcu_i2c_stop))) = true;

if (!data || !datalen)
{
Expand All @@ -1486,7 +1493,7 @@ uint8_t mcu_i2c_receive(uint8_t address, uint8_t *data, uint8_t datalen, uint32_

/*Enable I2C*/
I2C_REG->CR1 |= I2C_CR1_PE;
I2C_REG->CR2 = I2C_CR2_HEAD10R | I2C_CR2_RD_WRN | STM_VAL2REG(datalen, I2C_CR2_NBYTES) | (address << 1);
I2C_REG->CR2 = I2C_CR2_HEAD10R | I2C_CR2_RD_WRN | STM_VAL2REG(datalen, I2C_CR2_NBYTES) | (address << 1) | I2C_CR2_AUTOEND;
I2C_REG->CR2 |= I2C_CR2_START;
while ((I2C_REG->CR2 & I2C_CR2_START))
;
Expand Down Expand Up @@ -1522,32 +1529,23 @@ uint8_t mcu_i2c_receive(uint8_t address, uint8_t *data, uint8_t datalen, uint32_
void mcu_i2c_config(uint32_t frequency)
{
RCC->APB1ENR &= ~I2C_APBEN;
RCC->CFGR3 &= ~RCC_CFGR3_I2C1SW; // use HSI clock source
while (RCC->CFGR3 & RCC_CFGR3_I2C1SW)
;
RCC->APB1ENR |= I2C_APBEN;
I2C_REG->CR1 &= ~I2C_CR1_PE;
RCC->APB1RSTR |= __helper__(RCC_APB1RSTR_I2C, I2C_PORT, RST);
RCC->APB1RSTR &= ~(__helper__(RCC_APB1RSTR_I2C, I2C_PORT, RST));
RCC->APB1ENR |= I2C_APBEN;
mcu_config_af(I2C_CLK, I2C_CLK_AFIO);
mcu_config_af(I2C_DATA, I2C_DATA_AFIO);
mcu_config_pullup(I2C_CLK);
mcu_config_pullup(I2C_DATA);
// set opendrain
mcu_config_opendrain(I2C_CLK);
mcu_config_opendrain(I2C_DATA);
// mcu_config_opendrain(I2C_CLK);
// mcu_config_opendrain(I2C_DATA);
#ifdef I2C_REMAP
AFIO->MAPR |= I2C_REMAP;
#endif
I2C_REG->CR1 &= ~(I2C_CR1_ANFOFF | I2C_CR1_DNF);
#if I2C_ADDRESS == 0
// set max freq
uint8_t presc = 0;
while ((HSI_VALUE / (presc + 1)) > (frequency * 255UL))
{
presc++;
}

float i2c_osc = ((float)HSI_VALUE / (presc + 1));
uint8_t scll = 0, sclh = scll = (uint8_t)CLAMP(0, (i2c_osc / (frequency * 2) - 1), 0xFFFF); // half time clock up and clock down
float risetime = 8333333.3f, falltime = 8333333.3f;
if (frequency <= 100000UL) // standart mode (max fall time 1000ns(1Mhz) and max rise time 300ns(3,3MHZ))
{
Expand All @@ -1560,16 +1558,26 @@ void mcu_i2c_config(uint32_t frequency)
falltime = 3333333.3f;
}

// set max freq
uint8_t presc = 0;
while ((HAL_RCC_GetPCLK1Freq() / (presc + 1)) > (frequency * 0xff) || (HAL_RCC_GetPCLK1Freq() / (presc + 1)) > (((uint32_t)falltime) * 0xf))
{
presc++;
}

float i2c_osc = ((float)HAL_RCC_GetPCLK1Freq() / (presc + 1));
uint8_t scll = 0, sclh = scll = (uint8_t)CLAMP(0, (i2c_osc / (frequency * 2) - 1), 0xFFFF); // half time clock up and clock down

uint8_t scldel = (uint8_t)CLAMP(0, (ceilf((float)i2c_osc / risetime) - 1), 0x0F);
uint8_t sdadel = (uint8_t)CLAMP(0, (ceilf((float)i2c_osc / falltime) - 1), 0x0F);

sclh -= (sclh >= sdadel) ? sdadel : 0;
// presc = 0; scll = 0x9; sclh=0x3; sdadel = 0x1; scldel = 0x3;
uint32_t tmr = (STM_VAL2REG(presc, I2C_TIMINGR_PRESC) | STM_VAL2REG(scll, I2C_TIMINGR_SCLL) | STM_VAL2REG(sclh, I2C_TIMINGR_SCLH) | STM_VAL2REG(scldel, I2C_TIMINGR_SCLDEL) | STM_VAL2REG(sdadel, I2C_TIMINGR_SDADEL));
I2C_REG->TIMEOUTR = tmr;
I2C_REG->CR1 &= ~I2C_CR1_NOSTRETCH;
I2C_REG->CR2 |= I2C_CR2_HEAD10R;
I2C_REG->CR2 &= ~I2C_CR2_ADD10;
I2C_REG->CR1 &= ~(I2C_CR1_ANFOFF | I2C_CR1_DNF);
I2C_REG->TIMINGR = tmr; // 0xB0420F13;
I2C_REG->CR1 &= ~(I2C_CR1_NOSTRETCH);
I2C_REG->CR2 = 0;
#else
// set address
I2C_REG->OAR1 &= ~(I2C_OAR1_OA1MODE | 0x0F);
Expand Down Expand Up @@ -1667,7 +1675,7 @@ void I2C_ISR(void)
#endif

#endif
#endif


#ifdef MCU_HAS_ONESHOT_TIMER

Expand Down
45 changes: 0 additions & 45 deletions uCNC/src/hal/mcus/stm32f0x/mcu_stm32f0x_arduino.cpp

This file was deleted.

0 comments on commit 4baa1a8

Please sign in to comment.