Skip to content

Commit ad69366

Browse files
committed
V1.0.2
1 parent 9871543 commit ad69366

File tree

26 files changed

+3035
-55
lines changed

26 files changed

+3035
-55
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ It will be a long-term support and maintenance project, unless we encounter forc
3434

3535
### CH32V00x EVT Boards
3636

37-
| Status | Boards name | Peripherals | Release |
38-
| :----: | ---- | ---- | :-----: |
39-
| :heavy_check_mark: | CH32V003F4P | ADC,DAC,USART,GPIO,EXTI,SysTick | 1.0.0 |
37+
| Status | Boards name | Peripherals | Release | Notes |
38+
| :----: | ---- | ---- | :-----: | :---- |
39+
| :heavy_check_mark: | CH32V003F4P | ADC,DAC,USART,GPIO,EXTI,SysTick | 1.0.0 | SPI,I2C since 1.0.2 |
4040

4141
### CH32V20x EVT Boards
4242

43-
| Status | Boards name | Peripherals | Release |
44-
| :----: | ---- | ---- | :-----: |
45-
| :heavy_check_mark: | CH32V203G8U | ADC,DAC,USART,GPIO,EXTI,SysTick | 1.0.0 |
43+
| Status | Boards name | Peripherals | Release | Notes |
44+
| :----: | ---- | ---- | :-----: | :---- |
45+
| :heavy_check_mark: | CH32V203G8U | ADC,DAC,USART,GPIO,EXTI,SysTick | 1.0.0 | SPI,I2C since 1.0.2 |
4646

4747
### CH32X035 EVT Boards
4848

49-
| Status | Boards name | Peripherals | Release |
50-
| :----: | ---- | ---- | :-----: |
51-
| :heavy_check_mark: | CH32X035G8U | ADC,DAC,USART,GPIO,EXTI,SysTick | 1.0.1 |
49+
| Status | Boards name | Peripherals | Release | Notes |
50+
| :----: | ---- | ---- | :-----: | :---- |
51+
| :heavy_check_mark: | CH32X035G8U | ADC,DAC,USART,GPIO,EXTI,SysTick | 1.0.1 | SPI,I2C since 1.0.2 |
5252

5353
## Submit bugs
5454

cores/arduino/Print.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ int Print::vprintf(const __FlashStringHelper *format, va_list ap)
283283

284284

