C8051F310 模拟C2协议浅析与实现

C2

C2协议标准

  • Each C2 frame is initiated with a START condition
  • Each C2 frame terminates with a STOP condition
  • All C2 devices include an 8-bit Device ID register and an 8-bit Revision ID register

寄存器

ADDRESS: Address Register

  • 1.ADDRESS selects which C2 Data register is accessed during C2 Data Read/Write frames.
  • 2.During Address Read frames, ADDRESS provides PI status information.

The Address Read command returns an 8-bit status code

Bit Name Description
7 EBusy or FLBusy This bit indicates when the EPROM or Flash is busy completing an operation.
6 EError This bit is set to 1 when the EPROM encounters an error.
5:2 Unused
1 InBusy This bit is set to 1 by the C2 Interface following a write to FPDAT. It is cleared to 0 when the PI acknowledges the write to FPDAT.
0 OutReady This bit is set to 1 by the PI when output data is available in the FPDAT regis-ter.

DEVICEID: Device ID Register

The Device ID register (DEVICEID) is a read-only C2 Data register containing the 8-bit device identifier of the target C2 device. The C2 address for register DEVICEID is 0x00

REVID: Revision ID Register

The Revision ID register (REVID) is a read-only C2 Data register containing the 8-bit revision identifier of the target C2 device. The C2 address for register REVID is 0x01

Data Register 0 ~ Data Register M-1
Revision ID (REVID)
Device ID (DEVICEID)	
ADDRESS

帧 C2 Instruction Frames

master访问目标device 有四种基本格式
C8051F310 模拟C2协议浅析与实现_第1张图片

Instruction INS Code
Data Read 00b
Address Read 1
Data Write 01b
Address Write 11b

查看Table 1.3. C2 Bit Field Descriptions
此表讲述了有关于帧 的详细内容。

Address Read Frame

C8051F310 模拟C2协议浅析与实现_第2张图片
This instruction is typically used to quickly access status information。

Address Write Frame

C8051F310 模拟C2协议浅析与实现_第3张图片
An Address Write frame loads the target Address register。


Data Write Frame

C8051F310 模拟C2协议浅析与实现_第4张图片
A Data Write frame writes a specified value to the target Data register, as selected by the target Address register.
DATA length in bytes = LENGTH + 1。
如果要传递的数据是一个字节。那么INS字段的值就应该是00b

Data Read Frame

在这里插入图片描述
A Data Read frame reads the contents of the target Data register, as selected by the target Address register.

Address Write, 
Address Read,
Data Write,
Data Read
  • 两个CPU。
    • MCU_master
    • MCU_key
      MCU_master模拟C2协议给MCU_key烧写程序。

C2 Timing Specifications 时序图

Device Reset Timing

C8051F310 模拟C2协议浅析与实现_第5张图片

Address Write Timing

C8051F310 模拟C2协议浅析与实现_第6张图片

Address Read Timing

C8051F310 模拟C2协议浅析与实现_第7张图片

Data Write Timing

C8051F310 模拟C2协议浅析与实现_第8张图片

Data Read Timing

C8051F310 模拟C2协议浅析与实现_第9张图片

引脚分配

  • MCU_master
P0.7  - MCU_C2CK2  - C2CK
P1.0  - MCU_C2DAT2 - C2D

需要注意的事项

  • 1.If the C2CK master is an MCU, care should be taken to disable interrupts while C2CK is low in order to prevent a clock low time extending beyond 5 μs and potentially causing a target device reset.

C2相应的操作

增加了一个设备的开关操作。对于MCU_master而言需要传送数据显然需要将其引脚设置为push pull

void C2CK_DriverOn(void){ P0MDOUT |= 0x80; }// P0 ^ 7 push pull
void C2CK_DriverOff(void){ 
    P0 |= 0x80;         // P0 ^ 7 set 1
    P0MDOUT &= (~0x80); // set P0^7 out  open-drain
    }
void C2D_DriverOn(void){
	P1MDOUT |= 0x01;
}
void C2D_DriverOff(void){
	P1 |= 0x01;
  	P1MDOUT &= (~0x01);
}

开启引脚就是将其设置为强推。关闭就是将其设置为开漏。没有驱动能力。

Device Reset

To generate the reset timing:

  1. Turn the C2CK driver on.
  2. Force C2CK low.
  3. Wait at least 20 μs.
  4. Force C2CK high.
  5. Wait at least 2 μs.
  6. (Optional) Turn the C2CK driver off.
    SetC2CK 高电平时一个下降沿保持最少tRD的时间然后一个上升沿。
  SetC2CK(LOW);
  Delay_us(20);
  SetC2CK(HIGH);

C2CK Clock Strobes

To generate C2CK clock strobes with a microcontroller-based programmer:

  1. Turn the C2CK driver on.
  2. Wait at least 40 ns. This helps ensure C2D data setup time.
  3. Force C2CK low. Ensure interrupts are disabled at this point.
  4. Wait between 80 and 5000 ns.
  5. Force C2CK high.
  6. Wait at least 120 ns. This helps ensure C2D data valid time.
void StrobeC2CK(void)
{
  SetC2CK(LOW);
  SetC2CK(HIGH);
}

注意 这里没有实现第一个步骤
因此调用StrobeC2CK之前需要确定C2CK为高电平

Performing the Address Write Instruction

The C2CK strobes mentioned in this section refer to the steps described in 1.5.2 C2CK Clock Strobes. To write to a target device’s ADDRESS register:

  1. Disable interrupts.
  2. Turn the C2CK driver on.
  3. Strobe C2CK. This will generate the START field and forces the C2D pin on the target device to become an input.
  4. Turn on the C2D driver.
  5. Force C2D high. This sets C2D for the 2-bit INS field, and the Address Write instruction is 11b.
  6. Strobe C2CK. This transfers the first bit of the INS field.
  7. Strobe C2CK. This transfers the second bit of the INS field.
  8. Force C2D to each bit of the address value being written to ADDRESS (starting with bit 0) and strobe C2CK for each bit.
  9. Turn off the C2D driver. This prepares C2D to possibly become an output from the target device.
  10. Strobe C2CK to generate the STOP field.
  11. (Optional) Turn off the C2CK driver.
  12. Re-enable interrupts.
void C2_WriteAR(unsigned char addr)
{
	static bit EA_SAVE = EA;
	EA = 0;
	C2CK_DriverOn();
	StrobeC2CK();
	C2D_DriverOn();
	SetC2D(HIGH);
	StrobeC2CK();
	//SetC2D(HIGH);  不确定需不需要再次实现C2D拉高 先保留着看一看
	StrobeC2CK();
	for (i = 0; i < 8; i++){
    SetC2D(addr & 0x01);
    StrobeC2CK();
    addr >>= 1;}  //发送每个字节 注意先发送低字节。
	C2D_DriverOff();
	// STOP field
  	StrobeC2CK();
  	C2CK_DriverOff();
  	EA = EA_SAVE;
}

解释
看了9号步骤才发现为什么在C2D_DriverOff以及C2CK_DriverOff为什么还需要将引脚从推挽设置为开漏。此时应该是需要接受来自target device输入。若是设置为推挽,那么target device就没有能力将引脚拉低,表示不了低电平。

Performing the Address Read Instruction

To read the status code from a target device’s ADDRESS register:

  1. Disable interrupts.
  2. Turn the C2CK driver on.
  3. Strobe C2CK. This will generate the START field and forces the C2D pin on the target device to become an input.
  4. Turn on the C2D driver.
  5. Force C2D low. This sets C2D for the first part of the 2-bit INS field, and the Address Read instruction is 01b.
  6. Strobe C2CK. This transfers the first bit of the INS field.
  7. Force C2D high. This sets C2D for the second part of the 2-bit INS field.
  8. Strobe C2CK. This transfers the second bit of the INS field.
  9. Turn off the C2D driver. This prepares C2D to become an output from the target device.
  10. Strobe C2CK and read C2D from the target device. The device will return the 8-bit status code returned from an Address Read instruction bit by bit starting with bit 0. C2CK must be strobed after each read of C2D.
  11. Strobe C2CK to generate the STOP field.
  12. (Optional) Turn off the C2CK driver.
  13. Re-enable interrupts.

基本和地址写指令区别不大,就是关闭中断然后引脚的设置

