Skip to content

Commit 636d7fa

Browse files
authored
Merge branch 'cnlohr:master' into master
2 parents 945a547 + ab05c94 commit 636d7fa

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

examples/i2c_slave/README.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,50 @@ The first byte written to the device within a transaction determines the offset
88

99
The example will turn on a LED connected to PA2 when the LSB of register 0 is set to 1 and off when it's set to 0.
1010

11-
## Usage
11+
# Usage
1212

13-
Initialize the I2C1 SDA and SCL pins you want to use as open-drain outputs:
13+
## Pin Setup
14+
15+
Caution: CH32V003 only supports I2C on a few specific pins.
16+
17+
Initialize the default I2C1 SDA and SCL pins as open-drain outputs. By default, pins PC1 and PC2 are used.
1418
```
1519
funPinMode(PC1, GPIO_CFGLR_OUT_10Mhz_AF_OD); // SDA
1620
funPinMode(PC2, GPIO_CFGLR_OUT_10Mhz_AF_OD); // SCL
1721
```
1822

19-
For chips other than the CH32V003 you will need to change the pin numbers to the pins corresponding with the I2C1 peripheral. If you want to use the alternative pins for the I2C periperal in addition to configuring the pins you have to configure the chip to use the the alternative pins using the `I2C1_RM` and `I2C1REMAP1` fields of the `AFIO_PCFR1` register.
23+
For chips other than the CH32V003 you should change the above pin numbers to the pins corresponding with the I2C1 peripheral.
24+
25+
## Alternate Pins
26+
27+
Optional: If you want to use alternative pins for the I2C periperal, then you must take an additional step to configure the chip to use one of the 2 alternative pin mappings.
28+
29+
You can do this using the `I2C1_RM` and `I2C1REMAP1` fields of the `AFIO_PCFR1` register.
30+
31+
Remapping examples:
32+
33+
```c++
34+
// 2 bits [bit 22 and bit 1] of AFIO_PCFR1 register are what control I2C1 pin remapping on ch32v003
35+
// [high bit, low bit]
36+
// [I2C1REMAP1, I2C1_RM]
37+
38+
// [0, 0] default mapping (SCL on pin PC2, SDA on pin PC1)
39+
// (note: you don't need to do this, since it's the defualt:)
40+
AFIO->PCFR1 &= ~AFIO_PCFR1_I2C1_HIGH_BIT_REMAP; // set high bit = 0 (I2C1REMAP1)
41+
AFIO->PCFR1 &= ~AFIO_PCFR1_I2C1_REMAP; // set low bit = 0 (I2C1_RM)
42+
43+
// [0, 1]: Remapping option #1 (SCL on pin PD1, SDA on pin PD0)
44+
AFIO->PCFR1 &= ~AFIO_PCFR1_I2C1_HIGH_BIT_REMAP; // set high bit = 0 (I2C1REMAP1)
45+
AFIO->PCFR1 |= AFIO_PCFR1_I2C1_REMAP; // set low bit = 1 (I2C1_RM)
46+
47+
// [1, X]: Remapping option #2 (SCL on pin PC5, SDA on pin PC6)
48+
AFIO->PCFR1 |= AFIO_PCFR1_I2C1_HIGH_BIT_REMAP; // set high bit = 1 (I2C1REMAP1)
49+
AFIO->PCFR1 |= AFIO_PCFR1_I2C1_REMAP; // set low bit [ignored / don't care] (I2C1_RM)
50+
```
51+
52+
## Initialization
2053

21-
Then initialize the I2C1 peripheral in slave mode using:
54+
Initialize the I2C1 peripheral in slave mode using:
2255

2356
```
2457
SetupI2CSlave(0x09, i2c_registers, sizeof(i2c_registers), onWrite, onRead, false);

examples/usart_dma_rx_circular/usart_dma_rx_circular.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Using any terminal, send "toggle\r\n" to control the LED connected to PC7.
33

44
#include "ch32fun.h"
5+
#include <stdio.h>
56

67
#define RX_BUF_LEN 16 // size of receive circular buffer
78

@@ -10,7 +11,7 @@ u8 cmd_buf[RX_BUF_LEN] = {0}; // buffer for complete command strings
1011

1112
void process_cmd(u8* buf)
1213
{
13-
if ( strncmp(buf, "toggle\r\n", 9) == 0) {
14+
if ( strncmp((char*)buf, "toggle\r\n", 9) == 0) {
1415
GPIOC->OUTDR ^= (1<<7);
1516
printf("Horay!\r\n");
1617
} else {

0 commit comments

Comments
 (0)