Let p be an odd prime, \(\ell \ne p\) a prime and m a positive integer such that p is a primitive root modulo \(N:=2\ell ^m\) . Let \(q = p^{\phi (N)}\) , where \(\phi\) is the Euler totient function, and denote by \(\mathbb {F}_q\) the finite field of order q. For \(\alpha \in \mathbb {F}_p\) and \(\beta \in \mathbb {F}_q\) , let \(\begin{aligned} \{x\in \mathbb {F}_q^*:\ \textrm{Tr}(x^{\frac{q-1}{N}} + \beta x) = \alpha \}=\{d_1,\dots ,d_n\}, \end{aligned}\) and define a p-ary code \(\begin{aligned} \mathcal {C}_{\alpha ,\beta } = \{(\textrm{Tr}(d_1 x), \ldots , \textrm{Tr}(d_n x))\in \mathbb {F}_p^n : x \in \mathbb {F}_q\}, \end{aligned}\) where \(\textrm{Tr}\) is the absolute trace function from \(\mathbb {F}_q\) to \(\mathbb {F}_p\) . In this paper, we investigate the weight distribution of the code \(\mathcal {C}_{\alpha ,\beta }\) depending on the choice of the parameters \(\alpha\) and \(\beta\) . More precisely, we establish that \(\mathcal {C}_{\alpha ,0}\) is a two-weight code, and determine its weight distribution. For \(\beta \ne 0\) , we determine all possible weights of codewords in \(\mathcal {C}_{\alpha ,\beta }\) , showing that it has at most \(p+1\) distinct nonzero weights. Furthermore, we prove that the dual code \(\mathcal {C}_{0,0}^{\perp }\) is optimal with respect to the sphere packing bound. Our results extend previous findings for \(p=2\) and \(p=3\) to all odd primes p.