提交SMOD ATSC IMX APP版本 V122

This commit is contained in:
2026-07-16 14:35:59 +08:00
commit 7601c270d5
485 changed files with 255032 additions and 0 deletions
+522
View File
@@ -0,0 +1,522 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h> //define O_WRONLY and O_RDONLY
#include <pthread.h> /* mux- */
#include <unistd.h> /* -System V */
#include <sys/ioctl.h> /* ioctl - BSD and Linux */
#include <stropts.h> /* -XSI STREAMS */
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <memory.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>
#include <errno.h>
#include <math.h>
#include <string.h>
#include "../inc/ispi.h"
#include "../inc/dac38j82.h"
#include "../inc/isys_config.h"
#include "../inc/lcd_task.h"
#include "../inc/isys_com.h"
#if 0//USE_38J82
int init_38j82_flg = 0;
int dac38j82_spi_fd = -1;
extern S_ATSC_PARA atsc_para;
static void spi_dac38j82_init(void);
static s8 spi_write_dac38j82(u8 addr, u16 data);
static s8 spi_read_dac38j82(u8 addr, u16 *pdata);
static void dac38j82_set_freq(uint32 u32freqMHz);
static void cw_cfg(int cw);
static void spi_dac38j82_init(void)
{
unsigned char mode = SPI_MODE_3 ; // 1 0 -- 1 闲时高电平, 0第一个边沿采样
unsigned char bits = 8;
unsigned int speed = 100000;
uint16 u16data = 0;
init_spi_dev(DAC38J82_SPI_NODE, &dac38j82_spi_fd, mode, bits, speed);
/* set SPI mode at 4-wire */
spi_read_dac38j82(REGMAP_ADDR_CONFIG2, &u16data);
u16data |= 0x80;
spi_write_dac38j82(REGMAP_ADDR_CONFIG2, u16data); // 0x2092
spi_read_dac38j82(REGMAP_ADDR_CONFIG2, &u16data);
if(u16data & 0x80)
{
log_printf(LOG_INFO, I_SYS_STATUS, "spi mode set 4-wire done.\n");
}
}
static s8 spi_write_dac38j82(u8 addr, u16 data)
{
int ret;
if (addr > 0xff) {
log_printf(LOG_ERR,I_SYS_STATUS,"spi_write_dac38j82 addr error!\r\n");
return -1;
}
ret = spi_w(dac38j82_spi_fd, addr, data);
if(ret == 0) {
return 0;
} else {
if(ret == -1)
log_printf(LOG_ERR,I_SYS_STATUS,"dac38j82_spi_fd err, < 0 \n");
else if(ret == -2)
log_printf(LOG_ERR,I_SYS_STATUS,"can't send spi message, for write dac38j82_spi_fd\n");
return -1;
}
}
static s8 spi_read_dac38j82(u8 addr, u16 *pdata)
{
int ret;
if (addr > 0xff) {
log_printf(LOG_ERR,I_SYS_STATUS,"spi_read_dac38j82 addr error!\n");
return RET_FAIL;
}
ret = spi_r(dac38j82_spi_fd, addr, pdata);
if(ret == 0) {
return 0;
} else {
if(ret == -1)
log_printf(LOG_ERR,I_SYS_STATUS,"dac38j82_spi_fd err, < 0 \n");
else if(ret == -2)
log_printf(LOG_ERR,I_SYS_STATUS,"can't send spi message, for read dac38j82_spi_fd\n");
return -1;
}
}
/*****************************************************************************
* Function : gpio_dac38j82_reset
* Description : reset imx6ull's pin DA33_RESET set low -> high now only high
* Input : void
* Output : None
* Return :
* Others :
* Record
* 1.Date : 20220302
* Author : Shu.Wang
* Modification: Created function
*****************************************************************************/
static void gpio_dac38j82_reset(void)
{
// reset imx6ull's pin DA33_RESET set low -> high now only high
log_printf(LOG_DEBUG,I_NOUSED,"RESETB : DA33_RESET 0--->1\n");
system("echo out > /sys/class/gpio/gpio116/direction");
system("echo in > /sys/class/gpio/gpio116/direction");
}
/* OneShu@20220124
config20、 config21、config22
data = int(freqMHz*2^48/1920)
*/
static void dac38j82_set_freq(uint32 u32freqMHz)
{
uint16 u16data = 0;
//u32freqMHz = u32freqMHz -15;
uint64 u64data =(uint64) (((uint64)u32freqMHz * (uint64)(2)<<(48-1)) / (uint64)1920);
log_printf(LOG_DEBUG,I_NOUSED,"do set freq %d MHz ~ (%#llx).\n", u32freqMHz, u64data);
u16data = (u64data>>0) & 0xFFFF;
spi_write_dac38j82(REGMAP_ADDR_CONFIG20, u16data);
u16data = (u64data>>16) & 0xFFFF;
spi_write_dac38j82(REGMAP_ADDR_CONFIG21, u16data);
u16data = (u64data>>32) & 0xFFFF;
spi_write_dac38j82(REGMAP_ADDR_CONFIG22, u16data);
log_printf(LOG_DEBUG,I_NOUSED,"spi wr freq %d MHz to 38j82 success.\n", u32freqMHz);
return;
}
// off 0x0400
// on 0x077D
static void cw_cfg_config_12_13(int cw)
{
// gaina 0x400 0x200 0x400 0x8400 0x400
if(cw == 0) {
spi_write_dac38j82(REGMAP_ADDR_CONFIG12, 0x0400); // 0x0400 0x07FF
spi_write_dac38j82(REGMAP_ADDR_CONFIG13, 0x0400); // 0x0400 0x07FF
}else {
spi_write_dac38j82(REGMAP_ADDR_CONFIG12, 0x077D); // 0x0400 0x07FF
spi_write_dac38j82(REGMAP_ADDR_CONFIG13, 0x077D); // 0x0400 0x07FF
}
}
// (自身数值减小的那个配置,必须先配置进38j82,使实际功率先降下来,再配置后面数值增加的那个cfg来提升功率)
static void cw_cfg(int cw)
{
log_printf(LOG_DEBUG,I_NOUSED,"cfg 38j82 cw = %d\n", cw);
if(cw == 0){ // cw 变 1
spi_write_dac38j82(REGMAP_ADDR_CONFIG13, 0x0400); // ↓
spi_write_dac38j82(REGMAP_ADDR_CONFIG12, 0x0400); // ↓
spi_write_dac38j82(REGMAP_ADDR_CONFIG2, 0x20D2); // ↓
spi_write_dac38j82(REGMAP_ADDR_CONFIG3, 0xC081); // ↑ (bit0:exenable)
}else if(cw == 1) { // cw 变 0
spi_write_dac38j82(REGMAP_ADDR_CONFIG3, 0x2081); // ↓ (bit0:exenable)
spi_write_dac38j82(REGMAP_ADDR_CONFIG12, 0x077D); // ↑
spi_write_dac38j82(REGMAP_ADDR_CONFIG13, 0x077D); // ↑
spi_write_dac38j82(REGMAP_ADDR_CONFIG2, 0x20F2); // ↑
}
}
static void *monitor_config100_thr(void*args)
{
uint16 u16data = 0;
uint32 u32data = 0;
int i = 0;
while(1){
for(i=0; i<9; i++){
spi_write_dac38j82(REGMAP_ADDR_CONFIG100+i, 0x0000);
usleep(2);
spi_read_dac38j82(REGMAP_ADDR_CONFIG100+i, &u16data);
if(u16data && i==0) {
log_printf(LOG_DEBUG,I_NOUSED,"monitor alarm [config%d] 0x%x\n", REGMAP_ADDR_CONFIG100+i, u16data);
}
}
// fpga_i2c_sim_spi_read(0x08, &u32data);
// printf("read jesd204b 0x08 = 0x%x\n", u32data);
// fpga_i2c_sim_spi_read(0x0c, &u32data);
// printf("read jesd204b 0x0c = 0x%x\n", u32data);
// fpga_i2c_sim_spi_read(0x10, &u32data);
// printf("read jesd204b 0x10 = 0x%x\n", u32data);
// fpga_i2c_sim_spi_read(0x1C, &u32data);
// printf("read jesd204b 0x1C = 0x%x\n", u32data);
// fpga_i2c_sim_spi_read(0x20, &u32data);
// printf("read jesd204b 0x20 = 0x%x\n", u32data);
// fpga_i2c_sim_spi_read(0x24, &u32data);
// printf("read jesd204b 0x24 = 0x%x\n", u32data);
// fpga_i2c_sim_spi_read(0x28, &u32data);
// printf("read jesd204b 0x28 = 0x%x\n", u32data);
// fpga_i2c_sim_spi_read(0x2C, &u32data);
// printf("read jesd204b 0x2C = 0x%x\n", u32data);
// fpga_i2c_sim_spi_read(0x38, &u32data);
// printf("read jesd204b 0x38 = 0x%x\n", u32data);
usleep(5000000);
}
}
static void dac38j82_setup(DAC38J82_CFG_STEP step, uint32 u32freqMHz, int cw)
{
u16 u16data = 0;
/* 1. Set TXENABLE Low . @ FPGA 0xB8*/ /* Has do it at jesd cfg function , done */
/* 2. supply all 0.9v */
/* 3. RESET JTAG by set TRSTB Low. @regmap-config*/
/* 4. Start DACCLK, 2871 give */
if(step == DAC38J82_CFG_STEP_ONE) {
/* 5. RESETB Low to reset SIF register */
/* bit0[mem_sif_reset] : 1
bit4[NCO_ena] : 1 ,
bit6[ mixer_ena] : 1 ,
bit7[sif4_ena] : 1
bit13[zero_invalid_data] : 1
*/
spi_write_dac38j82(REGMAP_ADDR_CONFIG2, (cw==0) ? 0x20D3:0x20F3);
log_printf(LOG_DEBUG,I_NOUSED,"only [step 0] set config2, nco en, mixer en, sif4 en, zero invalid data, sif rst 1\n");
spi_read_dac38j82(REGMAP_ADDR_CONFIG127, &u16data);
log_printf(LOG_DEBUG,I_NOUSED,"one read config127=0x%x. Vendor ID is %s 1 .\n",u16data, (0x08&u16data)?" ":"not");
}
if(cw == 0)
cw_cfg_config_12_13(0);
/* 6. Program the DAC PLL settings (config26, config49, config50, config51).
If the PLL is not used, set pll_sleep and pll_reset to 1 and pll_ena to 0.
pll_sleep : 1 */
spi_write_dac38j82(REGMAP_ADDR_CONFIG26, 0x0023); // 0x0020 0x00A7 0x0020 0x0127
spi_write_dac38j82(REGMAP_ADDR_CONFIG27, 0x0000); // 0x8000 0x0000
spi_write_dac38j82(REGMAP_ADDR_CONFIG47, 0x0000);
// pll_n 1/5-> 0 , pll_n 2/5-> 0, pll_ena -> 0 ,pll_reset -> 1
spi_write_dac38j82(REGMAP_ADDR_CONFIG49, 0x1018);
spi_write_dac38j82(REGMAP_ADDR_CONFIG50, 0x0400);
spi_write_dac38j82(REGMAP_ADDR_CONFIG51, 0x8000);
spi_write_dac38j82(REGMAP_ADDR_CONFIG7, 0x0000);
/* 7.Program the SERDES settings (config61, config62) including the serdes_clk_sel and serdes_refclk_div. */
// cfg 59 - serdes_refclk_div --* 0x1800--->0x0800-->0x0000 0x1800 0x0800 0x1800
spi_write_dac38j82(REGMAP_ADDR_CONFIG59, 0x1800);
//0x0028 - 0x0050 0x0028
spi_write_dac38j82(REGMAP_ADDR_CONFIG60, 0x0028);
spi_write_dac38j82(REGMAP_ADDR_CONFIG61, 0x0088);
// 0x108 -> 0x128 -0x0108 0x128 0x8128 0x128
spi_write_dac38j82(REGMAP_ADDR_CONFIG62, 0x0128);
/* 8. Program the SERDES lane settings (config63, config71, config73, config74, config96). */
spi_write_dac38j82(REGMAP_ADDR_CONFIG63, 0x0000);
spi_write_dac38j82(REGMAP_ADDR_CONFIG70, 0x0044);
spi_write_dac38j82(REGMAP_ADDR_CONFIG71, 0x190A);
spi_write_dac38j82(REGMAP_ADDR_CONFIG72, 0x31C5); // subclass2 : 0x31C5 -0x31C1 0x31C5
spi_write_dac38j82(REGMAP_ADDR_CONFIG73, 0x0000);
spi_write_dac38j82(REGMAP_ADDR_CONFIG96, 0x4567);
/* 9.Program clkjesd_div, cdrvser_sysref_mode, and interp */
// cfg37 log: 0x8000->0x6000 -0x0000!!!-->0x6000-0x0000-0x8000-0x6000 0x8000 0x6000 0x8000 0x6000
spi_write_dac38j82(REGMAP_ADDR_CONFIG37, 0x6000);
spi_write_dac38j82(REGMAP_ADDR_CONFIG36, 0x0000);
// 0x818--0x418--0x818 0x218 0x0018-0x818
spi_write_dac38j82(REGMAP_ADDR_CONFIG0, 0x281A);// 0x0818 0x2818 281A
/* 10. Program the JESD settings (config3, config74-77, config79, config80-85, config92, config97). */
if(step == DAC38J82_CFG_STEP_ONE){
spi_write_dac38j82(REGMAP_ADDR_CONFIG3, (cw==0)?0xC080:0x2080 ); //0xF080 -- > 0xF081 0xC081 0xD081 0xC081 0xE081 0xc081 0xc080
log_printf(LOG_DEBUG,I_NOUSED,"step 0 set config3 0xC080, sif_txenable 0\n");
}else{
spi_write_dac38j82(REGMAP_ADDR_CONFIG3, (cw==0)?0xC081:0x2081); // 0xC081 0xF081 0xC081
log_printf(LOG_DEBUG,I_NOUSED,"step 1 set config3 0xC081, sif_txenable 1\n");
}
// dual 1
spi_write_dac38j82(REGMAP_ADDR_CONFIG74, 0x013E);
spi_write_dac38j82(REGMAP_ADDR_CONFIG75, 0x0003);
spi_write_dac38j82(REGMAP_ADDR_CONFIG76, 0x1F00);
spi_write_dac38j82(REGMAP_ADDR_CONFIG77, 0x0100);
// 0x0F0F - 0x0F2F
spi_write_dac38j82(REGMAP_ADDR_CONFIG78, 0x0F2F);
//0x1cc1 0x1c41 -0x1c01 0x1c41 0x1cc1
spi_write_dac38j82(REGMAP_ADDR_CONFIG79, 0x1CC1);
spi_write_dac38j82(REGMAP_ADDR_CONFIG80, 0x0000);
spi_write_dac38j82(REGMAP_ADDR_CONFIG81, 0x00ff);
spi_write_dac38j82(REGMAP_ADDR_CONFIG82, 0x00ff);
spi_write_dac38j82(REGMAP_ADDR_CONFIG83, 0x0000);
spi_write_dac38j82(REGMAP_ADDR_CONFIG84, 0x00ff);
spi_write_dac38j82(REGMAP_ADDR_CONFIG85, 0x00ff);
spi_write_dac38j82(REGMAP_ADDR_CONFIG92, 0x1100);
spi_write_dac38j82(REGMAP_ADDR_CONFIG94, 0x0000);
spi_write_dac38j82(REGMAP_ADDR_CONFIG95, 0x0123);
spi_write_dac38j82(REGMAP_ADDR_CONFIG97, 0x0013);
/* 11. Program the DIG block settings
(NCO, PA protection <page 74config 32>, QMC, fractional delay, etc.)
and set the preferred SYNC modes for the digital blocks (config30-32). */
spi_write_dac38j82(REGMAP_ADDR_CONFIG2, (cw==0) ? 0x20D2 : 0x20F2); // NCO_ena mixer enable 0x20D2-0x20F2 0x2092 0x20D2
log_printf(LOG_DEBUG,I_NOUSED,"set config2, nco en, mixer en, sif4 en, zero invalid data; \n");
if(step == DAC38J82_CFG_STEP_TWO && cw == 1)
cw_cfg_config_12_13(1);
spi_write_dac38j82(REGMAP_ADDR_CONFIG16, 0x0000);
spi_write_dac38j82(REGMAP_ADDR_CONFIG18, 0x0000);
spi_write_dac38j82(REGMAP_ADDR_CONFIG19, 0x0000);
dac38j82_set_freq(u32freqMHz);
spi_write_dac38j82(REGMAP_ADDR_CONFIG30, 0x1111);
spi_write_dac38j82(REGMAP_ADDR_CONFIG31, 0x1142); // sif sync --> 1
spi_write_dac38j82(REGMAP_ADDR_CONFIG31, 0x1140); // sif sync --> 0
spi_write_dac38j82(REGMAP_ADDR_CONFIG32, 0x0000);
spi_write_dac38j82(REGMAP_ADDR_CONFIG34, 0x1B00); // 0x1B1B 0x0b0b 0x1B1B
spi_write_dac38j82(REGMAP_ADDR_CONFIG38, 0x3001); // mem_dither_zero[15:15] 0x3001 0x3000 0x0000 0x3001
spi_write_dac38j82(REGMAP_ADDR_CONFIG1, 0x0000); // 0x0080
/* 12 . Verify the SERDES PLL lock status by checking the SERDES PLL alarms:
alarm_rw0_pll (alarm for lanes 0 through 3),
alarm_rw1_pll (alarm for lanes 4 through 7).*/
spi_read_dac38j82(REGMAP_ADDR_CONFIG108, &u16data);
#if 0
if(u16data & 0x02)
log_printf(LOG_DEBUG,I_NOUSED,"alarm_rw1_pll alarm!\n");
if(u16data & 0x04)
log_printf(LOG_DEBUG,I_NOUSED,"alarm_rw0_pll alarm!\n");
#endif
/* 13. Set init_state to "0000" and jesd_reset_n to "1" to start the JESD204B link initialization.*/
spi_write_dac38j82(REGMAP_ADDR_CONFIG74, 0x0101); // state clear
/* 14 .Start the SYSREF generation. */
/* 15. Enable transmission of data by asserting the TXENABLE pin or setting sif_txenable to “1”. */
if(step == DAC38J82_CFG_STEP_ONE)
spi_write_dac38j82(REGMAP_ADDR_CONFIG3, (cw==0) ? 0xC081 : 0x2081);
/* 16. Clear the alarms, then wait approximately 1-2µs and check values. */
/*
for( i = 0; i < 9; i++){
spi_write_dac38j82(REGMAP_ADDR_CONFIG100+i, 0x0000);
usleep(2);
}
*/
/* 17. Verify that DAC output is the desired output */
jesd_reset_by_fpga();
}
/*****************************************************************************
* Function : cfg_dac38j82
* Description :
init setup
1. Set TXENABLE low.
2. Supply all 0.9-V supplies (VDDDIG09, VDDT09, VDDDAC09, VDDCLK09), all 1.8-V supplies (VDDR18,
VDDS18, VQPS18, VDDIO18, VDDAPLL18, VDDAREF18), and all 3.3-V supplies (VDDADAC33). The
supplies can be powered up simultaneously or in any order. There are no specific requirements on the ramp
rate for the supplies.
3. RESET the JTAG port by either toggling TRSTB low if using the JTAG port or holding TRSTB low if not using JTAG.
4. Start the DACCLK generation.
5. Toggle RESETB low to reset the SIF registers.
6. Program the DAC PLL settings (config26, config49, config50, config51). If the PLL is not used, set pll_sleep
and pll_reset to “1” and pll_ena to “0”.
7. Program the SERDES settings (config61, config62) including the serdes_clk_sel and serdes_refclk_div.
8. Program the SERDES lane settings (config63, config71, config73, config74, config96).
9. Program clkjesd_div, cdrvser_sysref_mode, and interp.
10. Program the JESD settings (config3, config74-77, config79, config80-85, config92, config97).
11. Program the DIG block settings (NCO, PA protection, QMC, fractional delay, etc.) and set the preferred
SYNC modes for the digital blocks (config30-32).
12. Verify the SERDES PLL lock status by checking the SERDES PLL alarms: alarm_rw0_pll (alarm for lanes 0
through 3) and alarm_rw1_pll (alarm for lanes 4 through 7).
13. Set init_state to “0000” and jesd_reset_n to “1” to start the JESD204B link initialization.
14. Start the SYSREF generation.
15. Enable transmission of data by asserting the TXENABLE pin or setting sif_txenable to “1”.
16. Clear the alarms, then wait approximately 1-2µs and check values.
17. Verify that DAC output is the desired output
* Input : void
* Output : None
* Return :
* Others :
* Record
* 1.Date : 20220117
* Author : Shu.Wang
* Modification: Created function
SPI
*****************************************************************************/
int cfg_dac38j82(uint8 txfreq, int cw)
{
log_printf(LOG_DEBUG,I_NOUSED,"\n\n============38j82 start==================\n");
u16 u16data = 0;
int i = 0;
uint32 u32FreqMHz = 0;
u32FreqMHz = txfreq_to_hz(txfreq);
log_printf(LOG_DEBUG,I_NOUSED,"u32FreqMHz=%dMHz\n", u32FreqMHz);
if(init_38j82_flg == 0){
spi_dac38j82_init();
init_38j82_flg = 1;
}
cfg_jesd204b();
dac38j82_setup(DAC38J82_CFG_STEP_ONE, u32FreqMHz, cw);
/* Do cfg DAC38j82 option once again. */
log_printf(LOG_DEBUG,I_NOUSED,"\n\n [%s,%d] do jesd and 38j82 cfg again.\n\n",__FUNCTION__,__LINE__);
cfg_jesd204b();
spi_write_dac38j82(REGMAP_ADDR_CONFIG3, (cw==0)? 0xC081 : 0x2081); // bit0:mem_sif_txenable 1
spi_read_dac38j82(REGMAP_ADDR_CONFIG127, &u16data);
log_printf(LOG_DEBUG,I_NOUSED,"222[%s,%d] read config127 = 0x%x. Vendor ID is %s 1 .\n",__FUNCTION__,__LINE__,u16data, (0x08&u16data)?" ":"not");
dac38j82_setup(DAC38J82_CFG_STEP_TWO, u32FreqMHz, cw);
for( i = 0; i < 9; i++) {
spi_write_dac38j82(REGMAP_ADDR_CONFIG100+i, 0x0000);
usleep(2);
spi_read_dac38j82(REGMAP_ADDR_CONFIG100+i, &u16data);
if(u16data && i==0) {
printf("alarm [config%d] 0x%x\n", REGMAP_ADDR_CONFIG100+i, u16data);
}
}
log_printf(LOG_DEBUG,I_NOUSED,"\n============38j82 end==================\n\n");
return 0;
}
void chang_freq(int freq_changed)
{
if(freq_changed > 1 || freq_changed <= 0)
return;
if(freq_changed){
turn_off_rf_agc_state(E_CHG_TX_FREQ);
cfg_dac38j82(atsc_para.rf_para.txfreq, atsc_para.ad3_para.cw);
}
return;
}
void chang_cw(int cw_changed)
{
if(cw_changed > 1 || cw_changed <= 0)
return ;
if(cw_changed) {
turn_off_rf_agc_state(E_CHG_CW);
if(atsc_para.ad3_para.cw == 0)
cw_cfg(0);
cfg_dac38j82(atsc_para.rf_para.txfreq, atsc_para.ad3_para.cw);
if(atsc_para.ad3_para.cw == 1)
cw_cfg(1);
}
}
#endif //USE_38J82
+109
View File
@@ -0,0 +1,109 @@
/*************************************************************************
系统延时相关函数
***************************************************************************/
/*
#include "stm32f4xx.h"
#include "type.h"
#include "os_task.h"
#include "FreeRTOS.h"
*/
#include "../inc/type.h"
#include "../inc/delay.h"
#include "../inc/isys_com.h"
#include <unistd.h>
extern volatile uint32 sys_ready;
//extern SYS_TASK sys_task;
void delay_ms(uint32 nms)
{
usleep(nms*1000);
}
void delay_us(uint32 nus)
{
uint32 index = 0;
//系统钟168M, 循环一次4条指令, 42*4 = 168 约为1us
//系统钟396M, 循环一次4条指令, 99*4 = 396 约为1us
//for(index = (42 * nus); index != 0; index--)
for(index = (99 * nus); index != 0; index--)
{
;
}
//usleep(nus);
}
void idelay_us(uint32 nus)
{
usleep(nus);
}
/*
void delay_us(uint32 nus)
{
uint32 index = 0;
//系统钟168M, 循环一次4条指令, 42*4 = 168 约为1us
for(index = (42 * nus); index != 0; index--)
{
;
}
}*/
uint32 check_delay_over(uint32 *ptick,uint32 cycle)
{
unsigned long tick_bak = get_sys_tick();
//printf("cur_tick =%d\n",tick_bak);
if((*ptick)+cycle>tick_bak)//延时还没到。
return 0;
else
{
*ptick=get_sys_tick();
return 1;
}
}
void delay_ticks(uint32 ticks)
{
if(!ticks)
return;
/*if(sys_ready)
{
vTaskDelay(ticks);
}
else
{
uint32 index = 0;
for(index = (41000 * OS_PER_TICK_MS*ticks); index != 0; index--)
{
;
}
}*/
delay_ms(ticks*10);
}
void task_delay(uint32 nms)
{
uint32 task_id;
/*if(!sys_ready)
{
delay_ms(nms);
}
get_current_task_id(&task_id);
if(task_id>=MAX_TASK)
return;
sys_task.pause[task_id] = (nms+999)/1000; //转换为秒
vTaskDelay((nms +(OS_PER_TICK_MS-1)) / OS_PER_TICK_MS); //转化为tick值 处理结果,向上对齐 最小时间为10ms
sys_task.pause[task_id] = 0;
*/
usleep(nms*1000);
}
+3223
View File
File diff suppressed because it is too large Load Diff
+3993
View File
File diff suppressed because it is too large Load Diff
+407
View File
@@ -0,0 +1,407 @@
#include <string.h>
#include "type.h"
//#include "stm32f4xx_spi.h"
//#include "stm32f4xx_gpio.h"
//#include "stm32f4xx_hal.h"
#include "stm32f4xx_spi.h"
#include "stm32f4xx_gpio.h"
#include "config.h"
#include "os_task.h"
#include "delay.h"
#include "flash.h"
#include "sys_config.h"
#if USE_SPI_FLASH
#define SST25_SECTOR_PER_SIZE (4096) //一个扇区为4kB
#define SST25_TOTAL_SIZE (4*1024*1024*2/8) //总大小为4Mb 512*2KB 128个扇区
#define SST25_TOTAL_SECTOR_NUM (SST25_TOTAL_SIZE/SST25_SECTOR_PER_SIZE)
#define FLASH_SPI SPI1
#define SST25_SELECT() do{GPIO_ResetBits(GPIOA,GPIO_Pin_4);}while(0)
#define SST25_DESELECT() do{GPIO_SetBits(GPIOA,GPIO_Pin_4);}while(0)
#define SST25_READDATA 0x03
#define SST25_FASTREADDATA 0x0B
#define SST25_4KBYTE_BLOCKERASE 0x20
#define SST25_32KBYTE_BLOCKERASE 0x52
#define SST25_64KBYTE_BLOCKERASE 0xD8
#define SST25_CHIPERASE 0xC7
#define SST25_BYTEPORGRAM 0x02
#define SST25_AAI_WORDPROGRAM 0xAD
#define SST25_READSTATUSREG 0x05
#define SST25_ENABLEWRITESTATUSREG 0x50
#define SST25_WRITESTATUSREG 0x01
#define SST25_WRITEENABLE 0x06
#define SST25_WRITEDISABLE 0x04
#define SST25_DEVICEID 0x90
#define SST25_JEDECDEVICEID 0x9F
#define SST25_EBSY 0x70
#define SST25_DBSY 0x80
//extern SPI_HandleTypeDef hspi1;
static uint8 spi1_read_byte(void)
{
while (SPI_I2S_GetFlagStatus(FLASH_SPI, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(FLASH_SPI,0xff);
while (SPI_I2S_GetFlagStatus(FLASH_SPI, SPI_I2S_FLAG_RXNE) == RESET);
return (SPI_I2S_ReceiveData(FLASH_SPI));
}
static void spi1_write_byte(uint8 byte)
{
while (SPI_I2S_GetFlagStatus(FLASH_SPI, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(FLASH_SPI, byte);
while (SPI_I2S_GetFlagStatus(FLASH_SPI, SPI_I2S_FLAG_RXNE) == RESET);
(void)SPI_I2S_ReceiveData(FLASH_SPI);
}
/******************************************
写一个命令
******************************************/
void sst25_write_cmd(uint8 cmd)
{
SST25_SELECT();
spi1_write_byte(cmd);
SST25_DESELECT();
}
/******************************************
写一个命令一个数据
******************************************/
void sst25_write_cmd_data(uint8 cmd,uint8 data)
{
SST25_SELECT();
spi1_write_byte(cmd);
spi1_write_byte(data);
SST25_DESELECT();
}
/******************************************
写一个命令三个数据
******************************************/
void sst25_write_cmd_data3(uint8 cmd, uint8* data)
{
SST25_SELECT();
spi1_write_byte(cmd);
spi1_write_byte(*data++);
spi1_write_byte(*data++);
spi1_write_byte(*data);
SST25_DESELECT();
}
/******************************************
写一个命令返回一个数据
******************************************/
uint8 sst25_read_data(uint8 cmd)
{
uint8 temp;
SST25_SELECT();
spi1_write_byte(cmd);
temp = spi1_read_byte();
SST25_DESELECT();
return temp;
}
/******************************************
写一个命令,三个地址 返回多个数据
******************************************/
void sst25_read_datas(uint8 cmd,uint8 *addr,uint8 *data,uint32 no)
{
uint16 i;
SST25_SELECT();
spi1_write_byte(cmd);
spi1_write_byte(*addr++);
spi1_write_byte(*addr++);
spi1_write_byte(*addr);
for(i=0;i<no;i++)
{
data[i] = spi1_read_byte();
}
SST25_DESELECT();
}
/******************************************
SST25读取状态寄存器
******************************************/
uint8 sst25_readsr(void)
{
uint8 sr;
sr = sst25_read_data(SST25_READSTATUSREG);
return sr;
}
/******************************************
SST25写状态寄存器
******************************************/
void sst25_writesr(uint8 cmd)
{
sst25_write_cmd(SST25_ENABLEWRITESTATUSREG);
sst25_write_cmd_data(SST25_WRITESTATUSREG,cmd);
}
/******************************************
SST25写使能
******************************************/
void sst25_wren(void)
{
sst25_write_cmd(SST25_WRITEENABLE);
}
/******************************************
SST25写失能
******************************************/
void sst25_wrdi(void)
{
sst25_write_cmd(SST25_WRITEDISABLE);
}
/******************************************
SST25忙检测
1 忙
******************************************/
void sst25_wait_busy(void)
{
while(sst25_readsr()&0x01);
}
/******************************************
SST25允许软件写保护
******************************************/
void sst25_wpen(void)
{
uint8 sta;
sta = sst25_read_data(SST25_READSTATUSREG)|0x1c;
sst25_write_cmd(SST25_ENABLEWRITESTATUSREG);
sst25_write_cmd_data(0x01,sta);
}
/******************************************
SST25先取消保护位,再允许写
******************************************/
void sst25_writeen(void)
{
uint8 sta;
sta = sst25_read_data(SST25_READSTATUSREG)&(~0x0c);
sst25_write_cmd(SST25_ENABLEWRITESTATUSREG);
sst25_write_cmd(0xff);
sst25_write_cmd_data(SST25_WRITESTATUSREG,sta);
sst25_write_cmd(SST25_WRITEENABLE);
}
/******************************************
SST25 读芯片ID
******************************************/
uint16 sst25_readid(void)
{
uint8 id[3]= {0,};
uint8 addr[3] = {0,0,0};
sst25_read_datas(SST25_DEVICEID,addr,id,2);
return ((id[0]<<8)+id[1]);
}
/******************************************
SST25 擦除整块flash
******************************************/
void sst25_chip_erase(void)
{
sst25_writesr(0x02);
sst25_wren();
sst25_write_cmd(SST25_CHIPERASE);
sst25_wait_busy();
}
/******************************************
SST25 擦除扇区
******************************************/
void sst25_sector_erase(uint32 addr)
{
uint8 ad[3]={0,};
ad[0] = ((addr>>16)&0xff);
ad[1] = ((addr>>8)&0xff);
ad[2] = (addr&0xff);
sst25_writesr(0x02);
sst25_wren();
SST25_SELECT();
spi1_write_byte(SST25_4KBYTE_BLOCKERASE);
spi1_write_byte(ad[0]);
spi1_write_byte(ad[1]);
spi1_write_byte(ad[2]);
SST25_DESELECT();
sst25_wait_busy();
}
/******************************************
SST25 写入一个字节
******************************************/
void sst25_byte_write(uint32 addr,uint8 byte)
{
sst25_writesr(0x02);
sst25_wren();
SST25_SELECT();
spi1_write_byte(SST25_BYTEPORGRAM);
spi1_write_byte((addr>>16)&0xff);
spi1_write_byte((addr>>8)&0xff);
spi1_write_byte(addr&0xff);
spi1_write_byte(byte);
SST25_DESELECT();
sst25_wait_busy();
}
/******************************************
SST25 写入多个字节
******************************************/
void sst25_datas_write(uint32 addr,uint8 *buf,uint32 no)
{
uint32 cnt;
if(no == 0)
return;
sst25_writesr(0x02);
sst25_wren();
if(no == 1)
{
sst25_byte_write(addr,*buf);
}
else
{
cnt = no;
SST25_SELECT();
spi1_write_byte(SST25_AAI_WORDPROGRAM);
spi1_write_byte((addr>>16)&0xff);
spi1_write_byte((addr>>8)&0xff);
spi1_write_byte(addr&0xff);
spi1_write_byte(*buf++);
spi1_write_byte(*buf++);
SST25_DESELECT();
cnt-=2;
sst25_wait_busy();
for(;cnt>1;cnt-=2)
{
SST25_SELECT();
spi1_write_byte(SST25_AAI_WORDPROGRAM);
spi1_write_byte(*buf++);
spi1_write_byte(*buf++);
SST25_DESELECT();
sst25_wait_busy();
}
sst25_wrdi();
if(cnt == 1)
{
sst25_wren();
sst25_byte_write(addr+no-1,*buf);
}
}
sst25_wpen();
}
/******************************************
SST25 高速读
******************************************/
void sst25_readh(uint32 addr,uint8 *buf,uint32 no)
{
uint16 i;
SST25_SELECT();
spi1_write_byte(SST25_FASTREADDATA);
spi1_write_byte((uint8)(addr>>16)&0xff);
spi1_write_byte((uint8)(addr>>8)&0xff);
spi1_write_byte((uint8)addr&0xff);
spi1_write_byte(0xff);
for(i=0;i<no;i++)
{
buf[i] = spi1_read_byte();
}
SST25_DESELECT();
}
/******************************************
SST25 低速读
******************************************/
void sst25_readl(uint32 addr,uint8 *buf,uint32 no)
{
uint8 ad[3];
ad[0] = (addr>>16)&0xff;
ad[1] = (addr>>8)&0xff;
ad[2] = addr&0xff;
sst25_read_datas(SST25_READDATA,ad,buf,no);
}
void flash_write_byte(uint8 byte,uint32 addr)
{
uint32 secpos;
secpos = addr/SST25_SECTOR_PER_SIZE;
if(secpos>=SST25_TOTAL_SECTOR_NUM)
return;
sst25_sector_erase(secpos);
sst25_byte_write(addr,byte);
}
void flash_write_buf(uint8 *buf,uint32 num,uint32 addr)
{
uint32 secpos;
secpos = addr/SST25_SECTOR_PER_SIZE;
if(secpos >= SST25_TOTAL_SECTOR_NUM)
return;
if(addr+num>SST25_TOTAL_SIZE)
num = SST25_TOTAL_SIZE - addr;
sst25_datas_write(addr, buf, num);
}
void flash_erase_sector(uint32 start_address,uint32 num)
{
uint32 secpos,secoff,secremain;
uint16 secnum=0;
int32 i = 0;
secpos = start_address/SST25_SECTOR_PER_SIZE;
if(secpos >= SST25_TOTAL_SECTOR_NUM)
return;
if(start_address+num>SST25_TOTAL_SIZE)
num = SST25_TOTAL_SIZE - start_address;
secoff = start_address%SST25_SECTOR_PER_SIZE;
secremain = SST25_SECTOR_PER_SIZE-secoff;
if(num < secremain)
{
secnum = 1;
}
else
{
secnum = 1;
i = num -secremain;
do
{
secnum++;
i = i - SST25_SECTOR_PER_SIZE;
}while(i>0);
}
for(i=0;i<secnum;i++)
{
sst25_sector_erase((secpos+i)*SST25_SECTOR_PER_SIZE);
}
}
void flash_read_buf(uint8 *buf,uint32 len,uint32 addr)
{
if(addr >= SST25_TOTAL_SIZE)
return;
sst25_readl(addr,buf,len);
}
#endif
+1690
View File
File diff suppressed because it is too large Load Diff
+139
View File
@@ -0,0 +1,139 @@
/******************************************************************************
版权所有 (C), 2001-2011, 上海全波通信技术有限公司
******************************************************************************
文 件 名 : general_api.c
版 本 号 : 初稿
作 者 : 郭金涛
生成日期 : 2013年7月31日
最近修改 :
功能描述 : 一些通用功能函数实现
函数列表 :
修改历史 :
1.日 期 : 2013年7月31日
作 者 : 郭金涛
修改内容 : 创建文件
******************************************************************************/
#include "../inc/general_api.h"
#include <string.h>
uint8 get_check_sum(void *p,uint32 len)
{
uint8 sum = 0;
uint8 *pstr = (uint8*)p;
while(len--)
sum+=*pstr++;
return sum;
}
/***********************************************************/
//CRC-16-CCITT(x16+x12+x5+1(0x1021))
/***********************************************************/
uint16 cal_crc_zb(const uint8 *ptr, uint32 len)
{
uint16 crc = 0;
uint8 da;
static const uint16 crc_ta[256] = { // CRC 余式表 //
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
};
while(len--)
{
da=(uint8) (crc/256); // 以8 位二进制数的形式暂存CRC 的高8 位 //
crc<<=8; // 左移8 位,相当于CRC 的低8 位乘以28 //
crc^=crc_ta[da^*ptr]; // 高8 位和当前字节相加后再查表求CRC ,再加上以前的CRC //
ptr++;
}
return crc;
}
void str_trunc_space(char *pstr)
{
uint32 len = strlen(pstr);
while(len)
{ //在最后面若有空格,则置为0
if(pstr[len-1]==' ')
{
pstr[len-1]=0;
len--;
}
else
{
return;
}
}
}
uint8 bcd2byte(uint8 bcd)
{
uint8 tmp = 0;
tmp = ((uint8)(bcd & (uint8)0xF0) >> (uint8)0x4) * 10;
return (tmp + (bcd & (uint8)0x0F));
}
uint8 byte2bcd(uint8 value)
{
uint8 bcdhigh = 0;
while (value >= 10)
{
bcdhigh++;
value -= 10;
}
return ((uint8)(bcdhigh << 4) | value);
}
/*
__inline __asm uint32 asm_rev_l(uint32 data)
{
REV R0,R0;
bx lr;
}
__inline __asm uint32 asm_rev_s(uint32 data)
{
REV16 R0,R0;
bx lr;
}
__inline __asm uint32 asm_rbit_l(uint32 data)
{
RBIT R0,R0;
BX LR;
}
*/
+1821
View File
File diff suppressed because it is too large Load Diff
+382
View File
@@ -0,0 +1,382 @@
#if 1
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h> //define O_WRONLY and O_RDONLY
#include "../inc/type.h"
#include "../inc/igpio.h"
#include "../inc/isys_com.h"
extern int executeCMD(const char *cmd, char *result, u16 result_size);
pthread_mutex_t mutex_gpio;
#define DIR_OUT_VAL "out"
#define DIR_IN_VAL "in"
#define GP_VAL_H "1"
#define GP_VAL_L "0"
int set_gpio_export(uint16_t gp_num)
{
#if SHELL_CTRL_GPIO
char cmd[300];
memset(cmd, 0, sizeof(cmd));
sprintf(cmd, "echo %d > /sys/class/gpio/export",gp_num);
system(cmd);
#else
int fd;
char s_gp_num[20];
pthread_mutex_lock(&mutex_gpio);
fd = open("/sys/class/gpio/export", O_WRONLY);
if(fd == -1) {
pthread_mutex_unlock(&mutex_gpio);
log_printf(LOG_ERR, I_NOUSED, "gpio %d pin open-export error.\n", gp_num);
return -1;
}
memset(s_gp_num,0,sizeof(s_gp_num));
sprintf(s_gp_num,"%d",gp_num);
write(fd, s_gp_num ,strlen(s_gp_num));
close(fd);
pthread_mutex_unlock(&mutex_gpio);
#endif
return 0;
}
int set_gpio_unexport(uint16_t gp_num)
{
#if SHELL_CTRL_GPIO
char cmd[300];
memset(cmd, 0, sizeof(cmd));
sprintf(cmd, "echo %d > /sys/class/gpio/unexport",gp_num);
system(cmd);
#else
int fd;
char s_gp_num[20];
pthread_mutex_lock(&mutex_gpio);
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if(fd == -1){
pthread_mutex_unlock(&mutex_gpio);
log_printf(LOG_ERR,I_NOUSED,"ERR: gpio %d pin open-unexport error.\n",gp_num);
return -1;
}
log_printf(LOG_DEBUG,I_NOUSED,".setxpt sys/gpio/class/gpio/unexport");
memset(s_gp_num,0,sizeof(s_gp_num));
sprintf(s_gp_num,"%d",gp_num);
write(fd, s_gp_num ,strlen(s_gp_num));
close(fd);
pthread_mutex_unlock(&mutex_gpio);
#endif
return 0;
}
int set_gpio_io_direct(uint16_t gp_num,E_IO io_dir)
{
int fd;
char cmd[300];
memset(cmd, 0, sizeof(cmd));
#if SHELL_CTRL_GPIO
sprintf(cmd, "echo %s > /sys/class/gpio/gpio%d/direction",(io_dir==E_IO_OUT?"out":"in"),gp_num);
system(cmd);
#else
if(io_dir==E_IO_OUT) {
sprintf(cmd, "/sys/class/gpio/gpio%d/direction",gp_num);
}
else{//E_IO_IN
sprintf(cmd, "/sys/class/gpio/gpio%d/direction",gp_num);
}
pthread_mutex_lock(&mutex_gpio);
fd = open(cmd, O_WRONLY);
if(fd == -1) {
pthread_mutex_unlock(&mutex_gpio);
log_printf(LOG_ERR, I_NOUSED, "set io_dir, gpio %d pin open-direction error.\n",gp_num);
return -1;
}
if(io_dir==E_IO_OUT) {
write(fd, DIR_OUT_VAL, sizeof(DIR_OUT_VAL));
}
else{
write(fd, DIR_IN_VAL, sizeof(DIR_IN_VAL));
}
close(fd);
pthread_mutex_unlock(&mutex_gpio);
#endif
return 0;
}
int set_gpio_value(uint16_t gp_num, E_IO_VAL val)
{
int fd;
char cmd[200];
memset(cmd, 0, sizeof(cmd));
#if SHELL_CTRL_GPIO
sprintf(cmd, "echo %d > /sys/class/gpio/gpio%d/value",val,gp_num);
system(cmd);
#else
char logbuf[50]={0};
sprintf(cmd, "/sys/class/gpio/gpio%d/value",gp_num);
pthread_mutex_lock(&mutex_gpio);
fd = open(cmd, O_WRONLY);
if(fd == -1){
pthread_mutex_unlock(&mutex_gpio);
sprintf(logbuf,"error. set_io_val %d open\n",gp_num);
usr_sec_printf("%s",logbuf);
return -1;
}
if(val==E_IO_H){
write(fd, GP_VAL_H, sizeof(GP_VAL_H));
}
else{
write(fd, GP_VAL_L, sizeof(GP_VAL_L));
}
close(fd);
pthread_mutex_unlock(&mutex_gpio);
#endif
return 0;
}
int get_gpio_value(uint16_t gp_num, uint8 *pval)
{
#if SHELL_CTRL_GPIO
char cmd[300];
char vbuf[20];
int ret = 0;
sprintf(cmd, "cat /sys/class/gpio/gpio%d/value",gp_num);
ret = executeCMD(cmd, vbuf, sizeof(cmd));
if(ret>=0){
*pval = atoi(vbuf);
}else{
log_printf(LOG_ERR,I_NOUSED,"%s get value err\n", cmd);
}
#else
int fd;
char cmd[300];
char vbuf[20];
uint8 u8val;
char logbuf[50]={0};
memset(cmd, 0, sizeof(cmd));
sprintf(cmd, "/sys/class/gpio/gpio%d/value",gp_num);
pthread_mutex_lock(&mutex_gpio);
fd = open(cmd, O_RDONLY);
if(fd == -1){
pthread_mutex_unlock(&mutex_gpio);
sprintf(logbuf,"error. get_io_val %d open\n",gp_num);
usr_sec_printf("%s",logbuf);
return -1;
}
memset(vbuf,0,sizeof(vbuf));
read(fd, vbuf, sizeof(vbuf));
if(vbuf!=NULL){
u8val = atoi(vbuf);
*pval = u8val;
}
else{
log_printf(LOG_DEBUG,I_NOUSED,"gpio read err\n");
}
close(fd);
pthread_mutex_unlock(&mutex_gpio);
#endif
return 0;
}
void key_gpio_init(void)
{
/*printf("\n\ninit key gpio unexport.\n");
set_gpio_unexport(KEY1_GP_NUM);
set_gpio_unexport(KEY2_GP_NUM);
set_gpio_unexport(KEY3_GP_NUM);
set_gpio_unexport(KEY4_GP_NUM);
set_gpio_unexport(KEY5_GP_NUM);
set_gpio_unexport(KEY6_GP_NUM);*/
log_printf(LOG_INFO, I_SYS_STATUS, "init key gpio export\n");
set_gpio_export(KEY1_GP_NUM);
set_gpio_export(KEY2_GP_NUM);
set_gpio_export(KEY3_GP_NUM);
set_gpio_export(KEY4_GP_NUM);
set_gpio_export(KEY5_GP_NUM);
set_gpio_export(KEY6_GP_NUM);
log_printf(LOG_INFO, I_SYS_STATUS, "over. init key gpio export\n");
log_printf(LOG_INFO, I_SYS_STATUS, "init key gpio input\n");
set_gpio_io_direct(KEY1_GP_NUM, E_IO_IN);//E_IO_IN
set_gpio_io_direct(KEY2_GP_NUM, E_IO_IN);
set_gpio_io_direct(KEY3_GP_NUM, E_IO_IN);
set_gpio_io_direct(KEY4_GP_NUM, E_IO_IN);
set_gpio_io_direct(KEY5_GP_NUM, E_IO_IN);
set_gpio_io_direct(KEY6_GP_NUM, E_IO_IN);
log_printf(LOG_INFO, I_SYS_STATUS, "over. init key gpio input\n");
}
#define KEY_GPIO_MAX_NUM 6
uint8 key_gp_list[KEY_GPIO_MAX_NUM]=
{
KEY1_GP_NUM ,
KEY2_GP_NUM ,
KEY3_GP_NUM ,
KEY4_GP_NUM ,
KEY5_GP_NUM ,
KEY6_GP_NUM ,
};
#define LCD_GPIO_MAX_NUM 12
uint8 lcd_gp_list[LCD_GPIO_MAX_NUM]=
{
LCD_CTL_GP_NUM ,
LCD_RS_GP_NUM ,
LCD_E_GP_NUM ,
LCD_RW_GP_NUM ,
LCD_D1_GP_NUM ,
LCD_D0_GP_NUM ,
LCD_D3_GP_NUM ,
LCD_D2_GP_NUM ,
LCD_D5_GP_NUM ,
LCD_D4_GP_NUM ,
LCD_D7_GP_NUM ,
LCD_D6_GP_NUM ,
};
void gpio_mutex_init(void)
{
pthread_mutex_init(&mutex_gpio, NULL);
}
void fpga_reset_gpio_init(void)
{
set_gpio_export(FPGA_RST_GP_NUM);
set_gpio_io_direct(FPGA_RST_GP_NUM,E_IO_OUT);
}
void lcd_gpio_init(void)
{
log_printf(LOG_INFO, I_SYS_STATUS, "init lcd gpio export\n");
set_gpio_export(LCD_CTL_GP_NUM);
set_gpio_export(LCD_RS_GP_NUM);
set_gpio_export(LCD_E_GP_NUM);
set_gpio_export(LCD_RW_GP_NUM);
set_gpio_export(LCD_D1_GP_NUM);
set_gpio_export(LCD_D0_GP_NUM);
set_gpio_export(LCD_D3_GP_NUM);
set_gpio_export(LCD_D2_GP_NUM);
set_gpio_export(LCD_D5_GP_NUM);
set_gpio_export(LCD_D4_GP_NUM);
set_gpio_export(LCD_D7_GP_NUM);
set_gpio_export(LCD_D6_GP_NUM);
log_printf(LOG_INFO, I_SYS_STATUS, "over. init lcd gpio export\n");
log_printf(LOG_INFO, I_SYS_STATUS, "init lcd gpio output\n");
set_gpio_io_direct(LCD_CTL_GP_NUM,E_IO_OUT);
set_gpio_io_direct(LCD_RS_GP_NUM, E_IO_OUT);
set_gpio_io_direct(LCD_E_GP_NUM, E_IO_OUT);
set_gpio_io_direct(LCD_RW_GP_NUM,E_IO_OUT);
set_gpio_io_direct(LCD_D1_GP_NUM,E_IO_OUT);
set_gpio_io_direct(LCD_D0_GP_NUM,E_IO_OUT);
set_gpio_io_direct(LCD_D3_GP_NUM,E_IO_OUT);
set_gpio_io_direct(LCD_D2_GP_NUM,E_IO_OUT);
set_gpio_io_direct(LCD_D5_GP_NUM,E_IO_OUT);
set_gpio_io_direct(LCD_D4_GP_NUM,E_IO_OUT);
set_gpio_io_direct(LCD_D7_GP_NUM,E_IO_OUT);
set_gpio_io_direct(LCD_D6_GP_NUM,E_IO_OUT);
log_printf(LOG_INFO, I_SYS_STATUS, "over. init lcd gpio output\n");
set_gpio_export(LCD_EN_GP_NUM);
set_gpio_io_direct(LCD_EN_GP_NUM, E_IO_OUT);
set_gpio_value(LCD_EN_GP_NUM, E_IO_H);
}
void i2c_gpio_init(void)
{
log_printf(LOG_DEBUG,I_NOUSED,"\n\n>>>:init i2c gpio export.\n");
set_gpio_export(I2C2_SDA_GP_NUM);
set_gpio_export(I2C2_SCL_GP_NUM);
set_gpio_export(I2C1_SDA_GP_NUM);
set_gpio_export(I2C1_SCL_GP_NUM);
log_printf(LOG_DEBUG,I_NOUSED,"\n\n===:over. init i2c gpio export.\n");
log_printf(LOG_DEBUG,I_NOUSED,"\n\n>>>:init i2c gpio input.\n");
set_gpio_io_direct(I2C2_SDA_GP_NUM, E_IO_OUT);//E_IO_IN
set_gpio_io_direct(I2C2_SCL_GP_NUM, E_IO_OUT);
set_gpio_io_direct(I2C1_SDA_GP_NUM, E_IO_OUT);
set_gpio_io_direct(I2C1_SCL_GP_NUM, E_IO_OUT);
log_printf(LOG_DEBUG,I_NOUSED,"\n\n===:over. init i2c gpio output.\n");
}
#if 0//test
void test_gpio_init()
{
key_gpio_init();
lcd_gpio_init();
}
void test_key_lcd_gpio(void)
{
static uint32 sec_cnt;
uint8 gp=0;
sec_cnt++;
for(gp=0;gp<KEY_GPIO_MAX_NUM;gp++){
set_gpio_value(key_gp_list[gp], (E_IO_VAL)(sec_cnt%2));
}
for(gp=0;gp<LCD_GPIO_MAX_NUM;gp++){
set_gpio_value(lcd_gp_list[gp], (E_IO_VAL)(sec_cnt%2));
}
}
#include "time.h"
void main()
{
int sec_cnt=0;
uint8 rdata=0;
test_gpio_init();
while(1)
{
sleep(2);
sec_cnt++;
test_key_lcd_gpio();
printf(".test round %d\n",sec_cnt);
}
}
#endif
#endif
+70
View File
@@ -0,0 +1,70 @@
/*
@add
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sched.h>
#include <time.h>
#include "../inc/type.h"
#include "../inc/delay.h"
#include "../inc/lcd_task.h"
#include "../inc/isys_config.h"
#include "../inc/i2c_drv.h"
#include "../inc/dpd_proc.h"
#include "../inc/iuart_user.h"
#include "../inc/eeprom.h"
#include "../inc/gps.h"
#include "../inc/main.h"
#include "../inc/msg.h"
#include "../inc/iweb_task.h"
#include "../inc/general_api.h"
#include "../inc/isys_com.h"
#include "../inc/dac38j82.h"
#include "../inc/os_task.h"
#include "../inc/config.h"
uint32 Net_write_RF(uint8 param)
{
#if USE_DRY_LOOP
if(dryloop_status.rf==INVALID_DATA)
{
remote_write_rf(param);
return UART_PARA_SET_ERR_OK;
}
else
#endif
{
if(atsc_para.ad3_para.rc_cmd==0) //IO
{
return UART_PARA_SET_ERR_DRYLOOP;
}
else //WEB
{
remote_write_rf(param);
return UART_PARA_SET_ERR_OK;
}
}
}
+101
View File
@@ -0,0 +1,101 @@
#if 1
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h> //define O_WRONLY and O_RDONLY
#include <unistd.h> /* System V */
#include <sys/ioctl.h> /* BSD and Linux */
#include <stropts.h> /* XSI STREAMS */
#include "../inc/type.h"
#include "../inc/igpio.h"
#include "../inc/ii2c.h"
//#include <i2c-dev.h>
#include <linux/i2c-dev.h>
#define __use_imx_i2c__ 0
#if __use_imx_i2c__
int ii2c_com_init(const char*i2c_node_name,uint8 i2c_addr, int *pfd)
{
int fd=-1;
int err;
fd = open(i2c_node_name, O_RDWR);
if(fd < 0)
{
log_printf(LOG_ERR,I_NOUSED,"i2c open %s err.\n",i2c_node_name);
return -1;
}
err = ioctl(fd, I2C_SLAVE, i2c_addr);
if (err < 0)
{
log_printf(LOG_ERR,I_NOUSED,"can't set i2c dev slave addr.\n");
close(fd);
return -1;
}
*pfd = fd;
}
uint8 i2c_write(int fd, uint8 reg, uint8 val)
{
int retries;
uint8 wdata[2];
uint8 retLen = 0;
if(fd<0)
return -1;
wdata[0] = reg;
wdata[1] = val;
for (retries = 2; retries; retries--)
{
retLen = write(fd, wdata, 2);
if(retLen == 2)
{
return 0;
}
log_printf(LOG_DEBUG,I_NOUSED,"ret writeLen = %d\n",retLen);
usleep(1000 * 2);
}
return -1;
}
uint8 i2c_read(int fd, uint8 reg, uint8 *pval)
{
int retries;
uint8 retLen=0;
if(fd<0)
return -1;
for (retries = 2; retries; retries--)
{
retLen = write(fd, &reg, 1);
if(retLen == 1)
{
if (read(fd, pval, 1) == 1)
{
return 0;
}
}
log_printf(LOG_DEBUG,I_NOUSED,"ret writeLen = %d\n",retLen);
usleep(1000 * 2);
}
return -1;
}
#endif//__imx_i2c__
#endif
+389
View File
@@ -0,0 +1,389 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h> //define O_WRONLY and O_RDONLY
#include "../inc/type.h"
#include "../inc/eeprom.h"
#include "../inc/ipara_cfg.h"
#include "../inc/i2c_drv.h"
#include "../inc/minIni.h"
#include "../inc/isys_com.h"
#define __un_used_ip2ts_i2c__ 1
#define MAP_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#if 0 // map
const S_PARA_IO_ELEMENT g_para_e1_map[65535] =
{
{ EEPROM_DEFAULT_PARAM_ADDR, E_HW_SYS_PARA_TYPE, "def_para", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_STANDARD_ADDR, E_HW_SYS_PARA_TYPE, "exc_std", 0, E_NORMAL_FILE_TYPE, },
{ EERPOM_RESET_TIME_ADDR, E_HW_SYS_PARA_TYPE, "rst_time", 0, E_NORMAL_FILE_TYPE, },
{ EERPOM_NEXT , E_HW_SYS_PARA_TYPE, "next", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_ASI_ADDR , E_HW_SYS_PARA_TYPE, "asi", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DPD_ADDR, E_HW_SYS_PARA_TYPE, "dpd", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_NET_ADDR, E_HW_SYS_PARA_TYPE, "net", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_GPS_ADDR, E_HW_SYS_PARA_TYPE, "gps", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_BAUD_ADDR, E_HW_SYS_PARA_TYPE, "baud", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_ASI2_ADDR, E_HW_SYS_PARA_TYPE, "asi2", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_INPUT_ADDR, E_HW_SYS_PARA_TYPE, "input", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_SFN_ID_H, E_HW_SYS_PARA_TYPE, "sfn_id_h", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_SFN_ID_L, E_HW_SYS_PARA_TYPE, "sfn_id_l", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_CHANNEL_ADDR , E_HW_SYS_PARA_TYPE, "channel", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_RX_CH_ADDR, E_HW_SYS_PARA_TYPE, "rx_ch", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_AGC_ADDR, E_HW_SYS_PARA_TYPE, "agc", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_HZ_UIH_ADDR, E_HW_SYS_PARA_TYPE, "hz_ui_h", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_HZ_UIL_ADDR, E_HW_SYS_PARA_TYPE, "hz_ui_l", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_POWER_UIH_ADDR, E_HW_SYS_PARA_TYPE, "power_ui_h", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_POWER_UIL_ADDR, E_HW_SYS_PARA_TYPE, "power_ui_l", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_CW_ADDR, E_HW_SYS_PARA_TYPE, "cw", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_RF_ADDR, E_HW_SYS_PARA_TYPE, "rf", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_0DBFLAGH_ADDR, E_HW_SYS_PARA_TYPE, "odb_flg_h", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_0DBFLAGL_ADDR, E_HW_SYS_PARA_TYPE, "odb_flg_l", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_AGC_LAST_ADDR, E_HW_SYS_PARA_TYPE, "agc_last", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_LDPC_QAM_ADDR , E_HW_SYS_PARA_TYPE, "ldpc_qam", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_PN_ADDR, E_HW_SYS_PARA_TYPE, "pn", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_ZBPILOT_ADDR, E_HW_SYS_PARA_TYPE, "zbpilot", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_INT_ADDR, E_HW_SYS_PARA_TYPE, "int", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DELAY_ADDR1, E_HW_SYS_PARA_TYPE, "delay_a1", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DELAY_ADDR2, E_HW_SYS_PARA_TYPE, "delay_a2", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DELAY_ADDR3, E_HW_SYS_PARA_TYPE, "delay_a3", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DELAY_ADDR4, E_HW_SYS_PARA_TYPE, "delay_a4", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_TXID_H_ADDR, E_HW_SYS_PARA_TYPE, "txid_h", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_TXID_L_ADDR, E_HW_SYS_PARA_TYPE, "txid_l", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_CF_ADDR, E_HW_SYS_PARA_TYPE, "cf", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_SIPDEL_ADDR, E_HW_SYS_PARA_TYPE, "sipdel", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_ZP_ADDR, E_HW_SYS_PARA_TYPE, "zp", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DLY_SEC_ADDR, E_HW_SYS_PARA_TYPE, "dly_sec", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_TOD_EXT_ADDR, E_HW_SYS_PARA_TYPE, "tod_ext", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_BPS_ADDR , E_HW_SYS_PARA_TYPE, "bps", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_ID_ADDR, E_HW_SYS_PARA_TYPE, "id", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_ID_ADDR_M, E_HW_SYS_PARA_TYPE, "id_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_PCR_ADDR , E_HW_SYS_PARA_TYPE, "pcr", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DS3_ADDR, E_HW_SYS_PARA_TYPE, "ds3", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_HI_ADDR, E_HW_SYS_PARA_TYPE, "hi", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_GNSS_ADDR, E_HW_SYS_PARA_TYPE, "gnss", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_UBLOX_ADDR, E_HW_SYS_PARA_TYPE, "ublox", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_AUTO_ADPC_ADDR , E_HW_SYS_PARA_TYPE, "auto_adpc", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_A_DPD_TIME, E_HW_SYS_PARA_TYPE, "a_dpd_time", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_A_DPD_A_SNR, E_HW_SYS_PARA_TYPE, "a_dpd_a_snr", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_A_DPD_A_IMD, E_HW_SYS_PARA_TYPE, "a_dpd_a_imd", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_A_DPD_R_SNR, E_HW_SYS_PARA_TYPE, "a_dpd_r_snr", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_A_DPD_R_IMD, E_HW_SYS_PARA_TYPE, "a_dpd_r_imd", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_A_DPD_C_T, E_HW_SYS_PARA_TYPE, "a_dpd_c_t", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_A_DPD_USE_DPD, E_HW_SYS_PARA_TYPE, "a_dpd_dpd", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_AD3_POWER_REF_H, E_HW_SYS_PARA_TYPE, "ad3_pwr_ref_h", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_AD3_POWER_REF_L, E_HW_SYS_PARA_TYPE, "ad3_pwr_ref_l", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_AD3_FWD_CAL, E_HW_SYS_PARA_TYPE, "ad3_fwd_cal", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_AD3_BITRATE_THRESHOLD_HH, E_HW_SYS_PARA_TYPE, "ad3_btrate_th_hh", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_AD3_BITRATE_THRESHOLD_HL, E_HW_SYS_PARA_TYPE, "ad3_btrate_th_hl", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_AD3_BITRATE_THRESHOLD_LH, E_HW_SYS_PARA_TYPE, "ad3_btrate_th_lh", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_AD3_BITRATE_THRESHOLD_LL, E_HW_SYS_PARA_TYPE, "ad3_btrate_th_ll", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_PA_IP4_ADDR , E_HW_SYS_PARA_TYPE, "pa_ip4", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_PA_GATEWAY_ADDR, E_HW_SYS_PARA_TYPE, "pa_gw", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_PA_NETMASK_ADDR, E_HW_SYS_PARA_TYPE, "pa_netmask", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_AGC_standard_addrH , E_HW_SYS_PARA_TYPE, "agc_std_h", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_AGC_standard_addrL , E_HW_SYS_PARA_TYPE, "agc_std_l", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_PWRVREF_O_HADDR, E_HW_SYS_PARA_TYPE, "pwr_ref0_h", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_PWRVREF_O_LADDR, E_HW_SYS_PARA_TYPE, "pwr_ref0_l", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_PWRVREF_VLDO_ADDR, E_HW_SYS_PARA_TYPE, "pwr_vld0", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DPD_DISTORTION_SW, E_HW_SYS_PARA_TYPE, "dpd_distortion_sw", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DPD_AGC_PWR_L, E_HW_SYS_PARA_TYPE, "dpd_agc_pwr_l", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DPD_AGC_PWR_H, E_HW_SYS_PARA_TYPE, "dpd_agc_pwr_h", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DPD_ERR_TH_L, E_HW_SYS_PARA_TYPE, "dpd_err_th_l", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DPD_ERR_TH_H, E_HW_SYS_PARA_TYPE, "dpd_err_th_h", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DPD_ERR_CNT_TH, E_HW_SYS_PARA_TYPE, "dpd_err_cnt_th", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DPD_AMP_SAT_TH_L, E_HW_SYS_PARA_TYPE, "dpd_amp_sat_th_l", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DPD_AMP_SAT_TH_H, E_HW_SYS_PARA_TYPE, "dpd_amp_sat_th_h", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DPD_TEST_L, E_HW_SYS_PARA_TYPE, "dpd_test_l", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DPD_TEST_H, E_HW_SYS_PARA_TYPE, "dpd_test_h", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DPD_STEP, E_HW_SYS_PARA_TYPE, "dpd_step", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DPD_FLASH_VALID, E_HW_SYS_PARA_TYPE, "dpd_flash_valid", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DPD_PHASE6_MINUTE, E_HW_SYS_PARA_TYPE, "dpd_stg6_minute", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DPD_WPP_TH_L, E_HW_SYS_PARA_TYPE, "dpd_wpp_th_l", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DPD_WPP_TH_H, E_HW_SYS_PARA_TYPE, "dpd_wpp_th_h", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_NOTICE_PART_END, E_HW_SYS_PARA_TYPE, "notice_end", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_ASI_ADDR_M , E_HW_SYS_PARA_TYPE, "asi_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_ADPC_ADDR_M, E_HW_SYS_PARA_TYPE, "adpc_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_GPS_ADDR_M, E_HW_SYS_PARA_TYPE, "gps_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_MON_ADDR_M, E_HW_SYS_PARA_TYPE, "mon_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_PWRVREF_H_ADDR_M, E_HW_SYS_PARA_TYPE, "pwrref_h_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_PWRVREF_L_ADDR_M, E_HW_SYS_PARA_TYPE, "pwrref_l_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_PWRVREF_VLDO_ADDR_M, E_HW_SYS_PARA_TYPE, "pwrref_vldo_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_CHANNEL_ADDR_M , E_HW_SYS_PARA_TYPE, "channel_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_RXFREQ_ADDR_M, E_HW_SYS_PARA_TYPE, "rxfreq_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_CW_ADDR_M, E_HW_SYS_PARA_TYPE, "cw_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_RF_ADDR_M, E_HW_SYS_PARA_TYPE, "rf_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_AGC_ADDR_M, E_HW_SYS_PARA_TYPE, "agc_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_0DBFLAGH_ADDR_M, E_HW_SYS_PARA_TYPE, "odbflag_h_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_0DBFLAGL_ADDR_M, E_HW_SYS_PARA_TYPE, "odbflag_l_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_POWER_UIH_ADDR_M, E_HW_SYS_PARA_TYPE, "pwr_uih_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_POWER_UIL_ADDR_M, E_HW_SYS_PARA_TYPE, "pwr_uil_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_HZ_H_ADDR_M, E_HW_SYS_PARA_TYPE, "hz_h_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_HZ_L_ADDR_M, E_HW_SYS_PARA_TYPE, "hz_l_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_NET_ADDR_M , E_HW_SYS_PARA_TYPE, "net_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DVBC_EN_ADDR_M, E_HW_SYS_PARA_TYPE, "dvbc_en_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_NET_FREQ_ADDR_M, E_HW_SYS_PARA_TYPE, "net_freq_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_QAM_ADDR_M, E_HW_SYS_PARA_TYPE, "qam_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_SYM_ADDR_M, E_HW_SYS_PARA_TYPE, "sys_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_INVER_ADDR_M, E_HW_SYS_PARA_TYPE, "inver_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_TXID_ADDR_M, E_HW_SYS_PARA_TYPE, "txid_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_AREA_ADDR_M, E_HW_SYS_PARA_TYPE, "area_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_TRANS_ADDR_M, E_HW_SYS_PARA_TYPE, "trans_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_SLOT_ADDR_M, E_HW_SYS_PARA_TYPE, "slot_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DLY_H_ADDR_M, E_HW_SYS_PARA_TYPE, "dly_h", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DLY_M_ADDR_M, E_HW_SYS_PARA_TYPE, "dly_m", 0, E_NORMAL_FILE_TYPE, },
{ EEPROM_DLY_L_ADDR_M, E_HW_SYS_PARA_TYPE, "dly_l", 0, E_NORMAL_FILE_TYPE, },
//end
{0, 0, "e",0, E_NULL_FILE_TYPE},
};
const S_PARA_IO_ELEMENT g_para_e2_map[65535] =
{
{ E2_AD3_POWER_REF_H, E_HW_SYS_PARA_TYPE, "ad3_pwr_ref_h", 0, E_BKP1_FILE_TYPE, },
{ E2_AD3_POWER_REF_L, E_HW_SYS_PARA_TYPE, "ad3_pwr_ref_l", 0, E_BKP1_FILE_TYPE, },
{ E2_PWRVREF_O_HADDR, E_HW_SYS_PARA_TYPE, "pwr_ref0_h", 0, E_BKP1_FILE_TYPE, },
{ E2_PWRVREF_O_LADDR, E_HW_SYS_PARA_TYPE, "pwr_ref0_l", 0, E_BKP1_FILE_TYPE, },
{ E2_PWRVREF_VLDO_ADDR, E_HW_SYS_PARA_TYPE, "pwr_vld0", 0, E_BKP1_FILE_TYPE, },
{ E2_AGC_ADDR, E_HW_SYS_PARA_TYPE, "agc", 0, E_BKP1_FILE_TYPE, },
{ E2_POWER_UIH_ADDR, E_HW_SYS_PARA_TYPE, "e2power_ui_h", 0, E_BKP1_FILE_TYPE, },
{ E2_POWER_UIL_ADDR, E_HW_SYS_PARA_TYPE, "e2power_ui_l", 0, E_BKP1_FILE_TYPE, },
{ E2_0DBFLAGH_ADDR, E_HW_SYS_PARA_TYPE, "odb_flg_h", 0, E_BKP1_FILE_TYPE, },
{ E2_0DBFLAGL_ADDR, E_HW_SYS_PARA_TYPE, "odb_flg_l", 0, E_BKP1_FILE_TYPE, },
{ BAK_EEPROM_POWER_UIH_ADDR, E_HW_SYS_PARA_TYPE, "bak_power_ui_h", 0, E_BKP1_FILE_TYPE, },
{ BAK_EEPROM_POWER_UIL_ADDR, E_HW_SYS_PARA_TYPE, "bak_power_ui_l", 0, E_BKP1_FILE_TYPE, },
{ BAK_EEPROM_0DBFLAGH_ADDR, E_HW_SYS_PARA_TYPE, "bak_odb_flg_h", 0, E_BKP1_FILE_TYPE, },
{ BAK_EEPROM_0DBFLAGL_ADDR, E_HW_SYS_PARA_TYPE, "bak_odb_flg_l", 0, E_BKP1_FILE_TYPE, },
{ BAK_E2_POWER_UIH_ADDR, E_HW_SYS_PARA_TYPE, "bak_e2power_ui_h", 0, E_BKP1_FILE_TYPE, },
{ BAK_E2_POWER_UIL_ADDR, E_HW_SYS_PARA_TYPE, "bak_e2power_ui_l", 0, E_BKP1_FILE_TYPE, },
{ BAK_E2_0DBFLAGH_ADDR, E_HW_SYS_PARA_TYPE, "bak_e2odb_flg_h", 0, E_BKP1_FILE_TYPE, },
{ BAK_E2_0DBFLAGL_ADDR, E_HW_SYS_PARA_TYPE, "bak_e2odb_flg_l", 0, E_BKP1_FILE_TYPE, },
{ BAK_EEPROM_CHANNEL_ADDR, E_HW_SYS_PARA_TYPE, "bak_channel", 0, E_BKP1_FILE_TYPE, },
{ BAK_EEPROM_RXCHANNEL_ADDR, E_HW_SYS_PARA_TYPE, "bak_rx_channel", 0, E_BKP1_FILE_TYPE, },
{ BAK_EEPROM_POWER_UIH_ADDR_M, E_HW_SYS_PARA_TYPE, "bak_power_ui_h_m", 0, E_BKP1_FILE_TYPE, },
{ BAK_EEPROM_POWER_UIL_ADDR_M, E_HW_SYS_PARA_TYPE, "bak_power_ui_l_m", 0, E_BKP1_FILE_TYPE, },
{ BAK_EEPROM_0DBFLAGH_ADDR_M, E_HW_SYS_PARA_TYPE, "bak_odb_flg_h_m", 0, E_BKP1_FILE_TYPE, },
{ BAK_EEPROM_0DBFLAGL_ADDR_M, E_HW_SYS_PARA_TYPE, "bak_odb_flg_l_m", 0, E_BKP1_FILE_TYPE, },
{ BAK_E2_POWER_UIH_ADDR_M, E_HW_SYS_PARA_TYPE, "bak_e2power_ui_h_m", 0, E_BKP1_FILE_TYPE, },
{ BAK_E2_POWER_UIL_ADDR_M, E_HW_SYS_PARA_TYPE, "bak_e2power_ui_l_m", 0, E_BKP1_FILE_TYPE, },
{ BAK_E2_0DBFLAGH_ADDR_M, E_HW_SYS_PARA_TYPE, "bak_e2odb_flg_h_m", 0, E_BKP1_FILE_TYPE, },
{ BAK_E2_0DBFLAGL_ADDR_M, E_HW_SYS_PARA_TYPE, "bak_e2odb_flg_l_m", 0, E_BKP1_FILE_TYPE, },
{ BAK_EEPROM_CHANNEL_ADDR_M, E_HW_SYS_PARA_TYPE, "bak_channel_m", 0, E_BKP1_FILE_TYPE, },
{ BAK_EEPROM_RXCHANNEL_ADDR_M, E_HW_SYS_PARA_TYPE, "bak_rx_channel_m", 0, E_BKP1_FILE_TYPE, },
//end
{0, 0, "e", 0, E_NULL_FILE_TYPE},
};
const S_PARA_IO_ELEMENT g_para_e3_map[65535] =
{
//end
{0, 0, "e", 0, E_NULL_FILE_TYPE},
};
#endif //map
#if 0
static uint8 get_e1_para_from_file(uint16 para_id,const S_PARA_IO_ELEMENT *pmap, uint16 map_ele_num, char *pfile)
{
uint16 i;
uint32 u32val;
char sname[30]={0};
//const S_PARA_IO_ELEMENT *pmap = &g_para_e1_map[0];
for(i=0;i<MAP_ARRAY_SIZE(g_para_e1_map);){
if(pmap->para_id == para_id){
if(pmap->para_type == E_HW_SYS_PARA_TYPE){
u32val = (uint32)ini_getl(STR_HW_SYS_PARA_TYPE, pmap->para_name, DEF_REG_PARA, F_EXC_CFG_PARA_FILE);
}
else if(pmap->para_type == E_LCD_SYS_PARA_TYPE){
u32val = (uint32)ini_getl(STR_LCD_SYS_PARA_TYPE, pmap->para_name, DEF_REG_PARA, F_EXC_CFG_PARA_FILE);
}
else{
u32val = (uint32)ini_getl(STR_HW_SYS_PARA_TYPE, pmap->para_name, DEF_REG_PARA, F_EXC_CFG_PARA_FILE);
}
return (uint8)u32val;
}
i++;
pmap = &g_para_e1_map[i];
}
sprintf(sname,"reg%d",para_id);
u32val = (uint32)ini_getl(STR_BLK_SYS_PARA_TYPE, sname, DEF_REG_PARA, F_EXC_CFG_PARA_FILE);
printf("Note.e1_map para not match for read.\n\n");
}
static uint8 get_e2_para_from_file(uint16 para_id)
{
uint16 i;
uint32 u32val;
char sname[30]={0};
const S_PARA_IO_ELEMENT *pmap = &g_para_e2_map[0];
for(i=0;i<MAP_ARRAY_SIZE(g_para_e2_map);){
if(pmap->para_id == para_id){
if(pmap->para_type == E_HW_SYS_PARA_TYPE){
u32val = (uint32)ini_getl(STR_HW_SYS_PARA_TYPE, pmap->para_name, DEF_REG_PARA, F_EXC_CFG_PARA_BKP1_FILE);
}
else if(pmap->para_type == E_LCD_SYS_PARA_TYPE){
u32val = (uint32)ini_getl(STR_LCD_SYS_PARA_TYPE, pmap->para_name, DEF_REG_PARA, F_EXC_CFG_PARA_BKP1_FILE);
}
else{
u32val = (uint32)ini_getl(STR_HW_SYS_PARA_TYPE, pmap->para_name, DEF_REG_PARA, F_EXC_CFG_PARA_BKP1_FILE);
}
return (uint8)u32val;
}
i++;
pmap = &g_para_e2_map[i];
}
sprintf(sname,"reg%d",para_id);
u32val = (uint32)ini_getl(STR_BLK_SYS_PARA_TYPE, sname, DEF_REG_PARA, F_EXC_CFG_PARA_FILE);
printf("Err.e2_map para not match for read.\n\n");
}
static uint8 get_e3_para_from_file(uint16 para_id)
{
uint16 i;
uint32 u32val;
char sname[30]={0};
const S_PARA_IO_ELEMENT *pmap = &g_para_e3_map[0];
for(i=0;i<MAP_ARRAY_SIZE(g_para_e3_map);){
if(pmap->para_id == para_id){
if(pmap->para_type == E_HW_SYS_PARA_TYPE){
u32val = (uint32)ini_getl(STR_HW_SYS_PARA_TYPE, pmap->para_name, DEF_REG_PARA, F_EXC_CFG_PARA_BKP2_FILE);
}
else if(pmap->para_type == E_LCD_SYS_PARA_TYPE){
u32val = (uint32)ini_getl(STR_LCD_SYS_PARA_TYPE, pmap->para_name, DEF_REG_PARA, F_EXC_CFG_PARA_BKP2_FILE);
}
else{
u32val = (uint32)ini_getl(STR_HW_SYS_PARA_TYPE, pmap->para_name, DEF_REG_PARA, F_EXC_CFG_PARA_BKP2_FILE);
}
return (uint8)u32val;
}
i++;
pmap = &g_para_e3_map[i];
}
sprintf(sname,"reg%d",para_id);
u32val = (uint32)ini_getl(STR_BLK_SYS_PARA_TYPE, sname, DEF_REG_PARA, F_EXC_CFG_PARA_FILE);
printf("Err.e3_map para not match for read.\n\n");
}
#endif
static uint8 get_e_para_from_file(uint16 para_id,const S_PARA_IO_ELEMENT *pe_map, uint16 map_ele_num, char *pfile)
{
uint32 u32val = 0;
char sname[30] = {0};
sprintf(sname, "reg%d", para_id);
u32val = (uint32)ini_getl(STR_BLK_SYS_PARA_TYPE, sname, DEF_REG_PARA, F_EXC_CFG_PARA_FILE);
return (uint8)u32val;
}
static uint8 get_e_para_from_file_v2(uint16 para_id, uint8 default_val, char *pfile)
{
uint32 u32val = 0;
char sname[30] = {0};
sprintf(sname, "reg%d", para_id);
u32val = (uint32)ini_getl(STR_BLK_SYS_PARA_TYPE, sname, default_val, F_EXC_CFG_PARA_FILE);
return (uint8)u32val;
}
uint8 get_para_from_e_map(uint16 para_id, uint8 chip)
{
uint32 data = 0;
if(chip==E2_CHIP_1){
data = get_e_para_from_file(para_id, NULL, 0, F_EXC_CFG_PARA_FILE);
}
else if(chip==E2_CHIP_2){
data = get_e_para_from_file(para_id, NULL, 0, F_EXC_CFG_PARA_BKP1_FILE);
}
else if(chip==E2_CHIP_3){
data = get_e_para_from_file(para_id, NULL, 0, F_EXC_CFG_PARA_BKP2_FILE);
}
else{
log_printf(LOG_DEBUG,I_NOUSED,"chip num not match for read.\n\n");
}
return data;
}
uint8 get_para_from_e_map_v2(uint16 para_id, uint8 default_val, uint8 chip)
{
uint32 data = 0;
if(chip==E2_CHIP_1){
data = get_e_para_from_file_v2(para_id, default_val, F_EXC_CFG_PARA_FILE);
}
else if(chip==E2_CHIP_2){
data = get_e_para_from_file_v2(para_id, default_val, F_EXC_CFG_PARA_BKP1_FILE);
}
else if(chip==E2_CHIP_3){
data = get_e_para_from_file_v2(para_id, default_val, F_EXC_CFG_PARA_BKP2_FILE);
}
else{
log_printf(LOG_DEBUG,I_NOUSED,"chip num not match for read.\n\n");
}
return data;
}
static int set_e_para_to_file(uint16 para_id, uint8 para_val, const S_PARA_IO_ELEMENT *pe_map, uint16 map_ele_num, char *pfile)
{
uint16 i;
uint32 u32val;
int ret=0;
char sname[30]={0};
sprintf(sname,"reg%d",para_id);
ret = ini_putl(STR_BLK_SYS_PARA_TYPE, sname, para_val, pfile);
if(ret!=1){
log_printf(LOG_ERR,I_NOUSED,"e_map para write failed.\n\n");
}
return ret;
}
int set_para_to_e_map(uint16 para_id, uint8 para_val, uint8 chip)
{
int ret=0;
if(chip==E2_CHIP_1){
ret = set_e_para_to_file(para_id, para_val, NULL, 0, F_EXC_CFG_PARA_FILE);
}
else if(chip==E2_CHIP_2){
ret = set_e_para_to_file(para_id, para_val, NULL, 0, F_EXC_CFG_PARA_BKP1_FILE);
}
else if(chip==E2_CHIP_3){
ret = set_e_para_to_file(para_id, para_val, NULL, 0, F_EXC_CFG_PARA_BKP2_FILE);
}
else{
log_printf(LOG_ERR,I_NOUSED,"chip num not match for write.\n\n");
}
return ret;
}
+512
View File
@@ -0,0 +1,512 @@
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <sys/ioctl.h>
#include <string.h>
#include <fcntl.h>
#include "../inc/ispi.h"
#include "../inc/isys_com.h"
static int openSpiDev(char *pDevName)
{
int fd ;
int ret ;
if(pDevName == NULL)
return -2;
if(!access(pDevName, F_OK))
{
fd = open(pDevName, O_RDWR);
if(fd < 0)
{
log_printf(LOG_ERR,I_NOUSED,"open error %s \n",pDevName);
return -1;
}
}
else
{
return -1;
}
//spidev_fd = fd;
return (fd);
}
static int initSpiDev(int fd, u8 mode, u8 bits, int speed)
{
int err;
err = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (err == -1)
{
log_printf(LOG_ERR,I_NOUSED,"can't set spi SPI_IOC_WR_MODE\n");
return err;
}
err = ioctl(fd, SPI_IOC_RD_MODE, &mode);
if (err == -1)
{
log_printf(LOG_ERR,I_NOUSED,"can't set spi SPI_IOC_RD_MODE\n");
return err;
}
err = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (err == -1)
{
log_printf(LOG_ERR,I_NOUSED,"can't set spi SPI_IOC_WR_BITS_PER_WORD\n");
return err;
}
err = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (err == -1)
{
log_printf(LOG_ERR,I_NOUSED,"can't set spi SPI_IOC_RD_BITS_PER_WORD\n");
return err;
}
// err = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
// if (err == -1)
// {
// printf("can't set spi SPI_IOC_WR_MAX_SPEED_HZ\n");
// return err;
// }
err = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (err == -1)
{
log_printf(LOG_ERR,I_NOUSED,"can't set spi SPI_IOC_RD_MAX_SPEED_HZ\n");
return err;
}
return 0;
}
static int transfer_r(int fd, u16 addr, u8 *pdata)
{
int ret;
uint8_t rx[3] = {0};
uint8_t tx[3] = {0};
u16 writeInstruct = 0;
if(fd < 0 || pdata == NULL)
return -1;
writeInstruct= (addr|0x8000);
tx[0]= writeInstruct>>8;
tx[1]= (u8)(writeInstruct&0xff);
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = ARRAY_SIZE(tx),
.delay_usecs = SPI_DELAY,
.speed_hz = SPEED,
.bits_per_word = 8,
};
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1)
{
log_printf(LOG_ERR,I_NOUSED,"can't send spi message,for read %d\n",fd);
return -1;
}
//printf("addr=%.2x rd: ",addr);
/*printf("r Tx:");
for (ret = 0; ret < ARRAY_SIZE(tx); ret++)
{
printf("%.2X ", tx[ret]);
}
printf("r Rx:");
for (ret = 0; ret < ARRAY_SIZE(tx); ret++)
{
printf("%.2X ", rx[ret]);
}
printf("\n");
*/
*pdata = rx[2];
return 0;
}
static int transfer_w(int fd, u32 addr, u8 data)
{
int ret;
uint8_t rx[3] = {0};
uint8_t tx[3] = {0};
if(fd < 0)
return -1;
addr &=~0x8000;
tx[0] =(u8)((addr & 0xFF00) >> 8);
tx[1] =(u8)(addr & 0x00FF);
tx[2] = data;
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = ARRAY_SIZE(tx),
.delay_usecs = SPI_DELAY,
.speed_hz = SPEED,
.bits_per_word = 8,
};
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1)
{
log_printf(LOG_ERR,I_NOUSED,"can't send spi message, for write fd %d\n",fd);
return -1;
}
//printf("[sub]addr=%.2x w-rd: ",addr);
/*printf("w Tx:");
for (ret = 0; ret < ARRAY_SIZE(tx); ret++)
{
printf(" %.2X ", tx[ret]);
}
printf("\n");
printf("w Rx:\n");
for (ret = 0; ret < ARRAY_SIZE(tx); ret++)
{
printf(" %.2X ", rx[ret]);
}
printf("\n");*/
return 0;
}
/* 4 wrie need tx and rx len : 3 ,return: ff xx xx*/
int spi_r(int fd, u8 addr, u16 *pdata)
{
int ret;
uint8_t rx[3] = {0};
uint8_t tx[3] = {0};
if(fd < 0)
return -1;
tx[0] = (u8)(addr | 0x80);
tx[1] = 0x00;
tx[2] = 0x00;
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = ARRAY_SIZE(tx),
.delay_usecs = SPI_DELAY,
.speed_hz = SPEED,
.bits_per_word = 8,
};
ret = ioctl(fd, SPI_IOC_MESSAGE(HALF_DUPLEX), &tr);
if (ret < 1){
log_printf(LOG_ERR,I_NOUSED,"can't send spi message, for write fd %d\n",fd);
return -1;
}
*pdata = (rx[1]<<8) + rx[2];
return 0;
}
/* 4 wrie need tx and rx len : 3 ,return: ff xx xx*/
int spi_r_addr16_data8(int fd, u16 addr, u8 *pdata)
{
int ret;
uint8_t rx[3] = {0};
uint8_t tx[3] = {0};
if(fd < 0)
return -1;
tx[0] = (u8)((addr>>8)&0xff | 0x80);
tx[1] = (u8)( addr&0xff);
tx[2] = 0x00;
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = ARRAY_SIZE(tx),
.delay_usecs = SPI_DELAY,
.speed_hz = SPEED,
.bits_per_word = 8,
};
ret = ioctl(fd, SPI_IOC_MESSAGE(HALF_DUPLEX), &tr);
if (ret < 1){
log_printf(LOG_ERR,I_NOUSED,"can't send spi message, for write fd %d\n",fd);
return -1;
}
*pdata = rx[2];
return 0;
}
uint8_t spi_rx_buff[35] = {0};
uint8_t spi_tx_buff[35] = {0};
int spi_transfer_big_block(int spi_fd, uint8 *pdata, int bytelen)
{
int ret;
if(spi_fd < 0)
return -1;
if(bytelen>SPI_BUFF_MAX)
printf("[%s,%d] buff full!! %d > 4KB\n", __func__,__LINE__, bytelen);
memset(spi_tx_buff, 0, sizeof(spi_tx_buff));
memcpy(spi_tx_buff, pdata, bytelen);
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)spi_tx_buff,
.rx_buf = (unsigned long)spi_rx_buff,
.len = bytelen,//ARRAY_SIZE(spi_tx_buff),
.delay_usecs = SPI_DELAY,
.speed_hz = SPEED,
.bits_per_word = 8,
};
#if 1
/* SPI_IOC_MESSAGE(n) -- [n] means numbers of struct spi_ioc_transfer, so 1. */
ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1) {
printf(" [%s,%d]send spi msg err, len=%d ret=%d rx[0]=%d tx[0]=%d (%d)\n", __func__,__LINE__, bytelen,ret, spi_rx_buff[0],spi_tx_buff[0], tr.len);
//pabort("can't send spi message");
return -1;
}
#endif
return 0;
}
int spi_receiver_big_block(int spi_fd, uint8 *pdata, int bytelen)
{
int ret;
if(spi_fd < 0)
return -1;
if(bytelen>SPI_BUFF_MAX)
printf("[%s,%d] buff full!! %d > 4KB\n", __func__,__LINE__, bytelen);
memset(spi_tx_buff, 0, sizeof(spi_tx_buff));
memcpy(spi_tx_buff, pdata, 3);
memset(spi_rx_buff, 0, sizeof(spi_rx_buff));
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)spi_tx_buff,
.rx_buf = (unsigned long)spi_rx_buff,
.len = bytelen,//ARRAY_SIZE(spi_tx_buff),
.delay_usecs = SPI_DELAY,
.speed_hz = SPEED,
.bits_per_word = 8,
};
#if 1
/* SPI_IOC_MESSAGE(n) -- [n] means numbers of struct spi_ioc_transfer, so 1. */
ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1) {
printf(" [%s,%d]send spi msg err, len=%d ret=%d rx[0]=%d tx[0]=%d (%d)\n", __func__,__LINE__, bytelen,ret, spi_rx_buff[0],spi_tx_buff[0], tr.len);
return -1;
}
//printf(" rxbuf[3-4]= 0x%02x 0x%02x\n",spi_rx_buff[3], spi_rx_buff[4]);
memcpy(pdata, spi_rx_buff, bytelen);
#endif
return 0;
}
int spi_w(int fd, u8 addr, uint16_t u16_data)
{
int ret;
uint8_t rx[3] = {0};
uint8_t tx[3] = {0};
uint32 u32addr = (uint32)addr << 16;
if(fd < 0)
return -1;
u32addr &= ~0x800000;
u32addr |= u16_data;
tx[0] = (u8)((u32addr >> 16) & 0xFF);
tx[1] = (u8)((u32addr >> 8) & 0xFF);
tx[2] = (u8)((u32addr >> 0) & 0xFF);
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = ARRAY_SIZE(tx),
.delay_usecs = SPI_DELAY,
.speed_hz = SPEED,
.bits_per_word = 8,
};
ret = ioctl(fd, SPI_IOC_MESSAGE(HALF_DUPLEX), &tr);
if (ret < 1){
log_printf(LOG_ERR,I_NOUSED,"can't send spi message, for write fd %d\n",fd);
return -1;
}
return 0;
}
int spi_w_addr16_data8(int fd, u16 addr, uint8_t data)
{
int ret;
uint8_t rx[3] = {0};
uint8_t tx[3] = {0};
uint32 u16addr=0;
if(fd < 0)
return -1;
u16addr &= ~0x8000;
u16addr |= (addr&0x7fff);
tx[0] = (u8)((u16addr >> 8) & 0xFF);
tx[1] = (u8)((u16addr) & 0xFF);
tx[2] = (u8)((data) & 0xFF);
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = ARRAY_SIZE(tx),
.delay_usecs = SPI_DELAY,
.speed_hz = SPEED,
.bits_per_word = 8,
};
ret = ioctl(fd, SPI_IOC_MESSAGE(HALF_DUPLEX), &tr);
if (ret < 1){
log_printf(LOG_ERR,I_NOUSED,"can't send spi message, for write fd %d\n",fd);
return -1;
}
return 0;
}
uint8_t iSPI_I2S_SendData(int spi_fd, uint16_t Data)
{
int ret;
uint8_t rx[2] = {0};
uint8_t tx[2] = {0};
if(spi_fd < 0)
return -1;
tx[0] = (u8)((Data & 0xFF00) >> 8);
tx[1] = (u8)((Data & 0x00FF) >> 0);
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = ARRAY_SIZE(tx),
.delay_usecs = SPI_DELAY,
.speed_hz = SPEED,
.bits_per_word = 8,
};
ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1)
{
log_printf(LOG_ERR,I_NOUSED,"can't send spi message, for write fd %d\n",spi_fd);
return -1;
}
return 0;
}
uint8_t iSPI_I2S_Send4Byte(int spi_fd, uint8 *pdata)
{
int ret;
uint8_t rx[4] = {0};
uint8_t tx[4] = {0};
if(spi_fd < 0)
return -1;
memcpy(tx, pdata, 4);
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = ARRAY_SIZE(tx),
.delay_usecs = SPI_DELAY,
.speed_hz = SPEED,
.bits_per_word = 8,
};
/* SPI_IOC_MESSAGE(n) -- [n] means numbers of struct spi_ioc_transfer, so 1. */
ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1) {
log_printf(LOG_ERR,I_NOUSED,"can't send spi message, for write fd %d\n", spi_fd);
return -1;
}
return 0;
}
int init_spi_dev(char *pspi_node_name, int *pfd, uint8 mode, uint8 bits, uint8 speed)
{
int ret;
int fd=-1;
/*
wr/rd speed: defined through macro definitions[ SPEED ].
*/
// unsigned char mode = SPI_MODE_3 ;
// unsigned char bits = 8;
// unsigned int speed = 100000;
fd = openSpiDev(pspi_node_name);
if(fd < 0) {
log_printf(LOG_ERR, I_NOUSED, "init_spi_dev >%s< open failed!\n", pspi_node_name);
}
ret = initSpiDev(fd, mode, bits, speed);
if(ret < 0 ) {
log_printf(LOG_ERR, I_NOUSED, "init_spi_dev >%s< init failed!\n", pspi_node_name);
return -1;
}
log_printf(LOG_INFO, I_SYS_STATUS, "init_spi_dev >%s< init ok!!\n", pspi_node_name);
*pfd = fd;
}
+572
View File
@@ -0,0 +1,572 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <sys/prctl.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdarg.h>
#include <signal.h>
#include "../inc/type.h"
#include "../inc/isys_com.h"
#include "../inc/ipara_cfg.h"
#include "../inc/eeprom.h"
#include "../inc/i2c_drv.h"
#define SYNC_PARA_TO_BKP_FILE "sh /aw/anywave/sync_to_bkp_file.sh"
#define LOAD_PARA_TO_CFG_FILE "sh /aw/anywave/load_to_cfg_file.sh"
extern int executeCMD(const char *cmd, char *result, u16 result_size);
typedef void (*sighandler_t)(int);
int read_file_data(char *file_name,char *buff, int len)
{
int fd;
int ret = 0;
fd = open(file_name,O_RDONLY);
if(fd == -1) {
log_printf(LOG_ERR,I_NOUSED," open %s failed\n",file_name);
return -1;
}
if(read(fd,buff,len)==-1) {
log_printf(LOG_ERR,I_NOUSED,"read %d bytes from %s failed\n", len, file_name);
close(fd);
return -1;
} else {
close(fd);
return 0;
}
}
int check_file_exist(char *filename)
{
if(filename == NULL) {
log_printf(LOG_ERR,I_NOUSED,"filename is null. \n");
return -1;
}
int ret;
if(!access(filename, F_OK))
return 0;
else
return -1;
}
#if 1//verify_sys_com
unsigned long get_sys_tick(void) // 10ms
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
return (unsigned long)(tv.tv_sec*1000+tv.tv_usec/1000);
//return ((unsigned long) time( NULL)); // us
}
int get_sys_time(S_SYS_TIME *ptime)
{
if(!ptime)
return -1;
/*ptime->hour = date_time.hour;
ptime->min = date_time.min;
ptime->sec = date_time.sec;
ptime->date = date_time.date;
ptime->month= date_time.month;
ptime->year = date_time.year;*/
struct tm *tim;
struct timeval t;
time_t b_time;
b_time = time(NULL);
tim = localtime(&b_time);
// gettimeofday(&t, NULL);
ptime->hour = tim->tm_hour;
ptime->min = tim->tm_min;
ptime->sec = tim->tm_sec;
ptime->date = tim->tm_mday;
ptime->month= tim->tm_mon + 1;
ptime->year = tim->tm_year + 1900;
//printf("%d-%d-%d %d:%d:%d\n",ptime->year,ptime->month, ptime->date, ptime->hour,ptime->min,ptime->sec );
return ((int)tim->tm_wday);
}
void utc2localtime(time_t tUtcTime, struct tm *ptNow, int iZoneOffset)
{
int month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (tUtcTime <= 0 || ptNow == NULL) {
return;
}
gmtime_r(&tUtcTime, ptNow);
ptNow->tm_year += 1900;
ptNow->tm_mon += 1;
ptNow->tm_hour += iZoneOffset;
if (ptNow->tm_hour >= 24) {
ptNow->tm_hour -= 24;
ptNow->tm_mday += 1;
} else if (ptNow->tm_hour < 0) {
ptNow->tm_hour += 24;
ptNow->tm_mday -= 1;
}
int current_month_max = month[ptNow->tm_mon];
if (ptNow->tm_mon == 2) {
if ((ptNow->tm_year % 400 == 0) || (ptNow->tm_year % 4 == 0 && ptNow->tm_year % 100 != 0)) {
current_month_max = 29;
}
}
if (ptNow->tm_mday > current_month_max) {
ptNow->tm_mday = 1;
ptNow->tm_mon += 1;
} else if (ptNow->tm_mday < 1) {
ptNow->tm_mon -= 1;
if (ptNow->tm_mon < 1) {
ptNow->tm_mon = 12;
ptNow->tm_year -= 1;
}
int prev_month = ptNow->tm_mon;
int prev_month_max = month[prev_month];
if (prev_month == 2) {
if ((ptNow->tm_year % 400 == 0) || (ptNow->tm_year % 4 == 0 && ptNow->tm_year % 100 != 0)) {
prev_month_max = 29;
}
}
ptNow->tm_mday = prev_month_max;
}
if (ptNow->tm_mon > 12) {
ptNow->tm_mon = 1;
ptNow->tm_year += 1;
} else if (ptNow->tm_mon < 1) {
ptNow->tm_mon = 12;
ptNow->tm_year -= 1;
}
}
void get_beijing_time(struct tm *ptmBJ)
{
time_t time_cur = 0;
int BeiJing_zone_dffset = 8; //Beijing +8
memset(ptmBJ, 0, sizeof(struct tm));
time_cur = time(NULL);
utc2localtime(time_cur, ptmBJ, BeiJing_zone_dffset);
}
void set_sys_time(S_SYS_TIME *p)
{
#if 1
struct tm tptr;
struct timeval tv;
if(!p) {
return;
}
tptr.tm_year = p->year - 1900;
tptr.tm_mon = p->month - 1;
tptr.tm_mday = p->date;
tptr.tm_hour = p->hour;
tptr.tm_min = p->min;
tptr.tm_sec = p->sec;
tv.tv_sec = mktime(&tptr);
tv.tv_usec = 0;
settimeofday(&tv, NULL);
#else
RTC_DateTypeDef date;
RTC_TimeTypeDef time;
if(!p) {
return;
}
date.RTC_Year = p->year-2000;
date.RTC_Date = p->date;
date.RTC_Month= p->month;
time.RTC_Hours = p->hour;
time.RTC_Minutes= p->min;
time.RTC_Seconds= p->sec;
rtc_set_date_time(&date,&time);
#endif
}
void usr_print_current_time(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
log_printf(LOG_DEBUG,I_NOUSED,"time: %d.%d\n",tv.tv_sec,tv.tv_usec/1000);
}
uint32 usr_get_cur_time_ms(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec*1000+tv.tv_usec/1000);
// printf("time: %d.%d\n",tv.tv_sec,tv.tv_usec/1000);
}
void thread_setnamev(const char* pname)
{
prctl(PR_SET_NAME,pname,NULL,NULL,NULL);
}
#if 1
#include <stdarg.h>
static struct tm bkp_time={0};
int check_sec_change(void)
{
int ret=-1;
struct tm *tim;
struct tm *bk_tim;
static struct tm bkp_time={0};
time_t t_time;
t_time = time(NULL);
tim = localtime(&t_time);
if(tim->tm_sec != bkp_time.tm_sec){
bkp_time.tm_sec = tim->tm_sec;
//printf("sec=%d\n",tim->tm_sec);
ret = 1;
}
return ret;
}
void printf_wrapperV(const char* format, va_list args_list)
{
vprintf(format, args_list);
}
void usr_sec_printf(const char* format, ...)
{
va_list marker;
if(check_sec_change()>0)
{
va_start(marker, format);
printf_wrapperV(format, marker);
va_end(marker);
}
}
#endif
int pox_system(const char *cmd_line)
{
int ret = 0;
sighandler_t old_handler;
old_handler = signal(SIGCHLD, SIG_DFL);
ret = system(cmd_line);
signal(SIGCHLD, old_handler);
return ret;
}
int usr_set_sys_rtc_time(S_SYS_TIME *psys_time)
{
char cmd[100] = {0};
struct tm usr_tm;
struct tm *p_utc_tm;
int zone_offset = 0; // beijing +8
usr_tm.tm_year = psys_time->year;// 2022-1900
usr_tm.tm_mon = psys_time->month-1;
usr_tm.tm_mday = psys_time->date;
usr_tm.tm_hour = psys_time->hour;
usr_tm.tm_min = psys_time->min;
usr_tm.tm_sec = psys_time->sec;
time_t timestamp = mktime(&usr_tm) - (zone_offset*60*60);
p_utc_tm = localtime(&timestamp);
if(p_utc_tm==NULL)
return -1;
memset(cmd, 0, sizeof(cmd));
sprintf(cmd, "date -s '%d-%02d-%02d %02d:%02d:%02d'",
p_utc_tm->tm_year+1900,
p_utc_tm->tm_mon+1,
p_utc_tm->tm_mday,
p_utc_tm->tm_hour,
p_utc_tm->tm_min,
p_utc_tm->tm_sec);
log_printf(LOG_DEBUG,I_NOUSED,"cmd=%s\n", cmd);
system(cmd);
system("hwclock -w");
}
void usr_get_sys_rtc_time(S_SYS_TIME *psys_time,S_ADVANCE_DT_PARA *pdt_time)
{
time_t time_cur = 0;
struct tm tmBJ;
int BeiJing_zone_dffset = 8; //Beijing +8
memset(&tmBJ, 0, sizeof(struct tm));
time_cur = time(NULL);
utc2localtime(time_cur, &tmBJ, BeiJing_zone_dffset);
if(pdt_time!=NULL){
pdt_time->dd.RTC_Year = tmBJ.tm_year-1900;
pdt_time->dd.RTC_Month = tmBJ.tm_mon;
pdt_time->dd.RTC_Date = tmBJ.tm_mday;
pdt_time->tt.RTC_Hours = tmBJ.tm_hour;
pdt_time->tt.RTC_Minutes = tmBJ.tm_min;
pdt_time->tt.RTC_Seconds = tmBJ.tm_sec;
pdt_time->tt.RTC_H12 = 0;// 0-AM24h 1-PM12h*/
}
if(psys_time!=NULL){
psys_time->year = tmBJ.tm_year-1900;
psys_time->month= tmBJ.tm_mon;
psys_time->date = tmBJ.tm_mday;
psys_time->hour = tmBJ.tm_hour;
psys_time->min = tmBJ.tm_min;
psys_time->sec = tmBJ.tm_sec;
}
}
#if 1
extern char release_ver[30];
enum
{
RUN_INFO_LOG_STEP_START=0,
RUN_INFO_LOG_TOTAL_CPU_STEP,
RUN_INFO_LOG_TOTAL_MEM_STEP,
RUN_INFO_LOG_TOTAL_DISK_STEP,
RUN_INFO_LOG_RUNTIME_STEP,
RUN_INFO_LOG_APP_CPU_RAM_STEP,
RUN_INFO_LOG_STEP_END
};
static void hardware_log_info(unsigned char log_step)
{
int ret=0;
char shellCmd[300];
if(log_step >= RUN_INFO_LOG_STEP_END)
{
return;
}
switch(log_step)
{
case RUN_INFO_LOG_STEP_START:
//version
//usr_normal_printf("log imx-app-ver:%s\n",release_ver);
log_printf(LOG_INFO, I_RUN_STATUS, "log imx-app-ver:%s\n",release_ver);
break;
case RUN_INFO_LOG_TOTAL_CPU_STEP:
//cpu
memset(shellCmd,0,sizeof(shellCmd));
sprintf(shellCmd,"top -n 1 -b | grep CPU | head -n 1 >&1");//len=54
ret = pox_system(shellCmd);
if(ret<0)
log_printf(LOG_ERR, I_RUN_STATUS, "Err.shell cmd get total CPU err.\n");
break;
case RUN_INFO_LOG_TOTAL_MEM_STEP:
//mem
memset(shellCmd,0,sizeof(shellCmd));
sprintf(shellCmd,"top -n 1 -b | grep Mem | head -n 1 >&1");
ret = pox_system(shellCmd);
if(ret<0)
log_printf(LOG_ERR, I_RUN_STATUS, "Err.shell cmd get total Mem err.\n");
break;
case RUN_INFO_LOG_TOTAL_DISK_STEP:
//disk
memset(shellCmd,0,sizeof(shellCmd));
sprintf(shellCmd,"df -h | grep Size >&1");
ret = pox_system(shellCmd);
if(ret<0)
log_printf(LOG_ERR, I_RUN_STATUS, "shell cmd df err.\n");
memset(shellCmd,0,sizeof(shellCmd));
sprintf(shellCmd,"df -h | grep / | head -n 1 >&1");
ret = pox_system(shellCmd);
if(ret<0)
log_printf(LOG_ERR, I_RUN_STATUS, "Err.shell cmd df err.\n");
//usr_normal_printf("Err.shell cmd df err.\n");
break;
case RUN_INFO_LOG_RUNTIME_STEP:
//runtime
memset(shellCmd,0,sizeof(shellCmd));
sprintf(shellCmd,"uptime >&1");
ret = pox_system(shellCmd);
if(ret<0)
log_printf(LOG_ERR, I_RUN_STATUS, "Err.shell cmd uptime err.\n");
break;
case RUN_INFO_LOG_APP_CPU_RAM_STEP:
//app cpu/ram
memset(shellCmd,0,sizeof(shellCmd));
sprintf(shellCmd,"top -n 1 -b | grep VSZ | head -n 1 >&1");
ret = pox_system(shellCmd);
if(ret<0)
log_printf(LOG_ERR, I_RUN_STATUS, "Err.shell cmd uptime err.\n");
memset(shellCmd,0,sizeof(shellCmd));
sprintf(shellCmd,"top -n 1 -b | grep imx_atsc_app | head -n 1 >&1");
ret = pox_system(shellCmd);
if(ret<0)
log_printf(LOG_ERR, I_RUN_STATUS, "Err.shell cmd uptime err.\n");
memset(shellCmd,0,sizeof(shellCmd));
sprintf(shellCmd,"top -n 1 -b | grep http | head -n 1 >&1");
ret = pox_system(shellCmd);
if(ret<0)
log_printf(LOG_ERR, I_RUN_STATUS, "Err.shell cmd uptime err.\n");
break;
default:break;
}
}
void check_and_load_bkppara_to_cfgfile()
{
u8 ret;
char shellCmd[100] = {0};
//cfg
ret = check_file_exist(F_EXC_CFG_PARA_FILE);
if(ret==0)
log_printf(LOG_INFO, I_RUN_STATUS, "cfg_file exist\n");
else
log_printf(LOG_INFO, I_RUN_STATUS, "cfg_file is non-exist\n");
//bkp
ret = check_file_exist(F_EXC_CFG_PARA_BKP_FILE);
if(ret==0){
//memset(shellCmd,0,sizeof(shellCmd));
//sprintf(shellCmd,"%s");//len=54
ret = pox_system(LOAD_PARA_TO_CFG_FILE);
//ret = executeCMD(LOAD_PARA_TO_CFG_FILE, msg);
if(ret==0)
log_printf(LOG_INFO, I_RUN_STATUS, "bkp_file is load to cfg_file\n");
else
log_printf(LOG_ERR, I_RUN_STATUS, "bkp_file failed load to cfg_file\n");
}else{
log_printf(LOG_WARN, I_RUN_STATUS, "bkp_file is non-existent\n");
}
}
static void sync_cfgpara_to_bkpfile()
{
u8 ret;
char msg[20] = {0};
ret = check_file_exist(F_EXC_CFG_PARA_FILE);
if(ret==0 && (E2PROM_MAGIC_WORD==c256_byteread(EEPROM_DEFAULT_PARAM_ADDR))){
ret = pox_system(SYNC_PARA_TO_BKP_FILE);
//ret = executeCMD(SYNC_PARA_TO_BKP_FILE, msg);
if(ret==0)
log_printf(LOG_INFO, I_RUN_STATUS, "cfg file is sync to bkp file\n");
else
log_printf(LOG_ERR, I_RUN_STATUS, "cfg file failed sync to bkp file\n");
}else{
log_printf(LOG_WARN, I_RUN_STATUS, "cfg file is non-existent\n");
}
}
void syslog_thr(void * pvParameters)
{
static int sec_cnt=0;
static char log_record_flg=0;
static char log_step=RUN_INFO_LOG_STEP_START;
static char boot_flg=1;
log_printf(LOG_INFO, I_NOUSED, "===syslog_thr start===\n");
while(1)
{
sec_cnt++;
// 2hour || boot 2min
if( (sec_cnt%(2*60*60)==0) || (sec_cnt>=120 && boot_flg) ){
boot_flg=0;
log_record_flg=1;
}
if(log_record_flg)
{
if(log_step >= RUN_INFO_LOG_STEP_END){
log_record_flg = 0;
log_step = RUN_INFO_LOG_STEP_START;
//usr_normal_printf("Sta.finish record run info to log.\n");
log_printf(LOG_INFO, I_RUN_STATUS, "Sta.finish record run info to log.\n");
sync_cfgpara_to_bkpfile();
}
else{
hardware_log_info(log_step);
log_step++;
}
}
sleep(1);
}
}
#endif
#endif//verify_sys_com
+1119
View File
File diff suppressed because it is too large Load Diff
+169
View File
@@ -0,0 +1,169 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <poll.h>
#include <sys/select.h>
#include <sys/time.h>
#include <linux/ioctl.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>
#include "../inc/key_gpio.h"
#include "../inc/config.h"
#include "../inc/igpio.h"
#define _DEBUG_KEY_PR_ 0
#if _DEBUG_KEY_PR_
#define PR_KEY(...) printf(__VA_ARGS__)
#else
#define PR_KEY(...)
#endif
#define KEY_MAX_NUM 6
const uint32 c_key_gpio_arry[KEY_MAX_NUM]=
{
KEY1_GP_NUM, // LEFT
KEY2_GP_NUM, // RIGHT ---> UP
KEY3_GP_NUM, // UP
KEY4_GP_NUM, // DOWN
KEY5_GP_NUM, // EXIT
KEY6_GP_NUM, // OK
};
uint8 pr_key(uint8 keyvalue)
{
uint8 key = KEY_NULL;
switch(keyvalue)
{
case 2:
key=KEY_RIGHT;
break;
case 1:
key=KEY_LEFT;
break;
case 4:
key=KEY_UP;
break;
case 8:
key=KEY_DOWN;
break;
case 16:
key=KEY_ENTER;
break;
case 14:
key=KEY_RIGHT_UP_DOWN;
break;
case 12:
key=KEY_UP_DOWN;
break;
case 3:
key=KEY_LEFT_RIGHT;
break;
case 13:
key=KEY_LEFT_UP_DOWN;
break;
case 32:
key=KEY_EXIT;
break;
default:
break;
}
return key;
}
uint8 get_key_value(void)
{
uint8 key = KEY_NULL;
uint8 i=0;
uint8 high_level=0;
uint8 tmpval=0;
for(i = 0; i < KEY_MAX_NUM; i++)
{
get_gpio_value(c_key_gpio_arry[i], &high_level);
if(high_level)
{
tmpval &= ~(uint8)(1 << i);
}
else
{
tmpval |= (uint8)(1 << i);
}
}
key = pr_key(tmpval);
if(key != KEY_NULL) {
PR_KEY("\n======KeyVal=%d======\n\n", tmpval);
}
return key;
}
uint8 printf_key_longpress(uint8 tmpval)
{
uint8 key = KEY_NULL;
switch(tmpval)
{
case 1:
key=KEY_LEFT;
PR_KEY("\n long press LEFT\n\n");
break;
case 2:
key=KEY_RIGHT;
PR_KEY("\n long press RIGHT\n\n");
break;
case 4:
key=KEY_UP;
PR_KEY("\n long press UP\n\n");
break;
case 8:
key=KEY_DOWN;
PR_KEY("\n long press DOWN\n\n");
break;
default:
break;
}
return key;
}
char key_value[] = {0x01,0x02,0x04,0x08,0x10,0x20};
char key_value_index[] = {0,0,1,0,2,0,0,0,3};
uint8 check_long_press(uint8 key)
{
int i = 0;
uint8 high_level = 0;
if(key == key_value[2] || key == key_value[3]) {
i = key_value_index[key];
get_gpio_value(c_key_gpio_arry[i], &high_level);
return high_level ? 0 : 1;
}
return 0;
}
uint8 get_long_press(void)
{
uint8 key = 0x00;
uint8 high_level = 0;
int i = 0;
for(i=0; i<4; i++) {
get_gpio_value(c_key_gpio_arry[i], &high_level);
if(high_level==0) {
key += key_value[i];
}
}
return printf_key_longpress(key);
}
+306
View File
@@ -0,0 +1,306 @@
#include <string.h>
#include <stdio.h>
#include "../inc/type.h"
#include "../inc/config.h"
#include "../inc/lcd.h"
#include "../inc/os_task.h"
#include "../inc/delay.h"
#include "../inc/isys_com.h"
#if USE_LCD_CACHE
static char lcd_cache[2][42];
#endif
#define LCD_HW_SUPPORT_CHAR_TYPE 88
uint8 lcd_show_char[LCD_HW_SUPPORT_CHAR_TYPE] =
{
'!','$','$','%','&','(',')','*','+',',','-','.',
'0','1','2','3','4','5','6','7','8','9',':',';','<','=','>','?',
'@','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
'P','Q','R','S','T','U','V','W','X','Y','Z','[',']','^','_',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',
'q','r','s','t','u','v','w','x','y','z','{','|','}'
};
const uint32 c_lcd_gpio_arry[8]=
{
LCD_D0_GP_NUM,
LCD_D1_GP_NUM,
LCD_D2_GP_NUM,
LCD_D3_GP_NUM,
LCD_D4_GP_NUM,
LCD_D5_GP_NUM,
LCD_D6_GP_NUM,
LCD_D7_GP_NUM
};
static void LCD_EN_SET(void)
{
set_gpio_value(LCD_E_GP_NUM, E_IO_H);
}
static void LCD_EN_CLEAR(void)
{
set_gpio_value(LCD_E_GP_NUM, E_IO_L);
}
static void LCD_RW_CLEAR(void)
{
set_gpio_value(LCD_RW_GP_NUM, E_IO_L);
}
static void LCD_RS_SET(void)
{
set_gpio_value(LCD_RS_GP_NUM, E_IO_H);
}
static void LCD_RS_CLEAR(void)
{
set_gpio_value(LCD_RS_GP_NUM, E_IO_L);
}
static void LCD_EN_Write(void)
{
LCD_EN_SET();
delay_us(20);
LCD_EN_CLEAR();
delay_us(20);
}
static void Lcd_Write_Data(uint8 LCD_DATA)
{
uint16 data;
uint8 temp;
uint8 i;
LCD_RW_CLEAR();
LCD_RS_SET();
for (i = 0; i < 8; i++)
{
temp = (LCD_DATA >> i) & 0x01;
set_gpio_value(c_lcd_gpio_arry[i], (E_IO_VAL)temp);
}
LCD_EN_Write();
}
static void Lcd_Write_Cmd(uint8 LCD_CMD)
{
uint16 data;
uint8 temp;
uint8 i;
LCD_RW_CLEAR();
LCD_RS_CLEAR();
for (i = 0; i < 8; i++)
{
temp = (LCD_CMD >> i) & 0x01;
set_gpio_value(c_lcd_gpio_arry[i], (E_IO_VAL)temp);
}
LCD_EN_Write();
}
#define LCD_INIT_TIME 3//
void lcd_init(void)
{
int i = 0;
log_printf(LOG_INFO, I_SYS_STATUS, "init lcd display.\n");
do {
//delay_ms(150);
Lcd_Write_Cmd(0x38);
delay_ms(1);
Lcd_Write_Cmd(0x0c);
delay_ms(1);
Lcd_Write_Cmd(0x01);
delay_ms(500);
}while(++i<LCD_INIT_TIME);
log_printf(LOG_INFO, I_SYS_STATUS, "over. init lcd display.\n");
}
static void LCD_SET_XY(uint8 x,uint8 y)
{
if(x==0)
{
Lcd_Write_Cmd(0x80+y);
}
else
{
Lcd_Write_Cmd(0xc0+y);
}
}
void lcd_write_arrow(uint32 x,uint32 y,ARROW_DIRECTON direct)
{
#if USE_LCD_CACHE
if(direct==ARROW_LEFT)
{
lcd_cache[x][y]=0x7f;
#if ARROW_CHAR_NUM==2
lcd_cache[x][y+1]=0x2d;
#endif
}
else if(direct==ARROW_RIGHT)
{
#if ARROW_CHAR_NUM==2
lcd_cache[x][y]=0x2d;
lcd_cache[x][y+1]=0x7e;
#else
lcd_cache[x][y]=0x7e;
#endif
}
#else
if(direct==ARROW_LEFT)
{
LCD_SET_XY(x,y);
delay_us(5);
Lcd_Write_Data(0x7f);
#if ARROW_CHAR_NUM==2
Lcd_Write_Data(0x2D);
#endif
}
else if(direct==ARROW_RIGHT)
{
LCD_SET_XY(x,y);
delay_us(5);
#if ARROW_CHAR_NUM==2
Lcd_Write_Data(0x2D);
#endif
Lcd_Write_Data(0x7e);
}
#endif
}
void lcd_write_temp_f(uint8 x,uint8 y)
{
#if USE_LCD_CACHE
if((x==LINE_1)||(x==LINE_2))
{
lcd_cache[x][y]=0xdf;
lcd_cache[x][y+1]='F';
}
#else
LCD_SET_XY(x,y);
delay_us(5);
Lcd_Write_Data(0xdf);
Lcd_Write_Data('F');
#endif
}
void lcd_write_temp_c(uint8 x,uint8 y)
{
#if USE_LCD_CACHE
if((x==LINE_1)||(x==LINE_2))
{
lcd_cache[x][y]=0xdf;
lcd_cache[x][y+1]='C';
}
#else
LCD_SET_XY(x,y);
delay_us(5);
Lcd_Write_Data(0xdf);
Lcd_Write_Data('C');
#endif
}
void lcd_write_string(uint8 X,uint8 Y,void *s)
{
#if USE_LCD_CACHE
uint32 i = 0;
char *pstr = (char*)s;
if(1)//sys_ready
{
if(X==LINE_1)
{
i = strlen((const char *)pstr)>LCD_CHAR_NUM-Y?LCD_CHAR_NUM-Y:strlen((const char *)pstr);
memcpy(&lcd_cache[0][Y],pstr,i);
}
if(X==LINE_2)
{
i = strlen((const char *)pstr)>LCD_CHAR_NUM-Y?LCD_CHAR_NUM-Y:strlen((const char *)pstr);
memcpy(&lcd_cache[1][Y],pstr,i);
}
}
else
{
LCD_SET_XY( X, Y );
delay_us(10);
while(*pstr)
{
Lcd_Write_Data(*pstr++);
}
}
#else
char *pstr = (char*)s;
LCD_SET_XY( X, Y );
delay_us(10);
while(*pstr)
{
Lcd_Write_Data(*pstr++);
}
#endif
}
void lcd_write_cache(void)
{
#if USE_LCD_CACHE
uint32 i = 0;
if(1)//sys_ready
{
LCD_SET_XY(0,0);
delay_us(10);
for(i=0;i<LCD_CHAR_NUM;i++)
{
Lcd_Write_Data(lcd_cache[0][i]);
}
LCD_SET_XY(1,0);
delay_us(10);
for(i=0;i<LCD_CHAR_NUM;i++)
{
Lcd_Write_Data(lcd_cache[1][i]);
}
}
#else
#endif
}
/***********************************/
// LCD backup light control
/***********************************/
//turn on the light
void light_on(void)
{
//log_printf(LOG_INFO, I_SYS_STATUS, "Turn on LCD light\n");
set_gpio_value(LCD_CTL_GP_NUM, E_IO_H);
}
void light_off(void)
{
log_printf(LOG_INFO, I_SYS_STATUS, "Turn off LCD light\n");
set_gpio_value(LCD_CTL_GP_NUM, E_IO_L);
}
void set_gpio_fpga_reset(uint8 set_val)
{
if(set_val==1){
printf("set_gpio_fpga_reset High\n");
set_gpio_value(FPGA_RST_GP_NUM, E_IO_H);
}
else{
printf("set_gpio_fpga_reset Low\n");
set_gpio_value(FPGA_RST_GP_NUM, E_IO_L);
}
}
/**********************************************END**************************************/
+876
View File
@@ -0,0 +1,876 @@
/* minIni - Multi-Platform INI file parser, suitable for embedded systems
*
* These routines are in part based on the article "Multiplatform .INI Files"
* by Joseph J. Graf in the March 1994 issue of Dr. Dobb's Journal.
*
* Copyright (c) CompuPhase, 2008-2017
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Version: $Id: minIni.c 53 2015-01-18 13:35:11Z thiadmer.riemersma@gmail.com $
*/
#if (defined _UNICODE || defined __UNICODE__ || defined UNICODE) && !defined INI_ANSIONLY
# if !defined UNICODE /* for Windows */
# define UNICODE
# endif
# if !defined _UNICODE /* for C library */
# define _UNICODE
# endif
#endif
#define MININI_IMPLEMENTATION
#include "../inc/minIni.h"
#if defined NDEBUG
#define assert(e)
#else
#include <assert.h>
#endif
#if !defined __T || defined INI_ANSIONLY
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define TCHAR char
#define __T(s) s
#define _tcscat strcat
#define _tcschr strchr
#define _tcscmp strcmp
#define _tcscpy strcpy
#define _tcsicmp stricmp
#define _tcslen strlen
#define _tcsncmp strncmp
#define _tcsnicmp strnicmp
#define _tcsrchr strrchr
#define _tcstol strtol
#define _tcstod strtod
#define _totupper toupper
#define _stprintf sprintf
#define _tfgets fgets
#define _tfputs fputs
#define _tfopen fopen
#define _tremove remove
#define _trename rename
#endif
#if defined __linux || defined __linux__
#define __LINUX__
#elif defined FREEBSD && !defined __FreeBSD__
#define __FreeBSD__
#elif defined(_MSC_VER)
#pragma warning(disable: 4996) /* for Microsoft Visual C/C++ */
#endif
#if !defined strnicmp && !defined PORTABLE_STRNICMP
#if defined __LINUX__ || defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__
#define strnicmp strncasecmp
#endif
#endif
#if !defined _totupper
#define _totupper toupper
#endif
#if !defined INI_LINETERM
#if defined __LINUX__ || defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__
#define INI_LINETERM __T("\n")
#else
#define INI_LINETERM __T("\r\n")
#endif
#endif
#if !defined INI_FILETYPE
#error Missing definition for INI_FILETYPE.
#endif
#if !defined sizearray
#define sizearray(a) (sizeof(a) / sizeof((a)[0]))
#endif
enum quote_option {
QUOTE_NONE,
QUOTE_ENQUOTE,
QUOTE_DEQUOTE,
};
#if defined PORTABLE_STRNICMP
int strnicmp(const TCHAR *s1, const TCHAR *s2, size_t n)
{
while (n-- != 0 && (*s1 || *s2)) {
register int c1, c2;
c1 = *s1++;
if ('a' <= c1 && c1 <= 'z')
c1 += ('A' - 'a');
c2 = *s2++;
if ('a' <= c2 && c2 <= 'z')
c2 += ('A' - 'a');
if (c1 != c2)
return c1 - c2;
} /* while */
return 0;
}
#endif /* PORTABLE_STRNICMP */
static TCHAR *skipleading(const TCHAR *str)
{
assert(str != NULL);
while ('\0' < *str && *str <= ' ')
str++;
return (TCHAR *)str;
}
static TCHAR *skiptrailing(const TCHAR *str, const TCHAR *base)
{
assert(str != NULL);
assert(base != NULL);
while (str > base && '\0' < *(str-1) && *(str-1) <= ' ')
str--;
return (TCHAR *)str;
}
static TCHAR *striptrailing(TCHAR *str)
{
TCHAR *ptr = skiptrailing(_tcschr(str, '\0'), str);
assert(ptr != NULL);
*ptr = '\0';
return str;
}
static TCHAR *ini_strncpy(TCHAR *dest, const TCHAR *source, size_t maxlen, enum quote_option option)
{
size_t d, s;
assert(maxlen>0);
assert(source != NULL && dest != NULL);
assert((dest < source || (dest == source && option != QUOTE_ENQUOTE)) || dest > source + strlen(source));
if (option == QUOTE_ENQUOTE && maxlen < 3)
option = QUOTE_NONE; /* cannot store two quotes and a terminating zero in less than 3 characters */
switch (option) {
case QUOTE_NONE:
for (d = 0; d < maxlen - 1 && source[d] != '\0'; d++)
dest[d] = source[d];
assert(d < maxlen);
dest[d] = '\0';
break;
case QUOTE_ENQUOTE:
d = 0;
dest[d++] = '"';
for (s = 0; source[s] != '\0' && d < maxlen - 2; s++, d++) {
if (source[s] == '"') {
if (d >= maxlen - 3)
break; /* no space to store the escape character plus the one that follows it */
dest[d++] = '\\';
} /* if */
dest[d] = source[s];
} /* for */
dest[d++] = '"';
dest[d] = '\0';
break;
case QUOTE_DEQUOTE:
for (d = s = 0; source[s] != '\0' && d < maxlen - 1; s++, d++) {
if ((source[s] == '"' || source[s] == '\\') && source[s + 1] == '"')
s++;
dest[d] = source[s];
} /* for */
dest[d] = '\0';
break;
default:
assert(0);
} /* switch */
return dest;
}
static TCHAR *cleanstring(TCHAR *string, enum quote_option *quotes)
{
int isstring;
TCHAR *ep;
assert(string != NULL);
assert(quotes != NULL);
/* Remove a trailing comment */
isstring = 0;
for (ep = string; *ep != '\0' && ((*ep != ';' && *ep != '#') || isstring); ep++) {
if (*ep == '"') {
if (*(ep + 1) == '"')
ep++; /* skip "" (both quotes) */
else
isstring = !isstring; /* single quote, toggle isstring */
} else if (*ep == '\\' && *(ep + 1) == '"') {
ep++; /* skip \" (both quotes */
} /* if */
} /* for */
assert(ep != NULL && (*ep == '\0' || *ep == ';' || *ep == '#'));
*ep = '\0'; /* terminate at a comment */
striptrailing(string);
/* Remove double quotes surrounding a value */
*quotes = QUOTE_NONE;
if (*string == '"' && (ep = _tcschr(string, '\0')) != NULL && *(ep - 1) == '"') {
string++;
*--ep = '\0';
*quotes = QUOTE_DEQUOTE; /* this is a string, so remove escaped characters */
} /* if */
return string;
}
static int getkeystring(INI_FILETYPE *fp, const TCHAR *Section, const TCHAR *Key,
int idxSection, int idxKey, TCHAR *Buffer, int BufferSize,
INI_FILEPOS *mark)
{
TCHAR *sp, *ep;
int len, idx;
enum quote_option quotes;
TCHAR LocalBuffer[INI_BUFFERSIZE];
assert(fp != NULL);
/* Move through file 1 line at a time until a section is matched or EOF. If
* parameter Section is NULL, only look at keys above the first section. If
* idxSection is postive, copy the relevant section name.
*/
len = (Section != NULL) ? (int)_tcslen(Section) : 0;
if (len > 0 || idxSection >= 0) {
assert(idxSection >= 0 || Section != NULL);
idx = -1;
do {
if (!ini_read(LocalBuffer, INI_BUFFERSIZE, fp))
return 0;
sp = skipleading(LocalBuffer);
ep = _tcsrchr(sp, ']');
} while (*sp != '[' || ep == NULL ||
(((int)(ep-sp-1) != len || Section == NULL || _tcsnicmp(sp+1,Section,len) != 0) && ++idx != idxSection));
if (idxSection >= 0) {
if (idx == idxSection) {
assert(ep != NULL);
assert(*ep == ']');
*ep = '\0';
ini_strncpy(Buffer, sp + 1, BufferSize, QUOTE_NONE);
return 1;
} /* if */
return 0; /* no more section found */
} /* if */
} /* if */
/* Now that the section has been found, find the entry.
* Stop searching upon leaving the section's area.
*/
assert(Key != NULL || idxKey >= 0);
len = (Key != NULL) ? (int)_tcslen(Key) : 0;
idx = -1;
do {
if (mark != NULL)
ini_tell(fp, mark); /* optionally keep the mark to the start of the line */
if (!ini_read(LocalBuffer,INI_BUFFERSIZE,fp) || *(sp = skipleading(LocalBuffer)) == '[')
return 0;
sp = skipleading(LocalBuffer);
ep = _tcschr(sp, '='); /* Parse out the equal sign */
if (ep == NULL)
ep = _tcschr(sp, ':');
} while (*sp == ';' || *sp == '#' || ep == NULL
|| ((len == 0 || (int)(skiptrailing(ep,sp)-sp) != len || _tcsnicmp(sp,Key,len) != 0) && ++idx != idxKey));
if (idxKey >= 0) {
if (idx == idxKey) {
assert(ep != NULL);
assert(*ep == '=' || *ep == ':');
*ep = '\0';
striptrailing(sp);
ini_strncpy(Buffer, sp, BufferSize, QUOTE_NONE);
return 1;
} /* if */
return 0; /* no more key found (in this section) */
} /* if */
/* Copy up to BufferSize chars to buffer */
assert(ep != NULL);
assert(*ep == '=' || *ep == ':');
sp = skipleading(ep + 1);
sp = cleanstring(sp, &quotes); /* Remove a trailing comment */
ini_strncpy(Buffer, sp, BufferSize, quotes);
return 1;
}
/** ini_gets()
* \param Section the name of the section to search for
* \param Key the name of the entry to find the value of
* \param DefValue default string in the event of a failed read
* \param Buffer a pointer to the buffer to copy into
* \param BufferSize the maximum number of characters to copy
* \param Filename the name and full path of the .ini file to read from
*
* \return the number of characters copied into the supplied buffer
*/
int ini_gets(const TCHAR *Section, const TCHAR *Key, const TCHAR *DefValue,
TCHAR *Buffer, int BufferSize, const TCHAR *Filename)
{
INI_FILETYPE fp;
int ok = 0;
if (Buffer == NULL || BufferSize <= 0 || Key == NULL)
return 0;
if (ini_openread(Filename, &fp)) {
ok = getkeystring(&fp, Section, Key, -1, -1, Buffer, BufferSize, NULL);
(void)ini_close(&fp);
} /* if */
if (!ok)
ini_strncpy(Buffer, (DefValue != NULL) ? DefValue : __T(""), BufferSize, QUOTE_NONE);
return (int)_tcslen(Buffer);
}
/** ini_getl()
* \param Section the name of the section to search for
* \param Key the name of the entry to find the value of
* \param DefValue the default value in the event of a failed read
* \param Filename the name of the .ini file to read from
*
* \return the value located at Key
*/
long ini_getl(const TCHAR *Section, const TCHAR *Key, long DefValue, const TCHAR *Filename)
{
TCHAR LocalBuffer[64] = {0};
int len = ini_gets(Section, Key, __T(""), LocalBuffer, sizearray(LocalBuffer), Filename);
return (len == 0) ? DefValue
: ((len >= 2 && _totupper((int)LocalBuffer[1]) == 'X') ? _tcstol(LocalBuffer, NULL, 16)
: _tcstol(LocalBuffer, NULL, 10));
}
#if defined INI_REAL
/** ini_getf()
* \param Section the name of the section to search for
* \param Key the name of the entry to find the value of
* \param DefValue the default value in the event of a failed read
* \param Filename the name of the .ini file to read from
*
* \return the value located at Key
*/
INI_REAL ini_getf(const TCHAR *Section, const TCHAR *Key, INI_REAL DefValue, const TCHAR *Filename)
{
TCHAR LocalBuffer[64];
int len = ini_gets(Section, Key, __T(""), LocalBuffer, sizearray(LocalBuffer), Filename);
return (len == 0) ? DefValue : ini_atof(LocalBuffer);
}
#endif
/** ini_getbool()
* \param Section the name of the section to search for
* \param Key the name of the entry to find the value of
* \param DefValue default value in the event of a failed read; it should
* zero (0) or one (1).
* \param Filename the name and full path of the .ini file to read from
*
* A true boolean is found if one of the following is matched:
* - A string starting with 'y' or 'Y'
* - A string starting with 't' or 'T'
* - A string starting with '1'
*
* A false boolean is found if one of the following is matched:
* - A string starting with 'n' or 'N'
* - A string starting with 'f' or 'F'
* - A string starting with '0'
*
* \return the true/false flag as interpreted at Key
*/
int ini_getbool(const TCHAR *Section, const TCHAR *Key, int DefValue, const TCHAR *Filename)
{
TCHAR LocalBuffer[2] = __T("");
int ret;
ini_gets(Section, Key, __T(""), LocalBuffer, sizearray(LocalBuffer), Filename);
LocalBuffer[0] = (TCHAR)_totupper((int)LocalBuffer[0]);
if (LocalBuffer[0] == 'Y' || LocalBuffer[0] == '1' || LocalBuffer[0] == 'T')
ret = 1;
else if (LocalBuffer[0] == 'N' || LocalBuffer[0] == '0' || LocalBuffer[0] == 'F')
ret = 0;
else
ret = DefValue;
return(ret);
}
/** ini_getsection()
* \param idx the zero-based sequence number of the section to return
* \param Buffer a pointer to the buffer to copy into
* \param BufferSize the maximum number of characters to copy
* \param Filename the name and full path of the .ini file to read from
*
* \return the number of characters copied into the supplied buffer
*/
int ini_getsection(int idx, TCHAR *Buffer, int BufferSize, const TCHAR *Filename)
{
INI_FILETYPE fp;
int ok = 0;
if (Buffer == NULL || BufferSize <= 0 || idx < 0)
return 0;
if (ini_openread(Filename, &fp)) {
ok = getkeystring(&fp, NULL, NULL, idx, -1, Buffer, BufferSize, NULL);
(void)ini_close(&fp);
} /* if */
if (!ok)
*Buffer = '\0';
return (int)_tcslen(Buffer);
}
/** ini_getkey()
* \param Section the name of the section to browse through, or NULL to
* browse through the keys outside any section
* \param idx the zero-based sequence number of the key to return
* \param Buffer a pointer to the buffer to copy into
* \param BufferSize the maximum number of characters to copy
* \param Filename the name and full path of the .ini file to read from
*
* \return the number of characters copied into the supplied buffer
*/
int ini_getkey(const TCHAR *Section, int idx, TCHAR *Buffer, int BufferSize, const TCHAR *Filename)
{
INI_FILETYPE fp;
int ok = 0;
if (Buffer == NULL || BufferSize <= 0 || idx < 0)
return 0;
if (ini_openread(Filename, &fp)) {
ok = getkeystring(&fp, Section, NULL, -1, idx, Buffer, BufferSize, NULL);
(void)ini_close(&fp);
} /* if */
if (!ok)
*Buffer = '\0';
return (int)_tcslen(Buffer);
}
#if !defined INI_NOBROWSE
/** ini_browse()
* \param Callback a pointer to a function that will be called for every
* setting in the INI file.
* \param UserData arbitrary data, which the function passes on the the
* \c Callback function
* \param Filename the name and full path of the .ini file to read from
*
* \return 1 on success, 0 on failure (INI file not found)
*
* \note The \c Callback function must return 1 to continue
* browsing through the INI file, or 0 to stop. Even when the
* callback stops the browsing, this function will return 1
* (for success).
*/
int ini_browse(INI_CALLBACK Callback, void *UserData, const TCHAR *Filename)
{
TCHAR LocalBuffer[INI_BUFFERSIZE];
int lenSec, lenKey;
enum quote_option quotes;
INI_FILETYPE fp;
if (Callback == NULL)
return 0;
if (!ini_openread(Filename, &fp))
return 0;
LocalBuffer[0] = '\0'; /* copy an empty section in the buffer */
lenSec = (int)_tcslen(LocalBuffer) + 1;
for ( ;; ) {
TCHAR *sp, *ep;
if (!ini_read(LocalBuffer + lenSec, INI_BUFFERSIZE - lenSec, &fp))
break;
sp = skipleading(LocalBuffer + lenSec);
/* ignore empty strings and comments */
if (*sp == '\0' || *sp == ';' || *sp == '#')
continue;
/* see whether we reached a new section */
ep = _tcsrchr(sp, ']');
if (*sp == '[' && ep != NULL) {
*ep = '\0';
ini_strncpy(LocalBuffer, sp + 1, INI_BUFFERSIZE, QUOTE_NONE);
lenSec = (int)_tcslen(LocalBuffer) + 1;
continue;
} /* if */
/* not a new section, test for a key/value pair */
ep = _tcschr(sp, '='); /* test for the equal sign or colon */
if (ep == NULL)
ep = _tcschr(sp, ':');
if (ep == NULL)
continue; /* invalid line, ignore */
*ep++ = '\0'; /* split the key from the value */
striptrailing(sp);
ini_strncpy(LocalBuffer + lenSec, sp, INI_BUFFERSIZE - lenSec, QUOTE_NONE);
lenKey = (int)_tcslen(LocalBuffer + lenSec) + 1;
/* clean up the value */
sp = skipleading(ep);
sp = cleanstring(sp, &quotes); /* Remove a trailing comment */
ini_strncpy(LocalBuffer + lenSec + lenKey, sp, INI_BUFFERSIZE - lenSec - lenKey, quotes);
/* call the callback */
if (!Callback(LocalBuffer, LocalBuffer + lenSec, LocalBuffer + lenSec + lenKey, UserData))
break;
} /* for */
(void)ini_close(&fp);
return 1;
}
#endif /* INI_NOBROWSE */
#if ! defined INI_READONLY
static void ini_tempname(TCHAR *dest, const TCHAR *source, int maxlength)
{
TCHAR *p;
ini_strncpy(dest, source, maxlength, QUOTE_NONE);
p = _tcsrchr(dest, '\0');
assert(p != NULL);
*(p - 1) = '~';
}
static enum quote_option check_enquote(const TCHAR *Value)
{
const TCHAR *p;
/* run through the value, if it has trailing spaces, or '"', ';' or '#'
* characters, enquote it
*/
assert(Value != NULL);
for (p = Value; *p != '\0' && *p != '"' && *p != ';' && *p != '#'; p++)
/* nothing */;
return (*p != '\0' || (p > Value && *(p - 1) == ' ')) ? QUOTE_ENQUOTE : QUOTE_NONE;
}
static void writesection(TCHAR *LocalBuffer, const TCHAR *Section, INI_FILETYPE *fp)
{
if (Section != NULL && _tcslen(Section) > 0) {
TCHAR *p;
LocalBuffer[0] = '[';
ini_strncpy(LocalBuffer + 1, Section, INI_BUFFERSIZE - 4, QUOTE_NONE); /* -1 for '[', -1 for ']', -2 for '\r\n' */
p = _tcsrchr(LocalBuffer, '\0');
assert(p != NULL);
*p++ = ']';
_tcscpy(p, INI_LINETERM); /* copy line terminator (typically "\n") */
if (fp != NULL)
(void)ini_write(LocalBuffer, fp);
} /* if */
}
static void writekey(TCHAR *LocalBuffer, const TCHAR *Key, const TCHAR *Value, INI_FILETYPE *fp)
{
TCHAR *p;
enum quote_option option = check_enquote(Value);
ini_strncpy(LocalBuffer, Key, INI_BUFFERSIZE - 3, QUOTE_NONE); /* -1 for '=', -2 for '\r\n' */
p = _tcsrchr(LocalBuffer, '\0');
assert(p != NULL);
*p++ = '=';
ini_strncpy(p, Value, INI_BUFFERSIZE - (p - LocalBuffer) - 2, option); /* -2 for '\r\n' */
p = _tcsrchr(LocalBuffer, '\0');
assert(p != NULL);
_tcscpy(p, INI_LINETERM); /* copy line terminator (typically "\n") */
if (fp != NULL)
(void)ini_write(LocalBuffer, fp);
}
static int cache_accum(const TCHAR *string, int *size, int max)
{
int len = (int)_tcslen(string);
if (*size + len >= max)
return 0;
*size += len;
return 1;
}
static int cache_flush(TCHAR *buffer, int *size,
INI_FILETYPE *rfp, INI_FILETYPE *wfp, INI_FILEPOS *mark)
{
int terminator_len = (int)_tcslen(INI_LINETERM);
int pos = 0;
(void)ini_seek(rfp, mark);
assert(buffer != NULL);
buffer[0] = '\0';
assert(size != NULL);
assert(*size <= INI_BUFFERSIZE);
while (pos < *size) {
(void)ini_read(buffer + pos, INI_BUFFERSIZE - pos, rfp);
while (pos < *size && buffer[pos] != '\0')
pos++; /* cannot use _tcslen() because buffer may not be zero-terminated */
} /* while */
if (buffer[0] != '\0') {
assert(pos > 0 && pos <= INI_BUFFERSIZE);
if (pos == INI_BUFFERSIZE)
pos--;
buffer[pos] = '\0'; /* force zero-termination (may be left unterminated in the above while loop) */
(void)ini_write(buffer, wfp);
}
ini_tell(rfp, mark); /* update mark */
*size = 0;
/* return whether the buffer ended with a line termination */
return (pos > terminator_len) && (_tcscmp(buffer + pos - terminator_len, INI_LINETERM) == 0);
}
static int close_rename(INI_FILETYPE *rfp, INI_FILETYPE *wfp, const TCHAR *filename, TCHAR *buffer)
{
(void)ini_close(rfp);
(void)ini_close(wfp);
(void)ini_remove(filename);
(void)ini_tempname(buffer, filename, INI_BUFFERSIZE);
(void)ini_rename(buffer, filename);
return 1;
}
/** ini_puts()
* \param Section the name of the section to write the string in
* \param Key the name of the entry to write, or NULL to erase all keys in the section
* \param Value a pointer to the buffer the string, or NULL to erase the key
* \param Filename the name and full path of the .ini file to write to
*
* \return 1 if successful, otherwise 0
*/
int ini_puts(const TCHAR *Section, const TCHAR *Key, const TCHAR *Value, const TCHAR *Filename)
{
INI_FILETYPE rfp;
INI_FILETYPE wfp;
INI_FILEPOS mark;
INI_FILEPOS head, tail;
TCHAR *sp, *ep;
TCHAR LocalBuffer[INI_BUFFERSIZE];
int len, match, flag, cachelen;
assert(Filename != NULL);
if (!ini_openread(Filename, &rfp)) {
/* If the .ini file doesn't exist, make a new file */
if (Key != NULL && Value != NULL) {
if (!ini_openwrite(Filename, &wfp))
return 0;
writesection(LocalBuffer, Section, &wfp);
writekey(LocalBuffer, Key, Value, &wfp);
(void)ini_close(&wfp);
} /* if */
return 1;
} /* if */
/* If parameters Key and Value are valid (so this is not an "erase" request)
* and the setting already exists, there are two short-cuts to avoid rewriting
* the INI file.
*/
if (Key != NULL && Value != NULL) {
ini_tell(&rfp, &mark);
match = getkeystring(&rfp, Section, Key, -1, -1, LocalBuffer, sizearray(LocalBuffer), &head);
if (match) {
/* if the current setting is identical to the one to write, there is
* nothing to do.
*/
if (_tcscmp(LocalBuffer,Value) == 0) {
(void)ini_close(&rfp);
return 1;
} /* if */
/* if the new setting has the same length as the current setting, and the
* glue file permits file read/write access, we can modify in place.
*/
#if defined ini_openrewrite
/* we already have the start of the (raw) line, get the end too */
ini_tell(&rfp, &tail);
/* create new buffer (without writing it to file) */
writekey(LocalBuffer, Key, Value, NULL);
if (_tcslen(LocalBuffer) == (size_t)(tail - head)) {
/* length matches, close the file & re-open for read/write, then
* write at the correct position
*/
(void)ini_close(&rfp);
if (!ini_openrewrite(Filename, &wfp))
return 0;
(void)ini_seek(&wfp, &head);
(void)ini_write(LocalBuffer, &wfp);
(void)ini_close(&wfp);
return 1;
} /* if */
#endif
} /* if */
/* key not found, or different value & length -> proceed (but rewind the
* input file first)
*/
(void)ini_seek(&rfp, &mark);
} /* if */
/* Get a temporary file name to copy to. Use the existing name, but with
* the last character set to a '~'.
*/
ini_tempname(LocalBuffer, Filename, INI_BUFFERSIZE);
if (!ini_openwrite(LocalBuffer, &wfp)) {
(void)ini_close(&rfp);
return 0;
} /* if */
(void)ini_tell(&rfp, &mark);
cachelen = 0;
/* Move through the file one line at a time until a section is
* matched or until EOF. Copy to temp file as it is read.
*/
len = (Section != NULL) ? (int)_tcslen(Section) : 0;
if (len > 0) {
do {
if (!ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp)) {
/* Failed to find section, so add one to the end */
flag = cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark);
if (Key!=NULL && Value!=NULL) {
if (!flag)
(void)ini_write(INI_LINETERM, &wfp); /* force a new line behind the last line of the INI file */
writesection(LocalBuffer, Section, &wfp);
writekey(LocalBuffer, Key, Value, &wfp);
} /* if */
return close_rename(&rfp, &wfp, Filename, LocalBuffer); /* clean up and rename */
} /* if */
/* Copy the line from source to dest, but not if this is the section that
* we are looking for and this section must be removed
*/
sp = skipleading(LocalBuffer);
ep = _tcsrchr(sp, ']');
match = (*sp == '[' && ep != NULL && (int)(ep-sp-1) == len && _tcsnicmp(sp + 1,Section,len) == 0);
if (!match || Key != NULL) {
if (!cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE)) {
cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark);
(void)ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp);
cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE);
} /* if */
} /* if */
} while (!match);
} /* if */
cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark);
/* when deleting a section, the section head that was just found has not been
* copied to the output file, but because this line was not "accumulated" in
* the cache, the position in the input file was reset to the point just
* before the section; this must now be skipped (again)
*/
if (Key == NULL) {
(void)ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp);
(void)ini_tell(&rfp, &mark);
} /* if */
/* Now that the section has been found, find the entry. Stop searching
* upon leaving the section's area. Copy the file as it is read
* and create an entry if one is not found.
*/
len = (Key != NULL) ? (int)_tcslen(Key) : 0;
for( ;; ) {
if (!ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp)) {
/* EOF without an entry so make one */
flag = cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark);
if (Key!=NULL && Value!=NULL) {
if (!flag)
(void)ini_write(INI_LINETERM, &wfp); /* force a new line behind the last line of the INI file */
writekey(LocalBuffer, Key, Value, &wfp);
} /* if */
return close_rename(&rfp, &wfp, Filename, LocalBuffer); /* clean up and rename */
} /* if */
sp = skipleading(LocalBuffer);
ep = _tcschr(sp, '='); /* Parse out the equal sign */
if (ep == NULL)
ep = _tcschr(sp, ':');
match = (ep != NULL && len > 0 && (int)(skiptrailing(ep,sp)-sp) == len && _tcsnicmp(sp,Key,len) == 0);
if ((Key != NULL && match) || *sp == '[')
break; /* found the key, or found a new section */
/* copy other keys in the section */
if (Key == NULL) {
(void)ini_tell(&rfp, &mark); /* we are deleting the entire section, so update the read position */
} else {
if (!cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE)) {
cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark);
(void)ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp);
cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE);
} /* if */
} /* if */
} /* for */
/* the key was found, or we just dropped on the next section (meaning that it
* wasn't found); in both cases we need to write the key, but in the latter
* case, we also need to write the line starting the new section after writing
* the key
*/
flag = (*sp == '[');
cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark);
if (Key != NULL && Value != NULL)
writekey(LocalBuffer, Key, Value, &wfp);
/* cache_flush() reset the "read pointer" to the start of the line with the
* previous key or the new section; read it again (because writekey() destroyed
* the buffer)
*/
(void)ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp);
if (flag) {
/* the new section heading needs to be copied to the output file */
cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE);
} else {
/* forget the old key line */
(void)ini_tell(&rfp, &mark);
} /* if */
/* Copy the rest of the INI file */
while (ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp)) {
if (!cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE)) {
cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark);
(void)ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp);
cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE);
} /* if */
} /* while */
cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark);
return close_rename(&rfp, &wfp, Filename, LocalBuffer); /* clean up and rename */
}
/* Ansi C "itoa" based on Kernighan & Ritchie's "Ansi C" book. */
#define ABS(v) ((v) < 0 ? -(v) : (v))
static void strreverse(TCHAR *str)
{
int i, j;
for (i = 0, j = (int)_tcslen(str) - 1; i < j; i++, j--) {
TCHAR t = str[i];
str[i] = str[j];
str[j] = t;
} /* for */
}
static void long2str(long value, TCHAR *str)
{
int i = 0;
long sign = value;
/* generate digits in reverse order */
do {
int n = (int)(value % 10); /* get next lowest digit */
str[i++] = (TCHAR)(ABS(n) + '0'); /* handle case of negative digit */
} while (value /= 10); /* delete the lowest digit */
if (sign < 0)
str[i++] = '-';
str[i] = '\0';
strreverse(str);
}
/** ini_putl()
* \param Section the name of the section to write the value in
* \param Key the name of the entry to write
* \param Value the value to write
* \param Filename the name and full path of the .ini file to write to
*
* \return 1 if successful, otherwise 0
*/
int ini_putl(const TCHAR *Section, const TCHAR *Key, long Value, const TCHAR *Filename)
{
TCHAR LocalBuffer[32];
long2str(Value, LocalBuffer);
return ini_puts(Section, Key, LocalBuffer, Filename);
}
#if defined INI_REAL
/** ini_putf()
* \param Section the name of the section to write the value in
* \param Key the name of the entry to write
* \param Value the value to write
* \param Filename the name and full path of the .ini file to write to
*
* \return 1 if successful, otherwise 0
*/
int ini_putf(const TCHAR *Section, const TCHAR *Key, INI_REAL Value, const TCHAR *Filename)
{
TCHAR LocalBuffer[64];
ini_ftoa(LocalBuffer, Value);
return ini_puts(Section, Key, LocalBuffer, Filename);
}
#endif /* INI_REAL */
#endif /* !INI_READONLY */
+272
View File
@@ -0,0 +1,272 @@
#include "../inc/mq.h"
#include <string.h> //for memcpy
#include <stdlib.h> //for malloc free
#include <sys/time.h> //for struct timeval gettimeofday
#include <signal.h> //for signal sigset_t sigfillset pthread_sigmask SIG*
#include <sys/msg.h> //for msgget msgsnd msgrcv
#include <unistd.h> //for pipe write read close
#include <pthread.h> //for pthread*
#include <errno.h> //for errno E*
#include "../inc/rbtree.h"
#include <stdio.h>
#define QUE_MSGTYPE 1l
static int timer_alive = 0;
static pthread_t timer_tid;
static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t timer_cond = PTHREAD_COND_INITIALIZER;
static rbtree_t timer_root;
typedef struct timer_node_s timer_node_t;
struct timer_node_s{
pthread_t tid;
uint64_t expire;
int status;//0 default, 1 timeout
rbtree_node_t rbtree_node;
};
int is_thread_alive(pthread_t tid){
int kill_rc = pthread_kill(tid,0);//0 success, ESRCH 线程不存在, EINVAL 信号不合法
int ret = 1;//alive
if(kill_rc == ESRCH){
ret = 0;
}
return ret;
}
static void* timer_loop(void*arg);
static int timer_create_new(){
if(timer_alive == 1){
return 0;
}
pthread_mutex_lock(&timer_mutex);
if(timer_alive == 1){
pthread_mutex_unlock(&timer_mutex);
return 0;
}
rbtree_init(&timer_root);
pthread_create(&timer_tid,NULL,timer_loop,NULL);
timer_alive = 1;
pthread_detach(timer_tid);//分离线程
pthread_mutex_unlock(&timer_mutex);
return 0;
}
static void* timer_loop(void*arg){
sigset_t sigset;
sigfillset(&sigset);
pthread_sigmask(SIG_SETMASK,&sigset,NULL);//当前线程屏蔽所有信号
uint64_t now;
struct timespec now_ts;
struct timespec timeout_ts;
rbtree_node_t *rbtree_node;
timer_node_t *timer_node=NULL,*timer_node_will_expire=NULL;
for(;;){
//加锁
pthread_mutex_lock(&timer_mutex);
clock_gettime(CLOCK_REALTIME, &now_ts);
now = (uint64_t)((uint64_t)now_ts.tv_sec*1000) + (uint64_t)((uint64_t)now_ts.tv_nsec/1000000);
timer_node = NULL;
timer_node_will_expire = NULL;
for(rbtree_node = rbtree_min(&timer_root,timer_root.root);
rbtree_node!=NULL;
rbtree_node = rbtree_next(&timer_root,rbtree_node)
){
timer_node = rbtree_data(rbtree_node,timer_node_t,rbtree_node);
//printf("[tim]loop now=%llu,exp=%llu\n",now,timer_node->expire);
if(timer_node->expire <= now){
printf("[tim]kill loop now=%llu,exp=%llu\n",now,timer_node->expire);
//超时, 则信号通知
timer_node->status = 1;
pthread_kill(timer_node->tid,SIGUSR1);
continue;
}
timer_node_will_expire = timer_node;
printf("[tim]loop now=%llu,exp=%llu\n",now,timer_node->expire);
break;
}
//条件等待
if(timer_node_will_expire == NULL){
pthread_cond_wait(&timer_cond,&timer_mutex);
}else{
//printf("[tim].will=%llu\n",timer_node_will_expire->expire);
timeout_ts.tv_sec = (uint64_t)((uint64_t)timer_node_will_expire->expire/1000);
timeout_ts.tv_nsec = (uint64_t)((uint64_t)timer_node_will_expire->expire%1000)*1000000;
printf("[tim]con sec=%llu,nsec=%llu\n",(uint64_t)timeout_ts.tv_sec, (uint64_t)timeout_ts.tv_nsec);
pthread_cond_timedwait(&timer_cond,&timer_mutex,&timeout_ts);//注意, 时间不能是NULL
}
//解锁
pthread_mutex_unlock(&timer_mutex);
}
timer_alive = 0;
pthread_exit(NULL);
}
int add_timer(timer_node_t *node,size_t timeout){//单位ms
node->tid = pthread_self();
node->status = 0;
struct timespec ts;
uint64_t lldata;
clock_gettime(CLOCK_REALTIME, &ts);
node->expire = (uint64_t)(((uint64_t)ts.tv_sec*1000)+ (uint64_t)timeout + (uint64_t)((uint64_t)ts.tv_nsec/1000000));
node->rbtree_node.key = node->expire;
lldata = (uint64_t)(((uint64_t)ts.tv_sec*1000) + (uint64_t)((uint64_t)ts.tv_nsec/1000000));
printf("[rec].time_exp=%llu\n",node->expire);
printf("[rec].time_cur=%llu\n",lldata);
pthread_mutex_lock(&timer_mutex);
rbtree_insert(&timer_root,&node->rbtree_node);
pthread_cond_signal(&timer_cond);
pthread_mutex_unlock(&timer_mutex);
//pthread_cond_signal(&timer_cond);
return 0;
}
int delete_timer(timer_node_t *node){
pthread_mutex_lock(&timer_mutex);
rbtree_delete(&timer_root,&node->rbtree_node);
pthread_mutex_unlock(&timer_mutex);
return 0;
}
int _signal(int signo, void (*sighandler)(int))
{
struct sigaction sa;
sigset_t set;
//sigemptyset(&set);//当进入sighandler时,不屏蔽所有信号,可被其他信号中断
sigfillset(&set);//当进入sighandler时,屏蔽所有信号
sa.sa_mask = set;
sa.sa_handler = sighandler;
sa.sa_restorer = NULL;
sa.sa_flags = 0;
return sigaction(signo, &sa, NULL);
}
void timer_sighandler(int signo){
return;
}
typedef struct {
long msgtype;
unsigned char buffer[0];
} MSGBUF;
ssize_t send_que_timedwait(key_t qid,void* pdata,size_t len,int timeout){
int msgqid,retu;
timer_node_t node = {0};
unsigned char* buf = NULL;
MSGBUF* que_msgbuf;
//https://man7.org/linux/man-pages/man2/msgget.2.html
msgqid=msgget(qid,0666);
if(msgqid == -1){
if(errno == ENOENT){
msgqid=msgget(qid,IPC_CREAT|0666);
}
if(msgqid == -1)
return -1;
}
buf = malloc(sizeof(MSGBUF) + len);
if(buf == NULL)
return -1;
que_msgbuf = (MSGBUF*)buf;
que_msgbuf->msgtype = QUE_MSGTYPE;
memcpy(que_msgbuf->buffer,pdata,len);
// int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg)
// msgflag
// 0:当消息队列满时,msgsnd将会阻塞,直到消息能写进消息队列
// IPC_NOWAIT:当消息队列已满的时候,msgsnd函数不等待立即返回
// IPC_NOERROR:若发送的消息大于size字节,则把该消息截断,截断部分将被丢弃,且不通知发送进程。
// 返回值
// 成功:0
// 出错:-1,错误原因存于error中
// EAGAIN:参数msgflg设为IPC_NOWAIT,而消息队列已满
// EIDRM:标识符为msqid的消息队列已被删除
// EACCESS:无权限写入消息队列
// EFAULT:参数msgp指向无效的内存地址
// EINTR:队列已满而处于等待情况下被信号中断
// EINVAL:无效的参数msqid、msgsz或参数消息类型type小于0
if(timeout == -1){
retu=msgsnd(msgqid,buf,len,0);
//printf("");
}
else if(timeout == 0){
retu=msgsnd(msgqid,buf,len,IPC_NOWAIT);
}else{
timer_create_new();
_signal(SIGUSR1,timer_sighandler);
add_timer(&node,timeout);
retu=msgsnd(msgqid,buf,len,0);
delete_timer(&node);
if(errno == EINTR && node.status == 1){
errno = ETIMEDOUT;
}
}
free(buf);
return retu;
}
ssize_t read_que_timedwait(key_t qid,void* pdata,size_t len,int timeout){
int msgqid,retu;
timer_node_t node = {0};
unsigned char* buf = NULL;
MSGBUF* que_msgbuf = NULL;
msgqid=msgget(qid,0666);//https://man7.org/linux/man-pages/man2/msgget.2.html
if(msgqid == -1){
if(errno == ENOENT){
msgqid=msgget(qid,IPC_CREAT|0666);
}
if(msgqid == -1)
printf("msgget err.\n");
return -1;
}
buf = malloc(sizeof(MSGBUF) + len);
if(buf == NULL){
printf("malloc err.\n");
return -1;
}
if(timeout == -1){
retu=msgrcv(msgqid,buf,len,0,0);
}
else if(timeout == 0){
retu=msgrcv(msgqid,buf,len,0,IPC_NOWAIT);
}else{
timer_create_new();
_signal(SIGUSR1,timer_sighandler);
add_timer(&node,timeout);
retu=msgrcv(msgqid,buf,len,0,0);
delete_timer(&node);
if(errno == EINTR && node.status == 1){
errno = ETIMEDOUT;
}
}
que_msgbuf = (MSGBUF*)buf;
if(retu > 0){
memcpy(pdata,que_msgbuf->buffer,retu);
}
free(buf);
return retu;
}
+594
View File
@@ -0,0 +1,594 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <math.h>
#include <pthread.h>
#include <semaphore.h>
#include <string.h>
#include <unistd.h>
#include <sys/prctl.h>
#include "../inc/main.h"
#include "../inc/msg.h"
#include "../inc/isys_config.h"
#include "../inc/iweb_task.h"
#include "../inc/os_task.h"
#include "../inc/iuart_user.h"
#include "../inc/http_get.h"
#include "../inc/upgrade.h"
#include "../inc/isys_com.h"
//extern void http_save_sys_para(S_CGI_SYS_PARA_T *ppara);
//extern void lcd_save_web_sys_para(MSG* pmsg);
extern void uart_remote(MSG *pmsg);
extern void uart_hw_init(void);
extern void uart_hw_deinit(void);
extern void udp_imgp_reinit(MSG *pmsg);
extern void rs232_uart_hw_init(void);
extern void rs232_uart_hw_deinit(void);
#if 1//@CONFIRMED
int sys_msg_send(MSG *p_msg, key_t keyid)
{
struct msg_st data;
int msgid = -1;
// creat msg queue
msgid = msgget((key_t)keyid, 0666 | IPC_CREAT);
if(msgid == -1) {
fprintf(stderr, "msgget failed with error: %d, keyid=%d\n", errno,keyid);
return -1;
}
switch (keyid)
{
case WEB_MSG_KEY_ID:
data.mtype = MSGQ_TYPE_WEB;
break;
case LCD_MSG_KEY_ID:
data.mtype = MSGQ_TYPE_LCD;
break;
case UART_MSG_KEY_ID:
data.mtype = MSGQ_TYPE_UART;
break;
case TSOIP_MSG_KEY_ID:
data.mtype = MSGQ_TYPE_TSOIP;
break;
case LOG_MSG_KEY_ID:
data.mtype = MSGQ_TYPE_LOG;
break;
case GPS_UART_MSG_KEY_ID:
data.mtype = MSGQ_TYPE_GPS_UART;
break;
//....
//add hear
//....
default:
data.mtype = MSGQ_TYPE_ALL;
break;
}
memcpy(&data.msg, p_msg, sizeof(MSG));
// send msg
if(msgsnd(msgid, (void*)&data, MAX_TEXT, 0) == -1) {
fprintf(stderr, "a-msgsnd failed, errno=%d, keyid=%d, msgid=%d\n",errno,keyid,msgid);
return -1;
}
//printf("send type=%d, rpdata= 0x%x ,lpara=0x%lx\n", p_msg->type , p_msg->rPara, p_msg->lPara);
return 0;
}
#endif
#if DEF_NEED_TO_EN //@NEED_TO_EN
static void * log_msg_recv_thr(void * args)
{
int running = 1;
int msgid = -1;
struct msg_st data;
long int msgtype = MSGQ_TYPE_LOG; //注意1
//建立消息队列
msgid = msgget((key_t)LOG_MSG_KEY_ID, 0666 | IPC_CREAT);
if(msgid == -1) {
fprintf(stderr, "msgget failed with error: %d\n", errno);
goto TSOIP_MSG_RECV_ERR_EXIT;
}
//从队列中获取消息
while(running) {
if(msgrcv(msgid, (void*)&data, BUFSIZ, msgtype, 0) == -1) {
fprintf(stderr, "msgrcv failed with errno: %d\n", errno);
goto TSOIP_MSG_RECV_ERR_EXIT;
}
//printf("[LOG MSG recv ]getmsg: data.msg_type=%ld , data.msg.type=%u\n",data.mtype, data.msg.type);
switch(data.msg.type)
{
case MSG_LOG:
break;
case MSG_LOG_RECORD_RESET:
break;
}
usleep(50000);
}
TSOIP_MSG_RECV_ERR_EXIT:
//删除消息队列
if(msgctl(msgid, IPC_RMID, 0) == -1) {
fprintf(stderr, "msgctl(IPC_RMID) failed\n");
}
return (void*)NULL;
}
#endif//DEF_NEED_TO_EN
#if 1
static void * tsoip_msg_recv_thr(void * args)
{
int running = 1;
int msgid = -1;
struct msg_st data;
long int msgtype = MSGQ_TYPE_TSOIP; //注意1
log_printf(LOG_DEBUG,I_NOUSED,"[.thr.] tsoip_msg_recv_thr Start.\n");
//建立消息队列
msgid = msgget((key_t)TSOIP_MSG_KEY_ID, 0666 | IPC_CREAT);
if(msgid == -1) {
fprintf(stderr, "msgget failed with error: %d\n", errno);
goto TSOIP_MSG_RECV_ERR_EXIT;
}
log_printf(LOG_DEBUG,I_NOUSED,"tsoip msg_id=%d\n",msgid);
//从队列中获取消息
while(running) {
if(get_upgrade_flg()){
usleep(1000000);
continue;
}
if(msgrcv(msgid, (void*)&data, BUFSIZ, msgtype, 0) == -1) {
fprintf(stderr, "msgrcv failed with errno: %d\n", errno);
goto TSOIP_MSG_RECV_ERR_EXIT;
}
//printf("[UART MSG recv ]getmsg: data.msg_type=%ld , data.msg.type=%u\n",data.mtype, data.msg.type);
switch(data.msg.type)
{
case MSG_IGMP_REINIT:
udp_imgp_reinit(&data.msg);
break;
}
// if(data.msg.need_free==MSG_NEED_FREE)
// free(&data.msg);
usleep(50000);
}
TSOIP_MSG_RECV_ERR_EXIT:
//删除消息队列
if(msgctl(msgid, IPC_RMID, 0) == -1) {
fprintf(stderr, "msgctl(IPC_RMID) failed\n");
}
return (void*)NULL;
}
#endif
#if 1
static void * uart_msg_recv_thr(void * args)
{
int running = 1;
int msgid = -1;
struct msg_st data;
long int msgtype = MSGQ_TYPE_UART; //注意1
log_printf(LOG_DEBUG,I_NOUSED,"[.thr.] uart_msg_recv_thr Start.\n");
//建立消息队列
msgid = msgget((key_t)UART_MSG_KEY_ID, 0666 | IPC_CREAT);
if(msgid == -1) {
fprintf(stderr, "msgget failed with error: %d\n", errno);
goto UART_MSG_RECV_ERR_EXIT;
}
log_printf(LOG_DEBUG,I_NOUSED,"u232 msg_id=%d\n",msgid);
//从队列中获取消息
while(running) {
if(get_upgrade_flg()){
usleep(1000000);
continue;
}
if(msgrcv(msgid, (void*)&data, BUFSIZ, msgtype, 0) == -1) {
fprintf(stderr, "msgrcv failed with errno: %d\n", errno);
goto UART_MSG_RECV_ERR_EXIT;
}
//printf("[UART MSG recv ]getmsg: data.msg_type=%ld , data.msg.type=%u\n",data.mtype, data.msg.type);
switch(data.msg.type)
{
case MSG_RS232:
uart_remote(&data.msg);
break;
case MSG_UART_RE_INIT:
uart_hw_deinit();
uart_hw_init();
rs232_uart_hw_init();
break;
}
usleep(1000);
}
UART_MSG_RECV_ERR_EXIT:
//删除消息队列
if(msgctl(msgid, IPC_RMID, 0) == -1) {
fprintf(stderr, "msgctl(IPC_RMID) failed\n");
}
return (void*)NULL;
}
extern void gps_uart_proc(MSG *pmsg);
extern void ublox_proc_state(void);
static void * gps_uart_msg_recv_thr(void * args)
{
int running = 1;
int msgid = -1;
struct msg_st data;
long int msgtype = MSGQ_TYPE_GPS_UART; //注意1
log_printf(LOG_DEBUG,I_NOUSED,"[.thr.] gps_uart_msg_recv_thr Start.\n");
//建立消息队列
msgid = msgget((key_t)GPS_UART_MSG_KEY_ID, 0666 | IPC_CREAT);
if(msgid == -1) {
log_printf(LOG_ERR,I_NOUSED,"msgget failed with error\n");
fprintf(stderr, "msgget failed with error: %d\n", errno);
goto GPS_UART_MSG_RECV_ERR_EXIT;
}
log_printf(LOG_DEBUG,I_NOUSED,"gps msg_id=%d\n",msgid);
//从队列中获取消息
while(running) {
if(msgrcv(msgid, (void*)&data, BUFSIZ, msgtype, 0) == -1) {
log_printf(LOG_ERR,I_NOUSED,"msgrcv failed with errno\n");
fprintf(stderr, "msgrcv failed with errno: %d\n", errno);
goto GPS_UART_MSG_RECV_ERR_EXIT;
}
//printf("[UART MSG recv ]getmsg: data.msg_type=%ld , data.msg.type=%u\n",data.mtype, data.msg.type);
switch(data.msg.type)
{
case MSG_RS232:
log_printf(LOG_DEBUG,I_NOUSED," gps_uart get MSG_RS232 id=%d\n",msgid);
gps_uart_proc(&data.msg);
break;
case MSG_SEC:
//printf(" gps_uart get MSG_SEC\n");
ublox_proc_state();
break;
default:
break;
}
usleep(1000);
}
GPS_UART_MSG_RECV_ERR_EXIT:
//删除消息队列
if(msgctl(msgid, IPC_RMID, 0) == -1) {
fprintf(stderr, "msgctl(IPC_RMID) failed\n");
}
return (void*)NULL;
}
#endif
//@tmp
/*uint8 g_flg_load=0;
uint8 g_web_flg_load=0;
#include "../inc/dpd_proc.h"*/
static void * lcd_msg_recv_thr(void * args)
{
int running = 1;
int msgid = -1;
struct msg_st data;
long int msgtype = MSGQ_TYPE_LCD; //注意1
log_printf(LOG_DEBUG,I_NOUSED,"[.thr.] lcd_msg_recv_thr Start.\n");
//建立消息队列
msgid = msgget((key_t)LCD_MSG_KEY_ID, 0666 | IPC_CREAT);
if(msgid == -1) {
fprintf(stderr, "msgget failed with error: %d\n", errno);
goto LCD_MSG_RECV_ERR_EXIT;
}
log_printf(LOG_DEBUG,I_NOUSED,"lcd msg_id=%d\n",msgid);
//从队列中获取消息
while(running) {
if(get_upgrade_flg()){
usleep(2000000);
continue;
}
if(msgrcv(msgid, (void*)&data, BUFSIZ, msgtype, 0) == -1) {
fprintf(stderr, "msgrcv failed with errno: %d\n", errno);
log_printf(LOG_ERR,I_NOUSED,"msgrcv failed with errno: %d\n", errno);
goto LCD_MSG_RECV_ERR_EXIT;
}
//printf("[LCD MSG ]getmsg: data.msg_type=%ld , data.msg.type=%u\n",data.mtype, data.msg.type);
switch(data.msg.type)
{
case MSG_WEB_SAVE_PARA:
http_save_change_para(&data.msg);
break;
case MSG_PSTS_UPDATE:
#if USE_PSTS
psts_para_update(&atsc_para.psts_para);
#endif
break;
}
usleep(50000);
}
LCD_MSG_RECV_ERR_EXIT:
//删除消息队列
if(msgctl(msgid, IPC_RMID, 0) == -1) {
fprintf(stderr, "msgctl(IPC_RMID) failed\n");
}
return (void*)NULL;
}
static void * web_msg_recv_thr(void * args)
{
int running = 1;
int msgid = -1;
struct msg_st data;
long int msgtype = MSGQ_TYPE_WEB; //注意
log_printf(LOG_DEBUG,I_NOUSED,"[.thr.] web_msg_recv_thr Start.\n");
//建立消息队列
msgid = msgget((key_t)WEB_MSG_KEY_ID, 0666 | IPC_CREAT);
if(msgid == -1) {
fprintf(stderr, "msgget failed with error: %d\n", errno);
goto WEB_MSG_RECV_ERR_EXIT;
}
log_printf(LOG_DEBUG,I_NOUSED,"web msg_id=%d\n",msgid);
while(running) {
if(get_upgrade_flg()){
usleep(1000000);
continue;
}
// recv msg
if(msgrcv(msgid, (void*)&data, BUFSIZ, msgtype, 0) == -1) {
fprintf(stderr, "msgrcv failed with errno: %d\n", errno);
goto WEB_MSG_RECV_ERR_EXIT;
}
#if 0
printf("[web msg] getmsg: data.msg_type=%ld , data.msg.type=%u, %s lpara=0x%lx\n",
data.mtype,
data.msg.type,
(data.msg.need_free==MSG_NEED_FREE)?"needfree":" ",
data.msg.lPara);
#endif
#if 1//proc on lcd, NEED_TO_EN
http_save_change_para(&data.msg);
#endif
usleep(50000);
}
WEB_MSG_RECV_ERR_EXIT:
//删除消息队列
if(msgctl(msgid, IPC_RMID, 0) == -1) {
fprintf(stderr, "msgctl(IPC_RMID) failed\n");
}
return (void*)NULL;
}
int creat_web_msg_thread(void)
{
int ret = -1;
pthread_t web_msg_thr_id;
log_printf(LOG_DEBUG,I_NOUSED,"[.thr.]->-\n");
ret = pthread_create(&web_msg_thr_id, NULL, (void *)web_msg_recv_thr, NULL);
if(ret != 0) {
log_printf(LOG_ERR,I_NOUSED," creat thread err.\n");
return -1;
}
else{
log_printf(LOG_DEBUG,I_NOUSED," Ok. creat thread succeed.\n");
}
return 0;
}
//@tmp
/*void send_msg_for_tst(){
static MSG msg;
MSG *pmsg;
pmsg = &msg;
pmsg->type = 0xff;//0xffMSG_WEB_SAVE_PARA
pmsg->wPara =0xff; // CGI_CTTB_WEB_SYSPARA
pmsg->lPara =0;
pmsg->need_free = MSG_NO_NEED_FREE;
printf("[%s,%d]\n",__FUNCTION__,__LINE__);
printf("send msg to LCd\n");
cttb_msg_send_to_lcd(pmsg);
}*/
int creat_lcd_msg_thread(void)
{
int ret = -1;
pthread_t lcd_msg_thr_id;
log_printf(LOG_DEBUG,I_NOUSED,"[.thr.]->-\n");
ret = pthread_create(&lcd_msg_thr_id, NULL, (void *)lcd_msg_recv_thr, NULL);
if(ret != 0) {
log_printf(LOG_ERR,I_NOUSED," creat thread err.\n");
return -1;
}
else{
log_printf(LOG_DEBUG,I_NOUSED," Ok . creat thread succeed.\n");
}
return 0;
}
#if 1
int creat_uart_msg_thread(void)
{
int ret = -1;
pthread_t uart_msg_thr_id;
log_printf(LOG_DEBUG,I_NOUSED,"[.thr.]->-\n");
ret = pthread_create(&uart_msg_thr_id, NULL, (void *)uart_msg_recv_thr, NULL);
if(ret != 0) {
log_printf(LOG_ERR,I_NOUSED," creat thread err.\n");
return -1;
}
else{
log_printf(LOG_DEBUG,I_NOUSED," Ok. creat thread succeed.\n");
}
return 0;
}
extern uint8 g_gps_init_ok;
void sec_msg_to_gps_uart_thr(void)
{
MSG msg;
if(g_gps_init_ok==1){
msg.type = MSG_SEC;
msg.need_free = MSG_NO_NEED_FREE; // lpara no need free
msg.rPara = UART_USER_232_COM;
msg_send_to_gps_uart(&msg);//cttb_msg_send_to_uart
}
}
int creat_gps_uart_msg_thread(void)
{
int ret = -1;
pthread_t gps_uart_msg_thr_id;
log_printf(LOG_DEBUG,I_NOUSED,"[.thr.]->-\n");
ret = pthread_create(&gps_uart_msg_thr_id, NULL, (void *)gps_uart_msg_recv_thr, NULL);
if(ret != 0) {
log_printf(LOG_ERR,I_NOUSED," creat thread err.\n");
return -1;
}
else{
log_printf(LOG_DEBUG,I_NOUSED," Ok . creat thread succeed.\n");
}
return 0;
}
#endif
#if 1
int creat_tsoip_msg_thread(void)
{
int ret = -1;
pthread_t tsoip_msg_thr_id;
log_printf(LOG_DEBUG,I_NOUSED,"[.thr.]->-\n");
ret = pthread_create(&tsoip_msg_thr_id, NULL, (void *)tsoip_msg_recv_thr, NULL);
if(ret != 0) {
log_printf(LOG_ERR,I_NOUSED," creat thread err.\n");
return -1;
}
else{
log_printf(LOG_DEBUG,I_NOUSED," Ok. creat thread succeed.\n");
}
return 0;
}
#endif
#if DEF_NEED_TO_EN //@NEED_TO_EN
int creat_log_msg_thread(void)
{
int ret = -1;
pthread_t log_msg_thr_id;
ret = pthread_create(&log_msg_thr_id, NULL, (void *)log_msg_recv_thr, NULL);
if(ret != 0) {
printf(" Err.[%s,%d] creat thread err.\n", __FUNCTION__,__LINE__);
return -1;
}
else{
printf(" Ok .[%s,%d] creat thread succeed.\n", __FUNCTION__,__LINE__);
}
return 0;
}
#endif//DEF_NEED_TO_EN
+368
View File
@@ -0,0 +1,368 @@
#include "config.h"
#include "type.h"
#include "lwipopts.h"
#include "string.h"
#include "opt.h"
extern struct tcp_pcb *usr_tcp_pcb_addr[MEMP_NUM_TCP_PCB];
extern struct memp *usr_seg_addr[MEMP_NUM_TCP_SEG];
extern struct pbuf *usr_pbuf_pool_addr[PBUF_POOL_SIZE];
static uint32 g_port;
#if USE_RESET_TCP
#include "lwip/netif.h"
#include "stm32f4xx_hal_eth.h"
extern struct netif gnetif; /* network interface structure */
extern ETH_HandleTypeDef heth;
void usr_close_network(void)
{
HAL_ETH_Stop(&heth);
netif_set_down(&gnetif);
}
void usr_open_network(void)
{
netif_set_up(&gnetif);
HAL_ETH_Start(&heth);
}
/*
0.检测tcp_seg的内存是否已经用尽;
1.定位到占用相应tcp_seg的内存对应的tcp_pcb;
2.将tcp_pcb从tcp_pcb_lists中清除;
3.释放相应的tcp_pcb;
*/
extern struct stats_ lwip_stats;
extern struct tcp_pcb *tcp_bound_pcbs;
extern union tcp_listen_pcbs_t tcp_listen_pcbs;
extern struct tcp_pcb *tcp_active_pcbs;
extern struct tcp_pcb *tcp_tw_pcbs;
#include "tcp.h"
extern void tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb);
static uint16 occupyed_num=0;
static uint16 rels_lis_num=0;
static uint16 rels_bod_num=0;
static uint16 rels_act_num=0;
static uint16 rels_tw_num=0;
static uint16 checked_times=0;
static uint16 reset_pbuf_pool_cnt=0;
uint16 get_tcp_seg_errnum(void)
{
return lwip_stats.memp[4]->err;
}
uint16 get_tcp_seg_occupy(void)
{
return (uint16)lwip_stats.memp[4]->used*100/lwip_stats.memp[4]->avail;
}
uint8 check_pcb_buf_err(void)
{
if(((lwip_stats.memp[4]->used >= lwip_stats.memp[4]->avail) && (lwip_stats.memp[4]->err >= 50))//tcp_seg
||((lwip_stats.memp[4]->used >= lwip_stats.memp[4]->avail*3/4) && (lwip_stats.memp[4]->err >= 50))){
return 1;
}
else if((lwip_stats.memp[14]->used >= lwip_stats.memp[14]->avail*3/4) && (lwip_stats.memp[14]->err >= 50)){//pbuf_pool
return 2;
}
else{
return 0;
}
}
void check_and_release_pbuf_pool(void)
{
struct pbuf *p;
uint16 i;
for (i=0; i<PBUF_POOL_SIZE; i++){
p = usr_pbuf_pool_addr[i];
pbuf_free(p);
}
lwip_stats.memp[14]->err=0;
reset_pbuf_pool_cnt++;
if(reset_pbuf_pool_cnt>=2000)reset_pbuf_pool_cnt=10;
}
void check_and_release_pcb(void)
{
struct tcp_pcb *pcb_occupy[MEMP_NUM_TCP_PCB]={NULL};
struct tcp_pcb *poccupy;
struct tcp_pcb *pcb;
// static uint16 check_cnt=0;
uint16 occupy_index=0;
uint16 rels_lis_cnt=0;
uint16 rels_bod_cnt=0;
uint16 rels_act_cnt=0;
uint16 rels_tw_cnt=0;
uint16 i,j;
//定位
for(i=0;i<MEMP_NUM_TCP_PCB;i++)
{
for(j=0;j<MEMP_NUM_TCP_SEG;j++)
{
if((usr_tcp_pcb_addr[i]->unsent == (struct tcp_seg *)((uint8*)usr_seg_addr[j]+MEMP_SIZE))
||(usr_tcp_pcb_addr[i]->unacked == (struct tcp_seg *)((uint8*)usr_seg_addr[j]+MEMP_SIZE))
||(usr_tcp_pcb_addr[i]->ooseq == (struct tcp_seg *)((uint8*)usr_seg_addr[j]+MEMP_SIZE))
)
{
pcb_occupy[occupy_index] = usr_tcp_pcb_addr[i];
printf("pcb_occupy:0x%8x\n",pcb_occupy[occupy_index]);
occupy_index++;
}
}
}
//释放
for(poccupy = pcb_occupy[0],i=0;poccupy!=NULL&&i<50;poccupy=pcb_occupy[i])
{
for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
{
if(pcb==poccupy){//刚好就是被占用的pcb
tcp_pcb_remove(&tcp_active_pcbs, pcb);
printf("checked and remove tcp form tcp_active_pcb 0x%8x\n",pcb);
rels_act_cnt++;
break;
}
}
for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next)
{
if(pcb==poccupy){//刚好就是被占用的pcb
tcp_pcb_remove(&tcp_tw_pcbs, pcb);
printf("checked and remove tcp form tcp_tw_pcbs 0x%8x\n",pcb);
rels_tw_cnt++;
break;
}
}
for (pcb = tcp_bound_pcbs; pcb != NULL; pcb = pcb->next)
{
if(pcb==poccupy){//刚好就是被占用的pcb
tcp_pcb_remove(&tcp_bound_pcbs, pcb);
printf("checked and remove tcp form tcp_bound_pcbs 0x%8x\n",pcb);
rels_bod_cnt++;
break;
}
}
i++;
/*for (pcb = (struct tcp_pcb *)tcp_listen_pcbs->pcbs; pcb != NULL; pcb = pcb->next)
{
if(pcb==poccupy){//刚好就是被占用的pcb
//tcp_pcb_remove(&tcp_listen_pcbs, pcb);
printf("checked tcp form tcp_listen_pcbs 0x%8x\n",pcb);
rels_lis_cnt++;
break;
}
}*/
}
lwip_stats.memp[4]->err=0;
occupyed_num = occupy_index;
checked_times++;
rels_lis_num = rels_lis_cnt;
rels_bod_num = rels_bod_cnt;
rels_act_num = rels_act_cnt;
rels_tw_num = rels_tw_cnt;
}
#endif//USE_RESET_TCP
#if USE_UART_CMD_PRINT_LWIP
//------------------------------
static char strBuf[100];
extern void uart_send_string(uint32 port,const uint8 *pbuf,uint32 len);
void usr_print(const char *pbuf,uint32 len)
{
uart_send_string(g_port,(uint8 *)pbuf,len);
}
void usr_lwip_print(char *pbuf,uint32 value)
{
sprintf(strBuf,pbuf,value);
usr_print(strBuf,strlen(strBuf));
}
#define STATS_PRINT(x) do {printf x;} while(0)
#include <stdio.h>
#include <stdlib.h>
//#if USE_ETH_DBG
extern s_USR_DBG_ETH udbg_eth;
//打印自定义的状态
void usr_stats_print()
{
/*STATS_PRINT(("\n%s\n\t", "usr_eth"));
STATS_PRINT(("eth_int: %d\n\t", udbg_eth.eth_int_in_cnt));
STATS_PRINT(("eth_recv_sem: %d\n\t", udbg_eth.eth_recv_sem_cnt));
STATS_PRINT(("eth_tcpip: %d\n\t", udbg_eth.eth_recv_tcpip_cnt));
STATS_PRINT(("eth_tcp_in: %d\n\t", udbg_eth.eth_tcp_in_cnt));
STATS_PRINT(("eth_ip4_in: %d\n\t", udbg_eth.eth_ip4_in_cnt));
STATS_PRINT(("eth_udp_in: %d\n\t", udbg_eth.eth_udp_in_cnt));
STATS_PRINT(("eth_icmp_in: %d\n\t", udbg_eth.eth_icmp_in_cnt));
STATS_PRINT(("eth_tcp_listen: %d\n\t", udbg_eth.eth_tcp_listen_cnt));
STATS_PRINT(("http_accept: %d\n\t", udbg_eth.eth_http_accept_c));
STATS_PRINT(("http_recv: %d\n\t", udbg_eth.eth_http_recv_c));
STATS_PRINT(("http_parse: %d\n\t", udbg_eth.eth_http_parse_c));
STATS_PRINT(("http_sent: %d\n\t", udbg_eth.eth_http_send_c));
STATS_PRINT(("tcp_out: %d\n\t", udbg_eth.eth_tcp_out_c));
STATS_PRINT(("lvl_alloc_err: %d\n\t", udbg_eth.low_level_in_alloc_err));
STATS_PRINT(("memp_tcp_seg_reset: %d\n\t", udbg_eth.memp_tcp_seg_reset_c));*/
usr_lwip_print("\nusr_eth %d\n\t", 0);
usr_lwip_print("eth_int: %d\n\t", udbg_eth.eth_int_in_cnt);
usr_lwip_print("eth_recv_sem: %d\n\t", udbg_eth.eth_recv_sem_cnt);
usr_lwip_print("eth_tcpip: %d\n\t", udbg_eth.eth_recv_tcpip_cnt);
usr_lwip_print("eth_tcp_in: %d\n\t", udbg_eth.eth_tcp_in_cnt);
usr_lwip_print("eth_ip4_in: %d\n\t", udbg_eth.eth_ip4_in_cnt);
usr_lwip_print("eth_udp_in: %d\n\t", udbg_eth.eth_udp_in_cnt);
usr_lwip_print("eth_icmp_in: %d\n\t", udbg_eth.eth_icmp_in_cnt);
usr_lwip_print("eth_tcp_listen: %d\n\t", udbg_eth.eth_tcp_listen_cnt);
usr_lwip_print("http_accept: %d\n\t", udbg_eth.eth_http_accept_c);
usr_lwip_print("http_recv: %d\n\t", udbg_eth.eth_http_recv_c);
usr_lwip_print("http_parse: %d\n\t", udbg_eth.eth_http_parse_c);
usr_lwip_print("http_sent: %d\n\t", udbg_eth.eth_http_send_c);
usr_lwip_print("tcp_out: %d\n\t", udbg_eth.eth_tcp_out_c);
usr_lwip_print("lvl_alloc_err: %d\n\t", udbg_eth.low_level_in_alloc_err);
usr_lwip_print("memp_tcp_seg_reset: %d\n\t", udbg_eth.memp_tcp_seg_reset_c);
}
#include "memp_priv.h"
extern uint8 other_place_call_seg;
//打印seg使用状态
void usr_seg_stats_print()
{
uint8 i=0;
/*STATS_PRINT(("\n%s\n\t", "tcp_seg_alloc"));
STATS_PRINT(("other_call_seg: %d\n\t", other_place_call_seg));
for(i=0;i<MEMP_NUM_TCP_SEG;i++){
STATS_PRINT(("seg_addr%2d: 0x%x alloc_id:%4d release_id:%4d \n\t", i,usr_seg_addr[i],usr_seg_addr[i]->claimer_id,usr_seg_addr[i]->release_id));
}*/
sprintf(strBuf,"\ntcp_seg_alloc\n\t");
usr_print(strBuf,strlen(strBuf));
sprintf(strBuf,"other_call_seg: %d\n\t", other_place_call_seg);
usr_print(strBuf,strlen(strBuf));
for(i=0;i<MEMP_NUM_TCP_SEG;i++){
sprintf(strBuf,"seg_adr%2d: 0x%x aloc_id:%4d rels_id:%4d \n\t", i,usr_seg_addr[i],usr_seg_addr[i]->claimer_id,usr_seg_addr[i]->release_id);
usr_print(strBuf,strlen(strBuf));
}
}
#include "tcp.h"
#include "memp_priv.h"
//打印tcp_pcb使用情况
void usr_tcp_pcb_stats_print()
{
struct tcp_seg * unsent;
struct tcp_seg * unacked;
struct tcp_seg * ooseq;
uint8 i=0;
//STATS_PRINT(("\n%s\n\t", "tcp_pcb_stats"));
sprintf(strBuf,"\ntcp_pcb_stats\n\t");
usr_print(strBuf,strlen(strBuf));
for(i=0;i<MEMP_NUM_TCP_PCB;i++){
if(usr_tcp_pcb_addr[i]->unsent!=NULL){
unsent = (struct tcp_seg *)((uint8*)usr_tcp_pcb_addr[i]->unsent - MEMP_SIZE);
}else{
unsent = usr_tcp_pcb_addr[i]->unsent;
}
if(usr_tcp_pcb_addr[i]->unacked!=NULL){
unacked = (struct tcp_seg *)((uint8*)usr_tcp_pcb_addr[i]->unacked - MEMP_SIZE);
}else{
unacked = usr_tcp_pcb_addr[i]->unacked;
}
if(usr_tcp_pcb_addr[i]->ooseq!=NULL){
ooseq = (struct tcp_seg *)((uint8*)usr_tcp_pcb_addr[i]->ooseq - MEMP_SIZE);
}else{
ooseq = usr_tcp_pcb_addr[i]->ooseq;
}
/*STATS_PRINT(("tcp_pcb ofs %2d: stat-%2d unsend-0x%8x uack-0x%8x ooseq-0x%8x \n\t", i,
usr_tcp_pcb_addr[i]->state,unsent,unacked,ooseq));*/
sprintf(strBuf,"tcp_pcb ofs %2d: stat-%2d unsend-0x%8x uack-0x%8x ooseq-0x%8x \n\t", i,
usr_tcp_pcb_addr[i]->state,unsent,unacked,ooseq);
usr_print(strBuf,strlen(strBuf));
}
}
#define USR_PRINTF_TIME 1000 // 10s
extern void stats_display(void);
extern uint32 check_delay_over(uint32 *ptick,uint32 cycle);
static void lwip_stats_print(uint32 port)
{
//static uint32 print_intv;
g_port = port;
if(1){
//if(check_delay_over(&print_intv,USR_PRINTF_TIME)){
stats_display();
usr_stats_print();
usr_seg_stats_print();
usr_tcp_pcb_stats_print();
/*printf("checked_rels_times:%3d occupy:%3d rels_lis:%3d rels_bod:%3d rels_act:%3d rels_tw:%3d \n",checked_times,occupyed_num,
rels_lis_num,rels_bod_num,rels_act_num,rels_tw_num);*/
sprintf(strBuf,"rels_times:%3d occupy:%3d rels_lis:%3d rels_bod:%3d rels_act:%3d rels_tw:%3d \n\t", checked_times,occupyed_num,
rels_lis_num,rels_bod_num,rels_act_num,rels_tw_num);
usr_print(strBuf,strlen(strBuf));
sprintf(strBuf,"reset_pbufPool:%4d used:%3d \n\t", reset_pbuf_pool_cnt,lwip_stats.memp[14]->used);
usr_print(strBuf,strlen(strBuf));
//}
}
}
uint32 protocol_print_lwip_info(uint8 *pbufin,uint32 port)
{
if(pbufin[3]==0x70)
{
lwip_stats_print(port);
return 0;
}
return 1;
}
#endif//USE_UART_CMD_PRINT_LWIP
File diff suppressed because it is too large Load Diff
+382
View File
@@ -0,0 +1,382 @@
#include <stddef.h> //for NULL
#include "../inc/rbtree.h"
#define rbtree_black 0
#define rbtree_red 1
void rbtree_init(rbtree_t *tree){
if(tree == NULL)
return;
tree->sentinel.left = &tree->sentinel;
tree->sentinel.right = &tree->sentinel;
tree->sentinel.parent = &tree->sentinel;
tree->sentinel.color = rbtree_black;
tree->root = &tree->sentinel;
return;
}
void rbtree_destory(rbtree_t *tree){
if(tree == NULL)
return;
tree->root = &tree->sentinel;
}
static void rbtree_left_rotate(rbtree_t *tree,rbtree_node_t* x){
if(x == &tree->sentinel){
return;
}
rbtree_node_t*y = x->right;
if(y == &tree->sentinel){
return;
}
x->right = y->left;
if(y->left != &tree->sentinel){
y->left->parent = x;
}
y->parent = x->parent;
if(x == tree->root){
tree->root = y;
}else if(x == x->parent->left){
x->parent->left = y;
}else{
x->parent->right = y;
}
x->parent = y;
y->left = x;
return;
}
static void rbtree_right_rotate(rbtree_t *tree,rbtree_node_t* x){
if(x == &tree->sentinel){
return;
}
rbtree_node_t*y = x->left;
if(y == &tree->sentinel){
return;
}
x->left = y->right;
if(y->right != &tree->sentinel){
y->right->parent = x;
}
y->parent = x->parent;
if(x == tree->root){
tree->root = y;
}else if(x == x->parent->left){
x->parent->left = y;
}else{
x->parent->right = y;
}
x->parent = y;
y->right = x;
return;
}
void rbtree_insert(rbtree_t *tree,rbtree_node_t* node){
if(tree == NULL || node == NULL || node == &tree->sentinel)
return;
rbtree_node_t *x,*x_tmp;
x = tree->root;
x_tmp = &tree->sentinel;
while(x != &tree->sentinel){
if(node->key<x->key){
x_tmp = x;
x = x->left;
}else{
x_tmp = x;
x = x->right;
}
}
node->parent = x_tmp;
node->left = &tree->sentinel;
node->right = &tree->sentinel;
node->color = rbtree_red;
if(tree->root == &tree->sentinel){
tree->root = node;
}else if(node->key < x_tmp->key){
x_tmp->left = node;
}else{
x_tmp->right = node;
}
while(node->parent->color == rbtree_red){
if(node->parent == node->parent->parent->left){
x = node->parent->parent->right;
if(x->color == rbtree_red){
node->parent->color = rbtree_black;
x->color = rbtree_black;
node->parent->parent->color = rbtree_red;
node = node->parent->parent;
}else{
if(node == node->parent->right){
node = node->parent;
rbtree_left_rotate(tree,node);
}
node->parent->color = rbtree_black;
node->parent->parent->color = rbtree_red;
rbtree_right_rotate(tree,node->parent->parent);
}
}else{
x = node->parent->parent->left;
if(x->color == rbtree_red){
node->parent->color = rbtree_black;
x->color = rbtree_black;
node->parent->parent->color = rbtree_red;
node = node->parent->parent;
}else{
if(node == node->parent->left){
node = node->parent;
rbtree_right_rotate(tree,node);
}
node->parent->color = rbtree_black;
node->parent->parent->color = rbtree_red;
rbtree_left_rotate(tree,node->parent->parent);
}
}
}
tree->root->color = rbtree_black;
return;
}
static rbtree_node_t* rbtree_min_node(rbtree_t *tree,rbtree_node_t* node){
while(node->left != &tree->sentinel){
node = node->left;
}
return node;
}
void rbtree_delete(rbtree_t *tree,rbtree_node_t* node){
if(tree == NULL || node == NULL || node == &tree->sentinel){
return;
}
if(node->left == NULL || node->right == NULL || node->parent == NULL){
return;
}
rbtree_node_t *temp,*subst,*w;
unsigned char color;
if(node->left == &tree->sentinel){
subst = node;
temp = node->right;
}else if(node->right == &tree->sentinel){
subst = node;
temp = node->left;
}else{
subst = rbtree_min_node(tree,node->right);
temp = subst->right;
}
if(subst == tree->root){
tree->root = temp;
temp->color = rbtree_black;
temp->parent = &tree->sentinel;
node->left = NULL;
node->right = NULL;
node->parent = NULL;
tree->sentinel.parent = &tree->sentinel;
return;
}
color = subst->color;
if(subst == subst->parent->left){
subst->parent->left = temp;
}else{
subst->parent->right = temp;
}
if(subst == node){
temp->parent = subst->parent;
}else{
if(subst->parent == node){
temp->parent = subst;
}else{
temp->parent = subst->parent;
}
subst->left = node->left;
subst->right = node->right;
subst->parent = node->parent;
subst->color = node->color;
if(node == tree->root){
tree->root = subst;
}else{
if(node == node->parent->left){
node->parent->left = subst;
}else{
node->parent->right = subst;
}
}
if(subst->left != &tree->sentinel){
subst->left->parent = subst;
}
if(subst->right != &tree->sentinel){
subst->right->parent = subst;
}
}
node->left = NULL;
node->right = NULL;
node->parent = NULL;
if(color == rbtree_red){
tree->sentinel.parent = &tree->sentinel;
return;
}
while(temp != tree->root && temp->color == rbtree_black){
if(temp == temp->parent->left){
w = temp->parent->right;
if(w->color == rbtree_red){
w->color = rbtree_black;
temp->parent->color = rbtree_red;
rbtree_left_rotate(tree,temp->parent);
w = temp->parent->right;
}
if(w->left->color == rbtree_black && w->right->color == rbtree_black){
w->color = rbtree_red;
temp = temp->parent;
}else{
if(w->left->color == rbtree_red){
w->color = rbtree_red;
w->left->color = rbtree_black;
rbtree_right_rotate(tree,w);
w = temp->parent->right;
}
w->color = temp->parent->color;
temp->parent->color = rbtree_black;
w->right->color = rbtree_black;
rbtree_left_rotate(tree,temp->parent);
temp = tree->root;
}
}else{
w = temp->parent->left;
if(w->color == rbtree_red){
w->color = rbtree_black;
temp->parent->color = rbtree_red;
rbtree_right_rotate(tree,temp->parent);
w = temp->parent->left;
}
if(w->left->color == rbtree_black && w->right->color == rbtree_black){
w->color = rbtree_red;
temp = temp->parent;
}else{
if(w->right->color == rbtree_red){
w->color = rbtree_red;
w->right->color = rbtree_black;
rbtree_left_rotate(tree,w);
w = temp->parent->left;
}
w->color = temp->parent->color;
temp->parent->color = rbtree_black;
w->left->color = rbtree_black;
rbtree_right_rotate(tree,temp->parent);
temp = tree->root;
}
}
}
temp->color = rbtree_black;
tree->sentinel.parent = &tree->sentinel;
return;
}
rbtree_node_t* rbtree_search(rbtree_t *tree,uint64_t key){
if(tree == NULL)
return NULL;
rbtree_node_t *node = tree->root;
while(node != &tree->sentinel){
if(key == node->key){
break;
}else if(key < node->key){
node = node->left;
}else{
node = node->right;
}
}
if(node == &tree->sentinel){
node = NULL;
}
return node;
}
rbtree_node_t* rbtree_min(rbtree_t *tree,rbtree_node_t* node){
if(tree == NULL || node == NULL || node==&tree->sentinel){
return NULL;
}
if(node->left == NULL || node->right == NULL || node->parent == NULL){
return NULL;
}
while(node->left != &tree->sentinel){
node = node->left;
}
if(node == &tree->sentinel){
node = NULL;
}
return node;
}
rbtree_node_t* rbtree_next(rbtree_t *tree,rbtree_node_t* node){
if(node == NULL || node == NULL || node == &tree->sentinel){
return NULL;
}
if(node->left == NULL || node->right == NULL || node->parent == NULL){
return NULL;
}
if(node->right != &tree->sentinel){
return rbtree_min(tree,node->right);
}
for(;;){
if(node == tree->root){
return NULL;
//return tree->sentinel;
}
if(node == node->parent->left){
return node->parent;
}
node = node->parent;
}
}
+355
View File
@@ -0,0 +1,355 @@
/*
@NOT_ACT_USED
*/
/******************************************************************************
版权所有 (C), 2001-2011, 上海全波通信技术有限公司
******************************************************************************
文 件 名 : rtc.c
版 本 号 : 初稿
作 者 : 郭金涛
生成日期 : 2013年7月30日
最近修改 :
功能描述 : RTC应用层相关API及初始化函数
函数列表 :
rtc_back_mem_para_init
rtc_init
修改历史 :
1.日 期 : 2013年7月30日
作 者 : 郭金涛
修改内容 : 创建文件
******************************************************************************/
#include <string.h>
#include "type.h"
#include "rtc.h"
#include "os_task.h"
#include "stm32f4xx.h"
#include "delay.h"
#include "eeprom.h"
#include "stm32f4xx_hal.h"
#if 1//_USE_RVL_
#include "i2c_drv.h"
#endif
#include "sys_config.h"
#define RTC_SET_MAGIC_FLAG 0XF236
static S_BACKUP_MEM *pbackup_mem = NULL;
RTC_HandleTypeDef hrtc;
static void rtc_config(void)
{
//
//uint32 time_out = 0;
/* Enable the PWR clock */
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
__HAL_RCC_PWR_CLK_ENABLE();
/* Allow access to RTC */
//PWR_BackupAccessCmd(ENABLE);
HAL_PWR_EnableBkUpAccess();
/* Enable the LSE OSC */
//RCC_LSEConfig(RCC_LSE_ON);
__HAL_RCC_LSE_CONFIG(RCC_LSE_ON);
//time_out = 0x10000;
//delay_ms(500);
/* Wait till LSE is ready */
while (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
{
//if (time_out-- == 0)
//{
// RCC_LSEConfig(RCC_LSE_OFF);
// return;
//}
}
/* Select the RTC Clock Source */
//RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
/* Enable the RTC Clock */
//RCC_RTCCLKCmd(ENABLE);
__HAL_RCC_RTC_ENABLE();
//RTC_WaitForSynchro();
HAL_RTC_WaitForSynchro(&hrtc);
//RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);
__HAL_RCC_BKPSRAM_CLK_ENABLE();
//PWR_BackupRegulatorCmd(ENABLE);
HAL_PWREx_EnableBkUpReg();
while(__HAL_PWR_GET_FLAG(PWR_FLAG_BRR) == RESET)
{
}
}
/*****************************************************************************
函 数 名 : rtc_init
功能描述 : 初始化RTC,当前系统使用的是32.768的LSE外部时钟
输入参数 : void
输出参数 : 无
返 回 值 :
调用函数 :
被调函数 :
修改历史 :
1.日 期 : 2013年7月24日
作 者 : 郭金涛
修改内容 : 新生成函数
*****************************************************************************/
uint32 rtc_init(void)
{
uint8 rtc_err = 0;
printf("%s..\n",__FUNCTION__);
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
__HAL_RCC_PWR_CLK_ENABLE();
//使能备份区内存功能
//PWR_BackupAccessCmd(ENABLE);
HAL_PWR_EnableBkUpAccess();
hrtc.Instance =RTC;
//if(HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR0) != RTC_SET_MAGIC_FLAG)
if(RTC_ReadBackupRegister(RTC_BKP_DR0) != RTC_SET_MAGIC_FLAG)
{//DR0,存放的为RTC初始化标识
RTC_DateTypeDef d;
RTC_TimeTypeDef t;
printf("rtc is do not set.\n",__FUNCTION__);
rtc_config();
RTC_WriteBackupRegister(RTC_BKP_DR0,RTC_SET_MAGIC_FLAG);
//HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,RTC_SET_MAGIC_FLAG);
d.RTC_Year = 14;
d.RTC_Month = 1;
d.RTC_Date = 1;
t.RTC_Hours = 0;
t.RTC_Minutes = 0;
t.RTC_Seconds = 0;
rtc_set_date_time(&d,&t);
}
else
{
/* Wait for RTC APB registers synchronisation */
if ( HAL_RTC_WaitForSynchro(&hrtc) == ERROR)
{
rtc_err = 2;
return rtc_err;
}
//RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);
__HAL_RCC_BKPSRAM_CLK_ENABLE();
}
printf("%s success.\n",__FUNCTION__);
return 0;
}
//初始化备份区的内存变量信息,此处内存可有电池供电,掉电不丢失
//预计有日志等部分变量放在备份内存区,也可以把临终遗言dump进去
uint32 rtc_back_mem_para_init(void)
{
uint32 *ptest;
uint32 reset_flag = 0;
uint16 i=0;
printf("%s ..\n",__FUNCTION__);
ptest = (uint32*)BACK_MEM_START_ADDRESS;
if(*ptest!=0x33ffda1d)
{
printf("back_mem need init.\n");
*ptest = 0x33ffda1d;
if(*ptest!=0x33ffda1d)
{
printf("%s failed 1.\n",__FUNCTION__);
pbackup_mem = NULL;
return 1;
}
reset_flag = 1;
}
if(sizeof(S_BACKUP_MEM)>BACK_MEM_SIZE-sizeof(uint32))
{
printf("%s failed 2.\n",__FUNCTION__);
pbackup_mem = NULL;
return 2;
}
//实际可用的为BACK_MEM_START_ADDRESS+4
pbackup_mem = (S_BACKUP_MEM*)(BACK_MEM_START_ADDRESS+sizeof(uint32));
printf("%s success.\n",__FUNCTION__);
if(reset_flag)
{
#if 1//_USE_RVL_
i = (c256_byteread(EERPOM_RESET_TIME_ADDR)<<8)|c256_byteread(EERPOM_RESET_TIME_ADDR+1);
if(i==0xffff)
{
i = 0;
c256_bytewrite(EERPOM_RESET_TIME_ADDR,0);
c256_bytewrite(EERPOM_RESET_TIME_ADDR+1,0);
}
#endif
pbackup_mem->reset_total_time = i;
memset(&pbackup_mem->close_time,0,sizeof(pbackup_mem->close_time));
return 0;
}
pbackup_mem->reset_total_time++;
#if 1//_USE_RVL_
c256_bytewrite(EERPOM_RESET_TIME_ADDR,pbackup_mem->reset_total_time>>8);
c256_bytewrite(EERPOM_RESET_TIME_ADDR+1,pbackup_mem->reset_total_time);
#endif
printf("total reset time:%d.\n",pbackup_mem->reset_total_time);
printf("last close sys time:%04d-%02d-%02d %02d:%02d:%02d.\n",
pbackup_mem->close_time.year,pbackup_mem->close_time.month,pbackup_mem->close_time.date,
pbackup_mem->close_time.hour,pbackup_mem->close_time.min,pbackup_mem->close_time.sec);
(void)pbackup_mem;
return 0;
}
uint32 sys_get_rst_time(void)
{
if(!pbackup_mem)
return 0;
return pbackup_mem->reset_total_time;
}
void sys_reset_rst_time(void)
{
if(pbackup_mem)
{
pbackup_mem->reset_total_time = 0;
}
#if 1//_USE_RVL_
c256_bytewrite(EERPOM_RESET_TIME_ADDR,0);
c256_bytewrite(EERPOM_RESET_TIME_ADDR+1,0);
#endif
}
void rtc_get_date_time(RTC_DateTypeDef *pd,RTC_TimeTypeDef *pt)
{
if((!pd)||(!pt))
{
return;
}
//RTC_GetTime(RTC_Format_BIN, pt);
//RTC_GetDate(RTC_Format_BIN, pd);
HAL_RTC_GetTime(&hrtc, pt, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, pd, RTC_FORMAT_BIN);
}
//必须先读取时间,后读日期,否则若刚设置过日期,则日期会读取异常
void rtc_get_date(RTC_DateTypeDef *pd)
{
if(pd)
{
//RTC_GetDate(RTC_Format_BIN, pd);
HAL_RTC_GetDate(&hrtc, pd, RTC_FORMAT_BIN);
}
}
void rtc_get_time(RTC_TimeTypeDef *pt)
{
if(pt)
{
//RTC_GetTime(RTC_Format_BIN, pt);
HAL_RTC_GetTime(&hrtc, pt, RTC_FORMAT_BIN);
}
}
void rtc_set_date_time(RTC_DateTypeDef *pd,RTC_TimeTypeDef *pt)
{
uint32 ret = 0;
if((!pd)||(!pt))
{
return;
}
pd->RTC_WeekDay = 1;
//ret = RTC_SetDate(RTC_Format_BIN, pd);
ret = HAL_RTC_SetDate(&hrtc, pd, RTC_FORMAT_BIN);
if(ERROR==ret)
{
printf("set date err!\n");
}
//ret = RTC_SetTime(RTC_Format_BIN, pt);
ret = HAL_RTC_SetTime(&hrtc, pt, RTC_FORMAT_BIN);
if(ERROR==ret)
{
printf("set time err!\n");
}
}
void rtc_set_date(RTC_DateTypeDef *pd)
{
uint32 ret = 0;
if(pd)
{
pd->RTC_WeekDay = 1;
//RTC_SetDate(RTC_Format_BIN, pd);
ret = HAL_RTC_SetDate(&hrtc, pd, RTC_FORMAT_BIN);
}
}
void rtc_set_time(RTC_TimeTypeDef *pt)
{
uint32 ret = 0;
if(pt)
{
//RTC_SetTime(RTC_Format_BIN, pt);
ret = HAL_RTC_SetTime(&hrtc, pt, RTC_FORMAT_BIN);
}
}
//更新系统关闭时间
void rtc_update_close_time(void)
{
if(pbackup_mem)
{
get_sys_time(&pbackup_mem->close_time);
}
}
void rtc_get_last_close_time(S_SYS_TIME *ptime)
{
if(pbackup_mem)
{
*ptime = pbackup_mem->close_time;
}
}
void* rtc_get_log_mem_address(void)
{
if(pbackup_mem)
{
return (void*)&pbackup_mem->os_log[0];
}
else
{
return (void*)0;
}
}
+334
View File
@@ -0,0 +1,334 @@
/*
@NOT_ACT_USED
*/
/**
******************************************************************************
* @file stm32f4x7_eth_bsp.c
* @author MCD Application Team
* @version V1.0.0
* @date 31-October-2011
* @brief STM32F4x7 Ethernet hardware configuration.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include "stm32f4x7_eth.h"
#include "stm32f4x7_eth_bsp.h"
#include "web_server.h"
void delay_ms(uint32 nms);
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO uint32_t EthInitStatus = 0;
/* Private function prototypes -----------------------------------------------*/
static void ETH_GPIO_Config(void);
static void ETH_NVIC_Config(void);
static void ETH_MACDMA_Config(void);
/* Private functions ---------------------------------------------------------*/
/**
* @brief ETH_BSP_Config
* @param None
* @retval None
*/
void ETH_BSP_Config(void)
{
/* Configure the GPIO ports for ethernet pins */
ETH_GPIO_Config();
/* Config NVIC for Ethernet */
ETH_NVIC_Config();
/* Configure the Ethernet MAC/DMA */
ETH_MACDMA_Config();
if (EthInitStatus != 0)
{
printf("%s init failed! Err Num:%d.\n",__FUNCTION__,EthInitStatus);
//delay_ms(100);
}
/* Configure the PHY to generate an interrupt on change of link status */
//Eth_Link_PHYITConfig(DP83848_PHY_ADDRESS);
/* Configure the EXTI for Ethernet link status. */
//Eth_Link_EXTIConfig(); //取消外部中断配置
}
/**
* @brief Configures the Ethernet Interface
* @param None
* @retval None
*/
static void ETH_MACDMA_Config(void)
{
ETH_InitTypeDef ETH_InitStructure;
/* Enable ETHERNET clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_ETH_MAC | RCC_AHB1Periph_ETH_MAC_Tx |
RCC_AHB1Periph_ETH_MAC_Rx, ENABLE);
/* Reset ETHERNET on AHB Bus */
ETH_DeInit();
/* Software reset */
ETH_SoftwareReset();
/* Wait for software reset */
while (ETH_GetSoftwareResetStatus() == SET);
/* ETHERNET Configuration --------------------------------------------------*/
/* Call ETH_StructInit if you don't like to configure all ETH_InitStructure parameter */
ETH_StructInit(&ETH_InitStructure);
/* Fill ETH_InitStructure parametrs */
/*------------------------ MAC -----------------------------------*/
//ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Disable;
ETH_InitStructure.ETH_Speed = ETH_Speed_100M;
ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex;
ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Disable;
ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Enable;
ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
ETH_InitStructure.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
#ifdef CHECKSUM_BY_HARDWARE
ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
#endif
/*------------------------ DMA -----------------------------------*/
/* When we use the Checksum offload feature, we need to enable the Store and Forward mode:
the store and forward guarantee that a whole frame is stored in the FIFO, so the MAC can insert/verify the checksum,
if the checksum is OK the DMA can handle the frame otherwise the frame is dropped */
ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Enable;
ETH_InitStructure.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
ETH_InitStructure.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
ETH_InitStructure.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;
ETH_InitStructure.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;
ETH_InitStructure.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Enable;
ETH_InitStructure.ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;
ETH_InitStructure.ETH_FixedBurst = ETH_FixedBurst_Enable;
ETH_InitStructure.ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat;
ETH_InitStructure.ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat;
ETH_InitStructure.ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_2_1;
/* Configure Ethernet */
EthInitStatus = ETH_Init(&ETH_InitStructure, DP83848_PHY_ADDRESS);
/* Enable the Ethernet Rx Interrupt */
ETH_DMAITConfig(ETH_DMA_IT_NIS | ETH_DMA_IT_R, ENABLE);
}
/**
* @brief Configures the different GPIO ports.
* @param None
* @retval None
*/
void ETH_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOs clocks */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB |
RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOE |
RCC_AHB1Periph_GPIOG , ENABLE);
/* Enable SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* MII/RMII Media interface selection --------------------------------------*/
#ifdef MII_MODE
#ifdef PHY_CLOCK_MCO
/* Configure MCO (PA8) */
/* Output HSE clock (25MHz) on MCO pin (PA8) to clock the PHY */
RCC_MCO1Config(RCC_MCO1Source_HSE, RCC_MCO1Div_1);
#endif /* PHY_CLOCK_MCO */
SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_MII);
#elif defined RMII_MODE
SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_RMII);
#endif
/* Ethernet pins configuration ************************************************/
/*
ETH_MDIO -------------------------> PA2
ETH_MDC --------------------------> PC1
ETH_PPS_OUT ----------------------> 未连接
ETH_MII_CRS ----------------------> PA0
ETH_MII_COL ----------------------> PA3
ETH_MII_RX_ER --------------------> PB10
ETH_MII_RXD2 ---------------------> PB0
ETH_MII_RXD3 ---------------------> PB1
ETH_MII_TX_CLK -------------------> PC3
ETH_MII_TXD2 ---------------------> PC2
ETH_MII_TXD3 ---------------------> PE2
ETH_MII_RX_CLK/ETH_RMII_REF_CLK---> PA1
ETH_MII_RX_DV/ETH_RMII_CRS_DV ----> PA7
ETH_MII_RXD0/ETH_RMII_RXD0 -------> PC4
ETH_MII_RXD1/ETH_RMII_RXD1 -------> PC5
ETH_MII_TX_EN/ETH_RMII_TX_EN -----> PG11
ETH_MII_TXD0/ETH_RMII_TXD0 -------> PG13
ETH_MII_TXD1/ETH_RMII_TXD1 -------> PG14
*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1| GPIO_Pin_2 |GPIO_Pin_3|GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_ETH);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_10;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_ETH);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource1, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource2, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource3, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_ETH);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource2, GPIO_AF_ETH);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_13 | GPIO_Pin_14;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource11, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource13, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource14, GPIO_AF_ETH);
}
/**
* @brief Configures and enable the Ethernet global interrupt.
* @param None
* @retval None
*/
void ETH_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* 2 bit for pre-emption priority, 2 bits for subpriority */
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* Enable the Ethernet global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = ETH_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 13;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief Configure the PHY to generate an interrupt on change of link status.
* @param PHYAddress: external PHY address
* @retval None
*/
uint32_t Eth_Link_PHYITConfig(uint16_t PHYAddress)
{
uint32_t tmpreg = 0;
/* Read MICR register */
tmpreg = ETH_ReadPHYRegister(PHYAddress, PHY_MICR);
/* Enable output interrupt events to signal via the INT pin */
tmpreg |= (uint32_t)PHY_MICR_INT_EN | PHY_MICR_INT_OE;
if(!(ETH_WritePHYRegister(PHYAddress, PHY_MICR, tmpreg)))
{
/* Return ERROR in case of write timeout */
return ETH_ERROR;
}
/* Read MISR register */
tmpreg = ETH_ReadPHYRegister(PHYAddress, PHY_MISR);
/* Enable Interrupt on change of link status */
tmpreg |= (uint32_t)PHY_MISR_LINK_INT_EN;
if(!(ETH_WritePHYRegister(PHYAddress, PHY_MISR, tmpreg)))
{
/* Return ERROR in case of write timeout */
return ETH_ERROR;
}
/* Return SUCCESS */
return ETH_SUCCESS;
}
/**
* @brief EXTI configuration for Ethernet link status.
* @param PHYAddress: external PHY address
* @retval None
*/
void Eth_Link_EXTIConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the INT (PB14) Clock */
RCC_AHB1PeriphClockCmd(ETH_LINK_GPIO_CLK, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* Configure INT pin as input */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = ETH_LINK_PIN;
GPIO_Init(ETH_LINK_GPIO_PORT, &GPIO_InitStructure);
/* Connect EXTI Line to INT Pin */
SYSCFG_EXTILineConfig(ETH_LINK_EXTI_PORT_SOURCE, ETH_LINK_EXTI_PIN_SOURCE);
/* Configure EXTI line */
EXTI_InitStructure.EXTI_Line = ETH_LINK_EXTI_LINE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set the EXTI interrupt to the highest priority */
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
+216
View File
@@ -0,0 +1,216 @@
/*
@NOT_ACT_USED
*/
/**
******************************************************************************
* @file stm32f4xx_it.c
* @author MCD Application Team
* @version V1.0.0
* @date 31-October-2011
* @brief Main Interrupt Service Routines.
* This file provides template for all exceptions handler and
* peripherals interrupt service routine.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_it.h"
#include "main.h"
#include "stm32f4x7_eth.h"
/* Scheduler includes */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
/* lwip includes */
#include "lwip/sys.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
extern xSemaphoreHandle s_xSemaphore;
/* Private function prototypes -----------------------------------------------*/
extern void xPortSysTickHandler(void);
/* Private functions ---------------------------------------------------------*/
/******************************************************************************/
/* Cortex-M4 Processor Exceptions Handlers */
/******************************************************************************/
/**
* @brief This function handles NMI exception.
* @param None
* @retval None
*/
void NMI_Handler(void)
{
}
/**
* @brief This function handles Hard Fault exception.
* @param None
* @retval None
*/
void HardFault_Handler(void)
{
/* Go to infinite loop when Hard Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Memory Manage exception.
* @param None
* @retval None
*/
void MemManage_Handler(void)
{
/* Go to infinite loop when Memory Manage exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Bus Fault exception.
* @param None
* @retval None
*/
void BusFault_Handler(void)
{
/* Go to infinite loop when Bus Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Usage Fault exception.
* @param None
* @retval None
*/
void UsageFault_Handler(void)
{
/* Go to infinite loop when Usage Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles SVCall exception.
* @param None
* @retval None
*/
void SVC_Handler(void)
{
}
/**
* @brief This function handles Debug Monitor exception.
* @param None
* @retval None
*/
void DebugMon_Handler(void)
{
}
/**
* @brief This function handles PendSVC exception.
* @param None
* @retval None
*/
void PendSV_Handler(void)
{
}
/**
* @brief This function handles SysTick Handler.
* @param None
* @retval None
*/
void SysTick_Handler(void)
{
xPortSysTickHandler();
}
/**
* @brief This function handles External line 10 interrupt request.
* @param None
* @retval None
*/
#if 0
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(ETH_LINK_EXTI_LINE) != RESET)
{
Eth_Link_ITHandler(DP83848_PHY_ADDRESS);
/* Clear interrupt pending bit */
EXTI_ClearITPendingBit(ETH_LINK_EXTI_LINE);
}
}
#endif
/**
* @brief This function handles ethernet DMA interrupt request.
* @param None
* @retval None
*/
void ETH_IRQHandler(void)
{
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
/* Frame received */
if ( ETH_GetDMAFlagStatus(ETH_DMA_FLAG_R) == SET)
{
/* Give the semaphore to wakeup LwIP task */
xSemaphoreGiveFromISR( s_xSemaphore, &xHigherPriorityTaskWoken );
/* Switch tasks if necessary. */
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
}
/* Clear the interrupt flags. */
/* Clear the Eth DMA Rx IT pending bits */
ETH_DMAClearITPendingBit(ETH_DMA_IT_R);
ETH_DMAClearITPendingBit(ETH_DMA_IT_NIS);
}
/******************************************************************************/
/* STM32F4xx Peripherals Interrupt Handlers */
/* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */
/* available peripheral interrupt handler's name please refer to the startup */
/* file (startup_stm32f4xx.s). */
/******************************************************************************/
/**
* @brief This function handles PPP interrupt request.
* @param None
* @retval None
*/
/*void PPP_IRQHandler(void)
{
}*/
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
File diff suppressed because it is too large Load Diff
+567
View File
@@ -0,0 +1,567 @@
/*
@NOT_ACT_USED
*/
/**
******************************************************************************
* @file system_stm32f4xx.c
* @author MCD Application Team
* @version V1.0.1
* @date 06-September-2012
* @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File.
* This file contains the system clock configuration for STM32F4xx devices,
* and is generated by the clock configuration tool
* stm32f4xx_Clock_Configuration_V1.0.0.xls
*
* 1. This file provides two functions and one global variable to be called from
* user application:
* - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
* and Divider factors, AHB/APBx prescalers and Flash settings),
* depending on the configuration made in the clock xls tool.
* This function is called at startup just after reset and
* before branch to main program. This call is made inside
* the "startup_stm32f4xx.s" file.
*
* - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
* by the user application to setup the SysTick
* timer or configure other parameters.
*
* - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
* be called whenever the core clock is changed
* during program execution.
*
* 2. After each device reset the HSI (16 MHz) is used as system clock source.
* Then SystemInit() function is called, in "startup_stm32f4xx.s" file, to
* configure the system clock before to branch to main program.
*
* 3. If the system clock source selected by user fails to startup, the SystemInit()
* function will do nothing and HSI still used as system clock source. User can
* add some code to deal with this issue inside the SetSysClock() function.
*
* 4. The default value of HSE crystal is set to 25MHz, refer to "HSE_VALUE" define
* in "stm32f4xx.h" file. When HSE is used as system clock source, directly or
* through PLL, and you are using different crystal you have to adapt the HSE
* value to your own configuration.
*
* 5. This file configures the system clock as follows:
*=============================================================================
*=============================================================================
* Supported STM32F4xx device revision | Rev A
*-----------------------------------------------------------------------------
* System Clock source | PLL (HSE)
*-----------------------------------------------------------------------------
* SYSCLK(Hz) | 168000000
*-----------------------------------------------------------------------------
* HCLK(Hz) | 168000000
*-----------------------------------------------------------------------------
* AHB Prescaler | 1
*-----------------------------------------------------------------------------
* APB1 Prescaler | 4
*-----------------------------------------------------------------------------
* APB2 Prescaler | 2
*-----------------------------------------------------------------------------
* HSE Frequency(Hz) | 25000000
*-----------------------------------------------------------------------------
* PLL_M | 25
*-----------------------------------------------------------------------------
* PLL_N | 336
*-----------------------------------------------------------------------------
* PLL_P | 2
*-----------------------------------------------------------------------------
* PLL_Q | 7
*-----------------------------------------------------------------------------
* PLLI2S_N | NA
*-----------------------------------------------------------------------------
* PLLI2S_R | NA
*-----------------------------------------------------------------------------
* I2S input clock | NA
*-----------------------------------------------------------------------------
* VDD(V) | 3.3
*-----------------------------------------------------------------------------
* Main regulator output voltage | Scale1 mode
*-----------------------------------------------------------------------------
* Flash Latency(WS) | 5
*-----------------------------------------------------------------------------
* Prefetch Buffer | OFF
*-----------------------------------------------------------------------------
* Instruction cache | ON
*-----------------------------------------------------------------------------
* Data cache | ON
*-----------------------------------------------------------------------------
* Require 48MHz for USB OTG FS, | Enabled
* SDIO and RNG clock |
*-----------------------------------------------------------------------------
*=============================================================================
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/** @addtogroup CMSIS
* @{
*/
/** @addtogroup stm32f4xx_system
* @{
*/
/** @addtogroup STM32F4xx_System_Private_Includes
* @{
*/
#include "stm32f4xx.h"
/**
* @}
*/
/** @addtogroup STM32F4xx_System_Private_TypesDefinitions
* @{
*/
/**
* @}
*/
/** @addtogroup STM32F4xx_System_Private_Defines
* @{
*/
/************************* Miscellaneous Configuration ************************/
/*!< Uncomment the following line if you need to use external SRAM mounted
on STM324xG_EVAL board as data memory */
/* #define DATA_IN_ExtSRAM */
/*!< Uncomment the following line if you need to relocate your vector Table in
Internal SRAM. */
/* #define VECT_TAB_SRAM */
/******************************************************************************/
/************************* PLL Parameters *************************************/
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
#define PLL_M 25
#define PLL_N 336
/* SYSCLK = PLL_VCO / PLL_P */
#define PLL_P 2
/* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
#define PLL_Q 7
/******************************************************************************/
/**
* @}
*/
/** @addtogroup STM32F4xx_System_Private_Macros
* @{
*/
/**
* @}
*/
/** @addtogroup STM32F4xx_System_Private_Variables
* @{
*/
static uint32_t SystemCoreClock = 168000000;
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
/**
* @}
*/
/** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
* @{
*/
static void SetSysClock(void);
#ifdef DATA_IN_ExtSRAM
static void SystemInit_ExtMemCtl(void);
#endif /* DATA_IN_ExtSRAM */
/**
* @}
*/
/** @addtogroup STM32F4xx_System_Private_Functions
* @{
*/
/**
* @brief Setup the microcontroller system
* Initialize the Embedded Flash Interface, the PLL and update the
* SystemFrequency variable.
* @param None
* @retval None
*/
void SystemInit(void)
{
/* FPU settings ------------------------------------------------------------*/
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access 0b11, */
#endif
/* Reset the RCC clock configuration to the default reset state ------------*/
/* Set HSION bit */
RCC->CR |= (uint32_t)0x00000001;
/* Reset CFGR register */
RCC->CFGR = 0x00000000;
/* Reset HSEON, CSSON and PLLON bits */
RCC->CR &= (uint32_t)0xFEF6FFFF;
/* Reset PLLCFGR register */
RCC->PLLCFGR = 0x24003010;
/* Reset HSEBYP bit */
RCC->CR &= (uint32_t)0xFFFBFFFF;
/* Disable all interrupts */
RCC->CIR = 0x00000000;
#ifdef DATA_IN_ExtSRAM
SystemInit_ExtMemCtl();
#endif /* DATA_IN_ExtSRAM */
/* Configure the System clock source, PLL Multiplier and Divider factors,
AHB/APBx prescalers and Flash settings ----------------------------------*/
SetSysClock();
/* Configure the Vector Table location add offset address ------------------*/
#ifdef VECT_TAB_SRAM
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
#else
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
#endif
}
/**
* @brief Update SystemCoreClock variable according to Clock Register Values.
* The SystemCoreClock variable contains the core clock (HCLK), it can
* be used by the user application to setup the SysTick timer or configure
* other parameters.
*
* @note Each time the core clock (HCLK) changes, this function must be called
* to update SystemCoreClock variable value. Otherwise, any configuration
* based on this variable will be incorrect.
*
* @note - The system frequency computed by this function is not the real
* frequency in the chip. It is calculated based on the predefined
* constant and the selected clock source:
*
* - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
*
* - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
*
* - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
* or HSI_VALUE(*) multiplied/divided by the PLL factors.
*
* (*) HSI_VALUE is a constant defined in stm32f4xx.h file (default value
* 16 MHz) but the real value may vary depending on the variations
* in voltage and temperature.
*
* (**) HSE_VALUE is a constant defined in stm32f4xx.h file (default value
* 25 MHz), user has to ensure that HSE_VALUE is same as the real
* frequency of the crystal used. Otherwise, this function may
* have wrong result.
*
* - The result of this function could be not correct when using fractional
* value for HSE crystal.
*
* @param None
* @retval None
*/
void SystemCoreClockUpdate(void)
{
uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
/* Get SYSCLK source -------------------------------------------------------*/
tmp = RCC->CFGR & RCC_CFGR_SWS;
switch (tmp)
{
case 0x00: /* HSI used as system clock source */
SystemCoreClock = HSI_VALUE;
break;
case 0x04: /* HSE used as system clock source */
SystemCoreClock = HSE_VALUE;
break;
case 0x08: /* PLL used as system clock source */
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
SYSCLK = PLL_VCO / PLL_P
*/
pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
if (pllsource != 0)
{
/* HSE used as PLL clock source */
pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
}
else
{
/* HSI used as PLL clock source */
pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
}
pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
SystemCoreClock = pllvco/pllp;
break;
default:
SystemCoreClock = HSI_VALUE;
break;
}
/* Compute HCLK frequency --------------------------------------------------*/
/* Get HCLK prescaler */
tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
/* HCLK frequency */
SystemCoreClock >>= tmp;
}
/**
* @brief Configures the System clock source, PLL Multiplier and Divider factors,
* AHB/APBx prescalers and Flash settings
* @Note This function should be called only once the RCC clock configuration
* is reset to the default reset state (done in SystemInit() function).
* @param None
* @retval None
*/
static void SetSysClock(void)
{
/******************************************************************************/
/* PLL (clocked by HSE) used as System clock source */
/******************************************************************************/
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
/* Enable HSE */
RCC->CR |= ((uint32_t)RCC_CR_HSEON);
/* Wait till HSE is ready and if Time out is reached exit */
do
{
HSEStatus = RCC->CR & RCC_CR_HSERDY;
StartUpCounter++;
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
if ((RCC->CR & RCC_CR_HSERDY) != RESET)
{
HSEStatus = (uint32_t)0x01;
}
else
{
HSEStatus = (uint32_t)0x00;
}
if (HSEStatus == (uint32_t)0x01)
{
/* Select regulator voltage output Scale 1 mode, System frequency up to 168 MHz */
RCC->APB1ENR |= RCC_APB1ENR_PWREN;
PWR->CR |= PWR_CR_VOS;
/* HCLK = SYSCLK / 1*/
RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
/* PCLK2 = HCLK / 2*/
RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
/* PCLK1 = HCLK / 4*/
RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;
/* Configure the main PLL */
RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
(RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24);
/* Enable the main PLL */
RCC->CR |= RCC_CR_PLLON;
/* Wait till the main PLL is ready */
while((RCC->CR & RCC_CR_PLLRDY) == 0)
{
}
/* Configure Flash prefetch, Instruction cache, Data cache and wait state */
/* Flash prefetch OFF, Instruction/Data caches ON and 5 wait states */
FLASH->ACR = FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
/* Select the main PLL as system clock source */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
RCC->CFGR |= RCC_CFGR_SW_PLL;
/* Wait till the main PLL is used as system clock source */
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
{
}
}
else
{ /* If HSE fails to start-up, the application will have wrong clock
configuration. User can add here some code to deal with this error */
}
}
/**
* @brief Setup the external memory controller. Called in startup_stm32f4xx.s
* before jump to __main
* @param None
* @retval None
*/
#ifdef DATA_IN_ExtSRAM
/**
* @brief Setup the external memory controller.
* Called in startup_stm32f4xx.s before jump to main.
* This function configures the external SRAM mounted on STM324xG_EVAL board
* This SRAM will be used as program data memory (including heap and stack).
* @param None
* @retval None
*/
void SystemInit_ExtMemCtl(void)
{
/*-- GPIOs Configuration -----------------------------------------------------*/
/*
+-------------------+--------------------+------------------+------------------+
+ SRAM pins assignment +
+-------------------+--------------------+------------------+------------------+
| PD0 <-> FSMC_D2 | PE0 <-> FSMC_NBL0 | PF0 <-> FSMC_A0 | PG0 <-> FSMC_A10 |
| PD1 <-> FSMC_D3 | PE1 <-> FSMC_NBL1 | PF1 <-> FSMC_A1 | PG1 <-> FSMC_A11 |
| PD4 <-> FSMC_NOE | PE3 <-> FSMC_A19 | PF2 <-> FSMC_A2 | PG2 <-> FSMC_A12 |
| PD5 <-> FSMC_NWE | PE4 <-> FSMC_A20 | PF3 <-> FSMC_A3 | PG3 <-> FSMC_A13 |
| PD8 <-> FSMC_D13 | PE7 <-> FSMC_D4 | PF4 <-> FSMC_A4 | PG4 <-> FSMC_A14 |
| PD9 <-> FSMC_D14 | PE8 <-> FSMC_D5 | PF5 <-> FSMC_A5 | PG5 <-> FSMC_A15 |
| PD10 <-> FSMC_D15 | PE9 <-> FSMC_D6 | PF12 <-> FSMC_A6 | PG9 <-> FSMC_NE2 |
| PD11 <-> FSMC_A16 | PE10 <-> FSMC_D7 | PF13 <-> FSMC_A7 |------------------+
| PD12 <-> FSMC_A17 | PE11 <-> FSMC_D8 | PF14 <-> FSMC_A8 |
| PD13 <-> FSMC_A18 | PE12 <-> FSMC_D9 | PF15 <-> FSMC_A9 |
| PD14 <-> FSMC_D0 | PE13 <-> FSMC_D10 |------------------+
| PD15 <-> FSMC_D1 | PE14 <-> FSMC_D11 |
| | PE15 <-> FSMC_D12 |
+-------------------+--------------------+
*/
/* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
RCC->AHB1ENR = 0x00000078;
/* Connect PDx pins to FSMC Alternate function */
GPIOD->AFR[0] = 0x00cc00cc;
GPIOD->AFR[1] = 0xcc0ccccc;
/* Configure PDx pins in Alternate function mode */
GPIOD->MODER = 0xaaaa0a0a;
/* Configure PDx pins speed to 100 MHz */
GPIOD->OSPEEDR = 0xffff0f0f;
/* Configure PDx pins Output type to push-pull */
GPIOD->OTYPER = 0x00000000;
/* No pull-up, pull-down for PDx pins */
GPIOD->PUPDR = 0x00000000;
/* Connect PEx pins to FSMC Alternate function */
GPIOE->AFR[0] = 0xc00cc0cc;
GPIOE->AFR[1] = 0xcccccccc;
/* Configure PEx pins in Alternate function mode */
GPIOE->MODER = 0xaaaa828a;
/* Configure PEx pins speed to 100 MHz */
GPIOE->OSPEEDR = 0xffffc3cf;
/* Configure PEx pins Output type to push-pull */
GPIOE->OTYPER = 0x00000000;
/* No pull-up, pull-down for PEx pins */
GPIOE->PUPDR = 0x00000000;
/* Connect PFx pins to FSMC Alternate function */
GPIOF->AFR[0] = 0x00cccccc;
GPIOF->AFR[1] = 0xcccc0000;
/* Configure PFx pins in Alternate function mode */
GPIOF->MODER = 0xaa000aaa;
/* Configure PFx pins speed to 100 MHz */
GPIOF->OSPEEDR = 0xff000fff;
/* Configure PFx pins Output type to push-pull */
GPIOF->OTYPER = 0x00000000;
/* No pull-up, pull-down for PFx pins */
GPIOF->PUPDR = 0x00000000;
/* Connect PGx pins to FSMC Alternate function */
GPIOG->AFR[0] = 0x00cccccc;
GPIOG->AFR[1] = 0x000000c0;
/* Configure PGx pins in Alternate function mode */
GPIOG->MODER = 0x00080aaa;
/* Configure PGx pins speed to 100 MHz */
GPIOG->OSPEEDR = 0x000c0fff;
/* Configure PGx pins Output type to push-pull */
GPIOG->OTYPER = 0x00000000;
/* No pull-up, pull-down for PGx pins */
GPIOG->PUPDR = 0x00000000;
/*-- FSMC Configuration ------------------------------------------------------*/
/* Enable the FSMC interface clock */
RCC->AHB3ENR = 0x00000001;
/* Configure and enable Bank1_SRAM2 */
FSMC_Bank1->BTCR[2] = 0x00001015;
FSMC_Bank1->BTCR[3] = 0x00010603;
FSMC_Bank1E->BWTR[2] = 0x0fffffff;
/*
Bank1_SRAM2 is configured as follow:
p.FSMC_AddressSetupTime = 3;
p.FSMC_AddressHoldTime = 0;
p.FSMC_DataSetupTime = 6;
p.FSMC_BusTurnAroundDuration = 1;
p.FSMC_CLKDivision = 0;
p.FSMC_DataLatency = 0;
p.FSMC_AccessMode = FSMC_AccessMode_A;
FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2;
FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_PSRAM;
FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
*/
}
#endif /* DATA_IN_ExtSRAM */
uint32_t get_sys_clk(void)
{
return SystemCoreClock;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/