unsigned char C2_ReadAR(void)
{
	static bit EA_SAVE = EA;
	EA = 0;
	C2CK_DriverOn();
	// START field
	StrobeC2CK();
	C2D_DriverOn();
	//This sets C2D for the first part of the 2-bit INS field, and the Address Read instruction is 01b.
	SetC2D(LOW);
	//This transfers the first bit of the INS field.
	StrobeC2CK();
	This sets C2D for the second part of the 2-bit INS field.
	SetC2D(HIGH);
	// This transfers the second bit of the INS field.
	StrobeC2CK();
	C2D_DriverOff();
	for (i = 0; i < 8; i++){
    addr >>= 1;StrobeC2CK();
    if (GetC2D()) {addr |= 0x80;}  // 先接受bit0
    //STOP field.
    StrobeC2CK();
    C2CK_DriverOff();
    EA = EA_SAVE;
    return addr;
}

Performing the Data Write Instruction

  1. Disable interrupts.
  2. Turn on the C2CK driver.
  3. Strobe C2CK. This will generate the START field and forces the C2D pin on the target device to become an input.
  4. Turn on the C2D driver.
  5. Force C2D high. This sets C2D for the first part of the 2-bit INS field, and the Data Write instruction is 10b.
  6. Strobe C2CK. This transfers the first bit of the INS field.
  7. Force C2D low. This sets C2D for the second part of the 2-bit INS field.
  8. Strobe C2CK. This transfers the second bit of the INS field.
  9. Leave C2D low. This is the start of the LENGTH field, which will be 00b in this example.
  10. Strobe C2CK to start the LENGTH field.
  11. Strobe C2CK to finish transferring the LENGTH field.
  12. Force C2D to each bit of the data value being written to the data register (starting with bit 0) and strobe C2CK for each bit.
  13. Turn off the C2D driver. This prepares C2D to become an output from the target device.
  14. Srobe C2CK.
  15. Check C2D for a value of 1b. If C2D becomes 1, the WAIT field is over, as it can contain zero or more 0’s and exactly one 1b. If
    C2D is not 1b, strobe C2CK and check the status again.
  16. At this point, C2D is driving a 1, indicating the end of the WAIT field. Strobe C2CK to generate the STOP field.
  17. (Optional) Turn off the C2CK driver.
  18. Re-enable interrupts.
void C2_WriteDR(unsigned char dat)
{
  unsigned char i;
  static bit EA_SAVE;
  EA_SAVE = EA;
  EA = 0;
  // START field
  C2CK_DriverOn();
  StrobeC2CK();
  // INS field (01b, LSB first)
  C2D_DriverOn();
  SetC2D(HIGH);
  StrobeC2CK();
  SetC2D(LOW);
  StrobeC2CK();
  // LENGTH field (00b -> 1 byte)
  SetC2D(LOW);
  StrobeC2CK();
  SetC2D(LOW);
  StrobeC2CK();
  // DATA field
  for (i = 0; i < 8; i++)
  {
    SetC2D(dat & 0x01);
    StrobeC2CK();
    dat >>= 1;
  }
  C2D_DriverOff();
  // WAIT field
  do
  {
    StrobeC2CK();
  } while (!GetC2D());
  // STOP field
  StrobeC2CK();
  C2CK_DriverOff();

  EA = EA_SAVE;
}

Performing the Data Read Instruction

  1. Disable interrupts.
  2. Turn on the C2CK driver.
  3. Strobe C2CK. This will generate the START field and forces the C2D pin on the target device to become an input.
  4. Turn on the C2D driver.
  5. Force C2D low. This sets C2D for the first part of the 2-bit INS field, and the Data Read instruction is 00b.
  6. Strobe C2CK to transfer the first bit of the INS field.
  7. Strobe C2CK to transfer the second bit of the INS field.
  8. Leave C2D low. This is the start of the LENGTH field, which will be 00b in this example.
  9. Strobe C2CK to start the LENGTH field.
  10. Strobe C2CK to finish transferring the LENGTH field.
  11. Turn off the C2D driver. This prepares C2D to become an output from the target device.
  12. Strobe C2CK.
  13. Check C2D for a value of 1b. If C2D becomes 1, the WAIT field is over, as it can contain zero or more 0’s and exactly one 1b. If
    C2D is not 1b, strobe C2CK and check the status again.
  14. At this point, C2D is driving a 1, indicating the end of the WAIT field.
    15.Strobe C2CK and read C2D from the target device. The device will return the contents of the data register bit by bit starting with bit
  15. C2CK must be strobed after each read of C2D.
  16. Strobe C2CK to generate the STOP field.
  17. (Optional) Turn off the C2CK driver.
  18. Re-enable interrupts.
unsigned char C2_ReadDR(void)
{
  unsigned char i;
  unsigned char dat;
  static bit EA_SAVE;
  EA_SAVE = EA;
  EA = 0;
  // START field
  C2CK_DriverOn();
  StrobeC2CK();
  // INS field (00b, LSB first)
  C2D_DriverOn();
  SetC2D(LOW);
  StrobeC2CK();
  SetC2D(LOW);
  StrobeC2CK();
  // LENGTH field (00b -> 1 byte)
  SetC2D(LOW);
  StrobeC2CK();
  SetC2D(LOW);
  StrobeC2CK();
  C2D_DriverOff();
  // WAIT field
  do
  {
    StrobeC2CK();
  } while (!GetC2D());
  // DATA field
  dat = 0;
  for (i = 0; i < 8; i++)
  {
    dat >>= 1;
    StrobeC2CK();
    if (GetC2D())
      dat |= 0x80;
  }
  // STOP field
  StrobeC2CK();
  C2CK_DriverOff();

  EA = EA_SAVE;
  return dat;
}

Programming Registers

  • FPCTL: Flash Programming Control Register
  • FPDAT: Flash Programming Data Register
  • EPCTL: EPROM Programming Control Register
  • EPDAT: EPROM Programming Data Register
  • EPADDRH and EPADDRL: EPROM Programming Address
  • EPSTAT: EPROM Programming Status

其他

C2 isolation circuitry

参考资料

Markdown使用技巧总结——字体,颜色,字号,背景,首行缩进等
C2 协议标准手册
CSDN-markdown 扩展教程

你可能感兴趣的:(基础)