By Jon Herskovitz and Lisa Maria Garza

DALLAS (Reuters) - Thousands of police officers joined by ordinary citizens attended funerals on Wednesday for three of the policemen shot dead in a racially motivated ambush attack last week that intensified America's long-running debate on race and justice.

At the Dallas megachurch called The Potter's House, officers by the thousands crowded into the funeral for Dallas Area Rapid Transit officer Brent Thompson, who had married a fellow officer just two weeks before last Thursday's attack.

"I know many of you have dealt with these things quite often," pastor Rick Lamb of Northside Baptist Church told the crowd. "Today is about Brent and trying to bring some closure to this family as they finish the job that they didn't want to start, but had to start last week."

As Thompson's funeral procession began the roughly 55-mile (89 km) journey back to his home of Corsicana, several police helicopters flew over the church as bagpipers played "Amazing Grace" and hundred of officers from different departments saluted his silver-colored casket.

Funerals also were held for Sergeant Michael Smith, 55, and Officer Lorne Ahrens, 48, of the Dallas Police Department.

Dallas police officer Eddie Coffey described Ahrens as a calming presence and a down-to-earth man.

"He was the guy you always wanted to show up as your backup," Coffey said. "He wanted to make sure everyone, from top down, made it home safely."

The funerals came a day after President Barack Obama praised the slain officers' heroism, condemned the attack as an "act not just of demented violence but of racial hatred" and made an impassioned plea for national unity.

The five officers were killed by a former U.S. Army Reserve soldier who told police that he was angry about police killings of two black men in Louisiana and Minnesota earlier that week and wanted to "kill white people," especially police.

Funerals for the other two slain officers, Michael Krol, 40, and Patrick Zamarripa, 32, are expected later in the week.

The shootings in Louisiana and Minnesota were the latest in a series of high-profile police killings of black men in various U.S. cities that have brought intense scrutiny of police use of force, particularly against black suspects.

The officers slain in Dallas last week were patrolling a demonstration decrying the killings by police of Alton Sterling, 37, in Baton Rouge, Louisiana, and Philando Castile, 32, outside St. Paul, Minnesota.

BATON ROUGE POLICE SUED

Groups including the American Civil Liberties Union of Louisiana sued the Baton Rouge Police Department in federal court on Wednesday, alleging that police violated protesters' rights to freedom of speech and assembly guaranteed by the U.S. Constitution's First Amendment.

Protesters have been staging demonstrations in the city since Sterling's death last week. The lawsuit also accused police of using excessive force and wrongful arrests to disperse protesters.

Police responded to peaceful rallies with "a military-grade assault on protesters' bodies and rights," the lawsuit stated. "Law enforcement officers have escalated peaceful situations," it added.

Baton Rouge police spokesman Sergeant Don Coppola said the department would not comment on pending litigation.

Sterling's 15-year-old son, Cameron Sterling, urged people to refrain from violence as they demand reforms in the U.S. criminal justice system.

"I feel that people in general, no matter what the race is, should come together as one united family," Cameron Sterling told reporters in the parking lot of the Triple S Food Mart, where his father was killed. "I want everyone to protest the right way. Protest in peace. ... No violence, whatsoever."

Justin Bamberg, a lawyer for Sterling's son, said he hoped the officer who shot Sterling would be criminally charged following the federal investigation into the incident.

"We want justice. We want an indictment," Bamberg said.

A lawyer for the officer has denied race was a factor in Sterling's shooting.

(Additional reporting by Letitia Stein in Baton Rouge, Louisiana, Colleen Jenkins in Winston-Salem, North Carolina, and Justin Madden in Chicago; Writing by Scott Malone; Editing by Will Dunham)