Skip to content

Commit b5fbee4

Browse files
author
Jan Kamidra
committed
Add initial SPI and I2C
1 parent ae7b752 commit b5fbee4

File tree

5 files changed

+240
-0
lines changed

5 files changed

+240
-0
lines changed

targets/TARGET_STM/TARGET_STM32U0/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ target_sources(mbed-stm32u0
1111
clock_cfg/system_clock.c
1212
analogin_device.c
1313
analogout_device.c
14+
flash_api.c
1415
gpio_irq_device.c
16+
i2c_device.c
17+
spi_api.c
1518
pwmout_device.c
1619
serial_device.c
1720
)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* mbed Microcontroller Library
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
******************************************************************************
4+
*
5+
* Copyright (c) 2015-2021 STMicroelectronics.
6+
* All rights reserved.
7+
*
8+
* This software component is licensed by ST under BSD 3-Clause license,
9+
* the "License"; You may not use this file except in compliance with the
10+
* License. You may obtain a copy of the License at:
11+
* opensource.org/licenses/BSD-3-Clause
12+
*
13+
******************************************************************************
14+
*/
15+
16+
#include "i2c_device.h"
17+
#include "mbed_assert.h"
18+
#include "mbed_error.h"
19+
#include "stm32u0xx_ll_rcc.h"
20+
21+
/* Define I2C Device */
22+
#if DEVICE_I2C
23+
24+
/**
25+
* @brief Get I2C clock source frequency according I2C instance used.
26+
* @param i2c I2C instance name.
27+
* @retval I2C clock source frequency in Hz.
28+
*/
29+
uint32_t i2c_get_pclk(I2CName i2c)
30+
{
31+
uint32_t clocksource;
32+
uint32_t pclk = 0;
33+
if (i2c == I2C_1) {
34+
clocksource = __HAL_RCC_GET_I2C1_SOURCE();
35+
switch (clocksource) {
36+
case RCC_I2C1CLKSOURCE_PCLK1:
37+
pclk = HAL_RCC_GetPCLK1Freq();
38+
break;
39+
40+
case RCC_I2C1CLKSOURCE_SYSCLK:
41+
pclk = HAL_RCC_GetSysClockFreq();
42+
break;
43+
44+
case RCC_I2C1CLKSOURCE_HSI:
45+
pclk = HSI_VALUE;
46+
break;
47+
}
48+
}
49+
#if defined I2C2_BASE
50+
else if (i2c == I2C_2) {
51+
pclk = HAL_RCC_GetPCLK1Freq();
52+
}
53+
#endif
54+
#if defined I2C3_BASE
55+
else if (i2c == I2C_3) {
56+
clocksource = __HAL_RCC_GET_I2C3_SOURCE();
57+
switch (clocksource) {
58+
case RCC_I2C3CLKSOURCE_PCLK1:
59+
pclk = HAL_RCC_GetPCLK1Freq();
60+
break;
61+
62+
case RCC_I2C3CLKSOURCE_SYSCLK:
63+
pclk = HAL_RCC_GetSysClockFreq();
64+
break;
65+
66+
case RCC_I2C3CLKSOURCE_HSI:
67+
pclk = HSI_VALUE;
68+
break;
69+
}
70+
}
71+
#endif
72+
#if defined I2C4_BASE
73+
else if (i2c == I2C_4) {
74+
pclk = HAL_RCC_GetPCLK1Freq();
75+
}
76+
#endif
77+
else {
78+
// should not happend
79+
error("I2C: unknown instance");
80+
}
81+
return pclk;
82+
}
83+
#endif // DEVICE_I2C
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* mbed Microcontroller Library
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
******************************************************************************
4+
*
5+
* Copyright (c) 2015-2021 STMicroelectronics.
6+
* All rights reserved.
7+
*
8+
* This software component is licensed by ST under BSD 3-Clause license,
9+
* the "License"; You may not use this file except in compliance with the
10+
* License. You may obtain a copy of the License at:
11+
* opensource.org/licenses/BSD-3-Clause
12+
*
13+
******************************************************************************
14+
*/
15+
16+
#ifndef MBED_I2C_DEVICE_H
17+
#define MBED_I2C_DEVICE_H
18+
19+
#include "PeripheralNames.h"
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
/* Define I2C Device */
26+
#if DEVICE_I2C
27+
28+
/* Define IP version */
29+
#define I2C_IP_VERSION_V2
30+
31+
#define I2C1_EV_IRQn I2C1_IRQn
32+
#define I2C1_ER_IRQn I2C1_IRQn
33+
#define I2C2_EV_IRQn I2C2_3_4_IRQn
34+
#define I2C2_ER_IRQn I2C2_3_4_IRQn
35+
#define I2C3_EV_IRQn I2C2_3_4_IRQn
36+
#define I2C3_ER_IRQn I2C2_3_4_IRQn
37+
#define I2C4_EV_IRQn I2C2_3_4_IRQn
38+
#define I2C4_ER_IRQn I2C2_3_4_IRQn
39+
40+
// Common settings: I2C clock = 56 MHz, Analog filter = ON, Digital filter coefficient = 0
41+
#define TIMING_VAL_56M_CLK_100KHZ 0x20C04963 // Standard mode with Rise Time = 400ns and Fall Time = 100ns
42+
#define TIMING_VAL_56M_CLK_400KHZ 0x2060091A // Fast mode with Rise Time = 250ns and Fall Time = 100ns
43+
#define TIMING_VAL_56M_CLK_1MHZ 0x00600A19 // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
44+
#define I2C_PCLK_56M 56000000 // 56 MHz
45+
46+
#define I2C_IT_ALL (I2C_IT_ERRI|I2C_IT_TCI|I2C_IT_STOPI|I2C_IT_NACKI|I2C_IT_ADDRI|I2C_IT_RXI|I2C_IT_TXI)
47+
48+
/* Family specifc settings for clock source */
49+
#define I2CAPI_I2C1_CLKSRC RCC_I2C1CLKSOURCE_SYSCLK
50+
#define I2CAPI_I2C3_CLKSRC RCC_I2C3CLKSOURCE_SYSCLK
51+
52+
uint32_t i2c_get_pclk(I2CName i2c);
53+
uint32_t i2c_get_timing(I2CName i2c, uint32_t current_timing, int current_hz, int hz);
54+
55+
#if MBED_CONF_TARGET_I2C_TIMING_VALUE_ALGO
56+
uint32_t i2c_compute_timing(uint32_t clock_src_freq, uint32_t i2c_freq);
57+
void i2c_compute_presc_scldel_sdadel(uint32_t clock_src_freq, uint32_t I2C_speed);
58+
uint32_t i2c_compute_scll_sclh(uint32_t clock_src_freq, uint32_t I2C_speed);
59+
#endif // MBED_CONF_TARGET_I2C_TIMING_VALUE_ALGO
60+
61+
#endif // DEVICE_I2C
62+
63+
#ifdef __cplusplus
64+
}
65+
#endif
66+
67+
#endif
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* mbed Microcontroller Library
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
******************************************************************************
4+
*
5+
* Copyright (c) 2015-2021 STMicroelectronics.
6+
* All rights reserved.
7+
*
8+
* This software component is licensed by ST under BSD 3-Clause license,
9+
* the "License"; You may not use this file except in compliance with the
10+
* License. You may obtain a copy of the License at:
11+
* opensource.org/licenses/BSD-3-Clause
12+
*
13+
******************************************************************************
14+
*/
15+
16+
#include "mbed_assert.h"
17+
#include "spi_api.h"
18+
19+
#if DEVICE_SPI
20+
21+
#include "cmsis.h"
22+
#include "pinmap.h"
23+
#include "mbed_error.h"
24+
#include "PeripheralPins.h"
25+
26+
#if DEVICE_SPI_ASYNCH
27+
#define SPI_S(obj) (( struct spi_s *)(&(obj->spi)))
28+
#else
29+
#define SPI_S(obj) (( struct spi_s *)(obj))
30+
#endif
31+
32+
/*
33+
* Only the frequency is managed in the family specific part
34+
* the rest of SPI management is common to all STM32 families
35+
*/
36+
int spi_get_clock_freq(spi_t *obj)
37+
{
38+
struct spi_s *spiobj = SPI_S(obj);
39+
int spi_hz = 0;
40+
41+
/* Get source clock depending on SPI instance */
42+
switch ((int)spiobj->spi) {
43+
case SPI_1:
44+
case SPI_2:
45+
case SPI_3:
46+
spi_hz = HAL_RCC_GetPCLK1Freq();
47+
break;
48+
default:
49+
error("CLK: SPI instance not set");
50+
break;
51+
}
52+
return spi_hz;
53+
}
54+
55+
#endif
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* mbed Microcontroller Library
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
******************************************************************************
4+
*
5+
* Copyright (c) 2015-2021 STMicroelectronics.
6+
* All rights reserved.
7+
*
8+
* This software component is licensed by ST under BSD 3-Clause license,
9+
* the "License"; You may not use this file except in compliance with the
10+
* License. You may obtain a copy of the License at:
11+
* opensource.org/licenses/BSD-3-Clause
12+
*
13+
******************************************************************************
14+
*/
15+
16+
#ifndef MBED_SPI_DEVICE_H
17+
#define MBED_SPI_DEVICE_H
18+
19+
#include "stm32u0xx_ll_spi.h"
20+
21+
#define SPI_IP_VERSION_V1 // SPI2S2 / SPI2S3 IP version
22+
23+
// Defines the word legnth capability of the device where Nth bit allows for N window size
24+
#define STM32_SPI_CAPABILITY_WORD_LENGTH (0x0000FFF8)
25+
26+
// We have DMA support
27+
#define STM32_SPI_CAPABILITY_DMA 1
28+
29+
#define SPI2_IRQn SPI2_3_IRQn
30+
#define SPI3_IRQn SPI2_3_IRQn
31+
32+
#endif

0 commit comments

Comments
 (0)