SOME/IP-Return Code

Return Code返回代码

  • The Error Handling is based on an 8 Bit Std_returnType of AUTOSAR. The two most significant bits are reserved and shall be set to 0. The receiver of a return code shall ignore the values of the two most significant bits.
    错误处理基于AUTOSAR的8位Std_returnType。两个最高有效位保留,并应设置为0.接收返回代码时应忽略两个最高有效位的值。

  • The system shall not return an error message for events/notifications.
    系统不应针对events/notification返回错误消息。

  • The system shall not return an error message for fire&forget methods.
    系统不应针对fire&forget method返回错误消息。

  • The system shall not return an error message for events/notifications and fire&forget methods if the Message Type is set incorrectly to Request or Response.
    如果消息类型被不正确地设置为Request or Response ,则系统也不应返回错误消息给events/notification 和 fire&forget method。

  • For request/response methods the error message shall copy over the fields of the SOME/IP header (i.e. Message ID, Request ID, and Interface Version) but not the payload. In addition Message Type and Return Code have to be set to the appropriate values.
    对于Request/Response method, error消息应复制SOME / IP header(比如消息ID,请求ID和接口版本),但不包括payload。此外,必须将Message Type 和Return Code设置为适当的值。
    在这里插入图片描述

  • The SOME/IP implementation shall not use an unknown protocol version but write a supported protocol version in the header.
    实施SOME / IP不应使用未知protocol version ,而是在header中写入支持的协议版本。

  • Generation and handling of return codes shall be configurable.
    返回码的生成和处理应该是可配置的。

  • Implementations shall not answer with errors to SOME/IP message already carrying an error (i.e. return code 0x01 - 0x1f).
    在实现上,不应用含有error的SOME/IP消息去回复已经携带了error的消息(比如. 返回码 0x01 - 0x1f)。

  • The following Return Codes are currently defined and shall be implemented as described:
    目前定义了以下返回代码,应按如下描述进行实施:

IDNameDescription
0x00E_OKNo error occurred
0x01E_NOT_OKAn unspecified error occurred
0x02E_UNKNOWN_SERVICEThe requested Service ID is unknown.
0x03E_UNKNOWN_METHODThe requested Method ID is unknown. Service ID is known.
0x04E_NOT_READYService ID and Method ID are known. Application not running.
0x05E_NOT_REACHABLESystem running the service is not reachable (internal error code only).
0x06E_TIMEOUTA timeout occurred (internal error code only).
0x07E_WRONG_PROTOCOL_ VERSIONVersionofSOME/IPprotocolnotsupported
0x08E_WRONG_INTERFACE_ VERSIONInterface version mismatch
0x09E_MALFORMED_MESSAGEDeserialization error, so that payload cannot be deserialized.
0x0aE_WRONG_MESSAGE_TYPEAn unexpected message type was received (e.g. REQUEST_NO_RETURN for a method defined as REQUEST.)
0x0b - 0x1fRESERVEDReserved for generic SOME/IP errors. These errors will be specified in future versions of this document.
0x20 - 0x3fRESERVEDReservedforspecificerrorsofservicesand methods. These errors are specified by the interface specification.

Error Message Format错误消息格式

  • For a more flexible error handling, SOME/IP allows the user to specify a message layout specific for errors instead of using the message layout for response messages. This is defined by the interface specification and can be used to transport exceptions of higher level programming languages.
    为了实现更灵活的error处理,SOME / IP允许用户指定专门的error message布局,而不是沿用response message的布局。 这由接口规范定义,可用于传输更高级编程语言的异常。
  • The recommended layout for the exception message is the following:
    异常消息的推荐布局如下:
    -Union of specific exceptions. At least a generic exception without fields needs to exist.
    特定异常的union。至少存在一个没有字段的通用异常。
    -Dynamic Length String for exception description.
    动态长度字符串用于异常描述。
  • The union gives the flexibility to add new exceptions in the future in a type-safe manner. The string is used to transport human readable exception descriptions to ease testing and debugging.
    union提供了以type-safe方式在将来添加新异常的灵活性。 该字符串用于传输具有可读性的异常描述,以便于测试和调试。