285285
// Private Methods /////////////////////////////////////////////////////////////
286-
287286
size_t Print::printNumber(unsigned long n, uint8_t base)
288287
{
289288
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
/*
2+
Winbond SPIFlash read and write
3+
4+
This example read/write a W25QXX Flash by SPI port.
5+
Pins:
6+
CS: PA2 (003 is PC4)
7+
MOSI: PA7 (003 is PC6)
8+
MISO: PA6 (003 is PC7)
9+
SCK: PA5 (003 is PC5)
10+
created 30 Jurn 2023
11+
by TempersLee
12+
*/
13+
14+
#include <SPI.h>
15+
16+
// Winbond SPIFLASH ID
17+
#define W25Q80 0xEF13
18+
#define W25Q16 0xEF14
19+
#define W25Q32 0xEF15
20+
#define W25Q64 0xEF16
21+
#define W25Q128 0xEF17
22+
23+
/* Winbond SPIFalsh Instruction List */
24+
#define W25X_WriteEnable 0x06
25+
#define W25X_WriteDisable 0x04
26+
#define W25X_ReadStatusReg 0x05
27+
#define W25X_WriteStatusReg 0x01
28+
#define W25X_ReadData 0x03
29+
#define W25X_FastReadData 0x0B
30+
#define W25X_FastReadDual 0x3B
31+
#define W25X_PageProgram 0x02
32+
#define W25X_BlockErase 0xD8
33+
#define W25X_SectorErase 0x20
34+
#define W25X_ChipErase 0xC7
35+
#define W25X_PowerDown 0xB9
36+
#define W25X_ReleasePowerDown 0xAB
37+
#define W25X_DeviceID 0xAB
38+
#define W25X_ManufactDeviceID 0x90
39+
#define W25X_JedecDeviceID 0x9F
40+
41+
42+
//define CS pin
43+
#define chipSelectPin A2
44+
45+
46+
uint16_t flashType;
47+
uint8_t readBuffer[256];
48+
uint8_t writeBuffer[256];
49+
50+
void setup() {
51+
uint16_t i=0;
52+
Serial.begin(115200);
53+
// start the SPI library:
54+
SPI.begin();
55+
Serial.printf("%s Chip ID: 0x%08x\r\n", "Hello CH32duino!", DBGMCU_GetDEVID());
56+
57+
// initialize the chip select pins:
58+
pinMode(chipSelectPin, OUTPUT);
59+
digitalWrite(chipSelectPin, HIGH);
60+
delay(100);
61+
62+
flashType = readFlashID();
63+
switch (flashType)
64+
{
65+
case W25Q16:
66+
Serial.printf("W25Q16 is OK!\r\n");
67+
break;
68+
case W25Q32:
69+
Serial.printf("W25Q32 is OK!\r\n");
70+
break;
71+
case W25Q64:
72+
Serial.printf("W25Q64 is OK!\r\n");
73+
break;
74+
case W25Q80:
75+
Serial.printf("W25Q80 is OK!\r\n");
76+
break;
77+
case W25Q128:
78+
Serial.printf("W25Q128 is OK!\r\n");
79+
break;
80+
default:
81+
Serial.printf("Flash is not valid!\r\n");
82+
break;
83+
}
84+
85+
//initial the writeBuffer
86+
for(i=0; i<256; i++)
87+
{
88+
writeBuffer[i] = i;
89+
}
90+
91+
//erase sector 0
92+
Serial.printf("Erase Flash:\r\n");
93+
eraseSector(0);
94+
readFlashToBuff(readBuffer, 0, 256);
95+
for(i=0; i<256; i++)
96+
{
97+
if(i%16==0 && i!=0) Serial.printf("\r\n");
98+
Serial.printf("%02x ",readBuffer[i]);
99+
}
100+
Serial.printf("\r\n");
101+
102+
Serial.printf("Write Flash:\r\n");
103+
writeFlashPage(writeBuffer, 0, 256);
104+
readFlashToBuff(readBuffer, 0, 256);
105+
for(i=0; i<256; i++)
106+
{
107+
if(i%16==0 && i!=0) Serial.printf("\r\n");
108+
Serial.printf("%02x ",readBuffer[i]);
109+
}
110+
Serial.printf("\r\n");
111+
112+
}
113+
114+
void loop() {
115+
116+
117+
118+
}
119+
120+
121+
122+
123+
//Read Flash ID
124+
uint16_t readFlashID(void)
125+
{
126+
uint16_t id;
127+
digitalWrite(chipSelectPin, LOW);
128+
SPI.transfer(W25X_ManufactDeviceID);
129+
SPI.transfer(0x00);
130+
SPI.transfer(0x00);
131+
SPI.transfer(0x00);
132+
id |= SPI.transfer(0xFF) << 8;
133+
id |= SPI.transfer(0xFF);
134+
digitalWrite(chipSelectPin, HIGH);
135+
return id;
136+
}
137+
138+
//Read Flash SR
139+
//bit7 6 5 4 3 2 1 0
140+
//SPR RV TB BP2 BP1 BP0 WEL BSY
141+
uint8_t readFlashSR(void)
142+
{
143+
uint8_t status;
144+
digitalWrite(chipSelectPin, LOW);
145+
SPI.transfer(W25X_ReadStatusReg);
146+
status = SPI.transfer(0xff);
147+
digitalWrite(chipSelectPin, HIGH);
148+
return status;
149+
}
150+
151+
//Write Flash SR
152+
void writeFlashSR(uint8_t status)
153+
{
154+
digitalWrite(chipSelectPin, LOW);
155+
SPI.transfer(W25X_WriteStatusReg);
156+
SPI.transfer(status);
157+
digitalWrite(chipSelectPin, HIGH);
158+
}
159+
160+
//Write Enable
161+
void writeEnable(void)
162+
{
163+
digitalWrite(chipSelectPin, LOW);
164+
SPI.transfer( W25X_WriteEnable );
165+
digitalWrite(chipSelectPin, HIGH);
166+
}
167+
168+
//Write Disable
169+
void writeDisable(void)
170+
{
171+
digitalWrite(chipSelectPin, LOW);
172+
SPI.transfer( W25X_WriteDisable );
173+
digitalWrite(chipSelectPin, HIGH);
174+
}
175+
176+
//Erase sector,,4K bytes per sector
177+
void eraseSector(uint32_t sectorNum)
178+
{
179+
sectorNum *= 4096;
180+
writeEnable();
181+
while( ( readFlashSR() & 0x01 ) == 0x01 ); //waite for busy
182+
183+
digitalWrite(chipSelectPin, LOW);
184+
SPI.transfer(W25X_SectorErase);
185+
SPI.transfer((uint8_t)(sectorNum >> 16));
186+
SPI.transfer((uint8_t)(sectorNum >> 8));
187+
SPI.transfer((uint8_t)(sectorNum));
188+
digitalWrite(chipSelectPin, HIGH);
189+
190+
while( ( readFlashSR() & 0x01 ) == 0x01 ); //waite for busy
191+
}
192+
193+
194+
//read flash data to pBuf
195+
void readFlashToBuff(uint8_t *pBuf, uint32_t readAddr, uint16_t size)
196+
{
197+
uint16_t i;
198+
digitalWrite(chipSelectPin, LOW);
199+
SPI.transfer(W25X_ReadData);
200+
SPI.transfer((uint8_t)(readAddr >> 16));
201+
SPI.transfer((uint8_t)(readAddr >> 8));
202+
SPI.transfer((uint8_t)(readAddr));
203+
for(i=0; i<size; i++)
204+
{
205+
pBuf[i] = SPI.transfer(0xff);
206+
}
207+
digitalWrite(chipSelectPin, HIGH);
208+
}
209+
210+
211+
212+
//write data to flash
213+
void writeFlashPage(uint8_t *pBuf, uint32_t writeAddr, uint16_t size)
214+
{
215+
uint16_t i;
216+
writeEnable();
217+
while( ( readFlashSR() & 0x01 ) == 0x01 ); //waite for busy
218+
digitalWrite(chipSelectPin, LOW);
219+
SPI.transfer(W25X_PageProgram);
220+
SPI.transfer((uint8_t)(writeAddr >> 16));
221+
SPI.transfer((uint8_t)(writeAddr >> 8));
222+
SPI.transfer((uint8_t)(writeAddr));
223+
for(i=0; i<size; i++)
224+
{
225+
SPI.transfer(pBuf[i]);
226+
}
227+
digitalWrite(chipSelectPin, HIGH);
228+
while( ( readFlashSR() & 0x01 ) == 0x01 ); //waite for busy
229+
}

libraries/SPI/keywords.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#######################################
2+
# Syntax Coloring Map SPI
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
SPI KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
begin KEYWORD2
15+
end KEYWORD2
16+
transfer KEYWORD2
17+
#setBitOrder KEYWORD2
18+
setDataMode KEYWORD2
19+
setClockDivider KEYWORD2
20+
setMISO KEYWORD2
21+
setMOSI KEYWORD2
22+
setSCLK KEYWORD2
23+
setSSEL KEYWORD2
24+
25+
#######################################
26+
# Constants (LITERAL1)
27+
#######################################
28+
SPI_MODE0 LITERAL1
29+
SPI_MODE1 LITERAL1
30+
SPI_MODE2 LITERAL1
31+
SPI_MODE3 LITERAL1
32+
33+
SPI_CONTINUE LITERAL1
34+
SPI_LAST LITERAL1

libraries/SPI/library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=SPI
2+
version=1.0.0
3+
author=Modified by Temperslee
4+
maintainer=CH32duino
5+
sentence=Enables the communication with devices that use the Serial Peripheral Interface (SPI) Bus.
6+
paragraph=This library is based on the official Arduino SPI library and adapted to CH32 boards.
7+
category=Communication
8+
url=http://www.arduino.cc/en/Reference/SPI
9+
architectures=ch32v

0 commit comments

Comments
 (0)