In this paper, several classes of three-weight codes and two-weight codes for the homogeneous metric over the chain ring R = F p + u F p + ⋯ + u k - 1 F p , with u k = 0 , are constructed that generalize the construction of Shi et al. (IEEE Commun. Lett. 20(12):2346–2349, 2016), which is the special case of p = k = 2. These codes are defined as trace codes. In some cases of their defining sets, they are abelian. Their homogeneous weight distributions are computed by using exponential sums. In particular, in the two-weight case, we give some conditions of optimality of their Gray images by using the Griesmer bound. Their dual homogeneous distance is also given. The codewords of these codes are shown to be minimal for inclusion of supports, a fact favorable to an application to secret sharing schemes. [ABSTRACT FROM AUTHOR]