//----------------------------------------------------------------------------- // Includes //----------------------------------------------------------------------------- #include <SI_C8051F300_Register_Enums.h> #include <stdio.h> //----------------------------------------------------------------------------- // Global CONSTANTS //----------------------------------------------------------------------------- #define SYSCLK 24500000 // SYSCLK frequency in Hz #define BAUDRATE 115200 // Baud rate of UART in bps SI_SBIT(DE_RE, SFR_P0, 6); //定義P0.6 控制名稱為DE_RE SI_SBIT(LED2, SFR_P0, 1); //定義P0.1 控制名稱為LED2 SI_SBIT(LED1, SFR_P0, 0); //定義P0.0 控制名稱為LED1 SI_SBIT(LED3, SFR_P0, 2); //定義P0.2 控制名稱為LED3 SI_SBIT(LED4, SFR_P0, 3); //定義P0.3 控制名稱為LED4 SI_SBIT(LED5, SFR_P0, 7); //定義P0.7 控制名稱為LED5 //----------------------------------------------------------------------------- // Function PROTOTYPES //----------------------------------------------------------------------------- SI_INTERRUPT_PROTO(UART0_Interrupt, 4); void SYSCLK_Init (void); void UART0_Init (void); void PORT_Init (void); void Timer2_Init (int16_t); void MyDelay (uint16_t Value); #define KEY_DOWN 1 #define KEY_UP 0 #define LED_ON 0 #define LED_OFF 1 //----------------------------------------------------------------------------- // Global Variables //----------------------------------------------------------------------------- #define UART_BUFFERSIZE 6 uint8_t UART_Buffer[UART_BUFFERSIZE]; uint8_t UART_Buffer_Size = 0; uint8_t UART_Input_First = 0; uint8_t UART_Output_First = 0; uint8_t TX_Ready =1; uint8_t RX_Ready =0; static char Byte; uint8_t SnapDown_TimeCnt =0; uint8_t fSnap = 0; uint8_t RecDown_TimeCnt =0; uint8_t fRec = 0; //----------------------------------------------------------------------------- // SiLabs_Startup() Routine // ---------------------------------------------------------------------------- // This function is called immediately after reset, before the initialization // code is run in SILABS_STARTUP.A51 (which runs before main() ). This is a // useful place to disable the watchdog timer, which is enable by default // and may trigger before main() in some instances. //----------------------------------------------------------------------------- void SiLabs_Startup (void) { PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer } //----------------------------------------------------------------------------- // MAIN Routine //----------------------------------------------------------------------------- void main (void) { // enable) PORT_Init(); // Initialize Port I/O SYSCLK_Init (); // Initialize Oscillator UART0_Init(); IE_EA = 1; DE_RE =0; Timer2_Init (SYSCLK / 12 / 1000); // Init Timer2 to generate // 每1ms 計時中斷 while(1) { if(RX_Ready) { RX_Ready = 0; UART_Buffer_Size =0; if(UART_Buffer[3]==0x01)//任一鍵按下 { if(UART_Buffer[4]==0x01)//REC { fRec = KEY_DOWN; RecDown_TimeCnt=0; } else if(UART_Buffer[4]==0x02)//SNAP { fSnap = KEY_DOWN; SnapDown_TimeCnt=0; } else if(UART_Buffer[4]==0x03)//LED UP LED3 = LED_ON; else if(UART_Buffer[4]==0x04)//LED DO LED4 = LED_ON; else if(UART_Buffer[4]==0x05)//Mirror LED5 = LED_ON; } if(UART_Buffer[3]==0x00)//任一鍵放開 { if(UART_Buffer[4]==0x01)//REC { fRec = KEY_UP; } else if(UART_Buffer[4]==0x02)//SNAP { fSnap = KEY_UP; } else if(UART_Buffer[4]==0x03)//LED UP LED3 = LED_OFF; else if(UART_Buffer[4]==0x04)//LED DO LED4 = LED_OFF; else if(UART_Buffer[4]==0x05)//Mirror LED5 = LED_OFF; } IE_ES0 =1; } if(SnapDown_TimeCnt >55 && fSnap == KEY_DOWN) { LED1 = LED_ON; } else if(SnapDown_TimeCnt >55 && fSnap == KEY_UP) { LED1 =LED_OFF; SnapDown_TimeCnt =0; } else if(SnapDown_TimeCnt >45 && SnapDown_TimeCnt <55 && fSnap == KEY_UP) { LED1 = LED_ON; LED2 = LED_ON; MyDelay(1100);//延遲約0.5秒 LED1 = LED_OFF; LED2 = LED_OFF; SnapDown_TimeCnt =0; } if(RecDown_TimeCnt >55 && fRec == KEY_DOWN) { LED2 = LED_ON; } else if(RecDown_TimeCnt >55 && fRec == KEY_UP) { LED2 = LED_OFF; RecDown_TimeCnt =0; } else if(RecDown_TimeCnt >45 && RecDown_TimeCnt <55 && fRec == KEY_UP) { LED4 = LED_ON; LED5 = LED_ON; MyDelay(1100);//延遲約0.5秒 LED4 = LED_OFF; LED5 = LED_OFF; RecDown_TimeCnt =0; } } } //----------------------------------------------------------------------------- // Initialization Subroutines //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // PORT_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // Configure the Crossbar and GPIO ports. // // P0.4 digital push-pull UART TX // P0.5 digital open-drain UART RX // //----------------------------------------------------------------------------- void PORT_Init (void) { P0MDOUT |= 0x10; // set UART TX to push-pull output XBR1 = 0x03; // Enable UTX, URX as push-pull output XBR2 = 0x40; // Enable crossbar, weak pull-ups // disabled XBR0 = 0x40; P0MDOUT |= 0x40; } //----------------------------------------------------------------------------- // SYSCLK_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // This routine initializes the system clock to use the internal oscillator // at its maximum frequency. // Also enables the Missing Clock Detector. //----------------------------------------------------------------------------- void SYSCLK_Init (void) { OSCICN |= 0x03; // Configure internal oscillator for // its maximum frequency RSTSRC = 0x04; // Enable missing clock detector } //----------------------------------------------------------------------------- // UART0_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1. //----------------------------------------------------------------------------- void UART0_Init (void) { SCON0 = 0x10; // SCON0: 8-bit variable bit rate // level of STOP bit is ignored // RX enabled // ninth bits are zeros // clear SCON0_RI and SCON0_TI bits if (SYSCLK/BAUDRATE/2/256 < 1) { TH1 = -(SYSCLK/BAUDRATE/2); CKCON |= 0x10; // T1M = 1; SCA1:0 = xx } else if (SYSCLK/BAUDRATE/2/256 < 4) { TH1 = -(SYSCLK/BAUDRATE/2/4); CKCON |= 0x01; // T1M = 0; SCA1:0 = 01 CKCON &= ~0x12; } else if (SYSCLK/BAUDRATE/2/256 < 12) { TH1 = -(SYSCLK/BAUDRATE/2/12); CKCON &= ~0x13; // T1M = 0; SCA1:0 = 00 } else { TH1 = -(SYSCLK/BAUDRATE/2/48); CKCON |= 0x02; // T1M = 0; SCA1:0 = 10 CKCON &= ~0x11; } TL1 = 0xff; // set Timer1 to overflow immediately TMOD |= 0x20; // TMOD: timer 1 in 8-bit autoreload TMOD &= ~0xD0; // mode TCON_TR1 = 1; // START Timer1 TX_Ready = 1; // Flag showing that UART can transmit IP |= 0x10; // Make UART high priority IE_ES0 = 1; // Enable UART0 interrupts } //----------------------------------------------------------------------------- // Interrupt Service Routines //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // UART0_Interrupt //----------------------------------------------------------------------------- // // This routine is invoked whenever a character is entered or displayed on the // Hyperterminal. // //----------------------------------------------------------------------------- SI_INTERRUPT(UART0_Interrupt, 4) { if (SCON0_RI == 1) { if( UART_Buffer_Size == 0) { // If new word is entered UART_Input_First = 0; } SCON0_RI = 0; // Clear interrupt flag Byte = SBUF0; // Read a character from UART if (UART_Buffer_Size < UART_BUFFERSIZE) { UART_Buffer[UART_Input_First] = Byte; // Store in array UART_Buffer_Size++; // Update array's size UART_Input_First++; // Update counter } if(UART_Buffer_Size >= UART_BUFFERSIZE) { RX_Ready =1; UART_Buffer_Size =0; IE_ES0 = 0; } } if (SCON0_TI == 1) // Check if transmit flag is set { SCON0_TI = 0; // Clear interrupt flag if (UART_Buffer_Size != 1) // If buffer not empty { // If a new word is being output if ( UART_Buffer_Size == UART_Input_First ) { UART_Output_First = 0; } // Store a character in the variable byte Byte = UART_Buffer[UART_Output_First]; if ((Byte >= 0x61) && (Byte <= 0x7A)) { // If upper case letter Byte -= 32; } SBUF0 = Byte; // Transmit to Hyperterminal UART_Output_First++; // Update counter UART_Buffer_Size--; // Decrease array size } else { UART_Buffer_Size = 0; // Set the array size to 0 TX_Ready = 1; // Indicate transmission complete } } } //----------------------------------------------------------------------------- // Timer2_Init //----------------------------------------------------------------------------- // // Configure Timer2 to 16-bit auto-reload and generate an interrupt at // interval specified by <counts> using SYSCLK/12 as its time base. // void Timer2_Init (int16_t counts) { TMR2CN = 0x00; // Stop Timer2; Clear TF2; // use SYSCLK/12 as timebase CKCON &= ~(CKCON_T2MH__BMASK | CKCON_T2ML__BMASK ); // Timer2 clocked based on TMR2CN_T2XCLK TMR2RL = -counts; // Init reload values TMR2 = 0xffff; // set to reload immediately IE_ET2 = 1; // enable Timer2 interrupts TMR2CN_TR2 = 1; // start Timer2 } //----------------------------------------------------------------------------- // Timer2_ISR //----------------------------------------------------------------------------- // This routine changes the state of the LED whenever Timer2 overflows. // SI_INTERRUPT(Timer2_ISR, TIMER2_IRQn) { TMR2CN_TF2H = 0; // clear Timer2 interrupt flag if(fSnap == KEY_DOWN) { SnapDown_TimeCnt++; // change state of LED if(SnapDown_TimeCnt==255) SnapDown_TimeCnt = 254; } else if(fRec == KEY_DOWN) { RecDown_TimeCnt++; // change state of LED if(RecDown_TimeCnt==255) RecDown_TimeCnt = 254; } } void MyDelay(uint16_t Value) { uint16_t i,j; for(i=0;i<Value;i++) { for(j=0;j<1000;j++) _nop_(); } } //----------------------------------------------------------------------------- // End Of File //-----------------------------------------------------------------------------
最新发布
07-04
### UART Communication and LED Control on C8051F300 Microcontroller The C8051F300 microcontroller is a popular choice for embedded systems due to its integrated peripherals, including UART communication capabilities. Understanding how to implement UART communication and control LEDs can be essential for various applications, such as data logging, sensor interfacing, and user feedback mechanisms. To establish UART communication, the microcontroller must be configured with appropriate settings for baud rate, data bits, parity, and stop bits. The following code snippet demonstrates how to initialize the UART module: ```c #include <C8051F300.h> void UART_Init(void) { SCON = 0x50; // Set UART mode to 8-bit, variable baud rate TMOD |= 0x20; // Timer 1 in mode 2 (8-bit auto-reload) TH1 = 0xFD; // Baud rate setting for 9600 bps TR1 = 1; // Start Timer 1 TI = 1; // Indicate that transmitter is ready } ``` In this initialization function, `SCON` is set to configure the UART for 8-bit data format with variable baud rate. The `TMOD` register is configured to operate Timer 1 in mode 2, which allows for an 8-bit auto-reload feature necessary for generating accurate baud rates. The value loaded into `TH1` determines the baud rate, where `0xFD` corresponds to a baud rate of 9600 bps[^1]. For LED control, the microcontroller's GPIO pins can be manipulated to turn LEDs on or off based on specific conditions within the program. Typically, an LED might be connected to a port pin through a current-limiting resistor to ground. Here's an example of how to toggle an LED connected to P1.0: ```c void Toggle_LED(void) { P1 ^= 0x01; // Toggle P1.0 } ``` This function uses bitwise XOR operation to invert the state of P1.0, effectively toggling the LED connected to it. Debugging issues related to UART communication often involves checking the configuration registers (`SCON`, `TMOD`, `TH1`) and ensuring that the correct baud rate is being used. Additionally, verifying the physical connections between devices and using oscilloscopes or logic analyzers can help identify timing issues or incorrect signal levels. When dealing with LED control, common problems include incorrect pin configurations, faulty wiring, or misinterpretation of active-high versus active-low signals. It's crucial to double-check the microcontroller's datasheet to ensure proper configuration of the I/O ports and any necessary pull-up or pull-down resistors.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值