📄 app_ml675050.c
字号:
/* 7 segment LED : ALL OFF */
debug_msg(_7_LED1_OFF, NULL);
/* Clear send_receive_buffer and err_status */
for(i=0; i<I2C_BUFF_SIZE; i++)
{
I2C_SendData[i].reg_adr = i;
I2C_SendData[i].reg_data = i;
#ifdef I2C_MASTER
I2C_ReceivData[i].reg_adr = i;
#else
I2C_ReceivData[i].reg_adr = 0xa5a5;
#endif
I2C_ReceivData[i].reg_data = 0x5a;
}
#ifdef I2C_MASTER
/* Initailize I2C. */
i2c_trans_param.channel = 0; /* 0 channel */
i2c_trans_param.trans_mode = 0; /* Master */
i2c_trans_param.slave_addr = I2C_SLAVE_ADDRESS; /* */
i2c_trans_param.trans_speed = 1; /* Fast(400kbps) */
#else
/* Initailize I2C. */
i2c_trans_param.channel = 1; /* 1 channel */
i2c_trans_param.trans_mode = 1; /* Slave */
i2c_trans_param.slave_addr = I2C_SLAVE_ADDRESS; /* */
i2c_trans_param.trans_speed = 1; /* Fast(400kbps) */
#endif
handle_i2c = smpDrv_open(DRVNO_I2C, (uint32_t)(&i2c_trans_param));
if (handle_i2c == HANDLE_ID_1) {
/* data send/receve proc */
for(i=0; (i<I2C_BUFF_SIZE)&&(rtnVal==OK); i++){
#ifdef I2C_MASTER
rtnVal = smpDrv_write(DRVNO_I2C, (int8_t *)&(I2C_SendData[i]), 2, handle_i2c);
#else
rtnVal = smpDrv_read(DRVNO_I2C, (int8_t *)&(I2C_ReceivData[i]), 2, handle_i2c);
#endif
}
if(rtnVal == OK)
{
/* data receve/send proc */
for(i=0; (i<I2C_BUFF_SIZE)&&(rtnVal==OK); i++){
#ifdef I2C_MASTER
rtnVal = smpDrv_read(DRVNO_I2C, (int8_t *)&(I2C_ReceivData[i]), 2, handle_i2c);
#else
rtnVal = smpDrv_write(DRVNO_I2C, (int8_t *)&(I2C_SendData[i]), 2, handle_i2c);
#endif
}
if(rtnVal == OK)
{ /* received data compare */
if(cmp_i2c_data_fn(&I2C_ReceivData[0], &I2C_SendData[0], I2C_BUFF_SIZE) != OK)
{
rtnVal = I2C_DATA_COMPARE_ERROR;
}
}
}
}
/* Execution result */
if(rtnVal == OK){
debug_msg(_3_LED_OK, NULL); /* 3 bit LED : OK */
}else{
debug_msg(_3_LED_NG, NULL); /* 3 bit LED : NG */
}
switch(rtnVal){
case OK: /* No Error */
debug_msg(_7_LED1_NUM_0, NULL); /* success(7 segment LED: 0) */
break;
case I2C_DATA_ACK_ERROR: /* Error */
debug_msg(_7_LED1_NUM_1, NULL); /* non ACK for receive data(7 segment LED: 1) */
break;
case I2C_ADDR_ACK_ERROR: /* Error */
debug_msg(_7_LED1_NUM_2, NULL); /* non ACK for transmission address(7 segment LED: 2) */
break;
case I2C_BUS_ERROR: /* Error */
debug_msg(_7_LED1_NUM_3, NULL); /* arbitration error(7 segment LED: 3) */
break;
case I2C_DATA_COMPARE_ERROR: /* Error */
debug_msg(_7_LED1_NUM_4, NULL); /* data comparison error(7 segment LED: 4) */
break;
default:
debug_msg(_7_LED1_NUM_F, NULL); /* other error(7 segment LED: F) */
break;
}
return rtnVal;
}
/************************************************************************/
/* */
/* Function Name : cmp_i2c_data_fn */
/* Input : Pointer of destination data(received data) */
/* : Pointer of source data(compare data) */
/* : Length of data */
/* Output : OK(1): destination data == source data */
/* : ERROR(-1): destination data != source data(-1) */
/* */
/* Note : Compare destination data with source data. */
/* */
/************************************************************************/
#if defined(_WIN32)
int16_t cmp_i2c_data_fn(struct I2CTransForm *des_data, struct I2CTransForm *src_data, uint16_t len)
#else
static int16_t cmp_i2c_data_fn(struct I2CTransForm *des_data, struct I2CTransForm *src_data, uint16_t len)
#endif
{
int16_t rtnVal = OK;
int i;
for( i=0; i<len; i++){
if(des_data[i].reg_adr == src_data[i].reg_adr){
if(des_data[i].reg_data != src_data[i].reg_data){
rtnVal = ERROR;
}
}else{
rtnVal = ERROR;
}
}
return rtnVal;
}
/************************************************************************/
/* */
/* Function Name : cmp_data_fn */
/* Input : Pointer of destination data(received data) */
/* : Pointer of source data(compare data) */
/* : Length of data */
/* Output : OK(1): destination data == source data */
/* : ERROR(-1): destination data != source data(-1) */
/* */
/* Note : Compare destination data with source data. */
/* */
/************************************************************************/
#if 0
#if defined(_WIN32)
int16_t cmp_data_fn(uint8_t *des_data, uint8_t *src_data, uint16_t len)
#else
static int16_t cmp_data_fn(uint8_t *des_data, uint8_t *src_data, uint16_t len)
#endif
{
int16_t rtnVal = OK;
for( ; 0<len; ++des_data, ++src_data, --len){
if(*des_data != *src_data){
rtnVal = ERROR;
}
}
return rtnVal;
}
#endif
/************************************************************************/
/* */
/* Function Name : change_rtc_to_str */
/* Input : str String of RTC data. */
/* rtc_time Struct of RTC data. */
/* Output : String of RTC data. */
/* */
/* Note : Change RTC data to string. */
/* */
/************************************************************************/
static int8_t *change_rtc_to_str(int8_t *str, ML675050_RtcTime *rtc_time) {
str[0] = (int8_t)(((rtc_time->year >> 4) & 0x0F) + 0x30) ;
str[1] = (int8_t)((rtc_time->year & 0x0F) + 0x30) ;
str[2] = '/';
str[3] = (int8_t)(((rtc_time->month >> 4) & 0xF0) + 0x30) ;
str[4] = (int8_t)((rtc_time->month & 0x0F) + 0x30) ;
str[5] = '/';
str[6] = (int8_t)(((rtc_time->day >> 4) & 0x0F) + 0x30) ;
str[7] = (int8_t)((rtc_time->day & 0x0F) + 0x30) ;
str[8] = ' ';
str[9] = (int8_t)((((rtc_time->hour >> 4) & 0x0F) & 0x03) + 0x30) ;
str[10] = (int8_t)(( rtc_time->hour & 0x0F) + 0x30) ;
str[11] = ':';
str[12] = (int8_t)(((rtc_time->minute >> 4) & 0x0F) + 0x30) ;
str[13] = (int8_t)((rtc_time->minute & 0x0F) + 0x30 ) ;
str[14] = ':';
str[15] = (int8_t)(((rtc_time->second >> 4) & 0x0F) + 0x30) ;
str[16] = (int8_t)((rtc_time->second & 0x0F) + 0x30) ;
if ((rtc_time->info_24_12) == 0) { /* 12h 24h mode */
str[17] = ' ';
if ( rtc_time->info_AM_PM == 1) { /* AM/PM */
/* PM */
str[18] = 'P';
}
else {
/* AM */
str[18] = 'A';
}
str[19] = 'M';
str[20] = '\r';
str[21] = '\n';
str[22] = '\0';
}
else {
str[17] = '\r';
str[18] = '\n';
str[19] = '\0';
}
return str;
}
/************************************************************************/
/* */
/* Function Name : change_str_to_rtc */
/* Input : rtc_time Struct of RTC data. */
/* str String of RTC data. */
/* Output : Struct of RTC data. */
/* */
/* Note : Change string RTC data to struct. */
/* */
/************************************************************************/
static ML675050_RtcTime *change_str_to_rtc(ML675050_RtcTime *rtc_time,
int8_t *str) {
rtc_time->year =(uint16_t)((((uint8_t)str[0] << 4) & 0xF0) | ((uint8_t)str[1] & 0x0F));
rtc_time->month = (uint16_t)((((uint8_t)str[3] << 4) & 0xF0) | ((uint8_t)str[4] & 0x0F));
rtc_time->day =(uint16_t)((((uint8_t)str[6] << 4) & 0xF0) | ((uint8_t)str[7] & 0x0F));
rtc_time->hour =(uint16_t)((((uint8_t)str[9] << 4) & 0xF0) | ((uint8_t)str[10] & 0x0F));
rtc_time->minute = (uint16_t)((((uint8_t)str[12] << 4) & 0xF0) | ((uint8_t)str[13] & 0x0F));
rtc_time->info_24_12 = (uint8_t)(((uint8_t)str[15] << 2) & 0x0F);
if(rtc_time->info_24_12==0){
rtc_time->info_AM_PM = (uint8_t)(((uint8_t)str[17] << 2) & 0x0F) ;
}
return rtc_time;
}
/************************************************************************/
/* */
/* Function Name : ML675050smpAP_Rtc */
/* Input : void */
/* Output : int16_t OK(1) */
/* ERROR(-1) */
/* */
/* Note : Sample program of rtc. */
/* */
/************************************************************************/
int16_t ML675050smpAP_Rtc(void){
int8_t *rtc_msg[] = {
"Please send setting data in the following format\r\nYear-Month-Day-Hour-Minute-1 or 0 (24hour = 1 or 12hour with PM/AM = 0)\r\nFor example...\r\n",
"\r\nPlease push EXINT1 button on CPU board to display RTC data.\r\n",
NULL
};
int16_t rtnVal = OK;
int16_t handle_fiq,handle_irq,handle_uart,handle_rtc;
struct uPLAT_UartParam uartParam;
ML675050_RtcInitParam rtc_init_param;
ML675050_RtcTime rtc_time;
uPLAT_InterruptParam int_param;
int8_t input_rtc_time[INPUT_RTC_TIME_LEN + 1];
int8_t output_rtc_time[OUTPUT_RTC_TIME_LEN + 3];
smpDrvInitPeripheral init_peripheral;
/* Initialize peripheral. */
init_peripheral.init_xbus.rombw = BWC_ROMBW_16;
init_peripheral.init_xbus.srambw = BWC_SRAMBW_16;
init_peripheral.init_xbus.iobw = BWC_IOBW_16;
init_peripheral.init_xbus.romtype = ROMAC_ROMTYPE_0;
init_peripheral.init_xbus.rombrst = ROMAC_ROMBRST_off;
init_peripheral.init_xbus.sramtype = RAMAC_RAMTYPE_1;
init_peripheral.init_xbus.srambrst = RAMAC_RAMBRST_off;
init_peripheral.init_xbus.iotype = IOAC_IOTYPE_2;
init_peripheral.init_xbus.sdrambw = DBWC_DBDRAM16;
init_peripheral.init_xbus.standby = FALSE;
init_peripheral.cache_mode = CACHE_WRITE_BACK;
init_peripheral.init_port.port_sel1 = PORT_CR1_INIT_VALUE;
init_peripheral.init_port.port_sel2 = PORT_CR2_INIT_VALUE;
init_peripheral.init_port.port_sel3 = PORT_CR3_INIT_VALUE;
init_peripheral.init_port.port_sel4 = PORT_CR4_INIT_VALUE;
rtnVal = smpDrv_reset(&init_peripheral);
if (rtnVal != OK) {
return rtnVal;
}
/* 3 bit LED start. */
debug_msg(_3_LED_START, "RTC sample application is start.\n");
/* 7 segment LED1 : OFF */
debug_msg(_7_LED1_OFF, NULL);
int_param.primary = INT_FIQ;
int_param.callback = AP_FIQ_handler;
handle_fiq = smpDrv_open(DRVNO_INTFIQ, (uint32_t)(&int_param));
int_param.primary = INT_EXINT1;
int_param.callback = AP_EXINT1_handler;
int_param.level = EXILCB_ILC34 & INT_LV1;
handle_irq = smpDrv_open(DRVNO_INTEXINT, (uint32_t)(&int_param));
/* Initailize UART. */
uartParam.baudrate = 115200;
uartParam.parity = 0;
uartParam.data_len = 8;
uartParam.stop_bit = 1;
uartParam.flow_ctrl = 0;
uartParam.fifo_mode = 1;
uartParam.dma_mode = 0;
uartParam.fifo_byte_num = 4;
handle_uart = smpDrv_open(DRVNO_UART,(uint32_t)(&uartParam));
/* Initailize RTC. */
rtc_init_param.info_24_12 = 0;
handle_rtc = smpDrv_open(DRVNO_RTC, (uint32_t)(&rtc_init_param));
/* send rtc_msg[0] to pc by uart */
rtnVal = smpDrv_write(DRVNO_UART, rtc_msg[0], (uint16_t)strlen(rtc_msg[0]), handle_uart);
if (rtnVal == OK) {
/* receive setting data from pc by uart */
rtnVal = smpDrv_read(DRVNO_UART, input_rtc_time, INPUT_RTC_TIME_LEN, handle_uart);
if (rtnVal == OK) {
/* write data(received from pc) to rtc */
rtnVal = smpDrv_ioctl(DRVNO_RTC,
ML675050_HAL_RTC_WRITE_DATE,
(uint32_t)(change_str_to_rtc(&rtc_time, input_rtc_time)),
handle_rtc);
if (rtnVal == OK) {
/* read rtc data */
rtnVal = smpDrv_ioctl(DRVNO_RTC,
ML675050_HAL_RTC_READ_DATE,
(uint32_t)(&rtc_time),
handle_rtc);
if (rtnVal == OK) {
/* send rtc data to pc by uart */
rtnVal = smpDrv_write(DRVNO_UART,
change_rtc_to_str(output_rtc_time, &rtc_time),
OUTPUT_RTC_TIME_LEN + 2,
handle_uart);
if (rtnVal == OK) {
/* send rtc_msg[1] to pc by uart */
rtnVal = smpDrv_write(DRVNO_UART, rtc_msg[1],(uint16_t)strlen(rtc_msg[1]), handle_uart);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -