unsigned char **cmd与char *argv[]有何区别?
时间: 2024-06-19 11:02:40 浏览: 8
unsigned char **cmd和char *argv[]都是指向字符指针数组的指针,它们的主要区别在于数据类型不同。unsigned char **cmd是指向无符号字符指针的指针,而char *argv[]是指向字符指针的数组。
通常情况下,char *argv[]被用来存储程序运行时的参数,例如在命令行中使用的参数。而unsigned char **cmd则可以用于存储任意类型的字符串数据,包括ASCII码和Unicode编码的字符串。
在使用这两种类型的指针时,需要注意它们的数据类型和指向的数据类型是否一致,以免出现数据类型转换错误或内存错误等问题。
相关问题
w25q128用C实现读写操作
以下为w25q128的C语言读写操作代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wiringPiSPI.h>
#define W25Q128_CS 0 // W25Q128芯片片选引脚
#define SPI_SPEED 500000 // SPI通信速度
// W25Q128的命令集
#define W25Q128_CMD_WRITE_ENABLE 0x06
#define W25Q128_CMD_WRITE_DISABLE 0x04
#define W25Q128_CMD_READ_STATUS_REG1 0x05
#define W25Q128_CMD_READ_DATA 0x03
#define W25Q128_CMD_PAGE_PROGRAM 0x02
#define W25Q128_CMD_ERASE_SECTOR 0x20
#define W25Q128_CMD_ERASE_CHIP 0xC7
// W25Q128的状态寄存器1位定义
#define W25Q128_SR1_BUSY_BIT (1 << 0)
// W25Q128的函数声明
static void w25q128_write_enable();
static void w25q128_write_disable();
static void w25q128_wait_for_busy();
static void w25q128_read_status_reg1(unsigned char *status);
static void w25q128_write_data(unsigned int addr, unsigned char *buf, unsigned int len);
static void w25q128_read_data(unsigned int addr, unsigned char *buf, unsigned int len);
static void w25q128_erase_sector(unsigned int addr);
static void w25q128_erase_chip();
int main(int argc, char *argv[])
{
unsigned char buf[256];
// 初始化SPI
wiringPiSPISetup(0, SPI_SPEED);
// 读取W25Q128的状态寄存器1
unsigned char status;
w25q128_read_status_reg1(&status);
printf("W25Q128 Status Reg1: 0x%02x\n", status);
// 写入数据
memset(buf, 0xaa, sizeof(buf));
w25q128_write_data(0x10000, buf, sizeof(buf));
// 读取数据
memset(buf, 0x00, sizeof(buf));
w25q128_read_data(0x10000, buf, sizeof(buf));
printf("Data: %02x%02x%02x%02x...\n", buf[0], buf[1], buf[2], buf[3]);
// 擦除扇区
w25q128_erase_sector(0x10000);
// 擦除整个芯片
w25q128_erase_chip();
return 0;
}
// 写使能
static void w25q128_write_enable()
{
unsigned char cmd = W25Q128_CMD_WRITE_ENABLE;
wiringPiSPIDataRW(0, &cmd, 1);
}
// 写禁止
static void w25q128_write_disable()
{
unsigned char cmd = W25Q128_CMD_WRITE_DISABLE;
wiringPiSPIDataRW(0, &cmd, 1);
}
// 等待忙结束
static void w25q128_wait_for_busy()
{
unsigned char status = 0;
do {
w25q128_read_status_reg1(&status);
delay(1);
} while (status & W25Q128_SR1_BUSY_BIT);
}
// 读取状态寄存器1
static void w25q128_read_status_reg1(unsigned char *status)
{
unsigned char cmd = W25Q128_CMD_READ_STATUS_REG1;
wiringPiSPIDataRW(0, &cmd, 1);
wiringPiSPIDataRW(0, status, 1);
}
// 写入数据
static void w25q128_write_data(unsigned int addr, unsigned char *buf, unsigned int len)
{
w25q128_write_enable();
unsigned char cmd[4];
cmd[0] = W25Q128_CMD_PAGE_PROGRAM;
cmd[1] = (addr >> 16) & 0xff;
cmd[2] = (addr >> 8) & 0xff;
cmd[3] = addr & 0xff;
wiringPiSPIDataRW(0, cmd, 4);
wiringPiSPIDataRW(0, buf, len);
w25q128_wait_for_busy();
w25q128_write_disable();
}
// 读取数据
static void w25q128_read_data(unsigned int addr, unsigned char *buf, unsigned int len)
{
unsigned char cmd[4];
cmd[0] = W25Q128_CMD_READ_DATA;
cmd[1] = (addr >> 16) & 0xff;
cmd[2] = (addr >> 8) & 0xff;
cmd[3] = addr & 0xff;
wiringPiSPIDataRW(0, cmd, 4);
wiringPiSPIDataRW(0, buf, len);
}
// 擦除扇区
static void w25q128_erase_sector(unsigned int addr)
{
w25q128_write_enable();
unsigned char cmd[4];
cmd[0] = W25Q128_CMD_ERASE_SECTOR;
cmd[1] = (addr >> 16) & 0xff;
cmd[2] = (addr >> 8) & 0xff;
cmd[3] = addr & 0xff;
wiringPiSPIDataRW(0, cmd, 4);
w25q128_wait_for_busy();
w25q128_write_disable();
}
// 擦除整个芯片
static void w25q128_erase_chip()
{
w25q128_write_enable();
unsigned char cmd = W25Q128_CMD_ERASE_CHIP;
wiringPiSPIDataRW(0, &cmd, 1);
w25q128_wait_for_busy();
w25q128_write_disable();
}
```
需要注意的是,在使用SPI通信时,需要先初始化SPI,然后通过wiringPiSPIDataRW函数进行读写操作。同时,为了避免写入数据时出现错误,需要先执行写使能操作,然后等待芯片忙结束后再执行写禁止操作。
ZYNQnandflush读写实例
ZYNQ是Xilinx公司推出的一款SoC芯片,其中包含了NAND闪存控制器。在ZYNQ上使用nandflush进行读写操作需要进行一些特殊的配置。以下是一个ZYNQnandflush读写的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#define PAGE_SIZE 0x2000
#define BLOCK_SIZE 0x40000
#define NAND_BASEADDR 0xE1000000
#define NAND_SIZE 0x1000
#define NAND_CMD_ADDR 0xE1000010
#define NAND_ADDR_ADDR 0xE1000014
#define NAND_DATA_ADDR 0xE1000018
volatile unsigned int *nand_cmd;
volatile unsigned int *nand_addr;
volatile unsigned char *nand_data;
void zynq_nand_init(void)
{
int fd;
fd = open("/dev/mem", O_RDWR | O_SYNC);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
nand_cmd = mmap(NULL, NAND_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, NAND_CMD_ADDR);
if (nand_cmd == MAP_FAILED) {
perror("mmap");
exit(EXIT_FAILURE);
}
nand_addr = mmap(NULL, NAND_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, NAND_ADDR_ADDR);
if (nand_addr == MAP_FAILED) {
perror("mmap");
exit(EXIT_FAILURE);
}
nand_data = mmap(NULL, NAND_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, NAND_DATA_ADDR);
if (nand_data == MAP_FAILED) {
perror("mmap");
exit(EXIT_FAILURE);
}
close(fd);
}
void zynq_nand_read(unsigned int page, unsigned int offset, unsigned char *data, unsigned int length)
{
unsigned int addr;
// 设置页和偏移量
addr = (page * BLOCK_SIZE) + (offset % PAGE_SIZE);
// 发送读命令
nand_cmd[0] = 0x00;
nand_addr[0] = addr & 0xFF;
nand_addr[1] = (addr >> 8) & 0xFF;
nand_addr[2] = (addr >> 16) & 0xFF;
nand_cmd[0] = 0x30;
// 读取数据
while (length--) {
*data++ = *nand_data;
}
}
void zynq_nand_write(unsigned int page, unsigned int offset, unsigned char *data, unsigned int length)
{
unsigned int addr;
// 设置页和偏移量
addr = (page * BLOCK_SIZE) + (offset % PAGE_SIZE);
// 发送写命令
nand_cmd[0] = 0x80;
nand_addr[0] = addr & 0xFF;
nand_addr[1] = (addr >> 8) & 0xFF;
nand_addr[2] = (addr >> 16) & 0xFF;
// 写入数据
while (length--) {
*nand_data = *data++;
}
// 发送写确认命令
nand_cmd[0] = 0x10;
}
int main(int argc, char *argv[])
{
unsigned char *data;
unsigned int page, offset, length;
// 初始化NAND控制器
zynq_nand_init();
// 设置页和偏移量
page = 0;
offset = 0;
// 分配内存空间
length = PAGE_SIZE;
data = malloc(length);
if (!data) {
perror("malloc");
exit(EXIT_FAILURE);
}
// 读取数据
zynq_nand_read(page, offset, data, length);
// 修改数据
data[0] = 0x55;
data[1] = 0xAA;
// 写入数据
zynq_nand_write(page, offset, data, length);
// 释放内存空间
free(data);
return 0;
}
```
这个示例首先通过mmap函数将NAND控制器的寄存器映射到用户空间。它设置要读取的页和偏移量,并分配足够的内存来存储要读取或写入的数据。然后,它使用zynq_nand_read和zynq_nand_write函数读取和写入数据。最后,它释放内存空间并关闭NAND控制器的映射。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)